@andy.ian/dsh-homing 0.0.0-stage → 2.0.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 +21 -0
- package/README.md +71 -2
- package/bin/auth.mjs +159 -0
- package/bin/backup.mjs +118 -0
- package/bin/bootstrap-server.mjs +436 -0
- package/bin/entry-link.mjs +69 -0
- package/bin/nginx-dsh.template.conf +91 -0
- package/bin/nginx-gate.conf +9 -0
- package/bin/nginx-ws-map.conf +5 -0
- package/bin/renew-cert.mjs +127 -0
- package/bin/watchdog.mjs +158 -0
- package/cordis.patch.yml +30 -0
- package/icon.svg +9 -0
- package/index.js +3339 -0
- package/lib/csrf.mjs +60 -0
- package/lib/devices.mjs +221 -0
- package/lib/files-page.mjs +391 -0
- package/lib/files.mjs +464 -0
- package/lib/git.mjs +154 -0
- package/lib/limits.mjs +135 -0
- package/lib/pty.mjs +354 -0
- package/lib/session.mjs +216 -0
- package/lib/shell-page.mjs +430 -0
- package/lib/shell.mjs +274 -0
- package/lib/term-app.js +318 -0
- package/lib/term-page.mjs +117 -0
- package/lib/vendor/fonts/JetBrainsMono-Regular.ttf +0 -0
- package/lib/vendor/fonts/jetbrains-mono-400.woff2 +0 -0
- package/lib/vendor/fonts/jetbrains-mono-700.woff2 +0 -0
- package/lib/vendor/node_modules/@xterm/addon-fit/LICENSE +19 -0
- package/lib/vendor/node_modules/@xterm/addon-fit/README.md +24 -0
- package/lib/vendor/node_modules/@xterm/addon-fit/lib/addon-fit.js +2 -0
- package/lib/vendor/node_modules/@xterm/addon-fit/lib/addon-fit.js.map +1 -0
- package/lib/vendor/node_modules/@xterm/addon-fit/package.json +26 -0
- package/lib/vendor/node_modules/@xterm/addon-fit/src/FitAddon.ts +90 -0
- package/lib/vendor/node_modules/@xterm/addon-fit/typings/addon-fit.d.ts +55 -0
- package/lib/vendor/node_modules/asn1/Jenkinsfile +65 -0
- package/lib/vendor/node_modules/asn1/LICENSE +19 -0
- package/lib/vendor/node_modules/asn1/README.md +50 -0
- package/lib/vendor/node_modules/asn1/lib/ber/errors.js +13 -0
- package/lib/vendor/node_modules/asn1/lib/ber/index.js +27 -0
- package/lib/vendor/node_modules/asn1/lib/ber/reader.js +262 -0
- package/lib/vendor/node_modules/asn1/lib/ber/types.js +36 -0
- package/lib/vendor/node_modules/asn1/lib/ber/writer.js +317 -0
- package/lib/vendor/node_modules/asn1/lib/index.js +20 -0
- package/lib/vendor/node_modules/asn1/package.json +31 -0
- package/lib/vendor/node_modules/bcrypt-pbkdf/CONTRIBUTING.md +13 -0
- package/lib/vendor/node_modules/bcrypt-pbkdf/LICENSE +66 -0
- package/lib/vendor/node_modules/bcrypt-pbkdf/README.md +45 -0
- package/lib/vendor/node_modules/bcrypt-pbkdf/index.js +556 -0
- package/lib/vendor/node_modules/bcrypt-pbkdf/package.json +15 -0
- package/lib/vendor/node_modules/safer-buffer/LICENSE +21 -0
- package/lib/vendor/node_modules/safer-buffer/Porting-Buffer.md +268 -0
- package/lib/vendor/node_modules/safer-buffer/Readme.md +156 -0
- package/lib/vendor/node_modules/safer-buffer/dangerous.js +58 -0
- package/lib/vendor/node_modules/safer-buffer/package.json +34 -0
- package/lib/vendor/node_modules/safer-buffer/safer.js +77 -0
- package/lib/vendor/node_modules/safer-buffer/tests.js +406 -0
- package/lib/vendor/node_modules/ssh2/.eslintignore +4 -0
- package/lib/vendor/node_modules/ssh2/.eslintrc.js +5 -0
- package/lib/vendor/node_modules/ssh2/.github/workflows/ci.yml +110 -0
- package/lib/vendor/node_modules/ssh2/.github/workflows/lint.yml +27 -0
- package/lib/vendor/node_modules/ssh2/LICENSE +19 -0
- package/lib/vendor/node_modules/ssh2/README.md +1529 -0
- package/lib/vendor/node_modules/ssh2/SFTP.md +413 -0
- package/lib/vendor/node_modules/ssh2/examples/server-chat.js +238 -0
- package/lib/vendor/node_modules/ssh2/examples/sftp-server-download-only.js +134 -0
- package/lib/vendor/node_modules/ssh2/install.js +27 -0
- package/lib/vendor/node_modules/ssh2/lib/Channel.js +295 -0
- package/lib/vendor/node_modules/ssh2/lib/agent.js +1123 -0
- package/lib/vendor/node_modules/ssh2/lib/client.js +2176 -0
- package/lib/vendor/node_modules/ssh2/lib/http-agents.js +84 -0
- package/lib/vendor/node_modules/ssh2/lib/index.js +44 -0
- package/lib/vendor/node_modules/ssh2/lib/keygen.js +582 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/Protocol.js +2136 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/SFTP.js +4052 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/constants.js +356 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/crypto/binding.gyp +23 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/crypto/poly1305.js +43 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/crypto/src/binding.cc +2300 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/crypto.js +1602 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/handlers.js +16 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/handlers.misc.js +1285 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/kex.js +1908 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/keyParser.js +1484 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/node-fs-compat.js +115 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/utils.js +356 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/zlib.js +255 -0
- package/lib/vendor/node_modules/ssh2/lib/server.js +1380 -0
- package/lib/vendor/node_modules/ssh2/lib/utils.js +336 -0
- package/lib/vendor/node_modules/ssh2/package.json +49 -0
- package/lib/vendor/node_modules/ssh2/test/common.js +316 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/bad_rsa_private_key +26 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/https_cert.pem +33 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/https_key.pem +52 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/id_dsa +12 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/id_ecdsa +5 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/id_rsa +15 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/id_rsa.ppk +26 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/id_rsa_enc +30 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa +21 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc +22 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm +23 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa +9 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc +10 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc.pub.result +8 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm +10 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm.pub.result +8 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519 +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa +27 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc +28 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc.pub.result +8 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm +29 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm.pub.result +8 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa +12 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc +15 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc.pub.result +8 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa +5 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc +8 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc.pub.result +8 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa +27 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc +30 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc.pub.result +8 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256 +54 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256.pub.result +6 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256.result +6 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_dsa_enc +17 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_dsa_enc.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa +26 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa_enc +18 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa_enc.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa.pub +9 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa2.pub +10 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa2.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa3.pub +11 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa3.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa4.pub +11 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa4.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa5.pub +8 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa5.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa6.pub +13 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa6.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/openssh_new_rsa +27 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/ssh_host_dsa_key +12 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/ssh_host_ecdsa_key +5 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/ssh_host_rsa_key +15 -0
- package/lib/vendor/node_modules/ssh2/test/test-exec.js +578 -0
- package/lib/vendor/node_modules/ssh2/test/test-integration-openssh.js +486 -0
- package/lib/vendor/node_modules/ssh2/test/test-keygen.js +60 -0
- package/lib/vendor/node_modules/ssh2/test/test-misc-client-server.js +1460 -0
- package/lib/vendor/node_modules/ssh2/test/test-openssh.js +261 -0
- package/lib/vendor/node_modules/ssh2/test/test-protocol-crypto.js +631 -0
- package/lib/vendor/node_modules/ssh2/test/test-protocol-keyparser.js +177 -0
- package/lib/vendor/node_modules/ssh2/test/test-server-hostkeys.js +138 -0
- package/lib/vendor/node_modules/ssh2/test/test-sftp.js +842 -0
- package/lib/vendor/node_modules/ssh2/test/test-shell.js +109 -0
- package/lib/vendor/node_modules/ssh2/test/test-userauth-agent-openssh.js +110 -0
- package/lib/vendor/node_modules/ssh2/test/test-userauth-agent.js +171 -0
- package/lib/vendor/node_modules/ssh2/test/test-userauth.js +611 -0
- package/lib/vendor/node_modules/ssh2/test/test-worker-imports.js +25 -0
- package/lib/vendor/node_modules/ssh2/test/test.js +20 -0
- package/lib/vendor/node_modules/ssh2/util/build_pagent.bat +2 -0
- package/lib/vendor/node_modules/ssh2/util/pagent.c +88 -0
- package/lib/vendor/node_modules/ssh2/util/pagent.exe +0 -0
- package/lib/vendor/node_modules/tweetnacl/AUTHORS.md +27 -0
- package/lib/vendor/node_modules/tweetnacl/CHANGELOG.md +283 -0
- package/lib/vendor/node_modules/tweetnacl/LICENSE +24 -0
- package/lib/vendor/node_modules/tweetnacl/PULL_REQUEST_TEMPLATE.md +20 -0
- package/lib/vendor/node_modules/tweetnacl/README.md +494 -0
- package/lib/vendor/node_modules/tweetnacl/nacl-fast.js +2391 -0
- package/lib/vendor/node_modules/tweetnacl/nacl-fast.min.js +1 -0
- package/lib/vendor/node_modules/tweetnacl/nacl.d.ts +98 -0
- package/lib/vendor/node_modules/tweetnacl/nacl.js +1178 -0
- package/lib/vendor/node_modules/tweetnacl/nacl.min.js +1 -0
- package/lib/vendor/node_modules/tweetnacl/package.json +56 -0
- package/lib/vendor/xterm.css +218 -0
- package/lib/vendor/xterm.js +2 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/ops/watchdog/dsh-watchdog.ps1 +105 -0
- package/ops/watchdog/restart-now.ps1 +247 -0
- package/package.json +69 -4
package/lib/shell.mjs
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* shell 执行:给 /shell 页面用(手机端的 "Git Bash")。
|
|
3
|
+
*
|
|
4
|
+
* 这是**故意**执行任意命令的能力,所以边界写清楚:
|
|
5
|
+
* - 只有已登录会话能进(路由层校验);
|
|
6
|
+
* - 每条命令写审计(含工作目录与命令行);
|
|
7
|
+
* - 30 秒超时 + 64KB 输出上限;超时杀整棵进程树;
|
|
8
|
+
* - 优先用 Git Bash(不能用 WSL 的 bash:它把 D:\ 当成 /mnt/d,路径语义不同),没有就退回 cmd.exe;
|
|
9
|
+
* - 支持 `cd <目录>` 这类"改变当前目录"的指令(页面维持一个 cwd 字段)。
|
|
10
|
+
*/
|
|
11
|
+
import fs from 'node:fs'
|
|
12
|
+
import path from 'node:path'
|
|
13
|
+
import { spawn, spawnSync } from 'node:child_process'
|
|
14
|
+
|
|
15
|
+
const MAX_OUTPUT = 64 * 1024
|
|
16
|
+
const DEFAULT_TIMEOUT = 30_000
|
|
17
|
+
|
|
18
|
+
let bashPath = null
|
|
19
|
+
let bashChecked = false
|
|
20
|
+
|
|
21
|
+
/** 找一个可用的 bash;找不到返回 null(用 cmd 兜底) */
|
|
22
|
+
export function findBash() {
|
|
23
|
+
if (bashChecked) return bashPath
|
|
24
|
+
bashChecked = true
|
|
25
|
+
// Windows 上裸 bash 通常是 WSL 启动器(C:\Windows\System32\bash.exe),
|
|
26
|
+
// 它把 D:\\ 当成 /mnt/d,路径语义完全不同 —— 所以只认 Git Bash 的显式路径。
|
|
27
|
+
const candidates = process.platform === 'win32'
|
|
28
|
+
? [
|
|
29
|
+
'C:/Program Files/Git/bin/bash.exe',
|
|
30
|
+
'C:/Program Files/Git/usr/bin/bash.exe',
|
|
31
|
+
'C:/Program Files (x86)/Git/bin/bash.exe',
|
|
32
|
+
]
|
|
33
|
+
: ['bash', '/bin/bash'];
|
|
34
|
+
for (const candidate of candidates) {
|
|
35
|
+
try {
|
|
36
|
+
const result = spawnSync(candidate, ['-lc', 'echo ok'], { encoding: 'utf8', timeout: 5000, windowsHide: true })
|
|
37
|
+
if (result.status === 0 && String(result.stdout).includes('ok')) {
|
|
38
|
+
bashPath = candidate
|
|
39
|
+
return bashPath
|
|
40
|
+
}
|
|
41
|
+
} catch {
|
|
42
|
+
// 换下一个
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return null
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 把 `cd xxx` 解析成新目录;不是 cd 就返回 null */
|
|
49
|
+
export function parseCd(command, cwd) {
|
|
50
|
+
const match = /^\s*cd(?:\s+(.+))?\s*$/u.exec(String(command ?? ''))
|
|
51
|
+
if (match === null) return null
|
|
52
|
+
const raw = (match[1] ?? '').trim().replace(/^["']|["']$/gu, '')
|
|
53
|
+
if (raw === '') return path.resolve(cwd)
|
|
54
|
+
const expanded = raw.startsWith('~') ? path.join(process.env.USERPROFILE ?? process.env.HOME ?? '', raw.slice(1)) : raw
|
|
55
|
+
return path.resolve(cwd, expanded)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 执行一条命令 */
|
|
59
|
+
export function runShell(cwd, command) {
|
|
60
|
+
const text = String(command ?? '').trim()
|
|
61
|
+
if (text === '') return Promise.resolve({ ok: false, error: '命令为空' })
|
|
62
|
+
|
|
63
|
+
let workdir = cwd
|
|
64
|
+
try {
|
|
65
|
+
if (!fs.statSync(workdir).isDirectory()) workdir = process.cwd()
|
|
66
|
+
} catch {
|
|
67
|
+
workdir = process.cwd()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const gitBash = findBash()
|
|
71
|
+
const spawnArgs = gitBash === null
|
|
72
|
+
? { command: 'cmd.exe', argv: ['/d', '/s', '/c', text], verbatim: true }
|
|
73
|
+
: { command: gitBash, argv: ['-lc', text], verbatim: false }
|
|
74
|
+
|
|
75
|
+
return new Promise((resolve) => {
|
|
76
|
+
let child
|
|
77
|
+
try {
|
|
78
|
+
child = spawn(spawnArgs.command, spawnArgs.argv, {
|
|
79
|
+
cwd: workdir,
|
|
80
|
+
windowsHide: true,
|
|
81
|
+
windowsVerbatimArguments: spawnArgs.verbatim,
|
|
82
|
+
env: {
|
|
83
|
+
...process.env,
|
|
84
|
+
// 避免交互式程序卡住
|
|
85
|
+
GIT_TERMINAL_PROMPT: '0',
|
|
86
|
+
GIT_ASKPASS: 'echo',
|
|
87
|
+
GIT_PAGER: 'cat',
|
|
88
|
+
GIT_EDITOR: 'true',
|
|
89
|
+
PAGER: 'cat',
|
|
90
|
+
TERM: 'dumb',
|
|
91
|
+
LANG: 'C.UTF-8',
|
|
92
|
+
},
|
|
93
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
94
|
+
})
|
|
95
|
+
} catch (error) {
|
|
96
|
+
resolve({ ok: false, error: `启动失败:${error.message}`, cwd: workdir })
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let settled = false
|
|
101
|
+
const finish = (value) => {
|
|
102
|
+
if (settled) return
|
|
103
|
+
settled = true
|
|
104
|
+
resolve(value)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const chunks = []
|
|
108
|
+
let total = 0
|
|
109
|
+
const collect = (chunk) => {
|
|
110
|
+
if (total >= MAX_OUTPUT) return
|
|
111
|
+
chunks.push(chunk)
|
|
112
|
+
total += chunk.length
|
|
113
|
+
}
|
|
114
|
+
child.stdout.on('data', collect)
|
|
115
|
+
child.stderr.on('data', collect)
|
|
116
|
+
|
|
117
|
+
const decode = () => {
|
|
118
|
+
const buffer = Buffer.concat(chunks).subarray(0, MAX_OUTPUT)
|
|
119
|
+
const utf8 = buffer.toString('utf8')
|
|
120
|
+
if (!utf8.includes('\uFFFD')) return utf8
|
|
121
|
+
try {
|
|
122
|
+
return new TextDecoder('gbk').decode(buffer)
|
|
123
|
+
} catch {
|
|
124
|
+
return utf8
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const timer = setTimeout(() => {
|
|
129
|
+
try {
|
|
130
|
+
if (process.platform === 'win32' && child.pid) {
|
|
131
|
+
spawn('taskkill', ['/pid', String(child.pid), '/T', '/F'], { windowsHide: true, stdio: 'ignore' })
|
|
132
|
+
} else {
|
|
133
|
+
child.kill('SIGKILL')
|
|
134
|
+
}
|
|
135
|
+
} catch { /* 已退出 */ }
|
|
136
|
+
finish({ ok: false, code: null, output: decode() + '\n(超时,已终止)', cwd: workdir, timeout: true })
|
|
137
|
+
}, DEFAULT_TIMEOUT)
|
|
138
|
+
|
|
139
|
+
child.on('error', (error) => {
|
|
140
|
+
clearTimeout(timer)
|
|
141
|
+
finish({ ok: false, error: `启动失败:${error.message}`, cwd: workdir })
|
|
142
|
+
})
|
|
143
|
+
child.on('close', (code) => {
|
|
144
|
+
clearTimeout(timer)
|
|
145
|
+
const output = decode()
|
|
146
|
+
finish({ ok: code === 0, code, output: output === '' ? '(无输出)' : output, cwd: workdir })
|
|
147
|
+
})
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 常见命令(补全第一段用) */
|
|
152
|
+
const COMMON_CMDS = [
|
|
153
|
+
'git', 'ls', 'cd', 'pwd', 'cat', 'head', 'tail', 'grep', 'find', 'rg',
|
|
154
|
+
'node', 'npm', 'pnpm', 'python', 'pip', 'code', 'curl', 'ssh', 'scp',
|
|
155
|
+
'mkdir', 'rm', 'cp', 'mv', 'echo', 'touch', 'tar', 'unzip', 'git-bash',
|
|
156
|
+
];
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 补全候选:
|
|
160
|
+
* - 光标所在段没有空格(在打命令)→ 常见命令 + 当前目录下的可执行文件
|
|
161
|
+
* - 段里有路径成分(/ 或 \ 或 . 开头)→ 该目录下的匹配项(目录带结尾 /)
|
|
162
|
+
* 返回 [{ value, label, kind }],页面点一下就把最后一段替换掉。
|
|
163
|
+
*/
|
|
164
|
+
/** git 子命令(补全 git <Tab> 与 git stat<Tab> 用) */
|
|
165
|
+
const GIT_SUBCOMMANDS = [
|
|
166
|
+
'add', 'am', 'apply', 'archive', 'bisect', 'blame', 'branch', 'bundle', 'checkout',
|
|
167
|
+
'cherry-pick', 'clean', 'clone', 'commit', 'config', 'describe', 'diff', 'fetch',
|
|
168
|
+
'format-patch', 'fsck', 'gc', 'grep', 'help', 'init', 'log', 'ls-files',
|
|
169
|
+
'ls-remote', 'merge', 'mv', 'notes', 'pull', 'push', 'rebase', 'reflog', 'remote',
|
|
170
|
+
'reset', 'restore', 'revert', 'rm', 'shortlog', 'show', 'stash', 'status',
|
|
171
|
+
'submodule', 'switch', 'tag', 'worktree',
|
|
172
|
+
];
|
|
173
|
+
const GIT_BRANCH_SUBS = new Set(['checkout', 'switch', 'branch', 'merge', 'rebase', 'reset', 'diff', 'log', 'show', 'cherry-pick']);
|
|
174
|
+
const NPM_SUBCOMMANDS = ['install', 'run', 'test', 'add', 'remove', 'update', 'exec', 'init', 'publish', 'pack', 'audit', 'list', 'version'];
|
|
175
|
+
const PNPM_SUBCOMMANDS = ['install', 'run', 'test', 'add', 'remove', 'update', 'exec', 'init', 'publish', 'pack', 'list', 'why', 'dlx'];
|
|
176
|
+
|
|
177
|
+
/** git 官方子命令清单(git --list-cmds,比手写列表全;实测约 30ms) */
|
|
178
|
+
function gitSubcommands(cwd) {
|
|
179
|
+
try {
|
|
180
|
+
const result = spawnSync('git', ['--list-cmds=main,others,alias'], {
|
|
181
|
+
cwd, encoding: 'utf8', timeout: 4000, windowsHide: true,
|
|
182
|
+
});
|
|
183
|
+
if (result.status !== 0) return [];
|
|
184
|
+
return String(result.stdout).split('\n').map((line) => line.trim()).filter((line) => line !== '');
|
|
185
|
+
} catch {
|
|
186
|
+
return [];
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** 仓库里的分支(同步取,用于 git checkout 的补全) */
|
|
191
|
+
function gitBranches(cwd) {
|
|
192
|
+
try {
|
|
193
|
+
const result = spawnSync('git', ['branch', '-a', '--format=%(refname:short)'], {
|
|
194
|
+
cwd, encoding: 'utf8', timeout: 4000, windowsHide: true,
|
|
195
|
+
});
|
|
196
|
+
if (result.status !== 0) return [];
|
|
197
|
+
return String(result.stdout).split('\n').map((line) => line.trim()).filter((line) => line !== '' && !line.includes('HEAD'));
|
|
198
|
+
} catch {
|
|
199
|
+
return [];
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function completeCandidates(cwd, line) {
|
|
204
|
+
const text = String(line ?? '');
|
|
205
|
+
// 最后一段(按空白切)
|
|
206
|
+
const parts = text.split(/(\s+)/u);
|
|
207
|
+
const last = parts.length === 0 ? '' : parts[parts.length - 1];
|
|
208
|
+
const prefixOf = (value, prefix) => value.toLowerCase().startsWith(prefix.toLowerCase());
|
|
209
|
+
|
|
210
|
+
// 命令专属补全:git 子命令/分支、npm/pnpm 子命令(真 bash 靠这些补全脚本)
|
|
211
|
+
const beforeLast = text.slice(0, Math.max(0, text.length - last.length)).trim();
|
|
212
|
+
if (beforeLast !== '') {
|
|
213
|
+
const head = beforeLast.split(/\s+/u);
|
|
214
|
+
const tool = head[0];
|
|
215
|
+
const sub = head[1] ?? '';
|
|
216
|
+
const push = (items) => items
|
|
217
|
+
.filter((item) => item.toLowerCase().startsWith(last.toLowerCase()))
|
|
218
|
+
.map((item) => ({ value: item, label: item, kind: 'cmd' }));
|
|
219
|
+
if ((tool === 'git' || tool === 'git.exe') && head.length === 1) {
|
|
220
|
+
const full = push(gitSubcommands(cwd));
|
|
221
|
+
return (full.length > 0 ? full : push(GIT_SUBCOMMANDS)).slice(0, 30);
|
|
222
|
+
}
|
|
223
|
+
if ((tool === 'git' || tool === 'git.exe') && head.length === 2 && GIT_BRANCH_SUBS.has(sub)) {
|
|
224
|
+
const hits = push(gitBranches(cwd));
|
|
225
|
+
if (hits.length > 0) return hits.slice(0, 20);
|
|
226
|
+
return push(GIT_SUBCOMMANDS).slice(0, 10);
|
|
227
|
+
}
|
|
228
|
+
if ((tool === 'npm' || tool === 'npm.cmd') && head.length === 1) return push(NPM_SUBCOMMANDS).slice(0, 15);
|
|
229
|
+
if (tool === 'pnpm' && head.length === 1) return push(PNPM_SUBCOMMANDS).slice(0, 15);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// 已打了命令时,最后一段按文件名补全(cat cand<Tab> → candidate.txt);含 / \ . ~ 也算路径
|
|
233
|
+
const isArgument = text.slice(0, text.length - last.length).trim() !== '';
|
|
234
|
+
const pathLike = isArgument || last.includes('/') || last.includes('\\') || last.startsWith('.') || last.startsWith('~');
|
|
235
|
+
if (pathLike) {
|
|
236
|
+
// 没有分隔符时,目录部分是当前目录,前缀就是整段(cat cand → 当前目录的 cand*)
|
|
237
|
+
const hasSep = /[\\/]/u.test(last);
|
|
238
|
+
const dirPart = hasSep ? last.replace(/[\\/][^\\/]*$/u, '') : '';
|
|
239
|
+
const basePart = hasSep ? last.slice(dirPart.length).replace(/^[\\/]/u, '') : last;
|
|
240
|
+
const dirAbs = parseCd('cd ' + (dirPart === '' ? '.' : dirPart), cwd) ?? cwd;
|
|
241
|
+
let names = [];
|
|
242
|
+
try {
|
|
243
|
+
names = fs.readdirSync(dirAbs);
|
|
244
|
+
} catch {
|
|
245
|
+
return [];
|
|
246
|
+
}
|
|
247
|
+
const out = [];
|
|
248
|
+
for (const name of names) {
|
|
249
|
+
if (!prefixOf(name, basePart)) continue;
|
|
250
|
+
const full = path.join(dirAbs, name);
|
|
251
|
+
let isDir = false;
|
|
252
|
+
try {
|
|
253
|
+
isDir = fs.statSync(full).isDirectory();
|
|
254
|
+
} catch {
|
|
255
|
+
isDir = false;
|
|
256
|
+
}
|
|
257
|
+
const prefix = dirPart === '' ? '' : dirPart + '/';
|
|
258
|
+
out.push({ value: prefix + name + (isDir ? '/' : ''), label: name + (isDir ? '/' : ''), kind: isDir ? 'dir' : 'file' });
|
|
259
|
+
if (out.length >= 30) break;
|
|
260
|
+
}
|
|
261
|
+
return out.sort((a, b) => (a.kind === b.kind ? a.label.localeCompare(b.label) : a.kind === 'dir' ? -1 : 1));
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// 命令补全
|
|
265
|
+
const seen = new Set();
|
|
266
|
+
const out = [];
|
|
267
|
+
for (const cmd of COMMON_CMDS) {
|
|
268
|
+
if (!prefixOf(cmd, last)) continue;
|
|
269
|
+
if (seen.has(cmd)) continue;
|
|
270
|
+
seen.add(cmd);
|
|
271
|
+
out.push({ value: cmd, label: cmd, kind: 'cmd' });
|
|
272
|
+
}
|
|
273
|
+
return out.slice(0, 20);
|
|
274
|
+
}
|
package/lib/term-app.js
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
(function () {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
// 注意:字体必须是真等宽,否则 xterm 量出来的字符宽度不准,会出现"字距被拉散"
|
|
5
|
+
// 'DSH Mono' 是页面里 @font-face 内嵌的 JetBrains Mono;中文回退到系统字体(CJK 占两格,xterm 会按宽字符处理)
|
|
6
|
+
var FONT = '"DSH Mono", ui-monospace, "SF Mono", Consolas, "Liberation Mono", Menlo, monospace';
|
|
7
|
+
var term = new window.Terminal({
|
|
8
|
+
cols: 80,
|
|
9
|
+
rows: 24,
|
|
10
|
+
cursorBlink: true,
|
|
11
|
+
cursorStyle: 'bar', // 默认插入模式:细竖线
|
|
12
|
+
cursorInactiveStyle: 'outline',
|
|
13
|
+
fontFamily: FONT,
|
|
14
|
+
fontSize: 12,
|
|
15
|
+
lineHeight: 1.0,
|
|
16
|
+
letterSpacing: 0,
|
|
17
|
+
scrollback: 2000,
|
|
18
|
+
allowProposedApi: true,
|
|
19
|
+
theme: {
|
|
20
|
+
background: '#0c0c0c',
|
|
21
|
+
foreground: '#cccccc',
|
|
22
|
+
cursor: '#ffffff',
|
|
23
|
+
selectionBackground: '#264f78',
|
|
24
|
+
black: '#0c0c0c',
|
|
25
|
+
red: '#e06c75',
|
|
26
|
+
green: '#6ad46a',
|
|
27
|
+
yellow: '#e6c07b',
|
|
28
|
+
blue: '#6cb6ff',
|
|
29
|
+
magenta: '#c678dd',
|
|
30
|
+
cyan: '#56c8d8',
|
|
31
|
+
white: '#d0d0d0'
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// 调试用句柄:无头浏览器里光标不渲染(窗口没有焦点),只能通过 options 验证状态
|
|
36
|
+
window.__dshTerm = term;
|
|
37
|
+
|
|
38
|
+
var host = document.getElementById('t');
|
|
39
|
+
term.open(host);
|
|
40
|
+
|
|
41
|
+
// 自适应列宽:xterm 的 fit 插件按容器尺寸算 cols/rows
|
|
42
|
+
var fitAddon = (window.FitAddon && window.FitAddon.FitAddon) ? new window.FitAddon.FitAddon() : null;
|
|
43
|
+
if (fitAddon) term.loadAddon(fitAddon);
|
|
44
|
+
|
|
45
|
+
var lastSize = { cols: 0, rows: 0 };
|
|
46
|
+
var sizeTimer = null;
|
|
47
|
+
function pushSize() {
|
|
48
|
+
if (term.cols === lastSize.cols && term.rows === lastSize.rows) return;
|
|
49
|
+
lastSize = { cols: term.cols, rows: term.rows };
|
|
50
|
+
clearTimeout(sizeTimer);
|
|
51
|
+
sizeTimer = setTimeout(function () {
|
|
52
|
+
fetch('/term/resize', {
|
|
53
|
+
method: 'POST',
|
|
54
|
+
headers: { 'content-type': 'application/json' },
|
|
55
|
+
body: JSON.stringify({ cols: term.cols, rows: term.rows })
|
|
56
|
+
}).catch(function () { /* 忽略 */ });
|
|
57
|
+
}, 120);
|
|
58
|
+
}
|
|
59
|
+
function doFit() {
|
|
60
|
+
if (fitAddon) {
|
|
61
|
+
try { fitAddon.fit(); } catch (e) { /* 忽略 */ }
|
|
62
|
+
}
|
|
63
|
+
pushSize();
|
|
64
|
+
}
|
|
65
|
+
window.addEventListener('resize', doFit);
|
|
66
|
+
if (window.visualViewport) {
|
|
67
|
+
window.visualViewport.addEventListener('resize', doFit);
|
|
68
|
+
}
|
|
69
|
+
term.focus();
|
|
70
|
+
|
|
71
|
+
var status = document.getElementById('status');
|
|
72
|
+
var seq = 0;
|
|
73
|
+
var ctrlLatched = false;
|
|
74
|
+
var ctrlButton = document.getElementById('ctrl');
|
|
75
|
+
var es = null;
|
|
76
|
+
|
|
77
|
+
function setStatus(text, isError) {
|
|
78
|
+
status.textContent = text;
|
|
79
|
+
status.className = isError ? 'bad' : '';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function send(data) {
|
|
83
|
+
if (!data) return;
|
|
84
|
+
fetch('/term/input', {
|
|
85
|
+
method: 'POST',
|
|
86
|
+
headers: { 'content-type': 'application/json' },
|
|
87
|
+
body: JSON.stringify({ data: data })
|
|
88
|
+
}).catch(function () {
|
|
89
|
+
setStatus('发送失败', true);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// DECSCUSR:应用(vim / readline vi-mode)用 ESC [ Ps SP q 指定光标形状。
|
|
94
|
+
// xterm 内部也处理,但它只改内部状态、且失焦时会统一成 outline,
|
|
95
|
+
// 所以这里自己解析一份:显式设置 options.cursorStyle,并在顶栏显示模式徽标。
|
|
96
|
+
var DECSCUSR = /\u001b\[(\d) q/g;
|
|
97
|
+
var modeBadge = document.getElementById('mode');
|
|
98
|
+
function applyCursorShape(ps) {
|
|
99
|
+
var n = Number(ps);
|
|
100
|
+
var shape = 'block';
|
|
101
|
+
var label = '普通';
|
|
102
|
+
if (n === 3 || n === 4) { shape = 'underline'; label = '替换'; }
|
|
103
|
+
else if (n === 5 || n === 6) { shape = 'bar'; label = '插入'; }
|
|
104
|
+
else if (n === 1 || n === 2) { shape = 'block'; label = '替换/普通'; }
|
|
105
|
+
try { term.options.cursorStyle = shape; } catch (e) { /* 忽略 */ }
|
|
106
|
+
if (modeBadge) {
|
|
107
|
+
modeBadge.hidden = false;
|
|
108
|
+
modeBadge.textContent = label;
|
|
109
|
+
modeBadge.className = shape === 'bar' ? 'mode-insert' : (shape === 'underline' ? 'mode-underline' : 'mode-normal');
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function scanCursorShapes(text) {
|
|
113
|
+
if (text.indexOf(' q') < 0) return;
|
|
114
|
+
DECSCUSR.lastIndex = 0;
|
|
115
|
+
var m;
|
|
116
|
+
while ((m = DECSCUSR.exec(text)) !== null) applyCursorShape(m[1]);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function resetCtrl() {
|
|
120
|
+
ctrlLatched = false;
|
|
121
|
+
if (!ctrlButton) return;
|
|
122
|
+
ctrlButton.classList.remove('pressed');
|
|
123
|
+
ctrlButton.setAttribute('aria-pressed', 'false');
|
|
124
|
+
ctrlButton.textContent = 'Ctrl';
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/* ---------- 插入 / 覆盖模式的本地跟踪 ----------
|
|
128
|
+
* bash readline 的 Ins(覆盖模式)是**内部状态**,不会给终端发 DECSCUSR,
|
|
129
|
+
* 所以光看输出没法知道"现在是插入还是覆盖"。
|
|
130
|
+
* 做法:自己记一个 overwrite 标志,按下 Ins 时翻转,并据此设置光标形状:
|
|
131
|
+
* 插入 → 细竖线(bar) 覆盖/替换 → 方块(block,也就是原来那种)
|
|
132
|
+
* 收到 Enter 视为新的一行,readline 会回到插入模式,于是重置为插入。
|
|
133
|
+
*/
|
|
134
|
+
var overwriteMode = false;
|
|
135
|
+
function applyModeCursor() {
|
|
136
|
+
try { term.options.cursorStyle = overwriteMode ? 'block' : 'bar'; } catch (e) { /* 忽略 */ }
|
|
137
|
+
if (modeBadge) {
|
|
138
|
+
modeBadge.hidden = false;
|
|
139
|
+
modeBadge.textContent = overwriteMode ? '替换' : '插入';
|
|
140
|
+
modeBadge.className = overwriteMode ? 'mode-normal' : 'mode-insert';
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function toggleOverwrite() {
|
|
144
|
+
overwriteMode = !overwriteMode;
|
|
145
|
+
applyModeCursor();
|
|
146
|
+
return overwriteMode;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// 键盘输入:Ctrl 按下时把字母转成控制字符(真 PTY,所以 ^C 是真的 SIGINT)
|
|
150
|
+
// 放在 onData 而不是 keydown —— 手机软键盘常常不发 keydown,但 onData 一定有数据。
|
|
151
|
+
term.onData(function (data) {
|
|
152
|
+
if (ctrlLatched && data.length === 1 && /[a-zA-Z]/u.test(data)) {
|
|
153
|
+
send(String.fromCharCode(data.toUpperCase().charCodeAt(0) - 64));
|
|
154
|
+
resetCtrl();
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
// 物理键盘的 Ins 键也是 ESC[2~
|
|
158
|
+
if (data === '\u001b[2~') {
|
|
159
|
+
toggleOverwrite();
|
|
160
|
+
send(data);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
// 回车:新的一行从插入模式开始
|
|
164
|
+
if (data.indexOf('\r') >= 0 || data.indexOf('\n') >= 0) {
|
|
165
|
+
overwriteMode = false;
|
|
166
|
+
applyModeCursor();
|
|
167
|
+
}
|
|
168
|
+
send(data);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
// 手机键盘:把输入焦点保持在终端上
|
|
172
|
+
host.addEventListener('click', function () { term.focus(); });
|
|
173
|
+
|
|
174
|
+
function connect() {
|
|
175
|
+
if (es) { es.close(); }
|
|
176
|
+
es = new EventSource('/term/stream?since=' + seq);
|
|
177
|
+
es.onmessage = function (event) {
|
|
178
|
+
if (event.data === '') return;
|
|
179
|
+
var payload;
|
|
180
|
+
try { payload = JSON.parse(event.data); } catch (e) { return; }
|
|
181
|
+
if (typeof payload.seq === 'number') seq = payload.seq;
|
|
182
|
+
if (typeof payload.text === 'string') {
|
|
183
|
+
scanCursorShapes(payload.text);
|
|
184
|
+
term.write(payload.text);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
es.onerror = function () {
|
|
188
|
+
setStatus('连接断开,正在重连…', true);
|
|
189
|
+
// EventSource 会自动重连;这里只提示
|
|
190
|
+
};
|
|
191
|
+
es.onopen = function () { setStatus('已连接'); };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function toggleCtrl() {
|
|
195
|
+
ctrlLatched = !ctrlLatched;
|
|
196
|
+
ctrlButton.classList.toggle('pressed', ctrlLatched);
|
|
197
|
+
ctrlButton.setAttribute('aria-pressed', ctrlLatched ? 'true' : 'false');
|
|
198
|
+
ctrlButton.textContent = ctrlLatched ? 'Ctrl ●' : 'Ctrl';
|
|
199
|
+
term.focus();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
document.getElementById('ctrl').addEventListener('click', toggleCtrl);
|
|
203
|
+
|
|
204
|
+
// Ctrl 按下时,把接下来输入的字母转成控制字符(真 PTY,所以是真正的 SIGINT)
|
|
205
|
+
term.attachCustomKeyEventHandler(function (event) {
|
|
206
|
+
if (ctrlLatched && /^[a-zA-Z]$/u.test(event.key)) {
|
|
207
|
+
var code = event.key.toUpperCase().charCodeAt(0) - 64; // Ctrl+A=1, Ctrl+C=3
|
|
208
|
+
send(String.fromCharCode(code));
|
|
209
|
+
ctrlLatched = false;
|
|
210
|
+
ctrlButton.classList.remove('pressed');
|
|
211
|
+
ctrlButton.setAttribute('aria-pressed', 'false');
|
|
212
|
+
ctrlButton.textContent = 'Ctrl';
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
return true;
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// 常用功能键:Tab / Esc / 方向键 / 中断
|
|
219
|
+
function special(data) {
|
|
220
|
+
send(data);
|
|
221
|
+
term.focus();
|
|
222
|
+
}
|
|
223
|
+
var on = function (id, handler) {
|
|
224
|
+
var el = document.getElementById(id);
|
|
225
|
+
if (el) el.addEventListener('click', handler);
|
|
226
|
+
};
|
|
227
|
+
on('tab', function () { special('\t'); });
|
|
228
|
+
on('esc', function () { special('\u001b'); });
|
|
229
|
+
on('ins', function () { toggleOverwrite(); special('\u001b[2~'); });
|
|
230
|
+
on('left', function () { special('\u001b[D'); });
|
|
231
|
+
on('right', function () { special('\u001b[C'); });
|
|
232
|
+
on('up', function () { special('\u001b[A'); });
|
|
233
|
+
on('down', function () { special('\u001b[B'); });
|
|
234
|
+
|
|
235
|
+
on('clear', function () {
|
|
236
|
+
special('\u000c');
|
|
237
|
+
try { term.clear(); } catch (e) { /* 忽略 */ }
|
|
238
|
+
});
|
|
239
|
+
on('reset', function () {
|
|
240
|
+
fetch('/term/reset', { method: 'POST' })
|
|
241
|
+
.then(function () {
|
|
242
|
+
seq = 0;
|
|
243
|
+
term.reset();
|
|
244
|
+
term.focus();
|
|
245
|
+
connect();
|
|
246
|
+
})
|
|
247
|
+
.catch(function () { setStatus('重置失败', true); });
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
/* ---------- 点击定位光标 ---------- */
|
|
251
|
+
// 终端里的光标位置由远端程序决定,客户端没法直接摆;
|
|
252
|
+
// 但当前输入行上可以:算出点到的列与当前列的差,发同样数量的 ←/→ 键。
|
|
253
|
+
function measureCell() {
|
|
254
|
+
var probe = document.querySelector('.xterm-rows');
|
|
255
|
+
var font = probe ? getComputedStyle(probe).font : '12px "DSH Mono"';
|
|
256
|
+
var span = document.createElement('span');
|
|
257
|
+
span.style.cssText = 'position:absolute;visibility:hidden;white-space:pre;font:' + font;
|
|
258
|
+
span.textContent = 'MMMMMMMMMM';
|
|
259
|
+
document.body.appendChild(span);
|
|
260
|
+
var cellW = span.getBoundingClientRect().width / 10;
|
|
261
|
+
span.remove();
|
|
262
|
+
var cellH = probe ? probe.getBoundingClientRect().height / Math.max(1, term.rows) : 16;
|
|
263
|
+
return { cellW: cellW || 7.2, cellH: cellH || 16 };
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function placeCursorByClick(event) {
|
|
267
|
+
var screen = document.querySelector('.xterm-screen');
|
|
268
|
+
if (!screen) return false;
|
|
269
|
+
var rect = screen.getBoundingClientRect();
|
|
270
|
+
var cell = measureCell();
|
|
271
|
+
var col = Math.floor((event.clientX - rect.left) / cell.cellW);
|
|
272
|
+
var row = Math.floor((event.clientY - rect.top) / cell.cellH);
|
|
273
|
+
if (col < 0 || row < 0 || col >= term.cols || row >= term.rows) return false;
|
|
274
|
+
|
|
275
|
+
var buf = term.buffer.active;
|
|
276
|
+
var cursorRow = buf.cursorY; // 视口内行号
|
|
277
|
+
var cursorCol = buf.cursorX;
|
|
278
|
+
// 只处理"同一行"的点击(别的行属于历史输出,点了也不该移动输入光标)
|
|
279
|
+
if (row !== cursorRow) return false;
|
|
280
|
+
var delta = col - cursorCol;
|
|
281
|
+
if (delta === 0) return false;
|
|
282
|
+
var key = delta > 0 ? '\u001b[C' : '\u001b[D';
|
|
283
|
+
var count = Math.min(Math.abs(delta), 200);
|
|
284
|
+
send(key.repeat(count));
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
host.addEventListener('click', function (event) {
|
|
289
|
+
if (placeCursorByClick(event)) event.preventDefault();
|
|
290
|
+
}, true);
|
|
291
|
+
|
|
292
|
+
// 字号缩放(手机上 80 列放不下,用小字号 + 双指放大更实际)
|
|
293
|
+
var fontSize = 12;
|
|
294
|
+
function applyFont(delta) {
|
|
295
|
+
fontSize = Math.max(8, Math.min(24, fontSize + delta));
|
|
296
|
+
term.options.fontSize = fontSize;
|
|
297
|
+
try { term.refresh(0, term.rows - 1); } catch (e) { /* 忽略 */ }
|
|
298
|
+
if (typeof doFit === 'function') doFit();
|
|
299
|
+
term.focus();
|
|
300
|
+
}
|
|
301
|
+
var zoomIn = document.getElementById('zoomIn');
|
|
302
|
+
var zoomOut = document.getElementById('zoomOut');
|
|
303
|
+
if (zoomIn) zoomIn.addEventListener('click', function () { applyFont(1); });
|
|
304
|
+
if (zoomOut) zoomOut.addEventListener('click', function () { applyFont(-1); });
|
|
305
|
+
|
|
306
|
+
// 字体加载完再重排一次,避免首帧用回退字体量宽度
|
|
307
|
+
if (document.fonts && document.fonts.ready) {
|
|
308
|
+
document.fonts.ready.then(function () {
|
|
309
|
+
try { term.refresh(0, term.rows - 1); } catch (e) { /* 忽略 */ }
|
|
310
|
+
doFit();
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
applyModeCursor();
|
|
315
|
+
doFit();
|
|
316
|
+
connect();
|
|
317
|
+
term.focus();
|
|
318
|
+
})();
|