Skip to content

Commit a0cb88e

Browse files
committed
p2p: add optional SOCKS5 proxy support for outbound peer dials
- Add `UseProxy` field to `p2p.Config` and corresponding `--use-proxy` CLI flag. - Implement proxy-aware `tcpDialer` (uses `golang.org/x/net/proxy` and attempts context-aware DialContext when supported). - Wire `UseProxy` through `Server` to the dialer so peer dials can go via SOCKS5 proxy defined by `ALL_PROXY`/`all_proxy`. - Minor debug `start.sh` for running geth with `--use-proxy`. Signed-off-by: cloorc <[email protected]>
1 parent da3822d commit a0cb88e

File tree

7 files changed

+37
-3
lines changed

7 files changed

+37
-3
lines changed

cmd/geth/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ var (
126126
utils.NoDiscoverFlag,
127127
utils.DiscoveryV4Flag,
128128
utils.DiscoveryV5Flag,
129+
utils.UseProxyFlag,
129130
utils.LegacyDiscoveryV5Flag, // deprecated
130131
utils.NetrestrictFlag,
131132
utils.NodeKeyFileFlag,

cmd/utils/flags.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,11 @@ var (
873873
Category: flags.NetworkingCategory,
874874
Value: node.DefaultConfig.P2P.DiscoveryV5,
875875
}
876+
UseProxyFlag = &cli.BoolFlag{
877+
Name: "use-proxy",
878+
Usage: "Use SOCKS5 proxy from ALL_PROXY or all_proxy environment variable for peer connections",
879+
Category: flags.NetworkingCategory,
880+
}
876881
NetrestrictFlag = &cli.StringFlag{
877882
Name: "netrestrict",
878883
Usage: "Restricts network communication to the given IP networks (CIDR masks)",
@@ -1379,6 +1384,7 @@ func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config) {
13791384
if ctx.IsSet(DiscoveryV5Flag.Name) {
13801385
cfg.DiscoveryV5 = ctx.Bool(DiscoveryV5Flag.Name)
13811386
}
1387+
cfg.UseProxy = ctx.Bool(UseProxyFlag.Name)
13821388

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

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ require (
144144
github.com/tklauser/numcpus v0.6.1 // indirect
145145
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
146146
golang.org/x/mod v0.22.0 // indirect
147-
golang.org/x/net v0.38.0 // indirect
147+
golang.org/x/net v0.38.0
148148
gopkg.in/yaml.v2 v2.4.0 // indirect
149149
)
150150

p2p/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ type Config struct {
124124
// Logger is a custom logger to use with the p2p.Server.
125125
Logger log.Logger `toml:"-"`
126126

127+
// If UseProxy is set to true, the server will use SOCKS5 proxy from ALL_PROXY or all_proxy environment variable for dialing peers.
128+
UseProxy bool `toml:",omitempty"`
129+
127130
clock mclock.Clock
128131
}
129132

p2p/config_toml.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

p2p/dial.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/ethereum/go-ethereum/p2p/enode"
3535
"github.com/ethereum/go-ethereum/p2p/enr"
3636
"github.com/ethereum/go-ethereum/p2p/netutil"
37+
"golang.org/x/net/proxy"
3738
)
3839

3940
const (
@@ -63,11 +64,28 @@ type nodeResolver interface {
6364

6465
// tcpDialer implements NodeDialer using real TCP connections.
6566
type tcpDialer struct {
66-
d *net.Dialer
67+
d *net.Dialer
68+
useProxy bool
6769
}
6870

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

p2p/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ func (srv *Server) setupDialScheduler() {
537537
config.resolver = srv.discv4
538538
}
539539
if config.dialer == nil {
540-
config.dialer = tcpDialer{&net.Dialer{Timeout: defaultDialTimeout}}
540+
config.dialer = tcpDialer{&net.Dialer{Timeout: defaultDialTimeout}, srv.UseProxy}
541541
}
542542
srv.dialsched = newDialScheduler(config, srv.discmix, srv.SetupConn)
543543
for _, n := range srv.StaticNodes {

0 commit comments

Comments
 (0)