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)
|
||||
|
||||
Reference in New Issue
Block a user