@dotrino/terminal-agent 0.49.3 → 0.50.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/bin/terminal.js +10 -0
- package/downloads.js +80 -0
- package/far.js +28 -3
- package/index.js +16 -3
- package/package.json +5 -4
package/bin/terminal.js
CHANGED
|
@@ -975,6 +975,16 @@ function interactive (conn, first, dir) {
|
|
|
975
975
|
// sigue: pasa a una que haya, o se queda sin consola. Terminar aquí cerraba la ventana
|
|
976
976
|
// (y la app, si era la última) en la máquina que solo estaba mirando.
|
|
977
977
|
if (m.code === 'machine-restarted') { lost(); return }
|
|
978
|
+
// LA OTRA MÁQUINA VOLVIÓ A PEDIR SU CLAVE (la sesión venció sin uso): desconectada. Se
|
|
979
|
+
// termina sin error, como cuando se queda sin consolas, y la app devuelve la ventana a las
|
|
980
|
+
// consolas de esta máquina; en el panel, esa máquina vuelve a salir con «Escribir la clave».
|
|
981
|
+
if (m.code === 'locked-again') return finish(0, t('Esa máquina se desconectó: vuelve a escribir su clave para entrar.', 'That machine disconnected: type its code again to get in.'))
|
|
982
|
+
// Solo venció la sesión y la consola sigue allí: se vuelve a enganchar a la misma.
|
|
983
|
+
if (m.code === 'session-resumed' && consoleId) {
|
|
984
|
+
switching = false; recovering = true
|
|
985
|
+
conn.send({ type: 'attach', id: consoleId, ...size(), ...tag() })
|
|
986
|
+
return
|
|
987
|
+
}
|
|
978
988
|
// FALLÓ UN CAMBIO DE CONSOLA (la que se pidió ya no existe: el panel de la app iba un
|
|
979
989
|
// instante por detrás). El agente rechaza ANTES de soltar la actual, así que esta ventana
|
|
980
990
|
// sigue en la suya: se vuelve a pintar y se sigue. Terminar aquí cerraba la ventana entera
|
package/downloads.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* downloads.js — un archivo de ESTA máquina, para abrirlo en el aparato que mira la consola.
|
|
3
|
+
*
|
|
4
|
+
* Para qué: la consola enseña rutas (`/tmp/informe.pdf`, `~/fotos/a.png`) y desde el teléfono o
|
|
5
|
+
* el navegador no hay otra forma de ver ese archivo. El aparato la pide por la sesión (cifrada,
|
|
6
|
+
* como todo lo demás) y se le manda en trozos.
|
|
7
|
+
*
|
|
8
|
+
* Quien llega hasta aquí ya tiene una shell en la máquina, así que leer un archivo no le da nada
|
|
9
|
+
* que no tuviera con `cat`. Lo que se cuida es otra cosa: solo archivos normales (ni carpetas ni
|
|
10
|
+
* dispositivos) y con tope de tamaño, para que una ruta mal leída no se ponga a mandar gigas.
|
|
11
|
+
*
|
|
12
|
+
* Sin estado: cada trozo se pide con la ruta y su número (`seq`), y cada respuesta lleva el
|
|
13
|
+
* tamaño y la fecha del archivo; si cambian a mitad, quien pide lo ve y para.
|
|
14
|
+
*/
|
|
15
|
+
import fs from 'node:fs'
|
|
16
|
+
import os from 'node:os'
|
|
17
|
+
import path from 'node:path'
|
|
18
|
+
|
|
19
|
+
/** Lo más que pesa un archivo que se manda. */
|
|
20
|
+
export const FETCH_MAX = 50 * 1024 * 1024
|
|
21
|
+
/** Lo que lleva cada trozo, antes del base64 (el mismo tamaño que las subidas del cliente). */
|
|
22
|
+
export const FETCH_CHUNK = 96 * 1024
|
|
23
|
+
|
|
24
|
+
const fail = (code, message) => Object.assign(new Error(message), { code })
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* La ruta absoluta de lo que se pide. `~` es la carpeta personal; una ruta relativa cuelga de
|
|
28
|
+
* `cwd` (la carpeta de la shell de la consola), y sin ella no se adivina: se dice.
|
|
29
|
+
*/
|
|
30
|
+
export function resolvePath (p, cwd = null) {
|
|
31
|
+
if (typeof p !== 'string' || !p || p.length > 4096 || p.includes('\0')) throw fail('bad-fetch', 'fetch: needs a `path`')
|
|
32
|
+
const home = os.homedir()
|
|
33
|
+
const expand = (s) => s === '~' ? home : s.startsWith('~/') ? path.join(home, s.slice(2)) : s
|
|
34
|
+
const full = expand(p)
|
|
35
|
+
if (path.isAbsolute(full)) return path.normalize(full)
|
|
36
|
+
if (!cwd) throw fail('no-cwd', `fetch: ${p} is relative and the folder of the console is unknown`)
|
|
37
|
+
return path.resolve(expand(cwd), full)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Un trozo del archivo.
|
|
42
|
+
* @param {{ id: string, path: string, seq: number }} msg
|
|
43
|
+
* @param {string|null} cwd la carpeta de la consola enganchada, para las rutas relativas.
|
|
44
|
+
* @returns {{ id: string, seq: number, path: string, name: string, size: number, mtime: number, data: string, last?: true }}
|
|
45
|
+
*/
|
|
46
|
+
export function readPiece (msg, cwd = null) {
|
|
47
|
+
const id = typeof msg.id === 'string' ? msg.id.slice(0, 64) : ''
|
|
48
|
+
const seq = msg.seq
|
|
49
|
+
if (!id || !Number.isInteger(seq) || seq < 0) throw fail('bad-fetch', 'fetch: needs `id`, `path` and `seq`')
|
|
50
|
+
const file = resolvePath(msg.path, cwd)
|
|
51
|
+
let st
|
|
52
|
+
try { st = fs.statSync(file) } catch (e) {
|
|
53
|
+
if (e.code === 'ENOENT' || e.code === 'ENOTDIR') throw fail('no-file', `no such file: ${file}`)
|
|
54
|
+
if (e.code === 'EACCES' || e.code === 'EPERM') throw fail('no-read', `not allowed to read: ${file}`)
|
|
55
|
+
throw e
|
|
56
|
+
}
|
|
57
|
+
if (st.isDirectory()) throw fail('is-dir', `that is a folder: ${file}`)
|
|
58
|
+
if (!st.isFile()) throw fail('not-file', `that is not a regular file: ${file}`)
|
|
59
|
+
if (st.size > FETCH_MAX) throw fail('too-big', `the file is over ${FETCH_MAX / 1024 / 1024} MB`)
|
|
60
|
+
const at = seq * FETCH_CHUNK
|
|
61
|
+
if (at > st.size || (at === st.size && st.size > 0)) throw fail('bad-fetch', 'fetch: that piece is past the end of the file')
|
|
62
|
+
const len = Math.min(FETCH_CHUNK, st.size - at)
|
|
63
|
+
const buf = Buffer.alloc(len)
|
|
64
|
+
let fd
|
|
65
|
+
try { fd = fs.openSync(file, 'r') } catch (e) {
|
|
66
|
+
if (e.code === 'EACCES' || e.code === 'EPERM') throw fail('no-read', `not allowed to read: ${file}`)
|
|
67
|
+
throw e
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
let got = 0
|
|
71
|
+
while (got < len) {
|
|
72
|
+
const n = fs.readSync(fd, buf, got, len - got, at + got)
|
|
73
|
+
if (n === 0) break
|
|
74
|
+
got += n
|
|
75
|
+
}
|
|
76
|
+
if (got < len) throw fail('changed', 'the file got shorter while it was being sent')
|
|
77
|
+
} finally { fs.closeSync(fd) }
|
|
78
|
+
const last = at + len >= st.size
|
|
79
|
+
return { id, seq, path: file, name: path.basename(file), size: st.size, mtime: st.mtimeMs, data: buf.toString('base64'), ...(last ? { last: true } : {}) }
|
|
80
|
+
}
|
package/far.js
CHANGED
|
@@ -166,10 +166,14 @@ export function makeFar ({ dir, loadLink, kind, discover = discoverReal, connect
|
|
|
166
166
|
idle.unref?.()
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
-
/**
|
|
169
|
+
/**
|
|
170
|
+
* Nadie pregunta: se sueltan las conexiones. La lista se olvida para no enseñar algo viejo, y
|
|
171
|
+
* las claves también: desconectado es desconectado, y la próxima vez se vuelven a escribir.
|
|
172
|
+
*/
|
|
170
173
|
function release () {
|
|
171
174
|
for (const m of machines.values()) drop(m)
|
|
172
175
|
machines.clear()
|
|
176
|
+
codes.clear()
|
|
173
177
|
try { state.probe?.close() } catch (_) {}
|
|
174
178
|
state.probe = null
|
|
175
179
|
discoveredAt = 0; refreshedAt = 0
|
|
@@ -247,14 +251,35 @@ export function makeFar ({ dir, loadLink, kind, discover = discoverReal, connect
|
|
|
247
251
|
return
|
|
248
252
|
}
|
|
249
253
|
if (gone) { try { rc.close() } catch (_) {} return }
|
|
254
|
+
let attached = null // la consola que mira la ventana allí
|
|
250
255
|
rc.on('message', (p) => {
|
|
256
|
+
if (p?.type === 'attached') attached = p.id
|
|
251
257
|
if (p?.type === 'unlocked' && typed != null) { codes.set(m.sub, typed); typed = null }
|
|
252
258
|
if (p?.type === 'fail' && (p.code === 'bad-code' || p.code === 'wait')) typed = null
|
|
253
259
|
session.send(p)
|
|
254
260
|
})
|
|
255
261
|
rc.on('error', (e) => session.send({ type: 'fail', code: e.code || 'far-error', message: e.message }))
|
|
256
|
-
//
|
|
257
|
-
|
|
262
|
+
// LA SESIÓN CON LA OTRA MÁQUINA SE PERDIÓ y se abrió otra: o la máquina se reinició, o la
|
|
263
|
+
// sesión venció (30 min sin mensajes: una consola quieta). Se pregunta a la sesión nueva:
|
|
264
|
+
// - pide la clave: DESCONECTADA. La clave guardada se olvida y el panel suelta su conexión,
|
|
265
|
+
// así deja de enseñar sus consolas y vuelve a salir «Escribir la clave» (dueño,
|
|
266
|
+
// 2026-10-27). Antes el panel la reenviaba solo y las consolas seguían a la vista.
|
|
267
|
+
// - la consola de la ventana sigue allí: solo venció la sesión, la ventana se vuelve a enganchar.
|
|
268
|
+
// - no está: la máquina se reinició y sus consolas ya no existen. Se dice; no se inventa otra.
|
|
269
|
+
rc.on('resumed', () => {
|
|
270
|
+
ask(rc, { type: 'list' }, 'consoles').then((r) => {
|
|
271
|
+
if (r.type === 'fail' && r.code === 'locked') {
|
|
272
|
+
codes.delete(m.sub)
|
|
273
|
+
const panel = machines.get(m.sub)
|
|
274
|
+
if (panel) { drop(panel); panel.consoles = null; panel.locked = true; panel.error = null }
|
|
275
|
+
session.send({ type: 'fail', code: 'locked-again', message: 'that machine asks for its code again' })
|
|
276
|
+
} else if (r.type === 'consoles' && attached && r.list.some((c) => c.id === attached)) {
|
|
277
|
+
session.send({ type: 'fail', code: 'session-resumed', message: 'the session with that machine was renewed' })
|
|
278
|
+
} else {
|
|
279
|
+
session.send({ type: 'fail', code: 'machine-restarted', message: 'that machine restarted: its consoles are gone' })
|
|
280
|
+
}
|
|
281
|
+
}, (e) => session.send({ type: 'fail', code: e.code || 'unreachable', message: e.message }))
|
|
282
|
+
})
|
|
258
283
|
session.send({ type: 'via', machine: { sub: m.sub, id: m.id, name: m.name } })
|
|
259
284
|
for (const msg of queue.splice(0)) forward(msg)
|
|
260
285
|
}
|
package/index.js
CHANGED
|
@@ -21,12 +21,16 @@
|
|
|
21
21
|
* { type:'unlock', code } (la clave de la máquina, si tiene: access.js) ·
|
|
22
22
|
* { type:'file', up, seq, data, last? } (una imagen, en trozos base64 y en
|
|
23
23
|
* orden; cada trozo se acusa con `filed`, y el último trae la `path` donde
|
|
24
|
-
* quedó en esta máquina: uploads.js)
|
|
24
|
+
* quedó en esta máquina: uploads.js) ·
|
|
25
|
+
* { type:'fetch', id, path, seq } (un trozo de un archivo de esta máquina, para
|
|
26
|
+
* abrirlo en el aparato; una ruta relativa cuelga de la carpeta de la consola
|
|
27
|
+
* enganchada: downloads.js)
|
|
25
28
|
* agente → cliente: { type:'consoles', list } · { type:'attached', id, fresh } ·
|
|
26
29
|
* { type:'replay', id, data, last } · { type:'out', id, data } ·
|
|
27
30
|
* { type:'exit', code, closedBy? } · { type:'fail', code, message, retryMs? } ·
|
|
28
31
|
* { type:'unlocked' } · { type:'noted', console } ·
|
|
29
|
-
* { type:'filed', up, seq, path? }
|
|
32
|
+
* { type:'filed', up, seq, path? } ·
|
|
33
|
+
* { type:'fetched', id, seq, path, name, size, mtime, data, last? }
|
|
30
34
|
*
|
|
31
35
|
* Con clave puesta, una sesión remota que no la ha escrito recibe `fail` con `code:'locked'`
|
|
32
36
|
* a todo lo que pida; tras `unlock` vale para esa conexión. `bad-code` y `wait` traen
|
|
@@ -40,11 +44,12 @@ import { startRemoteAgent } from '@dotrino/remote-agent/agent'
|
|
|
40
44
|
import { dataDir, loadLink, LABEL } from './link.js'
|
|
41
45
|
import { listTransports, netReport } from '@dotrino/proxy-client'
|
|
42
46
|
import { shortId } from './far.js'
|
|
43
|
-
import { ConsoleHub, pieces, isTyped } from './consoles.js'
|
|
47
|
+
import { ConsoleHub, pieces, isTyped, cwdOf } from './consoles.js'
|
|
44
48
|
import { listenLocal } from './local.js'
|
|
45
49
|
import { makeGate } from './access.js'
|
|
46
50
|
import { makeFar } from './far.js'
|
|
47
51
|
import { makeUploads } from './uploads.js'
|
|
52
|
+
import { readPiece } from './downloads.js'
|
|
48
53
|
|
|
49
54
|
const require = createRequire(import.meta.url)
|
|
50
55
|
const { version: VERSION } = require('./package.json')
|
|
@@ -254,6 +259,14 @@ export function serveSession (session, hub, { origin = 'remote', gate = null, up
|
|
|
254
259
|
}
|
|
255
260
|
return
|
|
256
261
|
}
|
|
262
|
+
// Un archivo de esta máquina, para abrirlo en el aparato (una ruta que salió en la consola).
|
|
263
|
+
if (msg.type === 'fetch') {
|
|
264
|
+
try { session.send({ type: 'fetched', ...readPiece(msg, current ? cwdOf(current.pty?.pid) : null) }) } catch (e) {
|
|
265
|
+
if (!e.code || !['bad-fetch', 'no-cwd', 'no-file', 'no-read', 'is-dir', 'not-file', 'too-big', 'changed'].includes(e.code)) console.error('[terminal-agent] could not send a file:', e.message)
|
|
266
|
+
fail(e.code || 'fetch-failed', e.message)
|
|
267
|
+
}
|
|
268
|
+
return
|
|
269
|
+
}
|
|
257
270
|
// El orden del panel: es de la máquina, así que lo ve igual cualquier pantalla.
|
|
258
271
|
if (msg.type === 'move') {
|
|
259
272
|
if (!hub.move(String(msg.id), msg.before == null ? null : String(msg.before))) return fail('no-console', 'that console no longer exists')
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dotrino/terminal-agent",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.50.0",
|
|
4
4
|
"description": "Dotrino Terminal: las ventanas de esta máquina (dotrino-terminal) y su agente, que deja abrir esas mismas consolas desde los otros aparatos de tu cuenta, cifrado punto a punto.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"claude.js",
|
|
20
20
|
"consoles.js",
|
|
21
21
|
"desktop.js",
|
|
22
|
+
"downloads.js",
|
|
22
23
|
"link.js",
|
|
23
24
|
"local.js",
|
|
24
25
|
"service.js",
|
|
@@ -34,11 +35,11 @@
|
|
|
34
35
|
"type-check": "tsc --noEmit"
|
|
35
36
|
},
|
|
36
37
|
"dependencies": {
|
|
37
|
-
"@dotrino/identity": "0.
|
|
38
|
-
"@dotrino/proxy-client": "0.
|
|
38
|
+
"@dotrino/identity": "0.131.0",
|
|
39
|
+
"@dotrino/proxy-client": "0.45.1",
|
|
39
40
|
"@dotrino/remote-agent": "0.25.0",
|
|
40
41
|
"@dotrino/update": "0.9.2",
|
|
41
|
-
"@dotrino/vault": "0.
|
|
42
|
+
"@dotrino/vault": "0.109.2",
|
|
42
43
|
"@xterm/addon-serialize": "0.13.0",
|
|
43
44
|
"@xterm/headless": "5.5.0",
|
|
44
45
|
"jsonc-parser": "3.3.1",
|