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
+37
View File
@@ -55,3 +55,40 @@ func TestStoreMissingFileReturnsDefaults(t *testing.T) {
t.Fatalf("unexpected defaults: %#v", got)
}
}
func TestStoreRoundTripsReferencedProxyWithoutCredentials(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.json")
store, err := New(path)
if err != nil {
t.Fatal(err)
}
want := File{
Proxies: []ProxyProfile{{ID: "proxy-sg", Name: "新加坡出口", Server: "HTTPS://Proxy.Local:8443"}},
Instances: []Instance{{ID: "a", Name: "运营", ProxyID: "proxy-sg", Launch: domain.LaunchSpec{Kind: domain.BrowserChrome, UserDataDir: `C:\profiles\a`}}},
}
if err := store.Save(want); err != nil {
t.Fatal(err)
}
got, err := store.Load()
if err != nil || len(got.Proxies) != 1 || got.Proxies[0].Server != "https://proxy.local:8443" || got.Instances[0].ProxyID != "proxy-sg" || got.Instances[0].Launch.ProxyServer != "" {
t.Fatalf("proxy round trip = %#v, error = %v", got, err)
}
}
func TestStoreMigratesLegacyProxyAndRejectsUnsafeConfig(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.json")
if err := os.WriteFile(path, []byte(`{"version":1,"settings":{},"instances":[{"id":"a","name":"运营","launch":{"Kind":"chrome","UserDataDir":"C:\\profiles\\a","ProxyServer":"http://127.0.0.1:8080"}}]}`), 0o600); err != nil {
t.Fatal(err)
}
store, err := New(path)
if err != nil {
t.Fatal(err)
}
got, err := store.Load()
if err != nil || len(got.Proxies) != 1 || got.Instances[0].ProxyID == "" || got.Instances[0].Launch.ProxyServer != "" {
t.Fatalf("legacy proxy migration = %#v, error = %v", got, err)
}
if err := store.Save(File{Proxies: []ProxyProfile{{ID: "bad", Name: "bad", Server: "http://user:secret@127.0.0.1:8080"}}}); err == nil {
t.Fatal("credential proxy was persisted")
}
}