reseed

package
v1.0.0-rc.21 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package reseed 实现 IYUU 辅种:把下载器里已经下完的种子的 info hash 交给 IYUU 查询,找出其他站点上数据相同的种子, 从站点下载种子文件、核对 info hash 与文件列表后,暂停加入同一台下载器(保存路径用原种子的),校验到 100% 才开始做种。 加入、校验与回滚复用转移做种的任务(transfer 包,任务种类 reseed)。默认关闭;token 加密保存。

Index

Constants

View Source
const (
	SkipNotSelected = "站点不在辅种范围"
	SkipExists      = "下载器里已经有"
	SkipTried       = "已经尝试过"
	SkipDailyLimit  = "今天这个站点已经加满"
	SkipActive      = "已有进行中的任务"
)

跳过的原因。

Variables

View Source
var (
	// ErrInvalid 表示设置有问题。
	ErrInvalid = errors.New("辅种设置无效")
	// ErrNoSites 表示没有可以辅种的站点。
	ErrNoSites = errors.New("没有可以辅种的站点:IYUU 支持的站点里,pt-tools 还没有配置或没有选中")
)

Functions

func SameFiles

func SameFiles(a, b []FileEntry) (bool, string)

SameFiles 报告两份文件列表是否完全一样(路径与大小逐个相同,顺序无关)。不一样时第二个返回值写明第一处差别。 辅种只有在文件完全一样时才加:同一份数据才能被新种子校验到 100%。

Types

type Config

type Config struct {
	DB          *gorm.DB
	Cipher      TokenCipher
	Transfer    *transfer.Service
	Downloaders transfer.Downloaders
	// Sites 按站点名取共享的站点实例;SiteIDs 返回 pt-tools 里已配置(注册了实例)的站点。
	Sites    func(name string) (v2.Site, bool)
	SiteIDs  func() []string
	Resolver *v2.TrackerResolver
	// NewClient 按 token 建 IYUU 客户端,测试里换成假服务。
	NewClient func(token string) *iyuu.Client
	Now       func() time.Time
	// Sleep 是限流时的等待,测试里不真等。
	Sleep  func(ctx context.Context, d time.Duration) error
	Logger *zap.SugaredLogger
}

Config 是 Service 的依赖。

type FileEntry

type FileEntry struct {
	Path string
	Size int64
}

FileEntry 是种子里的一个文件:相对保存路径的路径(用 / 分隔)与大小。

func DownloaderFiles

func DownloaderFiles(files []downloader.TorrentFile) []FileEntry

DownloaderFiles 把下载器的文件列表换成 FileEntry(分隔符统一成 /)。

func TorrentFiles

func TorrentFiles(data []byte) ([]FileEntry, error)

TorrentFiles 列出 .torrent 文件里的文件:单文件种子是 name;多文件种子是 name/path...(与下载器的文件列表同一写法)。

type RecordView

type RecordView struct {
	models.ReseedRecord
	JobState   string `json:"job_state,omitempty"`
	JobMessage string `json:"job_message,omitempty"`
	JobMissing bool   `json:"job_missing,omitempty"`
}

RecordView 是记录列表里的一项:记录加上任务的状态。JobMissing 表示记录指向的任务已经不在了(结果不知道)。

type RunResult

type RunResult struct {
	Downloaders int            `json:"downloaders"`
	Hashes      int            `json:"hashes"`
	Candidates  int            `json:"candidates"`
	Created     int            `json:"created"`
	Failed      int            `json:"failed"`
	Skipped     map[string]int `json:"skipped,omitempty"`
	Errors      []string       `json:"errors,omitempty"`
	// Stopped 说明这一轮为什么提前停下(限流等)。
	Stopped string `json:"stopped,omitempty"`
}

RunResult 是一轮辅种的结果。

func (RunResult) Summary

func (r RunResult) Summary() string

Summary 是写进设置 last_result 的一句话。

type Service

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

Service 管理辅种设置并运行辅种。

func New

func New(cfg Config) *Service

New 构造 Service。

func (*Service) ClearFinishedJobs

func (s *Service) ClearFinishedJobs(ctx context.Context) (int64, error)

ClearFinishedJobs 删除已经结束的辅种任务,返回删除的条数。删除前把任务的结果写进引用它的辅种记录, 记录不再指向任务;不改记录的更新时间(重试间隔和每天的上限按它算)。

func (*Service) Due

func (s *Service) Due(ctx context.Context) bool

Due 报告这一刻该不该定时运行:开启了、有 token、距上次运行满了间隔。

func (*Service) RecordRun

func (s *Service) RecordRun(ctx context.Context, at time.Time, summary string) error

RecordRun 记下这一轮的时间和结果。

func (*Service) Records

func (s *Service) Records(ctx context.Context) ([]RecordView, error)

Records 返回最近尝试的辅种记录(重试过的按最后一次尝试的时间排)。

func (*Service) Run

func (s *Service) Run(ctx context.Context) (RunResult, error)

Run 运行一轮辅种。返回的错误是这一轮根本没法开始(没有 token、没有站点等);单个下载器或站点的问题记在结果里。

func (*Service) SaveSettings

func (s *Service) SaveSettings(ctx context.Context, u SettingsUpdate) (Settings, error)

SaveSettings 校验并保存设置(token 以外的项全量保存)。开启时必须有 token;换 token 时作废缓存的站点列表与 sid_sha1。 只写配置列:运行中的一轮会写上次运行结果和 sid_sha1 缓存,整行写回会把它们改回旧值。

func (*Service) Settings

func (s *Service) Settings(ctx context.Context) (Settings, error)

Settings 返回当前设置(0 的间隔与上限换成默认值)。

func (*Service) SiteMap

func (s *Service) SiteMap(ctx context.Context) ([]SiteMapItem, error)

SiteMap 列出 IYUU 支持的站点和它们在 pt-tools 里对应的站点(按站点地址的主机名与可注册域识别)。

type Settings

type Settings struct {
	Enabled          bool       `json:"enabled"`
	HasToken         bool       `json:"has_token"`
	IntervalHours    int        `json:"interval_hours"`
	DownloaderIDs    []uint     `json:"downloader_ids"`
	SiteNames        []string   `json:"site_names"`
	MaxPerSitePerDay int        `json:"max_per_site_per_day"`
	LastRunAt        *time.Time `json:"last_run_at,omitempty"`
	LastResult       string     `json:"last_result"`
}

Settings 是接口里的辅种设置:token 只说有没有设置,不返回内容。

type SettingsUpdate

type SettingsUpdate struct {
	Enabled          bool     `json:"enabled"`
	Token            *string  `json:"token,omitempty"`
	IntervalHours    int      `json:"interval_hours"`
	DownloaderIDs    []uint   `json:"downloader_ids"`
	SiteNames        []string `json:"site_names"`
	MaxPerSitePerDay int      `json:"max_per_site_per_day"`
}

SettingsUpdate 是保存设置的请求。Token 为 nil 时保留原来的;为空串时清除。

type SiteMapItem

type SiteMapItem struct {
	SID        int    `json:"sid"`
	IYUUSite   string `json:"iyuu_site"`
	Nickname   string `json:"nickname"`
	Host       string `json:"host"`
	SiteName   string `json:"site_name,omitempty"`
	Configured bool   `json:"configured"`
	Selected   bool   `json:"selected"`
}

SiteMapItem 是 IYUU 的一个站点,以及它在 pt-tools 里对应哪个站点。

type TokenCipher

type TokenCipher interface {
	Encrypt(plain string) (string, error)
	Decrypt(cipherText string) (string, error)
}

TokenCipher 加解密 IYUU token(生产环境是 ConfigStore 的 EncryptCookie / DecryptCookie)。

Jump to

Keyboard shortcuts

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