function killPeer($userid, $hash, $left, $assumepeer = false) { require "config.php"; if (!$assumepeer) { $peer = getPeerInfo($userid, $hash); if (!$peer) { return; } if ($left != $peer["bytes"]) { $bytes = sqlSubtract($peer["bytes"], $left); } else { $bytes = 0; } } else { $bytes = 0; $peer = $assumepeer; } quickQuery("DELETE FROM " . $prefix . "x{$hash} WHERE peer_id=\"{$userid}\""); if (mysql_affected_rows() == 1) { //peercaching ALWAYS on quickQuery("DELETE FROM " . $prefix . "y{$hash} WHERE sequence=" . $peer["sequence"]); if ($peer["status"] == "leecher") { summaryAdd("leechers", -1); } else { summaryAdd("seeds", -1); } if ($GLOBALS["countbytes"] && (double) $bytes > 0) { summaryAdd("dlbytes", $bytes); } if ($peer["bytes"] != 0 && $left == 0) { summaryAdd("finished", 1); } } }
function killPeer($userid, $hash, $left, $assumepeer = false) { global $db; MCached::connect(); if (!$assumepeer) { $peer = getPeerInfo($userid, $hash); if (!$peer) { return; } if ($left != $peer["bytes"]) { $bytes = bcsub($peer["bytes"], $left); } else { $bytes = 0; } } else { $bytes = 0; $peer = $assumepeer; } quickQuery("DELETE FROM peers WHERE peer_id = '" . $userid . "' AND infohash = '" . $hash . "'"); MCached::del('peers::count::' . $hash); if ($db->affected_rows == 1) { if ($peer["status"] == "leecher") { summaryAdd("leechers", -1); } else { summaryAdd("seeds", -1); } if ($GLOBALS["countbytes"] && (double) $bytes > 0) { summaryAdd("dlbytes", $bytes); } if ($peer["bytes"] != 0 && $left == 0) { summaryAdd("finished", 1); } summaryAdd("lastcycle", "UNIX_TIMESTAMP()", true); } }
function start($info_hash, $ip, $port, $peer_id, $left, $uploaded, $clientVer) { if ($left == 0) { $status = "seeder"; } else { $status = "leecher"; } if (@isFireWalled($info_hash, $peer_id, $ip, $port)) { $nat = "'Y'"; } else { $nat = "'N'"; } $results = @mysql_query("INSERT INTO x{$info_hash} SET peer_id=\"{$peer_id}\", port=\"{$port}\", ip=\"{$ip}\", lastupdate=UNIX_TIMESTAMP(), bytes=\"{$left}\", status=\"{$status}\", natuser={$nat}, uploaded={$uploaded}, clientversion=\"{$clientVer}\""); // Special case: duplicated peer_id. if (!$results) { $error = mysql_error(); if (stristr($error, "key")) { // Duplicate peer_id! Check IP address $peer = getPeerInfo($peer_id, $info_hash); if ($ip == $peer["ip"]) { // Same IP address. Tolerate this error. updatePeer($peer_id, $info_hash); return "WHERE natuser='******'"; } //showError("Duplicated peer_id or changed IP address. Please restart BitTorrent."); // Different IP address. Assume they were disconnected, and alter the IP address. quickQuery("UPDATE x{$info_hash} SET ip=\"{$ip}\", uploaded={$uploaded}, clientversion=\"{$clientVer}\" WHERE peer_id=\"{$peer_id}\""); return "WHERE natuser='******'"; } error_log("PHPBTTracker: start: " . $error); showError("Tracker/database error. The details are in the error log."); } $GLOBALS["trackerid"] = mysql_insert_id(); if ($GLOBALS["peercaching"]) { $compact = mysql_real_escape_string(pack('Nn', ip2long($ip), $port)); $peerid = mysql_real_escape_string('2:ip' . strlen($ip) . ':' . $ip . '7:peer id20:' . hex2bin($peer_id) . "4:porti{$port}e"); $no_peerid = mysql_real_escape_string('2:ip' . strlen($ip) . ':' . $ip . "4:porti{$port}e"); mysql_query("INSERT INTO y{$info_hash} SET sequence=\"{$GLOBALS["trackerid"]}\", compact=\"{$compact}\", with_peerid=\"{$peerid}\", without_peerid=\"{$no_peerid}\""); // Let's just assume success... :/ } if ($left == 0) { summaryAdd("seeds", 1); return "WHERE status=\"leecher\" AND natuser='******'"; } else { summaryAdd("leechers", 1); return "WHERE natuser='******'"; } }
sendRandomPeers($info_hash); break; case "completed": $peer_exists = getPeerInfo($uid, $info_hash); if (!is_array($peer_exists)) { start($info_hash, $ip, $port, $uid, $tid); } else { $db->query("UPDATE {$tablepre}xbtit_peers SET status=\"seeder\", lastupdate=UNIX_TIMESTAMP() WHERE uid={$uid} AND infohash=\"{$info_hash}\""); if (mysql_affected_rows() == 1) { add_finished($info_hash); } } sendRandomPeers($info_hash); break; case "": $peer_exists = getPeerInfo($uid, $info_hash); if (!is_array($peer_exists)) { start($info_hash, $ip, $port, $uid, $tid); } if ($left == 0) { $db->query("UPDATE {$tablepre}xbtit_peers SET status=\"seeder\", lastupdate=UNIX_TIMESTAMP() WHERE uid={$uid} AND infohash=\"{$info_hash}\""); } sendRandomPeers($info_hash); break; default: show_error("客户端发送未定义的事件。"); } mysql_close(); //*********************函数*****************// //******************************************// function sendRandomPeers($info_hash)
function killPeer($userid, $hash, $left, $assumepeer = false) { global $TABLE_PREFIX; if (!$assumepeer) { $peer = getPeerInfo($userid, $hash); if (!$peer) { return; } if ($left != $peer["bytes"]) { $bytes = bcsub($peer["bytes"], $left); } else { $bytes = 0; } } else { $bytes = 0; $peer = $assumepeer; } quickQuery("DELETE FROM {$TABLE_PREFIX}peers WHERE peer_id=\"{$userid}\" AND infohash=\"{$hash}\""); if (mysql_affected_rows() == 1) { if ($peer["status"] == "leecher") { summaryAdd("leechers", -1); } else { summaryAdd("seeds", -1); } if ($GLOBALS["countbytes"] && (double) $bytes > 0) { summaryAdd("dlbytes", $bytes); } if ($peer["bytes"] != 0 && $left == 0) { summaryAdd("finished", 1); } summaryAdd("lastcycle", "UNIX_TIMESTAMP()", true); } }
function start($info_hash, $ip, $port, $peer_id, $left) { if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) { foreach (explode(",", $_SERVER["HTTP_X_FORWARDED_FOR"]) as $address) { $addr = ip2long(trim($address)); if ($addr != -1) { if ($addr >= -1062731776 && $addr <= -1062666241) { // 192.168.x.x } else { if ($addr >= -1442971648 && $addr <= -1442906113) { // 169.254.x.x } else { if ($addr >= 167772160 && $addr <= 184549375) { // 10.x.x.x } else { if ($addr >= 2130706432 && $addr <= 2147483647) { // 127.0.0.1 } else { if ($addr >= -1408237568 && $addr <= -1407188993) { // 172.[16-31].x.x } else { // Finally, we can accept it as a "real" ip address. $ip = mysql_escape_string(trim($address)); break; } } } } } } } } if (isset($_GET["ip"]) && $GLOBALS["ip_override"]) { // compact check: valid IP address: if (ip2long($_GET["ip"]) == -1) { showError("Invalid IP address. Must be standard dotted decimal (hostnames not allowed)"); } $ip = mysql_escape_string($_GET["ip"]); } if ($left == 0) { $status = "seeder"; } else { $status = "leecher"; } if (@isFireWalled($info_hash, $peer_id, $ip, $port)) { $nat = "'Y'"; } else { $nat = "'N'"; } $results = @mysql_query("INSERT INTO x{$info_hash} SET peer_id=\"{$peer_id}\", port=\"{$port}\", ip=\"{$ip}\", lastupdate=UNIX_TIMESTAMP(), bytes=\"{$left}\", status=\"{$status}\", natuser={$nat}"); // Special case: duplicated peer_id. if (!$results) { $error = mysql_error(); if (stristr($error, "key")) { // Duplicate peer_id! Check IP address $peer = getPeerInfo($peer_id, $info_hash); if ($ip == $peer["ip"]) { // Same IP address. Tolerate this error. updatePeer($peer_id, $info_hash); return "WHERE natuser='******'"; } //showError("Duplicated peer_id or changed IP address. Please restart BitTorrent."); // Different IP address. Assume they were disconnected, and alter the IP address. quickQuery("UPDATE x{$info_hash} SET ip=\"{$ip}\" WHERE peer_id=\"{$peer_id}\""); return "WHERE natuser='******'"; } error_log("PHPBTTracker: start: " . $error); showError("Tracker/database error. The details are in the error log."); } $GLOBALS["trackerid"] = mysql_insert_id(); if ($GLOBALS["peercaching"]) { $compact = mysql_escape_string(pack('Nn', ip2long($ip), $port)); $peerid = mysql_escape_string('2:ip' . strlen($ip) . ':' . $ip . '7:peer id20:' . hex2bin($peer_id) . "4:porti{$port}e"); $no_peerid = mysql_escape_string('2:ip' . strlen($ip) . ':' . $ip . "4:porti{$port}e"); mysql_query("INSERT INTO y{$info_hash} SET sequence=\"{$GLOBALS["trackerid"]}\", compact=\"{$compact}\", with_peerid=\"{$peerid}\", without_peerid=\"{$no_peerid}\""); // Let's just assume success... :/ } if ($left == 0) { summaryAdd("seeds", 1); return "WHERE status=\"leecher\" AND natuser='******'"; } else { summaryAdd("leechers", 1); return "WHERE natuser='******'"; } }