Desktop's built-in SSH remote sessions hand the profile's gateway address
and key to the remote host. magpie wrote 127.0.0.1 and this computer's own
key there, so the host refused it (provider_loopback; a non-loopback address
must be https), the gateway turned the key away from another machine, and
3p mode's sshHostAllowlist (empty) allowed no host.
Desktop's row gets a "gateway" picker, drawn only while LAN sharing is on
and an address of this computer is https (gateway.LANURLs: HTTPS on, or
MAGPIE_PUBLIC_URL): "This computer" (default, loopback) or that address.
The pick is settings.DesktopGateway (carried by the settings save, kept by
KeepOwn). desktopOn / desktopCheck / desktopKeyApplied write and compare
desktopGateway() and the key at it (agentKeyAt: the sharing key). On a
non-loopback address sshHostAllowlist is seeded once with ["*"]
(desktopSeedOnce; the user may narrow it, never rewritten). The pick holds
only while it is still one of the https addresses; Agent.Gateway is
desktopGateway, so Rekey (after the sharing toggle, and now after the HTTPS
toggle) sets Desktop again, back to loopback when sharing goes off.
desktopIsOurGateway takes this computer's LAN addresses; realReachProbe no
longer verifies the certificate (magpie's CA is not trusted on this
computer), which read a LAN https address as unreachable.
Run (mica, sandbox HOME): go test ./internal/agent ./internal/settings
./internal/settingsapi ok; ./internal/gui ok except
TestPanelHiddenByWailsMarksPageOutOfSight, flaky the same on origin/main
fc615339f (#5322); go vet darwin, linux (nogui) and windows on the three
packages; gofmt clean. TestDesktopPointedAtLANHTTPS is red when desktopOn
writes loopback and when the key is not the sharing key. GUI
desktop-gateway-address.test.cjs 20/20 (chromium + webkit, en zh zh-TW ja
de, 1100 and 420px), red 20/20 with the field left in the "nothing to pick"
filter's absence; i18n-keys, desktop-*, click-scroll files.
Not tried: a real Desktop SSH session to a remote host, the remote host
trusting magpie's CA (it must, or use an imported certificate), the real
Claude Desktop app. Windows and Linux runs only vetted.
138 lines
4.2 KiB
Go
138 lines
4.2 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// The gateway's port is set in Settings (Magic_zero on Discord): every
|
|
// agent magpie connected has the gateway's URL written into its config, so
|
|
// moving the gateway moves them too. Before the port changes, OnGateway
|
|
// notes which agents reach the gateway where it is; once it has moved,
|
|
// Rewire sets each of them again, which writes the URL it has now.
|
|
|
|
// OnGateway are the agents connected to magpie whose configs reach the
|
|
// gateway at its address now: none of their wiring is off.
|
|
func OnGateway() []*Agent {
|
|
var out []*Agent
|
|
for _, a := range All() {
|
|
if len(a.Fields) == 0 || !a.Wired() {
|
|
continue
|
|
}
|
|
if d := a.Drift(); d != nil && d.Kind == "unwired" {
|
|
continue // taken off magpie before: the user's to set again
|
|
}
|
|
out = append(out, a)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Rewire puts the gateway's address now into the configs of agents, taken
|
|
// by OnGateway before it moved: each one whose wiring is off now is set
|
|
// again, an agent joined to magpie beside its own models (Codex signed in
|
|
// with ChatGPT) joined again. It says the names of those it moved, and
|
|
// what one it couldn't move failed with. Cursor Private Inference's
|
|
// variables and dsh's routes, which magpie keeps on its own, follow too;
|
|
// Claude Science's daemon, started for the old address, is drift
|
|
// (scienceCheck) and is started again with the rest.
|
|
func Rewire(as []*Agent) (moved []string, err error) {
|
|
var off []*Agent
|
|
for _, a := range as {
|
|
if d := a.Drift(); d != nil && d.Kind == "unwired" {
|
|
off = append(off, a)
|
|
}
|
|
}
|
|
moved, errs := setAgain(off)
|
|
KeepCursorLocalEnv(context.Background())
|
|
if trouble := dshWiredOnce(); trouble != "" {
|
|
errs = append(errs, fmt.Errorf("%s", trouble))
|
|
}
|
|
return moved, joinErrs(errs)
|
|
}
|
|
|
|
// Rekey sets again those of as, taken by OnGateway before LAN sharing was
|
|
// turned on or off or its key made anew, that reach the gateway from beyond
|
|
// loopback (an agent in a WSL distro under NAT): the key they send there is
|
|
// the sharing key while sharing is on (keyAt), so it changed with it. One
|
|
// connected while nothing was shared kept gateway.Token, which the gateway
|
|
// turns away from beyond loopback, after sharing was turned on for it.
|
|
func Rekey(as []*Agent) (moved []string, err error) {
|
|
var away []*Agent
|
|
for _, a := range as {
|
|
if a.Gateway != nil && (onAnotherMachine(a.Gateway()) || a.ID == "claude-desktop" && settings.Load().DesktopGateway != "") {
|
|
away = append(away, a)
|
|
}
|
|
}
|
|
moved, errs := setAgain(away)
|
|
return moved, joinErrs(errs)
|
|
}
|
|
|
|
// setAgain sets each agent's wiring again, keeping what it had before
|
|
// magpie, and says the names of those it set and what one failed with.
|
|
func setAgain(as []*Agent) (moved []string, errs []error) {
|
|
for _, a := range as {
|
|
// set again on the old URL, an agent reads as not routed through
|
|
// magpie yet, and its set notes the old URL, magpie's token and
|
|
// model as what it had before magpie: what it really had is kept,
|
|
// for switching it off to put back
|
|
was := stashLoad()
|
|
e := reset(a)
|
|
// the address magpie wrote follows it (wiredAt)
|
|
for k, v := range stashLoad() {
|
|
if strings.HasSuffix(k, wiredAt) {
|
|
was[k] = v
|
|
}
|
|
}
|
|
stashPut(was)
|
|
if e != nil {
|
|
errs = append(errs, fmt.Errorf("%s: %w", a.Name, e))
|
|
continue
|
|
}
|
|
if a.Sync != nil {
|
|
_ = a.Sync()
|
|
}
|
|
moved = append(moved, a.Name)
|
|
}
|
|
return moved, errs
|
|
}
|
|
|
|
// joinErrs is errs as one error, "; " between them; nil for none.
|
|
func joinErrs(errs []error) (err error) {
|
|
if len(errs) > 0 {
|
|
err = errs[0]
|
|
for _, e := range errs[1:] {
|
|
err = fmt.Errorf("%w; %w", err, e)
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
// reset sets an agent's wiring again: one joined beside its own models
|
|
// joined again, any other's fields set again.
|
|
func reset(a *Agent) error {
|
|
if a.Native != nil {
|
|
return a.Native.Connect()
|
|
}
|
|
if a.Join != nil && a.Joined != nil && a.Joined() {
|
|
ok, err := a.Join()
|
|
if err != nil || ok {
|
|
return err
|
|
}
|
|
}
|
|
return a.Reapply()
|
|
}
|
|
|
|
// wiredAt ends a stash key holding the gateway address magpie last wrote
|
|
// into an agent's config, which a move of the gateway updates.
|
|
const wiredAt = ".wired_at"
|
|
|
|
// stashPut writes the stash as m has it.
|
|
func stashPut(m map[string]string) {
|
|
stashMu.Lock()
|
|
defer stashMu.Unlock()
|
|
stashWrite(m)
|
|
}
|