HuanCheng Bai on Discord runs `magpie daemon` on a Linux server and asked
for the daemon to serve the web page too, or for an option to turn it on.
`magpie web` run beside it is a second process, and he read it as not
seeing the daemon's state.
- `magpie serve --web[=host:port]` serves the gateway and `magpie web`'s
page from one process (stats, StartWeb, Wait). The default page address
is 127.0.0.1:3430. Any other argument is refused, where before it was
ignored.
- `magpie daemon install --web | --lan | --addr host:port | --no-web` and
`magpie daemon web [on|off] [--lan] [--addr host:port]`. The unit's
ExecStart and the plist's ProgramArguments become `serve --web=<addr>`,
and the key goes into the 0600 file's environment as MAGPIE_WEB_KEY. The
key is made once. A reinstall with no flags keeps the page and its key
(read back from the file by daemon.Web), so the link survives restarts
and reinstalls. `web off` drops it. MAGPIE_WEB_KEY in the shell, when
set, is used and checked like magpie web's. The page defaults to
loopback; --lan or a non-loopback --addr prints the network links and
magpie web --lan's warning.
- `magpie daemon status`, install and `daemon web` print the page's link.
- `magpie web` while the service serves a page prints that page's link and
exits. Beside a gateway another magpie serves, it says so and points to
`magpie daemon web on`.
- docs/reference.md (the service section and the Use list), and the
site's start page (en, zh, ja). The READMEs don't cover the daemon.
Run:
- Tests: TestInstallWithWeb (systemd and launchd, plutil lint, read back,
reinstall without the page, the shell's key not carried, no key
refused), TestWebNotInstalled, TestParseDaemon, TestWebOptions,
TestDaemonStatusPrintsPage and TestServeArgs. Each of these went red
with its line reverted by hand: withKey, prevKey in webOptions, and the
status page line.
- Sandbox HOME with a fake launchctl:
- `daemon install --web --lan` wrote a 0600 plist with
`serve --web=0.0.0.0:3430` and the key. plutil said OK, and it printed
the link, the network links and the warning.
- Reinstalling with no flags kept the key. `daemon status` showed the
link.
- `daemon web off` / `daemon web` / `daemon web on` behaved as above,
with a new key after off.
- `daemon web bogus` and `serve --lan` gave the usage errors.
- `serve --web=127.0.0.1:3462` served the gateway (3461) and the page in
one process. The page answered 200 with the key and 401 without, and the
page and /v1/models both showed 3 models. `magpie web --no-open` beside
it printed the service's link and exited 0.
- gofmt -l; go vet (gui), and go vet -tags nogui for darwin, linux and
windows; go test -tags nogui ./... in a sandbox HOME.
Not tried: a real systemd on Linux. The unit text is checked by test only.
182 lines
6.2 KiB
Go
182 lines
6.2 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/daemon"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/gui"
|
|
"github.com/yetone/magpie/internal/stats"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
// the service serves the page already: its own, beside the gateway
|
|
// it serves, is the one to open
|
|
if a, k := daemon.Web(); a != "" && gateway.ServedBy().Running {
|
|
link := webLink(a, k)
|
|
fmt.Println(green.Render("●"), "the magpie service serves this page:", bold.Render(link))
|
|
fmt.Println(muted.Render(" magpie daemon web off turns it off"))
|
|
if open {
|
|
openInBrowser(link)
|
|
}
|
|
return nil
|
|
}
|
|
w, err := gui.StartWeb(addr, version)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !w.ServesGateway() {
|
|
// another magpie has the gateway (the service, magpie serve, the
|
|
// app): this page shows its settings and accounts, read from the
|
|
// same files, and takes the gateway up when that one stops
|
|
o := gateway.ServedBy()
|
|
who := "another magpie"
|
|
if o.Version != "" {
|
|
who += " (" + o.Version + ")"
|
|
}
|
|
fmt.Println(muted.Render(" the gateway on " + gateway.URL() + " is served by " + who + "; this page shows the same settings and accounts, and serves the gateway once that one stops"))
|
|
if st, err := daemon.Get(); err == nil && st.Running {
|
|
fmt.Println(muted.Render(" to have the service serve this page itself: magpie daemon web on"))
|
|
}
|
|
}
|
|
printWeb(w)
|
|
if open {
|
|
openInBrowser(w.Link)
|
|
}
|
|
return w.Wait()
|
|
}
|
|
|
|
// printWeb prints where a started page is, and what its link carries.
|
|
func printWeb(w *gui.Web) {
|
|
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("!"), lanWarning)
|
|
} 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"))
|
|
}
|
|
|
|
// lanWarning is said wherever the page is served on the network.
|
|
const lanWarning = "anyone with the link can change magpie and see its keys, and the network carries it unencrypted"
|
|
|
|
// webLink is the link to a page served on addr with key, from this
|
|
// computer.
|
|
func webLink(addr, key string) string {
|
|
host, port, err := net.SplitHostPort(addr)
|
|
if err != nil {
|
|
return "http://" + addr + "/?k=" + url.QueryEscape(key)
|
|
}
|
|
if ip := net.ParseIP(host); host == "" || ip != nil && ip.IsUnspecified() {
|
|
host = "127.0.0.1"
|
|
}
|
|
return "http://" + net.JoinHostPort(host, port) + "/?k=" + url.QueryEscape(key)
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// serveCmd: magpie serve [--web[=host:port]] — the gateway, and with
|
|
// --web magpie web's page from the same process: what the service runs
|
|
// (`magpie daemon install --web`), so the page shows the gateway that
|
|
// serves the agents, its routing included (HuanCheng on Discord).
|
|
func serveCmd(args []string) error {
|
|
web := ""
|
|
for _, a := range args {
|
|
switch {
|
|
case a == "--web":
|
|
web = webAddr
|
|
case strings.HasPrefix(a, "--web="):
|
|
web = strings.TrimPrefix(a, "--web=")
|
|
if _, _, err := net.SplitHostPort(web); err != nil {
|
|
return fmt.Errorf("magpie serve: --web=%s isn't host:port", web)
|
|
}
|
|
default:
|
|
return fmt.Errorf("magpie serve: unknown %q · magpie serve [--web[=host:port]]", a)
|
|
}
|
|
}
|
|
if web == "" {
|
|
return serve()
|
|
}
|
|
go stats.Run(version, "serve")
|
|
w, err := gui.StartWeb(web, version)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
public := advertisedURL()
|
|
fmt.Println(green.Render("●"), "magpie gateway on", bold.Render(gateway.URL()))
|
|
fmt.Println(muted.Render(" OpenAI "), public+"/v1/chat/completions", muted.Render("·"), public+"/v1/responses")
|
|
fmt.Println(muted.Render(" Anthropic"), public+"/v1/messages")
|
|
if !w.ServesGateway() {
|
|
fmt.Println(amber.Render("!"), "another magpie serves the gateway there: this one serves it once that one stops")
|
|
}
|
|
printWeb(w)
|
|
return w.Wait()
|
|
}
|