Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- header('Content-type: text/plain');
- // -- initialize -- //
- $info_hash = clear_magic_quotes($_GET['info_hash']);
- if (strlen($info_hash) != 20) fail('Invalid info hash');
- $peer_id = clear_magic_quotes($_GET['peer_id']);
- if (strlen($peer_id) != 20) fail('Invalid peer id');
- $ip = $_SERVER['REMOTE_ADDR'];
- if ($ip == '127.0.0.1') { // we deal with connections from localhost differently
- if (long2ip(ip2long($_GET['ip'])) == $_GET['ip'])
- $ip = $_GET['ip'];
- }
- $port = intval($_GET['port']);
- if (($port < 1) || ($port > 65535)) fail('Invalid port number');
- $uploader = ($_GET['left'] == 0) ? 1 : 0;
- $event = in_array($_GET['event'], array('started', 'completed', 'stopped')) ? $_GET['event'] : 'empty';
- $success = mysql_connect('localhost', 'root', '');
- if ($success) $success = mysql_select_db('tracker');
- if (!$success) fail('Error connecting to database');
- $ih = mysql_real_escape_string($info_hash);
- $pi = mysql_real_escape_string($peer_id);
- // -- down to business -- //
- // Queries:
- // started 3
- // empty 3
- // completed 3
- // stopped 1
- if ($event == 'stopped') {
- mysql_query("DELETE FROM peers WHERE last_connect+0 < UNIX_TIMESTAMP() - 3600 OR (info_hash = '$ih' AND peer_id = '$pi');");
- die();
- }
- mysql_query("DELETE FROM peers WHERE last_connect+0 < UNIX_TIMESTAMP() - 3600;");
- if ($event == 'started') {
- mysql_query("INSERT INTO peers (info_hash, peer_id, uploader, ip, port) VALUES ('$ih', '$pi', $uploader, '$ip', $port);");
- } else {
- mysql_query("UPDATE peers SET uploader = $uploader WHERE info_hash = '$ih' AND peer_id = '$pi';");
- }
- // -- output -- //
- echo 'd8:intervali1800e5:peersl';
- $res = mysql_query("SELECT peer_id, ip, port FROM peers WHERE info_hash = '$info_hash' AND peer_id != '$peer_id'" . ($uploader ? " AND uploader = 0" : "") . " ORDER BY RAND() LIMIT 25;");
- if (!mysql_num_rows($res)) die('ee');
- while ($row = mysql_fetch_assoc($res))
- echo 'd7:peer id' . strlen($row['peer_id']) . ':' . $row['peer_id'] . '2:ip' . strlen($row['ip']) . ':' . $row['ip'] . '4:porti' . $row['port'] . 'ee';
- die('ee');
- // -- supporting functions -- //
- function clear_magic_quotes($string) {
- return get_magic_quotes_gpc() ? stripslashes($string) : $string;
- }
- function fail($message) {
- die('d14:failure reason' . strlen($message) . ':' . $message . 'e');
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement