transport.go 1.71 KB
Newer Older
tavit ohanian's avatar
tavit ohanian committed
1
package dms3p2p
2 3

import (
Steven Allen's avatar
Steven Allen committed
4 5
	"fmt"

6
	quic "github.com/lucas-clemente/quic-go"
tavit ohanian's avatar
tavit ohanian committed
7 8 9 10 11 12
	config "gitlab.dms3.io/dms3/go-dms3-config"
	p2p "gitlab.dms3.io/p2p/go-p2p"
	metrics "gitlab.dms3.io/p2p/go-p2p-core/metrics"
	p2pquic "gitlab.dms3.io/p2p/go-p2p-quic-transport"
	tcp "gitlab.dms3.io/p2p/go-tcp-transport"
	websocket "gitlab.dms3.io/p2p/go-ws-transport"
Steven Allen's avatar
Steven Allen committed
13 14

	"go.uber.org/fx"
15 16
)

tavit ohanian's avatar
tavit ohanian committed
17
// See https://gitlab.dms3.io/dms3/go-dms3/issues/7526 and
18 19 20 21 22 23
// https://github.com/lucas-clemente/quic-go/releases/tag/v0.17.3.
// TODO: remove this once the network has upgraded to > v0.6.0.
func init() {
	quic.RetireBugBackwardsCompatibilityMode = true
}

24 25 26 27
func Transports(tptConfig config.Transports) interface{} {
	return func(pnet struct {
		fx.In
		Fprint PNetFingerprint `optional:"true"`
tavit ohanian's avatar
tavit ohanian committed
28
	}) (opts P2pOpts, err error) {
29
		privateNetworkEnabled := pnet.Fprint != nil
Steven Allen's avatar
Steven Allen committed
30

31
		if tptConfig.Network.TCP.WithDefault(true) {
tavit ohanian's avatar
tavit ohanian committed
32
			opts.Opts = append(opts.Opts, p2p.Transport(tcp.NewTCPTransport))
33
		}
Steven Allen's avatar
Steven Allen committed
34

35
		if tptConfig.Network.Websocket.WithDefault(true) {
tavit ohanian's avatar
tavit ohanian committed
36
			opts.Opts = append(opts.Opts, p2p.Transport(websocket.New))
37
		}
Steven Allen's avatar
Steven Allen committed
38

39 40 41 42 43 44 45 46 47
		if tptConfig.Network.QUIC.WithDefault(!privateNetworkEnabled) {
			if privateNetworkEnabled {
				// QUIC was force enabled while the private network was turned on.
				// Fail and tell the user.
				return opts, fmt.Errorf(
					"The QUIC transport does not support private networks. " +
						"Please disable Swarm.Transports.Network.QUIC.",
				)
			}
tavit ohanian's avatar
tavit ohanian committed
48
			opts.Opts = append(opts.Opts, p2p.Transport(p2pquic.NewTransport))
Steven Allen's avatar
Steven Allen committed
49 50
		}

51
		return opts, nil
52 53 54
	}
}

tavit ohanian's avatar
tavit ohanian committed
55
func BandwidthCounter() (opts P2pOpts, reporter *metrics.BandwidthCounter) {
56
	reporter = metrics.NewBandwidthCounter()
tavit ohanian's avatar
tavit ohanian committed
57
	opts.Opts = append(opts.Opts, p2p.BandwidthReporter(reporter))
58 59
	return opts, reporter
}