Witam
Znalazłem w necie skrypt który łączy się z trackerem i podaje nam seedy oraz peery do torrenta.
Problem w tym że są na stronie trackery które są obciążone oraz nieaktywne, a wtedy mam błąd "500 Internal Server Error".
Czy może ktoś pomóc z tym problemem
skrypt aktualizacyjny trackery
BEncode2.php
Znalazłem w necie skrypt który łączy się z trackerem i podaje nam seedy oraz peery do torrenta.
Problem w tym że są na stronie trackery które są obciążone oraz nieaktywne, a wtedy mam błąd "500 Internal Server Error".
Czy może ktoś pomóc z tym problemem
skrypt aktualizacyjny trackery
PHP:
<?php
require 'BEncode2.php';
/**
* hex2bin
*
* from php.net
*
* @param $h string
* @return string
*/
function hex2bin($h)
{
if( !is_string($h) ) return null;
if( strlen($h)%2 != 0 ) return null;
$r='';
for( $a=0; $a<strlen($h); $a+=2) { $r.=chr(hexdec($h{$a}.$h{($a+1)})); }
return $r;
}
class Tracker {
/**
* Tracker url
* @var string
*/
protected $url;
public function __construct($url)
{
$this->url = $url;
}
/**
* Get data from tracker
*
* @param $hash_info string
* @return mixed false if failed
*/
public function ask( $hash_info )
{
$url = $this->url
.'?info_hash='. urlencode(hex2bin( $hash_info ))
.'&key='. urlencode(hex2bin( $hash_info ))
.'&peer_id='. self::getPeerId()
.'&port=0&uploaded=0&downloaded=0&left=100';
$result = @file_get_contents($url);
if( !isset( $result ) && empty($result) ) return false;
try {
$result = BEncode2::decode2($result);
}
catch(Exception $e)
{
return false;
}
return $result;
}
/**
* Validate the url
* @param $url string
* @return bool
*/
public static function validUrl($url)
{
if( !isset( $url ) || empty($url) ) return false;
return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)|i', $url);
}
/**
* PeerID
* @var string
*/
private static $sId = null;
public static function getPeerId()
{
if( self::$sId === null )
{
self::$sId = urlencode( hex2bin( sha1( uniqid() ) ) );
}
return self::$sId;
}
}
class Torrent {
/**
* Torrent data
* @var array
*/
protected $data;
/**
* Hash (sha1)
* @var string
*/
protected $hash;
/**
* @var int
*/
protected $peers = 0;
/**
* @var int
*/
protected $seeds = 0;
/**
* Constructor
* @param $data string
*/
public function __construct($data)
{
$this->data = BEncode2::decode2($data);
$this->hash = sha1( BEncode2::encode2($this->data['info']) );
}
/**
*
* @return string
*/
public function getName()
{
return @$this->data['info']['name'];
}
/**
*
* @return array
*/
public function getFiles()
{
return @$this->data['info']['files'];
}
public function getHash()
{
return $this->hash;
}
public function getSeeds()
{
return $this->seeds;
}
public function getPeers()
{
return $this->peers;
}
/**
* Update tracker info
* @return bool
*/
public function updateTracker()
{
if( !is_array($this->data) ) return false;
$trackers = array();
if( @Tracker::validUrl($this->data['announce']) )
$trackers[] = $this->data['announce'];
if( is_array($this->data['announce-list']) )
{
foreach( $this->data['announce-list'] as $ar )
{
if( is_array($ar) )
$trackers = array_merge($trackers, $ar);
}
}
$trackers = array_unique($trackers);
$this->seeds = 0;
$this->peers = 0;
$result = false;
foreach( $trackers as $url )
{
if( @!Tracker::validUrl($url) ) continue;
$t = new Tracker($url);
$result = $t->ask( $this->getHash() );
if( !is_array($result) || isset($result['failure reason']) ) continue;
if( isset( $result['complete'] ) ) $this->seeds += (int)$result['complete'];
// if( isset( $result['complete'] ) && $this->seeds > (int)$result['complete'] ) $this->seeds = (int)$result['complete'];
if( isset( $result['incomplete'] ) ) $this->peers += (int)$result['incomplete'];
// if( isset( $result['incomplete'] ) && $this->peers > (int)$result['incomplete'] ) $this->peers = (int)$result['incomplete'];
$result = true;
}
return $result;
}
/**
* Create from torrent file
*
* @param $filename string
* @return Torrent
*/
public static function fromFile($filename)
{
$content = file_get_contents($filename);
return new Torrent($content);
}
}
?>
BEncode2.php
PHP:
<?php
class BEncode2 {
/**
* @var string
*/
protected static $haystack = null;
/**
* @var int
*/
protected static $offset = 0;
/**
* Decode
*
* @param $haystack string
* @return mixed
*/
public static function decode2($haystack)
{
if( !isset($haystack) ) return null;
self::$haystack = $haystack;
self::$offset = 0;
$result = self::parse2();
self::$haystack = null;
return $result;
}
/**
*
* @return array
*/
protected static function parseDictionarie2()
{
self::$offset++;
$result = array();
while( !self::end2() )
{
$key = self::parseString2();
if( strlen( $key ) < 1 ) throw new Exception('Unknown key name at offset:'.self::$offset, 2);
$result[ $key ] = self::parse2();
}
self::$offset += 1;
return $result;
}
/**
*
* @return array
*/
protected static function parseList2()
{
self::$offset++;
$result = array();
while( !self::end2() )
{
$result[] = self::parse2();
}
self::$offset += 1;
return $result;
}
/**
*
* @return int
*/
protected static function parseIntiger2()
{
self::$offset += 1;
$l = strpos(self::$haystack,'e', self::$offset) - self::$offset;
$value = (int)substr(self::$haystack, self::$offset, $l);
if( !is_numeric($value) ) throw new Exception('Parsed data is not integer \''.$value.'\' at offset:'.self::$offset, 3);
self::$offset += $l + 1;
return $value;
}
/**
*
* @return string
*/
protected static function parseString2()
{
$string = '';
$l = strpos(self::$haystack,':', self::$offset) - self::$offset;
$length = (int)substr(self::$haystack, self::$offset, $l);
self::$offset += $l+1;
$string = (string)substr(self::$haystack, self::$offset, $length);
self::$offset += $length;
if( !isset($string) ) $string = '';
return $string;
}
/**
*
* @return mixed
*/
protected static function parse2()
{
$result = null;
if( self::end2() ) return null;
switch( self::$haystack[self::$offset] )
{
case 'd':
$result = self::parseDictionarie2();
break;
case 'l':
$result = self::parseList2();
break;
case 'i':
$result = self::parseIntiger2();
break;
default:
if( is_numeric( self::$haystack[self::$offset] ) )
$result = self::parseString2();
else
throw new Exception('Unknown type. \''.self::$haystack[self::$offset].'\' at offset:'.self::$offset, 1);
}
return $result;
}
protected static function end2()
{
if( @!isset( self::$haystack[self::$offset] ) ) return true;
if( self::$haystack[self::$offset] == 'e' ) return true;
return false;
}
/* ENCODE */
/**
* Encode
* @param $mixed mixed
* @return string
*/
public static function encode2($mixed)
{
if( !isset($mixed) ) return '';
$result = '';
if( is_array($mixed) )
{
if( count($mixed) == 0 ) $type = 'd'; // empty array should be dictionarie
else
{
$type = 'l';
// find what type of array we have
// if there's one(or more) none numeric key we have dictionarie
foreach($mixed as $key => $e)
{
if( !is_int($key) )
{
$type = 'd';
break;
}
}
}
$result .= $type;
foreach($mixed as $key => $e)
{
if( $type == 'l' )
$result .= self::encode2($e);
else
{
$result .= (int)strlen($key).':'.$key.self::encode2($e);
}
}
$result .= 'e';
}
else
{
if( is_int($mixed) )
$result .= 'i'.(int)$mixed.'e';
else
{
$result .= (int)strlen($mixed).':'.$mixed;
}
}
return $result;
}
}
?>