-
Notifications
You must be signed in to change notification settings - Fork 2
/
manage-server.py
executable file
·201 lines (165 loc) · 5.33 KB
/
manage-server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python
"""
Simple script to manage virtual servers.
This needs the same setup as mumble-authenticator.py, see README.md.
Run, e.g.: python manage-server.py 127.0.0.1 6502
"""
import os
import sys
import Ice
# load slice
ice_slice = os.getcwd() + '/Murmur.ice'
if len(sys.argv) > 3:
ice_slice = sys.argv[3]
try:
# noinspection PyArgumentList
Ice.loadSlice('', ['-I' + Ice.getSliceDir(), ice_slice])
except RuntimeError as e:
print(format(e))
sys.exit(0)
# noinspection PyUnresolvedReferences
import Murmur
class ManageServer:
def __init__(self, host: str, port: str):
self.host = host
self.port = port
self.ice = None
self.meta: Murmur.Meta = None
def connect(self) -> bool:
self.ice = Ice.initialize()
try:
proxy = self.ice.stringToProxy('Meta -e 1.0:tcp -h %s -p %s' % (self.host, self.port))
except Exception as ex:
write(str(ex))
return False
self.meta = Murmur.MetaPrx.checkedCast(proxy)
write("Connected to {0}:{1}".format(ice_host, ice_port))
return True
def destroy(self):
self.ice.destroy()
def list(self):
default_port = int(self.meta.getDefaultConf().get('port'))
default_register_name = self.meta.getDefaultConf().get('registerName')
for server in self.meta.getAllServers():
write('id: {0}, running: {1}, port: {2}, registerName: {3}'.format(
server.id(),
server.isRunning(),
server.getConf('port') if server.getConf('port') else default_port + server.id() - 1,
server.getConf('registerName') if server.getConf('registerName') else default_register_name,
))
def default_conf(self):
items = self.meta.getDefaultConf().items()
self.__print_items(items)
def conf(self):
server_id = input('Server id: ')
server = self.meta.getServer(int(server_id))
if server is None:
write('Invalid id')
return
items = server.getAllConf().items()
self.__print_items(items)
def new(self):
new_server = self.meta.newServer()
write('New id: {}'.format(new_server.id()))
def pw(self):
server_id = input('Server id: ')
server = self.meta.getServer(int(server_id))
if server is None:
write('Invalid id')
return
password = input('Password: ')
server.setSuperuserPassword(password)
write('Done')
def conf_set(self):
server_id = input('Server id: ')
server = self.meta.getServer(int(server_id))
if server is None:
write('Invalid id')
return
key = input('Key (port, registerName, welcometext, ...): ')
value = input('Value: ')
server.setConf(key, value)
write('Done')
def start(self):
server_id = input('Server id: ')
server = self.meta.getServer(int(server_id))
if server is None:
write('Invalid id')
return
server.start()
write('Done')
def stop(self):
server_id = input('Server id: ')
server = self.meta.getServer(int(server_id))
if server is None:
write('Invalid id')
return
server.stop()
write('Done')
def delete(self):
server_id = input('Server id: ')
server = self.meta.getServer(int(server_id))
if server is None:
write('Invalid id')
return
sys.stdout.write('Are you sure? [yes/No] ')
if input().lower() == 'yes':
server.delete()
write('Server deleted')
else:
write('No')
@staticmethod
def __print_items(items):
if len(items) == 0:
write('(none)')
else:
for key, value in items:
write('{0}: {1}'.format(key, value))
def write(text: str):
print(text)
# Run
if len(sys.argv) > 2:
ice_host = sys.argv[1]
ice_port = sys.argv[2]
else:
write('Usage: manage-server.py host port [ice file]')
sys.exit(0)
write('')
write(' Virtual Mumble Server Manager ')
write('')
write(' !! Attention, be careful! ')
write(' !! This script can delete ALL servers, ')
write(' !! including the default server with ID 1. ')
write('')
manage_server = ManageServer(ice_host, ice_port)
run = manage_server.connect()
while run:
try:
write('')
command = input('command (list, conf, default-conf, new, pw, conf-set, start, stop, delete, quit): ')
if command == 'list':
manage_server.list()
elif command == 'conf':
manage_server.conf()
elif command == 'default-conf':
manage_server.default_conf()
elif command == 'new':
manage_server.new()
elif command == 'pw':
manage_server.pw()
elif command == 'conf-set':
manage_server.conf_set()
elif command == 'start':
manage_server.start()
elif command == 'stop':
manage_server.stop()
elif command == 'delete':
manage_server.delete()
elif command == 'quit':
run = False
else:
write('Invalid command.')
except KeyboardInterrupt:
run = False
write('')
manage_server.destroy()