diff --git a/.env.example b/.env.example index 8448414..8bce778 100644 --- a/.env.example +++ b/.env.example @@ -1,29 +1,26 @@ -# ============================================================================= -# DSH 部署环境配置模板 —— 复制为 .env 后按需填写,切勿提交 .env 到 git。 -# cp .env.example .env -# ============================================================================= +# Copy this file to .env, edit it, and never commit .env. -# ---- 浏览器加密访问 ---- -# 公网访问 IP 或域名(写入 dsh-web 的 --trusted-host,用于 /api 浏览器信任栅栏) +# Public IP address or DNS hostname accepted by dsh-web. TRUSTED_HOST=your-server-ip-or-domain -# nginx Basic Auth 用户名(浏览器打开时的登录账号) +# Browser-facing Nginx Basic Auth credentials. BASIC_AUTH_USER=dsh - -# nginx Basic Auth 密码(明文,install.sh 会用 openssl 生成 htpasswd 哈希) BASIC_AUTH_PASSWORD=change-me-strong-password -# ---- 部署参数(可选,有默认值)---- -# DSH npm 包版本(⚠️ 建议锁定具体版本,不要用 latest:caret 范围会连带升级子包,可能造成插件不兼容) -DSH_VERSION= -# web-ui 全家桶版本(@linxin666/dsh-web-ui-all) -WEB_UI_ALL_VERSION= -# 监听端口(nginx 反向代理目标) +# Exact package versions only; latest and version ranges are rejected. +DSH_VERSION=0.1.0-rc.8 + +# Optional aggregate plugin. Leave empty for an official-core-only profile. +# Version 0.1.12 is the release tested on DSH RC.8. +WEB_UI_ALL_VERSION=0.1.12 + DSH_PORT=3080 -# 安装用户(systemd 服务运行用户) DSH_USER=ubuntu +DEPLOY_DIR=/opt/dsh-deploy + +# Usually blank. setup-letsencrypt.sh persists trusted paths in DEPLOY_DIR/tls.env. +SSL_CERT_PATH= +SSL_KEY_PATH= -# ---- 模型 / API Key 配置 ---- -# 说明:本仓库不预设任何模型 provider 或密钥。 -# 部署完成后,进入 DSH 的 Web UI 设置页配置你自己的模型与 API Key。 -# 如需通过环境变量注入模型配置,请参考 templates/settings.yaml.tpl 的结构自行添加。 +# Add optional *_API_KEY entries here. The installer writes them to +# ~/.dsh/.credentials.yaml with YAML-safe quoted values. diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dd1cdec --- /dev/null +++ b/.gitattributes @@ -0,0 +1,6 @@ +* text=auto +*.sh text eol=lf +*.yml text eol=lf +*.yaml text eol=lf +*.tpl text eol=lf +*.md text eol=lf diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..aee0228 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,18 @@ +name: CI + +on: + push: + pull_request: + +permissions: + contents: read + +jobs: + static-smoke: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: '22' + - run: bash tests/static-smoke.sh diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..a842c01 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,22 @@ +# Changelog + +## 0.2.0-rc.8 — 2026-08-20 + +- Upgrade the tested DSH baseline from `0.1.0-rc.7` to `0.1.0-rc.8`. +- Keep the official global DSH installation clean; remove the realtime source patch from the default deployment and upgrade paths. +- Install `@linxin666/dsh-web-ui-all` through `dsh plugin` as an optional, pinned profile dependency. +- Add full core/profile/plugin/runtime verification with `scripts/verify.sh`. +- Make updates exact-version-only, stop the service during package replacement, validate the complete DSH dependency tree, and roll back automatically. +- Preserve existing settings, credentials, and profile manifests on installer reruns. +- Fix the fresh-clone repository-root resolution bug. +- Fix deployment-directory creation ordering. +- Fix the auto-restart unit's script path and permission model. +- Use Node.js 22 for RC.8 and plugin compatibility. +- Disable `remote-web-ui` by default behind Nginx Basic Auth. +- Use bcrypt for Basic Auth and strip its `Authorization` header before proxying to DSH. +- Add static CI and document the live RC.8 audit. +- Preserve Let's Encrypt certificate paths across installer reruns. + +## 0.1.0-rc.7 — 2026-08-19 + +- Initial published deployment template. diff --git a/README.md b/README.md index 244e44a..9f820f5 100644 --- a/README.md +++ b/README.md @@ -1,70 +1,133 @@ -# DSH 部署(web-ui 版) +# DSH Cloud Server Deploy — clean-core RC.8 + +Deploy [DeepSeek Harness (DSH)](https://github.com/deepseek-ai/deepseek-harness) on an Ubuntu/Debian cloud server with a deliberately small boundary: + +- official `@deepseek-ai/dsh` core, pinned to an exact version; +- the official `dsh-base` + `dsh-web-app` profile bundles; +- optional packages installed only through `dsh plugin`; +- Nginx TLS, Basic Auth, WebSocket/SSE proxying, and systemd supervision. + +The installer does **not** patch files inside the global DSH package. RC.8 was tested on the reference EC2 instance with the official core restored byte-for-byte; the service and installed plugins remained healthy. + +## Architecture + +```text +Internet + │ HTTPS + Basic Auth + ▼ +Nginx :443 + │ loopback proxy + ▼ +Official DSH web profile :3080 + ├── @deepseek-ai/dsh-base (core) + ├── @deepseek-ai/dsh-web-app (core) + └── profile packages (plugins) +``` -DeepSeek Harness (DSH) 在云服务器(AWS EC2 等)上的**自用部署模板**:官方 `@deepseek-ai/dsh` 核心 + web-ui 全家桶(`@linxin666/dsh-web-ui-all`,视为本体核心组件)+ HTTPS 公网访问。定位是**不含非必要插件的精简参考版本**,供有需要的人参考复用。 +`@linxin666/dsh-web-ui-all` is enabled by default in `.env.example`, but it is installed through the official plugin command and remains a removable profile dependency. Set `WEB_UI_ALL_VERSION=` for an official-core-only deployment. -## 特性 +## What is included -- **官方核心 + web-ui**:`@deepseek-ai/dsh` + `@linxin666/dsh-web-ui-all`(aionui 面板 / 任务看板 / git 图 / SSH 运维 / 皮肤等,视为本体核心)。**不包含**非必要第三方插件 -- **HTTPS 公网访问**:nginx 443 SSL(默认自签,任何服务器可跑;Let's Encrypt 正式证书为可选)+ Basic Auth + 80 强制跳转 443 -- **可重复部署**:`install.sh` 一键从零部署;`update.sh` 升级并重打补丁 -- **可重放补丁**:`patch-realtime.sh`(连接保活,幂等)+ `patch-plugins-key.sh`(rc.7 keyed 插槽兼容,幂等) +- Exact-version DSH installation (`0.1.0-rc.8` in this release). +- Optional Web UI aggregate plugin (`0.1.12`, the version validated on RC.8). +- Nginx HTTP→HTTPS redirect, TLS, Basic Auth, WebSocket/SSE support, upload sizing, and stripped upstream Basic credentials. +- A systemd DSH service and a root-owned package-change watcher that can restart it safely. +- Safe upgrades with complete DSH dependency-tree validation and automatic rollback. +- `scripts/verify.sh`, which validates: + - every installed `@deepseek-ai/dsh-*` package matches the target version; + - profile JSON and generated Cordis configuration; + - no plugin uses a volatile `link:/tmp` dependency; + - linked plugin targets exist; + - systemd/Nginx/upstream health; + - every discovered third-party client bundle is served with HTTP 200; + - the current DSH boot has no plugin/runtime errors. -## 目录结构 +## Quick start +```bash +git clone https://github.com/qtimy/dsh-cloud-server-deploy.git +cd dsh-cloud-server-deploy +cp .env.example .env +# Edit TRUSTED_HOST and BASIC_AUTH_PASSWORD at minimum. +sudo bash install.sh ``` -. -├── install.sh # 一键部署(sudo bash install.sh) -├── .env.example # 环境配置模板(cp 为 .env 后填写) -├── templates/ # settings / credentials / profile 模板 -├── scripts/ -│ ├── patch-realtime.sh # 实时连接保活补丁(幂等可重放) -│ ├── patch-plugins-key.sh # rc.7 插件 keyed 插槽兼容补丁(幂等) -│ ├── update.sh # 升级 + 重打补丁 -│ ├── setup-letsencrypt.sh # 可选的 Let's Encrypt 正式证书(需域名) -│ └── dsh-autorestart.sh # 插件安装后自动重启监听 -├── nginx/dsh.conf.tpl # HTTPS + Basic Auth 反代配置 -├── systemd/ # dsh-web / dsh-autorestart 服务单元 -└── LICENSE # MIT -``` -## 快速开始 +Then open `https:///`. A self-signed certificate is created by default, so the browser will show a warning until you install a trusted certificate. + +For a core-only profile, set this before installation: ```bash -# 1. 准备环境配置 -cp .env.example .env -# 编辑 .env,填入你自己的服务器地址、访问用户名密码等 +WEB_UI_ALL_VERSION= +``` -# 2. 一键部署(需要 root) -sudo bash install.sh +## Configuration ownership + +- Core: globally installed `@deepseek-ai/dsh`; never modified by this repository. +- Profile: `/home//.dsh/profiles/web/package.json` and its plugin dependencies. +- User settings and credentials: `/home//.dsh/settings.yaml` and `.credentials.yaml`. +- Public edge: `/etc/nginx/sites-available/dsh` and `/etc/nginx/.htpasswd`. +- Service runtime: `/etc/systemd/system/dsh-web.service`. +- Deployment helpers: `/opt/dsh-deploy` by default. + +Re-running `install.sh` preserves existing DSH settings, credentials, and profile manifests. It installs missing templates only, reconciles the requested optional plugin, refreshes the public/service structure, and verifies the result. -# 3. 浏览器打开(默认自签证书会告警,确认放行即可) -# https://<你的服务器地址>/ +## Plugins + +Install additional functionality through DSH rather than editing core files: + +```bash +sudo -u ubuntu -H dsh plugin --profile web add package-name@exact-version +sudo -u ubuntu -H dsh plugin --profile web remove package-name +sudo /opt/dsh-deploy/verify.sh ``` -> **部署后的配置**:首次通过浏览器访问后,进入 DSH 的 Web UI 设置页配置你自己的模型 provider、API Key 等。仓库不预设任何模型/密钥配置。 +Persistent local plugins must live under a durable path such as `/home/ubuntu/.dsh/plugins/`. Never install a profile dependency from `/tmp`; the verifier rejects such links. -## 正式证书(可选) +The Web UI `settings.plugin.item` compatibility fix is intentionally limited to the plugin's own generated client file. It does not touch DSH core. -`install.sh` 不强制证书方案。如果你有公网域名且想用 Let's Encrypt 免费证书: +## Upgrade or rollback + +Always specify an exact target version: ```bash -# 前提:域名已解析到本机公网 IP,且 80 端口可被公网访问 -sudo bash scripts/setup-letsencrypt.sh <你的域名> [联系邮箱] +sudo /opt/dsh-deploy/update.sh 0.1.0-rc.8 ``` -- 无域名 / 内网 / 纯 IP 服务器:**跳过此步**,直接用自签证书即可 -- 已有自有证书(非 LetsEncrypt):直接把证书放入 `/etc/nginx/certs/`,并修改 `nginx/dsh.conf.tpl` 的 `ssl_certificate*` 路径 -- 前置已有反向代理处理 TLS:可停用本模板的 443 server 块,仅保留 dsh-web 的 3080 回源 +The updater backs up profile manifests, stops DSH, installs the pinned core, validates the complete DSH package tree, checks plugins without upgrading them, restarts, and performs a stability check. Any failure attempts an automatic rollback to the previous DSH version. + +## Trusted certificate (optional) -## 版本升级 +After DNS points to the server and port 80 is publicly reachable: + +```bash +sudo bash scripts/setup-letsencrypt.sh dsh.example.com admin@example.com +``` + +The certificate paths are persisted in `/opt/dsh-deploy/tls.env`, so a later installer run keeps the trusted certificate. + +## Repository layout + +```text +install.sh +scripts/ + dsh-autorestart.sh + patch-plugins-key.sh + setup-letsencrypt.sh + update.sh + verify.sh +templates/ +nginx/ +systemd/ +tests/ +docs/ +``` -1. **查版本**:`npm view @deepseek-ai/dsh version` -2. **升级**:`sudo bash scripts/update.sh <明确版本>`(脚本会锁版安装 → 验证子包一致 → 重打补丁 → 重启 → 健康检查 → 失败自动回滚;不带版本参数会报错退出) -3. **补丁失效**:`patch-realtime.sh` 锚点对不上时,更新其 `EDITS` 锚点以匹配新版源码并重新验证。 -4. **同步**:本仓库是唯一真源,服务器 `/opt/dsh-deploy/` 由脚本生成,不是维护目标。 +See [the RC.8 live audit](docs/live-audit-rc8.md) and [the changelog](CHANGELOG.md) for the evidence behind this release. -## 安全说明 +## Security notes -- **绝不提交 `.env`**(已 `.gitignore`) -- 部署后凭据文件权限收紧(`0600` / `0640`) -- 默认自签证书仅用于加密传输;正式证书走可选的 `scripts/setup-letsencrypt.sh` +- Never commit `.env`; it contains the Basic Auth password and may contain API keys. +- The generated password file uses bcrypt and is readable only by root/Nginx. +- Nginx authenticates public traffic and removes the Basic `Authorization` header before proxying to DSH. +- DSH listens only on `127.0.0.1`; expose ports 80/443, not 3080. +- The optional self-signed certificate encrypts traffic but does not provide public identity verification. diff --git a/docs/live-audit-rc8.md b/docs/live-audit-rc8.md new file mode 100644 index 0000000..977db2f --- /dev/null +++ b/docs/live-audit-rc8.md @@ -0,0 +1,41 @@ +# Live EC2 audit: DSH RC.8 clean-core baseline + +Audit date: 2026-08-20 + +## Core + +- Top-level version: `@deepseek-ai/dsh@0.1.0-rc.8`. +- Dependency tree: 186 installed `@deepseek-ai/dsh-*` packages, all at `0.1.0-rc.8`. +- The previous deployment modified exactly three official files for SSE/WebSocket heartbeat behavior: + - `dsh-host-apiproxy/lib/types/fetch/handler.js` + - `dsh-client-connection/lib/index.js` + - `dsh-client-connection/lib/client.js` +- Those files were restored from clean RC.8 package contents. DSH remained stable and all plugin checks continued to pass, so the patch is excluded from this release. + +## Profile and plugins + +The reference instance has more plugins than this minimal repository installs. They were audited to validate the RC.8 host and plugin structure: + +| Profile dependency | Version/source | Result | +| --- | --- | --- | +| `@linxin666/dsh-web-ui-all` | `0.1.12` | Loaded; all 11 discovered client packages served HTTP 200 | +| `dsh-agent-orchestrator` | persistent local link, `0.1.0` | Present in generated config | +| `dsh-usage-stats` | persistent local link, `0.1.0` | Generated config + client HTTP 200 | +| `dsh-better-sidebar-skin-yield` | persistent local link, `0.1.0` | Generated config + client HTTP 200 | +| `dsh-better-sidebar` | `0.12.1` | Client HTTP 200; Sidebar API/file/HTML/WebSocket routes registered | +| `dsh-plugin-marketplace` | GitHub package, resolved `1.5.4` | Client HTTP 200; marketplace list HTTP 200; management routes registered | + +Additional observations: + +- All local links resolve under `/home/ubuntu/.dsh/plugins`; none use `/tmp`. +- DSH `--dump-config` completes with no warnings. +- AionUI, Git Graph, SSH, Marketplace, and Sidebar host route families are registered. +- `remote-web-ui` is installed by the aggregate but intentionally configured with `enabled: false` because public access already uses Nginx Basic Auth. +- After restoring clean core files, the DSH PID remained stable, upstream `/` returned HTTP 200, every discovered third-party client bundle returned HTTP 200, and the current boot log contained no plugin/runtime failures. + +## Repository decisions + +- Only `dsh-base` and `dsh-web-app` belong in the core profile template. +- Optional packages are installed through `dsh plugin` and remain removable dependencies. +- Compatibility edits may target plugin-owned files, but never the global DSH installation. +- The deployment verifier treats volatile links, mixed core versions, config warnings, missing client bundles, restarts, and known boot errors as failures. diff --git a/install.sh b/install.sh index 14f11d1..b96a691 100644 --- a/install.sh +++ b/install.sh @@ -1,202 +1,233 @@ #!/usr/bin/env bash -# install.sh — DSH 一键部署(官方核心 + web-ui 全家桶 + HTTPS 公网访问) -# -# 功能: -# 1. 从 .env 读取密钥/配置(密钥不落库、不落脚本) -# 2. 安装 Node 依赖(pnpm)+ 全局 DSH(官方 @deepseek-ai/dsh,默认锁定版本) -# 3. 生成 ~/.dsh 配置(settings.yaml / .credentials.yaml / profiles/web) -# 4. 安装 web-ui 插件(@linxin666/dsh-web-ui-all)+ key 兼容补丁 -# 5. 重放 realtime-connectivity 补丁(patch-realtime.sh) -# 6. 配置 nginx HTTPS + Basic Auth 反向代理(浏览器加密访问) -# 7. 安装 systemd 服务(dsh-web / dsh-autorestart)并健康检查 -# -# 用法:sudo bash install.sh (首次会引导你 cp .env.example .env) -set -euo pipefail +# Clean-core DSH cloud installer. Run from the repository root with sudo. +set -Eeuo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -REPO_DIR="$(dirname "$SCRIPT_DIR")" -ENV_FILE="$REPO_DIR/.env" - -# ---- 0. 前置:必须有 .env ---- -if [ ! -f "$ENV_FILE" ]; then - echo "[install] 未找到 $ENV_FILE,请先:" - echo " cp $REPO_DIR/.env.example $ENV_FILE" - echo " 然后编辑 .env 填入真实密钥与配置。" - exit 1 +REPO_DIR="${SCRIPT_DIR}" +ENV_FILE="${REPO_DIR}/.env" + +if [[ ${EUID} -ne 0 ]]; then + echo "ERROR: run with sudo bash install.sh" >&2 + exit 2 +fi +if [[ ! -f "${ENV_FILE}" ]]; then + echo "ERROR: ${ENV_FILE} is missing." >&2 + echo "Run: cp ${REPO_DIR}/.env.example ${ENV_FILE}, then edit it." >&2 + exit 2 fi -# 读取 .env(不 set -a,避免污染;逐项 source) -set -a; source "$ENV_FILE"; set +a -# ---- 版本参数(必须从 .env 提供,不提供默认值,避免硬编码过期版本)---- +# The operator owns this file; exporting lets template variables and API-key +# names remain available to child commands without putting secrets in the repo. +set -a +# shellcheck disable=SC1090 +source "${ENV_FILE}" +set +a + DSH_VERSION="${DSH_VERSION:-}" WEB_UI_ALL_VERSION="${WEB_UI_ALL_VERSION:-}" DSH_PORT="${DSH_PORT:-3080}" DSH_USER="${DSH_USER:-ubuntu}" -DSH_HOME_TARGET="/home/${DSH_USER}/.dsh" DEPLOY_DIR="${DEPLOY_DIR:-/opt/dsh-deploy}" +TRUSTED_HOST="${TRUSTED_HOST:-}" +BASIC_AUTH_USER="${BASIC_AUTH_USER:-dsh}" +BASIC_AUTH_PASSWORD="${BASIC_AUTH_PASSWORD:-}" +DSH_HOME_TARGET="/home/${DSH_USER}/.dsh" +PROFILE_DIR="${DSH_HOME_TARGET}/profiles/web" -if [ "$(id -u)" -ne 0 ]; then - echo "[install] 需要 root 权限(写入 /etc/nginx、/etc/systemd、全局 npm)。请用 sudo bash install.sh" - exit 1 +exact_version_re='^[0-9]+\.[0-9]+\.[0-9]+(-[A-Za-z0-9.]+)?$' +if [[ ! "${DSH_VERSION}" =~ ${exact_version_re} ]]; then + echo "ERROR: DSH_VERSION must be an exact version (for example 0.1.0-rc.8)." >&2 + exit 2 fi -# 版本必填:避免硬编码过期版本号,复制 .env.example 后必须显式填写 -# 占位符(<...>)或空值均视为未填写 -if [ -z "$DSH_VERSION" ] || [[ "$DSH_VERSION" == *"<"*">"* ]]; then - echo "[install] 错误:请先在 .env 中填写 DSH_VERSION(例如 npm view @deepseek-ai/dsh version 查最新)。" >&2 - exit 1 +if [[ -n "${WEB_UI_ALL_VERSION}" && ! "${WEB_UI_ALL_VERSION}" =~ ${exact_version_re} ]]; then + echo "ERROR: WEB_UI_ALL_VERSION must be empty or an exact version." >&2 + exit 2 fi -if [ -z "$WEB_UI_ALL_VERSION" ] || [[ "$WEB_UI_ALL_VERSION" == *"<"*">"* ]]; then - echo "[install] 错误:请先在 .env 中填写 WEB_UI_ALL_VERSION(例如 npm view @linxin666/dsh-web-ui-all version 查最新)。" >&2 - exit 1 +if [[ -z "${TRUSTED_HOST}" || "${TRUSTED_HOST}" == "your-server-ip-or-domain" ]]; then + echo "ERROR: set TRUSTED_HOST in .env." >&2 + exit 2 +fi +if [[ ! "${TRUSTED_HOST}" =~ ^[A-Za-z0-9.-]+(:[0-9]+)?$ ]]; then + echo "ERROR: TRUSTED_HOST must be a hostname or IPv4 address, optionally with a port." >&2 + exit 2 +fi +if [[ ! "${BASIC_AUTH_USER}" =~ ^[A-Za-z0-9._-]+$ ]]; then + echo "ERROR: BASIC_AUTH_USER contains unsupported characters." >&2 + exit 2 +fi +if [[ -z "${BASIC_AUTH_PASSWORD}" || "${BASIC_AUTH_PASSWORD}" == "change-me-strong-password" ]]; then + echo "ERROR: set a strong BASIC_AUTH_PASSWORD in .env." >&2 + exit 2 +fi +if [[ ! "${DSH_PORT}" =~ ^[0-9]+$ ]] || (( DSH_PORT < 1 || DSH_PORT > 65535 )); then + echo "ERROR: DSH_PORT must be between 1 and 65535." >&2 + exit 2 +fi +if [[ ! "${DSH_USER}" =~ ^[a-z_][a-z0-9_-]*$ ]]; then + echo "ERROR: invalid DSH_USER: ${DSH_USER}" >&2 + exit 2 fi -echo "===== [install] 开始 DSH web-ui 部署 =====" - -# ---- 1. 安装系统依赖 ---- -echo "===== 1/7 安装系统依赖 (node/npm/pnpm/nginx/openssl) =====" +echo "===== 1/8 system dependencies =====" export DEBIAN_FRONTEND=noninteractive -if ! command -v node >/dev/null 2>&1; then - curl -fsSL https://deb.nodesource.com/setup_20.x | bash - - apt-get install -y nodejs +apt-get update -qq +apt-get install -y ca-certificates curl gnupg nginx openssl apache2-utils gettext-base util-linux >/dev/null + +node_major=0 +if command -v node >/dev/null 2>&1; then + node_major="$(node -p 'Number(process.versions.node.split(".")[0])')" +fi +if (( node_major < 22 )); then + curl -fsSL https://deb.nodesource.com/setup_22.x | bash - + apt-get install -y nodejs >/dev/null fi if ! command -v pnpm >/dev/null 2>&1; then npm install -g pnpm fi -apt-get install -y nginx openssl curl >/dev/null 2>&1 || true -# ---- 2. 安装 DSH 核心(官方包)---- -echo "===== 2/7 安装 DSH 核心 (@deepseek-ai/dsh) =====" -if [ "${DSH_VERSION}" = "latest" ]; then - npm install -g @deepseek-ai/dsh@latest -else - npm install -g "@deepseek-ai/dsh@${DSH_VERSION}" +echo "===== 2/8 official DSH core ${DSH_VERSION} =====" +npm view "@deepseek-ai/dsh@${DSH_VERSION}" version >/dev/null +systemctl stop dsh-autorestart dsh-web 2>/dev/null || true +npm install -g "@deepseek-ai/dsh@${DSH_VERSION}" +if [[ "$(dsh --version)" != "${DSH_VERSION}" ]]; then + echo "ERROR: installed DSH version does not match ${DSH_VERSION}." >&2 + exit 1 fi -echo "dsh = $(dsh --version 2>/dev/null || echo unknown)" -# ---- 3. 生成 ~/.dsh 配置 ---- -echo "===== 3/7 生成 ~/.dsh 配置 =====" +echo "===== 3/8 user and clean web profile =====" +if ! id "${DSH_USER}" >/dev/null 2>&1; then + useradd --create-home --shell /bin/bash "${DSH_USER}" +fi mkdir -p "${DSH_HOME_TARGET}"/{profiles/web,sessions,plugins,storages,marketplace} -chown -R "${DSH_USER}:${DSH_USER}" "${DSH_HOME_TARGET}" -# 3a. credentials(通用:从 .env 自动发现所有 *_API_KEY 变量,映射到 credentials.yaml,写成 0600) +install_if_missing() { + local source=$1 target=$2 mode=$3 + if [[ ! -e "${target}" ]]; then + install -o "${DSH_USER}" -g "${DSH_USER}" -m "${mode}" "${source}" "${target}" + echo "created ${target}" + else + echo "preserved ${target}" + fi +} + +install_if_missing "${REPO_DIR}/templates/settings.yaml.tpl" \ + "${DSH_HOME_TARGET}/settings.yaml" 0600 +install_if_missing "${REPO_DIR}/templates/profile-package.json.tpl" \ + "${PROFILE_DIR}/package.json" 0644 +install_if_missing "${REPO_DIR}/templates/cordis.patch.yml.tpl" \ + "${PROFILE_DIR}/cordis.patch.yml" 0644 +install_if_missing "${REPO_DIR}/templates/pnpm-workspace.yaml.tpl" \ + "${PROFILE_DIR}/pnpm-workspace.yaml" 0644 + CRED_FILE="${DSH_HOME_TARGET}/.credentials.yaml" -cp "$REPO_DIR/templates/credentials.yaml.tpl" "$CRED_FILE" -# 从 .env 提取所有 *_API_KEY 变量名,逐个映射(值不落明文模板,仅在此写入最终文件) -API_KEY_VARS="$(grep -oE '^[A-Za-z_][A-Za-z0-9_]*_API_KEY=' "$ENV_FILE" 2>/dev/null | sed 's/=$//' | sort -u)" -if [ -n "$API_KEY_VARS" ]; then - : > "$CRED_FILE" - for VAR in $API_KEY_VARS; do - VAL="${!VAR:-}" - printf '%s: %s\n' "$VAR" "$VAL" >> "$CRED_FILE" - done -fi -chmod 600 "$CRED_FILE" -chown "${DSH_USER}:${DSH_USER}" "$CRED_FILE" - -# 3b. settings.yaml -cp "$REPO_DIR/templates/settings.yaml.tpl" "${DSH_HOME_TARGET}/settings.yaml" -chmod 600 "${DSH_HOME_TARGET}/settings.yaml" -chown "${DSH_USER}:${DSH_USER}" "${DSH_HOME_TARGET}/settings.yaml" - -# 3c. profiles/web/{package.json,cordis.patch.yml,pnpm-workspace.yaml} -PROFILE_DIR="${DSH_HOME_TARGET}/profiles/web" -cp "$REPO_DIR/templates/profile-package.json.tpl" "$PROFILE_DIR/package.json" -cp "$REPO_DIR/templates/cordis.patch.yml.tpl" "$PROFILE_DIR/cordis.patch.yml" -cp "$REPO_DIR/templates/pnpm-workspace.yaml.tpl" "$PROFILE_DIR/pnpm-workspace.yaml" -# envsubst 渲染模板中残留的 ${...}(干净版基本无占位,但保险起见) -command -v envsubst >/dev/null 2>&1 && for f in "$PROFILE_DIR"/package.json "$PROFILE_DIR"/cordis.patch.yml "$PROFILE_DIR"/pnpm-workspace.yaml; do - envsubst < "$f" > "$f.tmp" && mv "$f.tmp" "$f" -done -chown -R "${DSH_USER}:${DSH_USER}" "$PROFILE_DIR" - -# 3d. 安装 profile 依赖 -echo " 安装 profile 依赖 (pnpm install)..." -sudo -u "$DSH_USER" bash -c "cd '$PROFILE_DIR' && pnpm install --frozen-lockfile=false 2>/dev/null || pnpm install" - -# 3e. web-ui 插件 key 兼容补丁(rc.7 起 settings.plugin.item 为 keyed 协议, -# 部分旧插件需 id: → key:) -if [ -f "$SCRIPT_DIR/patch-plugins-key.sh" ]; then - echo " 应用 web-ui key 兼容补丁..." - sudo -u "$DSH_USER" DSH_HOME="$DSH_HOME_TARGET" bash "$SCRIPT_DIR/patch-plugins-key.sh" -fi - -# ---- 4. 重放 realtime-connectivity 补丁 ---- -echo "===== 4/7 重放 realtime 补丁 =====" -bash "$SCRIPT_DIR/patch-realtime.sh" - -# ---- 5. 生成 dsh-web.env / deploy.env ---- -echo "===== 5/7 生成环境文件 =====" -mkdir -p /etc -# dsh-web.env 只写 TRUSTED_HOST;模型/密钥统一走 ~/.dsh/.credentials.yaml(见 3a) -cat > /etc/dsh-web.env </dev/null | sed 's/=$//' | sort -u)" + if [[ -n "${api_key_vars}" ]]; then + : > "${CRED_FILE}" + while IFS= read -r variable; do + [[ -n "${variable}" ]] || continue + printf '%s: ' "${variable}" >> "${CRED_FILE}" + node -e 'process.stdout.write(JSON.stringify(process.argv[1]) + "\n")' \ + "${!variable:-}" >> "${CRED_FILE}" + done <<< "${api_key_vars}" + chown "${DSH_USER}:${DSH_USER}" "${CRED_FILE}" + chmod 0600 "${CRED_FILE}" + fi +else + echo "preserved ${CRED_FILE}" +fi -cat > "$DEPLOY_DIR/env-placeholder" < "$DEPLOY_DIR/deploy.env" < "${DEPLOY_DIR}/deploy.env" < /etc/dsh-web.env </dev/null 2>&1 fi -# htpasswd -printf '%s:%s\n' "${BASIC_AUTH_USER:-dsh}" "$(openssl passwd -apr1 "${BASIC_AUTH_PASSWORD:-change-me}")" > /etc/nginx/.htpasswd -chmod 640 /etc/nginx/.htpasswd +if [[ ! -s "${SSL_CERT_PATH}" || ! -s "${SSL_KEY_PATH}" ]]; then + echo "ERROR: configured TLS certificate or key does not exist." >&2 + exit 1 +fi +chmod 0600 "${SSL_KEY_PATH}" +printf '%s\n' "${BASIC_AUTH_PASSWORD}" | \ + htpasswd -i -B -c /etc/nginx/.htpasswd "${BASIC_AUTH_USER}" >/dev/null +chmod 0640 /etc/nginx/.htpasswd +chown root:www-data /etc/nginx/.htpasswd -# 渲染 nginx 配置 export SSL_CERT_PATH SSL_KEY_PATH DSH_PORT -envsubst < "$REPO_DIR/nginx/dsh.conf.tpl" > /etc/nginx/sites-available/dsh -ln -sf /etc/nginx/sites-available/dsh /etc/nginx/sites-enabled/dsh -# 移除默认站点避免冲突 +envsubst '${SSL_CERT_PATH} ${SSL_KEY_PATH} ${DSH_PORT}' \ + < "${REPO_DIR}/nginx/dsh.conf.tpl" > /etc/nginx/sites-available/dsh +ln -sfn /etc/nginx/sites-available/dsh /etc/nginx/sites-enabled/dsh rm -f /etc/nginx/sites-enabled/default -nginx -t && systemctl enable --now nginx || systemctl reload nginx +nginx -t +systemctl enable nginx >/dev/null +systemctl restart nginx -# ---- 7. systemd 服务 ---- -echo "===== 7/7 安装 systemd 服务 =====" +echo "===== 7/8 systemd services =====" sed -e "s|__DSH_USER__|${DSH_USER}|g" \ -e "s|__DSH_PORT__|${DSH_PORT}|g" \ - "$REPO_DIR/systemd/dsh-web.service.tpl" > /etc/systemd/system/dsh-web.service + "${REPO_DIR}/systemd/dsh-web.service.tpl" > /etc/systemd/system/dsh-web.service sed -e "s|__DSH_USER__|${DSH_USER}|g" \ -e "s|__DEPLOY_DIR__|${DEPLOY_DIR}|g" \ - "$REPO_DIR/systemd/dsh-autorestart.service.tpl" > /etc/systemd/system/dsh-autorestart.service + "${REPO_DIR}/systemd/dsh-autorestart.service.tpl" > /etc/systemd/system/dsh-autorestart.service systemctl daemon-reload -systemctl enable --now dsh-web dsh-autorestart - -# ---- 健康检查 ---- -sleep 6 -echo "===== 健康检查 =====" -systemctl is-active dsh-web -CODE="$(curl -s -o /dev/null -w '%{http_code}' "http://127.0.0.1:${DSH_PORT}/" 2>/dev/null || echo 000)" -echo "dsh-web http_code = ${CODE}" -if [ "${CODE}" = "200" ] || [ "${CODE}" = "302" ] || [ "${CODE}" = "401" ]; then - echo "OK: dsh-web 已就绪" -else - echo "警告:健康检查未通过,查看日志: journalctl -u dsh-web -n 50" -fi +systemctl enable dsh-web dsh-autorestart >/dev/null +systemctl restart dsh-web +systemctl restart dsh-autorestart + +echo "===== 8/8 deployment verification =====" +"${DEPLOY_DIR}/verify.sh" "${DSH_VERSION}" -echo "" -echo "===== 部署完成 =====" -echo "供浏览器加密访问的地址: https://${TRUSTED_HOST}/" -echo "登录账号: ${BASIC_AUTH_USER:-dsh} (密码见 .env 的 BASIC_AUTH_PASSWORD)" -echo "提示: 自签证书会触发浏览器告警,可替换 /etc/nginx/certs/ 下为真实证书。" +echo +echo "DSH ${DSH_VERSION} is ready at https://${TRUSTED_HOST}/" +echo "Basic Auth user: ${BASIC_AUTH_USER}" +echo "Run future checks with: sudo ${DEPLOY_DIR}/verify.sh" diff --git a/nginx/dsh.conf.tpl b/nginx/dsh.conf.tpl index 2dbd7f5..fcf475d 100644 --- a/nginx/dsh.conf.tpl +++ b/nginx/dsh.conf.tpl @@ -11,7 +11,14 @@ server { listen 80 default_server; listen [::]:80 default_server; server_name _; - return 301 https://$host$request_uri; + # Harmless before Certbot is configured; required for HTTP-01 renewal. + location ^~ /.well-known/acme-challenge/ { + root /var/www/certbot; + default_type text/plain; + } + location / { + return 301 https://$host$request_uri; + } } server { listen 443 ssl default_server; @@ -22,6 +29,8 @@ server { ssl_certificate_key ${SSL_KEY_PATH}; ssl_protocols TLSv1.2 TLSv1.3; + client_max_body_size 110m; + # ---- Basic Auth:浏览器加密打开时的登录账号 ---- auth_basic "DSH Login"; auth_basic_user_file /etc/nginx/.htpasswd; @@ -35,6 +44,7 @@ server { # 不缓冲流式响应(SSE/事件流即时下发) proxy_buffering off; + proxy_request_buffering off; proxy_connect_timeout 60s; proxy_read_timeout 3600s; @@ -48,13 +58,15 @@ server { proxy_set_header X-Forwarded-Proto $scheme; # 带 rev 哈希的静态资源永久缓存 - location ~* ^/(assets|plugins)/.*.(js|css|svg|png|woff2?|map)$ { + location ~* ^/(assets|plugins)/.*\.(js|css|svg|png|woff2?|map)$ { proxy_set_header Host 127.0.0.1; proxy_set_header Origin http://127.0.0.1; # 必须保留:location 内任何 proxy_set_header 都会整体替换 server 级继承, # 少了这两行 WebSocket 升级头就会被剥掉 → /api/events.* 全部 426,实时推送全挂。 proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; + # Basic Auth terminates at Nginx; never forward its credential upstream. + proxy_set_header Authorization ""; proxy_pass http://127.0.0.1:${DSH_PORT}; add_header Cache-Control "public, max-age=31536000, immutable"; } @@ -65,6 +77,7 @@ server { # 同上:必须保留 Upgrade/Connection,否则 WebSocket 升级失败 proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; + proxy_set_header Authorization ""; proxy_pass http://127.0.0.1:${DSH_PORT}; } -} \ No newline at end of file +} diff --git a/scripts/dsh-autorestart.sh b/scripts/dsh-autorestart.sh index a6036ec..15fe146 100644 --- a/scripts/dsh-autorestart.sh +++ b/scripts/dsh-autorestart.sh @@ -1,28 +1,31 @@ #!/usr/bin/env bash -# dsh-autorestart.sh — 监听 profile 的 package.json 变更,安装完成后自动重启 dsh-web。 -# 由 dsh-autorestart.service 调用;本脚本无敏感信息,可安全入库。 -set -u +# Restart dsh-web after an official plugin command changes the profile manifest. +set -Eeuo pipefail -WATCH="${DSH_HOME:-$HOME/.dsh}/profiles/web/package.json" -LOCK=/run/lock/dsh-autorestart +: "${DSH_HOME:?DSH_HOME must point to the service user .dsh directory}" +WATCH="${DSH_HOME}/profiles/web/package.json" +LOCK=/run/lock/dsh-autorestart.lock -LAST=0 -CUR=$(stat -c %Y "$WATCH" 2>/dev/null || echo 0) -LAST=$CUR +signature() { + stat -c '%Y:%s:%i' "${WATCH}" 2>/dev/null || printf 'missing' +} + +last="$(signature)" while true; do sleep 5 - CUR=$(stat -c %Y "$WATCH" 2>/dev/null || echo 0) - [ "$CUR" != "$LAST" ] || continue - LAST=$CUR - if ! mkdir "$LOCK" 2>/dev/null; then continue; fi - echo "[dsh-autorestart] package.json changed; waiting for install to finish" | systemd-cat -t dsh-autorestart - for _ in $(seq 1 18); do - pgrep -f 'pnpm|npm|git clone' >/dev/null 2>&1 || break - sleep 10 + current="$(signature)" + [[ "${current}" != "${last}" ]] || continue + last="${current}" + + exec 9>"${LOCK}" + flock -n 9 || continue + logger -t dsh-autorestart 'profile package.json changed; waiting for package manager' + for _ in $(seq 1 36); do + pgrep -f '[p]npm|[n]pm|[g]it clone' >/dev/null 2>&1 || break + sleep 5 done - sleep 10 - echo "[dsh-autorestart] restarting dsh-web" | systemd-cat -t dsh-autorestart - systemctl restart dsh-web - rmdir "$LOCK" 2>/dev/null sleep 5 + logger -t dsh-autorestart 'restarting dsh-web' + systemctl restart dsh-web + flock -u 9 done diff --git a/scripts/patch-plugins-key.sh b/scripts/patch-plugins-key.sh index 151498c..f8e89f9 100644 --- a/scripts/patch-plugins-key.sh +++ b/scripts/patch-plugins-key.sh @@ -1,40 +1,24 @@ #!/usr/bin/env bash -# patch-plugins-key.sh — web-ui 插件 key 兼容补丁 -# -# 背景: 较新版本 DSH 将 settings.plugin.item 插槽改为 keyed 协议(要求 key: 而非 id:)。 -# 部分社区插件仍用旧写法 id:,需替换为 key: 才能正常加载。 -# 本脚本幂等地把 id: "web-ui-plugins" 替换为 key: "web-ui-plugins"。 -# -# 用法: bash patch-plugins-key.sh -# 幂等: 已修复时输出 "already OK",不会重复替换。 -set -u +# Plugin-owned compatibility for older @linxin666/dsh-web-ui releases. +# This script never modifies the global @deepseek-ai/dsh installation. +set -Eeuo pipefail -DSH_HOME="${DSH_HOME:-$HOME/.dsh}" +DSH_HOME="${DSH_HOME:-${HOME}/.dsh}" +FILE="${DSH_HOME}/profiles/web/node_modules/@linxin666/dsh-client-ui-web-ui-settings/lib/client.js" -# 目标文件(web-ui 全家桶中需兼容的插件),幂等替换 -declare -a TARGETS=( - "${DSH_HOME}/profiles/web/node_modules/@linxin666/dsh-client-ui-web-ui-settings/lib/client.js" -) +if [[ ! -f "${FILE}" ]]; then + echo "patch-plugins-key: web-ui settings plugin is not installed; skipped" + exit 0 +fi +if grep -q 'key: "web-ui-plugins"' "${FILE}"; then + echo "patch-plugins-key: already compatible" + exit 0 +fi +if ! grep -q 'id: "web-ui-plugins"' "${FILE}"; then + echo "patch-plugins-key: ERROR: unknown plugin slot shape in ${FILE}" >&2 + exit 1 +fi -STATUS=0 -for FILE in "${TARGETS[@]}"; do - [ -f "$FILE" ] || continue - FIXED=0 - # 1) 已修复(key: 存在)→ 跳过 - if grep -q 'key: "web-ui-plugins"' "$FILE" 2>/dev/null; then - echo " ✅ already OK: ${FILE}" - continue - fi - # 2) 旧写法 id: → key: - if grep -q 'id: "web-ui-plugins"' "$FILE" 2>/dev/null; then - sed -i 's/id: "web-ui-plugins"/key: "web-ui-plugins"/g' "$FILE" - echo " 🔧 patched web-ui-plugins: ${FILE}" - FIXED=1 - fi - if [ "$FIXED" = "0" ]; then - echo " (unchanged, no old-style id found): ${FILE}" - fi -done - -echo "patch-plugins-key: done (status=$STATUS)" -exit "$STATUS" +sed -i 's/id: "web-ui-plugins"/key: "web-ui-plugins"/g' "${FILE}" +grep -q 'key: "web-ui-plugins"' "${FILE}" +echo "patch-plugins-key: patched plugin-owned settings slot" diff --git a/scripts/patch-realtime.sh b/scripts/patch-realtime.sh deleted file mode 100755 index 7c942e6..0000000 --- a/scripts/patch-realtime.sh +++ /dev/null @@ -1,365 +0,0 @@ -#!/usr/bin/env bash -# patch-realtime.sh — idempotent re-apply of the DSH realtime-connectivity patches. -# -# Problem it fixes: the web GUI pushes session events over server-to-browser -# streams (/api/events.mux, /api/events.host — WebSocket in the browser, SSE for -# in-process clients). Idle streams emitted zero bytes, so NATs/proxies/browser -# sleep silently reaped the socket (half-open TCP). The client only reconnects -# when the stream errors or ends, so a silently dead connection left the page -# stale until a manual refresh. -# -# Patches: -# 1. server dsh-host-apiproxy .../fetch/handler.js (SSE path): periodic -# `: keepalive` comment every 15s on every SSE stream. -# 2. server dsh-client-connection/lib/index.js (WebSocket path): periodic -# `stream/heartbeat` frame every 15s per open downlink socket. -# 3. client dsh-client-connection/lib/client.js: -# - accept `stream/heartbeat` frames in the mux/host frame schemas; -# - wire-liveness watchdog (abort + reconnect after 45s of silence); -# - visibilitychange handler forcing a fresh reconnect on tab return; -# - wire-activity tracking in both the SSE reader and WebSocket reader. -# -# Every sub-edit is idempotent (skipped when its new content is already present) -# and loud when an anchor is missing (upstream refactor). Exits 0 when everything -# is in place, 1 with warnings otherwise. Implemented in python3 for reliable -# multi-line string handling. -set -uo pipefail - -python3 - <<'PY' -import sys - -FILES = { - "handler": "/usr/lib/node_modules/@deepseek-ai/dsh/node_modules/@deepseek-ai/dsh-host-apiproxy/lib/types/fetch/handler.js", - "downlink": "/usr/lib/node_modules/@deepseek-ai/dsh/node_modules/@deepseek-ai/dsh-client-connection/lib/index.js", - "client": "/usr/lib/node_modules/@deepseek-ai/dsh/node_modules/@deepseek-ai/dsh-client-connection/lib/client.js", -} - -# (file_key, old, new, marker, label) -EDITS = [ - # ---- 1. server SSE heartbeat (handler.js) ---- - ("handler", -""" const stream = new ReadableStream({ - async start(controller) { - try { - // Send an SSE comment line on open so clients/proxies see a live channel (the host - // stream has no baseline frames and would otherwise emit zero bytes while idle; - // a comment line is not a frame, so client frame parsing skips it naturally). - controller.enqueue(encoder.encode(': connected\\n\\n')); - for await (const narrow of frames) { - controller.enqueue(encoder.encode(`data: ${JSON.stringify(fullFrame(narrow))}\\n\\n`)); - } - }""", -""" const stream = new ReadableStream({ - async start(controller) { - // Periodic SSE comment heartbeat while the upstream stays open. Idle streams would - // otherwise emit zero bytes, so NATs/proxies/browsers silently reap the socket - // (half-open TCP) and the client hangs on a dead stream with no error — stale UI - // until a manual refresh. The comment keeps the channel alive through idle - // timeouts and gives the client watchdog a liveness signal; it is not a frame, - // so client frame parsing skips it naturally. - const heartbeat = setInterval(() => { - try { - controller.enqueue(encoder.encode(': keepalive\\n\\n')); - } - catch { - clearInterval(heartbeat); - } - }, 15_000); - try { - // Send an SSE comment line on open so clients/proxies see a live channel (the host - // stream has no baseline frames and would otherwise emit zero bytes while idle; - // a comment line is not a frame, so client frame parsing skips it naturally). - controller.enqueue(encoder.encode(': connected\\n\\n')); - for await (const narrow of frames) { - controller.enqueue(encoder.encode(`data: ${JSON.stringify(fullFrame(narrow))}\\n\\n`)); - } - }""", -": keepalive\\n\\n", -"server SSE heartbeat"), - ("handler", -""" finally { - try { - controller.close(); - } - catch { /* already cancelled by the consumer: a double close is the only reachable error */ } - }""", -""" finally { - clearInterval(heartbeat); - try { - controller.close(); - } - catch { /* already cancelled by the consumer: a double close is the only reachable error */ } - }""", -"finally {\n clearInterval(heartbeat);", -"server SSE heartbeat cleanup"), - # ---- 2. server WebSocket heartbeat (downlink index.js) ---- - ("downlink", -""" async pump(socket, frames, abort) { - try { - for await (const frame of frames) await send(socket, frame); - } catch (error) { - if (!abort.signal.aborted) try { - await send(socket, failureFrame(error)); - } catch {} - } finally { - abort.abort(); - if (socket.readyState === WebSocket.OPEN) socket.close(); - } - }""", -""" async pump(socket, frames, abort) { - // Application-level heartbeat frame while the socket stays open. Idle streams - // would otherwise emit zero bytes, so NATs/proxies/browser sleep silently reap - // the socket (half-open TCP) and the client hangs on a dead channel with no - // error — stale UI until a manual refresh. The client treats the frame as a - // liveness signal and ignores it otherwise. - const heartbeat = setInterval(() => { - if (socket.readyState !== WebSocket.OPEN) return; - send(socket, { - rpcId: RpcId(randomUUID()), - payload: { type: "stream/heartbeat" } - }).catch(() => {}); - }, 15_000); - try { - for await (const frame of frames) await send(socket, frame); - } catch (error) { - if (!abort.signal.aborted) try { - await send(socket, failureFrame(error)); - } catch {} - } finally { - clearInterval(heartbeat); - abort.abort(); - if (socket.readyState === WebSocket.OPEN) socket.close(); - } - }""", -'payload: { type: "stream/heartbeat" }', -"server WebSocket heartbeat"), - # ---- 3a. client liveness constants ---- - ("client", -""" const CONNECTION_DEFAULTS = { - backoffBaseMs: 500, - backoffFactor: 2, - backoffMaxMs: 1e4, - streamOpenTimeoutMs: 3e3 - };""", -""" const CONNECTION_DEFAULTS = { - backoffBaseMs: 500, - backoffFactor: 2, - backoffMaxMs: 1e4, - streamOpenTimeoutMs: 3e3 - }; - /** Wire-liveness bookkeeping shared by the SSE readers and the reconnect watchdog. - * Keyed by the generation's AbortSignal; every byte chunk (SSE comment heartbeats - * included) refreshes the timestamp. The server heartbeats every 15s, so silence - * well beyond that means the socket died silently (half-open TCP) and the read - * would otherwise hang forever with no error. */ - const wireActivity = /* @__PURE__ */ new Map(); - const WATCHDOG_POLL_MS = 15e3; - const WATCHDOG_IDLE_MS = 45e3;""", -"const WATCHDOG_IDLE_MS = 45e3;", -"client liveness constants"), - # ---- 3b. client visibility reconnect ---- - ("client", -""" /** Idempotent: begin the connect/pump/reconnect loop. */ - start() { - if (this.running) return; - this.running = true; - this.loop(); - } - /** Stop the loop and abort the current generation's streams. */ - stop() { - this.running = false; - this.current?.abort(); - this.current = null; - }""", -""" /** Idempotent: begin the connect/pump/reconnect loop. */ - start() { - if (this.running) return; - this.running = true; - /* Returning to a backgrounded/slept tab: the previous generation's sockets are - * almost certainly dead (browsers throttle timers while hidden, OS sleep kills - * every TCP connection) and neither side notices until data flows again. Force a - * fresh generation on visibility so the UI re-syncs immediately instead of - * waiting for the watchdog. */ - this.onVisibilityChange = () => { - if (typeof document === "undefined") return; - if (document.visibilityState !== "visible") return; - if (!this.isRunning()) return; - this.attempt = 0; - this.current?.abort(); - }; - if (typeof document !== "undefined") document.addEventListener("visibilitychange", this.onVisibilityChange); - this.loop(); - } - /** Stop the loop and abort the current generation's streams. */ - stop() { - this.running = false; - if (typeof document !== "undefined" && this.onVisibilityChange !== void 0) document.removeEventListener("visibilitychange", this.onVisibilityChange); - this.current?.abort(); - this.current = null; - }""", -"this.onVisibilityChange = () => {", -"client visibility reconnect"), - # ---- 3c. client reconnect watchdog ---- - ("client", -""" while (this.running) { - const gen = ++this.generation; - const ac = new AbortController(); - this.current = ac; - /* v8 ignore next -- initializer placeholder: the Promise executor""", -""" while (this.running) { - const gen = ++this.generation; - const ac = new AbortController(); - this.current = ac; - /* Reconnect watchdog: abort the generation when the wire shows no bytes for - * WATCHDOG_IDLE_MS. The server heartbeats every 15s, so silence that long means - * the socket is silently dead (half-open TCP) — a read that never errors. - * Aborting ends the pumps below, which settles `failed` and reconnects. */ - const watchdog = setInterval(() => { - if (!this.isRunning() || gen !== this.generation) return; - const last = wireActivity.get(ac.signal); - if (last === void 0) return; - if (Date.now() - last <= WATCHDOG_IDLE_MS) return; - console.warn("[web-runtime] stream idle watchdog fired; forcing reconnect"); - ac.abort(); - }, WATCHDOG_POLL_MS); - /* v8 ignore next -- initializer placeholder: the Promise executor""", -"stream idle watchdog fired", -"client reconnect watchdog"), - # ---- 3d. client watchdog cleanup ---- - ("client", -""" await failed; - if (!this.isRunning()) return; - this.emitState("reconnecting");""", -""" await failed; - clearInterval(watchdog); - if (!this.isRunning()) return; - this.emitState("reconnecting");""", -"clearInterval(watchdog);", -"client watchdog cleanup"), - # ---- 3e. client SSE reader liveness ---- - ("client", -""" while (true) { - const { done, value } = await reader.read(); - if (done) return; - buffer += decoder.decode(value, { stream: true });""", -""" while (true) { - const { done, value } = await reader.read(); - if (done) return; - wireActivity.set(signal, Date.now()); - buffer += decoder.decode(value, { stream: true });""", -"wireActivity.set(signal, Date.now());\n\t\t\t\t\t\tbuffer += decoder.decode", -"client SSE liveness tracking"), - # ---- 3f. client SSE reader cleanup ---- - ("client", -""" } finally { - await reader.cancel().catch(() => void 0); - }""", -""" } finally { - wireActivity.delete(signal); - await reader.cancel().catch(() => void 0); - }""", -"wireActivity.delete(signal);", -"client SSE liveness cleanup"), - # ---- 3g. client mux schema heartbeat ---- - ("client", -""" object({ - type: literal("stream/error"), - error: rpcErrorSchema - }) - ]); - /** HostFrame union (payload slot of a host-stream ServerRequest). */""", -""" object({ - type: literal("stream/error"), - error: rpcErrorSchema - }), - object({ - type: literal("stream/heartbeat") - }) - ]); - /** HostFrame union (payload slot of a host-stream ServerRequest). */""", -"""object({ - type: literal("stream/heartbeat") - }) - ]); - /** HostFrame union (payload slot of a host-stream ServerRequest). */""", -"client mux schema heartbeat"), - # ---- 3h. client host schema heartbeat ---- - ("client", -""" object({ - type: literal("host/remote-event"), - event: string().min(1), - args: array(unknown()) - }), - object({ - type: literal("stream/error"), - error: rpcErrorSchema - }) - ]);""", -""" object({ - type: literal("host/remote-event"), - event: string().min(1), - args: array(unknown()) - }), - object({ - type: literal("stream/error"), - error: rpcErrorSchema - }), - object({ - type: literal("stream/heartbeat") - }) - ]);""", -"""object({ - type: literal("stream/heartbeat") - }) - ]); - object({});""", -"client host schema heartbeat"), - # ---- 3i. client WebSocket reader liveness ---- - ("client", -""" const handleMessage = (event) => { - let full; - let frame; - try { - if (typeof event.data !== "string") throw new Error("binary WebSocket frame");""", -""" const handleMessage = (event) => { - /* Any wire bytes (including the server's periodic stream/heartbeat - * frames) refresh the liveness timestamp consumed by the reconnect - * watchdog; a socket with no traffic for WATCHDOG_IDLE_MS is treated - * as silently dead and forced into a fresh generation. */ - wireActivity.set(signal, Date.now()); - let full; - let frame; - try { - if (typeof event.data !== "string") throw new Error("binary WebSocket frame");""", -"Any wire bytes (including the server's periodic stream/heartbeat", -"client WebSocket liveness tracking"), -] - -status = 0 -for key, old, new, marker, label in EDITS: - path = FILES[key] - try: - with open(path, encoding="utf-8") as f: - s = f.read() - except OSError as e: - print(f"patch-realtime: WARN {label} — cannot read {path}: {e}", file=sys.stderr) - status = 1 - continue - if marker in s: - print(f"patch-realtime: {label} already applied") - continue - n = s.count(old) - if n != 1: - print(f"patch-realtime: ERROR {label} anchor not found (count={n})", file=sys.stderr) - status = 1 - continue - with open(path, "w", encoding="utf-8") as f: - f.write(s.replace(old, new, 1)) - print(f"patch-realtime: {label} applied") - -if status == 0: - print("patch-realtime: OK — all realtime patches in place") -else: - print("patch-realtime: DONE with warnings (see above)", file=sys.stderr) -sys.exit(status) -PY diff --git a/scripts/setup-letsencrypt.sh b/scripts/setup-letsencrypt.sh index 60dbcd2..ea37898 100644 --- a/scripts/setup-letsencrypt.sh +++ b/scripts/setup-letsencrypt.sh @@ -1,117 +1,76 @@ #!/usr/bin/env bash -# setup-letsencrypt.sh — 可选的 LetsEncrypt 正式证书配置(需公网域名) -# -# ⚠️ 本脚本【不是 install.sh 的必选步骤】。仅当目标服务器有公网域名、 -# 且你想用 Let's Encrypt 免费证书时才手动执行。无域名/内网/纯 IP 的 -# 服务器请用 install.sh 默认的自签证书,不要跑本脚本。 -# -# 前置条件: -# 1. 域名已解析到本机公网 IP(dig +short <域名>) -# 2. 80 端口可被 Let's Encrypt 从公网访问(用于 HTTP-01 验证) -# 3. 已先执行 install.sh(nginx 已就位,监听 80/443) -# -# 用法: -# sudo bash scripts/setup-letsencrypt.sh <域名> [邮箱] -# 例:sudo bash scripts/setup-letsencrypt.sh dsh.example.com admin@example.com -# -# 效果:签发证书 → 切换 nginx 到正式证书 → 配置自动续期 → 验证证书链。 -set -euo pipefail +# Install a trusted Let's Encrypt certificate after the base deployment. +set -Eeuo pipefail + +if [[ ${EUID} -ne 0 ]]; then + echo "ERROR: run as root: sudo $0 [email]" >&2 + exit 2 +fi DOMAIN="${1:-}" EMAIL="${2:-}" +DEPLOY_DIR="${DEPLOY_DIR:-/opt/dsh-deploy}" +NGINX_CONF=/etc/nginx/sites-available/dsh -if [ -z "$DOMAIN" ]; then - echo "ERROR: 缺少域名参数" >&2 - echo "用法: sudo bash scripts/setup-letsencrypt.sh <域名> [邮箱]" >&2 - exit 1 +if [[ ! "${DOMAIN}" =~ ^[A-Za-z0-9.-]+$ ]]; then + echo "ERROR: provide a valid domain name." >&2 + exit 2 fi -# 域名格式校验(防注入) -if [[ ! "$DOMAIN" =~ ^[a-zA-Z0-9.-]+$ ]]; then - echo "ERROR: 域名格式非法: $DOMAIN" >&2 - exit 1 +EMAIL="${EMAIL:-admin@${DOMAIN}}" +if [[ ! "${EMAIL}" =~ ^[^[:space:]@]+@[^[:space:]@]+$ ]]; then + echo "ERROR: provide a valid contact email." >&2 + exit 2 fi -if [ "$(id -u)" -ne 0 ]; then - echo "ERROR: 需要 root 权限(装 certbot、改 nginx、写 /etc/letsencrypt)" >&2 - exit 1 +if [[ ! -f "${NGINX_CONF}" ]]; then + echo "ERROR: run install.sh before setting up Let's Encrypt." >&2 + exit 2 fi -[ -n "$EMAIL" ] || EMAIL="admin@${DOMAIN#*.}" -echo "===== 1/6 前置检查:域名解析到本机公网 IP =====" -PUBLIC_IP="$(curl -s --max-time 10 ifconfig.me 2>/dev/null || curl -s --max-time 10 https://api.ipify.org 2>/dev/null || echo '')" -DOMAIN_IP="$(getent hosts "$DOMAIN" 2>/dev/null | awk '{print $1}' | head -1 || echo '')" -echo "域名 ${DOMAIN} → ${DOMAIN_IP:-解析失败}" -echo "本机公网 IP → ${PUBLIC_IP:-无法获取}" -if [ -n "$PUBLIC_IP" ] && [ -n "$DOMAIN_IP" ] && [ "$PUBLIC_IP" != "$DOMAIN_IP" ]; then - echo "⚠️ 警告:域名解析 IP 与本机公网 IP 不一致,证书签发可能失败。" >&2 - echo " 请确认 DNS 记录指向本机,或 CDN/代理已正确回源。" >&2 -fi - -echo "===== 2/6 安装 certbot 与 nginx 插件 =====" +echo "===== 1/5 install Certbot =====" export DEBIAN_FRONTEND=noninteractive apt-get update -qq -apt-get install -y certbot python3-certbot-nginx >/dev/null 2>&1 +apt-get install -y certbot >/dev/null -echo "===== 3/6 临时注入 ACME 挑战放行(webroot)=====" +echo "===== 2/5 enable the ACME HTTP-01 path =====" mkdir -p /var/www/certbot/.well-known/acme-challenge -# 在 80 端口 server 块注入 /.well-known/acme-challenge/ 放行(幂等) -NGINX_CONF=/etc/nginx/sites-available/dsh -if ! grep -q 'acme-challenge' "$NGINX_CONF" 2>/dev/null; then - python3 - "$NGINX_CONF" <<'PY' -import sys -p = sys.argv[1] -s = open(p).read() -old = ''' listen 80 default_server; - listen [::]:80 default_server; - server_name _; - return 301 https://$host$request_uri;''' -new = ''' listen 80 default_server; - listen [::]:80 default_server; - server_name _; - # ACME HTTP-01 挑战放行(certbot webroot,签发正式证书用) - location ^~ /.well-known/acme-challenge/ { - root /var/www/certbot; - default_type text/plain; +if ! grep -q 'acme-challenge' "${NGINX_CONF}"; then + candidate="$(mktemp)" + awk ' + { print } + !added && /^[[:space:]]*server_name _;/ { + print " location ^~ /.well-known/acme-challenge/ { root /var/www/certbot; }" + added=1 } - location / { - return 301 https://$host$request_uri; - }''' -if old in s: - open(p, 'w').write(s.replace(old, new, 1)) - print("ACME 放行已注入") -else: - print("WARN: 未找到标准 80 端口块,请手动确认") -PY + ' "${NGINX_CONF}" > "${candidate}" + install -m 0644 "${candidate}" "${NGINX_CONF}" + rm -f "${candidate}" fi -nginx -t && systemctl reload nginx +nginx -t +systemctl reload nginx -echo "===== 4/6 签发证书(webroot 模式)=====" +echo "===== 3/5 request the certificate =====" certbot certonly --webroot -w /var/www/certbot \ - -d "$DOMAIN" \ - --email "$EMAIL" \ - --agree-tos --no-eff-email --rsa-key-size 2048 + -d "${DOMAIN}" --email "${EMAIL}" --agree-tos --no-eff-email --non-interactive -echo "===== 5/6 切换 nginx 443 到正式证书 =====" CERT="/etc/letsencrypt/live/${DOMAIN}/fullchain.pem" KEY="/etc/letsencrypt/live/${DOMAIN}/privkey.pem" -python3 - "$NGINX_CONF" "$CERT" "$KEY" <<'PY' -import sys -p, cert, key = sys.argv[1], sys.argv[2], sys.argv[3] -s = open(p).read() -import re -s2 = re.sub(r'ssl_certificate\s+[^;]+;', f'ssl_certificate {cert};', s, count=1) -s2 = re.sub(r'ssl_certificate_key\s+[^;]+;', f'ssl_certificate_key {key};', s2, count=1) -open(p, 'w').write(s2) -print("证书路径已切换") -PY -nginx -t && systemctl reload nginx -echo "===== 6/6 验证证书链 =====" -sleep 2 -echo | openssl s_client -connect "${DOMAIN}:443" -servername "$DOMAIN" 2>/dev/null \ - | grep -E 'subject=|issuer=|Verify return code' | head -4 +echo "===== 4/5 switch Nginx and persist the certificate paths =====" +sed -i -E "s|^[[:space:]]*ssl_certificate[[:space:]]+[^;]+;| ssl_certificate ${CERT};|" \ + "${NGINX_CONF}" +sed -i -E "s|^[[:space:]]*ssl_certificate_key[[:space:]]+[^;]+;| ssl_certificate_key ${KEY};|" \ + "${NGINX_CONF}" +mkdir -p "${DEPLOY_DIR}" +printf 'SSL_CERT_PATH=%s\nSSL_KEY_PATH=%s\n' "${CERT}" "${KEY}" > "${DEPLOY_DIR}/tls.env" +chmod 0600 "${DEPLOY_DIR}/tls.env" +nginx -t +systemctl reload nginx + +echo "===== 5/5 install a renewal deploy hook =====" +install -d -m 0755 /etc/letsencrypt/renewal-hooks/deploy +printf '#!/usr/bin/env bash\nnginx -t && systemctl reload nginx\n' \ + > /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh +chmod 0755 /etc/letsencrypt/renewal-hooks/deploy/reload-nginx.sh +certbot renew --dry-run -echo "" -echo "===== 完成 =====" -echo "https://${DOMAIN}/ 已使用 Let's Encrypt 正式证书。" -echo "certbot 已配置自动续期(systemd timer)。" -echo "提示:续期后 nginx 会自动 reload(certbot renew 钩子已由插件处理)。" \ No newline at end of file +echo "OK: https://${DOMAIN}/ now uses Let's Encrypt." diff --git a/scripts/update.sh b/scripts/update.sh index f3dcc14..ff20987 100755 --- a/scripts/update.sh +++ b/scripts/update.sh @@ -1,107 +1,91 @@ #!/usr/bin/env bash -# update.sh — DSH 版本升级(锁死版本,防止 caret 范围自动拉新破坏插件) -# -# 说明:@latest / 未锁定的 caret 版本范围会在升级时连带拉高众多 dsh-* 子包, -# 可能造成社区插件(id:/key: 插槽协议)不兼容。 -# 因此本脚本【必须显式指定目标版本】,拒绝默认升级。 -# -# 用法: -# sudo bash update.sh # 升级到指定版本(推荐,先查 npm view) -# sudo bash update.sh # 报错退出,提示必须带版本 -# -# 流程: 记旧版 → 锁版安装 → 验证子包一致 → 重打补丁 → 插件 key 修复 → 重启 → 健康检查 → 失败回滚 -set -euo pipefail +# Pinned DSH core upgrade with full-tree validation and automatic rollback. +set -Eeuo pipefail -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -DEPLOY_DIR="$(dirname "$SCRIPT_DIR")" +if [[ ${EUID} -ne 0 ]]; then + echo "ERROR: run as root: sudo $0 " >&2 + exit 2 +fi -# 读取部署变量(DSH_USER/DSH_PORT 等) -# shellcheck disable=SC1090 -[ -f "$DEPLOY_DIR/deploy.env" ] && . "$DEPLOY_DIR/deploy.env" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +if [[ -f "${SCRIPT_DIR}/deploy.env" ]]; then + DEPLOY_DIR="${SCRIPT_DIR}" +elif [[ -f "$(dirname "${SCRIPT_DIR}")/deploy.env" ]]; then + DEPLOY_DIR="$(dirname "${SCRIPT_DIR}")" +else + DEPLOY_DIR="${SCRIPT_DIR}" +fi +# shellcheck disable=SC1091 +[[ -f "${DEPLOY_DIR}/deploy.env" ]] && source "${DEPLOY_DIR}/deploy.env" DSH_USER="${DSH_USER:-ubuntu}" DSH_PORT="${DSH_PORT:-3080}" - -# ---- 0. 必须显式指定版本 ---- +DSH_HOME_TARGET="/home/${DSH_USER}/.dsh" +PROFILE="${DSH_HOME_TARGET}/profiles/web" TARGET="${1:-}" -if [ -z "$TARGET" ]; then - echo "ERROR: 必须显式指定目标版本,例如:" >&2 - echo " sudo bash update.sh " >&2 - echo "当前最新: $(npm view @deepseek-ai/dsh version 2>/dev/null || echo '?')" >&2 - echo "已安装: $(dsh --version 2>/dev/null || echo '?')" >&2 - echo "提示: 升级前请确认已安装的社区插件兼容该版本。" >&2 - exit 1 -fi -# 校验版本号格式(防注入) -if [[ ! "$TARGET" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?$ ]]; then - echo "ERROR: 版本号格式非法: $TARGET" >&2 - exit 1 +if [[ ! "${TARGET}" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[A-Za-z0-9.]+)?$ ]]; then + echo "ERROR: provide an exact target version, for example 0.1.0-rc.8." >&2 + exit 2 fi -echo "===== 1/6 记录当前版本 =====" -OLD_DSH="$(dsh --version 2>/dev/null || echo '未知')" -echo "当前 = ${OLD_DSH}" -echo "目标 = ${TARGET}" -if [ "${OLD_DSH}" = "${TARGET}" ]; then - echo "已是目标版本,无需升级。" +OLD_DSH="$(dsh --version)" +if [[ "${OLD_DSH}" == "${TARGET}" ]]; then + echo "DSH is already ${TARGET}; verifying the current deployment." + "${DEPLOY_DIR}/verify.sh" "${TARGET}" exit 0 fi -echo "===== 2/6 锁版安装 @deepseek-ai/dsh@${TARGET} =====" -npm install -g "@deepseek-ai/dsh@${TARGET}" -echo "dsh now = $(dsh --version 2>/dev/null || echo unknown)" +STAMP="$(date -u +%Y%m%dT%H%M%SZ)" +BACKUP="/home/${DSH_USER}/dsh-upgrades/${STAMP}-${OLD_DSH}-to-${TARGET}" +mkdir -p "${BACKUP}" +cp "${PROFILE}/package.json" "${BACKUP}/package.json" +cp "${PROFILE}/pnpm-lock.yaml" "${BACKUP}/pnpm-lock.yaml" +cp "${DSH_HOME_TARGET}/settings.yaml" "${BACKUP}/settings.yaml" +if [[ -f "${DSH_HOME_TARGET}/cordis.patch.yml" ]]; then + cp "${DSH_HOME_TARGET}/cordis.patch.yml" "${BACKUP}/cordis.patch.yml" +fi +chown -R "${DSH_USER}:${DSH_USER}" "${BACKUP}" -echo "===== 3/6 验证关键子包版本一致性 =====" -# 若外壳是新版但子包是旧版(caret 范围解析不一致),立即中止,避免半吊子升级 -EXPECT="$TARGET" -MISMATCH=0 -for SUB in dsh-web-app dsh-client-ui-slots dsh-client-ui-settings-plugins dsh-host-apiproxy; do - SUB_V="$(cat "/usr/lib/node_modules/@deepseek-ai/dsh/node_modules/@deepseek-ai/${SUB}/package.json" 2>/dev/null | grep '"version"' | head -1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.]+)?' || echo '缺失')" - if [ "${SUB_V}" != "${EXPECT}" ]; then - echo " ⚠️ 子包 ${SUB} 版本 = ${SUB_V},与目标 ${EXPECT} 不一致" - MISMATCH=1 - else - echo " ✅ ${SUB} = ${SUB_V}" +rollback() { + local rc=$? + trap - ERR + set +e + echo "FAILED (exit ${rc}); rolling core back to ${OLD_DSH}." >&2 + npm install -g "@deepseek-ai/dsh@${OLD_DSH}" + if [[ -x "${DEPLOY_DIR}/patch-plugins-key.sh" ]]; then + sudo -u "${DSH_USER}" -H env DSH_HOME="${DSH_HOME_TARGET}" \ + bash "${DEPLOY_DIR}/patch-plugins-key.sh" fi -done -if [ "${MISMATCH}" = "1" ]; then - echo "ERROR: 关键子包版本不一致。不要继续使用本脚本降级/混装。" >&2 - echo "建议: 整树重装 —— npm uninstall -g @deepseek-ai/dsh && npm install -g @deepseek-ai/dsh@${TARGET}" >&2 - exit 1 -fi + systemctl restart dsh-web + "${DEPLOY_DIR}/verify.sh" "${OLD_DSH}" || true + echo "Rollback attempted; inspect journalctl -u dsh-web -n 100." >&2 + exit "${rc}" +} +trap rollback ERR -echo "===== 4/6 重放 realtime 补丁 =====" -bash "$SCRIPT_DIR/patch-realtime.sh" || echo " (realtime patches failed/skipped — inspect $SCRIPT_DIR/patch-realtime.sh)" +echo "===== 1/5 verify release ${TARGET} =====" +npm view "@deepseek-ai/dsh@${TARGET}" version >/dev/null -echo "===== 4.5/6 插件 key 兼容补丁 =====" -if [ -f "$SCRIPT_DIR/patch-plugins-key.sed" ]; then - bash "$SCRIPT_DIR/patch-plugins-key.sh" || echo " (插件 key 补丁 failed/skipped)" -else - echo " (未找到 patch-plugins-key.sh,跳过)" -fi +echo "===== 2/5 install a clean pinned core =====" +systemctl stop dsh-web +npm install -g "@deepseek-ai/dsh@${TARGET}" +[[ "$(dsh --version)" == "${TARGET}" ]] -echo "===== 5/6 重启 + 健康检查 =====" -systemctl restart dsh-web -sleep 8 -HTTP_CODE="$(curl -s -o /dev/null -w '%{http_code}' "http://127.0.0.1:${DSH_PORT}/" 2>/dev/null || echo 000)" -echo "http_code = ${HTTP_CODE}" -if [ "${HTTP_CODE}" = "200" ] || [ "${HTTP_CODE}" = "302" ] || [ "${HTTP_CODE}" = "401" ]; then - echo "OK: dsh-web 健康" -else - echo "FAILED: dsh-web 健康检查失败,回滚到 ${OLD_DSH}" - npm install -g "@deepseek-ai/dsh@${OLD_DSH}" - systemctl restart dsh-web - echo "已回滚。日志: journalctl -u dsh-web -n 50" - exit 1 -fi +echo "===== 3/5 validate core and profile offline =====" +DSH_VERIFY_RUNTIME=0 "${DEPLOY_DIR}/verify.sh" "${TARGET}" -echo "===== 6/6 插件加载检查(keyed slot 报错检测)=====" -sleep 3 -if journalctl -u dsh-web --since "1 minute ago" --no-pager 2>/dev/null | grep -q 'keyed slot.*requires options.key'; then - echo "⚠️ 检测到 keyed slot 报错——插件未兼容。请运行插件 key 补丁或回滚。" -else - echo "✅ 无 keyed slot 报错,插件加载正常" +echo "===== 4/5 apply plugin-owned compatibility only =====" +if [[ -x "${DEPLOY_DIR}/patch-plugins-key.sh" ]]; then + sudo -u "${DSH_USER}" -H env DSH_HOME="${DSH_HOME_TARGET}" \ + bash "${DEPLOY_DIR}/patch-plugins-key.sh" fi +sudo -u "${DSH_USER}" -H dsh --profile web --dump-config >/dev/null + +echo "===== 5/5 restart and verify =====" +systemctl start dsh-web +"${DEPLOY_DIR}/verify.sh" "${TARGET}" -echo "Done. 硬刷新浏览器 (Ctrl/Cmd+Shift+R)。" \ No newline at end of file +trap - ERR +echo "OK: upgraded ${OLD_DSH} -> ${TARGET}; official core remains unpatched." +echo "Backup: ${BACKUP}" diff --git a/scripts/verify.sh b/scripts/verify.sh new file mode 100755 index 0000000..977f773 --- /dev/null +++ b/scripts/verify.sh @@ -0,0 +1,151 @@ +#!/usr/bin/env bash +# Verify core consistency, profile/plugin integrity, and runtime health. +set -Eeuo pipefail + +if [[ ${EUID} -ne 0 ]]; then + echo "ERROR: run as root: sudo $0 [expected-version]" >&2 + exit 2 +fi + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +if [[ -f "${SCRIPT_DIR}/deploy.env" ]]; then + DEPLOY_DIR="${SCRIPT_DIR}" +elif [[ -f "$(dirname "${SCRIPT_DIR}")/deploy.env" ]]; then + DEPLOY_DIR="$(dirname "${SCRIPT_DIR}")" +else + DEPLOY_DIR="${SCRIPT_DIR}" +fi +# shellcheck disable=SC1091 +[[ -f "${DEPLOY_DIR}/deploy.env" ]] && source "${DEPLOY_DIR}/deploy.env" + +DSH_USER="${DSH_USER:-ubuntu}" +DSH_PORT="${DSH_PORT:-3080}" +DSH_HOME_TARGET="/home/${DSH_USER}/.dsh" +PROFILE="${DSH_HOME_TARGET}/profiles/web" +EXPECTED_VERSION="${1:-$(dsh --version)}" +DSH_VERIFY_RUNTIME="${DSH_VERIFY_RUNTIME:-1}" + +echo "[verify] DSH core ${EXPECTED_VERSION}" +[[ "$(dsh --version)" == "${EXPECTED_VERSION}" ]] +GLOBAL_ROOT="$(npm root -g)" +EXPECTED_VERSION="${EXPECTED_VERSION}" GLOBAL_ROOT="${GLOBAL_ROOT}" node <<'NODE' +const fs = require('node:fs') +const path = require('node:path') +const root = path.join(process.env.GLOBAL_ROOT, '@deepseek-ai/dsh/node_modules/@deepseek-ai') +const expected = process.env.EXPECTED_VERSION +const mismatches = [] +let count = 0 +for (const entry of fs.readdirSync(root)) { + const file = path.join(root, entry, 'package.json') + if (!fs.existsSync(file)) continue + const pkg = JSON.parse(fs.readFileSync(file, 'utf8')) + if (!pkg.name?.startsWith('@deepseek-ai/dsh')) continue + count += 1 + if (pkg.version !== expected) mismatches.push(`${pkg.name}@${pkg.version}`) +} +if (mismatches.length) throw new Error(`mixed DSH tree: ${mismatches.join(', ')}`) +console.log(`[verify] ${count} DSH packages match ${expected}`) +NODE + +echo "[verify] profile and plugin links" +node -e 'JSON.parse(require("node:fs").readFileSync(process.argv[1], "utf8"))' \ + "${PROFILE}/package.json" +profile_files=("${PROFILE}/package.json") +[[ -f "${PROFILE}/pnpm-lock.yaml" ]] && profile_files+=("${PROFILE}/pnpm-lock.yaml") +if grep -Rqs 'link:/tmp' "${profile_files[@]}"; then + echo "ERROR: volatile link:/tmp plugin dependency found." >&2 + exit 1 +fi +PROFILE="${PROFILE}" node <<'NODE' +const fs = require('node:fs') +const path = require('node:path') +const profile = process.env.PROFILE +const manifest = JSON.parse(fs.readFileSync(path.join(profile, 'package.json'), 'utf8')) +for (const [name, spec] of Object.entries(manifest.dependencies ?? {})) { + const file = path.join(profile, 'node_modules', ...name.split('/'), 'package.json') + if (!fs.existsSync(file)) throw new Error(`missing plugin dependency: ${name} (${spec})`) + const pkg = JSON.parse(fs.readFileSync(file, 'utf8')) + console.log(`[verify] plugin ${name}@${pkg.version} (${spec})`) +} +NODE + +config_file="$(mktemp)" +config_errors="$(mktemp)" +trap 'rm -f "${config_file}" "${config_errors}"' EXIT +sudo -u "${DSH_USER}" -H dsh --profile web --dump-config \ + > "${config_file}" 2> "${config_errors}" +if [[ -s "${config_errors}" ]]; then + cat "${config_errors}" >&2 + echo "ERROR: DSH generated configuration contains warnings/errors." >&2 + exit 1 +fi +echo "[verify] generated plugin configuration is clean" + +if [[ "${DSH_VERIFY_RUNTIME}" == 0 ]]; then + echo "[verify] offline checks complete" + exit 0 +fi + +echo "[verify] services and upstream" +for _ in $(seq 1 15); do + if systemctl is-active --quiet dsh-web && \ + curl -fsS -o /dev/null "http://127.0.0.1:${DSH_PORT}/"; then + break + fi + sleep 2 +done +systemctl is-active --quiet dsh-web +systemctl is-active --quiet nginx +curl -fsS -o /dev/null "http://127.0.0.1:${DSH_PORT}/" + +pid_before="$(systemctl show -p MainPID --value dsh-web)" +sleep 10 +pid_after="$(systemctl show -p MainPID --value dsh-web)" +if [[ "${pid_before}" == 0 || "${pid_before}" != "${pid_after}" ]]; then + echo "ERROR: dsh-web restarted during the stability check." >&2 + exit 1 +fi + +echo "[verify] third-party client bundles" +while IFS= read -r package_name; do + [[ -n "${package_name}" ]] || continue + code="$(curl -sS -o /dev/null -w '%{http_code}' \ + "http://127.0.0.1:${DSH_PORT}/plugins/${package_name}/client.js")" + if [[ "${code}" != 200 ]]; then + echo "ERROR: client bundle ${package_name} returned HTTP ${code}." >&2 + exit 1 + fi + echo "[verify] client ${package_name}: HTTP 200" +done < <(PROFILE="${PROFILE}" node <<'NODE' +const fs = require('node:fs') +const path = require('node:path') +const root = path.join(process.env.PROFILE, 'node_modules') +const names = new Set() +const inspect = (dir) => { + const file = path.join(dir, 'package.json') + if (!fs.existsSync(file)) return + const pkg = JSON.parse(fs.readFileSync(file, 'utf8')) + if (pkg.name && pkg.dsh?.client && !pkg.name.startsWith('@deepseek-ai/')) names.add(pkg.name) +} +if (!fs.existsSync(root)) process.exit(0) +for (const entry of fs.readdirSync(root, { withFileTypes: true })) { + if (entry.name.startsWith('.')) continue + const full = path.join(root, entry.name) + if (entry.name.startsWith('@')) { + for (const child of fs.readdirSync(full, { withFileTypes: true })) { + if (child.isDirectory() || child.isSymbolicLink()) inspect(path.join(full, child.name)) + } + } else if (entry.isDirectory() || entry.isSymbolicLink()) inspect(full) +} +for (const name of [...names].sort()) console.log(name) +NODE +) + +active_since="$(systemctl show -p ActiveEnterTimestamp --value dsh-web)" +if journalctl -u dsh-web --since "${active_since}" --no-pager 2>/dev/null | \ + grep -Eqi 'plugin tree failed|duplicate prefix route|MODULE_NOT_FOUND|keyed slot.*requires|Main process exited'; then + echo "ERROR: current DSH boot contains a runtime/plugin failure." >&2 + exit 1 +fi + +echo "[verify] OK: clean core, valid plugins, stable PID ${pid_after}, upstream HTTP 200" diff --git a/systemd/dsh-autorestart.service.tpl b/systemd/dsh-autorestart.service.tpl index 9299406..a903975 100644 --- a/systemd/dsh-autorestart.service.tpl +++ b/systemd/dsh-autorestart.service.tpl @@ -5,9 +5,10 @@ Wants=dsh-web.service [Service] Type=simple -User=__DSH_USER__ -Group=__DSH_USER__ -ExecStart=/bin/bash __DEPLOY_DIR__/scripts/dsh-autorestart.sh +User=root +Group=root +Environment=DSH_HOME=/home/__DSH_USER__/.dsh +ExecStart=/bin/bash __DEPLOY_DIR__/dsh-autorestart.sh Restart=on-failure RestartSec=5 diff --git a/systemd/dsh-web.service.tpl b/systemd/dsh-web.service.tpl index 8333d9a..5ebc196 100644 --- a/systemd/dsh-web.service.tpl +++ b/systemd/dsh-web.service.tpl @@ -9,7 +9,7 @@ User=__DSH_USER__ Group=__DSH_USER__ EnvironmentFile=/etc/dsh-web.env WorkingDirectory=/home/__DSH_USER__ -ExecStart=dsh web --port __DSH_PORT__ --trusted-host ${TRUSTED_HOST} +ExecStart=dsh web --no-open --port __DSH_PORT__ --trusted-host ${TRUSTED_HOST} Restart=on-failure RestartSec=5 TimeoutStopSec=120 diff --git a/templates/pnpm-workspace.yaml.tpl b/templates/pnpm-workspace.yaml.tpl index 41ecd6b..74dc414 100644 --- a/templates/pnpm-workspace.yaml.tpl +++ b/templates/pnpm-workspace.yaml.tpl @@ -8,15 +8,3 @@ allowBuilds: cloudflared: true cpu-features: true ssh2: true -minimumReleaseAgeExclude: - - '@linxin666/dsh-client-ui-aionui-panel@${WEB_UI_ALL_VERSION}' - - '@linxin666/dsh-client-ui-git-graph@${WEB_UI_ALL_VERSION}' - - '@linxin666/dsh-client-ui-skin-center@${WEB_UI_ALL_VERSION}' - - '@linxin666/dsh-client-ui-task-board@${WEB_UI_ALL_VERSION}' - - '@linxin666/dsh-client-ui-web-ui-settings@${WEB_UI_ALL_VERSION}' - - '@linxin666/dsh-live-stats@${WEB_UI_ALL_VERSION}' - - '@linxin666/dsh-pet@${WEB_UI_ALL_VERSION}' - - '@linxin666/dsh-remote-web-ui@${WEB_UI_ALL_VERSION}' - - '@linxin666/dsh-skins@${WEB_UI_ALL_VERSION}' - - '@linxin666/dsh-ssh@${WEB_UI_ALL_VERSION}' - - '@linxin666/dsh-web-ui-all@${WEB_UI_ALL_VERSION}' diff --git a/templates/profile-package.json.tpl b/templates/profile-package.json.tpl index baa53c1..5094da7 100644 --- a/templates/profile-package.json.tpl +++ b/templates/profile-package.json.tpl @@ -5,19 +5,8 @@ "profile": { "bundles": [ "@deepseek-ai/dsh-base", - "@deepseek-ai/dsh-web-app", - "@linxin666/dsh-web-ui-all" + "@deepseek-ai/dsh-web-app" ] } - }, - "pnpm": { - "onlyBuiltDependencies": [ - "cloudflared", - "cpu-features", - "ssh2" - ] - }, - "dependencies": { - "@linxin666/dsh-web-ui-all": "${WEB_UI_ALL_VERSION}" } -} \ No newline at end of file +} diff --git a/templates/settings.yaml.tpl b/templates/settings.yaml.tpl index 584cb19..920a269 100644 --- a/templates/settings.yaml.tpl +++ b/templates/settings.yaml.tpl @@ -21,5 +21,7 @@ # provider: my-provider # model: my-model-id -# ---- 默认留空,等待用户自行配置 ---- -{} +# remote-web-ui 会随可选 web-ui aggregate 安装。公网 Nginx Basic Auth 部署中 +# 默认关闭,避免未配对心跳与浏览器 Basic Auth 产生重复登录循环。 +remote-web-ui: + enabled: false diff --git a/tests/static-smoke.sh b/tests/static-smoke.sh new file mode 100755 index 0000000..00ea441 --- /dev/null +++ b/tests/static-smoke.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "${ROOT}" + +while IFS= read -r -d '' script; do + bash -n "${script}" +done < <(find . -type f -name '*.sh' -print0) + +node -e 'JSON.parse(require("node:fs").readFileSync(process.argv[1], "utf8"))' \ + templates/profile-package.json.tpl + +grep -q 'REPO_DIR="${SCRIPT_DIR}"' install.sh +grep -q 'DSH_VERSION=0.1.0-rc.8' .env.example +grep -q 'ExecStart=/bin/bash __DEPLOY_DIR__/dsh-autorestart.sh' \ + systemd/dsh-autorestart.service.tpl +grep -q 'Environment=DSH_HOME=/home/__DSH_USER__/.dsh' \ + systemd/dsh-autorestart.service.tpl +grep -q 'proxy_set_header Authorization "";' nginx/dsh.conf.tpl + +if grep -Rqs 'patch-realtime' README.md install.sh scripts systemd templates; then + echo 'core patch reference found in the deployment path' >&2 + exit 1 +fi +if grep -Rqs '@deepseek-ai/dsh@latest' install.sh scripts; then + echo 'implicit latest core install found' >&2 + exit 1 +fi + +echo 'static smoke checks passed'