/** * Delete all old peers from database * * */ public static function deleteOldPeers() { // Deleting old peers from the database foreach (Peer::all() as $peer) { if (time() - strtotime($peer->updated_at) > 10 * 60) { $peer->delete(); } } return true; }
/** * Announce code * * @access public * @param $passkey Passkey de l'utilisateur * @return Bencoded response for the torrent client */ public function announce($passkey = null) { //Log::info(Input::all()); // Correct info hash $infoHash = bin2hex(Input::get('info_hash') != null ? Input::get('info_hash') : Input::get('hash_id')); // Finding the torrent on the DB $torrent = Torrent::where('info_hash', '=', $infoHash)->first(); // Is torrent incorrect ? if ($torrent == null) { return Response::make(Bencode::bencode(array('failure reason' => 'This torrent does not exist'), 200, array('Content-Type' => 'text/plain'))); } // Is this a public tracker ? if (Config::get('other.freeleech') == false) { // Finding the user in the DB $user = User::where('passkey', '=', $passkey)->first(); // The user is incorrect ? if ($user == null) { return Response::make(Bencode::bencode(array('failure reason' => 'This user does not exist'), 200, array('Content-Type' => 'text/plain'))); } } // Finding the correct client/peer by the md5 of the peer_id $client = Peer::whereRaw('md5_peer_id = ?', [md5(Input::get('peer_id'))])->first(); // First time the client connect if ($client == null) { $client = new Peer(); } // Deleting old peers from the database foreach (Peer::all() as $peer) { if (time() - strtotime($peer->updated_at) > 50 * 60) { $peer->delete(); } } // Finding peers for this torrent on the database $peers = Peer::whereRaw('torrent_id = ?', array($torrent->id))->take(50)->get()->toArray(); // Removing useless data from the foreach ($peers as $k => $p) { unset($p['uploaded']); unset($p['downloaded']); unset($p['left']); unset($p['seeder']); unset($p['connectable']); unset($p['user_id']); unset($p['torrent_id']); unset($p['client']); unset($p['created_at']); unset($p['updated_at']); unset($p['md5_peer_id']); $peers[$k] = $p; } // Get the event of the tracker if (Input::get('event') == 'started' || Input::get('event') == null) { // Set the torrent data $client->peer_id = Input::get('peer_id'); $client->md5_peer_id = md5($client->peer_id); $client->ip = Request::getClientIp(); $client->port = Input::get('port'); $client->left = Input::get('left'); $client->uploaded = Input::get('uploaded'); $client->downloaded = Input::get('downloaded'); $client->seeder = $client->left > 0 ? false : true; if (Config::get('other.freeleech') == true) { $client->user_id = 0; } else { $client->user_id = $user->id; } $client->torrent_id = $torrent->id; $client->save(); $torrent->seeders = Peer::whereRaw('torrent_id = ? AND `left` = 0', array($torrent->id))->count(); $torrent->leechers = Peer::whereRaw('torrent_id = ? AND `left` > 0', array($torrent->id))->count(); $torrent->save(); } if (Input::get('event') == 'completed') { if ($client == null && $client->left > 0) { return Response::make(Bencode::bencode(array('failure reason' => 'Are you f*****g kidding me ?'), 200, array('Content-Type' => 'text/plain'))); } $torrent->times_completed++; $torrent->save(); $client->left = 0; $client->seeder = 0; $client->save(); } if (Input::get('event') == 'stopped') { if ($client != null) { $client->delete(); } else { return Response::make(Bencode::bencode(array('failure reason' => 'You don\'t have a life'), 200, array('Content-Type' => 'text/plain'))); } } // Savegarde le ratio de l'utilisateur if (Config::get('other.freeleech') == false) { // Modification de l'upload/download de l'utilisateur pour le ratio $user->uploaded += Input::get('uploaded') - $client->uploaded; $user->downloaded += Input::get('downloaded') - $client->downloaded; $user->save(); } $resp['interval'] = 60; // Set to 60 for debug $resp['min interval'] = 30; // Set to 30 for debug $resp['tracker_id'] = $client->md5_peer_id; // A string that the client should send back on its next announcements. $resp['complete'] = $torrent->seeders; $resp['incomplete'] = $torrent->leechers; $resp['peers'] = $peers; //Log::info($resp); return Response::make(Bencode::bencode($resp), 200, array('Content-Type' => 'text/plain')); }