@@ -3,35 +3,52 @@ package libp2p
3
3
import (
4
4
"fmt"
5
5
"os"
6
+ "strings"
6
7
7
8
"github.com/ipfs/kubo/config"
8
9
9
10
"github.com/libp2p/go-libp2p"
10
- "github.com/libp2p/go-libp2p/core/network "
11
+ "github.com/libp2p/go-libp2p/p2p/muxer/mplex "
11
12
"github.com/libp2p/go-libp2p/p2p/muxer/yamux"
12
13
)
13
14
14
- func yamuxTransport () network.Multiplexer {
15
- tpt := * yamux .DefaultTransport
16
- tpt .AcceptBacklog = 512
17
- if os .Getenv ("YAMUX_DEBUG" ) != "" {
18
- tpt .LogOutput = os .Stderr
19
- }
20
- return & tpt
21
- }
22
-
23
15
func makeSmuxTransportOption (tptConfig config.Transports ) (libp2p.Option , error ) {
24
16
if prefs := os .Getenv ("LIBP2P_MUX_PREFS" ); prefs != "" {
25
- return nil , fmt .Errorf ("configuring muxers with LIBP2P_MUX_PREFS is no longer supported" )
26
- }
27
- if tptConfig .Multiplexers .Mplex != 0 {
28
- return nil , fmt .Errorf ("Swarm.Transports.Multiplexers.Mplex is no longer supported" )
29
- }
30
- if tptConfig .Multiplexers .Yamux < 0 {
31
- return nil , fmt .Errorf ("Swarm.Transports.Multiplexers.Yamux is disabled even tho it is the only multiplexer available" )
32
- }
17
+ // Using legacy LIBP2P_MUX_PREFS variable.
18
+ log .Error ("LIBP2P_MUX_PREFS is now deprecated." )
19
+ log .Error ("Use the `Swarm.Transports.Multiplexers' config field." )
20
+ muxers := strings .Fields (prefs )
21
+ enabled := make (map [string ]bool , len (muxers ))
33
22
34
- return libp2p .Muxer (yamux .ID , yamuxTransport ()), nil
23
+ var opts []libp2p.Option
24
+ for _ , tpt := range muxers {
25
+ if enabled [tpt ] {
26
+ return nil , fmt .Errorf (
27
+ "duplicate muxer found in LIBP2P_MUX_PREFS: %s" ,
28
+ tpt ,
29
+ )
30
+ }
31
+ switch tpt {
32
+ case yamux .ID :
33
+ opts = append (opts , libp2p .Muxer (tpt , yamux .DefaultTransport ))
34
+ case mplex .ID :
35
+ opts = append (opts , libp2p .Muxer (tpt , mplex .DefaultTransport ))
36
+ default :
37
+ return nil , fmt .Errorf ("unknown muxer: %s" , tpt )
38
+ }
39
+ }
40
+ return libp2p .ChainOptions (opts ... ), nil
41
+ } else {
42
+ return prioritizeOptions ([]priorityOption {{
43
+ priority : tptConfig .Multiplexers .Yamux ,
44
+ defaultPriority : 100 ,
45
+ opt : libp2p .Muxer (yamux .ID , yamux .DefaultTransport ),
46
+ }, {
47
+ priority : tptConfig .Multiplexers .Mplex ,
48
+ defaultPriority : config .Disabled ,
49
+ opt : libp2p .Muxer (mplex .ID , mplex .DefaultTransport ),
50
+ }}), nil
51
+ }
35
52
}
36
53
37
54
func SmuxTransport (tptConfig config.Transports ) func () (opts Libp2pOpts , err error ) {
0 commit comments