Files
magpie/web_cli.go
T
yetone a690d8d7a1 main: magpie web behind a reverse proxy prints its link through the proxy, key and all (#1479, uclort)
magpie web on a server, listening on 127.0.0.1:3430 behind nginx or
Caddy, printed only http://127.0.0.1:3430/?k=…, and the reporter had to
join "/?k=…" onto their domain by hand. It now also prints
"through your proxy https://magpie.example.com/?k=…" when it is told
the proxy's address: MAGPIE_WEB_URL (the page's own address there), or
else MAGPIE_PUBLIC_URL with no port of its own, which NetworkLinks
already takes as a reverse proxy for --lan (#372). A MAGPIE_WEB_URL with
a query, a fragment, credentials or another scheme is ignored.

Only the printed link changes: the key, its cookie and webGuard are as
they were, and nothing new is listened on. The key itself was already
fixed with MAGPIE_WEB_KEY (16+ letters, digits, - . _ ~); the usage text
and docs/reference.md say both.

Run: go test -tags nogui -run TestWebProxyLink . (red with proxyLink
returning ""); a built magpie web under a sandbox HOME with
MAGPIE_WEB_URL=https://magpie.example.com, MAGPIE_WEB_KEY set, on
127.0.0.1:3493 printed the proxy line, answered 401 with no key and 303
with the key under Host: magpie.example.com.
2026-10-10 18:31:32 +08:00

96 lines
3.1 KiB
Go

package main
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
}