feat: 优化代理准备并完善远端部署
- 增加严格代理、两分钟预取、白名单去重与分组状态展示\n- 增加远端注册令牌和安全地址校验\n- 增加 Docker Compose、Caddy 示例与控制中心部署文档
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user