@streamr/dht 100.0.0-pretestnet.4 → 100.0.0-testnet-one.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/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 +2 -2
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.js +4 -1
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- 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 +121 -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.js +1 -0
- package/dist/src/connection/websocket/ClientWebsocket.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnector.d.ts +0 -1
- package/dist/src/connection/websocket/WebsocketConnector.js +41 -7
- package/dist/src/connection/websocket/WebsocketConnector.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +2 -2
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js +2 -2
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +1 -1
- package/dist/src/dht/DhtNode.d.ts +9 -19
- package/dist/src/dht/DhtNode.js +76 -216
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.js +3 -3
- 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 +48 -0
- package/dist/src/dht/PeerManager.js +208 -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/discovery/DiscoverySession.d.ts +2 -9
- package/dist/src/dht/discovery/DiscoverySession.js +12 -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 +13 -15
- 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 +13 -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 +50 -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 +2 -6
- package/dist/src/dht/routing/Router.js +21 -18
- 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 +16 -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 +91 -99
- 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.js +2 -2
- 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 +3 -3
- 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 +2 -2
- package/src/connection/ConnectionManager.ts +4 -1
- package/src/connection/ConnectorFacade.ts +5 -4
- package/src/connection/ManagedConnection.ts +1 -0
- package/src/connection/connectivityChecker.ts +101 -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 +1 -0
- package/src/connection/websocket/WebsocketConnector.ts +22 -12
- package/src/connection/websocket/WebsocketConnectorRpcRemote.ts +2 -2
- package/src/dht/DhtNode.ts +102 -269
- package/src/dht/DhtNodeRpcLocal.ts +3 -3
- 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 +256 -0
- package/src/dht/contact/ContactList.ts +3 -2
- package/src/dht/contact/{Remote.ts → RpcRemote.ts} +7 -6
- package/src/dht/discovery/DiscoverySession.ts +15 -40
- package/src/dht/discovery/PeerDiscovery.ts +18 -23
- package/src/dht/find/FindRpcLocal.ts +2 -2
- package/src/dht/find/FindSession.ts +20 -20
- package/src/dht/find/FindSessionRpcLocal.ts +1 -1
- package/src/dht/find/FindSessionRpcRemote.ts +2 -2
- package/src/dht/find/Finder.ts +79 -58
- package/src/dht/routing/FindRpcRemote.ts +7 -5
- package/src/dht/routing/Router.ts +22 -21
- package/src/dht/routing/RouterRpcLocal.ts +5 -5
- package/src/dht/routing/RouterRpcRemote.ts +13 -10
- package/src/dht/routing/RoutingSession.ts +15 -10
- package/src/dht/routing/getPreviousPeer.ts +1 -1
- package/src/dht/store/LocalDataStore.ts +18 -17
- package/src/dht/store/StoreRpcLocal.ts +101 -108
- package/src/dht/store/StoreRpcRemote.ts +7 -23
- package/src/exports.ts +1 -1
- package/src/helpers/PeerID.ts +2 -2
- package/src/helpers/UUID.ts +1 -1
- package/src/helpers/{kademliaId.ts → nodeId.ts} +1 -1
- package/src/helpers/peerIdFromPeerDescriptor.ts +3 -3
- 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 +4 -4
- package/test/benchmark/KademliaCorrectness.test.ts +2 -2
- package/test/benchmark/kademlia-simulation/Contact.ts +1 -1
- package/test/benchmark/kademlia-simulation/KademliaSimulation.ts +1 -1
- package/test/end-to-end/Layer0-Layer1.test.ts +1 -1
- package/test/end-to-end/Layer0MixedConnectionTypes.test.ts +1 -1
- package/test/end-to-end/Layer0Webrtc-Layer1.test.ts +2 -2
- package/test/end-to-end/Layer0Webrtc.test.ts +2 -2
- package/test/end-to-end/Layer1-Scale-WebSocket.test.ts +1 -1
- package/test/end-to-end/Layer1-Scale-Webrtc.test.ts +1 -1
- package/test/end-to-end/WebsocketConnectionRequest.test.ts +1 -1
- package/test/end-to-end/memory-leak.test.ts +8 -12
- package/test/integration/ConnectionLocking.test.ts +2 -2
- package/test/integration/ConnectionManager.test.ts +14 -14
- package/test/integration/DhtJoinPeerDiscovery.test.ts +1 -1
- 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 +2 -2
- package/test/integration/Mock-Layer1-Layer0.test.ts +1 -1
- package/test/integration/{MigrateData.test.ts → ReplicateData.test.ts} +9 -9
- 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 +8 -8
- package/test/integration/Store.test.ts +33 -13
- package/test/integration/StoreAndDelete.test.ts +19 -17
- package/test/integration/StoreOnDhtWithTwoNodes.test.ts +20 -20
- 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 +2 -2
- package/test/integration/WebsocketConnectorRpc.test.ts +2 -2
- 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 +2 -2
- 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
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ConnectivityRequest, ConnectivityResponse,
|
|
3
|
-
Message, MessageType, PeerDescriptor
|
|
4
|
-
} from '../proto/packages/dht/protos/DhtRpc'
|
|
5
|
-
import { ConnectionEvents, IConnection } from './IConnection'
|
|
6
|
-
import { Logger, runAndRaceEvents3, RunAndRaceEventsReturnType } from '@streamr/utils'
|
|
7
|
-
import * as Err from '../helpers/errors'
|
|
8
|
-
import { ClientWebsocket } from './websocket/ClientWebsocket'
|
|
9
|
-
import { v4 } from 'uuid'
|
|
10
|
-
import { NatType } from './ConnectionManager'
|
|
11
|
-
import { ServerWebsocket } from './websocket/ServerWebsocket'
|
|
12
|
-
import { connectivityMethodToWebsocketUrl } from './websocket/WebsocketConnector'
|
|
13
|
-
|
|
14
|
-
const logger = new Logger(module)
|
|
15
|
-
|
|
16
|
-
// Class for handling both client and server side of the connectivity
|
|
17
|
-
// checks. This is attached to all ServerWebsockets to listen to
|
|
18
|
-
// ConnectivityRequest messages.
|
|
19
|
-
|
|
20
|
-
export class ConnectivityChecker {
|
|
21
|
-
|
|
22
|
-
private static readonly CONNECTIVITY_CHECKER_SERVICE_ID = 'system/connectivity-checker'
|
|
23
|
-
private static readonly CONNECTIVITY_CHECKER_TIMEOUT = 5000
|
|
24
|
-
private destroyed = false
|
|
25
|
-
private readonly websocketPort: number
|
|
26
|
-
private readonly tls: boolean
|
|
27
|
-
private host?: string
|
|
28
|
-
|
|
29
|
-
constructor(websocketPort: number, tls: boolean, host?: string) {
|
|
30
|
-
this.websocketPort = websocketPort
|
|
31
|
-
this.tls = tls
|
|
32
|
-
this.host = host
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public async sendConnectivityRequest(entryPoint: PeerDescriptor, selfSigned: boolean): Promise<ConnectivityResponse> {
|
|
36
|
-
if (this.destroyed) {
|
|
37
|
-
throw new Err.ConnectionFailed('ConnectivityChecker is destroyed')
|
|
38
|
-
}
|
|
39
|
-
let outgoingConnection: IConnection
|
|
40
|
-
const wsServerInfo = {
|
|
41
|
-
host: entryPoint.websocket!.host,
|
|
42
|
-
port: entryPoint.websocket!.port,
|
|
43
|
-
tls: entryPoint.websocket!.tls,
|
|
44
|
-
}
|
|
45
|
-
const url = connectivityMethodToWebsocketUrl(wsServerInfo, 'connectivityRequest')
|
|
46
|
-
try {
|
|
47
|
-
outgoingConnection = await this.connectAsync({
|
|
48
|
-
url,
|
|
49
|
-
selfSigned
|
|
50
|
-
})
|
|
51
|
-
} catch (e) {
|
|
52
|
-
throw new Err.ConnectionFailed(`Failed to connect to the entrypoint ${url}`, e)
|
|
53
|
-
}
|
|
54
|
-
// send connectivity request
|
|
55
|
-
const connectivityRequestMessage: ConnectivityRequest = { port: this.websocketPort, host: this.host, tls: this.tls, selfSigned }
|
|
56
|
-
const msg: Message = {
|
|
57
|
-
serviceId: ConnectivityChecker.CONNECTIVITY_CHECKER_SERVICE_ID,
|
|
58
|
-
messageType: MessageType.CONNECTIVITY_REQUEST, messageId: v4(),
|
|
59
|
-
body: {
|
|
60
|
-
oneofKind: 'connectivityRequest',
|
|
61
|
-
connectivityRequest: connectivityRequestMessage
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
const responseAwaiter = () => {
|
|
65
|
-
return new Promise((resolve: (res: ConnectivityResponse) => void, reject) => {
|
|
66
|
-
const timeoutId = setTimeout(() => {
|
|
67
|
-
outgoingConnection.close(false)
|
|
68
|
-
reject(new Err.ConnectivityResponseTimeout('timeout'))
|
|
69
|
-
}, ConnectivityChecker.CONNECTIVITY_CHECKER_TIMEOUT)
|
|
70
|
-
const listener = (bytes: Uint8Array) => {
|
|
71
|
-
outgoingConnection.close(false)
|
|
72
|
-
try {
|
|
73
|
-
const message: Message = Message.fromBinary(bytes)
|
|
74
|
-
if (message.body.oneofKind === 'connectivityResponse') {
|
|
75
|
-
logger.trace('ConnectivityResponse received: ' + JSON.stringify(Message.toJson(message)))
|
|
76
|
-
const connectivityResponseMessage = message.body.connectivityResponse
|
|
77
|
-
outgoingConnection!.off('data', listener)
|
|
78
|
-
clearTimeout(timeoutId)
|
|
79
|
-
resolve(connectivityResponseMessage)
|
|
80
|
-
} else {
|
|
81
|
-
return
|
|
82
|
-
}
|
|
83
|
-
} catch (err) {
|
|
84
|
-
logger.trace(`Could not parse message: ${err}`)
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
outgoingConnection!.on('data', listener)
|
|
88
|
-
})
|
|
89
|
-
}
|
|
90
|
-
try {
|
|
91
|
-
const retPromise = responseAwaiter()
|
|
92
|
-
outgoingConnection.send(Message.toBinary(msg))
|
|
93
|
-
logger.trace('ConnectivityRequest sent: ' + JSON.stringify(Message.toJson(msg)))
|
|
94
|
-
return await retPromise
|
|
95
|
-
} catch (e) {
|
|
96
|
-
logger.error('error getting connectivityresponse')
|
|
97
|
-
throw e
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
public setHost(hostName: string): void {
|
|
102
|
-
this.host = hostName
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
public listenToIncomingConnectivityRequests(connectionToListenTo: ServerWebsocket): void {
|
|
106
|
-
connectionToListenTo.on('data', (data: Uint8Array) => {
|
|
107
|
-
logger.trace('server received data')
|
|
108
|
-
try {
|
|
109
|
-
const message = Message.fromBinary(data)
|
|
110
|
-
if (message.body.oneofKind === 'connectivityRequest') {
|
|
111
|
-
logger.trace('ConnectivityRequest received: ' + JSON.stringify(Message.toJson(message)))
|
|
112
|
-
this.handleIncomingConnectivityRequest(connectionToListenTo, message.body.connectivityRequest).then(() => {
|
|
113
|
-
logger.trace('handleIncomingConnectivityRequest ok')
|
|
114
|
-
return
|
|
115
|
-
}).catch((e) => {
|
|
116
|
-
logger.error('handleIncomingConnectivityRequest' + e)
|
|
117
|
-
})
|
|
118
|
-
}
|
|
119
|
-
} catch (err) {
|
|
120
|
-
logger.trace(`Could not parse message: ${err}`)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
})
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
private async handleIncomingConnectivityRequest(
|
|
127
|
-
connection: ServerWebsocket,
|
|
128
|
-
connectivityRequest: ConnectivityRequest
|
|
129
|
-
): Promise<void> {
|
|
130
|
-
if (this.destroyed) {
|
|
131
|
-
return
|
|
132
|
-
}
|
|
133
|
-
let outgoingConnection: IConnection | undefined
|
|
134
|
-
let connectivityResponseMessage: ConnectivityResponse | undefined
|
|
135
|
-
const host = connectivityRequest.host ?? connection.getRemoteAddress()
|
|
136
|
-
try {
|
|
137
|
-
const wsServerInfo = {
|
|
138
|
-
host,
|
|
139
|
-
port: connectivityRequest.port,
|
|
140
|
-
tls: connectivityRequest.tls
|
|
141
|
-
}
|
|
142
|
-
const url = connectivityMethodToWebsocketUrl(wsServerInfo, 'connectivityProbe')
|
|
143
|
-
logger.trace(`Attempting Connectivity Check to ${url}`)
|
|
144
|
-
outgoingConnection = await this.connectAsync({
|
|
145
|
-
url,
|
|
146
|
-
selfSigned: connectivityRequest.selfSigned
|
|
147
|
-
})
|
|
148
|
-
} catch (err) {
|
|
149
|
-
logger.debug('error', { err })
|
|
150
|
-
connectivityResponseMessage = {
|
|
151
|
-
host,
|
|
152
|
-
natType: NatType.UNKNOWN
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
if (outgoingConnection) {
|
|
156
|
-
outgoingConnection.close(false)
|
|
157
|
-
logger.trace('Connectivity test produced positive result, communicating reply to the requester ' + host + ':' + connectivityRequest.port)
|
|
158
|
-
connectivityResponseMessage = {
|
|
159
|
-
host,
|
|
160
|
-
natType: NatType.OPEN_INTERNET,
|
|
161
|
-
websocket: { host, port: connectivityRequest.port, tls: connectivityRequest.tls }
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
const msg: Message = {
|
|
165
|
-
serviceId: ConnectivityChecker.CONNECTIVITY_CHECKER_SERVICE_ID,
|
|
166
|
-
messageType: MessageType.CONNECTIVITY_RESPONSE, messageId: v4(),
|
|
167
|
-
body: {
|
|
168
|
-
oneofKind: 'connectivityResponse',
|
|
169
|
-
connectivityResponse: connectivityResponseMessage!
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
connection.send(Message.toBinary(msg))
|
|
173
|
-
logger.trace('ConnectivityResponse sent: ' + JSON.stringify(Message.toJson(msg)))
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// eslint-disable-next-line class-methods-use-this
|
|
177
|
-
private async connectAsync({ url, selfSigned, timeoutMs = 1000, }:
|
|
178
|
-
{ url: string, selfSigned: boolean, timeoutMs?: number }
|
|
179
|
-
): Promise<IConnection> {
|
|
180
|
-
const socket = new ClientWebsocket()
|
|
181
|
-
let result: RunAndRaceEventsReturnType<ConnectionEvents>
|
|
182
|
-
try {
|
|
183
|
-
result = await runAndRaceEvents3<ConnectionEvents>([
|
|
184
|
-
() => { socket.connect(url, selfSigned) }],
|
|
185
|
-
socket, ['connected', 'error'],
|
|
186
|
-
timeoutMs)
|
|
187
|
-
} catch (e) {
|
|
188
|
-
throw (new Err.ConnectionFailed('WebSocket connection timed out'))
|
|
189
|
-
}
|
|
190
|
-
if (result.winnerName === 'error') {
|
|
191
|
-
throw (new Err.ConnectionFailed('Could not open WebSocket connection'))
|
|
192
|
-
}
|
|
193
|
-
return socket
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
public destroy(): void {
|
|
197
|
-
this.destroyed = true
|
|
198
|
-
}
|
|
199
|
-
}
|