netproxy: a bridge handed its SOCKS proxy's just-freed port listens elsewhere, not carrying to itself (TestPluginSOCKSProxy's 60s CI timeouts)

The plugin host gives Bun an HTTP bridge to a SOCKS proxy, listening on
127.0.0.1:0. A port just let go can be handed out again at once, so a
bridge to a SOCKS proxy on loopback that is down could be given that very
port. It then dialled itself, and bridgeConn read the SOCKS greeting as an
HTTP request under its one-minute deadline: every request through it hung
60s instead of failing at once. That is TestPluginSOCKSProxy's 60s timeouts
on ubuntu CI (closedPort frees a port, then the bridge listens), rerun as
load before. A user whose local SOCKS proxy is down could meet it too.

Bridge now listens again while still holding such a port (up to 3 times),
then lets the first go.

Run:
- Injected the reuse (listenLoopback handing out closedPort's port): before
  the fix internal/plugin's SOCKS test failed after 60.03s with "context
  deadline exceeded"; with it, the package passed in 2.4s.
- New TestBridgeNeverCarriesToItself (addr and localhost:<port>): red with
  the loop taken out by hand, green with it.
- go test -race -count=20 -run TestBridge ./internal/netproxy: ok.
- go vet darwin, GOOS=linux -tags nogui, GOOS=windows: clean. gofmt -l clean.
- go test -tags nogui -count=1 -p 4 -timeout 45m ./... at d0ccbbebc + this:
  58 ok; internal/gateway red only in TestMaxRPMCountsRetriesAndCounts
  ("a count with no room: 200 after 2.72s"), the open #1791 under load.

Not tried: the Windows box.
This commit is contained in:
yetone
2026-10-15 11:42:47 +08:00
parent d0ccbbebc6
commit 9a852ae9db
2 changed files with 76 additions and 1 deletions
+35 -1
View File
@@ -39,10 +39,26 @@ func Bridge(socks *url.URL) (string, error) {
if a, ok := bridges[key]; ok {
return a, nil
}
ln, err := net.Listen("tcp", "127.0.0.1:0")
ln, err := listenLoopback()
if err != nil {
return "", err
}
// A port just let go can be handed out again at once. A bridge given
// the port of the SOCKS proxy it carries to (one on loopback that is
// down) would dial itself and hold each request until its deadline, so
// it listens again while still holding that port.
for tries := 0; carriesToItself(ln.Addr(), socks); tries++ {
held := ln
if tries == 3 {
held.Close()
return "", fmt.Errorf("no loopback port apart from the SOCKS proxy's %s", socks.Host)
}
ln, err = listenLoopback()
held.Close()
if err != nil {
return "", err
}
}
s := *socks
go func() {
for {
@@ -58,6 +74,24 @@ func Bridge(socks *url.URL) (string, error) {
return a, nil
}
// listenLoopback is where a bridge listens; a test hands out a chosen port.
var listenLoopback = func() (net.Listener, error) { return net.Listen("tcp", "127.0.0.1:0") }
// carriesToItself is whether a bridge at a would carry to its own address:
// the SOCKS proxy is on this computer at the same port.
func carriesToItself(a net.Addr, socks *url.URL) bool {
ta, ok := a.(*net.TCPAddr)
if !ok || socks.Port() != strconv.Itoa(ta.Port) {
return false
}
h := socks.Hostname()
if h == "localhost" {
return true
}
ip := net.ParseIP(h)
return ip != nil && (ip.IsLoopback() || ip.IsUnspecified())
}
// bridgeConn serves one client of a bridge: a CONNECT is tunnelled, a
// plain http:// request sent on and answered.
func bridgeConn(c net.Conn, socks *url.URL) {
+41
View File
@@ -14,6 +14,7 @@ import (
"strings"
"sync/atomic"
"testing"
"time"
)
// fakeSOCKS is a SOCKS5 proxy taking user u and password pw ("" for none),
@@ -165,6 +166,46 @@ func TestBridgeSaysTheProxyIsDown(t *testing.T) {
}
}
// A bridge handed the port of the SOCKS proxy it carries to, which a port
// just let go can be (Linux CI gave TestPluginSOCKSProxy's dead proxy port
// to its bridge), listens elsewhere: carrying to itself, it held each
// request for a minute instead of saying the proxy is down.
func TestBridgeNeverCarriesToItself(t *testing.T) {
ln, _ := net.Listen("tcp", "127.0.0.1:0")
addr := ln.Addr().String()
ln.Close()
first := true
listenLoopback = func() (net.Listener, error) {
if first {
first = false
return net.Listen("tcp", addr)
}
return net.Listen("tcp", "127.0.0.1:0")
}
t.Cleanup(func() { listenLoopback = func() (net.Listener, error) { return net.Listen("tcp", "127.0.0.1:0") } })
for _, host := range []string{addr, "localhost:" + strings.Split(addr, ":")[1]} {
b, err := Bridge(&url.URL{Scheme: "socks5h", Host: host})
if err != nil {
t.Fatal(err)
}
if strings.TrimPrefix(b, "http://") == addr {
t.Fatalf("the bridge for %s listens at %s, the proxy's own port", host, b)
}
c, err := net.Dial("tcp", strings.TrimPrefix(b, "http://"))
if err != nil {
t.Fatal(err)
}
c.SetDeadline(time.Now().Add(10 * time.Second))
io.WriteString(c, "CONNECT example.com:443 HTTP/1.1\r\nHost: example.com:443\r\n\r\n")
got, _ := io.ReadAll(c)
c.Close()
if !strings.HasPrefix(string(got), "HTTP/1.1 502") {
t.Fatalf("through %s: answered %q", host, got)
}
first = true
}
}
// Bun itself, the plugin host's runtime, fetches through the bridge: run
// when a bun is at hand ($MAGPIE_BUN or on PATH).
func TestBunThroughBridge(t *testing.T) {