@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/index.js
CHANGED
|
@@ -32,7 +32,7 @@ import * as deviceStore from './lib/devices.mjs';
|
|
|
32
32
|
import { checkUnsafeOrigin } from './lib/csrf.mjs';
|
|
33
33
|
import * as limits from './lib/limits.mjs';
|
|
34
34
|
|
|
35
|
-
export const name = 'dsh-
|
|
35
|
+
export const name = 'dsh-homing';
|
|
36
36
|
|
|
37
37
|
// 插件版本(调试页显示用),读不到也不影响运行
|
|
38
38
|
const PLUGIN_VERSION = (() => {
|
|
@@ -94,7 +94,7 @@ function normalizeConfig(raw) {
|
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
/* ------------------------------------------------------------------ *
|
|
97
|
-
* 数据目录(跨平台:$DSH_HOME/integrations/dsh-
|
|
97
|
+
* 数据目录(跨平台:$DSH_HOME/integrations/dsh-homing)
|
|
98
98
|
* ------------------------------------------------------------------ */
|
|
99
99
|
|
|
100
100
|
/** 本机所有局域网 IPv4,用来在日志里给出「同一个 Wi-Fi 用哪条地址」。 */
|
|
@@ -139,7 +139,21 @@ function sendAlert(dir, config, title, text) {
|
|
|
139
139
|
|
|
140
140
|
function dataDir() {
|
|
141
141
|
const home = process.env.DSH_HOME || path.join(os.homedir(), '.dsh');
|
|
142
|
-
const
|
|
142
|
+
const legacyDir = path.join(home, 'integrations', 'dsh-homing');
|
|
143
|
+
const dir = path.join(home, 'integrations', 'dsh-homing');
|
|
144
|
+
// 数据目录随插件改名:旧目录存在而新目录不存在时,自动迁过去。
|
|
145
|
+
// 只 rename 一次;旧目录里的设备表/密码/证书全部保留,已配对设备不会掉线。
|
|
146
|
+
try {
|
|
147
|
+
if (fs.existsSync(legacyDir) && !fs.existsSync(dir)) {
|
|
148
|
+
fs.mkdirSync(path.dirname(dir), { recursive: true });
|
|
149
|
+
fs.renameSync(legacyDir, dir);
|
|
150
|
+
}
|
|
151
|
+
} catch {
|
|
152
|
+
// 迁移失败就退回旧目录,不能因为改名把服务弄挂
|
|
153
|
+
if (fs.existsSync(legacyDir)) {
|
|
154
|
+
return legacyDir;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
143
157
|
fs.mkdirSync(dir, { recursive: true });
|
|
144
158
|
return dir;
|
|
145
159
|
}
|
|
@@ -188,7 +202,7 @@ function readCookies(headerValue) {
|
|
|
188
202
|
* 账号(存本机,scrypt 哈希)
|
|
189
203
|
* ------------------------------------------------------------------ */
|
|
190
204
|
|
|
191
|
-
const SESSION_COOKIE = 'dsh-session';
|
|
205
|
+
const SESSION_COOKIE = 'dsh-homing-session';
|
|
192
206
|
const SESSION_SECONDS = 3 * 24 * 60 * 60;
|
|
193
207
|
|
|
194
208
|
function loadAuth(dir) {
|
|
@@ -356,17 +370,17 @@ const MOBILE_CSS = `html { -webkit-text-size-adjust: 100%; }
|
|
|
356
370
|
[class*="_frame"] { min-width: 0; }
|
|
357
371
|
|
|
358
372
|
/* 隐藏时只把侧边栏那一列压成 0,主内容自然占满;其余布局仍归应用自己算 */
|
|
359
|
-
html[data-dsh-
|
|
373
|
+
html[data-dsh-homing-nav="hidden"] [class*="_frame"] {
|
|
360
374
|
grid-template-columns: 0 1fr 0 !important;
|
|
361
375
|
transition: none !important;
|
|
362
376
|
}
|
|
363
|
-
html[data-dsh-
|
|
377
|
+
html[data-dsh-homing-nav="hidden"] [class*="sidebarCol"] {
|
|
364
378
|
min-width: 0 !important;
|
|
365
379
|
overflow: hidden !important;
|
|
366
380
|
}
|
|
367
381
|
|
|
368
382
|
/* 侧边栏完全隐藏时,左上角探出的小耳朵(磁吸感):点它恢复完整侧边栏 */
|
|
369
|
-
.dsh-
|
|
383
|
+
.dsh-homing-ear {
|
|
370
384
|
position: fixed;
|
|
371
385
|
left: 0;
|
|
372
386
|
top: 16px;
|
|
@@ -389,7 +403,7 @@ const MOBILE_CSS = `html { -webkit-text-size-adjust: 100%; }
|
|
|
389
403
|
user-select: none;
|
|
390
404
|
touch-action: manipulation;
|
|
391
405
|
}
|
|
392
|
-
.dsh-
|
|
406
|
+
.dsh-homing-ear:active { background: rgba(60, 66, 78, 0.95); color: #e8eaed; }
|
|
393
407
|
}
|
|
394
408
|
`;
|
|
395
409
|
|
|
@@ -401,7 +415,7 @@ const MOBILE_JS = `(() => {
|
|
|
401
415
|
const sidebar = () => document.querySelector('[class*="sidebarCol"]')
|
|
402
416
|
const openBtn = () => document.querySelector('[aria-label="打开侧边栏"]')
|
|
403
417
|
const collapseBtn = () => document.querySelector('[aria-label="收起侧边栏"]')
|
|
404
|
-
const isHidden = () => root.getAttribute('data-dsh-
|
|
418
|
+
const isHidden = () => root.getAttribute('data-dsh-homing-nav') === 'hidden'
|
|
405
419
|
// 我们自己去点应用按钮时,要绕过下面那个捕获拦截,否则会自己拦自己、无限递归
|
|
406
420
|
let quietClick = false
|
|
407
421
|
const clickQuietly = (btn) => {
|
|
@@ -420,10 +434,10 @@ const MOBILE_JS = `(() => {
|
|
|
420
434
|
const hide = () => {
|
|
421
435
|
if (!narrow()) return false
|
|
422
436
|
const before = widthOf(mainCol())
|
|
423
|
-
root.setAttribute('data-dsh-
|
|
437
|
+
root.setAttribute('data-dsh-homing-nav', 'hidden')
|
|
424
438
|
const after = widthOf(mainCol())
|
|
425
439
|
if (!(after > before + 10)) {
|
|
426
|
-
root.removeAttribute('data-dsh-
|
|
440
|
+
root.removeAttribute('data-dsh-homing-nav')
|
|
427
441
|
console.warn('[dsh-homing] 侧边栏隐藏未生效,已自动撤销')
|
|
428
442
|
updateEar()
|
|
429
443
|
return false
|
|
@@ -433,7 +447,7 @@ const MOBILE_JS = `(() => {
|
|
|
433
447
|
}
|
|
434
448
|
|
|
435
449
|
const showAttribute = () => {
|
|
436
|
-
root.removeAttribute('data-dsh-
|
|
450
|
+
root.removeAttribute('data-dsh-homing-nav')
|
|
437
451
|
updateEar()
|
|
438
452
|
}
|
|
439
453
|
|
|
@@ -481,7 +495,7 @@ const MOBILE_JS = `(() => {
|
|
|
481
495
|
const installEar = () => {
|
|
482
496
|
ear = document.createElement('button')
|
|
483
497
|
ear.type = 'button'
|
|
484
|
-
ear.className = 'dsh-
|
|
498
|
+
ear.className = 'dsh-homing-ear'
|
|
485
499
|
ear.setAttribute('aria-label', '展开侧边栏')
|
|
486
500
|
ear.textContent = '›'
|
|
487
501
|
ear.addEventListener('click', (event) => {
|
|
@@ -509,7 +523,7 @@ const MOBILE_JS = `(() => {
|
|
|
509
523
|
|
|
510
524
|
// 记住最后点过的工作区:切换工作区必然有一次点击,这是最可靠的信号。
|
|
511
525
|
// 存的只是名字,由服务端映射成路径;对不上就退回默认工作区。
|
|
512
|
-
const LAST_WS_KEY = 'dsh-
|
|
526
|
+
const LAST_WS_KEY = 'dsh-homing:last-workspace'
|
|
513
527
|
document.addEventListener('click', (event) => {
|
|
514
528
|
const node = event.target
|
|
515
529
|
if (node === null || typeof node.closest !== 'function') return
|
|
@@ -578,7 +592,7 @@ const MOBILE_JS = `(() => {
|
|
|
578
592
|
return
|
|
579
593
|
}
|
|
580
594
|
let hint = ''
|
|
581
|
-
try { hint = currentSessionTitle() || window.localStorage.getItem('dsh-
|
|
595
|
+
try { hint = currentSessionTitle() || window.localStorage.getItem('dsh-homing:last-workspace') || '' } catch { hint = '' }
|
|
582
596
|
window.location.href = '/files' + (hint === '' ? '' : '?hint=' + encodeURIComponent(hint))
|
|
583
597
|
}
|
|
584
598
|
|
|
@@ -707,8 +721,8 @@ const MOBILE_JS = `(() => {
|
|
|
707
721
|
try {
|
|
708
722
|
if (document.visibilityState === 'hidden') return
|
|
709
723
|
const body = JSON.stringify({ at: now, path: location.pathname })
|
|
710
|
-
if (navigator.sendBeacon) navigator.sendBeacon('/
|
|
711
|
-
else fetch('/
|
|
724
|
+
if (navigator.sendBeacon) navigator.sendBeacon('/dsh_homing/alive', body)
|
|
725
|
+
else fetch('/dsh_homing/alive', { method: 'POST', headers: { 'content-type': 'application/json' }, body, keepalive: true }).catch(() => {})
|
|
712
726
|
} catch (error) {
|
|
713
727
|
/* 忽略 */
|
|
714
728
|
}
|
|
@@ -724,11 +738,11 @@ const MOBILE_JS = `(() => {
|
|
|
724
738
|
})();
|
|
725
739
|
`;
|
|
726
740
|
|
|
727
|
-
const INJECTION = '<link rel="stylesheet" href="/
|
|
741
|
+
const INJECTION = '<link rel="stylesheet" href="/dsh_homing/mobile.css"><script defer src="/dsh_homing/mobile.js"></script>';
|
|
728
742
|
|
|
729
743
|
// 紧急开关:带这个 Cookie 时桥接不注入移动端样式,回到 DSH 原生布局。
|
|
730
744
|
// 调试页上一键即可设置,页面坏了也点得到。
|
|
731
|
-
const MOBILE_OFF_COOKIE = 'dsh-
|
|
745
|
+
const MOBILE_OFF_COOKIE = 'dsh-homing-off';
|
|
732
746
|
|
|
733
747
|
const DEBUG_HTML = `<!doctype html>
|
|
734
748
|
<html lang="zh-CN"><head><meta charset="utf-8">
|
|
@@ -763,6 +777,7 @@ const DEBUG_HTML = `<!doctype html>
|
|
|
763
777
|
<body><div class="wrap">
|
|
764
778
|
<h1>调试页</h1>
|
|
765
779
|
<p class="sub">这一页不依赖移动端样式,界面被改坏时也能打开。</p>
|
|
780
|
+
<p class="sub"><a href="/">← 返回 DSH</a> · <a href="/term">终端</a> · <a href="/files">文件</a> · <a href="/git">Git</a></p>
|
|
766
781
|
{{message}}
|
|
767
782
|
|
|
768
783
|
<h2>状态</h2>
|
|
@@ -803,8 +818,8 @@ const DEBUG_HTML = `<!doctype html>
|
|
|
803
818
|
|
|
804
819
|
<h2>快捷入口</h2>
|
|
805
820
|
<div class="card">
|
|
806
|
-
<a class="btn ghost" href="/
|
|
807
|
-
<a class="btn ghost" href="/
|
|
821
|
+
<a class="btn ghost" href="/dsh_homing/account">账户设置</a>
|
|
822
|
+
<a class="btn ghost" href="/dsh_homing/devices">设备管理</a>
|
|
808
823
|
<a class="btn ghost" href="/logout">退出登录</a>
|
|
809
824
|
</div>
|
|
810
825
|
</div>
|
|
@@ -877,7 +892,8 @@ const ACCOUNT_HTML = `<!doctype html>
|
|
|
877
892
|
</style></head>
|
|
878
893
|
<body><div class="wrap">
|
|
879
894
|
<h1>账户设置</h1>
|
|
880
|
-
<p class="sub"><a href="/
|
|
895
|
+
<p class="sub"><a href="/dsh_homing/devices">设备管理(吊销/删除已配对设备)→</a></p>
|
|
896
|
+
<p class="sub"><a href="/">← 返回 DSH</a> · <a href="/term">终端</a> · <a href="/files">文件</a> · <a href="/git">Git</a></p>
|
|
881
897
|
<div class="muted">用户名:{{username}}</div>
|
|
882
898
|
{{message}}
|
|
883
899
|
|
|
@@ -969,7 +985,7 @@ const LOGIN_HTML = `<!doctype html>
|
|
|
969
985
|
<button type="submit">登录</button>
|
|
970
986
|
</form>
|
|
971
987
|
{{message}}
|
|
972
|
-
<div class="hint">登录一次后 3 天内免登录 · <a href="/
|
|
988
|
+
<div class="hint">登录一次后 3 天内免登录 · <a href="/dsh_homing/reset" style="color:#7aa7ff">忘记密码?</a></div>
|
|
973
989
|
</div></body></html>`;
|
|
974
990
|
|
|
975
991
|
const RESET_HTML = `<!doctype html>
|
|
@@ -999,7 +1015,7 @@ const RESET_HTML = `<!doctype html>
|
|
|
999
1015
|
<input type="hidden" name="action" value="request">
|
|
1000
1016
|
<button type="submit">获取重置码</button>
|
|
1001
1017
|
</form>
|
|
1002
|
-
<p style="margin-top:10px">生成后在你电脑上打开:<br><code>$DSH_HOME/integrations/dsh-
|
|
1018
|
+
<p style="margin-top:10px">生成后在你电脑上打开:<br><code>$DSH_HOME/integrations/dsh-homing/reset-code.txt</code><br>(文件里就一行「重置码:XXXX」)</p>
|
|
1003
1019
|
</div>
|
|
1004
1020
|
|
|
1005
1021
|
<div class="card">
|
|
@@ -1024,7 +1040,7 @@ function sendLogin(res, message = '', requireTotp = false) {
|
|
|
1024
1040
|
'cache-control': 'no-store',
|
|
1025
1041
|
// 登录页的安全头:禁止被嵌入(防点击劫持)、禁止外链、限制表单只能提交给自己
|
|
1026
1042
|
'x-frame-options': 'DENY',
|
|
1027
|
-
'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; base-uri 'none'",
|
|
1043
|
+
'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; connect-src 'self'; form-action 'self'; base-uri 'none'; img-src 'self' data:",
|
|
1028
1044
|
'x-content-type-options': 'nosniff',
|
|
1029
1045
|
'referrer-policy': 'no-referrer',
|
|
1030
1046
|
});
|
|
@@ -1174,12 +1190,12 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
1174
1190
|
return Number.isFinite(expiresAt) && expiresAt > Date.now() ? expiresAt : 0;
|
|
1175
1191
|
}
|
|
1176
1192
|
|
|
1177
|
-
/* ---- 逐设备令牌(dsh-device)----------------------------------------
|
|
1193
|
+
/* ---- 逐设备令牌(dsh-homing-device)----------------------------------------
|
|
1178
1194
|
* 入口令牌 ?k= 只用来「配对」一次:配对后发一枚设备令牌 Cookie,
|
|
1179
1195
|
* 之后由设备表放行。设备表在 devices.json,可单独吊销某一台,
|
|
1180
1196
|
* 吊销后下一次请求就失效——丢手机不用改密码、也不用重启。
|
|
1181
1197
|
*/
|
|
1182
|
-
const DEVICE_COOKIE = 'dsh-device';
|
|
1198
|
+
const DEVICE_COOKIE = 'dsh-homing-device';
|
|
1183
1199
|
const DEVICE_SECONDS = 180 * 24 * 60 * 60;
|
|
1184
1200
|
// 设备表缓存按 mtime 失效:手工改 devices.json(比如在电脑上直接吊销)
|
|
1185
1201
|
// 也会在下一个请求立刻生效,不用重启。
|
|
@@ -1263,9 +1279,10 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
1263
1279
|
const touchActivity = createActivityRecorder(dir);
|
|
1264
1280
|
|
|
1265
1281
|
const handler = async (req, res) => {
|
|
1266
|
-
|
|
1282
|
+
|
|
1283
|
+
const url = new URL(req.url ?? '/', 'http://dsh-homing.invalid');
|
|
1267
1284
|
// 任何来自浏览器的请求都算"人在用"(静态资源除外,避免预加载把空闲判断拖长)
|
|
1268
|
-
if (!/^\/(?:
|
|
1285
|
+
if (!/^\/(?:dsh_homing\/alive|assets?\/|favicon)/u.test(url.pathname)) touchActivity('request');
|
|
1269
1286
|
// 写操作来源校验:
|
|
1270
1287
|
// - 代理到 DSH 的请求必须严格校验(代理会改写 Origin/Referer,上游的同源检查形同虚设);
|
|
1271
1288
|
// - 本插件自己的页面(登录/账户/设备)只记录不阻断:它的 Cookie 都是 SameSite=Lax,
|
|
@@ -1274,7 +1291,7 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
1274
1291
|
const originCheck = checkUnsafeOrigin(req);
|
|
1275
1292
|
if (!originCheck.ok) {
|
|
1276
1293
|
const ownPath = url.pathname === '/login' || url.pathname === '/logout'
|
|
1277
|
-
|| url.pathname.startsWith('/
|
|
1294
|
+
|| url.pathname.startsWith('/dsh_homing/')
|
|
1278
1295
|
|| url.pathname === '/files' || url.pathname.startsWith('/files/')
|
|
1279
1296
|
|| url.pathname === '/git' || url.pathname.startsWith('/git/')
|
|
1280
1297
|
|| url.pathname === '/shell' || url.pathname.startsWith('/shell/')
|
|
@@ -1295,18 +1312,18 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
1295
1312
|
}
|
|
1296
1313
|
}
|
|
1297
1314
|
|
|
1298
|
-
if (url.pathname === '/
|
|
1315
|
+
if (url.pathname === '/dsh_homing/mobile.css') {
|
|
1299
1316
|
res.writeHead(200, { 'content-type': 'text/css; charset=utf-8', 'cache-control': 'no-store' });
|
|
1300
1317
|
res.end(MOBILE_CSS);
|
|
1301
1318
|
return;
|
|
1302
1319
|
}
|
|
1303
|
-
if (url.pathname === '/
|
|
1320
|
+
if (url.pathname === '/dsh_homing/mobile.js') {
|
|
1304
1321
|
res.writeHead(200, { 'content-type': 'text/javascript; charset=utf-8', 'cache-control': 'no-store' });
|
|
1305
1322
|
res.end(MOBILE_JS);
|
|
1306
1323
|
return;
|
|
1307
1324
|
}
|
|
1308
1325
|
// 局域网证书(公开,不含任何秘密):手机装上它就能消除"不安全"警告并挡住中间人
|
|
1309
|
-
if ((url.pathname === '/
|
|
1326
|
+
if ((url.pathname === '/dsh_homing/ca.crt' || url.pathname === '/dsh_homing/ca.der') && caCertPath && fs.existsSync(caCertPath)) {
|
|
1310
1327
|
// 鸿蒙 / 部分安卓要求 DER(.cer),iOS 和多数安卓吃 PEM(.crt):两种都给
|
|
1311
1328
|
const wantDer = url.pathname.endsWith('.der');
|
|
1312
1329
|
res.writeHead(200, {
|
|
@@ -1335,14 +1352,14 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
1335
1352
|
|
|
1336
1353
|
// 登录 / 登出 / 账户页 / 重置密码:这些本身就有各自的凭据保护,不再要求入口暗号
|
|
1337
1354
|
const isLoginPath = url.pathname === '/login' || url.pathname === '/logout'
|
|
1338
|
-
|| url.pathname === '/
|
|
1339
|
-
|| url.pathname === '/
|
|
1355
|
+
|| url.pathname === '/dsh_homing/account' || url.pathname === '/dsh_homing/reset'
|
|
1356
|
+
|| url.pathname === '/dsh_homing/devices'
|
|
1340
1357
|
|| url.pathname === '/files' || url.pathname.startsWith('/files/')
|
|
1341
|
-
|| url.pathname === '/debug' || url.pathname === '/
|
|
1342
|
-
|| url.pathname.startsWith('/debug/') || url.pathname.startsWith('/
|
|
1358
|
+
|| url.pathname === '/debug' || url.pathname === '/dsh_homing/debug'
|
|
1359
|
+
|| url.pathname.startsWith('/debug/') || url.pathname.startsWith('/dsh_homing/debug/');
|
|
1343
1360
|
|
|
1344
1361
|
// 重置密码:忘掉密码时用,凭据是「能在运行 DSH 的电脑上读到重置码」
|
|
1345
|
-
if (url.pathname === '/
|
|
1362
|
+
if (url.pathname === '/dsh_homing/reset') {
|
|
1346
1363
|
const ip = clientKey(req);
|
|
1347
1364
|
const resetFile = path.join(dir, 'reset-code.txt');
|
|
1348
1365
|
const issueCode = () => {
|
|
@@ -1408,7 +1425,7 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
1408
1425
|
'content-type': 'text/html; charset=utf-8',
|
|
1409
1426
|
'cache-control': 'no-store',
|
|
1410
1427
|
'x-frame-options': 'DENY',
|
|
1411
|
-
'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; base-uri 'none'",
|
|
1428
|
+
'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; connect-src 'self'; form-action 'self'; base-uri 'none'; img-src 'self' data:",
|
|
1412
1429
|
'x-content-type-options': 'nosniff',
|
|
1413
1430
|
'referrer-policy': 'no-referrer',
|
|
1414
1431
|
});
|
|
@@ -1432,7 +1449,7 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
1432
1449
|
'content-type': 'text/html; charset=utf-8',
|
|
1433
1450
|
'cache-control': 'no-store',
|
|
1434
1451
|
'x-frame-options': 'DENY',
|
|
1435
|
-
'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; base-uri 'none'",
|
|
1452
|
+
'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; connect-src 'self'; form-action 'self'; base-uri 'none'; img-src 'self' data:",
|
|
1436
1453
|
'x-content-type-options': 'nosniff',
|
|
1437
1454
|
'referrer-policy': 'no-referrer',
|
|
1438
1455
|
});
|
|
@@ -1461,8 +1478,58 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
1461
1478
|
}
|
|
1462
1479
|
}
|
|
1463
1480
|
|
|
1481
|
+
// 自检:把"兜底转发"这一段的状态暴露出来,便于定位 404
|
|
1482
|
+
if (url.pathname === '/dsh_homing/selfcheck') {
|
|
1483
|
+
const up = getUpstreamPort();
|
|
1484
|
+
const authority = up ? '127.0.0.1:' + up : null;
|
|
1485
|
+
let cookieName = null;
|
|
1486
|
+
let accepted = null;
|
|
1487
|
+
let upstreamStatus = null;
|
|
1488
|
+
let sample = null;
|
|
1489
|
+
try {
|
|
1490
|
+
const minted = authority ? await mintCookie(authority) : undefined;
|
|
1491
|
+
cookieName = minted ? minted.name : null;
|
|
1492
|
+
if (authority && minted) {
|
|
1493
|
+
accepted = await new Promise((resolve) => {
|
|
1494
|
+
const probe = http.request({
|
|
1495
|
+
host: '127.0.0.1', port: up, path: '/', method: 'GET',
|
|
1496
|
+
headers: { host: authority, cookie: minted.name + '=' + minted.value },
|
|
1497
|
+
timeout: 5000,
|
|
1498
|
+
}, (res) => { upstreamStatus = res.statusCode; res.resume(); res.on('end', () => resolve(res.statusCode === 200)); });
|
|
1499
|
+
probe.on('timeout', () => { probe.destroy(); resolve(false); });
|
|
1500
|
+
probe.on('error', () => resolve(false));
|
|
1501
|
+
probe.end();
|
|
1502
|
+
});
|
|
1503
|
+
}
|
|
1504
|
+
const direct = await new Promise((resolve) => {
|
|
1505
|
+
const req2 = http.request({ host: '127.0.0.1', port: up, path: '/', method: 'GET', headers: { host: authority ?? '' }, timeout: 5000 }, (res) => {
|
|
1506
|
+
const chunks = [];
|
|
1507
|
+
res.on('data', (c) => chunks.push(c));
|
|
1508
|
+
res.on('end', () => resolve({ status: res.statusCode, head: Buffer.concat(chunks).toString('utf8').slice(0, 120) }));
|
|
1509
|
+
});
|
|
1510
|
+
req2.on('timeout', () => { req2.destroy(); resolve({ status: 'timeout' }); });
|
|
1511
|
+
req2.on('error', (e) => resolve({ status: 'error: ' + e.code }));
|
|
1512
|
+
req2.end();
|
|
1513
|
+
});
|
|
1514
|
+
sample = direct;
|
|
1515
|
+
} catch (error) {
|
|
1516
|
+
sample = { error: String(error?.message ?? error) };
|
|
1517
|
+
}
|
|
1518
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
1519
|
+
res.end(JSON.stringify({
|
|
1520
|
+
upstreamPort: up ?? null,
|
|
1521
|
+
authority,
|
|
1522
|
+
cookieName,
|
|
1523
|
+
cookieAccepted: accepted,
|
|
1524
|
+
upstreamRootStatusNoCookie: upstreamStatus,
|
|
1525
|
+
upstreamSample: sample,
|
|
1526
|
+
pluginVersion: PLUGIN_VERSION,
|
|
1527
|
+
}, null, 2));
|
|
1528
|
+
return;
|
|
1529
|
+
}
|
|
1530
|
+
|
|
1464
1531
|
// 活跃度信标:手机端打字/滑动时上报,重启脚本据此避免打断用户
|
|
1465
|
-
if (url.pathname === '/
|
|
1532
|
+
if (url.pathname === '/dsh_homing/alive' && (req.method === 'POST' || req.method === 'GET')) {
|
|
1466
1533
|
touchActivity('beacon');
|
|
1467
1534
|
res.writeHead(204, { 'cache-control': 'no-store' });
|
|
1468
1535
|
res.end();
|
|
@@ -1471,7 +1538,7 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
1471
1538
|
|
|
1472
1539
|
// 真终端:/term(xterm.js + 本机 sshd 的 PTY)。需要登录。
|
|
1473
1540
|
if (url.pathname === '/term' || url.pathname.startsWith('/term/')) {
|
|
1474
|
-
if (!sessionValid(req)) {
|
|
1541
|
+
if (!sessionValid(req) && currentDevice(req) === null) {
|
|
1475
1542
|
res.writeHead(303, { location: '/login', 'cache-control': 'no-store' });
|
|
1476
1543
|
res.end();
|
|
1477
1544
|
return;
|
|
@@ -1648,7 +1715,7 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
1648
1715
|
|
|
1649
1716
|
// 终端:执行任意 shell 命令(手机端的 Git Bash)。需要登录。
|
|
1650
1717
|
if (url.pathname === '/shell' || url.pathname === '/shell/complete' || url.pathname === '/shell/run') {
|
|
1651
|
-
if (!sessionValid(req)) {
|
|
1718
|
+
if (!sessionValid(req) && currentDevice(req) === null) {
|
|
1652
1719
|
res.writeHead(303, { location: '/login', 'cache-control': 'no-store' });
|
|
1653
1720
|
res.end();
|
|
1654
1721
|
return;
|
|
@@ -1799,7 +1866,7 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
1799
1866
|
|
|
1800
1867
|
// git 页:跑任意 git 子命令(不经过 shell),需要登录
|
|
1801
1868
|
if (url.pathname === '/git' || url.pathname === '/git/run') {
|
|
1802
|
-
if (!sessionValid(req)) {
|
|
1869
|
+
if (!sessionValid(req) && currentDevice(req) === null) {
|
|
1803
1870
|
res.writeHead(303, { location: '/login', 'cache-control': 'no-store' });
|
|
1804
1871
|
res.end();
|
|
1805
1872
|
return;
|
|
@@ -1809,7 +1876,7 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
1809
1876
|
'content-type': 'text/html; charset=utf-8',
|
|
1810
1877
|
'cache-control': 'no-store',
|
|
1811
1878
|
'x-frame-options': 'DENY',
|
|
1812
|
-
'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; base-uri 'none'",
|
|
1879
|
+
'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; connect-src 'self'; form-action 'self'; base-uri 'none'; img-src 'self' data:",
|
|
1813
1880
|
'x-content-type-options': 'nosniff',
|
|
1814
1881
|
'referrer-policy': 'no-referrer',
|
|
1815
1882
|
});
|
|
@@ -1860,8 +1927,11 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
1860
1927
|
return;
|
|
1861
1928
|
}
|
|
1862
1929
|
|
|
1863
|
-
if (url.pathname === '/files' || url.pathname === '/files/view' || url.pathname === '/files/run'
|
|
1864
|
-
|
|
1930
|
+
if (url.pathname === '/files' || url.pathname === '/files/view' || url.pathname === '/files/run'
|
|
1931
|
+
|| url.pathname === '/files/ask' || url.pathname === '/files/rename'
|
|
1932
|
+
|| url.pathname === '/files/delete' || url.pathname === '/files/paste'
|
|
1933
|
+
|| url.pathname === '/files/mkdir' || url.pathname === '/files/newfile') {
|
|
1934
|
+
if (!sessionValid(req) && currentDevice(req) === null) {
|
|
1865
1935
|
res.writeHead(303, { location: '/login', 'cache-control': 'no-store' });
|
|
1866
1936
|
res.end();
|
|
1867
1937
|
return;
|
|
@@ -1871,7 +1941,7 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
1871
1941
|
'content-type': 'text/html; charset=utf-8',
|
|
1872
1942
|
'cache-control': 'no-store',
|
|
1873
1943
|
'x-frame-options': 'DENY',
|
|
1874
|
-
'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; base-uri 'none'",
|
|
1944
|
+
'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; connect-src 'self'; form-action 'self'; base-uri 'none'; img-src 'self' data:",
|
|
1875
1945
|
'x-content-type-options': 'nosniff',
|
|
1876
1946
|
'referrer-policy': 'no-referrer',
|
|
1877
1947
|
});
|
|
@@ -1917,6 +1987,86 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
1917
1987
|
return;
|
|
1918
1988
|
}
|
|
1919
1989
|
|
|
1990
|
+
if (url.pathname === '/files/ask' && req.method === 'GET') {
|
|
1991
|
+
const target = url.searchParams.get('path') ?? '';
|
|
1992
|
+
const name = path.basename(target);
|
|
1993
|
+
const dir = path.dirname(target);
|
|
1994
|
+
const esc = (v) => String(v).replace(/[&<>"]/gu, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"' })[c]);
|
|
1995
|
+
sendHtml(`<!doctype html><html lang="zh-CN"><head><meta charset="utf-8">
|
|
1996
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
1997
|
+
<title>操作</title><style>
|
|
1998
|
+
body{background:#12161d;color:#dbe2ea;font:15px/1.5 system-ui;margin:0;padding:16px}
|
|
1999
|
+
h1{font-size:18px;margin:0 0 12px}
|
|
2000
|
+
.card{background:#1a1f27;border:1px solid #2b3340;border-radius:10px;padding:14px;margin-bottom:12px}
|
|
2001
|
+
input{width:100%;box-sizing:border-box;padding:11px;border-radius:8px;border:1px solid #39424f;background:#0f1319;color:#e6edf5;font-size:15px}
|
|
2002
|
+
button{width:100%;margin-top:10px;padding:12px;border-radius:8px;border:0;background:#4f7cff;color:#fff;font-size:15px;font-weight:600}
|
|
2003
|
+
a.btn{display:block;padding:11px;border-radius:8px;background:#2a2f3a;color:#c7cdd8;text-decoration:none;text-align:center;margin-top:8px}
|
|
2004
|
+
.path{color:#8b93a1;font-size:12px;word-break:break-all;margin-bottom:8px}
|
|
2005
|
+
</style></head><body>
|
|
2006
|
+
<h1>⋯ 操作</h1>
|
|
2007
|
+
<div class="card"><div class="path">${esc(target)}</div>
|
|
2008
|
+
<form method="post" action="/files/rename">
|
|
2009
|
+
<input type="hidden" name="path" value="${esc(target)}">
|
|
2010
|
+
<input type="text" name="name" value="${esc(name)}" autofocus>
|
|
2011
|
+
<button type="submit">重命名</button>
|
|
2012
|
+
</form></div>
|
|
2013
|
+
<a class="btn" href="/files?path=${encodeURIComponent(dir)}">返回所在目录</a>
|
|
2014
|
+
<a class="btn" href="/files/view?path=${encodeURIComponent(target)}">查看内容</a>
|
|
2015
|
+
<a class="btn" href="/">返回 DSH</a>
|
|
2016
|
+
</body></html>`);
|
|
2017
|
+
return;
|
|
2018
|
+
}
|
|
2019
|
+
|
|
2020
|
+
if ((url.pathname === '/files/mkdir' || url.pathname === '/files/newfile') && req.method === 'POST') {
|
|
2021
|
+
const form = new URLSearchParams(await readBody(req, 64 * 1024));
|
|
2022
|
+
const targetDir = form.get('dir') ?? '';
|
|
2023
|
+
const name = form.get('name') ?? '';
|
|
2024
|
+
const isDir = url.pathname === '/files/mkdir';
|
|
2025
|
+
const result = filebrowse.createEntry(targetDir, name, isDir);
|
|
2026
|
+
appendAudit(dir, isDir ? 'file-mkdir' : 'file-newfile', { dir: targetDir, name, ok: result.ok, error: result.error ?? null });
|
|
2027
|
+
res.writeHead(303, { location: '/files?path=' + encodeURIComponent(targetDir), 'cache-control': 'no-store' });
|
|
2028
|
+
res.end();
|
|
2029
|
+
return;
|
|
2030
|
+
}
|
|
2031
|
+
|
|
2032
|
+
if (url.pathname === '/files/paste' && req.method === 'POST') {
|
|
2033
|
+
const form = new URLSearchParams(await readBody(req, 64 * 1024));
|
|
2034
|
+
const src = form.get('src') ?? '';
|
|
2035
|
+
const dir = form.get('dir') ?? '';
|
|
2036
|
+
const op = form.get('op') === 'cut' ? 'cut' : 'copy';
|
|
2037
|
+
const result = op === 'cut' ? filebrowse.moveInto(src, dir) : filebrowse.copyInto(src, dir);
|
|
2038
|
+
appendAudit(dir, op === 'cut' ? 'file-move' : 'file-paste', { src, dir, ok: result.ok, target: result.path ?? null, error: result.error ?? null });
|
|
2039
|
+
res.writeHead(303, { location: '/files?path=' + encodeURIComponent(dir), 'cache-control': 'no-store' });
|
|
2040
|
+
res.end();
|
|
2041
|
+
return;
|
|
2042
|
+
}
|
|
2043
|
+
|
|
2044
|
+
if (url.pathname === '/files/delete' && req.method === 'POST') {
|
|
2045
|
+
const form = new URLSearchParams(await readBody(req, 64 * 1024));
|
|
2046
|
+
const target = form.get('path') ?? '';
|
|
2047
|
+
const result = filebrowse.deleteEntry(target);
|
|
2048
|
+
appendAudit(dir, 'file-delete', { target, ok: result.ok, error: result.error ?? null });
|
|
2049
|
+
res.writeHead(303, { location: '/files?path=' + encodeURIComponent(path.dirname(target)), 'cache-control': 'no-store' });
|
|
2050
|
+
res.end();
|
|
2051
|
+
return;
|
|
2052
|
+
}
|
|
2053
|
+
|
|
2054
|
+
if (url.pathname === '/files/rename' && req.method === 'POST') {
|
|
2055
|
+
const form = new URLSearchParams(await readBody(req, 64 * 1024));
|
|
2056
|
+
const target = form.get('path') ?? '';
|
|
2057
|
+
const name = form.get('name') ?? '';
|
|
2058
|
+
const result = filebrowse.renameEntry(target, name);
|
|
2059
|
+
appendAudit(dir, 'file-rename', { target, name, ok: result.ok, error: result.error ?? null });
|
|
2060
|
+
if (!result.ok) {
|
|
2061
|
+
res.writeHead(303, { location: '/files?path=' + encodeURIComponent(path.dirname(target)) + '&msg=' + encodeURIComponent(result.error), 'cache-control': 'no-store' });
|
|
2062
|
+
res.end();
|
|
2063
|
+
return;
|
|
2064
|
+
}
|
|
2065
|
+
res.writeHead(303, { location: '/files?path=' + encodeURIComponent(path.dirname(result.path)), 'cache-control': 'no-store' });
|
|
2066
|
+
res.end();
|
|
2067
|
+
return;
|
|
2068
|
+
}
|
|
2069
|
+
|
|
1920
2070
|
if (url.pathname === '/files/run' && req.method === 'POST') {
|
|
1921
2071
|
const form = new URLSearchParams(await readBody(req, 64 * 1024));
|
|
1922
2072
|
const file = form.get('path') ?? '';
|
|
@@ -1933,16 +2083,16 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
1933
2083
|
}
|
|
1934
2084
|
|
|
1935
2085
|
// 调试页:独立于移动端样式,页面被改坏时也能打开;需要登录。
|
|
1936
|
-
// 两个地址都能用:/debug(好记)和 /
|
|
2086
|
+
// 两个地址都能用:/debug(好记)和 /dsh_homing/debug(兼容旧链接)
|
|
1937
2087
|
const debugPrefix = url.pathname === '/debug' || url.pathname.startsWith('/debug/')
|
|
1938
2088
|
? '/debug'
|
|
1939
|
-
: url.pathname === '/
|
|
1940
|
-
? '/
|
|
2089
|
+
: url.pathname === '/dsh_homing/debug' || url.pathname.startsWith('/dsh_homing/debug/')
|
|
2090
|
+
? '/dsh_homing/debug'
|
|
1941
2091
|
: null;
|
|
1942
2092
|
const debugSub = debugPrefix === null ? null : url.pathname.slice(debugPrefix.length);
|
|
1943
2093
|
|
|
1944
2094
|
if (debugSub === '') {
|
|
1945
|
-
if (!sessionValid(req)) {
|
|
2095
|
+
if (!sessionValid(req) && currentDevice(req) === null) {
|
|
1946
2096
|
res.writeHead(303, { location: '/login', 'cache-control': 'no-store' });
|
|
1947
2097
|
res.end();
|
|
1948
2098
|
return;
|
|
@@ -1999,13 +2149,13 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
1999
2149
|
.replaceAll('{{errors}}', errors.length > 0 ? errors.map((line) => escapeHtml(line)).join('<br>') : '无')
|
|
2000
2150
|
.replaceAll('{{shots}}', shots.length === 0
|
|
2001
2151
|
? ''
|
|
2002
|
-
: `<div class="shots">${shots.map((name) => `<a href="${debugPrefix}/files/${encodeURIComponent(name)}" target="_blank"><img src="/
|
|
2152
|
+
: `<div class="shots">${shots.map((name) => `<a href="${debugPrefix}/files/${encodeURIComponent(name)}" target="_blank"><img src="/dsh_homing/debug/files/${encodeURIComponent(name)}" alt="${escapeHtml(name)}"></a>`).join('')}</div>`));
|
|
2003
2153
|
return;
|
|
2004
2154
|
}
|
|
2005
2155
|
|
|
2006
2156
|
// 上传截图:接收 dataURL,落盘到 debug/shots/
|
|
2007
2157
|
if (debugSub === '/upload' && req.method === 'POST') {
|
|
2008
|
-
if (!sessionValid(req)) {
|
|
2158
|
+
if (!sessionValid(req) && currentDevice(req) === null) {
|
|
2009
2159
|
res.writeHead(401, { 'content-type': 'text/plain; charset=utf-8' });
|
|
2010
2160
|
res.end('未登录');
|
|
2011
2161
|
return;
|
|
@@ -2034,7 +2184,7 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
2034
2184
|
|
|
2035
2185
|
// 查看已上传的截图
|
|
2036
2186
|
if (debugSub !== null && debugSub.startsWith('/files/') && req.method === 'GET') {
|
|
2037
|
-
if (!sessionValid(req)) {
|
|
2187
|
+
if (!sessionValid(req) && currentDevice(req) === null) {
|
|
2038
2188
|
res.writeHead(303, { location: '/login', 'cache-control': 'no-store' });
|
|
2039
2189
|
res.end();
|
|
2040
2190
|
return;
|
|
@@ -2062,7 +2212,7 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
2062
2212
|
|
|
2063
2213
|
// 设备管理:需要登录。它在入口闸门的白名单里,所以即使把所有设备都吊销了,
|
|
2064
2214
|
// 你仍然能在电脑上登录后进来恢复,不会把自己彻底锁死。
|
|
2065
|
-
if (url.pathname === '/
|
|
2215
|
+
if (url.pathname === '/dsh_homing/devices') {
|
|
2066
2216
|
if (!sessionValid(req)) {
|
|
2067
2217
|
res.writeHead(303, { location: '/login', 'cache-control': 'no-store' });
|
|
2068
2218
|
res.end();
|
|
@@ -2082,7 +2232,7 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
2082
2232
|
appendAudit(dir, `device-${action}`, { id, ip: clientKey(req) });
|
|
2083
2233
|
}
|
|
2084
2234
|
res.writeHead(303, {
|
|
2085
|
-
location: `/
|
|
2235
|
+
location: `/dsh_homing/devices?msg=${encodeURIComponent(result.changed ? '已更新。' : '没有变化。')}`,
|
|
2086
2236
|
'cache-control': 'no-store',
|
|
2087
2237
|
});
|
|
2088
2238
|
res.end();
|
|
@@ -2112,7 +2262,7 @@ function createBridge({ getUpstreamPort, mintCookie, auth, config, dir, secureCo
|
|
|
2112
2262
|
'content-type': 'text/html; charset=utf-8',
|
|
2113
2263
|
'cache-control': 'no-store',
|
|
2114
2264
|
'x-frame-options': 'DENY',
|
|
2115
|
-
'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; base-uri 'none'",
|
|
2265
|
+
'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; connect-src 'self'; form-action 'self'; base-uri 'none'; img-src 'self' data:",
|
|
2116
2266
|
'x-content-type-options': 'nosniff',
|
|
2117
2267
|
'referrer-policy': 'no-referrer',
|
|
2118
2268
|
});
|
|
@@ -2133,7 +2283,7 @@ button.danger{background:#3a2226;border-color:#54303a;color:#f0b6bf}
|
|
|
2133
2283
|
a{color:#8ab4f8}
|
|
2134
2284
|
</style>
|
|
2135
2285
|
<h1>设备管理</h1>
|
|
2136
|
-
<p class="sub"><a href="/
|
|
2286
|
+
<p class="sub"><a href="/dsh_homing/account">← 返回账户设置</a></p>
|
|
2137
2287
|
<p class="sub">每台配对过的手机/浏览器各占一条。吊销后它的下一次请求立即失效,不用改密码、不用重启。</p>
|
|
2138
2288
|
${message ? `<div class="msg">${escapeHtml(message)}</div>` : ''}
|
|
2139
2289
|
<table><thead><tr><th>设备</th><th>最后活跃</th><th>配对日期</th><th></th></tr></thead>
|
|
@@ -2144,14 +2294,34 @@ ${revokedRows ? `<h2>已吊销</h2><table><tbody>${revokedRows}</tbody></table>`
|
|
|
2144
2294
|
return;
|
|
2145
2295
|
}
|
|
2146
2296
|
|
|
2147
|
-
// 入口闸门:只认签名令牌或签名 Cookie
|
|
2297
|
+
// 入口闸门:只认签名令牌或签名 Cookie。
|
|
2298
|
+
// 浏览器(Accept 含 text/html)访问未放行路径时,**引导去登录**而不是给死胡同 404:
|
|
2299
|
+
// 登录成功即证明持有凭据,之后由登录分支自动补设备令牌(ensureDeviceCookie),
|
|
2300
|
+
// 用户不需要再去开外部配对链接。非浏览器的探测仍回 404,不暴露"这里有个门"。
|
|
2148
2301
|
if (!isLoginPath && !entryAllowed(req, url)) {
|
|
2302
|
+
try {
|
|
2303
|
+
const jar = readCookies(req.headers.cookie);
|
|
2304
|
+
appendAudit(dir, 'gate-redirect', {
|
|
2305
|
+
path: url.pathname,
|
|
2306
|
+
cookieNames: Object.keys(jar).join(','),
|
|
2307
|
+
hasDevice: Boolean(jar[DEVICE_COOKIE]),
|
|
2308
|
+
hasSession: Boolean(jar[SESSION_COOKIE]),
|
|
2309
|
+
ua: String(req.headers['user-agent'] ?? '').slice(0, 40),
|
|
2310
|
+
});
|
|
2311
|
+
} catch { /* 诊断不影响响应 */ }
|
|
2312
|
+
|
|
2313
|
+
const wantsHtml = String(req.headers.accept ?? '').includes('text/html');
|
|
2314
|
+
if (wantsHtml && (url.pathname === '/' || url.pathname === '')) {
|
|
2315
|
+
res.writeHead(303, { location: '/login', 'cache-control': 'no-store' });
|
|
2316
|
+
res.end();
|
|
2317
|
+
return;
|
|
2318
|
+
}
|
|
2149
2319
|
res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8', 'cache-control': 'no-store' });
|
|
2150
2320
|
res.end('Not Found\n');
|
|
2151
2321
|
return;
|
|
2152
2322
|
}
|
|
2153
2323
|
// 首次带 ?k= 进来:登记一台设备、发设备令牌,再 303 到干净 URL
|
|
2154
|
-
// (令牌不进地址栏/历史;之后靠 dsh-device Cookie 长期放行)
|
|
2324
|
+
// (令牌不进地址栏/历史;之后靠 dsh-homing-device Cookie 长期放行)
|
|
2155
2325
|
if (!isLoginPath && config.entryGate !== false) {
|
|
2156
2326
|
const token = url.searchParams.get('k');
|
|
2157
2327
|
if (token && entryExpiry(token) > 0 && currentDevice(req) === null) {
|
|
@@ -2171,7 +2341,7 @@ ${revokedRows ? `<h2>已吊销</h2><table><tbody>${revokedRows}</tbody></table>`
|
|
|
2171
2341
|
appendAudit(dir, 'device-paired', { id: created.record.id, name: created.record.name });
|
|
2172
2342
|
console.log(`[dsh-homing] 新设备已配对:${created.record.name} (${created.record.id})`);
|
|
2173
2343
|
url.searchParams.delete('k');
|
|
2174
|
-
|
|
2344
|
+
res.writeHead(303, {
|
|
2175
2345
|
location: `${url.pathname}${url.search}`,
|
|
2176
2346
|
'cache-control': 'no-store',
|
|
2177
2347
|
'set-cookie': `${DEVICE_COOKIE}=${created.token}; Path=/; HttpOnly; SameSite=Lax; Max-Age=${DEVICE_SECONDS}${secureCookies ? '; Secure' : ''}`,
|
|
@@ -2298,11 +2468,28 @@ ${revokedRows ? `<h2>已吊销</h2><table><tbody>${revokedRows}</tbody></table>`
|
|
|
2298
2468
|
res.end();
|
|
2299
2469
|
return;
|
|
2300
2470
|
}
|
|
2301
|
-
if (!sessionValid(req)) {
|
|
2471
|
+
if (!sessionValid(req) && currentDevice(req) === null) {
|
|
2302
2472
|
if (req.method === 'GET' && String(req.headers.accept ?? '').includes('text/html')) {
|
|
2303
2473
|
res.writeHead(303, { location: '/login', 'cache-control': 'no-store' });
|
|
2304
2474
|
res.end();
|
|
2305
2475
|
} else {
|
|
2476
|
+
// 诊断:把会话校验失败的细节记进审计,区分"没收到 cookie"与"收到但校验不过"
|
|
2477
|
+
try {
|
|
2478
|
+
const jar = readCookies(req.headers.cookie);
|
|
2479
|
+
const raw = jar[SESSION_COOKIE];
|
|
2480
|
+
const parts = typeof raw === 'string' ? raw.split('.') : [];
|
|
2481
|
+
const boundDevice = parts.length === 4 ? parts[2] : null;
|
|
2482
|
+
const known = boundDevice ? devices().devices.some((item) => item.id === boundDevice) : false;
|
|
2483
|
+
appendAudit(dir, 'session-invalid', {
|
|
2484
|
+
path: url.pathname,
|
|
2485
|
+
hasDeviceCookie: Boolean(jar[DEVICE_COOKIE]),
|
|
2486
|
+
hasSessionCookie: Boolean(raw),
|
|
2487
|
+
sessionShape: parts.length,
|
|
2488
|
+
boundDevice,
|
|
2489
|
+
deviceKnown: known,
|
|
2490
|
+
cookieNames: Object.keys(jar).join(','),
|
|
2491
|
+
});
|
|
2492
|
+
} catch { /* 诊断失败不影响响应 */ }
|
|
2306
2493
|
res.writeHead(401, { 'content-type': 'text/plain; charset=utf-8', 'cache-control': 'no-store' });
|
|
2307
2494
|
res.end('请先登录:/login\n');
|
|
2308
2495
|
}
|
|
@@ -2311,7 +2498,7 @@ ${revokedRows ? `<h2>已吊销</h2><table><tbody>${revokedRows}</tbody></table>`
|
|
|
2311
2498
|
}
|
|
2312
2499
|
|
|
2313
2500
|
// 账户页:改密码 / 开启二次验证 / 看登录记录(必须先登录)
|
|
2314
|
-
if (url.pathname === '/
|
|
2501
|
+
if (url.pathname === '/dsh_homing/account') {
|
|
2315
2502
|
const ip = clientKey(req);
|
|
2316
2503
|
if (!sessionValid(req)) {
|
|
2317
2504
|
res.writeHead(303, { location: '/login', 'cache-control': 'no-store' });
|
|
@@ -2328,7 +2515,7 @@ ${revokedRows ? `<h2>已吊销</h2><table><tbody>${revokedRows}</tbody></table>`
|
|
|
2328
2515
|
{ encoding: 'utf8', mode: 0o600 },
|
|
2329
2516
|
);
|
|
2330
2517
|
const fail = (text) => {
|
|
2331
|
-
res.writeHead(303, { location: `/
|
|
2518
|
+
res.writeHead(303, { location: `/dsh_homing/account?msg=${encodeURIComponent(text)}`, 'cache-control': 'no-store' });
|
|
2332
2519
|
res.end();
|
|
2333
2520
|
};
|
|
2334
2521
|
|
|
@@ -2382,7 +2569,7 @@ ${revokedRows ? `<h2>已吊销</h2><table><tbody>${revokedRows}</tbody></table>`
|
|
|
2382
2569
|
if (action === 'totp-start') {
|
|
2383
2570
|
auth.totpPending = base32Encode(crypto.randomBytes(20));
|
|
2384
2571
|
save();
|
|
2385
|
-
res.writeHead(303, { location: '/
|
|
2572
|
+
res.writeHead(303, { location: '/dsh_homing/account', 'cache-control': 'no-store' });
|
|
2386
2573
|
res.end();
|
|
2387
2574
|
return;
|
|
2388
2575
|
}
|
|
@@ -2392,7 +2579,7 @@ ${revokedRows ? `<h2>已吊销</h2><table><tbody>${revokedRows}</tbody></table>`
|
|
|
2392
2579
|
delete auth.totpPending;
|
|
2393
2580
|
save();
|
|
2394
2581
|
appendAudit(dir, 'totp-cancelled', { ip });
|
|
2395
|
-
res.writeHead(303, { location: '/
|
|
2582
|
+
res.writeHead(303, { location: '/dsh_homing/account?ok=1', 'cache-control': 'no-store' });
|
|
2396
2583
|
res.end();
|
|
2397
2584
|
return;
|
|
2398
2585
|
}
|
|
@@ -2405,7 +2592,7 @@ ${revokedRows ? `<h2>已吊销</h2><table><tbody>${revokedRows}</tbody></table>`
|
|
|
2405
2592
|
delete auth.totpPending;
|
|
2406
2593
|
save();
|
|
2407
2594
|
appendAudit(dir, 'totp-enabled', { ip });
|
|
2408
|
-
res.writeHead(303, { location: '/
|
|
2595
|
+
res.writeHead(303, { location: '/dsh_homing/account?ok=1', 'cache-control': 'no-store' });
|
|
2409
2596
|
res.end();
|
|
2410
2597
|
return;
|
|
2411
2598
|
}
|
|
@@ -2416,7 +2603,7 @@ ${revokedRows ? `<h2>已吊销</h2><table><tbody>${revokedRows}</tbody></table>`
|
|
|
2416
2603
|
delete auth.totpPending;
|
|
2417
2604
|
save();
|
|
2418
2605
|
appendAudit(dir, 'totp-disabled', { ip });
|
|
2419
|
-
res.writeHead(303, { location: '/
|
|
2606
|
+
res.writeHead(303, { location: '/dsh_homing/account?ok=1', 'cache-control': 'no-store' });
|
|
2420
2607
|
res.end();
|
|
2421
2608
|
return;
|
|
2422
2609
|
}
|
|
@@ -2473,7 +2660,7 @@ ${revokedRows ? `<h2>已吊销</h2><table><tbody>${revokedRows}</tbody></table>`
|
|
|
2473
2660
|
'content-type': 'text/html; charset=utf-8',
|
|
2474
2661
|
'cache-control': 'no-store',
|
|
2475
2662
|
'x-frame-options': 'DENY',
|
|
2476
|
-
'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; base-uri 'none'",
|
|
2663
|
+
'content-security-policy': "default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'; connect-src 'self'; form-action 'self'; base-uri 'none'; img-src 'self' data:",
|
|
2477
2664
|
'x-content-type-options': 'nosniff',
|
|
2478
2665
|
'referrer-policy': 'no-referrer',
|
|
2479
2666
|
...(deviceCookies.length > 0 ? { 'set-cookie': deviceCookies } : {}),
|
|
@@ -2583,7 +2770,7 @@ ${revokedRows ? `<h2>已吊销</h2><table><tbody>${revokedRows}</tbody></table>`
|
|
|
2583
2770
|
};
|
|
2584
2771
|
|
|
2585
2772
|
const onUpgrade = async (req, socket, head) => {
|
|
2586
|
-
const url = new URL(req.url ?? '/', 'http://dsh-
|
|
2773
|
+
const url = new URL(req.url ?? '/', 'http://dsh-homing.invalid');
|
|
2587
2774
|
// WebSocket 握手同样校验来源:跨站页面可以发起带 Cookie 的 WS 连接
|
|
2588
2775
|
if (!checkUnsafeOrigin({ method: 'POST', headers: req.headers }).ok) {
|
|
2589
2776
|
socket.end('HTTP/1.1 403 Forbidden\r\ncontent-length: 0\r\n\r\n');
|
|
@@ -3013,7 +3200,7 @@ export function apply(ctx, rawConfig) {
|
|
|
3013
3200
|
// 所以用闭包延迟读取。
|
|
3014
3201
|
const mintCookie = createCookieMinter(ctx.credentials, () => port);
|
|
3015
3202
|
const warn = (message) => {
|
|
3016
|
-
ctx.logger?.('dsh-
|
|
3203
|
+
ctx.logger?.('dsh-homing')?.warn?.(message);
|
|
3017
3204
|
console.warn(`[dsh-homing] ${message}`);
|
|
3018
3205
|
};
|
|
3019
3206
|
if (config.serverHost) {
|
|
@@ -3261,12 +3448,12 @@ export function apply(ctx, rawConfig) {
|
|
|
3261
3448
|
entries.push(`公网 ${publicEntry}`);
|
|
3262
3449
|
entries.push(` 上面这条 ${Math.max(1, config.entryTokenTtlDays)} 天内有效;打开一次会种签名 Cookie,之后书签只存 https://域名/ 就行`);
|
|
3263
3450
|
}
|
|
3264
|
-
entries.push(`设备管理 ${scheme}://localhost:${config.listenPort}/
|
|
3451
|
+
entries.push(`设备管理 ${scheme}://localhost:${config.listenPort}/dsh_homing/devices`);
|
|
3265
3452
|
for (const ip of lanIpv4s()) {
|
|
3266
3453
|
entries.push(`局域网 ${scheme}://${ip}:${config.listenPort}/${query}`);
|
|
3267
3454
|
}
|
|
3268
3455
|
console.log(`[dsh-homing] 已启动(本地 ${scheme} ${config.bindAddress}:${config.listenPort} -> DSH 127.0.0.1:${port},TLS=${tls.mode})`);
|
|
3269
|
-
entries.push(`账户设置 ${scheme}://localhost:${config.listenPort}/
|
|
3456
|
+
entries.push(`账户设置 ${scheme}://localhost:${config.listenPort}/dsh_homing/account`);
|
|
3270
3457
|
for (const entry of entries) console.log(`[dsh-homing] ${entry}`);
|
|
3271
3458
|
try {
|
|
3272
3459
|
fs.writeFileSync(path.join(dir, 'entry.txt'), `# 更新于 ${new Date().toISOString()}\n${entries.join('\n')}\n`, 'utf8');
|