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
- Variables
- func ParseServer(raw string) (*url.URL, error)
- func SameCookie(a, b string) bool
- type Cipher
- type Config
- type Cookie
- type Data
- type ImportResult
- type Match
- type Payload
- type Preview
- type PreviewItem
- type Service
- func (s *Service) Due(ctx context.Context) bool
- func (s *Service) Import(ctx context.Context, siteNames []string) (ImportResult, error)
- func (s *Service) Preview(ctx context.Context) (Preview, error)
- func (s *Service) RecordSync(ctx context.Context, at time.Time, summary string) error
- func (s *Service) SaveSettings(ctx context.Context, u SettingsUpdate) (Settings, error)
- func (s *Service) Settings(ctx context.Context) (Settings, error)
- func (s *Service) Sync(ctx context.Context) (ImportResult, error)
- type Settings
- type SettingsUpdate
- type Site
- type SiteError
- type SiteState
Constants ¶
const ( CryptoLegacy = "legacy" CryptoFixed = "aes-128-cbc-fixed" )
加密方式。
const MaxImportSites = 500
MaxImportSites 是一次导入最多选几个站点。
Variables ¶
var ( // ErrDecrypt 表示解不开:多半是 UUID 或密码不对。 ErrDecrypt = errors.New("解密失败:请检查 UUID 和密码") // ErrNotFound 表示服务端没有这个 UUID 的数据。 ErrNotFound = errors.New("CookieCloud 服务上没有这个 UUID 的数据") )
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 ¶
ParseServer 解析服务地址:必须是 http(s) 地址,不能带用户名密码、查询参数或 #,末尾的 / 去掉。
func SameCookie ¶
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 ¶
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 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 是一次导入或同步的结果。
type Match ¶
type Match struct {
Site string
// Host 是站点地址的主机名。
Host string
// Header 是拼好的 Cookie 请求头(name=value; ...)。只在本机使用,不出现在接口返回和日志里。
Header string
// Names 是 Cookie 的名字(按名字排序),预览时给用户看。
Names []string
}
Match 是一个站点在 CookieCloud 里找到的、对它的地址有效的 Cookie。
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 (*Service) RecordSync ¶
RecordSync 记下这一次同步的时间和结果。
func (*Service) SaveSettings ¶
SaveSettings 校验并保存设置,只写配置列(上次同步的结果不受影响)。打开定时同步时地址、UUID、密码都要有。
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"`
}
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 时保留原来的,空串时清除。