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,128 +1,128 @@
|
|
|
1
|
-
// Story 12.1 (step 2b): the CLI's wording for coordinator refusals. The core (@asyx/tunnel-core)
|
|
2
|
-
// reports the coordinator's reason and data; this module prints exactly what the CLI has always
|
|
3
|
-
// printed and exits where it always has (code 1 for refusals that end the open; others keep running).
|
|
4
|
-
// Importing this module registers it with the core.
|
|
5
1
|
import { setOpenRejectReporter } from '#tunnel-core/mqtt/events/handlers.js';
|
|
6
|
-
|
|
7
2
|
export function printOpenReject({ code, reason, data }) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
3
|
+
const d = data || {};
|
|
4
|
+
switch (code) {
|
|
5
|
+
case 'invalid_secgroup': {
|
|
6
|
+
const slug = d.slug || d.secGroupSlug || 'unknown';
|
|
7
|
+
process.stderr.write(`QuickPass group "${slug}" not found. Re-run \`asyx tunnel\` with a valid group.\n`);
|
|
8
|
+
process.exit(1);
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
case 'tcp_port_out_of_block': {
|
|
12
|
+
const requestedPort = d.requestedPort || d.requestedExternalPort || 'X';
|
|
13
|
+
const blockStart = d.blockStart || 'A';
|
|
14
|
+
const blockEnd = d.blockEnd || 'B';
|
|
15
|
+
process.stderr.write(`Requested external port ${requestedPort} is outside your assigned block ${blockStart}–${blockEnd}.\n`);
|
|
16
|
+
process.exit(1);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
case 'tcp_port_already_in_use': {
|
|
20
|
+
const requestedPort = d.requestedPort || d.requestedExternalPort || 'X';
|
|
21
|
+
process.stderr.write(`External port ${requestedPort} is already in use by another active tunnel.\n`);
|
|
22
|
+
process.exit(1);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
case 'tcp_port_block_exhausted_for_tenant':
|
|
26
|
+
case 'tcp_port_block_exhausted': {
|
|
27
|
+
const blockStart = d.blockStart || 'A';
|
|
28
|
+
const blockEnd = d.blockEnd || 'B';
|
|
29
|
+
process.stderr.write(`All 10 ports in your block ${blockStart}–${blockEnd} are currently in use.\n`);
|
|
30
|
+
process.exit(1);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
case 'tcp_port_capacity_exhausted':
|
|
34
|
+
process.stderr.write(`Relay TCP port capacity exhausted. Please retry later.\n`);
|
|
35
|
+
process.exit(1);
|
|
36
|
+
return;
|
|
37
|
+
case 'tenant_tcp_lease_limit_reached': {
|
|
38
|
+
const relayId = d.relayId || 'unknown';
|
|
39
|
+
process.stderr.write(`Tenant TCP lease limit reached on relay ${relayId}\n`);
|
|
40
|
+
process.exit(1);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
case 'target_not_this_device': {
|
|
44
|
+
const to = d.to || 'that address';
|
|
45
|
+
const mine = d.hostname ? ` This computer serves ${d.hostname}.` : '';
|
|
46
|
+
process.stderr.write(`This computer can't serve ${to}.${mine} Run the command on the computer that owns ${to}, or change --to.\n`);
|
|
47
|
+
process.exit(1);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
case 'external_port_mismatch': {
|
|
51
|
+
process.stderr.write(`--external-port ${d.requested ?? ''} does not match this address's public port ${d.port ?? ''}. Leave --external-port out when you use --to.\n`);
|
|
52
|
+
process.exit(1);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
case 'lease_closed':
|
|
56
|
+
return;
|
|
57
|
+
case 'target_not_in_account': {
|
|
58
|
+
const to = d.to || 'that address';
|
|
59
|
+
process.stderr.write(`${to} is not an address of this account. Check the name, or add it in the console (asyx.ai/console/tunnel).\n`);
|
|
60
|
+
process.exit(1);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
case 'no_default_address': {
|
|
64
|
+
const list = Array.isArray(d.addresses) && d.addresses.length ? ` Use --to with one of: ${d.addresses.join(', ')}.` : ' Add one in the console (asyx.ai/console/tunnel), then use --to.';
|
|
65
|
+
process.stderr.write(`This device has no default address.${list}\n`);
|
|
66
|
+
process.exit(1);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
case 'app_address_only': {
|
|
70
|
+
process.stderr.write(`An app can serve only its own address${d.hostname ? `, not ${d.hostname}` : ''}. Leave out --to, or run it from a device.\n`);
|
|
71
|
+
process.exit(1);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
case 'address_suspended': {
|
|
75
|
+
const what = d.scope === 'account' ? 'This account is suspended' : `${d.hostname || 'This address'} is suspended`;
|
|
76
|
+
process.stderr.write(`${what}${d.reason ? `: ${d.reason}` : ''}. Contact support@asyx.ai.\n`);
|
|
77
|
+
process.exit(1);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
case 'external_port_assigned_elsewhere': {
|
|
81
|
+
process.stderr.write(`Port ${d.requestedPort ?? ''} belongs to ${d.address || 'another address'} of this account. Use that address with --to, release the port in the console, or pick another port.\n`);
|
|
82
|
+
process.exit(1);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
case 'port_pinned_to_app': {
|
|
86
|
+
process.stderr.write(`Port ${d.requestedPort ?? ''} is used by the app on ${d.address || 'another address'}. Release it in the console (asyx.ai/console/tunnel) to use it for another address, or pick another port.\n`);
|
|
87
|
+
process.exit(1);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
case 'port_changed_retry': {
|
|
91
|
+
process.stderr.write(`Port ${d.requestedPort ?? ''} changed while the tunnel was opening. Run the command again.\n`);
|
|
92
|
+
process.exit(75);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
case 'port_block_full': {
|
|
96
|
+
const where = d.assigned && typeof d.assigned === 'object' ? Object.entries(d.assigned).map(([p, a]) => `${p} → ${a}`).join(', ') : '';
|
|
97
|
+
process.stderr.write(`All 10 ${String(d.kind || '').toUpperCase()} ports of this account are assigned${where ? ` (${where})` : ''}. Release one in the console (asyx.ai/console/tunnel), then try again.\n`);
|
|
98
|
+
process.exit(1);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
case 'identity_inactive': {
|
|
102
|
+
if (process.env.ASYX_APP_ID) {
|
|
103
|
+
import('./app.js').then((m) => m.explainAppFailure(process.env.ASYX_APP_ID)).catch(() => false).then((said) => {
|
|
104
|
+
if (!said)
|
|
105
|
+
process.stderr.write('This app has been revoked or deleted. asyx app list shows its state.\n');
|
|
106
|
+
process.exit(1);
|
|
107
|
+
});
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
process.stderr.write('This device or app has been revoked or removed from its account. Run asyx setup to set it up again.\n');
|
|
111
|
+
process.exit(1);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
case 'app_replaced': {
|
|
115
|
+
process.stderr.write(`Another app now serves ${d.hostname || 'this address'} (it replaced this one). Revoke or delete this app in the console.\n`);
|
|
116
|
+
process.exit(1);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
case 'account_not_migrated': {
|
|
120
|
+
process.stderr.write(`${d.message || 'This device is not set up for your account yet. Run asyx setup again on this computer; if that does not help, contact support@asyx.ai.'}\n`);
|
|
121
|
+
process.exit(1);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
default:
|
|
125
|
+
process.stdout.write(`❌ Connection rejected${reason ? ` (${reason})` : ''}${typeof d.message === 'string' && d.message ? `: ${d.message}` : ''}\n`);
|
|
15
126
|
}
|
|
16
|
-
case 'tcp_port_out_of_block': {
|
|
17
|
-
const requestedPort = d.requestedPort || d.requestedExternalPort || 'X';
|
|
18
|
-
const blockStart = d.blockStart || 'A';
|
|
19
|
-
const blockEnd = d.blockEnd || 'B';
|
|
20
|
-
process.stderr.write(`Requested external port ${requestedPort} is outside your assigned block ${blockStart}–${blockEnd}.\n`);
|
|
21
|
-
process.exit(1);
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
case 'tcp_port_already_in_use': {
|
|
25
|
-
const requestedPort = d.requestedPort || d.requestedExternalPort || 'X';
|
|
26
|
-
process.stderr.write(`External port ${requestedPort} is already in use by another active tunnel.\n`);
|
|
27
|
-
process.exit(1);
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
case 'tcp_port_block_exhausted_for_tenant':
|
|
31
|
-
case 'tcp_port_block_exhausted': {
|
|
32
|
-
const blockStart = d.blockStart || 'A';
|
|
33
|
-
const blockEnd = d.blockEnd || 'B';
|
|
34
|
-
process.stderr.write(`All 10 ports in your block ${blockStart}–${blockEnd} are currently in use.\n`);
|
|
35
|
-
process.exit(1);
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
case 'tcp_port_capacity_exhausted':
|
|
39
|
-
process.stderr.write(`Relay TCP port capacity exhausted. Please retry later.\n`);
|
|
40
|
-
process.exit(1);
|
|
41
|
-
return;
|
|
42
|
-
case 'tenant_tcp_lease_limit_reached': {
|
|
43
|
-
const relayId = d.relayId || 'unknown';
|
|
44
|
-
process.stderr.write(`Tenant TCP lease limit reached on relay ${relayId}\n`);
|
|
45
|
-
process.exit(1);
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
case 'target_not_this_device': {
|
|
49
|
-
// Story 4.10 r3
|
|
50
|
-
const to = d.to || 'that address';
|
|
51
|
-
const mine = d.hostname ? ` This computer serves ${d.hostname}.` : '';
|
|
52
|
-
process.stderr.write(`This computer can't serve ${to}.${mine} Run the command on the computer that owns ${to}, or change --to.\n`);
|
|
53
|
-
process.exit(1);
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
case 'external_port_mismatch': {
|
|
57
|
-
process.stderr.write(`--external-port ${d.requested ?? ''} does not match this address's public port ${d.port ?? ''}. Leave --external-port out when you use --to.\n`);
|
|
58
|
-
process.exit(1);
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
case 'lease_closed':
|
|
62
|
-
// Story 3.13: handled by reopening the tunnel (it prints its own line); nothing to say here.
|
|
63
|
-
return;
|
|
64
|
-
// Story 2.9: the account model's refusals. Each ends the open with code 1 and says what to do.
|
|
65
|
-
case 'target_not_in_account': {
|
|
66
|
-
const to = d.to || 'that address';
|
|
67
|
-
process.stderr.write(`${to} is not an address of this account. Check the name, or add it in the console (asyx.ai/console/tunnel).\n`);
|
|
68
|
-
process.exit(1);
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
case 'no_default_address': {
|
|
72
|
-
const list = Array.isArray(d.addresses) && d.addresses.length ? ` Use --to with one of: ${d.addresses.join(', ')}.` : ' Add one in the console (asyx.ai/console/tunnel), then use --to.';
|
|
73
|
-
process.stderr.write(`This device has no default address.${list}\n`);
|
|
74
|
-
process.exit(1);
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
case 'app_address_only': {
|
|
78
|
-
process.stderr.write(`An app can serve only its own address${d.hostname ? `, not ${d.hostname}` : ''}. Leave out --to, or run it from a device.\n`);
|
|
79
|
-
process.exit(1);
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
case 'address_suspended': {
|
|
83
|
-
const what = d.scope === 'account' ? 'This account is suspended' : `${d.hostname || 'This address'} is suspended`;
|
|
84
|
-
process.stderr.write(`${what}${d.reason ? `: ${d.reason}` : ''}. Contact support@asyx.ai.\n`);
|
|
85
|
-
process.exit(1);
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
case 'external_port_assigned_elsewhere': {
|
|
89
|
-
process.stderr.write(`Port ${d.requestedPort ?? ''} belongs to ${d.address || 'another address'} of this account. Use that address with --to, release the port in the console, or pick another port.\n`);
|
|
90
|
-
process.exit(1);
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
// Story 2.11 c3 (port labels): a port an app's address uses stays with the app
|
|
94
|
-
case 'port_pinned_to_app': {
|
|
95
|
-
process.stderr.write(`Port ${d.requestedPort ?? ''} is used by the app on ${d.address || 'another address'}. Release it in the console (asyx.ai/console/tunnel) to use it for another address, or pick another port.\n`);
|
|
96
|
-
process.exit(1);
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
case 'port_changed_retry': {
|
|
100
|
-
process.stderr.write(`Port ${d.requestedPort ?? ''} changed while the tunnel was opening. Run the command again.\n`);
|
|
101
|
-
process.exit(75);
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
case 'port_block_full': {
|
|
105
|
-
const where = d.assigned && typeof d.assigned === 'object' ? Object.entries(d.assigned).map(([p, a]) => `${p} → ${a}`).join(', ') : '';
|
|
106
|
-
process.stderr.write(`All 10 ${String(d.kind || '').toUpperCase()} ports of this account are assigned${where ? ` (${where})` : ''}. Release one in the console (asyx.ai/console/tunnel), then try again.\n`);
|
|
107
|
-
process.exit(1);
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
case 'identity_inactive': {
|
|
111
|
-
process.stderr.write('This device or app has been revoked or removed from its account. Run asyx setup to set it up again.\n');
|
|
112
|
-
process.exit(1);
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
// Story 2.12 c3: should never happen; the coordinator sends the words (shown as sent), with ours as fallback.
|
|
116
|
-
case 'account_not_migrated': {
|
|
117
|
-
process.stderr.write(`${d.message || 'This device is not set up for your account yet. Run asyx setup again on this computer; if that does not help, contact support@asyx.ai.'}\n`);
|
|
118
|
-
process.exit(1);
|
|
119
|
-
return;
|
|
120
|
-
}
|
|
121
|
-
default:
|
|
122
|
-
// Not fatal: print and keep running (e.g. a refused renewal). Story 2.12: a message the coordinator
|
|
123
|
-
// sends with a reason this CLI does not know is shown rather than a bare code.
|
|
124
|
-
process.stdout.write(`❌ Connection rejected${reason ? ` (${reason})` : ''}${typeof d.message === 'string' && d.message ? `: ${d.message}` : ''}\n`);
|
|
125
|
-
}
|
|
126
127
|
}
|
|
127
|
-
|
|
128
128
|
setOpenRejectReporter(printOpenReject);
|
package/src/commands/setup.js
CHANGED
|
@@ -6,150 +6,126 @@ import crypto from 'node:crypto';
|
|
|
6
6
|
import { ensureKeyAndCsr } from '../lib/csr.js';
|
|
7
7
|
import { loadOrCreateClientId } from '../lib/device.js';
|
|
8
8
|
import { parseSetupArgs, approvalUrl, checkName, describeAnswer, setupCode, EXIT } from '../lib/naming.js';
|
|
9
|
-
|
|
10
|
-
const CONSOLE_URL = process.env.ASYX_CONSOLE_URL || 'https://www.asyx.ai/console/devices'; // Story 4.15 c4: approval opens on the Devices page
|
|
9
|
+
const CONSOLE_URL = process.env.ASYX_CONSOLE_URL || 'https://www.asyx.ai/console/devices';
|
|
11
10
|
const STS_URL = process.env.ASYX_STS_URL || 'https://www.asyx.ai/www/sts';
|
|
12
|
-
// A person has to open the link, maybe sign in, and press Approve (4.7): wait up to 10 minutes.
|
|
13
11
|
const APPROVAL_DEADLINE_MS = Number(process.env.ASYX_SETUP_TIMEOUT_MS || 10 * 60_000);
|
|
14
|
-
|
|
15
12
|
function randHex(bytes = 16) {
|
|
16
|
-
|
|
13
|
+
return crypto.randomBytes(bytes).toString('hex');
|
|
17
14
|
}
|
|
18
|
-
|
|
19
15
|
function isJsonResponse(res) {
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
const ct = res.headers.get('content-type') || '';
|
|
17
|
+
return ct.toLowerCase().includes('application/json');
|
|
22
18
|
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* `asyx setup [--hostname <name> | --random | --address <name>] [--json]` (Stories 2.1 r3, 4.7, 2.11 c5/c8).
|
|
26
|
-
* --address: one of the account's addresses as this device's default (a new device, or one already set up).
|
|
27
|
-
* Returns the exit code. The terminal never prompts: it prints the approval link, waits, and reports
|
|
28
|
-
* the approved hostname. With --json, stdout carries one JSON object per line and nothing else.
|
|
29
|
-
*
|
|
30
|
-
* Story 2.1 c6: the machine keeps its device id across setups (a new id only when it has none), so it
|
|
31
|
-
* keeps its hostname; the old certificate is retired by the server when the new one is issued.
|
|
32
|
-
*/
|
|
33
19
|
export async function runSetup(argv = [], deps = {}) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (opts.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
if (answer.status !== 'available') {
|
|
66
|
-
if (opts.json) emit({ event: 'error', error: answer.status, hostname: answer.hostname, suggestions: answer.suggestions || [] });
|
|
67
|
-
else stderr(`❌ ${describeAnswer(answer)}`);
|
|
68
|
-
return EXIT.NAME_UNAVAILABLE;
|
|
69
|
-
}
|
|
70
|
-
} catch (e) {
|
|
71
|
-
say(`⚠️ Could not check the name first (${e.message}); the approval page will check it.`);
|
|
20
|
+
const { fetchImpl = fetch, openImpl = open, sleep = (ms) => new Promise((r) => setTimeout(r, ms)), now = () => Date.now(), stdout = (s) => process.stdout.write(s + '\n'), stderr = (s) => process.stderr.write(s + '\n'), clientIdImpl = loadOrCreateClientId, csrImpl = ensureKeyAndCsr, writeFiles = writeCertificateFiles, deadlineMs = APPROVAL_DEADLINE_MS, } = deps;
|
|
21
|
+
const opts = parseSetupArgs(argv);
|
|
22
|
+
const say = (s) => (opts.json ? stderr(s) : stdout(s));
|
|
23
|
+
const emit = (o) => { if (opts.json)
|
|
24
|
+
stdout(JSON.stringify(o)); };
|
|
25
|
+
const fail = (code, error, message) => {
|
|
26
|
+
if (opts.json)
|
|
27
|
+
emit({ event: 'error', error, message });
|
|
28
|
+
else
|
|
29
|
+
stderr(`❌ ${message}`);
|
|
30
|
+
return code;
|
|
31
|
+
};
|
|
32
|
+
if (opts.error)
|
|
33
|
+
return fail(EXIT.USAGE, 'usage', `${opts.error}\nUsage: asyx setup [--hostname <name> | --random | --address <name>] [--json]`);
|
|
34
|
+
const clientId = clientIdImpl();
|
|
35
|
+
if (!/^cli_[A-F0-9]{12}$/.test(clientId))
|
|
36
|
+
return fail(EXIT.FAILED, 'invalid_client_id', 'Invalid clientId format (expected cli_[A-F0-9]{12})');
|
|
37
|
+
if (opts.hostname) {
|
|
38
|
+
try {
|
|
39
|
+
const answer = await checkName(opts.hostname, { fetchImpl });
|
|
40
|
+
if (answer.status !== 'available') {
|
|
41
|
+
if (opts.json)
|
|
42
|
+
emit({ event: 'error', error: answer.status, hostname: answer.hostname, suggestions: answer.suggestions || [] });
|
|
43
|
+
else
|
|
44
|
+
stderr(`❌ ${describeAnswer(answer)}`);
|
|
45
|
+
return EXIT.NAME_UNAVAILABLE;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
say(`⚠️ Could not check the name first (${e.message}); the approval page will check it.`);
|
|
50
|
+
}
|
|
72
51
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
say(` ${url}`);
|
|
82
|
-
say(`The page will ask for this code: ${code}`);
|
|
83
|
-
say('Only approve it if you started this setup yourself.');
|
|
84
|
-
try { await openImpl(url); } catch { /* no browser here: the link above is enough */ }
|
|
85
|
-
say('⏳ Waiting for approval…');
|
|
86
|
-
|
|
87
|
-
// 2) Key and CSR (the key stays on this machine)
|
|
88
|
-
const { csrPem } = await csrImpl(clientId);
|
|
89
|
-
|
|
90
|
-
// 3) Poll until approved
|
|
91
|
-
const start = now();
|
|
92
|
-
let attempt = 0;
|
|
93
|
-
let issued = null;
|
|
94
|
-
while (now() - start < deadlineMs) {
|
|
95
|
-
attempt++;
|
|
96
|
-
const backoff = Math.min(1000 + attempt * 250, 2000);
|
|
97
|
-
let res;
|
|
52
|
+
const ticket = randHex(16);
|
|
53
|
+
const url = approvalUrl(CONSOLE_URL, { clientId, ticket, hostname: opts.hostname, random: opts.random, address: opts.address });
|
|
54
|
+
const code = setupCode(ticket);
|
|
55
|
+
emit({ event: 'approval_required', url, code, clientId, expiresInSeconds: Math.round(deadlineMs / 1000) });
|
|
56
|
+
say('Approve this device in your browser:');
|
|
57
|
+
say(` ${url}`);
|
|
58
|
+
say(`The page will ask for this code: ${code}`);
|
|
59
|
+
say('Only approve it if you started this setup yourself.');
|
|
98
60
|
try {
|
|
99
|
-
|
|
100
|
-
method: 'POST',
|
|
101
|
-
headers: { 'content-type': 'application/json', accept: 'application/json', 'cache-control': 'no-store' },
|
|
102
|
-
body: JSON.stringify({ ticket, clientId, csrPem }),
|
|
103
|
-
});
|
|
104
|
-
} catch {
|
|
105
|
-
await sleep(backoff);
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
// 202: not approved yet (Defect 11.33). A non-JSON 200 is a CloudFront page: keep waiting.
|
|
109
|
-
if (res.status === 202 || (res.ok && !isJsonResponse(res))) {
|
|
110
|
-
await res.text().catch(() => undefined);
|
|
111
|
-
await sleep(backoff);
|
|
112
|
-
continue;
|
|
61
|
+
await openImpl(url);
|
|
113
62
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
63
|
+
catch { }
|
|
64
|
+
say('⏳ Waiting for approval…');
|
|
65
|
+
const { csrPem } = await csrImpl(clientId);
|
|
66
|
+
const start = now();
|
|
67
|
+
let attempt = 0;
|
|
68
|
+
let issued = null;
|
|
69
|
+
while (now() - start < deadlineMs) {
|
|
70
|
+
attempt++;
|
|
71
|
+
const backoff = Math.min(1000 + attempt * 250, 2000);
|
|
72
|
+
let res;
|
|
73
|
+
try {
|
|
74
|
+
res = await fetchImpl(STS_URL, {
|
|
75
|
+
method: 'POST',
|
|
76
|
+
headers: { 'content-type': 'application/json', accept: 'application/json', 'cache-control': 'no-store' },
|
|
77
|
+
body: JSON.stringify({ ticket, clientId, csrPem }),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
await sleep(backoff);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (res.status === 202 || (res.ok && !isJsonResponse(res))) {
|
|
85
|
+
await res.text().catch(() => undefined);
|
|
86
|
+
await sleep(backoff);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (!res.ok) {
|
|
90
|
+
if (res.status === 401 || res.status === 403)
|
|
91
|
+
return fail(EXIT.FAILED, 'unauthorized', 'Please sign in at https://www.asyx.ai and run `asyx setup` again.');
|
|
92
|
+
if (res.status === 404 || res.status === 410)
|
|
93
|
+
return fail(EXIT.FAILED, 'expired', 'The approval expired. Run `asyx setup` again.');
|
|
94
|
+
if (res.status === 409)
|
|
95
|
+
return fail(EXIT.FAILED, 'csr_mismatch', "The certificate request didn't match this machine's identity. Run `asyx setup` again on the machine that opened the link.");
|
|
96
|
+
let payload = '';
|
|
97
|
+
try {
|
|
98
|
+
payload = await res.text();
|
|
99
|
+
}
|
|
100
|
+
catch { }
|
|
101
|
+
return fail(EXIT.FAILED, 'setup_failed', `Setup failed (HTTP ${res.status}) ${payload}`.trim());
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
issued = await res.json();
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
await sleep(backoff);
|
|
109
|
+
}
|
|
127
110
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
return EXIT.OK;
|
|
111
|
+
if (!issued)
|
|
112
|
+
return fail(EXIT.FAILED, 'timeout', 'Nobody approved the device in time. Run `asyx setup` again.');
|
|
113
|
+
await writeFiles(clientId, issued);
|
|
114
|
+
const hostname = issued.identity?.hostname || null;
|
|
115
|
+
emit({ event: 'enrolled', clientId, hostname });
|
|
116
|
+
say('✅ Certificate issued and stored.');
|
|
117
|
+
if (hostname)
|
|
118
|
+
say(` Hostname: ${hostname}`);
|
|
119
|
+
return EXIT.OK;
|
|
138
120
|
}
|
|
139
|
-
|
|
140
121
|
async function writeCertificateFiles(clientId, issued) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
path.join(dir, 'manifest.json'),
|
|
147
|
-
JSON.stringify({ clientId, endpoint: issued.endpoint, region: issued.region, issuedAt: Date.now(), hostname: issued.identity?.hostname || undefined }, null, 2),
|
|
148
|
-
{ mode: 0o600 },
|
|
149
|
-
);
|
|
122
|
+
const dir = path.join(os.homedir(), '.asyx', 'certs', clientId);
|
|
123
|
+
await fs.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
124
|
+
await fs.writeFile(path.join(dir, 'AmazonRootCA1.pem'), issued.caPem, { mode: 0o644 });
|
|
125
|
+
await fs.writeFile(path.join(dir, 'device.pem.crt'), issued.certPem, { mode: 0o600 });
|
|
126
|
+
await fs.writeFile(path.join(dir, 'manifest.json'), JSON.stringify({ clientId, endpoint: issued.endpoint, region: issued.region, issuedAt: Date.now(), hostname: issued.identity?.hostname || undefined }, null, 2), { mode: 0o600 });
|
|
150
127
|
}
|
|
151
|
-
|
|
152
128
|
export default async function cmdSetup(argv = process.argv.slice(2)) {
|
|
153
|
-
|
|
154
|
-
|
|
129
|
+
const code = await runSetup(argv);
|
|
130
|
+
process.exit(code);
|
|
155
131
|
}
|
package/src/commands/sts.js
CHANGED
|
@@ -7,67 +7,51 @@ import path from 'node:path';
|
|
|
7
7
|
import os from 'node:os';
|
|
8
8
|
import { ensureKeyAndCsr } from '../lib/csr.js';
|
|
9
9
|
import { loadOrCreateClientId, getDeviceJsonPath } from '../lib/device.js';
|
|
10
|
-
|
|
11
10
|
const STS_URL = process.env.ASYX_STS_URL || 'https://www.asyx.ai/www/sts';
|
|
12
|
-
|
|
13
11
|
export default async function cmdSts(argv = process.argv.slice(2)) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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.');
|
|
12
|
+
const deviceJsonPath = getDeviceJsonPath();
|
|
13
|
+
await access(deviceJsonPath, fsConstants.F_OK).catch(() => {
|
|
14
|
+
console.error('This command is for dev use only. Missing ~/.asyx/device.json');
|
|
15
|
+
process.exit(2);
|
|
16
|
+
});
|
|
17
|
+
const args = new Set(argv);
|
|
18
|
+
const tIndex = argv.indexOf('--ticket');
|
|
19
|
+
const ticket = tIndex >= 0 ? argv[tIndex + 1] : null;
|
|
20
|
+
if (!ticket) {
|
|
21
|
+
console.error('Usage: asyx sts --ticket <ticket>');
|
|
22
|
+
process.exit(2);
|
|
23
|
+
}
|
|
24
|
+
const clientId = loadOrCreateClientId();
|
|
25
|
+
if (!/^cli_[A-F0-9]{12}$/.test(clientId)) {
|
|
26
|
+
console.error('Invalid clientId format (expected cli_[A-F0-9]{12})');
|
|
27
|
+
process.exit(2);
|
|
28
|
+
}
|
|
29
|
+
console.log(`🔐 Fetching certificate via STS for ${clientId} …`);
|
|
30
|
+
const { csrPem, dir } = await ensureKeyAndCsr(clientId);
|
|
31
|
+
const payload = { ticket, clientId, csrPem };
|
|
32
|
+
const res = await fetch(STS_URL, {
|
|
33
|
+
method: 'POST',
|
|
34
|
+
headers: { 'content-type': 'application/json' },
|
|
35
|
+
body: JSON.stringify(payload),
|
|
36
|
+
});
|
|
37
|
+
if (res.status === 202) {
|
|
38
|
+
console.error('⏳ Not ready yet (202). Try again shortly.');
|
|
39
|
+
process.exit(2);
|
|
40
|
+
}
|
|
41
|
+
if (!res.ok) {
|
|
42
|
+
const txt = await res.text();
|
|
43
|
+
console.error(`STS failed: ${res.status} ${txt}`);
|
|
44
|
+
process.exit(1);
|
|
45
|
+
}
|
|
46
|
+
const bundle = await res.json();
|
|
47
|
+
await fs.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
48
|
+
await fs.writeFile(path.join(dir, 'AmazonRootCA1.pem'), bundle.caPem, { mode: 0o644 });
|
|
49
|
+
await fs.writeFile(path.join(dir, 'device.pem.crt'), bundle.certPem, { mode: 0o600 });
|
|
50
|
+
await fs.writeFile(path.join(dir, 'manifest.json'), JSON.stringify({
|
|
51
|
+
clientId,
|
|
52
|
+
endpoint: bundle.endpoint,
|
|
53
|
+
region: bundle.region,
|
|
54
|
+
issuedAt: Date.now(),
|
|
55
|
+
}, null, 2), { mode: 0o600 });
|
|
56
|
+
console.log('✅ Certificate issued and stored.');
|
|
73
57
|
}
|