@streamr/dht 0.0.1-tatum.7 → 100.0.0-pretestnet.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/ConnectionLockRpcRemote.d.ts +10 -0
- package/dist/src/connection/{RemoteConnectionLocker.js → ConnectionLockRpcRemote.js} +20 -35
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -0
- package/dist/src/connection/ConnectionManager.d.ts +0 -1
- package/dist/src/connection/ConnectionManager.js +27 -25
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectivityChecker.js +4 -4
- package/dist/src/connection/ConnectivityChecker.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +1 -1
- package/dist/src/connection/ConnectorFacade.js +5 -8
- package/dist/src/connection/ConnectorFacade.js.map +1 -1
- package/dist/src/connection/Handshaker.d.ts +1 -2
- package/dist/src/connection/Handshaker.js +1 -4
- package/dist/src/connection/Handshaker.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +1 -2
- package/dist/src/connection/ManagedConnection.js +4 -5
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/ManagedWebRtcConnection.d.ts +1 -1
- package/dist/src/connection/ManagedWebRtcConnection.js +2 -2
- package/dist/src/connection/ManagedWebRtcConnection.js.map +1 -1
- package/dist/src/connection/Simulator/SimulatorConnector.d.ts +1 -2
- package/dist/src/connection/Simulator/SimulatorConnector.js +3 -4
- package/dist/src/connection/Simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/WebRTC/NodeWebRtcConnection.d.ts +1 -1
- package/dist/src/connection/WebRTC/{WebRtcConnector.d.ts → WebRtcConnectorRpcLocal.d.ts} +9 -9
- package/dist/src/connection/WebRTC/{WebRtcConnector.js → WebRtcConnectorRpcLocal.js} +38 -30
- package/dist/src/connection/WebRTC/WebRtcConnectorRpcLocal.js.map +1 -0
- package/dist/src/connection/WebRTC/WebRtcConnectorRpcRemote.d.ts +11 -0
- package/dist/src/connection/WebRTC/WebRtcConnectorRpcRemote.js +55 -0
- package/dist/src/connection/WebRTC/WebRtcConnectorRpcRemote.js.map +1 -0
- package/dist/src/connection/WebRTC/iceServerAsString.d.ts +1 -1
- package/dist/src/connection/WebSocket/{WebSocketConnector.d.ts → WebSocketConnectorRpcLocal.d.ts} +6 -7
- package/dist/src/connection/WebSocket/{WebSocketConnector.js → WebSocketConnectorRpcLocal.js} +20 -20
- package/dist/src/connection/WebSocket/WebSocketConnectorRpcLocal.js.map +1 -0
- package/dist/src/connection/WebSocket/WebSocketConnectorRpcRemote.d.ts +8 -0
- package/dist/src/connection/WebSocket/{RemoteWebSocketConnector.js → WebSocketConnectorRpcRemote.js} +11 -16
- package/dist/src/connection/WebSocket/WebSocketConnectorRpcRemote.js.map +1 -0
- package/dist/src/dht/DhtNode.d.ts +6 -7
- package/dist/src/dht/DhtNode.js +22 -23
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/{RemoteExternalApi.d.ts → ExternalApiRpcRemote.d.ts} +2 -2
- package/dist/src/dht/{RemoteExternalApi.js → ExternalApiRpcRemote.js} +5 -6
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -0
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.js +3 -2
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/find/RecursiveFindSession.d.ts +5 -5
- package/dist/src/dht/find/RecursiveFindSession.js +7 -7
- package/dist/src/dht/find/RecursiveFindSession.js.map +1 -1
- package/dist/src/dht/find/RecursiveFinder.d.ts +7 -7
- package/dist/src/dht/find/RecursiveFinder.js +24 -24
- package/dist/src/dht/find/RecursiveFinder.js.map +1 -1
- package/dist/src/dht/find/RemoteRecursiveFindSession.d.ts +1 -1
- package/dist/src/dht/find/RemoteRecursiveFindSession.js +4 -4
- package/dist/src/dht/find/RemoteRecursiveFindSession.js.map +1 -1
- package/dist/src/dht/registerExternalApiRpcMethods.js +4 -3
- package/dist/src/dht/registerExternalApiRpcMethods.js.map +1 -1
- package/dist/src/dht/routing/Router.d.ts +2 -2
- package/dist/src/dht/routing/Router.js +2 -4
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/{RemoteRouter.d.ts → RouterRpcRemote.d.ts} +2 -2
- package/dist/src/dht/routing/{RemoteRouter.js → RouterRpcRemote.js} +4 -4
- package/dist/src/dht/routing/RouterRpcRemote.js.map +1 -0
- package/dist/src/dht/routing/RoutingSession.js +2 -2
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/{DataStore.d.ts → StoreRpcLocal.d.ts} +4 -4
- package/dist/src/dht/store/{DataStore.js → StoreRpcLocal.js} +27 -27
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -0
- package/dist/src/dht/store/{RemoteStore.d.ts → StoreRpcRemote.d.ts} +2 -2
- package/dist/src/dht/store/{RemoteStore.js → StoreRpcRemote.js} +4 -4
- package/dist/src/dht/store/StoreRpcRemote.js.map +1 -0
- package/dist/src/exports.d.ts +1 -1
- package/dist/src/helpers/protoClasses.js +2 -2
- package/dist/src/helpers/protoClasses.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +29 -29
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +39 -39
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +49 -158
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +46 -86
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +15 -15
- package/dist/src/transport/RoutingRpcCommunicator.js +1 -0
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
- package/package.json +4 -4
- package/protos/DhtRpc.proto +30 -59
- package/src/connection/ConnectionLockRpcRemote.ts +62 -0
- package/src/connection/ConnectionManager.ts +33 -32
- package/src/connection/ConnectivityChecker.ts +1 -1
- package/src/connection/ConnectorFacade.ts +7 -10
- package/src/connection/Handshaker.ts +0 -5
- package/src/connection/ManagedConnection.ts +3 -6
- package/src/connection/ManagedWebRtcConnection.ts +0 -2
- package/src/connection/Simulator/SimulatorConnector.ts +2 -7
- package/src/connection/WebRTC/BrowserWebRtcConnection.ts +1 -1
- package/src/connection/WebRTC/NodeWebRtcConnection.ts +1 -1
- package/src/connection/WebRTC/{WebRtcConnector.ts → WebRtcConnectorRpcLocal.ts} +46 -34
- package/src/connection/WebRTC/WebRtcConnectorRpcRemote.ts +71 -0
- package/src/connection/WebRTC/iceServerAsString.ts +1 -1
- package/src/connection/WebSocket/{WebSocketConnector.ts → WebSocketConnectorRpcLocal.ts} +26 -26
- package/src/connection/WebSocket/{RemoteWebSocketConnector.ts → WebSocketConnectorRpcRemote.ts} +13 -18
- package/src/dht/DhtNode.ts +32 -33
- package/src/dht/{RemoteExternalApi.ts → ExternalApiRpcRemote.ts} +3 -4
- package/src/dht/discovery/PeerDiscovery.ts +4 -3
- package/src/dht/find/RecursiveFindSession.ts +11 -11
- package/src/dht/find/RecursiveFinder.ts +29 -30
- package/src/dht/find/RemoteRecursiveFindSession.ts +6 -6
- package/src/dht/registerExternalApiRpcMethods.ts +7 -4
- package/src/dht/routing/Router.ts +4 -6
- package/src/dht/routing/{RemoteRouter.ts → RouterRpcRemote.ts} +2 -2
- package/src/dht/routing/RoutingSession.ts +6 -6
- package/src/dht/store/{DataStore.ts → StoreRpcLocal.ts} +31 -31
- package/src/dht/store/{RemoteStore.ts → StoreRpcRemote.ts} +2 -2
- package/src/exports.ts +1 -1
- package/src/helpers/protoClasses.ts +4 -4
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +54 -54
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +15 -15
- package/src/proto/packages/dht/protos/DhtRpc.ts +76 -210
- package/src/transport/RoutingRpcCommunicator.ts +1 -0
- package/test/integration/{RemoteRouter.test.ts → RouterRpcRemote.test.ts} +8 -8
- package/test/integration/{RemoteStore.test.ts → StoreRpcRemote.test.ts} +13 -13
- package/test/integration/WebRtcConnectorRpc.test.ts +11 -24
- package/test/integration/WebSocketConnectorRpc.test.ts +8 -12
- package/test/unit/RecursiveFinder.test.ts +5 -6
- package/test/unit/{WebSocketConnector.test.ts → WebSocketConnectorRpcLocal.test.ts} +3 -3
- package/test/unit/webrtcReplaceInternalIpWithExternalIp.test.ts +1 -1
- package/test/utils/utils.ts +16 -26
- package/dist/src/connection/RemoteConnectionLocker.d.ts +0 -13
- package/dist/src/connection/RemoteConnectionLocker.js.map +0 -1
- package/dist/src/connection/WebRTC/RemoteWebrtcConnector.d.ts +0 -12
- package/dist/src/connection/WebRTC/RemoteWebrtcConnector.js +0 -74
- package/dist/src/connection/WebRTC/RemoteWebrtcConnector.js.map +0 -1
- package/dist/src/connection/WebRTC/WebRtcConnector.js.map +0 -1
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.d.ts +0 -9
- package/dist/src/connection/WebSocket/RemoteWebSocketConnector.js.map +0 -1
- package/dist/src/connection/WebSocket/WebSocketConnector.js.map +0 -1
- package/dist/src/dht/RemoteExternalApi.js.map +0 -1
- package/dist/src/dht/routing/RemoteRouter.js.map +0 -1
- package/dist/src/dht/store/DataStore.js.map +0 -1
- package/dist/src/dht/store/RemoteStore.js.map +0 -1
- package/src/connection/RemoteConnectionLocker.ts +0 -85
- package/src/connection/WebRTC/RemoteWebrtcConnector.ts +0 -93
|
@@ -8,13 +8,13 @@ import { Empty } from '../../proto/google/protobuf/empty'
|
|
|
8
8
|
import { ITransport } from '../../transport/ITransport'
|
|
9
9
|
import { ListeningRpcCommunicator } from '../../transport/ListeningRpcCommunicator'
|
|
10
10
|
import { NodeWebRtcConnection } from './NodeWebRtcConnection'
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
11
|
+
import { WebRtcConnectorRpcRemote } from './WebRtcConnectorRpcRemote'
|
|
12
|
+
import { WebRtcConnectorRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
13
13
|
import { PeerIDKey } from '../../helpers/PeerID'
|
|
14
14
|
import { ManagedWebRtcConnection } from '../ManagedWebRtcConnection'
|
|
15
15
|
import { Logger } from '@streamr/utils'
|
|
16
16
|
import * as Err from '../../helpers/errors'
|
|
17
|
-
import {
|
|
17
|
+
import { IWebRtcConnectorRpc } from '../../proto/packages/dht/protos/DhtRpc.server'
|
|
18
18
|
import { ManagedConnection } from '../ManagedConnection'
|
|
19
19
|
import { toProtoRpcClient } from '@streamr/proto-rpc'
|
|
20
20
|
import {
|
|
@@ -24,6 +24,8 @@ import {
|
|
|
24
24
|
} from '../../helpers/peerIdFromPeerDescriptor'
|
|
25
25
|
import { getAddressFromIceCandidate, isPrivateIPv4 } from '../../helpers/AddressTools'
|
|
26
26
|
import { PortRange } from '../ConnectionManager'
|
|
27
|
+
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
28
|
+
import { DhtCallContext } from '../../rpc-protocol/DhtCallContext'
|
|
27
29
|
|
|
28
30
|
const logger = new Logger(module)
|
|
29
31
|
|
|
@@ -36,9 +38,8 @@ export const replaceInternalIpWithExternalIp = (candidate: string, ip: string):
|
|
|
36
38
|
return parsed.join(' ')
|
|
37
39
|
}
|
|
38
40
|
|
|
39
|
-
export interface
|
|
41
|
+
export interface WebRtcConnectorRpcLocalConfig {
|
|
40
42
|
transport: ITransport
|
|
41
|
-
protocolVersion: string
|
|
42
43
|
iceServers?: IceServer[]
|
|
43
44
|
allowPrivateAddresses?: boolean
|
|
44
45
|
bufferThresholdLow?: number
|
|
@@ -57,7 +58,8 @@ export interface IceServer {
|
|
|
57
58
|
tcp?: boolean
|
|
58
59
|
}
|
|
59
60
|
|
|
60
|
-
export class
|
|
61
|
+
export class WebRtcConnectorRpcLocal implements IWebRtcConnectorRpc {
|
|
62
|
+
|
|
61
63
|
private static readonly WEBRTC_CONNECTOR_SERVICE_ID = 'system/webrtc-connector'
|
|
62
64
|
private readonly rpcCommunicator: ListeningRpcCommunicator
|
|
63
65
|
private readonly ongoingConnectAttempts: Map<PeerIDKey, ManagedWebRtcConnection> = new Map()
|
|
@@ -65,11 +67,11 @@ export class WebRtcConnector implements IWebRtcConnectorService {
|
|
|
65
67
|
private stopped = false
|
|
66
68
|
private iceServers: IceServer[]
|
|
67
69
|
private allowPrivateAddresses: boolean
|
|
68
|
-
private config:
|
|
70
|
+
private config: WebRtcConnectorRpcLocalConfig
|
|
69
71
|
private onIncomingConnection: (connection: ManagedConnection) => boolean
|
|
70
72
|
|
|
71
73
|
constructor(
|
|
72
|
-
config:
|
|
74
|
+
config: WebRtcConnectorRpcLocalConfig,
|
|
73
75
|
onIncomingConnection: (connection: ManagedConnection) => boolean
|
|
74
76
|
) {
|
|
75
77
|
this.config = config
|
|
@@ -77,17 +79,17 @@ export class WebRtcConnector implements IWebRtcConnectorService {
|
|
|
77
79
|
this.allowPrivateAddresses = config.allowPrivateAddresses || true
|
|
78
80
|
this.onIncomingConnection = onIncomingConnection
|
|
79
81
|
|
|
80
|
-
this.rpcCommunicator = new ListeningRpcCommunicator(
|
|
82
|
+
this.rpcCommunicator = new ListeningRpcCommunicator(WebRtcConnectorRpcLocal.WEBRTC_CONNECTOR_SERVICE_ID, config.transport, {
|
|
81
83
|
rpcRequestTimeout: 15000
|
|
82
84
|
})
|
|
83
85
|
this.rpcCommunicator.registerRpcNotification(RtcOffer, 'rtcOffer',
|
|
84
|
-
(req: RtcOffer) => this.rtcOffer(req))
|
|
86
|
+
(req: RtcOffer, context: ServerCallContext) => this.rtcOffer(req, context))
|
|
85
87
|
this.rpcCommunicator.registerRpcNotification(RtcAnswer, 'rtcAnswer',
|
|
86
|
-
(req: RtcAnswer) => this.rtcAnswer(req))
|
|
88
|
+
(req: RtcAnswer, context: ServerCallContext) => this.rtcAnswer(req, context))
|
|
87
89
|
this.rpcCommunicator.registerRpcNotification(IceCandidate, 'iceCandidate',
|
|
88
|
-
(req: IceCandidate) => this.iceCandidate(req))
|
|
90
|
+
(req: IceCandidate, context: ServerCallContext) => this.iceCandidate(req, context))
|
|
89
91
|
this.rpcCommunicator.registerRpcNotification(WebRtcConnectionRequest, 'requestConnection',
|
|
90
|
-
(req: WebRtcConnectionRequest) => this.requestConnection(req))
|
|
92
|
+
(req: WebRtcConnectionRequest, context: ServerCallContext) => this.requestConnection(req, context))
|
|
91
93
|
}
|
|
92
94
|
|
|
93
95
|
connect(targetPeerDescriptor: PeerDescriptor): ManagedConnection {
|
|
@@ -116,9 +118,9 @@ export class WebRtcConnector implements IWebRtcConnectorService {
|
|
|
116
118
|
let managedConnection: ManagedWebRtcConnection
|
|
117
119
|
|
|
118
120
|
if (offering) {
|
|
119
|
-
managedConnection = new ManagedWebRtcConnection(this.ownPeerDescriptor!,
|
|
121
|
+
managedConnection = new ManagedWebRtcConnection(this.ownPeerDescriptor!, connection)
|
|
120
122
|
} else {
|
|
121
|
-
managedConnection = new ManagedWebRtcConnection(this.ownPeerDescriptor!,
|
|
123
|
+
managedConnection = new ManagedWebRtcConnection(this.ownPeerDescriptor!, undefined, connection)
|
|
122
124
|
}
|
|
123
125
|
|
|
124
126
|
managedConnection.setPeerDescriptor(targetPeerDescriptor)
|
|
@@ -135,9 +137,10 @@ export class WebRtcConnector implements IWebRtcConnectorService {
|
|
|
135
137
|
connection.on('disconnected', delFunc)
|
|
136
138
|
managedConnection.on('handshakeCompleted', delFunc)
|
|
137
139
|
|
|
138
|
-
const remoteConnector = new
|
|
140
|
+
const remoteConnector = new WebRtcConnectorRpcRemote(
|
|
141
|
+
this.ownPeerDescriptor!,
|
|
139
142
|
targetPeerDescriptor,
|
|
140
|
-
toProtoRpcClient(new
|
|
143
|
+
toProtoRpcClient(new WebRtcConnectorRpcClient(this.rpcCommunicator.getRpcClientTransport()))
|
|
141
144
|
)
|
|
142
145
|
|
|
143
146
|
connection.on('localCandidate', (candidate: string, mid: string) => {
|
|
@@ -145,23 +148,23 @@ export class WebRtcConnector implements IWebRtcConnectorService {
|
|
|
145
148
|
candidate = replaceInternalIpWithExternalIp(candidate, this.config.externalIp)
|
|
146
149
|
logger.debug(`onLocalCandidate injected external ip ${candidate} ${mid}`)
|
|
147
150
|
}
|
|
148
|
-
remoteConnector.sendIceCandidate(
|
|
151
|
+
remoteConnector.sendIceCandidate(candidate, mid, connection.connectionId.toString())
|
|
149
152
|
})
|
|
150
153
|
|
|
151
154
|
if (offering) {
|
|
152
155
|
connection.once('localDescription', (description: string) => {
|
|
153
|
-
remoteConnector.sendRtcOffer(
|
|
156
|
+
remoteConnector.sendRtcOffer(description, connection.connectionId.toString())
|
|
154
157
|
})
|
|
155
158
|
} else {
|
|
156
159
|
connection.once('localDescription', (description: string) => {
|
|
157
|
-
remoteConnector.sendRtcAnswer(
|
|
160
|
+
remoteConnector.sendRtcAnswer(description, connection.connectionId.toString())
|
|
158
161
|
})
|
|
159
162
|
}
|
|
160
163
|
|
|
161
164
|
connection.start(offering)
|
|
162
165
|
|
|
163
166
|
if (!offering) {
|
|
164
|
-
remoteConnector.requestConnection(
|
|
167
|
+
remoteConnector.requestConnection(connection.connectionId.toString())
|
|
165
168
|
}
|
|
166
169
|
|
|
167
170
|
return managedConnection
|
|
@@ -196,24 +199,25 @@ export class WebRtcConnector implements IWebRtcConnectorService {
|
|
|
196
199
|
|
|
197
200
|
if (!managedConnection) {
|
|
198
201
|
connection = new NodeWebRtcConnection({ remotePeerDescriptor: remotePeer })
|
|
199
|
-
managedConnection = new ManagedWebRtcConnection(this.ownPeerDescriptor!,
|
|
202
|
+
managedConnection = new ManagedWebRtcConnection(this.ownPeerDescriptor!, undefined, connection)
|
|
200
203
|
|
|
201
204
|
managedConnection.setPeerDescriptor(remotePeer)
|
|
202
205
|
|
|
203
206
|
this.ongoingConnectAttempts.set(peerKey, managedConnection)
|
|
204
207
|
this.onIncomingConnection(managedConnection)
|
|
205
208
|
|
|
206
|
-
const remoteConnector = new
|
|
209
|
+
const remoteConnector = new WebRtcConnectorRpcRemote(
|
|
210
|
+
this.ownPeerDescriptor!,
|
|
207
211
|
remotePeer,
|
|
208
|
-
toProtoRpcClient(new
|
|
212
|
+
toProtoRpcClient(new WebRtcConnectorRpcClient(this.rpcCommunicator.getRpcClientTransport()))
|
|
209
213
|
)
|
|
210
214
|
|
|
211
215
|
connection.on('localCandidate', (candidate: string, mid: string) => {
|
|
212
|
-
remoteConnector.sendIceCandidate(
|
|
216
|
+
remoteConnector.sendIceCandidate(candidate, mid, connection!.connectionId.toString())
|
|
213
217
|
})
|
|
214
218
|
|
|
215
219
|
connection.once('localDescription', (description: string) => {
|
|
216
|
-
remoteConnector.sendRtcAnswer(
|
|
220
|
+
remoteConnector.sendRtcAnswer(description, connection!.connectionId.toString())
|
|
217
221
|
})
|
|
218
222
|
|
|
219
223
|
connection.start(false)
|
|
@@ -301,23 +305,31 @@ export class WebRtcConnector implements IWebRtcConnectorService {
|
|
|
301
305
|
}
|
|
302
306
|
|
|
303
307
|
// IWebRTCConnector implementation
|
|
304
|
-
|
|
305
|
-
|
|
308
|
+
// TODO should we read connectionId from WebRtcConnectionRequest (or remove the field)?
|
|
309
|
+
async requestConnection(_request: WebRtcConnectionRequest, context: ServerCallContext): Promise<Empty> {
|
|
310
|
+
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
311
|
+
this.onConnectionRequest(senderPeerDescriptor)
|
|
306
312
|
return {}
|
|
307
313
|
}
|
|
308
314
|
|
|
309
|
-
async rtcOffer(request: RtcOffer): Promise<Empty> {
|
|
310
|
-
|
|
315
|
+
async rtcOffer(request: RtcOffer, context: ServerCallContext): Promise<Empty> {
|
|
316
|
+
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
317
|
+
const receiverPeerDescriptor = (context as DhtCallContext).incomingTargetDescriptor!
|
|
318
|
+
this.onRtcOffer(senderPeerDescriptor, receiverPeerDescriptor, request.description, request.connectionId)
|
|
311
319
|
return {}
|
|
312
320
|
}
|
|
313
321
|
|
|
314
|
-
async rtcAnswer(request: RtcAnswer): Promise<Empty> {
|
|
315
|
-
|
|
322
|
+
async rtcAnswer(request: RtcAnswer, context: ServerCallContext): Promise<Empty> {
|
|
323
|
+
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
324
|
+
const receiverPeerDescriptor = (context as DhtCallContext).incomingTargetDescriptor!
|
|
325
|
+
this.onRtcAnswer(senderPeerDescriptor, receiverPeerDescriptor, request.description, request.connectionId)
|
|
316
326
|
return {}
|
|
317
327
|
}
|
|
318
328
|
|
|
319
|
-
async iceCandidate(request: IceCandidate): Promise<Empty> {
|
|
320
|
-
|
|
329
|
+
async iceCandidate(request: IceCandidate, context: ServerCallContext): Promise<Empty> {
|
|
330
|
+
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
331
|
+
const receiverPeerDescriptor = (context as DhtCallContext).incomingTargetDescriptor!
|
|
332
|
+
this.onRemoteCandidate(senderPeerDescriptor, receiverPeerDescriptor, request.candidate, request.mid, request.connectionId)
|
|
321
333
|
return {}
|
|
322
334
|
}
|
|
323
335
|
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Remote } from '../../dht/contact/Remote'
|
|
2
|
+
import {
|
|
3
|
+
IceCandidate,
|
|
4
|
+
PeerDescriptor,
|
|
5
|
+
RtcAnswer,
|
|
6
|
+
RtcOffer,
|
|
7
|
+
WebRtcConnectionRequest
|
|
8
|
+
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
9
|
+
import { IWebRtcConnectorRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
10
|
+
import { ProtoRpcClient } from '@streamr/proto-rpc'
|
|
11
|
+
import { Logger } from '@streamr/utils'
|
|
12
|
+
|
|
13
|
+
const logger = new Logger(module)
|
|
14
|
+
|
|
15
|
+
export class WebRtcConnectorRpcRemote extends Remote<IWebRtcConnectorRpcClient> {
|
|
16
|
+
|
|
17
|
+
constructor(
|
|
18
|
+
localPeerDescriptor: PeerDescriptor,
|
|
19
|
+
remotePeerDescriptor: PeerDescriptor,
|
|
20
|
+
client: ProtoRpcClient<IWebRtcConnectorRpcClient>
|
|
21
|
+
) {
|
|
22
|
+
super(localPeerDescriptor, remotePeerDescriptor, 'DUMMY', client)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
requestConnection(connectionId: string): void {
|
|
26
|
+
const request: WebRtcConnectionRequest = {
|
|
27
|
+
connectionId
|
|
28
|
+
}
|
|
29
|
+
const options = this.formDhtRpcOptions({
|
|
30
|
+
notification: true
|
|
31
|
+
})
|
|
32
|
+
this.getClient().requestConnection(request, options).catch((_e) => {
|
|
33
|
+
logger.trace('Failed to send requestConnection')
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
sendRtcOffer(description: string, connectionId: string): void {
|
|
38
|
+
const request: RtcOffer = {
|
|
39
|
+
connectionId,
|
|
40
|
+
description
|
|
41
|
+
}
|
|
42
|
+
const options = this.formDhtRpcOptions()
|
|
43
|
+
this.getClient().rtcOffer(request, options).catch((_e) => {
|
|
44
|
+
logger.trace('Failed to send rtcOffer')
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
sendRtcAnswer(description: string, connectionId: string): void {
|
|
49
|
+
const request: RtcAnswer = {
|
|
50
|
+
connectionId,
|
|
51
|
+
description
|
|
52
|
+
}
|
|
53
|
+
const options = this.formDhtRpcOptions()
|
|
54
|
+
this.getClient().rtcAnswer(request, options).catch((_e) => {
|
|
55
|
+
logger.trace('Failed to send rtcAnswer')
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
sendIceCandidate(candidate: string, mid: string, connectionId: string): void {
|
|
60
|
+
const request: IceCandidate = {
|
|
61
|
+
connectionId,
|
|
62
|
+
mid,
|
|
63
|
+
candidate
|
|
64
|
+
}
|
|
65
|
+
const options = this.formDhtRpcOptions()
|
|
66
|
+
this.getClient().iceCandidate(request, options).catch((_e) => {
|
|
67
|
+
logger.trace('Failed to send iceCandidate')
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
@@ -2,7 +2,7 @@ import { ClientWebSocket } from './ClientWebSocket'
|
|
|
2
2
|
import { IConnection, ConnectionType } from '../IConnection'
|
|
3
3
|
import { ITransport } from '../../transport/ITransport'
|
|
4
4
|
import { ListeningRpcCommunicator } from '../../transport/ListeningRpcCommunicator'
|
|
5
|
-
import {
|
|
5
|
+
import { WebSocketConnectorRpcRemote } from './WebSocketConnectorRpcRemote'
|
|
6
6
|
import {
|
|
7
7
|
ConnectivityMethod,
|
|
8
8
|
ConnectivityResponse,
|
|
@@ -11,9 +11,9 @@ import {
|
|
|
11
11
|
WebSocketConnectionRequest,
|
|
12
12
|
WebSocketConnectionResponse
|
|
13
13
|
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
14
|
-
import {
|
|
14
|
+
import { WebSocketConnectorRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
15
15
|
import { Logger, binaryToHex, wait } from '@streamr/utils'
|
|
16
|
-
import {
|
|
16
|
+
import { IWebSocketConnectorRpc } from '../../proto/packages/dht/protos/DhtRpc.server'
|
|
17
17
|
import { ManagedConnection } from '../ManagedConnection'
|
|
18
18
|
import { WebSocketServer } from './WebSocketServer'
|
|
19
19
|
import { ConnectivityChecker } from '../ConnectivityChecker'
|
|
@@ -26,6 +26,8 @@ import { keyFromPeerDescriptor, peerIdFromPeerDescriptor } from '../../helpers/p
|
|
|
26
26
|
import { ParsedUrlQuery } from 'querystring'
|
|
27
27
|
import { range, sample } from 'lodash'
|
|
28
28
|
import { isPrivateIPv4 } from '../../helpers/AddressTools'
|
|
29
|
+
import { ServerCallContext } from '@protobuf-ts/runtime-rpc'
|
|
30
|
+
import { DhtCallContext } from '../../rpc-protocol/DhtCallContext'
|
|
29
31
|
|
|
30
32
|
const logger = new Logger(module)
|
|
31
33
|
|
|
@@ -40,8 +42,7 @@ const canOpenConnectionFromBrowser = (websocketServer: ConnectivityMethod) => {
|
|
|
40
42
|
|
|
41
43
|
const ENTRY_POINT_CONNECTION_ATTEMPTS = 5
|
|
42
44
|
|
|
43
|
-
interface
|
|
44
|
-
protocolVersion: string
|
|
45
|
+
interface WebSocketConnectorRpcLocalConfig {
|
|
45
46
|
transport: ITransport
|
|
46
47
|
canConnect: (peerDescriptor: PeerDescriptor, _ip: string, port: number) => boolean
|
|
47
48
|
onIncomingConnection: (connection: ManagedConnection) => boolean
|
|
@@ -52,7 +53,8 @@ interface WebSocketConnectorConfig {
|
|
|
52
53
|
tlsCertificate?: TlsCertificate
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
export class
|
|
56
|
+
export class WebSocketConnectorRpcLocal implements IWebSocketConnectorRpc {
|
|
57
|
+
|
|
56
58
|
private static readonly WEBSOCKET_CONNECTOR_SERVICE_ID = 'system/websocket-connector'
|
|
57
59
|
private readonly rpcCommunicator: ListeningRpcCommunicator
|
|
58
60
|
private readonly canConnectFunction: (peerDescriptor: PeerDescriptor, _ip: string, port: number) => boolean
|
|
@@ -64,13 +66,11 @@ export class WebSocketConnector implements IWebSocketConnectorService {
|
|
|
64
66
|
private readonly entrypoints?: PeerDescriptor[]
|
|
65
67
|
private readonly tlsCertificate?: TlsCertificate
|
|
66
68
|
private selectedPort?: number
|
|
67
|
-
private readonly protocolVersion: string
|
|
68
69
|
private ownPeerDescriptor?: PeerDescriptor
|
|
69
70
|
private connectingConnections: Map<PeerIDKey, ManagedConnection> = new Map()
|
|
70
71
|
private destroyed = false
|
|
71
72
|
|
|
72
|
-
constructor(config:
|
|
73
|
-
this.protocolVersion = config.protocolVersion
|
|
73
|
+
constructor(config: WebSocketConnectorRpcLocalConfig) {
|
|
74
74
|
this.webSocketServer = config.portRange ? new WebSocketServer({
|
|
75
75
|
portRange: config.portRange!,
|
|
76
76
|
tlsCertificate: config.tlsCertificate,
|
|
@@ -83,7 +83,7 @@ export class WebSocketConnector implements IWebSocketConnectorService {
|
|
|
83
83
|
|
|
84
84
|
this.canConnectFunction = config.canConnect.bind(this)
|
|
85
85
|
|
|
86
|
-
this.rpcCommunicator = new ListeningRpcCommunicator(
|
|
86
|
+
this.rpcCommunicator = new ListeningRpcCommunicator(WebSocketConnectorRpcLocal.WEBSOCKET_CONNECTOR_SERVICE_ID, config.transport, {
|
|
87
87
|
rpcRequestTimeout: 15000
|
|
88
88
|
})
|
|
89
89
|
|
|
@@ -91,12 +91,12 @@ export class WebSocketConnector implements IWebSocketConnectorService {
|
|
|
91
91
|
WebSocketConnectionRequest,
|
|
92
92
|
WebSocketConnectionResponse,
|
|
93
93
|
'requestConnection',
|
|
94
|
-
(req: WebSocketConnectionRequest) => this.requestConnection(req)
|
|
94
|
+
(req: WebSocketConnectionRequest, context: ServerCallContext) => this.requestConnection(req, context)
|
|
95
95
|
)
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
private attachHandshaker(connection: IConnection) {
|
|
99
|
-
const handshaker = new Handshaker(this.ownPeerDescriptor!,
|
|
99
|
+
const handshaker = new Handshaker(this.ownPeerDescriptor!, connection)
|
|
100
100
|
handshaker.once('handshakeRequest', (peerDescriptor: PeerDescriptor) => {
|
|
101
101
|
this.onServerSocketHandshakeRequest(peerDescriptor, connection)
|
|
102
102
|
})
|
|
@@ -155,13 +155,13 @@ export class WebSocketConnector implements IWebSocketConnectorService {
|
|
|
155
155
|
}
|
|
156
156
|
return preconfiguredConnectivityResponse
|
|
157
157
|
} else {
|
|
158
|
-
// Do real connectivity checking
|
|
158
|
+
// Do real connectivity checking
|
|
159
159
|
return await this.connectivityChecker!.sendConnectivityRequest(entryPoint)
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
} catch (err) {
|
|
163
163
|
if (reattempt < ENTRY_POINT_CONNECTION_ATTEMPTS) {
|
|
164
|
-
const error = `Failed to connect to entrypoint with id ${binaryToHex(entryPoint.kademliaId)} `
|
|
164
|
+
const error = `Failed to connect to entrypoint with id ${binaryToHex(entryPoint.kademliaId)} `
|
|
165
165
|
+ `and URL ${connectivityMethodToWebSocketUrl(entryPoint.websocket!)}`
|
|
166
166
|
logger.error(error, { error: err })
|
|
167
167
|
await wait(2000)
|
|
@@ -195,8 +195,7 @@ export class WebSocketConnector implements IWebSocketConnectorService {
|
|
|
195
195
|
|
|
196
196
|
const url = connectivityMethodToWebSocketUrl(targetPeerDescriptor.websocket!)
|
|
197
197
|
|
|
198
|
-
const managedConnection = new ManagedConnection(this.ownPeerDescriptor!,
|
|
199
|
-
ConnectionType.WEBSOCKET_CLIENT, socket, undefined)
|
|
198
|
+
const managedConnection = new ManagedConnection(this.ownPeerDescriptor!, ConnectionType.WEBSOCKET_CLIENT, socket, undefined)
|
|
200
199
|
managedConnection.setPeerDescriptor(targetPeerDescriptor)
|
|
201
200
|
|
|
202
201
|
this.connectingConnections.set(keyFromPeerDescriptor(targetPeerDescriptor), managedConnection)
|
|
@@ -219,13 +218,14 @@ export class WebSocketConnector implements IWebSocketConnectorService {
|
|
|
219
218
|
|
|
220
219
|
private requestConnectionFromPeer(ownPeerDescriptor: PeerDescriptor, targetPeerDescriptor: PeerDescriptor): ManagedConnection {
|
|
221
220
|
setImmediate(() => {
|
|
222
|
-
const remoteConnector = new
|
|
221
|
+
const remoteConnector = new WebSocketConnectorRpcRemote(
|
|
222
|
+
ownPeerDescriptor,
|
|
223
223
|
targetPeerDescriptor,
|
|
224
|
-
toProtoRpcClient(new
|
|
224
|
+
toProtoRpcClient(new WebSocketConnectorRpcClient(this.rpcCommunicator.getRpcClientTransport()))
|
|
225
225
|
)
|
|
226
|
-
remoteConnector.requestConnection(ownPeerDescriptor
|
|
226
|
+
remoteConnector.requestConnection(ownPeerDescriptor.websocket!.host, ownPeerDescriptor.websocket!.port)
|
|
227
227
|
})
|
|
228
|
-
const managedConnection = new ManagedConnection(this.ownPeerDescriptor!,
|
|
228
|
+
const managedConnection = new ManagedConnection(this.ownPeerDescriptor!, ConnectionType.WEBSOCKET_SERVER)
|
|
229
229
|
managedConnection.on('disconnected', () => this.ongoingConnectRequests.delete(keyFromPeerDescriptor(targetPeerDescriptor)))
|
|
230
230
|
managedConnection.setPeerDescriptor(targetPeerDescriptor)
|
|
231
231
|
this.ongoingConnectRequests.set(keyFromPeerDescriptor(targetPeerDescriptor), managedConnection)
|
|
@@ -242,8 +242,7 @@ export class WebSocketConnector implements IWebSocketConnectorService {
|
|
|
242
242
|
ongoingConnectReguest.acceptHandshake()
|
|
243
243
|
this.ongoingConnectRequests.delete(peerId.toKey())
|
|
244
244
|
} else {
|
|
245
|
-
const managedConnection = new ManagedConnection(this.ownPeerDescriptor!,
|
|
246
|
-
ConnectionType.WEBSOCKET_SERVER, undefined, serverWebSocket)
|
|
245
|
+
const managedConnection = new ManagedConnection(this.ownPeerDescriptor!, ConnectionType.WEBSOCKET_SERVER, undefined, serverWebSocket)
|
|
247
246
|
|
|
248
247
|
managedConnection.setPeerDescriptor(peerDescriptor)
|
|
249
248
|
|
|
@@ -273,14 +272,15 @@ export class WebSocketConnector implements IWebSocketConnectorService {
|
|
|
273
272
|
await this.webSocketServer?.stop()
|
|
274
273
|
}
|
|
275
274
|
|
|
276
|
-
//
|
|
277
|
-
public async requestConnection(request: WebSocketConnectionRequest): Promise<WebSocketConnectionResponse> {
|
|
278
|
-
|
|
275
|
+
// IWebSocketConnectorRpc implementation
|
|
276
|
+
public async requestConnection(request: WebSocketConnectionRequest, context: ServerCallContext): Promise<WebSocketConnectionResponse> {
|
|
277
|
+
const senderPeerDescriptor = (context as DhtCallContext).incomingSourceDescriptor!
|
|
278
|
+
if (!this.destroyed && this.canConnectFunction(senderPeerDescriptor, request.ip, request.port)) {
|
|
279
279
|
setImmediate(() => {
|
|
280
280
|
if (this.destroyed) {
|
|
281
281
|
return
|
|
282
282
|
}
|
|
283
|
-
const connection = this.connect(
|
|
283
|
+
const connection = this.connect(senderPeerDescriptor)
|
|
284
284
|
this.onIncomingConnection(connection)
|
|
285
285
|
})
|
|
286
286
|
const res: WebSocketConnectionResponse = {
|
package/src/connection/WebSocket/{RemoteWebSocketConnector.ts → WebSocketConnectorRpcRemote.ts}
RENAMED
|
@@ -2,39 +2,34 @@ import {
|
|
|
2
2
|
PeerDescriptor,
|
|
3
3
|
WebSocketConnectionRequest
|
|
4
4
|
} from '../../proto/packages/dht/protos/DhtRpc'
|
|
5
|
-
import {
|
|
6
|
-
import { DhtRpcOptions } from '../../rpc-protocol/DhtRpcOptions'
|
|
5
|
+
import { IWebSocketConnectorRpcClient } from '../../proto/packages/dht/protos/DhtRpc.client'
|
|
7
6
|
import { Logger } from '@streamr/utils'
|
|
8
7
|
import * as Err from '../../helpers/errors'
|
|
9
8
|
import { ProtoRpcClient } from '@streamr/proto-rpc'
|
|
10
9
|
import { keyFromPeerDescriptor } from '../../helpers/peerIdFromPeerDescriptor'
|
|
10
|
+
import { Remote } from '../../dht/contact/Remote'
|
|
11
11
|
|
|
12
12
|
const logger = new Logger(module)
|
|
13
13
|
|
|
14
|
-
export class
|
|
14
|
+
export class WebSocketConnectorRpcRemote extends Remote<IWebSocketConnectorRpcClient> {
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
constructor(
|
|
17
|
+
localPeerDescriptor: PeerDescriptor,
|
|
18
|
+
remotePeerDescriptor: PeerDescriptor,
|
|
19
|
+
client: ProtoRpcClient<IWebSocketConnectorRpcClient>
|
|
20
|
+
) {
|
|
21
|
+
super(localPeerDescriptor, remotePeerDescriptor, 'DUMMY', client)
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
async requestConnection(
|
|
25
|
-
logger.trace(`Requesting WebSocket connection from ${keyFromPeerDescriptor(this.
|
|
24
|
+
async requestConnection(ip: string, port: number): Promise<boolean> {
|
|
25
|
+
logger.trace(`Requesting WebSocket connection from ${keyFromPeerDescriptor(this.getLocalPeerDescriptor())}`)
|
|
26
26
|
const request: WebSocketConnectionRequest = {
|
|
27
|
-
target: this.peerDescriptor,
|
|
28
|
-
requester: sourceDescriptor,
|
|
29
27
|
ip,
|
|
30
28
|
port
|
|
31
29
|
}
|
|
32
|
-
const options
|
|
33
|
-
sourceDescriptor: sourceDescriptor,
|
|
34
|
-
targetDescriptor: this.peerDescriptor
|
|
35
|
-
}
|
|
30
|
+
const options = this.formDhtRpcOptions()
|
|
36
31
|
try {
|
|
37
|
-
const res = await this.
|
|
32
|
+
const res = await this.getClient().requestConnection(request, options)
|
|
38
33
|
|
|
39
34
|
if (res.reason) {
|
|
40
35
|
logger.debug('WebSocketConnectionRequest Rejected', {
|