@streamr/dht 100.0.0-pretestnet.2 → 100.0.0-pretestnet.4
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/dist/src/connection/ConnectionLockHandler.d.ts +7 -8
- package/dist/src/connection/ConnectionLockHandler.js +16 -14
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcLocal.d.ts +20 -0
- package/dist/src/connection/ConnectionLockRpcLocal.js +47 -0
- package/dist/src/connection/ConnectionLockRpcLocal.js.map +1 -0
- package/dist/src/connection/ConnectionLockRpcRemote.d.ts +4 -3
- package/dist/src/connection/ConnectionLockRpcRemote.js +9 -9
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +9 -16
- package/dist/src/connection/ConnectionManager.js +101 -132
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectivityChecker.d.ts +7 -10
- package/dist/src/connection/ConnectivityChecker.js +30 -32
- package/dist/src/connection/ConnectivityChecker.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +19 -13
- package/dist/src/connection/ConnectorFacade.js +74 -29
- package/dist/src/connection/ConnectorFacade.js.map +1 -1
- package/dist/src/connection/Handshaker.d.ts +8 -8
- package/dist/src/connection/Handshaker.js +30 -33
- package/dist/src/connection/Handshaker.js.map +1 -1
- package/dist/src/connection/IConnection.d.ts +5 -6
- package/dist/src/connection/IConnection.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +9 -13
- package/dist/src/connection/ManagedConnection.js +63 -99
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/ManagedWebrtcConnection.d.ts +7 -0
- package/dist/src/connection/ManagedWebrtcConnection.js +20 -0
- package/dist/src/connection/ManagedWebrtcConnection.js.map +1 -0
- package/dist/src/connection/{Simulator → simulator}/Simulator.js +3 -3
- package/dist/src/connection/{Simulator → simulator}/Simulator.js.map +1 -1
- package/dist/src/connection/{Simulator → simulator}/SimulatorConnection.d.ts +3 -4
- package/dist/src/connection/{Simulator → simulator}/SimulatorConnection.js +25 -24
- package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -0
- package/dist/src/connection/{Simulator → simulator}/SimulatorConnector.d.ts +3 -3
- package/dist/src/connection/{Simulator → simulator}/SimulatorConnector.js +17 -18
- package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -0
- package/dist/src/connection/{Simulator → simulator}/SimulatorTransport.d.ts +1 -1
- package/dist/src/connection/{Simulator → simulator}/SimulatorTransport.js +2 -2
- package/dist/src/connection/simulator/SimulatorTransport.js.map +1 -0
- package/dist/src/connection/{Simulator → simulator}/pings.js.map +1 -1
- package/dist/src/connection/{WebRTC/IWebRtcConnection.d.ts → webrtc/IWebrtcConnection.d.ts} +2 -2
- package/dist/src/connection/{WebRTC/IWebRtcConnection.js → webrtc/IWebrtcConnection.js} +1 -1
- package/dist/src/connection/webrtc/IWebrtcConnection.js.map +1 -0
- package/dist/src/connection/{WebRTC/NodeWebRtcConnection.d.ts → webrtc/NodeWebrtcConnection.d.ts} +6 -7
- package/dist/src/connection/{WebRTC/NodeWebRtcConnection.js → webrtc/NodeWebrtcConnection.js} +41 -42
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -0
- package/dist/src/connection/webrtc/WebrtcConnector.d.ts +38 -0
- package/dist/src/connection/webrtc/WebrtcConnector.js +178 -0
- package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -0
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.d.ts +26 -0
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +99 -0
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -0
- package/dist/src/connection/{WebRTC/WebRtcConnectorRpcRemote.d.ts → webrtc/WebrtcConnectorRpcRemote.d.ts} +4 -4
- package/dist/src/connection/{WebRTC/WebRtcConnectorRpcRemote.js → webrtc/WebrtcConnectorRpcRemote.js} +6 -8
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js.map +1 -0
- package/dist/src/connection/{WebRTC → webrtc}/iceServerAsString.d.ts +1 -1
- package/dist/src/connection/{WebRTC → webrtc}/iceServerAsString.js.map +1 -1
- package/dist/src/connection/websocket/AutoCertifierClientFacade.d.ts +26 -0
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js +37 -0
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js.map +1 -0
- package/dist/src/connection/{WebSocket/ClientWebSocket.d.ts → websocket/ClientWebsocket.d.ts} +5 -4
- package/dist/src/connection/{WebSocket/ClientWebSocket.js → websocket/ClientWebsocket.js} +23 -16
- package/dist/src/connection/websocket/ClientWebsocket.js.map +1 -0
- package/dist/src/connection/{WebSocket/ServerWebSocket.d.ts → websocket/ServerWebsocket.d.ts} +2 -2
- package/dist/src/connection/{WebSocket/ServerWebSocket.js → websocket/ServerWebsocket.js} +25 -27
- package/dist/src/connection/websocket/ServerWebsocket.js.map +1 -0
- package/dist/src/connection/websocket/WebsocketConnector.d.ts +53 -0
- package/dist/src/connection/websocket/WebsocketConnector.js +242 -0
- package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -0
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +17 -0
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +26 -0
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +1 -0
- package/dist/src/connection/{WebSocket/WebSocketConnectorRpcRemote.d.ts → websocket/WebsocketConnectorRpcRemote.d.ts} +3 -3
- package/dist/src/connection/{WebSocket/WebSocketConnectorRpcRemote.js → websocket/WebsocketConnectorRpcRemote.js} +6 -11
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +1 -0
- package/dist/src/connection/{WebSocket/WebSocketServer.d.ts → websocket/WebsocketServer.d.ts} +6 -3
- package/dist/src/connection/{WebSocket/WebSocketServer.js → websocket/WebsocketServer.js} +39 -19
- package/dist/src/connection/websocket/WebsocketServer.js.map +1 -0
- package/dist/src/dht/DhtNode.d.ts +11 -19
- package/dist/src/dht/DhtNode.js +135 -163
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +22 -0
- package/dist/src/dht/DhtNodeRpcLocal.js +42 -0
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -0
- package/dist/src/dht/{RemoteDhtNode.d.ts → DhtNodeRpcRemote.d.ts} +5 -3
- package/dist/src/dht/{RemoteDhtNode.js → DhtNodeRpcRemote.js} +20 -24
- package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -0
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +16 -0
- package/dist/src/dht/ExternalApiRpcLocal.js +30 -0
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -0
- package/dist/src/dht/ExternalApiRpcRemote.d.ts +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.js +2 -2
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/contact/Remote.d.ts +4 -1
- package/dist/src/dht/contact/Remote.js +28 -2
- package/dist/src/dht/contact/Remote.js.map +1 -1
- package/dist/src/dht/contact/SortedContactList.d.ts +2 -2
- package/dist/src/dht/contact/SortedContactList.js +3 -3
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +9 -7
- package/dist/src/dht/discovery/DiscoverySession.js +9 -9
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +7 -10
- package/dist/src/dht/discovery/PeerDiscovery.js +6 -6
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/find/FindRpcLocal.d.ts +14 -0
- package/dist/src/dht/find/FindRpcLocal.js +25 -0
- package/dist/src/dht/find/FindRpcLocal.js.map +1 -0
- package/dist/src/dht/find/{RecursiveFindSession.d.ts → FindSession.d.ts} +12 -13
- package/dist/src/dht/find/{RecursiveFindSession.js → FindSession.js} +17 -15
- package/dist/src/dht/find/FindSession.js.map +1 -0
- package/dist/src/dht/find/FindSessionRpcLocal.d.ts +12 -0
- package/dist/src/dht/find/FindSessionRpcLocal.js +17 -0
- package/dist/src/dht/find/FindSessionRpcLocal.js.map +1 -0
- package/dist/src/dht/find/{RemoteRecursiveFindSession.d.ts → FindSessionRpcRemote.d.ts} +2 -2
- package/dist/src/dht/find/{RemoteRecursiveFindSession.js → FindSessionRpcRemote.js} +5 -5
- package/dist/src/dht/find/FindSessionRpcRemote.js.map +1 -0
- package/dist/src/dht/find/{RecursiveFinder.d.ts → Finder.d.ts} +16 -19
- package/dist/src/dht/find/{RecursiveFinder.js → Finder.js} +50 -46
- package/dist/src/dht/find/Finder.js.map +1 -0
- package/dist/src/dht/routing/FindRpcRemote.d.ts +6 -0
- package/dist/src/dht/routing/FindRpcRemote.js +41 -0
- package/dist/src/dht/routing/FindRpcRemote.js.map +1 -0
- package/dist/src/dht/routing/Router.d.ts +10 -17
- package/dist/src/dht/routing/Router.js +50 -93
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.d.ts +22 -0
- package/dist/src/dht/routing/RouterRpcLocal.js +66 -0
- package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -0
- package/dist/src/dht/routing/RouterRpcRemote.d.ts +0 -1
- package/dist/src/dht/routing/RouterRpcRemote.js +17 -45
- package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +17 -8
- package/dist/src/dht/routing/RoutingSession.js +124 -131
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/routing/getPreviousPeer.d.ts +2 -0
- package/dist/src/dht/routing/getPreviousPeer.js +9 -0
- package/dist/src/dht/routing/getPreviousPeer.js.map +1 -0
- package/dist/src/dht/store/StoreRpcLocal.d.ts +11 -8
- package/dist/src/dht/store/StoreRpcLocal.js +26 -25
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/dht/store/StoreRpcRemote.js +6 -10
- package/dist/src/dht/store/StoreRpcRemote.js.map +1 -1
- package/dist/src/exports.d.ts +11 -5
- package/dist/src/exports.js +17 -4
- package/dist/src/exports.js.map +1 -1
- package/dist/src/helpers/Connectivity.d.ts +4 -0
- package/dist/src/helpers/Connectivity.js +24 -0
- package/dist/src/helpers/Connectivity.js.map +1 -0
- package/dist/src/helpers/errors.d.ts +3 -3
- package/dist/src/helpers/errors.js +7 -7
- package/dist/src/helpers/peerIdFromPeerDescriptor.d.ts +1 -0
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +7 -1
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -1
- package/dist/src/helpers/protoClasses.js +3 -3
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +56 -39
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +49 -36
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +85 -96
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +82 -71
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +26 -21
- package/dist/src/rpc-protocol/DhtCallContext.d.ts +0 -1
- package/dist/src/rpc-protocol/DhtCallContext.js.map +1 -1
- package/dist/src/transport/ITransport.d.ts +5 -6
- package/dist/src/transport/ListeningRpcCommunicator.d.ts +2 -1
- package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -1
- package/dist/src/transport/RoutingRpcCommunicator.d.ts +2 -1
- package/dist/src/transport/RoutingRpcCommunicator.js +0 -1
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
- package/dist/src/types/ServiceID.d.ts +1 -0
- package/dist/src/types/ServiceID.js +3 -0
- package/dist/src/types/ServiceID.js.map +1 -0
- package/karma.config.js +4 -4
- package/package.json +10 -8
- package/protos/DhtRpc.proto +41 -40
- package/src/connection/ConnectionLockHandler.ts +19 -17
- package/src/connection/ConnectionLockRpcLocal.ts +74 -0
- package/src/connection/ConnectionLockRpcRemote.ts +11 -11
- package/src/connection/ConnectionManager.ts +88 -130
- package/src/connection/ConnectivityChecker.ts +36 -34
- package/src/connection/ConnectorFacade.ts +91 -40
- package/src/connection/Handshaker.ts +20 -23
- package/src/connection/IConnection.ts +5 -6
- package/src/connection/ManagedConnection.ts +76 -101
- package/src/connection/{ManagedWebRtcConnection.ts → ManagedWebrtcConnection.ts} +12 -10
- package/src/connection/{Simulator → simulator}/Simulator.ts +4 -4
- package/src/connection/{Simulator → simulator}/SimulatorConnection.ts +27 -27
- package/src/connection/{Simulator → simulator}/SimulatorConnector.ts +22 -23
- package/src/connection/{Simulator → simulator}/SimulatorTransport.ts +2 -2
- package/src/connection/{WebRTC/BrowserWebRtcConnection.ts → webrtc/BrowserWebrtcConnection.ts} +13 -14
- package/src/connection/{WebRTC/IWebRtcConnection.ts → webrtc/IWebrtcConnection.ts} +2 -2
- package/src/connection/{WebRTC/NodeWebRtcConnection.ts → webrtc/NodeWebrtcConnection.ts} +38 -40
- package/src/connection/webrtc/WebrtcConnector.ts +226 -0
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +132 -0
- package/src/connection/{WebRTC/WebRtcConnectorRpcRemote.ts → webrtc/WebrtcConnectorRpcRemote.ts} +6 -7
- package/src/connection/{WebRTC → webrtc}/iceServerAsString.ts +1 -1
- package/src/connection/websocket/AutoCertifierClientFacade.ts +91 -0
- package/src/connection/{WebSocket/ClientWebSocket.ts → websocket/ClientWebsocket.ts} +22 -17
- package/src/connection/{WebSocket/ServerWebSocket.ts → websocket/ServerWebsocket.ts} +23 -27
- package/src/connection/websocket/WebsocketConnector.ts +328 -0
- package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +41 -0
- package/src/connection/{WebSocket/WebSocketConnectorRpcRemote.ts → websocket/WebsocketConnectorRpcRemote.ts} +8 -14
- package/src/connection/{WebSocket/WebSocketServer.ts → websocket/WebsocketServer.ts} +40 -22
- package/src/dht/DhtNode.ts +166 -193
- package/src/dht/DhtNodeRpcLocal.ts +68 -0
- package/src/dht/{RemoteDhtNode.ts → DhtNodeRpcRemote.ts} +17 -18
- package/src/dht/ExternalApiRpcLocal.ts +46 -0
- package/src/dht/ExternalApiRpcRemote.ts +4 -4
- package/src/dht/contact/Remote.ts +32 -4
- package/src/dht/contact/SortedContactList.ts +4 -4
- package/src/dht/discovery/DiscoverySession.ts +25 -22
- package/src/dht/discovery/PeerDiscovery.ts +16 -19
- package/src/dht/find/FindRpcLocal.ts +35 -0
- package/src/dht/find/{RecursiveFindSession.ts → FindSession.ts} +25 -25
- package/src/dht/find/FindSessionRpcLocal.ts +25 -0
- package/src/dht/find/{RemoteRecursiveFindSession.ts → FindSessionRpcRemote.ts} +9 -5
- package/src/dht/find/{RecursiveFinder.ts → Finder.ts} +77 -71
- package/src/dht/routing/FindRpcRemote.ts +40 -0
- package/src/dht/routing/Router.ts +78 -109
- package/src/dht/routing/RouterRpcLocal.ts +81 -0
- package/src/dht/routing/RouterRpcRemote.ts +18 -49
- package/src/dht/routing/RoutingSession.ts +58 -63
- package/src/dht/routing/getPreviousPeer.ts +6 -0
- package/src/dht/store/StoreRpcLocal.ts +46 -39
- package/src/dht/store/StoreRpcRemote.ts +8 -12
- package/src/exports.ts +11 -5
- package/src/helpers/Connectivity.ts +19 -0
- package/src/helpers/errors.ts +3 -3
- package/src/helpers/peerIdFromPeerDescriptor.ts +6 -0
- package/src/helpers/protoClasses.ts +6 -6
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +75 -59
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +26 -21
- package/src/proto/packages/dht/protos/DhtRpc.ts +112 -131
- package/src/rpc-protocol/DhtCallContext.ts +0 -1
- package/src/transport/ITransport.ts +5 -8
- package/src/transport/ListeningRpcCommunicator.ts +2 -1
- package/src/transport/RoutingRpcCommunicator.ts +3 -4
- package/src/types/ServiceID.ts +1 -0
- package/test/benchmark/{RecursiveFind.test.ts → Find.test.ts} +7 -7
- package/test/benchmark/KademliaCorrectness.test.ts +3 -3
- package/test/benchmark/kademlia-simulation/SimulationNode.ts +3 -3
- package/test/end-to-end/Layer0-Layer1.test.ts +13 -5
- package/test/end-to-end/Layer0.test.ts +35 -17
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +28 -9
- package/test/end-to-end/{Layer0WebRTC-Layer1.test.ts → Layer0Webrtc-Layer1.test.ts} +1 -1
- package/test/end-to-end/{Layer0WebRTC.test.ts → Layer0Webrtc.test.ts} +14 -10
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +8 -4
- package/test/end-to-end/{Layer1-Scale-WebRTC.test.ts → Layer1-Scale-Webrtc.test.ts} +2 -2
- package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +52 -0
- package/test/end-to-end/{WebSocketConnectionRequest.test.ts → WebsocketConnectionRequest.test.ts} +16 -8
- package/test/end-to-end/memory-leak.test.ts +85 -0
- package/test/integration/ConnectionLocking.test.ts +10 -7
- package/test/integration/ConnectionManager.test.ts +107 -30
- package/test/integration/DhtJoinPeerDiscovery.test.ts +2 -2
- package/test/integration/DhtNodeExternalAPI.test.ts +10 -10
- package/test/integration/{RemoteDhtNode.test.ts → DhtNodeRpcRemote.test.ts} +10 -10
- package/test/integration/DhtRpc.test.ts +5 -5
- package/test/integration/{RecursiveFind.test.ts → Find.test.ts} +6 -9
- package/test/integration/Layer1-scale.test.ts +1 -1
- package/test/integration/MigrateData.test.ts +8 -7
- package/test/integration/Mock-Layer1-Layer0.test.ts +1 -1
- package/test/integration/MultipleEntryPointJoining.test.ts +8 -8
- package/test/integration/RouteMessage.test.ts +26 -26
- package/test/integration/RpcErrors.test.ts +10 -10
- package/test/integration/ScaleDownDht.test.ts +6 -6
- package/test/integration/SimultaneousConnections.test.ts +10 -7
- package/test/integration/Store.test.ts +4 -7
- package/test/integration/StoreAndDelete.test.ts +4 -7
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +12 -12
- package/test/integration/{WebRtcConnectionManagement.test.ts → WebrtcConnectionManagement.test.ts} +5 -5
- package/test/integration/{WebRtcConnectorRpc.test.ts → WebrtcConnectorRpc.test.ts} +7 -8
- package/test/integration/{WebSocket.test.ts → Websocket.test.ts} +14 -13
- package/test/integration/{WebSocketConnectionManagement.test.ts → WebsocketConnectionManagement.test.ts} +4 -4
- package/test/integration/{WebSocketConnectorRpc.test.ts → WebsocketConnectorRpc.test.ts} +15 -15
- package/test/unit/AutoCertifierClientFacade.test.ts +58 -0
- package/test/unit/ConnectivityHelpers.test.ts +69 -0
- package/test/unit/{RecursiveFinder.test.ts → Finder.test.ts} +19 -17
- package/test/unit/Router.test.ts +26 -26
- package/test/unit/UUID.test.ts +1 -1
- package/test/unit/{WebSocketConnectorRpcLocal.test.ts → WebsocketConnector.test.ts} +8 -8
- package/test/unit/{WebSocketServer.test.ts → WebsocketServer.test.ts} +20 -14
- package/test/unit/webrtcReplaceInternalIpWithExternalIp.test.ts +1 -1
- package/test/utils/FakeRpcCommunicator.ts +23 -0
- package/test/utils/mock/Transport.ts +1 -1
- package/test/utils/utils.ts +22 -26
- package/tsconfig.browser.json +1 -1
- package/tsconfig.jest.json +3 -2
- package/tsconfig.node.json +3 -2
- package/dist/src/connection/ManagedWebRtcConnection.d.ts +0 -7
- package/dist/src/connection/ManagedWebRtcConnection.js +0 -20
- package/dist/src/connection/ManagedWebRtcConnection.js.map +0 -1
- package/dist/src/connection/Simulator/SimulatorConnection.js.map +0 -1
- package/dist/src/connection/Simulator/SimulatorConnector.js.map +0 -1
- package/dist/src/connection/Simulator/SimulatorTransport.js.map +0 -1
- package/dist/src/connection/WebRTC/IWebRtcConnection.js.map +0 -1
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.js.map +0 -1
- package/dist/src/connection/WebRTC/WebRtcConnectorRpcLocal.d.ts +0 -51
- package/dist/src/connection/WebRTC/WebRtcConnectorRpcLocal.js +0 -249
- package/dist/src/connection/WebRTC/WebRtcConnectorRpcLocal.js.map +0 -1
- package/dist/src/connection/WebRTC/WebRtcConnectorRpcRemote.js.map +0 -1
- package/dist/src/connection/WebSocket/ClientWebSocket.js.map +0 -1
- package/dist/src/connection/WebSocket/ServerWebSocket.js.map +0 -1
- package/dist/src/connection/WebSocket/WebSocketConnectorRpcLocal.d.ts +0 -45
- package/dist/src/connection/WebSocket/WebSocketConnectorRpcLocal.js +0 -234
- package/dist/src/connection/WebSocket/WebSocketConnectorRpcLocal.js.map +0 -1
- package/dist/src/connection/WebSocket/WebSocketConnectorRpcRemote.js.map +0 -1
- package/dist/src/connection/WebSocket/WebSocketServer.js.map +0 -1
- package/dist/src/dht/RemoteDhtNode.js.map +0 -1
- package/dist/src/dht/find/RecursiveFindSession.js.map +0 -1
- package/dist/src/dht/find/RecursiveFinder.js.map +0 -1
- package/dist/src/dht/find/RemoteRecursiveFindSession.js.map +0 -1
- package/dist/src/dht/registerExternalApiRpcMethods.d.ts +0 -2
- package/dist/src/dht/registerExternalApiRpcMethods.js +0 -32
- package/dist/src/dht/registerExternalApiRpcMethods.js.map +0 -1
- package/src/connection/WebRTC/WebRtcConnectorRpcLocal.ts +0 -335
- package/src/connection/WebSocket/WebSocketConnectorRpcLocal.ts +0 -295
- package/src/dht/registerExternalApiRpcMethods.ts +0 -44
- package/test/utils/mock/RecursiveFinder.ts +0 -19
- /package/dist/src/connection/{Simulator → simulator}/Simulator.d.ts +0 -0
- /package/dist/src/connection/{Simulator → simulator}/pings.d.ts +0 -0
- /package/dist/src/connection/{Simulator → simulator}/pings.js +0 -0
- /package/dist/src/connection/{WebRTC → webrtc}/iceServerAsString.js +0 -0
- /package/src/connection/{Simulator → simulator}/pings.ts +0 -0
|
@@ -1,335 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
IceCandidate,
|
|
3
|
-
PeerDescriptor,
|
|
4
|
-
RtcAnswer,
|
|
5
|
-
RtcOffer, WebRtcConnectionRequest
|
|
6
|
-
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
7
|
-
import { Empty } from '../../proto/google/protobuf/empty'
|
|
8
|
-
import { ITransport } from '../../transport/ITransport'
|
|
9
|
-
import { ListeningRpcCommunicator } from '../../transport/ListeningRpcCommunicator'
|
|
10
|
-
import { NodeWebRtcConnection } from './NodeWebRtcConnection'
|
|
11
|
-
import { WebRtcConnectorRpcRemote } from './WebRtcConnectorRpcRemote'
|
|
12
|
-
import { WebRtcConnectorRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
13
|
-
import { PeerIDKey } from '../../helpers/PeerID'
|
|
14
|
-
import { ManagedWebRtcConnection } from '../ManagedWebRtcConnection'
|
|
15
|
-
import { Logger } from '@streamr/utils'
|
|
16
|
-
import * as Err from '../../helpers/errors'
|
|
17
|
-
import { IWebRtcConnectorRpc } from '../../proto/packages/dht/protos/DhtRpc.server'
|
|
18
|
-
import { ManagedConnection } from '../ManagedConnection'
|
|
19
|
-
import { toProtoRpcClient } from '@streamr/proto-rpc'
|
|
20
|
-
import {
|
|
21
|
-
areEqualPeerDescriptors,
|
|
22
|
-
keyFromPeerDescriptor,
|
|
23
|
-
peerIdFromPeerDescriptor
|
|
24
|
-
} from '../../helpers/peerIdFromPeerDescriptor'
|
|
25
|
-
import { getAddressFromIceCandidate, isPrivateIPv4 } from '../../helpers/AddressTools'
|
|
26
|
-
import { PortRange } from '../ConnectionManager'
|
|
27
|
-
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
28
|
-
import { DhtCallContext } from '../../rpc-protocol/DhtCallContext'
|
|
29
|
-
|
|
30
|
-
const logger = new Logger(module)
|
|
31
|
-
|
|
32
|
-
export const replaceInternalIpWithExternalIp = (candidate: string, ip: string): string => {
|
|
33
|
-
const parsed = candidate.split(' ')
|
|
34
|
-
const type = parsed[7]
|
|
35
|
-
if (type === 'host') {
|
|
36
|
-
parsed[4] = ip
|
|
37
|
-
}
|
|
38
|
-
return parsed.join(' ')
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export interface WebRtcConnectorRpcLocalConfig {
|
|
42
|
-
transport: ITransport
|
|
43
|
-
iceServers?: IceServer[]
|
|
44
|
-
allowPrivateAddresses?: boolean
|
|
45
|
-
bufferThresholdLow?: number
|
|
46
|
-
bufferThresholdHigh?: number
|
|
47
|
-
maxMessageSize?: number
|
|
48
|
-
connectionTimeout?: number
|
|
49
|
-
externalIp?: string
|
|
50
|
-
portRange?: PortRange
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export interface IceServer {
|
|
54
|
-
url: string
|
|
55
|
-
port: number
|
|
56
|
-
username?: string
|
|
57
|
-
password?: string
|
|
58
|
-
tcp?: boolean
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export class WebRtcConnectorRpcLocal implements IWebRtcConnectorRpc {
|
|
62
|
-
|
|
63
|
-
private static readonly WEBRTC_CONNECTOR_SERVICE_ID = 'system/webrtc-connector'
|
|
64
|
-
private readonly rpcCommunicator: ListeningRpcCommunicator
|
|
65
|
-
private readonly ongoingConnectAttempts: Map<PeerIDKey, ManagedWebRtcConnection> = new Map()
|
|
66
|
-
private ownPeerDescriptor?: PeerDescriptor
|
|
67
|
-
private stopped = false
|
|
68
|
-
private iceServers: IceServer[]
|
|
69
|
-
private allowPrivateAddresses: boolean
|
|
70
|
-
private config: WebRtcConnectorRpcLocalConfig
|
|
71
|
-
private onIncomingConnection: (connection: ManagedConnection) => boolean
|
|
72
|
-
|
|
73
|
-
constructor(
|
|
74
|
-
config: WebRtcConnectorRpcLocalConfig,
|
|
75
|
-
onIncomingConnection: (connection: ManagedConnection) => boolean
|
|
76
|
-
) {
|
|
77
|
-
this.config = config
|
|
78
|
-
this.iceServers = config.iceServers || []
|
|
79
|
-
this.allowPrivateAddresses = config.allowPrivateAddresses || true
|
|
80
|
-
this.onIncomingConnection = onIncomingConnection
|
|
81
|
-
|
|
82
|
-
this.rpcCommunicator = new ListeningRpcCommunicator(WebRtcConnectorRpcLocal.WEBRTC_CONNECTOR_SERVICE_ID, config.transport, {
|
|
83
|
-
rpcRequestTimeout: 15000
|
|
84
|
-
})
|
|
85
|
-
this.rpcCommunicator.registerRpcNotification(RtcOffer, 'rtcOffer',
|
|
86
|
-
(req: RtcOffer, context: ServerCallContext) => this.rtcOffer(req, context))
|
|
87
|
-
this.rpcCommunicator.registerRpcNotification(RtcAnswer, 'rtcAnswer',
|
|
88
|
-
(req: RtcAnswer, context: ServerCallContext) => this.rtcAnswer(req, context))
|
|
89
|
-
this.rpcCommunicator.registerRpcNotification(IceCandidate, 'iceCandidate',
|
|
90
|
-
(req: IceCandidate, context: ServerCallContext) => this.iceCandidate(req, context))
|
|
91
|
-
this.rpcCommunicator.registerRpcNotification(WebRtcConnectionRequest, 'requestConnection',
|
|
92
|
-
(req: WebRtcConnectionRequest, context: ServerCallContext) => this.requestConnection(req, context))
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
connect(targetPeerDescriptor: PeerDescriptor): ManagedConnection {
|
|
96
|
-
if (areEqualPeerDescriptors(targetPeerDescriptor, this.ownPeerDescriptor!)) {
|
|
97
|
-
throw new Err.CannotConnectToSelf('Cannot open WebRTC Connection to self')
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
logger.trace(`Opening WebRTC connection to ${keyFromPeerDescriptor(targetPeerDescriptor)}`)
|
|
101
|
-
|
|
102
|
-
const peerKey = keyFromPeerDescriptor(targetPeerDescriptor)
|
|
103
|
-
const existingConnection = this.ongoingConnectAttempts.get(peerKey)
|
|
104
|
-
if (existingConnection) {
|
|
105
|
-
return existingConnection
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const connection = new NodeWebRtcConnection({
|
|
109
|
-
remotePeerDescriptor: targetPeerDescriptor,
|
|
110
|
-
iceServers: this.iceServers,
|
|
111
|
-
bufferThresholdLow: this.config.bufferThresholdLow,
|
|
112
|
-
bufferThresholdHigh: this.config.bufferThresholdHigh,
|
|
113
|
-
connectingTimeout: this.config.connectionTimeout,
|
|
114
|
-
portRange: this.config.portRange
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
const offering = this.isOffering(targetPeerDescriptor)
|
|
118
|
-
let managedConnection: ManagedWebRtcConnection
|
|
119
|
-
|
|
120
|
-
if (offering) {
|
|
121
|
-
managedConnection = new ManagedWebRtcConnection(this.ownPeerDescriptor!, connection)
|
|
122
|
-
} else {
|
|
123
|
-
managedConnection = new ManagedWebRtcConnection(this.ownPeerDescriptor!, undefined, connection)
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
managedConnection.setPeerDescriptor(targetPeerDescriptor)
|
|
127
|
-
|
|
128
|
-
this.ongoingConnectAttempts.set(keyFromPeerDescriptor(targetPeerDescriptor), managedConnection)
|
|
129
|
-
|
|
130
|
-
const delFunc = () => {
|
|
131
|
-
if (this.ongoingConnectAttempts.has(peerKey)) {
|
|
132
|
-
this.ongoingConnectAttempts.delete(peerKey)
|
|
133
|
-
}
|
|
134
|
-
connection.off('disconnected', delFunc)
|
|
135
|
-
managedConnection.off('handshakeCompleted', delFunc)
|
|
136
|
-
}
|
|
137
|
-
connection.on('disconnected', delFunc)
|
|
138
|
-
managedConnection.on('handshakeCompleted', delFunc)
|
|
139
|
-
|
|
140
|
-
const remoteConnector = new WebRtcConnectorRpcRemote(
|
|
141
|
-
this.ownPeerDescriptor!,
|
|
142
|
-
targetPeerDescriptor,
|
|
143
|
-
toProtoRpcClient(new WebRtcConnectorRpcClient(this.rpcCommunicator.getRpcClientTransport()))
|
|
144
|
-
)
|
|
145
|
-
|
|
146
|
-
connection.on('localCandidate', (candidate: string, mid: string) => {
|
|
147
|
-
if (this.config.externalIp) {
|
|
148
|
-
candidate = replaceInternalIpWithExternalIp(candidate, this.config.externalIp)
|
|
149
|
-
logger.debug(`onLocalCandidate injected external ip ${candidate} ${mid}`)
|
|
150
|
-
}
|
|
151
|
-
remoteConnector.sendIceCandidate(candidate, mid, connection.connectionId.toString())
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
if (offering) {
|
|
155
|
-
connection.once('localDescription', (description: string) => {
|
|
156
|
-
remoteConnector.sendRtcOffer(description, connection.connectionId.toString())
|
|
157
|
-
})
|
|
158
|
-
} else {
|
|
159
|
-
connection.once('localDescription', (description: string) => {
|
|
160
|
-
remoteConnector.sendRtcAnswer(description, connection.connectionId.toString())
|
|
161
|
-
})
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
connection.start(offering)
|
|
165
|
-
|
|
166
|
-
if (!offering) {
|
|
167
|
-
remoteConnector.requestConnection(connection.connectionId.toString())
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
return managedConnection
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
setOwnPeerDescriptor(peerDescriptor: PeerDescriptor): void {
|
|
174
|
-
this.ownPeerDescriptor = peerDescriptor
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
private isIceCandidateAllowed(candidate: string): boolean {
|
|
178
|
-
if (!this.allowPrivateAddresses) {
|
|
179
|
-
const address = getAddressFromIceCandidate(candidate)
|
|
180
|
-
if (address && isPrivateIPv4(address)) {
|
|
181
|
-
return false
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
return true
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
private onRtcOffer(
|
|
188
|
-
remotePeer: PeerDescriptor,
|
|
189
|
-
targetPeer: PeerDescriptor,
|
|
190
|
-
description: string,
|
|
191
|
-
connectionId: string
|
|
192
|
-
): void {
|
|
193
|
-
if (this.stopped || !areEqualPeerDescriptors(targetPeer, this.ownPeerDescriptor!)) {
|
|
194
|
-
return
|
|
195
|
-
}
|
|
196
|
-
const peerKey = keyFromPeerDescriptor(remotePeer)
|
|
197
|
-
let managedConnection = this.ongoingConnectAttempts.get(peerKey)
|
|
198
|
-
let connection = managedConnection?.getWebRtcConnection()
|
|
199
|
-
|
|
200
|
-
if (!managedConnection) {
|
|
201
|
-
connection = new NodeWebRtcConnection({ remotePeerDescriptor: remotePeer })
|
|
202
|
-
managedConnection = new ManagedWebRtcConnection(this.ownPeerDescriptor!, undefined, connection)
|
|
203
|
-
|
|
204
|
-
managedConnection.setPeerDescriptor(remotePeer)
|
|
205
|
-
|
|
206
|
-
this.ongoingConnectAttempts.set(peerKey, managedConnection)
|
|
207
|
-
this.onIncomingConnection(managedConnection)
|
|
208
|
-
|
|
209
|
-
const remoteConnector = new WebRtcConnectorRpcRemote(
|
|
210
|
-
this.ownPeerDescriptor!,
|
|
211
|
-
remotePeer,
|
|
212
|
-
toProtoRpcClient(new WebRtcConnectorRpcClient(this.rpcCommunicator.getRpcClientTransport()))
|
|
213
|
-
)
|
|
214
|
-
|
|
215
|
-
connection.on('localCandidate', (candidate: string, mid: string) => {
|
|
216
|
-
remoteConnector.sendIceCandidate(candidate, mid, connection!.connectionId.toString())
|
|
217
|
-
})
|
|
218
|
-
|
|
219
|
-
connection.once('localDescription', (description: string) => {
|
|
220
|
-
remoteConnector.sendRtcAnswer(description, connection!.connectionId.toString())
|
|
221
|
-
})
|
|
222
|
-
|
|
223
|
-
connection.start(false)
|
|
224
|
-
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// Always use offerers connectionId
|
|
228
|
-
connection!.setConnectionId(connectionId)
|
|
229
|
-
connection!.setRemoteDescription(description, 'offer')
|
|
230
|
-
|
|
231
|
-
managedConnection.on('handshakeRequest', () => {
|
|
232
|
-
if (this.ongoingConnectAttempts.has(peerKey)) {
|
|
233
|
-
this.ongoingConnectAttempts.delete(peerKey)
|
|
234
|
-
}
|
|
235
|
-
managedConnection!.acceptHandshake()
|
|
236
|
-
})
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
private onRtcAnswer(
|
|
240
|
-
remotePeerDescriptor: PeerDescriptor,
|
|
241
|
-
targetPeerDescriptor: PeerDescriptor,
|
|
242
|
-
description: string,
|
|
243
|
-
connectionId: string
|
|
244
|
-
): void {
|
|
245
|
-
if (this.stopped || !areEqualPeerDescriptors(targetPeerDescriptor, this.ownPeerDescriptor!)) {
|
|
246
|
-
return
|
|
247
|
-
}
|
|
248
|
-
const peerKey = keyFromPeerDescriptor(remotePeerDescriptor)
|
|
249
|
-
const connection = this.ongoingConnectAttempts.get(peerKey)?.getWebRtcConnection()
|
|
250
|
-
if (!connection) {
|
|
251
|
-
return
|
|
252
|
-
} else if (connection.connectionId.toString() !== connectionId) {
|
|
253
|
-
logger.trace(`Ignoring RTC answer due to connectionId mismatch`)
|
|
254
|
-
return
|
|
255
|
-
}
|
|
256
|
-
connection.setRemoteDescription(description, 'answer')
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
private onConnectionRequest(targetPeerDescriptor: PeerDescriptor): void {
|
|
260
|
-
if (this.stopped || this.ongoingConnectAttempts.has(keyFromPeerDescriptor(targetPeerDescriptor))) {
|
|
261
|
-
return
|
|
262
|
-
}
|
|
263
|
-
const managedConnection = this.connect(targetPeerDescriptor)
|
|
264
|
-
managedConnection.setPeerDescriptor(targetPeerDescriptor)
|
|
265
|
-
|
|
266
|
-
this.onIncomingConnection(managedConnection)
|
|
267
|
-
}
|
|
268
|
-
private onRemoteCandidate(
|
|
269
|
-
remotePeerDescriptor: PeerDescriptor,
|
|
270
|
-
targetPeerDescriptor: PeerDescriptor,
|
|
271
|
-
candidate: string,
|
|
272
|
-
mid: string,
|
|
273
|
-
connectionId: string
|
|
274
|
-
): void {
|
|
275
|
-
if (this.stopped || !areEqualPeerDescriptors(targetPeerDescriptor, this.ownPeerDescriptor!)) {
|
|
276
|
-
return
|
|
277
|
-
}
|
|
278
|
-
const peerKey = keyFromPeerDescriptor(remotePeerDescriptor)
|
|
279
|
-
const connection = this.ongoingConnectAttempts.get(peerKey)?.getWebRtcConnection()
|
|
280
|
-
|
|
281
|
-
if (!connection) {
|
|
282
|
-
return
|
|
283
|
-
} else if (connection.connectionId.toString() !== connectionId) {
|
|
284
|
-
logger.trace(`Ignoring remote candidate due to connectionId mismatch`)
|
|
285
|
-
return
|
|
286
|
-
} else if (this.isIceCandidateAllowed(candidate)) {
|
|
287
|
-
connection.addRemoteCandidate(candidate, mid)
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
public async stop(): Promise<void> {
|
|
292
|
-
logger.trace('stop()')
|
|
293
|
-
this.stopped = true
|
|
294
|
-
|
|
295
|
-
const attempts = Array.from(this.ongoingConnectAttempts.values())
|
|
296
|
-
await Promise.allSettled(attempts.map((conn) => conn.close('OTHER')))
|
|
297
|
-
|
|
298
|
-
this.rpcCommunicator.destroy()
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
public isOffering(targetPeerDescriptor: PeerDescriptor): boolean {
|
|
302
|
-
const myId = peerIdFromPeerDescriptor(this.ownPeerDescriptor!)
|
|
303
|
-
const theirId = peerIdFromPeerDescriptor(targetPeerDescriptor)
|
|
304
|
-
return myId.hasSmallerHashThan(theirId)
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
// IWebRTCConnector implementation
|
|
308
|
-
// TODO should we read connectionId from WebRtcConnectionRequest (or remove the field)?
|
|
309
|
-
async requestConnection(_request: WebRtcConnectionRequest, context: ServerCallContext): Promise<Empty> {
|
|
310
|
-
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
311
|
-
this.onConnectionRequest(senderPeerDescriptor)
|
|
312
|
-
return {}
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
async rtcOffer(request: RtcOffer, context: ServerCallContext): Promise<Empty> {
|
|
316
|
-
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
317
|
-
const receiverPeerDescriptor = (context as DhtCallContext).incomingTargetDescriptor!
|
|
318
|
-
this.onRtcOffer(senderPeerDescriptor, receiverPeerDescriptor, request.description, request.connectionId)
|
|
319
|
-
return {}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
async rtcAnswer(request: RtcAnswer, context: ServerCallContext): Promise<Empty> {
|
|
323
|
-
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
324
|
-
const receiverPeerDescriptor = (context as DhtCallContext).incomingTargetDescriptor!
|
|
325
|
-
this.onRtcAnswer(senderPeerDescriptor, receiverPeerDescriptor, request.description, request.connectionId)
|
|
326
|
-
return {}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
async iceCandidate(request: IceCandidate, context: ServerCallContext): Promise<Empty> {
|
|
330
|
-
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
331
|
-
const receiverPeerDescriptor = (context as DhtCallContext).incomingTargetDescriptor!
|
|
332
|
-
this.onRemoteCandidate(senderPeerDescriptor, receiverPeerDescriptor, request.candidate, request.mid, request.connectionId)
|
|
333
|
-
return {}
|
|
334
|
-
}
|
|
335
|
-
}
|
|
@@ -1,295 +0,0 @@
|
|
|
1
|
-
import { ClientWebSocket } from './ClientWebSocket'
|
|
2
|
-
import { IConnection, ConnectionType } from '../IConnection'
|
|
3
|
-
import { ITransport } from '../../transport/ITransport'
|
|
4
|
-
import { ListeningRpcCommunicator } from '../../transport/ListeningRpcCommunicator'
|
|
5
|
-
import { WebSocketConnectorRpcRemote } from './WebSocketConnectorRpcRemote'
|
|
6
|
-
import {
|
|
7
|
-
ConnectivityMethod,
|
|
8
|
-
ConnectivityResponse,
|
|
9
|
-
NodeType,
|
|
10
|
-
PeerDescriptor,
|
|
11
|
-
WebSocketConnectionRequest,
|
|
12
|
-
WebSocketConnectionResponse
|
|
13
|
-
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
14
|
-
import { WebSocketConnectorRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
15
|
-
import { Logger, binaryToHex, wait } from '@streamr/utils'
|
|
16
|
-
import { IWebSocketConnectorRpc } from '../../proto/packages/dht/protos/DhtRpc.server'
|
|
17
|
-
import { ManagedConnection } from '../ManagedConnection'
|
|
18
|
-
import { WebSocketServer } from './WebSocketServer'
|
|
19
|
-
import { ConnectivityChecker } from '../ConnectivityChecker'
|
|
20
|
-
import { NatType, PortRange, TlsCertificate } from '../ConnectionManager'
|
|
21
|
-
import { PeerIDKey } from '../../helpers/PeerID'
|
|
22
|
-
import { ServerWebSocket } from './ServerWebSocket'
|
|
23
|
-
import { toProtoRpcClient } from '@streamr/proto-rpc'
|
|
24
|
-
import { Handshaker } from '../Handshaker'
|
|
25
|
-
import { keyFromPeerDescriptor, peerIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
26
|
-
import { ParsedUrlQuery } from 'querystring'
|
|
27
|
-
import { range, sample } from 'lodash'
|
|
28
|
-
import { isPrivateIPv4 } from '../../helpers/AddressTools'
|
|
29
|
-
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
30
|
-
import { DhtCallContext } from '../../rpc-protocol/DhtCallContext'
|
|
31
|
-
|
|
32
|
-
const logger = new Logger(module)
|
|
33
|
-
|
|
34
|
-
export const connectivityMethodToWebSocketUrl = (ws: ConnectivityMethod): string => {
|
|
35
|
-
return (ws.tls ? 'wss://' : 'ws://') + ws.host + ':' + ws.port
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const canOpenConnectionFromBrowser = (websocketServer: ConnectivityMethod) => {
|
|
39
|
-
const hasPrivateAddress = ((websocketServer.host === 'localhost') || isPrivateIPv4(websocketServer.host))
|
|
40
|
-
return websocketServer.tls || hasPrivateAddress
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const ENTRY_POINT_CONNECTION_ATTEMPTS = 5
|
|
44
|
-
|
|
45
|
-
interface WebSocketConnectorRpcLocalConfig {
|
|
46
|
-
transport: ITransport
|
|
47
|
-
canConnect: (peerDescriptor: PeerDescriptor, _ip: string, port: number) => boolean
|
|
48
|
-
onIncomingConnection: (connection: ManagedConnection) => boolean
|
|
49
|
-
portRange?: PortRange
|
|
50
|
-
maxMessageSize?: number
|
|
51
|
-
host?: string
|
|
52
|
-
entrypoints?: PeerDescriptor[]
|
|
53
|
-
tlsCertificate?: TlsCertificate
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export class WebSocketConnectorRpcLocal implements IWebSocketConnectorRpc {
|
|
57
|
-
|
|
58
|
-
private static readonly WEBSOCKET_CONNECTOR_SERVICE_ID = 'system/websocket-connector'
|
|
59
|
-
private readonly rpcCommunicator: ListeningRpcCommunicator
|
|
60
|
-
private readonly canConnectFunction: (peerDescriptor: PeerDescriptor, _ip: string, port: number) => boolean
|
|
61
|
-
private readonly webSocketServer?: WebSocketServer
|
|
62
|
-
private connectivityChecker?: ConnectivityChecker
|
|
63
|
-
private readonly ongoingConnectRequests: Map<PeerIDKey, ManagedConnection> = new Map()
|
|
64
|
-
private onIncomingConnection: (connection: ManagedConnection) => boolean
|
|
65
|
-
private host?: string
|
|
66
|
-
private readonly entrypoints?: PeerDescriptor[]
|
|
67
|
-
private readonly tlsCertificate?: TlsCertificate
|
|
68
|
-
private selectedPort?: number
|
|
69
|
-
private ownPeerDescriptor?: PeerDescriptor
|
|
70
|
-
private connectingConnections: Map<PeerIDKey, ManagedConnection> = new Map()
|
|
71
|
-
private destroyed = false
|
|
72
|
-
|
|
73
|
-
constructor(config: WebSocketConnectorRpcLocalConfig) {
|
|
74
|
-
this.webSocketServer = config.portRange ? new WebSocketServer({
|
|
75
|
-
portRange: config.portRange!,
|
|
76
|
-
tlsCertificate: config.tlsCertificate,
|
|
77
|
-
maxMessageSize: config.maxMessageSize
|
|
78
|
-
}) : undefined
|
|
79
|
-
this.onIncomingConnection = config.onIncomingConnection
|
|
80
|
-
this.host = config.host
|
|
81
|
-
this.entrypoints = config.entrypoints
|
|
82
|
-
this.tlsCertificate = config.tlsCertificate
|
|
83
|
-
|
|
84
|
-
this.canConnectFunction = config.canConnect.bind(this)
|
|
85
|
-
|
|
86
|
-
this.rpcCommunicator = new ListeningRpcCommunicator(WebSocketConnectorRpcLocal.WEBSOCKET_CONNECTOR_SERVICE_ID, config.transport, {
|
|
87
|
-
rpcRequestTimeout: 15000
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
this.rpcCommunicator.registerRpcMethod(
|
|
91
|
-
WebSocketConnectionRequest,
|
|
92
|
-
WebSocketConnectionResponse,
|
|
93
|
-
'requestConnection',
|
|
94
|
-
(req: WebSocketConnectionRequest, context: ServerCallContext) => this.requestConnection(req, context)
|
|
95
|
-
)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
private attachHandshaker(connection: IConnection) {
|
|
99
|
-
const handshaker = new Handshaker(this.ownPeerDescriptor!, connection)
|
|
100
|
-
handshaker.once('handshakeRequest', (peerDescriptor: PeerDescriptor) => {
|
|
101
|
-
this.onServerSocketHandshakeRequest(peerDescriptor, connection)
|
|
102
|
-
})
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
public async start(): Promise<void> {
|
|
106
|
-
if (!this.destroyed && this.webSocketServer) {
|
|
107
|
-
this.webSocketServer.on('connected', (connection: IConnection) => {
|
|
108
|
-
|
|
109
|
-
const serverSocket = connection as unknown as ServerWebSocket
|
|
110
|
-
if (serverSocket.resourceURL &&
|
|
111
|
-
serverSocket.resourceURL.query) {
|
|
112
|
-
const query = serverSocket.resourceURL.query as unknown as ParsedUrlQuery
|
|
113
|
-
if (query.connectivityRequest) {
|
|
114
|
-
logger.trace('Received connectivity request connection from ' + serverSocket.getRemoteAddress())
|
|
115
|
-
this.connectivityChecker!.listenToIncomingConnectivityRequests(serverSocket)
|
|
116
|
-
} else if (query.connectivityProbe) {
|
|
117
|
-
logger.trace('Received connectivity probe connection from ' + serverSocket.getRemoteAddress())
|
|
118
|
-
} else {
|
|
119
|
-
this.attachHandshaker(connection)
|
|
120
|
-
}
|
|
121
|
-
} else {
|
|
122
|
-
this.attachHandshaker(connection)
|
|
123
|
-
}
|
|
124
|
-
})
|
|
125
|
-
const port = await this.webSocketServer.start()
|
|
126
|
-
this.selectedPort = port
|
|
127
|
-
this.connectivityChecker = new ConnectivityChecker(this.selectedPort, this.tlsCertificate !== undefined, this.host)
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
public async checkConnectivity(): Promise<ConnectivityResponse> {
|
|
132
|
-
// TODO: this could throw if the server is not running
|
|
133
|
-
const noServerConnectivityResponse: ConnectivityResponse = {
|
|
134
|
-
openInternet: false,
|
|
135
|
-
host: '127.0.0.1',
|
|
136
|
-
natType: NatType.UNKNOWN
|
|
137
|
-
}
|
|
138
|
-
if (this.destroyed) {
|
|
139
|
-
return noServerConnectivityResponse
|
|
140
|
-
}
|
|
141
|
-
for (const reattempt of range(ENTRY_POINT_CONNECTION_ATTEMPTS)) {
|
|
142
|
-
const entryPoint = sample(this.entrypoints)!
|
|
143
|
-
try {
|
|
144
|
-
if (!this.webSocketServer) {
|
|
145
|
-
// If no websocket server, return openInternet: false
|
|
146
|
-
return noServerConnectivityResponse
|
|
147
|
-
} else {
|
|
148
|
-
if (!this.entrypoints || this.entrypoints.length < 1) {
|
|
149
|
-
// return connectivity info given in config
|
|
150
|
-
const preconfiguredConnectivityResponse: ConnectivityResponse = {
|
|
151
|
-
openInternet: true,
|
|
152
|
-
host: this.host!,
|
|
153
|
-
natType: NatType.OPEN_INTERNET,
|
|
154
|
-
websocket: { host: this.host!, port: this.selectedPort!, tls: this.tlsCertificate !== undefined }
|
|
155
|
-
}
|
|
156
|
-
return preconfiguredConnectivityResponse
|
|
157
|
-
} else {
|
|
158
|
-
// Do real connectivity checking
|
|
159
|
-
return await this.connectivityChecker!.sendConnectivityRequest(entryPoint)
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
} catch (err) {
|
|
163
|
-
if (reattempt < ENTRY_POINT_CONNECTION_ATTEMPTS) {
|
|
164
|
-
const error = `Failed to connect to entrypoint with id ${binaryToHex(entryPoint.kademliaId)} `
|
|
165
|
-
+ `and URL ${connectivityMethodToWebSocketUrl(entryPoint.websocket!)}`
|
|
166
|
-
logger.error(error, { error: err })
|
|
167
|
-
await wait(2000)
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
throw Error(`Failed to connect to the entrypoints after ${ENTRY_POINT_CONNECTION_ATTEMPTS} attempts`)
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
public isPossibleToFormConnection(targetPeerDescriptor: PeerDescriptor): boolean {
|
|
175
|
-
if (this.ownPeerDescriptor!.websocket !== undefined) {
|
|
176
|
-
return (targetPeerDescriptor.type !== NodeType.BROWSER) || canOpenConnectionFromBrowser(this.ownPeerDescriptor!.websocket)
|
|
177
|
-
} else if (targetPeerDescriptor.websocket !== undefined) {
|
|
178
|
-
return (this.ownPeerDescriptor!.type !== NodeType.BROWSER) || canOpenConnectionFromBrowser(targetPeerDescriptor.websocket)
|
|
179
|
-
} else {
|
|
180
|
-
return false
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
public connect(targetPeerDescriptor: PeerDescriptor): ManagedConnection {
|
|
185
|
-
const peerKey = keyFromPeerDescriptor(targetPeerDescriptor)
|
|
186
|
-
const existingConnection = this.connectingConnections.get(peerKey)
|
|
187
|
-
if (existingConnection) {
|
|
188
|
-
return existingConnection
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if (this.ownPeerDescriptor!.websocket && !targetPeerDescriptor.websocket) {
|
|
192
|
-
return this.requestConnectionFromPeer(this.ownPeerDescriptor!, targetPeerDescriptor)
|
|
193
|
-
} else {
|
|
194
|
-
const socket = new ClientWebSocket()
|
|
195
|
-
|
|
196
|
-
const url = connectivityMethodToWebSocketUrl(targetPeerDescriptor.websocket!)
|
|
197
|
-
|
|
198
|
-
const managedConnection = new ManagedConnection(this.ownPeerDescriptor!, ConnectionType.WEBSOCKET_CLIENT, socket, undefined)
|
|
199
|
-
managedConnection.setPeerDescriptor(targetPeerDescriptor)
|
|
200
|
-
|
|
201
|
-
this.connectingConnections.set(keyFromPeerDescriptor(targetPeerDescriptor), managedConnection)
|
|
202
|
-
|
|
203
|
-
const delFunc = () => {
|
|
204
|
-
if (this.connectingConnections.has(peerKey)) {
|
|
205
|
-
this.connectingConnections.delete(peerKey)
|
|
206
|
-
}
|
|
207
|
-
socket.off('disconnected', delFunc)
|
|
208
|
-
managedConnection.off('handshakeCompleted', delFunc)
|
|
209
|
-
}
|
|
210
|
-
socket.on('disconnected', delFunc)
|
|
211
|
-
managedConnection.on('handshakeCompleted', delFunc)
|
|
212
|
-
|
|
213
|
-
socket.connect(url)
|
|
214
|
-
|
|
215
|
-
return managedConnection
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
private requestConnectionFromPeer(ownPeerDescriptor: PeerDescriptor, targetPeerDescriptor: PeerDescriptor): ManagedConnection {
|
|
220
|
-
setImmediate(() => {
|
|
221
|
-
const remoteConnector = new WebSocketConnectorRpcRemote(
|
|
222
|
-
ownPeerDescriptor,
|
|
223
|
-
targetPeerDescriptor,
|
|
224
|
-
toProtoRpcClient(new WebSocketConnectorRpcClient(this.rpcCommunicator.getRpcClientTransport()))
|
|
225
|
-
)
|
|
226
|
-
remoteConnector.requestConnection(ownPeerDescriptor.websocket!.host, ownPeerDescriptor.websocket!.port)
|
|
227
|
-
})
|
|
228
|
-
const managedConnection = new ManagedConnection(this.ownPeerDescriptor!, ConnectionType.WEBSOCKET_SERVER)
|
|
229
|
-
managedConnection.on('disconnected', () => this.ongoingConnectRequests.delete(keyFromPeerDescriptor(targetPeerDescriptor)))
|
|
230
|
-
managedConnection.setPeerDescriptor(targetPeerDescriptor)
|
|
231
|
-
this.ongoingConnectRequests.set(keyFromPeerDescriptor(targetPeerDescriptor), managedConnection)
|
|
232
|
-
return managedConnection
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
private onServerSocketHandshakeRequest = (peerDescriptor: PeerDescriptor, serverWebSocket: IConnection) => {
|
|
236
|
-
|
|
237
|
-
const peerId = peerIdFromPeerDescriptor(peerDescriptor)
|
|
238
|
-
|
|
239
|
-
if (this.ongoingConnectRequests.has(peerId.toKey())) {
|
|
240
|
-
const ongoingConnectReguest = this.ongoingConnectRequests.get(peerId.toKey())!
|
|
241
|
-
ongoingConnectReguest.attachImplementation(serverWebSocket)
|
|
242
|
-
ongoingConnectReguest.acceptHandshake()
|
|
243
|
-
this.ongoingConnectRequests.delete(peerId.toKey())
|
|
244
|
-
} else {
|
|
245
|
-
const managedConnection = new ManagedConnection(this.ownPeerDescriptor!, ConnectionType.WEBSOCKET_SERVER, undefined, serverWebSocket)
|
|
246
|
-
|
|
247
|
-
managedConnection.setPeerDescriptor(peerDescriptor)
|
|
248
|
-
|
|
249
|
-
if (this.onIncomingConnection(managedConnection)) {
|
|
250
|
-
managedConnection.acceptHandshake()
|
|
251
|
-
} else {
|
|
252
|
-
managedConnection.rejectHandshake('Duplicate connection')
|
|
253
|
-
managedConnection.destroy()
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
public setOwnPeerDescriptor(ownPeerDescriptor: PeerDescriptor): void {
|
|
259
|
-
this.ownPeerDescriptor = ownPeerDescriptor
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
public async destroy(): Promise<void> {
|
|
263
|
-
this.destroyed = true
|
|
264
|
-
this.rpcCommunicator.destroy()
|
|
265
|
-
|
|
266
|
-
const requests = Array.from(this.ongoingConnectRequests.values())
|
|
267
|
-
await Promise.allSettled(requests.map((conn) => conn.close('OTHER')))
|
|
268
|
-
|
|
269
|
-
const attempts = Array.from(this.connectingConnections.values())
|
|
270
|
-
await Promise.allSettled(attempts.map((conn) => conn.close('OTHER')))
|
|
271
|
-
this.connectivityChecker?.destroy()
|
|
272
|
-
await this.webSocketServer?.stop()
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
// IWebSocketConnectorRpc implementation
|
|
276
|
-
public async requestConnection(request: WebSocketConnectionRequest, context: ServerCallContext): Promise<WebSocketConnectionResponse> {
|
|
277
|
-
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
278
|
-
if (!this.destroyed && this.canConnectFunction(senderPeerDescriptor, request.ip, request.port)) {
|
|
279
|
-
setImmediate(() => {
|
|
280
|
-
if (this.destroyed) {
|
|
281
|
-
return
|
|
282
|
-
}
|
|
283
|
-
const connection = this.connect(senderPeerDescriptor)
|
|
284
|
-
this.onIncomingConnection(connection)
|
|
285
|
-
})
|
|
286
|
-
const res: WebSocketConnectionResponse = {
|
|
287
|
-
accepted: true
|
|
288
|
-
}
|
|
289
|
-
return res
|
|
290
|
-
}
|
|
291
|
-
return {
|
|
292
|
-
accepted: false
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { DhtNode } from '../dht/DhtNode'
|
|
2
|
-
import { ExternalStoreDataRequest, ExternalStoreDataResponse, FindDataRequest, FindDataResponse } from '../proto/packages/dht/protos/DhtRpc'
|
|
3
|
-
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
4
|
-
import { DhtCallContext } from '../rpc-protocol/DhtCallContext'
|
|
5
|
-
|
|
6
|
-
export const registerExternalApiRpcMethods = (thisNode: DhtNode): void => {
|
|
7
|
-
const rpcCommunicator = thisNode.getRpcCommunicator()
|
|
8
|
-
rpcCommunicator.registerRpcMethod(
|
|
9
|
-
FindDataRequest,
|
|
10
|
-
FindDataResponse,
|
|
11
|
-
'findData',
|
|
12
|
-
(req: FindDataRequest, context: ServerCallContext) => findData(thisNode, req, context),
|
|
13
|
-
{ timeout: 10000 }
|
|
14
|
-
)
|
|
15
|
-
rpcCommunicator.registerRpcMethod(
|
|
16
|
-
ExternalStoreDataRequest,
|
|
17
|
-
ExternalStoreDataResponse,
|
|
18
|
-
'externalStoreData',
|
|
19
|
-
(req: ExternalStoreDataRequest) => externalStoreData(thisNode, req),
|
|
20
|
-
{ timeout: 10000 }
|
|
21
|
-
)
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// IDHTRpcService method for external findRecursive calls
|
|
25
|
-
const findData = async (thisNode: DhtNode, findDataRequest: FindDataRequest, context: ServerCallContext): Promise<FindDataResponse> => {
|
|
26
|
-
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
27
|
-
const result = await thisNode.startRecursiveFind(findDataRequest.kademliaId, true, senderPeerDescriptor)
|
|
28
|
-
if (result.dataEntries) {
|
|
29
|
-
return FindDataResponse.create({ dataEntries: result.dataEntries })
|
|
30
|
-
} else {
|
|
31
|
-
return FindDataResponse.create({
|
|
32
|
-
dataEntries: [],
|
|
33
|
-
error: 'Could not find data with the given key'
|
|
34
|
-
})
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// IDHTRpcService method for external storeData calls
|
|
39
|
-
const externalStoreData = async (thisNode: DhtNode, request: ExternalStoreDataRequest): Promise<ExternalStoreDataResponse> => {
|
|
40
|
-
const result = await thisNode.storeDataToDht(request.key, request.data!)
|
|
41
|
-
return ExternalStoreDataResponse.create({
|
|
42
|
-
storers: result
|
|
43
|
-
})
|
|
44
|
-
}
|