@andy.ian/dsh-homing 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +71 -2
- package/bin/auth.mjs +159 -0
- package/bin/backup.mjs +118 -0
- package/bin/bootstrap-server.mjs +436 -0
- package/bin/entry-link.mjs +69 -0
- package/bin/nginx-dsh.template.conf +91 -0
- package/bin/nginx-gate.conf +9 -0
- package/bin/nginx-ws-map.conf +5 -0
- package/bin/renew-cert.mjs +127 -0
- package/bin/supervisor.mjs +488 -0
- package/bin/watchdog.mjs +163 -0
- package/client.js +79 -0
- package/cordis.patch.yml +30 -0
- package/icon.svg +9 -0
- package/index.js +3526 -0
- package/lib/csrf.mjs +60 -0
- package/lib/devices.mjs +221 -0
- package/lib/files-page.mjs +592 -0
- package/lib/files.mjs +551 -0
- package/lib/git.mjs +154 -0
- package/lib/limits.mjs +135 -0
- package/lib/pty.mjs +361 -0
- package/lib/session.mjs +216 -0
- package/lib/shell-page.mjs +430 -0
- package/lib/shell.mjs +274 -0
- package/lib/term-app.js +318 -0
- package/lib/term-page.mjs +117 -0
- package/lib/vendor/fonts/JetBrainsMono-Regular.ttf +0 -0
- package/lib/vendor/fonts/jetbrains-mono-400.woff2 +0 -0
- package/lib/vendor/fonts/jetbrains-mono-700.woff2 +0 -0
- package/lib/vendor/node_modules/@xterm/addon-fit/LICENSE +19 -0
- package/lib/vendor/node_modules/@xterm/addon-fit/README.md +24 -0
- package/lib/vendor/node_modules/@xterm/addon-fit/lib/addon-fit.js +2 -0
- package/lib/vendor/node_modules/@xterm/addon-fit/lib/addon-fit.js.map +1 -0
- package/lib/vendor/node_modules/@xterm/addon-fit/package.json +26 -0
- package/lib/vendor/node_modules/@xterm/addon-fit/src/FitAddon.ts +90 -0
- package/lib/vendor/node_modules/@xterm/addon-fit/typings/addon-fit.d.ts +55 -0
- package/lib/vendor/node_modules/asn1/Jenkinsfile +65 -0
- package/lib/vendor/node_modules/asn1/LICENSE +19 -0
- package/lib/vendor/node_modules/asn1/README.md +50 -0
- package/lib/vendor/node_modules/asn1/lib/ber/errors.js +13 -0
- package/lib/vendor/node_modules/asn1/lib/ber/index.js +27 -0
- package/lib/vendor/node_modules/asn1/lib/ber/reader.js +262 -0
- package/lib/vendor/node_modules/asn1/lib/ber/types.js +36 -0
- package/lib/vendor/node_modules/asn1/lib/ber/writer.js +317 -0
- package/lib/vendor/node_modules/asn1/lib/index.js +20 -0
- package/lib/vendor/node_modules/asn1/package.json +31 -0
- package/lib/vendor/node_modules/bcrypt-pbkdf/CONTRIBUTING.md +13 -0
- package/lib/vendor/node_modules/bcrypt-pbkdf/LICENSE +66 -0
- package/lib/vendor/node_modules/bcrypt-pbkdf/README.md +45 -0
- package/lib/vendor/node_modules/bcrypt-pbkdf/index.js +556 -0
- package/lib/vendor/node_modules/bcrypt-pbkdf/package.json +15 -0
- package/lib/vendor/node_modules/safer-buffer/LICENSE +21 -0
- package/lib/vendor/node_modules/safer-buffer/Porting-Buffer.md +268 -0
- package/lib/vendor/node_modules/safer-buffer/Readme.md +156 -0
- package/lib/vendor/node_modules/safer-buffer/dangerous.js +58 -0
- package/lib/vendor/node_modules/safer-buffer/package.json +34 -0
- package/lib/vendor/node_modules/safer-buffer/safer.js +77 -0
- package/lib/vendor/node_modules/safer-buffer/tests.js +406 -0
- package/lib/vendor/node_modules/ssh2/.eslintignore +4 -0
- package/lib/vendor/node_modules/ssh2/.eslintrc.js +5 -0
- package/lib/vendor/node_modules/ssh2/.github/workflows/ci.yml +110 -0
- package/lib/vendor/node_modules/ssh2/.github/workflows/lint.yml +27 -0
- package/lib/vendor/node_modules/ssh2/LICENSE +19 -0
- package/lib/vendor/node_modules/ssh2/README.md +1529 -0
- package/lib/vendor/node_modules/ssh2/SFTP.md +413 -0
- package/lib/vendor/node_modules/ssh2/examples/server-chat.js +238 -0
- package/lib/vendor/node_modules/ssh2/examples/sftp-server-download-only.js +134 -0
- package/lib/vendor/node_modules/ssh2/install.js +27 -0
- package/lib/vendor/node_modules/ssh2/lib/Channel.js +295 -0
- package/lib/vendor/node_modules/ssh2/lib/agent.js +1123 -0
- package/lib/vendor/node_modules/ssh2/lib/client.js +2176 -0
- package/lib/vendor/node_modules/ssh2/lib/http-agents.js +84 -0
- package/lib/vendor/node_modules/ssh2/lib/index.js +44 -0
- package/lib/vendor/node_modules/ssh2/lib/keygen.js +582 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/Protocol.js +2136 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/SFTP.js +4052 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/constants.js +356 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/crypto/binding.gyp +23 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/crypto/poly1305.js +43 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/crypto/src/binding.cc +2300 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/crypto.js +1602 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/handlers.js +16 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/handlers.misc.js +1285 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/kex.js +1908 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/keyParser.js +1484 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/node-fs-compat.js +115 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/utils.js +356 -0
- package/lib/vendor/node_modules/ssh2/lib/protocol/zlib.js +255 -0
- package/lib/vendor/node_modules/ssh2/lib/server.js +1380 -0
- package/lib/vendor/node_modules/ssh2/lib/utils.js +336 -0
- package/lib/vendor/node_modules/ssh2/package.json +49 -0
- package/lib/vendor/node_modules/ssh2/test/common.js +316 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/bad_rsa_private_key +26 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/https_cert.pem +33 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/https_key.pem +52 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/id_dsa +12 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/id_ecdsa +5 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/id_rsa +15 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/id_rsa.ppk +26 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/id_rsa_enc +30 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa +21 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc +22 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm +23 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_dsa_enc_gcm.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa +9 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc +10 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc.pub.result +8 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm +10 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm.pub.result +8 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ecdsa_enc_gcm.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519 +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_ed25519.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa +27 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc +28 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc.pub.result +8 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm +29 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm.pub.result +8 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_new_rsa_enc_gcm.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa +12 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc +15 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc.pub.result +8 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_dsa_enc.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa +5 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc +8 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc.pub.result +8 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_ecdsa_enc.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa +27 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc +30 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc.pub.result +8 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256 +54 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256.pub +1 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256.pub.result +6 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/openssh_old_rsa_enc_aes256.result +6 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_dsa_enc +17 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_dsa_enc.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa +26 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa_enc +18 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/ppk_rsa_enc.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa.pub +9 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa2.pub +10 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa2.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa3.pub +11 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa3.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa4.pub +11 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa4.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa5.pub +8 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa5.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa6.pub +13 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/keyParser/rfc4716_rsa6.pub.result +7 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/openssh_new_rsa +27 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/ssh_host_dsa_key +12 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/ssh_host_ecdsa_key +5 -0
- package/lib/vendor/node_modules/ssh2/test/fixtures/ssh_host_rsa_key +15 -0
- package/lib/vendor/node_modules/ssh2/test/test-exec.js +578 -0
- package/lib/vendor/node_modules/ssh2/test/test-integration-openssh.js +486 -0
- package/lib/vendor/node_modules/ssh2/test/test-keygen.js +60 -0
- package/lib/vendor/node_modules/ssh2/test/test-misc-client-server.js +1460 -0
- package/lib/vendor/node_modules/ssh2/test/test-openssh.js +261 -0
- package/lib/vendor/node_modules/ssh2/test/test-protocol-crypto.js +631 -0
- package/lib/vendor/node_modules/ssh2/test/test-protocol-keyparser.js +177 -0
- package/lib/vendor/node_modules/ssh2/test/test-server-hostkeys.js +138 -0
- package/lib/vendor/node_modules/ssh2/test/test-sftp.js +842 -0
- package/lib/vendor/node_modules/ssh2/test/test-shell.js +109 -0
- package/lib/vendor/node_modules/ssh2/test/test-userauth-agent-openssh.js +110 -0
- package/lib/vendor/node_modules/ssh2/test/test-userauth-agent.js +171 -0
- package/lib/vendor/node_modules/ssh2/test/test-userauth.js +611 -0
- package/lib/vendor/node_modules/ssh2/test/test-worker-imports.js +25 -0
- package/lib/vendor/node_modules/ssh2/test/test.js +20 -0
- package/lib/vendor/node_modules/ssh2/util/build_pagent.bat +2 -0
- package/lib/vendor/node_modules/ssh2/util/pagent.c +88 -0
- package/lib/vendor/node_modules/ssh2/util/pagent.exe +0 -0
- package/lib/vendor/node_modules/tweetnacl/AUTHORS.md +27 -0
- package/lib/vendor/node_modules/tweetnacl/CHANGELOG.md +283 -0
- package/lib/vendor/node_modules/tweetnacl/LICENSE +24 -0
- package/lib/vendor/node_modules/tweetnacl/PULL_REQUEST_TEMPLATE.md +20 -0
- package/lib/vendor/node_modules/tweetnacl/README.md +494 -0
- package/lib/vendor/node_modules/tweetnacl/nacl-fast.js +2391 -0
- package/lib/vendor/node_modules/tweetnacl/nacl-fast.min.js +1 -0
- package/lib/vendor/node_modules/tweetnacl/nacl.d.ts +98 -0
- package/lib/vendor/node_modules/tweetnacl/nacl.js +1178 -0
- package/lib/vendor/node_modules/tweetnacl/nacl.min.js +1 -0
- package/lib/vendor/node_modules/tweetnacl/package.json +56 -0
- package/lib/vendor/xterm.css +218 -0
- package/lib/vendor/xterm.js +2 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/ops/watchdog/dsh-watchdog.ps1 +105 -0
- package/ops/watchdog/restart-hidden.vbs +7 -0
- package/ops/watchdog/restart-now.ps1 +261 -0
- package/ops/watchdog/run-hidden.vbs +4 -0
- package/package.json +71 -4
package/lib/csrf.mjs
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 写操作的来源校验(CSRF)。
|
|
3
|
+
*
|
|
4
|
+
* 背景:这个反向代理会把 Origin/Referer 改写成上游地址,所以 DSH 自己的
|
|
5
|
+
* 同源校验在这条路径上是「永远通过」的——挡不住跨站伪造请求。
|
|
6
|
+
* 因此在改写之前先自己判一次来源。
|
|
7
|
+
*
|
|
8
|
+
* 判据(按浏览器实际会发的头,任一命中即拒):
|
|
9
|
+
* 1. 有 Origin → 必须与本次请求的 Host 同源(scheme 允许 http/https,因为
|
|
10
|
+
* 局域网是 https,而隧道内部是 http);
|
|
11
|
+
* 2. 有 Sec-Fetch-Site → 只能是 same-origin / none;
|
|
12
|
+
* 3. 两者都没有(curl、脚本、老浏览器)→ 放行:此时浏览器不会自动带
|
|
13
|
+
* 跨站 Cookie(SameSite=Lax 已挡住跨站 POST),再拦只会误伤工具。
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const UNSAFE_METHODS = new Set(['POST', 'PUT', 'PATCH', 'DELETE'])
|
|
17
|
+
|
|
18
|
+
export function isUnsafeMethod(method) {
|
|
19
|
+
return UNSAFE_METHODS.has(String(method ?? '').toUpperCase())
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** 从 URL 里取 host:port,失败返回空串。 */
|
|
23
|
+
function hostOf(value) {
|
|
24
|
+
try {
|
|
25
|
+
return new URL(value).host.toLowerCase()
|
|
26
|
+
} catch {
|
|
27
|
+
return ''
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {object} req - Node 的 IncomingMessage
|
|
33
|
+
* @returns {{ ok: boolean, reason?: string }}
|
|
34
|
+
*/
|
|
35
|
+
export function checkUnsafeOrigin(req) {
|
|
36
|
+
const method = String(req?.method ?? '').toUpperCase()
|
|
37
|
+
if (!UNSAFE_METHODS.has(method)) return { ok: true }
|
|
38
|
+
|
|
39
|
+
const hostHeader = typeof req.headers?.host === 'string' ? req.headers.host.toLowerCase() : ''
|
|
40
|
+
|
|
41
|
+
const origin = req.headers?.origin
|
|
42
|
+
if (typeof origin === 'string' && origin !== '') {
|
|
43
|
+
if (origin === 'null') return { ok: false, reason: 'origin-null' }
|
|
44
|
+
const originHost = hostOf(origin)
|
|
45
|
+
// 只比 host,不比 scheme:局域网可能是 https,隧道内部是 http
|
|
46
|
+
if (originHost === '' || hostHeader === '' || originHost !== hostHeader) {
|
|
47
|
+
return { ok: false, reason: `origin-mismatch:${originHost || 'invalid'}!=${hostHeader || 'none'}` }
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const site = req.headers?.['sec-fetch-site']
|
|
52
|
+
if (typeof site === 'string' && site !== '') {
|
|
53
|
+
const value = site.toLowerCase()
|
|
54
|
+
if (value !== 'same-origin' && value !== 'none') {
|
|
55
|
+
return { ok: false, reason: `sec-fetch-site:${value}` }
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return { ok: true }
|
|
60
|
+
}
|
package/lib/devices.mjs
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 逐设备令牌:每个手机/浏览器一条记录,可单独吊销。
|
|
3
|
+
*
|
|
4
|
+
* 设计要点:
|
|
5
|
+
* - 令牌格式 `<deviceId>.<secret>`,secret 是 32 字节随机数;
|
|
6
|
+
* - 磁盘上**只存** secret 的 SHA-256,devices.json 泄漏也换不来访问权;
|
|
7
|
+
* - 校验每次都查表,所以「吊销」立即生效,不需要等 Cookie 过期;
|
|
8
|
+
* - 吊销只删记录,连带删掉该设备的会话(由调用方决定是否清 Cookie)。
|
|
9
|
+
*
|
|
10
|
+
* 这个模块不依赖 DSH,也不读环境变量,方便单测。
|
|
11
|
+
*/
|
|
12
|
+
import fs from 'node:fs'
|
|
13
|
+
import path from 'node:path'
|
|
14
|
+
import crypto from 'node:crypto'
|
|
15
|
+
|
|
16
|
+
const FILE = 'devices.json'
|
|
17
|
+
|
|
18
|
+
/** 记录的最后活跃时间超过这个天数就不再参与「活跃设备」展示(但不自动吊销)。 */
|
|
19
|
+
export const STALE_DAYS = 180
|
|
20
|
+
|
|
21
|
+
const b64url = (buf) => Buffer.from(buf).toString('base64')
|
|
22
|
+
.replaceAll('+', '-').replaceAll('/', '_').replaceAll(/=+$/gu, '')
|
|
23
|
+
|
|
24
|
+
const sha256 = (text) => b64url(crypto.createHash('sha256').update(text).digest())
|
|
25
|
+
|
|
26
|
+
/** 空注册表。 */
|
|
27
|
+
export function emptyState() {
|
|
28
|
+
return { version: 1, devices: [] }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** 把任意输入收敛成一个结构合法的注册表(坏数据不抛错,直接丢弃)。 */
|
|
32
|
+
export function normalizeState(raw) {
|
|
33
|
+
const devices = []
|
|
34
|
+
const seen = new Set()
|
|
35
|
+
const list = Array.isArray(raw?.devices) ? raw.devices : []
|
|
36
|
+
for (const item of list) {
|
|
37
|
+
if (!item || typeof item !== 'object') continue
|
|
38
|
+
const id = typeof item.id === 'string' ? item.id.trim() : ''
|
|
39
|
+
const keyHash = typeof item.keyHash === 'string' ? item.keyHash.trim() : ''
|
|
40
|
+
if (!/^dev_[a-f0-9]{8,16}$/u.test(id) || keyHash === '' || seen.has(id)) continue
|
|
41
|
+
seen.add(id)
|
|
42
|
+
devices.push({
|
|
43
|
+
id,
|
|
44
|
+
keyHash,
|
|
45
|
+
name: typeof item.name === 'string' && item.name !== '' ? item.name.slice(0, 64) : '未命名设备',
|
|
46
|
+
createdAt: typeof item.createdAt === 'string' ? item.createdAt : new Date(0).toISOString(),
|
|
47
|
+
lastSeenAt: typeof item.lastSeenAt === 'string' ? item.lastSeenAt : null,
|
|
48
|
+
lastIp: typeof item.lastIp === 'string' ? item.lastIp.slice(0, 64) : null,
|
|
49
|
+
revokedAt: typeof item.revokedAt === 'string' ? item.revokedAt : null,
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
return { version: 1, devices }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function devicesFile(dir) {
|
|
56
|
+
return path.join(dir, FILE)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function loadState(dir) {
|
|
60
|
+
try {
|
|
61
|
+
return normalizeState(JSON.parse(fs.readFileSync(devicesFile(dir), 'utf8')))
|
|
62
|
+
} catch {
|
|
63
|
+
return emptyState()
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function saveState(dir, state) {
|
|
68
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
69
|
+
const next = normalizeState(state)
|
|
70
|
+
const body = `${JSON.stringify(next, null, 2)}\n`
|
|
71
|
+
const tmp = `${devicesFile(dir)}.tmp`
|
|
72
|
+
fs.writeFileSync(tmp, body, { encoding: 'utf8', mode: 0o600 })
|
|
73
|
+
fs.renameSync(tmp, devicesFile(dir))
|
|
74
|
+
return next
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** 从 User-Agent 猜一个人类可读的设备名,纯展示用。 */
|
|
78
|
+
export function guessName(userAgent) {
|
|
79
|
+
const ua = String(userAgent ?? '').trim();
|
|
80
|
+
if (ua === '') return '未知设备';
|
|
81
|
+
|
|
82
|
+
const platform = detectPlatform(ua);
|
|
83
|
+
const browser = detectBrowser(ua, platform);
|
|
84
|
+
if (browser === '') return platform;
|
|
85
|
+
return `${platform} · ${browser}`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 平台识别顺序很重要:
|
|
90
|
+
* 鸿蒙的 UA 里同时有 OpenHarmony 和 Android,必须先判鸿蒙;
|
|
91
|
+
* iPadOS 13+ 把自己伪装成 Macintosh,但会带 Mobile/触屏特征,这里用 iPad 关键字兜住常见情况。
|
|
92
|
+
*/
|
|
93
|
+
function detectPlatform(ua) {
|
|
94
|
+
if (/iPhone|iPod/u.test(ua)) return 'iPhone';
|
|
95
|
+
if (/iPad/u.test(ua)) return 'iPad';
|
|
96
|
+
if (/OpenHarmony|HarmonyOS|ArkWeb/u.test(ua)) return '鸿蒙';
|
|
97
|
+
if (/Android/u.test(ua)) return detectAndroidBrand(ua) ?? 'Android';
|
|
98
|
+
if (/CrOS/u.test(ua)) return 'ChromeOS';
|
|
99
|
+
if (/Macintosh|Mac OS X/u.test(ua)) return 'Mac';
|
|
100
|
+
if (/Windows/u.test(ua)) return 'Windows';
|
|
101
|
+
if (/Linux|X11/u.test(ua)) return 'Linux';
|
|
102
|
+
return '未知设备';
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** 从 Android UA 的品牌段里取厂商名(不猜具体型号,避免误判)。 */
|
|
106
|
+
function detectAndroidBrand(ua) {
|
|
107
|
+
const brands = [
|
|
108
|
+
[/HUAWEI|HONOR/u, '华为'],
|
|
109
|
+
[/Xiaomi|Redmi|POCO/u, '小米'],
|
|
110
|
+
[/OPPO|realme|OnePlus/u, 'OPPO'],
|
|
111
|
+
[/vivo|iQOO/u, 'vivo'],
|
|
112
|
+
[/samsung|SM-/iu, '三星'],
|
|
113
|
+
[/Pixel/u, 'Pixel'],
|
|
114
|
+
[/Motorola|moto /u, 'Moto'],
|
|
115
|
+
];
|
|
116
|
+
for (const [pattern, name] of brands) if (pattern.test(ua)) return `${name}手机`;
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* 浏览器识别:先认内置 WebView(微信/QQ/钉钉等),再认具体浏览器。
|
|
122
|
+
* 鸿蒙的系统浏览器会带 Chrome/xxx 字样,但那不是 Chrome,标成「浏览器」更诚实。
|
|
123
|
+
*/
|
|
124
|
+
function detectBrowser(ua, platform) {
|
|
125
|
+
const inApp = [
|
|
126
|
+
[/MicroMessenger/u, '微信'],
|
|
127
|
+
[/QQ\/|MQQBrowser/u, 'QQ'],
|
|
128
|
+
[/DingTalk/u, '钉钉'],
|
|
129
|
+
[/AlipayClient|AliApp/u, '支付宝'],
|
|
130
|
+
[/weibo/iu, '微博'],
|
|
131
|
+
[/baiduboxapp|BaiduHD/u, '百度'],
|
|
132
|
+
[/Quark/u, '夸克'],
|
|
133
|
+
[/UCBrowser|UCWEB/u, 'UC'],
|
|
134
|
+
[/HuaweiBrowser/u, '华为浏览器'],
|
|
135
|
+
[/HeyTapBrowser/u, 'OPPO 浏览器'],
|
|
136
|
+
[/VivoBrowser/u, 'vivo 浏览器'],
|
|
137
|
+
[/MiuiBrowser|XiaoMi\//u, '小米浏览器'],
|
|
138
|
+
];
|
|
139
|
+
for (const [pattern, name] of inApp) if (pattern.test(ua)) return name;
|
|
140
|
+
|
|
141
|
+
if (/SamsungBrowser/u.test(ua)) return '三星浏览器';
|
|
142
|
+
if (/EdgiOS|EdgA?\//u.test(ua)) return 'Edge';
|
|
143
|
+
if (/OPR\/|Opera/u.test(ua)) return 'Opera';
|
|
144
|
+
if (/FxiOS|Firefox\//u.test(ua)) return 'Firefox';
|
|
145
|
+
if (/CriOS/u.test(ua)) return 'Chrome';
|
|
146
|
+
if (/HeadlessChrome/u.test(ua)) return '无头浏览器';
|
|
147
|
+
if (/Chrome\//u.test(ua)) return platform === '鸿蒙' ? '浏览器' : 'Chrome';
|
|
148
|
+
if (/Safari\//u.test(ua)) return 'Safari';
|
|
149
|
+
return '';
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** 新建一台设备,返回明文令牌(只此一次)与记录。 */
|
|
153
|
+
export function createDevice(state, { name, userAgent, ip } = {}) {
|
|
154
|
+
const secret = b64url(crypto.randomBytes(32))
|
|
155
|
+
const id = `dev_${crypto.randomBytes(8).toString('hex')}`
|
|
156
|
+
const now = new Date().toISOString()
|
|
157
|
+
const record = {
|
|
158
|
+
id,
|
|
159
|
+
keyHash: sha256(secret),
|
|
160
|
+
name: (typeof name === 'string' && name.trim() !== '' ? name.trim() : guessName(userAgent)).slice(0, 64),
|
|
161
|
+
createdAt: now,
|
|
162
|
+
lastSeenAt: now,
|
|
163
|
+
lastIp: typeof ip === 'string' ? ip.slice(0, 64) : null,
|
|
164
|
+
revokedAt: null,
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
token: `${id}.${secret}`,
|
|
168
|
+
record,
|
|
169
|
+
state: { version: 1, devices: [...state.devices, record] },
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** 校验令牌,命中则返回设备记录(并顺手更新 lastSeen,由调用方决定是否落盘)。 */
|
|
174
|
+
export function verifyToken(state, token, { touch = true, ip } = {}) {
|
|
175
|
+
if (typeof token !== 'string') return null
|
|
176
|
+
const dot = token.indexOf('.')
|
|
177
|
+
if (dot <= 0) return null
|
|
178
|
+
const id = token.slice(0, dot)
|
|
179
|
+
const secret = token.slice(dot + 1)
|
|
180
|
+
if (!/^dev_[a-f0-9]{8,16}$/u.test(id) || secret.length < 20) return null
|
|
181
|
+
const device = state.devices.find((item) => item.id === id)
|
|
182
|
+
if (!device || device.revokedAt !== null) return null
|
|
183
|
+
const expected = Buffer.from(device.keyHash)
|
|
184
|
+
const actual = Buffer.from(sha256(secret))
|
|
185
|
+
if (expected.length !== actual.length || !crypto.timingSafeEqual(expected, actual)) return null
|
|
186
|
+
if (touch) {
|
|
187
|
+
device.lastSeenAt = new Date().toISOString()
|
|
188
|
+
if (typeof ip === 'string') device.lastIp = ip.slice(0, 64)
|
|
189
|
+
}
|
|
190
|
+
return device
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** 吊销一台设备(保留记录,便于在列表里显示「已吊销」)。 */
|
|
194
|
+
export function revokeDevice(state, id) {
|
|
195
|
+
const device = state.devices.find((item) => item.id === id)
|
|
196
|
+
if (!device) return { changed: false, state }
|
|
197
|
+
if (device.revokedAt === null) device.revokedAt = new Date().toISOString()
|
|
198
|
+
return { changed: true, state }
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** 彻底删除一条记录。 */
|
|
202
|
+
export function removeDevice(state, id) {
|
|
203
|
+
const devices = state.devices.filter((item) => item.id !== id)
|
|
204
|
+
return { changed: devices.length !== state.devices.length, state: { version: 1, devices } }
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** 改名。 */
|
|
208
|
+
export function renameDevice(state, id, name) {
|
|
209
|
+
const device = state.devices.find((item) => item.id === id)
|
|
210
|
+
const next = typeof name === 'string' ? name.trim().slice(0, 64) : ''
|
|
211
|
+
if (!device || next === '') return { changed: false, state }
|
|
212
|
+
device.name = next
|
|
213
|
+
return { changed: true, state }
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** 活跃设备(未吊销)与已吊销设备的拆分,供页面渲染。 */
|
|
217
|
+
export function partition(state) {
|
|
218
|
+
const active = state.devices.filter((item) => item.revokedAt === null)
|
|
219
|
+
const revoked = state.devices.filter((item) => item.revokedAt !== null)
|
|
220
|
+
return { active, revoked }
|
|
221
|
+
}
|