BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tracker.class.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. #declare(strict_types=1);
  3. // TODO: Turn this into a class with nice functions like update_user, delete_torrent, etc.
  4. class Tracker
  5. {
  6. const STATS_MAIN = 0;
  7. const STATS_USER = 1;
  8. public static $Requests = [];
  9. /**
  10. * Send a GET request over a socket directly to the tracker
  11. * For example, Tracker::update_tracker('change_passkey', array('oldpasskey' => OLD_PASSKEY, 'newpasskey' => NEW_PASSKEY)) will send the request:
  12. * GET /tracker_32_char_secret_code/update?action=change_passkey&oldpasskey=OLD_PASSKEY&newpasskey=NEW_PASSKEY HTTP/1.1
  13. *
  14. * @param string $Action The action to send
  15. * @param array $Updates An associative array of key->value pairs to send to the tracker
  16. * @param boolean $ToIRC Sends a message to the channel #tracker with the GET URL.
  17. */
  18. public static function update_tracker($Action, $Updates, $ToIRC = false)
  19. {
  20. // Build request
  21. $Get = TRACKER_SECRET . "/update?action=$Action";
  22. foreach ($Updates as $Key => $Value) {
  23. $Get .= "&$Key=$Value";
  24. }
  25. $MaxAttempts = 3;
  26. // don't wait around if we're debugging
  27. if (DEBUG_MODE) {
  28. $MaxAttempts = 1;
  29. }
  30. $Err = false;
  31. if (self::send_request($Get, $MaxAttempts, $Err) === false) {
  32. send_irc(DEBUG_CHAN, "$MaxAttempts $Err $Get");
  33. if (G::$Cache->get_value('ocelot_error_reported') === false) {
  34. send_irc(ADMIN_CHAN, "Failed to update Ocelot: $Err $Get");
  35. G::$Cache->cache_value('ocelot_error_reported', true, 3600);
  36. }
  37. return false;
  38. }
  39. return true;
  40. }
  41. /**
  42. * Get global peer stats from the tracker
  43. *
  44. * @return array(0 => $Leeching, 1 => $Seeding) or false if request failed
  45. */
  46. public static function global_peer_count()
  47. {
  48. $Stats = self::get_stats(self::STATS_MAIN);
  49. if (isset($Stats['leechers tracked']) && isset($Stats['seeders tracked'])) {
  50. $Leechers = $Stats['leechers tracked'];
  51. $Seeders = $Stats['seeders tracked'];
  52. } else {
  53. return false;
  54. }
  55. return array($Leechers, $Seeders);
  56. }
  57. /**
  58. * Get peer stats for a user from the tracker
  59. *
  60. * @param string $TorrentPass The user's pass key
  61. * @return array(0 => $Leeching, 1 => $Seeding) or false if the request failed
  62. */
  63. public static function user_peer_count($TorrentPass)
  64. {
  65. $Stats = self::get_stats(self::STATS_USER, array('key' => $TorrentPass));
  66. if ($Stats === false) {
  67. return false;
  68. }
  69. if (isset($Stats['leeching']) && isset($Stats['seeding'])) {
  70. $Leeching = $Stats['leeching'];
  71. $Seeding = $Stats['seeding'];
  72. } else {
  73. // User doesn't exist, but don't tell anyone
  74. $Leeching = $Seeding = 0;
  75. }
  76. return array($Leeching, $Seeding);
  77. }
  78. /**
  79. * Get whatever info the tracker has to report
  80. *
  81. * @return results from get_stats()
  82. */
  83. public static function info()
  84. {
  85. return self::get_stats(self::STATS_MAIN);
  86. }
  87. /**
  88. * Send a stats request to the tracker and process the results
  89. *
  90. * @param int $Type Stats type to get
  91. * @param array $Params Parameters required by stats type
  92. * @return array with stats in named keys or false if the request failed
  93. */
  94. private static function get_stats($Type, $Params = false)
  95. {
  96. if (!defined('TRACKER_REPORTKEY')) {
  97. return false;
  98. }
  99. $Get = TRACKER_REPORTKEY . '/report?';
  100. if ($Type === self::STATS_MAIN) {
  101. $Get .= 'get=stats';
  102. } elseif ($Type === self::STATS_USER && !empty($Params['key'])) {
  103. $Get .= "get=user&key=$Params[key]";
  104. } else {
  105. return false;
  106. }
  107. $Response = self::send_request($Get);
  108. if ($Response === false) {
  109. return false;
  110. }
  111. $Stats = [];
  112. foreach (explode("\n", $Response) as $Stat) {
  113. list($Val, $Key) = explode(" ", $Stat, 2);
  114. $Stats[$Key] = $Val;
  115. }
  116. return $Stats;
  117. }
  118. /**
  119. * Send a request to the tracker
  120. *
  121. * @param string $Path GET string to send to the tracker
  122. * @param int $MaxAttempts Maximum number of failed attempts before giving up
  123. * @param $Err Variable to use as storage for the error string if the request fails
  124. * @return tracker response message or false if the request failed
  125. */
  126. private static function send_request($Get, $MaxAttempts = 1, &$Err = false)
  127. {
  128. $Header = "GET /$Get HTTP/1.1\r\nConnection: Close\r\n\r\n";
  129. $Attempts = 0;
  130. $Sleep = 0;
  131. $Success = false;
  132. $StartTime = microtime(true);
  133. while (!$Success && $Attempts++ < $MaxAttempts) {
  134. if ($Sleep) {
  135. sleep($Sleep);
  136. }
  137. // spend some time retrying if we're not in DEBUG_MODE
  138. if (!DEBUG_MODE) {
  139. $Sleep = 6;
  140. }
  141. // Send request
  142. $File = fsockopen(TRACKER_HOST, TRACKER_PORT, $ErrorNum, $ErrorString);
  143. if ($File) {
  144. if (fwrite($File, $Header) === false) {
  145. $Err = "Failed to fwrite()";
  146. $Sleep = 3;
  147. continue;
  148. }
  149. } else {
  150. $Err = "Failed to fsockopen() - $ErrorNum - $ErrorString";
  151. continue;
  152. }
  153. // Check for response.
  154. $Response = '';
  155. while (!feof($File)) {
  156. $Response .= fread($File, 1024);
  157. }
  158. $DataStart = strpos($Response, "\r\n\r\n") + 4;
  159. $DataEnd = strrpos($Response, "\n");
  160. if ($DataEnd > $DataStart) {
  161. $Data = substr($Response, $DataStart, $DataEnd - $DataStart);
  162. } else {
  163. $Data = "";
  164. }
  165. $Status = substr($Response, $DataEnd + 1);
  166. if ($Status == "success") {
  167. $Success = true;
  168. }
  169. }
  170. $Request = array(
  171. 'path' => substr($Get, strpos($Get, '/')),
  172. 'response' => ($Success ? $Data : $Response),
  173. 'status' => ($Success ? 'ok' : 'failed'),
  174. 'time' => 1000 * (microtime(true) - $StartTime)
  175. );
  176. self::$Requests[] = $Request;
  177. if ($Success) {
  178. return $Data;
  179. }
  180. return false;
  181. }
  182. }