feat(config): 支持共享代理配置文件,发布 0.5.2
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
const SharedProxyConfigSchemaVersion = 1
|
||||
const sharedProxyConfigPointerFile = "shared-proxy-config-path.txt"
|
||||
|
||||
// DefaultSharedProxyConfigPointerPath 返回所有本机实例共享的配置路径指针。
|
||||
func DefaultSharedProxyConfigPointerPath() string {
|
||||
return filepath.Join(defaultDataDir(), sharedProxyConfigPointerFile)
|
||||
}
|
||||
|
||||
// LoadSharedProxyConfigPointer 读取本机默认共享配置路径。
|
||||
func LoadSharedProxyConfigPointer() (string, error) {
|
||||
data, err := os.ReadFile(DefaultSharedProxyConfigPointerPath())
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return "", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strings.TrimSpace(string(data)), nil
|
||||
}
|
||||
|
||||
// SaveSharedProxyConfigPointer 保存本机默认共享配置路径,供新实例自动继承。
|
||||
func SaveSharedProxyConfigPointer(path string) error {
|
||||
path = strings.TrimSpace(path)
|
||||
if path == "" {
|
||||
return nil
|
||||
}
|
||||
pointerPath := DefaultSharedProxyConfigPointerPath()
|
||||
if err := os.MkdirAll(filepath.Dir(pointerPath), 0700); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(pointerPath, []byte(path), 0600)
|
||||
}
|
||||
|
||||
// LoadSharedProxyConfig 读取并校验共享代理配置。
|
||||
func LoadSharedProxyConfig(path string) (types.SharedProxyConfig, error) {
|
||||
path = strings.TrimSpace(path)
|
||||
if path == "" {
|
||||
return types.SharedProxyConfig{}, errors.New("共享代理配置路径为空")
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return types.SharedProxyConfig{}, fmt.Errorf("读取共享代理配置失败: %w", err)
|
||||
}
|
||||
var shared types.SharedProxyConfig
|
||||
if err := json.Unmarshal(data, &shared); err != nil {
|
||||
return types.SharedProxyConfig{}, fmt.Errorf("解析共享代理配置失败: %w", err)
|
||||
}
|
||||
if shared.SchemaVersion != SharedProxyConfigSchemaVersion {
|
||||
return types.SharedProxyConfig{}, fmt.Errorf(
|
||||
"不支持的共享代理配置版本 %d,当前仅支持 %d",
|
||||
shared.SchemaVersion, SharedProxyConfigSchemaVersion,
|
||||
)
|
||||
}
|
||||
if !hasSharedProxySettings(shared.Proxy) {
|
||||
return types.SharedProxyConfig{}, errors.New("共享代理配置中没有 proxy 设置")
|
||||
}
|
||||
return shared, nil
|
||||
}
|
||||
|
||||
// ApplySharedProxyConfig 将共享代理字段叠加到实例配置,未提供的字段保持不变。
|
||||
func ApplySharedProxyConfig(cfg *types.Config, shared types.SharedProxyConfig) {
|
||||
if cfg == nil {
|
||||
return
|
||||
}
|
||||
proxy := shared.Proxy
|
||||
if proxy.Enabled != nil {
|
||||
cfg.ProxyEnabled = *proxy.Enabled
|
||||
}
|
||||
if proxy.API != nil {
|
||||
cfg.ProxyAPI = strings.TrimSpace(*proxy.API)
|
||||
}
|
||||
if proxy.AccountsPerProxy != nil {
|
||||
cfg.AccountsPerProxy = *proxy.AccountsPerProxy
|
||||
}
|
||||
if proxy.SparePercent != nil {
|
||||
cfg.ProxySparePercent = *proxy.SparePercent
|
||||
}
|
||||
if proxy.AutoFetchOnSchedule != nil {
|
||||
cfg.AutoFetchProxyOnSchedule = *proxy.AutoFetchOnSchedule
|
||||
}
|
||||
if proxy.AutoWhitelist != nil {
|
||||
cfg.ProxyAutoWhitelist = *proxy.AutoWhitelist
|
||||
}
|
||||
if proxy.WhitelistReplace != nil {
|
||||
cfg.ProxyWhitelistReplace = *proxy.WhitelistReplace
|
||||
}
|
||||
if proxy.WhitelistPassword != nil {
|
||||
cfg.ProxyWhitelistPassword = strings.TrimSpace(*proxy.WhitelistPassword)
|
||||
}
|
||||
if proxy.WhitelistSignKey != nil {
|
||||
cfg.ProxyWhitelistSignKey = strings.TrimSpace(*proxy.WhitelistSignKey)
|
||||
}
|
||||
normalizeProxyConfig(cfg)
|
||||
}
|
||||
|
||||
// SaveSharedProxyConfig 将当前代理配置写入共享文件,不包含实例代理 URL 或账号数据。
|
||||
func SaveSharedProxyConfig(path string, cfg types.Config) error {
|
||||
path = strings.TrimSpace(path)
|
||||
if path == "" {
|
||||
return errors.New("共享代理配置路径为空")
|
||||
}
|
||||
shared := types.SharedProxyConfig{
|
||||
SchemaVersion: SharedProxyConfigSchemaVersion,
|
||||
UpdatedAt: time.Now().Format(time.RFC3339),
|
||||
Proxy: types.SharedProxySettings{
|
||||
Enabled: boolPointer(cfg.ProxyEnabled),
|
||||
API: stringPointer(cfg.ProxyAPI),
|
||||
AccountsPerProxy: intPointer(cfg.AccountsPerProxy),
|
||||
SparePercent: intPointer(cfg.ProxySparePercent),
|
||||
AutoFetchOnSchedule: boolPointer(cfg.AutoFetchProxyOnSchedule),
|
||||
AutoWhitelist: boolPointer(cfg.ProxyAutoWhitelist),
|
||||
WhitelistReplace: boolPointer(cfg.ProxyWhitelistReplace),
|
||||
WhitelistPassword: stringPointer(cfg.ProxyWhitelistPassword),
|
||||
WhitelistSignKey: stringPointer(cfg.ProxyWhitelistSignKey),
|
||||
},
|
||||
}
|
||||
data, err := json.MarshalIndent(shared, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("生成共享代理配置失败: %w", err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
||||
return fmt.Errorf("创建共享配置目录失败: %w", err)
|
||||
}
|
||||
temp, err := os.CreateTemp(filepath.Dir(path), "."+filepath.Base(path)+".*.tmp")
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建共享配置临时文件失败: %w", err)
|
||||
}
|
||||
tempPath := temp.Name()
|
||||
defer func() {
|
||||
if tempPath != "" {
|
||||
_ = os.Remove(tempPath)
|
||||
}
|
||||
}()
|
||||
if err := temp.Chmod(0600); err != nil {
|
||||
_ = temp.Close()
|
||||
return err
|
||||
}
|
||||
if _, err := temp.Write(data); err != nil {
|
||||
_ = temp.Close()
|
||||
return err
|
||||
}
|
||||
if err := temp.Sync(); err != nil {
|
||||
_ = temp.Close()
|
||||
return err
|
||||
}
|
||||
if err := temp.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := replaceSharedProxyConfigFile(tempPath, path); err != nil {
|
||||
return fmt.Errorf("替换共享代理配置失败: %w", err)
|
||||
}
|
||||
tempPath = ""
|
||||
return nil
|
||||
}
|
||||
|
||||
func hasSharedProxySettings(proxy types.SharedProxySettings) bool {
|
||||
return proxy.Enabled != nil ||
|
||||
proxy.API != nil ||
|
||||
proxy.AccountsPerProxy != nil ||
|
||||
proxy.SparePercent != nil ||
|
||||
proxy.AutoFetchOnSchedule != nil ||
|
||||
proxy.AutoWhitelist != nil ||
|
||||
proxy.WhitelistReplace != nil ||
|
||||
proxy.WhitelistPassword != nil ||
|
||||
proxy.WhitelistSignKey != nil
|
||||
}
|
||||
|
||||
func boolPointer(value bool) *bool { return &value }
|
||||
func intPointer(value int) *int { return &value }
|
||||
func stringPointer(value string) *string { return &value }
|
||||
@@ -0,0 +1,9 @@
|
||||
//go:build !windows
|
||||
|
||||
package config
|
||||
|
||||
import "os"
|
||||
|
||||
func replaceSharedProxyConfigFile(tempPath, targetPath string) error {
|
||||
return os.Rename(tempPath, targetPath)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
//go:build windows
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
)
|
||||
|
||||
func replaceSharedProxyConfigFile(tempPath, targetPath string) error {
|
||||
if err := os.Rename(tempPath, targetPath); err == nil {
|
||||
return nil
|
||||
}
|
||||
if err := os.Remove(targetPath); err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tempPath, targetPath)
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
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,
|
||||
ProxyURL: "http://user:pass@127.0.0.1:9000",
|
||||
ProxyAPI: "https://service.ipzan.com/core-extract?mode=auth",
|
||||
AccountsPerProxy: 5,
|
||||
ProxySparePercent: 25,
|
||||
AutoFetchProxyOnSchedule: true,
|
||||
ProxyAutoWhitelist: true,
|
||||
ProxyWhitelistReplace: true,
|
||||
ProxyWhitelistPassword: "login-pass",
|
||||
ProxyWhitelistSignKey: "0123456789abcdef",
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user