@dotrino/terminal-agent 0.2.2 → 0.2.5
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/bin/cli.js +12 -8
- package/index.js +17 -2
- package/link.js +11 -5
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -34,19 +34,23 @@ datos en ${dataDir()} (override DOTRINO_TERMINAL_DIR)`)
|
|
|
34
34
|
try {
|
|
35
35
|
if (cmd === 'enroll') {
|
|
36
36
|
console.log('Enlazar esta máquina con tu vault.')
|
|
37
|
-
console.log('
|
|
38
|
-
|
|
37
|
+
console.log('El código lo generas en el CERTIFICADOR (tu vault). Hay dos formas:')
|
|
38
|
+
console.log(' · Sin vault externo → abre https://terminal.dotrino.com, elige')
|
|
39
|
+
console.log(' "Usar este dispositivo como bóveda" → "Enlazar otra máquina" y copia el código.')
|
|
40
|
+
console.log(' · Con vault en un PC → ahí corre `dotrino-vault pair` y copia el QR/JSON.\n')
|
|
41
|
+
const text = await ask('Pega el código y Enter:\n> ')
|
|
39
42
|
const qr = parseQr(text)
|
|
40
|
-
console.log('\nConectando
|
|
43
|
+
console.log('\nConectando…')
|
|
41
44
|
const link = await enroll({
|
|
42
45
|
qr,
|
|
43
46
|
dir: opt('--dir'),
|
|
44
47
|
label: opt('--label') || 'terminal-agent',
|
|
45
|
-
onChallenge: ({ deviceId,
|
|
46
|
-
console.log('\n
|
|
47
|
-
console.log(` código: ${
|
|
48
|
-
console.log(` máquina ${deviceId}`)
|
|
49
|
-
console.log(
|
|
48
|
+
onChallenge: ({ deviceId, code }) => {
|
|
49
|
+
console.log('\n Escribe ESTE código en tu bóveda para aprobar esta máquina:')
|
|
50
|
+
console.log(` código: ${code}`)
|
|
51
|
+
console.log(` máquina: ${deviceId}`)
|
|
52
|
+
console.log(' (en terminal.dotrino.com escríbelo en el campo y pulsa "Aprobar";')
|
|
53
|
+
console.log(` en el PC del vault: dotrino-vault approve ${code})\n`)
|
|
50
54
|
console.log(' Esperando aprobación…')
|
|
51
55
|
}
|
|
52
56
|
})
|
package/index.js
CHANGED
|
@@ -17,6 +17,8 @@
|
|
|
17
17
|
* cacheada + el TTL del cert acota el riesgo. I/O cifrado E2E (../shared/e2e.js).
|
|
18
18
|
*/
|
|
19
19
|
import os from 'node:os'
|
|
20
|
+
import fs from 'node:fs'
|
|
21
|
+
import path from 'node:path'
|
|
20
22
|
import { createRequire } from 'node:module'
|
|
21
23
|
import { verifyChain, signWithDevice, pubkeyId } from '@dotrino/identity/capabilities'
|
|
22
24
|
import { installNodeGlobals } from './node-globals.js'
|
|
@@ -68,6 +70,13 @@ export async function startAgent (opts = {}) {
|
|
|
68
70
|
await identify()
|
|
69
71
|
client.on('token', () => { identify().catch(() => {}) })
|
|
70
72
|
|
|
73
|
+
// Bitácora persistente de sesiones (sessions.log, JSONL): qué dispositivo abrió
|
|
74
|
+
// consola y cuándo — auditoría local si un dispositivo tuyo cae en malas manos.
|
|
75
|
+
const sessionsLog = path.join(dir, 'sessions.log')
|
|
76
|
+
const audit = (op, info = {}) => {
|
|
77
|
+
try { fs.appendFileSync(sessionsLog, JSON.stringify({ ts: Date.now(), op, ...info }) + '\n') } catch (_) {}
|
|
78
|
+
}
|
|
79
|
+
|
|
71
80
|
const send = (to, obj) => { try { client.send(to, obj) } catch (e) { if (!opts.quiet) console.error('[terminal] send:', e.message) } }
|
|
72
81
|
|
|
73
82
|
// --- Revocación: refrescar la lista del vault por el proxy (best-effort) ---
|
|
@@ -96,7 +105,7 @@ export async function startAgent (opts = {}) {
|
|
|
96
105
|
const sessions = new Map()
|
|
97
106
|
const sweeper = setInterval(() => {
|
|
98
107
|
const now = Date.now()
|
|
99
|
-
for (const [sid, s] of sessions) if (now > s.exp) { try { s.term?.kill() } catch {}; sessions.delete(sid) }
|
|
108
|
+
for (const [sid, s] of sessions) if (now > s.exp) { try { s.term?.kill() } catch {}; sessions.delete(sid); audit('session-expire', { sid: sid.slice(0, 8) }) }
|
|
100
109
|
}, 60 * 1000); sweeper.unref?.()
|
|
101
110
|
|
|
102
111
|
async function pushOut (s, data) {
|
|
@@ -108,6 +117,11 @@ export async function startAgent (opts = {}) {
|
|
|
108
117
|
async function handleHandshake (from, p) {
|
|
109
118
|
const { data, signature, cert } = p
|
|
110
119
|
if (!data || !signature || !cert) return send(from, { type: T.ERROR, error: 'handshake incompleto' })
|
|
120
|
+
// FRESCURA anti-replay: el HS debe ser reciente (±5 min); sin esto un relay
|
|
121
|
+
// malicioso podía reproducir handshakes viejos (sesiones huérfanas / DoS).
|
|
122
|
+
if (typeof data.ts !== 'number' || Math.abs(Date.now() - data.ts) > 5 * 60 * 1000) {
|
|
123
|
+
return send(from, { type: T.ERROR, error: 'handshake vencido (posible replay, o el reloj del dispositivo está desfasado)' })
|
|
124
|
+
}
|
|
111
125
|
const chk = await verifyChain({ data, signature, cert, expectedScope: SIGN_SCOPE, trustedIssuer: master, revoked: revokedSet })
|
|
112
126
|
if (!chk.ok) return send(from, { type: T.ERROR, error: 'no autorizado: ' + chk.reason })
|
|
113
127
|
if (data.op !== 'terminal.hs' || typeof data.eph !== 'string') return send(from, { type: T.ERROR, error: 'handshake inválido' })
|
|
@@ -123,6 +137,7 @@ export async function startAgent (opts = {}) {
|
|
|
123
137
|
const { signature: ackSig } = await signWithDevice({ privateJwk: link.device.privateJwk, data: ack })
|
|
124
138
|
|
|
125
139
|
sessions.set(sid, { sid, key, term: null, from, exp: Date.now() + SESSION_TTL_MS })
|
|
140
|
+
audit('session-open', { sid: sid.slice(0, 8), device: (await pubkeyId(chk.device)).slice(0, 8).toUpperCase() })
|
|
126
141
|
send(from, { type: T.ACK, sid, ack, signature: ackSig, cert: link.cert })
|
|
127
142
|
if (!opts.quiet) console.log(`[terminal] sesión ${sid.slice(0, 8)} autorizada (device ${chk.device?.slice?.(0, 8) || '?'})`)
|
|
128
143
|
}
|
|
@@ -148,7 +163,7 @@ export async function startAgent (opts = {}) {
|
|
|
148
163
|
}
|
|
149
164
|
if (msg.type === 'input') return void s.term?.write(msg.data)
|
|
150
165
|
if (msg.type === 'resize') { try { s.term?.resize(msg.cols, msg.rows) } catch {}; return }
|
|
151
|
-
if (msg.type === 'close') { try { s.term?.kill() } catch {}; sessions.delete(p.sid); return }
|
|
166
|
+
if (msg.type === 'close') { try { s.term?.kill() } catch {}; sessions.delete(p.sid); audit('session-close', { sid: String(p.sid).slice(0, 8) }); return }
|
|
152
167
|
}
|
|
153
168
|
|
|
154
169
|
client.on('message', (from, payload) => {
|
package/link.js
CHANGED
|
@@ -13,7 +13,7 @@ import fs from 'node:fs'
|
|
|
13
13
|
import path from 'node:path'
|
|
14
14
|
import os from 'node:os'
|
|
15
15
|
import {
|
|
16
|
-
makeDeviceKey, signWithDevice, verifyDelegation,
|
|
16
|
+
makeDeviceKey, signWithDevice, verifyDelegation, makePairingCode, pubkeyId
|
|
17
17
|
} from '@dotrino/identity/capabilities'
|
|
18
18
|
import { installNodeGlobals } from './node-globals.js'
|
|
19
19
|
|
|
@@ -58,22 +58,28 @@ async function freshClient (proxyUrl, dir) {
|
|
|
58
58
|
/**
|
|
59
59
|
* Enrola esta máquina contra el vault del QR. Devuelve y persiste
|
|
60
60
|
* `{ device, cert, iss, proxy, label }`.
|
|
61
|
-
* @param {(c:{deviceId:string,
|
|
61
|
+
* @param {(c:{deviceId:string,code:string})=>void} onChallenge mostrar el CÓDIGO para que un humano lo TIPEE en la bóveda al aprobar (el código NO viaja de aquí)
|
|
62
62
|
*/
|
|
63
63
|
export async function enroll ({ qr, label = 'terminal-agent', dir = dataDir(), onChallenge, timeoutMs = 180000 } = {}) {
|
|
64
64
|
const client = await freshClient(qr.proxy, dir)
|
|
65
65
|
try {
|
|
66
66
|
const device = await makeDeviceKey({ label })
|
|
67
67
|
const deviceId = (await pubkeyId(device.publickey)).slice(0, 8).toUpperCase().replace(/(.{4})(.{4})/, '$1-$2')
|
|
68
|
-
|
|
68
|
+
// Código de emparejamiento ALEATORIO (un secreto, NO derivable de valores públicos):
|
|
69
|
+
// lo mostramos y NO lo enviamos. La bóveda lo aprende solo cuando un humano lo tipea
|
|
70
|
+
// y nos lo ECHA de vuelta → así aprobar exige TENER esta máquina (de aquí sale el código).
|
|
71
|
+
const code = makePairingCode()
|
|
69
72
|
const data = { op: 'enroll', dpub: device.publickey, token: qr.token, sn: qr.sn, label, ts: Date.now() }
|
|
70
73
|
const { signature } = await signWithDevice({ privateJwk: device.privateJwk, data })
|
|
71
74
|
|
|
72
75
|
const done = new Promise((resolve, reject) => {
|
|
73
76
|
const off = client.on('message', (_from, p) => {
|
|
74
77
|
if (!p || typeof p !== 'object') return
|
|
75
|
-
if (p.type === MSG.ENROLL_CHALLENGE) onChallenge?.({ deviceId,
|
|
76
|
-
|
|
78
|
+
if (p.type === MSG.ENROLL_CHALLENGE) onChallenge?.({ deviceId, code })
|
|
79
|
+
// Aceptamos SOLO si el código echado coincide con el que generamos (anti
|
|
80
|
+
// aprobación-a-ciegas / bóveda impostora). Un eco distinto se IGNORA y seguimos
|
|
81
|
+
// esperando la aprobación correcta (o el timeout).
|
|
82
|
+
else if (p.type === MSG.ENROLLED) { if (String(p.code || '').trim() === code) { cleanup(); resolve(p) } }
|
|
77
83
|
else if (p.type === MSG.ERROR) { cleanup(); reject(new Error(p.error)) }
|
|
78
84
|
})
|
|
79
85
|
const t = setTimeout(() => { cleanup(); reject(new Error('timeout esperando la aprobación en el vault')) }, timeoutMs)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dotrino/terminal-agent",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"description": "Agente de Dotrino Terminal: abre una shell (PTY) en la máquina del vault solo para el dispositivo enlazado, vía @dotrino/tunnel y cifrado E2E.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|