@andy.ian/dsh-homing 2.0.0 → 2.0.1
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 +3 -3
- package/bin/auth.mjs +2 -2
- package/bin/backup.mjs +4 -4
- package/bin/bootstrap-server.mjs +4 -4
- package/bin/entry-link.mjs +2 -2
- package/bin/renew-cert.mjs +1 -1
- package/bin/supervisor.mjs +488 -0
- package/bin/watchdog.mjs +17 -12
- package/client.js +79 -0
- package/cordis.patch.yml +4 -4
- package/index.js +261 -74
- package/lib/files-page.mjs +228 -27
- package/lib/files.mjs +89 -2
- package/lib/limits.mjs +1 -1
- package/lib/pty.mjs +10 -3
- package/ops/watchdog/restart-hidden.vbs +7 -0
- package/ops/watchdog/restart-now.ps1 +25 -11
- package/ops/watchdog/run-hidden.vbs +4 -0
- package/package.json +5 -3
package/lib/files-page.mjs
CHANGED
|
@@ -44,7 +44,21 @@ export function layout({ title, body }) {
|
|
|
44
44
|
<html lang="zh-CN"><head><meta charset="utf-8">
|
|
45
45
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
46
46
|
<title>${escapeHtml(title)} · DSH 文件</title>
|
|
47
|
-
<style>${STYLE}
|
|
47
|
+
<style>${STYLE}.flist { display: flex; flex-direction: column; }
|
|
48
|
+
.sortbar { display: flex; align-items: center; gap: 6px; padding: 6px 2px 8px; color: #8b93a1; font-size: 12px; }
|
|
49
|
+
.sortlabel { margin-right: 2px; }
|
|
50
|
+
.sortbtn { padding: 4px 9px; border-radius: 6px; background: #222833; border: 1px solid #333c48; color: #b9c3d1; font-size: 12px; }
|
|
51
|
+
.sortbtn[data-active="1"] { background: #2f3a49; color: #e6edf5; border-color: #4a586b; }
|
|
52
|
+
.fitem { position: relative; display: grid; grid-template-columns: 22px auto 1fr auto; grid-template-areas: "ico size size menu" "name name name menu"; align-items: center; gap: 2px 6px; padding: 9px 2px 11px; border-bottom: 1px solid #232a35; }
|
|
53
|
+
.fico { grid-area: ico; font-size: 15px; line-height: 1; }
|
|
54
|
+
.fsize { grid-area: size; justify-self: start; color: #8b93a1; font-size: 12px; white-space: nowrap; pointer-events: none; }
|
|
55
|
+
.ftime { grid-area: 1 / 2 / 2 / 4; text-align: center; color: #8b93a1; font-size: 12px; white-space: nowrap; pointer-events: none; }
|
|
56
|
+
.fname { grid-area: name; color: #dfe6ef; text-decoration: none; font-size: 15px; word-break: break-all; }
|
|
57
|
+
.fname.dir { color: #cfe3ff; }
|
|
58
|
+
.fname.exe { color: #ffd479; }
|
|
59
|
+
.fempty { padding: 18px 4px; color: #8b93a1; }
|
|
60
|
+
.flist .mm { grid-area: menu; }
|
|
61
|
+
</style></head>
|
|
48
62
|
<body><div class="wrap">${body}</div></body></html>`
|
|
49
63
|
}
|
|
50
64
|
|
|
@@ -53,26 +67,187 @@ const messageHtml = (message, isError) => (message
|
|
|
53
67
|
: '')
|
|
54
68
|
|
|
55
69
|
/** 目录列表页 */
|
|
70
|
+
const LONG_PRESS_JS = `<script>
|
|
71
|
+
(() => {
|
|
72
|
+
var openMenu = null;
|
|
73
|
+
function closeAll() {
|
|
74
|
+
document.querySelectorAll('.rowmenu').forEach(function (m) { m.hidden = true; });
|
|
75
|
+
openMenu = null;
|
|
76
|
+
}
|
|
77
|
+
function position(btn, menu) {
|
|
78
|
+
menu.style.left = '0px';
|
|
79
|
+
menu.style.top = '0px';
|
|
80
|
+
var r = btn.getBoundingClientRect();
|
|
81
|
+
var mw = menu.offsetWidth;
|
|
82
|
+
var mh = menu.offsetHeight;
|
|
83
|
+
var left = Math.max(8, Math.min(r.right - mw, window.innerWidth - mw - 8));
|
|
84
|
+
var up = (window.innerHeight - r.bottom) < (mh + 20);
|
|
85
|
+
var top = up ? (r.top - mh - 6) : (r.bottom + 6);
|
|
86
|
+
menu.style.left = left + 'px';
|
|
87
|
+
menu.style.top = Math.max(8, top) + 'px';
|
|
88
|
+
requestAnimationFrame(function () {
|
|
89
|
+
var w2 = menu.offsetWidth, h2 = menu.offsetHeight;
|
|
90
|
+
var l2 = Math.max(8, Math.min(r.right - w2, window.innerWidth - w2 - 8));
|
|
91
|
+
var t2 = ((window.innerHeight - r.bottom) < (h2 + 20)) ? (r.top - h2 - 6) : (r.bottom + 6);
|
|
92
|
+
menu.style.left = l2 + 'px';
|
|
93
|
+
menu.style.top = Math.max(8, t2) + 'px';
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
document.addEventListener('click', function (e) {
|
|
97
|
+
var btn = e.target.closest ? e.target.closest('.mm-btn') : null;
|
|
98
|
+
if (btn) {
|
|
99
|
+
e.preventDefault(); e.stopPropagation();
|
|
100
|
+
var menu = btn.parentNode.querySelector('.rowmenu');
|
|
101
|
+
var wasOpen = openMenu === menu;
|
|
102
|
+
closeAll();
|
|
103
|
+
if (!wasOpen) { menu.hidden = false; position(btn, menu); openMenu = menu; }
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
var act = e.target.closest ? e.target.closest('.mi') : null;
|
|
107
|
+
if (act) {
|
|
108
|
+
e.preventDefault(); e.stopPropagation();
|
|
109
|
+
var kind = act.getAttribute('data-act');
|
|
110
|
+
var path = act.getAttribute('data-path');
|
|
111
|
+
closeAll();
|
|
112
|
+
if (kind === 'edit') {
|
|
113
|
+
location.href = '/files/edit?path=' + encodeURIComponent(path);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (kind === 'run') {
|
|
117
|
+
location.href = '/files/run?path=' + encodeURIComponent(path);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (kind === 'copy' || kind === 'cut') {
|
|
121
|
+
try { localStorage.setItem('dsh-files-clip', JSON.stringify({ path: path, op: kind === 'cut' ? 'cut' : 'copy' })); } catch (error) { /* 忽略 */ }
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (kind === 'mkdir' || kind === 'newfile') {
|
|
125
|
+
var nm = window.prompt(kind === 'mkdir' ? '新文件夹名:' : '新文件名:', '');
|
|
126
|
+
if (!nm) return;
|
|
127
|
+
var api = kind === 'mkdir' ? '/files/mkdir' : '/files/newfile';
|
|
128
|
+
fetch(api, {
|
|
129
|
+
method: 'POST',
|
|
130
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
131
|
+
body: 'dir=' + encodeURIComponent(path) + '&name=' + encodeURIComponent(nm),
|
|
132
|
+
}).then(function () { location.reload(); }).catch(function () { location.reload(); });
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (kind === 'paste') {
|
|
136
|
+
var raw = null;
|
|
137
|
+
try { raw = localStorage.getItem('dsh-files-clip'); } catch (error) { /* 忽略 */ }
|
|
138
|
+
if (!raw) { window.alert('剪贴板是空的,先点「复制」或「剪切」'); return; }
|
|
139
|
+
var clip = { path: raw, op: 'copy' };
|
|
140
|
+
try { var parsed = JSON.parse(raw); if (parsed && parsed.path) clip = parsed; } catch (error) { /* 兼容旧格式 */ }
|
|
141
|
+
fetch('/files/paste', {
|
|
142
|
+
method: 'POST',
|
|
143
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
144
|
+
body: 'src=' + encodeURIComponent(clip.path) + '&dir=' + encodeURIComponent(path) + '&op=' + encodeURIComponent(clip.op || 'copy'),
|
|
145
|
+
}).then(function () {
|
|
146
|
+
try { if ((clip.op || 'copy') === 'cut') localStorage.removeItem('dsh-files-clip'); } catch (error) { /* 忽略 */ }
|
|
147
|
+
location.reload();
|
|
148
|
+
}).catch(function () { location.reload(); });
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
if (kind === 'copypath') {
|
|
152
|
+
if (navigator.clipboard) navigator.clipboard.writeText(path);
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (kind === 'rename') {
|
|
156
|
+
var next = window.prompt('重命名为:', act.getAttribute('data-name'));
|
|
157
|
+
if (!next) return;
|
|
158
|
+
fetch('/files/rename', {
|
|
159
|
+
method: 'POST',
|
|
160
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
161
|
+
body: 'path=' + encodeURIComponent(path) + '&name=' + encodeURIComponent(next),
|
|
162
|
+
}).then(function () { location.reload(); }).catch(function () { location.reload(); });
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
if (kind === 'delete') {
|
|
166
|
+
var isDir2 = act.getAttribute('data-dir') === '1';
|
|
167
|
+
if (!window.confirm(isDir2 ? '删除这个目录及其全部内容?' : '删除这个文件?')) return;
|
|
168
|
+
fetch('/files/delete', {
|
|
169
|
+
method: 'POST',
|
|
170
|
+
headers: { 'content-type': 'application/x-www-form-urlencoded' },
|
|
171
|
+
body: 'path=' + encodeURIComponent(path),
|
|
172
|
+
}).then(function () { location.reload(); }).catch(function () { location.reload(); });
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
closeAll();
|
|
178
|
+
}, true);
|
|
179
|
+
var sortState = { key: '', asc: true };
|
|
180
|
+
function applySort(key) {
|
|
181
|
+
var list = document.querySelector('.flist');
|
|
182
|
+
if (!list) return;
|
|
183
|
+
var asc = (sortState.key === key) ? !sortState.asc : true;
|
|
184
|
+
sortState = { key: key, asc: asc };
|
|
185
|
+
var items = Array.prototype.slice.call(list.querySelectorAll('.fitem'));
|
|
186
|
+
items.sort(function (a, b) {
|
|
187
|
+
var ad = a.getAttribute('data-dir') === '1', bd = b.getAttribute('data-dir') === '1';
|
|
188
|
+
if (ad !== bd) return ad ? -1 : 1;
|
|
189
|
+
var r = 0;
|
|
190
|
+
if (key === 'name') {
|
|
191
|
+
r = a.getAttribute('data-name').localeCompare(b.getAttribute('data-name'), 'zh');
|
|
192
|
+
} else if (key === 'size') {
|
|
193
|
+
r = Number(a.getAttribute('data-size')) - Number(b.getAttribute('data-size'));
|
|
194
|
+
} else {
|
|
195
|
+
r = Number(a.getAttribute('data-time')) - Number(b.getAttribute('data-time'));
|
|
196
|
+
}
|
|
197
|
+
return asc ? r : -r;
|
|
198
|
+
});
|
|
199
|
+
items.forEach(function (el) { list.appendChild(el); });
|
|
200
|
+
document.querySelectorAll('.sortbtn').forEach(function (btn) {
|
|
201
|
+
btn.setAttribute('data-active', btn.getAttribute('data-sort') === key ? '1' : '0');
|
|
202
|
+
btn.textContent = btn.getAttribute('data-sort') === 'name' ? '名称' : (btn.getAttribute('data-sort') === 'size' ? '大小' : '时间');
|
|
203
|
+
if (btn.getAttribute('data-sort') === key) btn.textContent += asc ? ' ↑' : ' ↓';
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
document.addEventListener('click', function (e) {
|
|
207
|
+
var btn = e.target.closest ? e.target.closest('.sortbtn') : null;
|
|
208
|
+
if (btn) { e.preventDefault(); applySort(btn.getAttribute('data-sort')); }
|
|
209
|
+
}, true);
|
|
210
|
+
document.addEventListener('keydown', function (e) { if (e.key === 'Escape') closeAll(); });
|
|
211
|
+
window.addEventListener('scroll', closeAll, { passive: true });
|
|
212
|
+
})();
|
|
213
|
+
</script>`;
|
|
214
|
+
|
|
56
215
|
export function renderListing({ info, message, isError, home, dataDir, workspaces = [] }) {
|
|
216
|
+
const menuFor = (entry) => `<span class="mm">
|
|
217
|
+
<button type="button" class="mm-btn" aria-haspopup="menu" title="更多操作">\u22ef</button>
|
|
218
|
+
<span class="rowmenu" hidden role="menu">
|
|
219
|
+
${entry.dir ? '' : `<a class="mi" role="menuitem" data-act="edit" data-path="${escapeHtml(entry.path)}" href="#"><span class="ico">\u270F\uFE0F</span>编辑</a>`}
|
|
220
|
+
${entry.runnable ? `<a class="mi" role="menuitem" data-act="run" data-path="${escapeHtml(entry.path)}" href="#"><span class="ico">\u25B6\uFE0F</span>执行</a>` : ''}
|
|
221
|
+
<a class="mi" role="menuitem" data-act="rename" data-path="${escapeHtml(entry.path)}" data-name="${escapeHtml(entry.name)}" href="#"><span class="ico">\u{1F3F7}\uFE0F</span>重命名</a>
|
|
222
|
+
<a class="mi" role="menuitem" data-act="cut" data-path="${escapeHtml(entry.path)}" href="#"><span class="ico">\u2702\uFE0F</span>剪切</a>
|
|
223
|
+
<a class="mi" role="menuitem" data-act="copy" data-path="${escapeHtml(entry.path)}" href="#"><span class="ico">\u{1F4C4}</span>复制</a>
|
|
224
|
+
${entry.dir ? `<a class="mi" role="menuitem" data-act="paste" data-path="${escapeHtml(entry.path)}" href="#"><span class="ico">\u{1F4E5}</span>粘贴到此处</a>
|
|
225
|
+
<a class="mi" role="menuitem" data-act="mkdir" data-path="${escapeHtml(entry.path)}" href="#"><span class="ico">\u{1F4C1}</span>新建文件夹</a>
|
|
226
|
+
<a class="mi" role="menuitem" data-act="newfile" data-path="${escapeHtml(entry.path)}" href="#"><span class="ico">\u{1F4C4}</span>新建文件</a>` : ''}
|
|
227
|
+
<a class="mi" role="menuitem" data-act="copypath" data-path="${escapeHtml(entry.path)}" href="#"><span class="ico">\u{1F4CB}</span>复制路径</a>
|
|
228
|
+
<a class="mi danger" role="menuitem" data-act="delete" data-path="${escapeHtml(entry.path)}" data-dir="${entry.dir ? 1 : 0}" href="#"><span class="ico">\u{1F5D1}\uFE0F</span>删除</a>
|
|
229
|
+
</span>
|
|
230
|
+
</span>`;
|
|
231
|
+
|
|
57
232
|
const rows = info.entries.map((entry) => {
|
|
58
|
-
const label = escapeHtml(entry.name)
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}).join('')
|
|
233
|
+
const label = escapeHtml(entry.name);
|
|
234
|
+
const icon = entry.dir ? '\u{1F4C1}' : (entry.runnable ? '\u2699\uFE0F' : '\u{1F4C4}');
|
|
235
|
+
const nameHtml = entry.dir
|
|
236
|
+
? `<a class="fname dir" href="/files?path=${encodeURIComponent(entry.path)}">${label}</a>`
|
|
237
|
+
: `<a class="fname${entry.runnable ? ' exe' : ''}" href="/files/view?path=${encodeURIComponent(entry.path)}">${label}</a>`;
|
|
238
|
+
return `<div class="fitem" data-path="${escapeHtml(entry.path)}" data-name="${escapeHtml(entry.name)}" data-dir="${entry.dir ? 1 : 0}" data-size="${Number(entry.size) || 0}" data-time="${Number(entry.mtimeMs) || 0}">
|
|
239
|
+
<div class="fico">${icon}</div>
|
|
240
|
+
<div class="fsize">${escapeHtml(entry.sizeText ?? '')}</div>
|
|
241
|
+
<div class="ftime">${escapeHtml(entry.mtime ?? '')}</div>
|
|
242
|
+
${nameHtml}
|
|
243
|
+
${menuFor(entry)}
|
|
244
|
+
</div>`;
|
|
245
|
+
}).join('');
|
|
72
246
|
|
|
247
|
+
const navStyle = 'flex:0 0 auto;padding:4px 7px;border-radius:6px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:11px;line-height:1.4;white-space:nowrap'
|
|
73
248
|
const parent = info.parent
|
|
74
|
-
? `<a class="btn ghost" href="/files?path=${encodeURIComponent(info.parent)}">上一级</a>`
|
|
75
|
-
:
|
|
249
|
+
? `<a class="btn ghost" href="/files?path=${encodeURIComponent(info.parent)}" style="${navStyle}">上一级</a>`
|
|
250
|
+
: `<span style="${navStyle};opacity:.3">上一级</span>`
|
|
76
251
|
|
|
77
252
|
const chips = workspaces.length === 0 ? '' : `
|
|
78
253
|
<div class="chips">
|
|
@@ -81,7 +256,17 @@ export function renderListing({ info, message, isError, home, dataDir, workspace
|
|
|
81
256
|
|
|
82
257
|
const body = `
|
|
83
258
|
<div class="bar">
|
|
84
|
-
<
|
|
259
|
+
<div style="display:flex;align-items:center;gap:8px;flex-wrap:nowrap;min-width:0">
|
|
260
|
+
<h1 style="margin:0;flex:0 0 auto;font-size:18px">文件</h1>
|
|
261
|
+
<div style="flex:1 1 auto;display:flex;align-items:center;justify-content:flex-end;gap:4px;flex-wrap:nowrap;overflow-x:auto;-webkit-overflow-scrolling:touch;scrollbar-width:none">
|
|
262
|
+
<a class="btn ghost" href="/" style="${navStyle}">DSH</a>
|
|
263
|
+
<a class="btn ghost" href="/term" style="${navStyle}">终端</a>
|
|
264
|
+
<a class="btn ghost" href="/git?path=${encodeURIComponent(info.path)}" style="${navStyle}">Git</a>
|
|
265
|
+
<a class="btn ghost" href="/files?path=${encodeURIComponent(home)}" style="${navStyle}">HOME</a>
|
|
266
|
+
<a class="btn ghost" href="/files?path=${encodeURIComponent(dataDir)}" style="${navStyle}">配置</a>
|
|
267
|
+
${parent}
|
|
268
|
+
</div>
|
|
269
|
+
</div>
|
|
85
270
|
<form method="get" action="/files" class="row">
|
|
86
271
|
<input type="text" name="path" list="path-suggestions" value="${escapeHtml(info.path)}" placeholder="输入绝对路径">
|
|
87
272
|
<datalist id="path-suggestions">
|
|
@@ -89,20 +274,32 @@ export function renderListing({ info, message, isError, home, dataDir, workspace
|
|
|
89
274
|
</datalist>
|
|
90
275
|
<button type="submit">前往</button>
|
|
91
276
|
</form>
|
|
92
|
-
<div class="row">
|
|
93
|
-
<a class="btn ghost" href="/files?path=${encodeURIComponent(home)}" style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">主目录</a>
|
|
94
|
-
<a class="btn ghost" href="/files?path=${encodeURIComponent(dataDir)}" style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">DSH 数据</a>
|
|
95
|
-
${parent}
|
|
96
|
-
<a class="btn ghost" href="/git?path=${encodeURIComponent(info.path)}"
|
|
97
|
-
style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">Git</a>
|
|
98
|
-
</div>
|
|
99
277
|
</div>
|
|
100
278
|
${chips}
|
|
101
279
|
${messageHtml(message, isError)}
|
|
102
280
|
${info.error ? `<div class="msg err">${escapeHtml(info.error)}</div>` : ''}
|
|
103
281
|
${info.truncated ? `<div class="msg">目录项过多,只显示前 2000 个</div>` : ''}
|
|
104
|
-
<
|
|
105
|
-
|
|
282
|
+
<style>
|
|
283
|
+
.mm { position: relative; display: inline-block; }
|
|
284
|
+
.mm-btn { margin-left: 6px; padding: 6px 10px; border-radius: 8px; background: #2a2f3a; border: 1px solid #3a4351; color: #c7cdd8; font-size: 14px; line-height: 1; }
|
|
285
|
+
.mm-btn:active { background: #3a4351; }
|
|
286
|
+
.rowmenu { position: fixed; z-index: 9999; min-width: 0; padding: 4px 0; background: #20262f; border: 1px solid #3a4351; border-radius: 10px; box-shadow: 0 12px 32px rgba(0,0,0,.6); }
|
|
287
|
+
.rowmenu[hidden] { display: none; }
|
|
288
|
+
.rowmenu .mi { display: flex; align-items: center; gap: 9px; padding: 8px 14px 8px 10px; color: #e3e9f1; text-decoration: none; font-size: 13px; line-height: 1.35; white-space: nowrap; text-align: left; }
|
|
289
|
+
.rowmenu .ico { width: 18px; flex: 0 0 18px; text-align: center; opacity: .95; font-size: 13px; }
|
|
290
|
+
.rowmenu .mi.danger { color: #ff8f8f; }
|
|
291
|
+
.rowmenu .mi:first-child { border-radius: 8px 8px 0 0; }
|
|
292
|
+
.rowmenu .mi:last-child { border-radius: 0 0 8px 8px; }
|
|
293
|
+
.rowmenu .mi:active, .rowmenu .mi:hover { background: #333c48; }
|
|
294
|
+
</style>
|
|
295
|
+
<div class="sortbar">
|
|
296
|
+
<span class="sortlabel">排序</span>
|
|
297
|
+
<button type="button" class="sortbtn" data-sort="name">名称</button>
|
|
298
|
+
<button type="button" class="sortbtn" data-sort="size">大小</button>
|
|
299
|
+
<button type="button" class="sortbtn" data-sort="time">时间</button>
|
|
300
|
+
</div>
|
|
301
|
+
<div class="flist">${rows || '<div class="fempty">空目录</div>'}</div>
|
|
302
|
+
${LONG_PRESS_JS}\n <p class="crumbs">提示:点目录进入,点文件名查看内容;标 ⚙️ 的可直接运行(会在这台电脑上执行)。</p>`
|
|
106
303
|
|
|
107
304
|
return layout({ title: info.path, body })
|
|
108
305
|
}
|
|
@@ -202,6 +399,10 @@ export function renderGitPage({ repoPath, summary = null, args = '', result = nu
|
|
|
202
399
|
style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">刷新</a>
|
|
203
400
|
<a class="btn ghost" href="/files?path=${encodeURIComponent(repoPath)}"
|
|
204
401
|
style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">看文件</a>
|
|
402
|
+
<a class="btn ghost" href="/term"
|
|
403
|
+
style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">终端</a>
|
|
404
|
+
<a class="btn ghost" href="/"
|
|
405
|
+
style="flex:1;text-align:center;padding:10px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;font-size:14px">DSH</a>
|
|
205
406
|
</div>
|
|
206
407
|
</div>
|
|
207
408
|
${message ? `<div class="msg">${escapeHtml(message)}</div>` : ''}
|
package/lib/files.mjs
CHANGED
|
@@ -30,7 +30,8 @@ const fmtSize = (bytes) => {
|
|
|
30
30
|
if (bytes < 1024) return `${bytes} B`
|
|
31
31
|
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`
|
|
32
32
|
if (bytes < 1024 * 1024 * 1024) return `${(bytes / 1024 / 1024).toFixed(1)} MB`
|
|
33
|
-
return `${(bytes / 1024
|
|
33
|
+
if (bytes < 1024 ** 4) return `${(bytes / 1024 ** 3).toFixed(1)} GB`
|
|
34
|
+
return `${(bytes / 1024 ** 4).toFixed(1)} TB`
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
const fmtTime = (date) => {
|
|
@@ -374,7 +375,7 @@ export function runFile(absPath, args = '', { timeoutMs = 15_000, cwd } = {}) {
|
|
|
374
375
|
let command = target;
|
|
375
376
|
let argv = argList;
|
|
376
377
|
if (kind === 'ps1') {
|
|
377
|
-
const wrapper = path.join(os.tmpdir(), 'dsh-
|
|
378
|
+
const wrapper = path.join(os.tmpdir(), 'dsh-homing-run-ps1.ps1');
|
|
378
379
|
fs.writeFileSync(wrapper, [
|
|
379
380
|
'[Console]::OutputEncoding = [System.Text.Encoding]::UTF8',
|
|
380
381
|
'$target = $args[0]',
|
|
@@ -462,3 +463,89 @@ export function runFile(absPath, args = '', { timeoutMs = 15_000, cwd } = {}) {
|
|
|
462
463
|
}
|
|
463
464
|
|
|
464
465
|
export { fmtSize, fmtTime }
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* 重命名同一目录下的文件/目录。
|
|
469
|
+
* 只接受纯名字(不含路径分隔符),目标已存在则拒绝,绝不跨目录移动。
|
|
470
|
+
*/
|
|
471
|
+
export function renameEntry(absPath, newName) {
|
|
472
|
+
const bad = /[\\/:*?"<>|\u0000-\u001f]/u;
|
|
473
|
+
const name = String(newName ?? '').trim();
|
|
474
|
+
if (!name) return { ok: false, error: '名字不能为空' };
|
|
475
|
+
if (name === '.' || name === '..') return { ok: false, error: '非法名字' };
|
|
476
|
+
if (bad.test(name)) return { ok: false, error: '名字里不能包含 \\ / : * ? " < > |' };
|
|
477
|
+
const dir = path.dirname(absPath);
|
|
478
|
+
const target = path.join(dir, name);
|
|
479
|
+
if (path.resolve(target) === path.resolve(absPath)) return { ok: true, path: absPath };
|
|
480
|
+
if (fs.existsSync(target)) return { ok: false, error: '同名文件已存在' };
|
|
481
|
+
fs.renameSync(absPath, target);
|
|
482
|
+
return { ok: true, path: target };
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/** 删除文件或目录(目录递归)。调用方必须已做二次确认。 */
|
|
486
|
+
export function deleteEntry(absPath) {
|
|
487
|
+
const target = path.resolve(String(absPath ?? ''));
|
|
488
|
+
if (!target || !fs.existsSync(target)) return { ok: false, error: '不存在' };
|
|
489
|
+
const stat = fs.statSync(target);
|
|
490
|
+
if (stat.isDirectory()) fs.rmSync(target, { recursive: true, force: true });
|
|
491
|
+
else fs.unlinkSync(target);
|
|
492
|
+
return { ok: true };
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/** 把 src 复制进 destDir;同名则自动加 " - 副本"/" - 副本2" 后缀,绝不覆盖。 */
|
|
496
|
+
export function copyInto(srcPath, destDir) {
|
|
497
|
+
const src = path.resolve(String(srcPath ?? ''));
|
|
498
|
+
const dir = path.resolve(String(destDir ?? ''));
|
|
499
|
+
if (!fs.existsSync(src)) return { ok: false, error: '源不存在' };
|
|
500
|
+
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) return { ok: false, error: '目标不是目录' };
|
|
501
|
+
const base = path.basename(src);
|
|
502
|
+
const ext = path.extname(base);
|
|
503
|
+
const stem = ext ? base.slice(0, -ext.length) : base;
|
|
504
|
+
let target = path.join(dir, base);
|
|
505
|
+
let n = 1;
|
|
506
|
+
while (fs.existsSync(target)) {
|
|
507
|
+
const name = ext ? `${stem} - 副本${n > 1 ? n : ''}${ext}` : `${stem} - 副本${n > 1 ? n : ''}`;
|
|
508
|
+
target = path.join(dir, name);
|
|
509
|
+
n += 1;
|
|
510
|
+
if (n > 100) return { ok: false, error: '同名文件过多' };
|
|
511
|
+
}
|
|
512
|
+
fs.cpSync(src, target, { recursive: true, errorOnExist: false, force: false });
|
|
513
|
+
return { ok: true, path: target };
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
const BAD_NAME = /[\\/:*?"<>|\u0000-\u001f]/u;
|
|
517
|
+
|
|
518
|
+
/** 在 dir 下新建文件或目录;名字非法或已存在则拒绝。 */
|
|
519
|
+
export function createEntry(dirPath, name, isDir) {
|
|
520
|
+
const dir = path.resolve(String(dirPath ?? ''));
|
|
521
|
+
const clean = String(name ?? '').trim();
|
|
522
|
+
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) return { ok: false, error: '目标不是目录' };
|
|
523
|
+
if (!clean) return { ok: false, error: '名字不能为空' };
|
|
524
|
+
if (clean === '.' || clean === '..' || BAD_NAME.test(clean)) return { ok: false, error: '名字不合法' };
|
|
525
|
+
const target = path.join(dir, clean);
|
|
526
|
+
if (fs.existsSync(target)) return { ok: false, error: '同名文件已存在' };
|
|
527
|
+
if (isDir) fs.mkdirSync(target);
|
|
528
|
+
else fs.writeFileSync(target, '');
|
|
529
|
+
return { ok: true, path: target };
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/** 把 src 移动进 destDir;同名自动改名,绝不覆盖。 */
|
|
533
|
+
export function moveInto(srcPath, destDir) {
|
|
534
|
+
const src = path.resolve(String(srcPath ?? ''));
|
|
535
|
+
const dir = path.resolve(String(destDir ?? ''));
|
|
536
|
+
if (!fs.existsSync(src)) return { ok: false, error: '源不存在' };
|
|
537
|
+
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) return { ok: false, error: '目标不是目录' };
|
|
538
|
+
if (path.dirname(src) === dir) return { ok: false, error: '已经在目标目录里' };
|
|
539
|
+
const base = path.basename(src);
|
|
540
|
+
const ext = path.extname(base);
|
|
541
|
+
const stem = ext ? base.slice(0, -ext.length) : base;
|
|
542
|
+
let target = path.join(dir, base);
|
|
543
|
+
let n = 1;
|
|
544
|
+
while (fs.existsSync(target)) {
|
|
545
|
+
target = path.join(dir, ext ? `${stem} - 副本${n > 1 ? n : ''}${ext}` : `${stem} - 副本${n > 1 ? n : ''}`);
|
|
546
|
+
n += 1;
|
|
547
|
+
if (n > 100) return { ok: false, error: '同名文件过多' };
|
|
548
|
+
}
|
|
549
|
+
fs.renameSync(src, target);
|
|
550
|
+
return { ok: true, path: target };
|
|
551
|
+
}
|
package/lib/limits.mjs
CHANGED
package/lib/pty.mjs
CHANGED
|
@@ -45,7 +45,7 @@ const KEYGEN_EXE = 'C:/Windows/System32/OpenSSH/ssh-keygen.exe'
|
|
|
45
45
|
|
|
46
46
|
/**
|
|
47
47
|
* DSH_HOME:优先用插件传进来的值。
|
|
48
|
-
* 不能从代码位置推(仓库里是 D:\...\plugin\lib,部署后是 <DSH_HOME>\plugins\dsh-
|
|
48
|
+
* 不能从代码位置推(仓库里是 D:\...\plugin\lib,部署后是 <DSH_HOME>\plugins\dsh-homing\lib),
|
|
49
49
|
* 所以这里只在调用方没传时用环境变量兜底。
|
|
50
50
|
*/
|
|
51
51
|
export const resolveDshHome = () => process.env.DSH_HOME ?? path.resolve(here, '..', '..', '..')
|
|
@@ -74,13 +74,20 @@ const waitFor = async (predicate, timeoutMs = 8000) => {
|
|
|
74
74
|
return false
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
export const terminalDir = (dshHome = resolveDshHome()) =>
|
|
77
|
+
export const terminalDir = (dshHome = resolveDshHome()) => {
|
|
78
|
+
const dir = path.join(dshHome, 'integrations', 'dsh-homing', 'terminal')
|
|
79
|
+
// 老版本把 sshd 的密钥/配置放在 dsh-homing/terminal 下;新目录没有就沿用旧的,
|
|
80
|
+
// 避免改名后终端连不上(密钥变了 sshd 得重建,会话会断一下)。
|
|
81
|
+
const legacy = path.join(dshHome, 'integrations', 'dsh-homing', 'terminal')
|
|
82
|
+
if (!fs.existsSync(dir) && fs.existsSync(legacy)) return legacy
|
|
83
|
+
return dir
|
|
84
|
+
}
|
|
78
85
|
|
|
79
86
|
export const ensureKeyPair = (dir) => {
|
|
80
87
|
const key = path.join(dir, 'id_ed25519')
|
|
81
88
|
if (fs.existsSync(key) && fs.existsSync(key + '.pub')) return key
|
|
82
89
|
fs.mkdirSync(dir, { recursive: true })
|
|
83
|
-
const result = spawnSync(KEYGEN_EXE, ['-t', 'ed25519', '-N', '', '-C', 'dsh-
|
|
90
|
+
const result = spawnSync(KEYGEN_EXE, ['-t', 'ed25519', '-N', '', '-C', 'dsh-homing-terminal', '-f', key], {
|
|
84
91
|
encoding: 'utf8',
|
|
85
92
|
timeout: 20000,
|
|
86
93
|
windowsHide: true,
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
' ???? restart-now.ps1?????????????????????
|
|
2
|
+
Set shell = CreateObject("WScript.Shell")
|
|
3
|
+
cmd = "powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File ""C:\Users\andyi\.dsh\watchdog\restart-now.ps1"""
|
|
4
|
+
For Each a In WScript.Arguments
|
|
5
|
+
cmd = cmd & " """ & a & """"
|
|
6
|
+
Next
|
|
7
|
+
shell.Run cmd, 0, False
|
|
@@ -195,17 +195,21 @@ try {
|
|
|
195
195
|
Write-Log ("关闭前进程数:{0}" -f $before.Count)
|
|
196
196
|
|
|
197
197
|
if ($before.Count -gt 0) {
|
|
198
|
-
|
|
199
|
-
Start-Sleep -Seconds 4
|
|
200
|
-
$left = Get-DshProcesses
|
|
201
|
-
if ($left.Count -gt 0) {
|
|
202
|
-
|
|
203
|
-
$left |
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
198
|
+
$before | ForEach-Object { $_.CloseMainWindow() | Out-Null }
|
|
199
|
+
Start-Sleep -Seconds 4
|
|
200
|
+
$left = Get-DshProcesses
|
|
201
|
+
if ($left.Count -gt 0) {
|
|
202
|
+
# 先杀 Electron 主进程(根):否则它把 Host 退出当崩溃,弹原生对话框+提示音
|
|
203
|
+
$ids = @($left | ForEach-Object { $_.Id })
|
|
204
|
+
$cim = @(Get-CimInstance Win32_Process -Filter ("Name = '" + $procName + ".exe'") -ErrorAction SilentlyContinue)
|
|
205
|
+
$roots = @($cim | Where-Object { $ids -notcontains $_.ParentProcessId })
|
|
206
|
+
foreach ($root in $roots) { Write-Log ('先终止主进程 PID ' + $root.ProcessId); Stop-Process -Id $root.ProcessId -Force -ErrorAction SilentlyContinue }
|
|
207
|
+
if ($roots.Count -gt 0) { $w = 0; while ($w -lt 20 -and (Get-DshProcesses).Count -gt 0) { Start-Sleep -Milliseconds 250; $w += 1 } }
|
|
208
|
+
$rest = Get-DshProcesses
|
|
209
|
+
if ($rest.Count -gt 0) { Write-Log ('再终止其余 ' + $rest.Count + ' 个进程'); $rest | Stop-Process -Force -ErrorAction SilentlyContinue; Start-Sleep -Seconds 3 }
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
$still = Get-DshProcesses
|
|
209
213
|
Write-Log ("关闭后进程数:{0}" -f $still.Count)
|
|
210
214
|
|
|
211
215
|
# 端口必须释放干净;释放不了就失败退出,绝不硬起第二个实例
|
|
@@ -223,6 +227,16 @@ try {
|
|
|
223
227
|
|
|
224
228
|
Start-Process -FilePath $exe
|
|
225
229
|
Write-Log '已发起启动'
|
|
230
|
+
# 等窗口:拿到 MainWindowHandle 才算真的起来了
|
|
231
|
+
$winWait = 0
|
|
232
|
+
$haveWindow = $false
|
|
233
|
+
while ($winWait -lt 40) {
|
|
234
|
+
Start-Sleep -Milliseconds 500
|
|
235
|
+
$winWait += 1
|
|
236
|
+
$live = Get-Process -Name $procName -ErrorAction SilentlyContinue
|
|
237
|
+
if (@($live | Where-Object { $_.MainWindowHandle -ne 0 }).Count -gt 0) { $haveWindow = $true; break }
|
|
238
|
+
}
|
|
239
|
+
if ($haveWindow) { Write-Log ('窗口已出现(等待 ' + $winWait + ' 次)') } else { Write-Log ('已启动但未拿到窗口(等待 ' + $winWait + ' 次)—— relaunched-no-window') }
|
|
226
240
|
|
|
227
241
|
# 就绪判断:进程活着 + 8787 在听
|
|
228
242
|
$ok = $false
|
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.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "把本机 DSH 通过 SSH 反向隧道安全开放到手机浏览器:TLS 端到端(服务器只透传)、账号 + TOTP、局域网与公网双入口,随 DSH 启动。",
|
|
6
6
|
"keywords": [
|
|
@@ -29,7 +29,8 @@
|
|
|
29
29
|
"dsh-homing-auth": "./bin/auth.mjs",
|
|
30
30
|
"dsh-homing-backup": "./bin/backup.mjs",
|
|
31
31
|
"dsh-homing-entry": "./bin/entry-link.mjs",
|
|
32
|
-
"dsh-homing-watchdog": "./bin/watchdog.mjs"
|
|
32
|
+
"dsh-homing-watchdog": "./bin/watchdog.mjs",
|
|
33
|
+
"dsh-homing-supervisor": "./bin/supervisor.mjs"
|
|
33
34
|
},
|
|
34
35
|
"exports": {
|
|
35
36
|
".": "./index.js",
|
|
@@ -54,7 +55,8 @@
|
|
|
54
55
|
"README.md",
|
|
55
56
|
"LICENSE",
|
|
56
57
|
"lib/vendor/**",
|
|
57
|
-
"ops/**"
|
|
58
|
+
"ops/**",
|
|
59
|
+
"client.js"
|
|
58
60
|
],
|
|
59
61
|
"publishConfig": {
|
|
60
62
|
"access": "public",
|