Skip to content

Commit 4cf1f96

Browse files
committed
p2p: add --use-proxy flag for SOCKS5 proxy support
Add support for routing P2P connections through a SOCKS5 proxy specified via ALL_PROXY or all_proxy environment variables. This enables nodes to connect to peers through proxy servers when needed. Key changes: - Add --use-proxy CLI flag to enable proxy detection - Implement proxy dialing with context cancellation support - Add debug logging to track proxy usage - Prevent goroutine leaks on context cancellation The implementation uses golang.org/x/net/proxy.FromEnvironment() which detects SOCKS5 proxies from ALL_PROXY/all_proxy environment variables. Signed-off-by: Cloorc <wittcnezh@foxmail.com> Signed-off-by: cloorc <wittcnezh@foxmail.com>
1 parent 4185df4 commit 4cf1f96

6 files changed

Lines changed: 34 additions & 6 deletions

File tree

cmd/geth/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ var (
144144
utils.NoDiscoverFlag,
145145
utils.DiscoveryV4Flag,
146146
utils.DiscoveryV5Flag,
147+
utils.UseProxyFlag,
147148
utils.LegacyDiscoveryV5Flag, // deprecated
148149
utils.NetrestrictFlag,
149150
utils.NodeKeyFileFlag,

cmd/utils/flags.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,11 @@ var (
941941
Usage: "Enables the experimental RLPx V5 (Topic Discovery) mechanism",
942942
Category: flags.NetworkingCategory,
943943
}
944+
UseProxyFlag = &cli.BoolFlag{
945+
Name: "use-proxy",
946+
Usage: "Use SOCKS5 proxy from ALL_PROXY or all_proxy environment variable for peer connections",
947+
Category: flags.NetworkingCategory,
948+
}
944949
NetrestrictFlag = &cli.StringFlag{
945950
Name: "netrestrict",
946951
Usage: "Restricts network communication to the given IP networks (CIDR masks)",
@@ -1552,6 +1557,7 @@ func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config) {
15521557
CheckExclusive(ctx, DiscoveryV5Flag, NoDiscoverFlag)
15531558
cfg.DiscoveryV4 = ctx.Bool(DiscoveryV4Flag.Name)
15541559
cfg.DiscoveryV5 = ctx.Bool(DiscoveryV5Flag.Name)
1560+
cfg.UseProxy = ctx.Bool(UseProxyFlag.Name)
15551561

15561562
if netrestrict := ctx.String(NetrestrictFlag.Name); netrestrict != "" {
15571563
list, err := netutil.ParseNetlist(netrestrict)

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ require (
6767
github.com/spf13/cobra v1.5.0
6868
github.com/status-im/keycard-go v0.2.0
6969
github.com/stretchr/testify v1.8.4
70-
github.com/supranational/blst v0.3.11
70+
github.com/supranational/blst v0.3.14
7171
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
7272
github.com/tidwall/gjson v1.6.0
7373
github.com/tyler-smith/go-bip39 v1.1.0
@@ -76,6 +76,7 @@ require (
7676
go.uber.org/automaxprocs v1.5.2
7777
golang.org/x/crypto v0.17.0
7878
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa
79+
golang.org/x/net v0.18.0
7980
golang.org/x/sync v0.5.0
8081
golang.org/x/sys v0.16.0
8182
golang.org/x/text v0.14.0
@@ -177,7 +178,6 @@ require (
177178
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
178179
golang.org/x/image v0.11.0 // indirect
179180
golang.org/x/mod v0.14.0 // indirect
180-
golang.org/x/net v0.18.0 // indirect
181181
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
182182
google.golang.org/protobuf v1.31.0 // indirect
183183
gopkg.in/yaml.v2 v2.4.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
662662
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
663663
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
664664
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
665-
github.com/supranational/blst v0.3.11 h1:LyU6FolezeWAhvQk0k6O/d49jqgO52MSDDfYgbeoEm4=
666-
github.com/supranational/blst v0.3.11/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
665+
github.com/supranational/blst v0.3.14 h1:xNMoHRJOTwMn63ip6qoWJ2Ymgvj7E2b9jY2FAwY+qRo=
666+
github.com/supranational/blst v0.3.14/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
667667
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY=
668668
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc=
669669
github.com/tidwall/gjson v1.6.0 h1:9VEQWz6LLMUsUl6PueE49ir4Ka6CzLymOAZDxpFsTDc=

p2p/dial.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/ethereum/go-ethereum/log"
3333
"github.com/ethereum/go-ethereum/p2p/enode"
3434
"github.com/ethereum/go-ethereum/p2p/netutil"
35+
"golang.org/x/net/proxy"
3536
)
3637

3738
const (
@@ -61,11 +62,28 @@ type nodeResolver interface {
6162

6263
// tcpDialer implements NodeDialer using real TCP connections.
6364
type tcpDialer struct {
64-
d *net.Dialer
65+
d *net.Dialer
66+
useProxy bool
6567
}
6668

69+
// dialerWithContext is an interface implemented by proxy dialers that support context-aware dialing.
70+
// see proxy/direct#direct, proxy.SOCKS5() and internal/socks#Dialer.
71+
type dialerWithContext interface {
72+
DialContext(ctx context.Context, network, address string) (net.Conn, error)
73+
}
74+
75+
var proxyDialer = proxy.FromEnvironment()
76+
6777
func (t tcpDialer) Dial(ctx context.Context, dest *enode.Node) (net.Conn, error) {
6878
addr, _ := dest.TCPEndpoint()
79+
if t.useProxy {
80+
log.Debug("Dialing peer via proxy", "direct", proxyDialer == proxy.Direct, "addr", addr.String())
81+
if v, ok := proxyDialer.(dialerWithContext); ok {
82+
return v.DialContext(ctx, "tcp", addr.String())
83+
} else {
84+
log.Warn("Proxy dialer does not support context, falling back to direct", "addr", addr.String())
85+
}
86+
}
6987
return t.d.DialContext(ctx, "tcp", addr.String())
7088
}
7189

p2p/server.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ type Config struct {
166166
// Logger is a custom logger to use with the p2p.Server.
167167
Logger log.Logger `toml:",omitempty"`
168168

169+
// If UseProxy is set to true, the server will use SOCKS5 proxy from ALL_PROXY or all_proxy environment variable for dialing peers.
170+
UseProxy bool `toml:",omitempty"`
171+
169172
clock mclock.Clock
170173
}
171174

@@ -617,7 +620,7 @@ func (srv *Server) setupDialScheduler() {
617620
config.resolver = srv.discv4
618621
}
619622
if config.dialer == nil {
620-
config.dialer = tcpDialer{&net.Dialer{Timeout: defaultDialTimeout}}
623+
config.dialer = tcpDialer{&net.Dialer{Timeout: defaultDialTimeout}, srv.UseProxy}
621624
}
622625
srv.dialsched = newDialScheduler(config, srv.discmix, srv.SetupConn)
623626
for _, n := range srv.StaticNodes {

0 commit comments

Comments
 (0)