feat: 优化代理准备并完善远端部署
- 增加严格代理、两分钟预取、白名单去重与分组状态展示\n- 增加远端注册令牌和安全地址校验\n- 增加 Docker Compose、Caddy 示例与控制中心部署文档
This commit is contained in:
@@ -106,7 +106,7 @@ func (a *App) SaveSettings(cfg types.Config) types.ErrorResult {
|
||||
if cfg.AccountsPerProxy > constants.MaxAccountsPerProxy {
|
||||
cfg.AccountsPerProxy = constants.MaxAccountsPerProxy
|
||||
}
|
||||
if cfg.ProxySparePercent <= 0 {
|
||||
if cfg.ProxySparePercent < constants.MinProxySparePercent {
|
||||
cfg.ProxySparePercent = constants.DefaultProxySparePercent
|
||||
}
|
||||
if cfg.ProxySparePercent > constants.MaxProxySparePercent {
|
||||
@@ -119,6 +119,7 @@ func (a *App) SaveSettings(cfg types.Config) types.ErrorResult {
|
||||
cfg.LogLevel = "info"
|
||||
}
|
||||
cfg.RemoteURL = strings.TrimSpace(cfg.RemoteURL)
|
||||
cfg.RemoteEnrollmentToken = strings.TrimSpace(cfg.RemoteEnrollmentToken)
|
||||
cfg.RemoteToken = strings.TrimSpace(cfg.RemoteToken)
|
||||
cfg.RemoteMachineName = strings.TrimSpace(cfg.RemoteMachineName)
|
||||
if proxyURL := a.effectiveProxyURL(cfg); proxyURL != "" {
|
||||
@@ -175,6 +176,7 @@ func (a *App) SaveSettings(cfg types.Config) types.ErrorResult {
|
||||
a.scheduler.UpdateProxySettings(cfg)
|
||||
remoteChanged := cfg.RemoteEnabled != oldCfg.RemoteEnabled ||
|
||||
cfg.RemoteURL != oldCfg.RemoteURL ||
|
||||
cfg.RemoteEnrollmentToken != oldCfg.RemoteEnrollmentToken ||
|
||||
cfg.RemoteToken != oldCfg.RemoteToken ||
|
||||
cfg.RemoteMachineName != oldCfg.RemoteMachineName
|
||||
if remoteChanged {
|
||||
|
||||
@@ -148,7 +148,7 @@ func proxyDrillStandbyCount(activeCount, sparePercent int) int {
|
||||
if activeCount <= 0 {
|
||||
return 0
|
||||
}
|
||||
if sparePercent <= 0 {
|
||||
if sparePercent < constants.MinProxySparePercent {
|
||||
sparePercent = constants.DefaultProxySparePercent
|
||||
}
|
||||
if sparePercent > constants.MaxProxySparePercent {
|
||||
@@ -195,7 +195,7 @@ func (a *App) extractProxyDrillPool(cfg types.Config, desired int) ([]proxyprovi
|
||||
}
|
||||
}
|
||||
if round < rounds-1 {
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
time.Sleep(1200 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
if len(endpoints) < desired {
|
||||
|
||||
@@ -11,21 +11,21 @@ import (
|
||||
)
|
||||
|
||||
func TestProxyDrillPlanAndTargets(t *testing.T) {
|
||||
if got := proxyDrillStandbyCount(6, 25); got != 2 {
|
||||
t.Fatalf("proxyDrillStandbyCount(6, 25)=%d, want 2", got)
|
||||
if got := proxyDrillStandbyCount(6, 50); got != 3 {
|
||||
t.Fatalf("proxyDrillStandbyCount(6, 50)=%d, want 3", got)
|
||||
}
|
||||
plan := proxyDrillPlan{accounts: 30, groupSize: 5, active: 6, standby: 2}
|
||||
endpoints := make([]proxyprovider.Endpoint, 8)
|
||||
plan := proxyDrillPlan{accounts: 30, groupSize: 5, active: 6, standby: 3}
|
||||
endpoints := make([]proxyprovider.Endpoint, 9)
|
||||
targets := buildProxyDrillTargets(plan, endpoints)
|
||||
if len(targets) != 8 {
|
||||
t.Fatalf("targets=%d, want 8", len(targets))
|
||||
if len(targets) != 9 {
|
||||
t.Fatalf("targets=%d, want 9", len(targets))
|
||||
}
|
||||
totalRequests := 0
|
||||
for _, target := range targets {
|
||||
totalRequests += target.requests
|
||||
}
|
||||
if totalRequests != 40 {
|
||||
t.Fatalf("totalRequests=%d, want 40 virtual requests", totalRequests)
|
||||
if totalRequests != 45 {
|
||||
t.Fatalf("totalRequests=%d, want 45 virtual requests", totalRequests)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,10 +18,14 @@ func (a *App) configureRemote(cfg types.Config) {
|
||||
}
|
||||
serverURL := strings.TrimSpace(cfg.RemoteURL)
|
||||
token := strings.TrimSpace(cfg.RemoteToken)
|
||||
enrollmentToken := strings.TrimSpace(cfg.RemoteEnrollmentToken)
|
||||
if serverURL == "" {
|
||||
logger.LogEvent("远端", "warn", "远端控制已启用,但控制中心地址为空")
|
||||
return
|
||||
}
|
||||
if token == "" && enrollmentToken == "" {
|
||||
logger.LogEvent("远端", "warn", "未填写注册令牌,仅能在控制中心自动注册窗口内首次连接")
|
||||
}
|
||||
machineID, err := remote.LoadOrCreateMachineID(a.paths.DataDir)
|
||||
if err != nil {
|
||||
logger.LogEvent("远端", "warn", "远端机器标识初始化失败: %v", err)
|
||||
@@ -32,14 +36,15 @@ func (a *App) configureRemote(cfg types.Config) {
|
||||
machineName, _ = os.Hostname()
|
||||
}
|
||||
agent, err := remote.NewAgent(remote.AgentOptions{
|
||||
DataDir: a.paths.DataDir,
|
||||
ServerURL: serverURL,
|
||||
Token: token,
|
||||
MachineID: machineID,
|
||||
MachineName: machineName,
|
||||
InstanceID: a.instanceName,
|
||||
Instance: a.instanceName,
|
||||
Version: a.GetVersion(),
|
||||
DataDir: a.paths.DataDir,
|
||||
ServerURL: serverURL,
|
||||
Token: token,
|
||||
EnrollmentToken: enrollmentToken,
|
||||
MachineID: machineID,
|
||||
MachineName: machineName,
|
||||
InstanceID: a.instanceName,
|
||||
Instance: a.instanceName,
|
||||
Version: a.GetVersion(),
|
||||
Snapshot: func() remote.Snapshot {
|
||||
scheduler := a.getScheduler()
|
||||
if scheduler == nil {
|
||||
@@ -50,6 +55,7 @@ func (a *App) configureRemote(cfg types.Config) {
|
||||
Running: scheduler.IsRunning(),
|
||||
Results: scheduler.Results(),
|
||||
AccountCount: scheduler.AccountCount(),
|
||||
ProxyStatus: proxyStatusPointer(scheduler.ProxyStatus()),
|
||||
}
|
||||
},
|
||||
Stop: func() error {
|
||||
@@ -77,6 +83,10 @@ func (a *App) configureRemote(cfg types.Config) {
|
||||
logger.LogEvent("远端", "info", "远端控制已启用: %s,机器=%s,实例=%s", serverURL, machineName, a.instanceName)
|
||||
}
|
||||
|
||||
func proxyStatusPointer(status types.ProxyStatusView) *types.ProxyStatusView {
|
||||
return &status
|
||||
}
|
||||
|
||||
func (a *App) closeRemote() {
|
||||
logger.SetObserver(nil)
|
||||
a.remoteMu.Lock()
|
||||
|
||||
@@ -28,6 +28,9 @@ func (a *App) StartSeckill(accountProducts map[string]string) types.MessageResul
|
||||
if err := a.scheduler.PrepareProxyGroups(accountProducts); err != nil {
|
||||
return types.MessageResult{Error: "代理准备失败: " + err.Error()}
|
||||
}
|
||||
if ok, reason := a.scheduler.ProxyRunGate(); !ok {
|
||||
return types.MessageResult{Error: "严格代理模式未就绪: " + reason}
|
||||
}
|
||||
if !a.scheduler.TryStartRun() {
|
||||
return types.MessageResult{Error: "抢购任务正在运行,请勿重复提交"}
|
||||
}
|
||||
@@ -81,6 +84,9 @@ func (a *App) startXiaodianSeckill(req types.FixedSeckillRequest, message string
|
||||
if err := a.scheduler.PrepareProxyGroups(req.AccountProducts); err != nil {
|
||||
return types.MessageResult{Error: "代理准备失败: " + err.Error()}
|
||||
}
|
||||
if ok, reason := a.scheduler.ProxyRunGate(); !ok {
|
||||
return types.MessageResult{Error: "严格代理模式未就绪: " + reason}
|
||||
}
|
||||
if !a.scheduler.TryStartRun() {
|
||||
return types.MessageResult{Error: "抢购任务正在运行,请勿重复提交"}
|
||||
}
|
||||
@@ -104,6 +110,14 @@ func (a *App) GetResults() map[string]string {
|
||||
return a.scheduler.Results()
|
||||
}
|
||||
|
||||
// GetProxyStatus returns the current instance proxy group and account mapping.
|
||||
func (a *App) GetProxyStatus() types.ProxyStatusView {
|
||||
if a.scheduler == nil {
|
||||
return types.ProxyStatusView{State: "disabled"}
|
||||
}
|
||||
return a.scheduler.ProxyStatus()
|
||||
}
|
||||
|
||||
// GetLogs 返回后端内存日志,支持增量获取。
|
||||
func (a *App) GetLogs(since int64) types.LogsResult {
|
||||
entries := a.logStore.Since(since)
|
||||
|
||||
@@ -175,6 +175,7 @@ func defaultConfig() types.Config {
|
||||
RetryStages: defaultRetryStages(),
|
||||
SeckillMode: types.SeckillModeAggressive,
|
||||
XiaodianPageSize: 200,
|
||||
ProxyRequired: true,
|
||||
AccountsPerProxy: constants.DefaultAccountsPerProxy,
|
||||
ProxySparePercent: constants.DefaultProxySparePercent,
|
||||
}
|
||||
@@ -240,6 +241,7 @@ func LoadConfig(path string) types.Config {
|
||||
|
||||
func normalizeRemoteConfig(cfg *types.Config) {
|
||||
cfg.RemoteURL = strings.TrimSpace(cfg.RemoteURL)
|
||||
cfg.RemoteEnrollmentToken = strings.TrimSpace(cfg.RemoteEnrollmentToken)
|
||||
cfg.RemoteToken = strings.TrimSpace(cfg.RemoteToken)
|
||||
cfg.RemoteMachineName = strings.TrimSpace(cfg.RemoteMachineName)
|
||||
}
|
||||
@@ -251,7 +253,7 @@ func normalizeProxyConfig(cfg *types.Config) {
|
||||
if cfg.AccountsPerProxy > constants.MaxAccountsPerProxy {
|
||||
cfg.AccountsPerProxy = constants.MaxAccountsPerProxy
|
||||
}
|
||||
if cfg.ProxySparePercent <= 0 {
|
||||
if cfg.ProxySparePercent < constants.MinProxySparePercent {
|
||||
cfg.ProxySparePercent = constants.DefaultProxySparePercent
|
||||
}
|
||||
if cfg.ProxySparePercent > constants.MaxProxySparePercent {
|
||||
|
||||
@@ -137,6 +137,12 @@ func TestLoadConfigNormalizesProxySparePercent(t *testing.T) {
|
||||
if got := LoadConfig(path).ProxySparePercent; got != constants.DefaultProxySparePercent {
|
||||
t.Fatalf("ProxySparePercent=%d, want %d", got, constants.DefaultProxySparePercent)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(`{"proxySparePercent":25}`), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := LoadConfig(path).ProxySparePercent; got != constants.MinProxySparePercent {
|
||||
t.Fatalf("legacy ProxySparePercent=%d, want minimum %d", got, constants.MinProxySparePercent)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(`{"proxySparePercent":999}`), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -145,6 +151,35 @@ func TestLoadConfigNormalizesProxySparePercent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfigDefaultsToStrictProxyMode(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "config.json")
|
||||
if got := LoadConfig(path).ProxyRequired; !got {
|
||||
t.Fatal("new config should default to strict proxy mode")
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(`{}`), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := LoadConfig(path).ProxyRequired; !got {
|
||||
t.Fatal("legacy config without proxyRequired should inherit strict mode")
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(`{"proxyRequired":false}`), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := LoadConfig(path).ProxyRequired; got {
|
||||
t.Fatal("explicit proxyRequired=false should disable strict mode")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfigNormalizesRemoteEnrollmentToken(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "config.json")
|
||||
if err := os.WriteFile(path, []byte(`{"remoteEnrollmentToken":" enroll-secret "}`), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := LoadConfig(path).RemoteEnrollmentToken; got != "enroll-secret" {
|
||||
t.Fatalf("RemoteEnrollmentToken=%q, want trimmed token", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfigMigratesLegacyTimeAPI(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "config.json")
|
||||
content := []byte(`{"timeApiUrl":"` + constants.LegacyDefaultTimeAPI + `"}`)
|
||||
|
||||
@@ -80,6 +80,9 @@ func ApplySharedProxyConfig(cfg *types.Config, shared types.SharedProxyConfig) {
|
||||
if proxy.Enabled != nil {
|
||||
cfg.ProxyEnabled = *proxy.Enabled
|
||||
}
|
||||
if proxy.Required != nil {
|
||||
cfg.ProxyRequired = *proxy.Required
|
||||
}
|
||||
if proxy.API != nil {
|
||||
cfg.ProxyAPI = strings.TrimSpace(*proxy.API)
|
||||
}
|
||||
@@ -111,6 +114,9 @@ func ApplySharedProxyConfig(cfg *types.Config, shared types.SharedProxyConfig) {
|
||||
if remote.URL != nil {
|
||||
cfg.RemoteURL = strings.TrimSpace(*remote.URL)
|
||||
}
|
||||
if remote.EnrollmentToken != nil {
|
||||
cfg.RemoteEnrollmentToken = strings.TrimSpace(*remote.EnrollmentToken)
|
||||
}
|
||||
if remote.Token != nil {
|
||||
cfg.RemoteToken = strings.TrimSpace(*remote.Token)
|
||||
}
|
||||
@@ -131,6 +137,7 @@ func SaveSharedProxyConfig(path string, cfg types.Config) error {
|
||||
UpdatedAt: time.Now().Format(time.RFC3339),
|
||||
Proxy: types.SharedProxySettings{
|
||||
Enabled: boolPointer(cfg.ProxyEnabled),
|
||||
Required: boolPointer(cfg.ProxyRequired),
|
||||
API: stringPointer(cfg.ProxyAPI),
|
||||
AccountsPerProxy: intPointer(cfg.AccountsPerProxy),
|
||||
SparePercent: intPointer(cfg.ProxySparePercent),
|
||||
@@ -141,10 +148,11 @@ func SaveSharedProxyConfig(path string, cfg types.Config) error {
|
||||
WhitelistSignKey: stringPointer(cfg.ProxyWhitelistSignKey),
|
||||
},
|
||||
Remote: types.SharedRemoteSettings{
|
||||
Enabled: boolPointer(cfg.RemoteEnabled),
|
||||
URL: stringPointer(cfg.RemoteURL),
|
||||
Token: stringPointer(cfg.RemoteToken),
|
||||
MachineName: stringPointer(cfg.RemoteMachineName),
|
||||
Enabled: boolPointer(cfg.RemoteEnabled),
|
||||
URL: stringPointer(cfg.RemoteURL),
|
||||
EnrollmentToken: stringPointer(cfg.RemoteEnrollmentToken),
|
||||
Token: stringPointer(cfg.RemoteToken),
|
||||
MachineName: stringPointer(cfg.RemoteMachineName),
|
||||
},
|
||||
}
|
||||
data, err := json.MarshalIndent(shared, "", " ")
|
||||
@@ -188,6 +196,7 @@ func SaveSharedProxyConfig(path string, cfg types.Config) error {
|
||||
|
||||
func hasSharedProxySettings(proxy types.SharedProxySettings) bool {
|
||||
return proxy.Enabled != nil ||
|
||||
proxy.Required != nil ||
|
||||
proxy.API != nil ||
|
||||
proxy.AccountsPerProxy != nil ||
|
||||
proxy.SparePercent != nil ||
|
||||
|
||||
@@ -13,10 +13,11 @@ func TestSharedProxyConfigRoundTripExcludesRuntimeProxy(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "shared-proxy.json")
|
||||
cfg := types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
ProxyURL: "http://user:pass@127.0.0.1:9000",
|
||||
ProxyAPI: "https://service.ipzan.com/core-extract?mode=auth",
|
||||
AccountsPerProxy: 5,
|
||||
ProxySparePercent: 25,
|
||||
ProxySparePercent: 50,
|
||||
AutoFetchProxyOnSchedule: true,
|
||||
ProxyAutoWhitelist: true,
|
||||
ProxyWhitelistReplace: true,
|
||||
@@ -24,6 +25,7 @@ func TestSharedProxyConfigRoundTripExcludesRuntimeProxy(t *testing.T) {
|
||||
ProxyWhitelistSignKey: "0123456789abcdef",
|
||||
RemoteEnabled: true,
|
||||
RemoteURL: "https://control.example.com",
|
||||
RemoteEnrollmentToken: "enroll-token",
|
||||
RemoteToken: "remote-token",
|
||||
RemoteMachineName: "PC-01",
|
||||
}
|
||||
@@ -52,7 +54,14 @@ func TestSharedProxyConfigRoundTripExcludesRuntimeProxy(t *testing.T) {
|
||||
if !local.ProxyEnabled || local.ProxyAPI != cfg.ProxyAPI || local.AccountsPerProxy != 5 {
|
||||
t.Fatalf("unexpected merged config: %+v", local)
|
||||
}
|
||||
if local.ProxySparePercent != 50 {
|
||||
t.Fatalf("ProxySparePercent=%d, want 50", local.ProxySparePercent)
|
||||
}
|
||||
if !local.ProxyRequired {
|
||||
t.Fatal("ProxyRequired should round-trip through shared config")
|
||||
}
|
||||
if !local.RemoteEnabled || local.RemoteURL != cfg.RemoteURL ||
|
||||
local.RemoteEnrollmentToken != cfg.RemoteEnrollmentToken ||
|
||||
local.RemoteToken != cfg.RemoteToken || local.RemoteMachineName != cfg.RemoteMachineName {
|
||||
t.Fatalf("remote settings were not merged: %+v", local)
|
||||
}
|
||||
|
||||
@@ -15,7 +15,8 @@ const (
|
||||
DefaultConcurrency = 150
|
||||
DefaultAccountsPerProxy = 5
|
||||
MaxAccountsPerProxy = 100
|
||||
DefaultProxySparePercent = 25
|
||||
MinProxySparePercent = 50
|
||||
DefaultProxySparePercent = 50
|
||||
MaxProxySparePercent = 100
|
||||
ProductCacheTTL = 30 * time.Second
|
||||
|
||||
|
||||
@@ -98,7 +98,11 @@ func (p *GenericProvider) Parse(apiURL string, body []byte) ([]Endpoint, error)
|
||||
}
|
||||
|
||||
func isGenericFatalMessage(message string) bool {
|
||||
if strings.Contains(message, "提取频率过快") || strings.Contains(message, "请求过于频繁") {
|
||||
if strings.Contains(message, "提取频率过快") ||
|
||||
strings.Contains(message, "请求过于频繁") ||
|
||||
strings.Contains(message, "访问频率过快") ||
|
||||
strings.Contains(message, "白名单仅支持1秒") ||
|
||||
strings.Contains(message, "白名单仅支持 1 秒") {
|
||||
return false
|
||||
}
|
||||
for _, hint := range []string{
|
||||
|
||||
+55
-9
@@ -14,16 +14,20 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const whitelistRequestGap = 1200 * time.Millisecond
|
||||
|
||||
// IPZanProvider 接入品赞 https://service.ipzan.com/core-extract。
|
||||
type IPZanProvider struct {
|
||||
baseURL string
|
||||
now func() time.Time
|
||||
baseURL string
|
||||
now func() time.Time
|
||||
whitelistGap time.Duration
|
||||
}
|
||||
|
||||
func NewIPZanProvider() *IPZanProvider {
|
||||
return &IPZanProvider{
|
||||
baseURL: "https://service.ipzan.com",
|
||||
now: time.Now,
|
||||
baseURL: "https://service.ipzan.com",
|
||||
now: time.Now,
|
||||
whitelistGap: whitelistRequestGap,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,10 +48,8 @@ func (p *IPZanProvider) RequestURL(apiURL string, count int) (string, error) {
|
||||
}
|
||||
query := u.Query()
|
||||
query.Set("num", strconv.Itoa(count))
|
||||
if strings.EqualFold(strings.TrimSpace(query.Get("mode")), "auth") {
|
||||
// txt 只返回 IP:端口,会丢失每个代理各自的账号密码,随后必然触发 407。
|
||||
query.Set("format", "json")
|
||||
}
|
||||
// JSON 才能保留每个代理独立的账号密码;txt 只返回 IP:端口,鉴权模式会触发 407。
|
||||
query.Set("format", "json")
|
||||
u.RawQuery = query.Encode()
|
||||
return u.String(), nil
|
||||
}
|
||||
@@ -160,6 +162,25 @@ func (p *IPZanProvider) AddWhitelist(ctx context.Context, httpClient *http.Clien
|
||||
if strings.TrimSpace(cfg.LoginPassword) == "" || strings.TrimSpace(cfg.SignKey) == "" {
|
||||
return &ProviderError{Provider: "品赞", Message: "自动白名单需要品赞登录密码和签名密钥", Fatal: true}
|
||||
}
|
||||
guard, err := acquireWhitelistLock(ctx, p.baseURL, no, cfg.IP)
|
||||
if err != nil {
|
||||
return fmt.Errorf("等待本机白名单协调锁失败: %w", err)
|
||||
}
|
||||
defer guard.release()
|
||||
if guard.recentlyAdded() {
|
||||
return nil
|
||||
}
|
||||
exists, err := p.verifyWhitelist(ctx, httpClient, no, cfg.IP)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
guard.markAdded()
|
||||
return nil
|
||||
}
|
||||
if err := p.waitWhitelistSlot(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
sign, err := p.signWhitelist(cfg.LoginPassword, secret, cfg.SignKey)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -186,9 +207,24 @@ func (p *IPZanProvider) AddWhitelist(ctx context.Context, httpClient *http.Clien
|
||||
if err := p.doWhitelistRequest(req, httpClient, "添加白名单"); err != nil {
|
||||
return err
|
||||
}
|
||||
guard.markAdded()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *IPZanProvider) waitWhitelistSlot(ctx context.Context) error {
|
||||
if p.whitelistGap <= 0 {
|
||||
return nil
|
||||
}
|
||||
timer := time.NewTimer(p.whitelistGap)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-timer.C:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p *IPZanProvider) verifyWhitelist(ctx context.Context, httpClient *http.Client, no, ip string) (bool, error) {
|
||||
endpoint, err := url.Parse(strings.TrimRight(p.baseURL, "/") + "/whiteList-verify")
|
||||
if err != nil {
|
||||
@@ -297,7 +333,17 @@ func (p *IPZanProvider) doWhitelistRequestData(req *http.Request, httpClient *ht
|
||||
}
|
||||
|
||||
func isIPZanFatalMessage(message string) bool {
|
||||
if strings.Contains(message, "提取频率过快") {
|
||||
for _, retryable := range []string{
|
||||
"提取频率过快",
|
||||
"访问频率过快",
|
||||
"白名单仅支持1秒",
|
||||
"白名单仅支持 1 秒",
|
||||
} {
|
||||
if strings.Contains(message, retryable) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if strings.Contains(message, "白名单") && strings.Contains(message, "频繁") {
|
||||
return false
|
||||
}
|
||||
for _, hint := range []string{
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestIPZanRequestURLAlwaysUsesJSON(t *testing.T) {
|
||||
provider := NewIPZanProvider()
|
||||
raw := "https://service.ipzan.com/core-extract?mode=auth&format=txt&num=1"
|
||||
got, err := provider.RequestURL(raw, 5)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
u, err := url.Parse(got)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if format := u.Query().Get("format"); format != "json" {
|
||||
t.Fatalf("format=%q, want json", format)
|
||||
}
|
||||
if num := u.Query().Get("num"); num != "5" {
|
||||
t.Fatalf("num=%q, want 5", num)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIPZanWhitelistRateLimitIsRetryable(t *testing.T) {
|
||||
for _, message := range []string{
|
||||
"访问频率过快,休息一下 白名单仅支持1秒访一次",
|
||||
"添加白名单失败: 访问频率过快",
|
||||
"提取频率过快",
|
||||
} {
|
||||
if isIPZanFatalMessage(message) {
|
||||
t.Fatalf("%q should be retryable", message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWhitelistGuardSerializesAndRemembersSuccessfulAdd(t *testing.T) {
|
||||
const (
|
||||
baseURL = "https://example.test"
|
||||
no = "guard-test"
|
||||
ip = "203.0.113.99"
|
||||
)
|
||||
first, err := acquireWhitelistLock(context.Background(), baseURL, no, ip)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first.markAdded()
|
||||
t.Cleanup(func() {
|
||||
_ = os.Remove(first.path)
|
||||
_ = os.Remove(first.markerPath)
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 80*time.Millisecond)
|
||||
defer cancel()
|
||||
if _, err := acquireWhitelistLock(ctx, baseURL, no, ip); err == nil {
|
||||
t.Fatal("second process should wait while the first guard is held")
|
||||
}
|
||||
first.release()
|
||||
|
||||
second, err := acquireWhitelistLock(context.Background(), baseURL, no, ip)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer second.release()
|
||||
if !second.recentlyAdded() {
|
||||
t.Fatal("successful whitelist add should be reused by sibling instances")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
const whitelistLockStaleAfter = 2 * time.Minute
|
||||
const whitelistMarkerTTL = 10 * time.Minute
|
||||
|
||||
type whitelistGuard struct {
|
||||
path string
|
||||
markerPath string
|
||||
}
|
||||
|
||||
func acquireWhitelistLock(ctx context.Context, baseURL, no, ip string) (*whitelistGuard, error) {
|
||||
sum := sha256.Sum256([]byte(baseURL + "\x00" + no + "\x00" + ip))
|
||||
dir := filepath.Join(os.TempDir(), "douyu-go-whitelist")
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
key := hex.EncodeToString(sum[:16])
|
||||
path := filepath.Join(dir, key+".lock")
|
||||
guard := &whitelistGuard{
|
||||
path: path,
|
||||
markerPath: filepath.Join(dir, key+".ok"),
|
||||
}
|
||||
for {
|
||||
file, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600)
|
||||
if err == nil {
|
||||
_, _ = fmt.Fprintf(file, "%d\n%s\n", os.Getpid(), time.Now().Format(time.RFC3339Nano))
|
||||
_ = file.Close()
|
||||
return guard, nil
|
||||
}
|
||||
if !errors.Is(err, os.ErrExist) {
|
||||
return nil, err
|
||||
}
|
||||
if info, statErr := os.Stat(path); statErr == nil && time.Since(info.ModTime()) > whitelistLockStaleAfter {
|
||||
_ = os.Remove(path)
|
||||
continue
|
||||
}
|
||||
timer := time.NewTimer(200 * time.Millisecond)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
timer.Stop()
|
||||
return nil, ctx.Err()
|
||||
case <-timer.C:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (g *whitelistGuard) recentlyAdded() bool {
|
||||
if g == nil {
|
||||
return false
|
||||
}
|
||||
info, err := os.Stat(g.markerPath)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
age := time.Since(info.ModTime())
|
||||
return age >= 0 && age < whitelistMarkerTTL
|
||||
}
|
||||
|
||||
func (g *whitelistGuard) markAdded() {
|
||||
if g == nil {
|
||||
return
|
||||
}
|
||||
_ = os.WriteFile(g.markerPath, []byte(time.Now().Format(time.RFC3339Nano)), 0o600)
|
||||
}
|
||||
|
||||
func (g *whitelistGuard) release() {
|
||||
if g != nil {
|
||||
_ = os.Remove(g.path)
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -444,11 +443,7 @@ func cloneResults(source map[string]string) map[string]string {
|
||||
}
|
||||
|
||||
func agentWebSocketURL(raw string) (string, error) {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return "", errors.New("远端地址为空")
|
||||
}
|
||||
u, err := url.Parse(raw)
|
||||
u, err := normalizeControlCenterURL(raw)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -461,9 +456,7 @@ func agentWebSocketURL(raw string) (string, error) {
|
||||
default:
|
||||
return "", errors.New("远端地址必须以 http://、https://、ws:// 或 wss:// 开头")
|
||||
}
|
||||
if strings.TrimSpace(u.Path) == "" || u.Path == "/" {
|
||||
u.Path = "/ws/agent"
|
||||
}
|
||||
u.Path = "/ws/agent"
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -139,7 +139,7 @@ func enrollAgent(serverURL string, options AgentOptions) (string, error) {
|
||||
}
|
||||
|
||||
func enrollHTTPURL(raw string) (string, error) {
|
||||
u, err := url.Parse(strings.TrimSpace(raw))
|
||||
u, err := normalizeControlCenterURL(raw)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -152,8 +152,33 @@ func enrollHTTPURL(raw string) (string, error) {
|
||||
default:
|
||||
return "", errors.New("远端地址协议无效")
|
||||
}
|
||||
if strings.TrimSpace(u.Path) == "" || u.Path == "/" {
|
||||
u.Path = "/api/enroll"
|
||||
}
|
||||
u.Path = "/api/enroll"
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
func normalizeControlCenterURL(raw string) (*url.URL, error) {
|
||||
u, err := url.Parse(strings.TrimSpace(raw))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch strings.ToLower(u.Scheme) {
|
||||
case "http", "https", "ws", "wss":
|
||||
default:
|
||||
return nil, errors.New("远端地址必须以 http://、https://、ws:// 或 wss:// 开头")
|
||||
}
|
||||
if u.Host == "" {
|
||||
return nil, errors.New("远端地址缺少主机名")
|
||||
}
|
||||
if u.User != nil {
|
||||
return nil, errors.New("远端地址不能包含用户名或密码")
|
||||
}
|
||||
if u.RawQuery != "" || u.Fragment != "" {
|
||||
return nil, errors.New("远端地址不能包含查询参数或 #admin 管理令牌")
|
||||
}
|
||||
if path := strings.TrimRight(strings.TrimSpace(u.Path), "/"); path != "" {
|
||||
return nil, errors.New("远端地址必须是控制中心根地址,不能包含子路径")
|
||||
}
|
||||
u.Path = ""
|
||||
u.RawPath = ""
|
||||
return u, nil
|
||||
}
|
||||
|
||||
+17
-14
@@ -3,6 +3,8 @@ package remote
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -17,20 +19,21 @@ const (
|
||||
|
||||
// Snapshot is the latest state reported by one application instance.
|
||||
type Snapshot struct {
|
||||
MachineID string `json:"machineId"`
|
||||
MachineName string `json:"machineName"`
|
||||
InstanceID string `json:"instanceId"`
|
||||
InstanceName string `json:"instanceName"`
|
||||
Version string `json:"version"`
|
||||
ActivityID string `json:"activityId,omitempty"`
|
||||
State string `json:"state"`
|
||||
Running bool `json:"running"`
|
||||
RunID string `json:"runId,omitempty"`
|
||||
Results map[string]string `json:"results,omitempty"`
|
||||
AccountCount int `json:"accountCount"`
|
||||
QueuedEvents uint64 `json:"queuedEvents"`
|
||||
DroppedEvents uint64 `json:"droppedEvents"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
MachineID string `json:"machineId"`
|
||||
MachineName string `json:"machineName"`
|
||||
InstanceID string `json:"instanceId"`
|
||||
InstanceName string `json:"instanceName"`
|
||||
Version string `json:"version"`
|
||||
ActivityID string `json:"activityId,omitempty"`
|
||||
State string `json:"state"`
|
||||
Running bool `json:"running"`
|
||||
RunID string `json:"runId,omitempty"`
|
||||
Results map[string]string `json:"results,omitempty"`
|
||||
ProxyStatus *types.ProxyStatusView `json:"proxyStatus,omitempty"`
|
||||
AccountCount int `json:"accountCount"`
|
||||
QueuedEvents uint64 `json:"queuedEvents"`
|
||||
DroppedEvents uint64 `json:"droppedEvents"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
|
||||
// LogEvent is one structured event uploaded by an application instance.
|
||||
|
||||
@@ -34,6 +34,29 @@ func TestLoadOrCreateMachineIDSharedByInstances(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateServerURLRequiresControlCenterRoot(t *testing.T) {
|
||||
for _, raw := range []string{
|
||||
"https://control.example.com/#admin=secret",
|
||||
"https://control.example.com/control",
|
||||
"https://control.example.com/?token=secret",
|
||||
"https://user:pass@control.example.com",
|
||||
} {
|
||||
if err := ValidateServerURL(raw); err == nil {
|
||||
t.Fatalf("ValidateServerURL(%q) should fail", raw)
|
||||
}
|
||||
}
|
||||
for _, raw := range []string{
|
||||
"https://control.example.com",
|
||||
"https://control.example.com/",
|
||||
"http://127.0.0.1:18080",
|
||||
"wss://control.example.com",
|
||||
} {
|
||||
if err := ValidateServerURL(raw); err != nil {
|
||||
t.Fatalf("ValidateServerURL(%q): %v", raw, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEventSpoolPersistsOffset(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
spool, err := NewEventSpool(dir)
|
||||
@@ -209,6 +232,50 @@ func TestAgentAutoEnrollsWithoutManualToken(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentEnrollsWithConfiguredEnrollmentToken(t *testing.T) {
|
||||
server, err := NewServer(ServerOptions{
|
||||
DataDir: t.TempDir(),
|
||||
OperatorToken: "operator-token",
|
||||
EnrollToken: "enroll-secret",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer server.Close()
|
||||
httpServer := httptest.NewServer(server.Handler())
|
||||
defer httpServer.Close()
|
||||
|
||||
dataDir := t.TempDir()
|
||||
agent, err := NewAgent(AgentOptions{
|
||||
DataDir: dataDir,
|
||||
ServerURL: httpServer.URL,
|
||||
EnrollmentToken: "enroll-secret",
|
||||
MachineID: "machine-secure",
|
||||
MachineName: "PC-SECURE",
|
||||
InstanceID: "实例 4",
|
||||
Instance: "实例 4",
|
||||
Version: "test",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
agent.Start(ctx)
|
||||
defer agent.Close()
|
||||
waitUntil(t, 5*time.Second, func() bool {
|
||||
instances := server.hub.Dashboard().Instances
|
||||
return len(instances) == 1 && instances[0].Online
|
||||
})
|
||||
token, err := loadAgentCredential(dataDir, httpServer.URL, "machine-secure", "实例 4")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if token == "" || token == "enroll-secret" {
|
||||
t.Fatalf("unexpected issued instance token %q", token)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHubAggregatesDynamicInstanceCount(t *testing.T) {
|
||||
hub, err := NewHub(t.TempDir())
|
||||
if err != nil {
|
||||
|
||||
@@ -310,6 +310,7 @@
|
||||
for (const item of instances) {
|
||||
machine.append(renderInstance(item));
|
||||
machine.append(renderResults(item));
|
||||
machine.append(renderProxyDetails(item));
|
||||
}
|
||||
machinesRoot.append(machine);
|
||||
}
|
||||
@@ -329,11 +330,17 @@
|
||||
const success = results.filter(value => String(value).startsWith('已抢到') || String(value).startsWith('成功') || String(value).includes('|积分-')).length;
|
||||
const resultCell = textNode('div', `结果 ${success}/${results.length || item.accountCount || 0}`);
|
||||
|
||||
const meta = textNode('div', `${item.accountCount || 0} 账号 · ${item.version || ''}`, 'muted');
|
||||
const proxy = item.proxyStatus;
|
||||
const proxyText = proxy?.enabled
|
||||
? `代理 ${proxy.activeCount || 0} · 备用 ${proxy.standbyCount || 0}/${proxy.standbyTarget || 0} · 直连 ${proxy.directCount || 0}`
|
||||
: '本地直连';
|
||||
const proxyWarning = ['direct_fallback', 'failed', 'degraded'].includes(proxy?.state);
|
||||
const meta = textNode('div', `${item.accountCount || 0} 账号 · ${proxyText}`, proxyWarning ? 'bad' : 'muted');
|
||||
|
||||
const actions = element('div', 'actions');
|
||||
actions.append(
|
||||
resultButton(item),
|
||||
proxyButton(item),
|
||||
logButton(item),
|
||||
commandButton(item, 'status', '刷新'),
|
||||
commandButton(item, 'stop', '停止', 'danger'),
|
||||
@@ -343,6 +350,42 @@
|
||||
return row;
|
||||
}
|
||||
|
||||
function renderProxyDetails(item) {
|
||||
const panel = element('div', 'results-details');
|
||||
panel.dataset.proxy = encodeURIComponent(`${item.machineId}:${item.instanceId}`);
|
||||
const proxy = item.proxyStatus;
|
||||
if (!proxy?.enabled) {
|
||||
panel.append(textNode('div', '当前实例未启用代理', 'muted'));
|
||||
return panel;
|
||||
}
|
||||
const summary = element('div', 'result-line');
|
||||
summary.append(
|
||||
textNode('span', `状态 ${proxy.state || 'unknown'}${proxy.required ? ' · 严格代理' : ''}`, ['direct_fallback', 'failed', 'degraded'].includes(proxy.state) ? 'bad' : 'ok'),
|
||||
textNode('span', `活动 ${proxy.activeCount || 0} · 备用 ${proxy.standbyCount || 0}/${proxy.standbyTarget || 0} · 直连 ${proxy.directCount || 0}`, 'muted')
|
||||
);
|
||||
panel.append(summary);
|
||||
for (const group of proxy.groups || []) {
|
||||
const line = element('div', 'result-line');
|
||||
line.append(
|
||||
textNode('span', `G${group.groupIndex} · ${group.accountCount || 0}号 · ${group.state || ''}`, group.state === 'direct' || group.state === 'failed' ? 'bad' : 'muted'),
|
||||
textNode('span', `${group.proxy || '-'} ${group.region ? '· ' + group.region : ''} ${group.rttMs ? '· ' + group.rttMs + 'ms' : ''} ${group.switchCount ? '· 切换 ' + group.switchCount : ''}`, 'muted')
|
||||
);
|
||||
panel.append(line);
|
||||
}
|
||||
if (proxy.lastError) panel.append(textNode('div', proxy.lastError, 'bad'));
|
||||
return panel;
|
||||
}
|
||||
|
||||
function proxyButton(item) {
|
||||
const button = element('button', '', '代理');
|
||||
button.addEventListener('click', () => {
|
||||
const key = encodeURIComponent(`${item.machineId}:${item.instanceId}`);
|
||||
const panel = machinesRoot.querySelector(`.results-details[data-proxy="${key}"]`);
|
||||
if (panel) panel.classList.toggle('open');
|
||||
});
|
||||
return button;
|
||||
}
|
||||
|
||||
function logButton(item) {
|
||||
const button = element('button', '', '日志');
|
||||
button.addEventListener('click', () => {
|
||||
|
||||
@@ -21,7 +21,7 @@ import (
|
||||
const (
|
||||
proxyFetchRounds = 3
|
||||
proxyFetchMaxRounds = 20
|
||||
proxyFetchRetryWait = 300 * time.Millisecond
|
||||
proxyFetchRetryWait = 1200 * time.Millisecond
|
||||
proxyRequestTimeout = 15 * time.Second
|
||||
proxyValidationTimeout = 3 * time.Second
|
||||
proxyValidationWorkers = 3
|
||||
@@ -38,6 +38,10 @@ type proxySettingsSnapshot struct {
|
||||
autoFetchOnTimer bool
|
||||
accountsPerProxy int
|
||||
sparePercent int
|
||||
required bool
|
||||
ready bool
|
||||
state string
|
||||
blockedReason string
|
||||
autoWhitelist bool
|
||||
whitelistReplace bool
|
||||
whitelistPassword string
|
||||
@@ -55,8 +59,14 @@ type validatedProxy struct {
|
||||
}
|
||||
|
||||
type proxyRuntimeGroup struct {
|
||||
groupIndex int
|
||||
accountNames []string
|
||||
activeURL string
|
||||
activeIP string
|
||||
activeRegion string
|
||||
activeRTT time.Duration
|
||||
switchCount int
|
||||
lastError string
|
||||
consecutiveFailures int
|
||||
failed bool
|
||||
}
|
||||
@@ -90,13 +100,14 @@ func (s *Scheduler) UpdateProxySettings(cfg types.Config) {
|
||||
autoFetch := cfg.AutoFetchProxyOnSchedule
|
||||
accountsPerProxy := cfg.AccountsPerProxy
|
||||
sparePercent := cfg.ProxySparePercent
|
||||
required := cfg.ProxyRequired
|
||||
if accountsPerProxy <= 0 {
|
||||
accountsPerProxy = constants.DefaultAccountsPerProxy
|
||||
}
|
||||
if accountsPerProxy > constants.MaxAccountsPerProxy {
|
||||
accountsPerProxy = constants.MaxAccountsPerProxy
|
||||
}
|
||||
if sparePercent <= 0 {
|
||||
if sparePercent < constants.MinProxySparePercent {
|
||||
sparePercent = constants.DefaultProxySparePercent
|
||||
}
|
||||
if sparePercent > constants.MaxProxySparePercent {
|
||||
@@ -113,6 +124,7 @@ func (s *Scheduler) UpdateProxySettings(cfg types.Config) {
|
||||
s.autoFetchProxyOnSchedule != autoFetch ||
|
||||
s.accountsPerProxy != accountsPerProxy ||
|
||||
s.proxySparePercent != sparePercent ||
|
||||
s.proxyRequired != required ||
|
||||
s.autoWhitelist != cfg.ProxyAutoWhitelist ||
|
||||
s.whitelistReplace != cfg.ProxyWhitelistReplace ||
|
||||
s.whitelistPassword != whitelistPassword ||
|
||||
@@ -123,6 +135,12 @@ func (s *Scheduler) UpdateProxySettings(cfg types.Config) {
|
||||
s.autoFetchProxyOnSchedule = autoFetch
|
||||
s.accountsPerProxy = accountsPerProxy
|
||||
s.proxySparePercent = sparePercent
|
||||
s.proxyRequired = required
|
||||
if changed {
|
||||
s.proxyReady = false
|
||||
s.proxyState = "stale"
|
||||
s.proxyBlockedReason = "代理配置已变更,等待重新准备"
|
||||
}
|
||||
s.autoWhitelist = cfg.ProxyAutoWhitelist
|
||||
s.whitelistReplace = cfg.ProxyWhitelistReplace
|
||||
s.whitelistPassword = whitelistPassword
|
||||
@@ -149,7 +167,7 @@ func (s *Scheduler) proxySettings() proxySettingsSnapshot {
|
||||
size = constants.MaxAccountsPerProxy
|
||||
}
|
||||
sparePercent := s.proxySparePercent
|
||||
if sparePercent <= 0 {
|
||||
if sparePercent < constants.MinProxySparePercent {
|
||||
sparePercent = constants.DefaultProxySparePercent
|
||||
}
|
||||
if sparePercent > constants.MaxProxySparePercent {
|
||||
@@ -162,6 +180,10 @@ func (s *Scheduler) proxySettings() proxySettingsSnapshot {
|
||||
autoFetchOnTimer: s.autoFetchProxyOnSchedule,
|
||||
accountsPerProxy: size,
|
||||
sparePercent: sparePercent,
|
||||
required: s.proxyRequired,
|
||||
ready: s.proxyReady,
|
||||
state: s.proxyState,
|
||||
blockedReason: s.proxyBlockedReason,
|
||||
autoWhitelist: s.autoWhitelist,
|
||||
whitelistReplace: s.whitelistReplace,
|
||||
whitelistPassword: s.whitelistPassword,
|
||||
@@ -180,6 +202,8 @@ func (s *Scheduler) closeAccountClients() {
|
||||
s.accountClientsDirty = true
|
||||
s.accountClientsExpireAt = time.Time{}
|
||||
s.proxyGroups = make(map[*client.DouyuClient]*proxyRuntimeGroup)
|
||||
s.proxyGroupHistory = nil
|
||||
s.accountProxyGroup = make(map[string]int)
|
||||
s.proxyStandbys = nil
|
||||
s.proxyClientPool = nil
|
||||
s.proxyExcludedURLs = make(map[string]struct{})
|
||||
@@ -211,6 +235,15 @@ func (s *Scheduler) replaceAccountClients(pool preparedProxyPool) {
|
||||
s.accountClientsDirty = false
|
||||
s.accountClientsExpireAt = pool.expiresAt
|
||||
s.proxyGroups = pool.groups
|
||||
s.accountProxyGroup = make(map[string]int)
|
||||
for _, group := range pool.groups {
|
||||
if group == nil {
|
||||
continue
|
||||
}
|
||||
for _, name := range group.accountNames {
|
||||
s.accountProxyGroup[name] = group.groupIndex
|
||||
}
|
||||
}
|
||||
s.proxyStandbys = pool.standbys
|
||||
s.proxyClientPool = pool.allClients
|
||||
s.proxyExcludedURLs = pool.excludedURLs
|
||||
@@ -222,6 +255,146 @@ func (s *Scheduler) replaceAccountClients(pool preparedProxyPool) {
|
||||
closeDouyuClientList(oldClients)
|
||||
}
|
||||
|
||||
func (s *Scheduler) replaceProxyGroupHistory(groups [][]string, activeCount int, proxies []validatedProxy) {
|
||||
history := make([]types.ProxyGroupStatus, 0, activeCount)
|
||||
for index, accounts := range groups {
|
||||
if index >= len(proxies) {
|
||||
break
|
||||
}
|
||||
proxy := proxies[index]
|
||||
history = append(history, types.ProxyGroupStatus{
|
||||
GroupIndex: index + 1,
|
||||
Role: "active",
|
||||
State: "active",
|
||||
AccountCount: len(accounts),
|
||||
Accounts: append([]string(nil), accounts...),
|
||||
Proxy: client.MaskProxyURL(proxy.url),
|
||||
Region: proxy.region,
|
||||
RTTMs: proxy.rtt.Milliseconds(),
|
||||
})
|
||||
}
|
||||
s.clientMu.Lock()
|
||||
s.proxyGroupHistory = history
|
||||
s.clientMu.Unlock()
|
||||
}
|
||||
|
||||
// ProxyStatus returns a structured status snapshot. It never includes proxy
|
||||
// credentials and is safe to expose to the desktop and remote dashboards.
|
||||
func (s *Scheduler) ProxyStatus() types.ProxyStatusView {
|
||||
cfg := s.proxySettings()
|
||||
s.clientMu.RLock()
|
||||
history := make([]types.ProxyGroupStatus, len(s.proxyGroupHistory))
|
||||
copy(history, s.proxyGroupHistory)
|
||||
for i := range history {
|
||||
history[i].Accounts = append([]string(nil), history[i].Accounts...)
|
||||
}
|
||||
accountGroups := make(map[string]int, len(s.accountProxyGroup))
|
||||
for name, index := range s.accountProxyGroup {
|
||||
accountGroups[name] = index
|
||||
}
|
||||
names := make([]string, 0, len(s.accountClients))
|
||||
for name := range s.accountClients {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
groupsByIndex := make(map[int]types.ProxyGroupStatus, len(history))
|
||||
for _, group := range history {
|
||||
groupsByIndex[group.GroupIndex] = group
|
||||
}
|
||||
accounts := make([]types.AccountProxyView, 0, len(names))
|
||||
directCount := 0
|
||||
for _, name := range names {
|
||||
groupIndex := accountGroups[name]
|
||||
view := types.AccountProxyView{
|
||||
AccountName: name,
|
||||
GroupIndex: groupIndex,
|
||||
GroupLabel: "未分配",
|
||||
Transport: "unassigned",
|
||||
}
|
||||
if cli := s.accountClients[name]; cli == s.directClient {
|
||||
view.GroupLabel = "直连"
|
||||
view.Transport = "direct"
|
||||
directCount++
|
||||
} else if groupIndex > 0 {
|
||||
view.GroupLabel = fmt.Sprintf("G%d", groupIndex)
|
||||
view.Transport = "proxy"
|
||||
if group, ok := groupsByIndex[groupIndex]; ok {
|
||||
view.Proxy = group.Proxy
|
||||
}
|
||||
}
|
||||
accounts = append(accounts, view)
|
||||
}
|
||||
standbyCount := len(s.proxyStandbys)
|
||||
s.clientMu.RUnlock()
|
||||
|
||||
activeCount := 0
|
||||
for _, group := range history {
|
||||
if group.Role == "active" {
|
||||
activeCount++
|
||||
}
|
||||
}
|
||||
standbyTarget := proxyStandbyCount(activeCount, cfg.sparePercent)
|
||||
state := cfg.state
|
||||
if state == "" {
|
||||
state = "empty"
|
||||
}
|
||||
lastError := cfg.blockedReason
|
||||
switch {
|
||||
case !cfg.enabled:
|
||||
state = "disabled"
|
||||
lastError = ""
|
||||
case directCount > 0:
|
||||
state = "direct_fallback"
|
||||
if lastError == "" {
|
||||
lastError = "账号已回退直连"
|
||||
}
|
||||
case cfg.ready:
|
||||
state = "ready"
|
||||
lastError = ""
|
||||
if reason := proxyDegradedReason(history, standbyCount, standbyTarget); reason != "" {
|
||||
state = "degraded"
|
||||
lastError = reason
|
||||
}
|
||||
case state == "preparing":
|
||||
// Keep the explicit preparing state visible while the pool is refreshed.
|
||||
case lastError != "":
|
||||
state = "failed"
|
||||
case len(history) == 0:
|
||||
state = "empty"
|
||||
default:
|
||||
state = "preparing"
|
||||
}
|
||||
return types.ProxyStatusView{
|
||||
Enabled: cfg.enabled,
|
||||
Required: cfg.required,
|
||||
State: state,
|
||||
ActiveCount: activeCount,
|
||||
StandbyCount: standbyCount,
|
||||
StandbyTarget: standbyTarget,
|
||||
DirectCount: directCount,
|
||||
LastError: lastError,
|
||||
UpdatedAt: time.Now().In(constants.CST).Format("2006-01-02 15:04:05.000"),
|
||||
Groups: history,
|
||||
Accounts: accounts,
|
||||
}
|
||||
}
|
||||
|
||||
func proxyDegradedReason(groups []types.ProxyGroupStatus, standbyCount, standbyTarget int) string {
|
||||
failed := 0
|
||||
for _, group := range groups {
|
||||
if group.State == "failed" {
|
||||
failed++
|
||||
}
|
||||
}
|
||||
if failed > 0 {
|
||||
return fmt.Sprintf("%d 个代理组失败,等待备用代理", failed)
|
||||
}
|
||||
if standbyCount < standbyTarget {
|
||||
return fmt.Sprintf("备用代理不足: %d/%d", standbyCount, standbyTarget)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func closeDouyuClients(clients map[string]*client.DouyuClient) {
|
||||
items := make([]*client.DouyuClient, 0, len(clients))
|
||||
for _, cli := range clients {
|
||||
@@ -318,6 +491,14 @@ func (s *Scheduler) allClients() []*client.DouyuClient {
|
||||
// prepareProxyGroups 将本次参与抢购的账号按固定顺序分组,并为每组准备独立客户端。
|
||||
// forceRefresh=true 用于定时任务启动前主动刷新短效代理。
|
||||
func (s *Scheduler) prepareProxyGroups(accountProducts map[string]string, forceRefresh bool) error {
|
||||
timeout := proxyImmediateTimeout
|
||||
if forceRefresh {
|
||||
timeout = proxyScheduledTimeout
|
||||
}
|
||||
return s.prepareProxyGroupsUntil(accountProducts, forceRefresh, time.Now().Add(timeout))
|
||||
}
|
||||
|
||||
func (s *Scheduler) prepareProxyGroupsUntil(accountProducts map[string]string, forceRefresh bool, deadline time.Time) error {
|
||||
s.proxyPrepareMu.Lock()
|
||||
defer s.proxyPrepareMu.Unlock()
|
||||
|
||||
@@ -328,38 +509,38 @@ func (s *Scheduler) prepareProxyGroups(accountProducts map[string]string, forceR
|
||||
}
|
||||
if !cfg.enabled {
|
||||
s.closeAccountClients()
|
||||
s.setProxyReadiness(false, "代理未启用")
|
||||
return nil
|
||||
}
|
||||
if !forceRefresh && s.proxyFallbackSnapshot() {
|
||||
if !forceRefresh && !cfg.required && s.proxyFallbackSnapshot() {
|
||||
logger.LogEvent("代理", "warn", "代理此前准备失败,本次抢购直接使用直连,避免再次等待")
|
||||
return nil
|
||||
}
|
||||
if !forceRefresh && s.accountClientsCover(names) {
|
||||
s.setProxyReadiness(true, "")
|
||||
return nil
|
||||
}
|
||||
s.setProxyPreparing("代理正在准备")
|
||||
|
||||
groups := chunkAccountNames(names, cfg.accountsPerProxy)
|
||||
activeCount := len(groups)
|
||||
standbyTarget := proxyStandbyCount(activeCount, cfg.sparePercent)
|
||||
base := s.clientSnapshot()
|
||||
if base == nil {
|
||||
s.activateProxyDirectFallback(errors.New("基础客户端未初始化"))
|
||||
return nil
|
||||
return s.handleProxyPrepareFailure(cfg, names, errors.New("基础客户端未初始化"))
|
||||
}
|
||||
|
||||
poolStartedAt := time.Now()
|
||||
timeout := proxyImmediateTimeout
|
||||
if forceRefresh {
|
||||
timeout = proxyScheduledTimeout
|
||||
timeout := time.Until(deadline)
|
||||
if timeout <= 0 {
|
||||
return s.handleProxyPrepareFailure(cfg, names, errors.New("代理准备截止时间已到"))
|
||||
}
|
||||
proxies, err := s.buildProxyPool(base, cfg, activeCount, activeCount+standbyTarget, nil, timeout)
|
||||
if err != nil {
|
||||
s.activateProxyDirectFallback(err)
|
||||
return nil
|
||||
return s.handleProxyPrepareFailure(cfg, names, err)
|
||||
}
|
||||
if len(proxies) < activeCount {
|
||||
s.activateProxyDirectFallback(fmt.Errorf("代理数量不足: 需要%d个活动代理,实际%d个", activeCount, len(proxies)))
|
||||
return nil
|
||||
closeValidatedProxies(proxies)
|
||||
return s.handleProxyPrepareFailure(cfg, names, fmt.Errorf("代理数量不足: 需要%d个活动代理,实际%d个", activeCount, len(proxies)))
|
||||
}
|
||||
if len(proxies) < activeCount+standbyTarget {
|
||||
logger.LogEvent("代理", "warn", "备用代理不足: 目标%d个,实际%d个;活动代理不受影响",
|
||||
@@ -373,8 +554,12 @@ func (s *Scheduler) prepareProxyGroups(accountProducts map[string]string, forceR
|
||||
proxy := proxies[i]
|
||||
clientOrder = append(clientOrder, proxy.client)
|
||||
runtimeGroups[proxy.client] = &proxyRuntimeGroup{
|
||||
groupIndex: i + 1,
|
||||
accountNames: append([]string(nil), group...),
|
||||
activeURL: proxy.url,
|
||||
activeIP: proxy.ip,
|
||||
activeRegion: proxy.region,
|
||||
activeRTT: proxy.rtt,
|
||||
}
|
||||
for _, name := range group {
|
||||
accountClients[name] = proxy.client
|
||||
@@ -394,6 +579,7 @@ func (s *Scheduler) prepareProxyGroups(accountProducts map[string]string, forceR
|
||||
s.recordProxyResult(proxyClient, err)
|
||||
})
|
||||
}
|
||||
poolStartedAt := time.Now()
|
||||
expiresAt := time.Time{}
|
||||
if cfg.proxyAPI != "" {
|
||||
expiresAt = proxyPoolExpiresAt(cfg.proxyAPI, poolStartedAt)
|
||||
@@ -416,11 +602,60 @@ func (s *Scheduler) prepareProxyGroups(accountProducts map[string]string, forceR
|
||||
standbyTarget: effectiveProxyStandbyTarget(cfg, standbyTarget, len(standbys)),
|
||||
expiresAt: expiresAt,
|
||||
})
|
||||
s.replaceProxyGroupHistory(groups, activeCount, proxies)
|
||||
logger.LogEvent("代理", "success", "代理池准备完成: 账号=%d,每组=%d,活动代理=%d,备用代理=%d/%d",
|
||||
len(names), cfg.accountsPerProxy, activeCount, len(standbys), standbyTarget)
|
||||
s.setProxyReadiness(true, "")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Scheduler) handleProxyPrepareFailure(cfg proxySettingsSnapshot, names []string, cause error) error {
|
||||
if cfg.required {
|
||||
s.closeAccountClients()
|
||||
s.setProxyReadiness(false, cause.Error())
|
||||
logger.LogEvent("代理", "error", "严格代理模式准备失败,本次不启动且不回退直连: %v", cause)
|
||||
return cause
|
||||
}
|
||||
s.activateProxyDirectFallback(cause)
|
||||
s.setDirectFallbackAccounts(names)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Scheduler) setDirectFallbackAccounts(names []string) {
|
||||
s.clientMu.Lock()
|
||||
if s.directClient != nil {
|
||||
s.accountClients = make(map[string]*client.DouyuClient, len(names))
|
||||
for _, name := range names {
|
||||
s.accountClients[name] = s.directClient
|
||||
}
|
||||
s.accountClientOrder = []*client.DouyuClient{s.directClient}
|
||||
s.accountClientsDirty = false
|
||||
}
|
||||
s.clientMu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Scheduler) setProxyReadiness(ready bool, reason string) {
|
||||
s.proxyMu.Lock()
|
||||
s.proxyReady = ready
|
||||
if ready {
|
||||
s.proxyState = "ready"
|
||||
} else if reason != "" {
|
||||
s.proxyState = "failed"
|
||||
} else {
|
||||
s.proxyState = "empty"
|
||||
}
|
||||
s.proxyBlockedReason = reason
|
||||
s.proxyMu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Scheduler) setProxyPreparing(reason string) {
|
||||
s.proxyMu.Lock()
|
||||
s.proxyReady = false
|
||||
s.proxyState = "preparing"
|
||||
s.proxyBlockedReason = reason
|
||||
s.proxyMu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Scheduler) proxyFallbackSnapshot() bool {
|
||||
s.clientMu.RLock()
|
||||
defer s.clientMu.RUnlock()
|
||||
@@ -434,9 +669,11 @@ func (s *Scheduler) activateProxyDirectFallback(cause error) {
|
||||
s.proxyFallbackActive = active
|
||||
s.clientMu.Unlock()
|
||||
if active {
|
||||
s.setProxyReadiness(false, cause.Error())
|
||||
logger.LogEvent("代理", "warn", "代理准备失败,已回退直连以保证抢购继续: %v", cause)
|
||||
return
|
||||
}
|
||||
s.setProxyReadiness(false, cause.Error())
|
||||
logger.LogEvent("代理", "error", "代理准备失败且直连客户端不可用: %v", cause)
|
||||
}
|
||||
|
||||
@@ -452,6 +689,10 @@ func (s *Scheduler) PrepareProxyGroups(accountProducts map[string]string) error
|
||||
return s.prepareProxyGroups(accountProducts, false)
|
||||
}
|
||||
|
||||
func (s *Scheduler) PrepareProxyGroupsUntil(accountProducts map[string]string, deadline time.Time) error {
|
||||
return s.prepareProxyGroupsUntil(accountProducts, true, deadline)
|
||||
}
|
||||
|
||||
func selectedAccountNames(accountProducts map[string]string) []string {
|
||||
names := make([]string, 0, len(accountProducts))
|
||||
for name, productID := range accountProducts {
|
||||
@@ -487,7 +728,7 @@ func proxyStandbyCount(activeCount, sparePercent int) int {
|
||||
if activeCount <= 0 {
|
||||
return 0
|
||||
}
|
||||
if sparePercent <= 0 {
|
||||
if sparePercent < constants.MinProxySparePercent {
|
||||
sparePercent = constants.DefaultProxySparePercent
|
||||
}
|
||||
if sparePercent > constants.MaxProxySparePercent {
|
||||
@@ -726,6 +967,7 @@ func (s *Scheduler) recordProxyResult(proxyClient *client.DouyuClient, err error
|
||||
if proxyClient == nil {
|
||||
return
|
||||
}
|
||||
cfg := s.proxySettings()
|
||||
|
||||
s.clientMu.Lock()
|
||||
group := s.proxyGroups[proxyClient]
|
||||
@@ -735,6 +977,8 @@ func (s *Scheduler) recordProxyResult(proxyClient *client.DouyuClient, err error
|
||||
}
|
||||
if err == nil {
|
||||
group.consecutiveFailures = 0
|
||||
group.lastError = ""
|
||||
s.updateProxyGroupHistoryLocked(group)
|
||||
s.clientMu.Unlock()
|
||||
return
|
||||
}
|
||||
@@ -744,6 +988,8 @@ func (s *Scheduler) recordProxyResult(proxyClient *client.DouyuClient, err error
|
||||
}
|
||||
|
||||
group.consecutiveFailures++
|
||||
group.lastError = err.Error()
|
||||
s.updateProxyGroupHistoryLocked(group)
|
||||
if group.consecutiveFailures < 2 {
|
||||
failures := group.consecutiveFailures
|
||||
activeURL := group.activeURL
|
||||
@@ -753,7 +999,9 @@ func (s *Scheduler) recordProxyResult(proxyClient *client.DouyuClient, err error
|
||||
}
|
||||
|
||||
event, expiredClients, switched := s.switchGroupToStandbyLocked(proxyClient, group)
|
||||
if !switched && s.directClient != nil {
|
||||
if !switched && cfg.required {
|
||||
group.failed = true
|
||||
} else if !switched && s.directClient != nil {
|
||||
event = s.switchGroupToDirectLocked(proxyClient, group)
|
||||
switched = true
|
||||
}
|
||||
@@ -767,6 +1015,7 @@ func (s *Scheduler) recordProxyResult(proxyClient *client.DouyuClient, err error
|
||||
failedAccounts = len(group.accountNames)
|
||||
failedURL = group.activeURL
|
||||
needRefill = !s.proxyRefillInFlight
|
||||
s.updateProxyGroupHistoryLocked(group)
|
||||
}
|
||||
s.clientMu.Unlock()
|
||||
|
||||
@@ -774,8 +1023,13 @@ func (s *Scheduler) recordProxyResult(proxyClient *client.DouyuClient, err error
|
||||
if switched {
|
||||
logProxySwitch("代理故障切换", event)
|
||||
} else {
|
||||
logger.LogEvent("代理", "warn", "代理组连续失败且暂无备用: 账号=%d 出口=%s,正在尝试补提取",
|
||||
failedAccounts, client.MaskProxyURL(failedURL))
|
||||
if cfg.required {
|
||||
logger.LogEvent("代理", "warn", "严格代理模式下代理组连续失败且暂无备用,暂不直连并等待补提取: 账号=%d 出口=%s",
|
||||
failedAccounts, client.MaskProxyURL(failedURL))
|
||||
} else {
|
||||
logger.LogEvent("代理", "warn", "代理组连续失败且暂无备用: 账号=%d 出口=%s,正在尝试补提取",
|
||||
failedAccounts, client.MaskProxyURL(failedURL))
|
||||
}
|
||||
}
|
||||
if needRefill {
|
||||
s.startProxyRefill()
|
||||
@@ -802,7 +1056,12 @@ func (s *Scheduler) switchGroupToStandbyLocked(oldClient *client.DouyuClient, gr
|
||||
toSource: standby.source,
|
||||
}
|
||||
group.activeURL = standby.url
|
||||
group.activeIP = standby.ip
|
||||
group.activeRegion = standby.region
|
||||
group.activeRTT = standby.rtt
|
||||
group.switchCount++
|
||||
group.consecutiveFailures = 0
|
||||
group.lastError = ""
|
||||
group.failed = false
|
||||
for _, accountName := range group.accountNames {
|
||||
s.accountClients[accountName] = standby.client
|
||||
@@ -810,6 +1069,7 @@ func (s *Scheduler) switchGroupToStandbyLocked(oldClient *client.DouyuClient, gr
|
||||
delete(s.proxyGroups, oldClient)
|
||||
s.proxyGroups[standby.client] = group
|
||||
replaceClientInOrder(s.accountClientOrder, oldClient, standby.client)
|
||||
s.updateProxyGroupHistoryLocked(group)
|
||||
return event, expiredClients, true
|
||||
}
|
||||
return proxySwitchEvent{}, expiredClients, false
|
||||
@@ -821,16 +1081,52 @@ func (s *Scheduler) switchGroupToDirectLocked(oldClient *client.DouyuClient, gro
|
||||
fromURL: group.activeURL,
|
||||
}
|
||||
group.activeURL = ""
|
||||
group.activeIP = ""
|
||||
group.activeRegion = ""
|
||||
group.activeRTT = 0
|
||||
group.switchCount++
|
||||
group.consecutiveFailures = 0
|
||||
group.lastError = ""
|
||||
group.failed = false
|
||||
for _, accountName := range group.accountNames {
|
||||
s.accountClients[accountName] = s.directClient
|
||||
}
|
||||
delete(s.proxyGroups, oldClient)
|
||||
replaceClientInOrder(s.accountClientOrder, oldClient, s.directClient)
|
||||
s.updateProxyGroupHistoryLocked(group)
|
||||
return event
|
||||
}
|
||||
|
||||
func (s *Scheduler) updateProxyGroupHistoryLocked(group *proxyRuntimeGroup) {
|
||||
if group == nil || group.groupIndex <= 0 {
|
||||
return
|
||||
}
|
||||
for i := range s.proxyGroupHistory {
|
||||
item := &s.proxyGroupHistory[i]
|
||||
if item.GroupIndex != group.groupIndex {
|
||||
continue
|
||||
}
|
||||
item.AccountCount = len(group.accountNames)
|
||||
item.Accounts = append([]string(nil), group.accountNames...)
|
||||
item.SwitchCount = group.switchCount
|
||||
item.LastError = group.lastError
|
||||
switch {
|
||||
case group.activeURL == "":
|
||||
item.State = "direct"
|
||||
item.Proxy = ""
|
||||
item.Region = ""
|
||||
case group.failed:
|
||||
item.State = "failed"
|
||||
default:
|
||||
item.State = "active"
|
||||
item.Proxy = client.MaskProxyURL(group.activeURL)
|
||||
item.Region = group.activeRegion
|
||||
item.RTTMs = group.activeRTT.Milliseconds()
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func replaceClientInOrder(order []*client.DouyuClient, oldClient, newClient *client.DouyuClient) {
|
||||
for i, cli := range order {
|
||||
if cli == oldClient {
|
||||
|
||||
@@ -47,10 +47,10 @@ func TestProxyStandbyCount(t *testing.T) {
|
||||
percent int
|
||||
want int
|
||||
}{
|
||||
{active: 1, percent: 25, want: 1},
|
||||
{active: 6, percent: 25, want: 2},
|
||||
{active: 10, percent: 25, want: 3},
|
||||
{active: 6, percent: 1, want: 1},
|
||||
{active: 1, percent: 50, want: 1},
|
||||
{active: 6, percent: 50, want: 3},
|
||||
{active: 10, percent: 50, want: 5},
|
||||
{active: 6, percent: 1, want: 3},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := proxyStandbyCount(tc.active, tc.percent); got != tc.want {
|
||||
@@ -59,6 +59,26 @@ func TestProxyStandbyCount(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldPrepareProxyOnSchedule(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
cfg proxySettingsSnapshot
|
||||
want bool
|
||||
}{
|
||||
{name: "disabled", cfg: proxySettingsSnapshot{required: true}, want: false},
|
||||
{name: "strict static", cfg: proxySettingsSnapshot{enabled: true, required: true}, want: true},
|
||||
{name: "auto fetch api", cfg: proxySettingsSnapshot{enabled: true, autoFetchOnTimer: true, proxyAPI: "https://example.test"}, want: true},
|
||||
{name: "optional api without auto fetch", cfg: proxySettingsSnapshot{enabled: true, proxyAPI: "https://example.test"}, want: false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := shouldPrepareProxyOnSchedule(tc.cfg); got != tc.want {
|
||||
t.Fatalf("shouldPrepareProxyOnSchedule()=%v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountClientsRespectExpiryAndSettingsRevision(t *testing.T) {
|
||||
scheduler := &Scheduler{
|
||||
client: &client.DouyuClient{},
|
||||
@@ -105,7 +125,7 @@ func TestPrepareProxyGroupsRoutesAccountsToDistinctClients(t *testing.T) {
|
||||
ProxyAPI: apiServer.URL,
|
||||
AutoFetchProxyOnSchedule: true,
|
||||
AccountsPerProxy: 2,
|
||||
ProxySparePercent: 25,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
|
||||
accountProducts := make(map[string]string)
|
||||
@@ -128,8 +148,8 @@ func TestPrepareProxyGroupsRoutesAccountsToDistinctClients(t *testing.T) {
|
||||
if scheduler.clientForAccount("e") == scheduler.clientForAccount("a") {
|
||||
t.Fatal("e 应使用独立代理客户端")
|
||||
}
|
||||
if len(scheduler.proxyStandbys) != 1 {
|
||||
t.Fatalf("备用代理=%d, want 1", len(scheduler.proxyStandbys))
|
||||
if len(scheduler.proxyStandbys) != 2 {
|
||||
t.Fatalf("备用代理=%d, want 2", len(scheduler.proxyStandbys))
|
||||
}
|
||||
|
||||
standbyClient := scheduler.proxyStandbys[0].client
|
||||
@@ -137,7 +157,7 @@ func TestPrepareProxyGroupsRoutesAccountsToDistinctClients(t *testing.T) {
|
||||
ProxyEnabled: true,
|
||||
ProxyURL: "http://127.0.0.1:1",
|
||||
AccountsPerProxy: 2,
|
||||
ProxySparePercent: 25,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
proxyErr := errors.New("read tcp: connection reset by peer")
|
||||
scheduler.recordProxyResult(scheduler.clientForAccount("a"), proxyErr)
|
||||
@@ -169,7 +189,7 @@ func TestPrepareProxyGroupsFallsBackDirectAndSkipsRepeatedWait(t *testing.T) {
|
||||
ProxyEnabled: true,
|
||||
ProxyAPI: apiServer.URL,
|
||||
AccountsPerProxy: 5,
|
||||
ProxySparePercent: 25,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
accountProducts := map[string]string{"a": "goods-1"}
|
||||
|
||||
@@ -218,6 +238,123 @@ func TestProxyGroupSwitchesDirectWhenStandbyUnavailable(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrictProxyModeDoesNotSwitchDirect(t *testing.T) {
|
||||
act := *activity.Get("peace")
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
AccountsPerProxy: 5,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
groupClient := &client.DouyuClient{}
|
||||
group := &proxyRuntimeGroup{
|
||||
groupIndex: 1,
|
||||
accountNames: []string{"a"},
|
||||
activeURL: "http://user:pass@127.0.0.1:9000",
|
||||
}
|
||||
scheduler.replaceAccountClients(preparedProxyPool{
|
||||
accountClients: map[string]*client.DouyuClient{"a": groupClient},
|
||||
activeClients: []*client.DouyuClient{groupClient},
|
||||
groups: map[*client.DouyuClient]*proxyRuntimeGroup{groupClient: group},
|
||||
allClients: []*client.DouyuClient{groupClient},
|
||||
})
|
||||
scheduler.replaceProxyGroupHistory([][]string{{"a"}}, 1, []validatedProxy{{
|
||||
url: group.activeURL,
|
||||
client: groupClient,
|
||||
}})
|
||||
|
||||
proxyErr := errors.New("read tcp: connection reset by peer")
|
||||
scheduler.recordProxyResult(groupClient, proxyErr)
|
||||
scheduler.recordProxyResult(groupClient, proxyErr)
|
||||
if scheduler.clientForAccount("a") == scheduler.directClient {
|
||||
t.Fatal("strict proxy mode must not switch to direct")
|
||||
}
|
||||
if !group.failed {
|
||||
t.Fatal("failed group should remain tracked for standby refill")
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyStatusReportsGroupAndAccountMapping(t *testing.T) {
|
||||
act := *activity.Get("peace")
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
AccountsPerProxy: 2,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
groupClient := &client.DouyuClient{}
|
||||
group := &proxyRuntimeGroup{
|
||||
groupIndex: 1,
|
||||
accountNames: []string{"a", "b"},
|
||||
activeURL: "http://user:pass@127.0.0.1:9000",
|
||||
activeIP: "127.0.0.1",
|
||||
activeRegion: "test",
|
||||
activeRTT: 45 * time.Millisecond,
|
||||
}
|
||||
standbyClient := &client.DouyuClient{}
|
||||
scheduler.replaceAccountClients(preparedProxyPool{
|
||||
accountClients: map[string]*client.DouyuClient{"a": groupClient, "b": groupClient},
|
||||
activeClients: []*client.DouyuClient{groupClient},
|
||||
groups: map[*client.DouyuClient]*proxyRuntimeGroup{groupClient: group},
|
||||
standbys: []validatedProxy{{client: standbyClient}},
|
||||
allClients: []*client.DouyuClient{groupClient, standbyClient},
|
||||
standbyTarget: 1,
|
||||
})
|
||||
scheduler.replaceProxyGroupHistory([][]string{{"a", "b"}}, 1, []validatedProxy{{
|
||||
url: group.activeURL,
|
||||
client: groupClient,
|
||||
ip: group.activeIP,
|
||||
region: group.activeRegion,
|
||||
rtt: group.activeRTT,
|
||||
}})
|
||||
scheduler.setProxyReadiness(true, "")
|
||||
|
||||
status := scheduler.ProxyStatus()
|
||||
if status.State != "ready" || status.ActiveCount != 1 || len(status.Groups) != 1 {
|
||||
t.Fatalf("unexpected status: %+v", status)
|
||||
}
|
||||
if len(status.Accounts) != 2 || status.Accounts[0].GroupLabel != "G1" || status.Accounts[0].Transport != "proxy" {
|
||||
t.Fatalf("unexpected account mapping: %+v", status.Accounts)
|
||||
}
|
||||
if status.Groups[0].Proxy == "" || status.Groups[0].Proxy == group.activeURL {
|
||||
t.Fatalf("proxy URL should be masked, got %q", status.Groups[0].Proxy)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrictPrepareFailureBlocksRun(t *testing.T) {
|
||||
apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, `{"code":-1,"status":200,"message":"secret密匙错误"}`)
|
||||
}))
|
||||
defer apiServer.Close()
|
||||
act := *activity.Get("peace")
|
||||
base := client.New(&act, "http://douyu.test/time", &trace.Recorder{}, "")
|
||||
scheduler := New(base, types.AppPaths{}, nil, 8, 1, nil, types.SeckillModeAggressive)
|
||||
scheduler.UpdateProxySettings(types.Config{
|
||||
ProxyEnabled: true,
|
||||
ProxyRequired: true,
|
||||
ProxyAPI: apiServer.URL,
|
||||
AccountsPerProxy: 5,
|
||||
ProxySparePercent: 50,
|
||||
})
|
||||
err := scheduler.prepareProxyGroupsUntil(map[string]string{"a": "goods-1"}, false, time.Now().Add(2*time.Second))
|
||||
if err == nil {
|
||||
t.Fatal("strict proxy preparation should fail")
|
||||
}
|
||||
if scheduler.proxyFallbackSnapshot() {
|
||||
t.Fatal("strict mode must not activate direct fallback")
|
||||
}
|
||||
if ok, _ := scheduler.ProxyRunGate(); ok {
|
||||
t.Fatal("strict mode run gate should remain closed")
|
||||
}
|
||||
if status := scheduler.ProxyStatus(); status.State != "failed" {
|
||||
t.Fatalf("proxy status=%q, want failed", status.State)
|
||||
}
|
||||
}
|
||||
|
||||
func newTimeProxyServer(t *testing.T) *httptest.Server {
|
||||
t.Helper()
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -48,7 +48,7 @@ func (s *Scheduler) ScheduleRun(req types.SeckillRequest) (types.ScheduleResult,
|
||||
|
||||
proxyCfg := s.proxySettings()
|
||||
|
||||
if proxyCfg.enabled && proxyCfg.autoFetchOnTimer && proxyCfg.proxyAPI != "" {
|
||||
if shouldPrepareProxyOnSchedule(proxyCfg) {
|
||||
proxyFetchRemain := triggerTime.Sub(serverTime) - proxyFetchBeforeTrigger
|
||||
if proxyFetchRemain < 0 {
|
||||
proxyFetchRemain = 0
|
||||
@@ -58,8 +58,10 @@ func (s *Scheduler) ScheduleRun(req types.SeckillRequest) (types.ScheduleResult,
|
||||
if s.IsRunning() {
|
||||
return
|
||||
}
|
||||
logger.LogEvent("代理", "info", "定时任务触发前,自动提取并校验分组代理...")
|
||||
if err := s.prepareProxyGroups(ap, true); err != nil {
|
||||
deadline := triggerTime.Add(-proxyReadyDeadline)
|
||||
logger.LogEvent("代理", "info", "定时任务触发前 2 分钟,开始提取并校验分组代理,截止=%s",
|
||||
deadline.In(constants.CST).Format("15:04:05.000"))
|
||||
if err := s.prepareProxyGroupsUntil(ap, true, deadline); err != nil {
|
||||
logger.LogEvent("代理", "error", "定时分组代理准备失败: %v", err)
|
||||
return
|
||||
}
|
||||
@@ -120,6 +122,13 @@ func (s *Scheduler) ScheduleRun(req types.SeckillRequest) (types.ScheduleResult,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func shouldPrepareProxyOnSchedule(cfg proxySettingsSnapshot) bool {
|
||||
if !cfg.enabled {
|
||||
return false
|
||||
}
|
||||
return cfg.required || (cfg.autoFetchOnTimer && cfg.proxyAPI != "")
|
||||
}
|
||||
|
||||
func (s *Scheduler) scheduleTimers(accountProducts map[string]string, targetTime, triggerTime time.Time, calibration types.TimeCalibration, attempts, delayMs int, resync bool) {
|
||||
serverTime := calibration.EstimatedServerNow()
|
||||
remain := triggerTime.Sub(serverTime)
|
||||
@@ -154,6 +163,10 @@ func (s *Scheduler) scheduleTimers(accountProducts map[string]string, targetTime
|
||||
estimatedServer.In(constants.CST).Format("15:04:05.000"),
|
||||
calibration.Offset.Milliseconds(),
|
||||
calibration.ErrorBound.Milliseconds())
|
||||
if ok, reason := s.ProxyRunGate(); !ok {
|
||||
logger.LogEvent("代理", "error", "严格代理模式未就绪,本次定时抢购不启动: %s", reason)
|
||||
return
|
||||
}
|
||||
if !s.TryStartRun() {
|
||||
logger.LogEvent("抢购", "warn", "已有抢购任务正在运行,本次定时触发已跳过")
|
||||
return
|
||||
|
||||
@@ -28,7 +28,8 @@ const (
|
||||
timeCalibrateRounds = 3
|
||||
timeCalibrateWait = 1200 * time.Millisecond
|
||||
resyncBeforeTrigger = 8 * time.Second
|
||||
proxyFetchBeforeTrigger = 30 * time.Second
|
||||
proxyFetchBeforeTrigger = 120 * time.Second
|
||||
proxyReadyDeadline = 15 * time.Second
|
||||
calibrationHistorySize = 3
|
||||
maxCooldownCount = 5
|
||||
xiaodianDefaultDelay = 500 * time.Millisecond
|
||||
@@ -68,6 +69,8 @@ type Scheduler struct {
|
||||
accountClientsDirty bool
|
||||
accountClientsExpireAt time.Time
|
||||
proxyGroups map[*client.DouyuClient]*proxyRuntimeGroup
|
||||
proxyGroupHistory []types.ProxyGroupStatus
|
||||
accountProxyGroup map[string]int
|
||||
proxyStandbys []validatedProxy
|
||||
proxyClientPool []*client.DouyuClient
|
||||
proxyExcludedURLs map[string]struct{}
|
||||
@@ -120,6 +123,10 @@ type Scheduler struct {
|
||||
autoFetchProxyOnSchedule bool
|
||||
accountsPerProxy int
|
||||
proxySparePercent int
|
||||
proxyRequired bool
|
||||
proxyReady bool
|
||||
proxyState string
|
||||
proxyBlockedReason string
|
||||
autoWhitelist bool
|
||||
whitelistReplace bool
|
||||
whitelistPassword string
|
||||
@@ -465,6 +472,23 @@ func (s *Scheduler) IsRunning() bool {
|
||||
return s.running
|
||||
}
|
||||
|
||||
// ProxyRunGate reports whether a run may start under strict proxy mode.
|
||||
func (s *Scheduler) ProxyRunGate() (bool, string) {
|
||||
s.proxyMu.RLock()
|
||||
defer s.proxyMu.RUnlock()
|
||||
if !s.proxyEnabled || !s.proxyRequired {
|
||||
return true, ""
|
||||
}
|
||||
if s.proxyReady {
|
||||
return true, ""
|
||||
}
|
||||
reason := s.proxyBlockedReason
|
||||
if reason == "" {
|
||||
reason = "代理尚未就绪"
|
||||
}
|
||||
return false, reason
|
||||
}
|
||||
|
||||
func (s *Scheduler) stopRunChan() <-chan struct{} {
|
||||
s.runMu.Lock()
|
||||
defer s.runMu.Unlock()
|
||||
@@ -519,6 +543,10 @@ func (s *Scheduler) RunNow(accountProducts map[string]string) {
|
||||
logger.LogEvent("代理", "error", "分组代理准备失败,本次抢购不执行: %v", err)
|
||||
return
|
||||
}
|
||||
if ok, reason := s.ProxyRunGate(); !ok {
|
||||
logger.LogEvent("代理", "error", "严格代理模式未就绪,本次抢购不执行: %s", reason)
|
||||
return
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
deadline := start.Add(seckillMaxDuration)
|
||||
@@ -1194,6 +1222,10 @@ func (s *Scheduler) RunXiaodian(accountProducts map[string]string, attempts, del
|
||||
logger.LogEvent("代理", "error", "分组代理准备失败,本次和平小店抢购不执行: %v", err)
|
||||
return
|
||||
}
|
||||
if ok, reason := s.ProxyRunGate(); !ok {
|
||||
logger.LogEvent("代理", "error", "严格代理模式未就绪,本次和平小店抢购不执行: %s", reason)
|
||||
return
|
||||
}
|
||||
s.startXiaodianReport(accounts, products, reportSessionID)
|
||||
|
||||
stoppedAccounts := make(map[string]string)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package types
|
||||
|
||||
// ProxyStatusView is the structured proxy state exposed to the desktop and
|
||||
// remote dashboards. Proxy credentials must never be included.
|
||||
type ProxyStatusView struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Required bool `json:"required"`
|
||||
State string `json:"state"`
|
||||
ActiveCount int `json:"activeCount"`
|
||||
StandbyCount int `json:"standbyCount"`
|
||||
StandbyTarget int `json:"standbyTarget"`
|
||||
DirectCount int `json:"directCount"`
|
||||
LastError string `json:"lastError,omitempty"`
|
||||
UpdatedAt string `json:"updatedAt"`
|
||||
Groups []ProxyGroupStatus `json:"groups"`
|
||||
Accounts []AccountProxyView `json:"accounts"`
|
||||
}
|
||||
|
||||
// ProxyGroupStatus describes one active or standby proxy group.
|
||||
type ProxyGroupStatus struct {
|
||||
GroupIndex int `json:"groupIndex"`
|
||||
Role string `json:"role"`
|
||||
State string `json:"state"`
|
||||
AccountCount int `json:"accountCount"`
|
||||
Accounts []string `json:"accounts,omitempty"`
|
||||
Proxy string `json:"proxy,omitempty"`
|
||||
Region string `json:"region,omitempty"`
|
||||
RTTMs int64 `json:"rttMs,omitempty"`
|
||||
SwitchCount int `json:"switchCount"`
|
||||
LastError string `json:"lastError,omitempty"`
|
||||
}
|
||||
|
||||
// AccountProxyView maps one account to its current proxy group or direct route.
|
||||
type AccountProxyView struct {
|
||||
AccountName string `json:"accountName"`
|
||||
GroupIndex int `json:"groupIndex,omitempty"`
|
||||
GroupLabel string `json:"groupLabel"`
|
||||
Transport string `json:"transport"`
|
||||
Proxy string `json:"proxy,omitempty"`
|
||||
}
|
||||
@@ -12,6 +12,7 @@ type SharedProxyConfig struct {
|
||||
// SharedProxySettings 使用指针区分“未提供”与显式零值,支持部分配置覆盖。
|
||||
type SharedProxySettings struct {
|
||||
Enabled *bool `json:"enabled,omitempty"`
|
||||
Required *bool `json:"required,omitempty"`
|
||||
API *string `json:"api,omitempty"`
|
||||
AccountsPerProxy *int `json:"accountsPerProxy,omitempty"`
|
||||
SparePercent *int `json:"sparePercent,omitempty"`
|
||||
@@ -25,10 +26,11 @@ type SharedProxySettings struct {
|
||||
// SharedRemoteSettings 是本机或跨电脑共享的远端控制中心配置。
|
||||
// 令牌会写入共享 JSON 文件,调用方必须限制该文件权限并避免外部泄露。
|
||||
type SharedRemoteSettings struct {
|
||||
Enabled *bool `json:"enabled,omitempty"`
|
||||
URL *string `json:"url,omitempty"`
|
||||
Token *string `json:"token,omitempty"`
|
||||
MachineName *string `json:"machineName,omitempty"`
|
||||
Enabled *bool `json:"enabled,omitempty"`
|
||||
URL *string `json:"url,omitempty"`
|
||||
EnrollmentToken *string `json:"enrollmentToken,omitempty"`
|
||||
Token *string `json:"token,omitempty"`
|
||||
MachineName *string `json:"machineName,omitempty"`
|
||||
}
|
||||
|
||||
// FileDialogResult 返回通过系统文件选择器选中的路径。
|
||||
|
||||
@@ -18,20 +18,22 @@ type Config struct {
|
||||
SeckillMode SeckillMode `json:"seckillMode"` // 抢购模式:0=标准, 1=激进
|
||||
XiaodianPageSize int `json:"xiaodianPageSize"`
|
||||
ProxyEnabled bool `json:"proxyEnabled"` // 是否启用代理
|
||||
ProxyRequired bool `json:"proxyRequired"` // 严格代理模式:代理未就绪时不启动且不回退直连
|
||||
ProxyURL string `json:"proxyUrl"` // 代理地址,支持 http://, https://, socks5://
|
||||
ProxyAPI string `json:"proxyApi"` // 代理提取 API 地址,支持品赞与通用格式
|
||||
SharedProxyConfigFile string `json:"sharedProxyConfigFile"` // 本机保存的共享代理配置路径
|
||||
ProxyExpiresAt int64 `json:"proxyExpiresAt,omitempty"` // 当前静态代理的过期时间(Unix 毫秒),0 表示未知
|
||||
AutoFetchProxyOnSchedule bool `json:"autoFetchProxyOnSchedule"` // 定时抢购触发前自动从 API 提取最新代理
|
||||
AccountsPerProxy int `json:"accountsPerProxy"` // 每个独立代理承载的账号数
|
||||
ProxySparePercent int `json:"proxySparePercent"` // 额外提取的备用代理比例,至少预留一个
|
||||
ProxySparePercent int `json:"proxySparePercent"` // 额外提取的备用代理比例,最低和默认均为 50%
|
||||
ProxyAutoWhitelist bool `json:"proxyAutoWhitelist"` // 提取前自动添加本机公网 IP 白名单
|
||||
ProxyWhitelistReplace bool `json:"proxyWhitelistReplace"` // 白名单已满时替换最早 IP
|
||||
ProxyWhitelistPassword string `json:"proxyWhitelistPassword"` // 白名单签名使用的登录密码
|
||||
ProxyWhitelistSignKey string `json:"proxyWhitelistSignKey"` // 白名单签名使用的 AES 密钥
|
||||
RemoteEnabled bool `json:"remoteEnabled"` // 是否上报远端控制中心
|
||||
RemoteURL string `json:"remoteUrl"` // 远端控制中心根地址
|
||||
RemoteToken string `json:"remoteToken"` // 实例连接远端使用的令牌
|
||||
RemoteEnrollmentToken string `json:"remoteEnrollmentToken"` // 首次注册使用的控制中心注册令牌
|
||||
RemoteToken string `json:"remoteToken"` // 兼容旧版手工实例令牌,通常留空
|
||||
RemoteMachineName string `json:"remoteMachineName"` // 远端展示的机器名称,留空使用主机名
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user