asyx 0.0.1 β 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/LICENSE +67 -0
- package/README.md +134 -5
- package/SECURITY.md +40 -0
- package/bin/asyx.js +2 -0
- package/package.json +47 -11
- package/src/commands/.gitkeep +0 -0
- package/src/commands/mqtt-broker.js +56 -0
- package/src/commands/mqtt.js +164 -0
- package/src/commands/setup.js +137 -0
- package/src/commands/sts.js +73 -0
- package/src/commands/tunnel.js +540 -0
- package/src/lib/.gitkeep +0 -0
- package/src/lib/certs.js +25 -0
- package/src/lib/csr.js +45 -0
- package/src/lib/device.js +18 -0
- package/src/lib/fingerprint.js +115 -0
- package/src/lib/ids.js +5 -0
- package/src/lib/logger.js +58 -0
- package/src/lib/logger.level.spec.js +81 -0
- package/src/lib/logger.test.js +42 -0
- package/src/lib/pairing.js +15 -0
- package/src/lib/shared-transport.js +15 -0
- package/src/main.js +57 -0
- package/src/mqtt/awaiter-map.js +157 -0
- package/src/mqtt/events/handlers.js +90 -0
- package/src/mqtt/events/index.js +14 -0
- package/src/mqtt/events/schemas.js +58 -0
- package/src/mqtt/ingress.js +172 -0
- package/src/state/assignment.js +156 -0
- package/src/tunnel/__test_utils__/testUtils.js +76 -0
- package/src/tunnel/__tests__/jwt-refresh.spec.js +948 -0
- package/src/tunnel/__tests__/ws-control.spec.js +164 -0
- package/src/tunnel/http-tunnel.js +1131 -0
- package/src/tunnel/ws-control.js +144 -0
- package/index.js +0 -13
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import open from 'open';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import crypto from 'node:crypto';
|
|
6
|
+
import { ensureKeyAndCsr } from '../lib/csr.js';
|
|
7
|
+
import { loadOrCreateClientId } from '../lib/device.js';
|
|
8
|
+
|
|
9
|
+
const CONSOLE_URL = process.env.ASYX_CONSOLE_URL || 'https://www.asyx.ai/console/tunnel';
|
|
10
|
+
const STS_URL = process.env.ASYX_STS_URL || 'https://www.asyx.ai/www/sts';
|
|
11
|
+
|
|
12
|
+
function randHex(bytes = 16) {
|
|
13
|
+
return crypto.randomBytes(bytes).toString('hex');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isJsonResponse(res) {
|
|
17
|
+
const ct = res.headers.get('content-type') || '';
|
|
18
|
+
return ct.toLowerCase().includes('application/json');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export default async function cmdSetup(argv = process.argv.slice(2)) {
|
|
22
|
+
const clientId = loadOrCreateClientId();
|
|
23
|
+
if (!/^cli_[A-F0-9]{12}$/.test(clientId)) {
|
|
24
|
+
console.error('Invalid clientId format (expected cli_[A-F0-9]{12})');
|
|
25
|
+
process.exit(2);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// 1) Generate ticket and claim URL
|
|
29
|
+
const ticket = randHex(16);
|
|
30
|
+
const claimUrl = `${CONSOLE_URL}?clientId=${encodeURIComponent(clientId)}&ticket=${encodeURIComponent(ticket)}`;
|
|
31
|
+
|
|
32
|
+
console.log('π Opening browserβ¦');
|
|
33
|
+
await open(claimUrl);
|
|
34
|
+
console.log('β³ Waiting for certificate issuance via www.asyx.ai β¦');
|
|
35
|
+
|
|
36
|
+
// 2) Generate CSR (key stays local)
|
|
37
|
+
const { csrPem } = await ensureKeyAndCsr(clientId);
|
|
38
|
+
|
|
39
|
+
// 3) Poll /www/sts until issued (bounded, resilient)
|
|
40
|
+
const pollStart = Date.now();
|
|
41
|
+
const deadlineMs = 60_000; // 60s total budget
|
|
42
|
+
let attempt = 0;
|
|
43
|
+
let issued = null;
|
|
44
|
+
|
|
45
|
+
while (Date.now() - pollStart < deadlineMs) {
|
|
46
|
+
attempt++;
|
|
47
|
+
let res;
|
|
48
|
+
try {
|
|
49
|
+
res = await fetch(STS_URL, {
|
|
50
|
+
method: 'POST',
|
|
51
|
+
headers: {
|
|
52
|
+
'content-type': 'application/json',
|
|
53
|
+
'accept': 'application/json',
|
|
54
|
+
'cache-control': 'no-store',
|
|
55
|
+
},
|
|
56
|
+
body: JSON.stringify({ ticket, clientId, csrPem }),
|
|
57
|
+
});
|
|
58
|
+
} catch (e) {
|
|
59
|
+
// Network hiccup; short backoff and retry
|
|
60
|
+
await new Promise(r => setTimeout(r, Math.min(1000 + attempt * 250, 2000)));
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 202: not ready yet β backoff and retry
|
|
65
|
+
if (res.status === 202) {
|
|
66
|
+
await new Promise(r => setTimeout(r, Math.min(1000 + attempt * 250, 2000)));
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Non-JSON (e.g., CloudFront fallback HTML). Ignore and keep polling within budget.
|
|
71
|
+
if (res.ok && !isJsonResponse(res)) {
|
|
72
|
+
await res.text().catch(() => undefined); // drain
|
|
73
|
+
await new Promise(r => setTimeout(r, Math.min(1000 + attempt * 250, 2000)));
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Error codes:
|
|
78
|
+
if (!res.ok) {
|
|
79
|
+
// Map specific error codes to user-friendly messages
|
|
80
|
+
if (res.status === 401 || res.status === 403) {
|
|
81
|
+
console.error('β Please sign in first at https://www.asyx.ai and run `asyx setup` again.');
|
|
82
|
+
process.exit(1);
|
|
83
|
+
}
|
|
84
|
+
if (res.status === 404) {
|
|
85
|
+
console.error('β Setup timed out. Run `asyx setup` again to start over.');
|
|
86
|
+
process.exit(1);
|
|
87
|
+
}
|
|
88
|
+
if (res.status === 409) {
|
|
89
|
+
console.error('β The certificate request didn\'t match this machine\'s identity. Please re-run asyx setup on the same machine that opened the Console.');
|
|
90
|
+
process.exit(1);
|
|
91
|
+
}
|
|
92
|
+
// Generic error
|
|
93
|
+
let payload = '';
|
|
94
|
+
try { payload = await res.text(); } catch {}
|
|
95
|
+
console.error(`β Setup failed (HTTP ${res.status}) β ${payload}`);
|
|
96
|
+
console.error('β Please sign in (or refresh) and run `asyx setup` again.');
|
|
97
|
+
process.exit(1);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Success JSON
|
|
101
|
+
try {
|
|
102
|
+
issued = await res.json();
|
|
103
|
+
break;
|
|
104
|
+
} catch {
|
|
105
|
+
// Malformed JSON β treat as transient and keep polling
|
|
106
|
+
await new Promise(r => setTimeout(r, Math.min(1000 + attempt * 250, 2000)));
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (!issued) {
|
|
112
|
+
console.error('β³ Setup timed out. Please sign in and run `asyx setup` again.');
|
|
113
|
+
process.exit(1);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 4) Write files
|
|
117
|
+
const dir = path.join(os.homedir(), '.asyx', 'certs', clientId);
|
|
118
|
+
await fs.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
119
|
+
await fs.writeFile(path.join(dir, 'AmazonRootCA1.pem'), issued.caPem, { mode: 0o644 });
|
|
120
|
+
await fs.writeFile(path.join(dir, 'device.pem.crt'), issued.certPem, { mode: 0o600 });
|
|
121
|
+
await fs.writeFile(
|
|
122
|
+
path.join(dir, 'manifest.json'),
|
|
123
|
+
JSON.stringify(
|
|
124
|
+
{
|
|
125
|
+
clientId,
|
|
126
|
+
endpoint: issued.endpoint,
|
|
127
|
+
region: issued.region,
|
|
128
|
+
issuedAt: Date.now(),
|
|
129
|
+
},
|
|
130
|
+
null,
|
|
131
|
+
2,
|
|
132
|
+
),
|
|
133
|
+
{ mode: 0o600 },
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
console.log('β
Certificate issued and stored.');
|
|
137
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { constants as fsConstants } from 'node:fs';
|
|
2
|
+
import { access } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import fs from 'node:fs/promises';
|
|
5
|
+
import fssync from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import os from 'node:os';
|
|
8
|
+
import { ensureKeyAndCsr } from '../lib/csr.js';
|
|
9
|
+
import { loadOrCreateClientId } from '../lib/device.js';
|
|
10
|
+
|
|
11
|
+
const STS_URL = process.env.ASYX_STS_URL || 'https://www.asyx.ai/www/sts';
|
|
12
|
+
const DEVICE_JSON_PATH = join(process.env.HOME ?? '', '.asyx', 'device.json');
|
|
13
|
+
|
|
14
|
+
export default async function cmdSts(argv = process.argv.slice(2)) {
|
|
15
|
+
// Device credential guard
|
|
16
|
+
await access(DEVICE_JSON_PATH, fsConstants.F_OK).catch(() => {
|
|
17
|
+
console.error('This command is for dev use only. Missing ~/.asyx/device.json');
|
|
18
|
+
process.exit(2);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const args = new Set(argv);
|
|
22
|
+
const tIndex = argv.indexOf('--ticket');
|
|
23
|
+
const ticket = tIndex >= 0 ? argv[tIndex + 1] : null;
|
|
24
|
+
if (!ticket) {
|
|
25
|
+
console.error('Usage: asyx sts --ticket <ticket>');
|
|
26
|
+
process.exit(2);
|
|
27
|
+
}
|
|
28
|
+
const clientId = loadOrCreateClientId();
|
|
29
|
+
if (!/^cli_[A-F0-9]{12}$/.test(clientId)) {
|
|
30
|
+
console.error('Invalid clientId format (expected cli_[A-F0-9]{12})');
|
|
31
|
+
process.exit(2);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
console.log(`π Fetching certificate via STS for ${clientId} β¦`);
|
|
35
|
+
|
|
36
|
+
// Generate CSR (key stays local)
|
|
37
|
+
const { csrPem, dir } = await ensureKeyAndCsr(clientId);
|
|
38
|
+
const payload = { ticket, clientId, csrPem };
|
|
39
|
+
|
|
40
|
+
const res = await fetch(STS_URL, {
|
|
41
|
+
method: 'POST',
|
|
42
|
+
headers: { 'content-type': 'application/json' },
|
|
43
|
+
body: JSON.stringify(payload),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
if (res.status === 202) {
|
|
47
|
+
console.error('β³ Not ready yet (202). Try again shortly.');
|
|
48
|
+
process.exit(2);
|
|
49
|
+
}
|
|
50
|
+
if (!res.ok) {
|
|
51
|
+
const txt = await res.text();
|
|
52
|
+
console.error(`STS failed: ${res.status} ${txt}`);
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const bundle = await res.json();
|
|
57
|
+
|
|
58
|
+
await fs.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
59
|
+
await fs.writeFile(path.join(dir, 'AmazonRootCA1.pem'), bundle.caPem, { mode: 0o644 });
|
|
60
|
+
await fs.writeFile(path.join(dir, 'device.pem.crt'), bundle.certPem, { mode: 0o600 });
|
|
61
|
+
await fs.writeFile(
|
|
62
|
+
path.join(dir, 'manifest.json'),
|
|
63
|
+
JSON.stringify({
|
|
64
|
+
clientId,
|
|
65
|
+
endpoint: bundle.endpoint,
|
|
66
|
+
region: bundle.region,
|
|
67
|
+
issuedAt: Date.now(),
|
|
68
|
+
}, null, 2),
|
|
69
|
+
{ mode: 0o600 }
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
console.log('β
Certificate issued and stored.');
|
|
73
|
+
}
|