- 增加严格代理、两分钟预取、白名单去重与分组状态展示\n- 增加远端注册令牌和安全地址校验\n- 增加 Docker Compose、Caddy 示例与控制中心部署文档
116 lines
3.5 KiB
Go
116 lines
3.5 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"douyu_go/internal/types"
|
|
)
|
|
|
|
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: 50,
|
|
AutoFetchProxyOnSchedule: true,
|
|
ProxyAutoWhitelist: true,
|
|
ProxyWhitelistReplace: true,
|
|
ProxyWhitelistPassword: "login-pass",
|
|
ProxyWhitelistSignKey: "0123456789abcdef",
|
|
RemoteEnabled: true,
|
|
RemoteURL: "https://control.example.com",
|
|
RemoteEnrollmentToken: "enroll-token",
|
|
RemoteToken: "remote-token",
|
|
RemoteMachineName: "PC-01",
|
|
}
|
|
if err := SaveSharedProxyConfig(path, cfg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(data), "proxyUrl") || strings.Contains(string(data), "127.0.0.1:9000") {
|
|
t.Fatalf("shared config leaked runtime proxy URL: %s", data)
|
|
}
|
|
|
|
shared, err := LoadSharedProxyConfig(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
local := types.Config{
|
|
ProxyURL: "http://runtime-only:8000",
|
|
}
|
|
ApplySharedProxyConfig(&local, shared)
|
|
if local.ProxyURL != "http://runtime-only:8000" {
|
|
t.Fatalf("ProxyURL=%q, runtime URL should remain local", local.ProxyURL)
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestApplySharedProxyConfigSupportsPartialOverrides(t *testing.T) {
|
|
enabled := false
|
|
shared := types.SharedProxyConfig{
|
|
SchemaVersion: SharedProxyConfigSchemaVersion,
|
|
Proxy: types.SharedProxySettings{
|
|
Enabled: &enabled,
|
|
},
|
|
}
|
|
cfg := types.Config{
|
|
ProxyEnabled: true,
|
|
ProxyAPI: "keep-api",
|
|
AccountsPerProxy: 7,
|
|
}
|
|
ApplySharedProxyConfig(&cfg, shared)
|
|
if cfg.ProxyEnabled {
|
|
t.Fatal("shared enabled=false should override local value")
|
|
}
|
|
if cfg.ProxyAPI != "keep-api" || cfg.AccountsPerProxy != 7 {
|
|
t.Fatalf("unspecified fields should remain unchanged: %+v", cfg)
|
|
}
|
|
}
|
|
|
|
func TestLoadSharedProxyConfigRejectsUnsupportedVersion(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "shared-proxy.json")
|
|
if err := os.WriteFile(path, []byte(`{"schemaVersion":2,"proxy":{"enabled":true}}`), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := LoadSharedProxyConfig(path); err == nil {
|
|
t.Fatal("expected unsupported schema error")
|
|
}
|
|
}
|
|
|
|
func TestSharedProxyConfigPointerIsSharedAcrossInstances(t *testing.T) {
|
|
t.Setenv("DOUYU_DATA_DIR", t.TempDir())
|
|
want := filepath.Join(t.TempDir(), "common-proxy.json")
|
|
if err := SaveSharedProxyConfigPointer(want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := LoadSharedProxyConfigPointer()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got != want {
|
|
t.Fatalf("LoadSharedProxyConfigPointer()=%q, want %q", got, want)
|
|
}
|
|
}
|