feat: 优化代理准备并完善远端部署

- 增加严格代理、两分钟预取、白名单去重与分组状态展示\n- 增加远端注册令牌和安全地址校验\n- 增加 Docker Compose、Caddy 示例与控制中心部署文档
This commit is contained in:
yml
2026-11-21 20:59:35 +08:00
parent d176c421a8
commit 9c83be798d
43 changed files with 2755 additions and 119 deletions
+146 -9
View File
@@ -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) {