1
0

Gtar.php 4.3 KB

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