分享

用Bittorrent和PHP搭建BT Tracker服务器和发布页

 cankoa 2016-12-15
  • <?
  • // be cautious with this !
  • require_once("bittorrent.inc.php");
  • require_once("bencode.php");
  •  
  • function url_validate( $link,$id )
  • {
  • $url_parts = @parse_url( $link );
  •  
  • if ( empty( $url_parts["host"] ) ) return( false );
  •  
  • if ( !empty( $url_parts["path"] ) )
  • {
  • $documentpath = $url_parts["path"];
  • //if (substr($documentpath, -6)=="scrape") { $documentpath.="/";}
  • }
  • else
  • {
  • $documentpath = "/";
  • }
  •  
  • if ( !empty( $url_parts["query"] ) )
  • {
  • $documentpath .= "?" . $url_parts["query"];
  • }
  •  
  • $host = $url_parts["host"];
  • $port = $url_parts["port"];
  • // Now (HTTP-)GET $documentpath at $host";
  •  
  • if (empty( $port ) ) $port = "80";
  • $socket = @fsockopen( $host, $port, $errno, $errstr, 30 );
  •  
  • if (!$socket)
  • {
  • return(false);
  • mysql_query("UPDATE torrents SET reason=\"Tracker seems to be dead,so it was skipped\" WHERE id = $id");
  • mysql_query("UPDATE torrents SET skip=\"yes\" WHERE id = $id");
  • }
  • else
  • {
  • // change this value if you want : 1s is enough for fast trackers, else it will slow too much bytestats
  • stream_set_timeout($socket, 1);
  • fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
  • $http_response = fgets( $socket, 22 );
  •  
  • // if ( ereg("200", $http_response, $regs ) || ereg("302", $http_response, $regs ) )
  • //{
  • mysql_query("UPDATE torrents SET reason=\"This torrent is working fine\" WHERE id = $id");
  • mysql_query("UPDATE torrents SET skip=\"no\" WHERE id = $id");
  • return(true);
  • fclose( $socket );
  • // } else
  • //{
  • //echo "HTTP-Response: $http_response<br>";
  • // return(false);
  • //mysql_query("UPDATE torrents SET reason=\"Tracker gave an abnormal answer,so it was skipped\" WHERE id = $id");
  • //mysql_query("UPDATE torrents SET skip=\"yes\" WHERE id = $id");
  • // }
  • }
  • }
  •  
  •  
  • function hex2bin($hex)
  • {
  • $result = '';
  • for ($i = 0; $i < strlen($hex); $i += 2)
  • $result .= chr(hexdec(substr($hex,$i,2)));
  • return $result;
  • }
  •  
  • function hexesc($matches) {
  • return sprintf("%02x", ord($matches[0]));
  • }
  •  
  •  
  •  
  •  
  • function docleanup() {
  • global $torrent_dir, $signup_timeout, $max_dead_torrent_time;
  •  
  •  
  • do {
  • $res = mysql_query("SELECT id FROM torrents");
  • $ar = array();
  • while ($row = mysql_fetch_array($res)) {
  • $id = $row[0];
  • $ar[$id] = 1;
  • }
  •  
  • if (!count($ar))
  • break;
  •  
  • $dp = @opendir($torrent_dir);
  • if (!$dp)
  • break;
  •  
  • $ar2 = array();
  • while (($file = readdir($dp)) !== false) {
  • if (!preg_match('/^(\d+)\.torrent$/', $file, $m))
  • continue;
  • $id = $m[1];
  • $ar2[$id] = 1;
  • if (isset($ar[$id]) && $ar[$id])
  • continue;
  • $ff = $torrent_dir . "/$file";
  • unlink($ff);
  • }
  • closedir($dp);
  •  
  • if (!count($ar2))
  • break;
  •  
  • $delids = array();
  • foreach (array_keys($ar) as $k) {
  • if (isset($ar2[$k]) && $ar2[$k])
  • continue;
  • $delids[] = $k;
  • unset($ar[$k]);
  • }
  • if (count($delids))
  • mysql_query("DELETE FROM torrents WHERE id IN (" . join(",", $delids) . ")");
  •  
  • $res = mysql_query("SELECT torrent FROM peers GROUP BY torrent");
  • $delids = array();
  • while ($row = mysql_fetch_array($res)) {
  • $id = $row[0];
  • if (isset($ar[$id]) && $ar[$id])
  • continue;
  • $delids[] = $id;
  • }
  • if (count($delids))
  • mysql_query("DELETE FROM peers WHERE torrent IN (" . join(",", $delids) . ")");
  •  
  • $res = mysql_query("SELECT torrent FROM files GROUP BY torrent");
  • $delids = array();
  • while ($row = mysql_fetch_array($res)) {
  • $id = $row[0];
  • if (@$ar[$id])
  • continue;
  • $delids[] = $id;
  • }
  • if (count($delids))
  • mysql_query("DELETE FROM files WHERE torrent IN (" . join(",", $delids) . ")");
  • } while (0);
  •  
  • $deadtime = deadtime();
  • mysql_query("DELETE FROM peers WHERE last_action < FROM_UNIXTIME($deadtime)");
  •  
  • $deadtime -= $max_dead_torrent_time;
  • mysql_query("UPDATE torrents SET visible='no' WHERE visible='yes' AND last_action < FROM_UNIXTIME($deadtime)");
  •  
  • $deadtime = time() - $signup_timeout;
  • mysql_query("DELETE FROM users WHERE status = 'pending' AND added < FROM_UNIXTIME($deadtime) AND last_login < FROM_UNIXTIME($deadtime) AND last_access < FROM_UNIXTIME($deadtime)");
  •  
  •  
  • $torrents = array();
  • $res = mysql_query("SELECT torrent, seeder, COUNT(*) AS c FROM peers GROUP BY torrent, seeder");
  • while ($row = mysql_fetch_assoc($res)) {
  • if ($row["seeder"] == "yes")
  • $key = "seeders";
  • else
  • $key = "leechers";
  • $torrents[$row["torrent"]][$key] = $row["c"];
  • }
  •  
  • $res = mysql_query("SELECT torrent, COUNT(*) AS c FROM comments GROUP BY torrent");
  • while ($row = mysql_fetch_assoc($res)) {
  • $torrents[$row["torrent"]]["comments"] = $row["c"];
  • }
  • // i thing it is here that modifications need to be made !
  • $fields = explode(":", "comments:leechers:seeders");
  • $res = mysql_query("SELECT id, seeders, leechers, comments,scrape_url FROM torrents");
  • if (mysql_num_rows($res)) while ($row = mysql_fetch_assoc($res)) {
  • $id = $row["id"];
  • $torr = @$torrents[$id];
  • foreach ($fields as $field) {
  • if (!isset($torr[$field]))
  • $torr[$field] = 0;
  • }
  • $update = array();
  • foreach ($fields as $field) {
  • if ($torr[$field] != $row[$field])
  • $update[] = "$field = " . $torr[$field];
  • }
  • // temporary fix :(
  • if ( ereg($tracker_url,$row["scrape_url"]) )
  • {
  • if (count($update))
  • mysql_query("UPDATE torrents SET " . implode(",", $update) . " WHERE id = $id");
  • }
  •  
  • }
  •  
  • $sql = 'SELECT id FROM torrents WHERE skip = "no" ';
  •  
  • // on envoie la requete
  • $req = mysql_query($sql) ;
  •  
  • // on fait une boucle qui va faire un tour pour chaque enregistrements
  • while($data = mysql_fetch_array($req))
  • {
  • $id = $data["id"];
  •  
  • $row[0] = '?' ;
  • $row[1] = '?' ;
  • // on affiche les informations de l'enregistrements en cours
  • $sql2 = "SELECT info_hash,scrape_url FROM torrents WHERE id = $id";
  • $req2 = mysql_query($sql2) ;
  • $dat = mysql_fetch_array($req2);
  • $infohash = preg_replace_callback('/./s', "hexesc", hash_pad($dat["info_hash"]));
  • if ( ereg($tracker_url,$dat["scrape_url"]) )
  • {
  • $max_cache_file_age = '360';
  • // $scrape = file_get_contents('http://torrentstats./bytestats.php?tracker='.$dat["scrape_url"].'&infohash='.$hash);
  • $bencode = new BEncodeLib();
  • // some optimisations can be made here : do not get twice or more the same scrape file from the same tracker ...
  •  
  • $tracker_md5 = $infohash;
  • $cache_age = @time() - @filemtime('temp/'.$tracker_md5);
  • if(file_exists('temp/'.$tracker_md5) && $cache_age < $max_cache_file_age) {
  •  
  • $scrape = @file_get_contents('temp/'.$tracker_md5, 'w+');
  • $scrape = gzuncompress($scrape);
  • @fclose($open);
  • mysql_query("UPDATE torrents SET reason=\"This torrent is working fine\" WHERE id = $id");
  • mysql_query("UPDATE torrents SET visible=\"yes\" WHERE id = $id");
  • $info = $bencode->bdecode($scrape);
  •  
  • $torrentseeds = $info['files'][hex2bin($infohash)]['complete'];
  • $torrentpeers = $info['files'][hex2bin($infohash)]['incomplete'];
  • // debug purposes only echo "<br>".$dat["scrape_url"]." - $infohash - $torrentseeds - $torrentpeers";
  •  
  •  
  • mysql_query("UPDATE torrents SET seeders=$torrentseeds WHERE id = $id");
  •  
  • mysql_query("UPDATE torrents SET leechers=$torrentpeers WHERE id = $id");
  • }
  • if(file_exists('temp/'.$tracker_md5) && $cache_age > $max_cache_file_age) {
  • if (url_validate($dat["scrape_url"],$id))
  • {
  • $scrape = @file_get_contents($dat["scrape_url"],'r');
  • $refresh='1';
  • if(strlen($scrape) > 10) {
  • $save = @fopen('temp/'.$tracker_md5, 'w+',1);
  • @fwrite($save, gzcompress($scrape,9));
  • @fclose($save);
  • mysql_query("UPDATE torrents SET reason=\"This torrent is working fine\" WHERE id = $id");
  • mysql_query("UPDATE torrents SET visible=\"yes\" WHERE id = $id");
  • $info = $bencode->bdecode($scrape);
  •  
  • $torrentseeds = $info['files'][hex2bin($infohash)]['complete'];
  • $torrentpeers = $info['files'][hex2bin($infohash)]['incomplete'];
  • // debug purposes only echo "<br>".$dat["scrape_url"]." - $infohash - $torrentseeds - $torrentpeers";
  •  
  •  
  • mysql_query("UPDATE torrents SET seeders=$torrentseeds WHERE id = $id");
  •  
  • mysql_query("UPDATE torrents SET leechers=$torrentpeers WHERE id = $id");
  • }
  • }
  • else { mysql_query("UPDATE torrents SET reason=\"Tracker seems to be dead,so it was skipped\" WHERE id = $id"); }
  • }
  • if(!file_exists('temp/'.$tracker_md5)) {
  • if (url_validate($dat["scrape_url"],$id))
  • {
  • $scrape = @file_get_contents($dat["scrape_url"],'r');
  • $refresh='1';
  • if(strlen($scrape) > 10) {
  • $save = @fopen('temp/'.$tracker_md5, 'w+');
  • @fwrite($save, gzcompress($scrape,9));
  • @fclose($save);
  • mysql_query("UPDATE torrents SET reason=\"This torrent is working fine\" WHERE id = $id");
  • mysql_query("UPDATE torrents SET visible=\"yes\" WHERE id = $id");
  • $info = $bencode->bdecode($scrape);
  •  
  • $torrentseeds = $info['files'][hex2bin($infohash)]['complete'];
  • $torrentpeers = $info['files'][hex2bin($infohash)]['incomplete'];
  • // debug purposes only echo "<br>".$dat["scrape_url"]." - $infohash - $torrentseeds - $torrentpeers";
  •  
  •  
  • mysql_query("UPDATE torrents SET seeders=$torrentseeds WHERE id = $id");
  •  
  • mysql_query("UPDATE torrents SET leechers=$torrentpeers WHERE id = $id");
  •  
  • }
  • else { mysql_query("UPDATE torrents SET reason=\"Tracker seems to be dead,so it was skipped\" WHERE id = $id"); }
  • }
  •  
  •  
  •  
  •  
  •  
  • }
  •  
  • }
  •  
  •  
  • }
  • }
  • ?>
    • 本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
      转藏 分享 献花(0

      0条评论

      发表

      请遵守用户 评论公约