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.7KB

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