Login
Username: Password:
Remember me

[Sign Up] / [Recover Password]

Theme / Language

Popular
New Torrents
new name
Size: 18.40 MB
Most active
new name
S: 74 L: 3
New Posters
BETA2 - Announce.php
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / V4 / Downloads & Updates

2020-08-26 02:24:31
New Announce : bugs

1. ) Started time does not insert to database yet.

2. ) Peers/Seeds are not yet accounted for yet for local torrents. Public seeds show in client without issues.  [color=rgb(44, 130, 201)]Fixed[/color]

3. ) Completed torrents are not added to 'completed' table - [color=rgb(226, 80, 65)]is this necessary? We handle this now in peer table[/color]

4. ) Max slots feature is not yet added - [color=rgb(226, 80, 65)]is this necessary? It was a old mod. [/color]

5. ) Client/Agent ID is not added to peer table yet.[color=rgb(226, 80, 65)]  [/color][color=rgb(44, 130, 201)]Fixed[/color]

Everything else is perfect. I've tested all night various of my own private torrents off seedbox and no problems, just no stats currently! Amazing speeds and very light script.

I imported a few TT-only announce features into the new announce with full success.

--

Announce has taken longer than expected, but due to the fact that it is a complete redo of the old aging TT announce script that will see dust soon (and mass depreciation).

[color=rgb(226, 80, 65)]is this necessary? [/color]means I can easily do it, but do we need it? Please let me know so I can add these in if their a staple of your tracker.

Final announce.php => to be added to BETA2 (with limits listed above in post)



<?

require_once '../site.php';
require_once '../bencoding.php';
$db->connect();

$db->query_params('DELETE FROM peers WHERE last_announce + ' . $db->interval('1 hour') . ' < CURRENT_TIMESTAMP');

header('Content-Type: text/plain');

function fail($reason) {
   die(bencode(array('failure reason' => $reason)));
}

function ip2bytes($ip) {
   $res = '';
   $parts = explode('.', $ip);
   if (count($parts) !== 4) return null;
   for ($i = 0; $i < 4; $i++) {
       $b = $parts[$i];
       if (!is_numeric($b)) return null;
       $b = intval($b);
       if ($b < 0 || $b >= 256) return null;
       $res .= chr($b);
   }
   return $res;
}

function isConnectable($ip, $port) {
   $fp = @fsockopen($ip, $port, $errno, $errstr, 0.1);
   if (!$fp) {
       return false;
   } else {
       fclose($fp);
       return true;
   }
}

$keys = array(
   'downloaded' => true,
   'uploaded' => true,
   'passkey' => true,
   'username' => false,
   'torrent_id' => false,
   'token' => false,
   'info_hash' => true,
   'peer_id' => true,
   'port' => true,
   'no_peer_id' => false,
   'ip' => false,
   'numwant' => false,
   'event' => false,
   'left' => true,
   'compact' => false,
);

$data = array();
foreach ($keys as $key => $req) {
   if (array_key_exists($key, $_GET)) {
       $data[$key] = $_GET[$key];
   } else if ($req) {
       fail(sprintf('missing key: %s', $key));
   }
}

// Added. Check if seeder
$seeder = ($data['left'] == 0) ? 'yes' : 'no';
$data['info_hash'] = bin2hex($data['info_hash']);
$data['peer_id'] = $data['peer_id']; // bin2hex to scramble peer ID (we wont get client version/name)

// Added. Get client info and strip out the rest
$clientstring = mb_substr($data['peer_id'], 0, 7);
$client2 = str_replace('-', '', $clientstring);
$client  = str_replace('M', 'Bittorrent ', $client2);

$res = $db->query_params('SELECT user_id, passkey FROM users WHERE passkey = :passkey', array('passkey' => $data['passkey'])) or fail('db error line 56');
$user_row = $res->fetch() or fail("access denied line 57");

// added
if (array_key_exists('token', $data)) {
   $token = md5('token{' . $data['torrent_id'] . ',' . $user_row['passkey'] . '}');
   if (!hash_equals($token, $data['token'])) fail('access denied line 67');
}

if (!is_numeric($data['port'])) fail('invalid port');

$data['port'] = intval($data['port']);
if ($data['port'] < 1 || $data['port'] >= 65536) fail('invalid port');

if (array_key_exists('left', $data)) {
   if (!is_numeric($data['left'])) fail('invalid left');
   $data['left'] = intval($data['left']);
   if ($data['left'] < 0) fail('invalid left');
}

if (!array_key_exists('ip', $data)) {
   $data['ip'] = getip();

   $try_keys = array('HTTP_X_FORWARDED_FOR', 'HTTP_X_REAL_IP');
   foreach ($try_keys as $key) {
       if (array_key_exists($key, $_SERVER)) {
           $potential_ip = trim(explode(',', $_SERVER[$key])[0]);

           // Ignore private IP addresses
           if (strpos($potential_ip, '10.') === 0) continue;
           if (strpos($potential_ip, '192.168.') === 0) continue;
           // TODO: 172.16.0.0/12 is also private

           $data['ip'] = $potential_ip;
           break;
       }
   }
}

if (array_key_exists('numwant', $data)) {
   if (!is_numeric($data['numwant'])) fail('invalid numwant');
   $data['numwant'] = intval($data['numwant']);
   if ($data['numwant'] < 0) $data['numwant'] = 1000;
   if ($data['numwant'] > 1000) $data['numwant'] = 1000;
} else {
   $data['numwant'] = 30;
}

// Added. Check if user is connectable
if (isConnectable($data['ip'], $data['port'])): $connectable = 'yes'; else: $connectable = 'no'; endif;

$res = $db->query_params('SELECT torrent_id FROM torrents WHERE info_hash = :info_hash', array('info_hash' => $data['info_hash'])) or fail('db error');
$torrent_row = $res->fetch() or fail('no such torrent');

$valid = $db->query_params('SELECT count(*) FROM peers WHERE torrent_id = :torrent_id AND passkey = :passkey', array('torrent_id' => $torrent_row['torrent_id'], 'passkey' => $data['passkey'])) or fail('db error');

// Added. Check connection limits
if ($active = $valid->fetch()) {
   //if ($active >= 1 && $seeder == 'no') fail('Connection limit exceeded! You may only leech from one location at a time.');
   //if ($active >= 3 && $seeder == 'yes') fail('Connection limit exceeded!');  
}
 
$res = $db->query_params('SELECT peer_id, completed, seeder FROM peers WHERE user_id = :user_id AND torrent_id = :torrent_id AND chosen_peer_id = :chosen_peer_id', array('user_id' => $user_row['user_id'], 'torrent_id' => $torrent_row['torrent_id'], 'chosen_peer_id' => $data['peer_id'])) or fail('db error');
 
if (!($peer_row = $res->fetch())) {
 
   $res = $db->query_params('INSERT INTO peers (connectable, user_id, torrent_id, chosen_peer_id, ip, port, client) VALUES (:connectable, :user_id, :torrent_id, :chosen_peer_id, :ip, :port, :client)', array('connectable' => $connectable, 'user_id' => $user_row['user_id'], 'torrent_id' => $torrent_row['torrent_id'], 'chosen_peer_id' => $data['peer_id'], 'ip' => $data['ip'], 'port' => $data['port'], 'client' => $client), 'peer_id')  or fail('Cant insert peer.');
   $peer_row = array('peer_id' => $res);
 
} else {
   $db->query_params('UPDATE peers SET connectable = :connectable, to_go = :to_go, uploaded = :uploaded, downloaded = :downloaded, seeder = :seeder, client = :client, ip = :ip, port = :port, last_announce = CURRENT_TIMESTAMP WHERE peer_id = :peer_id', array('connectable' => $connectable, 'to_go' => $data['left'], 'uploaded' => $data['uploaded'], 'downloaded' => $data['downloaded'], 'seeder' => $seeder, 'client' => $client, 'ip' => $data['ip'], 'port' => $data['port'], 'peer_id' => $peer_row['peer_id'])) or fail('db error');
}

if (array_key_exists('left', $data)) {
 
   // Cheap fix until events are working
   if ($seeder === 'no'){
       $db->query_params('UPDATE torrents SET leechers = leechers + :addleecher WHERE torrent_id = :torrent_id', array('addleecher' => $db->encode_bool($data['left'] === 0), 'torrent_id' => $torrent_row['torrent_id'])) or fail('db error');
   }else{
       $db->query_params('UPDATE torrents SET seeders = seeders + :addseeder WHERE torrent_id = :torrent_id', array('addseeder' => $db->encode_bool($data['left'] === 0), 'torrent_id' => $torrent_row['torrent_id'])) or fail('db error');
   }
 
   $db->query_params('UPDATE peers SET completed = :completed, seeder = :seeder WHERE peer_id = :peer_id', array('completed' => $db->encode_bool($data['left'] === 0), 'seeder' => $seeder, 'peer_id' => $peer_row['peer_id']));
}

if (array_key_exists('event', $data) && $data['event'] === 'stopped') {
   $db->query_params('DELETE FROM peers WHERE peer_id = :peer_id', array('peer_id' => $peer_row['peer_id']));
}
 
$res = $db->query_params('SELECT count(nullif(completed,false)) AS complete, count(nullif(completed,true)) AS incomplete FROM peers WHERE torrent_id = :torrent_id', array('torrent_id' => $torrent_row['torrent_id'])) or fail('db error');
$comp_res = $res->fetch() or fail('db error');

$output = array(
   'interval' => 30 * 60,
   'complete' => intval($comp_res['complete']),
   'incomplete' => intval($comp_res['incomplete']),
   'peers' => array(),
);

$res = $db->query_params('SELECT chosen_peer_id, ip, port FROM peers WHERE torrent_id = :torrent_id AND peer_id != :peer_id ORDER BY ' . $db->random() . ' LIMIT :limit', array('torrent_id' => $torrent_row['torrent_id'], 'peer_id' => $peer_row['peer_id'], 'limit' => $data['numwant'])) or fail('db error');

while ($row = $res->fetch()) {
   $peer = array(
       'ip' => $row['ip'],
       'port' => intval($row['port']),
   );
   if (!array_key_exists('no_peer_id', $data)) {
       $peer['peer_id'] = hex2bin($row['chosen_peer_id']);
   }
   $output['peers'] []= $peer;
}

if (array_key_exists('compact', $data)) {
   $peer_data = '';
   foreach ($output['peers'] as $peer) {
       $bs = ip2bytes($peer['ip']);
       if (is_null($bs)) continue;
       $peer_data .= $bs;
       $peer_data .= chr($peer['port'] >> 8);
       $peer_data .= chr($peer['port'] & 0xff);
   }

   if (empty($output['peers']) || !empty($peer_data)) {
       $output['peers'] = $peer_data;
   }
}

echo bencode($output);
2020-08-26 08:40:58
great work mate, updating the announce and torrent bencoding is no easy task but this is a huge step forward fantastic work from the list i think completed is necessary
This leads to events, cant find anything online about this theres a few options which i know from  looking at snatchlist if someone downloads 1mb and runs i wanted it to count as snatched

this line in your announce

if (array_key_exists('event', $data) && $data['event'] === 'stopped') {
   $db->query_params('DELETE FROM peers WHERE peer_id = :peer_id', array('peer_id' => $peer_row['peer_id']));
}


if we could get more details on the event options finished, completed, stopped etc we could add specific queries add to completed, snatched, add client to db so maybe be able to add the things listed, thats how the current one runs  but the documentation online is so poor for this kind of stuff - the code looks great ill try this at weekend but if we could use events to target stuff i think it would be much more customisable
2020-08-26 09:28:22
Thanks man! Events key is true/false - can be set anytime. You are right, and I can't seem to get events working though,, so I got around it with a bool check in the class by adding or deducting 1 from seeders/leechers in torrents table based on completed or not and connectable or not (those 2 conditions), that seems to be doing the trick 'without events'.

Cheap trick, really as it is relying now on 'left' (to_go in peers table). It'll do for now. More deeper testing from ppl will be required for announce to be tuned right. For now, it works fine and has 2 fixes updated in post. It's pretty aware about things now with the clients and the users.

Better IP/port checking too in the function isConnectable() - old one was not working and was not compliant either. fclose() was not in the right place and yet checks were made anyway, doesn't make sense. Its been improved and fixed into a function now. I settled for fsockopen for flexibility sake. Not everyone has cURL installed on their box.

We need events, its really important. I'll keep tracking at it. Its supported and built in by default so its strange i am missing a key to that array when I set to true. Very odd. It worked before.
Users online

No users online

Search



Statistics

    Torrents

  • Tracking: 3
  • Todays torrents: 0
  • Seeders: 0
  • Leechers: 0
  • Peers: 0
  • Downloaded: 89.96 GB
  • Uploaded: 9.86 GB

  • Members

  • We have: 838
  • New today: 0
  • Guest today: 9

  • Online

  • Total online: 2
  • Members: 0
  • Guests online: 2
  • Comments: 3
  • Messages: 1,728