From 8ad6e63f8a4331d74feffffbfe12c38438258086 Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Tue, 21 Dec 2021 21:51:48 +0100 Subject: [PATCH] :wrench: Several fixes and enhancements - Enable HolePunch - Go back at legacy mdns. See referenced PR in comments for rationale - Enable NAT service throttling by default. Expose to cli options --- cmd/util.go | 50 ++++++++++++++++++++++++++++++++++++++----- go.sum | 2 ++ pkg/discovery/mdns.go | 20 +++++++++++++---- 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/cmd/util.go b/cmd/util.go index a7cbcf87..159670f9 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -45,13 +45,36 @@ var CommonFlags []cli.Flag = []cli.Flag{ EnvVar: "EDGEVPNLEDGERSYNCINTERVAL", Value: 10, }, + &cli.IntFlag{ + Name: "nat-ratelimit-global", + Usage: "Rate limit global requests", + EnvVar: "EDGEVPNNATRATELIMITGLOBAL", + Value: 10, + }, + &cli.IntFlag{ + Name: "nat-ratelimit-peer", + Usage: "Rate limit perr requests", + EnvVar: "EDGEVPNNATRATELIMITPEER", + Value: 10, + }, + &cli.IntFlag{ + Name: "nat-ratelimit-interval", + Usage: "Rate limit interval", + EnvVar: "EDGEVPNNATRATELIMITINTERVAL", + Value: 60, + }, + &cli.BoolTFlag{ + Name: "nat-ratelimit", + Usage: "Changes the default rate limiting configured in helping other peers determine their reachability status", + EnvVar: "EDGEVPNNATRATELIMIT", + }, &cli.StringFlag{ Name: "ledger-state", Usage: "Specify a ledger state directory", EnvVar: "EDGEVPNLEDGERSTATE", }, - &cli.BoolFlag{ - Name: "enable-mdns", + &cli.BoolTFlag{ + Name: "mdns", Usage: "Enable mDNS for peer discovery", EnvVar: "EDGEVPNMDNS", }, @@ -60,6 +83,11 @@ var CommonFlags []cli.Flag = []cli.Flag{ Usage: "Automatically act as a relay if the node can accept inbound connections", EnvVar: "EDGEVPNAUTORELAY", }, + &cli.BoolTFlag{ + Name: "holepunch", + Usage: "Automatically try holepunching when possible", + EnvVar: "EDGEVPNHOLEPUNCH", + }, &cli.BoolTFlag{ Name: "natservice", Usage: "Tries to determine reachability status of nodes", @@ -71,7 +99,7 @@ var CommonFlags []cli.Flag = []cli.Flag{ EnvVar: "EDGEVPNNATMAP", }, &cli.BoolTFlag{ - Name: "enable-dht", + Name: "dht", Usage: "Enable DHT for peer discovery", EnvVar: "EDGEVPNDHT", }, @@ -110,7 +138,7 @@ func cliToOpts(c *cli.Context) []edgevpn.Option { iface := c.String("interface") logLevel := c.String("log-level") libp2plogLevel := c.String("libp2p-log-level") - dht, mDNS := c.Bool("enable-dht"), c.Bool("enable-mdns") + dht, mDNS := c.Bool("dht"), c.Bool("mdns") ledgerState := c.String("ledger-state") @@ -158,12 +186,24 @@ func cliToOpts(c *cli.Context) []edgevpn.Option { edgevpn.FromYaml(mDNS, dht, config), } - libp2pOpts := []libp2p.Option{} + libp2pOpts := []libp2p.Option{libp2p.UserAgent("edgevpn")} if c.Bool("autorelay") { libp2pOpts = append(libp2pOpts, libp2p.EnableAutoRelay()) } + if c.Bool("nat-ratelimit") { + libp2pOpts = append(libp2pOpts, libp2p.AutoNATServiceRateLimit( + c.Int("nat-ratelimit-global"), + c.Int("nat-ratelimit-peer"), + time.Duration(c.Int("nat-ratelimit-interval"))*time.Second, + )) + } + + if c.Bool("holepunch") { + libp2pOpts = append(libp2pOpts, libp2p.EnableHolePunching()) + } + if c.Bool("natservice") { libp2pOpts = append(libp2pOpts, libp2p.EnableNATService()) } diff --git a/go.sum b/go.sum index 3dad58c8..eccb13de 100644 --- a/go.sum +++ b/go.sum @@ -776,6 +776,7 @@ github.com/miekg/dns v1.1.28/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7 github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI= github.com/miekg/dns v1.1.43 h1:JKfpVSCB84vrAmHzyrsxB5NAr5kLoMXZArPSw7Qlgyg= github.com/miekg/dns v1.1.43/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4= +github.com/miekg/dns v1.1.44/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME= github.com/mikioh/tcp v0.0.0-20190314235350-803a9b46060c h1:bzE/A84HN25pxAuk9Eej1Kz9OUelF97nAc82bDquQI8= github.com/mikioh/tcp v0.0.0-20190314235350-803a9b46060c/go.mod h1:0SQS9kMwD2VsyFEB++InYyBJroV/FRmBgcydeSUcJms= github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b h1:z78hV3sbSMAUoyUMM0I83AUIT6Hu17AWfgjzIbtrYFc= @@ -1095,6 +1096,7 @@ github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1/go.mod h github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM= github.com/whyrusleeping/go-logging v0.0.1/go.mod h1:lDPYj54zutzG1XYfHAhcc7oNXEburHQBn+Iqd4yS4vE= github.com/whyrusleeping/mafmt v1.2.8/go.mod h1:faQJFPbLSxzD9xpA02ttW/tS9vZykNvXwGvqIpk20FA= +github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9 h1:Y1/FEOpaCpD21WxrmfeIYCFPuVPRCY2XZTWzTNHGw30= github.com/whyrusleeping/mdns v0.0.0-20190826153040-b9b60ed33aa9/go.mod h1:j4l84WPFclQPj320J9gp0XwNKBb3U0zt5CBqjPp22G4= github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 h1:E9S12nwJwEOXe2d6gT6qxdvqMnNq+VnSsKPgm2ZZNds= github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7/go.mod h1:X2c0RVCI1eSUFI8eLcY3c0423ykwiUdxLJtkDvruhjI= diff --git a/pkg/discovery/mdns.go b/pkg/discovery/mdns.go index c1b87d26..b3830904 100644 --- a/pkg/discovery/mdns.go +++ b/pkg/discovery/mdns.go @@ -2,13 +2,14 @@ package discovery import ( "context" + "time" "github.com/ipfs/go-log" "github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/peer" - mdns "github.com/libp2p/go-libp2p/p2p/discovery/mdns" + mdns "github.com/libp2p/go-libp2p/p2p/discovery/mdns_legacy" ) type MDNS struct { @@ -37,9 +38,20 @@ func (d *MDNS) Option(ctx context.Context) func(c *libp2p.Config) error { } func (d *MDNS) Run(l log.StandardLogger, ctx context.Context, host host.Host) error { - // setup mDNS discovery to find local peers - disc := mdns.NewMdnsService(host, d.DiscoveryServiceTag, &discoveryNotifee{h: host, c: l}) + // XXX: Valid for new mdns + // disc := mdns.NewMdnsService(host, d.DiscoveryServiceTag, &discoveryNotifee{h: host, c: l}) + // return disc.Start() + // We stick to legacy atm as mdns 0.15 is kinda of broken + // see: https://github.com/libp2p/go-libp2p/pull/1192 + disc, err := mdns.NewMdnsService(ctx, host, time.Hour, d.DiscoveryServiceTag) + if err != nil { + return err + } + + n := &discoveryNotifee{h: host, c: l} + + disc.RegisterNotifee(n) - return disc.Start() + return nil }