From 5c4c2ccf8f58138c6449feaa778a0e10f09d0b2a Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Wed, 9 Jun 2021 02:23:09 +0800 Subject: [PATCH] fix rss download link + migrate bencode to rhilip/bencode --- app/Console/Commands/Test.php | 3 +- include/functions_announce.php | 4 +- include/globalfunctions.php | 31 ++++++ public/announce.php | 86 +++++++--------- public/download.php | 29 +++--- public/scrape.php | 17 +-- public/takeupload.php | 158 ++++++++++------------------ public/torrent_info.php | 183 +++++++-------------------------- public/torrentrss.php | 3 +- 9 files changed, 185 insertions(+), 329 deletions(-) diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index 38497a2a..75659466 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -52,8 +52,7 @@ class Test extends Command */ public function handle() { - $file = storage_path('logs/11270.torrent'); - $r = Bencode::load($file); + $r = 'https://hdtime.org/download.php?downhash=' . urlencode('1|Roqd'); dd($r); } diff --git a/include/functions_announce.php b/include/functions_announce.php index d9eb0770..b402d538 100644 --- a/include/functions_announce.php +++ b/include/functions_announce.php @@ -44,7 +44,7 @@ function block_browser() function benc_resp($d) { - benc_resp_raw(benc(array('type' => 'dictionary', 'value' => $d))); + benc_resp_raw(\Rhilip\Bencode\Bencode::encode($d)); } function benc_resp_raw($x) { do_log($x); @@ -60,7 +60,7 @@ function benc_resp_raw($x) { } function err($msg, $userid = 0, $torrentid = 0) { - benc_resp(array('failure reason' => array('type' => 'string', 'value' => $msg))); + benc_resp(['failure reason' => $msg]); exit(); } function check_cheater($userid, $torrentid, $uploaded, $downloaded, $anctime, $seeders=0, $leechers=0){ diff --git a/include/globalfunctions.php b/include/globalfunctions.php index 81ebc0b5..f8e5b7d2 100644 --- a/include/globalfunctions.php +++ b/include/globalfunctions.php @@ -584,3 +584,34 @@ function isRunningInConsole(): bool { return php_sapi_name() == 'cli'; } + +function get_base_announce_url() +{ + global $https_announce_urls, $announce_urls; + $httpsAnnounceUrls = array_filter($https_announce_urls); + $log = "cookie: " . json_encode($_COOKIE) . ", https_announce_urls: " . json_encode($httpsAnnounceUrls); + if ((isset($_COOKIE["c_secure_tracker_ssl"]) && $_COOKIE["c_secure_tracker_ssl"] == base64("yeah")) || !empty($httpsAnnounceUrls)) { + $log .= ", c_secure_tracker_ssl = base64('yeah'): " . base64("yeah") . ", or not empty https_announce_urls"; + $tracker_ssl = true; + } else { + $tracker_ssl = false; + } + $log .= ", tracker_ssl: $tracker_ssl"; + + if ($tracker_ssl == true){ + $ssl_torrent = "https://"; + if ($https_announce_urls[0] != "") { + $log .= ", https_announce_urls not empty, use it"; + $base_announce_url = $https_announce_urls[0]; + } else { + $log .= ", https_announce_urls empty, use announce_urls[0]"; + $base_announce_url = $announce_urls[0]; + } + } + else{ + $ssl_torrent = "http://"; + $base_announce_url = $announce_urls[0]; + } + do_log($log); + return $ssl_torrent . $base_announce_url; +} diff --git a/public/announce.php b/public/announce.php index 48f850df..977fd06d 100644 --- a/public/announce.php +++ b/public/announce.php @@ -1,6 +1,6 @@ $announce_wait) && (TIMENOW - $torrent['ts']) >= ($annintertwoage * 86400)) $real_annnounce_interval = $annintertwo; -$resp = "d" . benc_str("interval") . "i" . $real_annnounce_interval . "e" . benc_str("min interval") . "i" . $announce_wait . "e". benc_str("complete") . "i" . $torrent["seeders"] . "e" . benc_str("incomplete") . "i" . $torrent["leechers"] . "e" . benc_str("peers"); +//$resp = "d" . benc_str("interval") . "i" . $real_annnounce_interval . "e" . benc_str("min interval") . "i" . $announce_wait . "e". benc_str("complete") . "i" . $torrent["seeders"] . "e" . benc_str("incomplete") . "i" . $torrent["leechers"] . "e" . benc_str("peers"); +$rep_dict = [ + "interval" => (int)$real_annnounce_interval, + "min interval" => (int)$announce_wait, + "complete" => (int)$torrent["seeders"], + "incomplete" => (int)$torrent["leechers"], + "peers" => [] // By default it is a array object, only when `&compact=1` then it should be a string +]; + +if ($compact == 1) { + $rep_dict['peers'] = ''; // Change `peers` from array to string + $rep_dict['peers6'] = ''; // If peer use IPv6 address , we should add packed string in `peers6` +} -$peer_list = ""; -$peer6_list = ""; unset($self); -// bencoding the peers info get for this announce -while ($row = mysql_fetch_assoc($res)) -{ - $row["peer_id"] = hash_pad($row["peer_id"]); - // $peer_id is the announcer's peer_id while $row["peer_id"] is randomly selected from the peers table - if ($row["peer_id"] === $peer_id) - { - $self = $row; - continue; - } - if ($compact == 1){ - $longip = ip2long($row['ip']); - if ($longip) //Ignore ipv6 address - $peer_list .= pack("Nn", sprintf("%d",$longip), $row['port']); - else - { - $ipv6_packed = inet_pton($row['ip']); - if ($ipv6_packed) - $peer6_list .= $ipv6_packed . pack("n", $row['port']); +if (isset($event) && $event == "stopped") { + // Don't fetch peers for stopped event +} else { + // bencoding the peers info get for this announce + while ($row = mysql_fetch_assoc($res)) { + $row["peer_id"] = hash_pad($row["peer_id"]); + + // $peer_id is the announcer's peer_id while $row["peer_id"] is randomly selected from the peers table + if ($row["peer_id"] === $peer_id) { + $self = $row; + continue; + } + + if ($compact == 1) { + $peerField = filter_var($row['ip'],FILTER_VALIDATE_IP,FILTER_FLAG_IPV6) ? 'peers6' : 'peers'; + $rep_dict[$peerField] .= inet_pton($row["ip"]) . pack("n", $row["port"]); + } else { + $peer = [ + 'ip' => $row["ip"], + 'port' => (int) $row["port"] + ]; + + if ($no_peer_id == 1) $peer['peer id'] = $row["peer_id"]; + $rep_dict['peers'][] = $peer; } } - elseif ($no_peer_id == 1) - $peer_list .= "d" . - benc_str("ip") . benc_str($row["ip"]) . - benc_str("port") . "i" . $row["port"] . "e" . - "e"; - else - $peer_list .= "d" . - benc_str("ip") . benc_str($row["ip"]) . - benc_str("peer id") . benc_str($row["peer_id"]) . - benc_str("port") . "i" . $row["port"] . "e" . - "e"; } -if ($compact == 1) - $resp .= benc_str($peer_list); -else - $resp .= "l".$peer_list."e"; - -if ($compact == 1 && strlen($peer6_list) > 0) -{ - $resp .= benc_str("peers6").benc_str($peer6_list); -} - -$resp .= "e"; $selfwhere = "torrent = $torrentid AND " . hash_where("peer_id", $peer_id); - //no found in the above random selection if (!isset($self)) { @@ -482,5 +474,5 @@ try { do_log("add exam progress fail: " . $exception->getMessage()); } -benc_resp_raw($resp); +benc_resp($rep_dict); ?> diff --git a/public/download.php b/public/download.php index 253c9a03..9b6e8731 100644 --- a/public/download.php +++ b/public/download.php @@ -64,23 +64,8 @@ if (@ini_get('output_handler') == 'ob_gzhandler' AND @ob_get_length() !== false) header('Content-Encoding:'); } */ -if ((isset($_COOKIE["c_secure_tracker_ssl"]) && $_COOKIE["c_secure_tracker_ssl"] == base64("yeah")) || !empty($https_announce_urls)) -$tracker_ssl = true; -else -$tracker_ssl = false; -if ($tracker_ssl == true){ - $ssl_torrent = "https://"; - if ($https_announce_urls[0] != "") - $base_announce_url = $https_announce_urls[0]; - else - $base_announce_url = $announce_urls[0]; -} -else{ - $ssl_torrent = "http://"; - $base_announce_url = $announce_urls[0]; -} - +$base_announce_url = get_base_announce_url(); $res = sql_query("SELECT torrents.name, torrents.filename, torrents.save_as, torrents.size, torrents.owner, torrents.banned, categories.mode as search_box_id FROM torrents left join categories on torrents.category = categories.id WHERE torrents.id = ".sqlesc($id)) or sqlerr(__FILE__, __LINE__); $row = mysql_fetch_assoc($res); @@ -97,7 +82,7 @@ if (($row['banned'] == 'yes' && get_user_class() < $seebanned_class) || !can_acc sql_query("UPDATE torrents SET hits = hits + 1 WHERE id = ".sqlesc($id)) or sqlerr(__FILE__, __LINE__); -require_once "include/benc.php"; +//require_once "include/benc.php"; if (strlen($CURUSER['passkey']) != 32) { $CURUSER['passkey'] = md5($CURUSER['username'].date("Y-m-d H:i:s").$CURUSER['passhash']); @@ -108,6 +93,16 @@ $trackerReportAuthKey = $torrentRep->getTrackerReportAuthKey($id, $CURUSER['id'] $dict = \Rhilip\Bencode\Bencode::load($fn); $dict['announce'] = $ssl_torrent . $base_announce_url . "?authkey=$trackerReportAuthKey"; +if (count($announce_urls) > 1) { + foreach ($announce_urls as $announce_url) { + /** d['announce-list'] = [[ tracker1, tracker2, tracker3 ]] */ + $dict['announce-list'][0][] = $ssl_torrent . $announce_url . "?authkey=$trackerReportAuthKey"; + + /** d['announce-list'] = [ [tracker1], [backup1], [backup2] ] */ + //$dict['announce-list'][] = [$ssl_torrent . $announce_url . "?passkey=" . $CURUSER['passkey']]; + } +} + //$dict = bdec_file($fn, $max_torrent_size); //$dict['value']['announce']['value'] = $ssl_torrent . $base_announce_url . "?passkey=$CURUSER[passkey]"; //$dict['value']['announce']['value'] = $ssl_torrent . $base_announce_url . "?authkey=$trackerReportAuthKey"; diff --git a/public/scrape.php b/public/scrape.php index 36c23c9c..b722846c 100644 --- a/public/scrape.php +++ b/public/scrape.php @@ -1,6 +1,6 @@ (int)$row['seeders'], + 'downloaded' => (int)$row['times_completed'], + 'incomplete' => (int)$row['leechers'] + ]; } -$r .= "ee"; -benc_resp_raw($r); +$d = ['files' => $torrent_details]; +benc_resp($d); diff --git a/public/takeupload.php b/public/takeupload.php index 1f8cdb48..4f6f22e5 100644 --- a/public/takeupload.php +++ b/public/takeupload.php @@ -1,5 +1,5 @@ "; - return; - } - - reset($array); - - switch($array['type'] ?? '') - { - case "string": - printf("
  • - [STRING] [%s] (%d): %s
  • ",$parent,$array['strlen'],$array['value']); - break; - case "integer": - printf("
  • - [INT] [%s] (%d): %s
  • ",$parent,$array['strlen'],$array['value']); - break; - case "list": - printf("
  • + [LIST] [%s] (%d)
    ",$parent,$array['strlen']); - echo "
  • "; - break; - case "dictionary": - printf("
  • + [DICT] [%s] (%d)
    ",$parent,$array['strlen']); - foreach ($array as $key => $val) - { - if (is_array($val)) - { - echo ""; - } - } - echo "
  • "; - - break; - default: - foreach ($array as $key => $val) - { - if (is_array($val)) - { - //echo $offset; - print_array($val, $offset_symbol, $offset, $key); - } - } - break; - - } - + $ret = ''; + foreach ($array as $item => $value) { + $value_length = strlen(\Rhilip\Bencode\Bencode::encode($value)); + if (is_iterable($value)) { // It may `dictionary` or `list` + $type = is_indexed_array($value) ? 'list' : 'dictionary'; + $ret .= "
  • "; + $ret .= "
  • "; + } else { // It may `interger` or `string` + $type = is_integer($value) ? 'integer' : 'string'; + $value = ($parent == 'info' && $item == 'pieces') ? "0x" . bin2hex(substr($value, 0, 25)) . "..." : $value; // Cut the info pieces.... + $ret .= "
  • - [" . $item . "] (" . ucfirst($type) . ") [" . $value_length . "]: " . $value . "
  • "; + } + } + return $ret; } dbconn(); @@ -110,104 +89,14 @@ li span.title {font-weight: bold;} begin_main_frame(); -// Heading -print("

    {$row['name']}

    "); - -$dict = bdec_file($fn, (1024*1024)); - -// Start table -print("
    "); - -$dict['value']['info']['value']['pieces']['value'] = "0x".bin2hex(substr($dict['value']['info']['value']['pieces']['value'], 0, 25))."..."; - - - -echo "
      "; -print_array($dict,"*", "", "root"); +$dict = \Rhilip\Bencode\Bencode::load($fn); +print("

      $row[name]

      "); // Heading +print("
      "); // Start table +echo "
        "; +echo torrent_structure_builder(['root' => $dict]); echo "
      "; +print("
      "); // End table -// End table - -print("
    "); - - -?> - - - - - - -encryptDownHash($row['id'], $user)); + $itemdlurl = $url."/download.php?id=".$row['id']."&downhash=" . rawurlencode( $user['id'] . '|'. $torrentRep->encryptDownHash($row['id'], $user)); else $itemdlurl = $url."/download.php?id=".$row['id']; if (!empty($_GET['icat'])) $title .= "[".$row['cat_name']."]"; $title .= $row['name'];