Files
magpie/internal/gateway/mtls.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

268 lines
9.2 KiB
Go

package gateway
import (
"crypto/tls"
"crypto/x509"
"errors"
"log"
"net"
"net/http"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/mtls"
"github.com/yetone/magpie/internal/settings"
)
// mTLS (hhdaxia on X, #5596): with Settings → Network and sharing →
// Require a client certificate on (settings MTLS, `magpie mtls on`), other
// computers reach the gateway and magpie web's page only over TLS, showing
// a client certificate magpie's own CA issued (`magpie mtls issue`) and
// hasn't revoked:
//
// - the TLS listener (tlsListener) asks every peer that isn't this
// computer, and isn't a WSL distro's, for one, and ends the handshake
// of a peer without a good one before any HTTP is read;
// - the guards (lanGuard, ClientCertGuard) refuse 403 every request from
// another machine that came without a verified one: a proxy's or
// tunnel's on loopback (proxied), magpie web's page port, the WSL
// listener's plain HTTP from a peer that isn't WSL's, a connection
// made before the switch was turned on, or a certificate revoked
// since its connection's handshake.
//
// This computer — loopback, and this computer calling its own address —
// and a WSL distro's agent with the WSL key (fromWSL) are answered as
// before, over plain HTTP too. Without --tls-cert the server certificate
// is the CA's own, made in memory for the names the client dials
// (ownServerCert); with --tls-cert it is that one, and the CA is used for
// the client certificates alone.
// MTLSOn: other computers need a client certificate magpie issued.
func MTLSOn() bool { return settings.Shared().MTLS }
// MTLSStore is where magpie's CA and the client certificates it issued are
// kept.
func MTLSStore() mtls.Store { return mtls.Store{Dir: filepath.Join(settings.Dir(), "mtls")} }
// clientCertAdmitted: r came over TLS with a client certificate magpie's
// CA verified at the handshake, which is still on the issued list.
func clientCertAdmitted(r *http.Request) bool {
if r.TLS == nil || len(r.TLS.VerifiedChains) == 0 || len(r.TLS.PeerCertificates) == 0 {
return false
}
return MTLSStore().Admits(r.TLS.PeerCertificates[0]) == nil
}
// ownAddress: r is this computer calling one of its own addresses (the
// kernel answers it without the network), not through a proxy.
func ownAddress(r *http.Request) bool {
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
return false
}
ip := net.ParseIP(host)
la, _ := r.Context().Value(http.LocalAddrContextKey).(net.Addr)
tcp, ok := la.(*net.TCPAddr)
return ip != nil && ok && tcp.IP.Equal(ip) && !proxied(r) && !viaPagePort(r)
}
// mtlsRefuses says why r, with mTLS on, is refused: "" when it is let
// through (mTLS off, a good certificate, or from this computer or WSL).
// Whether r would be let through is asked first, so this computer's own
// requests never wait on settings.json for the switch.
func mtlsRefuses(r *http.Request) string {
why := NeedsClientCert(r)
if why == "" || !MTLSOn() {
return ""
}
return why
}
// NeedsClientCert is why r would be refused with mTLS on, "" when it
// would be let through: the page turning it on is asked, so it never
// shuts out the computer it is open on.
func NeedsClientCert(r *http.Request) string {
if clientCertAdmitted(r) || local(r) || ownAddress(r) || fromWSL(takeWSLPathKey(r)) {
return ""
}
why := "magpie asks other computers for a client certificate it issued (Settings → Network and sharing → Require a client certificate), and this request came without a good one"
switch {
case r.TLS == nil && proxied(r):
why += ": it came through a proxy or tunnel, which can't pass a certificate on; reach magpie directly over https://, or have the proxy check client certificates itself and set MAGPIE_TRUST_PROXY=1"
case r.TLS == nil:
why += ": it came over plain HTTP; call magpie at https://"
case len(r.TLS.PeerCertificates) > 0:
why += ": the certificate shown was revoked or isn't one this magpie issued"
default:
why += ": issue one with `magpie mtls issue <device>` and give it to the client"
}
return why
}
// refuseWithoutClientCert answers a gateway request mtlsRefuses, in the
// error shape agents show; true when it did.
func refuseWithoutClientCert(w http.ResponseWriter, r *http.Request) bool {
why := mtlsRefuses(r)
if why == "" {
return false
}
log.Printf("refused %s %s from %s: no good client certificate", r.Method, r.URL.Path, r.RemoteAddr)
http.Error(w, why, http.StatusForbidden)
return true
}
// ClientCertGuard refuses a request mtlsRefuses, for a server other than
// the gateway's own (magpie web's page).
func ClientCertGuard(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if refuseWithoutClientCert(w, r) {
return
}
next.ServeHTTP(w, r)
})
}
// tlsServer is what a listener serves TLS with: the files --tls-cert
// names, or with mTLS on and none named, a certificate magpie's CA signs.
// It is asked for each connection, so turning mTLS on or off takes effect
// for the next one without listening again.
type tlsServer struct {
files *certFiles // nil without --tls-cert
}
// config is the TLS config for a connection, nil to serve it plain HTTP
// (no --tls-cert, mTLS off and HTTPS off). strict asks for a client certificate and
// ends a handshake without one, for a peer that isn't this computer or a
// WSL distro.
func (t *tlsServer) config(strict bool) *tls.Config {
on := MTLSOn()
if t == nil || (t.files == nil && !on && !HTTPSOn()) {
return nil
}
cfg := &tls.Config{
MinVersion: tls.VersionTLS12,
// HTTP/1.1 alone, as over plain HTTP: Codex's websocket and the
// streams' handling hijack the connection
NextProtos: []string{"http/1.1"},
}
if t.files != nil {
cfg.GetCertificate = t.files.get
} else {
cfg.GetCertificate = settingsCert
}
if !on {
return cfg
}
st := MTLSStore()
pool, err := mtlsPool(st)
if err != nil {
log.Printf("mTLS: magpie's CA can't be read (%v): no client certificate is taken", err)
pool = x509.NewCertPool()
}
cfg.ClientCAs = pool
cfg.ClientAuth = tls.VerifyClientCertIfGiven
if strict {
cfg.ClientAuth = tls.RequireAndVerifyClientCert
}
cfg.VerifyConnection = func(cs tls.ConnectionState) error {
if len(cs.PeerCertificates) == 0 {
return nil // judged by the guards, for a lenient peer
}
return st.Admits(cs.PeerCertificates[0])
}
return cfg
}
var poolCache struct {
sync.Mutex
path string
stamp fileStamp
pool *x509.CertPool
}
// mtlsPool is the store's CA as a pool, read again when ca.pem changes.
func mtlsPool(st mtls.Store) (*x509.CertPool, error) {
poolCache.Lock()
defer poolCache.Unlock()
now := stampOf(st.CAFile())
if poolCache.pool != nil && poolCache.path == st.CAFile() && poolCache.stamp == now && now != (fileStamp{}) {
return poolCache.pool, nil
}
p, err := st.Pool()
if err != nil {
return nil, err
}
poolCache.path, poolCache.stamp, poolCache.pool = st.CAFile(), stampOf(st.CAFile()), p
return p, nil
}
var ownCerts struct {
sync.Mutex
key string
stamp fileStamp
made time.Time
cert *tls.Certificate
}
// ownServerCert is a server certificate magpie's CA signs for this
// computer's names and addresses and the name the client dialled (a DDNS
// or Tailscale name): only a client given the CA trusts it, so the name a
// client asks for is the one it gets. Made in memory, and again when the
// names, the addresses or the CA change, or after a day.
func ownServerCert(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
names := []string{"localhost"}
if h, err := os.Hostname(); err == nil && h != "" {
h = strings.TrimSuffix(strings.ToLower(h), ".")
names = append(names, h)
if !strings.Contains(h, ".") {
names = append(names, h+".local")
}
}
if hello != nil && hello.ServerName != "" && net.ParseIP(hello.ServerName) == nil {
names = append(names, strings.ToLower(hello.ServerName))
}
slices.Sort(names)
names = slices.Compact(names)
ips := []net.IP{net.IPv4(127, 0, 0, 1), net.IPv6loopback}
if addrs, err := net.InterfaceAddrs(); err == nil {
for _, a := range addrs {
if n, ok := a.(*net.IPNet); ok && !n.IP.IsLoopback() {
ips = append(ips, n.IP)
}
}
}
var key strings.Builder
key.WriteString(strings.Join(names, ","))
for _, ip := range ips {
key.WriteString("|" + ip.String())
}
st := MTLSStore()
ownCerts.Lock()
defer ownCerts.Unlock()
stamp := stampOf(st.CAFile())
if ownCerts.cert != nil && ownCerts.key == key.String() && ownCerts.stamp == stamp && time.Since(ownCerts.made) < 24*time.Hour {
return ownCerts.cert, nil
}
c, err := st.ServerCert(names, ips)
if err != nil {
return nil, errors.New("TLS: magpie's CA can't sign a server certificate: " + err.Error())
}
ownCerts.key, ownCerts.stamp, ownCerts.made, ownCerts.cert = key.String(), stampOf(st.CAFile()), time.Now(), &c
return &c, nil
}
// fromWSLNet: c's peer is on one of WSL's networks, at the address
// Windows has there (a distro's agent, judged by its key in fromWSL).
func fromWSLNet(c net.Conn) bool {
r, ok := c.RemoteAddr().(*net.TCPAddr)
l, ok2 := c.LocalAddr().(*net.TCPAddr)
if !ok || !ok2 {
return false
}
return slices.ContainsFunc(WSLNets(), func(n *net.IPNet) bool { return n.IP.Equal(l.IP) && n.Contains(r.IP) })
}