diff --git a/internal/netproxy/bridge.go b/internal/netproxy/bridge.go index 09991bf5d..5b6ed1900 100644 --- a/internal/netproxy/bridge.go +++ b/internal/netproxy/bridge.go @@ -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) { diff --git a/internal/netproxy/bridge_test.go b/internal/netproxy/bridge_test.go index c1087e2fe..ccc215b4d 100644 --- a/internal/netproxy/bridge_test.go +++ b/internal/netproxy/bridge_test.go @@ -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) {