Skip to content

Commit

Permalink
⚙️ Enable low-profile mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mudler committed Jan 26, 2022
1 parent 10a7b08 commit 0042928
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
24 changes: 21 additions & 3 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (

"github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p"
connmanager "github.com/libp2p/go-libp2p-connmgr"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/mudler/edgevpn/internal"
"github.com/mudler/edgevpn/pkg/blockchain"
"github.com/mudler/edgevpn/pkg/discovery"
Expand Down Expand Up @@ -143,6 +145,11 @@ var CommonFlags []cli.Flag = []cli.Flag{
Usage: "Enable DHT for peer discovery",
EnvVar: "EDGEVPNDHT",
},
&cli.BoolTFlag{
Name: "low-profile",
Usage: "Enable low profile. Lowers connections usage",
EnvVar: "EDGEVPNLOWPROFILE",
},
&cli.StringFlag{
Name: "log-level",
Usage: "Specify loglevel",
Expand Down Expand Up @@ -184,7 +191,7 @@ func cliToOpts(c *cli.Context) ([]node.Option, []vpn.Option, *logger.Logger) {
iface := c.String("interface")
logLevel := c.String("log-level")
libp2plogLevel := c.String("libp2p-log-level")
dht, mDNS := c.Bool("dht"), c.Bool("mdns")
dhtE, mDNS := c.Bool("dht"), c.Bool("mdns")

ledgerState := c.String("ledger-state")

Expand Down Expand Up @@ -215,6 +222,12 @@ func cliToOpts(c *cli.Context) ([]node.Option, []vpn.Option, *logger.Logger) {
}
}

dhtOpts := []dht.Option{}

if c.Bool("low-profile") {
dhtOpts = append(dhtOpts, dht.BucketSize(20))
}

opts := []node.Option{
node.WithDiscoveryInterval(time.Duration(c.Int("discovery-interval")) * time.Second),
node.WithLedgerAnnounceTime(time.Duration(c.Int("ledger-announce-interval")) * time.Second),
Expand All @@ -224,8 +237,8 @@ func cliToOpts(c *cli.Context) ([]node.Option, []vpn.Option, *logger.Logger) {
node.WithBlacklist(c.StringSlice("blacklist")...),
node.LibP2PLogLevel(libp2plvl),
node.WithInterfaceAddress(address),
node.FromBase64(mDNS, dht, token),
node.FromYaml(mDNS, dht, config),
node.FromBase64(mDNS, dhtE, token, dhtOpts...),
node.FromYaml(mDNS, dhtE, config, dhtOpts...),
}

vpnOpts := []vpn.Option{
Expand Down Expand Up @@ -257,6 +270,11 @@ func cliToOpts(c *cli.Context) ([]node.Option, []vpn.Option, *logger.Logger) {
))
}

if c.Bool("low-profile") {
cm := connmanager.NewConnManager(20, 100, 80*time.Second)
libp2pOpts = append(libp2pOpts, libp2p.ConnectionManager(cm))
}

if c.Bool("holepunch") {
libp2pOpts = append(libp2pOpts, libp2p.EnableHolePunching())
}
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/google/btree v1.0.1 // indirect
github.com/gookit/color v1.5.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/golang-lru v0.5.4
github.com/ipfs/go-cid v0.1.0 // indirect
github.com/ipfs/go-datastore v0.5.1 // indirect
github.com/ipfs/go-log v1.0.5
Expand All @@ -20,6 +20,7 @@ require (
github.com/labstack/echo/v4 v4.6.1
github.com/labstack/gommon v0.3.1 // indirect
github.com/libp2p/go-libp2p v0.17.0
github.com/libp2p/go-libp2p-connmgr v0.2.4
github.com/libp2p/go-libp2p-core v0.13.0
github.com/libp2p/go-libp2p-discovery v0.6.0
github.com/libp2p/go-libp2p-kad-dht v0.15.0
Expand Down
7 changes: 6 additions & 1 deletion pkg/discovery/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ type DHT struct {
console log.StandardLogger
RefreshDiscoveryTime time.Duration
dht *dht.IpfsDHT
dhtOptions []dht.Option
}

func NewDHT(d ...dht.Option) *DHT {
return &DHT{dhtOptions: d}
}

func (d *DHT) Option(ctx context.Context) func(c *libp2p.Config) error {
Expand All @@ -67,7 +72,7 @@ func (d *DHT) startDHT(ctx context.Context, h host.Host) (*dht.IpfsDHT, error) {
// client because we want each peer to maintain its own local copy of the
// DHT, so that the bootstrapping node of the DHT can go down without
// inhibiting future peer discovery.
kad, err := dht.New(ctx, h)
kad, err := dht.New(ctx, h, d.dhtOptions...)
if err != nil {
return d.dht, err
}
Expand Down
27 changes: 14 additions & 13 deletions pkg/node/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/mudler/edgevpn/pkg/blockchain"
discovery "github.com/mudler/edgevpn/pkg/discovery"
"github.com/mudler/edgevpn/pkg/protocol"
Expand Down Expand Up @@ -227,15 +228,15 @@ func (y YAMLConnectionConfig) YAML() string {
return string(bytesData)
}

func (y YAMLConnectionConfig) copy(mdns, dht bool, cfg *Config) {
d := &discovery.DHT{
RefreshDiscoveryTime: cfg.DiscoveryInterval,
OTPInterval: y.OTP.DHT.Interval,
OTPKey: y.OTP.DHT.Key,
KeyLength: y.OTP.DHT.Length,
RendezvousString: y.Rendezvous,
BootstrapPeers: cfg.DiscoveryBootstrapPeers,
}
func (y YAMLConnectionConfig) copy(mdns, dht bool, cfg *Config, opts ...dht.Option) {
d := discovery.NewDHT(opts...)
d.RefreshDiscoveryTime = cfg.DiscoveryInterval
d.OTPInterval = y.OTP.DHT.Interval
d.OTPKey = y.OTP.DHT.Key
d.KeyLength = y.OTP.DHT.Length
d.RendezvousString = y.Rendezvous
d.BootstrapPeers = cfg.DiscoveryBootstrapPeers

m := &discovery.MDNS{DiscoveryServiceTag: y.MDNS}
cfg.ExchangeKey = y.OTP.Crypto.Key
cfg.RoomName = y.RoomName
Expand Down Expand Up @@ -283,7 +284,7 @@ func GenerateNewConnectionData(i ...int) *YAMLConnectionConfig {
}
}

func FromYaml(enablemDNS, enableDHT bool, path string) func(cfg *Config) error {
func FromYaml(enablemDNS, enableDHT bool, path string, d ...dht.Option) func(cfg *Config) error {
return func(cfg *Config) error {
if len(path) == 0 {
return nil
Expand All @@ -299,12 +300,12 @@ func FromYaml(enablemDNS, enableDHT bool, path string) func(cfg *Config) error {
return errors.Wrap(err, "parsing yaml")
}

t.copy(enablemDNS, enableDHT, cfg)
t.copy(enablemDNS, enableDHT, cfg, d...)
return nil
}
}

func FromBase64(enablemDNS, enableDHT bool, bb string) func(cfg *Config) error {
func FromBase64(enablemDNS, enableDHT bool, bb string, d ...dht.Option) func(cfg *Config) error {
return func(cfg *Config) error {
if len(bb) == 0 {
return nil
Expand All @@ -318,7 +319,7 @@ func FromBase64(enablemDNS, enableDHT bool, bb string) func(cfg *Config) error {
if err := yaml.Unmarshal(configDec, &t); err != nil {
return errors.Wrap(err, "parsing yaml")
}
t.copy(enablemDNS, enableDHT, cfg)
t.copy(enablemDNS, enableDHT, cfg, d...)
return nil
}
}

0 comments on commit 0042928

Please sign in to comment.