cmd/geth, p2p: add support for custom discovery UDP port (#24979)

This adds a new flag to set the discovery port to be different from
the TCP listener port.

Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
zeim839 2022-06-28 11:25:47 -04:00 committed by GitHub
parent 1438e7c324
commit 9a5c1000c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 3 deletions

View File

@ -119,6 +119,7 @@ var (
utils.CachePreimagesFlag, utils.CachePreimagesFlag,
utils.FDLimitFlag, utils.FDLimitFlag,
utils.ListenPortFlag, utils.ListenPortFlag,
utils.DiscoveryPortFlag,
utils.MaxPeersFlag, utils.MaxPeersFlag,
utils.MaxPendingPeersFlag, utils.MaxPendingPeersFlag,
utils.MiningEnabledFlag, utils.MiningEnabledFlag,

View File

@ -835,6 +835,12 @@ var (
Usage: "Sets DNS discovery entry points (use \"\" to disable DNS)", Usage: "Sets DNS discovery entry points (use \"\" to disable DNS)",
Category: flags.NetworkingCategory, Category: flags.NetworkingCategory,
} }
DiscoveryPortFlag = &cli.IntFlag{
Name: "discovery.port",
Usage: "Use a custom UDP port for P2P discovery",
Value: 30303,
Category: flags.NetworkingCategory,
}
// Console // Console
JSpathFlag = &flags.DirectoryFlag{ JSpathFlag = &flags.DirectoryFlag{
@ -1116,12 +1122,15 @@ func setBootstrapNodesV5(ctx *cli.Context, cfg *p2p.Config) {
} }
} }
// setListenAddress creates a TCP listening address string from set command // setListenAddress creates TCP/UDP listening address strings from set command
// line flags. // line flags
func setListenAddress(ctx *cli.Context, cfg *p2p.Config) { func setListenAddress(ctx *cli.Context, cfg *p2p.Config) {
if ctx.IsSet(ListenPortFlag.Name) { if ctx.IsSet(ListenPortFlag.Name) {
cfg.ListenAddr = fmt.Sprintf(":%d", ctx.Int(ListenPortFlag.Name)) cfg.ListenAddr = fmt.Sprintf(":%d", ctx.Int(ListenPortFlag.Name))
} }
if ctx.IsSet(DiscoveryPortFlag.Name) {
cfg.DiscAddr = fmt.Sprintf(":%d", ctx.Int(DiscoveryPortFlag.Name))
}
} }
// setNAT creates a port mapper from command line flags. // setNAT creates a port mapper from command line flags.

View File

@ -136,6 +136,10 @@ type Config struct {
// the server is started. // the server is started.
ListenAddr string ListenAddr string
// If DiscAddr is set to a non-nil value, the server will use ListenAddr
// for TCP and DiscAddr for the UDP discovery protocol.
DiscAddr string
// If set to a non-nil value, the given NAT port mapper // If set to a non-nil value, the given NAT port mapper
// is used to make the listening port available to the // is used to make the listening port available to the
// Internet. // Internet.
@ -549,7 +553,15 @@ func (srv *Server) setupDiscovery() error {
return nil return nil
} }
addr, err := net.ResolveUDPAddr("udp", srv.ListenAddr) listenAddr := srv.ListenAddr
// Use an alternate listening address for UDP if
// a custom discovery address is configured.
if srv.DiscAddr != "" {
listenAddr = srv.DiscAddr
}
addr, err := net.ResolveUDPAddr("udp", listenAddr)
if err != nil { if err != nil {
return err return err
} }