Chicring: Settings showed "网关端口" (gateway port), but the row showed
MAGPIE_ADDR's host:port.
What the field takes, from the code: renderPort posts to
/api/settings/port, and setPort (internal/gui/port.go) accepts only a port
from 1024 to 65535 (settings.CheckPort). The gateway listens on that port,
on 127.0.0.1, or on 0.0.0.0 while shared on the LAN. When MAGPIE_ADDR is
set, it is the whole host:port and comes first. The row then shows its
value and is disabled. So the row is the gateway's address, but only its
port is typed here; it isn't a second way to write MAGPIE_ADDR.
The row is renamed in every language: Gateway address / 网关地址 / 閘道位址 /
ゲートウェイのアドレス / Adresse des Zugangspunkts. The new hint says "Only
the port is set here, 1024 to 65535; MAGPIE_ADDR=host:port sets the whole
address instead. Agents magpie connected move with it. Now {url}". With
MAGPIE_ADDR set, the hint says "MAGPIE_ADDR={addr} sets the gateway's whole
address, host:port: unset it to set the port here". The CLI help (cpu,
healthcheck, heap, quota, statusline) and setPort's comment named
"Settings' Gateway port". They now say "the port in Settings' Gateway
address".
Test: gateway-port.test.cjs pins the new name and hints in en and zh. With
MAGPIE_ADDR set, it now also checks that the field shows 127.0.0.1:3426
and that the row's name and aria-label say the address. gui-zh-tw, gui-ja,
gui-de and i18n-keys cover the other languages. Broken by hand: on the old
app.js it is red, "not ok 1 - en expected: 'Gateway address' actual:
'Gateway port'", and zh red against '网关地址'.
What was run. The whole suites ran at dcf7eeae3, which was these two commits
on origin/main 592899467. They were rebased cleanly onto 5c557c793 and
re-checked there.
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./... and
GOOS=windows go vet ./...: ok, at both heads.
- GOMODCACHE=... go test -timeout 45m ./... in a sandbox HOME: 63 ok.
internal/agent, internal/gateway and internal/library hit the 45m package
timeout while other sessions' full suites ran (load ~27-40). Their stacks
were in short tests (TestDisconnectStaysThroughSync 21s,
TestSub2APIThinkingOffRefused 0s, TestSkillHow 10s), so nothing hung.
Each passed when run alone:
"ok internal/agent 1127.439s", "ok internal/gateway 1044.808s",
"ok internal/library 1029.999s".
- The whole GUI suite, node --test --test-concurrency=2
internal/gui/tests/*.test.cjs, with playwright 1.62.0:
- BROWSER=chromium: "# tests 5862 # pass 5844 # fail 18".
- BROWSER=webkit: "# tests 5854 # pass 5835 # fail 19".
Every red file was rerun alone on a freshly fetched origin/main
(a3ebed202 / 5c557c793) and is red there too:
- library-row-remove (chromium, 4 of 8 runs on main): filed #3099.
- quota-count-wrap and quota-name-translated (webkit): #2228.
- usage-agents-hide (both engines): #2276.
- usage-fold (chromium): #2163.
- plugin-card-update (webkit, 2 of 4 runs on 5c557c793): #3079.
- s3-sync (webkit): #2448.
- usage-checkin's Qoder tab (webkit): #2549.
- tray-usages and routing-rule-remove: passed alone on both trees.
None of these files touch the provider editor or the gateway address row.
- At 5c557c793 + these commits: decide-endpoint-label, gateway-port,
system-one, remote-magpie, i18n-keys, gui-zh-tw, gui-ja, gui-de and
lan-docker in both engines: "# tests 54 # pass 54 # fail 0".
go test ./internal/gui ./internal/cli: ok.
133 lines
4.0 KiB
Go
133 lines
4.0 KiB
Go
package gui
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/agent"
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/proc"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// portJSON is what setting the gateway's port did: the port it is on, the
|
|
// agents whose configs were moved with it, and what one couldn't be.
|
|
type portJSON struct {
|
|
Port int `json:"port"`
|
|
Moved []string `json:"moved,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
// portFree says whether the gateway could listen at addr: nothing else
|
|
// does. A var so tests can say a port is taken.
|
|
var portFree = func(addr string) error {
|
|
ln, err := net.Listen("tcp", addr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ln.Close()
|
|
}
|
|
|
|
// magpieOnPort says whether a magpie program of this user's listens on
|
|
// port: the one serving the gateway is then this computer's, whose port is
|
|
// set there. Not when the program listening is another one answering for
|
|
// a magpie inside a container (OrbStack Helper, docker-proxy) or none this
|
|
// user can see; when the processes can't be listed at all, it is taken to
|
|
// be one.
|
|
func magpieOnPort(port string) bool {
|
|
n, _ := strconv.Atoi(port)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
pids, err := proc.ListeningOn(ctx, n)
|
|
if err != nil {
|
|
return true
|
|
}
|
|
for _, pid := range pids {
|
|
if pid == os.Getpid() {
|
|
continue
|
|
}
|
|
if path, err := proc.Executable(pid); err == nil && proc.IsMagpie(path) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// errPortTaken is a port another program listens on.
|
|
func errPortTaken(p int) error {
|
|
return fmt.Errorf("port %d is in use by another program: pick another one", p)
|
|
}
|
|
|
|
// setPort moves the gateway to port p (0 for the default) — the port in
|
|
// Settings' Gateway address (Magic_zero on Discord: 3425 was taken, and a port of one's
|
|
// own is easier to tell apart): it checks nothing listens there, saves it,
|
|
// moves the gateway this magpie serves (or starts it, when the port it was
|
|
// on was taken), and then every agent connected to magpie, whose configs
|
|
// have the gateway's URL in them. A magpie of this computer's serving the
|
|
// gateway has it moved there; one out of this one's reach — in a container
|
|
// (leslie_luo on Discord: an old magpie in OrbStack held 3425, so the port
|
|
// could be changed neither there nor here), another user's — is left on
|
|
// its port, and this one serves on the new one.
|
|
func setPort(p int) (portJSON, error) {
|
|
if err := settings.CheckPort(p); err != nil {
|
|
return portJSON{}, err
|
|
}
|
|
if a := os.Getenv("MAGPIE_ADDR"); a != "" {
|
|
return portJSON{}, fmt.Errorf("MAGPIE_ADDR=%s sets the gateway's address, which comes before Settings: unset it to set the port here", a)
|
|
}
|
|
want := p
|
|
if want == 0 {
|
|
want = settings.DefaultPort
|
|
}
|
|
cur := settings.Load()
|
|
was := cur.Port
|
|
if strconv.Itoa(want) == gateway.Port() {
|
|
if was != p {
|
|
cur.Port = p
|
|
if err := settings.Save(cur); err != nil {
|
|
return portJSON{}, err
|
|
}
|
|
}
|
|
return portJSON{Port: want}, nil
|
|
}
|
|
gw := served.Load()
|
|
if gw == nil && gateway.Running() && magpieOnPort(gateway.Port()) {
|
|
return portJSON{}, fmt.Errorf("another magpie serves the gateway at %s: set its port there, or quit it first", gateway.URL())
|
|
}
|
|
host := "127.0.0.1"
|
|
if cur.LAN {
|
|
host = "0.0.0.0"
|
|
}
|
|
if err := portFree(net.JoinHostPort(host, strconv.Itoa(want))); err != nil {
|
|
return portJSON{}, errPortTaken(want)
|
|
}
|
|
// the agents reaching the gateway where it is now, to move with it
|
|
on := agent.OnGateway()
|
|
cur.Port = p
|
|
if err := settings.Save(cur); err != nil {
|
|
return portJSON{}, err
|
|
}
|
|
if gw != nil {
|
|
if err := gw.Relisten(); err != nil {
|
|
back := settings.Load()
|
|
back.Port = was
|
|
_ = settings.Save(back)
|
|
return portJSON{}, errPortTaken(want)
|
|
}
|
|
} else {
|
|
// the port it was on was taken by something else: it serves now
|
|
serveGateway()
|
|
}
|
|
out := portJSON{Port: want}
|
|
moved, err := agent.Rewire(on)
|
|
out.Moved = moved
|
|
if err != nil {
|
|
out.Error = err.Error()
|
|
}
|
|
return out, nil
|
|
}
|