orpheusnet/bencode-torrent

PHP 库,用于解码和编码 BitTorrent BEncoded 数据,为 Gazelle 构建而成

v1.3.0 2023-10-13 00:09 UTC

This package is auto-updated.

Last update: 2024-09-19 07:44:49 UTC


README

Test Packagist Packagist PHP Version Support

PHP 库,用于编码和解码 BitTorrent BEncode 数据,主要针对在 Gazelle 中的使用。

用法

composer require orpheusnet/bencode-torrent

require('vendor/autoload.php');
$bencode = new \OrpheusNET\BencodeTorrent\BencodeTorrent();
$bencode->decodeFile('path/to/file.torrent');
var_dump($bencode->getName());
var_dump($bencode->getHexInfoHash());
var_dump($bencode->getFileList());

有关可用方法的更多信息,请参阅 BencodeTorrent 类。此外,有关编码/解码 BEncode 数据的底层库,请参阅 Bencode

描述

BEncode 是 BitTorrent 用于存储和传输松散结构化数据所使用的编码。它支持

  • 字节字符串
  • 整数
  • 列表
  • 字典(关联数组,其中键按字母顺序排序)

有关这些类型如何得到支持的更多信息,请参阅 BitTorrentSpecification#Bencoding

除此之外,期望 torrent 文件是包含至少以下键的 BEncoded 字典:announce(字节字符串)和 info(字典)。在 info 字典中,我们期望有 piece length(整数)和 pieces(字节字符串)。如果 torrent 只有一个文件,我们期望有 name(字节字符串)和 length(整数),而对于多文件 torrent,我们将有 name(字节字符串)和 files(列表),其中每个元素都是一个具有键 length(整数)和 path(字符串列表)的字典。

因此,该库在加载数据时会进行一些检查,以确保这些必填字段存在,否则会抛出异常。有关这些字段的更多信息,请参阅 BitTorrentSpecification#Metainfo_File_Structure

最后,该库主要针对在 Gazelle 中使用,因此库中包含一些实用函数,在那里它们有意义以完成以下事情

  • 确保 torrent 文件标记为 'private'
  • 在 torrents 中设置 'source'(以确保唯一的 info hash)
  • 清除不必要的字段,这些字段也可能泄露有关用户的信息(如 announce listcreated by
  • 生成 Gazelle 所期望的字符串文件列表以供显示

这基于(大致)WCD 的 Gazelle 中两个独立的 BEncode 库的代码(bencodetorrent.class.phptorrent.class.php),但去除了现在不再必要的 32 位模拟,并将其作为一个统一的库用于上传和下载 torrent 文件。