diodejs 0.1.0 → 0.1.1
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 +16 -5
- package/examples/publishPortTest.js +1 -1
- package/package.json +1 -1
- package/publishPort.js +40 -4
package/README.md
CHANGED
|
@@ -87,13 +87,22 @@ async function main() {
|
|
|
87
87
|
const connection = new DiodeConnection(host, port, certPath);
|
|
88
88
|
await connection.connect();
|
|
89
89
|
|
|
90
|
-
|
|
91
|
-
const
|
|
92
|
-
|
|
90
|
+
// Option 1: Simple array of ports (all public)
|
|
91
|
+
const publishedPorts = [8080, 3000];
|
|
92
|
+
|
|
93
|
+
// Option 2: Object with port configurations for public/private access control
|
|
94
|
+
const publishedPortsWithConfig = {
|
|
95
|
+
8080: { mode: 'public' }, // Public port, accessible by any device
|
|
96
|
+
3000: {
|
|
97
|
+
mode: 'private',
|
|
98
|
+
whitelist: ['0x1234abcd5678...', '0x9876fedc5432...'] // Only these devices can connect
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const publishPort = new PublishPort(connection, publishedPortsWithConfig, certPath);
|
|
93
103
|
}
|
|
94
104
|
|
|
95
105
|
main();
|
|
96
|
-
|
|
97
106
|
```
|
|
98
107
|
|
|
99
108
|
## Reference
|
|
@@ -149,7 +158,9 @@ main();
|
|
|
149
158
|
|
|
150
159
|
- **Constructor**: `new PublishPort(connection, publishedPorts, certPath)`
|
|
151
160
|
- `connection` (DiodeConnection): An instance of `DiodeConnection`.
|
|
152
|
-
- `publishedPorts` (array):
|
|
161
|
+
- `publishedPorts` (array|object): Either:
|
|
162
|
+
- An array of ports to publish (all public mode)
|
|
163
|
+
- An object mapping ports to their configuration: `{ port: { mode: 'public'|'private', whitelist: ['0x123...'] } }`
|
|
153
164
|
- `certPath` (string): The path to the device certificate.
|
|
154
165
|
|
|
155
166
|
- **Methods**:
|
|
@@ -11,7 +11,7 @@ async function startPublishing() {
|
|
|
11
11
|
const connection = new DiodeConnection(host, port, certPath);
|
|
12
12
|
await connection.connect();
|
|
13
13
|
|
|
14
|
-
const publishedPorts =
|
|
14
|
+
const publishedPorts = {8080: {mode: 'private', whitelist: ['0xca1e71d8105a598810578fb6042fa8cbc1e7f039']}}
|
|
15
15
|
const publishPort = new PublishPort(connection, publishedPorts, certPath);
|
|
16
16
|
|
|
17
17
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "diodejs",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
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
|
@@ -39,9 +39,34 @@ class PublishPort extends EventEmitter {
|
|
|
39
39
|
constructor(connection, publishedPorts, certPath) {
|
|
40
40
|
super();
|
|
41
41
|
this.connection = connection;
|
|
42
|
-
|
|
43
|
-
//
|
|
42
|
+
|
|
43
|
+
// Convert publishedPorts to a Map with configurations
|
|
44
|
+
this.publishedPorts = new Map();
|
|
45
|
+
|
|
46
|
+
// Handle both array format and object format
|
|
47
|
+
if (Array.isArray(publishedPorts)) {
|
|
48
|
+
// Legacy array format - treat all ports as public
|
|
49
|
+
publishedPorts.forEach(port => {
|
|
50
|
+
this.publishedPorts.set(port, { mode: 'public', whitelist: [] });
|
|
51
|
+
});
|
|
52
|
+
} else if (typeof publishedPorts === 'object' && publishedPorts !== null) {
|
|
53
|
+
// New object format with configurations
|
|
54
|
+
Object.entries(publishedPorts).forEach(([port, config]) => {
|
|
55
|
+
const portNum = parseInt(port, 10);
|
|
56
|
+
// Ensure config is properly structured
|
|
57
|
+
const portConfig = typeof config === 'object' && config !== null
|
|
58
|
+
? {
|
|
59
|
+
mode: config.mode || 'public',
|
|
60
|
+
whitelist: Array.isArray(config.whitelist) ? config.whitelist : []
|
|
61
|
+
}
|
|
62
|
+
: { mode: 'public', whitelist: [] };
|
|
63
|
+
|
|
64
|
+
this.publishedPorts.set(portNum, portConfig);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
44
68
|
this.startListening();
|
|
69
|
+
logger.info(`Publishing ports: ${Array.from(this.publishedPorts.keys())}`);
|
|
45
70
|
this.rpc = new DiodeRPC(connection);
|
|
46
71
|
this.certPath = certPath;
|
|
47
72
|
}
|
|
@@ -76,7 +101,7 @@ class PublishPort extends EventEmitter {
|
|
|
76
101
|
const sessionId = Buffer.from(sessionIdRaw);
|
|
77
102
|
const portString = makeReadable(portStringRaw);
|
|
78
103
|
const ref = Buffer.from(refRaw);
|
|
79
|
-
const deviceId = Buffer.from(deviceIdRaw).toString('hex')
|
|
104
|
+
const deviceId = `0x${Buffer.from(deviceIdRaw).toString('hex')}`;
|
|
80
105
|
|
|
81
106
|
logger.info(`Received portopen request for portString ${portString} with ref ${ref.toString('hex')} from device ${deviceId}`);
|
|
82
107
|
|
|
@@ -96,13 +121,24 @@ class PublishPort extends EventEmitter {
|
|
|
96
121
|
}
|
|
97
122
|
|
|
98
123
|
// Check if the port is published
|
|
99
|
-
if (!this.publishedPorts.has(port)) {
|
|
124
|
+
if (!this.publishedPorts.has(port)) {
|
|
100
125
|
logger.warn(`Port ${port} is not published. Rejecting request.`);
|
|
101
126
|
// Send error response
|
|
102
127
|
this.rpc.sendError(sessionId, ref, 'Port is not published');
|
|
103
128
|
return;
|
|
104
129
|
}
|
|
105
130
|
|
|
131
|
+
// Get port configuration and check whitelist if in private mode
|
|
132
|
+
const portConfig = this.publishedPorts.get(port);
|
|
133
|
+
if (portConfig.mode === 'private' && Array.isArray(portConfig.whitelist)) {
|
|
134
|
+
if (!portConfig.whitelist.includes(deviceId)) {
|
|
135
|
+
logger.warn(`Device ${deviceId} is not whitelisted for port ${port}. Rejecting request.`);
|
|
136
|
+
this.rpc.sendError(sessionId, ref, 'Device not whitelisted');
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
logger.info(`Device ${deviceId} is whitelisted for port ${port}. Accepting request.`);
|
|
140
|
+
}
|
|
141
|
+
|
|
106
142
|
// Handle based on protocol
|
|
107
143
|
if (protocol === 'tcp') {
|
|
108
144
|
this.handleTCPConnection(sessionId, ref, port);
|