Files
magpie/internal/cli/web_cli.go
T
yetone 23e764d0e0 cli: magpie's commands move from the root into internal/cli; the root keeps only main.go
The root held 89 .go files (30 commands, 59 tests). They move as they are
to internal/cli (package cli), with git mv so history follows them. The
only code change is main() becoming cli.Main(version): the root's new
main.go keeps `var version` in package main, so -ldflags "-X
main.version=…" in the Makefile, the Dockerfile and magpie-releases, and
every workflow, work unchanged. go-winres's rsrc_windows_*.syso still land
next to package main in the root.

Tests that read testdata by a root-relative path now go up one level
(../plugin/testdata/fake, ../sessions/testdata). Five subsystem doc links
point at internal/cli/.

Run:
- go build ./..., gofmt -l, go vet on darwin, linux and windows (nogui)
- builds with tags production, dev, "production nogui"; windows
  -H windowsgui; linux CGO_ENABLED=0
- make release-cli TARGETS="darwin/arm64 linux/amd64 windows/amd64
  android/arm64" VERSION=9.9.9-tidy: all four built, binary prints
  "magpie 9.9.9-tidy"
- docker build --build-arg VERSION=7.7.7-tidy: container prints
  "magpie 7.7.7-tidy"
- go test ./internal/cli (default and nogui): 152 tests, the same count
  as the root on main
- go test ./...: all packages pass (agent, gateway, library, provider
  timed out at 10m under other sessions' load and passed rerun alone
  with -timeout 40m; none of them changed)
- the built binary in a sandbox HOME: version, help, ls, providers,
  claude, and an unknown command's error
2026-10-12 20:40:22 +08:00

96 lines
3.1 KiB
Go

package cli
import (
"fmt"
"net"
"net/url"
"os"
"strings"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/gui"
)
// webAddr is where `magpie web` listens unless told: beside the gateway.
const webAddr = "127.0.0.1:3430"
// webCmd: magpie web [--addr host:port] [--lan] [--no-open] [--gateway] —
// the app's window in a browser, for a computer that can't show the app;
// --gateway shows it in gateway mode (gui.WebGateway) unless Settings says
// otherwise.
func webCmd(args []string) error {
addr, lan, open := webAddr, false, true
for i := 0; i < len(args); i++ {
switch a := args[i]; {
case a == "--addr" && i+1 < len(args):
i++
addr = args[i]
case strings.HasPrefix(a, "--addr="):
addr = strings.TrimPrefix(a, "--addr=")
case a == "--lan":
lan = true
case a == "--no-open":
open = false
case a == "--gateway":
gui.WebGateway.Store(true)
default:
return fmt.Errorf("magpie web: unknown %q · magpie web [--addr host:port] [--lan] [--no-open] [--gateway], MAGPIE_WEB_KEY to keep one key", a)
}
}
if lan && addr == webAddr {
_, port, _ := net.SplitHostPort(webAddr)
addr = "0.0.0.0:" + port
}
w, err := gui.StartWeb(addr, version)
if err != nil {
return err
}
fmt.Println(green.Render("●"), "magpie web on", bold.Render(w.Link))
host, port, _ := net.SplitHostPort(w.Addr)
key := w.Link[strings.Index(w.Link, "/?k="):]
if ip := net.ParseIP(host); ip != nil && !ip.IsLoopback() {
for _, l := range gui.NetworkLinks(port, key) {
fmt.Println(muted.Render(" on the network"), l)
}
if gateway.ContainerAddrs() {
fmt.Println(muted.Render(" " + containerNote))
}
fmt.Println(amber.Render("!"), "anyone with the link can change magpie and see its keys, and the network carries it unencrypted")
} else if l := proxyLink(key); l != "" {
fmt.Println(muted.Render(" through your proxy"), l)
}
carries := "this run's key (MAGPIE_WEB_KEY keeps one across runs)"
if os.Getenv("MAGPIE_WEB_KEY") != "" {
carries = "MAGPIE_WEB_KEY"
}
fmt.Println(muted.Render(" the link carries " + carries + " · gateway " + advertisedURL() + " · Ctrl-C to stop"))
if open {
openInBrowser(w.Link)
}
return w.Wait()
}
// proxyLink is the page's link through a reverse proxy in front of a page
// served on loopback (#1479), with the key: MAGPIE_WEB_URL, the page's own
// address there (https://magpie.example.com), or else MAGPIE_PUBLIC_URL
// with no port of its own, as NetworkLinks takes it (#372). One with a
// port names a host whose ports are published, not a proxy. "" with
// neither, or a MAGPIE_WEB_URL that isn't a plain http(s) address.
func proxyLink(key string) string {
if v := strings.TrimSpace(os.Getenv("MAGPIE_WEB_URL")); v != "" {
if !strings.Contains(v, "://") {
v = "https://" + v
}
u, err := url.Parse(v)
if err != nil || u.Host == "" || (u.Scheme != "http" && u.Scheme != "https") || u.RawQuery != "" || u.Fragment != "" || u.User != nil {
return ""
}
return strings.TrimRight(u.String(), "/") + key
}
u, err := url.Parse(gateway.PublicURL())
if err != nil || u.Host == "" || u.Port() != "" {
return ""
}
return strings.TrimRight(u.String(), "/") + key
}