-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathase.py
126 lines (113 loc) · 4.45 KB
/
ase.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
import struct
ASE_HAS_PLAYER_COUNT = 0x0004
ASE_HAS_MAX_PLAYER_COUNT = 0x0008
ASE_HAS_GAME_NAME = 0x0010
ASE_HAS_SERVER_NAME = 0x0020
ASE_HAS_GAME_MODE = 0x0040
ASE_HAS_MAP_NAME = 0x0080
ASE_HAS_SERVER_VERSION = 0x0100
ASE_HAS_PASSWORDED_FLAG = 0x0200
ASE_HAS_SERIALS_FLAG = 0x0400
ASE_HAS_PLAYER_LIST = 0x0800
ASE_HAS_RESPONDING_FLAG = 0x1000
ASE_HAS_RESTRICTION_FLAGS = 0x2000
ASE_HAS_SEARCH_IGNORE_SECTIONS = 0x4000
ASE_HAS_KEEP_FLAG = 0x8000
ASE_HAS_HTTP_PORT = 0x080000
ASE_HAS_SPECIAL_FLAGS = 0x100000
class MTAServerlist():
def readInt(self):
return self.read(">I")
def readShort(self):
return self.read(">H")
def readChar(self):
return self.read(">B")
def read(self, format):
value = struct.unpack_from(format, self.data, self.offset)
self.offset += struct.calcsize(format)
return value[0]
def readString(self):
strlen = self.readChar()
string = ""
for i in range(0, strlen):
string = string + chr(self.readChar())
return string
async def parse(self, data):
self.data = data
self.offset = 0
self.readShort() # skip length (unused)
self.version = self.readShort()
self.flags = self.readInt()
self.sequence = self.readInt()
self.count = self.readInt()
self.hasPlayerCount = self.flags & ASE_HAS_PLAYER_COUNT
self.hasMaxPlayerCount = self.flags & ASE_HAS_MAX_PLAYER_COUNT
self.hasGameName = self.flags & ASE_HAS_GAME_NAME
self.hasServerName = self.flags & ASE_HAS_SERVER_NAME
self.hasGameMode = self.flags & ASE_HAS_GAME_MODE
self.hasMapName = self.flags & ASE_HAS_MAP_NAME
self.hasServerVersion = self.flags & ASE_HAS_SERVER_VERSION
self.hasPasswordedFlag = self.flags & ASE_HAS_PASSWORDED_FLAG
self.hasSerialsFlag = self.flags & ASE_HAS_SERIALS_FLAG
self.hasPlayerList = self.flags & ASE_HAS_PLAYER_LIST
self.hasRespondingFlag = self.flags & ASE_HAS_RESPONDING_FLAG
self.hasRestrictionFlags = self.flags & ASE_HAS_RESTRICTION_FLAGS
self.hasSearchIgnoreSections = self.flags & ASE_HAS_SEARCH_IGNORE_SECTIONS
self.hasKeepFlag = self.flags & ASE_HAS_KEEP_FLAG
self.hasHttpPort = self.flags & ASE_HAS_HTTP_PORT
self.hasSpecial = self.flags & ASE_HAS_SPECIAL_FLAGS
self.servers = []
for i in range(0, self.count):
self.servers.append(self.parseServer())
def parseServer(self):
server = {}
entrylength = self.readShort()
nextoffset = self.offset + entrylength - 2
ip1 = str(self.readChar())
ip2 = str(self.readChar())
ip3 = str(self.readChar())
ip4 = str(self.readChar())
server["ip"] = ip4 + "." + ip3 + "." + ip2 + "." + ip1
server["port"] = self.readShort()
if self.hasPlayerCount:
server["players"] = self.readShort()
if self.hasMaxPlayerCount:
server["maxplayers"] = self.readShort()
if self.hasGameName:
server["gamename"] = self.readString()
if self.hasServerName:
server["name"] = self.readString()
if self.hasGameMode:
server["gamemode"] = self.readString()
if self.hasMapName:
server["map"] = self.readString()
if self.hasServerVersion:
server["version"] = self.readString()
if self.hasPasswordedFlag:
server["password"] = self.readChar()
if self.hasSerialsFlag:
server["serials"] = self.readChar()
if self.hasPlayerList:
count = self.readShort()
server["players"] = []
for i in range(0, count):
server["players"].append(self.readString)
if self.hasRespondingFlag:
server["responding"] = self.readChar()
if self.hasRestrictionFlags:
server["restriction"] = self.readInt()
if self.hasSearchIgnoreSections:
num = self.readChar()
server["searchIgnore"] = []
for i in range(0, num):
offset = self.readChar()
length = self.readChar()
server["searchIgnore"].append((offset, length))
if self.hasKeepFlag:
server["keep"] = self.readChar()
if self.hasHttpPort:
server["http"] = self.readShort()
if self.hasSpecial:
server["special"] = self.readChar()
self.offset = nextoffset
return server