|
| 1 | +import re |
| 2 | + |
| 3 | +from opengsq.binary_reader import BinaryReader |
| 4 | +from opengsq.exceptions import InvalidPacketException |
| 5 | +from opengsq.protocol_base import ProtocolBase |
| 6 | +from opengsq.socket_async import SocketAsync |
| 7 | + |
| 8 | + |
| 9 | +class Unreal2(ProtocolBase): |
| 10 | + """Unreal 2 Protocol""" |
| 11 | + full_name = 'Unreal 2 Protocol' |
| 12 | + |
| 13 | + _DETAILS = 0x00 |
| 14 | + _RULES = 0x01 |
| 15 | + _PLAYERS = 0x02 |
| 16 | + |
| 17 | + async def get_details(self): |
| 18 | + with SocketAsync() as sock: |
| 19 | + sock.settimeout(self._timeout) |
| 20 | + await sock.connect((self._address, self._query_port)) |
| 21 | + |
| 22 | + # Send Request |
| 23 | + sock.send(b'\x79\x00\x00\x00' + bytes([self._DETAILS])) |
| 24 | + |
| 25 | + # Server response |
| 26 | + response = await sock.recv() |
| 27 | + |
| 28 | + # Remove the first 4 bytes \x80\x00\x00\x00 |
| 29 | + br = BinaryReader(response[4:]) |
| 30 | + header = br.read_byte() |
| 31 | + |
| 32 | + if header != self._DETAILS: |
| 33 | + raise InvalidPacketException( |
| 34 | + 'Packet header mismatch. Received: {}. Expected: {}.' |
| 35 | + .format(chr(header), chr(self._DETAILS)) |
| 36 | + ) |
| 37 | + |
| 38 | + details = {} |
| 39 | + details['ServerId'] = br.read_long() # 0 |
| 40 | + details['ServerIP'] = br.read_string() # empty |
| 41 | + details['GamePort'] = br.read_long() |
| 42 | + details['QueryPort'] = br.read_long() # 0 |
| 43 | + details['ServerName'] = self.__read_string(br) |
| 44 | + details['MapName'] = self.__read_string(br) |
| 45 | + details['GameType'] = self.__read_string(br) |
| 46 | + details['NumPlayers'] = br.read_long() |
| 47 | + details['MaxPlayers'] = br.read_long() |
| 48 | + |
| 49 | + if br.length() > 12: |
| 50 | + try: |
| 51 | + # Killing Floor |
| 52 | + stream_position = br.stream_position |
| 53 | + details['WaveCurrent'] = br.read_long() |
| 54 | + details['WaveTotal'] = br.read_long() |
| 55 | + details['Ping'] = br.read_long() |
| 56 | + details['Flags'] = br.read_long() |
| 57 | + details['Skill'] = self.__read_string(br) |
| 58 | + except: |
| 59 | + br.stream_position = stream_position |
| 60 | + details['Ping'] = br.read_long() |
| 61 | + details['Flags'] = br.read_long() |
| 62 | + details['Skill'] = self.__read_string(br) |
| 63 | + |
| 64 | + return details |
| 65 | + |
| 66 | + async def get_rules(self): |
| 67 | + with SocketAsync() as sock: |
| 68 | + sock.settimeout(self._timeout) |
| 69 | + await sock.connect((self._address, self._query_port)) |
| 70 | + |
| 71 | + # Send Request |
| 72 | + sock.send(b'\x79\x00\x00\x00' + bytes([self._RULES])) |
| 73 | + |
| 74 | + # Server response |
| 75 | + response = await sock.recv() |
| 76 | + |
| 77 | + # Remove the first 4 bytes \x80\x00\x00\x00 |
| 78 | + br = BinaryReader(response[4:]) |
| 79 | + header = br.read_byte() |
| 80 | + |
| 81 | + if header != self._RULES: |
| 82 | + |
| 83 | + raise InvalidPacketException( |
| 84 | + 'Packet header mismatch. Received: {}. Expected: {}.' |
| 85 | + .format(chr(header), chr(self._RULES)) |
| 86 | + ) |
| 87 | + |
| 88 | + rules = {} |
| 89 | + rules['Mutators'] = [] |
| 90 | + |
| 91 | + while not br.is_end(): |
| 92 | + key = self.__read_string(br) |
| 93 | + val = self.__read_string(br) |
| 94 | + |
| 95 | + if key.lower() == 'mutator': |
| 96 | + rules['Mutators'].append(val) |
| 97 | + else: |
| 98 | + rules[key] = val |
| 99 | + |
| 100 | + return rules |
| 101 | + |
| 102 | + async def get_players(self): |
| 103 | + with SocketAsync() as sock: |
| 104 | + sock.settimeout(self._timeout) |
| 105 | + await sock.connect((self._address, self._query_port)) |
| 106 | + |
| 107 | + # Send Request |
| 108 | + sock.send(b'\x79\x00\x00\x00' + bytes([self._PLAYERS])) |
| 109 | + |
| 110 | + # Server response |
| 111 | + response = await sock.recv() |
| 112 | + |
| 113 | + # Remove the first 4 bytes \x80\x00\x00\x00 |
| 114 | + br = BinaryReader(response[4:]) |
| 115 | + header = br.read_byte() |
| 116 | + |
| 117 | + if header != self._PLAYERS: |
| 118 | + raise InvalidPacketException( |
| 119 | + 'Packet header mismatch. Received: {}. Expected: {}.' |
| 120 | + .format(chr(header), chr(self._PLAYERS)) |
| 121 | + ) |
| 122 | + |
| 123 | + players = [] |
| 124 | + |
| 125 | + while not br.is_end(): |
| 126 | + player = {} |
| 127 | + player['Id'] = br.read_long() |
| 128 | + player['Name'] = self.__read_string(br) |
| 129 | + player['Ping'] = br.read_long() |
| 130 | + player['Score'] = br.read_long() |
| 131 | + player['StatsId'] = br.read_long() |
| 132 | + players.append(player) |
| 133 | + |
| 134 | + return players |
| 135 | + |
| 136 | + @staticmethod |
| 137 | + def strip_colors(text: bytes): |
| 138 | + """Strip color codes""" |
| 139 | + return re.compile(b'\x7f|[\x00-\x1a]|[\x1c-\x1f]').sub(b'', text).replace(b'\x1b@@', b'').replace(b'\x1b@', b'').replace(b'\x1b', b'') |
| 140 | + |
| 141 | + def __read_string(self, br: BinaryReader): |
| 142 | + length = br.read_byte() |
| 143 | + string = br.read_string() |
| 144 | + |
| 145 | + if length == len(string) + 1: |
| 146 | + b = bytes(string, encoding='utf-8') |
| 147 | + else: |
| 148 | + b = bytes(string, encoding='utf-16') |
| 149 | + |
| 150 | + b = Unreal2.strip_colors(b) |
| 151 | + |
| 152 | + return b.decode('utf-8', 'ignore') |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == '__main__': |
| 156 | + import asyncio |
| 157 | + import json |
| 158 | + |
| 159 | + async def main_async(): |
| 160 | + # ut2004 |
| 161 | + unreal2 = Unreal2(address='109.230.224.189', query_port=6970, timeout=10.0) |
| 162 | + details = await unreal2.get_details() |
| 163 | + print(json.dumps(details, indent=None) + '\n') |
| 164 | + rules = await unreal2.get_rules() |
| 165 | + print(json.dumps(rules, indent=None) + '\n') |
| 166 | + players = await unreal2.get_players() |
| 167 | + print(json.dumps(players, indent=None) + '\n') |
| 168 | + |
| 169 | + asyncio.run(main_async()) |
0 commit comments