-
Notifications
You must be signed in to change notification settings - Fork 21.6k
p2p: add optional SOCKS5 proxy support for outbound peer dials #33346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ import ( | |
| "github.com/ethereum/go-ethereum/p2p/enode" | ||
| "github.com/ethereum/go-ethereum/p2p/enr" | ||
| "github.com/ethereum/go-ethereum/p2p/netutil" | ||
| "golang.org/x/net/proxy" | ||
| ) | ||
|
|
||
| const ( | ||
|
|
@@ -63,11 +64,28 @@ type nodeResolver interface { | |
|
|
||
| // tcpDialer implements NodeDialer using real TCP connections. | ||
| type tcpDialer struct { | ||
| d *net.Dialer | ||
| d *net.Dialer | ||
| useProxy bool | ||
| } | ||
|
|
||
| // dialerWithContext is an interface implemented by proxy dialers that support context-aware dialing. | ||
| // see proxy/direct#direct, proxy.SOCKS5() and internal/socks#Dialer. | ||
| type dialerWithContext interface { | ||
| DialContext(ctx context.Context, network, address string) (net.Conn, error) | ||
| } | ||
|
|
||
| var proxyDialer = proxy.FromEnvironment() | ||
|
|
||
| func (t tcpDialer) Dial(ctx context.Context, dest *enode.Node) (net.Conn, error) { | ||
| addr, _ := dest.TCPEndpoint() | ||
| if t.useProxy { | ||
| log.Debug("Dialing peer via proxy", "direct", proxyDialer == proxy.Direct, "addr", addr.String()) | ||
| if v, ok := proxyDialer.(dialerWithContext); ok { | ||
| return v.DialContext(ctx, "tcp", addr.String()) | ||
| } else { | ||
| log.Warn("Proxy dialer does not support context, falling back to direct", "addr", addr.String()) | ||
| } | ||
|
Comment on lines
+85
to
+87
|
||
| } | ||
|
Comment on lines
+81
to
+88
|
||
| return t.d.DialContext(ctx, "tcp", addr.String()) | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The global variable
proxyDialeris initialized once at package level and accessed by alltcpDialerinstances. This could lead to a race condition if the environment variable changes at runtime or if different instances need different proxy configurations. Consider makingproxyDialera field oftcpDialerthat is initialized when the dialer is created, or usesync.Onceto ensure thread-safe initialization if shared state is intended.