Browse Source

:gear: Allow to enable mplex

Ettore Di Giacinto 3 years ago
parent
commit
12b34002e0
4 changed files with 22 additions and 3 deletions
  1. 6 0
      cmd/util.go
  2. 2 1
      go.mod
  3. 13 1
      pkg/config/config.go
  4. 1 1
      pkg/vpn/vpn.go

+ 6 - 0
cmd/util.go

@@ -162,6 +162,11 @@ var CommonFlags []cli.Flag = []cli.Flag{
 		Usage:  "Enable low profile. Lowers connections usage",
 		EnvVar: "EDGEVPNLOWPROFILE",
 	},
+	&cli.BoolFlag{
+		Name:   "mplex-multiplexer",
+		Usage:  "Enable mplex multiplexer.",
+		EnvVar: "EDGEVPNMPLEX",
+	},
 	&cli.BoolTFlag{
 		Name:   "low-profile-vpn",
 		Usage:  "Enable low profile on VPN",
@@ -368,6 +373,7 @@ func cliToOpts(c *cli.Context) ([]node.Option, []vpn.Option, *logger.Logger) {
 			MaxConnections: c.Int("max-connections"),
 			MaxStreams:     c.Int("max-streams"),
 			HolePunch:      c.Bool("holepunch"),
+			Mplex:          c.Bool("mplex-multiplexer"),
 			StaticRelays:   c.StringSlice("autorelay-static-peer"),
 		},
 		Limit: config.ResourceLimit{

+ 2 - 1
go.mod

@@ -20,9 +20,10 @@ require (
 	github.com/libp2p/go-libp2p-core v0.15.1
 	github.com/libp2p/go-libp2p-discovery v0.6.0
 	github.com/libp2p/go-libp2p-kad-dht v0.15.0
-	github.com/libp2p/go-libp2p-mplex v0.6.0 // indirect
+	github.com/libp2p/go-libp2p-mplex v0.6.0
 	github.com/libp2p/go-libp2p-pubsub v0.6.1
 	github.com/libp2p/go-libp2p-resource-manager v0.2.1
+	github.com/libp2p/go-libp2p-yamux v0.9.1
 	github.com/mattn/go-colorable v0.1.12 // indirect
 	github.com/miekg/dns v1.1.48
 	github.com/mudler/go-isterminal v0.0.0-20211031135732-5e4e06fc5a58

+ 13 - 1
pkg/config/config.go

@@ -27,7 +27,9 @@ import (
 	"github.com/libp2p/go-libp2p-core/network"
 	"github.com/libp2p/go-libp2p-core/peer"
 	dht "github.com/libp2p/go-libp2p-kad-dht"
+	mplex "github.com/libp2p/go-libp2p-mplex"
 	rcmgr "github.com/libp2p/go-libp2p-resource-manager"
+	yamux "github.com/libp2p/go-libp2p-yamux"
 	"github.com/libp2p/go-libp2p/p2p/host/autorelay"
 	"github.com/mudler/edgevpn/pkg/blockchain"
 	"github.com/mudler/edgevpn/pkg/crypto"
@@ -91,6 +93,7 @@ type Connection struct {
 	RelayV1      bool
 	StaticRelays []string
 
+	Mplex          bool
 	MaxConnections int
 	MaxStreams     int
 }
@@ -223,7 +226,16 @@ func (c Config) ToOpts(l *logger.Logger) ([]node.Option, []vpn.Option, error) {
 			relayOpts = append(relayOpts, autorelay.WithStaticRelays(peers2AddrInfo(c.Connection.StaticRelays)))
 		}
 
-		libp2pOpts = append(libp2pOpts, libp2p.EnableAutoRelay(relayOpts...))
+		libp2pOpts = append(libp2pOpts,
+			libp2p.EnableAutoRelay(relayOpts...))
+	}
+
+	if c.Connection.Mplex {
+		libp2pOpts = append(libp2pOpts,
+			libp2p.ChainOptions(
+				libp2p.Muxer("/yamux/1.0.0", yamux.DefaultTransport),
+				libp2p.Muxer("/mplex/6.7.0", mplex.DefaultTransport),
+			))
 	}
 
 	if c.NAT.RateLimit {

+ 1 - 1
pkg/vpn/vpn.go

@@ -218,7 +218,7 @@ func handleFrame(mgr streamManager, frame ethernet.Frame, c *Config, n *node.Nod
 
 	stream, err = n.Host().NewStream(ctx, d, protocol.EdgeVPN.ID())
 	if err != nil {
-		return errors.Wrap(err, "could not open stream")
+		return fmt.Errorf("could not open stream to %s: %w", d.String(), err)
 	}
 
 	if mgr != nil {