forked from spring/uberserver
/
DataHandler.py
483 lines (431 loc) · 15.7 KB
/
DataHandler.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import thread, time, sys, os, socket
import traceback
from protocol.Channel import Channel
from protocol.Protocol import Protocol
from SQLUsers import UsersHandler, ChannelsHandler
from CryptoHandler import UNICODE_ENCODING
import ip2country
import datetime
separator = '-'*60
class DataHandler:
def __init__(self):
self.local_ip = None
self.online_ip = None
self.session_id = 0
self.dispatcher = None
self.console_buffer = []
self.port = 8200
self.xmlport = 8300
self.xmlhost = '127.0.0.1'
self.natport = self.port + 1
self.latestspringversion = '*'
self.log = False
self.logfile = None
self.logfilename = 'server.log'
self.agreementfile = 'agreement.rtf' #FIXME: remove/deprecated
self.agreementfileplain = 'agreement.txt'
self.agreement = [] #FIXME: remove
self.agreementplain = []
self.server = 'TASServer'
self.server_version = 0.36
self.sighup = False
self.crypto_key_dir = "server-rsa-keys/"
self.force_secure_client_auths = False ## if true, LOGIN and REGISTER must be encrypted
self.force_secure_client_comms = False ## if true, ALL commands must be encrypted
self.use_message_authent_codes = False ## if true, all messages must include (H)MACs
self.chanserv = None
self.userdb = None
self.channeldb = None
self.engine = None
self.protocol = None
self.updatefile = None
self.trusted_proxyfile = None
self.max_threads = 25
self.sqlurl = 'sqlite:///server.db'
self.nextbattle = 0
self.SayHooks = __import__('SayHooks')
self.censor = True
self.motd = None
self.running = True
self.redirect = None
self.trusted_proxies = []
self.start_time = time.time()
self.channels = {}
self.usernames = {}
self.clients = {}
self.db_ids = {}
self.battles = {}
thread.start_new_thread(self.event_loop, ())
def showhelp(self):
print('Usage: server.py [OPTIONS]...')
print('Starts uberserver.')
print()
print('Options:')
print(' -h, --help')
print(' { Displays this screen then exits }')
print(' -p, --port number')
print(' { Server will host on this port (default is 8200) }')
print(' -n, --natport number')
print(' { Server will use this port for NAT transversal (default is 8201) }')
print(' -g, --loadargs filename')
print(' { Reads additional command-line arguments from file }')
print(' -o, --output /path/to/file.log')
print(' { Writes console output to file (for logging) }')
print(' -u, --sighup')
print(' { Reload the server on SIGHUP (if SIGHUP is supported by OS) }')
print(' -v, --latestspringversion version')
print(' { Sets latest Spring version to this string. Defaults to "*" }')
print(' -m, --maxthreads number')
print(' { Uses the specified number of threads for handling clients }')
print(' -s, --sqlurl SQLURL')
print(' { Uses SQL database at the specified sqlurl for user, channel, and ban storage. }')
print(' -c, --no-censor')
print(' { Disables censoring of #main, #newbies, and usernames (default is to censor) }')
print(' --proxies /path/to/proxies.txt')
print(' { Path to proxies.txt, for trusting proxies to pass real IP through local IP }')
print(' -a --agreement /path/to/agreement.txt')
print(' { sets the pat to the agreement file which is sent to a client registering at the server }')
print(' -r --redirect "hostname/ip port"')
print(' { redirects connecting clients to the given ip and port')
print('SQLURL Examples:')
#print(' "sqlite:///:memory:" or "sqlite:///"')
#print(' { both make a temporary database in memory }')
print(' "sqlite:////absolute/path/to/database.txt"')
print(' { uses a database in the file specified }')
print(' "sqlite:///relative/path/to/database.txt"')
print(' { note sqlite is slower than a real SQL server }')
print(' "mysql://user:password@server:port/database?charset=utf8&use_unicode=0"')
print(' { requires the MySQLdb module }')
print(' "oracle://user:password@server:port/database"')
print(' { requires the cx_Oracle module }')
print(' "postgres://user:password@server:port/database"')
print(' { requires the psycopg2 module }')
print(' "mssql://user:password@server:port/database"')
print(' { requires pyodbc (recommended) or adodbapi or pymssql }')
print(' "firebird://user:password@server:port/database"')
print(' { requires the kinterbasdb module }')
print()
print('Usage example (this is what the test server uses at the moment):')
print(' server.py -p 8300 -n 8301')
print()
exit()
def parseArgv(self, argv):
'parses command-line options'
args = {'ignoreme':[]}
mainarg = 'ignoreme'
tempargv = list(argv)
while tempargv:
arg = tempargv.pop(0)
if arg.startswith('-'):
mainarg = arg.lstrip('-').lower()
if mainarg in ['g', 'loadargs']:
try:
name = tempargv[0]
if name.startswith('-'): raise Exception
f = file(name, 'r')
lines = f.read().split('\n')
f.close()
tempargv += ' '.join(lines).split(' ')
except:
pass
args[mainarg] = []
else:
args[mainarg].append(arg)
del args['ignoreme']
for arg in args:
argp = args[arg]
if arg in ['r', 'redirect']:
self.redirect = argp[0]
if arg in ['h', 'help']:
self.showhelp()
if arg in ['p', 'port']:
try: self.port = int(argp[0])
except: print('Invalid port specification')
elif arg in ['n', 'natport']:
try: self.natport = int(argp[0])
except: print('Invalid NAT port specification')
elif arg in ['o', 'output']:
try: self.logfilename = argp[0]
except: print('Error specifying log location')
self.rotatelogfile()
elif arg in ['u', 'sighup']:
self.sighup = True
elif arg in ['v', 'latestspringversion']:
try: self.latestspringversion = argp[0] # ' '.join(argp) # shouldn't have spaces
except: print('Error specifying latest spring version')
elif arg in ['m', 'maxthreads']:
try: self.max_threads = int(argp[0])
except: print('Error specifing max threads')
elif arg in ['s', 'sqlurl']:
try:
self.sqlurl = argp[0]
except:
print('Error specifying SQL URL')
elif arg in ['c', 'no-censor']:
self.censor = False
elif arg in ['a', 'agreement']:
try:
self.argeementfile = argp[0]
except:
print('Error reading agreement file')
elif arg in ['p', 'agreementplain']:
try:
self.argeementfileplain = argp[0]
except:
print('Error reading agreement file')
elif arg == 'proxies':
try:
self.trusted_proxyfile = argp[0]
open(self.trusted_proxyfile, 'r').close()
except:
print('Error opening trusted proxy file.')
self.trusted_proxyfile = None
elif arg == 'sec_auths':
try: self.force_secure_client_auths = (int(argp[0]) != 0)
except: pass
elif arg == 'sec_comms':
try: self.force_secure_client_comms = (int(argp[0]) != 0)
except: pass
elif arg == 'msg_hmacs':
try: self.use_message_authent_codes = (int(argp[0]) != 0)
except: pass
sqlalchemy = __import__('sqlalchemy')
if self.sqlurl.startswith('sqlite'):
print('Multiple threads are not supported with sqlite, forcing a single thread')
print('Please note the server performance will not be optimal')
print('You might want to install a real database server or use LAN mode')
print()
self.max_threads = 1
self.engine = sqlalchemy.create_engine(self.sqlurl)
def _fk_pragma_on_connect(dbapi_con, con_record):
dbapi_con.execute('PRAGMA journal_mode = MEMORY')
dbapi_con.execute('PRAGMA synchronous = OFF')
## FIXME: "ImportError: cannot import name event"
from sqlalchemy import event
event.listen(self.engine, 'connect', _fk_pragma_on_connect)
else:
self.engine = sqlalchemy.create_engine(self.sqlurl, pool_size=self.max_threads * 2, pool_recycle=300)
self.userdb = UsersHandler(self, self.engine)
self.channeldb = ChannelsHandler(self, self.engine)
channels = self.channeldb.load_channels()
for name in channels:
channel = channels[name]
owner = None
admins = []
client = self.userdb.clientFromUsername(channel['owner'])
if client and client.id: owner = client.id
for user in channel['admins']:
client = userdb.clientFromUsername(user)
if client and client.id:
admins.append(client.id)
self.channels[name] = Channel(self, name, chanserv=bool(owner), id = channel['id'], owner=owner, admins=admins, key=channel['key'], antispam=channel['antispam'], topic={'user':'ChanServ', 'text':channel['topic'], 'time':int(time.time())})
if self.chanserv:
for name in channels:
self.chanserv.client.HandleProtocolCommand('JOIN %s' % name)
if not self.log:
self.rotatelogfile()
self.parseFiles()
self.protocol = Protocol(self)
def parseFiles(self):
if os.path.isfile('motd.txt'):
motd = []
f = open('motd.txt', 'r')
data = f.read()
f.close()
if data:
for line in data.split('\n'):
motd.append(line.strip())
self.motd = motd
if self.trusted_proxyfile:
self.trusted_proxies = set([])
f = open(self.trusted_proxyfile, 'r')
data = f.read()
f.close()
if data:
for line in data.split('\n'):
proxy = line.strip()
if not proxy.replace('.', '', 3).isdigit():
proxy = socket.gethostbyname(proxy)
if proxy:
self.trusted_proxies.add(proxy)
self.agreement = []
ins = open(self.agreementfile, "r" )
for line in ins:
self.agreement.append(line.rstrip('\r\n'))
self.agreementplain = []
ins = open(self.agreementfileplain, "r" )
for line in ins:
self.agreementplain.append(line.rstrip('\r\n'))
ins.close()
def getUserDB(self):
return self.userdb
def clientFromID(self, db_id):
if db_id in self.db_ids: return self.db_ids[db_id]
def clientFromUsername(self, username):
if username in self.usernames: return self.usernames[username]
def event_loop(self):
start = time.time()
lastsave = lastmute = lastidle = lastload = start
load = 0
try:
os.getloadavg()
hasload = True
except:
hasload = False
while self.running:
try:
now = time.time()
if now - lastmute >= 1:
lastmute = now
self.mute_timeout_step()
elif now - lastidle > 10:
lastidle = now
self.idle_timeout_step()
else:
self.console_print_step()
except:
self.error(traceback.format_exc())
time.sleep(max(0.1, 1 - (time.time() - start)))
def mute_timeout_step(self):
try:
now = time.time()
channels = dict(self.channels)
for chan in channels:
channel = channels[chan]
mutelist = dict(channel.mutelist)
for db_id in mutelist:
expiretime = mutelist[db_id]['expires']
if 0 < expiretime and expiretime < now:
del channel.mutelist[db_id]
client = self.protocol.clientFromID(db_id)
if client:
channel.channelMessage('<%s> has been unmuted (mute expired).' % client.username)
except:
self.error(traceback.format_exc())
def idle_timeout_step(self):
now = time.time()
for client in self.clients.values():
if client.static: continue
if not client.logged_in and client.last_login < now - 60:
client.Send("SERVERMSG timed out, no login within 60 seconds!")
client.Remove("Connection timed out, didn't login")
elif client.lastdata < now - 60:
client.Send("SERVERMSG timed out, no data or PING received for >60 seconds, closing connection")
client.Remove("Connection timed out")
def console_print_step(self):
try:
while self.console_buffer:
line = self.console_buffer.pop(0).encode(UNICODE_ENCODING)
print line
if self.log:
self.logfile.write(line+'\n')
if self.logfile:
self.logfile.flush()
except:
print(separator)
print(traceback.format_exc())
print(separator)
def error(self, error):
error = '%s\n%s\n%s'%(separator,error,separator)
self.console_write(error)
for user in dict(self.usernames):
try:
if self.usernames[user].debug:
for line in error.split('\n'):
if line:
self.usernames[user].Send('SERVERMSG %s'%line)
except KeyError: pass # the user was removed
def console_write(self, lines=''):
if type(lines) in(str, unicode):
lines = lines.split('\n')
elif not type(lines) in (list, tuple, set):
try: lines = [lines.__repr__()]
except: lines = ['Failed to print lines of type %s'%type(lines)]
strtime = datetime.datetime.fromtimestamp(int(time.time())).isoformat() + ' '
for line in lines:
self.console_buffer += [ strtime + line ]
# the sourceClient is only sent for SAY*, and RING commands
def multicast(self, clients, msg, ignore=(), sourceClient=None):
if type(ignore) in (str, unicode): ignore = [ignore]
static = []
for client in clients:
if client and not client.username in ignore and \
(sourceClient == None or not sourceClient.db_id in client.ignored):
if client.static: static.append(client)
else: client.Send(msg)
# this is so static clients don't respond before other people even receive the message
for client in static:
client.Send(msg)
# the sourceClient is only sent for SAY*, and RING commands
def broadcast(self, msg, chan=None, ignore=(), sourceClient=None):
if type(ignore) in (str, unicode): ignore = [ignore]
try:
if chan in self.channels:
channel = self.channels[chan]
if len(channel.users) > 0:
clients = [self.clientFromUsername(user) for user in list(channel.users)]
self.multicast(clients, msg, ignore, sourceClient)
else:
clients = [self.clientFromUsername(user) for user in list(self.usernames)]
self.multicast(clients, msg, ignore, sourceClient)
except: self.error(traceback.format_exc())
# the sourceClient is only sent for SAY*, and RING commands
def broadcast_battle(self, msg, battle_id, ignore=[], sourceClient=None):
if type(ignore) in (str, unicode): ignore = [ignore]
if battle_id in self.battles:
battle = self.battles[battle_id]
clients = [self.clientFromUsername(user) for user in list(battle.users)]
self.multicast(clients, msg, ignore, sourceClient)
def admin_broadcast(self, msg):
for user in dict(self.usernames):
client = self.usernames[user]
if 'admin' in client.accesslevels:
client.Send('SERVERMSG Admin broadcast: %s'%msg)
def _rebind_slow(self):
try:
self.dispatcher.rebind()
for channel in dict(self.channels): # hack, but I guess reloading is all a hack :P
chan = self.channels[channel].copy()
del chan['name'] # 'cause we're passing it ourselves
self.channels[channel] = sys.modules['protocol.Protocol'].Channel(self, channel, **chan)
self.protocol = Protocol(self)
except:
self.error(traceback.format_exc())
self.admin_broadcast('Done reloading.')
self.console_write('Done reloading.')
def reload(self):
self.admin_broadcast('Reloading...')
self.console_write('Reloading...')
self.rotatelogfile()
self.parseFiles()
reload(sys.modules['SayHooks'])
reload(sys.modules['ChanServ'])
reload(sys.modules['BaseClient'])
reload(sys.modules['SQLUsers'])
reload(sys.modules['Client'])
reload(sys.modules['CryptoHandler'])
reload(sys.modules['protocol.AutoDict'])
reload(sys.modules['protocol.Channel'])
reload(sys.modules['protocol.Battle'])
reload(sys.modules['protocol.Protocol'])
reload(sys.modules['protocol'])
self.SayHooks = __import__('SayHooks')
ip2country.reloaddb()
thread.start_new_thread(self._rebind_slow, ()) # why should reloading block the thread? :)
def rotatelogfile(self):
self.log = False
try:
if self.logfile:
self.console_print_step() # flush logfile
self.logfile.close()
oldfilename = self.logfilename + ".old"
if os.path.exists(oldfilename):
os.remove(oldfilename)
os.rename(self.logfilename, oldfilename)
except OSError as e:
print("Error rotaing logfile %s"% (e.strerror))
except IOError as e:
print("Error rotaing logfile %s"% (e.strerror))
self.logfile = file(self.logfilename, 'w')
print('Logging enabled at: %s' % self.logfilename)
self.log = True