@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
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { WebsocketClientConnection } from './NodeWebsocketClientConnection'
|
|
2
|
+
import { ConnectionType } from '../IConnection'
|
|
3
|
+
import { ListeningRpcCommunicator } from '../../transport/ListeningRpcCommunicator'
|
|
4
|
+
import { WebsocketClientConnectorRpcLocal } from './WebsocketClientConnectorRpcLocal'
|
|
5
|
+
import {
|
|
6
|
+
ConnectivityMethod,
|
|
7
|
+
PeerDescriptor,
|
|
8
|
+
WebsocketConnectionRequest
|
|
9
|
+
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
10
|
+
import { WebsocketServer } from './WebsocketServer'
|
|
11
|
+
import { createOutgoingHandshaker } from '../Handshaker'
|
|
12
|
+
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
13
|
+
import { expectedConnectionType } from '../../helpers/Connectivity'
|
|
14
|
+
import { Empty } from '../../proto/google/protobuf/empty'
|
|
15
|
+
import { DhtAddress, getNodeIdFromPeerDescriptor } from '../../identifiers'
|
|
16
|
+
import { GeoIpLocator } from '@streamr/geoip-location'
|
|
17
|
+
import { PendingConnection } from '../PendingConnection'
|
|
18
|
+
|
|
19
|
+
export type Action = 'connectivityRequest' | 'connectivityProbe'
|
|
20
|
+
|
|
21
|
+
export const connectivityMethodToWebsocketUrl = (ws: ConnectivityMethod, action?: Action): string => {
|
|
22
|
+
return (ws.tls ? 'wss://' : 'ws://') + ws.host + ':' + ws.port + ((action !== undefined) ? '?action=' + action : '')
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface WebsocketClientConnectorOptions {
|
|
26
|
+
onNewConnection: (connection: PendingConnection) => boolean
|
|
27
|
+
hasConnection: (nodeId: DhtAddress) => boolean
|
|
28
|
+
rpcCommunicator: ListeningRpcCommunicator
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class WebsocketClientConnector {
|
|
32
|
+
|
|
33
|
+
public static readonly WEBSOCKET_CONNECTOR_SERVICE_ID = 'system/websocket-connector'
|
|
34
|
+
private readonly websocketServer?: WebsocketServer
|
|
35
|
+
private geoIpLocator?: GeoIpLocator
|
|
36
|
+
|
|
37
|
+
private localPeerDescriptor?: PeerDescriptor
|
|
38
|
+
private connectingConnections: Map<DhtAddress, PendingConnection> = new Map()
|
|
39
|
+
private abortController = new AbortController()
|
|
40
|
+
private readonly options: WebsocketClientConnectorOptions
|
|
41
|
+
|
|
42
|
+
constructor(options: WebsocketClientConnectorOptions) {
|
|
43
|
+
this.options = options
|
|
44
|
+
|
|
45
|
+
this.registerLocalRpcMethods()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private registerLocalRpcMethods() {
|
|
49
|
+
const rpcLocal = new WebsocketClientConnectorRpcLocal({
|
|
50
|
+
connect: (targetPeerDescriptor: PeerDescriptor) => this.connect(targetPeerDescriptor),
|
|
51
|
+
hasConnection: (nodeId: DhtAddress): boolean => (this.connectingConnections.has(nodeId)
|
|
52
|
+
|| this.options.hasConnection(nodeId))
|
|
53
|
+
,
|
|
54
|
+
onNewConnection: (connection: PendingConnection) => this.options.onNewConnection(connection),
|
|
55
|
+
abortSignal: this.abortController.signal
|
|
56
|
+
})
|
|
57
|
+
this.options.rpcCommunicator.registerRpcNotification(
|
|
58
|
+
WebsocketConnectionRequest,
|
|
59
|
+
'requestConnection',
|
|
60
|
+
async (req: WebsocketConnectionRequest, context: ServerCallContext): Promise<Empty> => {
|
|
61
|
+
if (!this.abortController.signal.aborted) {
|
|
62
|
+
return rpcLocal.requestConnection(req, context)
|
|
63
|
+
} else {
|
|
64
|
+
return {}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public isPossibleToFormConnection(targetPeerDescriptor: PeerDescriptor): boolean {
|
|
71
|
+
const connectionType = expectedConnectionType(this.localPeerDescriptor!, targetPeerDescriptor)
|
|
72
|
+
return connectionType === ConnectionType.WEBSOCKET_CLIENT
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public connect(targetPeerDescriptor: PeerDescriptor): PendingConnection {
|
|
76
|
+
const nodeId = getNodeIdFromPeerDescriptor(targetPeerDescriptor)
|
|
77
|
+
const existingConnection = this.connectingConnections.get(nodeId)
|
|
78
|
+
if (existingConnection) {
|
|
79
|
+
return existingConnection
|
|
80
|
+
}
|
|
81
|
+
const socket = new WebsocketClientConnection()
|
|
82
|
+
|
|
83
|
+
const url = connectivityMethodToWebsocketUrl(targetPeerDescriptor.websocket!)
|
|
84
|
+
|
|
85
|
+
const pendingConnection = new PendingConnection(targetPeerDescriptor)
|
|
86
|
+
createOutgoingHandshaker(this.localPeerDescriptor!, pendingConnection, socket, targetPeerDescriptor)
|
|
87
|
+
this.connectingConnections.set(nodeId, pendingConnection)
|
|
88
|
+
|
|
89
|
+
const delFunc = () => {
|
|
90
|
+
if (this.connectingConnections.has(nodeId)) {
|
|
91
|
+
this.connectingConnections.delete(nodeId)
|
|
92
|
+
}
|
|
93
|
+
socket.off('disconnected', delFunc)
|
|
94
|
+
pendingConnection.off('disconnected', delFunc)
|
|
95
|
+
pendingConnection.off('connected', delFunc)
|
|
96
|
+
}
|
|
97
|
+
socket.on('disconnected', delFunc)
|
|
98
|
+
pendingConnection.on('disconnected', delFunc)
|
|
99
|
+
pendingConnection.on('connected', delFunc)
|
|
100
|
+
|
|
101
|
+
socket.connect(url, false)
|
|
102
|
+
|
|
103
|
+
return pendingConnection
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public setLocalPeerDescriptor(peerDescriptor: PeerDescriptor): void {
|
|
107
|
+
this.localPeerDescriptor = peerDescriptor
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
public async destroy(): Promise<void> {
|
|
111
|
+
this.abortController.abort()
|
|
112
|
+
|
|
113
|
+
const requests = Array.from(this.connectingConnections.values())
|
|
114
|
+
await Promise.allSettled(requests.map((conn) => conn.close(true)))
|
|
115
|
+
|
|
116
|
+
await this.websocketServer?.stop()
|
|
117
|
+
await this.geoIpLocator?.stop()
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
2
|
+
import {
|
|
3
|
+
PeerDescriptor,
|
|
4
|
+
WebsocketConnectionRequest
|
|
5
|
+
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
6
|
+
import { IWebsocketClientConnectorRpc } from '../../proto/packages/dht/protos/DhtRpc.server'
|
|
7
|
+
import { DhtCallContext } from '../../rpc-protocol/DhtCallContext'
|
|
8
|
+
import { Empty } from '../../proto/google/protobuf/empty'
|
|
9
|
+
import { getNodeIdFromPeerDescriptor } from '../../identifiers'
|
|
10
|
+
import { DhtAddress } from '../../identifiers'
|
|
11
|
+
import { PendingConnection } from '../PendingConnection'
|
|
12
|
+
|
|
13
|
+
interface WebsocketClientConnectorRpcLocalOptions {
|
|
14
|
+
connect: (targetPeerDescriptor: PeerDescriptor) => PendingConnection
|
|
15
|
+
hasConnection: (nodeId: DhtAddress) => boolean
|
|
16
|
+
onNewConnection: (connection: PendingConnection) => boolean
|
|
17
|
+
abortSignal: AbortSignal
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export class WebsocketClientConnectorRpcLocal implements IWebsocketClientConnectorRpc {
|
|
21
|
+
|
|
22
|
+
private readonly options: WebsocketClientConnectorRpcLocalOptions
|
|
23
|
+
|
|
24
|
+
constructor(options: WebsocketClientConnectorRpcLocalOptions) {
|
|
25
|
+
this.options = options
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public async requestConnection(_request: WebsocketConnectionRequest, context: ServerCallContext): Promise<Empty> {
|
|
29
|
+
if (this.options.abortSignal.aborted) {
|
|
30
|
+
return {}
|
|
31
|
+
}
|
|
32
|
+
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
33
|
+
if (!this.options.hasConnection(getNodeIdFromPeerDescriptor(senderPeerDescriptor))) {
|
|
34
|
+
const connection = this.options.connect(senderPeerDescriptor)
|
|
35
|
+
this.options.onNewConnection(connection)
|
|
36
|
+
}
|
|
37
|
+
return {}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -3,12 +3,12 @@ import {
|
|
|
3
3
|
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
4
4
|
import { Logger } from '@streamr/utils'
|
|
5
5
|
import { RpcRemote } from '../../dht/contact/RpcRemote'
|
|
6
|
-
import {
|
|
6
|
+
import { WebsocketClientConnectorRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
7
7
|
import { getNodeIdFromPeerDescriptor } from '../../identifiers'
|
|
8
8
|
|
|
9
9
|
const logger = new Logger(module)
|
|
10
10
|
|
|
11
|
-
export class
|
|
11
|
+
export class WebsocketClientConnectorRpcRemote extends RpcRemote<WebsocketClientConnectorRpcClient> {
|
|
12
12
|
|
|
13
13
|
async requestConnection(): Promise<void> {
|
|
14
14
|
logger.trace(`Requesting WebSocket connection from ${getNodeIdFromPeerDescriptor(this.getLocalPeerDescriptor())}`)
|
|
@@ -3,7 +3,6 @@ import { createServer as createHttpsServer, Server as HttpsServer } from 'https'
|
|
|
3
3
|
import EventEmitter from 'eventemitter3'
|
|
4
4
|
import WebSocket from 'ws'
|
|
5
5
|
import { WebsocketServerConnection } from './WebsocketServerConnection'
|
|
6
|
-
import { ConnectionSourceEvents } from '../IConnectionSource'
|
|
7
6
|
import { Logger, asAbortable } from '@streamr/utils'
|
|
8
7
|
import { createSelfSignedCertificate } from '@streamr/autocertifier-client'
|
|
9
8
|
import { WebsocketServerStartError } from '../../helpers/errors'
|
|
@@ -12,33 +11,38 @@ import { range } from 'lodash'
|
|
|
12
11
|
import fs from 'fs'
|
|
13
12
|
import { v4 as uuid } from 'uuid'
|
|
14
13
|
import { parse } from 'url'
|
|
14
|
+
import { IConnection } from '../IConnection'
|
|
15
15
|
|
|
16
16
|
const logger = new Logger(module)
|
|
17
17
|
|
|
18
|
-
interface
|
|
18
|
+
interface WebsocketServerOptions {
|
|
19
19
|
portRange: PortRange
|
|
20
20
|
enableTls: boolean
|
|
21
21
|
tlsCertificate?: TlsCertificate
|
|
22
22
|
maxMessageSize?: number
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
interface Events {
|
|
26
|
+
connected: ((connection: IConnection) => void)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class WebsocketServer extends EventEmitter<Events> {
|
|
26
30
|
|
|
27
31
|
private httpServer?: HttpServer | HttpsServer
|
|
28
32
|
private wsServer?: WebSocket.Server
|
|
29
33
|
private readonly abortController = new AbortController()
|
|
30
|
-
private readonly
|
|
34
|
+
private readonly options: WebsocketServerOptions
|
|
31
35
|
|
|
32
|
-
constructor(
|
|
36
|
+
constructor(options: WebsocketServerOptions) {
|
|
33
37
|
super()
|
|
34
|
-
this.
|
|
38
|
+
this.options = options
|
|
35
39
|
}
|
|
36
40
|
|
|
37
41
|
public async start(): Promise<number> {
|
|
38
|
-
const ports = range(this.
|
|
42
|
+
const ports = range(this.options.portRange.min, this.options.portRange.max + 1)
|
|
39
43
|
for (const port of ports) {
|
|
40
44
|
try {
|
|
41
|
-
await asAbortable(this.startServer(port, this.
|
|
45
|
+
await asAbortable(this.startServer(port, this.options.enableTls), this.abortController.signal)
|
|
42
46
|
return port
|
|
43
47
|
} catch (err) {
|
|
44
48
|
if (err.originalError?.code === 'EADDRINUSE') {
|
|
@@ -49,7 +53,7 @@ export class WebsocketServer extends EventEmitter<ConnectionSourceEvents> {
|
|
|
49
53
|
}
|
|
50
54
|
}
|
|
51
55
|
throw new WebsocketServerStartError(
|
|
52
|
-
`Failed to start WebSocket server on any port in range: ${this.
|
|
56
|
+
`Failed to start WebSocket server on any port in range: ${this.options.portRange.min}-${this.options.portRange.min}`
|
|
53
57
|
)
|
|
54
58
|
}
|
|
55
59
|
|
|
@@ -62,15 +66,15 @@ export class WebsocketServer extends EventEmitter<ConnectionSourceEvents> {
|
|
|
62
66
|
response.end()
|
|
63
67
|
}
|
|
64
68
|
return new Promise((resolve, reject) => {
|
|
65
|
-
if (this.
|
|
69
|
+
if (this.options.tlsCertificate) {
|
|
66
70
|
this.httpServer = createHttpsServer({
|
|
67
|
-
key: fs.readFileSync(this.
|
|
68
|
-
cert: fs.readFileSync(this.
|
|
71
|
+
key: fs.readFileSync(this.options.tlsCertificate.privateKeyFileName),
|
|
72
|
+
cert: fs.readFileSync(this.options.tlsCertificate.certFileName)
|
|
69
73
|
}, requestListener)
|
|
70
74
|
} else if (!tls) {
|
|
71
75
|
this.httpServer = createHttpServer(requestListener)
|
|
72
76
|
} else {
|
|
73
|
-
// TODO use
|
|
77
|
+
// TODO use options option or named constant?
|
|
74
78
|
const certificate = createSelfSignedCertificate('streamr-self-signed-' + uuid(), 1000)
|
|
75
79
|
this.httpServer = createHttpsServer({
|
|
76
80
|
key: certificate.serverKey,
|
|
@@ -151,7 +155,7 @@ export class WebsocketServer extends EventEmitter<ConnectionSourceEvents> {
|
|
|
151
155
|
}
|
|
152
156
|
|
|
153
157
|
private createWsServer(): WebSocket.Server {
|
|
154
|
-
const maxPayload = this.
|
|
158
|
+
const maxPayload = this.options.maxMessageSize ?? 1048576
|
|
155
159
|
return this.wsServer = new WebSocket.Server({
|
|
156
160
|
noServer: true,
|
|
157
161
|
maxPayload
|