Skip to main content

Torrent

  • .torrent
  • Magnet Link
    • 替代 .torrent 文件,通过 URI 表示共享的文件信息。
  • Tracker
    • Peer Protocol
    • HTTP Tracker
    • UDP Tracker Protocol
    • Distributed Hash Table (DHT)
      • Kademlia
  • P2P Protocol
    • Handshake:初次连接时交换信息。
    • Piece:交换文件数据。
    • Have:通知拥有的数据块。
  • PEX - Peer Exchange
    • 让节点之间共享已知的 Peer 列表,减少对 Tracker 的依赖。
  • 分片(Piece)机制
    • 将大文件分成小片段(Piece),实现高效的并行下载。
    • 每个文件被分成多个 Piece,每个 Piece 都有一个 SHA-1 哈希值。
    • Peer 下载时验证每个 Piece 的完整性。
  • 超种(Super-Seeding)模式
    • 提高 Seeder 初次分发文件的效率。
    • Seeder 优先将不同的 Piece 传给不同的 Peer,减少重复传输。
    • 适用于文件刚发布、节点数较少的场景。
  • 扩展
    • uTP(Micro Transport Protocol):
      • 基于 UDP 的传输协议,减少网络拥塞。
    • Fast Extension:
      • 提高下载速度的功能,例如允许 Peer 请求跳跃式的 Piece。
    • Peer Identification:
      • 识别 Peer 的下载客户端(如 μTorrent、qBittorrent)。
SpecTitleStatus
BEP03The BitTorrent Protocol Specification🟢
BEP04Known Number Allocations🟢
BEP05DHT Protocol
BEP06Fast Extension
BEP07IPv6 Tracker Extension📝
BEP08Tracker Peer Obfuscation🟡
BEP09Extension for Peers to Send Metadata Files
BEP10Extension Protocol
BEP11Peer Exchange (PEX)
BEP12Multitracker Metadata Extension
BEP14Local Service Discovery
BEP15UDP Tracker Protocol
BEP16Superseeding📝
BEP17HTTP Seeding (Hoffman-style)📝
BEP18Search Engine Specification🟡
BEP19WebSeed - HTTP/FTP Seeding (GetRight-style)
BEP20Peer ID Conventions🟢
BEP21Extension for Partial Seeds📝
BEP22BitTorrent Local Tracker Discovery Protocol🟡
BEP23Tracker Returns Compact Peer Lists
BEP24Tracker Returns External IP📝
BEP26Zeroconf Peer Advertising and Discovery🟡
BEP27Private Torrents
BEP28Tracker exchange🟡
BEP29uTorrent transport protocol
BEP30Merkle tree torrent extension📝
BEP31Tracker Failure Retry Extension📝
BEP32IPv6 extension for DHT📝
BEP33DHT scrape📝
BEP34DNS Tracker Preferences📝
BEP35Torrent Signing📝
BEP36Torrent RSS feeds📝
BEP38Finding Local Data Via Torrent File Hints📝
BEP39Updating Torrents Via Feed URL📝
BEP40Canonical Peer Priority📝
BEP41UDP Tracker Protocol Extensions📝
BEP42DHT Security Extension📝
BEP43Read-only DHT Nodes📝
BEP44Storing arbitrary data in the DHT📝
BEP45Multiple-address operation for the BitTorrent DHT📝
BEP46Updating Torrents Via DHT Mutable Items📝
BEP47Padding files and extended file attributes📝
BEP48Tracker Protocol Extension: Scrape📝
BEP49Distributed Torrent Feeds📝
BEP50Publish/Subscribe Protocol📝
BEP51DHT Infohash Indexing📝
BEP52The BitTorrent Protocol Specification v2📝
BEP53Magnet URI extension - Select specific file indices for download📝
BEP54The lt_donthave extension📝
BEP55Holepunch extension
BEP1000Pending Standards Track Documents🟢

🟢 - Final & Active ✅ - Accepted 📝 - Draft 🟡 - Deferred

BEP03

.torrent

  • bencode meta file
metainfo.json
{
"announce": "",
"announce-list": [[""]],
"comment": "",
"created by": "",
"creation date": 0,
"encoding": "UTF-8",
"info": {
"name": "file",
"piece length": 262144,
"pieces": [[]],
"length": 0,
"files": [
{
"length": 0,
"path": ["file.mkv"]
}
]
},
// 非标准字段
"nodes": [["", 0]],
"saved by": "",
"save date": 0,
"hash": ""
}
  • announce 有时为 数组
  • info 为主要内容 - infohash 基于 sha1(info)
    • piece length
      • 源文件切分块大小
        • 2^18 = 256 K - BitTorrent 3.2+
        • 2^20 = 1 M
    • pieces - [][]byte
      • sh1 数组 - sha1 长为 20
      • md4
    • length - 单文件时存在
    • files - 目录时存在
      • path
      • length
caution
  • piece 可能包含多文件 - 有的客户端会生成 padding 文件

BEP05 DHT

  • router.bittorrent.com

BEP30 Merkle hash torrent extension

  • SHA1

BEP48

Scrape for HTTP Tracker

GET http://tracker/scrape?info_hash=xxxxxxxxxxxxxxxxxxxx&info_hash=yyyyyyyyyyyyyyyyyyyy
bencode
d5:filesd20:xxxxxxxxxxxxxxxxxxxxd8:completei11e10:downloadedi13772e10:incompletei19e
20:yyyyyyyyyyyyyyyyyyyyd8:completei21e10:downloadedi206e10:incompletei20eee
{
"files": {
"xxxxxxxxxxxxxxxxxxxx": { "complete": 11, "downloaded": 13772, "incomplete": 19 },
"yyyyyyyyyyyyyyyyyyyy": { "complete": 21, "downloaded": 206, "incomplete": 20 }
}
}

BEP52 BitTorrent v2

  • metainfo
    • announce
    • info
      • name
      • piece length
      • meta version=2
        • v1 不会设置
      • file tree
        • <path> - UTF-8, 不能有 . 和 .., root 必须是目录
          • 文件包含 空 key
          • length - 文件长度
          • pieces root
            • root hash of a merkle tree with a branching factor of 2
            • 32 bytes
            • N*16KiB
            • SHA2-256
      • files - 兼容 v1
        • attr=p 为 padding 文件
        • length
        • path
    • piece layers
      • root -> tree

  • infohash - sha2-256
    • 有时作为 torrent id 会截取 20 bytes