Skip to content

Commit

Permalink
Merge pull request #8 from eustasy/procedural
Browse files Browse the repository at this point in the history
Procedural
  • Loading branch information
lewisgoddard committed Dec 28, 2014
2 parents 6d9606c + 8ff5652 commit 075e3cc
Show file tree
Hide file tree
Showing 22 changed files with 682 additions and 618 deletions.
2 changes: 0 additions & 2 deletions TRACKERS
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
https://ashrise.com:443/announce

udp://open.demonii.com:1337/announce

udp://tracker.ccc.de:80
Expand Down
182 changes: 105 additions & 77 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,118 @@

// TODO Secure

// error level
error_reporting(E_ERROR | E_PARSE);
//error_reporting(E_ALL & ~E_WARNING);
//error_reporting(E_ALL | E_STRICT | E_DEPRECATED);

// ignore disconnects
ignore_user_abort(true);

require_once __DIR__.'/phoenix.php';

// MySQL Setup
function setupMySQL() {

// open db
phoenix::open();
global $connection, $settings;

// setup
if (
phoenix::$api->query("DROP TABLE IF EXISTS `{$_SERVER['tracker']['db_prefix']}peers`") &&
phoenix::$api->query(
"CREATE TABLE IF NOT EXISTS `{$_SERVER['tracker']['db_prefix']}peers` (" .
"`info_hash` binary(20) NOT NULL," .
"`peer_id` binary(20) NOT NULL," .
"`compact` binary(6) NOT NULL," .
"`ip` char(15) NOT NULL," .
"`port` smallint(5) unsigned NOT NULL," .
"`state` tinyint(1) unsigned NOT NULL DEFAULT '0'," .
"`updated` int(10) unsigned NOT NULL," .
"PRIMARY KEY (`info_hash`,`peer_id`)" .
") ENGINE=MyISAM DEFAULT CHARSET=latin1"
) &&
phoenix::$api->query("DROP TABLE IF EXISTS `{$_SERVER['tracker']['db_prefix']}tasks`") &&
phoenix::$api->query(
"CREATE TABLE IF NOT EXISTS `{$_SERVER['tracker']['db_prefix']}tasks` (" .
"`name` varchar(5) NOT NULL," .
"`value` int(10) unsigned NOT NULL" .
") ENGINE=MyISAM DEFAULT CHARSET=latin1"
) &&
phoenix::$api->query(
"CREATE TABLE IF NOT EXISTS `{$_SERVER['tracker']['db_prefix']}torrents` (" .
"`name` varchar(255) NOT NULL," .
") ENGINE=MyISAM DEFAULT CHARSET=latin1"
)
) {
// Check Table
phoenix::$api->query('CHECK TABLE `'.$_SERVER['tracker']['db_prefix'].'peers`');
// no errors, hopefully???
$_GET['message'] = 'Your MySQL Tracker Database has been setup.';
require_once __DIR__.'/once.db.connect.php';
$success = true;

$result = mysqli_query($connection, 'DROP TABLE IF EXISTS `'.$settings['db_prefix'].'peers`');
if ( !$result ) {
echo mysqli_error($connection);
$success = false;
}
// error
else
{
$_GET['message'] = 'Could not setup the MySQL Database.';
$result = mysqli_query($connection, 'DROP TABLE IF EXISTS `'.$settings['db_prefix'].'tasks`');
if ( !$result ) {
echo mysqli_error($connection);
$success = false;
}
$result = mysqli_query($connection, 'DROP TABLE IF EXISTS `'.$settings['db_prefix'].'torrents`');
if ( !$result ) {
echo mysqli_error($connection);
$success = false;
}
$result = mysqli_query($connection,
'CREATE TABLE IF NOT EXISTS `'.$settings['db_prefix'].'peers` (' .
'`info_hash` binary(20) NOT NULL,' .
'`peer_id` binary(20) NOT NULL,' .
'`compact` binary(6) NOT NULL,' .
'`ip` char(15) NOT NULL,' .
'`port` smallint(5) unsigned NOT NULL,' .
'`left` int(100) unsigned NOT NULL DEFAULT \'0\',' .
'`state` tinyint(1) unsigned NOT NULL DEFAULT \'0\',' .
'`updated` int(10) unsigned NOT NULL,' .
'PRIMARY KEY (`info_hash`,`peer_id`)' .
') ENGINE=MyISAM DEFAULT CHARSET=latin1'
);
if ( !$result ) {
echo mysqli_error($connection);
$success = false;
}
$result = mysqli_query($connection,
'CREATE TABLE IF NOT EXISTS `'.$settings['db_prefix'].'tasks` (' .
'`name` varchar(5) NOT NULL,' .
'`value` int(10) unsigned NOT NULL' .
') ENGINE=MyISAM DEFAULT CHARSET=latin1'
);
if ( !$result ) {
echo mysqli_error($connection);
$success = false;
}
$result = mysqli_query($connection,
'CREATE TABLE IF NOT EXISTS `'.$settings['db_prefix'].'torrents` (' .
'`name` varchar(255) NOT NULL' .
') ENGINE=MyISAM DEFAULT CHARSET=latin1'
);
if ( !$result ) {
echo mysqli_error($connection);
$success = false;
}
$result = mysqli_query($connection, 'CHECK TABLE `'.$settings['db_prefix'].'peers`');
if ( !$result ) {
echo mysqli_error($connection);
$success = false;
}
$result = mysqli_query($connection, 'CHECK TABLE `'.$settings['db_prefix'].'tasks`');
if ( !$result ) {
echo mysqli_error($connection);
$success = false;
}
$result = mysqli_query($connection, 'CHECK TABLE `'.$settings['db_prefix'].'torrents`');
if ( !$result ) {
echo mysqli_error($connection);
$success = false;
}

phoenix::close();
if ( $success ) {
$_GET['message'] = 'Your MySQL Tracker Database has been setup.';
} else {
$_GET['message'] = 'Could not setup the MySQL Database.';
}

}

// MySQL Optimizer
function optimizeMySQL() {

// open db
phoenix::open();
global $connection, $settings;

require_once __DIR__.'/once.db.connect.php';

// optimize
if (
phoenix::$api->query("CHECK TABLE `{$_SERVER['tracker']['db_prefix']}peers`") &&
phoenix::$api->query("ANALYZE TABLE `{$_SERVER['tracker']['db_prefix']}peers`") &&
phoenix::$api->query("REPAIR TABLE `{$_SERVER['tracker']['db_prefix']}peers`") &&
phoenix::$api->query("OPTIMIZE TABLE `{$_SERVER['tracker']['db_prefix']}peers`")
)
{
// no errors, hopefully???
$_GET['notice'] = 'yes';
mysqli_query($connection, 'CHECK TABLE `'.$settings['db_prefix'].'peers`') &&
mysqli_query($connection, 'ANALYZE TABLE `'.$settings['db_prefix'].'peers`') &&
mysqli_query($connection, 'REPAIR TABLE `'.$settings['db_prefix'].'peers`') &&
mysqli_query($connection, 'OPTIMIZE TABLE `'.$settings['db_prefix'].'peers`') &&
mysqli_query($connection, 'CHECK TABLE `'.$settings['db_prefix'].'tasks') &&
mysqli_query($connection, 'ANALYZE TABLE `'.$settings['db_prefix'].'tasks') &&
mysqli_query($connection, 'REPAIR TABLE `'.$settings['db_prefix'].'tasks') &&
mysqli_query($connection, 'OPTIMIZE TABLE `'.$settings['db_prefix'].'tasks') &&
mysqli_query($connection, 'CHECK TABLE `'.$settings['db_prefix'].'torrents') &&
mysqli_query($connection, 'ANALYZE TABLE `'.$settings['db_prefix'].'torrents') &&
mysqli_query($connection, 'REPAIR TABLE `'.$settings['db_prefix'].'torrents') &&
mysqli_query($connection, 'OPTIMIZE TABLE `'.$settings['db_prefix'].'torrents')
) {
$_GET['message'] = 'Your MySQL Tracker Database has been optimized.';
}
// error
else
{
$_GET['notice'] = 'no';
} else {
$_GET['message'] = 'Could not optimize the MySQL Database.';
}

// close
phoenix::close();
}


Expand All @@ -96,7 +122,7 @@ function optimizeMySQL() {
// MySQL
if (
$_GET['do'] == 'setup' &&
$_SERVER['tracker']['db_name']
$settings['db_name']
) {
setupMySQL();
} else if ($_GET['do'] == 'optimize') {
Expand Down Expand Up @@ -206,19 +232,19 @@ function optimizeMySQL() {
// Tables Exist
$tables = array('peers', 'tasks', 'torrents');
$actual = 0;
phoenix::open();
require_once __DIR__.'/once.db.connect.php';
foreach ( $tables as $table ) {
$sql = 'SELECT * FROM `information_schema`.`TABLES`';
$sql .= " WHERE TABLE_SCHEMA = '{$_SERVER['tracker']['db_name']}' AND TABLE_NAME = '{$_SERVER['tracker']['db_prefix']}{$table}'";
$result = phoenix::$api->query($sql);
if ( !$result->num_rows ) {
$sql = 'SELECT * FROM `information_schema`.`TABLES` ';
$sql .= 'WHERE TABLE_SCHEMA = \''.$settings['db_name'].'\' AND TABLE_NAME = \''.$settings['db_prefix'].$table.'\'';
$result = mysqli_query($connection, $sql);
$count = mysqli_num_rows($result);
if ( !$count ) {
echo '
<p class="box background-pomegranate color-clouds">The table "'.$table.'" is not installed.</td>';
} else {
$actual += $result->num_rows;
$actual += $count;
}
}
phoenix::close();
if ( count($tables) == $actual ) {
echo '
<p class="box background-green-sea color-clouds">All your tables are installed.</td>';
Expand All @@ -244,15 +270,17 @@ function optimizeMySQL() {
// TODO
// Buttons should be POST form submits to prevent repeat on reload.
// Buttons should disable on click to prevent double submission.
if ( $_SERVER['tracker']['db_name'] ) {
if ( $settings['db_reset'] ) {
echo '
<p class="text-left">Install, Upgrade and Reset <a class="button background-belize-hole color-clouds float-right" href="?do=setup">Setup</a></p>';
<p class="text-left">Install, Upgrade and Reset <a class="button background-belize-hole color-clouds float-right" href="?do=setup">Setup</a></p>
<p class="text-left">Check, Analyze, Repair and Optimize <a class="button background-belize-hole color-clouds float-right" href="?do=optimize">Optimize</a></p>';
} else {
echo '
<p class="text-left color-asbestos">Install, Upgrade and Reset <span class="button background-clouds float-right">Disabled</span></p>';
<p class="text-left color-asbestos">Install, Upgrade and Reset <span class="button background-clouds float-right">Disabled</span></p>
<p class="text-left">Check, Analyze, Repair and Optimize <a class="button background-belize-hole color-clouds float-right" href="?do=optimize">Optimize</a></p>
<p class="box background-pomegranate color-clouds">You should set $settings[\'db_reset\'] to false to disable resets,<br>
or delete <code>admin.php</code> when you\'re up and running.</td>';
}
echo '
<p class="text-left">Check, Analyze, Repair and Optimize <a class="button background-belize-hole color-clouds float-right" href="?do=optimize">Optimize</a></p>';

}

Expand Down
68 changes: 39 additions & 29 deletions announce.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,28 @@
// IF MAGIC QUOTES
if ( get_magic_quotes_gpc() ) {
// Strip auto-escaped data.
$_GET['info_hash'] = stripslashes($_GET['info_hash']);
$_GET['peer_id'] = stripslashes($_GET['peer_id']);
if ( isset($_GET['info_hash']) ) {
$_GET['info_hash'] = stripslashes($_GET['info_hash']);
}
if ( isset($_GET['peer_id']) ) {
$_GET['peer_id'] = stripslashes($_GET['peer_id']);
}
} // END IF MAGIC QUOTES

if (
// 20-bytes - info_hash
// sha-1 hash of torrent metainfo
!isset($_GET['info_hash']) ||
strlen($_GET['info_hash']) != 20
(
strlen($_GET['info_hash']) != 20 &&
strlen($_GET['info_hash']) != 40
)
) {
tracker_error('Torrent Hash is invalid.');

// Check torrent is allowed when private.
} else if (
!$_SERVER['tracker']['open_tracker'] &&
!$settings['open_tracker'] &&
!in_array(bin2hex($_GET['info_hash']), $torrents) &&
!in_array($_GET['info_hash'], $torrents)
) {
Expand All @@ -47,13 +54,18 @@

// integer - left
// number of bytes left for the peer to download
if (
isset($_GET['left']) &&
is_numeric($_GET['left'])
) {
$_SERVER['tracker']['seeding'] = ($_GET['left'] > 0 ? 0 : 1);
if ( isset($_GET['left']) ) {
if (
is_numeric($_GET['left']) &&
$_GET['left'] == 0
) {
$settings['seeding'] = 1;
} else {
$settings['seeding'] = 0;
}
} else {
$_SERVER['tracker']['seeding'] = 0;
$_GET['left'] = -1;
$settings['seeding'] = 0;
}

// integer boolean - compact - optional
Expand All @@ -64,19 +76,19 @@
isset($_GET['compact']) &&
intval($_GET['compact']) > 0
) ||
$_SERVER['tracker']['force_compact']
$settings['force_compact']
) {
$_GET['compact'] = 1;
} else {
$_GET['compact'] += 0;
$_GET['compact'] = 0;
}

// integer boolean - no_peer_id - optional
// omit peer_id in dictionary announce response
if ( !isset($_GET['no_peer_id']) ) {
$_GET['no_peer_id'] = 0;
} else {
$_GET['no_peer_id'] += 0;
$_GET['no_peer_id'] = intval($_GET['no_peer_id']);
}

// string - ip - optional
Expand All @@ -87,7 +99,7 @@

if (
isset($_GET['ip']) &&
$_SERVER['tracker']['external_ip']
$settings['external_ip']
) {
// dotted decimal only
$_GET['ip'] = trim($_GET['ip'],'::ffff:');
Expand All @@ -108,30 +120,28 @@
// integer - numwant - optional
// number of peers that the client has requested
if ( !isset($_GET['numwant']) ) {
$_GET['numwant'] = $_SERVER['tracker']['default_peers'];
} else if ( ( $_GET['numwant'] + 0 ) > $_SERVER['tracker']['max_peers'] ) {
$_GET['numwant'] = $_SERVER['tracker']['max_peers'];
$_GET['numwant'] = $settings['default_peers'];
} else if ( intval($_GET['numwant']) > $settings['max_peers'] ) {
$_GET['numwant'] = $settings['max_peers'];
} else {
$_GET['numwant'] += 0;
$_GET['numwant'] = intval($_GET['numwant']);
}

// Open Database
phoenix::open();

// Make info_hash & peer_id SQL friendly
$_GET['info_hash'] = phoenix::$api->escape_sql($_GET['info_hash']);
$_GET['peer_id'] = phoenix::$api->escape_sql($_GET['peer_id']);
require_once __DIR__.'/once.db.connect.php';
$_GET['info_hash'] = mysqli_real_escape_string($connection, $_GET['info_hash']);
$_GET['peer_id'] = mysqli_real_escape_string($connection, $_GET['peer_id']);

// Track Client
phoenix::event();
require_once __DIR__.'/function.peer.event.php';
peer_event();

// Clean Up
phoenix::clean();
require_once __DIR__.'/function.tracker.clean.php';
tracker_clean();

// Announce Peers
phoenix::peers();

// Close Database
phoenix::close();
require_once __DIR__.'/function.torrent.announce.php';
torrent_announce();

}
Loading

0 comments on commit 075e3cc

Please sign in to comment.