diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..9933f8f --- /dev/null +++ b/.gitattributes @@ -0,0 +1,10 @@ +deployable/*.yaml text eol=lf +deployable/*.md text eol=lf +deployable/*.example text eol=lf +deployable/Dockerfile* text eol=lf +deployable/scripts/* text eol=lf +deployable/.dockerignore text eol=lf +deployable/** whitespace=cr-at-eol +deployable/relay/** -text +deployable/requirements.lock -text +deployable/pyproject.toml -text diff --git a/.gitignore b/.gitignore index 79df8b0..3e081cb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ __pycache__/ +.pytest_cache/ *.pyc *.log .env @@ -13,3 +14,9 @@ device.pem device.json hainuo-native.json registration.json +!deployable/.env.example +deployable/settings.env +deployable/runtime/ +deployable/backups/ +deployable/wheelhouse/ +deployable/images/ diff --git a/DEPLOYMENT_DELIVERY.md b/DEPLOYMENT_DELIVERY.md new file mode 100644 index 0000000..a121078 --- /dev/null +++ b/DEPLOYMENT_DELIVERY.md @@ -0,0 +1,16 @@ +# 本地部署项目交付说明 + +日期:2026-10-27。 + +- 新环境部署代码入口:`deployable/README.md`。 +- 本地离线包文件名:`hainuo-relay-deployable-arm64-20261027.tar.gz`(独立于 Git 仓库交付)。 +- 字节数:233831757。 +- SHA256:`3f12b00cf3e6e344a7eba1f884d60a6a55ad6c6f42ef81ac59897b7640af40de`。 +- 附带同名 `.sha256` 文件;包内 `OFFLINE_SHA256SUMS` 用于逐文件复核。 +- 离线包使用新 deployable 源码,配套 4 个 Linux ARM64 基础镜像及与 requirements.lock 全部 41 项版本一致的 wheel,保留依赖许可证说明。 +- 依赖文件已与原离线包的 SHA256SUMS 校验,镜像 config 已检查 linux/arm64。 +- 不包含实际 settings.env、.env、运行密钥、设备身份、用户 token 或数据库。 + +本地整理和测试结果见 `deployable/VALIDATION_REPORT.md`。Docker 构建及新环境业务验收尚未执行,不能将依赖校验等同于安装验收通过。 + +本次按用户要求整理本地仓库;没有更新 ECS 服务。 diff --git a/README.md b/README.md index eca2e7d..05c0cad 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,11 @@ # ECS 中继服务源码快照 +## 新环境部署入口 + +需要部署服务时,请使用 [deployable/README.md](deployable/README.md),该目录已整理统一源码、完整 Compose、配置模板、首次初始化及运维脚本。下面的快照目录保留原样用于追溯。 + +离线部署需另行取得 ARM64 依赖包;源码仓库不包含镜像二进制或真实运行凭据。实机验收状态见 [部署检查报告](deployable/VALIDATION_REPORT.md)。 + 本仓库保存 2026-10-27 从 ECS 导出的解压后源码,共 316 个原始文件,包括运行容器源码、主机源码、部署参考文件和历史补丁源码。 ## 来源与校验 diff --git a/deployable/.dockerignore b/deployable/.dockerignore new file mode 100644 index 0000000..a502ecb --- /dev/null +++ b/deployable/.dockerignore @@ -0,0 +1,10 @@ +**/__pycache__ +*.pyc +.env* +settings.env +runtime +operator +backups +images +tests +.git diff --git a/deployable/.env.example b/deployable/.env.example new file mode 100644 index 0000000..b28585a --- /dev/null +++ b/deployable/.env.example @@ -0,0 +1,17 @@ +# Copy to settings.env; never commit settings.env or .env. +# Change this to your own public HTTPS address (hostname or IP, no trailing slash). +HN_PUBLIC_URL=https://relay.example.invalid +# Private Work platform; change for another organization. +HN_FEISHU_API_BASE=https://open.w.cnooc.com.cn +HN_FEISHU_AUTHORIZE_URL=https://open.w.cnooc.com.cn/open-apis/authen/v1/authorize +HN_FEISHU_APP_ID= +HN_FEISHU_APP_SECRET= +HN_FEISHU_ENABLED=true +HN_OAUTH_ENABLED=true +# Scope names must also be enabled for your application and consented by each user. +HN_FEISHU_OAUTH_SCOPES=calendar:calendar calendar:calendar:readonly contact:contact.base:readonly contact:user:search wiki:wiki:readonly docx:document:readonly +HN_REQUEST_TIMEOUT=180 +HN_LISTEN_ADDRESS=0.0.0.0 +HN_HTTPS_PORT=443 +COMPOSE_PROJECT_NAME=hainuo-relay +HN_IMAGE=hainuo-relay:deploy-20261027 diff --git a/deployable/ACCEPTANCE.md b/deployable/ACCEPTANCE.md new file mode 100644 index 0000000..ae8acb5 --- /dev/null +++ b/deployable/ACCEPTANCE.md @@ -0,0 +1,20 @@ +# 新环境业务验收清单 + +记录日期、主机架构、源码提交、镜像 ID、操作者、证据路径。日志不要包含 token、App Secret、OAuth code/state 或私钥。截图对绑定码和身份信息脱敏。 + +| 项目 | 操作 | 通过证据 | +| --- | --- | --- | +| 初始化 | 空环境按 README 构建、配置、初始化 | 新密钥生成、权限正确;第二次 init 被拒绝;无凭据打印 | +| 容器 | check、start、status | 7 个服务稳定运行;数据库与签发服务健康;无反复重启 | +| 平台连接 | 查看 longconn 日志,向机器人发送消息 | platform_connected、事件接收与实际机器人回复 | +| 注册绑定 | 两位测试用户在不同 PC 安装客户端,分别领取配对码 | 两组不同 owner/host_id,均在线;不需逐台管理员审批 | +| 指令收发 | 各自发送 `/主机名`、只读自然语言任务 | 各自返回本机真实结果,实际工具调用有后台记录 | +| 用户隔离 | A/B 同时发送不同标记;分别断开其中一台 PC | 不串设备/回复;离线用户提示离线,另一用户仍可用 | +| 本人授权 | 从客户端发起 OAuth,完成回调 | owner 校验、领取成功;token 不出现在日志/浏览器持久存储 | +| 只读业务 | 主日历、搜索已知用户、读取本人有权访问的云文档 | 原始 API 成功结果与实际内容匹配;无权限文档拒绝 | +| 重启恢复 | 正常停止并启动容器,客户端重连 | 绑定仍在、数据库持久化、机器人收发恢复 | +| 备份恢复 | 隔离环境恢复并重新验证 | 数据、绑定及证书对应一致;不是仅检查备份文件存在 | + +真实创建日程和发送邀请会影响其他人,必须另行确认具体对象及时间后验收。当前清单不自动执行写入测试。 + +本地语法检查和容器 ready 都不能代替以上业务验收;未执行应记“未验证”,不能记通过。 diff --git a/deployable/Dockerfile b/deployable/Dockerfile new file mode 100644 index 0000000..cd582a5 --- /dev/null +++ b/deployable/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.12-slim-bookworm +ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 +WORKDIR /app +COPY requirements.lock pyproject.toml ./ +COPY relay ./relay +RUN pip install --no-cache-dir -r requirements.lock +USER 10001:10001 +ENTRYPOINT ["python", "-m", "relay.cli"] diff --git a/deployable/Dockerfile.offline b/deployable/Dockerfile.offline new file mode 100644 index 0000000..8af3dff --- /dev/null +++ b/deployable/Dockerfile.offline @@ -0,0 +1,10 @@ +FROM python:3.12-slim-bookworm +ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 +WORKDIR /app +COPY wheelhouse /wheels +COPY requirements.lock pyproject.toml ./ +COPY relay ./relay +RUN pip install --no-index --find-links=/wheels --no-cache-dir -r requirements.lock && rm -rf /wheels +RUN chmod -R a+rX /app +USER 10001:10001 +ENTRYPOINT ["python", "-m", "relay.cli"] diff --git a/deployable/README.md b/deployable/README.md new file mode 100644 index 0000000..bf7e0f1 --- /dev/null +++ b/deployable/README.md @@ -0,0 +1,141 @@ +# 海诺中继服务部署项目 + +本目录用于新环境部署,源码对应 2026-10-27 ECS connector/router/longconn 导出版本。协议和业务源码保持原样。仓库其他快照目录用于追溯,不需要手工合并。 + +## 环境要求 + +- Linux ARM64 是当前内网目标;Docker Engine、Compose 插件 v2.20+(支持 `up --wait`)。Docker 需由可信管理员操作。 +- 预留 443/TCP,客户端能访问该地址;服务器能直连私有化 Work HTTPS 和其返回的 WebSocket 地址。 +- PostgreSQL、Redis、自动签发服务只在 Docker 网络内开放;管理员接口仅绑定服务器回环地址 127.0.0.1:18081。 +- Docker 和 Compose 本身不包含在离线依赖包内。默认未设 `platform`,在线 AMD64 构建理论可用,但没有实际部署验收。 +- `.env.example` 中平台地址是当前私有化环境默认值,其他单位必须替换。代理仅在本项目应用容器内禁用,不改宿主机代理。 + +## 一、取得代码和依赖 + +联网部署: + +```bash +git clone git@git.yssing.cloud:ex_tianzy/ecs----.git +cd ecs----/deployable +bash scripts/relayctl.sh build online +``` + +内网部署:使用另行交付的完整离线包(含本目录最新代码、`images/`、`wheelhouse/` 和校验文件),而不是旧版 0.1.0 离线包中的旧源码。 + +```bash +tar -xzf hainuo-relay-deployable-arm64-20261027.tar.gz +cd hainuo-relay-deployable +bash scripts/load-offline.sh +bash scripts/relayctl.sh build offline +``` + +如需自行准备离线依赖,在联网的 Linux ARM64/Python 3.12 环境运行 `bash scripts/prepare-offline.sh`,任何下载或版本失败会停止,不自动替换版本。 + +## 二、填写平台和网关配置 + +```bash +umask 077 +cp .env.example settings.env +chmod 600 settings.env +vi settings.env +mkdir -p runtime/tls +chmod 700 runtime/tls +``` + +必须填写 `HN_PUBLIC_URL`、平台 API/授权地址、App ID、App Secret。公网 URL 示例是占位符,初始化会拒绝。IP 地址可以使用,证书必须包含该 IP 的 SAN。非 443 时公开 URL 中的端口必须与 `HN_HTTPS_PORT` 一致。 + +将组织认可的网关证书链和对应私钥放入: + +```text +runtime/tls/server.pem +runtime/tls/server.key +``` + +证书必须匹配 `HN_PUBLIC_URL` 的主机名/IP,且在有效期内。初始化检查证书与私钥匹配、SAN、有效期;客户端信任链仍须按组织规范配置。不要关闭 TLS 校验。设备 CA 与 HTTPS 网关证书是两套用途不同的证书。 + +只做隔离测试时可使用后文 `--test-cert`;它生成 30 天自签证书,客户端仍须显式信任该测试证书。不能作为正式证书交付。 + +私有化应用后台需配置: + +1. 机器人可用范围及消息接收事件 `im.message.receive_v1`,事件接收方式为 SDK 长连接。 +2. OAuth 回调为 `${HN_PUBLIC_URL}/oauth/cb`,例如 `https://relay.company.example/oauth/cb`。 +3. 按实际业务启用日历、用户搜索、文档、知识库等权限;`.env.example` 的 scope 只是请求范围,不等于平台已经授予。 +4. 按平台规则让应用配置对测试用户生效,再由各用户完成本人授权。新增权限后可能需要重新授权。 + +配对码仍由用户在机器人中获取,再输入其 Windows Work 连接组件;Windows 安装包单独交付,本目录不会安装桌面 Agent。 + +## 三、首次初始化及启动 + +```bash +# offline 换成 online 即联网模式;不需要联网下载时必须用 offline。 +bash scripts/relayctl.sh init offline +bash scripts/relayctl.sh check offline +bash scripts/relayctl.sh start offline +bash scripts/relayctl.sh status offline +bash scripts/relayctl.sh logs offline longconn connector +``` + +仅测试且未提供证书时:`bash scripts/relayctl.sh init offline --test-cert`。 + +初始化容器以 root 运行一次,为服务 UID 10001 设置私钥权限;常驻应用以 UID 10001 运行。生成新密码、设备 CA、连接器签名密钥和 `.env`,不输出凭据。自动签发 CA 私钥仅挂载到内部签发服务,connector/router 无法读取。不要重复初始化;已有 `.env` 或连接器私钥时拒绝覆盖。中途失败需先检查部分生成文件,不要在已有业务的目录中删除密钥后重新初始化。 + +默认镜像名 `hainuo-relay:deploy-20261027`。自定义镜像名时首次构建及初始化命令设置相同 `HN_BUILD_IMAGE`,并在 `settings.env` 中设置相同 `HN_IMAGE`;后续构建读取 `.env` 中的 `HN_IMAGE`。 + +`start` 的成功表示容器健康检查通过,不代表机器人和 PC 业务成功。日志应出现 `platform_connected`,然后必须完成验收清单。 + +## 四、验收 + +参见 [ACCEPTANCE.md](ACCEPTANCE.md)。当前本地 Windows 没有 Docker Engine,尚未完成此目录的干净 ARM64 主机部署验收;本地检查结果见 [VALIDATION_REPORT.md](VALIDATION_REPORT.md)。 + +## 五、日常维护 + +```bash +bash scripts/relayctl.sh status offline +bash scripts/relayctl.sh logs offline longconn connector router +bash scripts/relayctl.sh stop offline +bash scripts/relayctl.sh start offline +bash scripts/relayctl.sh backup offline +``` + +停止不会删除数据库卷。不要执行 `docker compose down -v`,这会删除元数据卷。无需重复传递多份历史 Compose override,统一配置在本目录 `compose.yaml`。 + +配置修改:保留 `.env` 备份,在服务器修改 `.env` 后重新 `check`、`start`。`settings.env` 只在首次初始化读取,修改它不会更新已有 `.env`。不得修改已有数据库密码而不同时修改 PostgreSQL 中的角色密码;不得重新生成 CA 或连接器密钥来处理普通配置问题。 + +## 六、升级与回滚 + +保留 `.env`、`runtime/`、`backups/` 和数据库卷,将新源码与匹配依赖放入当前部署目录后执行: + +```bash +bash scripts/relayctl.sh update offline +``` + +脚本先备份,再给当前应用镜像打 `hainuo-relay:rollback-时间` 标签,然后构建和启动。失败会停下,旧镜像标签已打印;日志和备份保留,不自动猜测修复。离线升级不替换数据库/Redis基础镜像;如基础镜像更新,应单独安排维护。 + +```bash +bash scripts/relayctl.sh rollback offline hainuo-relay:rollback-实际时间 +``` + +回滚后按提示把 `.env` 的 `HN_IMAGE` 改成旧镜像标签,再检查业务。此操作只回滚应用镜像,不回滚配置、数据库结构或已有数据;涉及数据库变更时需遵循版本迁移说明和恢复流程,不能直接承诺镜像回滚兼容。 + +## 七、备份和恢复 + +`backup` 使用 PostgreSQL 自洽逻辑转储,并归档 `.env` 与 `runtime/`,保存到 `backups/时间/`。这些备份含真实密钥及 App Secret,权限 700/600,必须加密保管;不要上传 Git。备份时会话及在途任务不作恢复保证,建议业务空闲时执行。 + +恢复必须在维护窗口进行: + +1. 校验备份 `SHA256SUMS`,保留当前数据的独立备份;确认镜像版本兼容。 +2. 停止本项目服务,向新的空目录恢复同版本源码,解压配置和密钥,按原样保留 UID 10001 权限;不要运行 init。 +3. 为隔离恢复测试设置新的 Compose 项目名、公开地址和端口;正式原机恢复保留原值。 +4. 启动 PostgreSQL:`docker compose -f compose.yaml -f compose.offline.yaml up -d postgres`;等待健康后向**空数据库**恢复:`docker compose -f compose.yaml -f compose.offline.yaml exec -T postgres pg_restore -U hainuo -d hainuo --no-owner --exit-on-error < backups/实际时间/metadata.dump`。 +5. 再执行 `check`、`start` 和业务验收。不要把 dump 直接灌入有业务数据的库;非空库覆盖恢复需要专门的维护方案。 + +备份脚本通过不代表恢复成功;当前恢复流程未在真实数据库上验收。 + +## 已知边界 + +- 新环境是新的设备 CA 与绑定数据库,已有 PC 不能默认沿用旧服务身份。迁移旧环境必须协调原 CA、连接器密钥、数据库及客户端地址,不能用首次初始化替代迁移。 +- H5 是测试入口,尚未成为正式移动端产品。 +- 用户 token 由本人授权获得;不把 tenant token 当用户 token,也不随代码交付。 +- 文档/日历/联系人能力受应用权限、个人授权、平台版本及现有业务白名单限制,不保证任意平台 API 可调用。 +- 自动注册没有逐台管理员审批;持包并能取得机器人配对码的范围由私有化应用可用范围控制。 +- 不包含向量库、向量检索以及未经验证的新功能。 diff --git a/deployable/SOURCE_ORIGIN.md b/deployable/SOURCE_ORIGIN.md new file mode 100644 index 0000000..9d610e7 --- /dev/null +++ b/deployable/SOURCE_ORIGIN.md @@ -0,0 +1,6 @@ +# 源码来源 + +应用源码来自本仓库 host-code/relay,与导出的 connector、router、longconn 源码逐文件一致。 +enrollment-signer 使用同一版本:其入口及签发所需 crypto/enrollment/common 模块与导出签发容器一致,旧签发镜像中的其他业务模块不作为版本基线。 +应用协议及业务模块没有修改。新增内容仅为部署配置、初始化和运维工具。 +原始归档及各目录继续保留用于核对;具体已部署记录见仓库根目录 deployed-services.tsv。 diff --git a/deployable/VALIDATION_REPORT.md b/deployable/VALIDATION_REPORT.md new file mode 100644 index 0000000..347c4ea --- /dev/null +++ b/deployable/VALIDATION_REPORT.md @@ -0,0 +1,50 @@ +# 部署项目整理与检查报告 + +日期:2026-10-27。范围:本地 Git 仓库新增 `deployable/`,不修改原始导出源码及当前 ECS 服务。 + +## 已完成 + +| 项目 | 结果及证据 | +| --- | --- | +| 原始快照保留 | 316 个原文件按根目录 SOURCE_SHA256SUMS.json 逐个校验一致 | +| 业务源码 | deployable/relay 与 host-code/relay 逐文件一致;协议、加密、绑定及 API 白名单未改 | +| 构建入口 | Dockerfile、Dockerfile.offline、relay、requirements.lock、pyproject.toml 位于统一目录 | +| Compose | 合并为 7 个服务;无 REDACTED 占位;CA/连接器路径、数据库名和内部服务地址恢复为明确值 | +| 配置模板 | .env.example 可提交;settings.env、.env、运行密钥、备份及依赖二进制被 Git 排除 | +| 自动签发 | 内部 enrollment-signer,无宿主端口;只有该服务挂载 CA 私钥 | +| 首次初始化 | 新随机密码、新 CA、新连接器密钥;拒绝覆盖已有部署;证书私钥匹配、SAN、有效期检查 | +| Work 和 OAuth | 长连接默认启动,用户授权开启;应用凭据由部署者提供;个人授权仍需用户操作 | +| 宿主机代理 | 本项目应用容器显式直连,保留已有业务源码的 trust_env=False 行为;宿主机代理不改 | +| 运维 | 构建、初始化、检查、启动、状态、日志、停止、备份、升级、镜像回滚入口 | +| 文档 | 新环境在线/离线部署、配置修改、备份恢复、升级限制及业务验收清单 | + +## 本地执行结果 + +1. 通过新增部署测试及现有源码测试:**60 passed,1 skipped**。跳过项为未提供真实 PostgreSQL 测试连接的用例;其他用例使用本地 HTTP/WebSocket、FakeRedis/平台桩,不冒充真实平台验收。 +2. 新增 6 项部署测试覆盖:IP SAN 证书生成、独立随机凭据、防覆盖、缺证书拒绝、证书主机名不符拒绝、模板占位拒绝、Compose 内部签发边界及构建输入检查。 +3. 在 WSL Bash 中执行 `bash -n`,relayctl.sh、load-offline.sh、prepare-offline.sh 语法通过。 +4. 新增 Python 文件 AST 语法检查通过。已修正 Python 3.12 不提供 ssl.match_hostname 的兼容问题,部署工具自行检查 SAN;业务 TLS 验证未关闭。 +5. 原始 316 个文件哈希校验通过,新部署业务源码没有修改。 + +可复核命令(在仓库根目录,测试环境需安装 pytest、pytest-asyncio、fakeredis、PyYAML 等锁定依赖): + +```bash +PYTHONPATH=deployable python -m pytest deployable/tests deployment-source/tests -q +bash -n deployable/scripts/relayctl.sh +bash -n deployable/scripts/load-offline.sh +bash -n deployable/scripts/prepare-offline.sh +``` + +## 未验证项目 + +- 本机没有 Docker Engine,未运行 `docker compose config`、镜像构建或容器启动;当前 YAML 检查是静态解析,不等同于 Compose 实际校验。 +- 新目录在干净 Linux ARM64 环境中的 UID 权限、容器健康、PostgreSQL/Redis 连接、网关 TLS、长连接重连尚待实机验证。 +- 真实机器人消息、PC 注册绑定、双用户隔离、OAuth 和平台业务 API 未针对本次新部署目录重新验收。 +- 数据备份和恢复、升级及镜像回滚未在真实 Docker 环境执行;镜像回滚不保证跨数据库结构变更兼容。 +- 未创建真实日程或发送邀请。 + +## 交付判断 + +代码已整理为有配置模板、统一构建/运维入口和部署步骤的项目;部署者仍需提供 Docker、平台凭据与权限、网络和受信任网关证书。此报告不将本地通过项等同于新环境部署验收通过。按 ACCEPTANCE.md 完成实机验收后才能作最终投产结论。 + +本次不包含向量检索或其他未验收功能。 diff --git a/deployable/compose.offline.yaml b/deployable/compose.offline.yaml new file mode 100644 index 0000000..3427db1 --- /dev/null +++ b/deployable/compose.offline.yaml @@ -0,0 +1,19 @@ +services: + postgres: + pull_policy: never + redis: + pull_policy: never + connector: + pull_policy: never + build: + context: . + dockerfile: Dockerfile.offline + network: none + router: + pull_policy: never + edge: + pull_policy: never + longconn: + pull_policy: never + enrollment-signer: + pull_policy: never diff --git a/deployable/compose.yaml b/deployable/compose.yaml new file mode 100644 index 0000000..a1ee2eb --- /dev/null +++ b/deployable/compose.yaml @@ -0,0 +1,294 @@ +name: ${COMPOSE_PROJECT_NAME:-hainuo-relay} +services: + postgres: + image: postgres:17-alpine + restart: unless-stopped + environment: + POSTGRES_DB: hainuo + POSTGRES_USER: hainuo + POSTGRES_PASSWORD: ${HN_POSTGRES_PASSWORD:?Initialize first} + volumes: + - metadata:/var/lib/postgresql/data + networks: + - backend + healthcheck: + test: + - CMD-SHELL + - pg_isready -U hainuo -d hainuo + interval: 10s + timeout: 3s + retries: 5 + mem_limit: 512m + security_opt: + - no-new-privileges:true + redis: + image: redis:8.2-alpine + restart: unless-stopped + environment: + REDIS_PASSWORD: ${HN_REDIS_PASSWORD} + command: + - sh + - -c + - exec redis-server --save '' --appendonly no --requirepass "$$REDIS_PASSWORD" + --maxmemory 256mb --maxmemory-policy noeviction + networks: + - backend + healthcheck: + test: + - CMD-SHELL + - REDISCLI_AUTH="$$REDIS_PASSWORD" redis-cli ping | grep -q PONG + interval: 10s + timeout: 3s + retries: 5 + mem_limit: 384m + security_opt: + - no-new-privileges:true + connector: + image: ${HN_IMAGE:-hainuo-relay:deploy-20261027} + build: + context: . + dockerfile: Dockerfile + restart: unless-stopped + command: + - serve + - connector + - --host + - 0.0.0.0 + - --port + - '8000' + environment: + HN_ENVIRONMENT: ${HN_ENVIRONMENT} + HN_REDIS_URL: ${HN_REDIS_URL} + HN_ADMIN_SECRET: ${HN_ADMIN_SECRET} + HN_INTERNAL_SECRET: ${HN_INTERNAL_SECRET} + HN_DATA_DIR: /data + HN_DATABASE_URL: ${HN_DATABASE_URL} + HN_CA_CERT: /trust/ca.pem + HN_CONNECTOR_KEY: /data/connector.key + HN_PUBLIC_URL: ${HN_PUBLIC_URL} + HN_ROUTER_URL: ${HN_ROUTER_URL} + HN_DEVICE_DOMAIN: ${HN_DEVICE_DOMAIN} + HN_FEISHU_ENABLED: ${HN_FEISHU_ENABLED} + HN_FEISHU_API_BASE: ${HN_FEISHU_API_BASE} + HN_FEISHU_AUTHORIZE_URL: ${HN_FEISHU_AUTHORIZE_URL} + HN_FEISHU_APP_ID: ${HN_FEISHU_APP_ID} + HN_FEISHU_APP_SECRET: ${HN_FEISHU_APP_SECRET} + HN_FEISHU_ENCRYPT_KEY: ${HN_FEISHU_ENCRYPT_KEY} + HN_FEISHU_VERIFICATION_TOKEN: ${HN_FEISHU_VERIFICATION_TOKEN} + HN_PROXY_ENABLED: ${HN_PROXY_ENABLED} + HN_CONNECTOR_PUBLIC_KEY: /trust/connector.pub + HN_FEISHU_EVENT_SOURCE: longconn + HN_OAUTH_ENABLED: ${HN_OAUTH_ENABLED:-true} + HN_FEISHU_OAUTH_SCOPES: ${HN_FEISHU_OAUTH_SCOPES:-} + HN_ENROLLMENT_SIGNER_URL: http://enrollment-signer:8000 + HN_REQUEST_TIMEOUT: ${HN_REQUEST_TIMEOUT:-180} + HTTP_PROXY: '' + HTTPS_PROXY: '' + ALL_PROXY: '' + http_proxy: '' + https_proxy: '' + all_proxy: '' + NO_PROXY: '*' + no_proxy: '*' + volumes: + - ./runtime/connector:/data + - ./runtime/trust:/trust:ro + ports: + - 127.0.0.1:18081:8000 + networks: + - backend + - edge + depends_on: + redis: + condition: service_healthy + postgres: + condition: service_healthy + enrollment-signer: + condition: service_healthy + healthcheck: + test: + - CMD + - python + - -c + - import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/ready', + timeout=3) + interval: 15s + timeout: 5s + retries: 3 + read_only: true + tmpfs: + - /tmp:size=16m,noexec,nosuid + cap_drop: + - ALL + security_opt: + - no-new-privileges:true + mem_limit: 768m + router: + image: ${HN_IMAGE:-hainuo-relay:deploy-20261027} + restart: unless-stopped + command: + - serve + - router + - --host + - 0.0.0.0 + - --port + - '8000' + environment: + HN_ENVIRONMENT: ${HN_ENVIRONMENT} + HN_REDIS_URL: ${HN_REDIS_URL} + HN_CA_CERT: /trust/ca.pem + HN_PUBLIC_URL: ${HN_PUBLIC_URL} + HN_ROUTER_URL: ${HN_ROUTER_URL} + HN_CONNECTOR_PUBLIC_KEY: /trust/connector.pub + HTTP_PROXY: '' + HTTPS_PROXY: '' + ALL_PROXY: '' + http_proxy: '' + https_proxy: '' + all_proxy: '' + NO_PROXY: '*' + no_proxy: '*' + volumes: + - ./runtime/trust:/trust:ro + networks: + - backend + - edge + depends_on: + connector: + condition: service_healthy + healthcheck: + test: + - CMD + - python + - -c + - import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/ready', + timeout=3) + interval: 15s + timeout: 5s + retries: 3 + read_only: true + tmpfs: + - /tmp:size=16m,noexec,nosuid + cap_drop: + - ALL + security_opt: + - no-new-privileges:true + mem_limit: 512m + edge: + image: caddy:2-alpine + restart: unless-stopped + ports: + - ${HN_LISTEN_ADDRESS:-0.0.0.0}:${HN_HTTPS_PORT:-443}:8443 + volumes: + - ./deploy/Caddyfile:/etc/caddy/Caddyfile:ro + - ./runtime/tls:/tls:ro + networks: + - edge + depends_on: + connector: + condition: service_healthy + router: + condition: service_healthy + read_only: true + tmpfs: + - /data:size=16m + - /config:size=16m + - /tmp:size=16m + cap_drop: + - ALL + security_opt: + - no-new-privileges:true + mem_limit: 128m + cap_add: + - NET_BIND_SERVICE + longconn: + image: ${HN_IMAGE:-hainuo-relay:deploy-20261027} + restart: unless-stopped + entrypoint: + - python + - -m + - relay.longconn + environment: + HN_ENVIRONMENT: ${HN_ENVIRONMENT} + HN_REDIS_URL: ${HN_REDIS_URL} + HN_INTERNAL_SECRET: ${HN_INTERNAL_SECRET} + HN_PUBLIC_URL: ${HN_PUBLIC_URL} + HN_ROUTER_URL: ${HN_ROUTER_URL} + HN_FEISHU_ENABLED: ${HN_FEISHU_ENABLED} + HN_FEISHU_API_BASE: ${HN_FEISHU_API_BASE} + HN_FEISHU_APP_ID: ${HN_FEISHU_APP_ID} + HN_FEISHU_APP_SECRET: ${HN_FEISHU_APP_SECRET} + HN_FEISHU_ENCRYPT_KEY: ${HN_FEISHU_ENCRYPT_KEY} + HN_FEISHU_VERIFICATION_TOKEN: ${HN_FEISHU_VERIFICATION_TOKEN} + HN_CONNECTOR_INTERNAL_URL: http://connector:8000 + HN_CA_CERT: /trust/ca.pem + HN_CONNECTOR_PUBLIC_KEY: /trust/connector.pub + HN_FEISHU_EVENT_SOURCE: longconn + HTTP_PROXY: '' + HTTPS_PROXY: '' + ALL_PROXY: '' + http_proxy: '' + https_proxy: '' + all_proxy: '' + NO_PROXY: '*' + no_proxy: '*' + networks: + - backend + - edge + depends_on: + connector: + condition: service_healthy + read_only: true + tmpfs: + - /tmp:size=16m,noexec,nosuid + cap_drop: + - ALL + security_opt: + - no-new-privileges:true + mem_limit: 512m + enrollment-signer: + image: ${HN_IMAGE:-hainuo-relay:deploy-20261027} + entrypoint: + - python + - -m + - relay.enrollment_signer + restart: unless-stopped + environment: + HN_INTERNAL_SECRET: ${HN_INTERNAL_SECRET} + HTTP_PROXY: '' + HTTPS_PROXY: '' + ALL_PROXY: '' + http_proxy: '' + https_proxy: '' + all_proxy: '' + NO_PROXY: '*' + no_proxy: '*' + volumes: + - ./runtime/issuer:/issuer:ro + - ./runtime/trust:/trust:ro + networks: + - backend + read_only: true + tmpfs: + - /tmp:size=16m,noexec,nosuid + cap_drop: + - ALL + security_opt: + - no-new-privileges:true + mem_limit: 128m + healthcheck: + test: + - CMD + - python + - -c + - import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/ready', + timeout=3) + interval: 10s + timeout: 5s + retries: 5 +networks: + backend: + internal: true + edge: {} +volumes: + metadata: {} diff --git a/deployable/deploy/Caddyfile b/deployable/deploy/Caddyfile new file mode 100644 index 0000000..89ddb25 --- /dev/null +++ b/deployable/deploy/Caddyfile @@ -0,0 +1,22 @@ +{ + admin off + auto_https off +} +:8443 { + tls /tls/server.pem /tls/server.key + # No access log: OAuth query strings and credentials must never be logged. + @private path /admin/* /internal/* /ready + respond @private 404 + handle /device/ws { + reverse_proxy router:8000 + } + handle { + reverse_proxy connector:8000 + } + header { + Strict-Transport-Security "max-age=31536000" + X-Content-Type-Options nosniff + Referrer-Policy no-referrer + -Server + } +} diff --git a/deployable/pyproject.toml b/deployable/pyproject.toml new file mode 100644 index 0000000..3bf5e2c --- /dev/null +++ b/deployable/pyproject.toml @@ -0,0 +1,32 @@ +[build-system] +requires = ["setuptools>=75"] +build-backend = "setuptools.build_meta" + +[project] +name = "hainuo-relay" +version = "0.1.0" +description = "Hainuo isolated connector and encrypted device router" +requires-python = ">=3.12" +dependencies = [ + "fastapi==0.119.1", "uvicorn==0.38.0", "httpx==0.28.1", + "redis==6.4.0", "cryptography==46.0.3", "websockets==15.0.1", + "pydantic-settings==2.11.0", + "psycopg[binary]==3.2.12", +] + +[project.optional-dependencies] +test = ["pytest==8.4.2", "pytest-asyncio==1.2.0", "fakeredis==2.32.0"] +longconn = ["lark-oapi==1.5.3"] + +[project.scripts] +hainuo-relay = "relay.cli:main" + +[tool.setuptools.packages.find] +include = ["relay*"] + +[tool.setuptools.package-data] +relay = ["web/*.html", "web/*.js", "web/*.css"] + +[tool.pytest.ini_options] +asyncio_mode = "auto" +testpaths = ["tests"] diff --git a/deployable/relay/__init__.py b/deployable/relay/__init__.py new file mode 100644 index 0000000..32754a7 --- /dev/null +++ b/deployable/relay/__init__.py @@ -0,0 +1 @@ +"""Hainuo relay 0.1.0.""" diff --git a/deployable/relay/cli.py b/deployable/relay/cli.py new file mode 100644 index 0000000..fad7979 --- /dev/null +++ b/deployable/relay/cli.py @@ -0,0 +1,216 @@ +import argparse +import asyncio +import json +import os +import secrets +import ssl +import uuid +from datetime import datetime, timedelta, timezone +from pathlib import Path + +import httpx +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ed25519 +from cryptography.x509.oid import NameOID + +from .crypto import issue_ca, issue_device, load_private, pem_private + + +def write_new(path, data, private=False): + path.parent.mkdir(parents=True, exist_ok=True) + with path.open("xb") as f: + f.write(data if isinstance(data, bytes) else data.encode()) + if private and os.name != "nt": + path.chmod(0o600) + + +def init_project(root: Path, hostname: str): + if (root / ".env").exists(): + raise RuntimeError(".env exists; init never overwrites credentials") + ca_key, ca = issue_ca() + signer = ed25519.Ed25519PrivateKey.generate() + write_new(root / "operator/ca.key", pem_private(ca_key), True) + write_new(root / "runtime/trust/ca.pem", ca.public_bytes(serialization.Encoding.PEM)) + write_new(root / "runtime/connector/connector.key", pem_private(signer), True) + write_new(root / "runtime/trust/connector.pub", signer.public_key().public_bytes( + serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo)) + tls_key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, hostname)]) + now = datetime.now(timezone.utc) + tls = (x509.CertificateBuilder().subject_name(name).issuer_name(name).public_key(tls_key.public_key()) + .serial_number(x509.random_serial_number()).not_valid_before(now - timedelta(minutes=5)) + .not_valid_after(now + timedelta(days=30)) + .add_extension(x509.SubjectAlternativeName([x509.DNSName(hostname)]), critical=False) + .sign(tls_key, algorithm=None)) + write_new(root / "runtime/tls/server.key", pem_private(tls_key), True) + write_new(root / "runtime/tls/server.pem", tls.public_bytes(serialization.Encoding.PEM)) + redis_secret = secrets.token_hex(32) + db_secret = secrets.token_hex(32) + env = (f"HN_ENVIRONMENT=development\nHN_PUBLIC_HOST={hostname}\nHN_PUBLIC_URL=https://{hostname}\n" + f"HN_ROUTER_URL=wss://{hostname}/device/ws\nHN_ADMIN_SECRET={secrets.token_hex(32)}\n" + f"HN_INTERNAL_SECRET={secrets.token_hex(32)}\nHN_REDIS_PASSWORD={redis_secret}\n" + f"HN_REDIS_URL=redis://:{redis_secret}@redis:6379/0\n" + f"HN_POSTGRES_PASSWORD={db_secret}\nHN_DATABASE_URL=postgresql://hainuo:{db_secret}@postgres:5432/hainuo\n" + "HN_FEISHU_ENABLED=false\nHN_FEISHU_API_BASE=https://open.feishu.cn\n" + "HN_FEISHU_AUTHORIZE_URL=https://open.feishu.cn/open-apis/authen/v1/authorize\n" + "HN_FEISHU_APP_ID=\nHN_FEISHU_APP_SECRET=\nHN_FEISHU_ENCRYPT_KEY=\n" + "HN_FEISHU_VERIFICATION_TOKEN=\nHN_PROXY_ENABLED=false\nHN_DEVICE_DOMAIN=\n") + write_new(root / ".env", env, True) + print("已生成配置、连接器密钥、设备CA及临时TLS证书;未输出凭据。") + print("operator/ca.key 仅保留在签发工作站,不要上传到ECS。临时TLS证书上线前须替换。") + + +def device_request(directory): + host = uuid.uuid4().hex + private = ed25519.Ed25519PrivateKey.generate() + write_new(directory / "device.key", pem_private(private), True) + public = private.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode() + write_new(directory / "request.json", json.dumps({"host_id": host, "public_key": public}, indent=2)) + print("已在本机生成私钥和 request.json;仅将request.json交给签发工作站。") + + +def issue(root, request_file, output): + request = json.loads(request_file.read_text(encoding="utf-8")) + host = request["host_id"] + if len(host) != 32 or any(c not in "0123456789abcdef" for c in host): + raise ValueError("invalid host identity") + ca = x509.load_pem_x509_certificate((root / "runtime/trust/ca.pem").read_bytes()) + public = serialization.load_pem_public_key(request["public_key"].encode()) + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("Ed25519 required") + cert = issue_device(load_private(root / "operator/ca.key"), ca, host, public) + pem = cert.public_bytes(serialization.Encoding.PEM) + write_new(output / "device.pem", pem) + write_new(output / "ca.pem", (root / "runtime/trust/ca.pem").read_bytes()) + write_new(output / "connector.pub", (root / "runtime/trust/connector.pub").read_bytes()) + write_new(output / "device.json", json.dumps({"host_id": host, "owner": None}, indent=2)) + write_new(output / "registration.json", json.dumps({"host_id": host, "cert": pem.decode()}, indent=2)) + print("已签发设备证书;将device.pem、ca.pem、connector.pub、device.json交回原设备。") + + +def main(): + parser = argparse.ArgumentParser(description="海诺中继部署和设备工具") + sub = parser.add_subparsers(dest="command", required=True) + p = sub.add_parser("init") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--hostname", required=True) + p = sub.add_parser("serve") + p.add_argument("role", choices=["connector", "router"]) + p.add_argument("--host", default="127.0.0.1") + p.add_argument("--port", type=int, default=8000) + p = sub.add_parser("device-request") + p.add_argument("--directory", type=Path, required=True) + p = sub.add_parser("issue-device") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--request", type=Path, required=True) + p.add_argument("--output", type=Path, required=True) + p = sub.add_parser("register") + p.add_argument("--registration", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("bind") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("device") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p.add_argument("--test-root", type=Path) + p.add_argument("--agent-url", help="Explicit loopback read-only Agent adapter endpoint") + p.add_argument("--hainuo-config", type=Path, help="Native installer-1.065 API adapter config; alternative to --agent-url") + p = sub.add_parser("hainuo-probe") + p.add_argument("--config", type=Path, required=True) + p = sub.add_parser("web-pair") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + for command in ("status", "binding-code", "dispatch", "revoke", "unbind", "reconcile"): + p = sub.add_parser(command) + p.add_argument("--url", default="http://127.0.0.1:18081") + p.add_argument("--tls-ca", type=Path) + if command in {"binding-code", "dispatch"}: + p.add_argument("--owner", required=True) + if command == "dispatch": + p.add_argument("--text", required=True) + p.add_argument("--turn-id", default=None) + if command in {"revoke", "unbind"}: + p.add_argument("--host-id", required=True) + args = parser.parse_args() + if args.command == "init": + init_project(args.root, args.hostname) + elif args.command == "serve": + import uvicorn + uvicorn.run(f"relay.{args.role}:create_app", factory=True, host=args.host, port=args.port, + workers=1, access_log=False, proxy_headers=False, + ws_max_size=262144, ws_max_queue=16, log_level="warning") + elif args.command == "device-request": + device_request(args.directory) + elif args.command == "issue-device": + issue(args.root, args.request, args.output) + elif args.command == "register": + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, follow_redirects=False, timeout=15) as client: + response = client.post(args.url.rstrip("/") + "/admin/devices", + headers={"X-Admin-Secret": secret}, + json=json.loads(args.registration.read_text(encoding="utf-8"))) + if response.status_code != 201: + raise RuntimeError(f"register failed: HTTP {response.status_code}") + print("设备登记成功。") + elif args.command == "bind": + import getpass + from .device import bind_device + code = getpass.getpass("Binding code: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + asyncio.run(bind_device(args.directory, args.url, code, tls_verify=context)) + print("绑定成功。") + elif args.command == "device": + from .device import run_device + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else None + asyncio.run(run_device(args.directory, args.url, root=args.test_root, ssl_context=context, + agent_url=args.agent_url, hainuo_config=args.hainuo_config)) + elif args.command == "hainuo-probe": + from .hainuo1065 import Hainuo1065Agent + async def probe(): + adapter = Hainuo1065Agent(args.config) + try: + print(json.dumps(await adapter.probe(), ensure_ascii=False, indent=2)) + finally: + await adapter.close() + asyncio.run(probe()) + elif args.command == "web-pair": + from .device import pair_browser + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + result = asyncio.run(pair_browser(args.directory, args.url, tls_verify=context)) + print("网页地址:" + result["url"]) + print("一次性网页配对码(120秒有效):" + result["code"]) + else: + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, timeout=200, follow_redirects=False) as client: + paths = {"status": "/admin/devices", "binding-code": "/admin/test/binding-code", + "dispatch": "/admin/test/dispatch", "reconcile": "/admin/reconcile"} + if args.command in {"revoke", "unbind"}: + if len(args.host_id) != 32 or any(c not in "0123456789abcdef" for c in args.host_id): + raise ValueError("invalid host identity") + path = f"/admin/devices/{args.host_id}/{args.command}" + else: + path = paths[args.command] + body = None + if args.command in {"binding-code", "dispatch"}: + body = {"open_id": args.owner, "text": getattr(args, "text", "binding")} + if getattr(args, "turn_id", None): + body["turn_id"] = args.turn_id + response = client.request("GET" if args.command == "status" else "POST", args.url.rstrip("/") + path, + headers={"X-Admin-Secret": secret}, json=body) + if response.status_code != 200: + raise RuntimeError(f"operation failed: HTTP {response.status_code}") + print(json.dumps(response.json(), ensure_ascii=False, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/deployable/relay/client_manage.py b/deployable/relay/client_manage.py new file mode 100644 index 0000000..6b543fe --- /dev/null +++ b/deployable/relay/client_manage.py @@ -0,0 +1,293 @@ +"""Per-user Windows connection companion. No platform/application secrets.""" +import argparse +import asyncio +import json +import os +import secrets +import socket +import ssl +import subprocess +import sys +import time +import uuid +from pathlib import Path + +import httpx +import psutil +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey +from .crypto import load_private, load_public, pem_private, raw_public, sign, verified, verify_device +from .windows_keys import protect + +BASE = Path(__file__).resolve().parent.parent +URL = "https://10.77.76.13" + + +def save(path, value): + temp = path.with_suffix(path.suffix + ".tmp") + temp.write_text(json.dumps(value, ensure_ascii=False, indent=2), encoding="utf-8") + os.replace(temp, path) + + +def secure_directory(directory): + directory.mkdir(parents=True, exist_ok=True) + # SID avoids localized account names. Fail closed if ACL protection fails. + result = subprocess.run(["whoami", "/user", "/fo", "csv", "/nh"], capture_output=True, text=True, check=True) + import csv + sid = list(csv.reader([result.stdout.strip()]))[0][1] + subprocess.run(["icacls", str(directory), "/inheritance:r", "/grant:r", f"*{sid}:(OI)(CI)F", "*S-1-5-18:(OI)(CI)F"], + capture_output=True, check=True, creationflags=subprocess.CREATE_NO_WINDOW) + + +def prepare(directory): + secure_directory(directory) + for name in ("server.pem", "ca.pem", "connector.pub"): + target = directory / name + if not target.exists(): + target.write_bytes((BASE / "trust" / name).read_bytes()) + if not (directory / "device.json").exists(): + if (directory / "device.key.dpapi").exists() or (directory / "device.key").exists(): + raise RuntimeError("发现不完整的设备身份,请保留目录并联系管理员恢复。") + private = Ed25519PrivateKey.generate() + (directory / "device.key.dpapi").write_bytes(protect(pem_private(private))) + save(directory / "device.json", {"host_id": uuid.uuid4().hex, "owner": None}) + + +def tls(directory): + return ssl.create_default_context(cafile=str(directory / "server.pem")) + + +def import_existing(directory, source): + """Explicit migration; only public trust and this PC's identity, never app keys.""" + if source.resolve() == directory.resolve(): + raise ValueError("请选择旧设备目录。") + if any((directory / name).exists() for name in ("device.json", "device.key.dpapi", "device.key")): + raise RuntimeError("本目录已有设备身份。请使用已有身份继续,不能用导入覆盖。") + config = json.loads((source / "device.json").read_text(encoding="utf-8-sig")) + for name in ("server.pem", "ca.pem", "connector.pub"): + if (source / name).read_bytes().strip() != (BASE / "trust" / name).read_bytes().strip(): + raise ValueError("旧设备的服务信任与本安装包不符,请联系管理员。") + cert = (source / "device.pem").read_text(encoding="utf-8") + public = verify_device(cert, (source / "ca.pem").read_bytes(), config["host_id"]) + private = load_private(source / "device.key") + if raw_public(public) != raw_public(private.public_key()): + raise ValueError("设备证书与本机私钥不匹配。") + secure_directory(directory) + for name in ("device.pem", "server.pem", "ca.pem", "connector.pub"): + (directory / name).write_bytes((source / name).read_bytes()) + (directory / "device.key.dpapi").write_bytes(protect(pem_private(private))) + save(directory / "device.json", config) + # Preserve local replay journal using SQLite's consistent backup API. + if (source / "journal.sqlite").is_file(): + import sqlite3 + with sqlite3.connect(source / "journal.sqlite") as original, sqlite3.connect(directory / "journal.sqlite") as target: + original.backup(target) + return {"message": "旧设备导入成功,Work绑定已保留。请先停止旧PowerShell桥接窗口,再点击启动连接。"} + + +def enrollment(directory): + prepare(directory) + cfg = json.loads((directory / "device.json").read_text()) + if (directory / "device.pem").exists(): + verify_device((directory / "device.pem").read_text(), (directory / "ca.pem").read_bytes(), cfg["host_id"]) + return {"message": "设备已注册,可在Work获取绑定码后绑定。已有绑定可直接启动连接。"} + with httpx.Client(verify=tls(directory), trust_env=False, timeout=15) as http: + key = load_private(directory / "device.key") + proof = sign({"host_id": cfg["host_id"], "purpose": "device-enroll-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + response = http.post(URL + "/device/register", json={"host_id": cfg["host_id"], "proof": proof, + "public_key": key.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode()}) + if response.status_code not in {200, 201}: + raise RuntimeError("自动注册失败:HTTP " + str(response.status_code) + ";请确认ECS已安装1.067自动注册更新。") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt.get("status") != "registered" or receipt["host_id"] != cfg["host_id"]: + raise ValueError("注册回执身份不符") + public = verify_device(receipt["cert"], (directory / "ca.pem").read_bytes(), cfg["host_id"]) + if raw_public(public) != raw_public(key.public_key()): + raise ValueError("注册证书公钥不符") + temp = directory / "device.pem.tmp" + temp.write_text(receipt["cert"], encoding="utf-8") + os.replace(temp, directory / "device.pem") + (directory / "enrollment.json").unlink(missing_ok=True) + return {"message": "设备已自动注册,无需管理员审批。请在Work获取绑定码,填写后点击绑定并连接。"} + + +def managed_processes(directory): + """Only processes with our absolute entry path AND exact device directory.""" + state = directory / "processes.json" + if not state.exists(): + return [] + result = [] + for item in json.loads(state.read_text()).get("processes", []): + try: + proc = psutil.Process(item["pid"]) + command = proc.cmdline() + if (abs(proc.create_time() - item["created"]) < 0.1 and str(BASE / "entry.py") in command + and str(directory) in command): + result.append(proc) + except psutil.Error: + pass + return result + + +def stop(directory): + procs = managed_processes(directory) + # Supervisor owns and terminates only its own child handles in its finally block. + (directory / "stop.flag").touch() + _, alive = psutil.wait_procs(procs, timeout=8) + for proc in alive: + proc.terminate() + return {"message": "已停止本安装包启动的连接进程。手工启动的旧桥接请在原窗口按Ctrl+C停止。"} + + +def start(directory, root): + from .process_lock import DeviceProcessLock + with DeviceProcessLock(directory / "control"): + return _start(directory, root) + + +def _start(directory, root): + cfg = json.loads((directory / "device.json").read_text()) + if not cfg.get("owner") or not (directory / "device.pem").exists(): + raise RuntimeError("请先完成设备准入和Work绑定。") + if managed_processes(directory): + return {"message": "连接进程已运行,请点击状态查看。"} + (directory / "stop.flag").unlink(missing_ok=True) + log = (directory / "bridge.log").open("a", encoding="utf-8") + proc = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "supervise", "--directory", str(directory), "--root", str(root)], + stdout=log, stderr=log, creationflags=subprocess.CREATE_NO_WINDOW) + log.close() + save(directory / "processes.json", {"processes": [{"pid": proc.pid, "created": psutil.Process(proc.pid).create_time()}]}) + return {"message": "连接进程已启动;约30秒后点击状态,再到Work发送 /主机名 验证。"} + + +def supervise(directory, root): + from .process_lock import DeviceProcessLock + lockdir = directory / "supervisor" + with DeviceProcessLock(lockdir): + children = [] + def record_children(): + processes = [psutil.Process(os.getpid())] + [psutil.Process(p.pid) for p in children if p.poll() is None] + save(directory / "processes.json", {"processes": [{"pid": p.pid, "created": p.create_time()} for p in processes]}) + try: + # Check local bind availability without an HTTP connection. Windows + # filtering can time out connections to a closed port rather than + # refuse them; that must not prevent starting our own listener. + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as port_probe: + try: + if os.name == "nt": + port_probe.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1) + port_probe.bind(("127.0.0.1", 18643)) + except OSError: + raise RuntimeError("原生API端口18643被占用或不允许绑定,请先检查旧连接。") from None + native = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "native", "--directory", str(directory), "--root", str(root)]) + children.append(native) + record_children() + for _ in range(90): + if (directory / "stop.flag").exists(): + return + if native.poll() is not None: + raise RuntimeError("本机原生API启动失败,请检查安装配置。") + try: + from .hainuo1065 import Hainuo1065Agent + async def probe(): + agent = Hainuo1065Agent(directory / "hainuo-native.json") + try: + return await agent.probe() + finally: + await agent.close() + asyncio.run(probe()) + break + except Exception: + time.sleep(1) + else: + raise RuntimeError("本机原生API未就绪。") + bridge = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "bridge", "--directory", str(directory), "--root", str(root)]) + children.append(bridge) + record_children() + while not (directory / "stop.flag").exists(): + if any(proc.poll() is not None for proc in children): + raise RuntimeError("连接子进程已退出,请查看状态后重新启动。") + time.sleep(0.5) + finally: + for proc in reversed(children): + if proc.poll() is None: + proc.terminate() + try: + proc.wait(timeout=5) + except subprocess.TimeoutExpired: + proc.kill() + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("action", choices=["authorize", "import", "enroll", "bind", "start", "stop", "status", "pair", "supervise", "native", "bridge"]) + parser.add_argument("--directory", type=Path, required=True) + parser.add_argument("--root", type=Path, required=True) + args = parser.parse_args() + directory, root = args.directory.resolve(), args.root.resolve() + directory.mkdir(parents=True, exist_ok=True) + try: + if args.action == "authorize": + from .work_client import WorkClient + client = WorkClient(directory) + try: + result = client.status() + result["message"] += ("\n" + result["authorization_url"]) if result.get("authorization_url") else "" + finally: + client.close() + elif args.action == "import": + result = import_existing(directory, Path(sys.stdin.readline().strip())) + elif args.action == "enroll": + result = enrollment(directory) + elif args.action == "bind": + from .device import bind_device + code = sys.stdin.readline().strip() + if not code: + raise ValueError("请填写Work机器人新生成的绑定码。") + enrollment(directory) + stop(directory) + asyncio.run(bind_device(directory, URL, code, tls_verify=tls(directory))) + result = start(directory, root) + result["message"] = "Work绑定成功。" + result["message"] + elif args.action == "start": + result = start(directory, root) + elif args.action == "stop": + result = stop(directory) + elif args.action == "pair": + from .device import pair_browser + receipt = asyncio.run(pair_browser(directory, URL, tls_verify=tls(directory))) + result = {"message": "网页:" + receipt["url"] + "\n一次性配对码(120秒):" + receipt["code"]} + elif args.action == "status": + cfg = json.loads((directory / "device.json").read_text()) if (directory / "device.json").exists() else {} + log = directory / "bridge.log" + tail = log.read_bytes()[-6000:].decode("utf-8", errors="replace") if log.exists() else "" + result = {"message": "设备:" + cfg.get("host_id", "未申请") + "\nWork身份:" + (cfg.get("owner") or "未绑定") + + "\n连接进程:" + ("运行中(请在Work发送 /主机名 确认当前链路)" if managed_processes(directory) else "未运行") + "\n" + tail[-2500:]} + elif args.action == "supervise": + return supervise(directory, root) + elif args.action == "native": + os.environ["HN_WORK_DEVICE_DIR"] = str(directory) + import importlib.util + spec = importlib.util.spec_from_file_location("native_companion", BASE / "hainuo_native_api.py") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + module.enable_profile_tools(root, "hainuo") + key = module.prepare_environment(root, "hainuo", 18643, directory / "hainuo-native.json", enable_tools=True) + import logging + logging.disable(logging.CRITICAL) + return asyncio.run(module.serve_native(18643, key, enable_tools=True)) + else: + from .device import run_device + return asyncio.run(run_device(directory, "wss://10.77.76.13/device/ws", ssl_context=tls(directory), hainuo_config=directory / "hainuo-native.json")) + print(json.dumps(result, ensure_ascii=False)) + except KeyboardInterrupt: + pass + except Exception as error: + # Native exception bodies may include URLs or secrets; emit bounded known messages only. + message = str(error) if isinstance(error, (RuntimeError, ValueError)) else type(error).__name__ + ";请检查网络、证书、安装目录。" + print(json.dumps({"message": message[:600], "error": True}, ensure_ascii=False)) + raise SystemExit(1) from None + + +if __name__ == "__main__": + main() diff --git a/deployable/relay/common.py b/deployable/relay/common.py new file mode 100644 index 0000000..c112b99 --- /dev/null +++ b/deployable/relay/common.py @@ -0,0 +1,61 @@ +import hashlib +import hmac +import json +import time +from fastapi import HTTPException, Request +from starlette.responses import JSONResponse + + +def require_secret(actual, expected): + if not actual or not hmac.compare_digest(actual, expected): + raise HTTPException(401, "unauthorized") + + +async def rate_limit(redis, bucket: str, limit=60, seconds=60): + # Atomic counter + expiry: a crash cannot leave a permanent bucket. + key = "hn:rate:" + hashlib.sha256(bucket.encode()).hexdigest() + ":" + str(int(time.time()) // seconds) + pipe = redis.pipeline(transaction=True) + pipe.incr(key) + pipe.expire(key, seconds * 2) + result = (await pipe.execute())[0] + if int(result) > limit: + raise HTTPException(429, "rate limited", headers={"Retry-After": str(seconds)}) + + +class BodyLimitMiddleware: + def __init__(self, app, max_body=262144): + self.app, self.max_body = app, max_body + + async def __call__(self, scope, receive, send): + if scope["type"] != "http": + return await self.app(scope, receive, send) + messages, size = [], 0 + while True: + message = await receive() + if message["type"] == "http.disconnect": + return + size += len(message.get("body", b"")) + if size > self.max_body: + return await JSONResponse({"detail": "body too large"}, status_code=413)(scope, receive, send) + messages.append(message) + if not message.get("more_body", False): + break + async def buffered(): + return messages.pop(0) if messages else await receive() + await self.app(scope, buffered, send) + + +def no_cache(response): + response.headers["Cache-Control"] = "no-store" + response.headers["X-Content-Type-Options"] = "nosniff" + response.headers["Referrer-Policy"] = "no-referrer" + return response + + +async def json_pub(redis, channel, data): + return await redis.publish(channel, json.dumps(data, separators=(",", ":"))) + + +def peer(request: Request): + # Do not trust arbitrary X-Forwarded-For; configure uvicorn trusted proxies explicitly. + return request.client.host if request.client else "unknown" diff --git a/deployable/relay/config.py b/deployable/relay/config.py new file mode 100644 index 0000000..c4c32ac --- /dev/null +++ b/deployable/relay/config.py @@ -0,0 +1,66 @@ +from pathlib import Path +from pydantic import model_validator +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + model_config = SettingsConfigDict(env_prefix="HN_", extra="ignore", hide_input_in_errors=True) + environment: str = "production" + role: str = "connector" + redis_url: str = "redis://127.0.0.1:6379/0" + data_dir: Path = Path("runtime/connector") + database_url: str = "" + admin_secret: str = "" + internal_secret: str = "" + ca_cert: Path = Path("runtime/pki/ca.pem") + connector_key: Path = Path("runtime/pki/connector.key") + connector_public_key: Path = Path("runtime/pki/connector.pub") + public_url: str = "https://relay.example.invalid" + router_url: str = "wss://relay.example.invalid/device/ws" + device_domain: str = "" + feishu_api_base: str = "https://open.feishu.cn" + feishu_authorize_url: str = "https://open.feishu.cn/open-apis/authen/v1/authorize" + feishu_oauth_scopes: str = "" + feishu_app_id: str = "" + feishu_app_secret: str = "" + feishu_encrypt_key: str = "" + feishu_verification_token: str = "" + feishu_enabled: bool = False + oauth_enabled: bool = True + feishu_event_source: str = "longconn" + proxy_enabled: bool = False + # A fixed allowlist, never a user supplied upstream URL. + proxy_paths: str = "/open-apis/drive/v1/files,/open-apis/docx/v1/documents" + request_timeout: int = 45 + max_body: int = 262144 + max_pending: int = 1000 + max_devices: int = 1000 + max_connections: int = 1000 + max_result: int = 16000 + allow_insecure_local: bool = False + enrollment_signer_url: str = "" + + @model_validator(mode="after") + def validate_runtime(self): + if self.environment not in {"production", "test", "development"}: + raise ValueError("invalid environment") + if self.role == "connector" and min(len(self.admin_secret), len(self.internal_secret)) < 32: + raise ValueError("admin/internal secrets must each be at least 32 characters") + if self.role == "longconn" and len(self.internal_secret) < 32: + raise ValueError("internal credential must be at least 32 characters") + if self.role == "connector" and self.admin_secret == self.internal_secret: + raise ValueError("admin and internal credentials must be different") + if self.environment == "production": + if self.allow_insecure_local or not self.public_url.startswith("https://"): + raise ValueError("production requires HTTPS") + if not self.router_url.startswith("wss://"): + raise ValueError("production requires WSS") + if not self.feishu_api_base.startswith("https://"): + raise ValueError("platform API requires HTTPS") + if self.feishu_event_source not in {"longconn", "webhook"}: + raise ValueError("invalid platform event source") + if self.feishu_enabled and not all((self.feishu_app_id, self.feishu_app_secret)): + raise ValueError("platform app ID and secret are required") + if self.feishu_enabled and self.feishu_event_source == "webhook" and not all((self.feishu_encrypt_key, self.feishu_verification_token)): + raise ValueError("webhook requires encryption key and verification token") + return self diff --git a/deployable/relay/connector.py b/deployable/relay/connector.py new file mode 100644 index 0000000..c01f3b6 --- /dev/null +++ b/deployable/relay/connector.py @@ -0,0 +1,506 @@ +import asyncio +import base64 +import contextlib +import hashlib +import hmac +import json +import re +import secrets +import sqlite3 +import time +import uuid +from contextlib import asynccontextmanager +from dataclasses import dataclass +from urllib.parse import unquote, urlsplit + +from cryptography.hazmat.primitives import hashes, padding +from cryptography.hazmat.primitives.asymmetric import x25519 +from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes +from fastapi import FastAPI, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from fastapi.exceptions import RequestValidationError +from pydantic import BaseModel, Field +from redis.asyncio import Redis + +from .common import BodyLimitMiddleware, no_cache, peer, rate_limit, require_secret +from .config import Settings +from .crypto import (fingerprint, load_private, open_envelope, raw_public, seal, + sign, verified, verify_device) +from .oauth import OAuth +from .platform import Feishu, PlatformError +from .store import Store, INTEGRITY_ERRORS + + +class Register(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + cert: str = Field(max_length=10000) + + +class Bind(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Instruction(BaseModel): + open_id: str = Field(min_length=1, max_length=128) + text: str = Field(min_length=1, max_length=8000) + # Stable identity chosen by upstream, not generated again on retries. + turn_id: str = Field(default_factory=lambda: uuid.uuid4().hex, pattern=r"^[A-Za-z0-9_-]{1,128}$") + + +@dataclass +class Pending: + future: asyncio.Future + private: object + public: object + host: str + session: str + + +class Broker: + def __init__(self, settings, redis, store): + self.s, self.r, self.store = settings, redis, store + self.signer = load_private(settings.connector_key) + self.pending: dict[str, Pending] = {} + + async def replies(self, pub): + async for message in pub.listen(): + if message["type"] != "message": + continue + try: + data = json.loads(message["data"]) + item = self.pending.get(data["turn_id"]) + if not item or item.future.done(): + continue + plain = open_envelope(data["envelope"], item.private, item.public, + host_id=item.host, session_id=item.session) + if plain["turn_id"] != data["turn_id"] or plain.get("type") != "result": + continue + result = plain.get("result", "") + if not isinstance(result, str) or len(result) > self.s.max_result: + continue + item.future.set_result({"status": "completed", "turn_id": data["turn_id"], "result": result}) + except Exception: + # Invalid packets never consume a pending result; do not log packet contents. + continue + + async def dispatch(self, owner, text, turn): + device = self.store.by_owner(owner) + if not device: + return {"status": "unavailable", "turn_id": turn, "result": "未绑定设备或设备不可用。"} + host = device["host_id"] + await rate_limit(self.r, "dispatch:" + owner, 30) + if len(self.pending) >= self.s.max_pending: + raise HTTPException(503, "too many in-flight requests") + presence = await self.r.get("hn:online:" + host) + if not presence or await self.r.exists("hn:revoked:" + host): + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线,请开机并连接网络。"} + try: + presence = json.loads(presence) + if fingerprint(presence["cert"]) != device["fingerprint"]: + raise ValueError("certificate changed") + public = verify_device(presence["cert"], self.s.ca_cert.read_bytes(), host) + proof = verified(presence["proof"], public) + if proof["host_id"] != host or proof["challenge"] != presence["challenge"]: + raise ValueError("invalid device session") + session = proof["session_id"] + except Exception: + self.store.audit(owner, "dispatch", host, "identity_rejected") + raise HTTPException(409, "device identity rejected") from None + if not self.store.begin_turn(turn, owner, host): + return {"status": "duplicate", "turn_id": turn, "result": "该请求已接收,不会重复执行。"} + private = x25519.X25519PrivateKey.generate() + future = asyncio.get_running_loop().create_future() + self.pending[turn] = Pending(future, private, public, host, session) + envelope = seal({"type": "instruction", "turn_id": turn, "owner": owner, + "text": text, "read_only": True, "channel": "im", + "reply_key": raw_public(private.public_key())}, proof["kx"], self.signer, + host_id=host, session_id=session) + key = f"hn:inflight:{host}:{turn}" + try: + await self.r.set(key, session, ex=self.s.request_timeout + 5) + count = await self.r.publish("hn:down:" + host, json.dumps({"type": "instruction", "envelope": envelope})) + if not count: + self.store.finish(turn, "offline") + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线。"} + result = await asyncio.wait_for(future, timeout=self.s.request_timeout) + self.store.finish(turn, "completed") + self.store.audit(owner, "instruction", host, "completed") + return result + except asyncio.TimeoutError: + self.store.finish(turn, "unknown") + return {"status": "unknown", "turn_id": turn, + "result": "等待结果超时,执行状态未知;不要直接重发有副作用的任务。"} + finally: + self.pending.pop(turn, None) + await self.r.delete(key) + + +def create_app(settings=None, redis=None, platform=None): + s = settings or Settings() + r = redis or Redis.from_url(s.redis_url, decode_responses=True) + store = Store(s.data_dir / "metadata.sqlite", s.database_url) + f = platform or Feishu(s) + broker = Broker(s, r, store) + oauth = OAuth(s, r, f) + oauth.store = store + background = set() + + def spawn(coro): + task = asyncio.create_task(coro) + background.add(task) + task.add_done_callback(background.discard) + # Retrieve errors without displaying possibly sensitive exception text. + task.add_done_callback(lambda t: t.exception() if not t.cancelled() else None) + + @asynccontextmanager + async def lifespan(app): + await r.ping() + for row in store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall(): + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + pub = r.pubsub() + await pub.subscribe("hn:replies") + reply_task = asyncio.create_task(broker.replies(pub)) + app.state.reply_task = reply_task + async def sweep(): + while True: + await asyncio.sleep(30) + async with oauth.lock: + oauth.prune() + sweep_task = asyncio.create_task(sweep()) + try: + yield + finally: + for task in [reply_task, sweep_task, *background]: + task.cancel() + await asyncio.gather(reply_task, sweep_task, *background, return_exceptions=True) + await pub.aclose() + await f.close() + store.close() + if redis is None: + await r.aclose() + + app = FastAPI(title="Hainuo B1 connector", lifespan=lifespan, docs_url=None, redoc_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=s.max_body) + app.include_router(oauth.router) + from .web_session import WebSessions + web_sessions = WebSessions(s, r, store, broker, f) + app.include_router(web_sessions.router) + app.state.store, app.state.broker, app.state.oauth = store, broker, oauth + app.state.platform, app.state.redis = f, r + + @app.exception_handler(RequestValidationError) + async def validation_error(request, exc): + # FastAPI's default error response includes invalid input values (possibly tokens). + return JSONResponse({"detail": "invalid request parameters"}, status_code=422) + + @app.middleware("http") + async def secure_response(request, call_next): + return no_cache(await call_next(request)) + + def admin(request): + require_secret(request.headers.get("X-Admin-Secret"), s.admin_secret) + + from .enrollment import enrollment_router + app.include_router(enrollment_router(s, r, store, broker.signer, admin)) + from .work_api import router as work_router + app.include_router(work_router(s, r, store, f)) + + @app.get("/health") + async def health(): + return {"status": "alive", "role": "connector", "version": "0.1.0"} + + @app.get("/ready") + async def ready(): + await r.ping() + store.db.execute("SELECT 1") + if not getattr(app.state, "reply_task", None) or app.state.reply_task.done(): + raise HTTPException(503, "reply subscriber unavailable") + return {"status": "ready"} + + @app.post("/admin/devices", status_code=201) + async def register(body: Register, request: Request): + admin(request) + try: + verify_device(body.cert, s.ca_cert.read_bytes(), body.host_id) + except Exception: + raise HTTPException(400, "invalid device certificate") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= s.max_devices: + raise HTTPException(503, "device capacity reached") + try: + fp = fingerprint(body.cert) + store.add_device(body.host_id, body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "device already exists") from None + await r.set("hn:device:" + body.host_id, fp) + store.audit("admin", "register", body.host_id, "success") + return {"host_id": body.host_id, "fingerprint": fp} + + @app.get("/admin/devices") + async def devices(request: Request): + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,owner,revoked FROM devices ORDER BY host_id").fetchall() + return {"devices": [{**dict(row), "online": bool(await r.exists("hn:online:" + row["host_id"]))} for row in rows]} + + @app.post("/admin/devices/{host}/revoke") + async def revoke(host: str, request: Request): + admin(request) + if not store.device(host): + raise HTTPException(404, "device not found") + # Persistent source of truth first; router block + disconnect follows. + store.revoke(host) + await r.incr("hn:web-generation:" + host) + await r.set("hn:revoked:" + host, "1") + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "revoke") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "revoked", "result": "设备已停用,结果已丢弃。"}) + store.audit("admin", "revoke", host, "success") + return {"status": "revoked"} + + @app.post("/admin/reconcile") + async def reconcile(request: Request): + """Rebuild router admission metadata after Redis recovery; no dialogue data.""" + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall() + for row in rows: + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + return {"devices": len(rows)} + + @app.post("/admin/devices/{host}/unbind") + async def admin_unbind(host: str, request: Request): + admin(request) + device = store.device(host) + if not device: + raise HTTPException(404, "device not found") + if device["owner"]: + store.unbind(device["owner"]) + await r.incr("hn:web-generation:" + host) + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "unbind") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + store.audit("admin", "unbind", host, "success") + return {"status": "unbound", "host_id": host, "revoked": bool(device["revoked"])} + + async def make_binding_code(owner): + if store.by_owner(owner): + return "你已绑定电脑,请先发送 /解绑。" + await rate_limit(r, "binding:" + owner, 5) + code = secrets.token_urlsafe(12) + await r.set("hn:bind:" + hashlib.sha256(code.encode()).hexdigest(), owner, ex=300) + return "请在本人电脑的桥接客户端输入绑定码(5分钟有效):" + code + + @app.post("/device/bind") + async def bind(body: Bind, request: Request): + await rate_limit(r, "bind:" + peer(request), 20) + device = store.device(body.host_id) + if not device or device["revoked"]: + raise HTTPException(404, "device unavailable") + try: + public = verify_device(device["cert"], s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, public) + if proof["host_id"] != body.host_id or abs(time.time() - proof["ts"]) > 90: + raise ValueError("invalid proof") + code = proof["code"] + if not isinstance(code, str) or len(code) > 128: + raise ValueError("invalid code") + except Exception: + raise HTTPException(403, "invalid device proof") from None + key = "hn:bind:" + hashlib.sha256(code.encode()).hexdigest() + owner = await r.get(key) + if not owner: + raise HTTPException(404, "binding code expired or consumed") + # GETDEL is atomic across connectors. Invalid device proofs never reach this point. + owner = await r.getdel(key) + if not owner: + raise HTTPException(404, "binding code consumed") + try: + store.bind(body.host_id, owner) + except (*INTEGRITY_ERRORS, ValueError): + raise HTTPException(409, "owner/device already bound; request new code") from None + await r.incr("hn:web-generation:" + body.host_id) + store.audit(owner, "bind", body.host_id, "success") + return sign({"host_id": body.host_id, "owner": owner, "ts": int(time.time())}, broker.signer) + + @app.post("/admin/test/binding-code") + async def test_code(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return {"message": await make_binding_code(body.open_id)} + + @app.post("/admin/test/dispatch") + async def test_dispatch(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return await broker.dispatch(body.open_id, body.text, body.turn_id) + + async def handle_event(data): + header, event = data.get("header", {}), data.get("event", {}) + if header.get("event_type") != "im.message.receive_v1": + return + message, sender = event.get("message", {}), event.get("sender", {}) + owner = sender.get("sender_id", {}).get("open_id") + message_id = message.get("message_id", "") + if (message.get("chat_type") != "p2p" or message.get("message_type") != "text" + or sender.get("sender_type") != "user" or not owner + or not re.fullmatch(r"[A-Za-z0-9_-]{1,128}", message_id)): + return + if header.get("app_id") != s.feishu_app_id: + return + try: + text = json.loads(message["content"])["text"].strip() + if not text or len(text) > 8000: + return + # Persistent receipt BEFORE any side effect. Restarts do not execute a delivery twice. + event_turn = "event_" + hashlib.sha256(message_id.encode()).hexdigest() + if not store.begin_turn(event_turn, owner, "event"): + return + if text == "/绑定": + result = await make_binding_code(owner) + elif text == "/解绑": + old = store.by_owner(owner) + store.unbind(owner) + if old: + await r.incr("hn:web-generation:" + old["host_id"]) + await r.publish("hn:kill:" + old["host_id"], "unbind") + for item in broker.pending.values(): + if item.host == old["host_id"] and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + result = "已解绑;重新绑定后请更新本机绑定信息。" + elif text == "/主机名" or text.startswith("/回显 "): + result = (await broker.dispatch(owner, text, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + else: + instruction = text if text.startswith("/助手 ") else "/助手 " + text + result = (await broker.dispatch(owner, instruction, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + await f.reply(message_id, result[:s.max_result]) + store.finish(event_turn, "replied") + except Exception: + store.audit(owner, "event", message_id, "failed_or_unknown") + + @app.post("/internal/events") + async def internal_event(request: Request): + require_secret(request.headers.get("X-Internal-Secret"), s.internal_secret) + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(await request.json())) + return {"accepted": True} + + @app.post("/feishu/event") + async def webhook(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if not s.feishu_encrypt_key or not s.feishu_verification_token: + raise HTTPException(503, "webhook credentials not configured; use selected event source") + await rate_limit(r, "webhook:" + peer(request), 600) + raw = await request.body() + timestamp, nonce, signature = (request.headers.get(x, "") for x in + ("X-Lark-Request-Timestamp", "X-Lark-Request-Nonce", "X-Lark-Signature")) + try: + if abs(time.time() - int(timestamp)) > 300 or not nonce or len(nonce) > 128: + raise ValueError("stale request") + expected = hashlib.sha256((timestamp + nonce + s.feishu_encrypt_key).encode() + raw).hexdigest() + if not hmac.compare_digest(expected, signature): + raise ValueError("bad signature") + data = json.loads(raw) + if "encrypt" in data: + cipher = base64.b64decode(data["encrypt"], validate=True) + decryptor = Cipher(algorithms.AES(hashlib.sha256(s.feishu_encrypt_key.encode()).digest()), + modes.CBC(cipher[:16])).decryptor() + padded = decryptor.update(cipher[16:]) + decryptor.finalize() + unpadder = padding.PKCS7(128).unpadder() + data = json.loads(unpadder.update(padded) + unpadder.finalize()) + token = data.get("token", data.get("header", {}).get("token", "")) + if not hmac.compare_digest(token, s.feishu_verification_token): + raise ValueError("invalid verification token") + except Exception: + raise HTTPException(403, "invalid event authentication") from None + if data.get("type") == "url_verification": + return {"challenge": data.get("challenge", "")} + # Same delivery is ACKed again; durable message_id receipt handles execution deduplication. + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(data)) + return {"code": 0} + + @app.get("/api/whoami") + async def whoami(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(r, "whoami:" + peer(request)) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await f.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "no device bound") + if not s.device_domain or not re.fullmatch(r"[a-zA-Z0-9.-]+", s.device_domain): + raise HTTPException(503, "device web domain not configured") + return {"host_id": device["host_id"], "url": f"https://h-{device['host_id']}.{s.device_domain}/", + "online": bool(await r.exists("hn:online:" + device["host_id"])), + "device_login_required": True} + + @app.get("/h5", response_class=HTMLResponse) + async def h5(): + from importlib.resources import files + return HTMLResponse(files("relay").joinpath("web/index.html").read_text(encoding="utf-8"), + headers={"Content-Security-Policy": "default-src 'self'; script-src 'self'; style-src 'self'; frame-ancestors 'self'; base-uri 'none'; form-action 'self'"}) + + @app.get("/assets/{name}") + async def asset(name: str): + from importlib.resources import files + from starlette.responses import Response + if name not in {"app.js", "style.css"}: + raise HTTPException(404, "not found") + return Response(files("relay").joinpath("web/" + name).read_bytes(), + media_type="text/javascript" if name.endswith(".js") else "text/css") + + @app.get("/proxy") + async def proxy(request: Request): + if not s.proxy_enabled or not s.feishu_enabled: + raise HTTPException(404, "not found") + auth = request.headers.get("Authorization", "") + target = request.headers.get("X-Relay-Target", "") + decoded = unquote(target) + parts = urlsplit(target) + allowed = s.proxy_paths.split(",") + if (not auth.startswith("Bearer ") or len(auth) > 8192 or len(target) > 4096 + or parts.scheme or parts.netloc or parts.fragment or target.startswith("//") + or any(x in decoded for x in ("..", "\\", "\r", "\n", "#", "%")) + or not any(parts.path == x or parts.path.startswith(x + "/") for x in allowed)): + raise HTTPException(403, "target not allowed") + try: + info = await f.user_info(auth[7:]) + await rate_limit(r, "proxy:" + info["open_id"], 60) + # Buffer bounded response; no disk/cache, redirects never followed. + async with f.http.stream("GET", target, headers={"Authorization": auth}) as response: + if response.status_code in {301, 302, 303, 307, 308}: + raise HTTPException(502, "upstream redirect refused") + chunks, size = [], 0 + async for chunk in response.aiter_bytes(): + size += len(chunk) + if size > 2 * 1024 * 1024: + raise HTTPException(502, "response too large") + chunks.append(chunk) + from starlette.responses import Response + store.audit(info["open_id"], "proxy", parts.path, str(response.status_code)) + return Response(b"".join(chunks), status_code=response.status_code, + media_type=response.headers.get("content-type", "application/json")) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + + return app diff --git a/deployable/relay/crypto.py b/deployable/relay/crypto.py new file mode 100644 index 0000000..dd962e5 --- /dev/null +++ b/deployable/relay/crypto.py @@ -0,0 +1,140 @@ +"""Signed ephemeral X25519 envelopes. The router never receives plaintext keys.""" +import base64 +import hashlib +import json +import secrets +import time +from datetime import datetime, timedelta, timezone + +from cryptography import x509 +from cryptography.hazmat.primitives import hashes, serialization +from cryptography.hazmat.primitives.asymmetric import ed25519, x25519 +from cryptography.hazmat.primitives.ciphers.aead import AESGCM +from cryptography.hazmat.primitives.kdf.hkdf import HKDF +from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID + + +def b64(data: bytes) -> str: + return base64.urlsafe_b64encode(data).decode() + + +def unb64(data: str) -> bytes: + return base64.b64decode(data.encode(), altchars=b"-_", validate=True) + + +def canonical(data: dict) -> bytes: + return json.dumps(data, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode() + + +def raw_public(key) -> str: + return b64(key.public_bytes(serialization.Encoding.Raw, serialization.PublicFormat.Raw)) + + +def pem_private(key) -> bytes: + return key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, + serialization.NoEncryption()) + + +def load_private(path): + protected = path.with_name(path.name + ".dpapi") + if protected.is_file(): + from .windows_keys import unprotect + data = unprotect(protected.read_bytes()) + else: + data = path.read_bytes() + return serialization.load_pem_private_key(data, password=None) + + +def load_public(path): + return serialization.load_pem_public_key(path.read_bytes()) + + +def fingerprint(pem: str) -> str: + return x509.load_pem_x509_certificate(pem.encode()).fingerprint(hashes.SHA256()).hex() + + +def verify_device(pem: str, ca_pem: bytes, host_id: str): + """v1 deliberately accepts only directly issued Ed25519 device certificates.""" + cert = x509.load_pem_x509_certificate(pem.encode()) + ca = x509.load_pem_x509_certificate(ca_pem) + now = datetime.now(timezone.utc) + if not (ca.not_valid_before_utc <= now <= ca.not_valid_after_utc): + raise ValueError("expired CA") + if not ca.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("not a CA") + cert.verify_directly_issued_by(ca) + if not (cert.not_valid_before_utc <= now <= cert.not_valid_after_utc): + raise ValueError("expired certificate") + if cert.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("CA cannot be used as device") + if ExtendedKeyUsageOID.CLIENT_AUTH not in cert.extensions.get_extension_for_class(x509.ExtendedKeyUsage).value: + raise ValueError("clientAuth missing") + names = cert.extensions.get_extension_for_class(x509.SubjectAlternativeName).value + if names.get_values_for_type(x509.UniformResourceIdentifier) != [f"urn:hainuo:host:{host_id}"]: + raise ValueError("host identity mismatch") + public = cert.public_key() + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("v1 requires Ed25519 device key") + return public + + +def sign(data: dict, key) -> dict: + return {**data, "signature": b64(key.sign(canonical(data)))} + + +def verified(data: dict, key) -> dict: + body = {k: v for k, v in data.items() if k != "signature"} + key.verify(unb64(data["signature"]), canonical(body)) + return body + + +def seal(payload: dict, recipient_public: str, signer, *, host_id: str, session_id: str) -> dict: + ephemeral = x25519.X25519PrivateKey.generate() + header = {"v": 1, "host_id": host_id, "session_id": session_id, + "ts": int(time.time()), "nonce": b64(secrets.token_bytes(12)), + "ephemeral": raw_public(ephemeral.public_key())} + shared = ephemeral.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(recipient_public))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + cipher = AESGCM(key).encrypt(unb64(header["nonce"]), canonical(payload), canonical(header)) + return sign({**header, "ciphertext": b64(cipher)}, signer) + + +def open_envelope(envelope: dict, recipient_private, sender_public, *, host_id: str, session_id: str) -> dict: + body = verified(envelope, sender_public) + if body["v"] != 1 or body["host_id"] != host_id or body["session_id"] != session_id: + raise ValueError("wrong destination/session") + if abs(time.time() - body["ts"]) > 90: + raise ValueError("expired envelope") + header = {k: v for k, v in body.items() if k != "ciphertext"} + shared = recipient_private.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(body["ephemeral"]))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + plain = AESGCM(key).decrypt(unb64(body["nonce"]), unb64(body["ciphertext"]), canonical(header)) + return json.loads(plain) + + +def issue_ca(): + key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Hainuo device test CA")]) + now = datetime.now(timezone.utc) + cert = (x509.CertificateBuilder().subject_name(name).issuer_name(name) + .public_key(key.public_key()).serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=3650)) + .add_extension(x509.BasicConstraints(ca=True, path_length=0), critical=True) + .add_extension(x509.KeyUsage(False, False, False, False, False, True, True, False, False), critical=True) + .sign(key, algorithm=None)) + return key, cert + + +def issue_device(ca_key, ca_cert, host_id: str, public_key, days=30): + now = datetime.now(timezone.utc) + return (x509.CertificateBuilder() + .subject_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, host_id)])) + .issuer_name(ca_cert.subject).public_key(public_key) + .serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=days)) + .add_extension(x509.BasicConstraints(ca=False, path_length=None), critical=True) + .add_extension(x509.SubjectAlternativeName([x509.UniformResourceIdentifier(f"urn:hainuo:host:{host_id}")]), critical=False) + .add_extension(x509.ExtendedKeyUsage([ExtendedKeyUsageOID.CLIENT_AUTH]), critical=False) + .sign(ca_key, algorithm=None)) diff --git a/deployable/relay/device.py b/deployable/relay/device.py new file mode 100644 index 0000000..a8fe010 --- /dev/null +++ b/deployable/relay/device.py @@ -0,0 +1,216 @@ +"""Reference PC bridge. No shell/PowerShell execution; local read-only diagnostics.""" +import asyncio +import contextlib +import json +import os +import secrets +import socket +import sqlite3 +import time +from pathlib import Path +from urllib.parse import urlsplit + +import httpx +from cryptography.hazmat.primitives.asymmetric import x25519 +from websockets.asyncio.client import connect + +from .crypto import (load_private, load_public, open_envelope, raw_public, seal, sign, + verified, verify_device) + + +class DeviceJournal: + def __init__(self, path): + path.parent.mkdir(parents=True, exist_ok=True) + self.db = sqlite3.connect(path) + self.db.execute("CREATE TABLE IF NOT EXISTS consumed(turn_id TEXT PRIMARY KEY, ts REAL NOT NULL)") + self.db.commit() + + def consume(self, turn): + with self.db: + return self.db.execute("INSERT OR IGNORE INTO consumed VALUES(?,?)", (turn, time.time())).rowcount == 1 + + def close(self): + self.db.close() + + +async def execute_readonly(text, root: Path | None): + if text in {"/主机名", "hostname"}: + return socket.gethostname() + if text.startswith("/回显 "): + return text[4:][:16000] + if text.startswith("/读取 "): + if root is None: + return "未配置测试文件目录。" + base = root.resolve(strict=True) + try: + path = (base / text[4:].strip()).resolve(strict=True) + if path == base or not path.is_relative_to(base) or not path.is_file(): + return "拒绝:文件不在允许的测试目录。" + if path.stat().st_size > 32768: + return "拒绝:测试文件超过32KB。" + return path.read_text(encoding="utf-8")[:16000] + except (OSError, UnicodeError, ValueError): + return "拒绝:文件不存在、不可读或不是UTF-8文本。" + return "当前桥接客户端仅开放只读诊断:/主机名、/回显 内容、/读取 相对路径;尚未接入海诺本地Agent接口。" + + +class LocalAgent: + """Explicit adapter contract, not a claim of compatibility with installer 1.065.""" + def __init__(self, url, transport=None): + parts = urlsplit(url) + if parts.scheme != "http" or parts.hostname not in {"127.0.0.1", "::1"} or parts.username or parts.password or parts.fragment: + raise ValueError("local Agent must use a fixed loopback HTTP endpoint") + self.url = url + self.http = httpx.AsyncClient(timeout=30, follow_redirects=False, transport=transport, trust_env=False) + + async def execute(self, text, owner, turn): + headers = {} + if os.environ.get("HN_LOCAL_AGENT_TOKEN"): + headers["Authorization"] = "Bearer " + os.environ["HN_LOCAL_AGENT_TOKEN"] + try: + async with self.http.stream("POST", self.url, headers=headers, + json={"text": text, "open_id": owner, "turn_id": turn, + "channel": "im", "read_only": True}) as response: + if response.status_code != 200: + return "本机Agent未返回成功结果,请查看本机服务状态。" + body = b"" + async for chunk in response.aiter_bytes(): + body += chunk + if len(body) > 65536: + return "本机Agent结果超过限制。" + result = json.loads(body)["result"] + if not isinstance(result, str): + return "本机Agent响应格式不匹配。" + return result[:16000] + except (httpx.HTTPError, ValueError, KeyError): + return "本机Agent连接失败或响应格式不匹配。" + + async def close(self): + await self.http.aclose() + + +async def bind_device(directory: Path, connector_url: str, code: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + private = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "code": code, "ts": int(time.time())}, private) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as http: + response = await http.post(connector_url.rstrip("/") + "/device/bind", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"binding failed: HTTP {response.status_code}") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt["host_id"] != config["host_id"]: + raise ValueError("wrong binding receipt") + config["owner"] = receipt["owner"] + temp = directory / "device.json.tmp" + temp.write_text(json.dumps(config, ensure_ascii=False, indent=2), encoding="utf-8") + temp.replace(directory / "device.json") + + +async def pair_browser(directory: Path, connector_url: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8-sig")) + if not config.get("owner"): + raise RuntimeError("device must be bound first") + key = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "owner": config["owner"], + "purpose": "h5-pair-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as client: + response = await client.post(connector_url.rstrip("/") + "/device/web-pair", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"browser pairing failed: HTTP {response.status_code}") + result = verified(response.json(), load_public(directory / "connector.pub")) + if result["host_id"] != config["host_id"]: + raise ValueError("wrong pairing receipt") + return result + + +async def run_device(directory: Path, router_url: str, **kwargs): + from .process_lock import DeviceProcessLock + with DeviceProcessLock(directory): + return await _run_device(directory, router_url, **kwargs) + + +async def _run_device(directory: Path, router_url: str, *, root=None, once=False, ssl_context=None, + stop_event=None, connected_event=None, agent_url=None, hainuo_config=None): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + if not config.get("owner"): + raise RuntimeError("bind device before starting") + host, owner = config["host_id"], config["owner"] + key = load_private(directory / "device.key") + connector = load_public(directory / "connector.pub") + cert = (directory / "device.pem").read_text(encoding="utf-8") + verify_device(cert, (directory / "ca.pem").read_bytes(), host) + journal = DeviceJournal(directory / "journal.sqlite") + if agent_url and hainuo_config: + raise ValueError("choose one Agent adapter") + if hainuo_config: + from .hainuo1065 import Hainuo1065Agent + agent = Hainuo1065Agent(hainuo_config) + else: + agent = LocalAgent(agent_url) if agent_url else None + stop_event = stop_event or asyncio.Event() + delay = 1 + try: + while not stop_event.is_set(): + try: + kwargs = {"ssl": ssl_context} if router_url.startswith("wss://") and ssl_context else {} + async with connect(router_url, max_size=262144, open_timeout=10, ping_interval=10, + proxy=None, **kwargs) as ws: + challenge = json.loads(await ws.recv())["challenge"] + ephemeral = x25519.X25519PrivateKey.generate() + session = secrets.token_hex(16) + proof = sign({"host_id": host, "session_id": session, "challenge": challenge, + "kx": raw_public(ephemeral.public_key()), "ts": int(time.time())}, key) + await ws.send(json.dumps({"host_id": host, "cert": cert, "proof": proof})) + accepted = json.loads(await ws.recv()) + if accepted.get("type") != "connected": + raise RuntimeError("device connection rejected") + print("设备连接成功,已绑定本人海能Work身份。", flush=True) + if connected_event: + connected_event.set() + delay = 1 + while not stop_event.is_set(): + try: + raw = await asyncio.wait_for(ws.recv(), timeout=1) + except asyncio.TimeoutError: + continue + data = json.loads(raw) + payload = open_envelope(data["envelope"], ephemeral, connector, + host_id=host, session_id=session) + if payload.get("owner") != owner or payload.get("type") != "instruction": + continue + turn = payload["turn_id"] + if not isinstance(turn, str) or len(turn) > 128: + continue + if not journal.consume(turn): + result = "该请求已处理,拒绝重复执行。" + elif payload.get("read_only") is not True or payload.get("channel") != "im": + result = "拒绝:通道或操作类型不受支持。" + else: + if agent and payload["text"].startswith("/助手 "): + result = await agent.execute(payload["text"][4:], owner, turn) + else: + result = await execute_readonly(payload["text"], root) + reply = seal({"type": "result", "turn_id": turn, "result": result}, + payload["reply_key"], key, host_id=host, session_id=session) + await ws.send(json.dumps({"type": "reply", "turn_id": turn, "envelope": reply})) + if once: + return + except asyncio.CancelledError: + raise + except Exception as error: + if once: + raise + from websockets.exceptions import ConnectionClosed + if isinstance(error, ConnectionClosed) and error.rcvd and error.rcvd.code == 1008: + print("设备被中继拒绝:请检查是否有另一桥接进程、设备准入或证书状态。", flush=True) + # No payloads/URLs/credentials in logs, bounded reconnect backoff + jitter. + print("设备连接中断,等待重连。", flush=True) + with contextlib.suppress(asyncio.TimeoutError): + await asyncio.wait_for(stop_event.wait(), timeout=delay + secrets.randbelow(1000) / 1000) + delay = min(delay * 2, 30) + finally: + journal.close() + if agent: + await agent.close() diff --git a/deployable/relay/enrollment.py b/deployable/relay/enrollment.py new file mode 100644 index 0000000..b4ac77f --- /dev/null +++ b/deployable/relay/enrollment.py @@ -0,0 +1,178 @@ +"""Device registration with key possession proof; Work binding is separate.""" +import json +import secrets +import time +import httpx + +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey +from fastapi import APIRouter, HTTPException, Request +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .crypto import verified, sign, raw_public, verify_device, fingerprint +from .web_session import digest +from fastapi.responses import JSONResponse + + +def validate_enrollment(body): + try: + key = serialization.load_pem_public_key(body.public_key.encode()) + if not isinstance(key, Ed25519PublicKey): + raise ValueError() + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["purpose"] != "device-enroll-v1" + or abs(time.time() - proof["ts"]) > 90 or len(proof["nonce"]) != 32): + raise ValueError() + return key + except Exception: + raise HTTPException(403, "invalid enrollment proof") from None + + +async def request_certificate(settings, body): + async with httpx.AsyncClient(trust_env=False, timeout=15, follow_redirects=False) as http: + response = await http.post(settings.enrollment_signer_url.rstrip("/") + "/issue", + headers={"X-Internal-Secret": settings.internal_secret}, + json=body.model_dump()) + response.raise_for_status() + return response.json()["cert"] + + +class EnrollmentRequest(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + public_key: str = Field(max_length=1024) + proof: dict + + +class EnrollmentApproval(BaseModel): + cert: str = Field(max_length=10000) + + +def enrollment_router(settings, redis, store, signer, admin): + router = APIRouter() + + @router.post("/device/register") + async def automatic_registration(body: EnrollmentRequest, request: Request): + # No human approval: eligibility follows intranet access plus Work app scope. + # Proof/TLS/limits protect identity and resources, and do not add user approval. + await rate_limit(redis, "auto-register-ip:" + peer(request), 120) + key = validate_enrollment(body) + await rate_limit(redis, "auto-register-host:" + body.host_id, 30) + existing = store.device(body.host_id) + + def check_existing(device): + if device["revoked"]: + raise HTTPException(403, "device revoked") + try: + registered = verify_device(device["cert"], settings.ca_cert.read_bytes(), body.host_id) + if raw_public(registered) != raw_public(key): + raise ValueError() + except Exception: + raise HTTPException(409, "existing device identity mismatch or certificate expired") from None + return device["cert"] + + if existing: + cert = check_existing(existing) + else: + if not settings.enrollment_signer_url: + raise HTTPException(503, "automatic registration is not enabled; update ECS first") + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= settings.max_devices: + raise HTTPException(503, "device capacity reached") + await rate_limit(redis, "auto-register-total", settings.max_pending, 3600) + try: + cert = await request_certificate(settings, body) + issued = verify_device(cert, settings.ca_cert.read_bytes(), body.host_id) + if raw_public(issued) != raw_public(key): + raise ValueError() + except Exception: + raise HTTPException(503, "certificate issuer unavailable or invalid response") from None + from .store import INTEGRITY_ERRORS + try: + store.add_device(body.host_id, cert, fingerprint(cert)) + except INTEGRITY_ERRORS: + # A retry/concurrent request must return the original certificate. + cert = check_existing(store.device(body.host_id)) + store.audit("device", "automatic_registration", body.host_id, "success") + await redis.set("hn:device:" + body.host_id, fingerprint(cert)) + return no_cache(JSONResponse(sign({"status": "registered", "host_id": body.host_id, + "cert": cert}, signer), status_code=201 if not existing else 200)) + + @router.post("/device/enroll") + async def request_enrollment(body: EnrollmentRequest, request: Request): + await rate_limit(redis, "enroll-ip:" + peer(request), 10) + validate_enrollment(body) + await rate_limit(redis, "enroll-host:" + body.host_id, 3, 3600) + if store.device(body.host_id): + raise HTTPException(409, "device already registered; recover its original certificate") + # Global rolling capacity bound; prevents anonymous request-table exhaustion. + await rate_limit(redis, "enroll-global", settings.max_pending, 1800) + identifier, claim_secret = secrets.token_hex(16), secrets.token_urlsafe(32) + record = {"host_id": body.host_id, "public_key": body.public_key, + "claim_hash": digest(claim_secret), "created": time.time(), "status": "pending"} + await redis.set("hn:enroll:" + identifier, json.dumps(record), ex=1800) + return no_cache(JSONResponse(sign({"request_id": identifier, "claim_secret": claim_secret, + "host_id": body.host_id, "expires_in": 1800}, signer), status_code=201)) + + @router.get("/device/enroll/{identifier}") + async def claim(identifier: str, request: Request): + await rate_limit(redis, "enroll-poll:" + peer(request), 60) + data = await redis.get("hn:enroll:" + identifier) + if not data: + raise HTTPException(404, "request expired") + record = json.loads(data) + import hmac + if not hmac.compare_digest(record["claim_hash"], digest(request.headers.get("X-Enrollment-Secret", ""))): + raise HTTPException(404, "request unavailable") + if record["status"] == "pending": + return no_cache(JSONResponse({"status": "pending"}, status_code=202)) + device = store.device(record["host_id"]) + if not device or device["revoked"]: + raise HTTPException(403, "device unavailable") + # Recoverable for its original claimant for the request TTL; public certificate only. + return no_cache(JSONResponse(sign({"status": "approved", "host_id": record["host_id"], + "request_id": identifier, "cert": device["cert"]}, signer))) + + @router.get("/admin/enrollments/{identifier}") + async def inspect(identifier: str, request: Request): + admin(request) + encoded = await redis.get("hn:enroll:" + identifier) + if not encoded: + raise HTTPException(404, "request expired") + record = json.loads(encoded) + return {k: v for k, v in record.items() if k != "claim_hash"} + + @router.post("/admin/enrollments/{identifier}/approve") + async def approve(identifier: str, body: EnrollmentApproval, request: Request): + admin(request) + encoded = await redis.get("hn:enroll:" + identifier) + if not encoded: + raise HTTPException(404, "request expired") + record = json.loads(encoded) + try: + public = verify_device(body.cert, settings.ca_cert.read_bytes(), record["host_id"]) + requested = serialization.load_pem_public_key(record["public_key"].encode()) + if raw_public(public) != raw_public(requested): + raise ValueError() + except Exception: + raise HTTPException(400, "certificate does not match enrollment") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= settings.max_devices: + raise HTTPException(503, "device capacity reached") + fp = fingerprint(body.cert) + existing = store.device(record["host_id"]) + if existing and (existing["revoked"] or existing["fingerprint"] != fp): + raise HTTPException(409, "device conflict") + if not existing: + from .store import INTEGRITY_ERRORS + try: + store.add_device(record["host_id"], body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "concurrent device registration") from None + await redis.set("hn:device:" + record["host_id"], fp) + record["status"] = "approved" + remaining = await redis.ttl("hn:enroll:" + identifier) + if remaining < 1: + raise HTTPException(409, "request expired during approval") + await redis.set("hn:enroll:" + identifier, json.dumps(record), ex=remaining) + store.audit("admin", "enrollment_approved", record["host_id"], "success") + return {"status": "approved", "host_id": record["host_id"]} + + return router diff --git a/deployable/relay/enrollment_signer.py b/deployable/relay/enrollment_signer.py new file mode 100644 index 0000000..6089d02 --- /dev/null +++ b/deployable/relay/enrollment_signer.py @@ -0,0 +1,43 @@ +"""Internal automatic CA issuer. No published host port, no application credentials.""" +import os +from pathlib import Path + +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from fastapi import FastAPI, Request +from .common import BodyLimitMiddleware, require_secret +from .crypto import load_private, issue_device, raw_public, verify_device +from .enrollment import EnrollmentRequest, validate_enrollment + + +def create_app(*, ca_key_path=None, ca_cert_path=None, secret=None): + key = load_private(Path(ca_key_path or os.environ.get("HN_ISSUER_CA_KEY", "/issuer/ca.key"))) + ca_bytes = Path(ca_cert_path or os.environ.get("HN_ISSUER_CA_CERT", "/trust/ca.pem")).read_bytes() + ca = x509.load_pem_x509_certificate(ca_bytes) + if raw_public(key.public_key()) != raw_public(ca.public_key()): + raise ValueError("issuer CA key mismatch") + token = secret or os.environ.get("HN_INTERNAL_SECRET", "") + if len(token) < 32: + raise ValueError("issuer requires internal credential") + app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=16384) + + @app.get("/ready") + async def ready(): + return {"status": "ready"} + + @app.post("/issue") + async def issue(body: EnrollmentRequest, request: Request): + require_secret(request.headers.get("X-Internal-Secret"), token) + public = validate_enrollment(body) + cert = issue_device(key, ca, body.host_id, public).public_bytes(serialization.Encoding.PEM).decode() + verify_device(cert, ca_bytes, body.host_id) + return {"cert": cert} + + return app + + +if __name__ == "__main__": + import uvicorn + uvicorn.run("relay.enrollment_signer:create_app", factory=True, host="0.0.0.0", port=8000, + workers=1, access_log=False, proxy_headers=False, log_level="warning") diff --git a/deployable/relay/hainuo1065.py b/deployable/relay/hainuo1065.py new file mode 100644 index 0000000..6f7735b --- /dev/null +++ b/deployable/relay/hainuo1065.py @@ -0,0 +1,107 @@ +"""Adapter for the native API actually found in installer 1.065. + +Loopback only, independent API key, no platform secrets, no redirects or retries. +The included native launcher offers only text inference with no tools. +""" +import hashlib +import json +import re +from pathlib import Path +from urllib.parse import urlsplit + +import httpx + + +class NativeAPIError(RuntimeError): + """Contains a sanitized status only, never response body or credentials.""" + + +def read_native_config(path): + path = Path(path) + data = json.loads(path.read_text(encoding="utf-8-sig")) + url = data.get("url", "") + parts = urlsplit(url) + if (parts.scheme != "http" or parts.hostname != "127.0.0.1" or not parts.port + or parts.username or parts.password or parts.query or parts.fragment + or parts.path not in {"", "/"}): + raise ValueError("native API must be a fixed IPv4 loopback HTTP base URL") + if not isinstance(data.get("api_key"), str) or len(data["api_key"]) < 32: + raise ValueError("native API requires a dedicated key of at least 32 characters") + if data.get("mode") not in {"text-only", "profile-tools"}: + raise ValueError("unsupported native API mode") + data["url"] = url.rstrip("/") + return data + + +class Hainuo1065Agent: + def __init__(self, config_path, transport=None): + self.config = read_native_config(config_path) + self.http = httpx.AsyncClient(base_url=self.config["url"], timeout=120, + follow_redirects=False, trust_env=False, transport=transport) + self.headers = {"Authorization": "Bearer " + self.config["api_key"]} + + async def _json(self, method, path, **kwargs): + try: + async with self.http.stream(method, path, headers={**self.headers, **kwargs.pop("headers", {})}, **kwargs) as response: + if response.status_code != 200: + raise NativeAPIError(f"native API HTTP {response.status_code}") + body = bytearray() + async for chunk in response.aiter_bytes(): + body.extend(chunk) + if len(body) > 262144: + raise NativeAPIError("native API response exceeds limit") + data = json.loads(body) + if not isinstance(data, dict): + raise NativeAPIError("native API response is not an object") + return data + except (httpx.HTTPError, ValueError): + raise NativeAPIError("native API connection/JSON failure") from None + + async def probe(self): + health = await self._json("GET", "/health") + if health.get("platform") != "hermes-agent" or health.get("status") != "ok": + raise NativeAPIError("endpoint is not the native Hermes API") + capabilities = await self._json("GET", "/v1/capabilities") + policy = capabilities.get("hainuo_relay_policy", {}) + # A stock unrestricted API endpoint is deliberately not sufficient. + expected_mode = self.config["mode"] + expected_tools = "profile" if expected_mode == "profile-tools" else "disabled" + if policy != {"mode": expected_mode, "tools": expected_tools, "contract": "hainuo-1065-v1"}: + raise NativeAPIError("native API policy missing; start the supplied restricted launcher") + models = await self._json("GET", "/v1/models") + model = self.config.get("model") + available = models.get("data") + if not isinstance(available, list) or not available or not all(isinstance(x, dict) and isinstance(x.get("id"), str) for x in available): + raise NativeAPIError("native model list invalid") + if model and model not in {x["id"] for x in available}: + raise NativeAPIError("configured model alias unavailable") + self.model = model or available[0]["id"] + return {"status": "ready", "platform": "hermes-agent", "model": self.model, + "mode": expected_mode, "version": health.get("version", "unknown")} + + async def execute(self, text, owner, turn): + try: + # Recheck the actual local service before each inference, including reconnects. + await self.probe() + digest = hashlib.sha256((owner + "\0" + turn).encode()).hexdigest() + data = await self._json("POST", "/v1/chat/completions", + headers={"Idempotency-Key": "hn-" + digest}, + json={"model": self.model, "stream": False, + "messages": [{"role": "user", "content": text}]}) + choices = data.get("choices") + if not isinstance(choices, list) or not choices or not isinstance(choices[0], dict): + raise NativeAPIError("native completion choices missing") + choice = choices[0] + message = choice.get("message", {}) + if (choice.get("finish_reason") != "stop" or message.get("tool_calls") + or not isinstance(message.get("content"), str) + or data.get("hermes", {}).get("failed") + or data.get("hermes", {}).get("partial")): + raise NativeAPIError("native completion incomplete or unexpected tool call") + return message["content"][:16000] + except NativeAPIError as error: + # Safe generated error strings only; never echo upstream model/API errors. + return "海诺原生接口未完成请求:" + str(error) + "。未自动重试。" + + async def close(self): + await self.http.aclose() diff --git a/deployable/relay/longconn.py b/deployable/relay/longconn.py new file mode 100644 index 0000000..6d7be70 --- /dev/null +++ b/deployable/relay/longconn.py @@ -0,0 +1,83 @@ +"""Optional official SDK event source, private platform domain explicitly configured.""" +import json +import logging +import os +import threading +import time + +import httpx +import redis +from .config import Settings + + +def disable_proxy_environment(): + """This dedicated SDK worker must connect directly, including discovery and WS.""" + for key in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "http_proxy", "https_proxy", "all_proxy"): + os.environ.pop(key, None) + os.environ["NO_PROXY"] = "*" + os.environ["no_proxy"] = "*" + + +def main(): + disable_proxy_environment() + import lark_oapi as lark + s = Settings(role="longconn") + if not s.feishu_enabled or s.feishu_event_source != "longconn": + raise RuntimeError("platform is disabled") + # The SDK may log websocket URLs, tokens or event bodies. Disable its logging entirely. + logging.disable(logging.CRITICAL) + r = redis.Redis.from_url(s.redis_url) + lease = r.lock("hn:longconn:leader", timeout=30, thread_local=False) + if not lease.acquire(blocking=False): + raise RuntimeError("another long-connection consumer owns the lease") + url = os.environ.get("HN_CONNECTOR_INTERNAL_URL", "http://connector:8000") + client = httpx.Client(base_url=url, timeout=10, follow_redirects=False, trust_env=False) + done = threading.Event() + + def maintain_lease(): + while not done.wait(8): + try: + lease.extend(30, replace_ttl=True) + except Exception: + # Fail closed. Continuing after lease loss could create two consumers. + os._exit(2) + + threading.Thread(target=maintain_lease, daemon=True).start() + + def received(event): + data = json.loads(lark.JSON.marshal(event)) + response = client.post("/internal/events", headers={"X-Internal-Secret": s.internal_secret}, json=data) + if response.status_code != 200: + raise RuntimeError("connector did not accept event") + print(json.dumps({"event": "platform_event_accepted", "type": data.get("header", {}).get("event_type")}), flush=True) + + handler = lark.EventDispatcherHandler.builder("", "").register_p2_im_message_receive_v1(received).build() + class ObservableClient(lark.ws.Client): + async def _connect(self): + try: + await super()._connect() + if self._conn is not None: + print(json.dumps({"event": "platform_connected", "api_host": s.feishu_api_base}), flush=True) + except Exception as error: + print(json.dumps({"event": "platform_connect_failed", "error_type": type(error).__name__}), flush=True) + raise + + try: + # domain is an actual supported SDK argument; no public-cloud-domain monkey patch. + ObservableClient(s.feishu_app_id, s.feishu_app_secret, event_handler=handler, + domain=s.feishu_api_base.rstrip("/"), log_level=lark.LogLevel.ERROR).start() + finally: + done.set() + client.close() + try: + lease.release() + except redis.exceptions.LockError: + pass + + +if __name__ == "__main__": + try: + main() + except Exception as error: + print(json.dumps({"event": "platform_consumer_stopped", "error_type": type(error).__name__}), flush=True) + raise SystemExit(1) from None diff --git a/deployable/relay/oauth.py b/deployable/relay/oauth.py new file mode 100644 index 0000000..1b35a14 --- /dev/null +++ b/deployable/relay/oauth.py @@ -0,0 +1,144 @@ +import asyncio +import hashlib +import hmac +import secrets +import time +from dataclasses import dataclass +from urllib.parse import urlencode + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .platform import PlatformError + + +@dataclass +class Transaction: + secret_hash: str + expires: float + status: str = "waiting" + result: dict | None = None + owner: str = "" + + +class Empty(BaseModel): + host_id: str = "" + proof: dict = Field(default_factory=dict) + + +class Refresh(BaseModel): + refresh_token: str = Field(min_length=1, max_length=4096) + + +class OAuth: + """A single connector process owns transactions and tokens in memory only.""" + def __init__(self, settings, redis, platform): + self.settings, self.redis, self.platform = settings, redis, platform + self.pending: dict[str, Transaction] = {} + self.lock = asyncio.Lock() + self.router = APIRouter() + self.router.add_api_route("/oauth/start", self.start, methods=["POST"], status_code=201) + self.router.add_api_route("/oauth/cb", self.callback, methods=["GET"]) + self.router.add_api_route("/oauth/claim", self.claim, methods=["GET"]) + self.router.add_api_route("/oauth/pending", self.status, methods=["GET"]) + self.router.add_api_route("/oauth/refresh", self.refresh, methods=["POST"]) + + def prune(self): + now = time.monotonic() + for state in list(self.pending): + if self.pending[state].expires <= now: + del self.pending[state] + + def authenticated(self, state, secret): + self.prune() + item = self.pending.get(state) + digest = hashlib.sha256((secret or "").encode()).hexdigest() + if not item or not hmac.compare_digest(item.secret_hash, digest): + raise HTTPException(404, "transaction not found") + return item + + async def start(self, body: Empty, request: Request): + if not self.settings.feishu_enabled or not self.settings.oauth_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.redis, "oauth:start:" + peer(request), 20) + owner = "" + if body.host_id: + from .work_api import device_owner + owner = await device_owner(self.settings, self.redis, self.store, body.host_id, + body.proof, "work-oauth-v1", {}) + async with self.lock: + self.prune() + if len(self.pending) >= self.settings.max_pending: + raise HTTPException(503, "too many pending transactions") + state, secret = secrets.token_urlsafe(32), secrets.token_urlsafe(32) + self.pending[state] = Transaction(hashlib.sha256(secret.encode()).hexdigest(), time.monotonic() + 600, owner=owner) + redirect = self.settings.public_url.rstrip("/") + "/oauth/cb" + parameters = {"app_id": self.settings.feishu_app_id, "redirect_uri": redirect, "state": state} + if self.settings.feishu_oauth_scopes: + parameters["scope"] = self.settings.feishu_oauth_scopes + url = self.settings.feishu_authorize_url + "?" + urlencode(parameters) + return no_cache(JSONResponse({"state": state, "claim_secret": secret, "authorization_url": url, + "redirect_uri": redirect, "expires_in": 600, + "code_ttl": 60, "poll_interval": 2}, status_code=201)) + + async def callback(self, request: Request, state: str = "", code: str = "", error: str = ""): + await rate_limit(self.redis, "oauth:cb:" + peer(request)) + async with self.lock: + self.prune() + item = self.pending.get(state) + if error and item and item.status == "waiting": + item.status = "failed" + return no_cache(HTMLResponse("

授权未完成,请返回客户端重新授权。

", status_code=400)) + if not code or len(code) > 4096 or not item or item.status != "waiting": + raise HTTPException(400, "invalid callback") + item.status = "exchanging" + try: + result = await self.platform.exchange(code) + info = await self.platform.user_info(result["user_access_token"]) + if not info.get("open_id") or (item.owner and info["open_id"] != item.owner): + raise PlatformError("authorization user mismatch") + result["open_id"] = info["open_id"] + except PlatformError: + async with self.lock: + item.status = "failed" + raise HTTPException(502, "authorization exchange failed") from None + async with self.lock: + if state not in self.pending or item.expires <= time.monotonic(): + self.pending.pop(state, None) + raise HTTPException(400, "authorization expired") + item.result, item.status = result, "ready" + item.expires = min(item.expires, time.monotonic() + 60) + return no_cache(HTMLResponse("

授权完成,请返回客户端。

")) + + async def claim(self, request: Request, state: str): + await rate_limit(self.redis, "oauth:claim:" + peer(request)) + async with self.lock: + item = self.authenticated(state, request.headers.get("X-OAuth-Claim-Secret")) + if item.status == "failed": + del self.pending[state] + raise HTTPException(502, "authorization failed") + if item.status != "ready": + return no_cache(JSONResponse({"status": "waiting", "retry_after": 2}, status_code=202)) + del self.pending[state] + return no_cache(JSONResponse({"status": "claimed", **item.result})) + + async def status(self, request: Request, state: str): + await rate_limit(self.redis, "oauth:pending:" + peer(request)) + async with self.lock: + item = self.authenticated(state, request.headers.get("X-OAuth-Claim-Secret")) + return no_cache(JSONResponse({"status": item.status})) + + async def refresh(self, body: Refresh, request: Request): + if not self.settings.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.redis, "oauth:refresh:" + peer(request), 20) + # Lock and consume before the upstream call; ambiguous failures never retry an old refresh token. + digest = hashlib.sha256(body.refresh_token.encode()).hexdigest() + if not await self.redis.set("hn:refresh:" + digest, "used", nx=True, ex=2592000): + raise HTTPException(409, "refresh already attempted; use returned token or reauthorize") + try: + result = await self.platform.refresh(body.refresh_token) + except PlatformError: + raise HTTPException(502, "refresh failed; reauthorize") from None + return no_cache(JSONResponse(result)) diff --git a/deployable/relay/platform.py b/deployable/relay/platform.py new file mode 100644 index 0000000..a43a181 --- /dev/null +++ b/deployable/relay/platform.py @@ -0,0 +1,92 @@ +import asyncio +import json +import time +import httpx + + +class PlatformError(Exception): + pass + + +class Feishu: + def __init__(self, settings, transport=None): + self.settings = settings + self.http = httpx.AsyncClient(base_url=settings.feishu_api_base.rstrip("/"), + timeout=15, follow_redirects=False, transport=transport, trust_env=False) + self._token = None + self._expires = 0 + self._tenant_token = None + self._tenant_expires = 0 + self._lock = asyncio.Lock() + + async def close(self): + await self.http.aclose() + + async def _json(self, method, path, **kwargs): + try: + response = await self.http.request(method, path, **kwargs) + response.raise_for_status() + data = response.json() + if data.get("code", 0) != 0: + raise PlatformError("platform rejected request") + return data + except (httpx.HTTPError, ValueError, KeyError) as exc: + # Never propagate upstream bodies or credentials to caller/logs. + raise PlatformError("platform request failed") from None + + async def app_token(self): + async with self._lock: + if self._token and self._expires > time.monotonic() + 1800: + return self._token + data = await self._json("POST", "/open-apis/auth/v3/app_access_token/internal", + json={"app_id": self.settings.feishu_app_id, + "app_secret": self.settings.feishu_app_secret}) + self._token = data["app_access_token"] + self._expires = time.monotonic() + int(data.get("expire", 7200)) + return self._token + + async def exchange(self, code): + token = await self.app_token() + data = await self._json("POST", "/open-apis/authen/v1/oidc/access_token", + headers={"Authorization": f"Bearer {token}"}, + json={"grant_type": "authorization_code", "code": code}) + return self.normalize(data["data"]) + + async def tenant_token(self): + async with self._lock: + if self._tenant_token and self._tenant_expires > time.monotonic() + 1800: + return self._tenant_token + data = await self._json("POST", "/open-apis/auth/v3/tenant_access_token/internal", + json={"app_id": self.settings.feishu_app_id, + "app_secret": self.settings.feishu_app_secret}) + self._tenant_token = data["tenant_access_token"] + self._tenant_expires = time.monotonic() + int(data.get("expire", 7200)) + return self._tenant_token + + async def refresh(self, refresh_token): + token = await self.app_token() + data = await self._json("POST", "/open-apis/authen/v1/oidc/refresh_access_token", + headers={"Authorization": f"Bearer {token}"}, + json={"grant_type": "refresh_token", "refresh_token": refresh_token}) + result = self.normalize(data["data"]) + info = await self.user_info(result["user_access_token"]) + result["open_id"] = info["open_id"] + return result + + @staticmethod + def normalize(data): + return {"user_access_token": data["access_token"], "refresh_token": data.get("refresh_token", ""), + "expires_in": data.get("expires_in", data.get("expire", 0)), + "refresh_expires_in": data.get("refresh_expires_in", 0), "open_id": data.get("open_id", "")} + + async def user_info(self, token): + data = await self._json("GET", "/open-apis/authen/v1/user_info", + headers={"Authorization": f"Bearer {token}"}) + return data["data"] + + async def reply(self, message_id, text): + # Reply to the original message only: no caller-controlled recipient. + token = await self.tenant_token() + await self._json("POST", f"/open-apis/im/v1/messages/{message_id}/reply", + headers={"Authorization": f"Bearer {token}"}, + json={"msg_type": "text", "content": json.dumps({"text": text}, ensure_ascii=False)}) diff --git a/deployable/relay/process_lock.py b/deployable/relay/process_lock.py new file mode 100644 index 0000000..c37da53 --- /dev/null +++ b/deployable/relay/process_lock.py @@ -0,0 +1,46 @@ +"""Per-device advisory process lock: same-user GUI and CLI cannot race.""" +import os +from pathlib import Path + + +class DeviceAlreadyRunning(RuntimeError): + pass + + +class DeviceProcessLock: + def __init__(self, directory): + self.path = Path(directory) / "bridge.lock" + self.file = None + + def __enter__(self): + self.path.parent.mkdir(parents=True, exist_ok=True) + self.file = self.path.open("a+b") + self.file.seek(0, 2) + if self.file.tell() == 0: + self.file.write(b"0") + self.file.flush() + self.file.seek(0) + try: + if os.name == "nt": + import msvcrt + msvcrt.locking(self.file.fileno(), msvcrt.LK_NBLCK, 1) + else: + import fcntl + fcntl.flock(self.file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) + except OSError: + self.file.close() + self.file = None + raise DeviceAlreadyRunning("该设备已有桥接进程运行,请勿重复启动。") from None + return self + + def __exit__(self, *args): + if self.file: + self.file.seek(0) + if os.name == "nt": + import msvcrt + msvcrt.locking(self.file.fileno(), msvcrt.LK_UNLCK, 1) + else: + import fcntl + fcntl.flock(self.file.fileno(), fcntl.LOCK_UN) + self.file.close() + self.file = None diff --git a/deployable/relay/router.py b/deployable/relay/router.py new file mode 100644 index 0000000..e8d91e1 --- /dev/null +++ b/deployable/relay/router.py @@ -0,0 +1,167 @@ +import asyncio +import contextlib +import json +import secrets +import time +from contextlib import asynccontextmanager + +from fastapi import FastAPI, WebSocket, WebSocketDisconnect +from redis.asyncio import Redis +from redis.exceptions import WatchError + +from .config import Settings +from .crypto import fingerprint, raw_public, verified, verify_device + + +async def compare_delete(redis, key, expected): + async with redis.pipeline() as pipe: + try: + await pipe.watch(key) + if await pipe.get(key) != expected: + return False + pipe.multi() + pipe.delete(key) + await pipe.execute() + return True + except WatchError: + return False + + +async def compare_expire(redis, key, expected, ttl): + async with redis.pipeline() as pipe: + try: + await pipe.watch(key) + if await pipe.get(key) != expected: + return False + pipe.multi() + pipe.expire(key, ttl) + await pipe.execute() + return True + except WatchError: + return False + + +def create_app(settings=None, redis=None): + s = settings or Settings(role="router") + r = redis or Redis.from_url(s.redis_url, decode_responses=True) + ca = s.ca_cert.read_bytes() + active = set() + + @asynccontextmanager + async def lifespan(app): + await r.ping() + yield + if redis is None: + await r.aclose() + + app = FastAPI(title="Hainuo B2 router", lifespan=lifespan, docs_url=None, redoc_url=None) + app.state.redis = r + + @app.get("/health") + async def health(): + return {"status": "alive", "role": "router"} + + @app.get("/ready") + async def ready(): + await r.ping() + return {"status": "ready", "connections": len(active)} + + @app.websocket("/device/ws") + async def device(ws: WebSocket): + from .common import rate_limit + from fastapi import HTTPException + await ws.accept() + if len(active) >= s.max_connections: + await ws.close(1013) + return + ip = ws.client.host if ws.client else "unknown" + try: + await rate_limit(r, "ws:" + ip, 30) + except HTTPException: + await ws.close(1008) + return + challenge = secrets.token_urlsafe(32) + host = session = presence = None + tasks = [] + pub = None + try: + await ws.send_json({"type": "challenge", "challenge": challenge}) + hello = await asyncio.wait_for(ws.receive_json(), timeout=10) + if len(json.dumps(hello)) > 20000: + raise ValueError("oversized hello") + host = hello["host_id"] + if not isinstance(host, str) or len(host) != 32 or any(c not in "0123456789abcdef" for c in host): + raise ValueError("invalid host") + public = verify_device(hello["cert"], ca, host) + proof = verified(hello["proof"], public) + if proof["challenge"] != challenge or proof["host_id"] != host or abs(time.time() - proof["ts"]) > 90: + raise ValueError("invalid proof") + if len(proof["kx"]) > 64 or len(proof["session_id"]) > 64: + raise ValueError("invalid session") + fp = fingerprint(hello["cert"]) + if await r.get("hn:device:" + host) != fp or await r.exists("hn:revoked:" + host): + raise ValueError("unregistered/revoked") + session = proof["session_id"] + presence = json.dumps({"cert": hello["cert"], "proof": hello["proof"], "challenge": challenge}) + if not await r.set("hn:online:" + host, presence, nx=True, ex=30): + raise ValueError("already connected") + active.add(session) + pub = r.pubsub() + await pub.subscribe("hn:down:" + host, "hn:kill:" + host) + await ws.send_json({"type": "connected", "session_id": session}) + + async def downstream(): + async for msg in pub.listen(): + if msg["type"] != "message": + continue + if msg["channel"] == "hn:kill:" + host: + await ws.close(1008) + return + await ws.send_text(msg["data"]) + + async def heartbeat(): + while True: + await asyncio.sleep(8) + if await r.exists("hn:revoked:" + host) or not await compare_expire(r, "hn:online:" + host, presence, 30): + await ws.close(1008) + return + + async def upstream(): + while True: + text = await ws.receive_text() + if len(text.encode()) > s.max_body: + raise ValueError("oversized frame") + data = json.loads(text) + if data.get("type") == "ping": + continue + await rate_limit(r, "ws:reply:" + host, 120) + turn = data.get("turn_id", "") + if data.get("type") != "reply" or await r.get(f"hn:inflight:{host}:{turn}") != session: + raise ValueError("unsolicited reply") + envelope = data["envelope"] + verified(envelope, public) + if envelope["host_id"] != host or envelope["session_id"] != session: + raise ValueError("wrong reply destination") + await r.publish("hn:replies", json.dumps(data)) + + tasks = [asyncio.create_task(x()) for x in (downstream, heartbeat, upstream)] + done, _ = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED) + for task in done: + task.result() + except (WebSocketDisconnect, asyncio.TimeoutError, ValueError, KeyError, TypeError): + with contextlib.suppress(Exception): + await ws.close(1008) + except Exception: + with contextlib.suppress(Exception): + await ws.close(1011) + finally: + for task in tasks: + task.cancel() + await asyncio.gather(*tasks, return_exceptions=True) + if pub: + await pub.aclose() + if presence: + await compare_delete(r, "hn:online:" + host, presence) + active.discard(session) + + return app diff --git a/deployable/relay/store.py b/deployable/relay/store.py new file mode 100644 index 0000000..9d6f8ef --- /dev/null +++ b/deployable/relay/store.py @@ -0,0 +1,97 @@ +"""Connector-owned persistent metadata. Never stores message/file/token bodies.""" +import sqlite3 +import time +import psycopg +from psycopg.rows import dict_row + +INTEGRITY_ERRORS = (sqlite3.IntegrityError, psycopg.IntegrityError) + + +class Postgres: + def __init__(self, url): + self.url = url + self.conn = psycopg.connect(url, autocommit=True, row_factory=dict_row) + self.transaction = None + + def execute(self, sql, params=()): + if self.conn.closed or self.conn.broken: + self.conn = psycopg.connect(self.url, autocommit=True, row_factory=dict_row) + return self.conn.execute(sql.replace("?", "%s"), params) + + def __enter__(self): + self.transaction = self.conn.transaction() + self.transaction.__enter__() + return self + + def __exit__(self, *args): + return self.transaction.__exit__(*args) + + def close(self): + self.conn.close() + + +class Store: + def __init__(self, path, database_url=""): + path.parent.mkdir(parents=True, exist_ok=True) + schema = """ + CREATE TABLE IF NOT EXISTS devices( + host_id TEXT PRIMARY KEY, cert TEXT NOT NULL, fingerprint TEXT NOT NULL, + owner TEXT UNIQUE, revoked INTEGER NOT NULL DEFAULT 0); + CREATE TABLE IF NOT EXISTS turns( + turn_id TEXT PRIMARY KEY, owner TEXT NOT NULL, host_id TEXT NOT NULL, + status TEXT NOT NULL, created DOUBLE PRECISION NOT NULL); + CREATE TABLE IF NOT EXISTS audit( + id INTEGER PRIMARY KEY, ts DOUBLE PRECISION NOT NULL, actor TEXT NOT NULL, + action TEXT NOT NULL, resource TEXT NOT NULL, result TEXT NOT NULL); + """ + if database_url: + self.db = Postgres(database_url) + self.db.conn.execute(schema.replace("id INTEGER PRIMARY KEY", "id BIGSERIAL PRIMARY KEY")) + else: + self.db = sqlite3.connect(path, check_same_thread=False) + self.db.row_factory = sqlite3.Row + self.db.executescript("PRAGMA journal_mode=WAL;" + schema) + + def device(self, host): + row = self.db.execute("SELECT * FROM devices WHERE host_id=?", (host,)).fetchone() + return dict(row) if row else None + + def by_owner(self, owner): + row = self.db.execute("SELECT * FROM devices WHERE owner=? AND revoked=0", (owner,)).fetchone() + return dict(row) if row else None + + def add_device(self, host, cert, fp): + with self.db: + self.db.execute("INSERT INTO devices(host_id,cert,fingerprint) VALUES(?,?,?)", (host, cert, fp)) + + def bind(self, host, owner): + with self.db: + # Conditional update also protects against two connectors racing for the same device. + count = self.db.execute("UPDATE devices SET owner=? WHERE host_id=? AND owner IS NULL AND revoked=0", (owner, host)).rowcount + if count != 1: + raise ValueError("device unavailable or already bound") + + def unbind(self, owner): + with self.db: + self.db.execute("UPDATE devices SET owner=NULL WHERE owner=?", (owner,)) + + def revoke(self, host): + with self.db: + self.db.execute("UPDATE devices SET revoked=1 WHERE host_id=?", (host,)) + + def begin_turn(self, turn, owner, host): + with self.db: + return self.db.execute("INSERT INTO turns VALUES(?,?,?,?,?) ON CONFLICT(turn_id) DO NOTHING", + (turn, owner, host, "inflight", time.time())).rowcount == 1 + + def finish(self, turn, status): + with self.db: + self.db.execute("UPDATE turns SET status=? WHERE turn_id=?", (status, turn)) + + def audit(self, actor, action, resource, result): + with self.db: + self.db.execute("INSERT INTO audit(ts,actor,action,resource,result) VALUES(?,?,?,?,?)", + (time.time(), actor, action, resource, result)) + + def close(self): + self.db.close() diff --git a/deployable/relay/web/app.js b/deployable/relay/web/app.js new file mode 100644 index 0000000..26219c2 --- /dev/null +++ b/deployable/relay/web/app.js @@ -0,0 +1,117 @@ +"use strict"; +const byId = id => document.getElementById(id); +const statusNode = byId("status"); +let sending = false; +let loggedIn = false; +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); +function note(text) { statusNode.textContent = text; } +function addMessage(text, role) { + const item = document.createElement("div"); + item.className = "message " + role; + item.textContent = text; + byId("messages").appendChild(item); + item.scrollIntoView({block:"nearest"}); +} +async function refresh() { + try { + const response = await fetch("/web/session", {cache:"no-store", credentials:"same-origin"}); + if (!response.ok) { + loggedIn = false; + byId("pair-panel").hidden = false; + byId("chat-panel").hidden = true; + note(response.status === 401 ? "请使用PC上的一次性配对码连接。" : "会话服务暂不可用。"); + return; + } + const data = await response.json(); + loggedIn = true; + byId("pair-panel").hidden = true; + byId("chat-panel").hidden = false; + byId("device-state").textContent = data.online ? "你的电脑已在线" : "你的电脑当前离线"; + if (!sending) note(data.online ? "已连接本人设备,可以开始对话。" : "请在PC启动桥接端,随后再发送消息。"); + } catch (_) { if (!sending) note("网络连接失败,请检查到中继的连接。"); } +} +byId("pair-form").addEventListener("submit", async event => { + event.preventDefault(); + byId("pair-button").disabled = true; + try { + const response = await fetch("/web/claim", {method:"POST", credentials:"same-origin", + headers:{"Content-Type":"application/json"}, body:JSON.stringify({code:byId("code").value.trim()})}); + if (!response.ok) throw new Error(response.status === 404 ? "配对码已过期或已使用,请在PC重新生成。" : "配对失败,请确认地址、配对码和设备绑定。"); + byId("code").value = ""; + await refresh(); + } catch (error) { note(error.message); } + finally { byId("pair-button").disabled = false; } +}); +byId("chat-form").addEventListener("submit", async event => { + event.preventDefault(); + if (sending || !loggedIn) return; + const text = byId("message").value.trim(); + if (!text) return; + const bytes = new Uint8Array(16); + crypto.getRandomValues(bytes); + const requestId = Array.from(bytes, n => n.toString(16).padStart(2,"0")).join(""); + sending = true; + byId("send").disabled = true; + byId("logout").disabled = true; + addMessage(text, "user"); + byId("message").value = ""; + note("正在等待你的电脑和模型返回结果…"); + const controller = new AbortController(); + const timer = setTimeout(() => controller.abort(), 200000); + try { + const response = await fetch("/web/chat", {method:"POST", credentials:"same-origin", signal:controller.signal, + headers:{"Content-Type":"application/json"}, body:JSON.stringify({text, request_id:requestId})}); + if (!response.ok) throw new Error(response.status === 401 ? "网页会话已失效,请重新配对。" : "请求失败,HTTP " + response.status + "。请核查执行状态后再决定是否重发。"); + const data = await response.json(); + addMessage(data.result || "未收到文本结果。", data.status === "completed" ? "assistant" : "notice"); + note("请求状态:" + data.status); + } catch (error) { + addMessage(error.name === "AbortError" ? "等待超时,执行状态未知。系统没有自动重发。" : error.message, "notice"); + note("请求ID:" + requestId + "。请保留此ID排查,勿盲目重发。"); + } finally { + clearTimeout(timer); sending = false; + byId("send").disabled = false; byId("logout").disabled = false; + } +}); +byId("logout").addEventListener("click", async () => { + try { + const response = await fetch("/web/logout", {method:"POST", credentials:"same-origin"}); + if (!response.ok) throw new Error("退出失败,请稍后重试。"); + byId("messages").replaceChildren(); + await refresh(); + } catch (error) { note(error.message); } +}); +byId("login").addEventListener("click", async () => { + const popup = window.open("about:blank", "hainuo_authorize"); + if (!popup) { note("请允许打开授权窗口。"); return; } + popup.opener = null; + byId("login").disabled = true; + try { + const started = await fetch("/oauth/start", {method:"POST", headers:{"Content-Type":"application/json"}, body:"{}", cache:"no-store"}); + if (!started.ok) throw new Error("海能Work授权尚未配置,可先使用PC配对码。"); + const transaction = await started.json(); + const url = new URL(transaction.authorization_url); + if (url.protocol !== "https:") throw new Error("授权地址必须使用HTTPS。"); + popup.location = url.href; + note("请在授权窗口完成登录。"); + const until = Date.now() + transaction.expires_in * 1000; + let token; + while (Date.now() < until) { + await sleep(2000); + const response = await fetch("/oauth/claim?state=" + encodeURIComponent(transaction.state), { + headers:{"X-OAuth-Claim-Secret":transaction.claim_secret}, cache:"no-store"}); + if (response.status === 202) continue; + if (!response.ok) throw new Error("授权已失效,请重新开始。"); + token = (await response.json()).user_access_token; + break; + } + if (!token) throw new Error("等待授权超时。"); + const response = await fetch("/web/platform-session", {method:"POST", headers:{Authorization:"Bearer " + token}}); + token = null; + if (!response.ok) throw new Error("请先在海能Work完成本人设备绑定。"); + await refresh(); + } catch (error) { note(error.message); try { popup.close(); } catch (_) {} } + finally { byId("login").disabled = false; } +}); +refresh(); +setInterval(() => { if (!sending) refresh(); }, 15000); diff --git a/deployable/relay/web/index.html b/deployable/relay/web/index.html new file mode 100644 index 0000000..bf469d9 --- /dev/null +++ b/deployable/relay/web/index.html @@ -0,0 +1,15 @@ + + +海诺 · 我的工作电脑 +
海诺 HAINUO 试点 · 纯文本
+

连接你的
工作电脑。

电脑上的助手,随时与你对话。先完成本人设备配对,再开始工作。

+
01 / 连接本人设备

输入电脑上的配对码

+

在已绑定的PC运行网页配对命令,将生成的一次性配对码填在这里。有效期两分钟,请勿转发。

+
+
使用海能Work授权

需要管理员已配置平台应用与OAuth回调。

+ +

正在检查网页会话…

+ +
diff --git a/deployable/relay/web/style.css b/deployable/relay/web/style.css new file mode 100644 index 0000000..a33d19c --- /dev/null +++ b/deployable/relay/web/style.css @@ -0,0 +1,3 @@ +:root{color-scheme:light;font-family:system-ui,-apple-system,"Segoe UI","Microsoft YaHei",sans-serif;color:#17372c;background:#f2f4ed}*{box-sizing:border-box}body{margin:0}main{max-width:820px;margin:0 auto;padding:60px 26px}.mark{font-size:13px;letter-spacing:.16em;color:#557764;font-weight:700}h1{font-size:clamp(42px,8vw,70px);letter-spacing:-.05em;line-height:1.12;font-weight:650;margin:40px 0 22px}.intro{font-size:17px;line-height:1.8;color:#56675a;max-width:520px}.card{background:#fff;border:1px solid #dbe3d8;border-radius:20px;padding:30px;margin:40px 0 24px;box-shadow:0 12px 44px #18372c06}.step{font-size:12px;letter-spacing:.1em;color:#6d806c}h2{font-size:24px;margin:16px 0}.card p{line-height:1.8;color:#5b6c60}button{background:#214e3a;color:white;border:0;border-radius:10px;font:inherit;font-weight:600;padding:16px 20px;width:100%;text-align:left;cursor:pointer;margin-top:16px}button span{float:right}button:disabled{opacity:.5;cursor:wait}button:focus-visible,a:focus-visible{outline:3px solid #d0a349;outline-offset:4px}#status{font-size:13px;margin-bottom:0}#open{display:block;margin-top:22px;color:#214e3a;font-weight:600}#open[hidden]{display:none}footer{color:#768373;font-size:12px;line-height:1.8}@media(max-width:480px){main{padding-top:36px}.card{padding:22px}h1{margin-top:28px}} + +[hidden]{display:none!important}.badge{float:right;font-size:11px;letter-spacing:0;background:#e4ebdf;border-radius:20px;padding:6px 10px}label{display:block;font-size:13px;color:#526c5b;margin:12px 0 8px}input,textarea{font:inherit;width:100%;border:1px solid #ccd8ce;border-radius:10px;padding:14px;color:#17372c;background:#fafcf8;resize:vertical}input:focus,textarea:focus{outline:2px solid #6c987b;outline-offset:2px}details{margin-top:24px;font-size:13px;color:#587561}summary{cursor:pointer}.chat-head{display:flex;align-items:center;justify-content:space-between;gap:20px}.secondary{width:auto;background:#edf2e9;color:#31523f;padding:9px 12px;font-size:12px;margin:0}.hint{font-size:13px}#messages{display:flex;flex-direction:column;gap:12px;margin:22px 0;max-height:420px;overflow:auto}.message{padding:14px 16px;border-radius:12px;line-height:1.8;white-space:pre-wrap;overflow-wrap:anywhere;font-size:15px}.message.user{background:#e5eee2;margin-left:36px}.message.assistant{background:#f2f5ef;margin-right:24px}.message.notice{background:#fbf4e5;color:#735c2e}#status{font-size:13px;color:#5f745f;line-height:1.8}#chat-panel h2{font-size:20px}footer{max-width:640px} diff --git a/deployable/relay/web_session.py b/deployable/relay/web_session.py new file mode 100644 index 0000000..1ac9adf --- /dev/null +++ b/deployable/relay/web_session.py @@ -0,0 +1,183 @@ +"""Pilot H5 pairing and conversation. Credentials never appear in URLs.""" +import hashlib +import json +import secrets +import time +from urllib.parse import urlsplit + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import JSONResponse +from pydantic import BaseModel, Field + +from .common import no_cache, peer, rate_limit +from .crypto import sign, verified, verify_device +from .platform import PlatformError + +COOKIE = "hn_web_session" + + +def digest(value): + return hashlib.sha256(value.encode()).hexdigest() + + +def origin_tuple(value): + try: + parts = urlsplit(value) + if parts.scheme not in {"https", "http"} or not parts.hostname or parts.username or parts.password: + return None + return parts.scheme, parts.hostname.lower(), parts.port or (443 if parts.scheme == "https" else 80) + except ValueError: + return None + + +class PairProof(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Claim(BaseModel): + code: str = Field(min_length=12, max_length=128) + + +class Chat(BaseModel): + text: str = Field(min_length=1, max_length=8000) + request_id: str = Field(pattern=r"^[a-f0-9]{32}$") + + +class WebSessions: + def __init__(self, settings, redis, store, broker, platform): + self.s, self.r, self.store, self.broker, self.platform = settings, redis, store, broker, platform + self.router = APIRouter() + for path, method, handler in [ + ("/device/web-pair", "POST", self.device_pair), + ("/web/claim", "POST", self.claim), + ("/web/session", "GET", self.session), + ("/web/platform-session", "POST", self.platform_login), + ("/web/chat", "POST", self.chat), + ("/web/logout", "POST", self.logout), + ]: + self.router.add_api_route(path, handler, methods=[method]) + + def check_origin(self, request): + actual = origin_tuple(request.headers.get("Origin", "")) + if not actual or actual != origin_tuple(self.s.public_url): + raise HTTPException(403, "same-origin browser request required") + + async def valid_device(self, record): + device = self.store.device(record["host_id"]) + if (not device or device["revoked"] or device["owner"] != record["owner"] + or device["fingerprint"] != record["fingerprint"]): + raise HTTPException(401, "device binding changed; pair again") + generation = await self.r.get("hn:web-generation:" + record["host_id"]) or "0" + if record.get("generation") != generation: + raise HTTPException(401, "device binding changed; pair again") + return device + + async def device_pair(self, body: PairProof, request: Request): + await rate_limit(self.r, "web-pair-ip:" + peer(request), 20) + device = self.store.device(body.host_id) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "active bound device required") + try: + key = verify_device(device["cert"], self.s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["owner"] != device["owner"] + or proof["purpose"] != "h5-pair-v1" or abs(time.time() - proof["ts"]) > 90 + or not isinstance(proof["nonce"], str) or len(proof["nonce"]) != 32): + raise ValueError("invalid proof") + except Exception: + raise HTTPException(403, "invalid device proof") from None + replay = "hn:web-pair-proof:" + digest(body.host_id + proof["nonce"]) + if not await self.r.set(replay, "used", nx=True, ex=180): + raise HTTPException(409, "pairing request already used") + await rate_limit(self.r, "web-pair-device:" + body.host_id, 5) + code = secrets.token_urlsafe(18) + record = {"host_id": body.host_id, "owner": device["owner"], "fingerprint": device["fingerprint"], + "generation": await self.r.get("hn:web-generation:" + body.host_id) or "0"} + await self.r.set("hn:web-code:" + digest(code), json.dumps(record), ex=120) + self.store.audit(device["owner"], "web_pair_issued", body.host_id, "success") + return no_cache(JSONResponse(sign({"host_id": body.host_id, "code": code, "expires_in": 120, + "url": self.s.public_url.rstrip("/") + "/h5"}, self.broker.signer))) + + async def new_session(self, record): + token = secrets.token_urlsafe(32) + record = {k: record[k] for k in ("host_id", "owner", "fingerprint", "generation")} + await self.r.set("hn:web-session:" + digest(token), json.dumps(record), ex=1800) + response = no_cache(JSONResponse({"status": "paired", "host_id": record["host_id"], "expires_in": 1800})) + response.set_cookie(COOKIE, token, max_age=1800, secure=True, httponly=True, samesite="strict", path="/") + return response + + async def claim(self, body: Claim, request: Request): + self.check_origin(request) + await rate_limit(self.r, "web-claim:" + peer(request), 20) + key = "hn:web-code:" + digest(body.code.strip()) + encoded = await self.r.get(key) + if not encoded: + raise HTTPException(404, "code expired or consumed") + await self.valid_device(json.loads(encoded)) + encoded = await self.r.getdel(key) + if not encoded: + raise HTTPException(404, "code already consumed") + record = json.loads(encoded) + await self.valid_device(record) + self.store.audit(record["owner"], "web_pair_claimed", record["host_id"], "success") + return await self.new_session(record) + + async def authenticated(self, request): + token = request.cookies.get(COOKIE, "") + if not token or len(token) > 128: + raise HTTPException(401, "pair this browser first") + encoded = await self.r.get("hn:web-session:" + digest(token)) + if not encoded: + raise HTTPException(401, "browser session expired") + record = json.loads(encoded) + await self.valid_device(record) + return record + + async def session(self, request: Request): + record = await self.authenticated(request) + return no_cache(JSONResponse({"authenticated": True, "host_id": record["host_id"], + "online": bool(await self.r.exists("hn:online:" + record["host_id"])), + "mode": "pilot-text-only", "relay_sees_content": True})) + + async def platform_login(self, request: Request): + self.check_origin(request) + if not self.s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.r, "web-platform:" + peer(request), 20) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await self.platform.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = self.store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "bind your device first") + return await self.new_session({**device, "generation": await self.r.get("hn:web-generation:" + device["host_id"]) or "0"}) + + async def chat(self, body: Chat, request: Request): + self.check_origin(request) + record = await self.authenticated(request) + await rate_limit(self.r, "web-chat:" + record["owner"], 20) + text = body.text.strip() + if not text: + raise HTTPException(422, "empty message") + # PC accepts only its bound owner and the existing IM read-only channel. + if not (text == "/主机名" or text.startswith(("/回显 ", "/读取 ", "/助手 "))): + text = "/助手 " + text + turn = "web_" + digest(record["owner"] + "\0" + body.request_id) + result = await self.broker.dispatch(record["owner"], text, turn) + # Revoke/unbind during a long model call invalidates the browser immediately. + await self.authenticated(request) + return no_cache(JSONResponse(result)) + + async def logout(self, request: Request): + self.check_origin(request) + token = request.cookies.get(COOKIE, "") + if token and len(token) <= 128: + await self.r.delete("hn:web-session:" + digest(token)) + response = no_cache(JSONResponse({"status": "logged_out"})) + response.delete_cookie(COOKIE, secure=True, httponly=True, samesite="strict", path="/") + return response diff --git a/deployable/relay/windows_keys.py b/deployable/relay/windows_keys.py new file mode 100644 index 0000000..0e84e19 --- /dev/null +++ b/deployable/relay/windows_keys.py @@ -0,0 +1,40 @@ +"""Current-user Windows DPAPI for device keys; never uses machine-wide scope.""" +import ctypes +import os +from ctypes import wintypes + + +class Blob(ctypes.Structure): + _fields_ = [("cbData", wintypes.DWORD), ("pbData", ctypes.POINTER(ctypes.c_ubyte))] + + +def transform(data, decrypt=False): + if os.name != "nt": + raise RuntimeError("DPAPI is supported only on Windows") + buffer = (ctypes.c_ubyte * len(data)).from_buffer_copy(data) + source = Blob(len(data), buffer) + target = Blob() + crypt = ctypes.WinDLL("crypt32", use_last_error=True) + kernel = ctypes.WinDLL("kernel32", use_last_error=True) + if decrypt: + fn = crypt.CryptUnprotectData + args = [ctypes.byref(source), None, None, None, None, 1, ctypes.byref(target)] + else: + fn = crypt.CryptProtectData + args = [ctypes.byref(source), "Hainuo device identity", None, None, None, 1, ctypes.byref(target)] + fn.restype = wintypes.BOOL + if not fn(*args): + raise ctypes.WinError(ctypes.get_last_error()) + try: + return ctypes.string_at(target.pbData, target.cbData) + finally: + kernel.LocalFree.argtypes = [ctypes.c_void_p] + kernel.LocalFree(target.pbData) + + +def protect(data): + return transform(data) + + +def unprotect(data): + return transform(data, decrypt=True) diff --git a/deployable/relay/work_api.py b/deployable/relay/work_api.py new file mode 100644 index 0000000..96a6e49 --- /dev/null +++ b/deployable/relay/work_api.py @@ -0,0 +1,152 @@ +"""Device-authenticated USER-token business gateway; no tenant-token fallback.""" +import hashlib +import json +import re +import time +from urllib.parse import unquote + +import httpx +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import JSONResponse +from pydantic import BaseModel, Field +from .common import no_cache, rate_limit +from .crypto import canonical, verified, verify_device +from .store import INTEGRITY_ERRORS + + +class DeviceProof(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class BusinessCall(DeviceProof): + method: str = Field(pattern=r"^(GET|POST|PATCH|DELETE)$") + path: str = Field(max_length=1500) + params: dict = Field(default_factory=dict) + body: dict = Field(default_factory=dict) + operation_id: str = Field(default="", max_length=100) + + +def business_allowed(method, path): + if any(x in unquote(path) for x in ("..", "\\", "%", "?", "#", "\r", "\n")): + return False + seg = r"[A-Za-z0-9_.@-]+" + cal = r"/open-apis/calendar/v4/calendars" + rules = { + "GET": [cal, cal + "/" + seg, cal + "/" + seg + "/events(?:/" + seg + ")?(?:/attendees)?", + r"/open-apis/contact/v3/users/" + seg, r"/open-apis/search/v1/user", + r"/open-apis/(?:drive/v1/files|docx/v1/documents|wiki/v2/spaces|bitable/v1/apps)(?:/" + seg + ")*"], + "POST": [cal + "/primary", cal + "/" + seg + "/events(?:/" + seg + "/attendees)?"], + "PATCH": [cal + "/" + seg + "/events/" + seg], + "DELETE": [cal + "/" + seg + "/events/" + seg, cal + "/" + seg + "/events/" + seg + "/attendees/" + seg], + } + return any(re.fullmatch(p, path) for p in rules.get(method, [])) + + +async def device_owner(settings, redis, store, host, proof, purpose, payload): + device = store.device(host) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "active bound device required") + try: + key = verify_device(device["cert"], settings.ca_cert.read_bytes(), host) + data = verified(proof, key) + if (data["host_id"] != host or data["purpose"] != purpose or abs(time.time() - data["ts"]) > 90 + or not re.fullmatch(r"[a-f0-9]{32}", data["nonce"]) + or data["payload_hash"] != hashlib.sha256(canonical(payload)).hexdigest()): + raise ValueError() + except Exception: + raise HTTPException(403, "invalid device request proof") from None + if not await redis.set("hn:work-proof:" + host + ":" + data["nonce"], "1", nx=True, ex=180): + raise HTTPException(409, "device request replayed") + return device["owner"] + + +def router(settings, redis, store, platform): + routes = APIRouter() + store.db.execute("""CREATE TABLE IF NOT EXISTS business_operations( + owner TEXT NOT NULL, operation_id TEXT NOT NULL, request_hash TEXT NOT NULL, + status TEXT NOT NULL, result_meta TEXT NOT NULL, created DOUBLE PRECISION NOT NULL, + PRIMARY KEY(owner,operation_id))""") + if not settings.database_url: + store.db.commit() + + @routes.post("/work/api") + async def call(body: BusinessCall, request: Request): + if not settings.feishu_enabled: + raise HTTPException(503, "platform disabled") + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or not 10 < len(auth) <= 8192: + raise HTTPException(401, "user access token required") + if not business_allowed(body.method, body.path): + raise HTTPException(403, "business method/path not supported") + payload = {"method": body.method, "path": body.path, "params": body.params, "body": body.body, + "operation_id": body.operation_id, "token_hash": hashlib.sha256(auth[7:].encode()).hexdigest()} + owner = await device_owner(settings, redis, store, body.host_id, body.proof, "work-api-v1", payload) + await rate_limit(redis, "work-api:" + owner, 60) + try: + info = await platform.user_info(auth[7:]) + except Exception: + raise HTTPException(401, "user token expired or invalid; refresh or authorize") from None + if info.get("open_id") != owner: + raise HTTPException(403, "token user does not match bound device owner") + write = body.method != "GET" and not body.path.endswith(("/primary", "/batch_get_id")) + digest = hashlib.sha256(canonical({k: v for k, v in payload.items() if k != "token_hash"})).hexdigest() + if write: + if not re.fullmatch(r"[A-Za-z0-9_-]{8,100}", body.operation_id): + raise HTTPException(422, "write requires stable operation_id") + try: + with store.db: + store.db.execute("INSERT INTO business_operations VALUES(?,?,?,?,?,?)", + (owner, body.operation_id, digest, "inflight", "{}", time.time())) + except INTEGRITY_ERRORS: + row = dict(store.db.execute("SELECT * FROM business_operations WHERE owner=? AND operation_id=?", + (owner, body.operation_id)).fetchone()) + if row["request_hash"] != digest: + raise HTTPException(409, "operation_id reused with different request") + return no_cache(JSONResponse({"status": "duplicate", "operation_id": body.operation_id, + "previous_status": row["status"], **json.loads(row["result_meta"])})) + params = dict(body.params) + if write and body.method == "POST" and body.path.endswith("/events"): + params["idempotency_key"] = hashlib.sha256((owner + body.operation_id).encode()).hexdigest() + try: + async with platform.http.stream(body.method, body.path, params=params, + json=body.body if body.method != "GET" else None, + headers={"Authorization": auth}) as upstream: + raw = bytearray() + async for chunk in upstream.aiter_bytes(): + raw.extend(chunk) + if len(raw) > 2 * 1024 * 1024: + raise ValueError() + data = json.loads(raw) + if not isinstance(data, dict): + raise ValueError() + code = data.get("code", 0) + if upstream.status_code != 200 or code != 0: + status = "platform_rejected" + def safe_diagnostic(value): + text = str(value) + for secret in (auth, auth[7:], settings.feishu_app_secret, settings.admin_secret, settings.internal_secret): + if secret: + text = text.replace(secret, "[redacted]") + return text[:2000] + detail = data.get("error") or {} + result = {"status": status, "http_status": upstream.status_code, "platform_code": code, + "message": "平台拒绝:请核对用户授权、应用scope、资源权限或接口版本。", + "platform_message": safe_diagnostic(data.get("msg", data.get("message", ""))), + "platform_log_id": safe_diagnostic(detail.get("log_id", "")) if isinstance(detail, dict) else "", + "permission_details": safe_diagnostic(detail.get("permission_violations", [])) if isinstance(detail, dict) else ""} + else: + status, result = "completed", {"status": "completed", "data": data.get("data", {})} + except (httpx.HTTPError, ValueError): + status = "unknown" if write else "failed" + result = {"status": status, "message": "平台请求未取得明确结果;写操作不要换新ID重试,请先核对日历。"} + if write: + event = result.get("data", {}).get("event", {}) + meta = {k: event[k] for k in ("event_id", "app_link") if k in event} + with store.db: + store.db.execute("UPDATE business_operations SET status=?,result_meta=? WHERE owner=? AND operation_id=?", + (status, json.dumps(meta), owner, body.operation_id)) + store.audit(owner, "work_api_" + body.method.lower(), body.path, status) + return no_cache(JSONResponse(result)) + + return routes diff --git a/deployable/relay/work_client.py b/deployable/relay/work_client.py new file mode 100644 index 0000000..4cc90b3 --- /dev/null +++ b/deployable/relay/work_client.py @@ -0,0 +1,190 @@ +"""PC-side user OAuth and calendar operations. Tokens are DPAPI-protected only here.""" +import hashlib +import json +import secrets +import ssl +import time +from datetime import datetime +from pathlib import Path +from urllib.parse import quote, urlsplit + +import httpx +from .crypto import canonical, load_private, sign +from .windows_keys import protect, unprotect +from .process_lock import DeviceProcessLock + + +class WorkError(RuntimeError): + pass + + +class AuthorizationRequired(WorkError): + def __init__(self, url): + self.url = url + super().__init__("需要本人Work授权,请打开授权链接,完成后重新发起原任务;尚未执行平台写操作。") + + +def atomic_secret(path, data): + temp = path.with_suffix(".tmp") + temp.write_bytes(protect(json.dumps(data, ensure_ascii=False).encode())) + temp.replace(path) + + +class WorkClient: + def __init__(self, directory, url="https://10.77.76.13", *, transport=None): + self.directory = Path(directory) + self.config = json.loads((self.directory / "device.json").read_text(encoding="utf-8-sig")) + if not self.config.get("owner"): + raise WorkError("请先绑定本人Work身份。") + context = ssl.create_default_context(cafile=str(self.directory / "server.pem")) + self.http = httpx.Client(base_url=url.rstrip("/"), verify=context, trust_env=False, + timeout=25, follow_redirects=False, transport=transport) + self.owner = self.config["owner"] + self.path = self.directory / "work-user-tokens.dpapi" + self.pending = self.directory / "work-oauth-pending.dpapi" + + def close(self): + self.http.close() + + def read(self, path): + return json.loads(unprotect(path.read_bytes())) + + def proof(self, purpose, payload): + return sign({"host_id": self.config["host_id"], "purpose": purpose, "ts": int(time.time()), + "nonce": secrets.token_hex(16), "payload_hash": hashlib.sha256(canonical(payload)).hexdigest()}, + load_private(self.directory / "device.key")) + + def authorize(self): + if self.pending.exists(): + data = self.read(self.pending) + if data.get("owner") == self.owner and data["deadline"] > time.time(): + return data["authorization_url"] + response = self.http.post("/oauth/start", json={"host_id": self.config["host_id"], + "proof": self.proof("work-oauth-v1", {})}) + if response.status_code != 201: + raise WorkError("授权事务创建失败(HTTP " + str(response.status_code) + "),请检查ECS平台配置。") + data = response.json() + if urlsplit(data["authorization_url"]).scheme != "https": + raise WorkError("授权地址必须使用HTTPS。") + data.update(owner=self.owner, deadline=time.time() + data["expires_in"]) + atomic_secret(self.pending, data) + return data["authorization_url"] + + def save_tokens(self, data): + if data.get("open_id") != self.owner or not data.get("user_access_token"): + raise WorkError("授权账号与本机Work绑定账号不一致,请使用本人账号重新授权。") + now = time.time() + data = {**data, "expires_at": now + int(data["expires_in"]), + "refresh_expires_at": now + int(data.get("refresh_expires_in", 0)), "needs_authorization": False} + atomic_secret(self.path, data) + return data + + def access_token(self): + # Shared across GUI and Agent calls; never rotate the same token concurrently. + with DeviceProcessLock(self.directory / "work-token-lock"): + if self.path.exists(): + data = self.read(self.path) + if data.get("open_id") == self.owner and not data.get("needs_authorization"): + if data["expires_at"] > time.time() + 120: + return data["user_access_token"] + if data.get("refresh_token") and data.get("refresh_expires_at", 0) > time.time(): + # Mark before network; ambiguous failure must not reuse a rotating refresh token. + atomic_secret(self.path, {**data, "needs_authorization": True}) + try: + response = self.http.post("/oauth/refresh", json={"refresh_token": data["refresh_token"]}) + if response.status_code != 200: + raise WorkError("刷新失败") + data = self.save_tokens(response.json()) + return data["user_access_token"] + except (httpx.HTTPError, ValueError, WorkError): + raise AuthorizationRequired(self.authorize()) from None + if self.pending.exists(): + pending = self.read(self.pending) + if pending.get("owner") == self.owner and pending["deadline"] > time.time(): + response = self.http.get("/oauth/claim", params={"state": pending["state"]}, + headers={"X-OAuth-Claim-Secret": pending["claim_secret"]}) + if response.status_code == 200: + data = self.save_tokens(response.json()) + self.pending.unlink(missing_ok=True) + return data["user_access_token"] + if response.status_code not in {202}: + self.pending.unlink(missing_ok=True) + raise AuthorizationRequired(self.authorize()) + + def status(self): + try: + token = self.access_token() + return {"authorized": True, "owner": self.owner, "message": "本人用户授权可用;令牌未输出。", + "local_time": datetime.now().astimezone().isoformat()} + except AuthorizationRequired as error: + return {"authorized": False, "authorization_url": error.url, "message": str(error)} + + def api(self, method, path, params=None, body=None, operation_id=""): + token = self.access_token() + payload = {"method": method, "path": path, "params": params or {}, "body": body or {}, + "operation_id": operation_id, "token_hash": hashlib.sha256(token.encode()).hexdigest()} + proof = self.proof("work-api-v1", payload) + wire = {k: v for k, v in payload.items() if k != "token_hash"} + response = self.http.post("/work/api", headers={"Authorization": "Bearer " + token}, + json={**wire, "host_id": self.config["host_id"], "proof": proof}) + if response.status_code == 401: + # Do not repeat a business write automatically, even when auth fails. + if self.path.exists(): + data = self.read(self.path) + data["expires_at"] = 0 + atomic_secret(self.path, data) + raise WorkError("用户令牌已失效。本次未重试,请检查授权后再核对操作状态。") + if response.status_code != 200: + raise WorkError("平台代理失败(HTTP " + str(response.status_code) + ");未重试。") + return response.json() + + def primary_calendar(self): + result = self.api("POST", "/open-apis/calendar/v4/calendars/primary", {"user_id_type": "open_id"}) + if result.get("status") != "completed": + return result + calendars = result.get("data", {}).get("calendars", []) + own = [c["calendar"] for c in calendars if c.get("user_id") == self.owner and c.get("calendar")] + if not own and len(calendars) == 1 and not calendars[0].get("user_id") and calendars[0].get("calendar"): + own = [calendars[0]["calendar"]] + if len(own) != 1: + raise WorkError("没有唯一确定的本人主日历,请先查询日历列表并明确选择。") + return {"status": "completed", "calendar": own[0]} + + def create_event(self, summary, start_iso, end_iso, attendee_open_ids, operation_id, description="", calendar_id=""): + import re + if not re.fullmatch(r"[A-Za-z0-9_-]{8,80}", operation_id): + raise WorkError("创建日程需8到80位稳定operation_id,重试不得换ID。") + if not summary.strip() or len(summary) > 1000: + raise WorkError("日程主题不能为空或超过1000字符。") + start, end = datetime.fromisoformat(start_iso), datetime.fromisoformat(end_iso) + if start.tzinfo is None or end.tzinfo is None or end <= start: + raise WorkError("起止时间必须包含时区且结束晚于开始;不要猜测时区或时长。") + if any(not isinstance(x, str) or not x.startswith("ou_") for x in attendee_open_ids): + raise WorkError("参会人必须为当前应用身份域的open_id,请先搜索并确认,不能把姓名/工号直接当ID。") + if not calendar_id: + primary = self.primary_calendar() + if primary.get("status") != "completed": + return primary + calendar_id = primary["calendar"]["calendar_id"] + path = "/open-apis/calendar/v4/calendars/" + quote(calendar_id, safe="") + "/events" + body = {"summary": summary, "description": description, "need_notification": True, + "start_time": {"timestamp": str(int(start.timestamp())), "timezone": "Asia/Shanghai"}, + "end_time": {"timestamp": str(int(end.timestamp())), "timezone": "Asia/Shanghai"}} + result = self.api("POST", path, {"user_id_type": "open_id"}, body, operation_id) + event = result.get("data", {}).get("event", {}) + if result.get("status") == "duplicate" and result.get("previous_status") == "completed": + event = {"event_id": result.get("event_id"), "app_link": result.get("app_link")} + elif result.get("status") != "completed": + return result + if not event.get("event_id"): + raise WorkError("平台未返回event_id,不能认定创建成功或继续邀请。") + ids = sorted(set([self.owner] + list(attendee_open_ids))) + invite = self.api("POST", path + "/" + quote(event["event_id"], safe="") + "/attendees", + {"user_id_type": "open_id"}, + {"attendees": [{"type": "user", "user_id": x} for x in ids], "need_notification": True}, + operation_id + "_attendees") + invited = invite.get("status") == "completed" or (invite.get("status") == "duplicate" and invite.get("previous_status") == "completed") + return {"status": "completed" if invited else "partial", "event_created": True, + "calendar_id": calendar_id, "event_id": event["event_id"], "app_link": event.get("app_link"), + "attendees_invited": invited, "invite_result": invite, + "message": "日程已创建并提交参会人邀请;不代表对方已接受。" if invited else "日程已创建,但邀请未完成;保留原操作ID和event_id处理,勿重新创建日程。"} diff --git a/deployable/relay/work_tools.py b/deployable/relay/work_tools.py new file mode 100644 index 0000000..b1c6da4 --- /dev/null +++ b/deployable/relay/work_tools.py @@ -0,0 +1,145 @@ +"""Hermes native tools backed by the authenticated user-token relay.""" +import json +import os +import threading +import time +from pathlib import Path +import httpx +from .work_client import WorkClient, WorkError, AuthorizationRequired + +INSTRUCTIONS = """海能Work平台能力已接入本机专用工具:work_authorization、work_calendar、work_contacts、work_api_request。 +涉及飞书/海能Work日历或文档时优先调用这些工具,勿使用manage_connections寻找公有云连接器,勿索要或搜索App Secret。 +用户token仅由本机安全存储和ECS换取,第一次需要本人打开授权链接。工具返回authorization_required时将链接完整提供给本人,不能声称已经创建。 +当前日期以work_authorization返回的local_time为准。创建日程必须确定带时区的起止时间和真实参会人open_id。 +姓名/工号须用work_contacts搜索核实,多个候选必须澄清,不能猜open_id。平台返回partial表示已创建但邀请未完成,勿再次创建。 +同一写操作的operation_id必须保持不变,网络异常不得换ID重试。日程创建成功、邀请提交成功、对方接受是不同状态。 +""" + +_watching = set() +_watch_lock = threading.Lock() + + +def watch_authorization(directory): + """Claim within the server's 60-second result TTL, without keeping tokens in ECS.""" + name = str(directory) + with _watch_lock: + if name in _watching: + return + _watching.add(name) + def poll(): + client = None + try: + client = WorkClient(directory, os.environ.get("HN_WORK_RELAY_URL", "https://10.77.76.13")) + for _ in range(295): + time.sleep(2) + try: + if client.pending.exists() and client.read(client.pending).get("deadline", 0) <= time.time(): + return + if client.status().get("authorized"): + return + except Exception: + # Another local consumer may hold the short token lock. + continue + except Exception: + pass + finally: + if client: + client.close() + with _watch_lock: + _watching.discard(name) + threading.Thread(target=poll, daemon=True, name="work-user-auth-claim").start() + + +def register(directory): + from tools.registry import registry + import toolsets + definitions = [ + ("work_authorization", "查询或发起本人海能Work用户授权,返回可点击链接和本机时间;不返回token。", { + "type": "object", "properties": {}, "additionalProperties": False}), + ("work_calendar", "本人用户身份的日历:list/primary/list_events/get_event/create_event/invite。写操作operation_id需稳定,起止时间均带时区。", { + "type": "object", "properties": { + "action": {"type": "string", "enum": ["list", "primary", "list_events", "get_event", "create_event", "invite"]}, + "calendar_id": {"type": "string"}, "event_id": {"type": "string"}, + "summary": {"type": "string"}, "description": {"type": "string"}, + "start_iso": {"type": "string", "description": "ISO8601,例如2026-10-27T09:00:00+08:00"}, + "end_iso": {"type": "string"}, "attendee_open_ids": {"type": "array", "items": {"type": "string"}}, + "operation_id": {"type": "string", "description": "8-80位稳定操作ID,同一意图重试不能更换"}, + "params": {"type": "object"}}, "required": ["action"]}), + ("work_contacts", "使用本人user token搜索参会人;query可为姓名或工号,返回候选,不能未经核实猜ID。open_id可读取具体用户信息。", { + "type": "object", "properties": {"query": {"type": "string"}, "open_id": {"type": "string"}}}), + ("work_api_request", "携本人user token代理受支持业务API(日历CRUD/邀请、通讯录查询、文档云盘知识库读取)。禁止认证接口,不支持的路径返回403。写操作必须operation_id。", { + "type": "object", "properties": {"method": {"type": "string", "enum": ["GET", "POST", "PATCH", "DELETE"]}, + "path": {"type": "string"}, "params": {"type": "object"}, "body": {"type": "object"}, + "operation_id": {"type": "string"}}, "required": ["method", "path"]}), + ] + + def handler_for(name): + def handle(args, **kwargs): + client = None + try: + client = WorkClient(directory, os.environ.get("HN_WORK_RELAY_URL", "https://10.77.76.13")) + if name == "work_authorization": + result = client.status() + if not result.get("authorized"): + watch_authorization(directory) + elif name == "work_api_request": + result = client.api(**args) + elif name == "work_contacts": + from urllib.parse import quote + result = client.api("GET", "/open-apis/contact/v3/users/" + quote(args["open_id"], safe=""), + {"user_id_type": "open_id"}) if args.get("open_id") else client.api( + "GET", "/open-apis/search/v1/user", {"query": args.get("query", ""), "page_size": 20}) + else: + result = calendar(client, args) + return json.dumps(result, ensure_ascii=False) + except AuthorizationRequired as error: + watch_authorization(directory) + return json.dumps({"status": "authorization_required", "authorization_url": error.url, "message": str(error)}, ensure_ascii=False) + except (WorkError, ValueError, KeyError) as error: + return json.dumps({"error": str(error)[:500], "status": "failed"}, ensure_ascii=False) + except Exception as error: + return json.dumps({"error": type(error).__name__ + ";操作状态未确认,未自动重试。"}, ensure_ascii=False) + finally: + if client: + client.close() + return handle + + for name, description, parameters in definitions: + registry.register(name=name, toolset="hainuo_work", schema={"name": name, "description": description, "parameters": parameters}, + handler=handler_for(name), description=description) + toolsets.TOOLSETS["hainuo_work"] = {"description": "海能Work本人用户授权及日历业务", "tools": [x[0] for x in definitions], "includes": []} + # Present these four small schemas directly; otherwise Hermes defers custom + # toolsets behind tool_search and can wrongly search a public-cloud connector. + for name, _, _ in definitions: + if name not in toolsets._HERMES_CORE_TOOLS: + toolsets._HERMES_CORE_TOOLS.append(name) + + +def calendar(client, args): + from urllib.parse import quote + action = args["action"] + if action == "primary": + return client.primary_calendar() + if action == "list": + return client.api("GET", "/open-apis/calendar/v4/calendars", args.get("params", {})) + if action == "create_event": + return client.create_event(**{k: v for k, v in args.items() if k != "action" and k != "params"}) + calendar_id = args.get("calendar_id") + if not calendar_id: + primary = client.primary_calendar() + if primary.get("status") != "completed": + return primary + calendar_id = primary["calendar"]["calendar_id"] + base = "/open-apis/calendar/v4/calendars/" + quote(calendar_id, safe="") + "/events" + if action == "list_events": + return client.api("GET", base, args.get("params", {})) + base += "/" + quote(args["event_id"], safe="") + if action == "get_event": + return client.api("GET", base, {"user_id_type": "open_id"}) + if action == "invite": + ids = args.get("attendee_open_ids", []) + if not ids or any(not x.startswith("ou_") for x in ids): + raise WorkError("需要已确认的参会人open_id。") + return client.api("POST", base + "/attendees", {"user_id_type": "open_id"}, + {"attendees": [{"type": "user", "user_id": x} for x in ids], "need_notification": True}, args["operation_id"]) + raise WorkError("不支持的日历操作。") diff --git a/deployable/requirements.lock b/deployable/requirements.lock new file mode 100644 index 0000000..c763938 --- /dev/null +++ b/deployable/requirements.lock @@ -0,0 +1,41 @@ +annotated-types==0.8.0 +anyio==4.15.1 +certifi==2026.10.23 +cffi==2.1.1 +charset-normalizer==3.5.2 +click==8.5.0 +colorama==0.4.6 +cryptography==46.0.3 +fakeredis==2.32.0 +fastapi==0.119.1 +h11==0.16.0 +httpcore==1.0.9 +httpx==0.28.1 +idna==3.20 +iniconfig==2.3.1 +lark-oapi==1.5.3 +packaging==26.3 +pluggy==1.7.0 +psycopg==3.2.12 +psycopg-binary==3.2.12 +pycparser==3.12 +pycryptodome==4.2.0 +pydantic==2.14.1 +pydantic-settings==2.11.0 +pydantic_core==2.50.1 +Pygments==2.21.0 +pytest==8.4.2 +pytest-asyncio==1.2.0 +python-dotenv==1.2.4 +PyYAML==6.0.3 +redis==6.4.0 +requests==2.34.2 +requests-toolbelt==1.0.0 +sortedcontainers==2.4.0 +starlette==0.48.0 +typing-inspection==0.4.4 +typing_extensions==4.16.0 +tzdata==2026.5 +urllib3==2.8.0 +uvicorn==0.38.0 +websockets==15.0.1 diff --git a/deployable/scripts/initialize.py b/deployable/scripts/initialize.py new file mode 100644 index 0000000..ab186ff --- /dev/null +++ b/deployable/scripts/initialize.py @@ -0,0 +1,140 @@ +"""Initialize a NEW deployment only. Run as root inside the application image.""" +import argparse +import ipaddress +import os +import re +import secrets +import ssl +from datetime import datetime, timedelta, timezone +from pathlib import Path +from urllib.parse import urlsplit + +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ed25519 +from cryptography.x509.oid import NameOID +from dotenv import dotenv_values, set_key +from relay.crypto import issue_ca, pem_private + + +def initialize(root, test_cert=False): + root = Path(root) + if os.name != 'nt' and os.geteuid() != 0: + raise ValueError('Run initialization as root for container UID 10001 ownership') + if (root / '.env').exists() or (root / 'runtime/connector/connector.key').exists(): + raise ValueError('Existing deployment detected; initialization will not overwrite it') + settings = {k: v for k, v in dotenv_values(root / 'settings.env').items() if v is not None} + required = ['HN_PUBLIC_URL', 'HN_FEISHU_API_BASE', 'HN_FEISHU_AUTHORIZE_URL'] + for field in required: + value = settings.get(field, '') + parsed = urlsplit(value) + if parsed.scheme != 'https' or not parsed.hostname or parsed.username or parsed.password or parsed.query or parsed.fragment: + raise ValueError(f'{field} must be a valid HTTPS URL') + if parsed.hostname.endswith('.invalid'): + raise ValueError(f'Configure {field} before initializing') + public = urlsplit(settings['HN_PUBLIC_URL']) + if public.path not in ('', '/'): + raise ValueError('HN_PUBLIC_URL must have no path prefix') + if (public.port or 443) != int(settings.get('HN_HTTPS_PORT', '443')): + raise ValueError('Public URL port must match HN_HTTPS_PORT') + if settings.get('HN_FEISHU_ENABLED', 'true').lower() == 'true': + if not all(settings.get(k) for k in ['HN_FEISHU_APP_ID', 'HN_FEISHU_APP_SECRET']): + raise ValueError('Configure platform app ID and secret') + if not re.fullmatch(r'[a-z0-9][a-z0-9_-]*', settings.get('COMPOSE_PROJECT_NAME', 'hainuo-relay')): + raise ValueError('Invalid Compose project name') + tls = root / 'runtime/tls' + tls.mkdir(parents=True, exist_ok=True) + cert_path, key_path = tls / 'server.pem', tls / 'server.key' + if test_cert: + if cert_path.exists() or key_path.exists(): + raise ValueError('Refusing to replace an existing TLS certificate') + key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, public.hostname)]) + try: + san = x509.IPAddress(ipaddress.ip_address(public.hostname)) + except ValueError: + san = x509.DNSName(public.hostname) + now = datetime.now(timezone.utc) + cert = (x509.CertificateBuilder().subject_name(name).issuer_name(name).public_key(key.public_key()) + .serial_number(x509.random_serial_number()).not_valid_before(now-timedelta(minutes=5)) + .not_valid_after(now+timedelta(days=30)) + .add_extension(x509.SubjectAlternativeName([san]), critical=False).sign(key, None)) + key_path.write_bytes(pem_private(key)) + cert_path.write_bytes(cert.public_bytes(serialization.Encoding.PEM)) + validate_tls(cert_path, key_path, public.hostname) + ca_key, ca = issue_ca() + signer = ed25519.Ed25519PrivateKey.generate() + for folder in ['runtime/trust', 'runtime/connector', 'runtime/issuer']: + (root / folder).mkdir(parents=True, exist_ok=False) + (root / 'runtime/trust/ca.pem').write_bytes(ca.public_bytes(serialization.Encoding.PEM)) + (root / 'runtime/trust/connector.pub').write_bytes(signer.public_key().public_bytes( + serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo)) + (root / 'runtime/connector/connector.key').write_bytes(pem_private(signer)) + (root / 'runtime/issuer/ca.key').write_bytes(pem_private(ca_key)) + redis_pass, db_pass = secrets.token_hex(32), secrets.token_hex(32) + url = settings['HN_PUBLIC_URL'].rstrip('/') + settings.update(HN_ENVIRONMENT='production', HN_PUBLIC_URL=url, + HN_ROUTER_URL='wss://' + urlsplit(url).netloc + '/device/ws', + HN_ADMIN_SECRET=secrets.token_hex(32), HN_INTERNAL_SECRET=secrets.token_hex(32), + HN_REDIS_PASSWORD=redis_pass, HN_POSTGRES_PASSWORD=db_pass, + HN_REDIS_URL=f'redis://:{redis_pass}@redis:6379/0', + HN_DATABASE_URL=f'postgresql://hainuo:{db_pass}@postgres:5432/hainuo', + HN_FEISHU_ENCRYPT_KEY='', HN_FEISHU_VERIFICATION_TOKEN='', + HN_PROXY_ENABLED='false', HN_DEVICE_DOMAIN='') + tmp = root / '.env.initializing' + tmp.touch(mode=0o600, exist_ok=False) + for key, value in settings.items(): + if '\n' in value or '\r' in value: + raise ValueError('Multiline settings are unsupported') + set_key(tmp, key, value, quote_mode='always') + tmp.replace(root / '.env') + for folder in ['runtime/connector', 'runtime/issuer']: + path = root / folder + path.chmod(0o700) + if os.name != 'nt': + os.chown(path, 10001, 10001) + for file in path.iterdir(): + file.chmod(0o600) + if os.name != 'nt': + os.chown(file, 10001, 10001) + (root / '.env').chmod(0o600) + (root / 'settings.env').chmod(0o600) + tls.chmod(0o700) + key_path.chmod(0o600) + cert_path.chmod(0o644) + (root / 'runtime/trust').chmod(0o755) + for path in (root / 'runtime/trust').iterdir(): + path.chmod(0o644) + print('Initialized new deployment. No credentials printed. Preserve runtime/issuer/ca.key securely.') + + +def validate_tls(cert_path, key_path, hostname): + context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) + context.load_cert_chain(str(cert_path), str(key_path)) + cert = x509.load_pem_x509_certificate(cert_path.read_bytes()) + now = datetime.now(timezone.utc) + if not cert.not_valid_before_utc <= now <= cert.not_valid_after_utc: + raise ValueError('Gateway TLS certificate is not currently valid') + san = cert.extensions.get_extension_for_class(x509.SubjectAlternativeName).value + try: + address = ipaddress.ip_address(hostname) + except ValueError: + host = hostname.encode('idna').decode().lower() + def matches(name): + name = name.lower() + return name == host or (name.startswith('*.') and name.count('*') == 1 + and len(host.split('.')) == len(name.split('.')) + and host.split('.', 1)[1] == name[2:]) + valid = any(matches(x) for x in san.get_values_for_type(x509.DNSName)) + else: + valid = address in san.get_values_for_type(x509.IPAddress) + if not valid: + raise ValueError('Gateway TLS certificate hostname mismatch') + + +if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('--root', default='/work') + parser.add_argument('--test-cert', action='store_true') + args = parser.parse_args() + initialize(args.root, args.test_cert) diff --git a/deployable/scripts/load-offline.sh b/deployable/scripts/load-offline.sh new file mode 100755 index 0000000..5de71d2 --- /dev/null +++ b/deployable/scripts/load-offline.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +set -euo pipefail +cd "$(dirname "$0")/.." +case "$(uname -m)" in aarch64|arm64) ;; *) echo 'This dependency bundle requires Linux ARM64'; exit 1 ;; esac +test -d images && test -d wheelhouse +test -f OFFLINE_SHA256SUMS +sha256sum -c OFFLINE_SHA256SUMS +for archive in images/*.tar; do docker load -i "$archive"; done +echo 'Offline images loaded. Next: build, configure settings.env, init, start.' diff --git a/deployable/scripts/package_offline.py b/deployable/scripts/package_offline.py new file mode 100644 index 0000000..2bec0f1 --- /dev/null +++ b/deployable/scripts/package_offline.py @@ -0,0 +1,99 @@ +"""Package current source with existing, checksum-verified Linux ARM64 dependencies. + +Usage: python scripts/package_offline.py --dependencies /path/to/old-bundle --output /path/to/new.tar.gz +The dependency directory must contain SHA256SUMS, images/, hainuo-relay/wheelhouse/. +Only dependencies are reused; source in the old bundle is never packaged. +""" +import argparse +import hashlib +import io +import json +import tarfile +import zipfile +from email.parser import Parser +from pathlib import Path + + +def digest(path): + with path.open('rb') as handle: + return hashlib.file_digest(handle, 'sha256').hexdigest() + + +def package(dependencies, output): + root = Path(__file__).resolve().parents[1] + dependencies, output = Path(dependencies), Path(output) + if output.exists(): + raise ValueError('Output already exists; refusing to overwrite') + checks = {} + for line in (dependencies/'SHA256SUMS').read_text(encoding='utf-8').splitlines(): + sha, rel = line.split(None, 1) + checks[rel.strip().lstrip('*')] = sha + assets = sorted(p for folder in [dependencies/'images', dependencies/'hainuo-relay/wheelhouse', dependencies/'licenses'] + for p in folder.rglob('*') if p.is_file()) + if not assets or not (dependencies/'licenses').is_dir(): + raise ValueError('Dependency bundle must include images, wheelhouse and license notices') + wheel_versions = {} + mapped = {} + for path in assets: + rel = path.relative_to(dependencies).as_posix() + if rel not in checks or digest(path) != checks[rel]: + raise ValueError('Dependency checksum mismatch: ' + rel) + if path.suffix == '.whl': + if not (path.name.endswith('none-any.whl') or 'aarch64' in path.name): + raise ValueError('Unsupported wheel architecture: ' + path.name) + with zipfile.ZipFile(path) as wheel: + name = next(x for x in wheel.namelist() if x.endswith('.dist-info/METADATA')) + meta = Parser().parsestr(wheel.read(name).decode('utf-8')) + wheel_versions[meta['Name'].lower().replace('_','-')] = meta['Version'] + target = 'wheelhouse/' + path.name + else: + target = rel + mapped[target] = path + required = {} + for line in (root/'requirements.lock').read_text(encoding='utf-8').splitlines(): + if line.strip() and not line.startswith('#'): + name, version = line.split('==') + required[name.lower().replace('_','-')] = version + assert all(wheel_versions.get(n) == v for n,v in required.items()), 'Pinned wheel dependency mismatch' + image_tars = list((dependencies/'images').glob('*.tar')) + assert len(image_tars) == 4, 'Expected four base images' + for path in image_tars: + with tarfile.open(path) as image: + manifest = json.load(image.extractfile('manifest.json')) + for entry in manifest: + config = json.load(image.extractfile(entry['Config'])) + assert config['architecture'] == 'arm64' and config['os'] == 'linux' + for path in root.rglob('*'): + rel = path.relative_to(root) + if not path.is_file() or any(x in {'runtime','backups','wheelhouse','images','__pycache__','.pytest_cache','.git'} for x in rel.parts): + continue + if path.name in {'.env','settings.env'} or path.suffix == '.pyc': + continue + mapped[rel.as_posix()] = path + manifest=''.join(f'{digest(path)} {name}\n' for name,path in sorted(mapped.items())) + output.parent.mkdir(parents=True, exist_ok=True) + with tarfile.open(output, 'w:gz', compresslevel=6) as tf: + for name,path in sorted(mapped.items()): + info=tf.gettarinfo(str(path), arcname='hainuo-relay-deployable/'+name) + info.mode=0o755 if path.suffix=='.sh' else 0o644 + info.uid=info.gid=0 + info.uname=info.gname='' + with path.open('rb') as handle: + tf.addfile(info,handle) + data=manifest.encode('utf-8') + info=tarfile.TarInfo('hainuo-relay-deployable/OFFLINE_SHA256SUMS') + info.size=len(data) + info.mode=0o644 + tf.addfile(info,io.BytesIO(data)) + checksum=digest(output) + Path(str(output)+'.sha256').write_text(checksum+' '+output.name+'\n',encoding='utf-8') + print(json.dumps({'output':str(output),'bytes':output.stat().st_size,'sha256':checksum, + 'pinned_requirements':len(required),'verified_wheels':len(wheel_versions),'verified_images':len(image_tars)},indent=2)) + + +if __name__=='__main__': + parser=argparse.ArgumentParser() + parser.add_argument('--dependencies',required=True) + parser.add_argument('--output',required=True) + args=parser.parse_args() + package(args.dependencies,args.output) diff --git a/deployable/scripts/prepare-offline.sh b/deployable/scripts/prepare-offline.sh new file mode 100755 index 0000000..614a00f --- /dev/null +++ b/deployable/scripts/prepare-offline.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# Execute on an INTERNET-CONNECTED LINUX ARM64 machine with Docker and Python 3.12. +set -euo pipefail +cd "$(dirname "$0")/.." +case "$(uname -m)" in aarch64|arm64) ;; *) echo 'Run on Linux ARM64'; exit 1 ;; esac +python3 -c 'import sys; assert sys.version_info[:2] == (3,12)' +mkdir -p images wheelhouse +python3 -m pip download --only-binary=:all: --dest wheelhouse -r requirements.lock +for spec in python:3.12-slim-bookworm postgres:17-alpine redis:8.2-alpine caddy:2-alpine; do + docker pull --platform linux/arm64 "$spec" + file=${spec//:/-} + docker save -o "images/$file-linux-arm64.tar" "$spec" +done +find images wheelhouse -type f -print0 | sort -z | xargs -0 sha256sum > OFFLINE_SHA256SUMS +echo 'Exact downloaded contents are recorded in OFFLINE_SHA256SUMS. Transfer the whole deployable directory.' diff --git a/deployable/scripts/relayctl.sh b/deployable/scripts/relayctl.sh new file mode 100755 index 0000000..31c2a8d --- /dev/null +++ b/deployable/scripts/relayctl.sh @@ -0,0 +1,78 @@ +#!/usr/bin/env bash +set -euo pipefail +umask 077 +cd "$(dirname "$0")/.." +action=${1:-help} +mode=${2:-offline} +args=(-f compose.yaml) +case "$mode" in + offline) args+=(-f compose.offline.yaml) ;; + online) ;; + *) echo 'Mode must be online or offline'; exit 1 ;; +esac +compose() { docker compose "${args[@]}" "$@"; } +image=${HN_BUILD_IMAGE:-hainuo-relay:deploy-20261027} +build() { + if [[ -f .env ]]; then + compose build connector + return + fi + if [[ "$mode" == offline ]]; then + test -d wheelhouse || { echo 'Missing wheelhouse; obtain the ARM64 offline bundle'; exit 1; } + docker build --network none --pull=false -f Dockerfile.offline -t "$image" . + else + docker build -f Dockerfile -t "$image" . + fi +} +case "$action" in + build) build ;; + init) + test -f settings.env || { echo 'Copy .env.example to settings.env and configure it'; exit 1; } + docker run --rm --user 0:0 --entrypoint python -e PYTHONPATH=/app -v "$PWD:/work" "$image" /work/scripts/initialize.py "${@:3}" + ;; + check) + test -f .env && test -f runtime/issuer/ca.key && test -f runtime/connector/connector.key + test -f runtime/trust/ca.pem && test -f runtime/tls/server.pem && test -f runtime/tls/server.key + compose config --quiet + docker run --rm --user 0:0 --entrypoint python -e PYTHONPATH=/app -v "$PWD:/work:ro" "$image" /work/scripts/validate.py + ;; + start) + bash scripts/relayctl.sh check "$mode" + compose up -d --no-build --pull never --wait --wait-timeout 180 + compose ps + echo 'Containers ready. Now verify platform_connected and perform the documented business acceptance.' + ;; + status) compose ps; compose exec -T connector python -m relay.cli status --url http://127.0.0.1:8000 ;; + logs) compose logs --since 10m --tail 80 "${@:3}" ;; + stop) compose stop ;; + backup) + stamp=$(date +%Y%m%d-%H%M%S) + out="backups/$stamp" + mkdir -p "$out" + compose exec -T postgres pg_dump -U hainuo -d hainuo -Fc > "$out/metadata.dump.partial" + mv "$out/metadata.dump.partial" "$out/metadata.dump" + tar -czf "$out/config-and-keys.tar.gz" .env runtime + cp compose*.yaml "$out/" + compose images -q > "$out/image-ids.txt" + (cd "$out" && sha256sum metadata.dump config-and-keys.tar.gz > SHA256SUMS) + echo "Backup: $PWD/$out (contains private keys and secrets; restrict access). Not a restore verification." + ;; + update) + bash scripts/relayctl.sh backup "$mode" + stamp=$(date +%Y%m%d-%H%M%S) + current=$(compose images -q connector) + test -n "$current" + rollback="hainuo-relay:rollback-$stamp" + docker image tag "$current" "$rollback" + echo "Rollback image: $rollback (record this before proceeding)" + build + bash scripts/relayctl.sh start "$mode" + ;; + rollback) + previous=${3:?Provide the recorded rollback image tag} + docker image inspect "$previous" >/dev/null + HN_IMAGE="$previous" compose up -d --no-build --pull never --wait --wait-timeout 180 + echo 'Temporary rollback active. Set HN_IMAGE in .env to this tag before subsequent commands.' + ;; + *) echo 'Usage: bash scripts/relayctl.sh {build|init|check|start|status|logs|stop|backup|update|rollback} {offline|online} [arguments]'; exit 1 ;; +esac diff --git a/deployable/scripts/validate.py b/deployable/scripts/validate.py new file mode 100644 index 0000000..090bf7d --- /dev/null +++ b/deployable/scripts/validate.py @@ -0,0 +1,26 @@ +"""Read-only configuration/key consistency checks; never output secret values.""" +import os +import argparse +from pathlib import Path +from urllib.parse import urlsplit +from dotenv import dotenv_values +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from relay.config import Settings +from relay.crypto import raw_public, load_private +from initialize import validate_tls + +parser=argparse.ArgumentParser() +parser.add_argument('--root',default='/work') +root = Path(parser.parse_args().root) +values = {k:v for k,v in dotenv_values(root/'.env').items() if v is not None} +os.environ.update(values) +Settings() +public=urlsplit(values['HN_PUBLIC_URL']) +validate_tls(root/'runtime/tls/server.pem',root/'runtime/tls/server.key',public.hostname) +ca=x509.load_pem_x509_certificate((root/'runtime/trust/ca.pem').read_bytes()) +assert raw_public(load_private(root/'runtime/issuer/ca.key').public_key()) == raw_public(ca.public_key()), 'Issuer CA mismatch' +pub=serialization.load_pem_public_key((root/'runtime/trust/connector.pub').read_bytes()) +assert raw_public(load_private(root/'runtime/connector/connector.key').public_key()) == raw_public(pub), 'Connector key mismatch' +assert values['HN_ROUTER_URL']=='wss://'+public.netloc+'/device/ws', 'Router URL mismatch' +print('Configuration, TLS hostname/expiry, CA and connector key pairs passed. Platform reachability not tested.') diff --git a/deployable/tests/test_deployment.py b/deployable/tests/test_deployment.py new file mode 100644 index 0000000..d3ddf82 --- /dev/null +++ b/deployable/tests/test_deployment.py @@ -0,0 +1,77 @@ +import importlib.util +import sys +from pathlib import Path + +import pytest +import yaml +from cryptography import x509 +from dotenv import dotenv_values + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT)) +spec = importlib.util.spec_from_file_location('deployment_initialize', ROOT/'scripts/initialize.py') +module = importlib.util.module_from_spec(spec) +spec.loader.exec_module(module) + + +def configured(tmp_path): + (tmp_path/'settings.env').write_text( + 'HN_PUBLIC_URL=https://192.0.2.10\nHN_FEISHU_API_BASE=https://work.example.test\n' + 'HN_FEISHU_AUTHORIZE_URL=https://work.example.test/open-apis/authen/v1/authorize\n' + 'HN_FEISHU_APP_ID=cli_test\nHN_FEISHU_APP_SECRET=fake-secret\n', encoding='utf-8') + return tmp_path + + +def test_initialize_ip_certificate_and_separate_keys(tmp_path): + module.initialize(configured(tmp_path), test_cert=True) + values = dotenv_values(tmp_path/'.env') + assert values['HN_ADMIN_SECRET'] != values['HN_INTERNAL_SECRET'] + assert min(len(values[k]) for k in ['HN_ADMIN_SECRET','HN_INTERNAL_SECRET','HN_REDIS_PASSWORD']) >= 32 + cert=x509.load_pem_x509_certificate((tmp_path/'runtime/tls/server.pem').read_bytes()) + san=cert.extensions.get_extension_for_class(x509.SubjectAlternativeName).value + assert str(san.get_values_for_type(x509.IPAddress)[0]) == '192.0.2.10' + module.validate_tls(tmp_path/'runtime/tls/server.pem',tmp_path/'runtime/tls/server.key','192.0.2.10') + assert (tmp_path/'runtime/issuer/ca.key').read_bytes() != (tmp_path/'runtime/connector/connector.key').read_bytes() + + +def test_refuses_existing_environment_without_changing_keys(tmp_path): + module.initialize(configured(tmp_path), test_cert=True) + previous=(tmp_path/'runtime/issuer/ca.key').read_bytes() + with pytest.raises(ValueError, match='Existing deployment'): + module.initialize(tmp_path, test_cert=True) + assert (tmp_path/'runtime/issuer/ca.key').read_bytes() == previous + + +def test_missing_tls_does_not_generate_device_identity(tmp_path): + configured(tmp_path) + with pytest.raises((FileNotFoundError, OSError)): + module.initialize(tmp_path) + assert not (tmp_path/'.env').exists() + assert not (tmp_path/'runtime/issuer/ca.key').exists() + + +def test_tls_hostname_mismatch_rejected(tmp_path): + module.initialize(configured(tmp_path), test_cert=True) + with pytest.raises(Exception, match='match'): + module.validate_tls(tmp_path/'runtime/tls/server.pem',tmp_path/'runtime/tls/server.key','other.example.test') + + +def test_placeholder_rejected_before_identity_generation(tmp_path): + text=(ROOT/'.env.example').read_text(encoding='utf-8') + (tmp_path/'settings.env').write_text(text,encoding='utf-8') + with pytest.raises(ValueError, match='Configure HN_PUBLIC_URL'): + module.initialize(tmp_path, test_cert=True) + assert not (tmp_path/'runtime').exists() + + +def test_compose_has_complete_build_and_internal_issuer(): + text=(ROOT/'compose.yaml').read_text(encoding='utf-8') + assert '' not in text + services=yaml.safe_load(text)['services'] + assert len(services)==7 + assert 'profiles' not in services['longconn'] + assert 'ports' not in services['enrollment-signer'] + for service in ['connector','router','longconn']: + assert all('/issuer' not in v for v in services[service].get('volumes', [])) + for file in ['Dockerfile','Dockerfile.offline','requirements.lock','pyproject.toml','relay/cli.py']: + assert (ROOT/file).is_file()