@dotrino/terminal-agent 0.7.0 → 0.8.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/README.md +1 -0
- package/bin/terminal.js +94 -4
- package/index.js +5 -2
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -29,6 +29,7 @@ dotrino-terminal attach <id> # retomar una
|
|
|
29
29
|
dotrino-terminal kill <id>
|
|
30
30
|
dotrino-terminal profiles [--json] # los perfiles (agentes con nombre) de esta máquina
|
|
31
31
|
dotrino-terminal link [--name <n>] # enlazar un perfil con tu bóveda
|
|
32
|
+
dotrino-terminal rename <perfil> [nuevo] # renombrarlo (mueve su carpeta y para su agente; mismo aparato)
|
|
32
33
|
```
|
|
33
34
|
|
|
34
35
|
Cerrar la ventana cierra su consola, como cualquier terminal; **Ctrl+] y luego d** la suelta
|
package/bin/terminal.js
CHANGED
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
* dotrino-terminal kill <id> [--name <n>] cierra una consola
|
|
9
9
|
* dotrino-terminal profiles [--json] los perfiles de esta máquina (enlazados o no)
|
|
10
10
|
* dotrino-terminal link [--name <n>] enlaza un perfil con tu bóveda
|
|
11
|
+
dotrino-terminal rename <perfil> [nuevo] renombra un perfil (para su agente: cierra sus consolas)
|
|
12
|
+
* dotrino-terminal rename <perfil> [nuevo] renombra un perfil (para su agente si corre)
|
|
11
13
|
*
|
|
12
14
|
* Un PERFIL es un agente con nombre (`~/.dotrino/agent/terminal-agent/<nombre>/`), enlazado a
|
|
13
15
|
* una bóveda o solo local. Cada consola vive en el agente de un perfil.
|
|
@@ -50,6 +52,7 @@ if (args.includes('-h') || args.includes('--help')) {
|
|
|
50
52
|
dotrino-terminal kill <id> [--name <n>] cierra una consola
|
|
51
53
|
dotrino-terminal profiles [--json] los perfiles de esta máquina
|
|
52
54
|
dotrino-terminal link [--name <n>] enlaza un perfil con tu bóveda
|
|
55
|
+
dotrino-terminal rename <perfil> [nuevo] renombra un perfil (para su agente: cierra sus consolas)
|
|
53
56
|
|
|
54
57
|
Dentro de una consola: Ctrl+] y luego d la suelta sin cerrarla.
|
|
55
58
|
Cerrar la ventana cierra la consola que abrió.`, `usage:
|
|
@@ -59,6 +62,7 @@ Cerrar la ventana cierra la consola que abrió.`, `usage:
|
|
|
59
62
|
dotrino-terminal kill <id> [--name <n>] close a console
|
|
60
63
|
dotrino-terminal profiles [--json] this machine's profiles
|
|
61
64
|
dotrino-terminal link [--name <n>] link a profile with your vault
|
|
65
|
+
dotrino-terminal rename <profile> [new] rename a profile (stops its agent: closes its consoles)
|
|
62
66
|
|
|
63
67
|
Inside a console: Ctrl+] then d detaches without closing it.
|
|
64
68
|
Closing the window closes the console it opened.`))
|
|
@@ -138,6 +142,87 @@ async function link () {
|
|
|
138
142
|
console.log(t(`Perfil «${name}» enlazado.`, `Profile "${name}" linked.`))
|
|
139
143
|
}
|
|
140
144
|
|
|
145
|
+
function ask (q) {
|
|
146
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
|
|
147
|
+
return new Promise((resolve) => rl.question(q, (a) => { rl.close(); resolve(a.trim()) }))
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Renombra un perfil: su carpeta, nada más. Es el mismo aparato con la misma llave, así que el
|
|
152
|
+
* acta no se entera ni hace falta volver a enrolar. El nombre es solo tuyo, de esta máquina.
|
|
153
|
+
*
|
|
154
|
+
* Si su agente está corriendo hay que pararlo (las ventanas lo encuentran por el socket de esa
|
|
155
|
+
* carpeta), y eso cierra sus consolas: si quedan abiertas, se dice cuántas y se pregunta.
|
|
156
|
+
*/
|
|
157
|
+
async function rename () {
|
|
158
|
+
const from = args[1]
|
|
159
|
+
if (!from) die(t('falta el perfil: dotrino-terminal rename <perfil> [nuevo]', 'missing profile: dotrino-terminal rename <profile> [new]'))
|
|
160
|
+
if (!isValidName(from)) die(t(`nombre no válido: «${from}»`, `invalid name: "${from}"`))
|
|
161
|
+
const { dir } = resolveInstance(LABEL, from)
|
|
162
|
+
if (!fs.existsSync(dir)) die(t(`no existe el perfil «${from}»`, `there is no profile "${from}"`))
|
|
163
|
+
let to = args[2] && !args[2].startsWith('-') ? args[2] : await ask(t(`Nuevo nombre para «${from}» (a-z, 0-9, -): `, `New name for "${from}" (a-z, 0-9, -): `))
|
|
164
|
+
if (!isValidName(to)) die(t(`nombre no válido: «${to}» (usa a-z, 0-9 y -, hasta 32)`, `invalid name: "${to}" (use a-z, 0-9 and -, up to 32)`))
|
|
165
|
+
if (to === from) die(t('es el mismo nombre', 'that is the same name'))
|
|
166
|
+
const target = resolveInstance(LABEL, to).dir
|
|
167
|
+
if (fs.existsSync(target)) die(t(`ya hay un perfil «${to}»`, `there is already a profile "${to}"`))
|
|
168
|
+
|
|
169
|
+
const pidFile = path.join(dir, 'agent.pid')
|
|
170
|
+
const pid = Number((() => { try { return fs.readFileSync(pidFile, 'utf8') } catch (_) { return '' } })())
|
|
171
|
+
const running = pid && alive(pid)
|
|
172
|
+
// ¿Corre esta orden DENTRO de una consola del perfil que se renombra? Entonces parar su
|
|
173
|
+
// agente cerraría esta misma consola a mitad de camino: el trabajo lo hace un proceso aparte.
|
|
174
|
+
const inside = !!process.env.DOTRINO_TERMINAL_PROFILE_DIR && samePath(process.env.DOTRINO_TERMINAL_PROFILE_DIR, dir)
|
|
175
|
+
if (running && !args.includes('--yes')) {
|
|
176
|
+
let open = 0
|
|
177
|
+
try {
|
|
178
|
+
const conn = await connectLocal(dir)
|
|
179
|
+
open = ((await request(conn, { type: 'list' }, 'consoles')).list || []).length
|
|
180
|
+
conn.close()
|
|
181
|
+
} catch (_) { /* agente viejo sin socket: igual hay que pararlo */ }
|
|
182
|
+
const others = open - (inside ? 1 : 0)
|
|
183
|
+
if (others > 0) {
|
|
184
|
+
const ok = await ask(t(`El perfil «${from}» tiene ${others} consola(s) abierta(s) más; renombrarlo las cierra. ¿Seguir? [s/N] `, `Profile "${from}" has ${others} more open console(s); renaming it closes them. Continue? [y/N] `))
|
|
185
|
+
if (!/^[sy]/i.test(ok)) die(t('No se renombró.', 'Not renamed.'))
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (running && inside && !args.includes('--detached-step')) {
|
|
189
|
+
const self = fileURLToPath(import.meta.url)
|
|
190
|
+
spawn(process.execPath, [self, 'rename', from, to, '--yes', '--detached-step'], { detached: true, stdio: 'ignore', env: process.env }).unref()
|
|
191
|
+
console.log(t(`Renombrando «${from}» → «${to}». Esta consola es de ese perfil y se va a cerrar; la ventana sigue en «${to}».`,
|
|
192
|
+
`Renaming "${from}" → "${to}". This console belongs to that profile and is about to close; the window continues in "${to}".`))
|
|
193
|
+
return
|
|
194
|
+
}
|
|
195
|
+
// PRIMERO se mueve la carpeta y DESPUÉS se para el agente: así quien estaba mirando ve que el
|
|
196
|
+
// perfil ya cambió de nombre cuando se le cae la conexión (y no lo confunde con un fallo).
|
|
197
|
+
// Mover la carpeta con el agente vivo no rompe nada: el socket y el pid se van con ella.
|
|
198
|
+
fs.renameSync(dir, target)
|
|
199
|
+
if (running) {
|
|
200
|
+
process.kill(pid, 'SIGTERM')
|
|
201
|
+
const t0 = Date.now()
|
|
202
|
+
while (alive(pid)) {
|
|
203
|
+
if (Date.now() - t0 > 5000) die(t(`renombrado, pero el agente (pid ${pid}) no se detuvo: páralo a mano`, `renamed, but the agent (pid ${pid}) did not stop: stop it by hand`))
|
|
204
|
+
await new Promise((r) => setTimeout(r, 100))
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// Lo que dejó el agente viejo (su pid y su socket ya no atienden): el próximo arranca limpio.
|
|
208
|
+
fs.rmSync(path.join(target, 'agent.pid'), { force: true })
|
|
209
|
+
fs.rmSync(path.join(target, 'terminal.sock'), { force: true })
|
|
210
|
+
// La app de escritorio recuerda el último perfil elegido: si era este, pasa al nombre nuevo.
|
|
211
|
+
const last = lastProfileFile()
|
|
212
|
+
try { if (fs.readFileSync(last, 'utf8').trim() === from) fs.writeFileSync(last, to) } catch (_) { /* sin preferencia guardada */ }
|
|
213
|
+
console.log(t(`Perfil «${from}» → «${to}». Es el mismo aparato: no hace falta volver a enrolar.`, `Profile "${from}" → "${to}". Same device: no need to enroll again.`))
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function samePath (a, b) {
|
|
217
|
+
try { return fs.realpathSync(a) === fs.realpathSync(b) } catch (_) { return path.resolve(a) === path.resolve(b) }
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** La preferencia de la app de escritorio: el último perfil elegido en su menú. */
|
|
221
|
+
function lastProfileFile () {
|
|
222
|
+
const base = process.env.XDG_CONFIG_HOME || path.join(process.env.HOME || '', '.config')
|
|
223
|
+
return path.join(base, 'dotrino-terminal', 'last-profile')
|
|
224
|
+
}
|
|
225
|
+
|
|
141
226
|
/** Una pregunta y su respuesta, para `ls` y `kill`. */
|
|
142
227
|
function request (conn, msg, type) {
|
|
143
228
|
return new Promise((resolve) => {
|
|
@@ -163,7 +248,7 @@ async function list (conn) {
|
|
|
163
248
|
}
|
|
164
249
|
|
|
165
250
|
/** La ventana: la TTY enganchada a una consola del agente. */
|
|
166
|
-
function interactive (conn, first) {
|
|
251
|
+
function interactive (conn, first, dir) {
|
|
167
252
|
if (!process.stdin.isTTY || !process.stdout.isTTY) die(t('dotrino-terminal necesita una terminal (TTY).', 'dotrino-terminal needs a terminal (TTY).'))
|
|
168
253
|
const out = process.stdout
|
|
169
254
|
let consoleId = null
|
|
@@ -206,7 +291,11 @@ function interactive (conn, first) {
|
|
|
206
291
|
if (m.type === 'exit') { finish(m.code || 0); return }
|
|
207
292
|
if (m.type === 'fail') { finish(1, `dotrino-terminal: ${m.message} (${m.code})`) }
|
|
208
293
|
})
|
|
209
|
-
conn.on('close', () =>
|
|
294
|
+
conn.on('close', () => {
|
|
295
|
+
// Si se fue porque el perfil cambió de nombre (`rename`), no es un error: se dice y se sale.
|
|
296
|
+
if (!fs.existsSync(dir)) return finish(0, t('Este perfil cambió de nombre.', 'This profile was renamed.'))
|
|
297
|
+
finish(1, t('dotrino-terminal: el agente se detuvo.', 'dotrino-terminal: the agent stopped.'))
|
|
298
|
+
})
|
|
210
299
|
|
|
211
300
|
process.stdin.setRawMode(true)
|
|
212
301
|
process.stdin.resume()
|
|
@@ -240,6 +329,7 @@ function interactive (conn, first) {
|
|
|
240
329
|
try {
|
|
241
330
|
if (cmd === 'profiles') { await profiles(); process.exit(0) }
|
|
242
331
|
if (cmd === 'link') { await link(); process.exit(0) }
|
|
332
|
+
if (cmd === 'rename') { await rename(); process.exit(0) }
|
|
243
333
|
const dir = opt('--dir') || dataDir(opt('--name'))
|
|
244
334
|
const conn = await agent(dir)
|
|
245
335
|
if (cmd === 'ls' || cmd === 'list') { await list(conn); conn.close() } else if (cmd === 'kill') {
|
|
@@ -250,10 +340,10 @@ try {
|
|
|
250
340
|
conn.close()
|
|
251
341
|
} else if (cmd === 'attach') {
|
|
252
342
|
if (!args[1]) die(t('falta el id: dotrino-terminal attach <id> (mira «dotrino-terminal ls»)', 'missing id: dotrino-terminal attach <id> (see "dotrino-terminal ls")'))
|
|
253
|
-
interactive(conn, { type: 'attach', id: args[1] })
|
|
343
|
+
interactive(conn, { type: 'attach', id: args[1] }, dir)
|
|
254
344
|
} else if (cmd === 'open') {
|
|
255
345
|
// Como cualquier terminal: la consola abre en la carpeta donde estás (o en `--cwd`).
|
|
256
|
-
interactive(conn, { type: 'open', cwd: path.resolve(opt('--cwd') || process.cwd()) })
|
|
346
|
+
interactive(conn, { type: 'open', cwd: path.resolve(opt('--cwd') || process.cwd()) }, dir)
|
|
257
347
|
} else die(t(`orden desconocida: ${cmd} (mira --help)`, `unknown command: ${cmd} (see --help)`))
|
|
258
348
|
} catch (e) {
|
|
259
349
|
if (e !== HOLD) { try { die(`dotrino-terminal: ${e.message}`) } catch (_) {} }
|
package/index.js
CHANGED
|
@@ -51,7 +51,10 @@ export function makeHub (pty, opts = {}) {
|
|
|
51
51
|
return new ConsoleHub({
|
|
52
52
|
spawn: ({ cols, rows, cwd }) => pty.spawn(shell, [], {
|
|
53
53
|
name: 'xterm-256color', cols, rows,
|
|
54
|
-
cwd: cwd || os.homedir(),
|
|
54
|
+
cwd: cwd || os.homedir(),
|
|
55
|
+
// La carpeta del perfil viaja a la shell: `dotrino-terminal rename` sabe así si corre
|
|
56
|
+
// dentro del mismo perfil que renombra (y que parar su agente cerraría su consola).
|
|
57
|
+
env: { ...process.env, TERM: 'xterm-256color', ...(opts.dir ? { DOTRINO_TERMINAL_PROFILE_DIR: opts.dir } : {}) }
|
|
55
58
|
})
|
|
56
59
|
})
|
|
57
60
|
}
|
|
@@ -156,7 +159,7 @@ export function serveSession (session, hub, { origin = 'remote' } = {}) {
|
|
|
156
159
|
*/
|
|
157
160
|
export async function startAgent (opts = {}) {
|
|
158
161
|
const dir = opts.dir || dataDir()
|
|
159
|
-
const hub = makeHub(loadPty(), opts)
|
|
162
|
+
const hub = makeHub(loadPty(), { ...opts, dir })
|
|
160
163
|
const local = await listenLocal({ dir, serve: (session) => serveSession(session, hub, { origin: 'local' }) })
|
|
161
164
|
let remote = null
|
|
162
165
|
let stopped = false
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dotrino/terminal-agent",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.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": {
|
|
@@ -24,11 +24,11 @@
|
|
|
24
24
|
"type-check": "tsc --noEmit"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@dotrino/identity": "0.
|
|
28
|
-
"@dotrino/proxy-client": "0.
|
|
29
|
-
"@dotrino/remote-agent": "0.12.
|
|
27
|
+
"@dotrino/identity": "0.107.2",
|
|
28
|
+
"@dotrino/proxy-client": "0.27.1",
|
|
29
|
+
"@dotrino/remote-agent": "0.12.1",
|
|
30
30
|
"@dotrino/update": "0.2.2",
|
|
31
|
-
"@dotrino/vault": "0.
|
|
31
|
+
"@dotrino/vault": "0.79.0",
|
|
32
32
|
"@homebridge/node-pty-prebuilt-multiarch": "0.14.1-beta.2",
|
|
33
33
|
"@xterm/addon-serialize": "0.13.0",
|
|
34
34
|
"@xterm/headless": "5.5.0"
|