What changed: with a proxy set (magpie's, the environment's or the system's), the plugin host sent a plugin's requests for a host on the user's own network - a Remote magpie, an LLM relay on the NAS - through that proxy, which can't reach it (502 Bad Gateway) or resolve its name. Go's netproxy.Func leaves such a host direct since #982 (lan.go onLAN); host.js's proxyFor and its node:http2 sessions (#1035) were that rule's siblings without it. - host.js ports lan.go's rule: onLAN takes a private (10/8, 172.16/12, 192.168/16, fc00::/7), link-local, loopback or CGNAT (100.64/10) address, a .local name, or a name every address of which is one of those; looked up for 400 ms at most, kept a minute (256 names), a failed or slow lookup is "not on the network". It is asked only when the global proxy would be taken (after loopback, a provider's own proxy or "direct", and NO_PROXY), as Func asks it, so a user without a proxy pays no lookup and a provider's own proxy is still taken as is. IPv6 is compared on its bytes, so a v4-mapped address in hex form (::ffff:a00:1, how URL writes it) is judged as its IPv4 address. - http2.connect returns its session at once, so tunnel() now takes a promise of the proxy and connects when onLAN has answered: straight to the host for one on the user's network, through the CONNECT otherwise. Unlike the issue's patch, a LAN name that needs a lookup is covered on this path too. - The issue's patch was reviewed, not applied: its lanIP missed loopback and hex v4-mapped IPv6, it didn't strip a trailing dot, it never cleared its lookup timer, and it left HTTP/2 to LAN names proxied. New TestPluginLeavesTheUserNetworkDirect (real Bun, a proxy that keeps every target it is asked): a fetch to this computer's private address over http:// (absolute form) and https:// (CONNECT; a certificate for that address via NODE_EXTRA_CA_CERTS), to "localhost." (a name decided by what it resolves to), and an HTTP/2 session to the private address all reach the server directly; a public address (203.0.113.5) and a name that resolves nowhere (vendor.invalid) still go through the proxy. Run: the new test against the old host.js fails on all three fetches (proxy asked for each); with only the HTTP/2 check removed it fails on the session ("h2: proxyconnect tcp: 502 Bad Gateway"); with every host taken as LAN it fails on the public address and the vendor name. A bun check of lanIP over 24 addresses (both sides of every range) matched lan.go. go test ./internal/plugin ./internal/netproxy ok; full go test -tags nogui ./... ok in every package (the five whose build hit another session's go-build cache prune were rerun, all ok: ., agent, gateway, gui, library); gofmt -l clean; go vet -tags nogui internal/plugin on darwin, GOOS=linux and GOOS=windows clean. Not tried with a real NAS or Remote magpie behind a real proxy.
89 lines
2.6 KiB
Go
89 lines
2.6 KiB
Go
package netproxy
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// A host on the user's own network — another computer's magpie (a Remote
|
|
// magpie), an Ollama on the NAS — is reached directly, whatever proxy the
|
|
// settings, the environment or the system name (#982: a Remote magpie at
|
|
// a LAN name was asked through the Mac's proxy, which answered 502 Bad
|
|
// Gateway, and its model list never came). A proxy is for reaching
|
|
// vendors; one elsewhere can't reach the user's network, and one on this
|
|
// computer would only go there directly itself. A provider's own proxy
|
|
// (With) is still taken as it is: that one was set for it. The plugin
|
|
// host applies the same rule to plugins' requests (onLAN in
|
|
// internal/plugin/host.js, #1572): a change here is made there too.
|
|
|
|
// lanLookup resolves a name to its addresses; tests replace it.
|
|
var lanLookup = func(ctx context.Context, host string) ([]net.IPAddr, error) {
|
|
return net.DefaultResolver.LookupIPAddr(ctx, host)
|
|
}
|
|
|
|
// lanWait is how long a name is looked up for before it is taken as not
|
|
// on the network: a vendor whose name doesn't resolve here (reached only
|
|
// through the proxy) waits no longer than this, once a minute.
|
|
const lanWait = 400 * time.Millisecond
|
|
|
|
// lanKeep is how long what a name resolved to is kept.
|
|
const lanKeep = time.Minute
|
|
|
|
var lanCache = struct {
|
|
sync.Mutex
|
|
m map[string]lanSeen
|
|
}{m: map[string]lanSeen{}}
|
|
|
|
type lanSeen struct {
|
|
lan bool
|
|
at time.Time
|
|
}
|
|
|
|
// onLAN reports whether host is on the user's own network: a private,
|
|
// link-local or carrier-grade NAT (Tailscale's 100.64/10) address, a
|
|
// .local name, or a name every address of which is one of those. A name
|
|
// that doesn't resolve is not.
|
|
func onLAN(host string) bool {
|
|
host = strings.ToLower(strings.TrimSuffix(host, "."))
|
|
if host == "" {
|
|
return false
|
|
}
|
|
if ip := net.ParseIP(host); ip != nil {
|
|
return lanIP(ip)
|
|
}
|
|
if strings.HasSuffix(host, ".local") {
|
|
return true
|
|
}
|
|
lanCache.Lock()
|
|
s, ok := lanCache.m[host]
|
|
lanCache.Unlock()
|
|
if ok && time.Since(s.at) < lanKeep {
|
|
return s.lan
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), lanWait)
|
|
addrs, err := lanLookup(ctx, host)
|
|
cancel()
|
|
lan := err == nil && len(addrs) > 0
|
|
for _, a := range addrs {
|
|
if !lanIP(a.IP) {
|
|
lan = false
|
|
}
|
|
}
|
|
lanCache.Lock()
|
|
if len(lanCache.m) >= 256 {
|
|
clear(lanCache.m)
|
|
}
|
|
lanCache.m[host] = lanSeen{lan: lan, at: time.Now()}
|
|
lanCache.Unlock()
|
|
return lan
|
|
}
|
|
|
|
var cgnat = &net.IPNet{IP: net.IPv4(100, 64, 0, 0), Mask: net.CIDRMask(10, 32)}
|
|
|
|
func lanIP(ip net.IP) bool {
|
|
return ip.IsPrivate() || ip.IsLinkLocalUnicast() || ip.IsLoopback() || cgnat.Contains(ip)
|
|
}
|