-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a transport config section
This way, users can disable transports (especially QUIC), and set muxer/security transport priorities.
- Loading branch information
Showing
15 changed files
with
595 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package libp2p | ||
|
||
import ( | ||
config "github.com/ipfs/go-ipfs-config" | ||
"github.com/libp2p/go-libp2p" | ||
noise "github.com/libp2p/go-libp2p-noise" | ||
secio "github.com/libp2p/go-libp2p-secio" | ||
tls "github.com/libp2p/go-libp2p-tls" | ||
) | ||
|
||
func Security(enabled bool, tptConfig config.Transports) interface{} { | ||
if !enabled { | ||
return func() (opts Libp2pOpts) { | ||
// TODO: shouldn't this be Errorf to guarantee visibility? | ||
log.Warnf(`Your IPFS node has been configured to run WITHOUT ENCRYPTED CONNECTIONS. | ||
You will not be able to connect to any nodes configured to use encrypted connections`) | ||
opts.Opts = append(opts.Opts, libp2p.NoSecurity) | ||
return opts | ||
} | ||
} | ||
|
||
// Using the new config options. | ||
return func() (opts Libp2pOpts) { | ||
opts.Opts = append(opts.Opts, prioritizeOptions([]priorityOption{{ | ||
priority: tptConfig.Security.TLS, | ||
defaultPriority: 100, | ||
opt: libp2p.Security(tls.ID, tls.New), | ||
}, { | ||
priority: tptConfig.Security.SECIO, | ||
defaultPriority: 200, | ||
opt: libp2p.Security(secio.ID, secio.New), | ||
}, { | ||
priority: tptConfig.Security.Noise, | ||
defaultPriority: 300, | ||
opt: libp2p.Security(noise.ID, noise.New), | ||
}})) | ||
return opts | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,76 @@ | ||
package libp2p | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
config "github.com/ipfs/go-ipfs-config" | ||
"github.com/libp2p/go-libp2p" | ||
smux "github.com/libp2p/go-libp2p-core/mux" | ||
mplex "github.com/libp2p/go-libp2p-mplex" | ||
yamux "github.com/libp2p/go-libp2p-yamux" | ||
) | ||
|
||
func makeSmuxTransportOption(mplexExp bool) libp2p.Option { | ||
func yamuxTransport() smux.Multiplexer { | ||
tpt := *yamux.DefaultTransport | ||
tpt.AcceptBacklog = 512 | ||
if os.Getenv("YAMUX_DEBUG") != "" { | ||
tpt.LogOutput = os.Stderr | ||
} | ||
|
||
return &tpt | ||
} | ||
|
||
func makeSmuxTransportOption(tptConfig config.Transports) (libp2p.Option, error) { | ||
const yamuxID = "/yamux/1.0.0" | ||
const mplexID = "/mplex/6.7.0" | ||
|
||
ymxtpt := *yamux.DefaultTransport | ||
ymxtpt.AcceptBacklog = 512 | ||
|
||
if os.Getenv("YAMUX_DEBUG") != "" { | ||
ymxtpt.LogOutput = os.Stderr | ||
} | ||
|
||
muxers := map[string]smux.Multiplexer{yamuxID: &ymxtpt} | ||
if mplexExp { | ||
muxers[mplexID] = mplex.DefaultTransport | ||
} | ||
|
||
// Allow muxer preference order overriding | ||
order := []string{yamuxID, mplexID} | ||
if prefs := os.Getenv("LIBP2P_MUX_PREFS"); prefs != "" { | ||
order = strings.Fields(prefs) | ||
} | ||
// Using legacy LIBP2P_MUX_PREFS variable. | ||
log.Error("LIBP2P_MUX_PREFS is now deprecated.") | ||
log.Error("Use the `Swarm.Transports.Multiplexers' config field.") | ||
muxers := strings.Fields(prefs) | ||
enabled := make(map[string]bool, len(muxers)) | ||
|
||
opts := make([]libp2p.Option, 0, len(order)) | ||
for _, id := range order { | ||
tpt, ok := muxers[id] | ||
if !ok { | ||
log.Warn("unknown or duplicate muxer in LIBP2P_MUX_PREFS: %s", id) | ||
continue | ||
var opts []libp2p.Option | ||
for _, tpt := range muxers { | ||
if enabled[tpt] { | ||
return nil, fmt.Errorf( | ||
"duplicate muxer found in LIBP2P_MUX_PREFS: %s", | ||
tpt, | ||
) | ||
} | ||
switch tpt { | ||
case yamuxID: | ||
opts = append(opts, libp2p.Muxer(tpt, yamuxTransport)) | ||
case mplexID: | ||
opts = append(opts, libp2p.Muxer(tpt, mplex.DefaultTransport)) | ||
default: | ||
return nil, fmt.Errorf("unknown muxer: %s", tpt) | ||
} | ||
} | ||
delete(muxers, id) | ||
opts = append(opts, libp2p.Muxer(id, tpt)) | ||
return libp2p.ChainOptions(opts...), nil | ||
} else { | ||
return prioritizeOptions([]priorityOption{{ | ||
priority: tptConfig.Multiplexers.Yamux, | ||
defaultPriority: 100, | ||
opt: libp2p.Muxer(yamuxID, yamuxTransport), | ||
}, { | ||
priority: tptConfig.Multiplexers.Mplex, | ||
defaultPriority: 200, | ||
opt: libp2p.Muxer(mplexID, mplex.DefaultTransport), | ||
}}), nil | ||
} | ||
|
||
return libp2p.ChainOptions(opts...) | ||
} | ||
|
||
func SmuxTransport(mplex bool) func() (opts Libp2pOpts, err error) { | ||
func SmuxTransport(tptConfig config.Transports) func() (opts Libp2pOpts, err error) { | ||
return func() (opts Libp2pOpts, err error) { | ||
opts.Opts = append(opts.Opts, makeSmuxTransportOption(mplex)) | ||
return | ||
res, err := makeSmuxTransportOption(tptConfig) | ||
opts.Opts = append(opts.Opts, res) | ||
return opts, err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.