| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of GameQ. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * GameQ is free software; you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * it under the terms of the GNU Lesser General Public License as published by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * the Free Software Foundation; either version 3 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * GameQ is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * GNU Lesser General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * You should have received a copy of the GNU Lesser General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | namespace GameQ\Protocols; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use GameQ\Exception\Protocol as Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use GameQ\Result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use GameQ\Server; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * Grand Theft Auto Rage Protocol Class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * https://rage.mp/masterlist/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * Result from this call should be a header + JSON response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @author K700 <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * @author Austin Bischoff <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | class Gtar extends Http | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Packets to send | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |      * Http protocol is SSL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     protected $transport = self::TRANSPORT_SSL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * The protocol being used | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     protected $protocol = 'gtar'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * String name of this protocol class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     protected $name = 'gtar'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * Longer string name of this protocol class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     protected $name_long = "Grand Theft Auto Rage"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * Holds the real ip so we can overwrite it back | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     protected $realIp = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     protected $realPortQuery = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * Normalize some items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     protected $normalize = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         // General | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         'general' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             // target       => source | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             'dedicated'  => 'dedicated', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             'hostname'   => 'hostname', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             'mod'        => 'mod', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             'maxplayers' => 'maxplayers', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             'numplayers' => 'numplayers', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 2 |  |     public function beforeSend(Server $server) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         // Loop over the packets and update them | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 2 |  |         foreach ($this->packets as $packetType => $packet) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             // Fill out the packet with the server info | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |             $this->packets[$packetType] = sprintf($packet, $server->ip . ':' . $server->port_query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 2 |  |         $this->realIp = $server->ip; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 2 |  |         $this->realPortQuery = $server->port_query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         // Override the existing settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 2 |  |         $server->ip = 'cdn.rage.mp'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 2 |  |         $server->port_query = 443; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * Process the response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @throws Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 120 | 2 |  |     public function processResponse() | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |         // No response, assume offline | 
            
                                                                        
                            
            
                                    
            
            
                | 123 | 2 |  |         if (empty($this->packets_response)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |             return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |                 'gq_address'    => $this->realIp, | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |                 'gq_port_query' => $this->realPortQuery, | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |             ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |         // Implode and rip out the JSON | 
            
                                                                        
                            
            
                                    
            
            
                | 131 | 2 |  |         preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches); | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |         // Return should be JSON, let's validate | 
            
                                                                        
                            
            
                                    
            
            
                | 134 | 2 |  |         if (!isset($matches[0]) || ($json = json_decode($matches[0])) === null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |             throw new Exception("JSON response from Gtar protocol is invalid."); | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 138 | 2 |  |         $address = $this->realIp.':'.$this->realPortQuery; | 
            
                                                                        
                            
            
                                    
            
            
                | 139 | 2 |  |         $server = $json->$address; | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 141 | 2 |  |         if (empty($server)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |             return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |                 'gq_address'    => $this->realIp, | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |                 'gq_port_query' => $this->realPortQuery, | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |             ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 148 | 2 |  |         $result = new Result(); | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |         // Server is always dedicated | 
            
                                                                        
                            
            
                                    
            
            
                | 151 | 2 |  |         $result->add('dedicated', 1); | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 153 | 2 |  |         $result->add('gq_address', $this->realIp); | 
            
                                                                        
                            
            
                                    
            
            
                | 154 | 2 |  |         $result->add('gq_port_query', $this->realPortQuery); | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |         // Add server items | 
            
                                                                        
                            
            
                                    
            
            
                | 157 | 2 |  |         $result->add('hostname', $server->name); | 
            
                                                                        
                            
            
                                    
            
            
                | 158 | 2 |  |         $result->add('mod', $server->gamemode); | 
            
                                                                        
                            
            
                                    
            
            
                | 159 | 2 |  |         $result->add('numplayers', $server->players); | 
            
                                                                        
                            
            
                                    
            
            
                | 160 | 2 |  |         $result->add('maxplayers', $server->maxplayers); | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 162 | 2 |  |         return $result->fetch(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 164 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 165 |  |  |  |