https://blog.rhilip.info/archives/1064/
根据文章中的评测,NexusPHP(benc.php)的种子解析性能非常低,即Bencode解码能力很低,(编码能力与其他库文件性能差不多),因此这里对种子文件解码部分代码进行修改,并与NexusPHP做到最大兼容.
https://raw.githubusercontent.com/Rhilip/RidPT/master/framework/Bencode/Bencode.php
使用这个编码库,并进行一定的代码修改
代码如下:
/**
*
* Rewrite from : https://github.com/OPSnet/bencode-torrent/blob/master/src/Bencode.php
*
* Created by PhpStorm.
* User: Rhilip
* Date: 2019/2/2
* Time: 20:00
*
* Edit by PhpStorm.
* Edit User: chenzhuyu
* Edit Date: 2019/08/26
* Edit Time: 09:00
*/
function bdec_file($path)
{
try{
return bdec_decode_new(file_get_contents($path, FILE_BINARY));
}
catch (Exception $e){
return;
}
}
function bdec_decode_new($data, &$pos = 0)
{
$start_decode = ($pos === 0);
if ($data[$pos] === 'd') {
$pos++;
$return = [];
while ($data[$pos] !== 'e') {
$key = bdec_decode_new($data, $pos)['value'];
$value = bdec_decode_new($data, $pos);
if ($key === null || $value === null) {
break;
}
if (!is_string($key)) {
throw new Exception('Invalid key type, must be string: ' . gettype($key));
}
$return[$key] = $value;
}
ksort($return);
$return = array('type' => "dictionary", 'value' => $return,'strlen' => 0, 'string' => 0);
$pos++;
} elseif ($data[$pos] === 'l') {
$pos++;
$return = [];
while ($data[$pos] !== 'e') {
$value = bdec_decode_new($data, $pos);
$return[]=$value;
}
$return = array('type' => "list", 'value' => $return,'strlen' => 0, 'string' => 0);
$pos++;
} elseif ($data[$pos] === 'i') {
$pos++;
$digits = strpos($data, 'e', $pos) - $pos;
$return = substr($data, $pos, $digits);
if ($return === '-0') {
throw new Exception('Cannot have integer value -0');
}
$multiplier = 1;
if ($return[0] === '-') {
$multiplier = -1;
$return = substr($return, 1);
}
if (!ctype_digit($return)) {
throw new Exception('Cannot have non-digit values in integer number: ' . $return);
}
$return = $multiplier * ((int)$return);
$pos += $digits + 1;
$return = array('type' => "integer", 'value' => $return,'strlen' => 0, 'string' => 0);
} else {
$digits = strpos($data, ':', $pos) - $pos;
$len = (int)substr($data, $pos, $digits);
$pos += ($digits + 1);
$return = substr($data, $pos, $len);
$pos += $len;
$return = array('type' => "string", 'value' => $return,'strlen' => 0, 'string' => 0);
}
if ($start_decode) {
if ($pos !== strlen($data)) {
throw new Exception('Could not fully decode bencode string');
}
}
return $return;
}
将上述代码追加到benc.php文件中,并覆盖函数bdec_file,
takeupload.php中的
//屏蔽以提高性能
//$dict=bdec(benc($dict));
//$infohash = pack("H*", sha1($info["string"]));
//修改为
$infohash = pack("H*", sha1(benc($info)));
//蚂蚁PT的torrenthashcheck.php也进行相应的修改
//虽然可能都不知道这个是做什么的
目前主要是种子文件解析会降低性能,因此只对bdec_file函数进行修改
其他功能,如字符串解码,数据编码等对性能影响很小,因此这里不做修改
Xdebug插件也要关闭
opcache,wincache之类的加速器要确保开启
gzip也用上