torrent

package
v4.2.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2026 License: AGPL-3.0 Imports: 20 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// DefaultPieceSize 默认分片大小 10MB,与天翼云 CAS 分片大小一致
	DefaultPieceSize int64 = 10 * 1024 * 1024

	// CASExtensionKey torrent 根字典中的 CAS 扩展 key
	CASExtensionKey = "x-cas"

	// CASSliceSizeKey CAS 分片大小 key
	CASSliceSizeKey = "slice_size"
	// CASSliceMD5sKey 每片 MD5 列表 key
	CASSliceMD5sKey = "slice_md5s"
	// CASSliceMD5Key 最终 sliceMd5 key
	CASSliceMD5Key = "slice_md5"
	// CASFileMD5Key 整文件 MD5 key
	CASFileMD5Key = "file_md5"
	// CASCloudKey 云盘类型 key
	CASCloudKey = "cloud"

	// Cloud189 identifies the 天翼云 (189) PC driver CAS slice rule.
	Cloud189 = "189"
	// Cloud115 identifies the 115 driver rapid-upload rule (whole-file SHA1).
	Cloud115 = "115"
	// CloudAliyundriveOpen identifies the aliyundrive_open rapid-upload rule (whole-file SHA1).
	CloudAliyundriveOpen = "aliyundrive_open"

	// OpenListExtensionKey is the optional root-level extension key.
	OpenListExtensionKey = "x-openlist"
	// OSSFormat identifies OpenList sharing seed JSON documents.
	OSSFormat = "openlist-sharing-seed"
	// OSSVersion is the currently supported sharing seed schema version.
	OSSVersion = 1
	// DefaultMaxSeedSize bounds untrusted seed documents.
	DefaultMaxSeedSize int64 = 10 * 1024 * 1024
	// DefaultMaxSeedFiles bounds file-list fan-out in untrusted seeds.
	DefaultMaxSeedFiles = 100000
)

Variables

This section is empty.

Functions

func BencodeDecode

func BencodeDecode(data []byte) (interface{}, error)

BencodeDecode 从字节数组解码 bencode 数据

func BencodeEncode

func BencodeEncode(v interface{}) ([]byte, error)

BencodeEncode 将值编码为 bencode 格式

func CASPartCount added in v4.2.10

func CASPartCount(fileSize int64) int64

CASPartCount 返回按天翼规则(CASSliceSize)切分后的分片数。

func CASSliceSize added in v4.2.10

func CASSliceSize(size int64) int64

CASSliceSize 返回天翼云(189)上传时对该文件大小使用的分片大小。

这正是 189pc 上传链路(drivers/189pc/help.go 的 partSize)的逐字实现: 分片大小只由文件大小决定,10MiB / 20MiB,更大的文件按 1999 片上限向上取 10MiB 的整数倍(至少 5 倍)。上传、秒传、种子生成三处必须使用同一个值, 否则计算出的 sliceMd5 与云端存储的摘要对不上。

func CASUniformSliceSize added in v4.2.10

func CASUniformSliceSize(files []SeedFile) (int64, bool)

CASUniformSliceSize 返回所有文件共用的 CAS 分片大小。 文件跨过分片档位(10MiB/20MiB…)时没有共用值,返回 false。 种子级 piece_size 只能存一个值,只有共用时逐片哈希才能挂到种子上。

func ClampSeedPieceSize added in v4.2.10

func ClampSeedPieceSize(size int64) int64

ClampSeedPieceSize 把值限制在 Seed 容器允许的 piece_size 范围内 (16KiB..64MiB,见 ValidateSeed)。

超大文件的 CAS 文件级分片可能超过 64MiB(约 117GiB 以上进入 70MiB 档), 而种子级的 PieceSize 只是一个容器字段,不该因此让整个 .cas 无法解码: 超出范围时退化为默认值,逐片哈希会按切法不符被丢弃,但最终摘要 (CASSliceMD5 / sliceMd5)仍然保留,秒传依旧可用。

func CopyAndHash

func CopyAndHash(dst io.Writer, src io.Reader, hw *HashWriter) (int64, error)

CopyAndHash 从 reader 读取数据,同时写入 writer 和 HashWriter

func DetectFormat added in v4.2.8

func DetectFormat(fileName string, data []byte) string

DetectFormat determines the seed container from a file name and content.

func DiagnoseConversion added in v4.2.8

func DiagnoseConversion(seed *Seed, format string) []string

DiagnoseConversion reports information missing for a lossless target conversion.

func EncodeCAS added in v4.2.8

func EncodeCAS(seed *Seed) ([]byte, error)

EncodeCAS writes the reference-compatible base64 encoded JSON payload. A single-file seed uses the legacy five fields; multiple files are stored in the "files" array.

func EncodeOSS added in v4.2.8

func EncodeOSS(seed *Seed) ([]byte, error)

EncodeOSS serializes a validated seed as UTF-8 JSON.

func EncodeSeed added in v4.2.8

func EncodeSeed(seed *Seed, format string) ([]byte, error)

EncodeSeed converts a normalized seed to the requested container.

func GenerateFromFile

func GenerateFromFile(filePath string) ([]byte, error)

GenerateFromFile 从文件路径生成通用的 torrent 文件(不含 CAS 扩展) 这是一个通用函数,适用于所有驱动

func GenerateFromFileWithCAS

func GenerateFromFileWithCAS(filePath string) ([]byte, error)

GenerateFromFileWithCAS 从文件路径生成包含 CAS 扩展的 torrent 文件

func GenerateFromReader

func GenerateFromReader(reader io.Reader, fileName string, fileSize int64, pieceSize int64) ([]byte, error)

GenerateFromReader 从 io.Reader 生成通用的 torrent 文件(不含 CAS 扩展) 返回 torrent 字节数据

func GenerateFromReaderWithCAS

func GenerateFromReaderWithCAS(reader io.Reader, fileName string, fileSize int64, pieceSize int64) ([]byte, error)

GenerateFromReaderWithCAS 从 io.Reader 生成包含 CAS 扩展的 torrent 文件 适用于天翼云等支持秒传的网盘

func GetMD5Str

func GetMD5Str(data string) string

GetMD5Str 计算字符串的 MD5(大写十六进制)

func SliceMD5FromPieces added in v4.2.8

func SliceMD5FromPieces(sliceMD5s []string, fileMD5 string) string

SliceMD5FromPieces is the single canonical implementation of the sliceMd5 rule shared by every CAS producer and consumer:

  • no piece, or a single piece -> the whole-file MD5
  • two or more pieces -> MD5 of the piece MD5s joined by "\n"

Keeping one implementation matters because this value is what the remote provider compares against: a divergence between the hash-generation side and the torrent/CAS encoding side silently turns rapid uploads into mismatches. All comparisons and the returned value are upper-case.

func UpdateSeedComment added in v4.2.10

func UpdateSeedComment(data []byte, format, comment string) ([]byte, error)

UpdateSeedComment changes container metadata without rebuilding torrent info. CAS updates preserve unknown JSON fields and the original plain/base64 wrapper.

func ValidateSeed added in v4.2.8

func ValidateSeed(seed *Seed, limits ParseLimits) error

ValidateSeed rejects unsafe paths, malformed hashes and unreasonable resource use.

func ValidateTorrent added in v4.2.8

func ValidateTorrent(t *Torrent, limits ParseLimits) error

ValidateTorrent checks standard BitTorrent invariants and portable paths.

Types

type CASFileEntry added in v4.2.8

type CASFileEntry struct {
	Name       string `json:"name"`
	Size       int64  `json:"size"`
	MD5        string `json:"md5"`
	SliceMD5   string `json:"sliceMd5"`
	CreateTime string `json:"create_time"`
	// PartsMD5 每个分片的 MD5(大写十六进制)。分片规则由文件大小决定,
	// 见 CASSliceSize;缺省时仅凭 sliceMd5 也能秒传。
	PartsMD5 []string `json:"partsMd5,omitempty"`
	// SliceMD5s 旧版字段名,含义同 PartsMD5,只读兼容。
	SliceMD5s []string `json:"slice_md5s,omitempty"`
	SliceSize int64    `json:"slice_size,omitempty"`
	// Cloud 标识该 CAS 条目所属的云盘类型(如 "189");留空视为 189。
	Cloud string `json:"cloud,omitempty"`
}

CASFileEntry describes one file inside a multi-file .cas payload.

sliceMd5 是「最终分片摘要」,也就是云端真正拿来做判重的那个值; 逐片 MD5 只是可选的附加信息(partsMd5),旧文件把它写在 slice_md5s 里。

type CASInfo

type CASInfo struct {
	// FileMD5 整文件 MD5(大写十六进制)
	FileMD5 string
	// SliceMD5 分片 MD5 的摘要(大写十六进制)
	SliceMD5 string
	// SliceMD5s 每个 10MB 分片的 MD5(大写十六进制)
	SliceMD5s []string
	// SliceSize 分片大小(字节)
	SliceSize int64
	// Cloud 云盘类型标识。可为具体驱动名(如 "189"、"115"、"aliyundrive_open"),
	// 或留空表示由使用方按目标驱动自行判定。不同类型的分片规则可能不同。
	Cloud string
}

CASInfo 天翼云 CAS 秒传所需信息

func BuildCASInfoFromMD5s

func BuildCASInfoFromMD5s(fileMD5 string, sliceMD5s []string, sliceSize int64) *CASInfo

BuildCASInfoFromMD5s 从分片 MD5 列表构建 CAS 信息(默认按天翼云分片规则标记 cloud=189)。 新代码应优先使用 BuildCASInfoFromMD5sWithCloud 显式指定云盘类型。

func BuildCASInfoFromMD5sWithCloud added in v4.2.8

func BuildCASInfoFromMD5sWithCloud(fileMD5 string, sliceMD5s []string, sliceSize int64, cloud string) *CASInfo

BuildCASInfoFromMD5sWithCloud 从分片 MD5 列表构建 CAS 信息,并指定云盘类型标识。

type CASPayload added in v4.2.8

type CASPayload struct {
	Comment    string `json:"comment,omitempty"`
	Name       string `json:"name"`
	Size       int64  `json:"size"`
	MD5        string `json:"md5"`
	SliceMD5   string `json:"sliceMd5"`
	CreateTime string `json:"create_time"`
	// PartsMD5 每个分片的 MD5(大写十六进制),可选。
	PartsMD5 []string `json:"partsMd5,omitempty"`
	// SliceMD5s 旧版字段名,含义同 PartsMD5,只读兼容。
	SliceMD5s []string `json:"slice_md5s,omitempty"`
	SliceSize int64    `json:"slice_size,omitempty"`
	// Cloud 标识该 CAS 载荷所属的云盘类型(如 "189");留空视为 189。
	Cloud string         `json:"cloud,omitempty"`
	Files []CASFileEntry `json:"files,omitempty"`
}

CASPayload matches the reference .cas JSON payload. The five legacy fields describe a single file (byte-for-byte compatible with the reference project); the optional "files" array extends it to multi-file seeds, partsMd5/slice_size preserve per-piece MD5, and comment preserves sharing metadata.

type HashWriter

type HashWriter struct {
	// contains filtered or unexported fields
}

HashWriter 同时计算文件的 MD5、分片 MD5 和 SHA-1 piece hash 用于在上传过程中一次性计算所有需要的哈希值

func NewDefaultHashWriter

func NewDefaultHashWriter() *HashWriter

NewDefaultHashWriter 创建默认的 HashWriter(10MB 分片)

func NewHashWriter

func NewHashWriter(sliceSize, pieceSize, fileSize int64) *HashWriter

NewHashWriter 创建一个新的 HashWriter sliceSize: CAS 分片大小(通常 10MB) pieceSize: BT piece 大小(设为与 sliceSize 相同以保持对齐) fileSize: 文件总大小(用于 GCID 初始化,0 表示未知)

func (*HashWriter) BuildSeedFile added in v4.2.8

func (hw *HashWriter) BuildSeedFile(filePath string, modified string) SeedFile

BuildSeedFile exports all hashes accumulated during this single stream pass.

func (*HashWriter) BuildTorrent

func (hw *HashWriter) BuildTorrent(fileName string, fileSize int64) *Torrent

BuildTorrent 根据计算结果构建 Torrent 结构

func (*HashWriter) BuildTorrentBytes

func (hw *HashWriter) BuildTorrentBytes(fileName string, fileSize int64) ([]byte, error)

BuildTorrentBytes 构建并编码 torrent 文件

func (*HashWriter) Finish

func (hw *HashWriter) Finish()

Finish 完成所有哈希计算(处理最后不完整的分片/piece)

func (*HashWriter) GetFileGCID added in v4.2.8

func (hw *HashWriter) GetFileGCID() string

GetFileGCID returns the uppercase GCID digest for Thunder/PikPak.

func (*HashWriter) GetFileMD5

func (hw *HashWriter) GetFileMD5() string

GetFileMD5 获取整文件 MD5(大写十六进制)

func (*HashWriter) GetFileSHA1 added in v4.2.8

func (hw *HashWriter) GetFileSHA1() string

GetFileSHA1 returns the lowercase whole-file SHA-1 digest.

func (*HashWriter) GetFileSHA256 added in v4.2.8

func (hw *HashWriter) GetFileSHA256() string

GetFileSHA256 returns the lowercase whole-file SHA-256 digest.

func (*HashWriter) GetPieceHashes

func (hw *HashWriter) GetPieceHashes() []byte

GetPieceHashes 获取所有 piece 的 SHA-1 哈希拼接

func (*HashWriter) GetPieceMD5s added in v4.2.8

func (hw *HashWriter) GetPieceMD5s() []string

GetPieceMD5s returns independent per-file MD5 piece hashes.

func (*HashWriter) GetPieceSHA1s added in v4.2.8

func (hw *HashWriter) GetPieceSHA1s() []string

GetPieceSHA1s returns independent per-file SHA-1 piece hashes.

func (*HashWriter) GetPieceSHA256s added in v4.2.8

func (hw *HashWriter) GetPieceSHA256s() []string

GetPieceSHA256s returns independent per-file SHA-256 piece hashes.

func (*HashWriter) GetSliceMD5

func (hw *HashWriter) GetSliceMD5(fileMD5 string) string

GetSliceMD5 获取最终的 sliceMD5(用于秒传)

func (*HashWriter) GetSliceMD5s

func (hw *HashWriter) GetSliceMD5s() []string

GetSliceMD5s 获取所有分片的 MD5 列表

func (*HashWriter) GetTotalWritten

func (hw *HashWriter) GetTotalWritten() int64

GetTotalWritten 获取总写入字节数

func (*HashWriter) Write

func (hw *HashWriter) Write(p []byte) (n int, err error)

Write 实现 io.Writer 接口

type OrderedDict

type OrderedDict struct {
	Keys   []string
	Values map[string]interface{}
}

OrderedDict 有序字典,保持插入顺序

func NewOrderedDict

func NewOrderedDict() OrderedDict

func (*OrderedDict) Get

func (d *OrderedDict) Get(key string) (interface{}, bool)

func (*OrderedDict) Set

func (d *OrderedDict) Set(key string, value interface{})

type ParseLimits added in v4.2.8

type ParseLimits struct {
	MaxBytes int64
	MaxFiles int
	MaxDepth int
}

ParseLimits controls resource use while parsing untrusted seeds.

func DefaultParseLimits added in v4.2.8

func DefaultParseLimits() ParseLimits

DefaultParseLimits returns conservative public API limits.

type Seed added in v4.2.8

type Seed struct {
	Format    string        `json:"format"`
	Version   int           `json:"version"`
	Name      string        `json:"name"`
	Comment   string        `json:"comment,omitempty"`
	CreatedAt string        `json:"created_at"`
	CreatedBy string        `json:"created_by"`
	PieceSize int64         `json:"piece_size"`
	Trackers  []string      `json:"trackers,omitempty"`
	Channels  []SeedChannel `json:"channels,omitempty"`
	Files     []SeedFile    `json:"files"`

	// GlobalPieces contains whole-package SHA1 pieces over concatenated files.
	// The optional OSS field preserves crossing pieces through format conversion.
	GlobalPieces []string `json:"global_pieces,omitempty"`

	// AvailableHashes / AvailablePieceHashes 表示种子整体已经带有哪些算法。
	// BT 的 pieces 是整包的,跨文件时无法拆到单个文件上,所以前端要按这个
	// 种子级字段判断 in seed,而不是逐个文件看 hashes。
	AvailableHashes      []string `json:"available_hashes,omitempty"`
	AvailablePieceHashes []string `json:"available_piece_hashes,omitempty"`
}

Seed describes the portable OpenList sharing seed contract.

func DecodeCAS added in v4.2.8

func DecodeCAS(data []byte, limits ParseLimits) (*Seed, error)

DecodeCAS accepts both reference base64 JSON and raw JSON payloads.

func DecodeOSS added in v4.2.8

func DecodeOSS(data []byte, limits ParseLimits) (*Seed, error)

DecodeOSS parses a bounded UTF-8 OSS document.

func DecodeSeed added in v4.2.8

func DecodeSeed(data []byte, format string, limits ParseLimits) (*Seed, error)

DecodeSeed normalizes OSS, torrent and CAS containers to the OSS contract.

func GenerateSeedFromReader added in v4.2.8

func GenerateSeedFromReader(reader io.Reader, filePath string, expectedSize, pieceSize int64, createdBy string) (*Seed, error)

GenerateSeedFromReader computes the complete OSS hash matrix in one stream pass.

func NewSeed added in v4.2.8

func NewSeed(name, createdBy string, pieceSize int64) *Seed

NewSeed creates a versioned OSS document with stable defaults.

func SeedFromTorrent added in v4.2.8

func SeedFromTorrent(t *Torrent, limits ParseLimits) (*Seed, error)

SeedFromTorrent normalizes a standard torrent and preserves x-openlist when present.

type SeedChannel added in v4.2.8

type SeedChannel struct {
	Driver    string `json:"driver"`
	MountPath string `json:"mount_path,omitempty"`
}

SeedChannel contains only public storage discovery metadata.

type SeedFile added in v4.2.8

type SeedFile struct {
	Path          string       `json:"path"`
	Size          int64        `json:"size"`
	Modified      string       `json:"modified,omitempty"`
	Comment       string       `json:"comment,omitempty"`
	Hashes        SeedHashes   `json:"hashes"`
	Sources       []SeedSource `json:"sources,omitempty"`
	CASSliceMD5   string       `json:"cas_slice_md5,omitempty"`
	CASCreateTime string       `json:"cas_create_time,omitempty"`
	// CASCloud 标识该文件 CAS 元数据所属的云盘类型(如 "189")。留空视为 189。
	CASCloud        string   `json:"cas_cloud,omitempty"`
	MissingChannels []string `json:"missing_channels,omitempty"`
}

SeedFile describes one relative file in a sharing seed.

func (*SeedFile) DropInconsistentCASParts added in v4.2.10

func (f *SeedFile) DropInconsistentCASParts(sliceSize int64)

DropInconsistentCASParts 丢弃与给定分片大小对不上的逐片 MD5。

逐片列表只是附加信息,判重只看最终摘要(CASSliceMD5),所以切法不符时 直接丢掉比留一份误导数据更安全。

type SeedHashes added in v4.2.8

type SeedHashes struct {
	MD5    string           `json:"md5,omitempty"`
	SHA1   string           `json:"sha1,omitempty"`
	SHA256 string           `json:"sha256,omitempty"`
	GCID   string           `json:"gcid,omitempty"`
	Pieces *SeedPieceHashes `json:"pieces,omitempty"`
}

SeedHashes contains whole-file and optional per-piece hashes.

type SeedPieceHashes added in v4.2.8

type SeedPieceHashes struct {
	MD5    []string `json:"md5,omitempty"`
	SHA1   []string `json:"sha1,omitempty"`
	SHA256 []string `json:"sha256,omitempty"`
}

SeedPieceHashes contains independent per-file piece hashes.

type SeedSource added in v4.2.8

type SeedSource struct {
	Type      string `json:"type"`
	URL       string `json:"url"`
	ExpiresAt string `json:"expires_at,omitempty"`
	ShareID   string `json:"share_id,omitempty"`
}

SeedSource is an optional retrievable public or signed source URL.

type Torrent

type Torrent struct {
	// Info info 字典
	Info TorrentInfo
	// InfoHash info 字典的 SHA-1 哈希(20 字节)
	InfoHash []byte
	// Announce tracker URL
	Announce string
	// AnnounceList tracker 列表
	AnnounceList [][]string
	// CreationDate 创建时间
	CreationDate int64
	// Comment 注释
	Comment string
	// CreatedBy 创建者
	CreatedBy string
	// CAS 天翼云 CAS 扩展信息(存储在 info 字典外部,不影响 info_hash)
	CAS *CASInfo
	// OpenList stores the portable extension outside info so info_hash stays standard.
	OpenList *Seed
}

Torrent 完整的 torrent 文件结构

func Decode

func Decode(data []byte) (*Torrent, error)

Decode 从 bencode 字节数组解析 torrent

func NewTorrent

func NewTorrent(name string, fileSize int64, fileMD5 string) *Torrent

NewTorrent 创建一个新的 torrent 结构

func TorrentFromSeed added in v4.2.8

func TorrentFromSeed(seed *Seed) (*Torrent, []string)

TorrentFromSeed constructs standard BitTorrent info/pieces plus x-openlist.

func (*Torrent) Encode

func (t *Torrent) Encode() ([]byte, error)

Encode 将 torrent 编码为 bencode 格式的字节数组

func (*Torrent) GetInfoHashHex

func (t *Torrent) GetInfoHashHex() string

GetInfoHashHex 获取 info_hash 的十六进制字符串

func (*Torrent) GetPieceHashes

func (t *Torrent) GetPieceHashes() [][]byte

GetPieceHashes 获取所有分片的 SHA-1 哈希(每个 20 字节)

func (*Torrent) GetTotalSize

func (t *Torrent) GetTotalSize() int64

GetTotalSize 获取 torrent 中所有文件的总大小

func (*Torrent) HasCASInfo

func (t *Torrent) HasCASInfo() bool

HasCASInfo 检查 torrent 是否包含 CAS 扩展信息

func (*Torrent) SetCASInfo

func (t *Torrent) SetCASInfo(cas *CASInfo)

SetCASInfo 设置 CAS 扩展信息

func (*Torrent) SetPieces

func (t *Torrent) SetPieces(pieces []byte)

SetPieces 设置 SHA-1 分片哈希

type TorrentFile

type TorrentFile struct {
	// Length 文件大小(字节)
	Length int64
	// Path 文件路径(多文件模式下的相对路径各段)
	Path []string
	// MD5Sum 文件的 MD5(可选,BT 标准字段)
	MD5Sum string
}

TorrentFile 表示 torrent 中的单个文件

type TorrentInfo

type TorrentInfo struct {
	// PieceLength 分片大小
	PieceLength int64
	// Pieces 所有分片的 SHA-1 哈希拼接(每 20 字节一个)
	Pieces []byte
	// Name 种子名称(单文件模式为文件名,多文件模式为目录名)
	Name string
	// Length 单文件模式下的文件大小
	Length int64
	// Files 多文件模式下的文件列表
	Files []TorrentFile
	// MD5Sum 单文件模式下的文件 MD5(可选)
	MD5Sum string
}

TorrentInfo torrent 的 info 字典

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL