185 lines
5.7 KiB
Go
185 lines
5.7 KiB
Go
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 }
|