今天是   本站已创建

闲看庭前花开花落,漫随天外云卷云舒...

现在的位置: 首页 云里雾里 >正文
 
2021年02月25日 ⁄ 冰柜 云里雾里 ⁄ 评论数 0+ ⁄ 被围观 118+
使用这个编码库,并进行一定的代码修改

代码如下:

/**
 *
 * 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)));

目前主要是种子文件解析会降低性能,因此只对bdec_file函数进行修改
其他功能,如字符串解码,数据编码等对性能影响很小,因此这里不做修改

Xdebug插件也要关闭
opcache,wincache之类的加速器要确保开启
gzip也用上


 
 
 
您可能还会对这些文章感兴趣!
 
目前有 118+ 人访问,有 0+ 条评论! 感谢支持!
 

发表评论:

 
广告
«    2025年5月    »
1234
567891011
12131415161718
19202122232425
262728293031
 
Copyright @2014-2024 808450.Cc. AllRights Reserved
Powered By Z-BlogPHP 1.7.4