diodejs 0.3.0 → 0.4.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/README.md +56 -5
- package/bindPort.js +405 -28
- package/clientManager.js +435 -0
- package/connection.js +107 -14
- package/examples/RPCTest.js +9 -7
- package/examples/nativeBindTest.js +55 -0
- package/examples/nativeForwardTest.js +80 -0
- package/examples/nativeTcpClientTest.js +56 -0
- package/examples/nativeUdpClientTest.js +40 -0
- package/examples/portForwardTest.js +5 -7
- package/examples/publishAndBind.js +6 -10
- package/examples/publishPortTest.js +4 -6
- package/index.js +2 -1
- package/nativeCrypto.js +321 -0
- package/package.json +1 -1
- package/publishPort.js +514 -52
- package/rpc.js +125 -1
- package/utils.js +23 -0
package/README.md
CHANGED
|
@@ -50,6 +50,36 @@ connection.setReconnectOptions({
|
|
|
50
50
|
});
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
+
### Multi-Relay Connections (Recommended)
|
|
54
|
+
|
|
55
|
+
You can connect to multiple Diode relays and automatically route binds to the relay where the target device is connected.
|
|
56
|
+
|
|
57
|
+
```javascript
|
|
58
|
+
const { DiodeClientManager, BindPort } = require('diodejs');
|
|
59
|
+
|
|
60
|
+
async function main() {
|
|
61
|
+
// Connect to default relay pool (pre-net defaults)
|
|
62
|
+
const client = new DiodeClientManager({ keyLocation: './db/keys.json' });
|
|
63
|
+
await client.connect();
|
|
64
|
+
|
|
65
|
+
const bind = new BindPort(client, {
|
|
66
|
+
3003: { targetPort: 8080, deviceIdHex: '0x...', protocol: 'tcp' }
|
|
67
|
+
});
|
|
68
|
+
bind.bind();
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
If you provide a host, only that relay is used initially (similar to `-diodeaddrs`):
|
|
73
|
+
|
|
74
|
+
```javascript
|
|
75
|
+
const client = new DiodeClientManager({
|
|
76
|
+
host: 'us2.prenet.diode.io',
|
|
77
|
+
port: 41046,
|
|
78
|
+
keyLocation: './db/keys.json'
|
|
79
|
+
});
|
|
80
|
+
await client.connect();
|
|
81
|
+
```
|
|
82
|
+
|
|
53
83
|
### Test RPC
|
|
54
84
|
|
|
55
85
|
Here's a quick example to get you started with RPC functions using `DiodeRPC` Class
|
|
@@ -133,7 +163,8 @@ async function main() {
|
|
|
133
163
|
3003: {
|
|
134
164
|
targetPort: 443,
|
|
135
165
|
deviceIdHex: "0x5365baf29cb7ab58de588dfc448913cb609283e2",
|
|
136
|
-
protocol: "tcp" // Can be "tls", "tcp", or "udp"
|
|
166
|
+
protocol: "tcp", // Can be "tls", "tcp", or "udp"
|
|
167
|
+
transport: "native" // Optional - "api" (default) or "native" for portopen2 (tcp/udp only)
|
|
137
168
|
}
|
|
138
169
|
};
|
|
139
170
|
|
|
@@ -235,6 +266,21 @@ main();
|
|
|
235
266
|
- `reconnected`: Emitted when reconnection is successful
|
|
236
267
|
- `reconnect_failed`: Emitted when all reconnection attempts have failed
|
|
237
268
|
|
|
269
|
+
#### `DiodeClientManager`
|
|
270
|
+
|
|
271
|
+
- **Constructor**: `new DiodeClientManager(options)`
|
|
272
|
+
- `options.host` (string, optional): Single relay host (with or without port). If provided, only this relay is used initially.
|
|
273
|
+
- `options.port` (number, optional): Port to use when `options.host` has no port. Defaults to `41046`.
|
|
274
|
+
- `options.hosts` (string[] or comma-separated string, optional): Explicit relay list.
|
|
275
|
+
- `options.keyLocation` (string, optional): Key storage path (default: `./db/keys.json`).
|
|
276
|
+
- `options.deviceCacheTtlMs` (number, optional): Cache TTL for device relay resolution (default: `30000`).
|
|
277
|
+
|
|
278
|
+
- **Methods**:
|
|
279
|
+
- `connect()`: Connects to the initial relay pool. Returns a promise.
|
|
280
|
+
- `getConnectionForDevice(deviceId)`: Resolves and returns a relay connection for the device. Returns a promise.
|
|
281
|
+
- `getConnections()`: Returns a list of active connections.
|
|
282
|
+
- `close()`: Closes all managed connections.
|
|
283
|
+
|
|
238
284
|
#### `DiodeRPC`
|
|
239
285
|
|
|
240
286
|
- **Constructor**: `new DiodeRPC(connection)`
|
|
@@ -244,8 +290,11 @@ main();
|
|
|
244
290
|
- `getBlockPeak()`: Retrieves the current block peak. Returns a promise.
|
|
245
291
|
- `getBlockHeader(index)`: Retrieves the block header for a given index. Returns a promise.
|
|
246
292
|
- `getBlock(index)`: Retrieves the block for a given index. Returns a promise.
|
|
293
|
+
- `getObject(deviceId)`: Retrieves a device ticket object. Returns a promise.
|
|
294
|
+
- `getNode(nodeId)`: Retrieves relay node information. Returns a promise.
|
|
247
295
|
- `ping()`: Sends a ping command. Returns a promise.
|
|
248
296
|
- `portOpen(deviceId, port, flags)`: Opens a port on the device. Returns a promise.
|
|
297
|
+
- `portOpen2(deviceId, port, flags)`: Opens a native relay port on the device (TCP/UDP). Returns the server relay port.
|
|
249
298
|
- `portSend(ref, data)`: Sends data to the device. Returns a promise.
|
|
250
299
|
- `portClose(ref)`: Closes a port on the device. Returns a promise.
|
|
251
300
|
- `sendError(sessionId, ref, error)`: Sends an error response. Returns a promise.
|
|
@@ -259,23 +308,25 @@ main();
|
|
|
259
308
|
|
|
260
309
|
Legacy Constructor:
|
|
261
310
|
- `new BindPort(connection, localPort, targetPort, deviceIdHex)`
|
|
262
|
-
- `connection` (DiodeConnection): An instance of `DiodeConnection`.
|
|
311
|
+
- `connection` (DiodeConnection|DiodeClientManager): An instance of `DiodeConnection` or `DiodeClientManager`.
|
|
263
312
|
- `localPort` (number): The local port to bind.
|
|
264
313
|
- `targetPort` (number): The target port on the device.
|
|
265
314
|
- `deviceIdHex` (string): The device ID in hexadecimal format (with or without '0x' prefix).
|
|
266
315
|
|
|
267
316
|
New Constructor:
|
|
268
317
|
- `new BindPort(connection, portsConfig)`
|
|
269
|
-
- `connection` (DiodeConnection): An instance of `DiodeConnection`.
|
|
318
|
+
- `connection` (DiodeConnection|DiodeClientManager): An instance of `DiodeConnection` or `DiodeClientManager`.
|
|
270
319
|
- `portsConfig` (object): A configuration object where keys are local ports and values are objects with:
|
|
271
320
|
- `targetPort` (number): The target port on the device.
|
|
272
321
|
- `deviceIdHex` (string): The device ID in hexadecimal format (with or without '0x' prefix).
|
|
273
322
|
- `protocol` (string, optional): The protocol to use ("tls", "tcp", or "udp"). Defaults to "tls".
|
|
323
|
+
- `transport` (string, optional): The relay transport to use ("api" or "native"). Defaults to "api". Native uses `portopen2` and supports TCP/UDP only.
|
|
274
324
|
|
|
275
325
|
- **Methods**:
|
|
276
326
|
- `bind()`: Binds all configured local ports to their target ports on the devices.
|
|
277
|
-
- `addPort(localPort, targetPort, deviceIdHex, protocol)`: Adds a new port binding configuration.
|
|
327
|
+
- `addPort(localPort, targetPort, deviceIdHex, protocol, transport)`: Adds a new port binding configuration.
|
|
278
328
|
- `protocol` (string, optional): The protocol to use. Can be "tls", "tcp", or "udp". Defaults to "tls".
|
|
329
|
+
- `transport` (string, optional): The relay transport to use ("api" or "native"). Defaults to "api".
|
|
279
330
|
- `removePort(localPort)`: Removes a port binding configuration.
|
|
280
331
|
- `bindSinglePort(localPort)`: Binds a single local port to its target.
|
|
281
332
|
- `closeAllServers()`: Closes all active server instances.
|
|
@@ -283,7 +334,7 @@ main();
|
|
|
283
334
|
#### `PublishPort`
|
|
284
335
|
|
|
285
336
|
- **Constructor**: `new PublishPort(connection, publishedPorts, _certPath)`
|
|
286
|
-
- `connection` (DiodeConnection): An instance of `DiodeConnection`.
|
|
337
|
+
- `connection` (DiodeConnection|DiodeClientManager): An instance of `DiodeConnection` or `DiodeClientManager`.
|
|
287
338
|
- `publishedPorts` (array|object): Either:
|
|
288
339
|
- An array of ports to publish (all public mode)
|
|
289
340
|
- An object mapping ports to their configuration: `{ port: { mode: 'public'|'private', whitelist: ['0x123...'] } }`
|