Gtan.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. use GameQ\Exception\Protocol as Exception;
  20. use GameQ\Result;
  21. use GameQ\Server;
  22. /**
  23. * Grand Theft Auto Network Protocol Class
  24. * https://stats.gtanet.work/
  25. *
  26. * Result from this call should be a header + JSON response
  27. *
  28. * References:
  29. * - https://master.gtanet.work/apiservers
  30. *
  31. * @author Austin Bischoff <[email protected]>
  32. */
  33. class Gtan extends Http
  34. {
  35. /**
  36. * Packets to send
  37. *
  38. * @var array
  39. */
  40. protected $packets = [
  41. //self::PACKET_STATUS => "GET /apiservers HTTP/1.0\r\nHost: master.gtanet.work\r\nAccept: */*\r\n\r\n",
  42. self::PACKET_STATUS => "GET /gtan/api.php?ip=%s&raw HTTP/1.0\r\nHost: multiplayerhosting.info\r\nAccept: */*\r\n\r\n",
  43. ];
  44. /**
  45. * Http protocol is SSL
  46. *
  47. * @var string
  48. */
  49. protected $transport = self::TRANSPORT_SSL;
  50. /**
  51. * The protocol being used
  52. *
  53. * @var string
  54. */
  55. protected $protocol = 'gtan';
  56. /**
  57. * String name of this protocol class
  58. *
  59. * @var string
  60. */
  61. protected $name = 'gtan';
  62. /**
  63. * Longer string name of this protocol class
  64. *
  65. * @var string
  66. */
  67. protected $name_long = "Grand Theft Auto Network";
  68. /**
  69. * Holds the real ip so we can overwrite it back
  70. *
  71. * @var string
  72. */
  73. protected $realIp = null;
  74. protected $realPortQuery = null;
  75. /**
  76. * Normalize some items
  77. *
  78. * @var array
  79. */
  80. protected $normalize = [
  81. // General
  82. 'general' => [
  83. // target => source
  84. 'dedicated' => 'dedicated',
  85. 'hostname' => 'hostname',
  86. 'mapname' => 'map',
  87. 'mod' => 'mod',
  88. 'maxplayers' => 'maxplayers',
  89. 'numplayers' => 'numplayers',
  90. 'password' => 'password',
  91. ],
  92. ];
  93. public function beforeSend(Server $server)
  94. {
  95. // Loop over the packets and update them
  96. foreach ($this->packets as $packetType => $packet) {
  97. // Fill out the packet with the server info
  98. $this->packets[$packetType] = sprintf($packet, $server->ip . ':' . $server->port_query);
  99. }
  100. $this->realIp = $server->ip;
  101. $this->realPortQuery = $server->port_query;
  102. // Override the existing settings
  103. //$server->ip = 'master.gtanet.work';
  104. $server->ip = 'multiplayerhosting.info';
  105. $server->port_query = 443;
  106. }
  107. /**
  108. * Process the response
  109. *
  110. * @return array
  111. * @throws Exception
  112. */
  113. public function processResponse()
  114. {
  115. // No response, assume offline
  116. if (empty($this->packets_response)) {
  117. return [
  118. 'gq_address' => $this->realIp,
  119. 'gq_port_query' => $this->realPortQuery,
  120. ];
  121. }
  122. // Implode and rip out the JSON
  123. preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches);
  124. // Return should be JSON, let's validate
  125. if (!isset($matches[0]) || ($json = json_decode($matches[0])) === null) {
  126. throw new Exception("JSON response from Gtan protocol is invalid.");
  127. }
  128. $result = new Result();
  129. // Server is always dedicated
  130. $result->add('dedicated', 1);
  131. $result->add('gq_address', $this->realIp);
  132. $result->add('gq_port_query', $this->realPortQuery);
  133. // Add server items
  134. $result->add('hostname', $json->ServerName);
  135. $result->add('serverversion', $json->ServerVersion);
  136. $result->add('map', ((!empty($json->Map)) ? $json->Map : 'Los Santos/Blaine Country'));
  137. $result->add('mod', $json->Gamemode);
  138. $result->add('password', (int)$json->Passworded);
  139. $result->add('numplayers', $json->CurrentPlayers);
  140. $result->add('maxplayers', $json->MaxPlayers);
  141. return $result->fetch();
  142. }
  143. }