用于读取种子文件的 PHP 模块 [已关闭]
2022-08-30 23:12:06
有没有一个PHP模块,你可以用它来以编程方式读取种子来查找有关它的信息,例如Seeders?
有没有一个PHP模块,你可以用它来以编程方式读取种子来查找有关它的信息,例如Seeders?
我曾经在一个小型网站中使用过这些功能。想想我发现他们与一个名为OpenTracker的php bittorrent跟踪器或其他东西,但找不到网站...
但是,您不会在洪流文件中找到播种器。洪流文件仅包含有关文件,哈希代码和长度等的信息。我相信一些跟踪器信息。您将必须从跟踪器中获得多少播种机等。您可以在 BitTorrent.org 阅读有关原型的信息。我相信,通信是本编码的,所以你也可以使用这些函数。这意味着您只需要弄清楚要发送什么就可以取回您想要的东西。
注意:我没有写这三个函数。就像我说的,我在开源洪流跟踪器的来源中找到它们。函数没有注释,但函数名称以及print_r洪流文件的结果,您知道这些信息应该足以理解如何使用它们。我在底部添加了一些示例代码来展示我如何使用它们。他们成功了。
function bdecode($str) {
$pos = 0;
return bdecode_r($str, $pos);
}
function bdecode_r($str, &$pos) {
$strlen = strlen($str);
if (($pos < 0) || ($pos >= $strlen)) {
return null;
}
else if ($str{$pos} == 'i') {
$pos++;
$numlen = strspn($str, '-0123456789', $pos);
$spos = $pos;
$pos += $numlen;
if (($pos >= $strlen) || ($str{$pos} != 'e')) {
return null;
}
else {
$pos++;
return intval(substr($str, $spos, $numlen));
}
}
else if ($str{$pos} == 'd') {
$pos++;
$ret = array();
while ($pos < $strlen) {
if ($str{$pos} == 'e') {
$pos++;
return $ret;
}
else {
$key = bdecode_r($str, $pos);
if ($key == null) {
return null;
}
else {
$val = bdecode_r($str, $pos);
if ($val == null) {
return null;
}
else if (!is_array($key)) {
$ret[$key] = $val;
}
}
}
}
return null;
}
else if ($str{$pos} == 'l') {
$pos++;
$ret = array();
while ($pos < $strlen) {
if ($str{$pos} == 'e') {
$pos++;
return $ret;
}
else {
$val = bdecode_r($str, $pos);
if ($val == null) {
return null;
}
else {
$ret[] = $val;
}
}
}
return null;
}
else {
$numlen = strspn($str, '0123456789', $pos);
$spos = $pos;
$pos += $numlen;
if (($pos >= $strlen) || ($str{$pos} != ':')) {
return null;
}
else {
$vallen = intval(substr($str, $spos, $numlen));
$pos++;
$val = substr($str, $pos, $vallen);
if (strlen($val) != $vallen) {
return null;
}
else {
$pos += $vallen;
return $val;
}
}
}
}
function bencode($var) {
if (is_int($var)) {
return 'i' . $var . 'e';
}
else if (is_array($var)) {
if (count($var) == 0) {
return 'de';
}
else {
$assoc = false;
foreach ($var as $key => $val) {
if (!is_int($key)) {
$assoc = true;
break;
}
}
if ($assoc) {
ksort($var, SORT_REGULAR);
$ret = 'd';
foreach ($var as $key => $val) {
$ret .= bencode($key) . bencode($val);
}
return $ret . 'e';
}
else {
$ret = 'l';
foreach ($var as $val) {
$ret .= bencode($val);
}
return $ret . 'e';
}
}
}
else {
return strlen($var) . ':' . $var;
}
}
一些示例用法:
# Read a file
$content = file_get_contents("file.torrent");
$content_d = bdecode($content);
# Check if bdecode succeeded
if(empty($content_d)) exit('Something is wrong with the torrent. BDecode failed.');
# Calculate info_hash
$info_hash = sha1(bencode($content_d['info']), true);
# Calculate length
$length = 0;
function add_length($value, $key)
{
global $length;
if($key == 'length') $length += $value;
}
array_walk_recursive($content_d, 'add_length');