libp2p 0.36.2 → 0.37.0
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 +34 -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 +216 -0
- package/dist/src/circuit/auto-relay.js.map +1 -0
- package/dist/src/circuit/circuit/hop.d.ts +26 -53
- 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 +18 -10
- 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 +19 -38
- package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stream-handler.js +59 -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 +62 -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/pb/index.d.ts +52 -0
- package/dist/src/circuit/pb/index.d.ts.map +1 -0
- package/dist/src/circuit/pb/index.js +98 -0
- package/dist/src/circuit/pb/index.js.map +1 -0
- 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 +192 -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 +91 -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/dialer/auto-dialer.d.ts +16 -0
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +1 -0
- package/dist/src/connection-manager/dialer/auto-dialer.js +37 -0
- package/dist/src/connection-manager/dialer/auto-dialer.js.map +1 -0
- package/dist/src/connection-manager/dialer/dial-request.d.ts +27 -0
- package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -0
- package/dist/src/connection-manager/dialer/dial-request.js +83 -0
- package/dist/src/connection-manager/dialer/dial-request.js.map +1 -0
- package/dist/src/connection-manager/dialer/index.d.ts +104 -0
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -0
- package/dist/src/connection-manager/dialer/index.js +267 -0
- package/dist/src/connection-manager/dialer/index.js.map +1 -0
- package/dist/src/connection-manager/index.d.ts +102 -136
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +333 -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 +27 -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/dht/dummy-dht.d.ts +20 -0
- package/dist/src/dht/dummy-dht.d.ts.map +1 -0
- package/dist/src/dht/dummy-dht.js +46 -0
- package/dist/src/dht/dummy-dht.js.map +1 -0
- package/dist/src/errors.d.ts +70 -63
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +75 -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 +129 -0
- package/dist/src/fetch/index.js.map +1 -0
- package/dist/src/fetch/pb/proto.d.ts +27 -0
- package/dist/src/fetch/pb/proto.d.ts.map +1 -0
- package/dist/src/fetch/pb/proto.js +50 -0
- package/dist/src/fetch/pb/proto.js.map +1 -0
- 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 +42 -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/dist/src/identify/pb/message.d.ts +16 -0
- package/dist/src/identify/pb/message.d.ts.map +1 -0
- package/dist/src/identify/pb/message.js +24 -0
- package/dist/src/identify/pb/message.js.map +1 -0
- package/dist/src/index.d.ts +150 -496
- 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 +81 -0
- package/dist/src/insecure/index.js.map +1 -0
- package/dist/src/insecure/pb/proto.d.ts +29 -0
- package/dist/src/insecure/pb/proto.d.ts.map +1 -0
- package/dist/src/insecure/pb/proto.js +53 -0
- package/dist/src/insecure/pb/proto.js.map +1 -0
- 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 +60 -81
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/keychain/index.js +492 -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 +80 -0
- package/dist/src/libp2p.d.ts.map +1 -0
- package/dist/src/libp2p.js +397 -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 +41 -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 +40 -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 +17 -31
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +57 -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 +12 -20
- package/dist/src/pnet/index.d.ts.map +1 -1
- package/dist/src/pnet/index.js +67 -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/pubsub/dummy-pubsub.d.ts +17 -0
- package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -0
- package/dist/src/pubsub/dummy-pubsub.js +37 -0
- package/dist/src/pubsub/dummy-pubsub.js.map +1 -0
- package/dist/src/registrar.d.ts +26 -65
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +144 -0
- package/dist/src/registrar.js.map +1 -0
- package/dist/src/transport-manager.d.ts +36 -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 +144 -201
- package/src/address-manager/index.ts +130 -0
- package/src/circuit/README.md +16 -11
- package/src/circuit/auto-relay.ts +286 -0
- package/src/circuit/circuit/hop.ts +211 -0
- package/src/circuit/circuit/stop.ts +78 -0
- package/src/circuit/circuit/stream-handler.ts +86 -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.ts +82 -0
- package/src/circuit/multicodec.ts +2 -0
- package/src/circuit/{protocol → pb}/index.proto +1 -1
- package/src/circuit/pb/index.ts +117 -0
- package/src/circuit/transport.ts +219 -0
- package/src/circuit/utils.ts +12 -0
- package/src/config.ts +99 -0
- package/src/connection-manager/auto-dialler.ts +154 -0
- package/src/connection-manager/dialer/auto-dialer.ts +59 -0
- package/src/{dialer/dial-request.js → connection-manager/dialer/dial-request.ts} +60 -51
- package/src/connection-manager/dialer/index.ts +382 -0
- package/src/connection-manager/index.ts +508 -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 +144 -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/dht/dummy-dht.ts +60 -0
- package/src/errors.ts +73 -0
- package/src/fetch/README.md +1 -1
- package/src/fetch/constants.ts +3 -0
- package/src/fetch/index.ts +167 -0
- package/src/fetch/{proto.proto → pb/proto.proto} +0 -0
- package/src/fetch/pb/proto.ts +65 -0
- package/src/get-peer.ts +57 -0
- package/src/identify/consts.ts +13 -0
- package/src/identify/index.ts +445 -0
- package/src/identify/{message.proto → pb/message.proto} +0 -0
- package/src/identify/pb/message.ts +37 -0
- package/src/index.ts +220 -0
- package/src/insecure/index.ts +101 -0
- package/src/insecure/{proto.proto → pb/proto.proto} +0 -0
- package/src/insecure/pb/proto.ts +68 -0
- package/src/keychain/{cms.js → cms.ts} +62 -58
- package/src/keychain/index.ts +587 -0
- package/src/keychain/{util.js → util.ts} +7 -17
- package/src/libp2p.ts +538 -0
- package/src/metrics/index.ts +310 -0
- package/src/metrics/moving-average.ts +53 -0
- package/src/metrics/stats.ts +238 -0
- package/src/nat-manager.ts +194 -0
- package/src/peer-record-updater.ts +55 -0
- package/src/peer-routing.ts +186 -0
- package/src/ping/README.md +1 -1
- package/src/ping/constants.ts +5 -0
- package/src/ping/index.ts +84 -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 +97 -0
- package/src/pnet/key-generator.ts +28 -0
- package/src/pubsub/dummy-pubsub.ts +51 -0
- package/src/registrar.ts +189 -0
- package/src/transport-manager.ts +281 -0
- package/src/upgrader.ts +499 -0
- package/src/version.ts +3 -0
- package/dist/index.min.js +0 -55
- package/dist/src/circuit/protocol/index.d.ts +0 -173
- package/dist/src/dialer/dial-request.d.ts +0 -55
- package/dist/src/dialer/dial-request.d.ts.map +0 -1
- package/dist/src/dialer/index.d.ts +0 -182
- package/dist/src/dialer/index.d.ts.map +0 -1
- package/dist/src/fetch/proto.d.ts +0 -134
- package/dist/src/identify/message.d.ts +0 -95
- package/dist/src/insecure/plaintext.d.ts +0 -12
- package/dist/src/insecure/plaintext.d.ts.map +0 -1
- package/dist/src/insecure/proto.d.ts +0 -128
- 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/listener.js +0 -75
- package/src/circuit/multicodec.js +0 -5
- package/src/circuit/protocol/index.d.ts +0 -173
- package/src/circuit/protocol/index.js +0 -530
- 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 -374
- 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/fetch/index.js +0 -159
- package/src/fetch/proto.d.ts +0 -134
- package/src/fetch/proto.js +0 -333
- package/src/get-peer.js +0 -49
- package/src/identify/consts.js +0 -15
- package/src/identify/index.js +0 -384
- package/src/identify/message.d.ts +0 -95
- package/src/identify/message.js +0 -328
- package/src/index.js +0 -813
- package/src/insecure/plaintext.js +0 -95
- package/src/insecure/proto.d.ts +0 -128
- package/src/insecure/proto.js +0 -371
- 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 -84
- 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,286 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
|
2
|
+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
3
|
+
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
4
|
+
import { RELAY_CODEC } from './multicodec.js'
|
|
5
|
+
import { canHop } from './circuit/hop.js'
|
|
6
|
+
import { namespaceToCid } from './utils.js'
|
|
7
|
+
import {
|
|
8
|
+
CIRCUIT_PROTO_CODE,
|
|
9
|
+
HOP_METADATA_KEY,
|
|
10
|
+
HOP_METADATA_VALUE,
|
|
11
|
+
RELAY_RENDEZVOUS_NS
|
|
12
|
+
} from './constants.js'
|
|
13
|
+
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
14
|
+
import type { AddressSorter, PeerProtocolsChangeData } from '@libp2p/interfaces/peer-store'
|
|
15
|
+
import type { Connection } from '@libp2p/interfaces/connection'
|
|
16
|
+
import type { Components } from '@libp2p/interfaces/components'
|
|
17
|
+
import sort from 'it-sort'
|
|
18
|
+
import all from 'it-all'
|
|
19
|
+
import { pipe } from 'it-pipe'
|
|
20
|
+
import { publicAddressesFirst } from '@libp2p/utils/address-sort'
|
|
21
|
+
|
|
22
|
+
const log = logger('libp2p:auto-relay')
|
|
23
|
+
|
|
24
|
+
const noop = () => {}
|
|
25
|
+
|
|
26
|
+
export interface AutoRelayInit {
|
|
27
|
+
addressSorter?: AddressSorter
|
|
28
|
+
maxListeners?: number
|
|
29
|
+
onError?: (error: Error, msg?: string) => void
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class AutoRelay {
|
|
33
|
+
private readonly components: Components
|
|
34
|
+
private readonly addressSorter: AddressSorter
|
|
35
|
+
private readonly maxListeners: number
|
|
36
|
+
private readonly listenRelays: Set<string>
|
|
37
|
+
private readonly onError: (error: Error, msg?: string) => void
|
|
38
|
+
|
|
39
|
+
constructor (components: Components, init: AutoRelayInit) {
|
|
40
|
+
this.components = components
|
|
41
|
+
this.addressSorter = init.addressSorter ?? publicAddressesFirst
|
|
42
|
+
this.maxListeners = init.maxListeners ?? 1
|
|
43
|
+
this.listenRelays = new Set()
|
|
44
|
+
this.onError = init.onError ?? noop
|
|
45
|
+
|
|
46
|
+
this._onProtocolChange = this._onProtocolChange.bind(this)
|
|
47
|
+
this._onPeerDisconnected = this._onPeerDisconnected.bind(this)
|
|
48
|
+
|
|
49
|
+
this.components.getPeerStore().addEventListener('change:protocols', (evt) => {
|
|
50
|
+
void this._onProtocolChange(evt).catch(err => {
|
|
51
|
+
log.error(err)
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
this.components.getConnectionManager().addEventListener('peer:disconnect', this._onPeerDisconnected)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Check if a peer supports the relay protocol.
|
|
59
|
+
* If the protocol is not supported, check if it was supported before and remove it as a listen relay.
|
|
60
|
+
* If the protocol is supported, check if the peer supports **HOP** and add it as a listener if
|
|
61
|
+
* inside the threshold.
|
|
62
|
+
*/
|
|
63
|
+
async _onProtocolChange (evt: CustomEvent<PeerProtocolsChangeData>) {
|
|
64
|
+
const {
|
|
65
|
+
peerId,
|
|
66
|
+
protocols
|
|
67
|
+
} = evt.detail
|
|
68
|
+
const id = peerId.toString()
|
|
69
|
+
|
|
70
|
+
// Check if it has the protocol
|
|
71
|
+
const hasProtocol = protocols.find(protocol => protocol === RELAY_CODEC)
|
|
72
|
+
|
|
73
|
+
// If no protocol, check if we were keeping the peer before as a listenRelay
|
|
74
|
+
if (hasProtocol == null) {
|
|
75
|
+
if (this.listenRelays.has(id)) {
|
|
76
|
+
await this._removeListenRelay(id)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (this.listenRelays.has(id)) {
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// If protocol, check if can hop, store info in the metadataBook and listen on it
|
|
87
|
+
try {
|
|
88
|
+
const connections = this.components.getConnectionManager().getConnections(peerId)
|
|
89
|
+
|
|
90
|
+
if (connections.length === 0) {
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const connection = connections[0]
|
|
95
|
+
|
|
96
|
+
// Do not hop on a relayed connection
|
|
97
|
+
if (connection.remoteAddr.protoCodes().includes(CIRCUIT_PROTO_CODE)) {
|
|
98
|
+
log(`relayed connection to ${id} will not be used to hop on`)
|
|
99
|
+
return
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const supportsHop = await canHop({ connection })
|
|
103
|
+
|
|
104
|
+
if (supportsHop) {
|
|
105
|
+
await this.components.getPeerStore().metadataBook.setValue(peerId, HOP_METADATA_KEY, uint8ArrayFromString(HOP_METADATA_VALUE))
|
|
106
|
+
await this._addListenRelay(connection, id)
|
|
107
|
+
}
|
|
108
|
+
} catch (err: any) {
|
|
109
|
+
this.onError(err)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Peer disconnects
|
|
115
|
+
*/
|
|
116
|
+
_onPeerDisconnected (evt: CustomEvent<Connection>) {
|
|
117
|
+
const connection = evt.detail
|
|
118
|
+
const peerId = connection.remotePeer
|
|
119
|
+
const id = peerId.toString()
|
|
120
|
+
|
|
121
|
+
// Not listening on this relay
|
|
122
|
+
if (!this.listenRelays.has(id)) {
|
|
123
|
+
return
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
this._removeListenRelay(id).catch(err => {
|
|
127
|
+
log.error(err)
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Attempt to listen on the given relay connection
|
|
133
|
+
*/
|
|
134
|
+
async _addListenRelay (connection: Connection, id: string): Promise<void> {
|
|
135
|
+
try {
|
|
136
|
+
// Check if already listening on enough relays
|
|
137
|
+
if (this.listenRelays.size >= this.maxListeners) {
|
|
138
|
+
return
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Get peer known addresses and sort them with public addresses first
|
|
142
|
+
const remoteAddrs = await pipe(
|
|
143
|
+
await this.components.getPeerStore().addressBook.get(connection.remotePeer),
|
|
144
|
+
(source) => sort(source, this.addressSorter),
|
|
145
|
+
async (source) => await all(source)
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
// Attempt to listen on relay
|
|
149
|
+
const result = await Promise.all(
|
|
150
|
+
remoteAddrs.map(async addr => {
|
|
151
|
+
try {
|
|
152
|
+
let multiaddr = addr.multiaddr
|
|
153
|
+
|
|
154
|
+
if (multiaddr.getPeerId() == null) {
|
|
155
|
+
multiaddr = multiaddr.encapsulate(`/p2p/${connection.remotePeer.toString()}`)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
multiaddr = multiaddr.encapsulate('/p2p-circuit')
|
|
159
|
+
|
|
160
|
+
// Announce multiaddrs will update on listen success by TransportManager event being triggered
|
|
161
|
+
await this.components.getTransportManager().listen([multiaddr])
|
|
162
|
+
return true
|
|
163
|
+
} catch (err: any) {
|
|
164
|
+
log.error('error listening on circuit address', err)
|
|
165
|
+
this.onError(err)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return false
|
|
169
|
+
})
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
if (result.includes(true)) {
|
|
173
|
+
this.listenRelays.add(id)
|
|
174
|
+
}
|
|
175
|
+
} catch (err: any) {
|
|
176
|
+
this.onError(err)
|
|
177
|
+
this.listenRelays.delete(id)
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Remove listen relay
|
|
183
|
+
*/
|
|
184
|
+
async _removeListenRelay (id: string) {
|
|
185
|
+
if (this.listenRelays.delete(id)) {
|
|
186
|
+
// TODO: this should be responsibility of the connMgr
|
|
187
|
+
await this._listenOnAvailableHopRelays([id])
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Try to listen on available hop relay connections.
|
|
193
|
+
* The following order will happen while we do not have enough relays.
|
|
194
|
+
* 1. Check the metadata store for known relays, try to listen on the ones we are already connected.
|
|
195
|
+
* 2. Dial and try to listen on the peers we know that support hop but are not connected.
|
|
196
|
+
* 3. Search the network.
|
|
197
|
+
*/
|
|
198
|
+
async _listenOnAvailableHopRelays (peersToIgnore: string[] = []) {
|
|
199
|
+
// TODO: The peer redial issue on disconnect should be handled by connection gating
|
|
200
|
+
// Check if already listening on enough relays
|
|
201
|
+
if (this.listenRelays.size >= this.maxListeners) {
|
|
202
|
+
return
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const knownHopsToDial = []
|
|
206
|
+
const peers = await this.components.getPeerStore().all()
|
|
207
|
+
|
|
208
|
+
// Check if we have known hop peers to use and attempt to listen on the already connected
|
|
209
|
+
for (const { id, metadata } of peers) {
|
|
210
|
+
const idStr = id.toString()
|
|
211
|
+
|
|
212
|
+
// Continue to next if listening on this or peer to ignore
|
|
213
|
+
if (this.listenRelays.has(idStr)) {
|
|
214
|
+
continue
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (peersToIgnore.includes(idStr)) {
|
|
218
|
+
continue
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const supportsHop = metadata.get(HOP_METADATA_KEY)
|
|
222
|
+
|
|
223
|
+
// Continue to next if it does not support Hop
|
|
224
|
+
if ((supportsHop == null) || uint8ArrayToString(supportsHop) !== HOP_METADATA_VALUE) {
|
|
225
|
+
continue
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const connections = this.components.getConnectionManager().getConnections(id)
|
|
229
|
+
|
|
230
|
+
// If not connected, store for possible later use.
|
|
231
|
+
if (connections.length === 0) {
|
|
232
|
+
knownHopsToDial.push(id)
|
|
233
|
+
continue
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
await this._addListenRelay(connections[0], idStr)
|
|
237
|
+
|
|
238
|
+
// Check if already listening on enough relays
|
|
239
|
+
if (this.listenRelays.size >= this.maxListeners) {
|
|
240
|
+
return
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Try to listen on known peers that are not connected
|
|
245
|
+
for (const peerId of knownHopsToDial) {
|
|
246
|
+
await this._tryToListenOnRelay(peerId)
|
|
247
|
+
|
|
248
|
+
// Check if already listening on enough relays
|
|
249
|
+
if (this.listenRelays.size >= this.maxListeners) {
|
|
250
|
+
return
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Try to find relays to hop on the network
|
|
255
|
+
try {
|
|
256
|
+
const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS)
|
|
257
|
+
for await (const provider of this.components.getContentRouting().findProviders(cid)) {
|
|
258
|
+
if (provider.multiaddrs.length === 0) {
|
|
259
|
+
continue
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const peerId = provider.id
|
|
263
|
+
await this.components.getPeerStore().addressBook.add(peerId, provider.multiaddrs)
|
|
264
|
+
|
|
265
|
+
await this._tryToListenOnRelay(peerId)
|
|
266
|
+
|
|
267
|
+
// Check if already listening on enough relays
|
|
268
|
+
if (this.listenRelays.size >= this.maxListeners) {
|
|
269
|
+
return
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
} catch (err: any) {
|
|
273
|
+
this.onError(err)
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
async _tryToListenOnRelay (peerId: PeerId) {
|
|
278
|
+
try {
|
|
279
|
+
const connection = await this.components.getConnectionManager().openConnection(peerId)
|
|
280
|
+
await this._addListenRelay(connection, peerId.toString())
|
|
281
|
+
} catch (err: any) {
|
|
282
|
+
log.error('Could not use %p as relay', peerId, err)
|
|
283
|
+
this.onError(err, `could not connect and listen on known hop relay ${peerId.toString()}`)
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
|
2
|
+
import errCode from 'err-code'
|
|
3
|
+
import { validateAddrs } from './utils.js'
|
|
4
|
+
import { StreamHandler } from './stream-handler.js'
|
|
5
|
+
import { CircuitRelay as CircuitPB } from '../pb/index.js'
|
|
6
|
+
import { pipe } from 'it-pipe'
|
|
7
|
+
import { codes as Errors } from '../../errors.js'
|
|
8
|
+
import { stop } from './stop.js'
|
|
9
|
+
import { RELAY_CODEC } from '../multicodec.js'
|
|
10
|
+
import type { Connection } from '@libp2p/interfaces/connection'
|
|
11
|
+
import { peerIdFromBytes } from '@libp2p/peer-id'
|
|
12
|
+
import type { Duplex } from 'it-stream-types'
|
|
13
|
+
import type { Circuit } from '../transport.js'
|
|
14
|
+
import type { ConnectionManager } from '@libp2p/interfaces/connection-manager'
|
|
15
|
+
|
|
16
|
+
const log = logger('libp2p:circuit:hop')
|
|
17
|
+
|
|
18
|
+
export interface HopRequest {
|
|
19
|
+
connection: Connection
|
|
20
|
+
request: CircuitPB
|
|
21
|
+
streamHandler: StreamHandler
|
|
22
|
+
circuit: Circuit
|
|
23
|
+
connectionManager: ConnectionManager
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function handleHop (hopRequest: HopRequest) {
|
|
27
|
+
const {
|
|
28
|
+
connection,
|
|
29
|
+
request,
|
|
30
|
+
streamHandler,
|
|
31
|
+
circuit,
|
|
32
|
+
connectionManager
|
|
33
|
+
} = hopRequest
|
|
34
|
+
|
|
35
|
+
// Ensure hop is enabled
|
|
36
|
+
if (!circuit.hopEnabled()) {
|
|
37
|
+
log('HOP request received but we are not acting as a relay')
|
|
38
|
+
return streamHandler.end({
|
|
39
|
+
type: CircuitPB.Type.STATUS,
|
|
40
|
+
code: CircuitPB.Status.HOP_CANT_SPEAK_RELAY
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Validate the HOP request has the required input
|
|
45
|
+
try {
|
|
46
|
+
validateAddrs(request, streamHandler)
|
|
47
|
+
} catch (err: any) {
|
|
48
|
+
log.error('invalid hop request via peer %p %o', connection.remotePeer, err)
|
|
49
|
+
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (request.dstPeer == null) {
|
|
54
|
+
log('HOP request received but we do not receive a dstPeer')
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Get the connection to the destination (stop) peer
|
|
59
|
+
const destinationPeer = peerIdFromBytes(request.dstPeer.id)
|
|
60
|
+
|
|
61
|
+
const destinationConnections = connectionManager.getConnections(destinationPeer)
|
|
62
|
+
if (destinationConnections.length === 0 && !circuit.hopActive()) {
|
|
63
|
+
log('HOP request received but we are not connected to the destination peer')
|
|
64
|
+
return streamHandler.end({
|
|
65
|
+
type: CircuitPB.Type.STATUS,
|
|
66
|
+
code: CircuitPB.Status.HOP_NO_CONN_TO_DST
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// TODO: Handle being an active relay
|
|
71
|
+
if (destinationConnections.length === 0) {
|
|
72
|
+
log('did not have connection to remote peer')
|
|
73
|
+
return streamHandler.end({
|
|
74
|
+
type: CircuitPB.Type.STATUS,
|
|
75
|
+
code: CircuitPB.Status.HOP_NO_CONN_TO_DST
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Handle the incoming HOP request by performing a STOP request
|
|
80
|
+
const stopRequest = {
|
|
81
|
+
type: CircuitPB.Type.STOP,
|
|
82
|
+
dstPeer: request.dstPeer,
|
|
83
|
+
srcPeer: request.srcPeer
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
let destinationStream: Duplex<Uint8Array>
|
|
87
|
+
try {
|
|
88
|
+
log('performing STOP request')
|
|
89
|
+
const result = await stop({
|
|
90
|
+
connection: destinationConnections[0],
|
|
91
|
+
request: stopRequest
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
if (result == null) {
|
|
95
|
+
throw new Error('Could not stop')
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
destinationStream = result
|
|
99
|
+
} catch (err: any) {
|
|
100
|
+
log.error(err)
|
|
101
|
+
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
log('hop request from %p is valid', connection.remotePeer)
|
|
106
|
+
streamHandler.write({
|
|
107
|
+
type: CircuitPB.Type.STATUS,
|
|
108
|
+
code: CircuitPB.Status.SUCCESS
|
|
109
|
+
})
|
|
110
|
+
const sourceStream = streamHandler.rest()
|
|
111
|
+
|
|
112
|
+
log('creating related connections')
|
|
113
|
+
// Short circuit the two streams to create the relayed connection
|
|
114
|
+
return await pipe(
|
|
115
|
+
sourceStream,
|
|
116
|
+
destinationStream,
|
|
117
|
+
sourceStream
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface HopConfig {
|
|
122
|
+
connection: Connection
|
|
123
|
+
request: CircuitPB
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Performs a HOP request to a relay peer, to request a connection to another
|
|
128
|
+
* peer. A new, virtual, connection will be created between the two via the relay.
|
|
129
|
+
*/
|
|
130
|
+
export async function hop (options: HopConfig): Promise<Duplex<Uint8Array>> {
|
|
131
|
+
const {
|
|
132
|
+
connection,
|
|
133
|
+
request
|
|
134
|
+
} = options
|
|
135
|
+
|
|
136
|
+
// Create a new stream to the relay
|
|
137
|
+
const { stream } = await connection.newStream(RELAY_CODEC)
|
|
138
|
+
// Send the HOP request
|
|
139
|
+
const streamHandler = new StreamHandler({ stream })
|
|
140
|
+
streamHandler.write(request)
|
|
141
|
+
|
|
142
|
+
const response = await streamHandler.read()
|
|
143
|
+
|
|
144
|
+
if (response == null) {
|
|
145
|
+
throw errCode(new Error('HOP request had no response'), Errors.ERR_HOP_REQUEST_FAILED)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (response.code === CircuitPB.Status.SUCCESS) {
|
|
149
|
+
log('hop request was successful')
|
|
150
|
+
return streamHandler.rest()
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
log('hop request failed with code %d, closing stream', response.code)
|
|
154
|
+
streamHandler.close()
|
|
155
|
+
|
|
156
|
+
throw errCode(new Error(`HOP request failed with code "${response.code ?? 'unknown'}"`), Errors.ERR_HOP_REQUEST_FAILED)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export interface CanHopOptions {
|
|
160
|
+
connection: Connection
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Performs a CAN_HOP request to a relay peer, in order to understand its capabilities
|
|
165
|
+
*/
|
|
166
|
+
export async function canHop (options: CanHopOptions) {
|
|
167
|
+
const {
|
|
168
|
+
connection
|
|
169
|
+
} = options
|
|
170
|
+
|
|
171
|
+
// Create a new stream to the relay
|
|
172
|
+
const { stream } = await connection.newStream(RELAY_CODEC)
|
|
173
|
+
|
|
174
|
+
// Send the HOP request
|
|
175
|
+
const streamHandler = new StreamHandler({ stream })
|
|
176
|
+
streamHandler.write({
|
|
177
|
+
type: CircuitPB.Type.CAN_HOP
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
const response = await streamHandler.read()
|
|
181
|
+
await streamHandler.close()
|
|
182
|
+
|
|
183
|
+
if (response == null || response.code !== CircuitPB.Status.SUCCESS) {
|
|
184
|
+
return false
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return true
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export interface HandleCanHopOptions {
|
|
191
|
+
connection: Connection
|
|
192
|
+
streamHandler: StreamHandler
|
|
193
|
+
circuit: Circuit
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Creates an unencoded CAN_HOP response based on the Circuits configuration
|
|
198
|
+
*/
|
|
199
|
+
export function handleCanHop (options: HandleCanHopOptions) {
|
|
200
|
+
const {
|
|
201
|
+
connection,
|
|
202
|
+
streamHandler,
|
|
203
|
+
circuit
|
|
204
|
+
} = options
|
|
205
|
+
const canHop = circuit.hopEnabled()
|
|
206
|
+
log('can hop (%s) request from %p', canHop, connection.remotePeer)
|
|
207
|
+
streamHandler.end({
|
|
208
|
+
type: CircuitPB.Type.STATUS,
|
|
209
|
+
code: canHop ? CircuitPB.Status.SUCCESS : CircuitPB.Status.HOP_CANT_SPEAK_RELAY
|
|
210
|
+
})
|
|
211
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
|
2
|
+
import { CircuitRelay as CircuitPB } from '../pb/index.js'
|
|
3
|
+
import { RELAY_CODEC } from '../multicodec.js'
|
|
4
|
+
import { StreamHandler } from './stream-handler.js'
|
|
5
|
+
import { validateAddrs } from './utils.js'
|
|
6
|
+
import type { Connection } from '@libp2p/interfaces/connection'
|
|
7
|
+
import type { Duplex } from 'it-stream-types'
|
|
8
|
+
|
|
9
|
+
const log = logger('libp2p:circuit:stop')
|
|
10
|
+
|
|
11
|
+
export interface HandleStopOptions {
|
|
12
|
+
connection: Connection
|
|
13
|
+
request: CircuitPB
|
|
14
|
+
streamHandler: StreamHandler
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Handles incoming STOP requests
|
|
19
|
+
*/
|
|
20
|
+
export function handleStop (options: HandleStopOptions): Duplex<Uint8Array> | undefined {
|
|
21
|
+
const {
|
|
22
|
+
connection,
|
|
23
|
+
request,
|
|
24
|
+
streamHandler
|
|
25
|
+
} = options
|
|
26
|
+
|
|
27
|
+
// Validate the STOP request has the required input
|
|
28
|
+
try {
|
|
29
|
+
validateAddrs(request, streamHandler)
|
|
30
|
+
} catch (err: any) {
|
|
31
|
+
log.error('invalid stop request via peer %p %o', connection.remotePeer, err)
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// The request is valid
|
|
36
|
+
log('stop request is valid')
|
|
37
|
+
streamHandler.write({
|
|
38
|
+
type: CircuitPB.Type.STATUS,
|
|
39
|
+
code: CircuitPB.Status.SUCCESS
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
return streamHandler.rest()
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface StopOptions {
|
|
46
|
+
connection: Connection
|
|
47
|
+
request: CircuitPB
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Creates a STOP request
|
|
52
|
+
*/
|
|
53
|
+
export async function stop (options: StopOptions) {
|
|
54
|
+
const {
|
|
55
|
+
connection,
|
|
56
|
+
request
|
|
57
|
+
} = options
|
|
58
|
+
|
|
59
|
+
const { stream } = await connection.newStream([RELAY_CODEC])
|
|
60
|
+
log('starting stop request to %p', connection.remotePeer)
|
|
61
|
+
const streamHandler = new StreamHandler({ stream })
|
|
62
|
+
|
|
63
|
+
streamHandler.write(request)
|
|
64
|
+
const response = await streamHandler.read()
|
|
65
|
+
|
|
66
|
+
if (response == null) {
|
|
67
|
+
streamHandler.close()
|
|
68
|
+
return
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (response.code === CircuitPB.Status.SUCCESS) {
|
|
72
|
+
log('stop request to %p was successful', connection.remotePeer)
|
|
73
|
+
return streamHandler.rest()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
log('stop request failed with code %d', response.code)
|
|
77
|
+
streamHandler.close()
|
|
78
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { logger } from '@libp2p/logger'
|
|
2
|
+
import * as lp from 'it-length-prefixed'
|
|
3
|
+
import { Handshake, handshake } from 'it-handshake'
|
|
4
|
+
import { CircuitRelay } from '../pb/index.js'
|
|
5
|
+
import type { Stream } from '@libp2p/interfaces/connection'
|
|
6
|
+
import type { Source } from 'it-stream-types'
|
|
7
|
+
|
|
8
|
+
const log = logger('libp2p:circuit:stream-handler')
|
|
9
|
+
|
|
10
|
+
export interface StreamHandlerOptions {
|
|
11
|
+
/**
|
|
12
|
+
* A duplex iterable
|
|
13
|
+
*/
|
|
14
|
+
stream: Stream
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* max bytes length of message
|
|
18
|
+
*/
|
|
19
|
+
maxLength?: number
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class StreamHandler {
|
|
23
|
+
private readonly stream: Stream
|
|
24
|
+
private readonly shake: Handshake
|
|
25
|
+
private readonly decoder: Source<Uint8Array>
|
|
26
|
+
|
|
27
|
+
constructor (options: StreamHandlerOptions) {
|
|
28
|
+
const { stream, maxLength = 4096 } = options
|
|
29
|
+
|
|
30
|
+
this.stream = stream
|
|
31
|
+
this.shake = handshake(this.stream)
|
|
32
|
+
this.decoder = lp.decode.fromReader(this.shake.reader, { maxDataLength: maxLength })
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Read and decode message
|
|
37
|
+
*/
|
|
38
|
+
async read () {
|
|
39
|
+
// @ts-expect-error FIXME is a source, needs to be a generator
|
|
40
|
+
const msg = await this.decoder.next()
|
|
41
|
+
|
|
42
|
+
if (msg.value != null) {
|
|
43
|
+
const value = CircuitRelay.decode(msg.value.slice())
|
|
44
|
+
log('read message type', value.type)
|
|
45
|
+
return value
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
log('read received no value, closing stream')
|
|
49
|
+
// End the stream, we didn't get data
|
|
50
|
+
this.close()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Encode and write array of buffers
|
|
55
|
+
*/
|
|
56
|
+
write (msg: CircuitRelay) {
|
|
57
|
+
log('write message type %s', msg.type)
|
|
58
|
+
this.shake.write(lp.encode.single(CircuitRelay.encode(msg)).slice())
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Return the handshake rest stream and invalidate handler
|
|
63
|
+
*/
|
|
64
|
+
rest () {
|
|
65
|
+
this.shake.rest()
|
|
66
|
+
return this.shake.stream
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param {CircuitRelay} msg - An unencoded CircuitRelay protobuf message
|
|
71
|
+
*/
|
|
72
|
+
end (msg: CircuitRelay) {
|
|
73
|
+
this.write(msg)
|
|
74
|
+
this.close()
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Close the stream
|
|
79
|
+
*/
|
|
80
|
+
close () {
|
|
81
|
+
log('closing the stream')
|
|
82
|
+
void this.rest().sink([]).catch(err => {
|
|
83
|
+
log.error(err)
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
}
|