Files
Ydisks-Xianyu-Helper/internal/server/server.go
T
Christ 16d63dc7c9 完善系统可靠性、登录风控与跨数据库回归
可靠性与数据一致性:为 SQLite、MySQL、PostgreSQL 增加 00017 至 00021 迁移,引入账号暂停时间、默认回复和批量铺货租约、自动化运行恢复、延迟任务、动作检查点与通知 outbox。补强订单、卡密发货、Cookie、批量铺货和自动化任务的原子认领、幂等更新、并发控制、失败分类、重试与进程重启恢复,并扩展跨方言覆盖。

登录与风控恢复:完善扫码、密码登录、Cookie 续期、MTOP token、UA 与 WebSocket 生命周期;加入账号级持久化浏览器目录、Playwright 主引擎和直接 Chromium/CDP 备用引擎、严格的新 x5sec 成功判定、失败控件恢复及真实浏览器集成测试。增加可配置远程滑块服务,在明确失败、链接过期和服务不可用场景中保持既定的本地回退语义。

安全与配置:通过 XIANYU_DATA_KEY 为 Cookie、登录密码、设备与访问令牌、AI/SMTP 密钥及通知配置提供 AES-256-GCM 落库加密和历史明文升级。新增出站网络目标分级、防 DNS rebinding、登录限流、HTTP 客户端超时与 SMTP 目标校验;管理员配置的 AI 服务允许可信内网地址,非可信 Webhook 和远程资源继续限制为公网目标。

自动化与通知:为发货、评价赠送、邀评和回复动作增加断点、租约、延迟执行及人工继续/重试/取消入口;通知改为 outbox 驱动的可恢复投递。同步完善账号运行态、暂停状态、续期冷却、告警审计和异常查询接口。

前端体验:新增风控验证面板、方形二维码、账号运行态合并、暂停状态保护、自动化异常处置、批量铺货状态选择、订单导入结果和最新请求门禁。完善订单搜索与导入、规则编辑、批次恢复、通知与设置页面,并为请求取消、超时、陈旧响应和主要状态转换补充 Vitest 覆盖。

测试与部署:扩展三库功能编排、SQLite 一致性备份、数据卷持久化、浏览器集成镜像、前端类型检查和构建资产分包;功能测试覆盖健康检查、订单、铺货、自动化异常、账号暂停、关键词和系统设置。更新 README 中数据密钥、可信 AI 地址和测试端口说明。

验证结果:go vet ./...、go build ./cmd/server、gofmt 检查、npm run typecheck、npm test(17 个文件、95 个用例)及 npm run build 通过。go test ./... 未完全通过:internal/adapter 仍有 2 个密码续期用例失败,internal/renewal 仍有 4 个浏览器续期调度用例失败;其余 Go 包通过,失败现状随本次既有工作区一并提交。
2026-07-14 21:20:22 +08:00

438 lines
13 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package server 实现 HTTP API 服务(chi 路由)。
// 复用 internal/auth 中间件、internal/db.Store、internal/account.Manager。
// 端点按分组组织在同一 package 的多个 handler 文件中。
package server
import (
"bytes"
"context"
"io/fs"
"log/slog"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"xianyu-go/internal/account"
"xianyu-go/internal/auth"
"xianyu-go/internal/automation"
"xianyu-go/internal/browser"
"xianyu-go/internal/db"
"xianyu-go/internal/notify"
"xianyu-go/internal/webui"
"xianyu-go/internal/xianyu/mtop"
"xianyu-go/internal/xianyu/qrlogin"
)
type qrLoginService interface {
GenerateQRCode(ctx context.Context) (sessionID string, qrCodeURL string, err error)
GetSessionStatus(sessionID string) map[string]any
CompleteVerification(ctx context.Context, sessionID string) (cookies string, unb string, err error)
}
type qrLoginPersistence struct {
AccountID string
IsNew bool
UserID int64
CreatedAt time.Time
}
type qrLoginOwner struct {
UserID int64
CreatedAt time.Time
}
type publishBatchWorker struct {
token string
cancel context.CancelFunc
}
// Server 聚合 HTTP 服务依赖。Automation 与 Notifier 由构造函数注入,
// 不再允许外部直接改字段,避免运行时被替换成 nil。
type Server struct {
Store *db.Store
Auth *auth.Service
Manager *account.Manager
automation *automation.Center
Browser *browser.Manager
PasswordLogin passwordLoginRunner
notifier *notify.Notifier
MTop mtop.Client
QRLogin qrLoginService
Logger *slog.Logger
WebDir string // 前端静态资源目录(含 index.html)
Addr string
publishMu sync.Mutex
publishCancels map[string]publishBatchWorker
workerMu sync.Mutex
workerCount int
workersDone chan struct{}
lifecycleMu sync.RWMutex
lifecycleCtx context.Context
passwordMu sync.Mutex
passwordSessions map[string]*passwordLoginSession
passwordProcessing map[string]string
qrMu sync.Mutex
qrPersisted map[string]qrLoginPersistence
qrOwners map[string]qrLoginOwner
// qrPersistLocks 按扫码会话串行化持久化,避免持有全局 qrMu 执行数据库、
// 资料刷新和账号重启等慢操作。
qrPersistLocks sync.Map
loginLimiter *loginFailureLimiter
}
// New 构造。autoCenter/notifier 由调用方完成创建后注入(创建顺序:
// adapter → manager → automation → notifier → server)。
func New(store *db.Store, manager *account.Manager, bm *browser.Manager, secure bool, webDir, addr string, logger *slog.Logger, autoCenter *automation.Center, notifier *notify.Notifier) *Server {
if logger == nil {
logger = slog.New(slog.NewTextHandler(os.Stdout, nil))
}
qrMgr := qrlogin.NewManager(logger)
if bm != nil {
qrMgr.SetBrowserRefresher(func(ctx context.Context, tmpCookies, verificationURL string, onScreenshot func(string)) (string, string, error) {
return bm.QRCookieRefresh(ctx, tmpCookies, verificationURL, onScreenshot)
})
}
return &Server{
Store: store,
Auth: &auth.Service{Store: store, Logger: logger, Secure: secure},
Manager: manager,
automation: autoCenter,
Browser: bm,
PasswordLogin: passwordLoginRunnerForBrowser(bm),
notifier: notifier,
MTop: mtop.NewClient(),
QRLogin: qrMgr,
Logger: logger,
WebDir: webDir,
Addr: addr,
publishCancels: make(map[string]publishBatchWorker),
workersDone: closedSignal(),
lifecycleCtx: context.Background(),
passwordSessions: make(map[string]*passwordLoginSession),
passwordProcessing: make(map[string]string),
qrPersisted: make(map[string]qrLoginPersistence),
qrOwners: make(map[string]qrLoginOwner),
loginLimiter: newLoginFailureLimiter(),
}
}
func passwordLoginRunnerForBrowser(bm *browser.Manager) passwordLoginRunner {
if bm == nil {
return nil
}
return bm
}
// mtopClient 返回注入的 mtop 客户端;未注入时退回默认 HTTP 实现(保证零值可用)。
func (s *Server) mtopClient() mtop.Client {
if s.MTop != nil {
return s.MTop
}
return mtop.NewClient()
}
// Router 构建完整路由树。
func (s *Server) Router() http.Handler {
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(middleware.Recoverer)
// 请求日志(精简)。
r.Use(s.requestLogger)
// 健康检查(无需认证)。
r.Get("/health", s.health)
// 认证组(无需登录的端点,但解析会话以判断登录态)。
r.Group(func(r chi.Router) {
r.Use(s.Auth.Middleware) // 解析 session,不强制登录
r.Post("/login", s.login)
r.Get("/verify", s.verify)
r.Post("/logout", s.logout)
})
r.Post("/change-admin-password", s.authMiddleware(http.HandlerFunc(s.changeAdminPassword)).ServeHTTP)
r.Post("/change-password", s.authMiddleware(http.HandlerFunc(s.changePassword)).ServeHTTP)
// 认证后的 API。
r.Group(func(r chi.Router) {
r.Use(s.Auth.Middleware)
r.Use(auth.RequireAuth)
r.Put("/account/credentials", s.updateCredentials)
// 账号 cookie
s.mountCookies(r)
// 扫码登录
s.mountQRLoginReal(r)
// 密码登录
s.mountPasswordLogin(r)
// 订单
s.mountOrdersReal(r)
// 订单分析(仪表盘)
s.mountAnalyticsReal(r)
// 卡密 + 发货规则
s.mountCardsReal(r)
// 自动化规则
s.mountAutomation(r)
// 商品
s.mountItemsReal(r)
// 关键字 + 指定商品回复
s.mountKeywordsReal(r)
s.mountItemRepliesReal(r)
// 默认回复
s.mountDefaultRepliesReal(r)
// 通知
s.mountNotificationsReal(r)
// 系统设置(已认证)
s.mountSettingsReal(r)
// AI 设置
s.mountAIReplyReal(r)
// 用户
s.mountUserReal(r)
// 管理员专用。
r.Group(func(r chi.Router) {
r.Use(auth.RequireAdmin)
s.mountAdminReal(r)
})
})
// 公开系统设置(无需登录,前端登录页读取主题等)。
s.mountPublicSettings(r)
// SPA 静态资源 catch-all(最后挂载)。
s.mountSPA(r)
return r
}
// mountPublicSettings 公开系统设置(无需登录)。
func (s *Server) mountPublicSettings(r chi.Router) {
r.Get("/system-settings/public", s.publicSettings)
}
// authMiddleware 仅对单个 handler 应用会话解析 + RequireAuth。
func (s *Server) authMiddleware(h http.Handler) http.Handler {
return s.Auth.Middleware(auth.RequireAuth(h))
}
// requestLogger 记录请求完成状态、耗时和 chi request_id。
func (s *Server) requestLogger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
next.ServeHTTP(ww, r)
status := ww.Status()
if status == 0 {
status = http.StatusOK
}
level := slog.LevelDebug
if status >= 500 {
level = slog.LevelError
} else if status >= 400 {
level = slog.LevelWarn
}
s.Logger.LogAttrs(r.Context(), level, "HTTP 请求完成",
slog.String("method", r.Method),
slog.String("path", r.URL.Path),
slog.Int("status", status),
slog.Int("bytes", ww.BytesWritten()),
slog.Duration("duration", time.Since(start)),
slog.String("request_id", middleware.GetReqID(r.Context())),
slog.String("remote", r.RemoteAddr),
)
})
}
// mountSPA 挂载前端静态资源与 SPA catch-all。
//
// 前端 vite base 为 /static/,构建后 index.html 引用 /static/assets/...、/static/favicon.svg。
// 故静态资源统一从 /static/ 前缀提供;非 API 的 GET 请求(/、/login 等客户端路由)
// 返回 /static/index.html,交给 React Router 接管。
func (s *Server) mountSPA(r chi.Router) {
if s.WebDir != "" {
s.mountDirSPA(r)
return
}
embedded, err := webui.Static()
if err != nil {
return
}
s.mountFSSPA(r, embedded)
}
func (s *Server) mountDirSPA(r chi.Router) {
indexFile := filepath.Join(s.WebDir, "index.html")
// /static/* 直接作为静态文件服务(assets/、favicon.svg 等)。
// StripPrefix("/static/") 后,URL /static/assets/x.js → WebDir/assets/x.js。
staticFiles := http.StripPrefix("/static/", http.FileServer(http.Dir(s.WebDir)))
r.Handle("/static/*", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/static/" || r.URL.Path == "/static/index.html" {
setNoStore(w)
}
staticFiles.ServeHTTP(w, r)
}))
// SPA catch-all:非 API 的 GET 请求返回 index.html。
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
if isAPIPath(r.URL.Path) {
writeJSON(w, http.StatusNotFound, errDetail("接口不存在"))
return
}
if _, err := os.Stat(indexFile); err != nil {
writeJSON(w, http.StatusNotFound, errDetail("前端未构建"))
return
}
setNoStore(w)
http.ServeFile(w, r, indexFile)
})
}
func (s *Server) mountFSSPA(r chi.Router, staticFS fs.FS) {
staticFiles := http.StripPrefix("/static/", http.FileServer(http.FS(staticFS)))
r.Handle("/static/*", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/static/" || r.URL.Path == "/static/index.html" {
setNoStore(w)
}
staticFiles.ServeHTTP(w, r)
}))
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
if isAPIPath(r.URL.Path) {
writeJSON(w, http.StatusNotFound, errDetail("接口不存在"))
return
}
index, err := fs.ReadFile(staticFS, "index.html")
if err != nil {
writeJSON(w, http.StatusNotFound, errDetail("前端未构建"))
return
}
setNoStore(w)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
http.ServeContent(w, r, "index.html", time.Time{}, bytes.NewReader(index))
})
}
func setNoStore(w http.ResponseWriter) {
w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
}
// isAPIPath 判断是否为 API 路径(不应被 SPA 拦截)。
// 仅保留实际挂载的路由前缀,与 Router() 中的 mount* 一一对应。
func isAPIPath(path string) bool {
apiPrefixes := []string{
"/api/", "/admin/", "/health", "/login", "/logout", "/verify",
"/change-password", "/change-admin-password", "/account/",
"/cookies", "/cookie/", "/orders", "/analytics",
"/cards", "/automation-rules", "/items", "/keywords", "/default-replies", "/default-reply",
"/notification-channels", "/message-notifications",
"/system-settings", "/ai-reply", "/ai-models",
"/user-settings",
"/item-reply", "/itemReplays",
"/qr-login", "/password-login",
"/static/", // 静态资源(由 /static/* handler 处理,不进 catch-all)
}
for _, p := range apiPrefixes {
if strings.HasPrefix(path, p) {
return true
}
}
return false
}
// health 健康检查。
func (s *Server) health(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
defer cancel()
if s.Store == nil || s.Store.DB == nil || s.Store.DB.PingContext(ctx) != nil {
writeJSON(w, http.StatusServiceUnavailable, map[string]any{"status": "degraded", "database": "unavailable"})
return
}
writeJSON(w, http.StatusOK, map[string]any{"status": "ok", "database": "ok"})
}
// 各分组 mount*Real 方法在 handlers 文件中实现;为避免单文件过大,按业务域分文件。
// Run 启动 HTTP 服务(阻塞)。
func (s *Server) Run(ctx context.Context) error {
s.lifecycleMu.Lock()
s.lifecycleCtx = ctx
s.lifecycleMu.Unlock()
srv := &http.Server{
Addr: s.Addr,
Handler: s.Router(),
ReadHeaderTimeout: 10 * time.Second,
// 批量发布允许上传约 200 MiB;请求头仍由 10 秒限制防慢连接,正文给足上传时间。
ReadTimeout: 10 * time.Minute,
WriteTimeout: 2 * time.Minute,
IdleTimeout: 2 * time.Minute,
}
// #nosec G118 -- 关闭协程由传入的服务生命周期上下文控制。
go func() {
<-ctx.Done()
shCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(shCtx); err != nil {
s.Logger.Warn("HTTP 服务关闭异常", "err", err)
}
}()
s.Logger.Info("HTTP 服务启动", "addr", s.Addr)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
return err
}
s.waitForWorkers(10 * time.Second)
return nil
}
func closedSignal() chan struct{} {
done := make(chan struct{})
close(done)
return done
}
func (s *Server) lifecycleContext() context.Context {
s.lifecycleMu.RLock()
defer s.lifecycleMu.RUnlock()
return s.lifecycleCtx
}
func (s *Server) beginWorker() func() {
s.workerMu.Lock()
if s.workerCount == 0 {
s.workersDone = make(chan struct{})
}
s.workerCount++
s.workerMu.Unlock()
return func() {
s.workerMu.Lock()
s.workerCount--
if s.workerCount == 0 {
close(s.workersDone)
}
s.workerMu.Unlock()
}
}
func (s *Server) waitForWorkers(timeout time.Duration) {
s.workerMu.Lock()
done := s.workersDone
s.workerMu.Unlock()
timer := time.NewTimer(timeout)
defer timer.Stop()
select {
case <-done:
case <-timer.C:
s.Logger.Warn("等待后台 worker 退出超时")
}
}