@streamr/dht 100.0.0-pretestnet.6 → 100.0.0-testnet-one.1
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.js +2 -2
- package/dist/src/connection/ConnectionLockHandler.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.d.ts +2 -2
- package/dist/src/connection/ConnectionLockRpcRemote.js +3 -27
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +0 -1
- package/dist/src/connection/ConnectionManager.js +11 -7
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +2 -2
- package/dist/src/connection/ConnectorFacade.js +1 -2
- package/dist/src/connection/ConnectorFacade.js.map +1 -1
- package/dist/src/connection/ManagedConnection.js +1 -0
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/connectivityChecker.d.ts +9 -0
- package/dist/src/connection/connectivityChecker.js +122 -0
- package/dist/src/connection/connectivityChecker.js.map +1 -0
- package/dist/src/connection/connectivityRequestHandler.d.ts +2 -0
- package/dist/src/connection/connectivityRequestHandler.js +79 -0
- package/dist/src/connection/connectivityRequestHandler.js.map +1 -0
- package/dist/src/connection/simulator/Simulator.js +3 -2
- package/dist/src/connection/simulator/Simulator.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnection.js +1 -1
- package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.js +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.d.ts +2 -2
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js +2 -2
- package/dist/src/connection/webrtc/WebrtcConnectorRpcRemote.js.map +1 -1
- package/dist/src/connection/webrtc/iceServerAsString.js +1 -1
- package/dist/src/connection/webrtc/iceServerAsString.js.map +1 -1
- package/dist/src/connection/websocket/ClientWebsocket.d.ts +1 -0
- package/dist/src/connection/websocket/ClientWebsocket.js +7 -3
- package/dist/src/connection/websocket/ClientWebsocket.js.map +1 -1
- package/dist/src/connection/websocket/ServerWebsocket.d.ts +4 -0
- package/dist/src/connection/websocket/ServerWebsocket.js +32 -21
- package/dist/src/connection/websocket/ServerWebsocket.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnector.d.ts +0 -2
- package/dist/src/connection/websocket/WebsocketConnector.js +61 -16
- package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +8 -11
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +4 -4
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js +5 -39
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketServer.js +21 -4
- package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
- package/dist/src/dht/DhtNode.d.ts +13 -23
- package/dist/src/dht/DhtNode.js +97 -226
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +1 -4
- package/dist/src/dht/DhtNodeRpcLocal.js +1 -5
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcRemote.d.ts +3 -3
- package/dist/src/dht/DhtNodeRpcRemote.js +4 -4
- package/dist/src/dht/DhtNodeRpcRemote.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +4 -4
- package/dist/src/dht/ExternalApiRpcLocal.js +5 -12
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.d.ts +3 -3
- package/dist/src/dht/ExternalApiRpcRemote.js +5 -5
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/PeerManager.d.ts +52 -0
- package/dist/src/dht/PeerManager.js +273 -0
- package/dist/src/dht/PeerManager.js.map +1 -0
- package/dist/src/dht/contact/ContactList.d.ts +1 -1
- package/dist/src/dht/contact/ContactList.js +1 -0
- package/dist/src/dht/contact/ContactList.js.map +1 -1
- package/dist/src/dht/contact/{Remote.d.ts → RpcRemote.d.ts} +3 -3
- package/dist/src/dht/contact/{Remote.js → RpcRemote.js} +8 -8
- package/dist/src/dht/contact/RpcRemote.js.map +1 -0
- package/dist/src/dht/contact/SortedContactList.d.ts +20 -6
- package/dist/src/dht/contact/SortedContactList.js +55 -24
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +4 -14
- package/dist/src/dht/discovery/DiscoverySession.js +15 -26
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +2 -9
- package/dist/src/dht/discovery/PeerDiscovery.js +11 -19
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/find/FindRpcLocal.js +2 -1
- package/dist/src/dht/find/FindRpcLocal.js.map +1 -1
- package/dist/src/dht/find/FindSession.d.ts +6 -6
- package/dist/src/dht/find/FindSession.js +18 -13
- package/dist/src/dht/find/FindSession.js.map +1 -1
- package/dist/src/dht/find/FindSessionRpcLocal.d.ts +1 -1
- package/dist/src/dht/find/FindSessionRpcRemote.d.ts +2 -2
- package/dist/src/dht/find/FindSessionRpcRemote.js +2 -2
- package/dist/src/dht/find/FindSessionRpcRemote.js.map +1 -1
- package/dist/src/dht/find/Finder.d.ts +4 -4
- package/dist/src/dht/find/Finder.js +55 -42
- package/dist/src/dht/find/Finder.js.map +1 -1
- package/dist/src/dht/routing/FindRpcRemote.d.ts +2 -2
- package/dist/src/dht/routing/FindRpcRemote.js +7 -5
- package/dist/src/dht/routing/FindRpcRemote.js.map +1 -1
- package/dist/src/dht/routing/Router.d.ts +3 -7
- package/dist/src/dht/routing/Router.js +29 -22
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.d.ts +2 -2
- package/dist/src/dht/routing/RouterRpcLocal.js +4 -3
- package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcRemote.d.ts +2 -2
- package/dist/src/dht/routing/RouterRpcRemote.js +13 -8
- package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +1 -1
- package/dist/src/dht/routing/RoutingSession.js +23 -11
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/routing/getPreviousPeer.js.map +1 -1
- package/dist/src/dht/store/LocalDataStore.d.ts +3 -3
- package/dist/src/dht/store/LocalDataStore.js +18 -17
- package/dist/src/dht/store/LocalDataStore.js.map +1 -1
- package/dist/src/dht/store/StoreRpcLocal.d.ts +10 -9
- package/dist/src/dht/store/StoreRpcLocal.js +108 -102
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/dht/store/StoreRpcRemote.d.ts +4 -5
- package/dist/src/dht/store/StoreRpcRemote.js +6 -15
- package/dist/src/dht/store/StoreRpcRemote.js.map +1 -1
- package/dist/src/exports.d.ts +1 -1
- package/dist/src/exports.js +4 -4
- package/dist/src/exports.js.map +1 -1
- package/dist/src/helpers/PeerID.d.ts +1 -0
- package/dist/src/helpers/PeerID.js +9 -4
- package/dist/src/helpers/PeerID.js.map +1 -1
- package/dist/src/helpers/UUID.js +1 -1
- package/dist/src/helpers/UUID.js.map +1 -1
- package/dist/src/helpers/nodeId.d.ts +1 -0
- package/dist/src/helpers/{kademliaId.js → nodeId.js} +4 -4
- package/dist/src/helpers/nodeId.js.map +1 -0
- package/dist/src/helpers/peerIdFromPeerDescriptor.js +4 -4
- package/dist/src/helpers/peerIdFromPeerDescriptor.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +5 -16
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +2 -9
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +80 -95
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +67 -66
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +3 -10
- package/dist/src/transport/RoutingRpcCommunicator.js +2 -0
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
- package/karma.config.js +2 -2
- package/package.json +5 -5
- package/protos/DhtRpc.proto +36 -36
- package/src/connection/ConnectionLockHandler.ts +2 -2
- package/src/connection/ConnectionLockRpcRemote.ts +3 -4
- package/src/connection/ConnectionManager.ts +19 -17
- package/src/connection/ConnectorFacade.ts +5 -7
- package/src/connection/ManagedConnection.ts +1 -0
- package/src/connection/connectivityChecker.ts +102 -0
- package/src/connection/connectivityRequestHandler.ts +79 -0
- package/src/connection/simulator/Simulator.ts +3 -2
- package/src/connection/simulator/SimulatorConnection.ts +1 -1
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +10 -10
- package/src/connection/webrtc/NodeWebrtcConnection.ts +1 -1
- package/src/connection/webrtc/WebrtcConnector.ts +1 -1
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +1 -1
- package/src/connection/webrtc/WebrtcConnectorRpcRemote.ts +2 -2
- package/src/connection/webrtc/iceServerAsString.ts +1 -1
- package/src/connection/websocket/ClientWebsocket.ts +6 -2
- package/src/connection/websocket/ServerWebsocket.ts +40 -25
- package/src/connection/websocket/WebsocketConnector.ts +43 -22
- package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +9 -11
- package/src/connection/websocket/WebsocketConnectorRpcRemote.ts +7 -16
- package/src/connection/websocket/WebsocketServer.ts +20 -5
- package/src/dht/DhtNode.ts +123 -280
- package/src/dht/DhtNodeRpcLocal.ts +2 -9
- package/src/dht/DhtNodeRpcRemote.ts +4 -4
- package/src/dht/ExternalApiRpcLocal.ts +8 -13
- package/src/dht/ExternalApiRpcRemote.ts +5 -5
- package/src/dht/PeerManager.ts +330 -0
- package/src/dht/contact/ContactList.ts +3 -2
- package/src/dht/contact/{Remote.ts → RpcRemote.ts} +7 -6
- package/src/dht/contact/SortedContactList.ts +87 -44
- package/src/dht/discovery/DiscoverySession.ts +19 -44
- package/src/dht/discovery/PeerDiscovery.ts +16 -28
- package/src/dht/find/FindRpcLocal.ts +2 -2
- package/src/dht/find/FindSession.ts +25 -20
- package/src/dht/find/FindSessionRpcLocal.ts +1 -1
- package/src/dht/find/FindSessionRpcRemote.ts +2 -2
- package/src/dht/find/Finder.ts +84 -64
- package/src/dht/routing/FindRpcRemote.ts +7 -5
- package/src/dht/routing/Router.ts +30 -25
- package/src/dht/routing/RouterRpcLocal.ts +5 -5
- package/src/dht/routing/RouterRpcRemote.ts +13 -10
- package/src/dht/routing/RoutingSession.ts +22 -17
- package/src/dht/routing/getPreviousPeer.ts +1 -1
- package/src/dht/store/LocalDataStore.ts +18 -17
- package/src/dht/store/StoreRpcLocal.ts +118 -113
- package/src/dht/store/StoreRpcRemote.ts +7 -23
- package/src/exports.ts +1 -1
- package/src/helpers/PeerID.ts +8 -4
- package/src/helpers/UUID.ts +1 -1
- package/src/helpers/{kademliaId.ts → nodeId.ts} +1 -1
- package/src/helpers/peerIdFromPeerDescriptor.ts +6 -6
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +6 -20
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +3 -10
- package/src/proto/packages/dht/protos/DhtRpc.ts +103 -135
- package/src/transport/RoutingRpcCommunicator.ts +2 -0
- package/test/benchmark/Find.test.ts +5 -5
- package/test/benchmark/KademliaCorrectness.test.ts +3 -3
- package/test/benchmark/SortedContactListBenchmark.test.ts +150 -0
- package/test/benchmark/WebsocketServerMemoryLeak.test.ts +41 -0
- package/test/benchmark/kademlia-simulation/Contact.ts +1 -1
- package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +1 -1
- package/test/benchmark/kademlia-simulation/SimulationNode.ts +6 -1
- package/test/end-to-end/Layer0-Layer1.test.ts +1 -1
- package/test/end-to-end/Layer0.test.ts +4 -4
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +11 -11
- package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +6 -6
- package/test/end-to-end/Layer0Webrtc.test.ts +2 -2
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +3 -3
- package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +3 -3
- package/test/end-to-end/RecoveryFromFailedAutoCertification.test.ts +1 -1
- package/test/end-to-end/WebsocketConnectionRequest.test.ts +1 -1
- package/test/end-to-end/memory-leak.test.ts +9 -12
- package/test/integration/ConnectionLocking.test.ts +2 -2
- package/test/integration/ConnectionManager.test.ts +14 -14
- package/test/integration/DhtJoinPeerDiscovery.test.ts +3 -3
- package/test/integration/DhtNodeExternalAPI.test.ts +10 -7
- package/test/integration/DhtNodeRpcRemote.test.ts +4 -4
- package/test/integration/DhtRpc.test.ts +6 -6
- package/test/integration/Find.test.ts +3 -3
- package/test/integration/Layer1-scale.test.ts +3 -3
- package/test/integration/Mock-Layer1-Layer0.test.ts +16 -16
- package/test/integration/MultipleEntryPointJoining.test.ts +7 -7
- package/test/integration/{MigrateData.test.ts → ReplicateData.test.ts} +15 -10
- package/test/integration/RouteMessage.test.ts +2 -2
- package/test/integration/RouterRpcRemote.test.ts +2 -2
- package/test/integration/RpcErrors.test.ts +2 -2
- package/test/integration/ScaleDownDht.test.ts +4 -2
- package/test/integration/SimultaneousConnections.test.ts +89 -57
- package/test/integration/Store.test.ts +33 -13
- package/test/integration/StoreAndDelete.test.ts +19 -17
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +21 -21
- package/test/integration/StoreRpcRemote.test.ts +3 -3
- package/test/integration/WebrtcConnectionManagement.test.ts +2 -2
- package/test/integration/WebrtcConnectorRpc.test.ts +1 -1
- package/test/integration/WebsocketConnectionManagement.test.ts +41 -3
- package/test/integration/WebsocketConnectorRpc.test.ts +5 -7
- package/test/unit/ConnectivityHelpers.test.ts +4 -4
- package/test/unit/Finder.test.ts +69 -23
- package/test/unit/LocalDataStore.test.ts +60 -43
- package/test/unit/RandomContactList.test.ts +2 -2
- package/test/unit/Router.test.ts +19 -11
- package/test/unit/RoutingSession.test.ts +76 -0
- package/test/unit/SortedContactList.test.ts +17 -12
- package/test/unit/WebsocketConnector.test.ts +1 -1
- package/test/unit/connectivityRequestHandler.test.ts +71 -0
- package/test/utils/mock/Router.ts +1 -1
- package/test/utils/utils.ts +24 -22
- package/dist/src/connection/ConnectivityChecker.d.ts +0 -17
- package/dist/src/connection/ConnectivityChecker.js +0 -208
- package/dist/src/connection/ConnectivityChecker.js.map +0 -1
- package/dist/src/dht/contact/Remote.js.map +0 -1
- package/dist/src/helpers/kademliaId.d.ts +0 -1
- package/dist/src/helpers/kademliaId.js.map +0 -1
- package/src/connection/ConnectivityChecker.ts +0 -199
|
@@ -16,7 +16,7 @@ import { WebsocketConnectorRpcClient } from '../../proto/packages/dht/protos/Dht
|
|
|
16
16
|
import { Logger, binaryToHex, wait } from '@streamr/utils'
|
|
17
17
|
import { ManagedConnection } from '../ManagedConnection'
|
|
18
18
|
import { WebsocketServer } from './WebsocketServer'
|
|
19
|
-
import {
|
|
19
|
+
import { sendConnectivityRequest } from '../connectivityChecker'
|
|
20
20
|
import { NatType, PortRange, TlsCertificate } from '../ConnectionManager'
|
|
21
21
|
import { PeerIDKey } from '../../helpers/PeerID'
|
|
22
22
|
import { ServerWebsocket } from './ServerWebsocket'
|
|
@@ -29,6 +29,9 @@ import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
|
29
29
|
import { expectedConnectionType } from '../../helpers/Connectivity'
|
|
30
30
|
import { WebsocketServerStartError } from '../../helpers/errors'
|
|
31
31
|
import { AutoCertifierClientFacade } from './AutoCertifierClientFacade'
|
|
32
|
+
import { attachConnectivityRequestHandler } from '../connectivityRequestHandler'
|
|
33
|
+
import * as Err from '../../helpers/errors'
|
|
34
|
+
|
|
32
35
|
const logger = new Logger(module)
|
|
33
36
|
|
|
34
37
|
export type Action = 'connectivityRequest' | 'connectivityProbe'
|
|
@@ -41,7 +44,6 @@ const ENTRY_POINT_CONNECTION_ATTEMPTS = 5
|
|
|
41
44
|
|
|
42
45
|
export interface WebsocketConnectorConfig {
|
|
43
46
|
transport: ITransport
|
|
44
|
-
canConnect: (peerDescriptor: PeerDescriptor) => boolean
|
|
45
47
|
onNewConnection: (connection: ManagedConnection) => boolean
|
|
46
48
|
portRange?: PortRange
|
|
47
49
|
maxMessageSize?: number
|
|
@@ -59,7 +61,6 @@ export class WebsocketConnector {
|
|
|
59
61
|
private static readonly WEBSOCKET_CONNECTOR_SERVICE_ID = 'system/websocket-connector'
|
|
60
62
|
private readonly rpcCommunicator: ListeningRpcCommunicator
|
|
61
63
|
private readonly websocketServer?: WebsocketServer
|
|
62
|
-
private connectivityChecker?: ConnectivityChecker
|
|
63
64
|
private readonly ongoingConnectRequests: Map<PeerIDKey, ManagedConnection> = new Map()
|
|
64
65
|
private onNewConnection: (connection: ManagedConnection) => boolean
|
|
65
66
|
private host?: string
|
|
@@ -77,7 +78,7 @@ export class WebsocketConnector {
|
|
|
77
78
|
|
|
78
79
|
constructor(config: WebsocketConnectorConfig) {
|
|
79
80
|
this.websocketServer = config.portRange ? new WebsocketServer({
|
|
80
|
-
portRange: config.portRange
|
|
81
|
+
portRange: config.portRange,
|
|
81
82
|
tlsCertificate: config.tlsCertificate,
|
|
82
83
|
maxMessageSize: config.maxMessageSize,
|
|
83
84
|
enableTls: config.serverEnableTls
|
|
@@ -98,8 +99,18 @@ export class WebsocketConnector {
|
|
|
98
99
|
|
|
99
100
|
private registerLocalRpcMethods(config: WebsocketConnectorConfig) {
|
|
100
101
|
const rpcLocal = new WebsocketConnectorRpcLocal({
|
|
101
|
-
canConnect: (peerDescriptor: PeerDescriptor) => config.canConnect(peerDescriptor),
|
|
102
102
|
connect: (targetPeerDescriptor: PeerDescriptor) => this.connect(targetPeerDescriptor),
|
|
103
|
+
hasConnection: (targetPeerDescriptor: PeerDescriptor): boolean => {
|
|
104
|
+
const peerKey = keyFromPeerDescriptor(targetPeerDescriptor)
|
|
105
|
+
if (this.connectingConnections.has(peerKey)
|
|
106
|
+
|| this.connectingConnections.has(peerKey)
|
|
107
|
+
|| this.ongoingConnectRequests.has(peerKey)
|
|
108
|
+
) {
|
|
109
|
+
return true
|
|
110
|
+
} else {
|
|
111
|
+
return false
|
|
112
|
+
}
|
|
113
|
+
},
|
|
103
114
|
onNewConnection: (connection: ManagedConnection) => config.onNewConnection(connection),
|
|
104
115
|
abortSignal: this.abortController.signal
|
|
105
116
|
})
|
|
@@ -107,7 +118,7 @@ export class WebsocketConnector {
|
|
|
107
118
|
WebsocketConnectionRequest,
|
|
108
119
|
WebsocketConnectionResponse,
|
|
109
120
|
'requestConnection',
|
|
110
|
-
async (req: WebsocketConnectionRequest, context: ServerCallContext) => {
|
|
121
|
+
async (req: WebsocketConnectionRequest, context: ServerCallContext): Promise<WebsocketConnectionResponse> => {
|
|
111
122
|
if (!this.abortController.signal.aborted) {
|
|
112
123
|
return rpcLocal.requestConnection(req, context)
|
|
113
124
|
} else {
|
|
@@ -134,13 +145,12 @@ export class WebsocketConnector {
|
|
|
134
145
|
updateCertificate: (certificate: string, privateKey: string) => this.websocketServer!.updateCertificate(certificate, privateKey)
|
|
135
146
|
})
|
|
136
147
|
logger.trace(`AutoCertifying subdomain...`)
|
|
137
|
-
await this.autoCertifierClient
|
|
148
|
+
await this.autoCertifierClient.start()
|
|
138
149
|
}
|
|
139
150
|
|
|
140
151
|
private setHost(hostName: string): void {
|
|
141
152
|
logger.trace(`Setting host name to ${hostName}`)
|
|
142
153
|
this.host = hostName
|
|
143
|
-
this.connectivityChecker!.setHost(hostName)
|
|
144
154
|
}
|
|
145
155
|
|
|
146
156
|
public async start(): Promise<void> {
|
|
@@ -151,7 +161,7 @@ export class WebsocketConnector {
|
|
|
151
161
|
const action = query?.action as (Action | undefined)
|
|
152
162
|
logger.trace('WebSocket client connected', { action, remoteAddress: serverSocket.getRemoteAddress() })
|
|
153
163
|
if (action === 'connectivityRequest') {
|
|
154
|
-
|
|
164
|
+
attachConnectivityRequestHandler(serverSocket)
|
|
155
165
|
} else if (action === 'connectivityProbe') {
|
|
156
166
|
// no-op
|
|
157
167
|
} else {
|
|
@@ -160,7 +170,6 @@ export class WebsocketConnector {
|
|
|
160
170
|
})
|
|
161
171
|
const port = await this.websocketServer.start()
|
|
162
172
|
this.selectedPort = port
|
|
163
|
-
this.connectivityChecker = new ConnectivityChecker(this.selectedPort, this.serverEnableTls, this.host)
|
|
164
173
|
}
|
|
165
174
|
}
|
|
166
175
|
|
|
@@ -189,12 +198,22 @@ export class WebsocketConnector {
|
|
|
189
198
|
return preconfiguredConnectivityResponse
|
|
190
199
|
} else {
|
|
191
200
|
// Do real connectivity checking
|
|
192
|
-
|
|
201
|
+
const connectivityRequest = {
|
|
202
|
+
port: this.selectedPort!,
|
|
203
|
+
host: this.host,
|
|
204
|
+
tls: this.serverEnableTls,
|
|
205
|
+
selfSigned
|
|
206
|
+
}
|
|
207
|
+
if (!this.abortController.signal.aborted) {
|
|
208
|
+
return await sendConnectivityRequest(connectivityRequest, entryPoint)
|
|
209
|
+
} else {
|
|
210
|
+
throw new Err.ConnectionFailed('ConnectivityChecker is destroyed')
|
|
211
|
+
}
|
|
193
212
|
}
|
|
194
213
|
}
|
|
195
214
|
} catch (err) {
|
|
196
215
|
if (reattempt < ENTRY_POINT_CONNECTION_ATTEMPTS) {
|
|
197
|
-
const error = `Failed to connect to entrypoint with id ${binaryToHex(entryPoint.
|
|
216
|
+
const error = `Failed to connect to entrypoint with id ${binaryToHex(entryPoint.nodeId)} `
|
|
198
217
|
+ `and URL ${connectivityMethodToWebsocketUrl(entryPoint.websocket!)}`
|
|
199
218
|
logger.error(error, { error: err })
|
|
200
219
|
await wait(2000)
|
|
@@ -257,7 +276,14 @@ export class WebsocketConnector {
|
|
|
257
276
|
targetPeerDescriptor,
|
|
258
277
|
toProtoRpcClient(new WebsocketConnectorRpcClient(this.rpcCommunicator.getRpcClientTransport()))
|
|
259
278
|
)
|
|
260
|
-
remoteConnector.requestConnection(
|
|
279
|
+
remoteConnector.requestConnection().then((_response: WebsocketConnectionResponse) => {
|
|
280
|
+
logger.trace('Sent WebsocketConnectionRequest request to peer', { targetPeerDescriptor })
|
|
281
|
+
return
|
|
282
|
+
}, (err) => {
|
|
283
|
+
logger.debug('Failed to send WebsocketConnectionRequest request to peer of failed to get the response ', {
|
|
284
|
+
error: err, targetPeerDescriptor
|
|
285
|
+
})
|
|
286
|
+
})
|
|
261
287
|
})
|
|
262
288
|
const managedConnection = new ManagedConnection(
|
|
263
289
|
this.localPeerDescriptor!,
|
|
@@ -265,7 +291,7 @@ export class WebsocketConnector {
|
|
|
265
291
|
undefined,
|
|
266
292
|
undefined,
|
|
267
293
|
targetPeerDescriptor
|
|
268
|
-
)
|
|
294
|
+
)
|
|
269
295
|
managedConnection.on('disconnected', () => this.ongoingConnectRequests.delete(keyFromPeerDescriptor(targetPeerDescriptor)))
|
|
270
296
|
managedConnection.setRemotePeerDescriptor(targetPeerDescriptor)
|
|
271
297
|
this.ongoingConnectRequests.set(keyFromPeerDescriptor(targetPeerDescriptor), managedConnection)
|
|
@@ -273,7 +299,7 @@ export class WebsocketConnector {
|
|
|
273
299
|
}
|
|
274
300
|
|
|
275
301
|
private onServerSocketHandshakeRequest(
|
|
276
|
-
sourcePeerDescriptor: PeerDescriptor,
|
|
302
|
+
sourcePeerDescriptor: PeerDescriptor,
|
|
277
303
|
serverWebsocket: IConnection,
|
|
278
304
|
targetPeerDescriptor?: PeerDescriptor
|
|
279
305
|
) {
|
|
@@ -282,12 +308,8 @@ export class WebsocketConnector {
|
|
|
282
308
|
if (this.ongoingConnectRequests.has(peerId.toKey())) {
|
|
283
309
|
const ongoingConnectRequest = this.ongoingConnectRequests.get(peerId.toKey())!
|
|
284
310
|
ongoingConnectRequest.attachImplementation(serverWebsocket)
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
} else {
|
|
288
|
-
ongoingConnectRequest.acceptHandshake()
|
|
289
|
-
this.ongoingConnectRequests.delete(peerId.toKey())
|
|
290
|
-
}
|
|
311
|
+
ongoingConnectRequest.acceptHandshake()
|
|
312
|
+
this.ongoingConnectRequests.delete(peerId.toKey())
|
|
291
313
|
} else {
|
|
292
314
|
const managedConnection = new ManagedConnection(
|
|
293
315
|
this.localPeerDescriptor!,
|
|
@@ -322,7 +344,6 @@ export class WebsocketConnector {
|
|
|
322
344
|
|
|
323
345
|
const attempts = Array.from(this.connectingConnections.values())
|
|
324
346
|
await Promise.allSettled(attempts.map((conn) => conn.close(false)))
|
|
325
|
-
this.connectivityChecker?.destroy()
|
|
326
347
|
await this.websocketServer?.stop()
|
|
327
348
|
}
|
|
328
349
|
}
|
|
@@ -9,8 +9,8 @@ import { DhtCallContext } from '../../rpc-protocol/DhtCallContext'
|
|
|
9
9
|
import { ManagedConnection } from '../ManagedConnection'
|
|
10
10
|
|
|
11
11
|
interface WebsocketConnectorRpcLocalConfig {
|
|
12
|
-
canConnect: (peerDescriptor: PeerDescriptor) => boolean
|
|
13
12
|
connect: (targetPeerDescriptor: PeerDescriptor) => ManagedConnection
|
|
13
|
+
hasConnection: (targetPeerDescriptor: PeerDescriptor) => boolean
|
|
14
14
|
onNewConnection: (connection: ManagedConnection) => boolean
|
|
15
15
|
abortSignal: AbortSignal
|
|
16
16
|
}
|
|
@@ -25,17 +25,15 @@ export class WebsocketConnectorRpcLocal implements IWebsocketConnectorRpc {
|
|
|
25
25
|
|
|
26
26
|
public async requestConnection(_request: WebsocketConnectionRequest, context: ServerCallContext): Promise<WebsocketConnectionResponse> {
|
|
27
27
|
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
setImmediate(() => {
|
|
29
|
+
if (this.config.abortSignal.aborted) {
|
|
30
|
+
return
|
|
31
|
+
}
|
|
32
|
+
if (!this.config.hasConnection(senderPeerDescriptor)) {
|
|
33
33
|
const connection = this.config.connect(senderPeerDescriptor)
|
|
34
34
|
this.config.onNewConnection(connection)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return { accepted: false }
|
|
39
|
-
}
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
return { accepted: true }
|
|
40
38
|
}
|
|
41
39
|
}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import {
|
|
2
2
|
PeerDescriptor,
|
|
3
|
-
WebsocketConnectionRequest
|
|
3
|
+
WebsocketConnectionRequest,
|
|
4
|
+
WebsocketConnectionResponse
|
|
4
5
|
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
5
6
|
import { IWebsocketConnectorRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
6
7
|
import { Logger } from '@streamr/utils'
|
|
7
|
-
import * as Err from '../../helpers/errors'
|
|
8
8
|
import { ProtoRpcClient } from '@streamr/proto-rpc'
|
|
9
9
|
import { getNodeIdFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
10
|
-
import {
|
|
10
|
+
import { RpcRemote } from '../../dht/contact/RpcRemote'
|
|
11
11
|
|
|
12
12
|
const logger = new Logger(module)
|
|
13
13
|
|
|
14
|
-
export class WebsocketConnectorRpcRemote extends
|
|
14
|
+
export class WebsocketConnectorRpcRemote extends RpcRemote<IWebsocketConnectorRpcClient> {
|
|
15
15
|
|
|
16
16
|
constructor(
|
|
17
17
|
localPeerDescriptor: PeerDescriptor,
|
|
@@ -21,19 +21,10 @@ export class WebsocketConnectorRpcRemote extends Remote<IWebsocketConnectorRpcCl
|
|
|
21
21
|
super(localPeerDescriptor, remotePeerDescriptor, 'DUMMY', client)
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
async requestConnection(
|
|
24
|
+
async requestConnection(): Promise<WebsocketConnectionResponse> {
|
|
25
25
|
logger.trace(`Requesting WebSocket connection from ${getNodeIdFromPeerDescriptor(this.getLocalPeerDescriptor())}`)
|
|
26
|
-
const request: WebsocketConnectionRequest = {
|
|
27
|
-
ip,
|
|
28
|
-
port
|
|
29
|
-
}
|
|
26
|
+
const request: WebsocketConnectionRequest = {}
|
|
30
27
|
const options = this.formDhtRpcOptions()
|
|
31
|
-
|
|
32
|
-
const res = await this.getClient().requestConnection(request, options)
|
|
33
|
-
return res.accepted
|
|
34
|
-
} catch (err) {
|
|
35
|
-
logger.debug(new Err.WebsocketConnectionRequestRejected('WebsocketConnectionRequest rejected', err).stack!)
|
|
36
|
-
return false
|
|
37
|
-
}
|
|
28
|
+
return this.getClient().requestConnection(request, options)
|
|
38
29
|
}
|
|
39
30
|
}
|
|
@@ -101,11 +101,17 @@ export class WebsocketServer extends EventEmitter<ConnectionSourceEvents> {
|
|
|
101
101
|
return
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
104
|
+
let connection
|
|
105
|
+
try {
|
|
106
|
+
connection = request.accept(undefined, request.origin)
|
|
107
|
+
logger.trace('Connection accepted.', { remoteAddress: request.remoteAddress })
|
|
108
|
+
} catch (err) {
|
|
109
|
+
logger.debug('Accepting websocket connection failed', { remoteAddress: request.remoteAddress, err })
|
|
110
|
+
}
|
|
107
111
|
|
|
108
|
-
|
|
112
|
+
if (connection) {
|
|
113
|
+
this.emit('connected', new ServerWebsocket(connection, request.resourceURL))
|
|
114
|
+
}
|
|
109
115
|
})
|
|
110
116
|
this.httpServer.once('error', (err: Error) => {
|
|
111
117
|
reject(new WebsocketServerStartError('Starting Websocket server failed', err))
|
|
@@ -137,9 +143,18 @@ export class WebsocketServer extends EventEmitter<ConnectionSourceEvents> {
|
|
|
137
143
|
this.removeAllListeners()
|
|
138
144
|
return new Promise((resolve, _reject) => {
|
|
139
145
|
this.wsServer?.shutDown()
|
|
140
|
-
this.httpServer?.close(
|
|
146
|
+
this.httpServer?.once('close', () => {
|
|
147
|
+
// removeAllListeners is maybe not needed?
|
|
148
|
+
this.httpServer?.removeAllListeners()
|
|
141
149
|
resolve()
|
|
142
150
|
})
|
|
151
|
+
this.httpServer?.close()
|
|
152
|
+
// the close method "Stops the server from accepting new connections and closes all
|
|
153
|
+
// connections connected to this server which are not sending a request or waiting for a
|
|
154
|
+
// response." (https://nodejs.org/api/http.html#serverclosecallback)
|
|
155
|
+
// i.e. we need to call closeAllConnections() to close the rest of the connections
|
|
156
|
+
// (in practice this closes the active websocket connections)
|
|
157
|
+
this.httpServer?.closeAllConnections()
|
|
143
158
|
})
|
|
144
159
|
}
|
|
145
160
|
|