function tracker_stats()
{
    global $connection, $settings;
    require_once __DIR__ . '/once.db.connect.php';
    require_once __DIR__ . '/function.mysqli.fetch.once.php';
    // Statistics
    $stats = mysqli_fetch_once('SELECT ' . 'SUM(`state`=\'1\') AS `seeders`, ' . 'SUM(`state`=\'0\') AS `leechers`, ' . 'COUNT(DISTINCT info_hash) AS `torrents` ' . 'FROM `' . $settings['db_prefix'] . 'peers`');
    // Downloads
    $downloads = mysqli_fetch_once('SELECT ' . 'SUM(`downloads`) AS `downloads` ' . 'FROM `' . $settings['db_prefix'] . 'torrents`');
    if (!$stats) {
        tracker_error('Unable to get stats.');
    } else {
        $phoenix_version = 'Phoenix Procedural 1.3 2015-02-16 20:44:00Z eustasy';
        $stats['seeders'] = intval($stats['seeders']);
        $stats['leechers'] = intval($stats['leechers']);
        $stats['torrents'] = intval($stats['torrents']);
        $stats['downloads'] = intval($downloads['downloads']);
        $stats['peers'] = $stats['seeders'] + $stats['leechers'];
        // XML
        if (isset($_GET['xml'])) {
            header('Content-Type: text/xml');
            echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . '<tracker version="$Id: ' . $phoenix_version . ' $">' . '<peers>' . $stats['peers'] . '</peers>' . '<seeders>' . $stats['seeders'] . '</seeders>' . '<leechers>' . $stats['leechers'] . '</leechers>' . '<torrents>' . $stats['torrents'] . '</torrents>' . '<downloads>' . $stats['downloads'] . '</downloads></tracker>';
            // JSON
        } else {
            if (isset($_GET['json'])) {
                header('Content-Type: application/json');
                echo json_encode(array('tracker' => array('version' => '$Id: ' . $phoenix_version . ' $,', 'peers' => $stats['peers'], 'seeders' => $stats['seeders'], 'leechers' => $stats['leechers'], 'torrents' => $stats['torrents'], 'downloads' => $stats['downloads'])));
                // HTML
            } else {
                echo '<!DocType html><html><head><meta charset="UTF-8">' . '<title>Phoenix: $Id: ' . $phoenix_version . ' $</title>' . '<body><pre>' . number_format($stats['peers']) . ' peers (' . number_format($stats['seeders']) . ' seeders + ' . number_format($stats['leechers']) . ' leechers) in ' . number_format($stats['torrents']) . ' torrents and' . ' ' . number_format($stats['downloads']) . ' downloads completed.</pre></body></html>';
            }
        }
    }
}
function peer_access($connection, $settings, $time, $peer)
{
    $peer_access = mysqli_query($connection, 'UPDATE `' . $settings['db_prefix'] . 'peers` ' . 'SET `updated`=\'' . $time . '\', `left`=\'' . $peer['left'] . '\' ' . 'WHERE `info_hash`=\'' . $peer['info_hash'] . '\' AND `peer_id`=\'' . $peer['peer_id'] . '\';');
    if ($peer_access) {
        return true;
    } else {
        tracker_error('Failed to update peers last access.');
    }
}
function torrent_scrape()
{
    global $connection, $settings;
    require_once __DIR__ . '/once.db.connect.php';
    require_once __DIR__ . '/function.mysqli.fetch.once.php';
    // select seeders and leechers
    $query = '
		SELECT
			`p`.`info_hash` AS `info_hash`,
			SUM(`p`.`state`=\'1\') AS `seeders`,
			SUM(`p`.`state`=\'0\') AS `leechers`,
			`t`.`downloads` AS `downloads`
		FROM `' . $settings['db_prefix'] . 'peers` AS `p`
			LEFT JOIN `' . $settings['db_prefix'] . 'torrents` AS `t`
			ON `p`.`info_hash`=`t`.`info_hash`
		WHERE `p`.`info_hash`=\'' . $_GET['info_hash'] . '\';';
    $scrape = mysqli_fetch_once($query);
    if (!$scrape) {
        tracker_error('Unable to scrape for that torrent.');
    } else {
        $scrape['seeders'] = intval($scrape['seeders']);
        $scrape['leechers'] = intval($scrape['leechers']);
        $scrape['downloads'] = intval($scrape['downloads']);
        $scrape['peers'] = $scrape['seeders'] + $scrape['leechers'];
        // XML
        if (isset($_GET['xml'])) {
            header('Content-Type: text/xml');
            echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . '<torrent>' . '<info_hash>' . $_GET['info_hash'] . '</info_hash>' . '<seeders>' . $scrape['seeders'] . '</seeders>' . '<leechers>' . $scrape['leechers'] . '</leechers>' . '<peers>' . $scrape['peers'] . '</peers>' . '<downloads>' . $scrape['downloads'] . '</downloads>' . '</torrent>';
            // JSON
        } else {
            if (isset($_GET['json'])) {
                header('Content-Type: application/json');
                echo json_encode(array('torrent' => array('info_hash' => $_GET['info_hash'], 'seeders' => $scrape['seeders'], 'leechers' => $scrape['leechers'], 'peers' => $scrape['peers'], 'downloads' => $scrape['downloads'])));
            } else {
                $echo = 'd
	5:files
	d
		20:' . hex2bin($_GET['info_hash']) . '
		d
			8:complete
			i' . $scrape['seeders'] . 'e
			10:downloaded
			i' . $scrape['downloads'] . 'e
			10:incomplete
			i' . $scrape['leechers'] . 'e
		e
	e
e';
                if (isset($_GET['verbose'])) {
                    echo $echo;
                } else {
                    echo preg_replace('/\\s+/', '', $echo);
                }
            }
        }
    }
}
function peer_delete($connection, $settings, $peer)
{
    $peer_delete = mysqli_query($connection, 'DELETE FROM `' . $settings['db_prefix'] . 'peers` ' . 'WHERE info_hash=\'' . $peer['info_hash'] . '\' AND peer_id=\'' . $peer['peer_id'] . '\';');
    if ($peer_delete) {
        return true;
    } else {
        tracker_error('Failed to remove peer.');
    }
}
function peer_delete()
{
    global $connection, $settings;
    require_once __DIR__ . '/once.db.connect.php';
    $peer_delete = mysqli_query($connection, 'DELETE FROM `' . $settings['db_prefix'] . 'peers` ' . 'WHERE info_hash=\'' . $_GET['info_hash'] . '\' AND peer_id=\'' . $_GET['peer_id'] . '\'');
    if ($peer_delete) {
        return true;
    } else {
        tracker_error('Failed to remove peer.');
    }
}
function peer_access()
{
    global $connection, $settings;
    require_once __DIR__ . '/once.db.connect.php';
    $peer_access = mysqli_query($connection, 'UPDATE `' . $settings['db_prefix'] . 'peers` ' . 'SET `updated`=\'' . time() . '\', `left`=\'' . $_GET['left'] . '\' ' . 'WHERE `info_hash`=\'' . $_GET['info_hash'] . '\' AND `peer_id`=\'' . $_GET['peer_id'] . '\'');
    if ($peer_access) {
        return true;
    } else {
        tracker_error('Failed to update peers last access.');
    }
}
Esempio n. 7
0
function peer_new()
{
    global $connection, $settings;
    require_once __DIR__ . '/once.db.connect.php';
    $peer_new = mysqli_query($connection, 'REPLACE INTO `' . $settings['db_prefix'] . 'peers` ' . '(`info_hash`, `peer_id`, `compact`, `ip`, `port`, `left`, `state`, `updated`) ' . 'VALUES (' . '\'' . $_GET['info_hash'] . '\', ' . '\'' . $_GET['peer_id'] . '\', ' . '\'' . bin2hex(pack('Nn', ip2long($_GET['ip']), $_GET['port'])) . '\', ' . '\'' . $_GET['ip'] . '\', ' . '\'' . $_GET['port'] . '\', ' . '\'' . $_GET['left'] . '\', ' . '\'' . $settings['seeding'] . '\', ' . '\'' . time() . '\'' . ');');
    if ($peer_new) {
        return true;
    } else {
        tracker_error('Failed to add new peer.');
    }
}
function tracker_allowed()
{
    global $connection, $settings;
    require_once __DIR__ . '/once.db.connect.php';
    require_once __DIR__ . '/function.mysqli.array.build.php';
    $torrents = mysqli_array_build('SELECT `info_hash` FROM `' . $settings['db_prefix'] . 'torrents`');
    if (!$torrents) {
        tracker_error('No torrents allowed at this time.');
    } else {
        return $torrents;
    }
}
function mysqli_array_build($connection, $sql)
{
    $result = mysqli_query($connection, $sql);
    if (!$result) {
        tracker_error('Failed to build array.');
    } else {
        $response = array();
        while ($thing = mysqli_fetch_array($result)) {
            $response[] = $thing[0];
        }
        return $response;
    }
}
function mysqli_array_build($sql)
{
    global $connection, $settings;
    require_once __DIR__ . '/once.db.connect.php';
    $result = mysqli_query($connection, $sql);
    if (!$result) {
        tracker_error('Failed to build array.');
    } else {
        $response = array();
        while ($thing = mysqli_fetch_array($result)) {
            $response[] = $thing[0];
        }
        return $response;
    }
}
function peer_new($connection, $settings, $time, $peer)
{
    $compactv4 = '';
    $compactv6 = '';
    if (!empty($peer['ipv4'])) {
        $compactv4 = bin2hex(pack('Nn', ip2long($peer['ipv4']), $peer['portv4']));
    }
    if (!empty($peer['ipv6'])) {
        $compactv6 = bin2hex(inet_pton($peer['ipv6']) . pack('n', $peer['portv6']));
    }
    $peer_new = mysqli_query($connection, 'REPLACE INTO `' . $settings['db_prefix'] . 'peers` ' . '(`info_hash`, `peer_id`, `compactv4`, `compactv6`, `ipv4`, `ipv6`, `portv4`,`portv6`, `left`, `state`, `updated`) ' . 'VALUES (' . '\'' . $peer['info_hash'] . '\', ' . '\'' . $peer['peer_id'] . '\', ' . '\'' . $compactv4 . '\', ' . '\'' . $compactv6 . '\', ' . '\'' . $peer['ipv4'] . '\', ' . '\'' . $peer['ipv6'] . '\', ' . '\'' . $peer['portv4'] . '\', ' . '\'' . $peer['portv6'] . '\', ' . '\'' . $peer['left'] . '\', ' . '\'' . $peer['state'] . '\', ' . '\'' . $time . '\'' . ');');
    if ($peer_new) {
        return true;
    } else {
        tracker_error('Failed to add new peer.');
    }
}
function tracker_clean()
{
    global $connection, $settings;
    require_once __DIR__ . '/once.db.connect.php';
    if (mt_rand(1, 100) <= $settings['clean_idle_peers']) {
        $time = time();
        $clean = mysqli_query($connection, 'DELETE FROM `' . $settings['db_prefix'] . 'peers` WHERE `updated` < ' . '\'' . ($time - $settings['announce_interval'] * 2) . '\'');
        if (!$clean) {
            tracker_error('Could not perform maintenance.');
        }
        $task = mysqli_query('REPLACE INTO `' . $settings['db_prefix'] . 'tasks` (`prune`) VALUES (\'' . $time . '\')');
        if (!$task) {
            tracker_error('Could not set last maintenance time.');
        }
    }
    return true;
}
function tracker_scrape()
{
    global $connection, $settings;
    require_once __DIR__ . '/once.db.connect.php';
    $tracker = mysqli_query($connection, 'SELECT ' . '`p`.`info_hash` AS `info_hash`, ' . 'SUM(`p`.`state`=\'1\') AS `seeders`, ' . 'SUM(`p`.`state`=\'0\') AS `leechers`, ' . '`t`.`downloads` AS `downloads` ' . 'FROM `' . $settings['db_prefix'] . 'peers` AS `p` ' . 'LEFT JOIN `' . $settings['db_prefix'] . 'torrents` AS `t` ' . 'ON `p`.`info_hash`=`t`.`info_hash` ' . 'GROUP BY `info_hash`');
    if (!$tracker) {
        tracker_error('Unable to scrape the tracker.');
    } else {
        // XML
        if (isset($_GET['xml'])) {
            header('Content-Type: text/xml');
            echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . '<tracker>';
            while ($scrape = mysqli_fetch_assoc($tracker)) {
                $scrape['peers'] = $scrape['seeders'] + $scrape['leechers'];
                echo '<torrent>' . '<info_hash>' . $scrape['info_hash'] . '</info_hash>' . '<seeders>' . intval($scrape['seeders']) . '</seeders>' . '<leechers>' . intval($scrape['leechers']) . '</leechers>' . '<peers>' . intval($scrape['peers']) . '</peers>' . '<downloads>' . intval($scrape['downloads']) . '</downloads>' . '</torrent>';
            }
            echo '</tracker>';
            // JSON
        } else {
            if (isset($_GET['json'])) {
                header('Content-Type: application/json');
                $json = array();
                while ($scrape = mysqli_fetch_assoc($tracker)) {
                    $scrape['peers'] = $scrape['seeders'] + $scrape['leechers'];
                    $json[$scrape['info_hash']] = array('seeders' => intval($scrape['seeders']), 'leechers' => intval($scrape['leechers']), 'peers' => intval($scrape['peers']), 'downloads' => intval($scrape['downloads']));
                }
                echo json_encode($json);
            } else {
                $response = 'd5:filesd';
                while ($scrape = mysqli_fetch_assoc($tracker)) {
                    $response .= '20:' . hex2bin($scrape['info_hash']) . 'd8:completei' . intval($scrape['seeders']) . 'e10:downloadedi' . intval($scrape['downloads']) . 'e10:incompletei' . intval($scrape['leechers']) . 'ee';
                }
                echo $response . 'ee';
            }
        }
    }
}
<?php

require_once $settings['functions'] . 'function.mysqli.fetch.once.php';
// Statistics
$sql = 'SELECT ' . 'SUM(`state`=\'1\') AS `seeders`, ' . 'SUM(`state`=\'0\') AS `leechers`, ' . 'COUNT(DISTINCT info_hash) AS `torrents` ' . 'FROM `' . $settings['db_prefix'] . 'peers`;';
$stats = mysqli_fetch_once($connection, $sql);
// Downloads
$sql = 'SELECT ' . 'SUM(`downloads`) AS `downloads` ' . 'FROM `' . $settings['db_prefix'] . 'torrents`;';
$downloads = mysqli_fetch_once($connection, $sql);
if (!$stats || !$downloads) {
    tracker_error('Unable to get stats.');
} else {
    $stats['seeders'] = intval($stats['seeders']);
    $stats['leechers'] = intval($stats['leechers']);
    $stats['torrents'] = intval($stats['torrents']);
    $stats['downloads'] = intval($downloads['downloads']);
    $stats['peers'] = $stats['seeders'] + $stats['leechers'];
    // XML
    if (isset($_GET['xml'])) {
        header('Content-Type: text/xml');
        echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . '<tracker version="$Id: ' . $settings['phoenix_version'] . ' $">' . '<peers>' . $stats['peers'] . '</peers>' . '<seeders>' . $stats['seeders'] . '</seeders>' . '<leechers>' . $stats['leechers'] . '</leechers>' . '<torrents>' . $stats['torrents'] . '</torrents>' . '<downloads>' . $stats['downloads'] . '</downloads></tracker>';
        // JSON
    } else {
        if (isset($_GET['json'])) {
            header('Content-Type: application/json');
            echo json_encode(array('tracker' => array('version' => '$Id: ' . $settings['phoenix_version'] . ' $,', 'peers' => $stats['peers'], 'seeders' => $stats['seeders'], 'leechers' => $stats['leechers'], 'torrents' => $stats['torrents'], 'downloads' => $stats['downloads'])));
            // HTML
        } else {
            echo '<!DocType html><html><head><meta charset="UTF-8">' . '<title>Phoenix: $Id: ' . $settings['phoenix_version'] . ' $</title>' . '<body><pre>' . number_format($stats['peers']) . ' peers (' . number_format($stats['seeders']) . ' seeders + ' . number_format($stats['leechers']) . ' leechers) in ' . number_format($stats['torrents']) . ' torrents and' . ' ' . number_format($stats['downloads']) . ' downloads completed.</pre></body></html>';
        }
    }
Esempio n. 15
0
 public static function stats()
 {
     // get stats
     $query = self::$db->query('SELECT SUM(state=1), SUM(state=0), ' . 'COUNT(DISTINCT info_hash) FROM peers;') or tracker_error('failed to retrieve tracker statistics');
     $stats = $query->fetchArray(SQLITE3_NUM);
     // output format
     switch ($_GET['stats']) {
         // xml
         case 'xml':
             header('Content-Type: text/xml');
             echo '<?xml version="1.0" encoding="ISO-8859-1"?>' . '<tracker version="$Id: tracker.sqlite3.php 148 2009-11-16 23:18:28Z trigunflame $">' . '<peers>' . number_format($stats[0] + $stats[1]) . '</peers>' . '<seeders>' . number_format($stats[0]) . '</seeders>' . '<leechers>' . number_format($stats[1]) . '</leechers>' . '<torrents>' . number_format($stats[2]) . '</torrents></tracker>';
             break;
             // json
         // json
         case 'json':
             header('Content-Type: text/javascript');
             echo '{"tracker":{"version":"$Id: tracker.sqlite3.php 148 2009-11-16 23:18:28Z trigunflame $",' . '"peers": "' . number_format($stats[0] + $stats[1]) . '",' . '"seeders":"' . number_format($stats[0]) . '",' . '"leechers":"' . number_format($stats[1]) . '",' . '"torrents":"' . number_format($stats[2]) . '"}}';
             break;
             // standard
         // standard
         default:
             echo '<!doctype html><html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8">' . '<title>PeerTracker: $Id: tracker.sqlite3.php 148 2009-11-16 23:18:28Z trigunflame $</title>' . '<body><pre>' . number_format($stats[0] + $stats[1]) . ' peers (' . number_format($stats[0]) . ' seeders + ' . number_format($stats[1]) . ' leechers) in ' . number_format($stats[2]) . ' torrents</pre></body></html>';
     }
     // cleanup
     $query->finalize();
 }
Esempio n. 16
0
    // Torrent is not Allowed
} else {
    if (!$settings['open_tracker'] && !in_array($peer['info_hash'], $allowed_torrents)) {
        tracker_error('Torrent is not allowed.');
        ////	peer_id
        // Required
        // 40 Characters
        // Hexadecimal
    } else {
        if (strlen($peer['peer_id']) != 40) {
            tracker_error('Peer ID is invalid.');
        } else {
            // IP Addresses & Port
            require_once $settings['onces'] . 'once.sanitize.announce.address.php';
            if (!$peer['ipv4'] && !$peer['portv4'] || !$peer['ipv6'] && !$peer['portv6']) {
                tracker_error('Unable to get IP and Port');
            }
            // Optional Items
            require_once $settings['onces'] . 'once.sanitize.announce.optional.php';
            // Track Client
            require_once $settings['onces'] . 'once.announce.peer.event.php';
            // Clean Up
            if (!$settings['clean_with_cron'] && $chance <= $settings['clean_with_requests']) {
                require_once $settings['functions'] . 'function.task.clean.php';
                task_clean($connection, $settings, $time);
            }
            // Announce Peers
            require_once $settings['onces'] . 'once.announce.torrent.php';
        }
    }
}
function torrent_announce()
{
    global $connection, $settings;
    require_once __DIR__ . '/once.db.connect.php';
    // begin response
    $response = 'd8:intervali' . $settings['announce_interval'] . 'e12:min intervali' . $settings['min_interval'] . 'e5:peers';
    require_once __DIR__ . '/function.mysqli.fetch.once.php';
    $peer_count = mysqli_fetch_once('SELECT COUNT(*) AS `count` FROM `' . $settings['db_prefix'] . 'peers` WHERE `info_hash`=\'' . $_GET['info_hash'] . '\'');
    if (!$peer_count) {
        $peer_count = 0;
    } else {
        $peer_count = $peer_count['count'];
    }
    $sql = 'SELECT * FROM `' . $settings['db_prefix'] . 'peers` WHERE `info_hash`=\'' . $_GET['info_hash'] . '\'';
    // IF there are more peers than requested,
    // only return the ones we need.
    if ($peer_count > $_GET['numwant']) {
        $sql .= ' LIMIT ' . $_GET['numwant'] . ' OFFSET ' . mt_rand(0, $peer_count - $_GET['numwant']);
        // IF there are more peers than the random limit.
    } else {
        if ($peer_count > $settings['random_limit']) {
            $sql .= ' ORDER BY RAND()';
        }
    }
    // IF Compact
    if ($_GET['compact']) {
        $peers = '';
        // END IF Compact
        // IF Not Compact
    } else {
        $response .= 'l';
    }
    // END IF Not Compact
    $query = mysqli_query($connection, $sql);
    if (!$query) {
        tracker_error('Failed to select peers.');
    } else {
        while ($peer = mysqli_fetch_assoc($query)) {
            // IF Compact
            if ($_GET['compact']) {
                $peers .= hex2bin($peer['compact']);
                // END IF Compact
                // IF No Peer ID
            } else {
                if ($_GET['no_peer_id']) {
                    $response .= 'd2:ip' . strlen($peer['ip']) . ':' . $peer['ip'] . '4:porti' . $peer['port'] . 'ee';
                    // END IF No Peer ID
                    // IF Normal
                } else {
                    $response .= 'd2:ip' . strlen($peer['ip']) . ':' . $peer['ip'] . '7:peer id20:' . hex2bin($peer['peer_id']) . '4:porti' . $peer['port'] . 'ee';
                }
            }
            // END IF Normal
        }
    }
    // IF Compact
    if ($_GET['compact']) {
        // 6-byte compacted peer info
        $response .= strlen($peers) . ':' . $peers;
        // END IF Compact
        // IF Not Compact
    } else {
        $response .= 'e';
    }
    // END IF Not Compact
    echo $response . 'e';
}
<?php

$peers = 'SELECT
		`p`.`info_hash` AS `info_hash`,
		SUM(`p`.`state`=\'1\') AS `seeders`,
		SUM(`p`.`state`=\'0\') AS `leechers`
	FROM `' . $settings['db_prefix'] . 'peers` AS `p`
	GROUP BY `info_hash`;';
$torrents = 'SELECT
		`p`.`info_hash` AS `info_hash`,
		`p`.`downloads` AS `downloads`
	FROM `' . $settings['db_prefix'] . 'torrents` AS `p`
	GROUP BY `info_hash`;';
$peers = mysqli_query($connection, $peers);
$torrents = mysqli_query($connection, $torrents);
if (!$peers || !$torrents) {
    tracker_error('Unable to scrape for that torrent.');
} else {
    require_once $settings['onces'] . 'once.scrape.output.php';
}
<?php

// IF database is configured
if (!empty($settings['db_host']) && !empty($settings['db_user']) && !empty($settings['db_name'])) {
    // IF persistent connection
    if ($settings['db_persist']) {
        $settings['db_host'] = 'p:' . $settings['db_host'];
    }
    $connection = mysqli_connect($settings['db_host'], $settings['db_user'], $settings['db_pass'], $settings['db_name']);
    if (!$connection) {
        tracker_error('Connection Failed. Tracker may be mis-configured. ' . mysqli_connect_error($connection));
    }
} else {
    tracker_error('Connection Failed. Tracker is not configured.');
}
Esempio n. 20
0
    $_GET['no_peer_id'] = 0;
} else {
    $_GET['no_peer_id'] += 0;
}
// string - ip - optional
// ip address the peer requested to use
if (isset($_GET['ip']) && $_SERVER['tracker']['external_ip'] === 'true') {
    // dotted decimal only
    $_GET['ip'] = trim($_GET['ip'], '::ffff:');
    if (!ip2long($_GET['ip'])) {
        tracker_error('invalid ip, dotted decimal only');
    }
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
    $_GET['ip'] = trim($_SERVER['REMOTE_ADDR'], '::ffff:');
} else {
    tracker_error('could not locate clients ip');
}
// integer - numwant - optional
// number of peers that the client has requested
if (!isset($_GET['numwant'])) {
    $_GET['numwant'] = $_SERVER['tracker']['default_peers'];
} elseif ($_GET['numwant'] + 0 > $_SERVER['tracker']['max_peers']) {
    $_GET['numwant'] = $_SERVER['tracker']['max_peers'];
} else {
    $_GET['numwant'] += 0;
}
// Handle Request //////////////////////////////////////////////////////////////////////////////////
// open database
peertracker::open();
// make info_hash & peer_id SQL friendly
$_GET['info_hash'] = peertracker::$api->escape_sql($_GET['info_hash']);
        $sql .= ' ORDER BY RAND();';
    }
}
// IF Compact
if ($peer['compact']) {
    $peers = '';
    $peersv6 = '';
    // END IF Compact
    // IF Not Compact
} else {
    $response .= 'l';
}
// END IF Not Compact
$query = mysqli_query($connection, $sql);
if (!$query) {
    tracker_error('Failed to select peers.');
} else {
    while ($return = mysqli_fetch_assoc($query)) {
        // IF Compact
        if ($peer['compact']) {
            if ($return['compactv4'] != null) {
                $peers .= hex2bin($return['compactv4']);
            }
            if ($return['compactv6'] != null) {
                $peersv6 .= hex2bin($return['compactv6']);
            }
            // END IF Compact
        } else {
            // IF IPv4
            if ($return['ipv4'] != null) {
                $response .= 'd2:ip' . strlen($return['ipv4']) . ':' . $return['ipv4'] . '4:porti' . $return['portv4'];
if (isset($_GET['ipv6'])) {
    $addresses[] = $_GET['ipv6'];
}
if (isset($_SERVER['REMOTE_ADDR'])) {
    $addresses[] = $_SERVER['REMOTE_ADDR'];
}
// If we're honoring X_FORWARDED_FOR, we check and use that first if its present.
if (isset($_SERVER['HTTP_CLIENT_IP']) && $settings['honor_xff']) {
    $addresses[] = $_SERVER['HTTP_CLIENT_IP'];
}
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $settings['honor_xff']) {
    $addresses[] = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
// Error if we can't find any addresses.
if (!count($addresses)) {
    tracker_error('Unable to obtain client IP');
}
// Reverse so we prioritise
$addresses = array_reverse($addresses);
////	Find Definites
// Find the highest possible rank for
// IPv4 and IPv6, plus their associated ports.
foreach ($addresses as $address) {
    // Check IPv4
    if (!$peer['ipv4']) {
        // Trim IPv6 Padding
        $address_ipv4 = trim($address, '::ffff:');
        // Try and find a port
        if (strpos($address_ipv4, ':') !== false) {
            $address_ipv4 = explode(':', $address_ipv4);
            $address_portv4 = $address_ipv4[1];
Esempio n. 23
0
 if (strpos($_GET['ip'], ':') !== false) {
     $_GET['ip'] = explode(':', $_GET['ip']);
     $_GET['port'] = $_GET['ip'][1];
     $_GET['ip'] = $_GET['ip'][0];
 }
 // TODO Add IPv6 Support
 // https://github.com/eustasy/phoenix/issues/3
 if (!ip2long($_GET['ip'])) {
     tracker_error('Invalid IP, dotted decimal only. No IPv6.');
 }
 // BEGIN OPTIONAL ITEMS
 ////	port
 // Required
 // An integer representing the port the peer is using.
 if (!isset($_GET['port']) || !is_numeric($_GET['port'])) {
     tracker_error('Client listening port is invalid.');
 }
 $_GET['port'] = intval($_GET['port']);
 ////	Left
 // Optional
 // An integer representing the remaining bytes to download.
 if (isset($_GET['left'])) {
     if (is_numeric($_GET['left']) && $_GET['left'] == 0) {
         $_GET['left'] = 0;
         $settings['seeding'] = 1;
     } else {
         $_GET['left'] = intval($_GET['left']);
         $settings['seeding'] = 0;
     }
 } else {
     $_GET['left'] = -1;
Esempio n. 24
0
    // IF NOT STATS
} else {
    // IF SCRAPE
    if (isset($_GET['info_hash']) && strlen($_GET['info_hash']) == 40 && ($settings['open_tracker'] || in_array($_GET['info_hash'], $torrents))) {
        // Perform a Scrape on the torrent.
        require_once __DIR__ . '/function.torrent.scrape.php';
        torrent_scrape();
        // END IF SCRAPE
        // IF FULL SCRAPE
    } else {
        if ($settings['full_scrape']) {
            // Scrape the full tracker.
            require_once __DIR__ . '/function.tracker.scrape.php';
            tracker_scrape();
            // END IF FULL SCRAPE
            // IF NOT ALLOWED TO SCRAPE
        } else {
            // IF ERROR TORRENT
            if (isset($_GET['info_hash'])) {
                tracker_error('Torrent is not allowed.');
                // END IF ERROR TORRENT
                // IF ERROR TRACKER
            } else {
                tracker_error('Tracker scraping is not allowed.');
            }
            // END IF ERROR TRACKER
        }
    }
    // END IF NOT ALLOWED TO SCRAPE
}
// END IF NOT STATS
Esempio n. 25
0
 public static function stats()
 {
     // statistics
     $stats = self::$api->fetch_once('SELECT SUM(state=1), SUM(state=0), ' . 'COUNT(DISTINCT info_hash) ' . "FROM `{$_SERVER['tracker']['db_prefix']}peers` ") or tracker_error('failed to retrieve tracker statistics');
     // output format
     switch ($_GET['stats']) {
         // xml
         case 'xml':
             header('Content-Type: text/xml');
             echo '<?xml version="1.0" encoding="ISO-8859-1"?>' . '<tracker version="$Id: tracker.mysql.php 164 2010-01-23 22:08:58Z trigunflame $">' . '<peers>' . number_format($stats[0] + $stats[1]) . '</peers>' . '<seeders>' . number_format($stats[0]) . '</seeders>' . '<leechers>' . number_format($stats[1]) . '</leechers>' . '<torrents>' . number_format($stats[2]) . '</torrents></tracker>';
             break;
             // json
         // json
         case 'json':
             header('Content-Type: text/javascript');
             echo '{"tracker":{"version":"$Id: tracker.mysql.php 164 2010-01-23 22:08:58Z trigunflame $",' . '"peers": "' . number_format($stats[0] + $stats[1]) . '",' . '"seeders":"' . number_format($stats[0]) . '",' . '"leechers":"' . number_format($stats[1]) . '",' . '"torrents":"' . number_format($stats[2]) . '"}}';
             break;
             // standard
         // standard
         default:
             echo '<!doctype html><html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8">' . '<title>PeerTracker: $Id: tracker.mysql.php 164 2010-01-23 22:08:58Z trigunflame $</title>' . '<body><pre>' . number_format($stats[0] + $stats[1]) . ' peers (' . number_format($stats[0]) . ' seeders + ' . number_format($stats[1]) . ' leechers) in ' . number_format($stats[2]) . ' torrents</pre></body></html>';
     }
 }
Esempio n. 26
0
 public static function stats()
 {
     // statistics
     $stats = pg_fetch_row(pg_query(self::$db, 'SELECT SUM(CASE WHEN state=1 THEN 1 ELSE 0 END), ' . 'SUM(CASE WHEN state=0 THEN 1 ELSE 0 END), ' . 'COUNT(DISTINCT info_hash) ' . "FROM {$_SERVER['tracker']['db_prefix']}peers;")) or tracker_error('failed to retrieve tracker statistics');
     // output format
     switch ($_GET['stats']) {
         // xml
         case 'xml':
             header('Content-Type: text/xml');
             echo '<?xml version="1.0" encoding="ISO-8859-1"?>' . '<tracker version="$Id: tracker.postgresql.php 149 2009-11-16 23:20:06Z trigunflame $">' . '<peers>' . number_format($stats[0] + $stats[1]) . '</peers>' . '<seeders>' . number_format($stats[0]) . '</seeders>' . '<leechers>' . number_format($stats[1]) . '</leechers>' . '<torrents>' . number_format($stats[2]) . '</torrents></tracker>';
             break;
             // json
         // json
         case 'json':
             header('Content-Type: text/javascript');
             echo '{"tracker":{"version":"$Id: tracker.postgresql.php 149 2009-11-16 23:20:06Z trigunflame $",' . '"peers": "' . number_format($stats[0] + $stats[1]) . '",' . '"seeders":"' . number_format($stats[0]) . '",' . '"leechers":"' . number_format($stats[1]) . '",' . '"torrents":"' . number_format($stats[2]) . '"}}';
             break;
             // standard
         // standard
         default:
             echo '<!doctype html><html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8">' . '<title>PeerTracker: $Id: tracker.postgresql.php 149 2009-11-16 23:20:06Z trigunflame $</title>' . '<body><pre>' . number_format($stats[0] + $stats[1]) . ' peers (' . number_format($stats[0]) . ' seeders + ' . number_format($stats[1]) . ' leechers) in ' . number_format($stats[2]) . ' torrents</pre></body></html>';
     }
 }