@andy.ian/dsh-homing 2.0.1 → 2.0.2
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/watchdog.mjs +99 -118
- package/index.js +2 -221
- package/lib/files-page.mjs +2 -113
- package/package.json +2 -4
- package/client.js +0 -79
- package/lib/session.mjs +0 -216
- package/lib/shell-page.mjs +0 -430
- package/lib/shell.mjs +0 -274
- package/ops/watchdog/dsh-watchdog.ps1 +0 -105
- package/ops/watchdog/restart-hidden.vbs +0 -7
- package/ops/watchdog/restart-now.ps1 +0 -261
- package/ops/watchdog/run-hidden.vbs +0 -4
package/lib/files-page.mjs
CHANGED
|
@@ -67,7 +67,7 @@ const messageHtml = (message, isError) => (message
|
|
|
67
67
|
: '')
|
|
68
68
|
|
|
69
69
|
/** 目录列表页 */
|
|
70
|
-
const
|
|
70
|
+
const MENU_JS = `<script>
|
|
71
71
|
(() => {
|
|
72
72
|
var openMenu = null;
|
|
73
73
|
function closeAll() {
|
|
@@ -299,7 +299,7 @@ ${entry.dir ? `<a class="mi" role="menuitem" data-act="paste" data-path="${escap
|
|
|
299
299
|
<button type="button" class="sortbtn" data-sort="time">时间</button>
|
|
300
300
|
</div>
|
|
301
301
|
<div class="flist">${rows || '<div class="fempty">空目录</div>'}</div>
|
|
302
|
-
${
|
|
302
|
+
${MENU_JS}\n <p class="crumbs">提示:点目录进入,点文件名查看内容;标 ⚙️ 的可直接运行(会在这台电脑上执行)。</p>`
|
|
303
303
|
|
|
304
304
|
return layout({ title: info.path, body })
|
|
305
305
|
}
|
|
@@ -479,114 +479,3 @@ export function renderEditPage({ info, message = '', isError = false }) {
|
|
|
479
479
|
}
|
|
480
480
|
|
|
481
481
|
/** shell 页(手机端 Git Bash):命令 + 工作目录 + 输出 */
|
|
482
|
-
export function renderShellPage({ cwd, command = '', result = null, history = [], shellName = '' }) {
|
|
483
|
-
const failed = result !== null && (result.ok === false || (typeof result.code === 'number' && result.code !== 0))
|
|
484
|
-
const body = `
|
|
485
|
-
<div class="bar">
|
|
486
|
-
<h1>终端</h1>
|
|
487
|
-
<div class="path">${escapeHtml(cwd)}${shellName ? ' · ' + escapeHtml(shellName) : ''}</div>
|
|
488
|
-
</div>
|
|
489
|
-
<form method="post" action="/shell">
|
|
490
|
-
<label style="display:block;margin-top:10px;font-size:13px;color:#9aa4b2">工作目录</label>
|
|
491
|
-
<input type="text" name="cwd" list="shell-paths" value="${escapeHtml(cwd)}" style="width:100%;box-sizing:border-box">
|
|
492
|
-
<label style="display:block;margin-top:10px;font-size:13px;color:#9aa4b2">命令</label>
|
|
493
|
-
<input type="text" id="cmd" name="command" value="${escapeHtml(command)}" autocomplete="off" autocapitalize="off"
|
|
494
|
-
autocorrect="off" spellcheck="false"
|
|
495
|
-
placeholder='例如:git status / ls -la / npm test'
|
|
496
|
-
style="width:100%;box-sizing:border-box">
|
|
497
|
-
<div class="row">
|
|
498
|
-
<button type="submit" style="flex:2">执行</button>
|
|
499
|
-
<button type="button" id="tabkey" style="flex:1;background:#2a2f3a;color:#c7cdd8">Tab</button>
|
|
500
|
-
<button type="submit" name="action" value="reset" formnovalidate style="flex:1;background:#3a2a2a;color:#f0b6bf">重置</button>
|
|
501
|
-
</div>
|
|
502
|
-
</form>
|
|
503
|
-
<div id="cands" style="display:flex;flex-wrap:wrap;gap:6px;margin-top:8px"></div>
|
|
504
|
-
<script id="shell-complete.js">
|
|
505
|
-
(function () {
|
|
506
|
-
var input = document.getElementById('cmd');
|
|
507
|
-
var box = document.getElementById('cands');
|
|
508
|
-
var cwdInput = document.querySelector('input[name=cwd]');
|
|
509
|
-
if (!input || !box) return;
|
|
510
|
-
var timer = null;
|
|
511
|
-
function apply(value) {
|
|
512
|
-
var text = input.value;
|
|
513
|
-
var m = text.match(/(\\S+)$/);
|
|
514
|
-
input.value = (m ? text.slice(0, m.index) : text) + value;
|
|
515
|
-
input.focus();
|
|
516
|
-
refresh();
|
|
517
|
-
}
|
|
518
|
-
function render(items) {
|
|
519
|
-
box.innerHTML = '';
|
|
520
|
-
items.slice(0, 12).forEach(function (item) {
|
|
521
|
-
var btn = document.createElement('button');
|
|
522
|
-
btn.type = 'button';
|
|
523
|
-
btn.textContent = item.label;
|
|
524
|
-
btn.style.cssText = 'padding:6px 10px;border-radius:999px;background:#1b2030;border:1px solid #2a3550;color:#9fc0ff;font-size:12px';
|
|
525
|
-
btn.addEventListener('click', function () { apply(item.value); });
|
|
526
|
-
box.appendChild(btn);
|
|
527
|
-
});
|
|
528
|
-
}
|
|
529
|
-
function refresh() {
|
|
530
|
-
var cwd = cwdInput ? cwdInput.value : '';
|
|
531
|
-
var line = input.value;
|
|
532
|
-
if (line.trim() === '') { box.innerHTML = ''; return; }
|
|
533
|
-
fetch('/shell/complete?cwd=' + encodeURIComponent(cwd) + '&line=' + encodeURIComponent(line))
|
|
534
|
-
.then(function (r) { return r.ok ? r.json() : []; })
|
|
535
|
-
.then(function (items) { render(Array.isArray(items) ? items : []); })
|
|
536
|
-
.catch(function () { /* 忽略补全失败 */ });
|
|
537
|
-
}
|
|
538
|
-
function completeNow() {
|
|
539
|
-
var cwd = cwdInput ? cwdInput.value : '';
|
|
540
|
-
var line = input.value;
|
|
541
|
-
if (line.trim() === '') return;
|
|
542
|
-
fetch('/shell/complete?cwd=' + encodeURIComponent(cwd) + '&line=' + encodeURIComponent(line))
|
|
543
|
-
.then(function (r) { return r.ok ? r.json() : []; })
|
|
544
|
-
.then(function (items) {
|
|
545
|
-
if (!Array.isArray(items) || items.length === 0) {
|
|
546
|
-
box.textContent = '无候选';
|
|
547
|
-
setTimeout(function () { box.textContent = ''; }, 1200);
|
|
548
|
-
return;
|
|
549
|
-
}
|
|
550
|
-
render(items);
|
|
551
|
-
if (items.length === 1) { apply(items[0].value); return; }
|
|
552
|
-
// 多个候选:把最后一段改成它们的公共前缀(像 bash 的 Tab 那样)
|
|
553
|
-
var prefix = items[0].value;
|
|
554
|
-
for (var i = 1; i < items.length; i += 1) {
|
|
555
|
-
while (prefix.length > 0 && items[i].value.indexOf(prefix) !== 0) prefix = prefix.slice(0, -1);
|
|
556
|
-
}
|
|
557
|
-
if (prefix.length > 0) apply(prefix);
|
|
558
|
-
})
|
|
559
|
-
.catch(function () { /* 忽略 */ });
|
|
560
|
-
}
|
|
561
|
-
var tabBtn = document.getElementById('tabkey');
|
|
562
|
-
if (tabBtn) tabBtn.addEventListener('click', completeNow);
|
|
563
|
-
|
|
564
|
-
input.addEventListener('input', function () {
|
|
565
|
-
clearTimeout(timer);
|
|
566
|
-
timer = setTimeout(refresh, 250);
|
|
567
|
-
});
|
|
568
|
-
input.addEventListener('focus', refresh);
|
|
569
|
-
})();
|
|
570
|
-
</script>
|
|
571
|
-
${history.length === 0 ? '' : `<div style="display:flex;flex-wrap:wrap;gap:6px;margin-top:10px">
|
|
572
|
-
${history.slice(0, 8).map((cmd) => `<form method="post" action="/shell" style="display:inline">
|
|
573
|
-
<input type="hidden" name="cwd" value="${escapeHtml(cwd)}">
|
|
574
|
-
<input type="hidden" name="command" value="${escapeHtml(cmd)}">
|
|
575
|
-
<button type="submit" style="padding:6px 10px;border-radius:999px;background:#1b2030;border:1px solid #2a3550;color:#9fc0ff;font-size:12px">${escapeHtml(cmd)}</button>
|
|
576
|
-
</form>`).join('')}
|
|
577
|
-
</div>`}
|
|
578
|
-
${result === null ? '' : `
|
|
579
|
-
<div class="msg${failed ? ' err' : ''}" style="margin-top:14px">
|
|
580
|
-
${failed ? escapeHtml(result.error ?? (result.timeout ? '超时(30 秒),已终止' : `退出码 ${result.code}`)) : `退出码 ${result.code}`}
|
|
581
|
-
</div>
|
|
582
|
-
<pre>${escapeHtml(result.output ?? '')}</pre>
|
|
583
|
-
`}
|
|
584
|
-
<div class="row">
|
|
585
|
-
<a class="btn ghost" href="/git?path=${encodeURIComponent(cwd)}"
|
|
586
|
-
style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">Git 页</a>
|
|
587
|
-
<a class="btn ghost" href="/files?path=${encodeURIComponent(cwd)}"
|
|
588
|
-
style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">文件</a>
|
|
589
|
-
</div>
|
|
590
|
-
<p class="crumbs">命令直接在你电脑上执行,输出截断到 64KB,30 秒超时。可以用 cd 切换工作目录。</p>`
|
|
591
|
-
return layout({ title: '终端', body })
|
|
592
|
-
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@andy.ian/dsh-homing",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "把本机 DSH 通过 SSH 反向隧道安全开放到手机浏览器:TLS 端到端(服务器只透传)、账号 + TOTP、局域网与公网双入口,随 DSH 启动。",
|
|
6
6
|
"keywords": [
|
|
@@ -54,9 +54,7 @@
|
|
|
54
54
|
"icon.svg",
|
|
55
55
|
"README.md",
|
|
56
56
|
"LICENSE",
|
|
57
|
-
"lib/vendor/**"
|
|
58
|
-
"ops/**",
|
|
59
|
-
"client.js"
|
|
57
|
+
"lib/vendor/**"
|
|
60
58
|
],
|
|
61
59
|
"publishConfig": {
|
|
62
60
|
"access": "public",
|
package/client.js
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-homing 的浏览器端:在侧边栏底部放一组守护进程控制按钮。
|
|
3
|
-
*
|
|
4
|
-
* 为什么放在客户端:用户要求用按钮代替命令行,且不要用 PowerShell。
|
|
5
|
-
* 按钮直接调用本机守护进程的控制接口(http://127.0.0.1:8790),
|
|
6
|
-
* 该接口已在 supervisor.mjs 里放行跨端口请求(CORS)。
|
|
7
|
-
*
|
|
8
|
-
* 契约参考 DSH 客户端插件(window.__ModuleLoader__.load + slots 注册)。
|
|
9
|
-
*/
|
|
10
|
-
window.__ModuleLoader__.load({
|
|
11
|
-
id: '@andy.ian/dsh-homing',
|
|
12
|
-
factory(require) {
|
|
13
|
-
const React = require('react')
|
|
14
|
-
const h = React.createElement
|
|
15
|
-
|
|
16
|
-
const CONTROL = 'http://127.0.0.1:8790'
|
|
17
|
-
|
|
18
|
-
async function call(pathname) {
|
|
19
|
-
const res = await fetch(CONTROL + pathname, { method: 'POST' })
|
|
20
|
-
return res.json()
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function Controls() {
|
|
24
|
-
const [notice, setNotice] = React.useState('')
|
|
25
|
-
const [busy, setBusy] = React.useState(false)
|
|
26
|
-
|
|
27
|
-
async function run(pathname, text) {
|
|
28
|
-
if (busy) return
|
|
29
|
-
setBusy(true)
|
|
30
|
-
try {
|
|
31
|
-
await call(pathname)
|
|
32
|
-
setNotice(text)
|
|
33
|
-
} catch (error) {
|
|
34
|
-
setNotice('守护进程未运行')
|
|
35
|
-
} finally {
|
|
36
|
-
setBusy(false)
|
|
37
|
-
setTimeout(() => setNotice(''), 4000)
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const button = (label, title, pathname, text) => h('button', {
|
|
42
|
-
type: 'button',
|
|
43
|
-
title,
|
|
44
|
-
disabled: busy,
|
|
45
|
-
onClick: () => void run(pathname, text),
|
|
46
|
-
style: {
|
|
47
|
-
background: 'transparent',
|
|
48
|
-
border: '1px solid rgba(255,255,255,.18)',
|
|
49
|
-
borderRadius: '6px',
|
|
50
|
-
color: 'inherit',
|
|
51
|
-
cursor: busy ? 'default' : 'pointer',
|
|
52
|
-
fontSize: '12px',
|
|
53
|
-
padding: '4px 8px',
|
|
54
|
-
},
|
|
55
|
-
}, label)
|
|
56
|
-
|
|
57
|
-
return h('div', { style: { display: 'flex', gap: '6px', alignItems: 'center', padding: '4px 8px' } },
|
|
58
|
-
button('⏻', '重启 DSH(等空闲)', '/restart?mode=default', '已请求重启'),
|
|
59
|
-
button('⚡', '立即重启 DSH', '/restart?mode=immediate', '已请求立即重启'),
|
|
60
|
-
button('⏸', '暂停自动拉起 30 分钟', '/pause?minutes=30', '已暂停 30 分钟'),
|
|
61
|
-
button('▶', '恢复自动拉起', '/resume', '已恢复'),
|
|
62
|
-
button('⏹', '停止守护进程(不再拉起)', '/stop', '守护进程已退出'),
|
|
63
|
-
notice ? h('span', { style: { fontSize: '11px', opacity: 0.7 } }, notice) : null,
|
|
64
|
-
)
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return {
|
|
68
|
-
name: 'ui-dsh-homing',
|
|
69
|
-
inject: ['slots'],
|
|
70
|
-
apply(ctx) {
|
|
71
|
-
const dispose = ctx.slots.inject('sidebar.footer.action', () => ctx.slots.register(
|
|
72
|
-
{ name: 'sidebar.footer.action', id: 'dsh-homing', order: 11 },
|
|
73
|
-
Controls,
|
|
74
|
-
))
|
|
75
|
-
if (typeof ctx.effect === 'function') ctx.effect(() => dispose, 'dsh-homing: sidebar controls')
|
|
76
|
-
},
|
|
77
|
-
}
|
|
78
|
-
},
|
|
79
|
-
})
|
package/lib/session.mjs
DELETED
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 常驻 shell 会话:让手机终端像真 bash 一样保留状态(cwd / 环境变量 / 函数)。
|
|
3
|
-
*
|
|
4
|
-
* 关键设计(踩过的坑写在这里):
|
|
5
|
-
* 1. 不用 PTY:Git Bash 自带的 winpty 在无控制台进程里断言失败;真 PTY 要 ConPTY + 原生模块。
|
|
6
|
-
* 2. 一个 bash 进程从 stdin 顺序读命令,命令前后打标记,一次往返拿到「输出 + 退出码 + $PWD」。
|
|
7
|
-
* 3. **子进程退出回调必须校验"我还当权吗"**:
|
|
8
|
-
* 旧进程的 exit 事件是异步投递的,若回调无条件把 this.child 置空,
|
|
9
|
-
* 会把刚重启的新进程引用清掉,导致"永远显示会话已退出"的假死循环。
|
|
10
|
-
*/
|
|
11
|
-
import { spawn } from 'node:child_process'
|
|
12
|
-
import { findBash } from './shell.mjs'
|
|
13
|
-
|
|
14
|
-
/** Git Bash 的 /d/x 转回 D:\\x(Git Bash 也认 D:/x,所以转换后可直接 cd) */
|
|
15
|
-
function toWindowsPath(value) {
|
|
16
|
-
const text = String(value ?? '').trim()
|
|
17
|
-
// pwd -W 给的是 C:/Users/... 形式,统一成反斜杠,和 path.resolve 比较时不打架
|
|
18
|
-
if (/^[A-Za-z]:[\\/]/u.test(text)) return text.replaceAll('/', '\\')
|
|
19
|
-
const match = /^\/([A-Za-z])(?:\/(.*))?$/u.exec(text)
|
|
20
|
-
if (match === null) return text
|
|
21
|
-
const drive = match[1].toUpperCase()
|
|
22
|
-
const rest = (match[2] ?? '').replaceAll('/', '\\')
|
|
23
|
-
return rest === '' ? drive + ':\\' : drive + ':\\' + rest
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const MAX_OUTPUT = 64 * 1024
|
|
27
|
-
const DEFAULT_TIMEOUT = 30_000
|
|
28
|
-
|
|
29
|
-
export class ShellSession {
|
|
30
|
-
constructor(cwd) {
|
|
31
|
-
// spawn 必须用 Windows 路径;bash 的 $PWD(/d/xxx)只用于显示
|
|
32
|
-
this.winCwd = cwd
|
|
33
|
-
this.cwd = cwd
|
|
34
|
-
this.child = null
|
|
35
|
-
this.queue = Promise.resolve()
|
|
36
|
-
this.startedAt = 0
|
|
37
|
-
this.commandCount = 0
|
|
38
|
-
this.buffer = ''
|
|
39
|
-
// 终端页的滚动记录(最近 100 条),页面刷新后还能看到上下文
|
|
40
|
-
this.transcript = []
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
get running() {
|
|
44
|
-
const child = this.child
|
|
45
|
-
return child !== null && child.exitCode === null && !child.killed
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
start() {
|
|
49
|
-
const bash = findBash()
|
|
50
|
-
if (bash === null) throw new Error('找不到 Git Bash')
|
|
51
|
-
const child = spawn(bash, ['--noprofile', '--norc'], {
|
|
52
|
-
cwd: this.winCwd,
|
|
53
|
-
env: {
|
|
54
|
-
...process.env,
|
|
55
|
-
TERM: 'dumb',
|
|
56
|
-
PAGER: 'cat',
|
|
57
|
-
GIT_PAGER: 'cat',
|
|
58
|
-
GIT_TERMINAL_PROMPT: '0',
|
|
59
|
-
LANG: 'C.UTF-8',
|
|
60
|
-
},
|
|
61
|
-
stdio: ['pipe', 'pipe', 'pipe'],
|
|
62
|
-
windowsHide: true,
|
|
63
|
-
})
|
|
64
|
-
this.child = child
|
|
65
|
-
this.buffer = ''
|
|
66
|
-
this.startedAt = Date.now()
|
|
67
|
-
this.commandCount = 0
|
|
68
|
-
|
|
69
|
-
child.stdout.on('data', (chunk) => { this.buffer += chunk.toString('utf8') })
|
|
70
|
-
child.stderr.on('data', (chunk) => { this.buffer += chunk.toString('utf8') })
|
|
71
|
-
// 只清理"自己这一代"的引用:否则旧进程的退出事件会把新进程清掉
|
|
72
|
-
const clearIfCurrent = () => {
|
|
73
|
-
if (this.child === child) this.child = null
|
|
74
|
-
}
|
|
75
|
-
child.on('error', clearIfCurrent)
|
|
76
|
-
child.on('exit', clearIfCurrent)
|
|
77
|
-
return this
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
record(entry) {
|
|
81
|
-
this.transcript.push(entry)
|
|
82
|
-
if (this.transcript.length > 100) this.transcript.shift()
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* 中断当前命令(相当于 Ctrl+C)。
|
|
87
|
-
*
|
|
88
|
-
* 注意:bash 的 stdin 是管道而不是终端,写 \x03 不会产生 SIGINT信号,
|
|
89
|
-
* 所以这里直接杀掉 bash 进程(连同它启动的子进程树),再起一个新的。
|
|
90
|
-
* 代价是 cwd / 环境变量会被重置 —— 这和"卡死的命令必须停下来"相比是值得的。
|
|
91
|
-
*/
|
|
92
|
-
interrupt() {
|
|
93
|
-
this.reset()
|
|
94
|
-
return { ok: true, code: null, output: '(已中断,会话已重启)', cwd: this.winCwd }
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
reset() {
|
|
98
|
-
this.kill()
|
|
99
|
-
this.start()
|
|
100
|
-
return this
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
kill() {
|
|
104
|
-
const child = this.child
|
|
105
|
-
this.child = null
|
|
106
|
-
try {
|
|
107
|
-
child?.kill()
|
|
108
|
-
} catch {
|
|
109
|
-
// 已退出
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/** 串行执行(同一个 shell 不能并发写 stdin) */
|
|
114
|
-
run(command) {
|
|
115
|
-
const job = () => this.#runOnce(command)
|
|
116
|
-
this.queue = this.queue.then(job, job)
|
|
117
|
-
return this.queue
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
#runOnce(command) {
|
|
121
|
-
const text = String(command ?? '').trim()
|
|
122
|
-
if (text === '') return Promise.resolve({ ok: false, error: '命令为空', cwd: this.cwd })
|
|
123
|
-
|
|
124
|
-
// exit 会真的结束 shell:直接当作"重置会话",并如实返回退出码
|
|
125
|
-
const exitMatch = /^exit(?:\s+(\d+))?\s*$/u.exec(text)
|
|
126
|
-
if (exitMatch !== null) {
|
|
127
|
-
const code = exitMatch[1] === undefined ? 0 : Number(exitMatch[1])
|
|
128
|
-
this.reset()
|
|
129
|
-
return Promise.resolve({
|
|
130
|
-
ok: code === 0,
|
|
131
|
-
code,
|
|
132
|
-
output: '(会话已重启:环境变量清空,当前目录保持)',
|
|
133
|
-
cwd: this.cwd,
|
|
134
|
-
})
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if (!this.running) this.start()
|
|
138
|
-
|
|
139
|
-
const id = `m${Math.random().toString(36).slice(2, 10)}`
|
|
140
|
-
const startTag = `<<${id}S>>`
|
|
141
|
-
const endTag = `<<${id}E:`
|
|
142
|
-
this.buffer = ''
|
|
143
|
-
this.child.stdin.write(`printf '${startTag}\\n'\n${text}\nprintf '${endTag}%s|%s>>\\n' "$?" "$(pwd -W 2>/dev/null || pwd)"\n`)
|
|
144
|
-
this.commandCount += 1
|
|
145
|
-
|
|
146
|
-
const started = Date.now()
|
|
147
|
-
return new Promise((resolve) => {
|
|
148
|
-
const timer = setInterval(() => {
|
|
149
|
-
const endIndex = this.buffer.indexOf(endTag)
|
|
150
|
-
if (endIndex >= 0) {
|
|
151
|
-
clearInterval(timer)
|
|
152
|
-
const after = this.buffer.slice(endIndex + endTag.length)
|
|
153
|
-
const closeIndex = after.indexOf('>>')
|
|
154
|
-
if (closeIndex < 0) return
|
|
155
|
-
const [codeText, cwdText] = after.slice(0, closeIndex).split('|')
|
|
156
|
-
const startIndex = this.buffer.indexOf(startTag)
|
|
157
|
-
const output = startIndex >= 0
|
|
158
|
-
? this.buffer.slice(startIndex + startTag.length, endIndex).replace(/^\r?\n/u, '')
|
|
159
|
-
: this.buffer.slice(0, endIndex)
|
|
160
|
-
if (cwdText) {
|
|
161
|
-
this.cwd = cwdText
|
|
162
|
-
this.winCwd = toWindowsPath(cwdText)
|
|
163
|
-
}
|
|
164
|
-
const code = Number(codeText)
|
|
165
|
-
resolve({
|
|
166
|
-
ok: code === 0,
|
|
167
|
-
code,
|
|
168
|
-
output: output.length > MAX_OUTPUT
|
|
169
|
-
? output.slice(0, MAX_OUTPUT)
|
|
170
|
-
: (output.trim() === '' ? '(无输出)' : output),
|
|
171
|
-
cwd: this.winCwd,
|
|
172
|
-
bashCwd: this.cwd,
|
|
173
|
-
truncated: output.length > MAX_OUTPUT,
|
|
174
|
-
})
|
|
175
|
-
return
|
|
176
|
-
}
|
|
177
|
-
if (Date.now() - started > DEFAULT_TIMEOUT) {
|
|
178
|
-
clearInterval(timer)
|
|
179
|
-
this.kill()
|
|
180
|
-
resolve({
|
|
181
|
-
ok: false,
|
|
182
|
-
code: null,
|
|
183
|
-
timeout: true,
|
|
184
|
-
output: this.buffer.slice(0, 2000) + '\n(超时 30 秒,会话已重启)',
|
|
185
|
-
cwd: this.winCwd,
|
|
186
|
-
})
|
|
187
|
-
}
|
|
188
|
-
}, 30)
|
|
189
|
-
})
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
info() {
|
|
193
|
-
return {
|
|
194
|
-
running: this.running,
|
|
195
|
-
cwd: this.winCwd,
|
|
196
|
-
bashCwd: this.cwd,
|
|
197
|
-
commands: this.commandCount,
|
|
198
|
-
transcript: this.transcript.slice(-100),
|
|
199
|
-
uptimeMs: this.startedAt === 0 ? 0 : Date.now() - this.startedAt,
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
let shared = null
|
|
205
|
-
|
|
206
|
-
export function sharedSession(cwd) {
|
|
207
|
-
if (shared === null || !shared.running) {
|
|
208
|
-
shared = new ShellSession(cwd)
|
|
209
|
-
shared.start()
|
|
210
|
-
}
|
|
211
|
-
return shared
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
export function resetSharedSession() {
|
|
215
|
-
if (shared !== null) shared.reset()
|
|
216
|
-
}
|