@streamr/dht 100.2.5-beta.1 → 101.0.0-beta.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/package.json +7 -7
- package/dist/src/connection/ConnectionLockRpcLocal.d.ts +3 -3
- package/dist/src/connection/ConnectionLockRpcLocal.js +8 -8
- package/dist/src/connection/ConnectionLockRpcLocal.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.js +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +4 -6
- package/dist/src/connection/ConnectionManager.js +128 -103
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +15 -14
- package/dist/src/connection/ConnectorFacade.js +70 -52
- package/dist/src/connection/ConnectorFacade.js.map +1 -1
- package/dist/src/connection/Handshaker.d.ts +9 -2
- package/dist/src/connection/Handshaker.js +117 -27
- package/dist/src/connection/Handshaker.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +13 -38
- package/dist/src/connection/ManagedConnection.js +31 -252
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/OutputBuffer.d.ts +9 -0
- package/dist/src/connection/OutputBuffer.js +26 -0
- package/dist/src/connection/OutputBuffer.js.map +1 -0
- package/dist/src/connection/PendingConnection.d.ts +19 -0
- package/dist/src/connection/PendingConnection.js +59 -0
- package/dist/src/connection/PendingConnection.js.map +1 -0
- package/dist/src/connection/connectivityChecker.js +3 -3
- package/dist/src/connection/connectivityChecker.js.map +1 -1
- package/dist/src/connection/connectivityRequestHandler.js +2 -2
- package/dist/src/connection/connectivityRequestHandler.js.map +1 -1
- package/dist/src/connection/simulator/Simulator.d.ts +1 -3
- package/dist/src/connection/simulator/Simulator.js +1 -4
- package/dist/src/connection/simulator/Simulator.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnection.js +1 -2
- package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnector.d.ts +3 -3
- package/dist/src/connection/simulator/SimulatorConnector.js +28 -21
- package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.d.ts +1 -6
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js +3 -20
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.d.ts +11 -6
- package/dist/src/connection/webrtc/WebrtcConnector.js +57 -42
- package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.d.ts +8 -10
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +21 -44
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/AbstractWebsocketClientConnection.js +8 -2
- package/dist/src/connection/websocket/AbstractWebsocketClientConnection.js.map +1 -1
- package/dist/src/connection/websocket/AutoCertifierClientFacade.d.ts +3 -3
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js +8 -8
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js.map +1 -1
- package/dist/src/connection/websocket/NodeWebsocketClientConnection.js +1 -1
- package/dist/src/connection/websocket/NodeWebsocketClientConnection.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketClientConnector.d.ts +26 -0
- package/dist/src/connection/websocket/WebsocketClientConnector.js +86 -0
- package/dist/src/connection/websocket/WebsocketClientConnector.js.map +1 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.d.ts +19 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.js +23 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.js.map +1 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcRemote.d.ts +5 -0
- package/dist/src/connection/websocket/{WebsocketConnectorRpcRemote.js → WebsocketClientConnectorRpcRemote.js} +4 -4
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcRemote.js.map +1 -0
- package/dist/src/connection/websocket/WebsocketServer.d.ts +8 -5
- package/dist/src/connection/websocket/WebsocketServer.js +11 -11
- package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
- package/dist/src/connection/websocket/{WebsocketConnector.d.ts → WebsocketServerConnector.d.ts} +16 -21
- package/dist/src/connection/websocket/{WebsocketConnector.js → WebsocketServerConnector.js} +112 -160
- package/dist/src/connection/websocket/WebsocketServerConnector.js.map +1 -0
- package/dist/src/dht/DhtNode.d.ts +4 -4
- package/dist/src/dht/DhtNode.js +85 -84
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +3 -3
- package/dist/src/dht/DhtNodeRpcLocal.js +9 -9
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +3 -3
- package/dist/src/dht/ExternalApiRpcLocal.js +5 -5
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.js +2 -2
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/PeerManager.d.ts +4 -4
- package/dist/src/dht/PeerManager.js +22 -22
- package/dist/src/dht/PeerManager.js.map +1 -1
- package/dist/src/dht/contact/SortedContactList.d.ts +3 -3
- package/dist/src/dht/contact/SortedContactList.js +9 -9
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +3 -3
- package/dist/src/dht/discovery/DiscoverySession.js +21 -21
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +3 -3
- package/dist/src/dht/discovery/PeerDiscovery.js +46 -44
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/discovery/RingDiscoverySession.d.ts +3 -3
- package/dist/src/dht/discovery/RingDiscoverySession.js +19 -19
- package/dist/src/dht/discovery/RingDiscoverySession.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +3 -3
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +33 -33
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.d.ts +3 -3
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js +8 -8
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +4 -4
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +24 -24
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.d.ts +4 -4
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js +5 -5
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/Router.d.ts +3 -3
- package/dist/src/dht/routing/Router.js +20 -20
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.d.ts +3 -3
- package/dist/src/dht/routing/RouterRpcLocal.js +16 -16
- package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +3 -3
- package/dist/src/dht/routing/RoutingSession.js +24 -24
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/StoreManager.d.ts +3 -3
- package/dist/src/dht/store/StoreManager.js +25 -25
- package/dist/src/dht/store/StoreManager.js.map +1 -1
- package/dist/src/dht/store/StoreRpcLocal.d.ts +3 -3
- package/dist/src/dht/store/StoreRpcLocal.js +12 -12
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/exports.d.ts +3 -0
- package/dist/src/exports.js +5 -1
- package/dist/src/exports.js.map +1 -1
- package/dist/src/proto/google/protobuf/any.d.ts +5 -8
- package/dist/src/proto/google/protobuf/any.js.map +1 -1
- package/dist/src/proto/google/protobuf/empty.d.ts +1 -0
- package/dist/src/proto/google/protobuf/empty.js.map +1 -1
- package/dist/src/proto/google/protobuf/timestamp.d.ts +1 -10
- package/dist/src/proto/google/protobuf/timestamp.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +4 -4
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +8 -8
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +3 -3
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +4 -4
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +2 -2
- package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js +1 -1
- package/dist/src/transport/ListeningRpcCommunicator.d.ts +2 -2
- package/dist/src/transport/ListeningRpcCommunicator.js +2 -2
- package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -1
- package/dist/src/transport/RoutingRpcCommunicator.d.ts +2 -2
- package/dist/src/transport/RoutingRpcCommunicator.js +2 -2
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
- package/package.json +7 -7
- package/protos/DhtRpc.proto +1 -1
- package/src/connection/ConnectionLockRpcLocal.ts +9 -9
- package/src/connection/ConnectionLockRpcRemote.ts +1 -1
- package/src/connection/ConnectionManager.ts +153 -111
- package/src/connection/ConnectorFacade.ts +84 -61
- package/src/connection/Handshaker.ts +131 -27
- package/src/connection/ManagedConnection.ts +41 -304
- package/src/connection/OutputBuffer.ts +28 -0
- package/src/connection/PendingConnection.ts +68 -0
- package/src/connection/connectivityChecker.ts +2 -2
- package/src/connection/connectivityRequestHandler.ts +1 -1
- package/src/connection/simulator/Simulator.ts +1 -5
- package/src/connection/simulator/SimulatorConnection.ts +1 -2
- package/src/connection/simulator/SimulatorConnector.ts +34 -33
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -6
- package/src/connection/webrtc/NodeWebrtcConnection.ts +3 -24
- package/src/connection/webrtc/WebrtcConnector.ts +73 -62
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +26 -56
- package/src/connection/websocket/AbstractWebsocketClientConnection.ts +8 -2
- package/src/connection/websocket/AutoCertifierClientFacade.ts +11 -11
- package/src/connection/websocket/NodeWebsocketClientConnection.ts +1 -1
- package/src/connection/websocket/WebsocketClientConnector.ts +119 -0
- package/src/connection/websocket/WebsocketClientConnectorRpcLocal.ts +39 -0
- package/src/connection/websocket/{WebsocketConnectorRpcRemote.ts → WebsocketClientConnectorRpcRemote.ts} +2 -2
- package/src/connection/websocket/WebsocketServer.ts +18 -14
- package/src/connection/websocket/{WebsocketConnector.ts → WebsocketServerConnector.ts} +128 -205
- package/src/dht/DhtNode.ts +90 -89
- package/src/dht/DhtNodeRpcLocal.ts +11 -11
- package/src/dht/ExternalApiRpcLocal.ts +6 -6
- package/src/dht/ExternalApiRpcRemote.ts +2 -2
- package/src/dht/PeerManager.ts +24 -24
- package/src/dht/contact/SortedContactList.ts +10 -10
- package/src/dht/discovery/DiscoverySession.ts +24 -24
- package/src/dht/discovery/PeerDiscovery.ts +47 -45
- package/src/dht/discovery/RingDiscoverySession.ts +23 -23
- package/src/dht/recursive-operation/RecursiveOperationManager.ts +36 -36
- package/src/dht/recursive-operation/RecursiveOperationRpcLocal.ts +9 -9
- package/src/dht/recursive-operation/RecursiveOperationSession.ts +25 -25
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +7 -7
- package/src/dht/routing/Router.ts +21 -21
- package/src/dht/routing/RouterRpcLocal.ts +17 -17
- package/src/dht/routing/RoutingSession.ts +26 -26
- package/src/dht/store/StoreManager.ts +27 -27
- package/src/dht/store/StoreRpcLocal.ts +13 -13
- package/src/exports.ts +3 -0
- package/src/proto/google/protobuf/any.ts +6 -9
- package/src/proto/google/protobuf/empty.ts +2 -1
- package/src/proto/google/protobuf/timestamp.ts +2 -11
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +9 -9
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +3 -3
- package/src/proto/packages/dht/protos/DhtRpc.ts +4 -4
- package/src/proto/packages/proto-rpc/protos/ProtoRpc.ts +1 -1
- package/src/transport/ListeningRpcCommunicator.ts +3 -3
- package/src/transport/RoutingRpcCommunicator.ts +3 -3
- package/test/end-to-end/Layer0Webrtc.test.ts +0 -10
- package/test/integration/ConnectionManager.test.ts +3 -2
- package/test/integration/GeoIpConnectivityChecking.test.ts +1 -1
- package/test/integration/SimultaneousConnections.test.ts +2 -2
- package/test/integration/WebrtcConnectionManagement.test.ts +2 -10
- package/test/integration/{WebsocketConnectorRpc.test.ts → WebsocketClientConnectorRpc.test.ts} +9 -9
- package/test/integration/WebsocketConnectionManagement.test.ts +11 -29
- package/test/unit/ConnectionManager.test.ts +64 -0
- package/test/unit/DiscoverySession.test.ts +1 -1
- package/test/unit/Handshaker.test.ts +169 -0
- package/test/unit/ManagedConnection.test.ts +58 -0
- package/test/unit/PendingConnection.test.ts +57 -0
- package/test/unit/WebrtcConnector.test.ts +56 -0
- package/test/unit/{WebsocketConnector.test.ts → WebsocketClientConnector.test.ts} +56 -11
- package/test/unit/WebsocketServerConnector.test.ts +102 -0
- package/test/utils/FakeConnectorFacade.ts +41 -0
- package/test/utils/mock/MockConnection.ts +26 -0
- package/test/utils/utils.ts +2 -2
- package/dist/src/connection/IConnectionSource.d.ts +0 -4
- package/dist/src/connection/IConnectionSource.js +0 -3
- package/dist/src/connection/IConnectionSource.js.map +0 -1
- package/dist/src/connection/webrtc/ManagedWebrtcConnection.d.ts +0 -7
- package/dist/src/connection/webrtc/ManagedWebrtcConnection.js +0 -20
- package/dist/src/connection/webrtc/ManagedWebrtcConnection.js.map +0 -1
- package/dist/src/connection/websocket/WebsocketConnector.js.map +0 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +0 -19
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +0 -23
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +0 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +0 -5
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +0 -1
- package/src/connection/IConnectionSource.ts +0 -6
- package/src/connection/webrtc/ManagedWebrtcConnection.ts +0 -27
- package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +0 -39
|
@@ -1,48 +1,33 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { IConnection, ConnectionType } from '../IConnection'
|
|
3
|
-
import { ITransport } from '../../transport/ITransport'
|
|
1
|
+
import { GeoIpLocator } from '@streamr/geoip-location'
|
|
4
2
|
import { ListeningRpcCommunicator } from '../../transport/ListeningRpcCommunicator'
|
|
5
|
-
import {
|
|
6
|
-
import { WebsocketConnectorRpcRemote } from './WebsocketConnectorRpcRemote'
|
|
7
|
-
import {
|
|
8
|
-
ConnectivityMethod,
|
|
9
|
-
ConnectivityResponse,
|
|
10
|
-
HandshakeError,
|
|
11
|
-
PeerDescriptor,
|
|
12
|
-
WebsocketConnectionRequest
|
|
13
|
-
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
14
|
-
import { WebsocketConnectorRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
15
|
-
import { ipv4ToNumber, Logger, wait } from '@streamr/utils'
|
|
16
|
-
import { ManagedConnection } from '../ManagedConnection'
|
|
3
|
+
import { Action, connectivityMethodToWebsocketUrl } from './WebsocketClientConnector'
|
|
17
4
|
import { WebsocketServer } from './WebsocketServer'
|
|
18
|
-
import {
|
|
5
|
+
import { areEqualPeerDescriptors, DhtAddress, getNodeIdFromPeerDescriptor } from '../../identifiers'
|
|
6
|
+
import { AutoCertifierClientFacade } from './AutoCertifierClientFacade'
|
|
7
|
+
import { ConnectivityResponse, HandshakeError, PeerDescriptor } from '../../proto/packages/dht/protos/DhtRpc'
|
|
19
8
|
import { NatType, PortRange, TlsCertificate } from '../ConnectionManager'
|
|
9
|
+
import { ITransport } from '../../transport/ITransport'
|
|
10
|
+
import { ipv4ToNumber, Logger, wait } from '@streamr/utils'
|
|
11
|
+
import { attachConnectivityRequestHandler, DISABLE_CONNECTIVITY_PROBE } from '../connectivityRequestHandler'
|
|
20
12
|
import { WebsocketServerConnection } from './WebsocketServerConnection'
|
|
21
|
-
import {
|
|
13
|
+
import { ConnectionType, IConnection } from '../IConnection'
|
|
22
14
|
import queryString from 'querystring'
|
|
15
|
+
import { isMaybeSupportedVersion, LOCAL_PROTOCOL_VERSION } from '../../helpers/version'
|
|
23
16
|
import { shuffle } from 'lodash'
|
|
24
|
-
import {
|
|
25
|
-
import {
|
|
17
|
+
import { sendConnectivityRequest } from '../connectivityChecker'
|
|
18
|
+
import { acceptHandshake, Handshaker, rejectHandshake } from '../Handshaker'
|
|
19
|
+
import { WebsocketClientConnectorRpcRemote } from './WebsocketClientConnectorRpcRemote'
|
|
20
|
+
import { WebsocketClientConnectorRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
26
21
|
import { WebsocketServerStartError } from '../../helpers/errors'
|
|
27
|
-
import { AutoCertifierClientFacade } from './AutoCertifierClientFacade'
|
|
28
|
-
import { DISABLE_CONNECTIVITY_PROBE, attachConnectivityRequestHandler } from '../connectivityRequestHandler'
|
|
29
22
|
import * as Err from '../../helpers/errors'
|
|
30
|
-
import {
|
|
31
|
-
import {
|
|
32
|
-
import { LOCAL_PROTOCOL_VERSION, isMaybeSupportedVersion } from '../../helpers/version'
|
|
33
|
-
import { GeoIpLocator } from '@streamr/geoip-location'
|
|
23
|
+
import { expectedConnectionType } from '../../helpers/Connectivity'
|
|
24
|
+
import { PendingConnection } from '../PendingConnection'
|
|
34
25
|
|
|
35
26
|
const logger = new Logger(module)
|
|
36
27
|
|
|
37
|
-
export
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return (ws.tls ? 'wss://' : 'ws://') + ws.host + ':' + ws.port + ((action !== undefined) ? '?action=' + action : '')
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export interface WebsocketConnectorConfig {
|
|
44
|
-
transport: ITransport
|
|
45
|
-
onNewConnection: (connection: ManagedConnection) => boolean
|
|
28
|
+
export interface WebsocketServerConnectorOptions {
|
|
29
|
+
onNewConnection: (connection: PendingConnection) => boolean
|
|
30
|
+
rpcCommunicator: ListeningRpcCommunicator
|
|
46
31
|
hasConnection: (nodeId: DhtAddress) => boolean
|
|
47
32
|
portRange?: PortRange
|
|
48
33
|
maxMessageSize?: number
|
|
@@ -56,82 +41,34 @@ export interface WebsocketConnectorConfig {
|
|
|
56
41
|
geoIpDatabaseFolder?: string
|
|
57
42
|
}
|
|
58
43
|
|
|
59
|
-
|
|
44
|
+
// TODO: Could delFunc be removed here if the Handshaker event based clean up works correctly now?
|
|
45
|
+
interface OngoingConnectionRequest {
|
|
46
|
+
delFunc: () => void
|
|
47
|
+
pendingConnection: PendingConnection
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// TODO: Move server starting logic including autocertification and connectivity checking to WebsocketServer.ts?
|
|
51
|
+
export class WebsocketServerConnector {
|
|
60
52
|
|
|
61
|
-
private static readonly WEBSOCKET_CONNECTOR_SERVICE_ID = 'system/websocket-connector'
|
|
62
|
-
private readonly rpcCommunicator: ListeningRpcCommunicator
|
|
63
53
|
private readonly websocketServer?: WebsocketServer
|
|
64
54
|
private geoIpLocator?: GeoIpLocator
|
|
65
|
-
private readonly ongoingConnectRequests: Map<DhtAddress,
|
|
55
|
+
private readonly ongoingConnectRequests: Map<DhtAddress, OngoingConnectionRequest> = new Map()
|
|
66
56
|
private host?: string
|
|
67
57
|
private autoCertifierClient?: AutoCertifierClientFacade
|
|
68
58
|
private selectedPort?: number
|
|
69
59
|
private localPeerDescriptor?: PeerDescriptor
|
|
70
|
-
private connectingConnections: Map<DhtAddress, ManagedConnection> = new Map()
|
|
71
60
|
private abortController = new AbortController()
|
|
72
|
-
private readonly
|
|
61
|
+
private readonly options: WebsocketServerConnectorOptions
|
|
73
62
|
|
|
74
|
-
constructor(
|
|
75
|
-
this.
|
|
76
|
-
this.websocketServer =
|
|
77
|
-
portRange:
|
|
78
|
-
tlsCertificate:
|
|
79
|
-
maxMessageSize:
|
|
80
|
-
enableTls:
|
|
63
|
+
constructor(options: WebsocketServerConnectorOptions) {
|
|
64
|
+
this.options = options
|
|
65
|
+
this.websocketServer = options.portRange ? new WebsocketServer({
|
|
66
|
+
portRange: options.portRange,
|
|
67
|
+
tlsCertificate: options.tlsCertificate,
|
|
68
|
+
maxMessageSize: options.maxMessageSize,
|
|
69
|
+
enableTls: options.serverEnableTls
|
|
81
70
|
}) : undefined
|
|
82
|
-
this.host =
|
|
83
|
-
this.rpcCommunicator = new ListeningRpcCommunicator(WebsocketConnector.WEBSOCKET_CONNECTOR_SERVICE_ID, config.transport, {
|
|
84
|
-
rpcRequestTimeout: 15000 // TODO use config option or named constant?
|
|
85
|
-
})
|
|
86
|
-
this.registerLocalRpcMethods(config)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
private registerLocalRpcMethods(config: WebsocketConnectorConfig) {
|
|
90
|
-
const rpcLocal = new WebsocketConnectorRpcLocal({
|
|
91
|
-
connect: (targetPeerDescriptor: PeerDescriptor) => this.connect(targetPeerDescriptor),
|
|
92
|
-
hasConnection: (nodeId: DhtAddress): boolean => (this.connectingConnections.has(nodeId)
|
|
93
|
-
|| this.ongoingConnectRequests.has(nodeId)
|
|
94
|
-
|| config.hasConnection(nodeId))
|
|
95
|
-
,
|
|
96
|
-
onNewConnection: (connection: ManagedConnection) => config.onNewConnection(connection),
|
|
97
|
-
abortSignal: this.abortController.signal
|
|
98
|
-
})
|
|
99
|
-
this.rpcCommunicator.registerRpcNotification(
|
|
100
|
-
WebsocketConnectionRequest,
|
|
101
|
-
'requestConnection',
|
|
102
|
-
async (req: WebsocketConnectionRequest, context: ServerCallContext): Promise<Empty> => {
|
|
103
|
-
if (!this.abortController.signal.aborted) {
|
|
104
|
-
return rpcLocal.requestConnection(req, context)
|
|
105
|
-
} else {
|
|
106
|
-
return {}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
private attachHandshaker(connection: IConnection) {
|
|
113
|
-
const handshaker = new Handshaker(this.localPeerDescriptor!, connection)
|
|
114
|
-
handshaker.once('handshakeRequest', (localPeerDescriptor: PeerDescriptor, sourceVersion: string, remotePeerDescriptor?: PeerDescriptor) => {
|
|
115
|
-
this.onServerSocketHandshakeRequest(localPeerDescriptor, connection, sourceVersion, remotePeerDescriptor)
|
|
116
|
-
})
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
public async autoCertify(): Promise<void> {
|
|
120
|
-
this.autoCertifierClient = new AutoCertifierClientFacade({
|
|
121
|
-
configFile: this.config.autoCertifierConfigFile,
|
|
122
|
-
transport: this.config.autoCertifierTransport,
|
|
123
|
-
url: this.config.autoCertifierUrl,
|
|
124
|
-
wsServerPort: this.selectedPort!,
|
|
125
|
-
setHost: (hostName: string) => this.setHost(hostName),
|
|
126
|
-
updateCertificate: (certificate: string, privateKey: string) => this.websocketServer!.updateCertificate(certificate, privateKey)
|
|
127
|
-
})
|
|
128
|
-
logger.trace(`AutoCertifying subdomain...`)
|
|
129
|
-
await this.autoCertifierClient.start()
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
private setHost(hostName: string): void {
|
|
133
|
-
logger.trace(`Setting host name to ${hostName}`)
|
|
134
|
-
this.host = hostName
|
|
71
|
+
this.host = options.host
|
|
135
72
|
}
|
|
136
73
|
|
|
137
74
|
public async start(): Promise<void> {
|
|
@@ -159,8 +96,8 @@ export class WebsocketConnector {
|
|
|
159
96
|
}
|
|
160
97
|
})
|
|
161
98
|
|
|
162
|
-
if (this.
|
|
163
|
-
const geoIpLocator = new GeoIpLocator(this.
|
|
99
|
+
if (this.options.geoIpDatabaseFolder) {
|
|
100
|
+
const geoIpLocator = new GeoIpLocator(this.options.geoIpDatabaseFolder)
|
|
164
101
|
try {
|
|
165
102
|
await geoIpLocator.start()
|
|
166
103
|
this.geoIpLocator = geoIpLocator
|
|
@@ -174,6 +111,48 @@ export class WebsocketConnector {
|
|
|
174
111
|
}
|
|
175
112
|
}
|
|
176
113
|
|
|
114
|
+
private attachHandshaker(connection: IConnection) {
|
|
115
|
+
// TODO: use createIncomingHandshaker here?
|
|
116
|
+
const handshaker = new Handshaker(this.localPeerDescriptor!, connection)
|
|
117
|
+
handshaker.once('handshakeRequest', (localPeerDescriptor: PeerDescriptor, sourceVersion: string, remotePeerDescriptor?: PeerDescriptor) => {
|
|
118
|
+
this.onServerSocketHandshakeRequest(localPeerDescriptor, connection, handshaker, sourceVersion, remotePeerDescriptor)
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private onServerSocketHandshakeRequest(
|
|
123
|
+
sourcePeerDescriptor: PeerDescriptor,
|
|
124
|
+
websocketServerConnection: IConnection,
|
|
125
|
+
handshaker: Handshaker,
|
|
126
|
+
remoteVersion: string,
|
|
127
|
+
targetPeerDescriptor?: PeerDescriptor
|
|
128
|
+
) {
|
|
129
|
+
const nodeId = getNodeIdFromPeerDescriptor(sourcePeerDescriptor)
|
|
130
|
+
if (this.ongoingConnectRequests.has(nodeId)) {
|
|
131
|
+
const { pendingConnection, delFunc } = this.ongoingConnectRequests.get(nodeId)!
|
|
132
|
+
if (!isMaybeSupportedVersion(remoteVersion)) {
|
|
133
|
+
rejectHandshake(pendingConnection, websocketServerConnection, handshaker, HandshakeError.UNSUPPORTED_VERSION)
|
|
134
|
+
delFunc()
|
|
135
|
+
} else if (targetPeerDescriptor && !areEqualPeerDescriptors(this.localPeerDescriptor!, targetPeerDescriptor)) {
|
|
136
|
+
rejectHandshake(pendingConnection, websocketServerConnection, handshaker, HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR)
|
|
137
|
+
delFunc()
|
|
138
|
+
} else {
|
|
139
|
+
acceptHandshake(handshaker, pendingConnection, websocketServerConnection)
|
|
140
|
+
}
|
|
141
|
+
} else {
|
|
142
|
+
const pendingConnection = new PendingConnection(sourcePeerDescriptor)
|
|
143
|
+
|
|
144
|
+
if (!isMaybeSupportedVersion(remoteVersion)) {
|
|
145
|
+
rejectHandshake(pendingConnection, websocketServerConnection, handshaker, HandshakeError.UNSUPPORTED_VERSION)
|
|
146
|
+
} else if (targetPeerDescriptor && !areEqualPeerDescriptors(this.localPeerDescriptor!, targetPeerDescriptor)) {
|
|
147
|
+
rejectHandshake(pendingConnection, websocketServerConnection, handshaker, HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR)
|
|
148
|
+
} else if (this.options.onNewConnection(pendingConnection)) {
|
|
149
|
+
acceptHandshake(handshaker, pendingConnection, websocketServerConnection)
|
|
150
|
+
} else {
|
|
151
|
+
rejectHandshake(pendingConnection, websocketServerConnection, handshaker, HandshakeError.DUPLICATE_CONNECTION)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
177
156
|
public async checkConnectivity(allowSelfSignedCertificate: boolean): Promise<ConnectivityResponse> {
|
|
178
157
|
// TODO: this could throw?
|
|
179
158
|
if (this.abortController.signal.aborted) {
|
|
@@ -184,22 +163,22 @@ export class WebsocketConnector {
|
|
|
184
163
|
version: LOCAL_PROTOCOL_VERSION
|
|
185
164
|
}
|
|
186
165
|
}
|
|
187
|
-
if (!this.
|
|
188
|
-
// return connectivity info given in
|
|
166
|
+
if (!this.options.entrypoints || this.options.entrypoints.length === 0) {
|
|
167
|
+
// return connectivity info given in options
|
|
189
168
|
return {
|
|
190
169
|
host: this.host!,
|
|
191
170
|
natType: NatType.OPEN_INTERNET,
|
|
192
171
|
websocket: {
|
|
193
172
|
host: this.host!,
|
|
194
173
|
port: this.selectedPort!,
|
|
195
|
-
tls: this.
|
|
174
|
+
tls: this.options.tlsCertificate !== undefined
|
|
196
175
|
},
|
|
197
176
|
// TODO: Resolve the given host name or or use as is if IP was given.
|
|
198
177
|
ipAddress: ipv4ToNumber('127.0.0.1'),
|
|
199
178
|
version: LOCAL_PROTOCOL_VERSION
|
|
200
179
|
}
|
|
201
180
|
}
|
|
202
|
-
const shuffledEntrypoints = shuffle(this.
|
|
181
|
+
const shuffledEntrypoints = shuffle(this.options.entrypoints)
|
|
203
182
|
while (shuffledEntrypoints.length > 0 && !this.abortController.signal.aborted) {
|
|
204
183
|
const entryPoint = shuffledEntrypoints[0]
|
|
205
184
|
try {
|
|
@@ -207,7 +186,7 @@ export class WebsocketConnector {
|
|
|
207
186
|
const connectivityRequest = {
|
|
208
187
|
port: this.selectedPort ?? DISABLE_CONNECTIVITY_PROBE,
|
|
209
188
|
host: this.host,
|
|
210
|
-
tls: this.websocketServer ? this.
|
|
189
|
+
tls: this.websocketServer ? this.options.serverEnableTls : false,
|
|
211
190
|
allowSelfSignedCertificate
|
|
212
191
|
}
|
|
213
192
|
if (!this.abortController.signal.aborted) {
|
|
@@ -224,64 +203,44 @@ export class WebsocketConnector {
|
|
|
224
203
|
}
|
|
225
204
|
}
|
|
226
205
|
throw new WebsocketServerStartError(
|
|
227
|
-
`Failed to connect to the entrypoints after ${this.
|
|
228
|
-
+ `Attempted hosts: ${this.
|
|
206
|
+
`Failed to connect to the entrypoints after ${this.options.entrypoints.length} attempts\n`
|
|
207
|
+
+ `Attempted hosts: ${this.options.entrypoints.map((entry) => `${entry.websocket!.host}:${entry.websocket!.port}`).join(', ')}`
|
|
229
208
|
)
|
|
230
209
|
}
|
|
231
210
|
|
|
232
|
-
public
|
|
233
|
-
|
|
234
|
-
|
|
211
|
+
public async autoCertify(): Promise<void> {
|
|
212
|
+
this.autoCertifierClient = new AutoCertifierClientFacade({
|
|
213
|
+
configFile: this.options.autoCertifierConfigFile,
|
|
214
|
+
transport: this.options.autoCertifierTransport,
|
|
215
|
+
url: this.options.autoCertifierUrl,
|
|
216
|
+
wsServerPort: this.selectedPort!,
|
|
217
|
+
setHost: (hostName: string) => this.setHost(hostName),
|
|
218
|
+
updateCertificate: (certificate: string, privateKey: string) => this.websocketServer!.updateCertificate(certificate, privateKey)
|
|
219
|
+
})
|
|
220
|
+
logger.trace(`AutoCertifying subdomain...`)
|
|
221
|
+
await this.autoCertifierClient.start()
|
|
235
222
|
}
|
|
236
223
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
return existingConnection
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
if (this.localPeerDescriptor!.websocket && !targetPeerDescriptor.websocket) {
|
|
245
|
-
return this.requestConnectionFromPeer(this.localPeerDescriptor!, targetPeerDescriptor)
|
|
246
|
-
} else {
|
|
247
|
-
const socket = new WebsocketClientConnection()
|
|
248
|
-
|
|
249
|
-
const url = connectivityMethodToWebsocketUrl(targetPeerDescriptor.websocket!)
|
|
250
|
-
|
|
251
|
-
const managedConnection = new ManagedConnection(
|
|
252
|
-
this.localPeerDescriptor!,
|
|
253
|
-
ConnectionType.WEBSOCKET_CLIENT,
|
|
254
|
-
socket,
|
|
255
|
-
undefined,
|
|
256
|
-
targetPeerDescriptor
|
|
257
|
-
)
|
|
258
|
-
managedConnection.setRemotePeerDescriptor(targetPeerDescriptor)
|
|
259
|
-
|
|
260
|
-
this.connectingConnections.set(nodeId, managedConnection)
|
|
261
|
-
|
|
262
|
-
const delFunc = () => {
|
|
263
|
-
if (this.connectingConnections.has(nodeId)) {
|
|
264
|
-
this.connectingConnections.delete(nodeId)
|
|
265
|
-
}
|
|
266
|
-
socket.off('disconnected', delFunc)
|
|
267
|
-
managedConnection.off('handshakeCompleted', delFunc)
|
|
268
|
-
}
|
|
269
|
-
socket.on('disconnected', delFunc)
|
|
270
|
-
managedConnection.on('handshakeCompleted', delFunc)
|
|
271
|
-
|
|
272
|
-
socket.connect(url, false)
|
|
224
|
+
private setHost(hostName: string): void {
|
|
225
|
+
logger.trace(`Setting host name to ${hostName}`)
|
|
226
|
+
this.host = hostName
|
|
227
|
+
}
|
|
273
228
|
|
|
274
|
-
|
|
229
|
+
public connect(targetPeerDescriptor: PeerDescriptor): PendingConnection {
|
|
230
|
+
const nodeId = getNodeIdFromPeerDescriptor(targetPeerDescriptor)
|
|
231
|
+
if (this.ongoingConnectRequests.has(nodeId)) {
|
|
232
|
+
return this.ongoingConnectRequests.get(nodeId)!.pendingConnection
|
|
275
233
|
}
|
|
234
|
+
return this.requestConnectionFromPeer(this.localPeerDescriptor!, targetPeerDescriptor)
|
|
276
235
|
}
|
|
277
236
|
|
|
278
|
-
private requestConnectionFromPeer(localPeerDescriptor: PeerDescriptor, targetPeerDescriptor: PeerDescriptor):
|
|
237
|
+
private requestConnectionFromPeer(localPeerDescriptor: PeerDescriptor, targetPeerDescriptor: PeerDescriptor): PendingConnection {
|
|
279
238
|
setImmediate(() => {
|
|
280
|
-
const remoteConnector = new
|
|
239
|
+
const remoteConnector = new WebsocketClientConnectorRpcRemote(
|
|
281
240
|
localPeerDescriptor,
|
|
282
241
|
targetPeerDescriptor,
|
|
283
|
-
this.rpcCommunicator,
|
|
284
|
-
|
|
242
|
+
this.options.rpcCommunicator,
|
|
243
|
+
WebsocketClientConnectorRpcClient
|
|
285
244
|
)
|
|
286
245
|
remoteConnector.requestConnection().then(() => {
|
|
287
246
|
logger.trace('Sent WebsocketConnectionRequest notification to peer', { targetPeerDescriptor })
|
|
@@ -291,57 +250,23 @@ export class WebsocketConnector {
|
|
|
291
250
|
})
|
|
292
251
|
})
|
|
293
252
|
})
|
|
294
|
-
const
|
|
295
|
-
this.localPeerDescriptor!,
|
|
296
|
-
ConnectionType.WEBSOCKET_SERVER,
|
|
297
|
-
undefined,
|
|
298
|
-
undefined,
|
|
299
|
-
targetPeerDescriptor
|
|
300
|
-
)
|
|
253
|
+
const pendingConnection = new PendingConnection(targetPeerDescriptor)
|
|
301
254
|
const nodeId = getNodeIdFromPeerDescriptor(targetPeerDescriptor)
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
private onServerSocketHandshakeRequest(
|
|
309
|
-
sourcePeerDescriptor: PeerDescriptor,
|
|
310
|
-
websocketServerConnection: IConnection,
|
|
311
|
-
remoteVersion: string,
|
|
312
|
-
targetPeerDescriptor?: PeerDescriptor
|
|
313
|
-
) {
|
|
314
|
-
const nodeId = getNodeIdFromPeerDescriptor(sourcePeerDescriptor)
|
|
315
|
-
if (this.ongoingConnectRequests.has(nodeId)) {
|
|
316
|
-
const ongoingConnectRequest = this.ongoingConnectRequests.get(nodeId)!
|
|
317
|
-
if (!isMaybeSupportedVersion(remoteVersion)) {
|
|
318
|
-
ongoingConnectRequest.rejectHandshake(HandshakeError.UNSUPPORTED_VERSION)
|
|
319
|
-
} else if (targetPeerDescriptor && !areEqualPeerDescriptors(this.localPeerDescriptor!, targetPeerDescriptor)) {
|
|
320
|
-
ongoingConnectRequest.rejectHandshake(HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR)
|
|
321
|
-
} else {
|
|
322
|
-
ongoingConnectRequest.attachImplementation(websocketServerConnection)
|
|
323
|
-
ongoingConnectRequest.acceptHandshake()
|
|
324
|
-
}
|
|
255
|
+
// TODO: can this leak?
|
|
256
|
+
const delFunc = () => {
|
|
257
|
+
pendingConnection.off('connected', delFunc)
|
|
258
|
+
pendingConnection.off('disconnected', delFunc)
|
|
325
259
|
this.ongoingConnectRequests.delete(nodeId)
|
|
326
|
-
} else {
|
|
327
|
-
const managedConnection = new ManagedConnection(
|
|
328
|
-
this.localPeerDescriptor!,
|
|
329
|
-
ConnectionType.WEBSOCKET_SERVER,
|
|
330
|
-
undefined,
|
|
331
|
-
websocketServerConnection,
|
|
332
|
-
targetPeerDescriptor
|
|
333
|
-
)
|
|
334
|
-
managedConnection.setRemotePeerDescriptor(sourcePeerDescriptor)
|
|
335
|
-
if (!isMaybeSupportedVersion(remoteVersion)) {
|
|
336
|
-
managedConnection.rejectHandshake(HandshakeError.UNSUPPORTED_VERSION)
|
|
337
|
-
} else if (targetPeerDescriptor && !areEqualPeerDescriptors(this.localPeerDescriptor!, targetPeerDescriptor)) {
|
|
338
|
-
managedConnection.rejectHandshake(HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR)
|
|
339
|
-
} else if (this.config.onNewConnection(managedConnection)) {
|
|
340
|
-
managedConnection.acceptHandshake()
|
|
341
|
-
} else {
|
|
342
|
-
managedConnection.rejectHandshake(HandshakeError.DUPLICATE_CONNECTION)
|
|
343
|
-
}
|
|
344
260
|
}
|
|
261
|
+
pendingConnection.on('connected', delFunc)
|
|
262
|
+
pendingConnection.on('disconnected', delFunc)
|
|
263
|
+
this.ongoingConnectRequests.set(nodeId, { pendingConnection, delFunc })
|
|
264
|
+
return pendingConnection
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
public isPossibleToFormConnection(targetPeerDescriptor: PeerDescriptor): boolean {
|
|
268
|
+
const connectionType = expectedConnectionType(this.localPeerDescriptor!, targetPeerDescriptor)
|
|
269
|
+
return (connectionType === ConnectionType.WEBSOCKET_SERVER)
|
|
345
270
|
}
|
|
346
271
|
|
|
347
272
|
public setLocalPeerDescriptor(localPeerDescriptor: PeerDescriptor): void {
|
|
@@ -350,14 +275,12 @@ export class WebsocketConnector {
|
|
|
350
275
|
|
|
351
276
|
public async destroy(): Promise<void> {
|
|
352
277
|
this.abortController.abort()
|
|
353
|
-
this.rpcCommunicator.destroy()
|
|
354
278
|
|
|
355
279
|
const requests = Array.from(this.ongoingConnectRequests.values())
|
|
356
|
-
await Promise.allSettled(requests.map((
|
|
280
|
+
await Promise.allSettled(requests.map((ongoingConnectRequest) => ongoingConnectRequest.pendingConnection.close(true)))
|
|
357
281
|
|
|
358
|
-
const attempts = Array.from(this.connectingConnections.values())
|
|
359
|
-
await Promise.allSettled(attempts.map((conn) => conn.close(false)))
|
|
360
282
|
await this.websocketServer?.stop()
|
|
361
283
|
await this.geoIpLocator?.stop()
|
|
362
284
|
}
|
|
285
|
+
|
|
363
286
|
}
|