Advertisement
Guest User

CurlyFries

a guest
Oct 27th, 2008
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. <?php
  2.  
  3. header('Content-type: text/plain');
  4.  
  5.  
  6. // -- initialize -- //
  7.  
  8. $info_hash = clear_magic_quotes($_GET['info_hash']);
  9. if (strlen($info_hash) != 20) fail('Invalid info hash');
  10.  
  11. $peer_id = clear_magic_quotes($_GET['peer_id']);
  12. if (strlen($peer_id) != 20) fail('Invalid peer id');
  13.  
  14. $ip = $_SERVER['REMOTE_ADDR'];
  15. if ($ip == '127.0.0.1') { // we deal with connections from localhost differently
  16. if (long2ip(ip2long($_GET['ip'])) == $_GET['ip'])
  17. $ip = $_GET['ip'];
  18. }
  19.  
  20. $port = intval($_GET['port']);
  21. if (($port < 1) || ($port > 65535)) fail('Invalid port number');
  22.  
  23. $uploader = ($_GET['left'] == 0) ? 1 : 0;
  24.  
  25. $event = in_array($_GET['event'], array('started', 'completed', 'stopped')) ? $_GET['event'] : 'empty';
  26.  
  27. $success = mysql_connect('localhost', 'root', '');
  28. if ($success) $success = mysql_select_db('tracker');
  29. if (!$success) fail('Error connecting to database');
  30.  
  31. $ih = mysql_real_escape_string($info_hash);
  32. $pi = mysql_real_escape_string($peer_id);
  33.  
  34.  
  35. // -- down to business -- //
  36.  
  37. // Queries:
  38. // started 3
  39. // empty 3
  40. // completed 3
  41. // stopped 1
  42.  
  43. if ($event == 'stopped') {
  44. mysql_query("DELETE FROM peers WHERE last_connect+0 < UNIX_TIMESTAMP() - 3600 OR (info_hash = '$ih' AND peer_id = '$pi');");
  45. die();
  46. }
  47.  
  48. mysql_query("DELETE FROM peers WHERE last_connect+0 < UNIX_TIMESTAMP() - 3600;");
  49.  
  50. if ($event == 'started') {
  51. mysql_query("INSERT INTO peers (info_hash, peer_id, uploader, ip, port) VALUES ('$ih', '$pi', $uploader, '$ip', $port);");
  52. } else {
  53. mysql_query("UPDATE peers SET uploader = $uploader WHERE info_hash = '$ih' AND peer_id = '$pi';");
  54. }
  55.  
  56.  
  57. // -- output -- //
  58.  
  59. echo 'd8:intervali1800e5:peersl';
  60.  
  61. $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;");
  62. if (!mysql_num_rows($res)) die('ee');
  63.  
  64. while ($row = mysql_fetch_assoc($res))
  65. echo 'd7:peer id' . strlen($row['peer_id']) . ':' . $row['peer_id'] . '2:ip' . strlen($row['ip']) . ':' . $row['ip'] . '4:porti' . $row['port'] . 'ee';
  66.  
  67. die('ee');
  68.  
  69.  
  70. // -- supporting functions -- //
  71.  
  72. function clear_magic_quotes($string) {
  73. return get_magic_quotes_gpc() ? stripslashes($string) : $string;
  74. }
  75. function fail($message) {
  76. die('d14:failure reason' . strlen($message) . ':' . $message . 'e');
  77. }
  78.  
  79. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement