Beispiel #1
0
 /**
  * Announce code
  *
  * @access public
  * @param $passkey Passkey de l'utilisateur
  * @return Bencoded response for the torrent client
  */
 public function announce($passkey = null)
 {
     // Pas de passkey et pas de freeleech
     if (Config::get('other.freeleech') == false && $passkey == null) {
         return Response::make(Bencode::bencode(array('failure reason' => 'Passkey is invalid')), 200, array('Content-Type' => 'text/plain'));
     }
     $hash = bin2hex(Input::get('info_hash'));
     // Get hash
     if (!Config::get('other.freelech')) {
         $torrent = Torrent::where('info_hash', '=', $hash)->first();
         if (is_null($torrent)) {
             return Response::make(Bencode::bencode(array('failure reason' => 'Torrent not found')), 200, array('Content-Type' => 'text/plain'));
         }
     } else {
         $torrent = null;
     }
     $user = Config::get('other.freelech') == false ? User::where('passkey', '=', $passkey)->first() : null;
     // Get the user
     $client = Peer::where('md5_peer_id', '=', md5(Input::get('peer_id')))->first();
     // Get the current peer
     if ($client == null) {
         $client = new Peer();
     }
     // Crée un nouveau client si non existant
     Peer::deleteOldPeers();
     // Delete olds peers from database
     $peers = Peer::where('hash', '=', $hash)->take(50)->get()->toArray();
     // Liste des pairs
     $seeders = 0;
     $leechers = 0;
     foreach ($peers as &$p) {
         if ($p['left'] > 0) {
             $leechers++;
         } else {
             $seeders++;
         }
         // Compte le nombre de seeders
         unset($p['uploaded'], $p['downloaded'], $p['left'], $p['seeder'], $p['connectable'], $p['user_id'], $p['torrent_id'], $p['client'], $p['created_at'], $p['updated_at'], $p['md5_peer_id']);
     }
     // 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->hash = $hash;
         $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 ? true : false;
         $client->user_id = Config::get('other.freeleech') == false ? $user->id : null;
         $client->torrent_id = Config::get('other.freeleech') == false ? $torrent->id : null;
         $client->save();
     } elseif (Input::get('event') == 'completed') {
         if (Config::get('other.freeleech') == false) {
             $torrent->times_completed++;
             $torrent->save();
         }
         $client->left = 0;
         $client->seeder = true;
         $client->save();
     } elseif (Input::get('event') == 'stopped') {
         $client->delete();
     } else {
     }
     if (Config::get('other.freeleech') == false && $torrent != null && $user != null) {
         $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();
         // 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();
     }
     $res['interval'] = 60;
     // Set to 60 for debug
     $res['min interval'] = 30;
     // Set to 30 for debug
     $res['tracker_id'] = $client->md5_peer_id;
     // A string that the client should send back on its next announcements.
     $res['complete'] = $seeders;
     $res['incomplete'] = $leechers;
     $res['peers'] = $peers;
     return Response::make(Bencode::bencode($res), 200, array('Content-Type' => 'text/plain'));
 }