multi: replace btcsuite/go-socks with an x/net/proxy wrapper
What changed, and why it matters
This commit swaps out an old, unmaintained SOCKS proxy library for a new internal wrapper around a modern Go package. The change is mostly a refactor, but it deliberately fixes several subtle SOCKS/Tor-related behaviors that could affect security: peer whitelisting, stream isolation, hostname handling, and handshake timeouts. The commit message and tests describe these as behavior-preserving fixes rather than as a response to a reported vulnerability.
Treat as a hardening/maintenance change with latent security implications. Review the new internal/socks implementation for correct context propagation and error handling, verify that all SOCKS/Tor configurations still work, and consider whether the old behavior could have allowed unintended whitelist bypasses in deployments using Tor. No emergency patch is indicated absent a specific advisory.
Security signals we found
Replacement of outdated/unmaintained SOCKS dependency
Fix for peer whitelisting bypass via proxy RemoteAddr semantics
Per-connection random Tor stream-isolation credentials
Correct handling of long proxy-bound hostnames that previously panicked
SOCKS handshake now covered by dial timeout/context
Hostname/onion addresses passed unresolved to proxy
Evidence from the diff
btcd replaced github.com/btcsuite/go-socks with a new internal/socks package wrapping golang.org/x/net/proxy. The wrapper preserves the API btcd relied on and fixes edge cases: (1) proxied connections now report the requested destination as RemoteAddr so outboundPeerConnected’s whitelist check applies to the peer, not the proxy; (2) LocalAddr reports the proxy-bound address, including long hostnames that previously panicked; (3) Tor stream isolation uses fresh random credentials per connection; (4) hostnames/onion addresses are passed unresolved to the proxy; (5) timeouts now cover the SOCKS handshake, and rpcclient websocket dials use NetDialContext with context. The commit includes unit tests for all of these behaviors and an end-to-end simnet test.
Changed components
internal/socks (new SOCKS5 wrapper)cmd/btcctl/httpclient.goconfig.gopeer/peer.gorpcclient/infrastructure.goserver_test.goInspect captured patch +545 / −11
### cmd/btcctl/httpclient.go
@@ -12,7 +12,7 @@ import (
"os"
"github.com/btcsuite/btcd/btcjson"
- "github.com/btcsuite/go-socks/socks"
+ "github.com/btcsuite/btcd/internal/socks"
)
// newHTTPClient returns a new HTTP client that is configured according to the
### config.go
@@ -30,10 +30,10 @@ import (
"github.com/btcsuite/btcd/connmgr"
"github.com/btcsuite/btcd/database"
_ "github.com/btcsuite/btcd/database/ffldb"
+ "github.com/btcsuite/btcd/internal/socks"
"github.com/btcsuite/btcd/mempool"
"github.com/btcsuite/btcd/peer"
"github.com/btcsuite/btcd/wire/v2"
- "github.com/btcsuite/go-socks/socks"
flags "github.com/jessevdk/go-flags"
)
### go.mod
@@ -12,7 +12,6 @@ require (
github.com/btcsuite/btcd/v2transport v1.1.0
github.com/btcsuite/btcd/wire/v2 v2.0.1
github.com/btcsuite/btclog v1.0.0
- github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd
github.com/davecgh/go-spew v1.1.1
github.com/decred/dcrd/lru v1.1.3
github.com/gorilla/websocket v1.5.3
@@ -21,6 +20,7 @@ require (
github.com/stretchr/testify v1.12.1
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d
golang.org/x/crypto v0.57.0
+ golang.org/x/net v0.60.0
golang.org/x/sys v0.48.0
golang.org/x/time v0.16.0
pgregory.net/rapid v1.3.0
### go.sum
@@ -18,8 +18,6 @@ github.com/btcsuite/btcd/wire/v2 v2.0.1 h1:edmb35tvRyQpFp331L9PwZFccaTCOQwevSsU6
github.com/btcsuite/btcd/wire/v2 v2.0.1/go.mod h1:ENBpJL0JYUNlqvajhIFTOcWGNNumjCynDk4PL0OG58I=
github.com/btcsuite/btclog v1.0.0 h1:sEkpKJMmfGiyZjADwEIgB1NSwMyfdD1FB8v6+w1T0Ns=
github.com/btcsuite/btclog v1.0.0/go.mod h1:w7xnGOhwT3lmrS4H3b/D1XAXxvh+tbhUm8xeHN2y3TQ=
-github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd h1:R/opQEbFEy9JGkIguV40SvRY1uliPX8ifOvi6ICsFCw=
-github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
@@ -107,8 +105,8 @@ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwY
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220607020251-c690dde0001d/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
-golang.org/x/net v0.58.0 h1:ynWG7rqYi4ccpTEuPZ2QGWHktVEM9DMCj9yzDE0Q7To=
-golang.org/x/net v0.58.0/go.mod h1:YwCddHnFlT7eLQqVprV19OnhLGtc5xOKgE0RyqgfWAU=
+golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU=
+golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
### internal/socks/socks.go
@@ -0,0 +1,184 @@
+// Package socks dials connections through a SOCKS5 proxy such as Tor.
+//
+// It wraps golang.org/x/net/proxy and keeps the behavior btcd relied on from
+// github.com/btcsuite/go-socks:
+//
+// - A connection reports the requested destination as its remote address,
+// and the address the proxy bound for it as its local address, rather
+// than the addresses of the connection to the proxy.
+// - With Tor stream isolation, every connection authenticates with new
+// random credentials, so Tor puts it on a circuit of its own.
+// - Destinations are sent to the proxy unresolved, so the proxy resolves
+// hostnames, including onion addresses.
+package socks
+
+import (
+ "context"
+ "crypto/rand"
+ "encoding/hex"
+ "net"
+ "strconv"
+ "time"
+
+ "golang.org/x/net/proxy"
+)
+
+// Proxy dials connections through a SOCKS5 proxy.
+type Proxy struct {
+ // Addr is the host:port address of the proxy.
+ Addr string
+
+ // Username and Password are the credentials for the proxy, if it
+ // requires them. They are ignored when TorIsolation is set.
+ Username string
+ Password string
+
+ // TorIsolation makes every connection authenticate with new random
+ // credentials, which Tor uses to isolate it on a circuit of its own.
+ TorIsolation bool
+}
+
+// ProxiedAddr is the address of a connection made through a proxy. Host may
+// be a hostname, such as an onion address, since the proxy resolves it.
+type ProxiedAddr struct {
+ Net string
+ Host string
+ Port int
+}
+
+// Network returns the network of the address, such as "tcp".
+func (a *ProxiedAddr) Network() string {
+ return a.Net
+}
+
+// String returns the address in host:port form.
+func (a *ProxiedAddr) String() string {
+ return net.JoinHostPort(a.Host, strconv.Itoa(a.Port))
+}
+
+// Dial connects to addr through the proxy.
+func (p *Proxy) Dial(network, addr string) (net.Conn, error) {
+ return p.DialContext(context.Background(), network, addr)
+}
+
+// DialTimeout is like Dial, but gives up after timeout, which covers both
+// connecting to the proxy and the SOCKS handshake. A zero timeout means no
+// timeout.
+func (p *Proxy) DialTimeout(network, addr string,
+ timeout time.Duration) (net.Conn, error) {
+
+ ctx := context.Background()
+ if timeout > 0 {
+ var cancel context.CancelFunc
+ ctx, cancel = context.WithTimeout(ctx, timeout)
+ defer cancel()
+ }
+
+ return p.DialContext(ctx, network, addr)
+}
+
+// DialContext connects to addr through the proxy. The network is "tcp",
+// "tcp4", "tcp6" or "onion", which btcd uses for onion addresses. The context
+// covers both connecting to the proxy and the SOCKS handshake.
+func (p *Proxy) DialContext(ctx context.Context, network,
+ addr string) (net.Conn, error) {
+
+ host, portStr, err := net.SplitHostPort(addr)
+ if err != nil {
+ return nil, err
+ }
+ port, err := strconv.Atoi(portStr)
+ if err != nil {
+ return nil, err
+ }
+
+ auth, err := p.auth()
+ if err != nil {
+ return nil, err
+ }
+ dialer, err := proxy.SOCKS5("tcp", p.Addr, auth, proxy.Direct)
+ if err != nil {
+ return nil, err
+ }
+
+ // x/net doesn't know the "onion" network. The proxy connects to onion
+ // addresses over TCP like to any other host.
+ proxyNetwork := network
+ if network == "onion" {
+ proxyNetwork = "tcp"
+ }
+
+ // The SOCKS5 dialer implements DialContext, which, unlike its Dial,
+ // returns a connection that knows the address the proxy bound.
+ conn, err := dialer.(proxy.ContextDialer).DialContext(
+ ctx, proxyNetwork, addr,
+ )
+ if err != nil {
+ return nil, err
+ }
+
+ var boundAddr net.Addr
+ if c, ok := conn.(interface{ BoundAddr() net.Addr }); ok {
+ boundAddr = c.BoundAddr()
+ }
+
+ return &proxiedConn{
+ Conn: conn,
+ boundAddr: boundAddr,
+ remoteAddr: &ProxiedAddr{
+ Net: network,
+ Host: host,
+ Port: port,
+ },
+ }, nil
+}
+
+// auth returns the credentials to authenticate with the proxy, or nil to not
+// authenticate.
+func (p *Proxy) auth() (*proxy.Auth, error) {
+ if p.TorIsolation {
+ var b [16]byte
+ if _, err := rand.Read(b[:]); err != nil {
+ return nil, err
+ }
+
+ return &proxy.Auth{
+ User: hex.EncodeToString(b[:8]),
+ Password: hex.EncodeToString(b[8:]),
+ }, nil
+ }
+
+ // SOCKS5 username/password authentication needs a username.
+ if p.Username == "" {
+ return nil, nil
+ }
+
+ return &proxy.Auth{User: p.Username, Password: p.Password}, nil
+}
+
+// proxiedConn is a connection made through a proxy.
+type proxiedConn struct {
+ net.Conn
+
+ // boundAddr is the address the proxy bound for the connection, if it
+ // reported one.
+ boundAddr net.Addr
+
+ // remoteAddr is the requested destination.
+ remoteAddr *ProxiedAddr
+}
+
+// LocalAddr returns the address the proxy bound for the connection, or the
+// local address of the connection to the proxy if it didn't report one.
+func (c *proxiedConn) LocalAddr() net.Addr {
+ if c.boundAddr != nil {
+ return c.boundAddr
+ }
+
+ return c.Conn.LocalAddr()
+}
+
+// RemoteAddr returns the requested destination.
+func (c *proxiedConn) RemoteAddr() net.Addr {
+ return c.remoteAddr
+}
### internal/socks/socks_test.go
@@ -0,0 +1,326 @@
+package socks
+
+import (
+ "bytes"
+ "encoding/binary"
+ "io"
+ "net"
+ "strconv"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/require"
+)
+
+// testTimeout bounds how long a test waits for the test proxy.
+const testTimeout = 5 * time.Second
+
+// proxyRequest is what a client sent to the test proxy.
+type proxyRequest struct {
+ // user and pass are the credentials the client authenticated with, if
+ // any.
+ user, pass string
+
+ // addrType is the SOCKS address type of the destination.
+ addrType byte
+
+ // host and port are the requested destination.
+ host string
+ port int
+}
+
+// successReply is the reply of a SOCKS5 proxy that connected to the
+// destination and reports 0.0.0.0:0 as the bound address, as Tor does.
+var successReply = []byte{5, 0, 0, 1, 0, 0, 0, 0, 0, 0}
+
+// startProxy runs a minimal SOCKS5 proxy that sends reply to every connect
+// request, then keeps the connection open. It returns the proxy address and a
+// channel that receives each request.
+func startProxy(t *testing.T, reply []byte) (string, chan proxyRequest) {
+ t.Helper()
+
+ ln, err := net.Listen("tcp", "127.0.0.1:0")
+ require.NoError(t, err)
+ t.Cleanup(func() {
+ _ = ln.Close()
+ })
+
+ requests := make(chan proxyRequest, 10)
+ go func() {
+ for {
+ conn, err := ln.Accept()
+ if err != nil {
+ return
+ }
+ go func() {
+ defer conn.Close()
+
+ req, err := readRequest(conn)
+ if err != nil {
+ return
+ }
+ requests <- req
+ if _, err := conn.Write(reply); err != nil {
+ return
+ }
+ _, _ = io.Copy(io.Discard, conn)
+ }()
+ }
+ }()
+
+ return ln.Addr().String(), requests
+}
+
+// readRequest reads the method negotiation, the optional username/password
+// authentication and the connect request of a SOCKS5 client.
+func readRequest(conn net.Conn) (proxyRequest, error) {
+ var req proxyRequest
+
+ // Greeting: version, number of methods, methods.
+ header := make([]byte, 2)
+ if _, err := io.ReadFull(conn, header); err != nil {
+ return req, err
+ }
+ methods := make([]byte, header[1])
+ if _, err := io.ReadFull(conn, methods); err != nil {
+ return req, err
+ }
+
+ // Pick username/password authentication when offered.
+ if !bytes.Contains(methods, []byte{2}) {
+ if _, err := conn.Write([]byte{5, 0}); err != nil {
+ return req, err
+ }
+ } else {
+ if _, err := conn.Write([]byte{5, 2}); err != nil {
+ return req, err
+ }
+ user, pass, err := readCredentials(conn)
+ if err != nil {
+ return req, err
+ }
+ req.user, req.pass = user, pass
+ if _, err := conn.Write([]byte{1, 0}); err != nil {
+ return req, err
+ }
+ }
+
+ // Request: version, command, reserved, address type, address, port.
+ head := make([]byte, 4)
+ if _, err := io.ReadFull(conn, head); err != nil {
+ return req, err
+ }
+ req.addrType = head[3]
+
+ var addr []byte
+ switch req.addrType {
+ case 1:
+ addr = make([]byte, net.IPv4len)
+ case 4:
+ addr = make([]byte, net.IPv6len)
+ case 3:
+ length := make([]byte, 1)
+ if _, err := io.ReadFull(conn, length); err != nil {
+ return req, err
+ }
+ addr = make([]byte, length[0])
+ }
+ if _, err := io.ReadFull(conn, addr); err != nil {
+ return req, err
+ }
+ if req.addrType == 3 {
+ req.host = string(addr)
+ } else {
+ req.host = net.IP(addr).String()
+ }
+
+ port := make([]byte, 2)
+ if _, err := io.ReadFull(conn, port); err != nil {
+ return req, err
+ }
+ req.port = int(binary.BigEndian.Uint16(port))
+
+ return req, nil
+}
+
+// readCredentials reads a username/password authentication request.
+func readCredentials(conn net.Conn) (string, string, error) {
+ var b [1]byte
+ field := func() (string, error) {
+ if _, err := io.ReadFull(conn, b[:]); err != nil {
+ return "", err
+ }
+ value := make([]byte, b[0])
+ _, err := io.ReadFull(conn, value)
+
+ return string(value), err
+ }
+
+ // Version of the subnegotiation.
+ if _, err := io.ReadFull(conn, b[:]); err != nil {
+ return "", "", err
+ }
+ user, err := field()
+ if err != nil {
+ return "", "", err
+ }
+ pass, err := field()
+
+ return user, pass, err
+}
+
+// receive returns the next request the test proxy received.
+func receive(t *testing.T, requests chan proxyRequest) proxyRequest {
+ t.Helper()
+
+ select {
+ case req := <-requests:
+ return req
+
+ case <-time.After(testTimeout):
+ t.Fatal("the proxy received no request")
+ return proxyRequest{}
+ }
+}
+
+// dial dials addr through p and closes the connection when the test ends.
+func dial(t *testing.T, p *Proxy, addr string) net.Conn {
+ t.Helper()
+
+ conn, err := p.DialTimeout("tcp", addr, testTimeout)
+ require.NoError(t, err)
+ t.Cleanup(func() {
+ _ = conn.Close()
+ })
+
+ return conn
+}
+
+// TestDialAddresses checks that a connection reports the destination as its
+// remote address and the address bound by the proxy as its local address.
+func TestDialAddresses(t *testing.T) {
+ t.Parallel()
+
+ proxyAddr, requests := startProxy(t, successReply)
+ conn := dial(t, &Proxy{Addr: proxyAddr}, "1.2.3.4:8333")
+
+ req := receive(t, requests)
+ require.Equal(t, byte(1), req.addrType)
+ require.Equal(t, "1.2.3.4", req.host)
+ require.Equal(t, 8333, req.port)
+ require.Empty(t, req.user)
+
+ require.Equal(t, &ProxiedAddr{Net: "tcp", Host: "1.2.3.4", Port: 8333},
+ conn.RemoteAddr())
+ require.Equal(t, "0.0.0.0:0", conn.LocalAddr().String())
+}
+
+// TestDialHostname checks that a hostname is sent to the proxy unresolved and
+// reported as the remote address, also with the "onion" network, which btcd
+// uses to dial onion addresses.
+func TestDialHostname(t *testing.T) {
+ t.Parallel()
+
+ host := "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopnpyyd.onion"
+ addr := net.JoinHostPort(host, "8333")
+
+ for _, network := range []string{"tcp", "onion"} {
+ proxyAddr, requests := startProxy(t, successReply)
+ p := &Proxy{Addr: proxyAddr}
+ conn, err := p.DialTimeout(network, addr, testTimeout)
+ require.NoError(t, err, network)
+ t.Cleanup(func() {
+ _ = conn.Close()
+ })
+
+ req := receive(t, requests)
+ require.Equal(t, byte(3), req.addrType, network)
+ require.Equal(t, host, req.host, network)
+ want := &ProxiedAddr{Net: network, Host: host, Port: 8333}
+ require.Equal(t, want, conn.RemoteAddr())
+ }
+}
+
+// TestDialCredentials checks that the configured credentials are sent to the
+// proxy.
+func TestDialCredentials(t *testing.T) {
+ t.Parallel()
+
+ proxyAddr, requests := startProxy(t, successReply)
+ p := &Proxy{Addr: proxyAddr, Username: "user", Password: "pass"}
+ dial(t, p, "1.2.3.4:8333")
+
+ req := receive(t, requests)
+ require.Equal(t, "user", req.user)
+ require.Equal(t, "pass", req.pass)
+}
+
+// TestDialTorIsolation checks that with Tor stream isolation, every
+// connection uses new random credentials instead of the configured ones.
+func TestDialTorIsolation(t *testing.T) {
+ t.Parallel()
+
+ proxyAddr, requests := startProxy(t, successReply)
+ p := &Proxy{
+ Addr: proxyAddr,
+ Username: "user",
+ Password: "pass",
+ TorIsolation: true,
+ }
+
+ dial(t, p, "1.2.3.4:8333")
+ first := receive(t, requests)
+ dial(t, p, "1.2.3.4:8333")
+ second := receive(t, requests)
+
+ for _, req := range []proxyRequest{first, second} {
+ require.Len(t, req.user, 16)
+ require.Len(t, req.pass, 16)
+ require.NotEqual(t, "user", req.user)
+ }
+ require.NotEqual(t, first.user, second.user)
+ require.NotEqual(t, first.pass, second.pass)
+}
+
+// TestDialLongBoundHostname checks that a long hostname as the bound address
+// in the proxy's reply is handled. btcsuite/go-socks panicked on it.
+func TestDialLongBoundHostname(t *testing.T) {
+ t.Parallel()
+
+ name := strings.Repeat("a", 255)
+ reply := append([]byte{5, 0, 0, 3, byte(len(name))}, name...)
+ reply = append(reply, 0x20, 0x8d)
+
+ proxyAddr, _ := startProxy(t, reply)
+ conn := dial(t, &Proxy{Addr: proxyAddr}, "1.2.3.4:8333")
+
+ require.Equal(t, net.JoinHostPort(name, strconv.Itoa(8333)),
+ conn.LocalAddr().String())
+}
+
+// TestDialTimeout checks that the timeout covers the SOCKS handshake, so a
+// proxy that never answers can't hang the dial.
+func TestDialTimeout(t *testing.T) {
+ t.Parallel()
+
+ ln, err := net.Listen("tcp", "127.0.0.1:0")
+ require.NoError(t, err)
+ t.Cleanup(func() {
+ _ = ln.Close()
+ })
+ go func() {
+ conn, err := ln.Accept()
+ if err != nil {
+ return
+ }
+ defer conn.Close()
+ _, _ = io.Copy(io.Discard, conn)
+ }()
+
+ p := &Proxy{Addr: ln.Addr().String()}
+ start := time.Now()
+ _, err = p.DialTimeout("tcp", "1.2.3.4:8333", 100*time.Millisecond)
+ require.Error(t, err)
+ require.Less(t, time.Since(start), testTimeout)
+}
### peer/peer.go
@@ -22,9 +22,9 @@ import (
"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/chaincfg/v2"
"github.com/btcsuite/btcd/chainhash/v2"
+ "github.com/btcsuite/btcd/internal/socks"
"github.com/btcsuite/btcd/v2transport"
"github.com/btcsuite/btcd/wire/v2"
- "github.com/btcsuite/go-socks/socks"
"github.com/davecgh/go-spew/spew"
"github.com/decred/dcrd/lru"
)
### peer/peer_test.go
@@ -17,9 +17,9 @@ import (
"github.com/btcsuite/btcd/chaincfg/v2"
"github.com/btcsuite/btcd/chainhash/v2"
+ "github.com/btcsuite/btcd/internal/socks"
"github.com/btcsuite/btcd/peer"
"github.com/btcsuite/btcd/wire/v2"
- "github.com/btcsuite/go-socks/socks"
)
// testHandshakeAdmission adapts a function to the v2 handshake admission
### rpcclient/infrastructure.go
@@ -28,7 +28,7 @@ import (
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/chaincfg/v2"
- "github.com/btcsuite/go-socks/socks"
+ "github.com/btcsuite/btcd/internal/socks"
"github.com/gorilla/websocket"
)
@@ -1491,7 +1491,7 @@ func dial(config *ConnConfig) (*websocket.Conn, error) {
Username: config.ProxyUser,
Password: config.ProxyPass,
}
- dialer.NetDial = proxy.Dial
+ dialer.NetDialContext = proxy.DialContext
}
// Configure generated basic access authorization. Caller-provided
### server_test.go
@@ -11,6 +11,7 @@ import (
"github.com/btcsuite/btcd/chaincfg/v2"
"github.com/btcsuite/btcd/internal/inbound"
+ "github.com/btcsuite/btcd/internal/socks"
"github.com/btcsuite/btcd/peer"
"github.com/btcsuite/btclog"
"github.com/stretchr/testify/assert"
@@ -197,6 +198,31 @@ func TestInboundPeerReservation(t *testing.T) {
}
}
+// TestIsWhitelistedProxiedAddr checks that a peer reached through a proxy is
+// whitelisted by its own address rather than the proxy's, so whitelisting a
+// local Tor proxy's address doesn't whitelist every peer reached through it.
+// Connections made through the proxy report the peer as their remote address,
+// which is what outboundPeerConnected passes to isWhitelisted.
+func TestIsWhitelistedProxiedAddr(t *testing.T) {
+ originalCfg := cfg
+ t.Cleanup(func() {
+ cfg = originalCfg
+ })
+
+ _, loopback, err := net.ParseCIDR("127.0.0.0/8")
+ require.NoError(t, err)
+ _, peers, err := net.ParseCIDR("192.0.2.0/24")
+ require.NoError(t, err)
+
+ addr := &socks.ProxiedAddr{Net: "tcp", Host: "192.0.2.1", Port: 8333}
+
+ cfg = &config{whitelists: []*net.IPNet{loopback}}
+ require.False(t, isWhitelisted(addr))
+
+ cfg = &config{whitelists: []*net.IPNet{peers}}
+ require.True(t, isWhitelisted(addr))
+}
+
// TestInboundPeerAdmissionSourceLimits verifies that loopback and whitelisted
// peers retain the ordinary pending-handshake and V2 source limits.
func TestInboundPeerAdmissionSourceLimits(t *testing.T) {Why this scored 46/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.