Files
Ydisks-Xianyu-Helper/internal/browser/credential_cookies.go
T
Christ 1ac6912c7b feat: 拆分 macOS 架构安装包并内置浏览器运行时
将 macOS 安装包拆分为 arm64 与 amd64 两个独立产物,每个安装包只携带对应架构的服务、托盘程序、Playwright driver 和 Chromium。

调整桌面端 CI,在构建阶段预下载并组装 Playwright runtime;服务启动时按当前进程架构解析 runtime 根目录,避免安装后联网下载 Chromium。

移除 Windows/macOS 安装阶段的 browser-install 调用和托盘中的安装/修复 Chromium 菜单,保留 Linux 安装脚本的既有流程。

升级 Playwright Go 依赖到 v0.6100.0,并迁移导入路径;仅按本次授权修改 CAPTCHA 相关文件的 import 行,不改变 CAPTCHA 行为。

验证:go test ./...、go vet ./...、macOS 安装脚本语法、Workflow YAML 语法、Windows amd64 交叉编译及两个 macOS pkg 的本地组装。
2026-08-12 01:09:59 +08:00

105 lines
3.6 KiB
Go

package browser
import (
"context"
"fmt"
"time"
"github.com/mxschmitt/playwright-go"
"xianyu-go/internal/xianyu/cookierefresh"
)
// TokenCaptchaCookieSnapshot 在滑块引擎退出并释放持久化 Profile 后,重新打开
// 同一 Profile 读取最终 Cookie Jar。它不访问任何业务页面,只用于把滑块验证
// 已经产生的精确 Domain/Path/HttpOnly/Expires 属性交回 Go 凭证层。
func (m *Manager) TokenCaptchaCookieSnapshot(ctx context.Context, cookieID string, headless bool) (string, []cookierefresh.BrowserCookie, error) {
bctx, release, err := m.newPersistentRenewContext(ctx, cookieID, "", nil, quickRenewHeadless(headless), true)
if err != nil {
return "", nil, err
}
defer release()
return readAuthoritativeCookieJar(bctx, goofishIMURL)
}
// syncCredentialCookies 为 token 滑块上下文同步 Cookie。普通登录、
// 续期、MTOP、Token 和 WebSocket 生产流程不得调用 Chromium。
func syncCredentialCookies(bctx playwright.BrowserContext, cookieStr string, snapshots ...[]cookierefresh.BrowserCookie) error {
if len(snapshots) > 0 && snapshots[0] != nil {
preserved := cookierefresh.NormalizeSnapshot(snapshots[0])
if err := bctx.ClearCookies(); err != nil {
return err
}
if len(preserved) == 0 {
return nil
}
return bctx.AddCookies(snapshotToOptionalCookies(preserved))
}
incoming := parseCookieStr(cookieStr)
if len(incoming) == 0 {
return fmt.Errorf("Cookie为空或格式错误")
}
existing, err := bctx.Cookies()
if err != nil {
return err
}
preserved := credentialCookieSnapshotForURL(cookieSnapshotFromPlaywright(existing), incoming, goofishIMURL)
if err := bctx.ClearCookies(); err != nil {
return err
}
return bctx.AddCookies(snapshotToOptionalCookies(preserved))
}
func credentialCookieSnapshot(existing []cookierefresh.BrowserCookie, incoming map[string]string) []cookierefresh.BrowserCookie {
return credentialCookieSnapshotForURL(existing, incoming, goofishIMURL)
}
func credentialCookieSnapshotForURL(existing []cookierefresh.BrowserCookie, incoming map[string]string, rawURL string) []cookierefresh.BrowserCookie {
preserved := make([]cookierefresh.BrowserCookie, 0, len(existing)+len(incoming))
matched := make(map[string]bool, len(incoming))
counts := make(map[string]int, len(existing))
for _, cookie := range cookierefresh.NormalizeSnapshot(existing) {
counts[cookie.Name]++
}
for _, cookie := range existing {
if !cookieScopeMatches(cookie, rawURL) {
if value, ok := incoming[cookie.Name]; ok {
if counts[cookie.Name] == 1 && cookie.PartitionKey == "" {
cookie.Value = value
}
matched[cookie.Name] = true
}
preserved = append(preserved, cookie)
continue
}
value, ok := incoming[cookie.Name]
if !ok {
continue
}
if counts[cookie.Name] == 1 {
cookie.Value = value
}
preserved = append(preserved, cookie)
matched[cookie.Name] = true
}
for name, value := range incoming {
if matched[name] {
continue
}
preserved = append(preserved, cookierefresh.BrowserCookie{
Name: name, Value: value, Domain: goofishDot, Path: "/", Secure: true,
})
}
return cookierefresh.NormalizeSnapshot(preserved)
}
func cookieScopeMatches(cookie cookierefresh.BrowserCookie, rawURL string) bool {
header, _ := cookierefresh.ScopedCookieHeaderForRequest([]cookierefresh.BrowserCookie{cookie}, rawURL, "https://goofish.com", time.Unix(0, 0))
return header != ""
}
func currentCookieHeader(snapshot []cookierefresh.BrowserCookie, rawURL string) string {
header, _ := cookierefresh.ScopedCookieHeaderForRequest(snapshot, rawURL, "https://goofish.com", time.Now())
return header
}