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

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