Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* One-file(+MySQL) BitTorrent Tracker
- * Written by kenkeiras
- *
- * This program is free software. It comes without any warranty, to
- * the extent permitted by applicable law. You can redistribute it
- * and/or modify it under the terms of the Do What The Fuck You Want
- * To Public License, Version 2, as published by Sam Hocevar. See
- * http://sam.zoy.org/wtfpl/COPYING for more details. */
- /* MySQL database configuration */
- /* Database host */
- $dbhost = 'localhost';
- /* Database user */
- $dbuser = "one_file_tracker";
- /* Database password */
- $dbpass = "one_file_tracker";
- /* Database name */
- $dbname = "one_file_tracker";
- /* Entry timeout (in seconds)
- * after this time, peer data will be no longer sended
- */
- $timeout = 3600;
- /* Peer table name
- * Columns:
- * Peer_id: char(28)
- * info_hash: char(28)
- * port: smallint unsigned
- * uploaded: int(1)
- * downloaded: int(1)
- * to_go: int(1)
- * ip: varchar(15)
- * peer_key: varchar(255)
- * completed: boolean
- * last_update: int(1)
- *
- * Keys:
- * Peer_id
- * info_hash
- *
- * create table peers( peer_id char(28), info_hash char(28), port smallint
- * unsigned, uploaded int(1), downloaded int(1), to_go int(1),
- * ip varchar(15), peer_key varchar(255), completed boolean,
- * last_update int(1),KEY (peer_id), KEY (info_hash));
- */
- $dbtable = "peers";
- /* Default number of peers returned
- * protocol's default is 50
- */
- $default_peer_num = 50;
- /* Interval in seconds that the client should wait between
- * sending regular requests to the tracker
- */
- $interval = 300;
- /* Max peer returned */
- $max_peer_num = 100;
- /* Prints a number in network order */
- function hton_spwn($n, $c){
- for($i=$c-1;$i>=0;$i--){
- echo chr($n>>($i*8));
- $n = $n % (1<<($i*8));
- }
- }
- /* Sends an error in the BitTorrent'ish way */
- function bt_die($s){
- $fail="failure reason";
- die( "d".strlen($fail).":".$fail.strlen($s).$s."e" );
- }
- /* Sends the peer list */
- function send_bencoded_peers($d){
- print "d";
- $int = "interval"; // Connection interval
- echo strlen($int).":".$int."i".$d['interval']."e";
- if (!$d['stop']){
- $comp = "complete"; // Seeds
- echo strlen($comp).":".$comp."i".$d['complete']."e";
- $incomp = "incomplete"; // Leechers
- echo strlen($incomp).":".$incomp."i".$d['incomplete']."e";
- }
- $peers = "peers";
- echo strlen($peers).":$peers"; // Peer list
- $has_peers = FALSE;
- if ($d['compact']){
- echo (6 * mysql_num_rows($d['peers']) ).":";
- }
- else{
- echo "l";
- }
- while ( ($n = mysql_fetch_array($d['peers'])) !== FALSE ){
- $has_peers = TRUE;
- if ($d['compact']){ // Compact format
- echo hton_spwn(ip2long($n['ip']),4);
- echo hton_spwn($n['port'],2);
- }
- else{ // Normal format
- $url_peer = urlencode($n['peer_id']);
- echo "d";
- if (strlen($url_peer) > 0){
- echo "2:id".strlen($url_peer).":".$url_peer;
- }
- echo "2:ip".strlen($n['ip']).":".$n['ip'];
- echo "4:port".strlen($n['port']).":".$n['port']."e";
- }
- }
- if (!$d['compact']){
- echo "e";
- }
- echo "e";
- }
- /* The true tracker function */
- function tracker($dbhost, $dbuser,$dbpass,$dbname,$dbtable){
- header('Content-type: text/plain');
- $db = mysql_connect( $dbhost, $dbuser, $dbpass ) or
- bt_die("Error connecting to database");
- mysql_select_db( "$dbname", $db ) or
- bt_die("Error selecting a database");
- /* Delete timeouted peers */
- mysql_query( "delete from $dbtable where ".
- "last_update<".(time()-$GLOBALS['timeout']).";") or
- bt_die("Error updating database");
- /* SQL-safe values */
- $peer_id = base64_encode($_GET['peer_id']);
- $info_hash = base64_encode($_GET['info_hash']);
- $port = intval($_GET['port']);
- $uploaded = intval($_GET['uploaded']);
- $downloaded = intval($_GET['downloaded']);
- $to_go = intval($_GET['left']);
- $ip = $_SERVER['REMOTE_ADDR'];
- $peer_key = base64_encode($_GET['key']);
- $stopping = FALSE;
- /* Checks if it needs to add or just update the rows */
- $res = mysql_query( "select count(1) as count from $dbtable where ".
- "peer_id='$peer_id' and info_hash='$info_hash';") or
- bt_die("Error reading database");
- $num = mysql_fetch_array($res);
- if ( isset( $_GET['event'])&&($_GET['event']=="stopped")){
- mysql_query("delete from $dbtable ".
- "where peer_id='$peer_id' and info_hash='$info_hash'".
- "and peer_key='$peer_key';") or
- bt_die( "Error deleting you from available peers");
- $stopping = TRUE;
- }
- $state = "";
- if ((isset($_GET['event'])&&($_GET['event']=="completed"))
- or ($to_go < 1)){
- $state = ",completed=true";
- }
- elseif (isset($_GET['event'])&&($_GET['event']=="started")){
- $state = ",completed=false";
- }
- if ( !$stopping ){
- /* Stores the data */
- if ( $num['count'] == 0 ){
- mysql_query( "insert into $dbtable set ".
- "last_update=".time().",".
- "peer_id='$peer_id',info_hash='$info_hash',".
- "uploaded=$uploaded,peer_key='$peer_key',".
- "downloaded=$downloaded,to_go=$to_go,ip='$ip',".
- "port=$port $state;") or
- bt_die( "Error adding you to the peer database");
- }
- else{
- mysql_query( "update $dbtable set uploaded=$uploaded,".
- "last_update=".time().",".
- " downloaded=$downloaded,to_go=$to_go,ip='$ip', ".
- "port=$port $state ".
- "where peer_id='$peer_id' and info_hash='$info_hash' ".
- "and peer_key='$peer_key';") or
- bt_die("Error updating database");
- }
- }
- /* Reads the total number of peers */
- $res = mysql_fetch_array(mysql_query("select count(1) as count ".
- " from $dbtable ".
- " where info_hash='$info_hash';"));
- $total_peers = $res['count'];
- $peerNum = $GLOBALS["default_peer_num"];
- if ( isset( $_GET['numwant'] ) &&
- $_GET['numwant'] <= $GLOBALS['max_peer_num']){
- $peerNum = $_GET['numwant'];
- }
- $top = $total_peers > $peerNum?$total_peers - $peerNum:0;
- $off = rand(0, $top);
- /* Takes some random peers */
- $peers = mysql_query("select peer_id,ip,port".
- " from $dbtable ".
- " where peer_id!='$peer_id'".
- " and info_hash='$info_hash' ".
- " limit $off,$peerNum;");
- /* Reads how many of the hava completed the download */
- $comp = mysql_fetch_array(mysql_query("select count(1) as count ".
- " from $dbtable ".
- " where info_hash='$info_hash' and".
- " peer_id!='$peer_id' and ".
- " completed=True ;"));
- /* Stores all in a dictionary */
- $dict = array( 'interval'=> $GLOBALS['interval']."",
- 'complete'=> $comp['count'],
- 'incomplete'=> $total_peers - $comp['count'],
- 'peers' => $peers,
- 'compact' => isset($_GET['compact']) &&
- ($_GET['compact'] == "1")?TRUE:FALSE,
- 'stop' => $stopping);
- /* And sends it */
- send_bencoded_peers($dict);
- mysql_close();
- }
- if (isset($_GET) && isset($_GET['peer_id']) && isset($_GET['peer_id'])){
- tracker($dbhost, $dbuser,$dbpass,$dbname,$dbtable);
- }
- else{
- echo <<< __EOF__
- <html><head><title>One File Tracker</title></head>
- <body>Are you a BitTorrent client? ¬¬</body></html>
- __EOF__;
- }
- ?>
Add Comment
Please, Sign In to add comment