forked from spring/uberserver
/
DataHandler.py
439 lines (388 loc) · 16.5 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
import time, sys, os, socket
import traceback
import SQLUsers
import ChanServ
import ip2country
import datetime
from protocol import Protocol, Channel
import logging
from logging.handlers import TimedRotatingFileHandler
from twisted.internet import ssl
separator = '-' * 60
try:
from urllib2 import urlopen
except:
# The urllib2 module has been split across several modules in Python 3.0
from urllib.request import urlopen
class DataHandler:
def __init__(self):
self.logfilename = "server.log"
self.initlogger(self.logfilename)
self.local_ip = None
self.online_ip = None
self.session_id = 0
self.dispatcher = None
self.console_buffer = []
self.port = 8200
self.natport = self.port + 1
self.latestspringversion = '*'
self.agreementfile = 'agreement.txt'
self.agreement = []
self.server = 'TASServer'
self.server_version = 0.36
self.sighup = False
self.chanserv = None
self.userdb = None
self.channeldb = None
self.engine = 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 = {}
self.detectIp()
self.cert = None
def initlogger(self, filename):
# logging
server_logfile = os.path.join(os.path.dirname(__file__), filename)
self.logger = logging.getLogger()
self.logger.setLevel(logging.DEBUG)
fh = TimedRotatingFileHandler(server_logfile, when="midnight", backupCount=6)
formatter = logging.Formatter(fmt='%(asctime)s %(levelname)-5s %(module)s.%(funcName)s:%(lineno)d %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
fh.setFormatter(formatter)
fh.setLevel(logging.DEBUG)
self.logger.addHandler(fh)
def init(self):
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')
print('')
self.max_threads = 1
self.engine = sqlalchemy.create_engine(self.sqlurl, echo=False)
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 = SQLUsers.UsersHandler(self, self.engine)
self.channeldb = SQLUsers.ChannelsHandler(self, self.engine)
channels = self.channeldb.load_channels()
for name in channels:
channel = channels[name]
owner = None
admins = set()
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)
assert (name not in self.channels)
newchan = Channel.Channel(self, name)
newchan.chanserv = bool(owner)
newchan.id = channel['id']
newchan.owner = owner
newchan.admins = admins
if channel['key'] in ('', None, '*'):
newchan.key = None
else:
newchan.key = channel['key']
newchan.antispam = channel['antispam']
newchan.topic = {'user': 'ChanServ', 'text': channel['topic'], 'time': int(time.time())}
newchan.store_history = channel['store_history']
self.channels[name] = newchan
self.parseFiles()
self.protocol = Protocol.Protocol(self)
self.chanserv = ChanServ.ChanServClient(self, (self.online_ip, 0), self.session_id)
for name in channels:
self.chanserv.HandleProtocolCommand("JOIN %s" % (name))
def shutdown(self):
self.running = False
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(' -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']:
name = tempargv[0]
f = open(name, 'r')
lines = f.read().split('\n')
f.close()
tempargv += ' '.join(lines).split(' ')
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')
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 ['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 == 'proxies':
try:
self.trusted_proxyfile = argp[0]
open(self.trusted_proxyfile, 'r').close()
except:
print('Error opening trusted proxy file.')
self.trusted_proxyfile = None
def loadCertificates(self):
certfile = "server.pem"
if not os.path.isfile(certfile):
import certificate
certificate.create_self_signed_cert(certfile)
os.chmod(certfile, 0o600)
with open(certfile, 'r') as data:
self.cert = ssl.PrivateCertificate.loadPEM(data.read()).options()
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'))
ins.close()
self.loadCertificates()
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 clientFromSession(self, session_id):
if session_id in self.clients: return self.clients[session_id]
def event_loop(self):
lastmute = lastidle = self.start_time
while self.running:
now = time.time()
try:
if now - lastmute >= 1: # FIXME: reenable after twisted switch
lastmute = now
self.mute_timeout_step(now)
except:
logging.error(traceback.format_exc())
time.sleep(max(0.1, 1 - (now - self.start_time)))
def mute_timeout_step(self, now):
try:
channels = self.channels
for chan in channels:
channel = channels[chan]
mutelist = 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.clientFromID(db_id)
if client:
channel.channelMessage('<%s> has been unmuted (mute expired).' % client.username)
except:
logging.error(traceback.format_exc())
# the sourceClient is only sent for SAY*, and RING commands
def multicast(self, clients, msg, ignore=(), sourceClient=None):
if type(ignore) == str: 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):
try:
if chan in self.channels:
channel = self.channels[chan]
if len(channel.users) > 0:
clients = [self.clientFromSession(user) for user in channel.users]
self.multicast(clients, msg, ignore, sourceClient)
else:
clients = [self.clientFromUsername(user) for user in self.usernames]
self.multicast(clients, msg, ignore, sourceClient)
except:
logging.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 battle_id in self.battles:
battle = self.battles[battle_id]
clients = [self.clientFromSession(user) for user in battle.users]
self.multicast(clients, msg, ignore, sourceClient)
def admin_broadcast(self, msg):
for user in self.usernames:
client = self.usernames[user]
if user == "ChanServ": # needed to allow "reload"
continue
if 'admin' in client.accesslevels:
client.Send('SERVERMSG Admin broadcast: %s' % msg)
def reload(self):
self.parseFiles()
ip2country.reloaddb()
def get_ip_address(self):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("springrts.com", 80))
res = s.getsockname()[0]
s.close()
return res
except Exception as e:
self.logger.debug(e)
pass
try:
return socket.gethostbyname(socket.gethostname())
except:
pass
return '127.0.0.1'
def detectIp(self):
logging.info('\nDetecting local IP:')
local_addr = self.get_ip_address()
logging.info(local_addr)
logging.info('Detecting online IP:')
try:
timeout = socket.getdefaulttimeout()
socket.setdefaulttimeout(5)
web_addr = urlopen('http://springrts.com/lobby/getip.php').read().decode("utf-8")
socket.setdefaulttimeout(timeout)
logging.info(web_addr)
except:
web_addr = local_addr
logging.info('not online')
self.local_ip = local_addr
self.online_ip = web_addr
def createSocket(self):
backlog = 100
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
server.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) | 1)
# fixes TIME_WAIT :D
server.bind(("", self.port))
server.listen(backlog)
return server