diodejs 0.0.5 → 0.1.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/bindPort.js +18 -12
- package/connection.js +38 -0
- package/examples/publishAndBind.js +23 -0
- package/package.json +1 -1
- package/publishPort.js +25 -18
package/bindPort.js
CHANGED
|
@@ -13,8 +13,7 @@ class BindPort {
|
|
|
13
13
|
|
|
14
14
|
bind () {
|
|
15
15
|
const deviceId = Buffer.from(this.deviceIdHex, 'hex');
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
// Remove local clientSockets map and use the one from connection
|
|
18
17
|
const rpc = new DiodeRPC(this.connection);
|
|
19
18
|
|
|
20
19
|
// Listen for data events from the device
|
|
@@ -31,12 +30,17 @@ class BindPort {
|
|
|
31
30
|
const dataRef = Buffer.from(refRaw);
|
|
32
31
|
const data = Buffer.from(dataRaw);
|
|
33
32
|
|
|
34
|
-
// Find the associated client socket
|
|
35
|
-
const clientSocket =
|
|
33
|
+
// Find the associated client socket from connection
|
|
34
|
+
const clientSocket = this.connection.getClientSocket(dataRef);
|
|
36
35
|
if (clientSocket) {
|
|
37
36
|
clientSocket.write(data);
|
|
38
37
|
} else {
|
|
39
|
-
|
|
38
|
+
const connectionInfo = this.connection.getConnection(dataRef);
|
|
39
|
+
if (connectionInfo) {
|
|
40
|
+
logger.debug(`No client socket found for ref: ${dataRef.toString('hex')}, but connection exists for ${connectionInfo.host}:${connectionInfo.port}`);
|
|
41
|
+
} else {
|
|
42
|
+
logger.warn(`No client socket found for ref: ${dataRef.toString('hex')}`);
|
|
43
|
+
}
|
|
40
44
|
}
|
|
41
45
|
} else if (messageType === 'portclose') {
|
|
42
46
|
const refRaw = messageContent[1];
|
|
@@ -44,14 +48,16 @@ class BindPort {
|
|
|
44
48
|
const dataRef = Buffer.from(refRaw);
|
|
45
49
|
|
|
46
50
|
// Close the associated client socket
|
|
47
|
-
const clientSocket =
|
|
51
|
+
const clientSocket = this.connection.getClientSocket(dataRef);
|
|
48
52
|
if (clientSocket) {
|
|
49
53
|
clientSocket.end();
|
|
50
|
-
|
|
54
|
+
this.connection.deleteClientSocket(dataRef);
|
|
51
55
|
logger.info(`Port closed for ref: ${dataRef.toString('hex')}`);
|
|
52
56
|
}
|
|
53
57
|
} else {
|
|
54
|
-
|
|
58
|
+
if (messageType != 'portopen') {
|
|
59
|
+
logger.warn(`Unknown unsolicited message type: ${messageType}`);
|
|
60
|
+
}
|
|
55
61
|
}
|
|
56
62
|
});
|
|
57
63
|
|
|
@@ -76,8 +82,8 @@ class BindPort {
|
|
|
76
82
|
return;
|
|
77
83
|
}
|
|
78
84
|
|
|
79
|
-
// Store the client socket with the ref
|
|
80
|
-
|
|
85
|
+
// Store the client socket with the ref using connection's method
|
|
86
|
+
this.connection.addClientSocket(ref, clientSocket);
|
|
81
87
|
|
|
82
88
|
// When data is received from the client, send it to the device
|
|
83
89
|
clientSocket.on('data', async (data) => {
|
|
@@ -92,11 +98,11 @@ class BindPort {
|
|
|
92
98
|
// Handle client socket closure
|
|
93
99
|
clientSocket.on('end', async () => {
|
|
94
100
|
logger.info('Client disconnected');
|
|
95
|
-
if (ref &&
|
|
101
|
+
if (ref && this.connection.hasClientSocket(ref)) {
|
|
96
102
|
try {
|
|
97
103
|
await rpc.portClose(ref);
|
|
98
104
|
logger.info(`Port closed on device for ref: ${ref.toString('hex')}`);
|
|
99
|
-
|
|
105
|
+
this.connection.deleteClientSocket(ref);
|
|
100
106
|
} catch (error) {
|
|
101
107
|
logger.error(`Error closing port on device: ${error}`);
|
|
102
108
|
}
|
package/connection.js
CHANGED
|
@@ -28,6 +28,10 @@ class DiodeConnection extends EventEmitter {
|
|
|
28
28
|
this.RPC = new DiodeRPC(this);
|
|
29
29
|
this.isReconnecting = false;
|
|
30
30
|
this.connectPromise = null;
|
|
31
|
+
|
|
32
|
+
// Add maps for storing client sockets and connections
|
|
33
|
+
this.clientSockets = new Map(); // For BindPort
|
|
34
|
+
this.connections = new Map(); // For PublishPort
|
|
31
35
|
|
|
32
36
|
// Check if certPath exists, if not generate the certificate
|
|
33
37
|
if (!fs.existsSync(this.certPath)) {
|
|
@@ -562,6 +566,40 @@ class DiodeConnection extends EventEmitter {
|
|
|
562
566
|
close() {
|
|
563
567
|
this.socket.end();
|
|
564
568
|
}
|
|
569
|
+
|
|
570
|
+
// Client sockets management methods (for BindPort)
|
|
571
|
+
addClientSocket(ref, socket) {
|
|
572
|
+
this.clientSockets.set(ref.toString('hex'), socket);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
getClientSocket(ref) {
|
|
576
|
+
return this.clientSockets.get(ref.toString('hex'));
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
deleteClientSocket(ref) {
|
|
580
|
+
return this.clientSockets.delete(ref.toString('hex'));
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
hasClientSocket(ref) {
|
|
584
|
+
return this.clientSockets.has(ref.toString('hex'));
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// Connections management methods (for PublishPort)
|
|
588
|
+
addConnection(ref, connectionInfo) {
|
|
589
|
+
this.connections.set(ref.toString('hex'), connectionInfo);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
getConnection(ref) {
|
|
593
|
+
return this.connections.get(ref.toString('hex'));
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
deleteConnection(ref) {
|
|
597
|
+
return this.connections.delete(ref.toString('hex'));
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
hasConnection(ref) {
|
|
601
|
+
return this.connections.has(ref.toString('hex'));
|
|
602
|
+
}
|
|
565
603
|
}
|
|
566
604
|
|
|
567
605
|
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const DiodeConnection = require('../connection')
|
|
2
|
+
const PublishPort = require('../publishPort')
|
|
3
|
+
const BindPort = require('../bindPort')
|
|
4
|
+
|
|
5
|
+
const host = 'us2.prenet.diode.io';
|
|
6
|
+
const port = 41046;
|
|
7
|
+
const certPath = 'device_certificate.pem';
|
|
8
|
+
|
|
9
|
+
const connection = new DiodeConnection(host, port, certPath);
|
|
10
|
+
|
|
11
|
+
async function main() {
|
|
12
|
+
await connection.connect();
|
|
13
|
+
const publishedPorts = [8080]; // Ports you want to publish
|
|
14
|
+
const publishPort = new PublishPort(connection, publishedPorts, certPath);
|
|
15
|
+
|
|
16
|
+
const portForward = new BindPort(connection, 3002, 8080, "5365baf29cb7ab58de588dfc448913cb609283e2");
|
|
17
|
+
portForward.bind();
|
|
18
|
+
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
main();
|
|
22
|
+
|
|
23
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "diodejs",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "A JavaScript client for interacting with the Diode network. It provides functionalities to bind and publish ports, send RPC commands, and handle responses.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
package/publishPort.js
CHANGED
|
@@ -40,7 +40,7 @@ class PublishPort extends EventEmitter {
|
|
|
40
40
|
super();
|
|
41
41
|
this.connection = connection;
|
|
42
42
|
this.publishedPorts = new Set(publishedPorts); // Convert array to a Set
|
|
43
|
-
|
|
43
|
+
// Remove local connections map and use the one from connection
|
|
44
44
|
this.startListening();
|
|
45
45
|
this.rpc = new DiodeRPC(connection);
|
|
46
46
|
this.certPath = certPath;
|
|
@@ -60,7 +60,9 @@ class PublishPort extends EventEmitter {
|
|
|
60
60
|
} else if (messageType === 'portclose') {
|
|
61
61
|
this.handlePortClose(sessionIdRaw, messageContent);
|
|
62
62
|
} else {
|
|
63
|
-
|
|
63
|
+
if (messageType != 'data') {
|
|
64
|
+
logger.warn(`Unknown unsolicited message type: ${messageType}`);
|
|
65
|
+
}
|
|
64
66
|
}
|
|
65
67
|
});
|
|
66
68
|
}
|
|
@@ -127,14 +129,14 @@ class PublishPort extends EventEmitter {
|
|
|
127
129
|
logger.info(`Local service disconnected`);
|
|
128
130
|
// Send portclose message to Diode
|
|
129
131
|
this.rpc.portClose(ref);
|
|
130
|
-
this.
|
|
132
|
+
this.connection.deleteConnection(ref);
|
|
131
133
|
});
|
|
132
134
|
|
|
133
135
|
localSocket.on('error', (err) => {
|
|
134
136
|
logger.error(`Error with local service: ${err}`);
|
|
135
137
|
// Send portclose message to Diode
|
|
136
138
|
this.rpc.portClose(ref);
|
|
137
|
-
this.
|
|
139
|
+
this.connection.deleteConnection(ref);
|
|
138
140
|
});
|
|
139
141
|
}
|
|
140
142
|
}
|
|
@@ -150,8 +152,8 @@ class PublishPort extends EventEmitter {
|
|
|
150
152
|
// Handle data, end, and error events
|
|
151
153
|
this.setupLocalSocketHandlers(localSocket, ref, 'tcp');
|
|
152
154
|
|
|
153
|
-
// Store the local socket with the ref
|
|
154
|
-
this.
|
|
155
|
+
// Store the local socket with the ref using connection's method
|
|
156
|
+
this.connection.addConnection(ref, { socket: localSocket, protocol: 'tcp' });
|
|
155
157
|
}
|
|
156
158
|
|
|
157
159
|
handleTLSConnection(sessionId, ref, port) {
|
|
@@ -189,16 +191,16 @@ class PublishPort extends EventEmitter {
|
|
|
189
191
|
tlsSocket.on('error', (err) => {
|
|
190
192
|
logger.error(`TLS Socket error: ${err}`);
|
|
191
193
|
this.rpc.portClose(ref);
|
|
192
|
-
this.
|
|
194
|
+
this.connection.deleteConnection(ref);
|
|
193
195
|
});
|
|
194
196
|
|
|
195
197
|
tlsSocket.on('close', () => {
|
|
196
198
|
console.log('TLS Socket closed');
|
|
197
|
-
this.
|
|
199
|
+
this.connection.deleteConnection(ref);
|
|
198
200
|
});
|
|
199
201
|
|
|
200
|
-
// Store the connection info
|
|
201
|
-
this.
|
|
202
|
+
// Store the connection info using connection's method
|
|
203
|
+
this.connection.addConnection(ref, {
|
|
202
204
|
diodeSocket,
|
|
203
205
|
tlsSocket,
|
|
204
206
|
localSocket,
|
|
@@ -216,8 +218,8 @@ class PublishPort extends EventEmitter {
|
|
|
216
218
|
// Send success response
|
|
217
219
|
this.rpc.sendResponse(sessionId, ref, 'ok');
|
|
218
220
|
|
|
219
|
-
// Store the connection info
|
|
220
|
-
this.
|
|
221
|
+
// Store the connection info using connection's method
|
|
222
|
+
this.connection.addConnection(ref, {
|
|
221
223
|
socket: localSocket,
|
|
222
224
|
protocol: 'udp',
|
|
223
225
|
remoteInfo,
|
|
@@ -238,7 +240,7 @@ class PublishPort extends EventEmitter {
|
|
|
238
240
|
localSocket.on('error', (err) => {
|
|
239
241
|
logger.error(`UDP Socket error: ${err}`);
|
|
240
242
|
this.rpc.portClose(ref);
|
|
241
|
-
this.
|
|
243
|
+
this.connection.deleteConnection(ref);
|
|
242
244
|
});
|
|
243
245
|
}
|
|
244
246
|
|
|
@@ -250,7 +252,7 @@ class PublishPort extends EventEmitter {
|
|
|
250
252
|
const ref = Buffer.from(refRaw);
|
|
251
253
|
const data = Buffer.from(dataRaw)//.slice(4);
|
|
252
254
|
|
|
253
|
-
const connectionInfo = this.
|
|
255
|
+
const connectionInfo = this.connection.getConnection(ref);
|
|
254
256
|
if (connectionInfo) {
|
|
255
257
|
const { socket: localSocket, protocol, remoteInfo } = connectionInfo;
|
|
256
258
|
|
|
@@ -277,8 +279,13 @@ class PublishPort extends EventEmitter {
|
|
|
277
279
|
diodeSocket.pushData(data);
|
|
278
280
|
}
|
|
279
281
|
} else {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
+
const clientSocket = this.connection.getClientSocket(ref);
|
|
283
|
+
if (clientSocket) {
|
|
284
|
+
logger.debug(`No local connection found for ref: ${ref.toString('hex')}, but client socket exists`);
|
|
285
|
+
} else {
|
|
286
|
+
logger.warn(`No local connection found for ref ${ref.toString('hex')}. Sending portclose.`);
|
|
287
|
+
this.rpc.sendError(sessionId, ref, 'No local connection found');
|
|
288
|
+
}
|
|
282
289
|
}
|
|
283
290
|
}
|
|
284
291
|
|
|
@@ -289,7 +296,7 @@ class PublishPort extends EventEmitter {
|
|
|
289
296
|
|
|
290
297
|
logger.info(`Received portclose for ref ${ref.toString('hex')}`);
|
|
291
298
|
|
|
292
|
-
const connectionInfo = this.
|
|
299
|
+
const connectionInfo = this.connection.getConnection(ref);
|
|
293
300
|
if (connectionInfo) {
|
|
294
301
|
const { diodeSocket, tlsSocket, socket: localSocket } = connectionInfo;
|
|
295
302
|
// End all sockets
|
|
@@ -302,7 +309,7 @@ class PublishPort extends EventEmitter {
|
|
|
302
309
|
localSocket.end();
|
|
303
310
|
}
|
|
304
311
|
}
|
|
305
|
-
this.
|
|
312
|
+
this.connection.deleteConnection(ref);
|
|
306
313
|
}
|
|
307
314
|
}
|
|
308
315
|
}
|