Files
magpie/internal/cli/https_cli.go
T
yetone 4623f54190 gateway: HTTPS for other computers, from magpie's CA or an imported certificate (zhangly1 on X)
zhangly1 on X: Claude on another computer on the LAN needed magpie at
https://. Settings → Network and sharing → HTTPS (magpie https on)
serves other computers TLS on the gateway's own port, with a
certificate magpie's CA (the mTLS one, in ~/.config/magpie/mtls) signs
for this computer's names and addresses; this computer keeps plain
http://. The switch is read per connection, so no restart.

- Certificate row: Import… takes the user's own PEM certificate (with
  chain) and unencrypted key, in any files and order; a pair that can't
  be served (no key, no cert, a stranger key, a passphrase, run out,
  nothing PEM) is refused with the reason in the page's language and
  leaves nothing behind. Remove goes back to magpie's CA. --tls-cert
  still comes first.
- magpie's CA row: SHA-256 shown, Save puts magpie-ca.crt in Downloads;
  the gateway serves it without a key at /magpie-ca.crt to any address
  the share lets in. Rows under it give curl -kO, macOS, Windows, Linux
  and NODE_EXTRA_CA_CERTS steps with Copy.
- A page open from another computer can't turn HTTPS on or off under
  itself (it would lose its own address).
- magpie https status|on|off|import|remove|ca --out; the serve banner
  names magpie's CA or the imported file instead of "(mTLS)".
- docs/reference.md and gateway-routing.md say all of it, and that
  Windows' curl.exe needs --ssl-no-revoke for any private CA.

Run:
- gofmt -l clean; go vet linux -tags nogui ./internal/..., GOOS=windows
  and GOOS=darwin -tags nogui vet of the touched packages (WSL, mote
  refused ssh all evening).
- WSL: go test -tags nogui gateway -run HTTPS|MTLS|ClientCert|CAFile|TLS|LAN
  ok; gui -run HTTPS|MTLS|Keep ok; settings, settingsapi, mtls ok;
  cli -run Help|HTTPS|TLS|Serve|Banner|Flag|Unknown ok. The whole cli
  package hit its 40m timeout in TestEveryAgentFieldNameWritesNothing
  (#5234, #4247, not this change).
- GUI (WSL Playwright, chromium + webkit): https-settings (en, zh, 390px,
  strings in all four languages) ok, plus mtls-settings, allow-ips,
  browser-access, cors-origins, gui-de/ja/zh-tw, i18n-keys,
  i18n-en-no-cjk, lan-*, public-url*, api-key-theme,
  connect-address-kept, gateway-connect-keys, gateway-caller-keys,
  side-gateway-lan, menu-scroll. Reds at load ~130: api-key-theme
  (#5044), connect-address-kept (#5972), gateway-caller-keys (#2572),
  gui-ja (#5943), lan-docker (#4692), all red the same at base
  10bef5fe0 run beside it; allow-ips and gateway-connect-keys went red
  once under that load and pass 18/18 at concurrency 1, base and this
  change side by side.
- Real: a sandbox HOME gateway built from this tree on WSL
  (MAGPIE_ADDR=0.0.0.0:3593, a gateway key, magpie https on, magpie
  serve). From the Windows host: curl -k /magpie-ca.crt 200 without a
  key; Node fetch with NODE_EXTRA_CA_CERTS=magpie-ca.crt 200 on
  /v1/models, 401 without the key, SELF_SIGNED_CERT_IN_CHAIN without
  the CA; plain http:// 400; curl.exe --cacert needs --ssl-no-revoke
  (schannel), then 200. Cert SANs: localhost, the hostname, .local,
  127.0.0.1, ::1 and every address. magpie https import of an openssl
  CA's leaf on the running gateway: Node trusting that CA 200, magpie's
  CA alone refused. remove + off on the running gateway: plain http://
  from Windows 200. This computer's plain http:// 200 throughout.
- At the pushed tree (on origin/main ee818e8bc), WSL:
  gofmt -l clean; go vet -tags nogui linux, GOOS=windows, GOOS=darwin
  nogui of gateway, gui, cli ok; go test -run 'HTTPS|CAFile|MTLS|LAN'
  gateway, gui ok. GUI https-settings, mtls-settings, i18n-keys,
  gui-de/ja/zh-tw, allow-ips, lan-* (both engines): 147/155; reds are
  gui-ja (#5943), lan-docker (#4692), allow-ips webkit under load, and
  gui-zh-tw chromium under load (2 of 3 green alone, filed #5983).

Broken by hand:
- TLSOn without HTTPSOn: TestHTTPSOtherComputer red.
- lanGuard without the caFileOpen exemption: TestHTTPSCAFile red.
- mtls config without GetCertificate = settingsCert: all three
  serving tests red.
- app.js without renderHTTPS(s): https-settings red in both engines.

Not tried: the macOS, Windows and Linux system trust commands on a real
store (they change the box's trust); Claude Desktop itself; a darwin
GUI run (mote unreachable); the full suite before the tag (started
after it).
2026-11-10 23:05:47 +08:00

138 lines
4.3 KiB
Go

package cli
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/mtls"
"github.com/yetone/magpie/internal/settings"
)
const httpsUsage = `usage: magpie https [on|off|import <cert.pem> [<key.pem>…]|remove|ca [--out <folder>]]
other computers reach this magpie's gateway, and magpie web's page, at https://
(zhangly1 on X: Claude on another computer on the LAN). This computer, and a WSL
distro's agent, may still call plain http://.
magpie https whether it is on, the certificate served and magpie's CA
magpie https on|off serve other computers over https://, or plain http:// again
magpie https import <files> serve your own certificate (its chain after it) and key,
PEM, in one file or several; kept in magpie's folder
magpie https remove back to a certificate magpie's own CA signs for this
computer's names and addresses
magpie https ca save magpie's CA as magpie-ca.crt in Downloads (or --out)
Without a certificate of your own, each computer that calls magpie trusts its CA once.
Get it from Settings, or on that computer from https://<this computer>:<port>/magpie-ca.crt
(check its SHA-256 against the one magpie https prints), then:
macOS sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain magpie-ca.crt
Windows certutil -addstore -f Root magpie-ca.crt (as administrator)
Linux sudo cp magpie-ca.crt /usr/local/share/ca-certificates/ && sudo update-ca-certificates
Node apps such as Claude Code: NODE_EXTRA_CA_CERTS=/path/to/magpie-ca.crt`
func httpsCmd(args []string) error {
if len(args) == 0 || args[0] == "status" {
return httpsStatus()
}
switch args[0] {
case "on", "off":
on := args[0] == "on"
if on {
if c, _ := gateway.ImportedFiles(); c == "" {
if _, _, err := gateway.CA(); err != nil {
return err
}
}
}
if err := settings.SetHTTPS(on); err != nil {
return err
}
if !on {
fmt.Println("HTTPS off: other computers call magpie at http:// again")
if gateway.TLSOn() {
fmt.Println("(still served over https://: --tls-cert or mTLS is on)")
}
return nil
}
fmt.Println("HTTPS on: other computers call magpie at https://; this computer may still call http://")
return httpsStatus()
case "import":
if len(args) < 2 {
return errors.New("usage: magpie https import <cert.pem> [<key.pem>…]")
}
var files [][]byte
for _, f := range args[1:] {
b, err := os.ReadFile(f)
if err != nil {
return err
}
files = append(files, b)
}
if err := gateway.ImportCert(files...); err != nil {
return err
}
fmt.Println("imported: other computers are served this certificate over https://")
if !gateway.HTTPSOn() && !gateway.TLSOn() {
fmt.Println("HTTPS is off: magpie https on to serve it")
}
return httpsStatus()
case "remove":
if err := gateway.RemoveImportedCert(); err != nil {
return err
}
fmt.Println("removed: magpie's CA signs the certificate served")
return nil
case "ca":
rest, out, err := outFlag(args[1:])
if err != nil {
return err
}
if len(rest) != 0 {
return errors.New("usage: magpie https ca [--out <folder>]")
}
b, fp, err := gateway.CA()
if err != nil {
return err
}
if out == "" {
out = downloadsDir()
}
p := filepath.Join(out, "magpie-ca.crt")
if err := mtls.WriteFile(p, b, 0o644); err != nil {
return err
}
fmt.Println(p)
fmt.Println(" SHA-256 " + fp)
return nil
}
return fmt.Errorf("magpie https: unknown %q\n%s", args[0], httpsUsage)
}
func httpsStatus() error {
state := "off"
if gateway.HTTPSOn() {
state = "on"
}
fmt.Println("HTTPS " + state)
if !gateway.TLSOn() {
return nil
}
c, err := gateway.ServedCert()
if err != nil {
return err
}
from := map[string]string{"files": "--tls-cert " + c.File, "imported": "imported", "magpie": "magpie's CA"}[c.From]
fmt.Printf(" certificate %s · %s · until %s\n", from, strings.Join(c.Names, ", "), c.Until.Format("2006-01-02"))
if c.From == "magpie" {
_, fp, err := gateway.CA()
if err != nil {
return err
}
fmt.Println(" CA " + gateway.MTLSStore().CAFile())
fmt.Println(" SHA-256 " + fp)
fmt.Println(" trust it on each computer that calls magpie: magpie https --help")
}
return nil
}