Пример #1
0
 protected function sendUpdate()
 {
     $query = http_build_query(array('info_hash' => $this->download->getTorrent()->getInfoHash(), 'peer_id' => $this->download->getPeerID(), 'downloaded' => $this->download->getDownloaded(), 'uploaded' => $this->download->getUploaded(), 'left' => $this->download->getRemaining(), 'port' => 0, 'compact' => 1));
     $response = file_get_contents("{$this->url}?{$query}");
     if ($response === false || strlen($response) <= 0) {
         $this->error = "No HTTP response";
         return;
     }
     try {
         $response = Bencode::decode($response);
     } catch (Exception $e) {
         trigger_error($e);
         $this->error = "Bencoding error";
         return;
     }
     var_dump($response);
     if (empty($response)) {
         return;
     }
     $this->setNext(time() + (int) $response['interval']);
     if (empty($response['peers'])) {
         return;
     }
     if (is_string($response['peers'])) {
         $response['peers'] = $this->decodePeerList($response['peers']);
     }
     foreach ($response['peers'] as $item) {
         $this->download->addPeer($item['ip'], $item['port']);
     }
 }
Пример #2
0
 public function send()
 {
     $response = array();
     if ($this->failure_reason) {
         $response['failure reason'] = $this->failure_reason;
         echo Bencode::encode($response);
     } else {
         $response['interval'] = $this->interval;
         $response['complete'] = $this->complete;
         $response['incomplete'] = $this->incomplete;
         $response['peers'] = $this->peers;
         echo Bencode::encode($response);
     }
 }
Пример #3
0
 private function torrent_data_extractor($str)
 {
     $TorrentProcessor = new Bencode();
     try {
         $result = $TorrentProcessor->decode($str);
     } catch (Exception $e) {
         throw new TorrentInvalidError($e->getMessage());
     }
     $size = 0;
     if ($result['info']['length']) {
         $size = $result['info']['length'];
     } else {
         foreach ($result['info']['files'] as $file) {
             $size += $file['length'];
         }
     }
     // end sum files
     $infohash = sha1($TorrentProcessor->encode($result['info']));
     $filecount = 1;
     if ($result['info']['files']) {
         $filecount = count($result['info']['files']);
     }
     return array('name' => $result['info']['name'], 'size' => $size, 'infohash' => $infohash, 'files' => $filecount);
 }
Пример #4
0
 /**
  * bencode解码
  * @param  string $msg 要解码的数据
  * @return   mixed      解码后的数据
  */
 public static function decode($msg)
 {
     return Bencode::decode($msg);
 }
Пример #5
0
/**
 * Shorthand for {@link Bencode::decode()}.
 *
 * @param   string
 * @return  mixed
 * @throws  Exception
 * @see     Bencode::decode()
 */
function bdecode($value)
{
    return Bencode::decode($value);
}
Пример #6
0
 function __construct($password = NULL, $host = "127.0.0.1", $port = 10010)
 {
     $this->socket = stream_socket_client("udp://" . $host . ":" . $port, $errorno, $errorstr);
     if (!$this->socket) {
         die("Failed to connect, Error #{$errorno}: {$errorstr}");
     }
     fwrite($this->socket, Bencode::encode(array("q" => "ping")));
     // Try to ping it
     $returndata = fread($this->socket, $this->buffersize);
     if (!endsWith($returndata, "1:q4:ponge")) {
         die("{$returndata}");
     }
     $this->password = $password;
     $page = 0;
     while (True) {
         $request = array("q" => "Admin_availableFunctions", "args" => array("page" => $page));
         fwrite($this->socket, Bencode::encode($request));
         $result = Bencode::decode(fread($this->socket, $this->buffersize));
         foreach ($result['availableFunctions'] as $function => $description) {
             $this->functions[$function] = $description;
         }
         if (isset($result['more'])) {
             $page++;
         } else {
             break;
         }
     }
 }
Пример #7
0
 /**
  * Retourne le sha1 (hash) du torrent
  *
  */
 public static function getTorrentHash($decodedTorrent)
 {
     return sha1(Bencode::bencode($decodedTorrent['info']));
 }
Пример #8
0
 /** Read a torrent from raw data */
 public static function read($data)
 {
     $pos = 1;
     $len = strlen($data);
     if ($data[0] !== 'd') {
         throw new Exception("Torrent data doesn't contain bencoding");
     }
     $meta = array();
     $hash = null;
     while ($pos < $len) {
         if ($data[$pos] === 'e') {
             $pos++;
             break;
         }
         $key = Bencode::decodeString($data, $pos);
         if ($key === 'info') {
             $start = $pos;
             $value = Bencode::decodeValue($data, $pos);
             $end = $pos;
             $hash = sha1(substr($data, $start, $end - $start), true);
         } else {
             $value = Bencode::decodeValue($data, $pos);
         }
         $meta[$key] = $value;
     }
     if (!isset($hash)) {
         throw new Exception("Torrent doesn't contain 'info' data");
     }
     return new self($hash, $meta);
 }