Skip to content

Commit

Permalink
🔧 Several fixes and enhancements
Browse files Browse the repository at this point in the history
- Enable HolePunch
- Go back at legacy mdns. See referenced PR in comments for rationale
- Enable NAT service throttling by default. Expose to cli options
  • Loading branch information
mudler committed Dec 21, 2021
1 parent 04b4eca commit 8ad6e63
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
50 changes: 45 additions & 5 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand All @@ -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",
Expand All @@ -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",
},
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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())
}
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
20 changes: 16 additions & 4 deletions pkg/discovery/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 8ad6e63

Please sign in to comment.