@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
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 一次性把云服务器配成 DSH 手机入口(跨平台:Windows / Linux / macOS 都能跑)。
|
|
4
|
+
*
|
|
5
|
+
* 客户端只需要 Node(DSH 自带),服务器支持 Debian/Ubuntu 与 RHEL/CentOS/Rocky/Alma 系。
|
|
6
|
+
* 幂等:可以重复执行;证书已存在就不重签;nginx -t 失败会自动回滚上一个配置。
|
|
7
|
+
*
|
|
8
|
+
* 用法:
|
|
9
|
+
* node bootstrap-server.mjs --server 1.2.3.4 --domain dsh.example.com
|
|
10
|
+
* node bootstrap-server.mjs --server 1.2.3.4 --domain dsh.example.com --dry-run
|
|
11
|
+
*
|
|
12
|
+
* 可选参数:
|
|
13
|
+
* --user ubuntu SSH 用户(默认 ubuntu;默认用 ssh 登录名)
|
|
14
|
+
* --key ~/.ssh/dsh_tunnel
|
|
15
|
+
* --ssh-port 22
|
|
16
|
+
* --remote-port 8787 隧道在服务器回环上占用的端口
|
|
17
|
+
* --email you@example.com Let's Encrypt 通知邮箱(可省)
|
|
18
|
+
*/
|
|
19
|
+
import fs from 'node:fs';
|
|
20
|
+
import os from 'node:os';
|
|
21
|
+
import path from 'node:path';
|
|
22
|
+
import net from 'node:net';
|
|
23
|
+
import dns from 'node:dns/promises';
|
|
24
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
25
|
+
import { fileURLToPath } from 'node:url';
|
|
26
|
+
|
|
27
|
+
const HERE = path.dirname(fileURLToPath(import.meta.url));
|
|
28
|
+
|
|
29
|
+
/** 解析 --key value / --flag。 */
|
|
30
|
+
function parseArgs(argv) {
|
|
31
|
+
const out = {};
|
|
32
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
33
|
+
const token = argv[i];
|
|
34
|
+
if (!token.startsWith('--')) continue;
|
|
35
|
+
const key = token.slice(2);
|
|
36
|
+
const next = argv[i + 1];
|
|
37
|
+
if (next === undefined || next.startsWith('--')) out[key] = true;
|
|
38
|
+
else {
|
|
39
|
+
out[key] = next;
|
|
40
|
+
i += 1;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const args = parseArgs(process.argv.slice(2));
|
|
47
|
+
const server = args.server;
|
|
48
|
+
const domain = args.domain;
|
|
49
|
+
if (!server || typeof server !== 'string' || !domain || typeof domain !== 'string') {
|
|
50
|
+
console.error('用法:node bootstrap-server.mjs --server <IP> --domain <域名> [--user ubuntu] [--remote-port 8787] [--install-key] [--harden] [--passthrough] [--dry-run]');
|
|
51
|
+
process.exit(2);
|
|
52
|
+
}
|
|
53
|
+
const user = typeof args.user === 'string' ? args.user : 'ubuntu';
|
|
54
|
+
const sshPort = Number(args['ssh-port'] ?? 22);
|
|
55
|
+
const remotePort = Number(args['remote-port'] ?? 8787);
|
|
56
|
+
const keyFile = typeof args.key === 'string' ? args.key : path.join(os.homedir(), '.ssh', 'dsh_tunnel');
|
|
57
|
+
const email = typeof args.email === 'string' ? args.email : '';
|
|
58
|
+
const dryRun = args['dry-run'] === true;
|
|
59
|
+
const installKey = args['install-key'] === true;
|
|
60
|
+
const harden = args.harden === true;
|
|
61
|
+
const passthrough = args.passthrough === true;
|
|
62
|
+
|
|
63
|
+
/** 跨平台找 ssh 可执行文件。 */
|
|
64
|
+
function findSsh() {
|
|
65
|
+
const candidates = process.platform === 'win32'
|
|
66
|
+
? [path.join(process.env.WINDIR ?? 'C:\\Windows', 'System32', 'OpenSSH', 'ssh.exe'), 'ssh.exe', 'ssh']
|
|
67
|
+
: ['/usr/bin/ssh', '/usr/local/bin/ssh', '/opt/homebrew/bin/ssh', 'ssh'];
|
|
68
|
+
for (const candidate of candidates) {
|
|
69
|
+
if (candidate.includes(path.sep) && !fs.existsSync(candidate)) continue;
|
|
70
|
+
return candidate;
|
|
71
|
+
}
|
|
72
|
+
throw new Error('找不到 ssh 命令:Windows 请启用 OpenSSH 客户端,Linux/macOS 一般自带');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const ssh = findSsh();
|
|
76
|
+
|
|
77
|
+
/** 和 ssh 同目录的 ssh-keygen(Windows 上 ssh 是绝对路径,直接取兄弟文件)。 */
|
|
78
|
+
function findKeygen() {
|
|
79
|
+
const dir = path.dirname(ssh);
|
|
80
|
+
const candidates = [
|
|
81
|
+
path.join(dir, process.platform === 'win32' ? 'ssh-keygen.exe' : 'ssh-keygen'),
|
|
82
|
+
'/usr/bin/ssh-keygen',
|
|
83
|
+
'/usr/local/bin/ssh-keygen',
|
|
84
|
+
'ssh-keygen',
|
|
85
|
+
];
|
|
86
|
+
for (const candidate of candidates) {
|
|
87
|
+
if (candidate.includes(path.sep) && !fs.existsSync(candidate)) continue;
|
|
88
|
+
return candidate;
|
|
89
|
+
}
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** 没有密钥就生成一对(ed25519,无口令),返回公钥内容。 */
|
|
94
|
+
function ensureKeypair() {
|
|
95
|
+
const pubFile = `${keyFile}.pub`;
|
|
96
|
+
if (fs.existsSync(keyFile) && fs.existsSync(pubFile)) return fs.readFileSync(pubFile, 'utf8').trim();
|
|
97
|
+
if (fs.existsSync(keyFile) && !fs.existsSync(pubFile)) {
|
|
98
|
+
throw new Error(`有私钥但没有公钥:${pubFile}(用 ssh-keygen -y -f ${keyFile} 导出,或删掉私钥重来)`);
|
|
99
|
+
}
|
|
100
|
+
const keygen = findKeygen();
|
|
101
|
+
if (!keygen) throw new Error('找不到 ssh-keygen,无法生成密钥');
|
|
102
|
+
fs.mkdirSync(path.dirname(keyFile), { recursive: true });
|
|
103
|
+
const result = spawnSync(keygen, ['-t', 'ed25519', '-N', '', '-C', 'dsh-homing', '-f', keyFile], { stdio: 'ignore', windowsHide: true });
|
|
104
|
+
if (result.status !== 0 || !fs.existsSync(pubFile)) throw new Error('ssh-keygen 生成密钥失败');
|
|
105
|
+
return fs.readFileSync(pubFile, 'utf8').trim();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 用密码登录一次,把公钥追加到服务器(等价 ssh-copy-id;密码只由你的终端输入,脚本不读取)。 */
|
|
109
|
+
function installPublicKey(pubKey) {
|
|
110
|
+
return new Promise((resolve) => {
|
|
111
|
+
console.log('');
|
|
112
|
+
console.log(`== 上传公钥到 ${user}@${server}(接下来会提示输入一次密码,脚本不读取也不保存)==`);
|
|
113
|
+
const child = spawn(ssh, [
|
|
114
|
+
'-p', String(sshPort),
|
|
115
|
+
'-o', 'PreferredAuthentications=password',
|
|
116
|
+
'-o', 'PubkeyAuthentication=no',
|
|
117
|
+
'-o', 'StrictHostKeyChecking=accept-new',
|
|
118
|
+
`${user}@${server}`,
|
|
119
|
+
'mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys',
|
|
120
|
+
], { stdio: ['pipe', 'inherit', 'inherit'], windowsHide: true });
|
|
121
|
+
child.stdin.end(`${pubKey}\n`);
|
|
122
|
+
child.on('error', () => resolve(false));
|
|
123
|
+
child.on('close', (code) => resolve(code === 0));
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const sshArgs = ['-i', keyFile, '-p', String(sshPort), '-o', 'IdentitiesOnly=yes', '-o', 'BatchMode=yes', '-o', 'StrictHostKeyChecking=accept-new', `${user}@${server}`];
|
|
128
|
+
|
|
129
|
+
/** 跑一条远程命令,返回 {code, stdout, stderr}。 */
|
|
130
|
+
function runRemote(command, input) {
|
|
131
|
+
return new Promise((resolve) => {
|
|
132
|
+
const child = spawn(ssh, [...sshArgs, command], { windowsHide: true });
|
|
133
|
+
let stdout = '';
|
|
134
|
+
let stderr = '';
|
|
135
|
+
child.stdout.on('data', (chunk) => { stdout += chunk; });
|
|
136
|
+
child.stderr.on('data', (chunk) => { stderr += chunk; });
|
|
137
|
+
child.on('error', (error) => resolve({ code: -1, stdout, stderr: String(error.message) }));
|
|
138
|
+
child.on('close', (code) => resolve({ code, stdout, stderr }));
|
|
139
|
+
if (input !== undefined) child.stdin.end(input);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** TCP 端口是否可达。 */
|
|
144
|
+
function probePort(host, port, timeoutMs = 4000) {
|
|
145
|
+
return new Promise((resolve) => {
|
|
146
|
+
const socket = net.connect({ host, port });
|
|
147
|
+
const done = (ok) => {
|
|
148
|
+
socket.destroy();
|
|
149
|
+
resolve(ok);
|
|
150
|
+
};
|
|
151
|
+
socket.setTimeout(timeoutMs);
|
|
152
|
+
socket.once('connect', () => done(true));
|
|
153
|
+
socket.once('timeout', () => done(false));
|
|
154
|
+
socket.once('error', () => done(false));
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/* ------------------------------------------------------------------ *
|
|
159
|
+
* 预检
|
|
160
|
+
* ------------------------------------------------------------------ */
|
|
161
|
+
|
|
162
|
+
console.log('== 预检 ==');
|
|
163
|
+
if (!fs.existsSync(keyFile) && !installKey) {
|
|
164
|
+
console.error(`找不到私钥:${keyFile}`);
|
|
165
|
+
console.error('要么先 ssh-keygen 生成,要么加 --install-key 让脚本生成并上传到服务器');
|
|
166
|
+
process.exit(1);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const pubKey = ensureKeypair();
|
|
170
|
+
let ping = await runRemote('true');
|
|
171
|
+
if (ping.code !== 0) {
|
|
172
|
+
if (!installKey) {
|
|
173
|
+
console.error(`SSH 连不上 ${user}@${server}:${ping.stderr.trim() || `退出码 ${ping.code}`}`);
|
|
174
|
+
console.error('如果这把公钥还没被服务器授权,加 --install-key 重新执行(会提示输入一次密码)');
|
|
175
|
+
process.exit(1);
|
|
176
|
+
}
|
|
177
|
+
if (dryRun) {
|
|
178
|
+
console.log(' SSH 连接 未授权(DryRun 不上传;正式跑会提示输入一次密码)');
|
|
179
|
+
} else {
|
|
180
|
+
const uploaded = await installPublicKey(pubKey);
|
|
181
|
+
ping = uploaded ? await runRemote('true') : { code: -1, stderr: 'password auth failed' };
|
|
182
|
+
if (ping.code !== 0) {
|
|
183
|
+
console.error('公钥上传后仍无法免密登录,请手动执行:');
|
|
184
|
+
console.error(` type "${keyFile}.pub" | ssh ${user}@${server} "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"`);
|
|
185
|
+
process.exit(1);
|
|
186
|
+
}
|
|
187
|
+
console.log(' 公钥已上传并验证');
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (ping.code === 0) {
|
|
192
|
+
console.log(' SSH 连接 OK');
|
|
193
|
+
const sudo = await runRemote('sudo -n true');
|
|
194
|
+
if (sudo.code !== 0) {
|
|
195
|
+
console.error(`${user} 没有免密 sudo,无法自动配置服务器`);
|
|
196
|
+
process.exit(1);
|
|
197
|
+
}
|
|
198
|
+
console.log(' 免密 sudo OK');
|
|
199
|
+
} else {
|
|
200
|
+
console.log(' 免密 sudo 跳过(需要先完成 SSH 授权)');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
let a = [];
|
|
204
|
+
let aaaa = [];
|
|
205
|
+
try { a = await dns.resolve4(domain); } catch { /* 没有 A 记录 */ }
|
|
206
|
+
try { aaaa = await dns.resolve6(domain); } catch { /* 没有 AAAA 记录 */ }
|
|
207
|
+
console.log(` DNS A=${a.join(',') || '-'} AAAA=${aaaa.join(',') || '-'}`);
|
|
208
|
+
if (!a.includes(server)) {
|
|
209
|
+
console.warn(` ⚠ ${domain} 的 A 记录里没有 ${server},证书申请大概率失败(先去加解析)`);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const port80 = await probePort(server, 80);
|
|
213
|
+
console.log(` 80 端口 ${port80 ? '开放' : '不可达'}`);
|
|
214
|
+
if (!port80) {
|
|
215
|
+
console.warn(' ⚠ 80 不通:云安全组没放行,或 nginx 还没起(首次可忽略,签证书前必须通)');
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/* ------------------------------------------------------------------ *
|
|
219
|
+
* 渲染配置
|
|
220
|
+
* ------------------------------------------------------------------ */
|
|
221
|
+
|
|
222
|
+
const siteTemplate = fs.readFileSync(path.join(HERE, 'nginx-dsh.template.conf'), 'utf8');
|
|
223
|
+
const siteConf = siteTemplate.replaceAll('__DOMAIN__', domain).replaceAll('__REMOTE_PORT__', String(remotePort));
|
|
224
|
+
const gateConf = fs.readFileSync(path.join(HERE, 'nginx-gate.conf'), 'utf8');
|
|
225
|
+
const wsMapConf = fs.readFileSync(path.join(HERE, 'nginx-ws-map.conf'), 'utf8');
|
|
226
|
+
const certbotMail = email ? `-m ${email}` : '--register-unsafely-without-email';
|
|
227
|
+
|
|
228
|
+
if (dryRun) {
|
|
229
|
+
console.log('');
|
|
230
|
+
console.log('DryRun:将在这台服务器上执行');
|
|
231
|
+
console.log(' 1) 按发行版安装 nginx / certbot(apt 或 dnf/yum)');
|
|
232
|
+
console.log(' 2) 自动识别模式:已有 stream.d 配置 → 保持 TLS 透传,否则由 nginx 终止 TLS');
|
|
233
|
+
console.log(' 3) 透传模式:80 只做 ACME + stream 透传 443(不在服务器申请/保存证书私钥)');
|
|
234
|
+
console.log(` 非透传模式:certbot --nginx -d ${domain},443 → https://127.0.0.1:${remotePort}`);
|
|
235
|
+
console.log(' 4) nginx -t 校验,失败保留原配置');
|
|
236
|
+
if (harden) console.log(' 6) 收紧 SSH:禁用密码登录/root 密码登录,安装并启用 fail2ban');
|
|
237
|
+
if (!fs.existsSync(keyFile)) console.log(' 0) 先生成 SSH 密钥对,再用密码上传公钥(--install-key)');
|
|
238
|
+
process.exit(0);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/* ------------------------------------------------------------------ *
|
|
242
|
+
* 远程执行
|
|
243
|
+
* ------------------------------------------------------------------ */
|
|
244
|
+
|
|
245
|
+
const remoteScript = `set -e
|
|
246
|
+
DOMAIN='${domain}'
|
|
247
|
+
RP='${remotePort}'
|
|
248
|
+
|
|
249
|
+
if command -v apt-get >/dev/null 2>&1; then
|
|
250
|
+
echo '==> install nginx / certbot (apt)'
|
|
251
|
+
export DEBIAN_FRONTEND=noninteractive
|
|
252
|
+
sudo apt-get update -qq
|
|
253
|
+
sudo apt-get install -y -qq nginx certbot python3-certbot-nginx >/dev/null
|
|
254
|
+
elif command -v dnf >/dev/null 2>&1; then
|
|
255
|
+
echo '==> install nginx / certbot (dnf)'
|
|
256
|
+
sudo dnf install -y -q nginx certbot python3-certbot-nginx >/dev/null
|
|
257
|
+
elif command -v yum >/dev/null 2>&1; then
|
|
258
|
+
echo '==> install nginx / certbot (yum)'
|
|
259
|
+
sudo yum install -y -q nginx certbot python3-certbot-nginx >/dev/null
|
|
260
|
+
else
|
|
261
|
+
echo 'UNSUPPORTED: no apt-get / dnf / yum'
|
|
262
|
+
exit 2
|
|
263
|
+
fi
|
|
264
|
+
|
|
265
|
+
# 站点目录:Debian 系有 sites-available/sites-enabled,RHEL 系直接用 conf.d
|
|
266
|
+
if [ -d /etc/nginx/sites-available ]; then
|
|
267
|
+
SITE=/etc/nginx/sites-available/dsh
|
|
268
|
+
ENABLED=/etc/nginx/sites-enabled/dsh
|
|
269
|
+
DEFAULT=/etc/nginx/sites-enabled/default
|
|
270
|
+
else
|
|
271
|
+
SITE=/etc/nginx/conf.d/dsh.conf
|
|
272
|
+
ENABLED=
|
|
273
|
+
DEFAULT=/etc/nginx/conf.d/default.conf
|
|
274
|
+
fi
|
|
275
|
+
|
|
276
|
+
MODE='${passthrough ? 'passthrough' : 'auto'}'
|
|
277
|
+
if [ "$MODE" = "auto" ] && sudo test -f /etc/nginx/stream.d/dsh.conf; then
|
|
278
|
+
MODE=passthrough
|
|
279
|
+
echo '==> 检测到已有透传配置,保持 TLS 在本机终止'
|
|
280
|
+
fi
|
|
281
|
+
|
|
282
|
+
if [ "$MODE" = "passthrough" ]; then
|
|
283
|
+
echo '==> 启用 nginx stream 模块'
|
|
284
|
+
if command -v apt-get >/dev/null 2>&1; then
|
|
285
|
+
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq libnginx-mod-stream >/dev/null
|
|
286
|
+
fi
|
|
287
|
+
sudo mkdir -p /etc/nginx/stream.d
|
|
288
|
+
sudo tee /etc/nginx/stream.d/dsh.conf >/dev/null <<'STREAM'
|
|
289
|
+
# DSH 手机入口:TLS 透传(nginx 只按 SNI 转发,不解密)
|
|
290
|
+
limit_conn_zone $binary_remote_addr zone=dsh_conn:10m;
|
|
291
|
+
|
|
292
|
+
map $ssl_preread_server_name $dsh_backend {
|
|
293
|
+
${domain} 127.0.0.1:${remotePort};
|
|
294
|
+
default 127.0.0.2:9; # 未知 SNI(含空 SNI):丢黑洞,不回任何东西
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
server {
|
|
298
|
+
listen 443;
|
|
299
|
+
listen [::]:443;
|
|
300
|
+
proxy_pass $dsh_backend;
|
|
301
|
+
ssl_preread on;
|
|
302
|
+
limit_conn dsh_conn 50;
|
|
303
|
+
proxy_timeout 3600s;
|
|
304
|
+
proxy_connect_timeout 5s;
|
|
305
|
+
}
|
|
306
|
+
STREAM
|
|
307
|
+
if ! sudo grep -q 'include /etc/nginx/stream.d' /etc/nginx/nginx.conf; then
|
|
308
|
+
sudo cp -f /etc/nginx/nginx.conf "/root/nginx.conf.bak.$(date +%s)"
|
|
309
|
+
printf '\nstream {\n include /etc/nginx/stream.d/*.conf;\n}\n' | sudo tee -a /etc/nginx/nginx.conf >/dev/null
|
|
310
|
+
fi
|
|
311
|
+
echo '==> 写站点配置(80 只做 ACME 校验)'
|
|
312
|
+
sudo mkdir -p /var/www/html
|
|
313
|
+
sudo tee "$SITE" >/dev/null <<'ACME_SITE'
|
|
314
|
+
# DSH 手机入口:443 是 TLS 透传(见 /etc/nginx/stream.d/dsh.conf),80 只服务 ACME
|
|
315
|
+
server {
|
|
316
|
+
listen 80 default_server;
|
|
317
|
+
listen [::]:80 default_server;
|
|
318
|
+
server_name ${domain};
|
|
319
|
+
|
|
320
|
+
location /.well-known/acme-challenge/ { root /var/www/html; }
|
|
321
|
+
location / { return 444; }
|
|
322
|
+
}
|
|
323
|
+
ACME_SITE
|
|
324
|
+
[ -n "$ENABLED" ] && sudo ln -sf "$SITE" "$ENABLED"
|
|
325
|
+
sudo rm -f "$DEFAULT"
|
|
326
|
+
sudo nginx -t
|
|
327
|
+
sudo systemctl enable nginx >/dev/null 2>&1 || true
|
|
328
|
+
sudo systemctl restart nginx
|
|
329
|
+
echo '==> 证书在本机(用 bin/renew-cert.mjs 续期),服务器不申请也不保存私钥'
|
|
330
|
+
else
|
|
331
|
+
echo '==> write func gate map'
|
|
332
|
+
sudo mkdir -p /var/www/html
|
|
333
|
+
sudo tee /etc/nginx/conf.d/dsh-gate.conf >/dev/null <<'GATE'
|
|
334
|
+
${gateConf}
|
|
335
|
+
GATE
|
|
336
|
+
sudo tee /etc/nginx/conf.d/dsh-ws.conf >/dev/null <<'WSMAP'
|
|
337
|
+
${wsMapConf}
|
|
338
|
+
WSMAP
|
|
339
|
+
|
|
340
|
+
if ! sudo test -d "/etc/letsencrypt/live/${domain}"; then
|
|
341
|
+
echo '==> request certificate (HTTP-01)'
|
|
342
|
+
sudo mkdir -p "$(dirname "$SITE")"
|
|
343
|
+
sudo tee "$SITE" >/dev/null <<ACME
|
|
344
|
+
server { listen 80; listen [::]:80; server_name ${domain}; location /.well-known/acme-challenge/ { root /var/www/html; } location / { return 444; } }
|
|
345
|
+
ACME
|
|
346
|
+
[ -n "$ENABLED" ] && sudo ln -sf "$SITE" "$ENABLED"
|
|
347
|
+
sudo rm -f "$DEFAULT"
|
|
348
|
+
sudo nginx -t
|
|
349
|
+
sudo systemctl enable nginx >/dev/null 2>&1 || true
|
|
350
|
+
sudo systemctl restart nginx
|
|
351
|
+
sudo certbot --nginx -d "${domain}" --non-interactive --agree-tos ${certbotMail} --redirect >/dev/null
|
|
352
|
+
else
|
|
353
|
+
echo '==> certificate exists, skip'
|
|
354
|
+
fi
|
|
355
|
+
|
|
356
|
+
echo '==> write site config'
|
|
357
|
+
sudo cp -f "$SITE" "/root/dsh.conf.bak.$(date +%s)" 2>/dev/null || true
|
|
358
|
+
sudo tee "$SITE" >/dev/null <<'SITE_CONF'
|
|
359
|
+
${siteConf}
|
|
360
|
+
SITE_CONF
|
|
361
|
+
[ -n "$ENABLED" ] && sudo ln -sf "$SITE" "$ENABLED"
|
|
362
|
+
sudo rm -f "$DEFAULT"
|
|
363
|
+
if ! sudo nginx -t >/tmp/nginx-test.log 2>&1; then
|
|
364
|
+
echo 'CONFIG-INVALID'
|
|
365
|
+
cat /tmp/nginx-test.log
|
|
366
|
+
LATEST=$(ls -t /root/dsh.conf.bak.* 2>/dev/null | head -1)
|
|
367
|
+
[ -n "$LATEST" ] && sudo cp -f "$LATEST" "$SITE" && echo 'rolled back to previous config'
|
|
368
|
+
exit 1
|
|
369
|
+
fi
|
|
370
|
+
sudo systemctl reload nginx
|
|
371
|
+
sudo systemctl enable nginx >/dev/null 2>&1 || true
|
|
372
|
+
fi
|
|
373
|
+
|
|
374
|
+
if [ "${harden ? '1' : '0'}" = "1" ]; then
|
|
375
|
+
echo '==> harden ssh (key only, no root password)'
|
|
376
|
+
BK="/root/sshd-backup-$(date +%Y%m%d-%H%M%S)"
|
|
377
|
+
sudo mkdir -p "$BK"
|
|
378
|
+
sudo cp -a /etc/ssh/sshd_config "$BK/" 2>/dev/null || true
|
|
379
|
+
sudo tee /etc/ssh/sshd_config.d/00-dsh-homing.conf >/dev/null <<'SSHD'
|
|
380
|
+
# 由 dsh-homing bootstrap 写入:只允许公钥登录
|
|
381
|
+
PasswordAuthentication no
|
|
382
|
+
KbdInteractiveAuthentication no
|
|
383
|
+
PubkeyAuthentication yes
|
|
384
|
+
PermitRootLogin prohibit-password
|
|
385
|
+
MaxAuthTries 4
|
|
386
|
+
SSHD
|
|
387
|
+
if sudo sshd -t; then
|
|
388
|
+
sudo systemctl reload ssh 2>/dev/null || sudo systemctl reload sshd 2>/dev/null || true
|
|
389
|
+
echo "sshd hardened (backup: $BK)"
|
|
390
|
+
else
|
|
391
|
+
echo 'SSHD-INVALID, rolling back'
|
|
392
|
+
sudo rm -f /etc/ssh/sshd_config.d/00-dsh-homing.conf
|
|
393
|
+
fi
|
|
394
|
+
# nginx 崩��时不把内存 dump 到磁盘(里面是解密后的明文)
|
|
395
|
+
sudo mkdir -p /etc/systemd/system/nginx.service.d
|
|
396
|
+
printf '[Service]\nLimitCORE=0\n' | sudo tee /etc/systemd/system/nginx.service.d/no-core.conf >/dev/null
|
|
397
|
+
sudo systemctl daemon-reload
|
|
398
|
+
sudo systemctl restart nginx 2>/dev/null || true
|
|
399
|
+
if command -v apt-get >/dev/null 2>&1; then
|
|
400
|
+
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq fail2ban >/dev/null 2>&1 || true
|
|
401
|
+
sudo tee /etc/fail2ban/jail.d/dsh-sshd.local >/dev/null <<'JAIL'
|
|
402
|
+
[DEFAULT]
|
|
403
|
+
bantime = 1h
|
|
404
|
+
findtime = 10m
|
|
405
|
+
maxretry = 5
|
|
406
|
+
|
|
407
|
+
[sshd]
|
|
408
|
+
enabled = true
|
|
409
|
+
backend = auto
|
|
410
|
+
JAIL
|
|
411
|
+
sudo systemctl enable fail2ban >/dev/null 2>&1 || true
|
|
412
|
+
sudo systemctl restart fail2ban 2>/dev/null || true
|
|
413
|
+
echo "fail2ban=$(systemctl is-active fail2ban 2>/dev/null || echo unknown)"
|
|
414
|
+
fi
|
|
415
|
+
fi
|
|
416
|
+
|
|
417
|
+
echo '==> result'
|
|
418
|
+
echo "nginx=$(systemctl is-active nginx)"
|
|
419
|
+
echo "proxy=$(sudo grep -hoE '127\\.0\\.0\\.1:[0-9]+' "$SITE" /etc/nginx/stream.d/dsh.conf 2>/dev/null | cut -d: -f2 | sort -u | paste -sd, -)"
|
|
420
|
+
echo "mode=$(sudo test -f /etc/nginx/stream.d/dsh.conf && echo passthrough || echo http)"
|
|
421
|
+
`;
|
|
422
|
+
|
|
423
|
+
console.log('');
|
|
424
|
+
console.log('== 开始配置服务器 ==');
|
|
425
|
+
const child = spawn(ssh, [...sshArgs, 'bash -s'], { stdio: ['pipe', 'inherit', 'inherit'], windowsHide: true });
|
|
426
|
+
child.stdin.end(remoteScript);
|
|
427
|
+
const code = await new Promise((resolve) => child.on('close', resolve));
|
|
428
|
+
if (code !== 0) {
|
|
429
|
+
console.error(`服务器配置失败(退出码 ${code})`);
|
|
430
|
+
process.exit(code || 1);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
console.log('');
|
|
434
|
+
console.log('== 完成 ==');
|
|
435
|
+
console.log(` 手机入口:https://${domain}/?func=dsh-view`);
|
|
436
|
+
console.log(` 提醒:本机插件 settings.json 的 remotePort 必须是 ${remotePort}(不一致时插件会自动对齐)`);
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 生成一条新的入口链接(?k=)。
|
|
4
|
+
*
|
|
5
|
+
* 用法:
|
|
6
|
+
* node bin/entry-link.mjs 打印新链接
|
|
7
|
+
* node bin/entry-link.mjs --days 7 指定有效期(默认读 settings.json 的 entryTokenTtlDays)
|
|
8
|
+
*
|
|
9
|
+
* 说明:链接里的令牌是 HMAC 签名 + 过期时间,服务端只认签名,没有共享口令。
|
|
10
|
+
* 打开一次后会换成 dsh-entry 签名 Cookie,之后书签只存 https://域名/ 即可。
|
|
11
|
+
* 想立刻作废所有已发出的链接和 Cookie:改 settings.json 里的 auth.json sessionKey
|
|
12
|
+
* (或用 /dsh_homing/account 的重置功能重新生成),旧的全部失效。
|
|
13
|
+
*/
|
|
14
|
+
import fs from 'node:fs'
|
|
15
|
+
import os from 'node:os'
|
|
16
|
+
import path from 'node:path'
|
|
17
|
+
import crypto from 'node:crypto'
|
|
18
|
+
|
|
19
|
+
const DIR = path.join(process.env.DSH_HOME || path.join(os.homedir(), '.dsh'), 'integrations', 'dsh-homing')
|
|
20
|
+
const SETTINGS = path.join(DIR, 'settings.json')
|
|
21
|
+
const AUTH = path.join(DIR, 'auth.json')
|
|
22
|
+
|
|
23
|
+
const readJson = (file) => {
|
|
24
|
+
try {
|
|
25
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'))
|
|
26
|
+
} catch (error) {
|
|
27
|
+
console.error(`读取失败 ${file}: ${error.message}`)
|
|
28
|
+
process.exit(2)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const settings = readJson(SETTINGS)
|
|
33
|
+
const auth = readJson(AUTH)
|
|
34
|
+
|
|
35
|
+
const publicUrl = String(settings.publicUrl || '')
|
|
36
|
+
if (!publicUrl) {
|
|
37
|
+
console.error('settings.json 里没有 publicUrl,无法生成链接')
|
|
38
|
+
process.exit(2)
|
|
39
|
+
}
|
|
40
|
+
if (typeof auth.sessionKey !== 'string' || auth.sessionKey.length === 0) {
|
|
41
|
+
console.error('auth.json 里没有 sessionKey,插件可能还没初始化过')
|
|
42
|
+
process.exit(2)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const daysArgIndex = process.argv.indexOf('--days')
|
|
46
|
+
const days = daysArgIndex > -1
|
|
47
|
+
? Number(process.argv[daysArgIndex + 1])
|
|
48
|
+
: Number(settings.entryTokenTtlDays ?? 30)
|
|
49
|
+
if (!Number.isFinite(days) || days <= 0) {
|
|
50
|
+
console.error('--days 需要一个正数')
|
|
51
|
+
process.exit(2)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const b64url = (buf) => Buffer.from(buf).toString('base64')
|
|
55
|
+
.replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '')
|
|
56
|
+
|
|
57
|
+
const expiresAt = Date.now() + days * 86_400_000
|
|
58
|
+
const body = b64url(String(expiresAt))
|
|
59
|
+
const key = Buffer.from(auth.sessionKey, 'base64')
|
|
60
|
+
const mac = b64url(crypto.createHmac('sha256', key).update(`entry:${body}`).digest())
|
|
61
|
+
|
|
62
|
+
const url = new URL(publicUrl)
|
|
63
|
+
url.searchParams.delete('func')
|
|
64
|
+
url.searchParams.set('k', `${body}.${mac}`)
|
|
65
|
+
|
|
66
|
+
console.log(`到期:${new Date(expiresAt).toISOString()}(${days} 天)`)
|
|
67
|
+
console.log(url.toString())
|
|
68
|
+
console.log('')
|
|
69
|
+
console.log('打开一次后会自动换成签名 Cookie,之后书签只存 https://域名/ 就行。')
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# DSH 手机入口 —— nginx 站点配置
|
|
2
|
+
# 结构:公网 80/443 -> nginx(TLS) -> 127.0.0.1:8787(ssh -R 反向隧道)-> 电脑上的 bridge -> DSH
|
|
3
|
+
# 访问规则:
|
|
4
|
+
# * 只允许 HTTPS;80 端口除了 Let's Encrypt 校验之外一律直接断开连接
|
|
5
|
+
# * 必须带 ?func=dsh-view(或已有 dsh-view=1 Cookie)才放行,否则 403
|
|
6
|
+
# 改完:sudo nginx -t && sudo systemctl reload nginx
|
|
7
|
+
|
|
8
|
+
server {
|
|
9
|
+
listen 80 default_server;
|
|
10
|
+
listen [::]:80 default_server;
|
|
11
|
+
server_name __DOMAIN__;
|
|
12
|
+
|
|
13
|
+
# 只保留证书自动续期用的校验路径,其余 HTTP 请求直接断开(不回任何内容,避免泄露口令)
|
|
14
|
+
location /.well-known/acme-challenge/ {
|
|
15
|
+
root /var/www/html;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
location / {
|
|
19
|
+
return 444;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
server {
|
|
24
|
+
listen 443 ssl;
|
|
25
|
+
listen [::]:443 ssl ipv6only=on;
|
|
26
|
+
server_name __DOMAIN__;
|
|
27
|
+
|
|
28
|
+
ssl_certificate /etc/letsencrypt/live/__DOMAIN__/fullchain.pem;
|
|
29
|
+
ssl_certificate_key /etc/letsencrypt/live/__DOMAIN__/privkey.pem;
|
|
30
|
+
include /etc/letsencrypt/options-ssl-nginx.conf;
|
|
31
|
+
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
|
|
32
|
+
|
|
33
|
+
# 不把响应写进磁盘临时文件:明文只留在内存
|
|
34
|
+
proxy_max_temp_file_size 0;
|
|
35
|
+
|
|
36
|
+
# 大文件上传(DSH 附件)
|
|
37
|
+
client_max_body_size 300m;
|
|
38
|
+
|
|
39
|
+
# 闸门:没有 func=dsh-view 也没有 Cookie 的请求直接 403
|
|
40
|
+
if ($dsh_gate = 0) {
|
|
41
|
+
return 403;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
# 带对了参数就种一个 Cookie,后续静态资源和 API 靠它放行
|
|
45
|
+
add_header Set-Cookie "dshview=1; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age=31536000" always;
|
|
46
|
+
add_header Strict-Transport-Security "max-age=31536000" always;
|
|
47
|
+
|
|
48
|
+
# 带 hash 的静态资源:缓存一年,手机第二次打开几乎不再下载
|
|
49
|
+
location /assets/ {
|
|
50
|
+
proxy_pass https://127.0.0.1:__REMOTE_PORT__;
|
|
51
|
+
proxy_ssl_verify off;
|
|
52
|
+
proxy_http_version 1.1;
|
|
53
|
+
proxy_set_header Host $host;
|
|
54
|
+
proxy_set_header Upgrade $http_upgrade;
|
|
55
|
+
proxy_set_header Connection $connection_upgrade;
|
|
56
|
+
add_header Cache-Control "public, max-age=31536000, immutable" always;
|
|
57
|
+
add_header Strict-Transport-Security "max-age=31536000" always;
|
|
58
|
+
proxy_read_timeout 300s;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
location /plugins/ {
|
|
62
|
+
proxy_pass https://127.0.0.1:__REMOTE_PORT__;
|
|
63
|
+
proxy_ssl_verify off;
|
|
64
|
+
proxy_http_version 1.1;
|
|
65
|
+
proxy_set_header Host $host;
|
|
66
|
+
proxy_set_header Upgrade $http_upgrade;
|
|
67
|
+
proxy_set_header Connection $connection_upgrade;
|
|
68
|
+
add_header Cache-Control "public, max-age=604800" always;
|
|
69
|
+
add_header Strict-Transport-Security "max-age=31536000" always;
|
|
70
|
+
proxy_read_timeout 300s;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
location / {
|
|
74
|
+
proxy_pass https://127.0.0.1:__REMOTE_PORT__;
|
|
75
|
+
proxy_ssl_verify off;
|
|
76
|
+
proxy_http_version 1.1;
|
|
77
|
+
|
|
78
|
+
# WebSocket(/api/remote.mux)
|
|
79
|
+
proxy_set_header Upgrade $http_upgrade;
|
|
80
|
+
proxy_set_header Connection $connection_upgrade;
|
|
81
|
+
|
|
82
|
+
proxy_set_header Host $host;
|
|
83
|
+
proxy_set_header X-Real-IP $remote_addr;
|
|
84
|
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
85
|
+
proxy_set_header X-Forwarded-Proto $scheme;
|
|
86
|
+
|
|
87
|
+
# 长连接不掐断(SSE / WebSocket / 长任务)
|
|
88
|
+
proxy_read_timeout 3600s;
|
|
89
|
+
proxy_send_timeout 3600s;
|
|
90
|
+
}
|
|
91
|
+
}
|