bencode

package
v1.0.2065 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultDecodeMaxStrLen = 1<<27 - 1 // ~128MiB

The default bencode string length limit. This is a poor attempt to prevent excessive memory allocation when parsing, but also leaves the window open to implement a better solution.

Variables

This section is empty.

Functions

func IsEmptyValue

func IsEmptyValue(v reflect.Value) bool

Returns whether the value represents the empty value for its type. Used for example to determine if complex types satisfy the common "omitempty" tag option for marshalling.

func Marshal

func Marshal(v any) ([]byte, error)

Marshal the value 'v' to the bencode form, return the result as []byte and an error if any.

func MustMarshal

func MustMarshal(v any) []byte

Marshal and throw panic when error

func Unmarshal

func Unmarshal(data []byte, v any) (err error)

Unmarshal the bencode value in the 'data' to a value pointed by the 'v' pointer, return a non-nil error if any. If there are trailing bytes, this results in UnusedTrailingBytesError, but the value will be valid. It's probably more consistent to use Decoder.Decode if you want to rely on this behaviour (inspired by Rust's serde here).

Types

type Bytes

type Bytes []byte

bytes data

func (Bytes) GoString

func (me Bytes) GoString() string

output string

func (Bytes) MarshalBencode

func (me Bytes) MarshalBencode() ([]byte, error)

Marshal Bytes

func (*Bytes) UnmarshalBencode

func (me *Bytes) UnmarshalBencode(b []byte) error

Unmarshal Bytes

type Data

type Data map[string]any

接收解析后的数据

func (Data) GetAnnounce

func (this Data) GetAnnounce() string

获取 Announce

func (Data) GetComment

func (this Data) GetComment() string

获取 Comment

func (Data) GetCreatedBy

func (this Data) GetCreatedBy() string

获取 CreatedBy

func (Data) GetCreationDate

func (this Data) GetCreationDate() int64

获取创建时间

func (Data) GetCreationDateTime

func (this Data) GetCreationDateTime(tz ...string) time.Time

获取格式化后的创建时间

func (Data) GetInfo

func (this Data) GetInfo() map[string]any

获取 Info

func (Data) GetInfoItem

func (this Data) GetInfoItem(key string) any

获取 Info

func (Data) GetInfoKeys

func (this Data) GetInfoKeys() []string

Info 已有的字段

func (Data) GetItem

func (this Data) GetItem(key string) any

获取单个数据

func (Data) GetItemInt64

func (this Data) GetItemInt64(key string) int64

获取单个数据 int64

func (Data) GetItemMap

func (this Data) GetItemMap(key string) map[string]any

获取单个数据 Map

func (Data) GetItemString

func (this Data) GetItemString(key string) string

获取单个数据字符

func (Data) GetKeys

func (this Data) GetKeys() []string

已有的字段

func (Data) SetAnnounce

func (this Data) SetAnnounce(data string) Data

设置 Announce

func (Data) SetComment

func (this Data) SetComment(data string) Data

设置 Comment

func (Data) SetCreatedBy

func (this Data) SetCreatedBy(data string) Data

设置 CreatedBy

func (Data) SetCreationDate

func (this Data) SetCreationDate(data int64) Data

设置创建时间

func (Data) SetCreationDateTime

func (this Data) SetCreationDateTime(t time.Time) Data

设置创建时间

func (Data) SetInfo

func (this Data) SetInfo(data map[string]any) Data

设置 Info

func (Data) SetItem

func (this Data) SetItem(key string, data any) Data

设置数据

func (Data) String

func (this Data) String() string

返回字符

func (Data) ToArray

func (this Data) ToArray() map[string]any

返回 map 数据

func (Data) ToInfoArray

func (this Data) ToInfoArray() map[string]any

返回 Info 数据

func (Data) ToInfoJSON

func (this Data) ToInfoJSON() string

返回 Info 的 json 字符数据

func (Data) ToJSON

func (this Data) ToJSON() string

返回 json 字符数据

type Decoder

type Decoder struct {
	// Maximum parsed bencode string length. Defaults to DefaultMaxStrLen if zero.
	MaxStrLen MaxStrLen

	// Sum of bytes used to Decode values.
	Offset int64
	// contains filtered or unexported fields
}

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

解析 New Decoder

func (*Decoder) Decode

func (d *Decoder) Decode(v any) (err error)

type Encoder

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

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

编码 New Encoder

func (*Encoder) Encode

func (e *Encoder) Encode(v any) (err error)

type FileInfo

type FileInfo struct {
	Path   string
	Length int
}

返回数据用的

type MarshalTypeError

type MarshalTypeError struct {
	Type reflect.Type
}

In case if marshaler cannot encode a type, it will return this error. Typical example of such type is float32/float64 which has no bencode representation.

func (*MarshalTypeError) Error

func (e *MarshalTypeError) Error() string

type Marshaler

type Marshaler interface {
	MarshalBencode() ([]byte, error)
}

Any type which implements this interface, will be marshaled using the specified method.

type MarshalerError

type MarshalerError struct {
	Type reflect.Type
	Err  error
}

A non-nil error was returned after calling MarshalBencode on a type which implements the Marshaler interface.

func (*MarshalerError) Error

func (e *MarshalerError) Error() string

type MaxStrLen

type MaxStrLen = int64

type MultipleInfo

type MultipleInfo struct {
	// 每个块的20个字节的SHA1 Hash的值(二进制格式)
	Pieces string `bencode:"pieces"`
	// 每个块的大小,单位字节 整数
	PieceLength int `bencode:"piece length"`
	// 文件长度 整数
	Length int `bencode:"length,omitempty"`

	// 目录名 字符串
	Name string `bencode:"name"`
	// 目录名编码
	NameUtf8 string `bencode:"name.utf-8,omitempty"`

	// 文件信息
	Files []MultipleInfoFile `bencode:"files"`
}

多文件信息

func (MultipleInfo) GetFileList

func (this MultipleInfo) GetFileList() []FileInfo

返回文件数据列表

func (MultipleInfo) GetPieceHashes

func (this MultipleInfo) GetPieceHashes() ([][20]byte, error)

每个分片的 SHA-1 hash 长度是20 把他们从Pieces中切出来

type MultipleInfoFile

type MultipleInfoFile struct {
	// 文件长度 单位字节 整数
	Length int `bencode:"length"`
	// 文件的路径和名字 列表
	Path []string `bencode:"path"`
	// path.utf-8:文件名的UTF-8编码
	PathUtf8 string `bencode:"path.utf-8,omitempty"`
}

文件信息

type MultipleTorrent

type MultipleTorrent struct {
	// `bencode:""`
	// tracker服务器的URL 字符串
	Announce string `bencode:"announce"`
	// 备用tracker服务器列表 列表
	// 发现 announce-list 后面跟了两个l(ll) announce-listll
	AnnounceList [][]string `bencode:"announce-list,omitempty"`
	// 种子的创建时间 整数
	CreatDate int64 `bencode:"creation date"`
	// 备注 字符串
	Comment string `bencode:"comment"`
	// 创建者 字符串
	CreatedBy string `bencode:"created by"`
	// 详情
	Info MultipleInfo `bencode:"info"`
	// 包含一系列ip和相应端口的列表,是用于连接DHT初始node
	Nodes [][]any `bencode:"nodes,omitempty"`
	// 文件的默认编码
	Encoding string `bencode:"encoding,omitempty"`
	// 备注的utf-8编码
	CommentUtf8 string `bencode:"comment.utf-8,omitempty"`
}

多文件 包含5:files 版本为通用类,非通用类需要直接用 map 获取数据或者生成数据

func (MultipleTorrent) GetAnnounceList

func (this MultipleTorrent) GetAnnounceList() []string

获取备用节点

func (MultipleTorrent) GetCreationDateTime

func (this MultipleTorrent) GetCreationDateTime(tz ...string) time.Time

获取格式化后的创建时间

func (MultipleTorrent) GetInfoHash

func (this MultipleTorrent) GetInfoHash() ([20]byte, error)

生成 info hash

func (MultipleTorrent) GetInfoHashString

func (this MultipleTorrent) GetInfoHashString() string

生成 info hash 字符

func (MultipleTorrent) SetCreationDateTime

func (this MultipleTorrent) SetCreationDateTime(t time.Time) MultipleTorrent

设置创建时间

type SingleInfo

type SingleInfo struct {
	Pieces      string `bencode:"pieces"`
	PieceLength int    `bencode:"piece length"`
	Length      int    `bencode:"length"`

	Name     string `bencode:"name"`
	NameUtf8 string `bencode:"name.utf-8,omitempty"`

	// 文件发布者
	Publisher     string `bencode:"publisher,omitempty"`
	PublisherUtf8 string `bencode:"publisher.utf-8,omitempty"`

	// 文件发布者的网址
	PublisherUrl     string `bencode:"publisher-url,omitempty"`
	PublisherUrlUtf8 string `bencode:"publisher-url.utf-8,omitempty"`

	MD5Sum  string `bencode:"md5sum,omitempty"`
	Private bool   `bencode:"private,omitempty"`
}

单文件信息

func (SingleInfo) GetPieceHashes

func (this SingleInfo) GetPieceHashes() ([][20]byte, error)

每个分片的 SHA-1 hash 长度是20 把他们从Pieces中切出来

type SingleTorrent

type SingleTorrent struct {
	// `bencode:""`
	// tracker服务器的URL 字符串
	Announce string `bencode:"announce"`
	// 备用tracker服务器列表 列表
	// 发现 announce-list 后面跟了两个l(ll) announce-listll
	AnnounceList [][]string `bencode:"announce-list,omitempty"`
	// 种子的创建时间 整数
	CreatDate int64 `bencode:"creation date"`
	// 备注 字符串
	Comment string `bencode:"comment"`
	// 创建者 字符串
	CreatedBy string `bencode:"created by"`
	// 详情
	Info SingleInfo `bencode:"info"`
	// 包含一系列ip和相应端口的列表,是用于连接DHT初始node
	Nodes [][]any `bencode:"nodes,omitempty"`
	// 文件的默认编码
	Encoding string `bencode:"encoding,omitempty"`
	// 备注的utf-8编码
	CommentUtf8 string `bencode:"comment.utf-8,omitempty"`
}

单文件 版本为通用类,非通用类需要直接用 map 获取数据或者生成数据

func (SingleTorrent) GetAnnounceList

func (this SingleTorrent) GetAnnounceList() []string

获取备用节点

func (SingleTorrent) GetCreationDateTime

func (this SingleTorrent) GetCreationDateTime(tz ...string) time.Time

获取格式化后的创建时间

func (SingleTorrent) GetInfoHash

func (this SingleTorrent) GetInfoHash() ([20]byte, error)

生成 info hash

func (SingleTorrent) GetInfoHashString

func (this SingleTorrent) GetInfoHashString() string

生成 info hash 字符

func (SingleTorrent) SetCreationDateTime

func (this SingleTorrent) SetCreationDateTime(t time.Time) SingleTorrent

设置创建时间

type SyntaxError

type SyntaxError struct {
	Offset int64 // location of the error
	What   error // error description
}

Malformed bencode input, unmarshaler failed to parse it.

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

type UnmarshalFieldError

type UnmarshalFieldError struct {
	Key   string
	Type  reflect.Type
	Field reflect.StructField
}

Unmarshaler tried to write to an unexported (therefore unwritable) field.

func (*UnmarshalFieldError) Error

func (e *UnmarshalFieldError) Error() string

type UnmarshalInvalidArgError

type UnmarshalInvalidArgError struct {
	Type reflect.Type
}

Unmarshal argument must be a non-nil value of some pointer type.

func (*UnmarshalInvalidArgError) Error

func (e *UnmarshalInvalidArgError) Error() string

type UnmarshalTypeError

type UnmarshalTypeError struct {
	BencodeTypeName     string
	UnmarshalTargetType reflect.Type
}

Unmarshaler spotted a value that was not appropriate for a given Go value.

func (*UnmarshalTypeError) Error

func (e *UnmarshalTypeError) Error() string

This could probably be a value type, but we may already have users assuming that it's passed by pointer.

type Unmarshaler

type Unmarshaler interface {
	UnmarshalBencode([]byte) error
}

Any type which implements this interface, will be unmarshaled using the specified method.

type UnmarshalerError

type UnmarshalerError struct {
	Type reflect.Type
	Err  error
}

A non-nil error was returned after calling UnmarshalBencode on a type which implements the Unmarshaler interface.

func (*UnmarshalerError) Error

func (e *UnmarshalerError) Error() string

type UnusedTrailingBytesError

type UnusedTrailingBytesError struct {
	NumUnusedBytes int
}

func (UnusedTrailingBytesError) Error

func (me UnusedTrailingBytesError) Error() string

Jump to

Keyboard shortcuts

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