Skip to content

Commit 02d291f

Browse files
fjljakub-freebit
authored andcommitted
p2p: support configuring NAT in TOML file (ethereum#31041)
This is an alternative for ethereum#27407 with a solution based on gencodec. With the PR, one can now configure like this: ``` # config.toml [Node.P2P] NAT = "extip:33.33.33.33" ``` ```shell $ geth --config config.toml ... INFO [01-17|16:37:31.436] Started P2P networking self=enode://[email protected]:30303 ```
1 parent eab1d5b commit 02d291f

File tree

7 files changed

+342
-123
lines changed

7 files changed

+342
-123
lines changed

go.mod

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/ethereum/go-ethereum
22

3-
go 1.22
3+
go 1.22.0
44

55
require (
66
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.2.0
@@ -25,7 +25,7 @@ require (
2525
github.com/ethereum/go-verkle v0.2.2
2626
github.com/fatih/color v1.16.0
2727
github.com/ferranbt/fastssz v0.1.2
28-
github.com/fjl/gencodec v0.0.0-20230517082657-f9840df7b83e
28+
github.com/fjl/gencodec v0.0.0-20250117152317-bc3e1c7619d4
2929
github.com/fsnotify/fsnotify v1.6.0
3030
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff
3131
github.com/gofrs/flock v0.8.1
@@ -62,13 +62,13 @@ require (
6262
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
6363
github.com/urfave/cli/v2 v2.25.7
6464
go.uber.org/automaxprocs v1.5.2
65-
golang.org/x/crypto v0.31.0
65+
golang.org/x/crypto v0.32.0
6666
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa
6767
golang.org/x/sync v0.10.0
68-
golang.org/x/sys v0.28.0
68+
golang.org/x/sys v0.29.0
6969
golang.org/x/text v0.21.0
7070
golang.org/x/time v0.5.0
71-
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d
71+
golang.org/x/tools v0.29.0
7272
google.golang.org/protobuf v1.34.2
7373
gopkg.in/natefinch/lumberjack.v2 v2.2.1
7474
gopkg.in/yaml.v3 v3.0.1
@@ -138,8 +138,8 @@ require (
138138
github.com/tklauser/go-sysconf v0.3.12 // indirect
139139
github.com/tklauser/numcpus v0.6.1 // indirect
140140
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
141-
golang.org/x/mod v0.17.0 // indirect
142-
golang.org/x/net v0.25.0 // indirect
141+
golang.org/x/mod v0.22.0 // indirect
142+
golang.org/x/net v0.34.0 // indirect
143143
gopkg.in/yaml.v2 v2.4.0 // indirect
144144
rsc.io/tmplfunc v0.0.3 // indirect
145145
)

go.sum

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
172172
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
173173
github.com/ferranbt/fastssz v0.1.2 h1:Dky6dXlngF6Qjc+EfDipAkE83N5I5DE68bY6O0VLNPk=
174174
github.com/ferranbt/fastssz v0.1.2/go.mod h1:X5UPrE2u1UJjxHA8X54u04SBwdAQjG2sFtWs39YxyWs=
175-
github.com/fjl/gencodec v0.0.0-20230517082657-f9840df7b83e h1:bBLctRc7kr01YGvaDfgLbTwjFNW5jdp5y5rj8XXBHfY=
176-
github.com/fjl/gencodec v0.0.0-20230517082657-f9840df7b83e/go.mod h1:AzA8Lj6YtixmJWL+wkKoBGsLWy9gFrAzi4g+5bCKwpY=
175+
github.com/fjl/gencodec v0.0.0-20250117152317-bc3e1c7619d4 h1:HhaRPDFEB1BrvBF+UCUnhyY9y5dtuBxy0myn+7sFjAw=
176+
github.com/fjl/gencodec v0.0.0-20250117152317-bc3e1c7619d4/go.mod h1:Um1dFHPONZGTHog1qD1NaWjXJW/SPB38wPv0O8uZ2fI=
177177
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
178178
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
179179
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
@@ -528,8 +528,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
528528
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
529529
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
530530
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
531-
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
532-
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
531+
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
532+
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
533533
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
534534
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
535535
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -563,8 +563,8 @@ golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzB
563563
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
564564
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
565565
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
566-
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
567-
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
566+
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
567+
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
568568
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
569569
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
570570
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -601,8 +601,8 @@ golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v
601601
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
602602
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
603603
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
604-
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
605-
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
604+
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
605+
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
606606
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
607607
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
608608
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -684,8 +684,8 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
684684
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
685685
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
686686
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
687-
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
688-
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
687+
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
688+
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
689689
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
690690
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
691691
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
@@ -750,8 +750,8 @@ golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc
750750
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
751751
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
752752
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
753-
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
754-
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
753+
golang.org/x/tools v0.29.0 h1:Xx0h3TtM9rzQpQuR4dKLrdglAmCEN5Oi+P74JdhdzXE=
754+
golang.org/x/tools v0.29.0/go.mod h1:KMQVMRsVxU6nHCFXrBPhDB8XncLNLM0lIy/F14RP588=
755755
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
756756
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
757757
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

p2p/config.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// Copyright 2025 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
package p2p
17+
18+
import (
19+
"crypto/ecdsa"
20+
"fmt"
21+
22+
"github.com/ethereum/go-ethereum/common/mclock"
23+
"github.com/ethereum/go-ethereum/log"
24+
"github.com/ethereum/go-ethereum/p2p/enode"
25+
"github.com/ethereum/go-ethereum/p2p/nat"
26+
"github.com/ethereum/go-ethereum/p2p/netutil"
27+
)
28+
29+
//go:generate go run github.com/fjl/gencodec -type Config -field-override configMarshaling -formats toml -out config_toml.go
30+
31+
// Config holds Server options.
32+
type Config struct {
33+
// This field must be set to a valid secp256k1 private key.
34+
PrivateKey *ecdsa.PrivateKey `toml:"-"`
35+
36+
// MaxPeers is the maximum number of peers that can be
37+
// connected. It must be greater than zero.
38+
MaxPeers int
39+
40+
// MaxPendingPeers is the maximum number of peers that can be pending in the
41+
// handshake phase, counted separately for inbound and outbound connections.
42+
// Zero defaults to preset values.
43+
MaxPendingPeers int `toml:",omitempty"`
44+
45+
// DialRatio controls the ratio of inbound to dialed connections.
46+
// Example: a DialRatio of 2 allows 1/2 of connections to be dialed.
47+
// Setting DialRatio to zero defaults it to 3.
48+
DialRatio int `toml:",omitempty"`
49+
50+
// NoDiscovery can be used to disable the peer discovery mechanism.
51+
// Disabling is useful for protocol debugging (manual topology).
52+
NoDiscovery bool
53+
54+
// DiscoveryV4 specifies whether V4 discovery should be started.
55+
DiscoveryV4 bool `toml:",omitempty"`
56+
57+
// DiscoveryV5 specifies whether the new topic-discovery based V5 discovery
58+
// protocol should be started or not.
59+
DiscoveryV5 bool `toml:",omitempty"`
60+
61+
// Name sets the node name of this server.
62+
Name string `toml:"-"`
63+
64+
// BootstrapNodes are used to establish connectivity
65+
// with the rest of the network.
66+
BootstrapNodes []*enode.Node
67+
68+
// BootstrapNodesV5 are used to establish connectivity
69+
// with the rest of the network using the V5 discovery
70+
// protocol.
71+
BootstrapNodesV5 []*enode.Node `toml:",omitempty"`
72+
73+
// Static nodes are used as pre-configured connections which are always
74+
// maintained and re-connected on disconnects.
75+
StaticNodes []*enode.Node
76+
77+
// Trusted nodes are used as pre-configured connections which are always
78+
// allowed to connect, even above the peer limit.
79+
TrustedNodes []*enode.Node
80+
81+
// Connectivity can be restricted to certain IP networks.
82+
// If this option is set to a non-nil value, only hosts which match one of the
83+
// IP networks contained in the list are considered.
84+
NetRestrict *netutil.Netlist `toml:",omitempty"`
85+
86+
// NodeDatabase is the path to the database containing the previously seen
87+
// live nodes in the network.
88+
NodeDatabase string `toml:",omitempty"`
89+
90+
// Protocols should contain the protocols supported
91+
// by the server. Matching protocols are launched for
92+
// each peer.
93+
Protocols []Protocol `toml:"-" json:"-"`
94+
95+
// If ListenAddr is set to a non-nil address, the server
96+
// will listen for incoming connections.
97+
//
98+
// If the port is zero, the operating system will pick a port. The
99+
// ListenAddr field will be updated with the actual address when
100+
// the server is started.
101+
ListenAddr string
102+
103+
// If DiscAddr is set to a non-nil value, the server will use ListenAddr
104+
// for TCP and DiscAddr for the UDP discovery protocol.
105+
DiscAddr string
106+
107+
// If set to a non-nil value, the given NAT port mapper
108+
// is used to make the listening port available to the
109+
// Internet.
110+
NAT nat.Interface `toml:",omitempty"`
111+
112+
// If Dialer is set to a non-nil value, the given Dialer
113+
// is used to dial outbound peer connections.
114+
Dialer NodeDialer `toml:"-"`
115+
116+
// If NoDial is true, the server will not dial any peers.
117+
NoDial bool `toml:",omitempty"`
118+
119+
// If EnableMsgEvents is set then the server will emit PeerEvents
120+
// whenever a message is sent to or received from a peer
121+
EnableMsgEvents bool
122+
123+
// Logger is a custom logger to use with the p2p.Server.
124+
Logger log.Logger `toml:"-"`
125+
126+
clock mclock.Clock
127+
}
128+
129+
type configMarshaling struct {
130+
NAT configNAT
131+
}
132+
133+
type configNAT struct {
134+
nat.Interface
135+
}
136+
137+
func (w *configNAT) UnmarshalText(input []byte) error {
138+
n, err := nat.Parse(string(input))
139+
if err != nil {
140+
return fmt.Errorf("invalid NAT specification: %v", err)
141+
}
142+
w.Interface = n
143+
return nil
144+
}

0 commit comments

Comments
 (0)