Guest User

Untitled

a guest
Dec 28th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.71 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. define('IN_TRACKER', true);
  5. define('BB_ROOT', './../');
  6. require(BB_ROOT .'common.php');
  7.  
  8. //var_dump($userdata);
  9. if (empty($_SERVER['HTTP_USER_AGENT']))
  10. {
  11.     header('Location: http://127.0.0.1', true, 301);
  12.     die;
  13. }
  14.  
  15. // Ignore 'completed' event
  16. if (isset($_GET['event']) && $_GET['event'] === 'completed')
  17. {
  18.     if (DBG_LOG) dbg_log(' ', '!die-event-completed');
  19.     dummy_exit(mt_rand(600, 1200));
  20. }
  21.  
  22. $announce_interval = $bb_cfg['announce_interval'];
  23. $passkey_key = $bb_cfg['passkey_key'];
  24. $max_left_val      = 536870912000;   // 500 GB
  25. $max_up_down_val   = 5497558138880;  // 5 TB
  26. $max_up_add_val    = 85899345920;    // 80 GB
  27. $max_down_add_val  = 85899345920;    // 80 GB
  28.  
  29. // Recover info_hash
  30. if (isset($_GET['?info_hash']) && !isset($_GET['info_hash']))
  31. {
  32.     $_GET['info_hash'] = $_GET['?info_hash'];
  33. }
  34.  
  35. // Initial request verification
  36. if (strpos($_SERVER['REQUEST_URI'], 'scrape') !== false)
  37. {
  38.     msg_die('Please disable SCRAPE!');
  39. }
  40. if (!isset($_GET[$passkey_key]) || !is_string($_GET[$passkey_key]) || strlen($_GET[$passkey_key]) != BT_AUTH_KEY_LENGTH)
  41. {
  42.     msg_die('Please LOG IN and REDOWNLOAD this torrent (passkey not found)');
  43. }
  44.  
  45. // Input var names
  46. // String
  47. $input_vars_str = array(
  48.     'info_hash',
  49.     'peer_id',
  50.     'event',
  51.     $passkey_key,
  52. );
  53. // Numeric
  54. $input_vars_num = array(
  55.     'port',
  56.     'uploaded',
  57.     'downloaded',
  58.     'left',
  59.     'numwant',
  60.     'compact',
  61. );
  62.  
  63. // Init received data
  64. // String
  65. foreach ($input_vars_str as $var_name)
  66. {
  67.     $$var_name = isset($_GET[$var_name]) ? (string) $_GET[$var_name] : null;
  68. }
  69. // Numeric
  70. foreach ($input_vars_num as $var_name)
  71. {
  72.     $$var_name = isset($_GET[$var_name]) ? (float) $_GET[$var_name] : null;
  73. }
  74. // Passkey
  75. $passkey = isset($$passkey_key) ? $$passkey_key : null;
  76.  
  77. // Verify request
  78. // Required params (info_hash, peer_id, port, uploaded, downloaded, left, passkey)
  79. if (!isset($info_hash) || strlen($info_hash) != 20)
  80. {
  81.     msg_die('Invalid info_hash');
  82. }
  83. if (!isset($peer_id) || strlen($peer_id) != 20)
  84. {
  85.     msg_die('Invalid peer_id');
  86. }
  87. if (!isset($port) || $port < 0 || $port > 0xFFFF)
  88. {
  89.     msg_die('Invalid port');
  90. }
  91. if (!isset($uploaded) || $uploaded < 0 || $uploaded > $max_up_down_val || $uploaded == 1844674407370)
  92. {
  93.     msg_die('Invalid uploaded value');
  94. }
  95. if (!isset($downloaded) || $downloaded < 0 || $downloaded > $max_up_down_val || $downloaded == 1844674407370)
  96. {
  97.     msg_die('Invalid downloaded value');
  98. }
  99. if (!isset($left) || $left < 0 || $left > $max_left_val)
  100. {
  101.     msg_die('Invalid left value');
  102. }
  103. if (!verify_id($passkey, BT_AUTH_KEY_LENGTH))
  104. {
  105.     msg_die('Invalid passkey');
  106. }
  107.  
  108. // IP
  109. $ip = $_SERVER['REMOTE_ADDR'];
  110.  
  111. if (!$bb_cfg['ignore_reported_ip'] && isset($_GET['ip']) && $ip !== $_GET['ip'])
  112. {
  113.     if (!$bb_cfg['verify_reported_ip'])
  114.     {
  115.         $ip = $_GET['ip'];
  116.     }
  117.     elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches))
  118.     {
  119.         foreach ($matches[0] as $x_ip)
  120.         {
  121.             if ($x_ip === $_GET['ip'])
  122.             {
  123.                 if (!$bb_cfg['allow_internal_ip'] && preg_match("#^(10|172\.16|192\.168)\.#", $x_ip))
  124.                 {
  125.                     break;
  126.                 }
  127.                 $ip = $x_ip;
  128.                 break;
  129.             }
  130.         }
  131.     }
  132. }
  133. // Check that IP format is valid
  134. if (!verify_ip($ip))
  135. {
  136.     msg_die("Invalid IP: $ip");
  137. }
  138. // Convert IP to HEX format
  139. $ip_sql = encode_ip($ip);
  140.  
  141. // Peer unique id
  142. $peer_hash = md5(
  143.     rtrim($info_hash, ' ') . $passkey . $ip . $port
  144. );
  145.  
  146. // Get cached peer info from previous announce (last peer info)
  147. $lp_info = CACHE('tr_cache')->get(PEER_HASH_PREFIX . $peer_hash);
  148.  
  149. if (DBG_LOG) dbg_log(' ', '$lp_info-get_from-CACHE-'. ($lp_info ? 'hit' : 'miss'));
  150.  
  151. // Drop fast announce
  152. if ($lp_info && (!isset($event) || $event !== 'stopped'))
  153. {
  154.     drop_fast_announce($lp_info);
  155. }
  156.  
  157. // Functions
  158. function drop_fast_announce ($lp_info)
  159. {
  160.     global $announce_interval;
  161.  
  162.     if ($lp_info['update_time'] < (TIMENOW - $announce_interval + 60))
  163.     {
  164.         return;  // if announce interval correct
  165.     }
  166.  
  167.     $new_ann_intrv = $lp_info['update_time'] + $announce_interval - TIMENOW;
  168.  
  169.     dummy_exit($new_ann_intrv);
  170. }
  171.  
  172. function msg_die ($msg)
  173. {
  174.     if (DBG_LOG) dbg_log(' ', '!die-'. clean_filename($msg));
  175.  
  176.     $output = bencode(array(
  177. #       'interval'        => (int) 1800,
  178.         'min interval'    => (int) 1800,
  179. #       'peers'           => (string) DUMMY_PEER,
  180.         'failure reason'  => (string) $msg,
  181.         'warning message' => (string) $msg,
  182.     ));
  183.  
  184.     die($output);
  185. }
  186.  
  187. # $agent = !empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '-';
  188. # bb_log("$agent  |  ". str_compact($peer_id) ."\n", 'agent');
  189.  
  190. // Start announcer
  191. define('TR_ROOT', './');
  192. require(TR_ROOT .'includes/init_tr.php');
  193.  
  194. $seeder  = ($left == 0) ? 1 : 0;
  195. $stopped = ($event === 'stopped');
  196.  
  197.  
  198.  
  199.  
  200. // Stopped event
  201. if ($stopped)
  202. {
  203.     CACHE('tr_cache')->rm(PEER_HASH_PREFIX . $peer_hash);
  204.     if (DBG_LOG) dbg_log(' ', 'stopped');
  205. }
  206.  
  207. // Get last peer info from DB
  208. if (!CACHE('tr_cache')->used && !$lp_info)
  209. {
  210.     $lp_info = DB()->fetch_row("
  211.         SELECT * FROM ". BB_BT_TRACKER ." WHERE peer_hash = '$peer_hash' LIMIT 1
  212.     ");
  213.  
  214.     if (DBG_LOG) dbg_log(' ', '$lp_info-get_from-DB-'. ($lp_info ? 'hit' : 'miss'));
  215. }
  216.  
  217. if ($lp_info)
  218. {
  219.     if (!$stopped)
  220.     {
  221.         drop_fast_announce($lp_info);
  222.     }
  223.  
  224.     $user_id  = $lp_info['user_id'];
  225.     $topic_id = $lp_info['topic_id'];
  226.     $releaser = $lp_info['releaser'];
  227.     $tor_type = $lp_info['tor_type'];
  228. }
  229. else
  230. {
  231.     // Verify if torrent registered on tracker and user authorized
  232.     $info_hash_sql = rtrim(DB()->escape($info_hash), ' ');
  233.     $passkey_sql   = DB()->escape($passkey);
  234.  
  235.     $sql = "
  236.         SELECT tor.topic_id, tor.poster_id, tor.tor_type, u.*
  237.         FROM ". BB_BT_TORRENTS ." tor
  238.         LEFT JOIN ". BB_BT_USERS ." u ON u.auth_key = '$passkey_sql'
  239.         WHERE tor.info_hash = '$info_hash_sql'
  240.         LIMIT 1
  241.     ";
  242.  
  243.     $row = DB()->fetch_row($sql);
  244.  
  245.     if (empty($row['topic_id']))
  246.     {
  247.         msg_die('Torrent not registered, info_hash = ' . bin2hex($info_hash_sql));
  248.     }
  249.     if (empty($row['user_id']))
  250.     {
  251.         msg_die('Please LOG IN and REDOWNLOAD this torrent (user not found)');
  252.     }
  253.  
  254.     $user_id  = $row['user_id'];
  255.     $topic_id = $row['topic_id'];
  256.     $releaser = (int) ($user_id == $row['poster_id']);
  257.     $tor_type = $row['tor_type'];
  258.  
  259.     // Ratio limits
  260.     if ((TR_RATING_LIMITS || $tr_cfg['limit_concurrent_ips']) && !$stopped)
  261.     {
  262.         $user_ratio = ($row['u_down_total'] && $row['u_down_total'] > MIN_DL_FOR_RATIO) ? ($row['u_up_total'] + $row['u_up_release'] + $row['u_up_bonus']) / $row['u_down_total'] : 1;
  263.         $rating_msg = '';
  264.  
  265.         if (!$seeder)
  266.         {
  267.            
  268.             $curr_limits = (isset($row['is_vip']) AND $row['is_vip']==1) ? $vip_limits : $rating_limits;
  269.            
  270.             foreach ($curr_limits as $ratio => $limit)
  271.             {
  272.                 if ($user_ratio < $ratio)
  273.                 {
  274.                     $tr_cfg['limit_active_tor'] = 1;
  275.                     $tr_cfg['limit_leech_count'] = $limit;
  276.                     $rating_msg = " (ratio < $ratio)";
  277.                     break;
  278.                 }
  279.             }
  280.         }
  281.  
  282.         // Limit active torrents
  283.         if (!isset($bb_cfg['unlimited_users'][$user_id]) && $tr_cfg['limit_active_tor'] && (($tr_cfg['limit_seed_count'] && $seeder) || ($tr_cfg['limit_leech_count'] && !$seeder)))
  284.         {
  285.             $sql = "SELECT COUNT(DISTINCT topic_id) AS active_torrents
  286.                 FROM ". BB_BT_TRACKER ."
  287.                 WHERE user_id = $user_id
  288.                     AND seeder = $seeder
  289.                     AND topic_id != $topic_id";
  290.  
  291.             if (!$seeder && $tr_cfg['leech_expire_factor'] && $user_ratio < 0.5)
  292.             {
  293.                 $sql .= " AND update_time > ". (TIMENOW - 60*$tr_cfg['leech_expire_factor']);
  294.             }
  295.             $sql .= "   GROUP BY user_id";
  296.  
  297.             if ($row = DB()->fetch_row($sql))
  298.             {
  299.                 if ($seeder && $tr_cfg['limit_seed_count'] && $row['active_torrents'] >= $tr_cfg['limit_seed_count'])
  300.                 {
  301.                     msg_die('Only '. $tr_cfg['limit_seed_count'] .' torrent(s) allowed for seeding');
  302.                 }
  303.                 elseif (!$seeder && $tr_cfg['limit_leech_count'] && $row['active_torrents'] >= $tr_cfg['limit_leech_count'])
  304.                 {
  305.                     msg_die('Only '. $tr_cfg['limit_leech_count'] .' torrent(s) allowed for leeching'. $rating_msg);
  306.                 }
  307.             }
  308.         }
  309.  
  310.         // Limit concurrent IPs
  311.         if ($tr_cfg['limit_concurrent_ips'] && (($tr_cfg['limit_seed_ips'] && $seeder) || ($tr_cfg['limit_leech_ips'] && !$seeder)))
  312.         {
  313.             $sql = "SELECT COUNT(DISTINCT ip) AS ips
  314.                 FROM ". BB_BT_TRACKER ."
  315.                 WHERE topic_id = $topic_id
  316.                     AND user_id = $user_id
  317.                     AND seeder = $seeder
  318.                     AND ip != '$ip_sql'";
  319.  
  320.             if (!$seeder && $tr_cfg['leech_expire_factor'])
  321.             {
  322.                 $sql .= " AND update_time > ". (TIMENOW - 60*$tr_cfg['leech_expire_factor']);
  323.             }
  324.             $sql .= "   GROUP BY topic_id";
  325.  
  326.             if ($row = DB()->fetch_row($sql))
  327.             {
  328.                 if ($seeder && $tr_cfg['limit_seed_ips'] && $row['ips'] >= $tr_cfg['limit_seed_ips'])
  329.                 {
  330.                     msg_die('You can seed only from '. $tr_cfg['limit_seed_ips'] ." IP's");
  331.                 }
  332.                 elseif (!$seeder && $tr_cfg['limit_leech_ips'] && $row['ips'] >= $tr_cfg['limit_leech_ips'])
  333.                 {
  334.                     msg_die('You can leech only from '. $tr_cfg['limit_leech_ips'] ." IP's");
  335.                 }
  336.             }
  337.         }
  338.     }
  339. }
  340.  
  341. // Up/Down speed
  342. $speed_up = $speed_down = 0;
  343.  
  344. if ($lp_info && $lp_info['update_time'] < TIMENOW)
  345. {
  346.     if ($uploaded > $lp_info['uploaded'])
  347.     {
  348.         $speed_up = ceil(($uploaded - $lp_info['uploaded']) / (TIMENOW - $lp_info['update_time']));
  349.     }
  350.     if ($downloaded > $lp_info['downloaded'])
  351.     {
  352.         $speed_down = ceil(($downloaded - $lp_info['downloaded']) / (TIMENOW - $lp_info['update_time']));
  353.     }
  354. }
  355.  
  356. // Up/Down addition
  357. $up_add = ($lp_info && $uploaded > $lp_info['uploaded']) ? $uploaded - $lp_info['uploaded'] : 0;
  358. $down_add = ($lp_info && $downloaded > $lp_info['downloaded']) ? $downloaded - $lp_info['downloaded'] : 0;
  359.  
  360. // Gold/Silver releases
  361. if ($tr_cfg['gold_silver_enabled'] && $down_add)
  362. {
  363.     if ($tor_type == TOR_TYPE_GOLD)
  364.     {
  365.         $down_add = 0;
  366.     }
  367.     // Silver releases
  368.     elseif ($tor_type == TOR_TYPE_SILVER)
  369.     {
  370.         $down_add = ceil($down_add/2);
  371.     }
  372. }
  373.  
  374. // Insert/update peer info
  375. $peer_info_updated = false;
  376. $update_time = ($stopped) ? 0 : TIMENOW;
  377.  
  378. if ($lp_info)
  379. {
  380.     $sql  = "UPDATE ". BB_BT_TRACKER ." SET update_time = $update_time";
  381.  
  382.     $sql .= ", seeder = $seeder";
  383.     $sql .= ($releaser != $lp_info['releaser']) ? ", releaser = $releaser" : '';
  384.  
  385.     $sql .= ($tor_type != $lp_info['tor_type']) ? ", tor_type = $tor_type" : '';
  386.  
  387.     $sql .= ($uploaded != $lp_info['uploaded']) ? ", uploaded = $uploaded" : '';
  388.    
  389.     $sql .= ($downloaded != $lp_info['downloaded']) ? ", downloaded = $downloaded" : '';
  390.     if(!empty($bb_cfg['goldday'])){
  391.         if ($bb_cfg['goldday'] == true ) {
  392.             $sql .= ($down_add) ? ", down_add = 0" : '';
  393.         }
  394.     }
  395.    
  396.     else {
  397.     $sql .= ($down_add) ? ", down_add = down_add + $down_add" : '';
  398.     }
  399.     $sql .= ", remain = $left";
  400.  
  401.     $sql .= ($up_add) ? ", up_add = up_add + $up_add" : '';
  402.     $sql .= ($down_add) ? ", down_add = down_add + $down_add" : '';
  403.    
  404.     $sql .= ($peer_id) ? ", peer_id = '".DB()->escape(strip_tags(preg_replace("/[^a-zA-Z0-9\-\_\.]/", '', $peer_id)))."'" : '';
  405.  
  406.     $sql .= ", speed_up = $speed_up";
  407.     $sql .= ", speed_down = $speed_down";
  408.  
  409.     $sql .= " WHERE peer_hash = '$peer_hash'";
  410.     $sql .= " LIMIT 1";
  411.  
  412.     DB()->query($sql);
  413.  
  414.     $peer_info_updated = DB()->affected_rows();
  415.  
  416.     if (DBG_LOG) dbg_log(' ', 'this_peer-update'. ($peer_info_updated ? '' : '-FAIL'));
  417. }
  418.  
  419. if (!$lp_info || !$peer_info_updated)
  420. {
  421.     $columns = 'peer_hash,    topic_id,  user_id, peer_id,  ip,       port,  seeder,  releaser, tor_type,  uploaded,  downloaded, remain, speed_up,  speed_down,  up_add,  down_add,  update_time';
  422.     $values = "'$peer_hash', $topic_id, $user_id, '".DB()->escape(strip_tags($peer_id))."',  '$ip_sql', $port, $seeder, $releaser, $tor_type, $uploaded, $downloaded, $left, $speed_up, $speed_down, $up_add, $down_add, $update_time";
  423.  
  424.     DB()->query("REPLACE INTO ". BB_BT_TRACKER ." ($columns) VALUES ($values)");
  425.  
  426.     if (DBG_LOG) dbg_log(' ', 'this_peer-insert');
  427. }
  428.  
  429. // Exit if stopped
  430. if ($stopped)
  431. {
  432.     silent_exit();
  433. }
  434.  
  435. // Store peer info in cache
  436. $lp_info = array(
  437.     'downloaded'  => (float) $downloaded,
  438.     'releaser'    => (int)   $releaser,
  439.     'seeder'      => (int)   $seeder,
  440.     'topic_id'    => (int)   $topic_id,
  441.     'update_time' => (int)   TIMENOW,
  442.     'uploaded'    => (float) $uploaded,
  443.     'user_id'     => (int)   $user_id,
  444.     'tor_type'    => (int)   $tor_type,
  445. );
  446.  
  447. $lp_info_cached = CACHE('tr_cache')->set(PEER_HASH_PREFIX . $peer_hash, $lp_info, PEER_HASH_EXPIRE);
  448.  
  449. if (DBG_LOG && !$lp_info_cached) dbg_log(' ', '$lp_info-caching-FAIL');
  450.  
  451. // Get cached output
  452. $output = CACHE('tr_cache')->get(PEERS_LIST_PREFIX . $topic_id);
  453.  
  454. if (DBG_LOG) dbg_log(' ', '$output-get_from-CACHE-'. ($output !== false ? 'hit' : 'miss'));
  455.  
  456. if (!$output)
  457. {
  458.     // Retrieve peers
  459.     $numwant      = (int) $tr_cfg['numwant'];
  460.     $compact_mode = ($tr_cfg['compact_mode'] || !empty($compact));
  461.  
  462.     $rowset = DB()->fetch_rowset("
  463.         SELECT ip, port
  464.         FROM ". BB_BT_TRACKER ."
  465.         WHERE topic_id = $topic_id
  466.         ORDER BY RAND()
  467.         LIMIT $numwant
  468.     ");
  469.  
  470.     if ($compact_mode)
  471.     {
  472.         $peers = '';
  473.  
  474.         foreach ($rowset as $peer)
  475.         {
  476.             $peers .= pack('Nn', ip2long(decode_ip($peer['ip'])), $peer['port']);
  477.         }
  478.     }
  479.     else
  480.     {
  481.         $peers = array();
  482.  
  483.         foreach ($rowset as $peer)
  484.         {
  485.             $peers[] = array(
  486.                 'ip'   => decode_ip($peer['ip']),
  487.                 'port' => intval($peer['port']),
  488.             );
  489.         }
  490.     }
  491.  
  492.     $seeders  = 0;
  493.     $leechers = 0;
  494.  
  495.     if ($tr_cfg['scrape'])
  496.     {
  497.         $row = DB()->fetch_row("
  498.             SELECT seeders, leechers
  499.             FROM ". BB_BT_TRACKER_SNAP ."
  500.             WHERE topic_id = $topic_id
  501.             LIMIT 1
  502.         ");
  503.  
  504.         $seeders  = $row['seeders'];
  505.         $leechers = $row['leechers'];
  506.     }
  507.  
  508.  
  509.     $output = array(
  510.         'interval'     => (int) $announce_interval,
  511.         'min interval' => (int) $announce_interval,
  512.         'peers'        => $peers,
  513.         'complete'     => (int) $seeders,
  514.         'incomplete'   => (int) $leechers,
  515.     );
  516.  
  517.     $peers_list_cached = CACHE('tr_cache')->set(PEERS_LIST_PREFIX . $topic_id, $output, PEERS_LIST_EXPIRE);
  518.  
  519.     if (DBG_LOG && !$peers_list_cached) dbg_log(' ', '$output-caching-FAIL');
  520. }
  521.  
  522. // Return data to client
  523. echo bencode($output);
  524.  
  525. tracker_exit();
  526. exit;
Advertisement
Add Comment
Please, Sign In to add comment