| 1 | package libp2p |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | |
| 7 | "github.com/ipfs/kubo/config" |
| 8 | "github.com/ipshipyard/p2p-forge/client" |
| 9 | "github.com/libp2p/go-libp2p" |
| 10 | "github.com/libp2p/go-libp2p/core/metrics" |
| 11 | quic "github.com/libp2p/go-libp2p/p2p/transport/quic" |
| 12 | "github.com/libp2p/go-libp2p/p2p/transport/tcp" |
| 13 | webrtc "github.com/libp2p/go-libp2p/p2p/transport/webrtc" |
| 14 | "github.com/libp2p/go-libp2p/p2p/transport/websocket" |
| 15 | webtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport" |
| 16 | |
| 17 | "go.uber.org/fx" |
| 18 | ) |
| 19 | |
| 20 | func Transports(tptConfig config.Transports) any { |
| 21 | return func(params struct { |
| 22 | fx.In |
| 23 | Fprint PNetFingerprint `optional:"true"` |
| 24 | ForgeMgr *client.P2PForgeCertMgr `optional:"true"` |
| 25 | }, |
| 26 | ) (opts Libp2pOpts, err error) { |
| 27 | privateNetworkEnabled := params.Fprint != nil |
| 28 | |
| 29 | tcpEnabled := tptConfig.Network.TCP.WithDefault(true) |
| 30 | wsEnabled := tptConfig.Network.Websocket.WithDefault(true) |
| 31 | if tcpEnabled { |
| 32 | // TODO(9290): Make WithMetrics configurable |
| 33 | opts.Opts = append(opts.Opts, libp2p.Transport(tcp.NewTCPTransport, tcp.WithMetrics())) |
| 34 | } |
| 35 | |
| 36 | if wsEnabled { |
| 37 | if params.ForgeMgr == nil { |
| 38 | opts.Opts = append(opts.Opts, libp2p.Transport(websocket.New)) |
| 39 | } else { |
| 40 | opts.Opts = append(opts.Opts, libp2p.Transport(websocket.New, websocket.WithTLSConfig(params.ForgeMgr.TLSConfig()))) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | if tcpEnabled && wsEnabled && os.Getenv("LIBP2P_TCP_MUX") != "false" { |
| 45 | if privateNetworkEnabled { |
| 46 | log.Error("libp2p.ShareTCPListener() is not supported in private networks, please disable Swarm.Transports.Network.Websocket or run with LIBP2P_TCP_MUX=false to make this message go away") |
| 47 | } else { |
| 48 | opts.Opts = append(opts.Opts, libp2p.ShareTCPListener()) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if tptConfig.Network.QUIC.WithDefault(!privateNetworkEnabled) { |
| 53 | if privateNetworkEnabled { |
| 54 | return opts, fmt.Errorf( |
| 55 | "QUIC transport does not support private networks, please disable Swarm.Transports.Network.QUIC", |
| 56 | ) |
| 57 | } |
| 58 | opts.Opts = append(opts.Opts, libp2p.Transport(quic.NewTransport)) |
| 59 | } |
| 60 | |
| 61 | if tptConfig.Network.WebTransport.WithDefault(!privateNetworkEnabled) { |
| 62 | if privateNetworkEnabled { |
| 63 | return opts, fmt.Errorf( |
| 64 | "WebTransport transport does not support private networks, please disable Swarm.Transports.Network.WebTransport", |
| 65 | ) |
| 66 | } |
| 67 | opts.Opts = append(opts.Opts, libp2p.Transport(webtransport.New)) |
| 68 | } |
| 69 | |
| 70 | if tptConfig.Network.WebRTCDirect.WithDefault(!privateNetworkEnabled) { |
| 71 | if privateNetworkEnabled { |
| 72 | return opts, fmt.Errorf( |
| 73 | "WebRTC Direct transport does not support private networks, please disable Swarm.Transports.Network.WebRTCDirect", |
| 74 | ) |
| 75 | } |
| 76 | opts.Opts = append(opts.Opts, libp2p.Transport(webrtc.New)) |
| 77 | } |
| 78 | |
| 79 | return opts, nil |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func BandwidthCounter() (opts Libp2pOpts, reporter *metrics.BandwidthCounter) { |
| 84 | reporter = metrics.NewBandwidthCounter() |
| 85 | opts.Opts = append(opts.Opts, libp2p.BandwidthReporter(reporter)) |
| 86 | return opts, reporter |
| 87 | } |