asyx 0.10.0 → 0.11.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/AGENTS.md +42 -0
- package/bin/asyx.js +1 -1
- package/package.json +6 -3
- package/src/commands/account.js +80 -71
- package/src/commands/app.js +317 -151
- package/src/commands/in-use-messages.js +47 -48
- package/src/commands/mqtt-broker.js +36 -49
- package/src/commands/mqtt.js +111 -149
- package/src/commands/name.js +24 -22
- package/src/commands/reject-messages.js +123 -123
- package/src/commands/setup.js +107 -131
- package/src/commands/sts.js +45 -61
- package/src/commands/tunnel.js +853 -931
- package/src/commands/validate-target-host.js +36 -59
- package/src/lib/aws-iot-config.js +30 -0
- package/src/lib/certs.js +15 -18
- package/src/lib/csr.js +107 -182
- package/src/lib/device.js +26 -37
- package/src/lib/ids.js +1 -2
- package/src/lib/naming.js +74 -68
- package/src/lib/pairing.js +10 -11
- package/src/lib/shared-transport.js +5 -9
- package/src/main.js +63 -53
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneClient.js +25 -287
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneTransport.js +0 -72
- package/vendor/tunnel-core/dist/control-plane/session.js +0 -20
- package/vendor/tunnel-core/dist/errors.js +0 -19
- package/vendor/tunnel-core/dist/flows/account.js +1 -6
- package/vendor/tunnel-core/dist/flows/goodbye.js +0 -4
- package/vendor/tunnel-core/dist/flows/http.js +24 -112
- package/vendor/tunnel-core/dist/flows/identity.js +11 -10
- package/vendor/tunnel-core/dist/flows/in-use.js +0 -6
- package/vendor/tunnel-core/dist/flows/reopen.js +0 -4
- package/vendor/tunnel-core/dist/flows/tcp.js +18 -84
- package/vendor/tunnel-core/dist/flows/udp.js +15 -52
- package/vendor/tunnel-core/dist/lib/fingerprint.js +38 -42
- package/vendor/tunnel-core/dist/lib/jwt-verifier.js +14 -67
- package/vendor/tunnel-core/dist/lib/logger.js +1 -9
- package/vendor/tunnel-core/dist/lib/refresh-helpers.js +1 -22
- package/vendor/tunnel-core/dist/lifecycle.js +1 -8
- package/vendor/tunnel-core/dist/mqtt/awaiter-map.js +1 -53
- package/vendor/tunnel-core/dist/mqtt/events/handlers.js +10 -64
- package/vendor/tunnel-core/dist/mqtt/events/index.js +3 -36
- package/vendor/tunnel-core/dist/mqtt/events/schemas.js +0 -1
- package/vendor/tunnel-core/dist/mqtt/ingress.js +0 -60
- package/vendor/tunnel-core/dist/state/assignment.js +6 -122
- package/vendor/tunnel-core/dist/state/instance-registry.js +0 -63
- package/vendor/tunnel-core/dist/tcp-v1/framing.js +2 -64
- package/vendor/tunnel-core/dist/tunnel/http-tunnel.js +64 -331
- package/vendor/tunnel-core/dist/tunnel/httpws-constants.js +5 -14
- package/vendor/tunnel-core/dist/tunnel/reconnect.js +10 -50
- package/vendor/tunnel-core/dist/tunnel/request-sink.js +0 -16
- package/vendor/tunnel-core/dist/tunnel/tcp-tunnel.js +32 -195
- package/vendor/tunnel-core/dist/tunnel/udp-tunnel.js +28 -153
- package/vendor/tunnel-core/dist/tunnel/ws-control.js +4 -39
- package/vendor/tunnel-core/dist/types.js +0 -2
- package/vendor/tunnel-core/dist/udp-v1/framing.js +2 -64
- package/vendor/tunnel-core/package.json +0 -25
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneClient.d.ts +0 -273
- package/vendor/tunnel-core/dist/control-plane/MqttControlPlaneTransport.d.ts +0 -79
- package/vendor/tunnel-core/dist/control-plane/session.d.ts +0 -55
- package/vendor/tunnel-core/dist/errors.d.ts +0 -49
- package/vendor/tunnel-core/dist/flows/account.d.ts +0 -24
- package/vendor/tunnel-core/dist/flows/goodbye.d.ts +0 -10
- package/vendor/tunnel-core/dist/flows/http.d.ts +0 -61
- package/vendor/tunnel-core/dist/flows/identity.d.ts +0 -23
- package/vendor/tunnel-core/dist/flows/in-use.d.ts +0 -27
- package/vendor/tunnel-core/dist/flows/reopen.d.ts +0 -9
- package/vendor/tunnel-core/dist/flows/tcp.d.ts +0 -85
- package/vendor/tunnel-core/dist/flows/udp.d.ts +0 -52
- package/vendor/tunnel-core/dist/lib/adapt-logger.d.ts +0 -2
- package/vendor/tunnel-core/dist/lib/fingerprint.d.ts +0 -145
- package/vendor/tunnel-core/dist/lib/jwt-verifier.d.ts +0 -27
- package/vendor/tunnel-core/dist/lib/logger.d.ts +0 -9
- package/vendor/tunnel-core/dist/lib/refresh-helpers.d.ts +0 -16
- package/vendor/tunnel-core/dist/lifecycle.d.ts +0 -24
- package/vendor/tunnel-core/dist/mqtt/awaiter-map.d.ts +0 -56
- package/vendor/tunnel-core/dist/mqtt/events/handlers.d.ts +0 -60
- package/vendor/tunnel-core/dist/mqtt/events/index.d.ts +0 -1
- package/vendor/tunnel-core/dist/mqtt/events/schemas.d.ts +0 -498
- package/vendor/tunnel-core/dist/mqtt/ingress.d.ts +0 -63
- package/vendor/tunnel-core/dist/state/assignment.d.ts +0 -136
- package/vendor/tunnel-core/dist/state/instance-registry.d.ts +0 -88
- package/vendor/tunnel-core/dist/tcp-v1/framing.d.ts +0 -43
- package/vendor/tunnel-core/dist/tunnel/http-tunnel.d.ts +0 -140
- package/vendor/tunnel-core/dist/tunnel/httpws-constants.d.ts +0 -13
- package/vendor/tunnel-core/dist/tunnel/reconnect.d.ts +0 -76
- package/vendor/tunnel-core/dist/tunnel/request-sink.d.ts +0 -40
- package/vendor/tunnel-core/dist/tunnel/tcp-tunnel.d.ts +0 -92
- package/vendor/tunnel-core/dist/tunnel/udp-tunnel.d.ts +0 -93
- package/vendor/tunnel-core/dist/tunnel/ws-control.d.ts +0 -22
- package/vendor/tunnel-core/dist/types.d.ts +0 -24
- package/vendor/tunnel-core/dist/udp-v1/framing.d.ts +0 -41
|
@@ -1,68 +1,45 @@
|
|
|
1
|
-
// UDP-S13: Target host validation with DNS resolution and loopback-only enforcement
|
|
2
1
|
import dns from 'node:dns';
|
|
3
2
|
import { promisify } from 'node:util';
|
|
4
|
-
|
|
5
3
|
const dnsLookup = promisify(dns.lookup);
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Validate that target host resolves only to loopback addresses.
|
|
9
|
-
* @param {string} targetHost - Hostname or IP address to validate
|
|
10
|
-
* @returns {Promise<{isLoopback: boolean, resolvedIPs: string[]}>}
|
|
11
|
-
*/
|
|
12
4
|
export async function validateTargetHost(targetHost) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (normalizedHost === '127.0.0.1' || normalizedHost === '::1' || normalizedHost === 'localhost') {
|
|
17
|
-
return { isLoopback: true, resolvedIPs: [normalizedHost] };
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// Try to resolve the hostname
|
|
21
|
-
try {
|
|
22
|
-
// dns.lookup resolves to the first IP (IPv4 or IPv6)
|
|
23
|
-
// We need to check all addresses, so we'll use dns.resolve4 and dns.resolve6
|
|
24
|
-
const resolve4 = promisify(dns.resolve4);
|
|
25
|
-
const resolve6 = promisify(dns.resolve6);
|
|
26
|
-
|
|
27
|
-
const ipv4Addresses = [];
|
|
28
|
-
const ipv6Addresses = [];
|
|
29
|
-
|
|
30
|
-
try {
|
|
31
|
-
const v4 = await resolve4(normalizedHost, { all: true });
|
|
32
|
-
ipv4Addresses.push(...v4.map(addr => addr.address));
|
|
33
|
-
} catch (e) {
|
|
34
|
-
// No IPv4 addresses
|
|
5
|
+
const normalizedHost = targetHost.toLowerCase().trim();
|
|
6
|
+
if (normalizedHost === '127.0.0.1' || normalizedHost === '::1' || normalizedHost === 'localhost') {
|
|
7
|
+
return { isLoopback: true, resolvedIPs: [normalizedHost] };
|
|
35
8
|
}
|
|
36
|
-
|
|
37
9
|
try {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
10
|
+
const resolve4 = promisify(dns.resolve4);
|
|
11
|
+
const resolve6 = promisify(dns.resolve6);
|
|
12
|
+
const ipv4Addresses = [];
|
|
13
|
+
const ipv6Addresses = [];
|
|
14
|
+
try {
|
|
15
|
+
const v4 = await resolve4(normalizedHost, { all: true });
|
|
16
|
+
ipv4Addresses.push(...v4.map(addr => addr.address));
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
const v6 = await resolve6(normalizedHost, { all: true });
|
|
22
|
+
ipv6Addresses.push(...v6.map(addr => addr.address));
|
|
23
|
+
}
|
|
24
|
+
catch (e) {
|
|
25
|
+
}
|
|
26
|
+
const allResolved = [...ipv4Addresses, ...ipv6Addresses];
|
|
27
|
+
if (allResolved.length === 0) {
|
|
28
|
+
return { isLoopback: false, resolvedIPs: [] };
|
|
29
|
+
}
|
|
30
|
+
const loopbackIPs = ['127.0.0.1', '::1'];
|
|
31
|
+
const isAllLoopback = allResolved.every(ip => {
|
|
32
|
+
if (loopbackIPs.includes(ip))
|
|
33
|
+
return true;
|
|
34
|
+
if (ip.startsWith('127.'))
|
|
35
|
+
return true;
|
|
36
|
+
if (ip === '::1' || ip.toLowerCase() === '::1')
|
|
37
|
+
return true;
|
|
38
|
+
return false;
|
|
39
|
+
});
|
|
40
|
+
return { isLoopback: isAllLoopback, resolvedIPs: allResolved };
|
|
42
41
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (allResolved.length === 0) {
|
|
47
|
-
// Couldn't resolve - treat as invalid
|
|
48
|
-
return { isLoopback: false, resolvedIPs: [] };
|
|
42
|
+
catch (error) {
|
|
43
|
+
throw new Error(`DNS resolution failed for "${targetHost}": ${error.message}`);
|
|
49
44
|
}
|
|
50
|
-
|
|
51
|
-
// Check if all resolved IPs are loopback
|
|
52
|
-
const loopbackIPs = ['127.0.0.1', '::1'];
|
|
53
|
-
const isAllLoopback = allResolved.every(ip => {
|
|
54
|
-
// Check exact match
|
|
55
|
-
if (loopbackIPs.includes(ip)) return true;
|
|
56
|
-
// Check IPv4 loopback range (127.0.0.0/8)
|
|
57
|
-
if (ip.startsWith('127.')) return true;
|
|
58
|
-
// Check IPv6 loopback
|
|
59
|
-
if (ip === '::1' || ip.toLowerCase() === '::1') return true;
|
|
60
|
-
return false;
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
return { isLoopback: isAllLoopback, resolvedIPs: allResolved };
|
|
64
|
-
} catch (error) {
|
|
65
|
-
// DNS resolution failed - throw so caller can handle it with proper error message
|
|
66
|
-
throw new Error(`DNS resolution failed for "${targetHost}": ${error.message}`);
|
|
67
|
-
}
|
|
68
45
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
export function loadAwsIotConfig() {
|
|
4
|
+
const home = process.env.HOME || process.env.USERPROFILE || '.';
|
|
5
|
+
const devPath = join(home, '.asyx', 'device.json');
|
|
6
|
+
const dev = existsSync(devPath) ? JSON.parse(readFileSync(devPath, 'utf8')) : {};
|
|
7
|
+
const clientId = process.env.ASYX_CLIENT_ID || dev.clientId;
|
|
8
|
+
if (!clientId)
|
|
9
|
+
throw new Error('missing clientId (ASYX_CLIENT_ID or ~/.asyx/device.json)');
|
|
10
|
+
if (!/^(cli|app)_[A-F0-9]{12}$/.test(clientId))
|
|
11
|
+
throw new Error(`invalid clientId format: ${clientId}`);
|
|
12
|
+
const isApp = clientId.startsWith('app_');
|
|
13
|
+
const base = isApp ? join(home, '.asyx', 'apps', clientId) : join(home, '.asyx', 'certs', clientId);
|
|
14
|
+
const manifestPath = join(base, 'manifest.json');
|
|
15
|
+
if (!existsSync(manifestPath))
|
|
16
|
+
throw new Error(`missing manifest: ${manifestPath}`);
|
|
17
|
+
const manifest = JSON.parse(readFileSync(manifestPath, 'utf8'));
|
|
18
|
+
const { endpoint, region } = manifest;
|
|
19
|
+
if (!endpoint || !/^[a-z0-9.-]+\.iot\.[a-z0-9-]+\.amazonaws\.com$/.test(endpoint))
|
|
20
|
+
throw new Error(`invalid endpoint in manifest: ${endpoint}`);
|
|
21
|
+
if (!region || !/^[a-z0-9-]+$/.test(region))
|
|
22
|
+
throw new Error(`invalid region in manifest: ${region}`);
|
|
23
|
+
const caPath = join(base, 'AmazonRootCA1.pem');
|
|
24
|
+
const certPath = join(base, 'device.pem.crt');
|
|
25
|
+
const keyPath = join(base, 'private.pem.key');
|
|
26
|
+
for (const p of [caPath, certPath, keyPath])
|
|
27
|
+
if (!existsSync(p))
|
|
28
|
+
throw new Error(`missing file: ${p}`);
|
|
29
|
+
return { endpoint, region, clientId, certPath, keyPath, caPath };
|
|
30
|
+
}
|
package/src/lib/certs.js
CHANGED
|
@@ -1,25 +1,22 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import os from 'node:os';
|
|
3
3
|
import path from 'node:path';
|
|
4
|
-
|
|
5
4
|
export function certsDirFor(clientId) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
const base = path.join(os.homedir(), '.asyx', 'certs', clientId);
|
|
6
|
+
fs.mkdirSync(base, { recursive: true, mode: 0o700 });
|
|
7
|
+
return base;
|
|
9
8
|
}
|
|
10
|
-
|
|
11
9
|
export function writeCertBundle({ clientId, certPem, caPem, keyPem, endpoint, region }) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return dir;
|
|
10
|
+
if (!clientId)
|
|
11
|
+
throw new Error('missing clientId');
|
|
12
|
+
if (!certPem || !caPem)
|
|
13
|
+
throw new Error('missing certificate bundle');
|
|
14
|
+
const dir = certsDirFor(clientId);
|
|
15
|
+
fs.writeFileSync(path.join(dir, 'device.pem.crt'), certPem, { mode: 0o600 });
|
|
16
|
+
if (keyPem)
|
|
17
|
+
fs.writeFileSync(path.join(dir, 'private.pem.key'), keyPem, { mode: 0o600 });
|
|
18
|
+
fs.writeFileSync(path.join(dir, 'AmazonRootCA1.pem'), caPem, { mode: 0o644 });
|
|
19
|
+
const manifest = { clientId, endpoint, region, when: Math.floor(Date.now() / 1000) };
|
|
20
|
+
fs.writeFileSync(path.join(dir, 'manifest.json'), JSON.stringify(manifest, null, 2), { mode: 0o600 });
|
|
21
|
+
return dir;
|
|
25
22
|
}
|
package/src/lib/csr.js
CHANGED
|
@@ -4,202 +4,127 @@ import path from 'node:path';
|
|
|
4
4
|
import os from 'node:os';
|
|
5
5
|
import nodeCrypto, { webcrypto } from 'node:crypto';
|
|
6
6
|
import { cryptoProvider, Pkcs10CertificateRequestGenerator } from '@peculiar/x509';
|
|
7
|
-
|
|
8
7
|
const crypto = webcrypto;
|
|
9
|
-
cryptoProvider.set(crypto);
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Convert ArrayBuffer to PEM format
|
|
13
|
-
* @param {ArrayBuffer} arrayBuffer - The binary data
|
|
14
|
-
* @param {string} header - PEM header (e.g., 'PRIVATE KEY')
|
|
15
|
-
* @returns {string} PEM formatted string
|
|
16
|
-
*/
|
|
8
|
+
cryptoProvider.set(crypto);
|
|
17
9
|
function bufferToPem(arrayBuffer, header) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
10
|
+
const base64 = Buffer.from(arrayBuffer).toString('base64');
|
|
11
|
+
const chunks = base64.match(/.{1,64}/g) || [];
|
|
12
|
+
const pem = [
|
|
13
|
+
`-----BEGIN ${header}-----`,
|
|
14
|
+
...chunks,
|
|
15
|
+
`-----END ${header}-----`
|
|
16
|
+
].join('\n');
|
|
17
|
+
return pem + '\n';
|
|
26
18
|
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Convert PEM string to ArrayBuffer
|
|
30
|
-
* @param {string} pem - PEM formatted string
|
|
31
|
-
* @returns {ArrayBuffer} Binary data
|
|
32
|
-
*/
|
|
33
19
|
function pemToBuffer(pem) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
20
|
+
const base64 = pem
|
|
21
|
+
.replace(/-----BEGIN [\s\S]+?-----/, '')
|
|
22
|
+
.replace(/-----END [\s\S]+?-----/, '')
|
|
23
|
+
.replace(/\s/g, '');
|
|
24
|
+
const buffer = Buffer.from(base64, 'base64');
|
|
25
|
+
return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
|
|
40
26
|
}
|
|
41
|
-
|
|
42
27
|
function certsDirFor(clientId, baseDir) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
28
|
+
if (baseDir) {
|
|
29
|
+
return path.join(baseDir, 'certs', clientId);
|
|
30
|
+
}
|
|
31
|
+
const home = os.homedir();
|
|
32
|
+
return path.join(home, '.asyx', 'certs', clientId);
|
|
48
33
|
}
|
|
49
|
-
|
|
50
34
|
async function ensureDir(p) {
|
|
51
|
-
|
|
35
|
+
await fs.mkdir(p, { recursive: true, mode: 0o700 });
|
|
52
36
|
}
|
|
53
|
-
|
|
54
37
|
async function chmodIfExists(p, mode) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
38
|
+
try {
|
|
39
|
+
await fs.chmod(p, mode);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
}
|
|
60
43
|
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Import private key from PEM file and extract public key
|
|
64
|
-
* @param {ArrayBuffer} pkcs8Bytes - PKCS8 formatted key bytes
|
|
65
|
-
* @returns {Promise<CryptoKeyPair>} Key pair with private and public keys
|
|
66
|
-
*/
|
|
67
44
|
async function importPrivateKey(pkcs8Bytes) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const jwk = await crypto.subtle.exportKey('jwk', privateKey);
|
|
86
|
-
|
|
87
|
-
// Create public key JWK by removing private components
|
|
88
|
-
const publicJwk = {
|
|
89
|
-
kty: jwk.kty,
|
|
90
|
-
n: jwk.n,
|
|
91
|
-
e: jwk.e,
|
|
92
|
-
alg: jwk.alg,
|
|
93
|
-
key_ops: ['verify']
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
// Import as public key
|
|
97
|
-
const publicKey = await crypto.subtle.importKey(
|
|
98
|
-
'jwk',
|
|
99
|
-
publicJwk,
|
|
100
|
-
alg,
|
|
101
|
-
true,
|
|
102
|
-
['verify']
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
return { privateKey, publicKey };
|
|
45
|
+
const alg = {
|
|
46
|
+
name: 'RSASSA-PKCS1-v1_5',
|
|
47
|
+
modulusLength: 2048,
|
|
48
|
+
publicExponent: new Uint8Array([1, 0, 1]),
|
|
49
|
+
hash: 'SHA-256'
|
|
50
|
+
};
|
|
51
|
+
const privateKey = await crypto.subtle.importKey('pkcs8', pkcs8Bytes, alg, true, ['sign']);
|
|
52
|
+
const jwk = await crypto.subtle.exportKey('jwk', privateKey);
|
|
53
|
+
const publicJwk = {
|
|
54
|
+
kty: jwk.kty,
|
|
55
|
+
n: jwk.n,
|
|
56
|
+
e: jwk.e,
|
|
57
|
+
alg: jwk.alg,
|
|
58
|
+
key_ops: ['verify']
|
|
59
|
+
};
|
|
60
|
+
const publicKey = await crypto.subtle.importKey('jwk', publicJwk, alg, true, ['verify']);
|
|
61
|
+
return { privateKey, publicKey };
|
|
106
62
|
}
|
|
107
|
-
|
|
108
63
|
export async function ensureKeyAndCsr(clientId, options = {}) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
64
|
+
const { baseDir } = options;
|
|
65
|
+
const dir = options.dir || certsDirFor(clientId, baseDir);
|
|
66
|
+
await ensureDir(dir);
|
|
67
|
+
const keyPath = path.join(dir, 'private.pem.key');
|
|
68
|
+
const csrPath = path.join(dir, 'device.csr.pem');
|
|
69
|
+
const alg = {
|
|
70
|
+
name: 'RSASSA-PKCS1-v1_5',
|
|
71
|
+
modulusLength: 2048,
|
|
72
|
+
publicExponent: new Uint8Array([1, 0, 1]),
|
|
73
|
+
hash: 'SHA-256'
|
|
74
|
+
};
|
|
75
|
+
let keyPair;
|
|
76
|
+
if (!fssync.existsSync(keyPath)) {
|
|
77
|
+
keyPair = await crypto.subtle.generateKey(alg, true, ['sign', 'verify']);
|
|
78
|
+
const pkcs8 = await crypto.subtle.exportKey('pkcs8', keyPair.privateKey);
|
|
79
|
+
const pkcs8Pem = bufferToPem(pkcs8, 'PRIVATE KEY');
|
|
80
|
+
const nodePrivateKey = nodeCrypto.createPrivateKey({
|
|
81
|
+
key: pkcs8Pem,
|
|
82
|
+
format: 'pem'
|
|
83
|
+
});
|
|
84
|
+
const pkcs1Pem = nodePrivateKey.export({
|
|
85
|
+
type: 'pkcs1',
|
|
86
|
+
format: 'pem'
|
|
87
|
+
});
|
|
88
|
+
await fs.writeFile(keyPath, pkcs1Pem, { encoding: 'utf8', mode: 0o600 });
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
const keyPem = await fs.readFile(keyPath, 'utf8');
|
|
92
|
+
const isPkcs1 = keyPem.includes('BEGIN RSA PRIVATE KEY');
|
|
93
|
+
let pkcs8Bytes;
|
|
94
|
+
if (isPkcs1) {
|
|
95
|
+
const nodePrivateKey = nodeCrypto.createPrivateKey({
|
|
96
|
+
key: keyPem,
|
|
97
|
+
format: 'pem'
|
|
98
|
+
});
|
|
99
|
+
const pkcs8Pem = nodePrivateKey.export({
|
|
100
|
+
type: 'pkcs8',
|
|
101
|
+
format: 'pem'
|
|
102
|
+
});
|
|
103
|
+
pkcs8Bytes = pemToBuffer(pkcs8Pem);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
const nodePrivateKey = nodeCrypto.createPrivateKey({
|
|
107
|
+
key: keyPem,
|
|
108
|
+
format: 'pem'
|
|
109
|
+
});
|
|
110
|
+
const pkcs1Pem = nodePrivateKey.export({
|
|
111
|
+
type: 'pkcs1',
|
|
112
|
+
format: 'pem'
|
|
113
|
+
});
|
|
114
|
+
pkcs8Bytes = pemToBuffer(keyPem);
|
|
115
|
+
await fs.writeFile(keyPath, pkcs1Pem, { encoding: 'utf8', mode: 0o600 });
|
|
116
|
+
}
|
|
117
|
+
keyPair = await importPrivateKey(pkcs8Bytes);
|
|
118
|
+
}
|
|
119
|
+
const csr = await Pkcs10CertificateRequestGenerator.create({
|
|
120
|
+
name: `CN=${clientId}, O=Asyx`,
|
|
121
|
+
keys: keyPair,
|
|
122
|
+
signingAlgorithm: alg
|
|
143
123
|
});
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
} else {
|
|
148
|
-
// Import existing private key
|
|
149
|
-
const keyPem = await fs.readFile(keyPath, 'utf8');
|
|
150
|
-
|
|
151
|
-
// Check if key is PKCS#1 (RSA PRIVATE KEY) or PKCS#8 (PRIVATE KEY)
|
|
152
|
-
const isPkcs1 = keyPem.includes('BEGIN RSA PRIVATE KEY');
|
|
153
|
-
|
|
154
|
-
let pkcs8Bytes;
|
|
155
|
-
|
|
156
|
-
if (isPkcs1) {
|
|
157
|
-
// Key is already PKCS#1 - keep it as is (don't convert to PKCS#8)
|
|
158
|
-
// Convert PKCS#1 to PKCS#8 bytes in memory for webcrypto import
|
|
159
|
-
const nodePrivateKey = nodeCrypto.createPrivateKey({
|
|
160
|
-
key: keyPem,
|
|
161
|
-
format: 'pem'
|
|
162
|
-
});
|
|
163
|
-
const pkcs8Pem = nodePrivateKey.export({
|
|
164
|
-
type: 'pkcs8',
|
|
165
|
-
format: 'pem'
|
|
166
|
-
});
|
|
167
|
-
pkcs8Bytes = pemToBuffer(pkcs8Pem);
|
|
168
|
-
// Keep file as PKCS#1 (no write needed)
|
|
169
|
-
} else {
|
|
170
|
-
// Key is PKCS#8 - convert to PKCS#1 for Windows TLS compatibility
|
|
171
|
-
const nodePrivateKey = nodeCrypto.createPrivateKey({
|
|
172
|
-
key: keyPem,
|
|
173
|
-
format: 'pem'
|
|
174
|
-
});
|
|
175
|
-
const pkcs1Pem = nodePrivateKey.export({
|
|
176
|
-
type: 'pkcs1',
|
|
177
|
-
format: 'pem'
|
|
178
|
-
});
|
|
179
|
-
pkcs8Bytes = pemToBuffer(keyPem); // Use original PKCS#8 for webcrypto
|
|
180
|
-
// Update the file to PKCS#1 format for future runs
|
|
181
|
-
await fs.writeFile(keyPath, pkcs1Pem, { encoding: 'utf8', mode: 0o600 });
|
|
124
|
+
const csrPem = csr.toString('pem');
|
|
125
|
+
if (!/^-----BEGIN CERTIFICATE REQUEST-----[\s\S]*-----END CERTIFICATE REQUEST-----\s*$/.test(csrPem)) {
|
|
126
|
+
throw new Error('csr_generation_failed');
|
|
182
127
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// Build CSR using static factory method
|
|
188
|
-
const csr = await Pkcs10CertificateRequestGenerator.create({
|
|
189
|
-
name: `CN=${clientId}, O=Asyx`,
|
|
190
|
-
keys: keyPair, // { publicKey, privateKey }
|
|
191
|
-
signingAlgorithm: alg // same object used for generateKey
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
const csrPem = csr.toString('pem');
|
|
195
|
-
|
|
196
|
-
// Ensure CSR is properly formatted
|
|
197
|
-
if (!/^-----BEGIN CERTIFICATE REQUEST-----[\s\S]*-----END CERTIFICATE REQUEST-----\s*$/.test(csrPem)) {
|
|
198
|
-
throw new Error('csr_generation_failed');
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// Write CSR file
|
|
202
|
-
await fs.writeFile(csrPath, csrPem, { encoding: 'utf8' });
|
|
203
|
-
|
|
204
|
-
return { keyPath, csrPath, csrPem, dir };
|
|
128
|
+
await fs.writeFile(csrPath, csrPem, { encoding: 'utf8' });
|
|
129
|
+
return { keyPath, csrPath, csrPem, dir };
|
|
205
130
|
}
|
package/src/lib/device.js
CHANGED
|
@@ -2,50 +2,39 @@ import * as fs from "fs";
|
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import crypto from "node:crypto";
|
|
5
|
-
|
|
6
5
|
function resolveConfigDir() {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return join(`${process.env.HOMEDRIVE}${process.env.HOMEPATH}`, ".asyx");
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
throw new Error("Unable to determine Asyx config directory (missing home directory)");
|
|
6
|
+
if (process.env.ASYX_CONFIG_DIR && process.env.ASYX_CONFIG_DIR.trim()) {
|
|
7
|
+
return process.env.ASYX_CONFIG_DIR.trim();
|
|
8
|
+
}
|
|
9
|
+
const home = homedir();
|
|
10
|
+
if (home)
|
|
11
|
+
return join(home, ".asyx");
|
|
12
|
+
if (process.env.USERPROFILE)
|
|
13
|
+
return join(process.env.USERPROFILE, ".asyx");
|
|
14
|
+
if (process.env.HOMEDRIVE && process.env.HOMEPATH) {
|
|
15
|
+
return join(`${process.env.HOMEDRIVE}${process.env.HOMEPATH}`, ".asyx");
|
|
16
|
+
}
|
|
17
|
+
throw new Error("Unable to determine Asyx config directory (missing home directory)");
|
|
23
18
|
}
|
|
24
|
-
|
|
25
19
|
const DIR = resolveConfigDir();
|
|
26
20
|
const FILE = join(DIR, "device.json");
|
|
27
|
-
|
|
28
21
|
export function loadOrCreateClientId() {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
22
|
+
if (!fs.existsSync(DIR))
|
|
23
|
+
fs.mkdirSync(DIR, { recursive: true });
|
|
24
|
+
if (fs.existsSync(FILE)) {
|
|
25
|
+
const raw = JSON.parse(fs.readFileSync(FILE, "utf8"));
|
|
26
|
+
if (raw?.clientId)
|
|
27
|
+
return raw.clientId;
|
|
28
|
+
}
|
|
29
|
+
const clientId = "cli_" + crypto.randomUUID().replace(/-/g, "").slice(0, 12).toUpperCase();
|
|
30
|
+
fs.writeFileSync(FILE, JSON.stringify({ clientId }, null, 2));
|
|
31
|
+
return clientId;
|
|
37
32
|
}
|
|
38
|
-
|
|
39
33
|
export function resetClientId() {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
34
|
+
if (fs.existsSync(FILE)) {
|
|
35
|
+
fs.writeFileSync(FILE, JSON.stringify({}, null, 2));
|
|
36
|
+
}
|
|
43
37
|
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Get the path to device.json using the same resolution logic as loadOrCreateClientId
|
|
47
|
-
* This ensures consistent path resolution across all commands (tunnel, setup, etc.)
|
|
48
|
-
*/
|
|
49
38
|
export function getDeviceJsonPath() {
|
|
50
|
-
|
|
39
|
+
return FILE;
|
|
51
40
|
}
|
package/src/lib/ids.js
CHANGED