-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlegions_client.py
291 lines (239 loc) · 9.42 KB
/
legions_client.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
from struct import *
from web import Webpage
from markupsafe import escape
from configparser import ConfigParser
import os
import errno
import socket
import binascii
import time
import sys
import traceback
import logging
class LegionsClient:
def __init__(self):
self.ip_list = []
self.server_info = {}
#Structs
self.reply_struct = Struct('BBHHBBH')
self.server_struct = Struct('4sH')
self.byte_struct = Struct('B')
self.info_struct_req = Struct('BBI')
self.info_struct_resp = Struct('BBBBH')
#Network
self.master_socket = ("master.legionsoverdrive.com",28002)
self.game_info_req = 18
self.game_ping_req = 14
self.game_ping_resp = 16
self.game_info_resp = 20
def pack_master(self):
packet_type = 6
flags = 0
session = 0
key = 1
index = 255
game_type = b"Legions: Overdrive"
game_len = len(game_type)
mission_type = b"any"
mission_len = len(mission_type)
min_players = 0
max_players = 255
region_mask = 2
version = 0
status = 0
max_bots = 0
min_cpu = 255
buddy_count = 0
buddy_list = 0
packet = pack('BBHHBB18sB3sBBiiBBBBB',
packet_type,
flags,
session,
key,
index,
game_len,
game_type,
mission_len,
mission_type,
min_players,
max_players,
region_mask,
version,
status,
max_bots,
min_cpu,
buddy_count,
buddy_list)
return packet
def send_master(self, packet):
data = []
fail_sleep_len = 15
req_sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
req_sock.settimeout(5)
try:
req_sock.connect(self.master_socket)
req_sock.send(packet)
data.append(req_sock.recv(4096))
print("MSG: {0}".format(binascii.hexlify(data[-1])))
packet_count = self.reply_struct.unpack_from(data[-1])[-2]
if packet_count-1 > 0:
for i in range(packet_count-1):
data.append(req_sock.recv(4096))
print("MSG: ", binascii.hexlify(data[-1]))
req_sock.close()
return data
except socket.timeout:
message = "ERR: Master server timed out, will retry..."
print(message)
time.sleep(fail_sleep_len)
return False
except socket.gaierror:
message = "ERR: Master server DNS fail, will retry..."
print(message)
time.sleep(fail_sleep_len)
return False
except socket.error as e:
if e.errno != errno.ECONNREFUSED:
print("ERR: Master server refused the connection, will retry...")
else:
logging.exception("socket error at send_master()")
except OSError:
message = "ERR: Connection error..."
print(message)
time.sleep(fail_sleep_len)
def parse_master(self, data):
offset = 10
server_data = []
for packet in data:
servers_in_packet = self.reply_struct.unpack_from(packet)[-1]
for servers in range(servers_in_packet):
data = self.server_struct.unpack_from(packet, offset = offset)
server_data.append(data)
offset += self.server_struct.size
for server in server_data:
self.ip_list.append((".".join([str(i) for i in server[0]]),server[1]))
def query_master(self):
packet = self.pack_master()
master_data = self.send_master(packet)
if master_data:
self.parse_master(master_data)
else:
self.retry = True
def pack_single(self, header):
flags = 2
session = 0
return self.info_struct_req.pack(header, flags, session)
def send_single(self, host, packet):
data = None
try:
req_sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
req_sock.connect(host)
req_sock.send(packet)
req_sock.settimeout(4)
data = req_sock.recv(4096)
except socket.timeout:
print("ERR: {0} timed out...".format(host))
except socket.gaierror:
print("ERR: {0} DNS error...".format(host))
except socket.error as e:
if e.errno != errno.ECONNREFUSED:
print("ERR: {0} refused the connection...".format(host))
else:
logging.exception("socket error at send_master()")
except OSError:
print("ERR: Connection error...")
req_sock.close()
return data
def parse_single(self, data):
offset = 1
if self.byte_struct.unpack_from(data)[0] == self.game_info_resp:
flags, key = Struct("<BI").unpack_from(data, offset = offset)
offset += 5
game_type_len = self.byte_struct.unpack_from(data, offset = offset)[0]
offset += self.byte_struct.size
game_type = Struct("<{0}s".format(game_type_len)).unpack_from(data, offset = offset)[0].decode()
offset += game_type_len
mission_type_len = self.byte_struct.unpack_from(data, offset = offset)[0]
offset += self.byte_struct.size
mission_type = Struct("<{0}s".format(mission_type_len)).unpack_from(data, offset = offset)[0].decode()
offset += mission_type_len
mission_name_len = self.byte_struct.unpack_from(data, offset = offset)[0]
offset += self.byte_struct.size
mission_name = Struct("<{0}s".format(mission_name_len)).unpack_from(data, offset = offset)[0].decode()
offset += mission_name_len
status, player_count, max_players, bot_count, cpu = self.info_struct_resp.unpack_from(data, offset = offset)
offset += self.info_struct_resp.size
offset += 3 #unused bytes
status = bin(status)[2:] #binary to string
status = '0'*(3-len(status)) + status #turns 1 to 001
#subscriptable flags
dedicated = bool(int(status[0]))
password = bool(int(status[1]))
raw_players = data[offset:].decode("utf-8", errors="replace").splitlines()
players = []
for player in raw_players:
players.append(str(escape(player[2:-2])))
return {"mission":mission_name, "gamemode":mission_type, "players":players, "player_count":player_count, "max_players":max_players, "passworded":password}
elif self.game_ping_resp:
flags, key, ver_len = Struct("<BIB").unpack_from(data, offset = offset)
offset += 6
ver = Struct("<{0}s".format(ver_len)).unpack_from(data, offset = offset)[0]
offset += ver_len
curr_ver, min_ver, ver_num, name_len = Struct("<IIIB").unpack_from(data, offset = offset)
offset += 13
name = Struct("<{0}s".format(name_len)).unpack_from(data, offset = offset)[0].decode("utf-8")
offset += name_len
return name
def query_all(self):
game_info_packet = self.pack_single(self.game_info_req)
game_ping_packet = self.pack_single(self.game_ping_req)
for server in self.ip_list:
print("MSG: Querying {0}...".format(server))
game_ping_data = self.send_single(server, game_ping_packet)
game_info_data = self.send_single(server, game_info_packet)
if game_info_data and game_ping_data:
print("MSG: Success, info and ping recieved.")
server_name = self.parse_single(game_ping_data)
server_dict = self.parse_single(game_info_data)
server_dict["socket"] = server
self.server_info[server_name] = server_dict
print(u"MSG: {0} with {1} players sucessfully parsed.".format(server_name, len(server_dict["players"])))
def get_cfg():
if "-c" in sys.argv:
flag_index = sys.argv.index("-c")
filen = sys.argv[flag_index+1]
if os.path.exists(filen) and os.path.isfile(filen):
return filen
if __name__ == "__main__":
error_count = 0
##cfg
cfg = ConfigParser()
cfg_file = get_cfg()
if cfg_file:
cfg.read(cfg_file)
else:
cfg.read("default.cfg")
##log
logging.basicConfig(filename=cfg.get("core", "errors"))
##setup webpage editor object
webpage = Webpage(cfg)
while True:
try:
l_client = LegionsClient()
l_client.query_master()
l_client.query_all()
server_data = l_client.server_info
webpage.write(server_data)
print("MSG: Sleeping for 60 seconds...")
time.sleep(60)
except KeyboardInterrupt:
sys.exit()
except:
error_count += 1
logging.exception("")
if error_count < 5:
print("ERR: Error has been logged and the script will continue in 5 seconds...")
else:
print("ERR: Continuous error, program cannot recover...")
exit()
time.sleep(5)