-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelnet_handler.py
376 lines (312 loc) · 12.9 KB
/
telnet_handler.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
'''
7D2D map markers Tornado Server
by: Adam Dybczak (RaTilicus)
Note: the Telnet and Tornado/Websocket code have recently been merged to allow exciting possibilities
like in game teleportation using web interface, updating entities via websocket push as opposed to polling ajax.
In the future, other possibilities like game to web to game chat, etc.
The code is in the process of being cleaned up, some things are done inconsistently or incorrectly
(such as how Websocket commands are sent, etc.) Please bear with me.
'''
# MOD name
MOD = 'Live Free or Die'
# message of the day (for announcing mod updates, map resets, etc)
MOTD = ''
import time
import random
import re
from simplejson import dumps as json_encode
from tornado import gen
import traceback
import telnetlib
import psutil
class CommandBase(object):
''' base class for commands that can be sent via telnet to game server '''
cmd = '' # command name
delay = 1 # min number of seconds between sending the command
def __init__(self, db, ts, telnet_handler):
self.db = db
self.next = ts
self.processing_flag = False
self.th = telnet_handler
def reset(self):
''' reset command status, if command times out, etc '''
self.processing_flag = False
def ready(self, ts):
''' returns True if command is ready to process '''
return not self.processing_flag and ts > self.next
def done(self):
''' returns True if command is done processing '''
return not self.processing_flag
def send(self, ts):
''' send command and init processing state '''
if self.processing_flag:
raise Exception('calling send on %s while processing' % self)
if self.ready(ts):
# print 'cmd: sending %s' % self
self.pre_send(ts)
self.th.telnet.write('%s\n' % self.cmd)
self.processing_flag = True
return True
else:
return False
def processing(self, ts, line):
'process the line, return True if still processing, False if done'
if not self.processing_flag:
return False
self.processing_flag = self.process_line(ts, line)
if not self.processing_flag:
self.next = ts + self.delay
#print 'cmd: processing done %s' % self
return self.processing_flag
def pre_send(self, ts):
''' any code you need to run before send
OVERRIDE THIS, IF NEED BE
'''
pass
def process_line(self, ts, line):
''' processes a line from telnet
return True if expecting more lines, False if done
OVERRIDE THIS
'''
return False
class GTCommand(CommandBase):
''' get time command '''
cmd = 'gt'
delay = 15
def get_stats(self, ts):
io = psutil.net_io_counters(pernic=True)['eth0']
if self.last_ts and self.last_ts < ts:
cpu = psutil.cpu_percent(interval=0)
mem = psutil.virtual_memory().percent
io = psutil.net_io_counters(pernic=True)['eth0']
tsd = ts - self.last_ts
sent, recv = (io.bytes_sent-self.last_sent)/tsd/1024, (io.bytes_recv-self.last_recv)/tsd/1024
self.last_ts, self.last_sent, self.last_recv = ts, io.bytes_sent, io.bytes_recv
return ('CPU: %5.1f%% | MEM: %5.1f%% | NET: ↑ %.1fkb/s ↓ %.1fkb/s' %
(cpu, mem, sent, recv))
self.last_ts, self.last_sent, self.last_recv = ts, io.bytes_sent, io.bytes_recv
def __init__(self, db, ts, telnet_handler):
super(GTCommand, self).__init__(db, ts, telnet_handler)
self.last_ts = None
self.get_stats(ts)
def process_line(self, ts, line):
if line.startswith('Day '):
stats = self.get_stats(ts)
data = '%s | %s' % (stats, line)
print 'GT day: ' + data
self.th.send_day_info(data)
return False
return True
class LECommand(CommandBase):
''' list entities command
gets the list of entities and compiles a partial and full update list,
send_update sends the proper version based on websock client needs.
'''
entity_pat = re.compile('^.*type=([^,]+).*name=([^,]+).*id=(\d+).*pos=\((-?\d+\.\d+), (-?\d+\.\d+), (-?\d+\.\d+)\).*rot=\((-?\d+\.\d+), (-?\d+\.\d+), (-?\d+\.\d+)\).*dead=(True|False).*health=(\d+).*$')
entities = {}
old_entities = {}
cmd = 'le'
delay = 1
def pre_send(self, ts):
# reset entity status
self.old_entities = self.entities
self.entities = {}
self.updates = {}
self.th.entities.clear()
def process_line(self, ts, line):
if line.startswith('Total of '):
# found last line of the telnet output, send partial and full entity updates, and removes
remove_entities = list(set(self.old_entities.keys()) - set(self.entities.keys()))
self.th.update_entities(updates=self.updates, entities=self.entities, remove_entities=remove_entities)
return False
try:
# process the line from telnet
if 'type=Entity' in line and not 'type=EntityCar' in line and not 'EntityItem' in line and 'dead=' in line:
pat_data = self.entity_pat.findall(line.strip())
type, name, eid, x, y, z, rx, ry, rz, dead, health = pat_data[0]
eid=int(eid)
data = dict(id=eid, type=type, name=name, x=float(x), y=float(y), z=float(z), h=float(ry), dead=dead=='True', health=int(health))
self.th.entities[eid] = data
self.entities[eid] = data
# if the entity is new or changed position/etc add it to updates dict
if not eid in self.old_entities or self.old_entities[eid] != data:
self.updates[eid] = data
except Exception, e:
print 'LE ERROR: findall: %r> %s \n %s \n %s' % (e, repr(line), pat_data, traceback.print_exc())
return True
class TelnetHandler(object):
''' Parses Telnet information and sends commands
There are 2 basic communications related to telnet
- parsing INF entries
- sending commands and parsing the returned data
'''
player_connected_pat = re.compile(r'entityid=(\d+), name=([^,]+), steamid=(\d+)')
def log(self, text, *args):
print 'TelnetHandler> %s %r' % (text, args)
@gen.coroutine
def player_connected(self, line):
''' handle player connections
- create the player entry if need be
- send website login info and MOTD
'''
entity_id, username, steam_id = self.player_connected_pat.findall(line[50:])[0]
entity_id = int(entity_id)
steam_id = int(steam_id)
player = yield self.db.players.find_one({'_id': steam_id})
print 'PLAYER CONNECTED', entity_id, username, steam_id, player
if not player:
password = '%04d' % random.randrange(0, 9999)
self.db.players.insert({
'_id': steam_id,
'eid': entity_id,
'username': username,
'password': password,
'admin': False,
'last_login': int(time.time())
})
else:
password = str(player['password'])
self.db.players.update({'_id': steam_id}, {'$set': {
'eid': entity_id,
'last_login': int(time.time())
}})
self.telnet.write('pm %s "7d2d.ratilicus.com (u: %s p: %s)"\n' % (entity_id, username, password))
self.telnet.write('pm %s "Please go to that site and read the notes."\n' % (entity_id))
self.telnet.write('pm %s "Please install the %s mod to avoid nasty problems."\n' % (entity_id, MOD))
if MOTD:
self.telnet.write('pm %s "%s"\n' % (entity_id, MOTD))
def parse_INF(self, line):
''' INF handler '''
# handle player login
if 'Player connected' in line:
self.player_connected(line)
# handle player messages
# handle custom commands?
def get_day_info(self):
return self.day_info
def send_day_info(self, day_info=None):
''' send day time info.. called from GT command '''
if day_info:
self.day_info = '%s' % day_info
print 'sdi', self.day_info, day_info
self.sockets.send_day_info(day_info)
@gen.coroutine
def send_teleport_command(self, uid, x, y, z):
if not self.telnet:
self.log('send_teleport_command | Error no telnet connection')
return
self.log('teleporting %s to (%s, %s, %s)' % (uid, x, y, z))
self.telnet.write('tele %s %s 1500 %s\n' % (uid, x, z))
yield gen.sleep(1.5)
self.telnet.write('tele %s %s %s %s\n' % (uid, x, y, z))
self.log('teleporting %s done' % (uid))
def update_entities(self, updates, entities, remove_entities):
print 'send update: s: %d e: %d u: %d r: %d' % (len(self.sockets), len(entities), len(updates), len(remove_entities))
self.sockets.send_global_message(
json={
'tt': 'ue',
'ue': updates,
're': remove_entities
},
full_json={
'tt': 'ue',
'ue': entities,
're': remove_entities,
'full': True
},
reset_flag=True
)
############# UPDATE ###############
def __init__(self, db, telnet_host, telnet_port):
self.ts = time.time()
self.day_info = ''
self.db = db
self.entities = {}
self.players = {}
self.blank_line_count = 0
self.last_cmd = ''
self.telnet_info = telnet_host, telnet_port
self.connect_telnet()
self.commands = [
GTCommand(db, self.ts, self),
LECommand(db, self.ts, self),
]
self.current_command = None
@gen.coroutine
def connect_telnet(self):
try:
self.log('Connecting: %s, %d' % self.telnet_info)
self.telnet = telnetlib.Telnet(*self.telnet_info)
self.blank_line_count = 0
self.current_command_loops = 0
yield gen.sleep(4)
except Exception, e:
self.log('Telnet Connection Failed: %s' % e)
self.telnet = None
def set_sockets(self, sockets):
self.sockets = sockets
def send_command(self):
if not self.current_command or self.current_command.done():
for cmd in self.commands:
if cmd.ready(self.ts):
self.last_cmd = cmd.cmd
self.current_command_loops = 0
self.current_command = cmd
self.current_command.send(self.ts)
return
def process_command(self, line):
if self.current_command and not self.current_command.done():
if self.current_command_loops > 1000:
self.reset_current_command('too many loops: %s' % self.current_command_loops)
else:
self.current_command_loops += 1
self.current_command.processing(self.ts, line)
def reset_current_command(self, reason='N/A'):
''' reset current command helper
- log reason for reset
- reset command status and que
- reconnect telnet (resets are usually due to timeout/telnet disconnect)
'''
self.blank_line_count = 0
self.current_command_loops = 0
self.current_command.reset()
self.current_command = None
print 'resetting current command: %s' % reason
self.connect_telnet()
def update(self):
''' update/process telnet parser
this gets called periodically (in sdtd-tornado.py)
- all tparser processes are updated from this
- sends commands
- gets data from telnet
- passes data to current command
- checks for timeouts
'''
if not self.telnet:
return
self.ts = time.time()
self.send_command()
try:
lines = self.telnet.read_very_eager()
except EOFError:
self.reset_current_command('EOF Error: %s' % self.blank_line_count)
lines = None
if not lines:
self.blank_line_count +=1
if self.blank_line_count > 5:
self.reset_current_command('too many blank lines: %s' % self.blank_line_count)
self.sockets.ping_all()
return False
self.blank_line_count = 0
for line in lines.split('\r\n'):
self.update_line(line)
return True
def update_line(self, line):
''' updates/parses one telnet line at a time
sends the data to INF handler or command handler
'''
if line:
if ' INF ' in line:
self.parse_INF(line)
else:
self.process_command(line)