treeman_26897 on Discord runs magpie as an MCP entry point for remote clients on a server with no agents. The router could only be turned on from the Library, and the Library said "No agents to write to" with no agent. In gateway mode (magpie web on a headless server, on by itself with no agents) GATEWAY_HIDES left the Library out altogether. The router's switch also showed only once a server was in the library. The gateway and `magpie library mcp router on` had no agent gate: the gate was the page's alone. Now, with no agent to write to, or in gateway mode (where no agent is shown), the Library is its MCP tab alone (`agentless` in client.js): - servers without agent chips, found servers or projects; - the router's switch, there before the first server too; - its lines and status say any app connects at its address, the router is on or off; - the servers it serves are checked once it is on; - Connect… has Other apps alone, with the gateway key line. Gateway mode hides only Agents now. The two keys of the empty state are gone; the gateway-mode subtitle no longer says "no Library". Remote use goes through the guard /v1 has, unchanged: lanGuard and serveMCP. TestHubMCPRemoteEntryNeedsKey holds /mcp/magpie/hub to it from another computer: not shared 403; shared with no key 401 (no Origin, tools/list, a wrong key); Origin null, chrome-extension://, file://, moz-extension://, a foreign site and another computer's page refused from there and from here; a rebound Host with a browser's marks (Origin = Host, Sec-Fetch only, a Mozilla UA) refused from here and there; a proxy or tunnel here (X-Forwarded-For, Cf-Connecting-IP) with no key refused; nothing ran for any of them; with the key it runs, through the proxy too. Run on mote at ef20ff690 (on origin/mainb4799a6bb); rebased again on6a78060c6(gateway.go changed there), the gateway's Hub and LAN tests and the Budget test ran again ok: - go test -tags nogui ./internal/gateway -run Hub: ok - go test ./internal/gui -run Budget: ok (no budget raised; the core scripts were 137 bytes over with a longer comment, cut back) - gofmt -l clean; GOOS=linux and GOOS=windows go vet of gateway, gui: ok - GUI, both engines, every language: library-mcp-agentless (new, 22 of 22), gateway-mode, last-page, library-mcp-hub, mcp-hub-direct, library-servers-bulk, i18n-keys: 87 of 88; the red is i18n-keys "a plugin's words are its own", below. - Before the rebase, the GUI files a change here can reach (every library-*, gateway-mode*, last-page, i18n-*, mcp-hub-direct, agent-prefs, bare-ids, images-library-way, search-/vision-mcp-library, gateway-conversations; 70 files): 1535 of 1536. The one red is i18n-keys "a plugin's words are its own", on main already: #5327, #5332, #5426 (files this change doesn't touch). Broken by hand: - library-mcp-agentless against origin/main's client.js and app.js: 22 of 22 red (no router row with no agent; no Library tab in gateway mode). - rebound() returning false and serveMCP's Origin check off: TestHubMCPRemoteEntryNeedsKey red on the rebound Host cases and on chrome-extension://, file:// and moz-extension:// from here. The test passes on the old Go code: the guard was there already. Not tried: a real remote MCP client against a headless magpie serve or magpie web on another machine; the TUI (it has no Library page of its own for the router; the CLI's router on needs no agent). Reported-by: treeman_26897
158 lines
4.7 KiB
Go
158 lines
4.7 KiB
Go
package gui
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"net/http"
|
||
"os"
|
||
"sync"
|
||
"sync/atomic"
|
||
"time"
|
||
|
||
"github.com/yetone/magpie/internal/agent"
|
||
"github.com/yetone/magpie/internal/sessions"
|
||
"github.com/yetone/magpie/internal/settings"
|
||
)
|
||
|
||
// Gateway mode (Player on Discord): `magpie web` on a server that is only
|
||
// the gateway for other machines' agents shows what such a gateway needs —
|
||
// Providers, Gateway, Routing, Usage, Sessions, Plugins, Settings and the
|
||
// Library's MCP servers with the router (treeman_26897) — and leaves out
|
||
// what is this machine's agents' (Agents, the Library's other tabs, the settings
|
||
// written into agents' files) and its desktop's (the tray, the Dock, open
|
||
// at login, notifications). It is the browser page's alone: the app's
|
||
// windows show everything whatever it says.
|
||
//
|
||
// Settings' choice (settings.GatewayMode, "on" or "off") wins; with none,
|
||
// `magpie web --gateway` turns it on, and so does a magpie web with no
|
||
// agent on its machine (a container, a NAS). Settings › General turns it
|
||
// off again, which brings the pages back.
|
||
|
||
// WebGateway is `magpie web --gateway`'s.
|
||
var WebGateway atomic.Bool
|
||
|
||
// webPage is set once the page is served by `magpie web`: settingsState,
|
||
// which has no Windows, tells the page its gateway mode by it.
|
||
var webPage atomic.Bool
|
||
|
||
// gatewayMode says whether the page is in gateway mode, and why: "on" or
|
||
// "off" (Settings' choice), "flag" (magpie web --gateway), "no-agents", or
|
||
// "" (the app's windows, or magpie web on a machine with agents).
|
||
func gatewayMode(web bool) (bool, string) {
|
||
if !web {
|
||
return false, ""
|
||
}
|
||
switch m := settings.Load().GatewayMode; m {
|
||
case "on", "off":
|
||
return m == "on", m
|
||
}
|
||
if WebGateway.Load() {
|
||
return true, "flag"
|
||
}
|
||
if !agentsHere() {
|
||
return true, "no-agents"
|
||
}
|
||
return false, ""
|
||
}
|
||
|
||
// agentsHere says whether an agent is found on this computer; a test's own.
|
||
var agentsHere = func() bool { return len(agent.Detected()) > 0 }
|
||
|
||
// gatewayModeRoutes: POST /api/settings/gateway-mode {"mode": "on" | "off"
|
||
// | ""} sets Settings' choice, "" leaving it to magpie again.
|
||
func gatewayModeRoutes(mux *http.ServeMux) {
|
||
mux.HandleFunc("POST /api/settings/gateway-mode", func(rw http.ResponseWriter, r *http.Request) {
|
||
var in struct {
|
||
Mode string `json:"mode"`
|
||
}
|
||
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
||
fail(rw, err)
|
||
return
|
||
}
|
||
if in.Mode != "" && in.Mode != "on" && in.Mode != "off" {
|
||
fail(rw, fmt.Errorf("gateway mode is on, off or \"\" (automatic), not %q", in.Mode))
|
||
return
|
||
}
|
||
s := settings.Load()
|
||
s.GatewayMode = in.Mode
|
||
if err := settings.Save(s); err != nil {
|
||
fail(rw, err)
|
||
return
|
||
}
|
||
// the Sessions page lists this computer's agents' sessions, or
|
||
// stops, at once (ownSessionsOff)
|
||
ownSessionsOff()
|
||
forgetOwnSessions()
|
||
writeJSON(rw, settingsState())
|
||
})
|
||
}
|
||
|
||
// ownSessionsOff is gatewayMode as the sessions' gate asks it
|
||
// (sessions.OwnOff; paultian on Discord): in gateway mode the Sessions page
|
||
// and Usage › Sessions list what came through this gateway, never this
|
||
// computer's own agents' session files, which a machine put in gateway
|
||
// mode with agents installed still listed. It is asked on every read of a
|
||
// session, so it reads the shared settings and an agents look kept a while
|
||
// (agentsHereKept) rather than gatewayMode's own. When its answer turns,
|
||
// what was read under the other answer is dropped (ownTurned).
|
||
func ownSessionsOff() bool {
|
||
on := false
|
||
if webPage.Load() {
|
||
switch settings.Shared().GatewayMode {
|
||
case "on":
|
||
on = true
|
||
case "off":
|
||
default:
|
||
on = WebGateway.Load() || !agentsHereKept()
|
||
}
|
||
}
|
||
ownTurned(on)
|
||
return on
|
||
}
|
||
|
||
// ownWas is ownSessionsOff's last answer: 0 none yet, 1 off, 2 on.
|
||
var ownWas atomic.Int32
|
||
|
||
// ownTurned drops the sessions read, and the stats made, under the other
|
||
// answer, off the caller's goroutine: the gate is asked under the
|
||
// sessions' own lock, which Reset takes.
|
||
func ownTurned(on bool) {
|
||
v := int32(1)
|
||
if on {
|
||
v = 2
|
||
}
|
||
if was := ownWas.Swap(v); was != 0 && was != v {
|
||
go forgetOwnSessions()
|
||
}
|
||
}
|
||
|
||
func forgetOwnSessions() {
|
||
sessions.Reset()
|
||
forgetStats()
|
||
}
|
||
|
||
// agentsHereKept is agentsHere looked at most every half minute: it looks
|
||
// for every agent's files and commands. Another HOME or PATH (a test's
|
||
// sandbox) is looked at afresh.
|
||
var agentsSeen struct {
|
||
sync.Mutex
|
||
at time.Time
|
||
on string // the HOME and PATH it was looked for in
|
||
here bool
|
||
}
|
||
|
||
func agentsHereKept() bool {
|
||
agentsSeen.Lock()
|
||
defer agentsSeen.Unlock()
|
||
on := os.Getenv("HOME") + "\x00" + os.Getenv("PATH")
|
||
if agentsSeen.at.IsZero() || agentsSeen.on != on || time.Since(agentsSeen.at) > 30*time.Second {
|
||
agentsSeen.here, agentsSeen.at, agentsSeen.on = agentsHere(), time.Now(), on
|
||
}
|
||
return agentsSeen.here
|
||
}
|
||
|
||
func init() {
|
||
f := ownSessionsOff
|
||
sessions.OwnOff.Store(&f)
|
||
}
|