libp2p 0.36.0 → 0.36.2-199395d
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +3 -21
- package/README.md +13 -10
- package/dist/src/address-manager/index.d.ts +33 -52
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +81 -0
- package/dist/src/address-manager/index.js.map +1 -0
- package/dist/src/circuit/auto-relay.d.ts +24 -85
- package/dist/src/circuit/auto-relay.d.ts.map +1 -1
- package/dist/src/circuit/auto-relay.js +215 -0
- package/dist/src/circuit/auto-relay.js.map +1 -0
- package/dist/src/circuit/circuit/hop.d.ts +25 -52
- package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/hop.js +138 -0
- package/dist/src/circuit/circuit/hop.js.map +1 -0
- package/dist/src/circuit/circuit/stop.d.ts +16 -8
- package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stop.js +49 -0
- package/dist/src/circuit/circuit/stop.js.map +1 -0
- package/dist/src/circuit/circuit/stream-handler.d.ts +16 -35
- package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stream-handler.js +60 -0
- package/dist/src/circuit/circuit/stream-handler.js.map +1 -0
- package/dist/src/circuit/circuit/utils.d.ts +3 -6
- package/dist/src/circuit/circuit/utils.d.ts.map +1 -1
- package/dist/src/circuit/circuit/utils.js +43 -0
- package/dist/src/circuit/circuit/utils.js.map +1 -0
- package/dist/src/circuit/constants.d.ts +24 -6
- package/dist/src/circuit/constants.d.ts.map +1 -1
- package/dist/src/circuit/constants.js +26 -0
- package/dist/src/circuit/constants.js.map +1 -0
- package/dist/src/circuit/index.d.ts +41 -74
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/index.js +69 -0
- package/dist/src/circuit/index.js.map +1 -0
- package/dist/src/circuit/listener.d.ts +8 -3
- package/dist/src/circuit/listener.d.ts.map +1 -1
- package/dist/src/circuit/listener.js +54 -0
- package/dist/src/circuit/listener.js.map +1 -0
- package/dist/src/circuit/multicodec.d.ts +1 -1
- package/dist/src/circuit/multicodec.d.ts.map +1 -1
- package/dist/src/circuit/multicodec.js +2 -0
- package/dist/src/circuit/multicodec.js.map +1 -0
- package/dist/src/circuit/{protocol → pb}/index.d.ts +0 -0
- package/{src/circuit/protocol → dist/src/circuit/pb}/index.js +7 -9
- package/dist/src/circuit/transport.d.ts +18 -78
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +190 -0
- package/dist/src/circuit/transport.js.map +1 -0
- package/dist/src/circuit/utils.d.ts +5 -2
- package/dist/src/circuit/utils.d.ts.map +1 -1
- package/dist/src/circuit/utils.js +11 -0
- package/dist/src/circuit/utils.js.map +1 -0
- package/dist/src/config.d.ts +3 -85
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +93 -0
- package/dist/src/config.js.map +1 -0
- package/dist/src/connection-manager/auto-dialler.d.ts +24 -41
- package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dialler.js +105 -0
- package/dist/src/connection-manager/auto-dialler.js.map +1 -0
- package/dist/src/connection-manager/index.d.ts +79 -144
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +295 -0
- package/dist/src/connection-manager/index.js.map +1 -0
- package/dist/src/connection-manager/latency-monitor.d.ts +72 -97
- package/dist/src/connection-manager/latency-monitor.d.ts.map +1 -1
- package/dist/src/connection-manager/latency-monitor.js +220 -0
- package/dist/src/connection-manager/latency-monitor.js.map +1 -0
- package/dist/src/connection-manager/visibility-change-emitter.d.ts +15 -16
- package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +1 -1
- package/dist/src/connection-manager/visibility-change-emitter.js +118 -0
- package/dist/src/connection-manager/visibility-change-emitter.js.map +1 -0
- package/dist/src/constants.d.ts +22 -10
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +27 -0
- package/dist/src/constants.js.map +1 -0
- package/dist/src/content-routing/index.d.ts +26 -81
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +99 -0
- package/dist/src/content-routing/index.js.map +1 -0
- package/dist/src/content-routing/utils.d.ts +6 -49
- package/dist/src/content-routing/utils.d.ts.map +1 -1
- package/dist/src/content-routing/utils.js +42 -0
- package/dist/src/content-routing/utils.js.map +1 -0
- package/dist/src/dht/dht-content-routing.d.ts +10 -24
- package/dist/src/dht/dht-content-routing.d.ts.map +1 -1
- package/dist/src/dht/dht-content-routing.js +32 -0
- package/dist/src/dht/dht-content-routing.js.map +1 -0
- package/dist/src/dht/dht-peer-routing.d.ts +9 -24
- package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
- package/dist/src/dht/dht-peer-routing.js +26 -0
- package/dist/src/dht/dht-peer-routing.js.map +1 -0
- package/dist/src/dialer/auto-dialer.d.ts +14 -0
- package/dist/src/dialer/auto-dialer.d.ts.map +1 -0
- package/dist/src/dialer/auto-dialer.js +24 -0
- package/dist/src/dialer/auto-dialer.js.map +1 -0
- package/dist/src/dialer/dial-request.d.ts +18 -46
- package/dist/src/dialer/dial-request.d.ts.map +1 -1
- package/dist/src/dialer/dial-request.js +83 -0
- package/dist/src/dialer/dial-request.js.map +1 -0
- package/dist/src/dialer/index.d.ts +41 -148
- package/dist/src/dialer/index.d.ts.map +1 -1
- package/dist/src/dialer/index.js +281 -0
- package/dist/src/dialer/index.js.map +1 -0
- package/dist/src/errors.d.ts +68 -63
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +73 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/fetch/constants.d.ts +1 -1
- package/dist/src/fetch/constants.d.ts.map +1 -1
- package/dist/src/fetch/constants.js +3 -0
- package/dist/src/fetch/constants.js.map +1 -0
- package/dist/src/fetch/index.d.ts +30 -49
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +130 -0
- package/dist/src/fetch/index.js.map +1 -0
- package/dist/src/fetch/{proto.d.ts → pb/proto.d.ts} +0 -0
- package/{src/fetch → dist/src/fetch/pb}/proto.js +7 -9
- package/dist/src/get-peer.d.ts +4 -11
- package/dist/src/get-peer.d.ts.map +1 -1
- package/dist/src/get-peer.js +43 -0
- package/dist/src/get-peer.js.map +1 -0
- package/dist/src/identify/consts.d.ts +9 -9
- package/dist/src/identify/consts.d.ts.map +1 -1
- package/dist/src/identify/consts.js +11 -0
- package/dist/src/identify/consts.js.map +1 -0
- package/dist/src/identify/index.d.ts +40 -96
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +322 -0
- package/dist/src/identify/index.js.map +1 -0
- package/{src/identify → dist/src/identify/pb}/message.d.ts +20 -5
- package/{src/identify → dist/src/identify/pb}/message.js +83 -42
- package/dist/src/index.d.ts +157 -583
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +9 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/insecure/index.d.ts +9 -0
- package/dist/src/insecure/index.d.ts.map +1 -0
- package/dist/src/insecure/index.js +78 -0
- package/dist/src/insecure/index.js.map +1 -0
- package/dist/src/insecure/{proto.d.ts → pb/proto.d.ts} +7 -1
- package/{src/insecure → dist/src/insecure/pb}/proto.js +39 -22
- package/dist/src/keychain/cms.d.ts +6 -14
- package/dist/src/keychain/cms.d.ts.map +1 -1
- package/dist/src/keychain/cms.js +129 -0
- package/dist/src/keychain/cms.js.map +1 -0
- package/dist/src/keychain/index.d.ts +62 -82
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/keychain/index.js +493 -0
- package/dist/src/keychain/index.js.map +1 -0
- package/dist/src/keychain/util.d.ts +3 -6
- package/dist/src/keychain/util.d.ts.map +1 -1
- package/dist/src/keychain/util.js +79 -0
- package/dist/src/keychain/util.js.map +1 -0
- package/dist/src/libp2p.d.ts +75 -0
- package/dist/src/libp2p.d.ts.map +1 -0
- package/dist/src/libp2p.js +368 -0
- package/dist/src/libp2p.js.map +1 -0
- package/dist/src/metrics/index.d.ts +47 -104
- package/dist/src/metrics/index.d.ts.map +1 -1
- package/dist/src/metrics/index.js +231 -0
- package/dist/src/metrics/index.js.map +1 -0
- package/dist/src/metrics/moving-average.d.ts +14 -0
- package/dist/src/metrics/moving-average.d.ts.map +1 -0
- package/dist/src/metrics/moving-average.js +40 -0
- package/dist/src/metrics/moving-average.js.map +1 -0
- package/dist/src/metrics/stats.d.ts +45 -93
- package/dist/src/metrics/stats.d.ts.map +1 -1
- package/dist/src/metrics/stats.js +183 -0
- package/dist/src/metrics/stats.js.map +1 -0
- package/dist/src/nat-manager.d.ts +42 -99
- package/dist/src/nat-manager.d.ts.map +1 -1
- package/dist/src/nat-manager.js +114 -0
- package/dist/src/nat-manager.js.map +1 -0
- package/dist/src/peer-record-updater.d.ts +15 -0
- package/dist/src/peer-record-updater.d.ts.map +1 -0
- package/dist/src/peer-record-updater.js +44 -0
- package/dist/src/peer-record-updater.js.map +1 -0
- package/dist/src/peer-routing.d.ts +39 -84
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +108 -0
- package/dist/src/peer-routing.js.map +1 -0
- package/dist/src/ping/constants.d.ts +4 -4
- package/dist/src/ping/constants.d.ts.map +1 -1
- package/dist/src/ping/constants.js +5 -0
- package/dist/src/ping/constants.js.map +1 -0
- package/dist/src/ping/index.d.ts +26 -33
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +56 -0
- package/dist/src/ping/index.js.map +1 -0
- package/dist/src/pnet/crypto.d.ts +15 -5
- package/dist/src/pnet/crypto.d.ts.map +1 -1
- package/dist/src/pnet/crypto.js +60 -0
- package/dist/src/pnet/crypto.js.map +1 -0
- package/dist/src/pnet/errors.d.ts +5 -5
- package/dist/src/pnet/errors.d.ts.map +1 -1
- package/dist/src/pnet/errors.js +6 -0
- package/dist/src/pnet/errors.js.map +1 -0
- package/dist/src/pnet/index.d.ts +11 -20
- package/dist/src/pnet/index.d.ts.map +1 -1
- package/dist/src/pnet/index.js +66 -0
- package/dist/src/pnet/index.js.map +1 -0
- package/dist/src/pnet/key-generator.d.ts +3 -7
- package/dist/src/pnet/key-generator.d.ts.map +1 -1
- package/dist/src/pnet/key-generator.js +25 -0
- package/dist/src/pnet/key-generator.js.map +1 -0
- package/dist/src/registrar.d.ts +27 -65
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +159 -0
- package/dist/src/registrar.js.map +1 -0
- package/dist/src/transport-manager.d.ts +34 -82
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +223 -0
- package/dist/src/transport-manager.js.map +1 -0
- package/dist/src/upgrader.d.ts +49 -129
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +402 -0
- package/dist/src/upgrader.js.map +1 -0
- package/dist/src/version.d.ts +3 -0
- package/dist/src/version.d.ts.map +1 -0
- package/dist/src/version.js +3 -0
- package/dist/src/version.js.map +1 -0
- package/package.json +145 -197
- package/src/address-manager/index.ts +129 -0
- package/src/circuit/README.md +16 -11
- package/src/circuit/auto-relay.ts +284 -0
- package/src/circuit/circuit/hop.ts +211 -0
- package/src/circuit/circuit/stop.ts +78 -0
- package/src/circuit/circuit/stream-handler.ts +87 -0
- package/src/circuit/circuit/{utils.js → utils.ts} +9 -25
- package/src/circuit/constants.ts +31 -0
- package/src/circuit/index.ts +120 -0
- package/src/circuit/{listener.js → listener.ts} +21 -27
- package/src/circuit/multicodec.ts +2 -0
- package/src/circuit/{protocol → pb}/index.d.ts +0 -0
- package/src/circuit/pb/index.js +528 -0
- package/src/circuit/{protocol → pb}/index.proto +0 -0
- package/src/circuit/transport.ts +216 -0
- package/src/circuit/utils.ts +12 -0
- package/src/config.ts +101 -0
- package/src/connection-manager/auto-dialler.ts +154 -0
- package/src/connection-manager/index.ts +422 -0
- package/src/connection-manager/latency-monitor.ts +319 -0
- package/src/connection-manager/{visibility-change-emitter.js → visibility-change-emitter.ts} +48 -38
- package/src/constants.ts +31 -0
- package/src/content-routing/index.ts +143 -0
- package/src/content-routing/utils.ts +54 -0
- package/src/dht/dht-content-routing.ts +43 -0
- package/src/dht/dht-peer-routing.ts +35 -0
- package/src/dialer/auto-dialer.ts +40 -0
- package/src/dialer/{dial-request.js → dial-request.ts} +60 -51
- package/src/dialer/index.ts +374 -0
- package/src/errors.ts +71 -0
- package/src/fetch/README.md +1 -1
- package/src/fetch/constants.ts +3 -0
- package/src/fetch/{index.js → index.ts} +79 -70
- package/src/fetch/{proto.d.ts → pb/proto.d.ts} +0 -0
- package/src/fetch/pb/proto.js +331 -0
- package/src/fetch/{proto.proto → pb/proto.proto} +0 -0
- package/src/get-peer.ts +57 -0
- package/src/identify/consts.ts +13 -0
- package/src/identify/index.ts +445 -0
- package/{dist/src/identify → src/identify/pb}/message.d.ts +20 -5
- package/src/identify/pb/message.js +369 -0
- package/src/identify/{message.proto → pb/message.proto} +0 -0
- package/src/index.ts +234 -0
- package/src/insecure/index.ts +97 -0
- package/src/insecure/{proto.d.ts → pb/proto.d.ts} +7 -1
- package/src/insecure/pb/proto.js +388 -0
- package/src/insecure/{proto.proto → pb/proto.proto} +0 -0
- package/src/keychain/{cms.js → cms.ts} +62 -58
- package/src/keychain/index.ts +588 -0
- package/src/keychain/{util.js → util.ts} +7 -17
- package/src/libp2p.ts +501 -0
- package/src/metrics/index.ts +310 -0
- package/src/metrics/moving-average.ts +53 -0
- package/src/metrics/stats.ts +243 -0
- package/src/nat-manager.ts +194 -0
- package/src/peer-record-updater.ts +55 -0
- package/src/peer-routing.ts +185 -0
- package/src/ping/README.md +1 -1
- package/src/ping/constants.ts +5 -0
- package/src/ping/index.ts +83 -0
- package/src/pnet/README.md +2 -2
- package/src/pnet/crypto.ts +67 -0
- package/src/pnet/errors.ts +5 -0
- package/src/pnet/index.ts +95 -0
- package/src/pnet/key-generator.ts +28 -0
- package/src/registrar.ts +205 -0
- package/src/transport-manager.ts +279 -0
- package/src/upgrader.ts +499 -0
- package/src/version.ts +3 -0
- package/dist/index.min.js +0 -55
- package/dist/src/insecure/plaintext.d.ts +0 -12
- package/dist/src/insecure/plaintext.d.ts.map +0 -1
- package/dist/src/metrics/old-peers.d.ts +0 -3
- package/dist/src/metrics/old-peers.d.ts.map +0 -1
- package/dist/src/metrics/tracked-map.d.ts +0 -8
- package/dist/src/metrics/tracked-map.d.ts.map +0 -1
- package/dist/src/peer-store/address-book.d.ts +0 -67
- package/dist/src/peer-store/address-book.d.ts.map +0 -1
- package/dist/src/peer-store/index.d.ts +0 -58
- package/dist/src/peer-store/index.d.ts.map +0 -1
- package/dist/src/peer-store/key-book.d.ts +0 -40
- package/dist/src/peer-store/key-book.d.ts.map +0 -1
- package/dist/src/peer-store/metadata-book.d.ts +0 -58
- package/dist/src/peer-store/metadata-book.d.ts.map +0 -1
- package/dist/src/peer-store/pb/peer.d.ts +0 -222
- package/dist/src/peer-store/proto-book.d.ts +0 -43
- package/dist/src/peer-store/proto-book.d.ts.map +0 -1
- package/dist/src/peer-store/store.d.ts +0 -73
- package/dist/src/peer-store/store.d.ts.map +0 -1
- package/dist/src/peer-store/types.d.ts +0 -202
- package/dist/src/peer-store/types.d.ts.map +0 -1
- package/dist/src/ping/util.d.ts +0 -5
- package/dist/src/ping/util.d.ts.map +0 -1
- package/dist/src/pubsub-adapter.d.ts +0 -22
- package/dist/src/pubsub-adapter.d.ts.map +0 -1
- package/dist/src/record/envelope/envelope.d.ts +0 -77
- package/dist/src/record/envelope/index.d.ts +0 -80
- package/dist/src/record/envelope/index.d.ts.map +0 -1
- package/dist/src/record/peer-record/consts.d.ts +0 -4
- package/dist/src/record/peer-record/consts.d.ts.map +0 -1
- package/dist/src/record/peer-record/index.d.ts +0 -60
- package/dist/src/record/peer-record/index.d.ts.map +0 -1
- package/dist/src/record/peer-record/peer-record.d.ts +0 -133
- package/dist/src/record/utils.d.ts +0 -12
- package/dist/src/record/utils.d.ts.map +0 -1
- package/dist/src/types.d.ts +0 -90
- package/dist/src/types.d.ts.map +0 -1
- package/src/address-manager/index.js +0 -96
- package/src/circuit/auto-relay.js +0 -302
- package/src/circuit/circuit/hop.js +0 -205
- package/src/circuit/circuit/stop.js +0 -81
- package/src/circuit/circuit/stream-handler.js +0 -94
- package/src/circuit/constants.js +0 -12
- package/src/circuit/index.js +0 -102
- package/src/circuit/multicodec.js +0 -5
- package/src/circuit/transport.js +0 -229
- package/src/circuit/utils.js +0 -17
- package/src/config.js +0 -114
- package/src/connection-manager/auto-dialler.js +0 -132
- package/src/connection-manager/index.js +0 -363
- package/src/connection-manager/latency-monitor.js +0 -264
- package/src/constants.js +0 -18
- package/src/content-routing/index.js +0 -163
- package/src/content-routing/utils.js +0 -89
- package/src/dht/dht-content-routing.js +0 -44
- package/src/dht/dht-peer-routing.js +0 -51
- package/src/dialer/index.js +0 -376
- package/src/errors.js +0 -66
- package/src/fetch/constants.js +0 -6
- package/src/get-peer.js +0 -49
- package/src/identify/consts.js +0 -15
- package/src/identify/index.js +0 -384
- package/src/index.js +0 -811
- package/src/insecure/plaintext.js +0 -95
- package/src/keychain/index.js +0 -561
- package/src/metrics/index.js +0 -290
- package/src/metrics/old-peers.js +0 -16
- package/src/metrics/stats.js +0 -270
- package/src/metrics/tracked-map.js +0 -94
- package/src/nat-manager.js +0 -197
- package/src/peer-routing.js +0 -176
- package/src/peer-store/README.md +0 -145
- package/src/peer-store/address-book.js +0 -382
- package/src/peer-store/index.js +0 -121
- package/src/peer-store/key-book.js +0 -141
- package/src/peer-store/metadata-book.js +0 -250
- package/src/peer-store/pb/peer.d.ts +0 -222
- package/src/peer-store/pb/peer.js +0 -643
- package/src/peer-store/pb/peer.proto +0 -31
- package/src/peer-store/proto-book.js +0 -237
- package/src/peer-store/store.js +0 -263
- package/src/peer-store/types.ts +0 -245
- package/src/ping/constants.js +0 -8
- package/src/ping/index.js +0 -85
- package/src/ping/util.js +0 -18
- package/src/pnet/crypto.js +0 -84
- package/src/pnet/errors.js +0 -7
- package/src/pnet/index.js +0 -86
- package/src/pnet/key-generator.js +0 -33
- package/src/pubsub-adapter.js +0 -61
- package/src/record/README.md +0 -130
- package/src/record/envelope/envelope.d.ts +0 -77
- package/src/record/envelope/envelope.js +0 -243
- package/src/record/envelope/envelope.proto +0 -19
- package/src/record/envelope/index.js +0 -183
- package/src/record/peer-record/consts.js +0 -14
- package/src/record/peer-record/index.js +0 -113
- package/src/record/peer-record/peer-record.d.ts +0 -133
- package/src/record/peer-record/peer-record.js +0 -367
- package/src/record/peer-record/peer-record.proto +0 -18
- package/src/record/utils.js +0 -25
- package/src/registrar.js +0 -127
- package/src/transport-manager.js +0 -269
- package/src/types.ts +0 -98
- package/src/upgrader.js +0 -492
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
|
2
|
+
import errCode from 'err-code'
|
|
3
|
+
import * as mafmt from '@multiformats/mafmt'
|
|
4
|
+
import { Multiaddr } from '@multiformats/multiaddr'
|
|
5
|
+
import { CircuitRelay as CircuitPB } from './pb/index.js'
|
|
6
|
+
import { codes } from '../errors.js'
|
|
7
|
+
import { streamToMaConnection } from '@libp2p/utils/stream-to-ma-conn'
|
|
8
|
+
import { RELAY_CODEC } from './multicodec.js'
|
|
9
|
+
import { createListener } from './listener.js'
|
|
10
|
+
import { handleCanHop, handleHop, hop } from './circuit/hop.js'
|
|
11
|
+
import { handleStop } from './circuit/stop.js'
|
|
12
|
+
import { StreamHandler } from './circuit/stream-handler.js'
|
|
13
|
+
import { symbol } from '@libp2p/interfaces/transport'
|
|
14
|
+
import { peerIdFromString } from '@libp2p/peer-id'
|
|
15
|
+
import { Components, Initializable } from '@libp2p/interfaces/components'
|
|
16
|
+
import type { AbortOptions } from '@libp2p/interfaces'
|
|
17
|
+
import type { IncomingStreamData } from '@libp2p/interfaces/registrar'
|
|
18
|
+
import type { Listener, Transport, CreateListenerOptions, ConnectionHandler } from '@libp2p/interfaces/transport'
|
|
19
|
+
import type { Connection } from '@libp2p/interfaces/connection'
|
|
20
|
+
|
|
21
|
+
const log = logger('libp2p:circuit')
|
|
22
|
+
|
|
23
|
+
export class Circuit implements Transport, Initializable {
|
|
24
|
+
private handler?: ConnectionHandler
|
|
25
|
+
private components: Components = new Components()
|
|
26
|
+
|
|
27
|
+
init (components: Components): void {
|
|
28
|
+
this.components = components
|
|
29
|
+
void this.components.getRegistrar().handle(RELAY_CODEC, (data) => {
|
|
30
|
+
void this._onProtocol(data).catch(err => {
|
|
31
|
+
log.error(err)
|
|
32
|
+
})
|
|
33
|
+
})
|
|
34
|
+
.catch(err => {
|
|
35
|
+
log.error(err)
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
hopEnabled () {
|
|
40
|
+
return true
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
hopActive () {
|
|
44
|
+
return true
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
get [symbol] (): true {
|
|
48
|
+
return true
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
get [Symbol.toStringTag] () {
|
|
52
|
+
return this.constructor.name
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async _onProtocol (data: IncomingStreamData) {
|
|
56
|
+
const { connection, stream } = data
|
|
57
|
+
const streamHandler = new StreamHandler({ stream })
|
|
58
|
+
const request = await streamHandler.read()
|
|
59
|
+
|
|
60
|
+
if (request == null) {
|
|
61
|
+
log('request was invalid, could not read from stream')
|
|
62
|
+
streamHandler.write({
|
|
63
|
+
type: CircuitPB.Type.STATUS,
|
|
64
|
+
code: CircuitPB.Status.MALFORMED_MESSAGE
|
|
65
|
+
})
|
|
66
|
+
streamHandler.close()
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let virtualConnection
|
|
71
|
+
|
|
72
|
+
switch (request.type) {
|
|
73
|
+
case CircuitPB.Type.CAN_HOP: {
|
|
74
|
+
log('received CAN_HOP request from %p', connection.remotePeer)
|
|
75
|
+
await handleCanHop({ circuit: this, connection, streamHandler })
|
|
76
|
+
break
|
|
77
|
+
}
|
|
78
|
+
case CircuitPB.Type.HOP: {
|
|
79
|
+
log('received HOP request from %p', connection.remotePeer)
|
|
80
|
+
virtualConnection = await handleHop({
|
|
81
|
+
connection,
|
|
82
|
+
request,
|
|
83
|
+
streamHandler,
|
|
84
|
+
circuit: this,
|
|
85
|
+
connectionManager: this.components.getConnectionManager()
|
|
86
|
+
})
|
|
87
|
+
break
|
|
88
|
+
}
|
|
89
|
+
case CircuitPB.Type.STOP: {
|
|
90
|
+
log('received STOP request from %p', connection.remotePeer)
|
|
91
|
+
virtualConnection = await handleStop({
|
|
92
|
+
connection,
|
|
93
|
+
request,
|
|
94
|
+
streamHandler
|
|
95
|
+
})
|
|
96
|
+
break
|
|
97
|
+
}
|
|
98
|
+
default: {
|
|
99
|
+
log('Request of type %s not supported', request.type)
|
|
100
|
+
streamHandler.write({
|
|
101
|
+
type: CircuitPB.Type.STATUS,
|
|
102
|
+
code: CircuitPB.Status.MALFORMED_MESSAGE
|
|
103
|
+
})
|
|
104
|
+
streamHandler.close()
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (virtualConnection != null) {
|
|
110
|
+
// @ts-expect-error dst peer will not be undefined
|
|
111
|
+
const remoteAddr = new Multiaddr(request.dstPeer.addrs[0])
|
|
112
|
+
// @ts-expect-error dst peer will not be undefined
|
|
113
|
+
const localAddr = new Multiaddr(request.srcPeer.addrs[0])
|
|
114
|
+
const maConn = streamToMaConnection({
|
|
115
|
+
stream: virtualConnection,
|
|
116
|
+
remoteAddr,
|
|
117
|
+
localAddr
|
|
118
|
+
})
|
|
119
|
+
const type = request.type === CircuitPB.Type.HOP ? 'relay' : 'inbound'
|
|
120
|
+
log('new %s connection %s', type, maConn.remoteAddr)
|
|
121
|
+
|
|
122
|
+
const conn = await this.components.getUpgrader().upgradeInbound(maConn)
|
|
123
|
+
log('%s connection %s upgraded', type, maConn.remoteAddr)
|
|
124
|
+
|
|
125
|
+
if (this.handler != null) {
|
|
126
|
+
this.handler(conn)
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Dial a peer over a relay
|
|
133
|
+
*/
|
|
134
|
+
async dial (ma: Multiaddr, options: AbortOptions = {}): Promise<Connection> {
|
|
135
|
+
// Check the multiaddr to see if it contains a relay and a destination peer
|
|
136
|
+
const addrs = ma.toString().split('/p2p-circuit')
|
|
137
|
+
const relayAddr = new Multiaddr(addrs[0])
|
|
138
|
+
const destinationAddr = new Multiaddr(addrs[addrs.length - 1])
|
|
139
|
+
const relayId = relayAddr.getPeerId()
|
|
140
|
+
const destinationId = destinationAddr.getPeerId()
|
|
141
|
+
|
|
142
|
+
if (relayId == null || destinationId == null) {
|
|
143
|
+
const errMsg = 'Circuit relay dial failed as addresses did not have peer id'
|
|
144
|
+
log.error(errMsg)
|
|
145
|
+
throw errCode(new Error(errMsg), codes.ERR_RELAYED_DIAL)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const relayPeer = peerIdFromString(relayId)
|
|
149
|
+
const destinationPeer = peerIdFromString(destinationId)
|
|
150
|
+
|
|
151
|
+
let disconnectOnFailure = false
|
|
152
|
+
let relayConnection = this.components.getConnectionManager().getConnection(relayPeer)
|
|
153
|
+
if (relayConnection == null) {
|
|
154
|
+
relayConnection = await this.components.getDialer().dial(relayAddr, options)
|
|
155
|
+
disconnectOnFailure = true
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
try {
|
|
159
|
+
const virtualConnection = await hop({
|
|
160
|
+
connection: relayConnection,
|
|
161
|
+
request: {
|
|
162
|
+
type: CircuitPB.Type.HOP,
|
|
163
|
+
srcPeer: {
|
|
164
|
+
id: this.components.getPeerId().toBytes(),
|
|
165
|
+
addrs: this.components.getAddressManager().getAddresses().map(addr => addr.bytes)
|
|
166
|
+
},
|
|
167
|
+
dstPeer: {
|
|
168
|
+
id: destinationPeer.toBytes(),
|
|
169
|
+
addrs: [new Multiaddr(destinationAddr).bytes]
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
const localAddr = relayAddr.encapsulate(`/p2p-circuit/p2p/${this.components.getPeerId().toString()}`)
|
|
175
|
+
const maConn = streamToMaConnection({
|
|
176
|
+
stream: virtualConnection,
|
|
177
|
+
remoteAddr: ma,
|
|
178
|
+
localAddr
|
|
179
|
+
})
|
|
180
|
+
log('new outbound connection %s', maConn.remoteAddr)
|
|
181
|
+
|
|
182
|
+
return await this.components.getUpgrader().upgradeOutbound(maConn)
|
|
183
|
+
} catch (err: any) {
|
|
184
|
+
log.error('Circuit relay dial failed', err)
|
|
185
|
+
disconnectOnFailure && await relayConnection.close()
|
|
186
|
+
throw err
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Create a listener
|
|
192
|
+
*/
|
|
193
|
+
createListener (options: CreateListenerOptions): Listener {
|
|
194
|
+
// Called on successful HOP and STOP requests
|
|
195
|
+
this.handler = options.handler
|
|
196
|
+
|
|
197
|
+
return createListener({
|
|
198
|
+
dialer: this.components.getDialer(),
|
|
199
|
+
connectionManager: this.components.getConnectionManager()
|
|
200
|
+
})
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Filter check for all Multiaddrs that this transport can dial on
|
|
205
|
+
*
|
|
206
|
+
* @param {Multiaddr[]} multiaddrs
|
|
207
|
+
* @returns {Multiaddr[]}
|
|
208
|
+
*/
|
|
209
|
+
filter (multiaddrs: Multiaddr[]): Multiaddr[] {
|
|
210
|
+
multiaddrs = Array.isArray(multiaddrs) ? multiaddrs : [multiaddrs]
|
|
211
|
+
|
|
212
|
+
return multiaddrs.filter((ma) => {
|
|
213
|
+
return mafmt.Circuit.matches(ma)
|
|
214
|
+
})
|
|
215
|
+
}
|
|
216
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { CID } from 'multiformats/cid'
|
|
2
|
+
import { sha256 } from 'multiformats/hashes/sha2'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Convert a namespace string into a cid
|
|
6
|
+
*/
|
|
7
|
+
export async function namespaceToCid (namespace: string): Promise<CID> {
|
|
8
|
+
const bytes = new TextEncoder().encode(namespace)
|
|
9
|
+
const hash = await sha256.digest(bytes)
|
|
10
|
+
|
|
11
|
+
return CID.createV0(hash)
|
|
12
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import mergeOptions from 'merge-options'
|
|
2
|
+
import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
|
|
3
|
+
import * as Constants from './constants.js'
|
|
4
|
+
import { AGENT_VERSION } from './identify/consts.js'
|
|
5
|
+
import * as RelayConstants from './circuit/constants.js'
|
|
6
|
+
import { publicAddressesFirst } from '@libp2p/utils/address-sort'
|
|
7
|
+
import { FAULT_TOLERANCE } from './transport-manager.js'
|
|
8
|
+
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
9
|
+
import type { Libp2pInit } from './index.js'
|
|
10
|
+
import { codes, messages } from './errors.js'
|
|
11
|
+
import errCode from 'err-code'
|
|
12
|
+
import type { RecursivePartial } from '@libp2p/interfaces'
|
|
13
|
+
|
|
14
|
+
const DefaultConfig: Partial<Libp2pInit> = {
|
|
15
|
+
addresses: {
|
|
16
|
+
listen: [],
|
|
17
|
+
announce: [],
|
|
18
|
+
noAnnounce: [],
|
|
19
|
+
announceFilter: (multiaddrs: Multiaddr[]) => multiaddrs
|
|
20
|
+
},
|
|
21
|
+
connectionManager: {
|
|
22
|
+
maxConnections: 300,
|
|
23
|
+
minConnections: 50,
|
|
24
|
+
autoDialInterval: 10000,
|
|
25
|
+
autoDial: true
|
|
26
|
+
},
|
|
27
|
+
connectionGater: {},
|
|
28
|
+
transportManager: {
|
|
29
|
+
faultTolerance: FAULT_TOLERANCE.FATAL_ALL
|
|
30
|
+
},
|
|
31
|
+
dialer: {
|
|
32
|
+
maxParallelDials: Constants.MAX_PARALLEL_DIALS,
|
|
33
|
+
maxDialsPerPeer: Constants.MAX_PER_PEER_DIALS,
|
|
34
|
+
dialTimeout: Constants.DIAL_TIMEOUT,
|
|
35
|
+
resolvers: {
|
|
36
|
+
dnsaddr: dnsaddrResolver
|
|
37
|
+
},
|
|
38
|
+
addressSorter: publicAddressesFirst
|
|
39
|
+
},
|
|
40
|
+
host: {
|
|
41
|
+
agentVersion: AGENT_VERSION
|
|
42
|
+
},
|
|
43
|
+
metrics: {
|
|
44
|
+
enabled: false,
|
|
45
|
+
computeThrottleMaxQueueSize: 1000,
|
|
46
|
+
computeThrottleTimeout: 2000,
|
|
47
|
+
movingAverageIntervals: [
|
|
48
|
+
60 * 1000, // 1 minute
|
|
49
|
+
5 * 60 * 1000, // 5 minutes
|
|
50
|
+
15 * 60 * 1000 // 15 minutes
|
|
51
|
+
],
|
|
52
|
+
maxOldPeersRetention: 50
|
|
53
|
+
},
|
|
54
|
+
peerRouting: {
|
|
55
|
+
refreshManager: {
|
|
56
|
+
enabled: true,
|
|
57
|
+
interval: 6e5,
|
|
58
|
+
bootDelay: 10e3
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
protocolPrefix: 'ipfs',
|
|
62
|
+
nat: {
|
|
63
|
+
enabled: true,
|
|
64
|
+
ttl: 7200,
|
|
65
|
+
keepAlive: true
|
|
66
|
+
},
|
|
67
|
+
relay: {
|
|
68
|
+
enabled: true,
|
|
69
|
+
advertise: {
|
|
70
|
+
bootDelay: RelayConstants.ADVERTISE_BOOT_DELAY,
|
|
71
|
+
enabled: false,
|
|
72
|
+
ttl: RelayConstants.ADVERTISE_TTL
|
|
73
|
+
},
|
|
74
|
+
hop: {
|
|
75
|
+
enabled: false,
|
|
76
|
+
active: false
|
|
77
|
+
},
|
|
78
|
+
autoRelay: {
|
|
79
|
+
enabled: false,
|
|
80
|
+
maxListeners: 2
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function validateConfig (opts: RecursivePartial<Libp2pInit>): Libp2pInit {
|
|
86
|
+
const resultingOptions: Libp2pInit = mergeOptions(DefaultConfig, opts)
|
|
87
|
+
|
|
88
|
+
if (resultingOptions.transports == null || resultingOptions.transports.length < 1) {
|
|
89
|
+
throw errCode(new Error(messages.ERR_TRANSPORTS_REQUIRED), codes.ERR_TRANSPORTS_REQUIRED)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (resultingOptions.connectionEncryption == null || resultingOptions.connectionEncryption.length === 0) {
|
|
93
|
+
throw errCode(new Error(messages.CONN_ENCRYPTION_REQUIRED), codes.CONN_ENCRYPTION_REQUIRED)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (resultingOptions.connectionProtector === null && globalThis.process?.env?.LIBP2P_FORCE_PNET != null) { // eslint-disable-line no-undef
|
|
97
|
+
throw errCode(new Error(messages.ERR_PROTECTOR_REQUIRED), codes.ERR_PROTECTOR_REQUIRED)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return resultingOptions
|
|
101
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
|
2
|
+
import mergeOptions from 'merge-options'
|
|
3
|
+
// @ts-expect-error retimer does not have types
|
|
4
|
+
import retimer from 'retimer'
|
|
5
|
+
import all from 'it-all'
|
|
6
|
+
import { pipe } from 'it-pipe'
|
|
7
|
+
import filter from 'it-filter'
|
|
8
|
+
import sort from 'it-sort'
|
|
9
|
+
import type { Startable } from '@libp2p/interfaces'
|
|
10
|
+
import type { Components } from '@libp2p/interfaces/components'
|
|
11
|
+
|
|
12
|
+
const log = logger('libp2p:connection-manager:auto-dialler')
|
|
13
|
+
|
|
14
|
+
export interface AutoDiallerInit {
|
|
15
|
+
/**
|
|
16
|
+
* Should preemptively guarantee connections are above the low watermark
|
|
17
|
+
*/
|
|
18
|
+
enabled?: boolean
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The minimum number of connections to avoid pruning
|
|
22
|
+
*/
|
|
23
|
+
minConnections?: number
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* How often, in milliseconds, it should preemptively guarantee connections are above the low watermark
|
|
27
|
+
*/
|
|
28
|
+
autoDialInterval?: number
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const defaultOptions: Partial<AutoDiallerInit> = {
|
|
32
|
+
enabled: true,
|
|
33
|
+
minConnections: 0,
|
|
34
|
+
autoDialInterval: 10000
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export class AutoDialler implements Startable {
|
|
38
|
+
private readonly components: Components
|
|
39
|
+
private readonly options: Required<AutoDiallerInit>
|
|
40
|
+
private running: boolean
|
|
41
|
+
private autoDialTimeout?: ReturnType<retimer>
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Proactively tries to connect to known peers stored in the PeerStore.
|
|
45
|
+
* It will keep the number of connections below the upper limit and sort
|
|
46
|
+
* the peers to connect based on wether we know their keys and protocols.
|
|
47
|
+
*/
|
|
48
|
+
constructor (components: Components, init: AutoDiallerInit) {
|
|
49
|
+
this.components = components
|
|
50
|
+
this.options = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, init)
|
|
51
|
+
this.running = false
|
|
52
|
+
this._autoDial = this._autoDial.bind(this)
|
|
53
|
+
|
|
54
|
+
log('options: %j', this.options)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
isStarted () {
|
|
58
|
+
return this.running
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Starts the auto dialer
|
|
63
|
+
*/
|
|
64
|
+
async start () {
|
|
65
|
+
if (!this.options.enabled) {
|
|
66
|
+
log('not enabled')
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
this.running = true
|
|
71
|
+
|
|
72
|
+
void this._autoDial().catch(err => {
|
|
73
|
+
log.error('could start autodial', err)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
log('started')
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Stops the auto dialler
|
|
81
|
+
*/
|
|
82
|
+
async stop () {
|
|
83
|
+
if (!this.options.enabled) {
|
|
84
|
+
log('not enabled')
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
this.running = false
|
|
89
|
+
|
|
90
|
+
if (this.autoDialTimeout != null) {
|
|
91
|
+
this.autoDialTimeout.clear()
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
log('stopped')
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async _autoDial () {
|
|
98
|
+
if (this.autoDialTimeout != null) {
|
|
99
|
+
this.autoDialTimeout.clear()
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const minConnections = this.options.minConnections
|
|
103
|
+
|
|
104
|
+
// Already has enough connections
|
|
105
|
+
if (this.components.getConnectionManager().getConnectionList().length >= minConnections) {
|
|
106
|
+
this.autoDialTimeout = retimer(this._autoDial, this.options.autoDialInterval)
|
|
107
|
+
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Sort peers on whether we know protocols or public keys for them
|
|
112
|
+
const allPeers = await this.components.getPeerStore().all()
|
|
113
|
+
|
|
114
|
+
const peers = await pipe(
|
|
115
|
+
// shuffle the peers
|
|
116
|
+
allPeers.sort(() => Math.random() > 0.5 ? 1 : -1),
|
|
117
|
+
(source) => filter(source, (peer) => !peer.id.equals(this.components.getPeerId())),
|
|
118
|
+
(source) => sort(source, (a, b) => {
|
|
119
|
+
if (b.protocols.length > a.protocols.length) {
|
|
120
|
+
return 1
|
|
121
|
+
} else if (b.id.publicKey != null && a.id.publicKey == null) {
|
|
122
|
+
return 1
|
|
123
|
+
}
|
|
124
|
+
return -1
|
|
125
|
+
}),
|
|
126
|
+
async (source) => await all(source)
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
for (let i = 0; this.running && i < peers.length && this.components.getConnectionManager().getConnectionList().length < minConnections; i++) {
|
|
130
|
+
// Connection Manager was stopped during async dial
|
|
131
|
+
if (!this.running) {
|
|
132
|
+
return
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const peer = peers[i]
|
|
136
|
+
|
|
137
|
+
if (this.components.getConnectionManager().getConnection(peer.id) == null) {
|
|
138
|
+
log('connecting to a peerStore stored peer %p', peer.id)
|
|
139
|
+
try {
|
|
140
|
+
await this.components.getDialer().dial(peer.id)
|
|
141
|
+
} catch (err: any) {
|
|
142
|
+
log.error('could not connect to peerStore stored peer', err)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Connection Manager was stopped
|
|
148
|
+
if (!this.running) {
|
|
149
|
+
return
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
this.autoDialTimeout = retimer(this._autoDial, this.options.autoDialInterval)
|
|
153
|
+
}
|
|
154
|
+
}
|