Repository navigation
Expand file tree
/
Copy pathweb_cli.go
More file actions
69 lines (65 loc) · 2.1 KB
/
Copy pathweb_cli.go
File metadata and controls
69 lines (65 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"net"
"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, _, _ := net.SplitHostPort(w.Addr)
if ip := net.ParseIP(host); ip != nil && !ip.IsLoopback() {
_, port, _ := net.SplitHostPort(w.Addr)
key := w.Link[strings.Index(w.Link, "/?k="):]
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")
}
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()
}