1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | namespace GameQ\Protocols; |
19: | |
20: | use GameQ\Exception\Protocol as Exception; |
21: | use GameQ\Result; |
22: | use GameQ\Server; |
23: | |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | |
31: | |
32: | |
33: | class Gtar extends Http |
34: | { |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | protected $packets = [ |
41: | self::PACKET_STATUS => "GET /master/ HTTP/1.0\r\nHost: cdn.rage.mp\r\nAccept: */*\r\n\r\n", |
42: | ]; |
43: | |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | protected $transport = self::TRANSPORT_SSL; |
50: | |
51: | |
52: | |
53: | |
54: | |
55: | |
56: | protected $protocol = 'gtar'; |
57: | |
58: | |
59: | |
60: | |
61: | |
62: | |
63: | protected $name = 'gtar'; |
64: | |
65: | |
66: | |
67: | |
68: | |
69: | |
70: | protected $name_long = "Grand Theft Auto Rage"; |
71: | |
72: | |
73: | |
74: | |
75: | |
76: | |
77: | protected $realIp = null; |
78: | |
79: | protected $realPortQuery = null; |
80: | |
81: | |
82: | |
83: | |
84: | |
85: | |
86: | protected $normalize = [ |
87: | |
88: | 'general' => [ |
89: | |
90: | 'dedicated' => 'dedicated', |
91: | 'hostname' => 'hostname', |
92: | 'mod' => 'mod', |
93: | 'maxplayers' => 'maxplayers', |
94: | 'numplayers' => 'numplayers', |
95: | ], |
96: | ]; |
97: | |
98: | public function beforeSend(Server $server) |
99: | { |
100: | |
101: | foreach ($this->packets as $packetType => $packet) { |
102: | |
103: | $this->packets[$packetType] = sprintf($packet, $server->ip . ':' . $server->port_query); |
104: | } |
105: | |
106: | $this->realIp = $server->ip; |
107: | $this->realPortQuery = $server->port_query; |
108: | |
109: | |
110: | $server->ip = 'cdn.rage.mp'; |
111: | $server->port_query = 443; |
112: | } |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | public function processResponse() |
121: | { |
122: | |
123: | if (empty($this->packets_response)) { |
124: | return [ |
125: | 'gq_address' => $this->realIp, |
126: | 'gq_port_query' => $this->realPortQuery, |
127: | ]; |
128: | } |
129: | |
130: | |
131: | preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches); |
132: | |
133: | |
134: | if (!isset($matches[0]) || ($json = json_decode($matches[0])) === null) { |
135: | throw new Exception("JSON response from Gtar protocol is invalid."); |
136: | } |
137: | |
138: | $address = $this->realIp.':'.$this->realPortQuery; |
139: | $server = $json->$address; |
140: | |
141: | if (empty($server)) { |
142: | return [ |
143: | 'gq_address' => $this->realIp, |
144: | 'gq_port_query' => $this->realPortQuery, |
145: | ]; |
146: | } |
147: | |
148: | $result = new Result(); |
149: | |
150: | |
151: | $result->add('dedicated', 1); |
152: | |
153: | $result->add('gq_address', $this->realIp); |
154: | $result->add('gq_port_query', $this->realPortQuery); |
155: | |
156: | |
157: | $result->add('hostname', $server->name); |
158: | $result->add('mod', $server->gamemode); |
159: | $result->add('numplayers', $server->players); |
160: | $result->add('maxplayers', $server->maxplayers); |
161: | |
162: | return $result->fetch(); |
163: | } |
164: | } |
165: | |