Oppaitime'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 5.7KB

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