1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | namespace GameQ\Protocols; |
20: | |
21: | use GameQ\Buffer; |
22: | use GameQ\Exception\Protocol as Exception; |
23: | use GameQ\Protocol; |
24: | use GameQ\Result; |
25: | use GameQ\Server; |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | |
34: | class Raknet extends Protocol |
35: | { |
36: | |
37: | |
38: | |
39: | const OFFLINE_MESSAGE_DATA_ID = "\x00\xFF\xFF\x00\xFE\xFE\xFE\xFE\xFD\xFD\xFD\xFD\x12\x34\x56\x78"; |
40: | |
41: | |
42: | |
43: | |
44: | const ID_UNCONNECTED_PONG = "\x1C"; |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | protected $packets = [ |
53: | self::PACKET_STATUS => "\x01%s%s\x02\x00\x00\x00\x00\x00\x00\x00", |
54: | ]; |
55: | |
56: | |
57: | |
58: | |
59: | |
60: | |
61: | protected $protocol = 'raknet'; |
62: | |
63: | |
64: | |
65: | |
66: | |
67: | |
68: | protected $name = 'raknet'; |
69: | |
70: | |
71: | |
72: | |
73: | |
74: | |
75: | protected $name_long = "Raknet Server"; |
76: | |
77: | |
78: | |
79: | |
80: | |
81: | |
82: | |
83: | |
84: | public function beforeSend(Server $server) |
85: | { |
86: | |
87: | $this->packets[self::PACKET_STATUS] = sprintf( |
88: | $this->packets[self::PACKET_STATUS], |
89: | pack('Q', time()), |
90: | self::OFFLINE_MESSAGE_DATA_ID |
91: | ); |
92: | } |
93: | |
94: | |
95: | |
96: | |
97: | |
98: | |
99: | |
100: | public function processResponse() |
101: | { |
102: | |
103: | $buffer = new Buffer(implode($this->packets_response)); |
104: | |
105: | |
106: | $header = $buffer->read(1); |
107: | |
108: | |
109: | if ($header !== self::ID_UNCONNECTED_PONG) { |
110: | throw new Exception(sprintf( |
111: | '%s The header returned "%s" does not match the expected header of "%s"', |
112: | __METHOD__, |
113: | bin2hex($header), |
114: | bin2hex(self::ID_UNCONNECTED_PONG) |
115: | )); |
116: | } |
117: | |
118: | |
119: | $buffer->skip(8); |
120: | |
121: | |
122: | $serverGUID = $buffer->readInt64(); |
123: | |
124: | |
125: | $magicCheck = $buffer->read(16); |
126: | |
127: | |
128: | if ($magicCheck !== self::OFFLINE_MESSAGE_DATA_ID) { |
129: | throw new Exception(sprintf( |
130: | '%s The magic value returned "%s" does not match the expected value of "%s"', |
131: | __METHOD__, |
132: | bin2hex($magicCheck), |
133: | bin2hex(self::OFFLINE_MESSAGE_DATA_ID) |
134: | )); |
135: | } |
136: | |
137: | |
138: | |
139: | |
140: | $buffer->skip(2); |
141: | |
142: | |
143: | $result = new Result(); |
144: | |
145: | |
146: | $info = explode(';', $buffer->getBuffer()); |
147: | |
148: | $result->add('edition', $info[0]); |
149: | $result->add('motd_line_1', $info[1]); |
150: | $result->add('protocol_version', (int)$info[2]); |
151: | $result->add('version', $info[3]); |
152: | $result->add('num_players', (int)$info[4]); |
153: | $result->add('max_players', (int)$info[5]); |
154: | $result->add('server_uid', $info[6]); |
155: | $result->add('motd_line_2', $info[7]); |
156: | $result->add('gamemode', $info[8]); |
157: | $result->add('gamemode_numeric', (int)$info[9]); |
158: | $result->add('port_ipv4', (isset($info[10])) ? (int)$info[10] : null); |
159: | $result->add('port_ipv6', (isset($info[11])) ? (int)$info[11] : null); |
160: | $result->add('dedicated', 1); |
161: | |
162: | unset($header, $serverGUID, $magicCheck, $info); |
163: | |
164: | return $result->fetch(); |
165: | } |
166: | } |
167: | |