cookiecloud

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 20 Imported by: 0 modules, 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

Overview

Package cookiecloud 从自建的 CookieCloud 服务取回加密的 Cookie,在本地解密,按站点地址挑出 pt-tools 已配置站点的 Cookie。

协议按 CookieCloud 的公开说明实现(GET {server}/get/{uuid} 取回 {encrypted, crypto_type}),只在本地解密, 密码不发给服务端。两种加密:

  • legacy:CryptoJS 口令模式。口令是 md5(uuid + "-" + password) 十六进制的前 16 个字符;密文是 OpenSSL 格式 base64("Salted__" + 8 字节 salt + 密文),key 与 iv 由 EVP_BytesToKey(MD5, 口令, salt) 派生,AES-256-CBC + PKCS7。
  • aes-128-cbc-fixed(CookieCloud 0.3.0 起):key 是上面那 16 个字符本身(AES-128),iv 是 16 个 0 字节, 密文是标准 base64,CBC + PKCS7。

Index

Constants

View Source
const (
	CryptoLegacy = "legacy"
	CryptoFixed  = "aes-128-cbc-fixed"
)

加密方式。

View Source
const MaxImportSites = 500

MaxImportSites 是一次导入最多选几个站点。

Variables

View Source
var (
	// ErrDecrypt 表示解不开:多半是 UUID 或密码不对。
	ErrDecrypt = errors.New("解密失败:请检查 UUID 和密码")
	// ErrNotFound 表示服务端没有这个 UUID 的数据。
	ErrNotFound = errors.New("CookieCloud 服务上没有这个 UUID 的数据")
)
View Source
var (
	// ErrInvalid 表示设置或请求有问题。
	ErrInvalid = errors.New("CookieCloud 设置无效")
	// ErrNotConfigured 表示还没填服务地址、UUID 或密码。
	ErrNotConfigured = errors.New("先填写 CookieCloud 服务地址、UUID 和密码")
	// ErrBusy 表示另一次预览、导入或同步正在进行。
	ErrBusy = errors.New("另一次 CookieCloud 导入正在进行,稍后再试")
	// ErrSettingsChanged 表示读取期间设置被改了:这一次读到的是旧设置的数据,不写。
	ErrSettingsChanged = errors.New("读取期间 CookieCloud 设置被修改了,这一次没有写入;请重新读取")
)

Functions

func ParseServer

func ParseServer(raw string) (*url.URL, error)

ParseServer 解析服务地址:必须是 http(s) 地址,不能带用户名密码、查询参数或 #,末尾的 / 去掉。

func SameCookie

func SameCookie(a, b string) bool

SameCookie 报告两个 Cookie 请求头是不是同一组 name=value(顺序与空白不计)。

Types

type Cipher

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

Cipher 加解密密码(生产环境是 ConfigStore 的 EncryptCookie / DecryptCookie)。

type Config

type Config struct {
	DB     *gorm.DB
	Cipher Cipher
	// HTTP 为 nil 时用 30 秒超时的客户端。
	HTTP *http.Client
	// Sites 返回 pt-tools 支持的站点(含没启用的)。
	Sites func(ctx context.Context) ([]SiteState, error)
	// Apply 把 Cookie(站点名 → Cookie 请求头)写进站点并启用,走与浏览器扩展同步凭据相同的路径:保存、刷新站点实例、请求登录探测。
	// 返回写不进去的站点和原因,其余的已经写好。
	Apply  func(ctx context.Context, cookies map[string]string) map[string]error
	Now    func() time.Time
	Logger *zap.SugaredLogger
}

Config 是 Service 的依赖。

type Cookie struct {
	Name   string `json:"name"`
	Value  string `json:"value"`
	Domain string `json:"domain"`
	Path   string `json:"path"`
	// HostOnly 为 true 时只对 Domain 这个主机名本身有效;没有这个字段时按 Domain 有没有前导点判断。
	HostOnly *bool `json:"hostOnly,omitempty"`
	// ExpirationDate 是过期时间(Unix 秒,可以带小数);会话 Cookie 没有。
	ExpirationDate *float64 `json:"expirationDate,omitempty"`
}

Cookie 是 CookieCloud 存的一条 Cookie(浏览器 cookies 接口给的字段)。

type Data

type Data struct {
	CookieData map[string][]Cookie `json:"cookie_data"`
}

Data 是解密后的内容:按域名分组的 Cookie。

func Decrypt

func Decrypt(p Payload, uuid, password string) (Data, error)

Decrypt 解密密文。cryptoType 为空(老服务端)时按 legacy,密文不是 OpenSSL 格式时再按 fixed 试一次; 明确写了 legacy 的只按 legacy。

type ImportResult

type ImportResult struct {
	// Imported 是写入了 Cookie 的站点;Unchanged 是 Cookie 没有变化、没写的站点;Missing 是选了、但 CookieCloud 里没有它的 Cookie 的站点;
	// Failed 是写不进去的站点。
	Imported  []string    `json:"imported"`
	Unchanged []string    `json:"unchanged"`
	Missing   []string    `json:"missing"`
	Failed    []SiteError `json:"failed"`
}

ImportResult 是一次导入或同步的结果。

func (ImportResult) Summary

func (r ImportResult) Summary() string

Summary 是一句话的结果(不含 Cookie 内容)。

type Match

type Match struct {
	Site string
	// Host 是站点地址的主机名。
	Host string
	// Header 是拼好的 Cookie 请求头(name=value; ...)。只在本机使用,不出现在接口返回和日志里。
	Header string
	// Names 是 Cookie 的名字(按名字排序),预览时给用户看。
	Names []string
}

Match 是一个站点在 CookieCloud 里找到的、对它的地址有效的 Cookie。

func MatchSites

func MatchSites(d Data, sites []Site, now time.Time) []Match

MatchSites 按站点地址的主机名挑出对它有效的 Cookie:带前导点(或 hostOnly 为 false)的域名对它本身和子域名都有效, 其余的只对同一个主机名有效;路径不是 / 的、已经过期的不要。同名的取域名最具体的那个。 没有任何 Cookie 的站点不出现在结果里;结果按站点名排序。

type Payload

type Payload struct {
	Encrypted  string `json:"encrypted"`
	CryptoType string `json:"crypto_type"`
}

Payload 是 /get/{uuid} 返回的密文。

func Fetch

func Fetch(ctx context.Context, httpc *http.Client, server, uuid string) (Payload, error)

Fetch 取回 uuid 的密文(不带密码,服务端不解密)。

type Preview

type Preview struct {
	Items   []PreviewItem `json:"items"`
	Domains int           `json:"domains"`
}

Preview 是预览结果。Domains 是 CookieCloud 里一共有多少个域名。

type PreviewItem

type PreviewItem struct {
	Site        string   `json:"site"`
	SiteName    string   `json:"site_name"`
	Host        string   `json:"host"`
	CookieNames []string `json:"cookie_names"`
	// Enabled 表示站点在 pt-tools 里已经启用;Changed 表示和现在保存的 Cookie 不同。
	Enabled bool `json:"enabled"`
	Changed bool `json:"changed"`
}

PreviewItem 是一个能导入的站点:CookieCloud 里有对它的地址有效的 Cookie。不含 Cookie 的值。

type Service

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

Service 管理 CookieCloud 设置,预览、导入与定时同步站点 Cookie。解密只在本机做,Cookie 不缓存、不写日志。 runMu 让预览、导入、同步一次只有一个;saveMu 让保存设置不和「确认设置没变、写入 Cookie」交错。

func New

func New(cfg Config) *Service

New 构造 Service。

func (*Service) Due

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

Due 报告这一刻该不该定时同步:打开了、填全了、距上次同步满了间隔。

func (*Service) Import

func (s *Service) Import(ctx context.Context, siteNames []string) (ImportResult, error)

Import 把选中站点的 Cookie 写进 pt-tools(没启用的站点会被启用)。每次重新取数解密,不用预览时的结果。

func (*Service) Preview

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

Preview 取回、解密并匹配,列出能导入的站点;不写任何东西。

func (*Service) RecordSync

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

RecordSync 记下这一次同步的时间和结果。

func (*Service) SaveSettings

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

SaveSettings 校验并保存设置,只写配置列(上次同步的结果不受影响)。打开定时同步时地址、UUID、密码都要有。

func (*Service) Settings

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

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

func (*Service) Sync

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

Sync 是定时同步:只更新已经启用、Cookie 有变化的站点,不启用新站点。 结果(成功或失败)记进设置的「上次同步」:只在数据来源仍是这一次用的那份时记,换了来源的话新来源马上到期; ctx 取消(程序退出)时不记。

type Settings

type Settings struct {
	ServerURL     string     `json:"server_url"`
	UUID          string     `json:"uuid"`
	HasPassword   bool       `json:"has_password"`
	AutoSync      bool       `json:"auto_sync"`
	IntervalHours int        `json:"interval_hours"`
	LastSyncAt    *time.Time `json:"last_sync_at,omitempty"`
	LastResult    string     `json:"last_result"`
	// NextSyncAt 是下一次定时同步大约什么时候(打开定时同步、填全了设置才有;到点以后在下一次检查时同步)
	NextSyncAt *time.Time `json:"next_sync_at,omitempty"`
}

Settings 是接口里的设置:密码只说有没有设置。

type SettingsUpdate

type SettingsUpdate struct {
	ServerURL     string  `json:"server_url"`
	UUID          string  `json:"uuid"`
	Password      *string `json:"password,omitempty"`
	AutoSync      bool    `json:"auto_sync"`
	IntervalHours int     `json:"interval_hours"`
}

SettingsUpdate 是保存设置的请求(密码以外整份保存)。Password 为 nil 时保留原来的,空串时清除。

type Site

type Site struct {
	Name    string
	BaseURL string
}

Site 是 pt-tools 的一个站点:名称和 pt-tools 访问它用的地址。

type SiteError

type SiteError struct {
	Site  string `json:"site"`
	Error string `json:"error"`
}

SiteError 是一个站点写不进去的原因。

type SiteState

type SiteState struct {
	Name        string
	DisplayName string
	BaseURL     string
	Enabled     bool
	Cookie      string
}

SiteState 是 pt-tools 支持的一个站点。BaseURL 是 pt-tools 访问它用的地址; Cookie 是现在保存的 Cookie(明文,只用来判断有没有变化,不出现在接口返回和日志里)。

Jump to

Keyboard shortcuts

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