Browse Source

Merge pull request #152 from mudler/fixups_2

do not rely on upstream fallback, close streams by default
Ettore Di Giacinto 1 year ago
parent
commit
1c624ff8aa
2 changed files with 76 additions and 7 deletions
  1. 74 1
      pkg/node/connection.go
  2. 2 6
      pkg/vpn/vpn.go

+ 74 - 1
pkg/node/connection.go

@@ -121,11 +121,84 @@ func (e *Node) genHost(ctx context.Context) (host.Host, error) {
 		opts = append(opts, libp2p.NoSecurity)
 	}
 
-	opts = append(opts, libp2p.FallbackDefaults)
+	opts = append(opts, FallbackDefaults)
 
 	return libp2p.New(opts...)
 }
 
+// FallbackDefaults applies default options to the libp2p node if and only if no
+// other relevant options have been applied. will be appended to the options
+// passed into New.
+var FallbackDefaults libp2p.Option = func(cfg *libp2p.Config) error {
+	for _, def := range defaults {
+		if !def.fallback(cfg) {
+			continue
+		}
+		if err := cfg.Apply(def.opt); err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
+// Complete list of default options and when to fallback on them.
+//
+// Please *DON'T* specify default options any other way. Putting this all here
+// makes tracking defaults *much* easier.
+var defaults = []struct {
+	fallback func(cfg *libp2p.Config) bool
+	opt      libp2p.Option
+}{
+	{
+		fallback: func(cfg *libp2p.Config) bool { return cfg.Transports == nil && cfg.ListenAddrs == nil },
+		opt:      libp2p.DefaultListenAddrs,
+	},
+	{
+		fallback: func(cfg *libp2p.Config) bool { return cfg.Transports == nil && cfg.PSK == nil },
+		opt:      libp2p.DefaultTransports,
+	},
+	{
+		fallback: func(cfg *libp2p.Config) bool { return cfg.Transports == nil && cfg.PSK != nil },
+		opt:      libp2p.DefaultPrivateTransports,
+	},
+	{
+		fallback: func(cfg *libp2p.Config) bool { return cfg.Muxers == nil },
+		opt:      libp2p.DefaultMuxers,
+	},
+	{
+		fallback: func(cfg *libp2p.Config) bool { return !cfg.Insecure && cfg.SecurityTransports == nil },
+		opt:      libp2p.DefaultSecurity,
+	},
+	{
+		fallback: func(cfg *libp2p.Config) bool { return cfg.PeerKey == nil },
+		opt:      libp2p.RandomIdentity,
+	},
+	{
+		fallback: func(cfg *libp2p.Config) bool { return cfg.Peerstore == nil },
+		opt:      libp2p.DefaultPeerstore,
+	},
+	{
+		fallback: func(cfg *libp2p.Config) bool { return !cfg.RelayCustom },
+		opt:      libp2p.DefaultEnableRelay,
+	},
+	//{
+	//	fallback: func(cfg *libp2p.Config) bool { return cfg.ResourceManager == nil },
+	//	opt:      libp2p.DefaultResourceManager,
+	//},
+	//{
+	//	fallback: func(cfg *libp2p.Config) bool { return cfg.ConnManager == nil },
+	//	opt:      libp2p.DefaultConnectionManager,
+	//},
+	{
+		fallback: func(cfg *libp2p.Config) bool { return cfg.MultiaddrResolver == nil },
+		opt:      libp2p.DefaultMultiaddrResolver,
+	},
+	//{
+	//	fallback: func(cfg *libp2p.Config) bool { return !cfg.DisableMetrics && cfg.PrometheusRegisterer == nil },
+	//	opt:      libp2p.DefaultPrometheusRegisterer,
+	//},
+}
+
 func (e *Node) sealkey() string {
 	return internalCrypto.MD5(internalCrypto.TOTP(sha256.New, e.config.SealKeyLength, e.config.SealKeyInterval, e.config.ExchangeKey))
 }

+ 2 - 6
pkg/vpn/vpn.go

@@ -154,9 +154,7 @@ func streamHandler(l *blockchain.Ledger, ifce *water.Interface, c *Config, nc no
 		if err != nil {
 			stream.Reset()
 		}
-		if c.lowProfile {
-			stream.Close()
-		}
+		stream.Close()
 	}
 }
 
@@ -260,15 +258,13 @@ func handleFrame(mgr streamManager, frame ethernet.Frame, c *Config, n *node.Nod
 	if err != nil {
 		return fmt.Errorf("could not open stream to %s: %w", d.String(), err)
 	}
+	defer stream.Close()
 
 	if mgr != nil {
 		mgr.Connected(n.Host().Network(), stream)
 	}
 
 	_, err = stream.Write(frame)
-	if c.lowProfile {
-		return stream.Close()
-	}
 	return err
 }