function runSpeed($info_hash, $delta) { global $db; MCached::connect(); // stick in our latest data before we calc it out quickQuery("INSERT IGNORE INTO timestamps (info_hash, bytes, delta, sequence) SELECT '" . $info_hash . "' AS info_hash, dlbytes, UNIX_TIMESTAMP() - lastSpeedCycle, NULL FROM summary WHERE info_hash = '" . $info_hash . "'"); $key = 'ann::bytes::timestamps::' . $info_hash; $data = MCached::get($key); if ($data === MCached::NO_RESULT) { $results = $db->query('SELECT (MAX(bytes) - MIN(bytes)) / SUM(delta), COUNT(*), MIN(sequence) FROM timestamps WHERE info_hash = "' . $info_hash . '"'); $data = $results->fetch_row(); MCached::add($key, $data, 300); } summaryAdd("speed", $data[0], true); summaryAdd("lastSpeedCycle", "UNIX_TIMESTAMP()", true); // if we have more than 20 drop the rest if ($data[1] == 21) { quickQuery("DELETE FROM timestamps WHERE info_hash='" . $info_hash . "' AND sequence = " . $data['2']); MCached::del($key); } elseif ($data[1] > 21) { quickQuery('DELETE FROM timestamps WHERE info_hash = "' . $info_hash . '" ORDER BY sequence LIMIT ' . ($data['1'] - 20)); MCached::del($key); } }
function runSpeed($info_hash, $delta) { require "config.php"; //stick in our latest data before we calc it out quickQuery("INSERT IGNORE INTO " . $prefix . "timestamps (info_hash, bytes, delta, sequence) SELECT '{$info_hash}' AS info_hash, dlbytes, UNIX_TIMESTAMP() - lastSpeedCycle, NULL FROM " . $prefix . "summary WHERE info_hash=\"{$info_hash}\""); // mysql blows sometimes so we have to read the data into php before updating it $results = mysql_query('SELECT (MAX(bytes)-MIN(bytes))/SUM(delta), COUNT(*), MIN(sequence) FROM ' . $prefix . 'timestamps WHERE info_hash="' . $info_hash . '"'); $data = mysql_fetch_row($results); $results2 = mysql_query('SELECT ' . $prefix . 'summary.leechers FROM ' . $prefix . 'summary WHERE info_hash="' . $info_hash . '"'); $data2 = mysql_fetch_row($results2); if ($data2[0] == 0) { //if no leechers, speed is zero $data[0] = 0; } $results3 = mysql_query("SELECT MIN(d1.bytes), MAX(d1.bytes) FROM (SELECT bytes FROM " . $prefix . "timestamps WHERE info_hash='" . $info_hash . "' ORDER BY sequence DESC LIMIT 5) AS d1"); $data3 = mysql_fetch_row($results3); //if the last 5 updates from clients show the same bytes, it's probably stalled, set speed to zero if ($data3[0] == $data3[1]) { $data[0] = 0; } summaryAdd("speed", $data[0], true); summaryAdd("lastSpeedCycle", "UNIX_TIMESTAMP()", true); // if we have more than 20 drop the rest //if ($data[1] == 21) //quickQuery("DELETE FROM timestamps WHERE info_hash=\"$info_hash\" AND sequence=${data[2]}"); if ($data[1] > 21) { // This query requires MySQL 4.0.x, but should rarely be used. quickQuery('DELETE FROM ' . $prefix . 'timestamps WHERE info_hash="' . $info_hash . '" ORDER BY sequence LIMIT ' . ($data['1'] - 20)); } }
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='******'"; } }
function runSpeed($info_hash, $delta) { global $TABLE_PREFIX; //stick in our latest data before we calc it out quickQuery("INSERT IGNORE INTO {$TABLE_PREFIX}timestamps (info_hash, bytes, delta, sequence) SELECT '{$info_hash}' AS info_hash, dlbytes, UNIX_TIMESTAMP() - lastSpeedCycle, NULL FROM {$TABLE_PREFIX}files WHERE info_hash=\"{$info_hash}\""); // mysql blows sometimes so we have to read the data into php before updating it $results = mysql_query('SELECT (MAX(bytes)-MIN(bytes))/SUM(delta), COUNT(*), MIN(sequence) FROM ' . $TABLE_PREFIX . 'timestamps WHERE info_hash="' . $info_hash . '"'); $data = mysql_fetch_row($results); summaryAdd("speed", $data[0], true); summaryAdd("lastSpeedCycle", "UNIX_TIMESTAMP()", true); // if we have more than 20 drop the rest if ($data[1] == 21) { quickQuery("DELETE FROM {$TABLE_PREFIX}timestamps WHERE info_hash=\"{$info_hash}\" AND sequence={$data[2]}"); } else { if ($data[1] > 21) { // This query requires MySQL 4.0.x, but should rarely be used. quickQuery('DELETE FROM ' . $TABLE_PREFIX . 'timestamps WHERE info_hash="' . $info_hash . '" ORDER BY sequence LIMIT ' . ($data['1'] - 20)); } } }
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='******'"; } }
function runSpeed($info_hash, $delta) { global $db; // stick in our latest data before we calc it out quickQuery("INSERT IGNORE INTO timestamps (info_hash, bytes, delta, sequence) SELECT '" . $info_hash . "' AS info_hash, dlbytes, UNIX_TIMESTAMP() - lastSpeedCycle, NULL FROM summary WHERE info_hash = '" . $info_hash . "'"); $announce_bytes_ts = $BASEPATH . 'cache/announce_bytes_timestamps_' . $info_hash . '.txt'; $announce_bytes_ts_expire = 5 * 60; if (file_exists($announce_bytes_ts) && is_array(unserialize(file_get_contents($announce_bytes_ts))) && vars::$timestamp - filemtime($announce_bytes_ts) < $announce_bytes_ts_expire) { $data = unserialize(@file_get_contents($announce_bytes_ts)); } else { $results = $db->query('SELECT (MAX(bytes) - MIN(bytes)) / SUM(delta), COUNT(*), MIN(sequence) FROM timestamps WHERE info_hash = "' . $info_hash . '"'); $data = $results->fetch_row(); $handle = fopen($announce_bytes_ts, "w+"); fwrite($handle, serialize($data)); fclose($handle); } summaryAdd("speed", $data[0], true); summaryAdd("lastSpeedCycle", "UNIX_TIMESTAMP()", true); @unlink($BASEPATH . 'cache/announce_count_bytes_' . $info_hash . '.txt'); // if we have more than 20 drop the rest if ($data[1] == 21) { quickQuery("DELETE FROM timestamps WHERE info_hash=\"{$info_hash}\" AND sequence = " . $data['2']); @unlink($BASEPATH . 'cache/announce_bytes_timestamps_' . $info_hash . '.txt'); } else { if ($data[1] > 21) { quickQuery('DELETE FROM timestamps WHERE info_hash = "' . $info_hash . '" ORDER BY sequence LIMIT ' . ($data['1'] - 20)); @unlink($BASEPATH . 'cache/announce_bytes_timestamps_' . $info_hash . '.txt'); } } }
function trashCollector($hash, $timeout) { global $db; if (isset($GLOBALS["trackerid"])) { unset($GLOBALS["trackerid"]); } if (!Lock($hash)) { return; } $results = $db->query("SELECT lastcycle FROM summary WHERE info_hash = '" . $hash . "'"); $lastcheck = $results->fetch_row(); // Check once every re-announce cycle if ($lastcheck[0] + $timeout < vars::$timestamp) { $peers = loadLostPeers($hash, $timeout); for ($i = 0; $i < $peers["size"]; $i++) { killPeer($peers[$i]["peer_id"], $hash, $peers[$i]["bytes"]); } summaryAdd("lastcycle", "UNIX_TIMESTAMP()", true); } Unlock($hash); }