diff --git a/internal/provider/agy_move.go b/internal/provider/agy_move.go index 438b29e06..0e88638f1 100644 --- a/internal/provider/agy_move.go +++ b/internal/provider/agy_move.go @@ -5,6 +5,8 @@ import ( "encoding/json" "errors" "fmt" + "maps" + "slices" "sort" "strings" @@ -132,7 +134,16 @@ func MoveToAgy(ctx context.Context) ([]ImportedAccount, error) { return nil, fmt.Errorf("%s takes no Antigravity accounts", agyCLIPackage) } forgetAccountCaches() - if PluginCLIMissing(agyCLIID) != "" { + // agy is downloaded from Google and checks each account with Google, + // so it goes the way Antigravity's requests went: where Google is + // reached only through Antigravity's own proxy (magpie in a container + // in China), going direct failed the install every time (huiqiang6 on + // X). agy-cli keeps that proxy for its requests after the move too. + if err := carryAgyProxy(accts); err != nil { + return nil, err + } + ctx = Via(ctx, PluginID(agyCLIID)) + if agyCLIMissing(agyCLIID) != "" { if err := installAgy(ctx, agyCLIID); err != nil { return nil, err } @@ -144,9 +155,68 @@ func MoveToAgy(ctx context.Context) ([]ImportedAccount, error) { return importPlugin(ctx, PluginID(agyCLIID), []string{string(text)}) } -// installAgy and importPlugin are InstallPluginCLI and ImportPluginFiles, -// for tests to stand in for. +// carryAgyProxy gives agy-cli the proxy Antigravity's requests went +// through: Antigravity's own, when agy-cli has no account yet and no proxy +// of its own; and each moving account's, where agy-cli would take another +// for it and has none of the account's own. A proxy the user chose in +// agy-cli is kept. +func carryAgyProxy(accts []map[string]any) error { + from, ok := agyProxyFrom() + if !ok { + return nil + } + id := PluginID(agyCLIID) + f, err := read() + if err != nil { + return err + } + i := slices.IndexFunc(f.Providers, func(p Provider) bool { return p.ID == id }) + if i < 0 { + f.Providers = append(f.Providers, Provider{ID: id}) + i = len(f.Providers) - 1 + } + p := &f.Providers[i] + changed := false + if p.Proxy == "" && from.Proxy != "" && len(plugin.Auths(agyCLIID)) == 0 { + p.Proxy, changed = from.Proxy, true + } + own := maps.Clone(p.AccountProxies) + if own == nil { + own = map[string]string{} + } + for _, a := range accts { + k := accountKey(agyWho(a)) + want := from.AccountProxies[k] + if want == "" { + want = from.Proxy + } + if k != "" && want != "" && want != p.Proxy && own[k] == "" { + own[k], changed = want, true + } + } + if !changed { + return nil + } + p.AccountProxies = normalAccountProxies(own) + return store(f) +} + +// agyProxyFrom is where Antigravity's proxy is kept: under the id its +// accounts were served by (the plugin's own, "antigravity-plugin", while +// the built-in was beside it), the first of them that has one. +func agyProxyFrom() (Provider, bool) { + for _, id := range []string{PluginID("antigravity"), "antigravity", "antigravity-plugin"} { + if p, ok := storedPicks(id); ok && (p.Proxy != "" || len(p.AccountProxies) > 0) { + return p, true + } + } + return Provider{}, false +} + +// agyCLIMissing, installAgy and importPlugin are PluginCLIMissing, +// InstallPluginCLI and ImportPluginFiles, for tests to stand in for. var ( - installAgy = InstallPluginCLI - importPlugin = ImportPluginFiles + agyCLIMissing = PluginCLIMissing + installAgy = InstallPluginCLI + importPlugin = ImportPluginFiles ) diff --git a/internal/provider/agy_move_test.go b/internal/provider/agy_move_test.go index a0ef2af9c..231f1cbb9 100644 --- a/internal/provider/agy_move_test.go +++ b/internal/provider/agy_move_test.go @@ -8,6 +8,7 @@ import ( "strings" "testing" + "github.com/yetone/magpie/internal/netproxy" "github.com/yetone/magpie/internal/plugin" ) @@ -17,12 +18,16 @@ import ( type agyStandIn struct { installed []string texts []string + // the proxy agy was installed and the accounts imported through, as + // their ctx chose it + cliVia, importVia string + cliInstalled bool } func standInAgy(t *testing.T, importable bool) *agyStandIn { t.Helper() s := &agyStandIn{} - oldInstall, oldProviders, oldCLI, oldImport := installPlugin, pluginProviders, installAgy, importPlugin + oldInstall, oldProviders, oldCLI, oldImport, oldMissing := installPlugin, pluginProviders, installAgy, importPlugin, agyCLIMissing installPlugin = func(_ context.Context, pkg, min string) error { s.installed = append(s.installed, pkg+"@"+min) return nil @@ -30,16 +35,21 @@ func standInAgy(t *testing.T, importable bool) *agyStandIn { pluginProviders = func(context.Context) ([]plugin.Provider, error) { return []plugin.Provider{{ID: agyCLIID, Spec: agyCLIPackage, Importable: importable}}, nil } - installAgy = func(context.Context, string) error { return errors.New("agy is installed here") } - importPlugin = func(_ context.Context, id string, texts []string) ([]ImportedAccount, error) { + agyCLIMissing = func(string) string { return "" } + installAgy = func(ctx context.Context, _ string) error { + s.cliInstalled, s.cliVia = true, netproxy.Choice(ctx) + return nil + } + importPlugin = func(ctx context.Context, id string, texts []string) ([]ImportedAccount, error) { if id != agyCLIID { return nil, errors.New("imported into " + id) } + s.importVia = netproxy.Choice(ctx) s.texts = append(s.texts, texts...) return []ImportedAccount{{User: "a@x.com", Status: "added"}}, nil } t.Cleanup(func() { - installPlugin, pluginProviders, installAgy, importPlugin = oldInstall, oldProviders, oldCLI, oldImport + installPlugin, pluginProviders, installAgy, importPlugin, agyCLIMissing = oldInstall, oldProviders, oldCLI, oldImport, oldMissing }) return s } @@ -112,3 +122,68 @@ func TestMoveToAgyNeedsAccountsAndAnImport(t *testing.T) { t.Fatalf("no import: %v, imported %v", err, s.texts) } } + +// Where Google is reached only through Antigravity's own proxy (magpie in +// a container, with that proxy set on Antigravity alone), agy is +// installed and the accounts checked through it, and agy-cli keeps it for +// its requests: going direct, the install failed every time (huiqiang6 on +// X). Each account with a proxy of its own keeps that one. +func TestMoveToAgyGoesThroughAntigravitysProxy(t *testing.T) { + signIn(t) + writePluginAuth(t, map[string]any{ + "antigravity": map[string]any{"type": "oauth", "refresh": "1//a|p", "accountId": "a@x.com"}, + "antigravity#b": map[string]any{"type": "oauth", "refresh": "1//b|p", "accountId": "B@x.com"}, + }) + if err := store(file{Providers: []Provider{{ID: "antigravity", Proxy: "http://10.0.0.7:3128", AccountProxies: map[string]string{"b@x.com": "socks5://10.0.0.8:1080"}}}}); err != nil { + t.Fatal(err) + } + s := standInAgy(t, true) + agyCLIMissing = func(string) string { return "Antigravity CLI" } + if _, err := MoveToAgy(context.Background()); err != nil { + t.Fatal(err) + } + if !s.cliInstalled || s.cliVia != "http://10.0.0.7:3128" || s.importVia != "http://10.0.0.7:3128" { + t.Fatalf("agy installed (%v) through %q, imported through %q", s.cliInstalled, s.cliVia, s.importVia) + } + if got := ProxyOfLogin(agyCLIID, "a@x.com"); got != "http://10.0.0.7:3128" { + t.Fatalf("a@x.com goes through %q", got) + } + if got := ProxyOfLogin(agyCLIID, "b@x.com"); got != "socks5://10.0.0.8:1080" { + t.Fatalf("b@x.com goes through %q", got) + } +} + +// A proxy the user chose in agy-cli stays its own; a moving account keeps +// Antigravity's as its own, as its requests went. +func TestMoveToAgyKeepsAgyCLIsOwnProxy(t *testing.T) { + signIn(t) + writePluginAuth(t, map[string]any{"antigravity": map[string]any{"type": "oauth", "refresh": "1//a|p", "accountId": "a@x.com"}}) + if err := store(file{Providers: []Provider{{ID: "antigravity", Proxy: "http://10.0.0.7:3128"}, {ID: agyCLIID, Proxy: "direct"}}}); err != nil { + t.Fatal(err) + } + s := standInAgy(t, true) + agyCLIMissing = func(string) string { return "Antigravity CLI" } + if _, err := MoveToAgy(context.Background()); err != nil { + t.Fatal(err) + } + if ProxyOf(agyCLIID) != "direct" || s.cliVia != "direct" { + t.Fatalf("agy-cli's proxy %q, installed through %q", ProxyOf(agyCLIID), s.cliVia) + } + if got := ProxyOfLogin(agyCLIID, "a@x.com"); got != "http://10.0.0.7:3128" { + t.Fatalf("a@x.com goes through %q", got) + } +} + +// With no proxy on Antigravity, agy-cli is left following the global one. +func TestMoveToAgyWithoutAProxyWritesNone(t *testing.T) { + signIn(t) + writePluginAuth(t, map[string]any{"antigravity": map[string]any{"type": "oauth", "refresh": "1//a|p", "accountId": "a@x.com"}}) + s := standInAgy(t, true) + agyCLIMissing = func(string) string { return "Antigravity CLI" } + if _, err := MoveToAgy(context.Background()); err != nil { + t.Fatal(err) + } + if _, ok := storedPicks(agyCLIID); ok || s.cliVia != "" { + t.Fatalf("agy-cli written (%v), installed through %q", ok, s.cliVia) + } +}