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: | |
34: | |
35: | class Gtan extends Http |
36: | { |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | protected $packets = [ |
43: | |
44: | self::PACKET_STATUS => "GET /gtan/api.php?ip=%s&raw HTTP/1.0\r\nHost: multiplayerhosting.info\r\nAccept: */*\r\n\r\n", |
45: | ]; |
46: | |
47: | |
48: | |
49: | |
50: | |
51: | |
52: | protected $transport = self::TRANSPORT_SSL; |
53: | |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | protected $protocol = 'gtan'; |
60: | |
61: | |
62: | |
63: | |
64: | |
65: | |
66: | protected $name = 'gtan'; |
67: | |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | protected $name_long = "Grand Theft Auto Network"; |
74: | |
75: | |
76: | |
77: | |
78: | |
79: | |
80: | protected $realIp = null; |
81: | |
82: | protected $realPortQuery = null; |
83: | |
84: | |
85: | |
86: | |
87: | |
88: | |
89: | protected $normalize = [ |
90: | |
91: | 'general' => [ |
92: | |
93: | 'dedicated' => 'dedicated', |
94: | 'hostname' => 'hostname', |
95: | 'mapname' => 'map', |
96: | 'mod' => 'mod', |
97: | 'maxplayers' => 'maxplayers', |
98: | 'numplayers' => 'numplayers', |
99: | 'password' => 'password', |
100: | ], |
101: | ]; |
102: | |
103: | public function beforeSend(Server $server) |
104: | { |
105: | |
106: | foreach ($this->packets as $packetType => $packet) { |
107: | |
108: | $this->packets[$packetType] = sprintf($packet, $server->ip . ':' . $server->port_query); |
109: | } |
110: | |
111: | $this->realIp = $server->ip; |
112: | $this->realPortQuery = $server->port_query; |
113: | |
114: | |
115: | |
116: | $server->ip = 'multiplayerhosting.info'; |
117: | $server->port_query = 443; |
118: | } |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | |
125: | |
126: | public function processResponse() |
127: | { |
128: | |
129: | if (empty($this->packets_response)) { |
130: | return [ |
131: | 'gq_address' => $this->realIp, |
132: | 'gq_port_query' => $this->realPortQuery, |
133: | ]; |
134: | } |
135: | |
136: | |
137: | preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches); |
138: | |
139: | |
140: | if (!isset($matches[0]) || ($json = json_decode($matches[0])) === null) { |
141: | throw new Exception("JSON response from Gtan protocol is invalid."); |
142: | } |
143: | |
144: | $result = new Result(); |
145: | |
146: | |
147: | $result->add('dedicated', 1); |
148: | |
149: | $result->add('gq_address', $this->realIp); |
150: | $result->add('gq_port_query', $this->realPortQuery); |
151: | |
152: | |
153: | $result->add('hostname', $json->ServerName); |
154: | $result->add('serverversion', $json->ServerVersion); |
155: | $result->add('map', ((!empty($json->Map)) ? $json->Map : 'Los Santos/Blaine Country')); |
156: | $result->add('mod', $json->Gamemode); |
157: | $result->add('password', (int)$json->Passworded); |
158: | $result->add('numplayers', $json->CurrentPlayers); |
159: | $result->add('maxplayers', $json->MaxPlayers); |
160: | |
161: | return $result->fetch(); |
162: | } |
163: | } |
164: | |