feat: add reusable proxy configuration

This commit is contained in:
QiuSW
2026-07-25 17:04:26 +08:00
parent f13e06500c
commit 3a3c92c875
13 changed files with 772 additions and 48 deletions
+98 -3
View File
@@ -6,6 +6,8 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"chub/internal/domain"
@@ -25,14 +27,25 @@ type Settings struct {
type Instance struct {
ID string `json:"id"`
Name string `json:"name"`
ProxyID string `json:"proxyId,omitempty"`
Launch domain.LaunchSpec `json:"launch"`
UpdatedAt time.Time `json:"updatedAt"`
}
// ProxyProfile is deliberately limited to a display name and a validated,
// non-authenticated endpoint. Credentials must never enter local config.
type ProxyProfile struct {
ID string `json:"id"`
Name string `json:"name"`
Server string `json:"server"`
UpdatedAt time.Time `json:"updatedAt"`
}
type File struct {
Version int `json:"version"`
Settings Settings `json:"settings"`
Instances []Instance `json:"instances"`
Version int `json:"version"`
Settings Settings `json:"settings"`
Instances []Instance `json:"instances"`
Proxies []ProxyProfile `json:"proxies"`
}
type Store struct{ path string }
@@ -75,6 +88,9 @@ func (s *Store) Load() (File, error) {
if result.Settings.RemoteDebugStartPort == 0 {
result.Settings.RemoteDebugStartPort = domain.DefaultRemoteDebugPort
}
if err := normalizeProxyConfig(&result); err != nil {
return File{}, err
}
return result, nil
}
@@ -94,6 +110,12 @@ func (s *Store) Save(value File) error {
if value.Instances == nil {
value.Instances = []Instance{}
}
if value.Proxies == nil {
value.Proxies = []ProxyProfile{}
}
if err := normalizeProxyConfig(&value); err != nil {
return err
}
data, err := json.MarshalIndent(value, "", " ")
if err != nil {
return fmt.Errorf("encode config: %w", err)
@@ -128,6 +150,79 @@ func (s *Store) Save(value File) error {
return nil
}
func normalizeProxyConfig(value *File) error {
if value == nil {
return errors.New("config is required")
}
byID := make(map[string]ProxyProfile, len(value.Proxies))
names := make(map[string]struct{}, len(value.Proxies))
for i := range value.Proxies {
profile := &value.Proxies[i]
profile.ID = strings.TrimSpace(profile.ID)
profile.Name = strings.TrimSpace(profile.Name)
if profile.ID == "" || profile.Name == "" {
return errors.New("invalid proxy configuration")
}
if _, exists := byID[profile.ID]; exists {
return errors.New("duplicate proxy configuration")
}
nameKey := strings.ToLower(profile.Name)
if _, exists := names[nameKey]; exists {
return errors.New("duplicate proxy configuration")
}
server, err := domain.NormalizeProxyServer(profile.Server)
if err != nil || server == "" {
return errors.New("invalid proxy configuration")
}
profile.Server = server
byID[profile.ID] = *profile
names[nameKey] = struct{}{}
}
legacyByServer := make(map[string]string, len(value.Proxies))
for _, profile := range value.Proxies {
legacyByServer[profile.Server] = profile.ID
}
for i := range value.Instances {
instance := &value.Instances[i]
instance.ProxyID = strings.TrimSpace(instance.ProxyID)
if instance.ProxyID != "" {
if _, exists := byID[instance.ProxyID]; !exists {
return errors.New("instance refers to an unavailable proxy")
}
instance.Launch.ProxyServer = ""
continue
}
if strings.TrimSpace(instance.Launch.ProxyServer) == "" {
continue
}
server, err := domain.NormalizeProxyServer(instance.Launch.ProxyServer)
if err != nil || server == "" {
return errors.New("invalid proxy configuration")
}
proxyID := legacyByServer[server]
if proxyID == "" {
proxyID = nextLegacyProxyID(byID)
profile := ProxyProfile{ID: proxyID, Name: "导入代理 " + strconv.Itoa(len(value.Proxies)+1), Server: server}
value.Proxies = append(value.Proxies, profile)
byID[proxyID] = profile
legacyByServer[server] = proxyID
}
instance.ProxyID = proxyID
instance.Launch.ProxyServer = ""
}
return nil
}
func nextLegacyProxyID(existing map[string]ProxyProfile) string {
for index := 1; ; index++ {
candidate := "legacy-proxy-" + strconv.Itoa(index)
if _, exists := existing[candidate]; !exists {
return candidate
}
}
}
func replace(target, temp string) error {
backup := target + ".bak"
_, statErr := os.Stat(target)