Import ECS deployed relay source and historical patches (2026-10-27)
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
services:
|
||||
acceptance:
|
||||
image: hainuo-relay:0.1.0
|
||||
entrypoint:
|
||||
- python
|
||||
- -m
|
||||
- pytest
|
||||
command:
|
||||
- -q
|
||||
- /tests
|
||||
environment:
|
||||
HN_TEST_DATABASE_URL: ${HN_DATABASE_URL}
|
||||
volumes:
|
||||
- ./tests:/tests:ro
|
||||
networks:
|
||||
- backend
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
tmpfs:
|
||||
- /tmp:size=64m
|
||||
working_dir: /tmp
|
||||
@@ -0,0 +1,39 @@
|
||||
services:
|
||||
connector:
|
||||
environment:
|
||||
HN_ENROLLMENT_SIGNER_URL: <REDACTED_LITERAL>
|
||||
depends_on:
|
||||
enrollment-signer:
|
||||
condition: service_healthy
|
||||
enrollment-signer:
|
||||
image: hainuo-relay:0.1.0
|
||||
entrypoint:
|
||||
- python
|
||||
- -m
|
||||
- relay.enrollment_signer
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
HN_INTERNAL_SECRET: ${HN_INTERNAL_SECRET}
|
||||
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
|
||||
@@ -0,0 +1,4 @@
|
||||
services:
|
||||
edge:
|
||||
cap_add:
|
||||
- NET_BIND_SERVICE
|
||||
@@ -0,0 +1,4 @@
|
||||
services:
|
||||
connector:
|
||||
environment:
|
||||
HN_REQUEST_TIMEOUT: <REDACTED_LITERAL>
|
||||
@@ -0,0 +1,13 @@
|
||||
services:
|
||||
postgres:
|
||||
pull_policy: never
|
||||
redis:
|
||||
pull_policy: never
|
||||
connector:
|
||||
pull_policy: never
|
||||
router:
|
||||
pull_policy: never
|
||||
edge:
|
||||
pull_policy: never
|
||||
longconn:
|
||||
pull_policy: never
|
||||
@@ -0,0 +1,6 @@
|
||||
services:
|
||||
connector:
|
||||
environment:
|
||||
HN_OAUTH_ENABLED: <REDACTED_LITERAL>
|
||||
HN_FEISHU_AUTHORIZE_URL: ${HN_FEISHU_AUTHORIZE_URL}
|
||||
HN_FEISHU_OAUTH_SCOPES: ${HN_FEISHU_OAUTH_SCOPES}
|
||||
@@ -0,0 +1,9 @@
|
||||
services:
|
||||
connector:
|
||||
environment:
|
||||
HN_FEISHU_EVENT_SOURCE: <REDACTED_LITERAL>
|
||||
HN_OAUTH_ENABLED: <REDACTED_LITERAL>
|
||||
HN_REQUEST_TIMEOUT: <REDACTED_LITERAL>
|
||||
longconn:
|
||||
environment:
|
||||
HN_FEISHU_EVENT_SOURCE: <REDACTED_LITERAL>
|
||||
@@ -0,0 +1,216 @@
|
||||
name: hainuo-relay
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:17-alpine
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
POSTGRES_DB: <REDACTED_LITERAL>
|
||||
POSTGRES_USER: <REDACTED_LITERAL>
|
||||
POSTGRES_PASSWORD: ${HN_POSTGRES_PASSWORD}
|
||||
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: hainuo-relay:0.1.0
|
||||
build: .
|
||||
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: <REDACTED_LITERAL>
|
||||
HN_DATABASE_URL: ${HN_DATABASE_URL}
|
||||
HN_CA_CERT: <REDACTED_LITERAL>
|
||||
HN_CONNECTOR_KEY: <REDACTED_LITERAL>
|
||||
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}
|
||||
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
|
||||
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: hainuo-relay:0.1.0
|
||||
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: <REDACTED_LITERAL>
|
||||
HN_PUBLIC_URL: ${HN_PUBLIC_URL}
|
||||
HN_ROUTER_URL: ${HN_ROUTER_URL}
|
||||
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:
|
||||
- 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
|
||||
longconn:
|
||||
image: hainuo-relay:0.1.0
|
||||
profiles:
|
||||
- longconn
|
||||
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: <REDACTED_LITERAL>
|
||||
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
|
||||
networks:
|
||||
backend:
|
||||
internal: true
|
||||
edge: {}
|
||||
volumes:
|
||||
metadata: {}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
[Unit]
|
||||
Description=Hainuo relay %i
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=hainuo
|
||||
Group=hainuo
|
||||
WorkingDirectory=/opt/hainuo-relay
|
||||
EnvironmentFile=/etc/hainuo-relay/%i.env
|
||||
ExecStart=/opt/hainuo-relay/.venv/bin/python -m relay.cli serve %i --host 127.0.0.1 --port 8000
|
||||
Restart=on-failure
|
||||
RestartSec=5
|
||||
UMask=0077
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectSystem=strict
|
||||
ProtectHome=true
|
||||
ReadWritePaths=/var/lib/hainuo-relay
|
||||
LimitCORE=0
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,56 @@
|
||||
# 田泽远测试清单
|
||||
|
||||
每次保留:日期、操作者、组件版本、ECS架构/页大小、设备host_id、测试turn_id、预期、实际、通过/失败/未执行。日志不保留code/token/应用密钥或业务文档正文。
|
||||
|
||||
## 第一组:服务启动
|
||||
|
||||
1. `docker compose ps`显示基础组件与B1/B2正常;`verify_deployment.sh`通过。
|
||||
2. 只从业务网络访问443;Redis、PostgreSQL及18081不应从公网可达。
|
||||
3. 公网 `/admin/devices`与`/internal/events`返回404;内部管理请求缺少凭据返回401。
|
||||
4. TLS链可信且域名匹配;不能用关闭校验代替通过。
|
||||
|
||||
## 第二组:两人两机
|
||||
|
||||
| 用例 | 动作 | 通过标准 |
|
||||
|---|---|---|
|
||||
| A-01 | 各自生成设备私钥、登记、绑定不同账号 | host_id不同、owner不同;私钥不上传 |
|
||||
| A-02 | A发送 `/主机名`,B发送同样命令 | 各自得到本机名,设备journal只出现自己的任务ID |
|
||||
| A-03 | 分别 `/回显 A唯一标记`、`/回显 B唯一标记` | 回复不串人 |
|
||||
| A-04 | A读取自己的测试目录UTF-8文本 | 内容正确;路径和大小限制生效 |
|
||||
| A-05 | A尝试 `../`或绝对路径越出测试目录 | 明确拒绝;不能读到目录外内容 |
|
||||
| A-06 | 相同turn_id发送两次 | 第二次duplicate,不执行第二次 |
|
||||
| A-07 | 停止A桥接端,保留B在线 | A提示离线,B仍可用;不误投 |
|
||||
| A-08 | 恢复A网络 | 自动重连后新请求成功 |
|
||||
| A-09 | 重启B1后重发已完成turn_id | 持久去重仍生效 |
|
||||
| A-10 | 管理员吊销A | A断连、不能重连、后续请求拒绝;B不受影响 |
|
||||
|
||||
## 第三组:平台事件
|
||||
|
||||
配置真实平台后执行:私聊绑定→主机名→回显→解绑。确认机器人回复到原消息。
|
||||
|
||||
- 群聊消息不派发到PC。
|
||||
- 错误应用ID、Token或签名不执行;过期签名拒绝。
|
||||
- 同一平台message_id重投只执行一次。
|
||||
- websocket/平台重连后不使用公共云错误域名。
|
||||
- 记录平台实际限流码与阈值,不能套用微信机器人的50条/分钟限制。
|
||||
- 断平台网络后检查失败状态和恢复;“HTTP200已接收”不代表PC已经执行。
|
||||
|
||||
## 第四组:OAuth与代理
|
||||
|
||||
- start返回随机state与独立claim_secret;URL中无claim_secret。
|
||||
- unknown state回调拒绝;相同state的并发回调只换一次。
|
||||
- **先错claim_secret再对claim_secret**:错的404,正确的仍能领取;再次领取404。
|
||||
- 600秒过期、限流429、内存容量503。
|
||||
- 令牌有效期采用响应原值;日志和数据库不含token/code。
|
||||
- 并发刷新旧refresh_token只有一个上游尝试;失败后重新授权,不能循环重试旧token。
|
||||
- 如开启数据代理:本人能读的资源可读;另一个用户对明确未共享资源被拒。拒绝访问token接口、任意外部URL及路径穿越。
|
||||
|
||||
## 第五组:H5与后续集成
|
||||
|
||||
当前可验:入口页适配、OAuth授权、本人设备查询、离线提示、静态目标域名、无令牌URL。
|
||||
|
||||
**本版不能直接验收通过**:PC网页TLS透传、设备SSO、真实1.065Agent接口、附件端到端、安卓/iOS/鸿蒙海能Work内完整对话。这些项目需要客户端与网络环境集成,不能将入口页面可打开记录成通过。
|
||||
|
||||
## 发布门槛
|
||||
|
||||
本版可用于受控开发联调;不能直接宣告领导P0九关全关。还需实际完成Windows私钥保护、读取权限模型、终端准入、证书生命周期、应急停服及目标环境测试。领导允许调整方案,不等于这些验收证据已经存在。
|
||||
@@ -0,0 +1,34 @@
|
||||
# 相对领导方案的调整
|
||||
|
||||
本次保留目标:一个海能Work机器人按人路由到本人的PC,中心不承担AI推理,B2不解密正文,PC离线不排队,逐步增加网页/H5入口。
|
||||
|
||||
| 原方案/材料 | 本次处理 | 原因与影响 |
|
||||
|---|---|---|
|
||||
| 先部署六台虚机 | 当前单机独立容器,主备后置 | 按田泽远最新要求先跑通链路;不再启动同机重复主备冒充高可用 |
|
||||
| P0使用六台、汇总12C/24G/240G | 不将错误汇总固化到代码 | 原逐台表实为14C/24G/260G;联调暂以4C8G参考起步,容量另测 |
|
||||
| B1/B2职责分离 | 保留两个独立进程与网络接口 | 拆机不需要重写协议;数据库与总线均可改为远程地址 |
|
||||
| 注册表保存公钥/后改为指纹 | B1持久保存证书与指纹,握手时独立核验证书和签名 | 不信任路由器任意替换的在线目录;B2被改目录时应拒绝而非静默错投 |
|
||||
| 注册token由本机UI签发并换密钥 | PC本机生成私钥、公钥申请由离线CA签发、管理员登记证书 | 明确可信签发源;持有本地UI不能自行让中心信任任意设备。当前为人工签发,未满足50台自动签发门槛 |
|
||||
| TLS到PC、A不持TLS私钥 | 当前A为HTTPS/WSS反代,持入口TLS私钥;另做B1↔PC应用层加密 | 便于单机部署标准代理。A可见握手证书与密文流量,但不能解密指令正文;不声称已实现浏览器到PC的TLS透传 |
|
||||
| turn_id在PC收到消息后生成 | 平台message_id派生稳定turn_id,B1与PC执行前均持久去重 | 防止平台重复投递时每次生成新ID导致重复执行 |
|
||||
| 所有流程不落盘 | 仅正文/token不落盘,身份绑定、吊销、去重与审计元数据持久化 | 重启不能清除绑定、吊销或已执行请求记录 |
|
||||
| token永不落盘,却要求临时文件rename | B1待领token仅在内存;刷新结果直接返回,客户端自行安全保存 | 消除中继落盘矛盾;本版桥接端不保存平台user/refresh token |
|
||||
| user token按7200秒处理 | 使用平台实际返回有效期 | 兼容材料提到的6900/7200差异 |
|
||||
| 错误claim示例有waiting/404两种 | 对错误密钥统一404,不消费;正确密钥待完成返回202 | 避免状态探测与破坏别人的待领结果 |
|
||||
| H5识别用户后302即完成入口 | H5查本人绑定且提示设备仍需独立登录,不携带user token到设备URL | 找到域名不等于获得设备访问权;缺少PC登录协议时不做假免登 |
|
||||
| tenant权限全空 | 机器人消息权限与用户数据权限分开核对 | 机器人需要平台允许的应用消息权限;用户文件代理仍使用本人user token |
|
||||
| 任意自然语言交给SYSTEM Agent | 默认普通用户、三项只读诊断;可显式接入固定回环Agent适配器 | 没有1.065真实本机接口,不能猜测接口后开放任意执行;Agent侧权限仍须实际实现 |
|
||||
| 5分钟批量吊销并停服 | 实现吊销、断连、阻止重连和拒绝派发 | 不冒充已停止Windows海诺服务;操作系统停服需客户端受信管理接口 |
|
||||
| 50人/1000人/10万人已给规格 | 本版不宣称容量与高可用达标 | 未做目标环境压测、故障域演练与生产验收 |
|
||||
| Redis7.4镜像 | 改为官方Redis8.2,选择AGPLv3开源许可 | 满足用户要求的官方开源;保留协议接口,目标环境仍需运行验收 |
|
||||
| 需要联网拉基础镜像与pip依赖 | 增加官方ARM64镜像、离线wheelhouse、禁联网构建及禁止拉取的Compose配置 | 可在预装Docker/Compose的新服务器上离线安装,不再依赖镜像仓库连通性 |
|
||||
|
||||
## 本版仍需后续交付的功能
|
||||
|
||||
- 1.065/后续海诺客户端的真实协议适配或本机只读Agent接口实现。
|
||||
- 浏览器到PC的TLS透传隧道、设备网页身份认证、文件上传下载。
|
||||
- 私有化海能Work JSAPI免登与移动WebView实际验证。
|
||||
- Windows DPAPI/TPM不可导出私钥、设备证书自动续期、根/连接器密钥轮换。
|
||||
- B1主备、入口VIP、数据库灾备、监控告警与实际容量验证。
|
||||
|
||||
这些项目没有通过修改文档被标记成“已完成”。领导文档的冻结与审批记录作为历史背景保存;本次实现差异在这里独立登记,不篡改原始文件。
|
||||
@@ -0,0 +1,211 @@
|
||||
# Linux ARM64单机部署操作手册
|
||||
|
||||
## 1. 环境要求和目录
|
||||
|
||||
- Linux `aarch64/arm64`,推荐4核8GB、40GB可用盘起步做两人两机联调;不是50人容量保证。
|
||||
- Docker Engine及Compose v2;能拉取镜像、访问Python依赖源。使用原生ARM64镜像,不强制amd64模拟。
|
||||
- 默认容器基础是Debian bookworm/glibc。已下载核验Python3.12 ARM64 wheels;麒麟等宿主机仍须实际运行验证,尤其64KiB页内核。
|
||||
- 安全组只开放业务需要的443;SSH限制管理来源。18081仅绑定服务器127.0.0.1,不开放到公网。
|
||||
- PC能够出站访问ECS的443;平台事件源或ECS到平台的网络按所选接入方式打通。
|
||||
|
||||
将源码包解压到 `/opt/hainuo-relay`。以下命令均在该目录执行。
|
||||
|
||||
```bash
|
||||
uname -m
|
||||
getconf PAGESIZE
|
||||
docker version
|
||||
docker compose version
|
||||
```
|
||||
|
||||
## 2. 初始化
|
||||
|
||||
```bash
|
||||
bash scripts/bootstrap.sh relay.company.example
|
||||
```
|
||||
|
||||
替换域名为你们实际提供的入口。脚本构建镜像并生成:
|
||||
|
||||
- `.env`:随机管理员凭据、内部调用凭据、Redis和PostgreSQL密码;不会回显。
|
||||
- `runtime/connector/connector.key`:B1签名私钥,只挂载给B1。
|
||||
- `runtime/trust/ca.pem`、`connector.pub`:设备CA与连接器信任公钥。
|
||||
- `runtime/tls/server.pem`、`server.key`:30天测试自签TLS证书。
|
||||
- `operator/ca.key`:设备CA私钥,仅供签发工作站使用,不挂载容器。初始化后移至离线签发工作站,并保留该工作站需要的ca.pem和connector.pub;不要放在公网业务目录或普通备份中。
|
||||
|
||||
初始化拒绝覆盖已有文件和密钥;失败后不要直接再次初始化覆盖半成品,先检查已生成内容。
|
||||
|
||||
配置前建议 `chmod 600 .env`。不要把 `.env` 或CA私钥发群聊。
|
||||
|
||||
## 3. TLS和域名
|
||||
|
||||
将入口DNS指向ECS实际可达地址,使用公司认可的服务器证书替换:
|
||||
|
||||
```text
|
||||
runtime/tls/server.pem # 服务器证书及中间链
|
||||
runtime/tls/server.key # 对应私钥
|
||||
```
|
||||
|
||||
测试自签证书时,必须在PC上显式信任该测试证书或通过CLI的 `--tls-ca server.pem` 指定;不要禁用证书校验。
|
||||
|
||||
设备CA是设备身份用途,与浏览器HTTPS信任不是同一件事。不要以“PC设备证书已签发”代替“手机信任网页证书”。
|
||||
|
||||
## 4. 启动与检查
|
||||
|
||||
```bash
|
||||
bash scripts/start.sh
|
||||
bash scripts/verify_deployment.sh
|
||||
docker compose ps
|
||||
```
|
||||
|
||||
默认启动五个容器:edge、connector、router、postgres、redis。官方SDK长连接作为可选第六个容器。
|
||||
|
||||
```bash
|
||||
# 不显示密钥,直接在B1容器内查看设备元数据。
|
||||
docker compose exec connector python -m relay.cli status --url http://127.0.0.1:8000
|
||||
```
|
||||
|
||||
`/health`证明进程活着;`/ready`还检查依赖和回复订阅线程。二者都不代表海能Work联调通过。
|
||||
|
||||
本机环境没有Docker,交付前只完成Compose结构检查与shell语法检查;目标ECS必须实际执行上面步骤。PostgreSQL适配器的真实验证命令:
|
||||
|
||||
```bash
|
||||
docker compose -f compose.yaml -f compose.acceptance.yaml run --rm acceptance
|
||||
```
|
||||
|
||||
该用例只在数据库中新建随机 `hn_test_*` schema,结束时删除该测试schema,不操作已有业务表。
|
||||
|
||||
## 5. 第一次不接平台,先接两台PC
|
||||
|
||||
PC安装Python3.12及本项目依赖,执行后续命令。每台PC必须生成自己的设备目录,禁止复制别人设备的私钥。
|
||||
|
||||
### 5.1 PC生成公钥申请
|
||||
|
||||
```powershell
|
||||
python -m relay.cli device-request --directory D:\HainuoRelayDevice
|
||||
```
|
||||
|
||||
得到 `device.key` 和 `request.json`。只将request.json交给签发工作站。
|
||||
|
||||
### 5.2 签发工作站签证书
|
||||
|
||||
签发工作站的项目目录需要保存 `operator/ca.key`、`runtime/trust/ca.pem`、`runtime/trust/connector.pub`。
|
||||
|
||||
```bash
|
||||
python -m relay.cli issue-device --root . --request request.json --output issued-device-a
|
||||
```
|
||||
|
||||
将输出的 `device.pem`、`ca.pem`、`connector.pub`、`device.json`交回原PC设备目录。设备私钥不经过服务器。
|
||||
|
||||
### 5.3 ECS登记证书
|
||||
|
||||
将 `registration.json`临时放到ECS,例如 `runtime/connector/registration-a.json`,然后:
|
||||
|
||||
```bash
|
||||
docker compose exec connector python -m relay.cli register \
|
||||
--registration /data/registration-a.json --url http://127.0.0.1:8000
|
||||
```
|
||||
|
||||
该文件只有证书和设备ID,没有私钥。登记成功后可移出服务数据目录保管。
|
||||
|
||||
### 5.4 生成模拟账号绑定码
|
||||
|
||||
```bash
|
||||
docker compose exec connector python -m relay.cli binding-code \
|
||||
--owner ou_test_a --url http://127.0.0.1:8000
|
||||
```
|
||||
|
||||
绑定码在受控管理终端显示,五分钟有效,一次使用。这是development/test专用;production环境不开放。
|
||||
|
||||
PC执行并在隐藏输入框输入绑定码:
|
||||
|
||||
```powershell
|
||||
python -m relay.cli bind --directory D:\HainuoRelayDevice --url https://relay.company.example
|
||||
# 若仍使用测试自签证书,上面命令额外加 --tls-ca D:\certs\server.pem。
|
||||
```
|
||||
|
||||
### 5.5 启动桥接端
|
||||
|
||||
```powershell
|
||||
python -m relay.cli device --directory D:\HainuoRelayDevice --url wss://relay.company.example/device/ws --test-root D:\HainuoRelayTestFiles
|
||||
```
|
||||
|
||||
测试目录由你自己创建,先放一个无敏感内容的UTF-8文本文件。桥接端以当前普通用户运行;不要使用SYSTEM或管理员账号扩展文件权限。
|
||||
|
||||
另一台PC重复上述步骤,绑定 `ou_test_b`。
|
||||
|
||||
### 5.6 从ECS发起测试
|
||||
|
||||
```bash
|
||||
docker compose exec connector python -m relay.cli dispatch \
|
||||
--owner ou_test_a --text '/回显 A机链路通过' --turn-id test-a-001 --url http://127.0.0.1:8000
|
||||
docker compose exec connector python -m relay.cli dispatch \
|
||||
--owner ou_test_b --text '/主机名' --turn-id test-b-001 --url http://127.0.0.1:8000
|
||||
```
|
||||
|
||||
重复同一turn-id应返回duplicate;断开PC应返回offline;超时返回unknown,表示结果不确定,不自动重新执行。
|
||||
|
||||
## 6. 接海能Work
|
||||
|
||||
编辑 `.env`:
|
||||
|
||||
```dotenv
|
||||
HN_FEISHU_ENABLED=true
|
||||
HN_FEISHU_API_BASE=https://实际私有化开放平台域名
|
||||
HN_FEISHU_AUTHORIZE_URL=https://实际授权页完整路径
|
||||
HN_FEISHU_APP_ID=实际应用ID
|
||||
HN_FEISHU_APP_SECRET=单独渠道取得的密钥
|
||||
HN_FEISHU_ENCRYPT_KEY=实际事件加密密钥
|
||||
HN_FEISHU_VERIFICATION_TOKEN=实际校验Token
|
||||
```
|
||||
|
||||
本版默认适配材料中的legacy OIDC接口:`auth/v3/app_access_token/internal`、`authen/v1/oidc/access_token`、`authen/v1/oidc/refresh_access_token`、`authen/v1/user_info`。机器人回复使用单独缓存的tenant_access_token,不把OAuth的app_access_token混用于消息API。实际私有化版本若不同,在 `relay/platform.py`中集中适配;不要直接改为公有云地址试错。
|
||||
|
||||
平台侧:
|
||||
|
||||
1. 创建/选择机器人应用,开通私聊消息接收和回复所需权限,发布版本并限制试点可见范围。
|
||||
2. 用户文档授权权限与机器人权限分开核对;不能把“tenant权限全空”直接套到需要应用身份回复消息的机器人。
|
||||
3. OAuth重定向地址配置为 `https://入口域名/oauth/cb`。
|
||||
4. 在以下事件源中选择一个,不要同时启用后再依赖去重掩盖配置错误。
|
||||
|
||||
### 方式A:长连接(领导方案过渡态)
|
||||
|
||||
后台选择长连接,订阅 `im.message.receive_v1`:
|
||||
|
||||
```bash
|
||||
docker compose up -d --force-recreate connector
|
||||
docker compose --profile longconn up -d longconn
|
||||
```
|
||||
|
||||
使用官方lark-oapi SDK支持的 `domain`参数,明确指向私有化平台;SDK日志关闭以防打印连接URL或正文。单Redis租约防止重复消费者。SDK实际建立连接、私有化平台消息格式和频控必须实测。
|
||||
|
||||
### 方式B:webhook
|
||||
|
||||
配置回调为 `https://入口域名/feishu/event`,要求请求签名、时间戳、校验Token及配置的加密参数一致。支持加密事件;校验失败拒绝,不开放绕过开关。
|
||||
|
||||
用户分别私聊机器人 `/绑定`,在自己PC输入绑定码,再发送 `/主机名`、`/回显 内容`。
|
||||
|
||||
## 7. 本地海诺Agent对接
|
||||
|
||||
默认桥接端不猜测1.065接口。若本地海诺适配层实现了[协议](PROTOCOL.md)中的只读HTTP接口,启动时增加:
|
||||
|
||||
```powershell
|
||||
python -m relay.cli device --directory D:\HainuoRelayDevice --url wss://relay.company.example/device/ws --agent-url http://127.0.0.1:8765/relay/read-only
|
||||
```
|
||||
|
||||
此时 `/助手 整理测试文件`才调用固定回环地址。Agent侧必须真实限制工具与文件权限;`read_only:true`字段本身不能阻止一个拥有写权限的Agent写文件。
|
||||
|
||||
## 8. 维护、备份、停用
|
||||
|
||||
```bash
|
||||
docker compose logs --tail 100 connector router
|
||||
# 不开access log/debug,不采集请求体、令牌或SDK明文事件。
|
||||
docker compose exec connector python -m relay.cli revoke --host-id 实际32位host_id --url http://127.0.0.1:8000
|
||||
docker compose exec -T postgres pg_dump -U hainuo -d hainuo -Fc > metadata-backup.dump
|
||||
# 恢复演练使用独立测试数据库,核对设备数、绑定与吊销状态;不要覆盖现有数据库。
|
||||
docker compose restart connector
|
||||
```
|
||||
|
||||
Redis不落盘;重启后在线状态重建,B1从持久库恢复准入/吊销数据。若需要手动同步,运行 `reconcile`子命令。
|
||||
|
||||
B1密钥、数据库和CA分别制定备份策略;不能把整个runtime打包当普通日志发出。不能只用 `docker compose down`冒充备份;不要执行 `down -v`删除元数据卷。
|
||||
|
||||
切换 `HN_ENVIRONMENT=production`会禁用模拟绑定码和模拟派发API。切换前必须替换测试TLS证书、配置真实平台并完成两人两机验收;该开关不代表项目自动满足P0九关。
|
||||
@@ -0,0 +1,77 @@
|
||||
# 海能Work私有化平台接入 · 长连接增量
|
||||
|
||||
## 已确认与未确认
|
||||
|
||||
用户提供App ID及App Secret,Secret未写入本项目源码或交付包。本文App ID可公开配置,Secret必须在ECS交互输入,不作为命令参数传入。
|
||||
|
||||
2026-10-26从本机**未携带任何密钥**验证:
|
||||
|
||||
| 请求 | 结果 | 判断 |
|
||||
|---|---|---|
|
||||
| GET https://open.w.cnooc.com.cn/app/ | HTTP200、HTML | 开发者后台页面,不是API根路径 |
|
||||
| POST https://open.w.cnooc.com.cn/open-apis/auth/v3/app_access_token/internal,空JSON | HTTP200、JSON code10003 | API路由存在;未验证凭据 |
|
||||
| POST https://open.w.cnooc.com.cn/open-apis/auth/v3/tenant_access_token/internal,空JSON | HTTP200、JSON code10003 | API路由存在;未验证凭据 |
|
||||
|
||||
所以API基址应为 `https://open.w.cnooc.com.cn`,不加 `/app/`。长连接配置的WSS目标由平台接口返回,不猜测地址或固定使用公有云。
|
||||
|
||||
## ECS更新与配置
|
||||
|
||||
将 `hainuo-relay-feishu-update-20261026.tar.gz`上传到/home/cnooc:
|
||||
|
||||
```bash
|
||||
tar -xzf /home/cnooc/hainuo-relay-feishu-update-20261026.tar.gz \
|
||||
-C /home/cnooc/hainuo-offline-arm64
|
||||
cd /home/cnooc/hainuo-offline-arm64/hainuo-relay
|
||||
sudo bash scripts/start-feishu-longconn.sh
|
||||
```
|
||||
|
||||
脚本离线构建已含官方SDK的服务镜像,不下载新依赖。提示 `App Secret (hidden input):`时,在服务器终端输入实际密钥,不回显,也不发到聊天。
|
||||
|
||||
配置工具依次验证app token、tenant token和 `/callback/ws/endpoint`(只获取连接配置,不发送平台消息);成功后才备份并原子写入.env。任何一步失败不会提交新配置。输出只显示成功状态、业务错误码或WSS主机名,不显示密钥、令牌、完整WSS URL或上游响应正文。
|
||||
|
||||
长连接不需要Webhook的Encrypt Key/Verification Token,已修正原配置校验;Webhook缺少鉴权参数时继续拒绝请求。OAuth保持关闭,避免在未核验IP回调白名单前把H5授权当成可用;当前PC配对H5不受影响。
|
||||
|
||||
脚本保留旧镜像标签、已有数据库、设备证书、绑定和H5,重建B1并启动独立longconn。继续使用compose.work.yaml及已有edge/H5覆盖文件。PC无需重注册。
|
||||
|
||||
检查:
|
||||
|
||||
```bash
|
||||
sudo docker compose logs --tail 30 longconn
|
||||
sudo docker compose ps connector longconn
|
||||
```
|
||||
|
||||
`platform_connected`证明SDK建立了平台连接;`platform_event_accepted`证明事件已转交B1,不等同已执行或平台回复成功。
|
||||
|
||||
## 平台后台仍必须配置
|
||||
|
||||
在https://open.w.cnooc.com.cn/app/选择对应应用:
|
||||
|
||||
1. 启用机器人,事件订阅选择长连接;不要同时配置另一个公网Webhook消费同一流。
|
||||
2. 订阅 `im.message.receive_v1`。
|
||||
3. 开通该私有化版本要求的私聊接收、机器人消息回复权限。精确scope以平台实际权限列表为准,不猜公有云名称。
|
||||
4. 发布应用,先只对两名测试人员可见。
|
||||
5. 用户在海能Work私聊机器人,发送 `/回显 海能Work消息已通`。此时尚未绑定真实open_id,若未绑定诊断会提示设备不可用;先发送 `/绑定`验证能收到绑定码。
|
||||
|
||||
当前机器人接入阶段只开放 /绑定、/解绑、/主机名、/回显。尚未将本机Profile全工具权限开放给IM,符合领导方案先做身份与归属验证的顺序;H5现有受控工具试验不因此关闭。
|
||||
|
||||
## 测试身份迁移到真实open_id
|
||||
|
||||
**先确认机器人能收消息并回复绑定码,再做迁移;不要在平台未通时先解绑。**
|
||||
|
||||
1. 停止PC桥接端,完成当前所有任务。
|
||||
2. ECS执行管理员解绑,只移除模拟owner,不吊销设备证书:
|
||||
|
||||
```bash
|
||||
sudo docker compose exec -T connector python -m relay.cli unbind \
|
||||
--host-id 79cb2127dd554a1fb592101945df5230 --url http://127.0.0.1:8000
|
||||
```
|
||||
|
||||
旧H5会话与配对码失效,模拟账号不再能调用。设备私钥、host_id和证书保留。
|
||||
|
||||
3. 本人在海能Work私聊 `/绑定`,取得新的5分钟绑定码。
|
||||
4. Windows运行原 `relay.cli bind --directory D:\HainuoRelayDevice --url https://10.77.76.13 --tls-ca D:\HainuoRelayDevice\server.pem`,输入新码。
|
||||
5. 重启使用hainuo-config的PC桥接端,海能Work发 `/主机名`,应返回LAPTOP-4N7G74MO。
|
||||
6. ECS status应显示真实平台open_id,而非ou_test_tianzeyuan。两人两机各自重复,验证不串机。
|
||||
7. 需要继续H5时,在PC重新生成网页配对码,不复用旧会话。
|
||||
|
||||
现有Secret曾出现在聊天中,正式试点前应在平台重置,使用本工具在ECS隐藏输入新值;不把新Secret发到聊天或打进镜像。
|
||||
@@ -0,0 +1,78 @@
|
||||
# H5配对和真实海诺对话 · 2026-10-26增量
|
||||
|
||||
## 已有环境无需重做
|
||||
|
||||
- ECS:10.77.76.13:443,目录 `/home/cnooc/hainuo-offline-arm64/hainuo-relay`。
|
||||
- PC已绑定 `ou_test_tianzeyuan`,证书目录 `D:\HainuoRelayDevice`。
|
||||
- 实际海诺安装位置 `%LOCALAPPDATA%\Hainuo`,不是示例D:\Hainuo1065。
|
||||
- 本机原生API:127.0.0.1:8643,纯文本模式;ECS→桥接端→真实模型已经返回42。
|
||||
|
||||
本次新增:网页配对、Secure/HttpOnly浏览器会话、设备在线状态、纯文本聊天、离线提示、请求去重、退出登录及绑定变更失效。暂不依赖海能Work OAuth配置。
|
||||
|
||||
## 1. 更新ECS
|
||||
|
||||
将 `hainuo-relay-h5-update-20261026.tar.gz`上传到 `/home/cnooc`,执行:
|
||||
|
||||
```bash
|
||||
tar -xzf /home/cnooc/hainuo-relay-h5-update-20261026.tar.gz \
|
||||
-C /home/cnooc/hainuo-offline-arm64
|
||||
cd /home/cnooc/hainuo-offline-arm64/hainuo-relay
|
||||
sudo bash scripts/update-h5.sh
|
||||
```
|
||||
|
||||
增量包只包含源码、网页资源、更新脚本、Dockerfile和说明,不包含.env、runtime、证书、CA或数据库。更新先保留旧镜像标签,使用原wheelhouse离线构建,只重建连接器;其他组件不重装。连接器重启时在途请求可能中断,等当前任务完成后再更新。
|
||||
|
||||
不要运行旧的install.sh重新初始化,不要重新解压整包覆盖更新后的源码。后续recreate使用compose.h5.yaml,以保留180秒模型等待时间。
|
||||
|
||||
## 2. Windows生成网页配对码
|
||||
|
||||
本机项目已更新 `web-pair`命令,原生API与桥接端保持运行。Windows PowerShell执行:
|
||||
|
||||
```powershell
|
||||
cd "C:\Users\huawei\Desktop\海能h5连接海诺客户端\hainuo-relay"
|
||||
& "..\.venv\Scripts\python.exe" -m relay.cli web-pair `
|
||||
--directory D:\HainuoRelayDevice `
|
||||
--url https://10.77.76.13 `
|
||||
--tls-ca D:\HainuoRelayDevice\server.pem
|
||||
```
|
||||
|
||||
输出网页地址和一次性配对码。有效期120秒;不要发到群里、工单或聊天截图。生成失败HTTP404表示ECS仍是旧版,先完成步骤1。
|
||||
|
||||
## 3. 浏览器配对和聊天
|
||||
|
||||
打开 `https://10.77.76.13/h5`,必要时Ctrl+F5刷新。页面应出现“输入电脑上的配对码”和“连接我的电脑”,不再只有原OAuth按钮。
|
||||
|
||||
粘贴配对码后点击连接。网页显示“你的电脑已在线”后,发送:
|
||||
|
||||
```text
|
||||
请计算17加25,只回复计算结果。
|
||||
```
|
||||
|
||||
预期回复42。普通文本自动加 `/助手`转给本机真实海诺API;`/主机名`和`/回显 内容`保留用于核对目标设备。
|
||||
|
||||
每个消息有独立请求ID;同一ID不会重复执行。网络失败不自动重发;出现状态未知时先核查,不要重复提交有副作用的任务。当前所有原生模型调用禁止工具,不能执行删除等指令。
|
||||
|
||||
## 会话和安全边界
|
||||
|
||||
- 网页登录依靠本机设备私钥签名申请的高熵一次性码,不接受网页自报owner/host作为身份。
|
||||
- 配对码120秒一次消费、会话30分钟固定过期。服务端只保存配对/会话凭据的SHA256索引;浏览器凭据只放Secure、HttpOnly、SameSite=Strict Cookie,不放URL或localStorage。
|
||||
- 网页POST必须Origin与HN_PUBLIC_URL一致;ECS当前应为 `https://10.77.76.13`。不开放任意Origin,不用管理员密钥给网页登录。
|
||||
- 请求主体由浏览器会话确定;切换用户、解绑、吊销或绑定代次变化会失效。另一用户不能通过提交open_id调用你的电脑。
|
||||
- 这是**试点中继网页模式**:浏览器HTTPS在入口终止,B1在内存中处理网页正文,再应用层加密发到PC;B2只见密文。中继不保存聊天正文,网页关闭或退出后当前页面历史不保留。
|
||||
- 这与领导原方案“浏览器TLS直接终止在PC、中心看不到网页正文”不同。不能将本模式称作浏览器到PC端到端加密或正式生产安全验收通过。
|
||||
- 海能Work授权入口仍保留;只有配置真实平台OAuth后才能使用。后续可独立切换平台身份来源。
|
||||
- 本地原生API+桥接端目前是后台进程,电脑重启后需要按原生接入手册恢复启动;尚未实现Windows服务自启动。
|
||||
|
||||
## 验收
|
||||
|
||||
1. 未配对网页不可派发任务,直接访问/web/chat应401。
|
||||
2. 配对后收到42,/主机名与Windows hostname一致。
|
||||
3. 同一配对码第二个浏览器使用失败。
|
||||
4. 退出网页登录后,原Cookie立即无效。
|
||||
5. PC停止桥接端时网页显示离线,新问题返回离线提示;重连后恢复。
|
||||
6. 两个设备不同账号分别配对,消息不串机。
|
||||
7. 跨站Origin不能兑换配对码或派发任务。
|
||||
|
||||
本地自动化用真实HTTP/WebSocket与模拟平台/模型检验链路;真实ECS H5页面更新后的配对与对话,仍需按上述步骤执行验收。
|
||||
|
||||
本轮回归结果:48项通过、1项真实PostgreSQL用例因本地环境缺失跳过。新增9项包含H5会话与原生HTTP组合链路,覆盖一次配对码、并发消费、跨站拒绝、两账号隔离、去重、退出与过期、绑定变更失效。
|
||||
@@ -0,0 +1,53 @@
|
||||
# 官方开源镜像与完全离线安装
|
||||
|
||||
本次补充的是Linux ARM64镜像,不是AMD64镜像。独立离线包目录包含 `images/`、源码项目、ARM64 wheelhouse、SHA256校验和及安装脚本。
|
||||
|
||||
## 镜像来源与许可证
|
||||
|
||||
| 镜像标签 | 官方发布来源 | 核心项目许可证 |
|
||||
|---|---|---|
|
||||
| python:3.12-slim-bookworm | Docker Official Images / Python | PSF License及其许可历史 |
|
||||
| postgres:17-alpine | Docker Official Images / PostgreSQL | PostgreSQL License |
|
||||
| redis:8.2-alpine | Docker Official Images / Redis | 本次选择AGPLv3,而非RSAL/SSPL |
|
||||
| caddy:2-alpine | Docker Official Images / Caddy | Apache-2.0 |
|
||||
|
||||
Docker Hub直连在当前网络超时,本次从Docker Official Images在Amazon ECR Public的公共发布仓库 `public.ecr.aws/docker/library/*`获取。不是第三方重新构建的同名镜像。保留原始镜像config和layer;导出Docker-load格式时逐项验证registry SHA256及解压rootfs diff_id。
|
||||
|
||||
`IMAGE_MANIFEST.json`记录index、ARM64 manifest、config与layers摘要及实际下载来源。`VERIFICATION.json`记录校验结果和镜像内版本环境变量。标签可能继续更新,本包具体内容以这些摘要为准;离线安装使用本包内容并禁止重新拉取。
|
||||
|
||||
Redis原7.4配置采用RSAL/SSPL,不能笼统称为OSI开源。因此调整到Redis8.2并选择其AGPLv3许可路径。应用使用的AUTH、GETDEL、租约、Pub/Sub等协议在8.2仍提供;本地自动化测试使用FakeRedis,真实Redis8.2运行仍须在目标Docker环境验收。
|
||||
|
||||
官方许可来源:
|
||||
|
||||
- https://redis.io/legal/licenses/
|
||||
- https://www.postgresql.org/about/licence/
|
||||
- https://docs.python.org/3/license.html
|
||||
- https://github.com/caddyserver/caddy/blob/master/LICENSE
|
||||
- https://gallery.ecr.aws/docker/library/python
|
||||
|
||||
基础发行版和各依赖另有许可证;本表不替代完整SBOM、漏洞扫描或企业开源准入。Redis官方源码和许可链接随离线包提供,使用与再分发遵守所选许可。
|
||||
|
||||
## 上传后的操作
|
||||
|
||||
服务器已有Docker Engine和Compose v2时:
|
||||
|
||||
```bash
|
||||
tar -xzf hainuo-relay-offline-arm64-0.1.0.tar.gz
|
||||
cd hainuo-offline-arm64
|
||||
bash install.sh relay.your-company-domain
|
||||
```
|
||||
|
||||
安装脚本会校验文件、导入四个官方镜像,使用已导入Python基础镜像与本地41项wheel构建服务,然后初始化和启动。构建使用 `--network none --pull=false`,启动使用 `--no-build --pull never`。
|
||||
|
||||
海诺服务镜像是本项目源码构建的 `hainuo-relay:0.1.0`,不是冒充某个官方已有服务镜像。本次不包含Docker Engine/Compose宿主软件、不包含业务数据,也不消除配置真实域名、TLS、海能Work凭据及客户端适配的必要性。
|
||||
|
||||
配置修改后离线重启:
|
||||
|
||||
```bash
|
||||
cd hainuo-relay
|
||||
docker compose -f compose.yaml -f compose.offline.yaml up -d --no-build --pull never
|
||||
# 若选择官方SDK长连接,在配置平台信息后:
|
||||
docker compose -f compose.yaml -f compose.offline.yaml --profile longconn up -d --no-build --pull never
|
||||
```
|
||||
|
||||
现有普通源码ZIP仍可用于联网构建;完全离线部署请使用新增的整包,而非只传旧源码ZIP。
|
||||
@@ -0,0 +1,117 @@
|
||||
# 中继协议 v1
|
||||
|
||||
## 服务接口
|
||||
|
||||
| 端点 | 身份/约束 | 用途 |
|
||||
|---|---|---|
|
||||
| GET /health | 无 | 存活 |
|
||||
| GET /ready | 内部网络 | 依赖就绪 |
|
||||
| POST /admin/devices | X-Admin-Secret | 登记CA签发的设备证书 |
|
||||
| GET /admin/devices | 同上 | 设备、绑定和在线状态 |
|
||||
| POST /admin/devices/{host}/revoke | 同上 | 持久吊销并断连 |
|
||||
| POST /admin/reconcile | 同上 | 从持久库恢复路由准入数据 |
|
||||
| POST /admin/test/binding-code | 同上,非production | 生成模拟账号绑定码 |
|
||||
| POST /admin/test/dispatch | 同上,非production | 同步等待模拟指令结果 |
|
||||
| POST /device/bind | 设备签名+一次性绑定码 | 本机完成绑定 |
|
||||
| WS /device/ws | B2:CA证书+挑战签名 | PC出站隧道 |
|
||||
| POST /feishu/event | 平台签名、Token、时窗 | webhook接入 |
|
||||
| POST /internal/events | X-Internal-Secret | 官方SDK进程转入事件 |
|
||||
| POST /oauth/start | 限流、容量限制 | 创建授权事务 |
|
||||
| GET /oauth/cb | 服务端state | 收码后立即换token |
|
||||
| GET /oauth/claim | state+X-OAuth-Claim-Secret | 一次领取token |
|
||||
| GET /oauth/pending | 同上 | 查询状态,无token |
|
||||
| POST /oauth/refresh | refresh_token,防并发重复 | 刷新、返回新token |
|
||||
| GET /proxy | 本人Bearer+X-Relay-Target | 默认关闭的只读数据代理 |
|
||||
| GET /api/whoami | 本人Bearer,经平台验证 | 查询本人设备URL |
|
||||
| GET /h5 | 无,页面内发起OAuth | 入口壳 |
|
||||
|
||||
管理接口和internal/events不通过公网反代。不要在nginx/Caddy上额外放开这些路径。
|
||||
|
||||
## 设备注册与绑定
|
||||
|
||||
1. PC生成Ed25519私钥和随机32位hex host_id,只发送公钥申请。
|
||||
2. 离线签发工作站签发带 `clientAuth`、SAN URI `urn:hainuo:host:{host_id}`的设备证书。
|
||||
3. 管理员将证书登记到B1。B1持久保存证书指纹,并把准入指纹同步到Redis。
|
||||
4. 本人私聊机器人 `/绑定`。绑定码具有96位随机熵、TTL300秒;Redis只保存码的SHA256索引和owner。
|
||||
5. PC向 `/device/bind`发送:
|
||||
|
||||
```json
|
||||
{"host_id":"32位hex", "proof":{"host_id":"同一host_id","code":"绑定码","ts":1793000000,"signature":"base64url签名"}}
|
||||
```
|
||||
|
||||
签名覆盖去掉signature后的规范JSON。B1先校验证书、签名、时间和注册状态,再原子消费绑定码,持久绑定owner↔host。返回连接器签名的绑定回执;PC使用预置信任公钥验证回执并保存owner。
|
||||
|
||||
本版一人一台、一台一人;解绑后必须在PC重新绑定。本版不提供远端任意指定owner的公开接口。
|
||||
|
||||
## 隧道握手
|
||||
|
||||
1. B2发送 `{type:"challenge",challenge:随机值}`。
|
||||
2. PC每次连接生成临时X25519密钥和session_id,提交证书与签名proof:
|
||||
|
||||
```json
|
||||
{"host_id":"...","cert":"PEM证书","proof":{"host_id":"...","session_id":"...","challenge":"...","kx":"X25519公钥base64url","ts":1793000000,"signature":"..."}}
|
||||
```
|
||||
|
||||
3. B2验证CA、证书有效期、用途、host_id、挑战、时间、注册指纹和吊销状态。通过后设置30秒在线租约,8秒续约;第二连接被拒绝。
|
||||
4. B1发送前独立核对持久库指纹、设备证书、握手签名,不能信任B2下发一个新公钥。
|
||||
|
||||
## 加密信封
|
||||
|
||||
```json
|
||||
{"v":1,"host_id":"...","session_id":"...","ts":1793000000,"nonce":"12随机字节base64url","ephemeral":"消息发送方临时X25519公钥","ciphertext":"AESGCM密文和tag","signature":"Ed25519签名"}
|
||||
```
|
||||
|
||||
- JSON规范化:UTF-8、键排序、无多余空白、非ASCII不转义。
|
||||
- ECDH:每消息临时发送密钥 ↔ 当前连接的临时接收密钥。
|
||||
- HKDF-SHA256:32字节,salt=session_id UTF-8,info=`hainuo-relay-envelope-v1`。
|
||||
- AEAD:AES-256-GCM;AAD是除ciphertext和signature以外的信封字段。
|
||||
- 签名:覆盖除signature外所有字段;验证后再解密。
|
||||
- 接收方验证host、session和90秒时间窗。真正执行去重使用稳定turn_id,PC侧持久记录,不依赖nonce缓存。
|
||||
- B1每次请求生成独立临时回复密钥,公钥放在加密的请求体中;私钥只在等待结果期间持有。
|
||||
|
||||
长期Ed25519身份密钥不参与内容ECDH。已销毁临时密钥的历史流量不能仅凭长期签名私钥解密。本版未接受独立密码协议审计,不将该设计等同已通过企业密码评估。
|
||||
|
||||
请求明文只在B1与对应PC内:
|
||||
|
||||
```json
|
||||
{"type":"instruction","turn_id":"稳定请求ID","owner":"ou_xxx","text":"/主机名","read_only":true,"channel":"im","reply_key":"B1临时回复公钥"}
|
||||
```
|
||||
|
||||
PC必须再次核对owner;签名有效不代表可以执行任意系统操作。
|
||||
|
||||
回复:
|
||||
|
||||
```json
|
||||
{"type":"reply","turn_id":"同一ID","envelope":{"...":"加密信封"}}
|
||||
```
|
||||
|
||||
解密后为 `{type:"result",turn_id:"同一ID",result:"结果"}`。B2检查存在该host、session、turn的在途记录,B1再检查签名、加密和请求归属。PC不能指定其他聊天收件人。
|
||||
|
||||
## 去重和失败语义
|
||||
|
||||
- 平台事件以message_id派生稳定turn_id;消息重投不会重新生成随机执行ID。
|
||||
- B1在发布前写持久去重台账,PC执行前写本机journal。两端均采取“宁可状态未知,不自动重复执行”。
|
||||
- Redis使用Pub/Sub,不缓存正文,不离线排队。主机离线即返回offline。
|
||||
- 超时/重启丢失在途结果返回unknown或等待超时,不能当成未执行。重新生成新ID有可能再次执行,应由用户核查。
|
||||
- 同一ID重复提交只返回duplicate,不缓存/重放原结果正文。
|
||||
- B1后台处理失败写元数据状态,当前版本没有可靠消息发件箱;已执行但回平台失败时不会自动再次执行。
|
||||
|
||||
## 本地Agent适配接口(需客户端开发实现)
|
||||
|
||||
固定回环地址,例如 `POST http://127.0.0.1:8765/relay/read-only`:
|
||||
|
||||
```json
|
||||
{"text":"整理测试目录内的文档","open_id":"ou_xxx","turn_id":"...","channel":"im","read_only":true}
|
||||
```
|
||||
|
||||
成功返回HTTP200:`{"result":"助手结果"}`。桥接端只接受文本结果、限制大小,不跟随重定向。可通过桥接端环境变量 `HN_LOCAL_AGENT_TOKEN`提供本地Bearer凭据。
|
||||
|
||||
该接口不是从1.065中发现的真实接口,而是本次新增适配契约。Agent服务需要实现按turn_id去重、主体与文件权限校验、只读工具白名单;不能只把read_only字段拼进提示词。未提供适配接口时,诊断命令仍能正常工作。
|
||||
|
||||
## OAuth与代理
|
||||
|
||||
沿用原手册v1.2响应字段;使用平台实际返回的expires_in/expire,不写死7200。待取token只存在B1内存,600秒过期,领取即删除。错误claim_secret返回404且不消费;同一code并发回调只换一次。
|
||||
|
||||
刷新用refresh_token摘要作为分布式一次尝试标记,保留30天。网络故障可能导致平台已轮换但客户端未拿到结果,因此不重试旧token,要求重新授权。
|
||||
|
||||
代理固定走配置的平台域名,只支持明确白名单的GET接口;永不使用应用token代替用户token,不代理换token接口,不跟随重定向,不持久保存响应正文。token每次由平台user_info验证,本版不缓存token验证结果。
|
||||
@@ -0,0 +1,46 @@
|
||||
# 从单机迁移到六台
|
||||
|
||||
## 服务归属
|
||||
|
||||
| 主机 | 部署内容 | 关键配置 |
|
||||
|---|---|---|
|
||||
| A1 | 入口代理 | 指向B1活动节点与B2;入口TLS证书 |
|
||||
| A2 | 同上 | VIP/负载均衡、健康探测 |
|
||||
| B1-1 | connector+longconn(如使用) | 平台凭据、连接器私钥、PostgreSQL/Redis地址 |
|
||||
| B1-2 | connector备用+longconn备用 | 相同身份信任与公共数据库;按主备策略切换 |
|
||||
| B2-1 | router | 设备CA公钥、Redis地址;不分配平台密钥/管理员凭据 |
|
||||
| P1 | PostgreSQL+Redis+外部监控 | 仅允许相应服务网段访问;备份、权限、时钟 |
|
||||
|
||||
CA私钥留离线签发工作站,**不因拆六台而复制到P1**。
|
||||
|
||||
## 网络矩阵
|
||||
|
||||
| 来源 | 目标 | 端口/用途 |
|
||||
|---|---|---|
|
||||
| PC、平台webhook、H5 | A的VIP/域名 | TCP443 |
|
||||
| A | B1 | 本版服务HTTP8000;跨主机应经受信TLS/mTLS代理 |
|
||||
| A | B2 | 本版WS8000;跨主机应经受信TLS/mTLS代理 |
|
||||
| B1 | 私有化开放平台 | TCP443,含SDK长连接目的域名 |
|
||||
| B1、B2、longconn | Redis | TLS端口按实际Redis配置,禁止公网 |
|
||||
| B1 | PostgreSQL | TCP5432,跨主机启用TLS验证 |
|
||||
| 管理堡垒机 | B1管理入口 | 经SSH隧道/受控内网,不直接公网开放 |
|
||||
|
||||
不要把单机Docker桥接网络里的明文连接原样暴露到公网。修改Redis地址为`rediss://`及PostgreSQL连接的`sslmode=verify-full`等参数,并配置CA校验。Redis角色ACL细分与跨机mTLS不由现有Compose自动完成。
|
||||
|
||||
## 迁移步骤
|
||||
|
||||
1. 维护窗口暂停新任务,等待在途任务结束;向用户说明连接会短暂中断。
|
||||
2. 备份PostgreSQL,保存B1签名密钥和信任公钥的受控副本。保持设备CA、host_id、owner及签名身份不变。
|
||||
3. 将PostgreSQL/Redis迁至P1,恢复数据库并验证设备绑定、吊销和去重数据。Redis在线状态可重建,不迁移正文。
|
||||
4. 部署B2,更新网络地址;部署B1并等待ready;然后更新A的上游。
|
||||
5. 保留原域名与443,PC无需换host_id或重新配对。若入口域名改变,更新桥接端连接地址。
|
||||
6. 两人两机重新验收后,再部署A2和B1-2,实施VIP/主备与故障演练。
|
||||
|
||||
## 并非只开端口就自动高可用
|
||||
|
||||
- 协议和业务代码可复用,但主备切换、DNS/VIP、证书和数据库恢复属于实际迁移工作。
|
||||
- B1使用公共PostgreSQL,不能让两个实例各自写不同SQLite。SQLite只用于本地测试,不用于跨主机主备。
|
||||
- OAuth待领令牌和等待结果仅在某个B1内存;活动节点故障时事务会丢失,客户端重新授权,已执行任务不能盲目重发。
|
||||
- 官方SDK事件源有Redis租约防重复消费者;入口仍需把OAuth事务固定到活动B1。暂不提供跨B1内存状态复制。
|
||||
- B2可按设备在线租约分布到多个节点,Redis频道把数据送到持有该设备隧道的节点;同一设备只能建立一条有效连接。
|
||||
- 六台中的B2与P仍是单点,不承诺全系统无中断;若六台共用一台物理宿主机,也未隔离宿主机故障。
|
||||
@@ -0,0 +1,50 @@
|
||||
# 本次交付验证记录
|
||||
|
||||
日期:2026-10-26。执行环境:Windows x64、Python3.12.10;本机未提供Docker Engine或PostgreSQL服务。
|
||||
|
||||
## 自动化结果
|
||||
|
||||
执行 `python -m pytest -q --junitxml=docs/test-results.xml`:**26项通过、1项真实PostgreSQL用例因缺少环境跳过**。逐项耗时以同目录JUnit XML为准。
|
||||
|
||||
测试使用:真实本地HTTP端口、真实WebSocket连接、两台独立设备目录及独立密钥、真实签名/加密/解密、SQLite持久台账;Redis使用FakeRedis,海能Work使用平台桩及HTTP MockTransport。
|
||||
|
||||
覆盖:
|
||||
|
||||
- 两设备并发独立回传;重复turn_id拒绝;设备持久journal重启后仍拒绝重复。
|
||||
- 错误CA、错误host、信封篡改、错误临时解密密钥拒绝。
|
||||
- 替换B2在线目录中的设备证书时,B1独立指纹检查拒绝。
|
||||
- 离线回执、吊销断连和持久吊销;同一设备第二连接被拒。
|
||||
- 只读测试目录读取、越界拒绝、未知系统命令不执行。
|
||||
- 超时返回unknown、不重执行;回复订阅退出时ready失败。
|
||||
- OAuth错误密钥不销毁、正确领取一次、并发callback/claim、过期、容量、限流。
|
||||
- refresh_token并发只有一次上游尝试;响应有效期使用6900等平台原值。
|
||||
- 明文/加密webhook签名验证、过期拒绝、重复平台消息只执行一次、群聊忽略。
|
||||
- H5资源、CSP、本人设备URL、拒绝任意跳转目标。
|
||||
- 只读数据代理拒绝任意上游、鉴权接口、路径穿越。
|
||||
- 本地Agent适配契约、回环限制、拒绝重定向。
|
||||
- 平台接口使用配置的私有化域名、原消息回复;上游异常不回显敏感内容。
|
||||
- 请求校验错误不返回包含token的原始输入。
|
||||
|
||||
## 其他已执行检查
|
||||
|
||||
- Python源码compileall、H5 JavaScript语法、部署shell脚本语法通过。
|
||||
- Compose YAML可解析;数据库/Redis不发布端口,管理端口只绑定回环;公网只发布443。
|
||||
- 官方SDK `lark-oapi==1.5.3`确实支持 `ws.Client(domain=...)`,已在本地检查参数签名,未连接平台。
|
||||
- requirements.lock中的41项依赖全部成功解析并下载Linux ARM64/Python3.12兼容wheel;原生扩展包括cryptography、cffi、psycopg-binary、pydantic-core和websockets。
|
||||
- 本轮补充:四个官方镜像从Docker Official Images的ECR Public发布仓库下载成功,校验所有config、压缩blob、解压rootfs摘要及linux/arm64架构;提供Docker-load格式tar。没有实际Docker daemon导入/运行验证。
|
||||
|
||||
## 未执行 / 受环境限制
|
||||
|
||||
| 项目 | 当前状态 | 目标环境执行方式 |
|
||||
|---|---|---|
|
||||
| 真实PostgreSQL元数据用例 | 本地跳过 | Compose acceptance测试容器,或设置HN_TEST_DATABASE_URL |
|
||||
| Docker镜像构建与Compose启动 | 未执行,本机无Docker | ECS执行bootstrap/start/verify脚本 |
|
||||
| ARM64原生运行/64KiB内核 | 未执行;已核验依赖wheel可得 | ECS检查uname、PAGESIZE后实际启动 |
|
||||
| Docker Hub直连 | auth.docker.io连接超时;本轮已通过ECR Public官方发布仓库取得四个镜像 | 使用离线包,无需目标机联网拉镜像 |
|
||||
| Redis8.2真实服务和网络故障恢复 | 镜像已下载,运行未执行,测试用FakeRedis | ECS断线/恢复用例 |
|
||||
| 海能Work长连接、权限、频控、OAuth | 未执行 | 实际私有化应用联合测试 |
|
||||
| 1.065桥接、真实本机Agent | 未执行,协议尚未提供 | 按PROTOCOL.md接入后验收 |
|
||||
| H5到PC完整链路与文件回传 | 未实现完整链路 | 后续PC网页隧道与设备登录集成 |
|
||||
| 六台主备与容量 | 未执行 | 拆分说明与放量压测 |
|
||||
|
||||
Windows测试结果不能被表述为“ECS部署通过”或“海能Work业务验收通过”。原领导材料中的公网自测属于此前开发侧陈述,不是本次代码的执行证据。
|
||||
@@ -0,0 +1,21 @@
|
||||
"""Online consistent metadata backup. No tokens, transcripts, or signing keys."""
|
||||
import argparse
|
||||
import hashlib
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
p = argparse.ArgumentParser()
|
||||
p.add_argument("--database", type=Path, required=True)
|
||||
p.add_argument("--output", type=Path, required=True)
|
||||
args = p.parse_args()
|
||||
if args.output.exists():
|
||||
raise SystemExit("output already exists")
|
||||
source = sqlite3.connect(f"file:{args.database.resolve().as_posix()}?mode=ro", uri=True)
|
||||
target = sqlite3.connect(args.output)
|
||||
source.backup(target)
|
||||
assert target.execute("PRAGMA integrity_check").fetchone()[0] == "ok"
|
||||
target.close()
|
||||
source.close()
|
||||
digest = hashlib.sha256(args.output.read_bytes()).hexdigest()
|
||||
args.output.with_suffix(args.output.suffix + ".sha256").write_text(digest + "\n", encoding="ascii")
|
||||
print("Metadata backup and integrity check completed.")
|
||||
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
test "$#" -eq 1 || { echo 'Usage: bash scripts/bootstrap.sh relay.your-domain'; exit 1; }
|
||||
test ! -f .env || { echo '.env exists; refusing to overwrite credentials'; exit 1; }
|
||||
docker build -t hainuo-relay:0.1.0 .
|
||||
docker run --rm --user "$(id -u):$(id -g)" \
|
||||
-v "$PWD:/work" -w /work hainuo-relay:0.1.0 init --hostname "$1"
|
||||
echo 'Initialized. Configure .env and replace runtime/tls/server.pem + server.key before acceptance.'
|
||||
echo 'Keep operator/ca.key offline on the certificate-signing workstation.'
|
||||
@@ -0,0 +1,24 @@
|
||||
"""Read-only Docker Hub manifest check; never prints anonymous registry tokens."""
|
||||
import json
|
||||
import httpx
|
||||
|
||||
images = {
|
||||
"python": "3.12-slim-bookworm", "redis": "8.2-alpine",
|
||||
"postgres": "17-alpine", "caddy": "2-alpine",
|
||||
}
|
||||
with httpx.Client(timeout=30, follow_redirects=True) as client:
|
||||
output = []
|
||||
for name, tag in images.items():
|
||||
token_response = client.get("https://auth.docker.io/token", params={"service": "registry.docker.io", "scope": f"repository:library/{name}:pull"})
|
||||
token_response.raise_for_status()
|
||||
token = token_response.json()["token"]
|
||||
response = client.get(f"https://registry-1.docker.io/v2/library/{name}/manifests/{tag}", headers={
|
||||
"Authorization": "Bearer " + token,
|
||||
"Accept": "application/vnd.oci.image.index.v1+json, application/vnd.docker.distribution.manifest.list.v2+json"})
|
||||
response.raise_for_status()
|
||||
manifests = response.json().get("manifests", [])
|
||||
arm = [x for x in manifests if x.get("platform", {}).get("architecture") == "arm64" and x.get("platform", {}).get("os") == "linux"]
|
||||
if not arm:
|
||||
raise RuntimeError(f"No linux/arm64 manifest for {name}:{tag}")
|
||||
output.append({"image": f"{name}:{tag}", "arm64_digest": arm[0]["digest"]})
|
||||
print(json.dumps(output, indent=2))
|
||||
@@ -0,0 +1,117 @@
|
||||
"""Runs on the ECS; asks for App Secret on the TTY and never echoes it.
|
||||
|
||||
Does not send messages, create platform subscriptions, or reset device binding.
|
||||
Only commits local configuration after platform credential + WS provisioning checks.
|
||||
"""
|
||||
import argparse
|
||||
import getpass
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import sys
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
import httpx
|
||||
|
||||
|
||||
def check_platform(base, app_id, secret, client):
|
||||
results = []
|
||||
for path, field in [("/open-apis/auth/v3/app_access_token/internal", "app_access_token"),
|
||||
("/open-apis/auth/v3/tenant_access_token/internal", "tenant_access_token")]:
|
||||
response = client.post(base + path, json={"app_id": app_id, "app_secret": secret})
|
||||
if response.status_code != 200:
|
||||
raise RuntimeError("platform HTTP " + str(response.status_code))
|
||||
data = response.json()
|
||||
if data.get("code") != 0 or not data.get(field):
|
||||
code = data.get("code")
|
||||
raise RuntimeError("platform code " + str(code)[:30] if isinstance(code, int) else "unexpected platform response")
|
||||
results.append(field + ": valid (not displayed)")
|
||||
response = client.post(base + "/callback/ws/endpoint", json={"AppID": app_id, "AppSecret": secret}, headers={"locale": "zh"})
|
||||
if response.status_code != 200:
|
||||
raise RuntimeError("long-connection provisioning HTTP " + str(response.status_code))
|
||||
data = response.json()
|
||||
if data.get("code") != 0:
|
||||
code = data.get("code")
|
||||
raise RuntimeError("long-connection code " + str(code)[:30] if isinstance(code, int) else "unexpected long-connection response")
|
||||
url = (data.get("data") or {}).get("URL", "")
|
||||
parsed = urlsplit(url)
|
||||
if parsed.scheme != "wss" or not parsed.hostname:
|
||||
raise RuntimeError("platform did not return a WSS endpoint")
|
||||
results.append("long-connection endpoint: " + parsed.hostname + " (credential query omitted)")
|
||||
return results
|
||||
|
||||
|
||||
def update_env(path, settings):
|
||||
if not path.is_file():
|
||||
raise RuntimeError("existing .env missing; refusing to initialize deployment")
|
||||
original = path.read_text(encoding="utf-8-sig")
|
||||
backup = path.with_name(".env.before-feishu-" + datetime.now().strftime("%Y%m%d-%H%M%S-%f"))
|
||||
shutil.copy2(path, backup)
|
||||
backup.chmod(0o600)
|
||||
lines, written = [], set()
|
||||
for line in original.splitlines():
|
||||
name = line.split("=", 1)[0].strip() if "=" in line and not line.lstrip().startswith("#") else ""
|
||||
if name in settings:
|
||||
if name not in written:
|
||||
lines.append(name + "=" + settings[name])
|
||||
written.add(name)
|
||||
else:
|
||||
lines.append(line)
|
||||
lines += [name + "=" + value for name, value in settings.items() if name not in written]
|
||||
temporary = path.with_name(".env.feishu-tmp-" + uuid.uuid4().hex)
|
||||
fd = os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
||||
try:
|
||||
with os.fdopen(fd, "w", encoding="utf-8", newline="\n") as out:
|
||||
out.write("\n".join(lines) + "\n")
|
||||
os.replace(temporary, path)
|
||||
path.chmod(0o600)
|
||||
finally:
|
||||
if temporary.exists():
|
||||
temporary.unlink()
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--root", type=Path, required=True)
|
||||
parser.add_argument("--app-id", default="cli_a8733bb23078d3c7")
|
||||
parser.add_argument("--api-base", default="https://open.w.cnooc.com.cn")
|
||||
args = parser.parse_args()
|
||||
base = args.api_base.rstrip("/")
|
||||
parts = urlsplit(base)
|
||||
if parts.scheme != "https" or not parts.hostname or parts.path or parts.query or parts.fragment or parts.username or parts.password:
|
||||
raise RuntimeError("API base must be HTTPS origin only; do not append /app/")
|
||||
if not re.fullmatch(r"cli_[a-zA-Z0-9]{16}", args.app_id):
|
||||
raise RuntimeError("invalid App ID format")
|
||||
if not sys.stdin.isatty():
|
||||
raise RuntimeError("TTY required; use docker run -it so the secret stays hidden")
|
||||
secret = getpass.getpass("App Secret (hidden input): ")
|
||||
if not re.fullmatch(r"[A-Za-z0-9_-]{16,256}", secret):
|
||||
raise RuntimeError("invalid App Secret shape; nothing saved")
|
||||
# Never follow HTTP redirects carrying credentials, trust proxy environment,
|
||||
# print upstream bodies, or disable TLS verification.
|
||||
with httpx.Client(timeout=20, follow_redirects=False, trust_env=False) as client:
|
||||
results = check_platform(base, args.app_id, secret, client)
|
||||
update_env(args.root / ".env", {
|
||||
"HN_FEISHU_APP_ID": args.app_id, "HN_FEISHU_APP_SECRET": secret,
|
||||
"HN_FEISHU_API_BASE": base,
|
||||
"HN_FEISHU_AUTHORIZE_URL": base + "/open-apis/authen/v1/authorize",
|
||||
"HN_FEISHU_ENABLED": "true", "HN_FEISHU_EVENT_SOURCE": "longconn", "HN_OAUTH_ENABLED": "false",
|
||||
})
|
||||
for result in results:
|
||||
print(result)
|
||||
print("Configuration saved on this ECS; secret/token not displayed. OAuth remains disabled pending callback verification.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except Exception as error:
|
||||
if isinstance(error, RuntimeError):
|
||||
print("Configuration not completed: " + str(error), file=sys.stderr)
|
||||
else:
|
||||
print("Configuration not completed: " + type(error).__name__ + "; no credentials displayed", file=sys.stderr)
|
||||
raise SystemExit(1) from None
|
||||
@@ -0,0 +1,167 @@
|
||||
"""Fetch official linux/arm64 images via Docker's ECR Public official-image mirror.
|
||||
|
||||
No daemon or third-party image rebuild is required. Verify registry manifest/blob
|
||||
digests and uncompressed rootfs diff_ids, then write Docker-load-compatible tar.
|
||||
"""
|
||||
import argparse
|
||||
import concurrent.futures
|
||||
import gzip
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import tarfile
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import httpx
|
||||
|
||||
REGISTRY = "https://public.ecr.aws"
|
||||
IMAGES = {"python": "3.12-slim-bookworm", "postgres": "17-alpine",
|
||||
"redis": "8.2-alpine", "caddy": "2-alpine"}
|
||||
ACCEPT = ",".join(["application/vnd.oci.image.index.v1+json",
|
||||
"application/vnd.docker.distribution.manifest.list.v2+json",
|
||||
"application/vnd.oci.image.manifest.v1+json",
|
||||
"application/vnd.docker.distribution.manifest.v2+json"])
|
||||
|
||||
|
||||
def digest_file(path):
|
||||
value = hashlib.sha256()
|
||||
with path.open("rb") as source:
|
||||
for chunk in iter(lambda: source.read(4 * 1024 * 1024), b""):
|
||||
value.update(chunk)
|
||||
return value.hexdigest()
|
||||
|
||||
|
||||
def checked_response(client, url, headers, expected=None):
|
||||
for attempt in range(6):
|
||||
response = client.get(url, headers=headers)
|
||||
if response.status_code not in {429, 500, 502, 503, 504}:
|
||||
break
|
||||
time.sleep(min(2 ** attempt, 12))
|
||||
response.raise_for_status()
|
||||
data = response.content
|
||||
actual = "sha256:" + hashlib.sha256(data).hexdigest()
|
||||
if expected and actual != expected:
|
||||
raise RuntimeError("manifest digest mismatch")
|
||||
return json.loads(data), actual
|
||||
|
||||
|
||||
def blob(name, headers, descriptor, cache):
|
||||
digest = descriptor["digest"].split(":", 1)[1]
|
||||
path = cache / (digest + ".blob")
|
||||
if path.exists() and path.stat().st_size == descriptor["size"] and digest_file(path) == digest:
|
||||
return path
|
||||
for attempt in range(5):
|
||||
try:
|
||||
temp = path.with_suffix(".part")
|
||||
# Do not forward the registry bearer token across a blob-storage redirect.
|
||||
with httpx.Client(timeout=httpx.Timeout(120, connect=20), follow_redirects=False) as client:
|
||||
url = f"{REGISTRY}/v2/docker/library/{name}/blobs/{descriptor['digest']}"
|
||||
request_headers = {"Authorization": headers["Authorization"]}
|
||||
for _ in range(6):
|
||||
with client.stream("GET", url, headers=request_headers) as response:
|
||||
if response.is_redirect:
|
||||
url = str(response.url.join(response.headers["location"]))
|
||||
request_headers = {} # AWS signed blob URL needs no bearer token.
|
||||
continue
|
||||
response.raise_for_status()
|
||||
value, count = hashlib.sha256(), 0
|
||||
with temp.open("wb") as output:
|
||||
for data in response.iter_bytes(1024 * 1024):
|
||||
output.write(data)
|
||||
value.update(data)
|
||||
count += len(data)
|
||||
if count != descriptor["size"] or value.hexdigest() != digest:
|
||||
raise RuntimeError("blob integrity mismatch")
|
||||
temp.replace(path)
|
||||
print(json.dumps({"downloaded_mb": round(count / 1048576, 1), "blob": digest[:16]}), flush=True)
|
||||
return path
|
||||
raise RuntimeError("too many blob redirects")
|
||||
except (httpx.HTTPError, OSError, RuntimeError) as exc:
|
||||
if attempt == 4:
|
||||
raise RuntimeError(f"blob download failed ({type(exc).__name__}); credentials omitted") from None
|
||||
time.sleep(min(2 ** attempt, 8))
|
||||
|
||||
|
||||
def add_bytes(archive, name, data):
|
||||
entry = tarfile.TarInfo(name)
|
||||
entry.size, entry.mode, entry.mtime = len(data), 0o644, 0
|
||||
archive.addfile(entry, io.BytesIO(data))
|
||||
|
||||
|
||||
def export_image(record, cache, out):
|
||||
config = record["config"]
|
||||
cfg_name = config["digest"].split(":")[1] + ".json"
|
||||
cfg_data = (cache / (config["digest"].split(":")[1] + ".blob")).read_bytes()
|
||||
cfg = json.loads(cfg_data)
|
||||
if cfg.get("architecture") != "arm64" or cfg.get("os") != "linux":
|
||||
raise RuntimeError("image is not linux/arm64")
|
||||
layers, diffids = [], cfg["rootfs"]["diff_ids"]
|
||||
if len(diffids) != len(record["layers"]):
|
||||
raise RuntimeError("rootfs/layer count mismatch")
|
||||
for desc, expected in zip(record["layers"], diffids):
|
||||
blobpath = cache / (desc["digest"].split(":")[1] + ".blob")
|
||||
plain = cache / (expected.split(":")[1] + ".layer.tar")
|
||||
if not plain.exists() or digest_file(plain) != expected.split(":")[1]:
|
||||
temp = plain.with_suffix(".part")
|
||||
with blobpath.open("rb") as source:
|
||||
magic = source.read(2)
|
||||
opener = gzip.open if magic == b"\x1f\x8b" else open
|
||||
value = hashlib.sha256()
|
||||
with opener(blobpath, "rb") as source, temp.open("wb") as destination:
|
||||
for chunk in iter(lambda: source.read(4 * 1024 * 1024), b""):
|
||||
value.update(chunk)
|
||||
destination.write(chunk)
|
||||
if "sha256:" + value.hexdigest() != expected:
|
||||
raise RuntimeError("uncompressed rootfs digest mismatch")
|
||||
temp.replace(plain)
|
||||
layers.append((expected.split(":")[1] + "/layer.tar", plain))
|
||||
output = out / (record["name"] + "-" + record["tag"].replace("/", "-") + "-linux-arm64.tar")
|
||||
with tarfile.open(output.with_suffix(".part"), "w") as archive:
|
||||
add_bytes(archive, cfg_name, cfg_data)
|
||||
for name, path in layers:
|
||||
archive.add(path, arcname=name, recursive=False)
|
||||
add_bytes(archive, "manifest.json", json.dumps([{"Config": cfg_name,
|
||||
"RepoTags": [record["name"] + ":" + record["tag"]],
|
||||
"Layers": [name for name, _ in layers]}]).encode())
|
||||
output.with_suffix(".part").replace(output)
|
||||
record["archive"] = output.name
|
||||
record["archive_sha256"] = digest_file(output)
|
||||
record["config_json"] = cfg
|
||||
print(json.dumps({"exported": output.name, "mb": round(output.stat().st_size / 1048576, 1)}), flush=True)
|
||||
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser()
|
||||
p.add_argument("--output", type=Path, required=True)
|
||||
p.add_argument("--cache", type=Path, required=True)
|
||||
args = p.parse_args()
|
||||
args.output.mkdir(parents=True, exist_ok=True)
|
||||
args.cache.mkdir(parents=True, exist_ok=True)
|
||||
records = []
|
||||
with httpx.Client(timeout=30, follow_redirects=False) as client:
|
||||
for name, tag in IMAGES.items():
|
||||
token_response = client.get(REGISTRY + "/token/", params={"service": "public.ecr.aws", "scope": f"repository:docker/library/{name}:pull"})
|
||||
token_response.raise_for_status()
|
||||
headers = {"Authorization": "Bearer " + token_response.json()["token"], "Accept": ACCEPT}
|
||||
index, index_digest = checked_response(client, f"{REGISTRY}/v2/docker/library/{name}/manifests/{tag}", headers)
|
||||
descriptor = next(x for x in index["manifests"] if x.get("platform", {}).get("architecture") == "arm64"
|
||||
and x.get("platform", {}).get("os") == "linux")
|
||||
manifest, manifest_digest = checked_response(client, f"{REGISTRY}/v2/docker/library/{name}/manifests/{descriptor['digest']}", headers, descriptor["digest"])
|
||||
record = {"name": name, "tag": tag, "upstream": f"docker.io/library/{name}:{tag}",
|
||||
"download_source": f"public.ecr.aws/docker/library/{name}:{tag}",
|
||||
"platform": "linux/arm64", "index_digest": index_digest,
|
||||
"manifest_digest": manifest_digest, "config": manifest["config"], "layers": manifest["layers"]}
|
||||
print(json.dumps({"image": name + ":" + tag, "compressed_mb": round(sum(x['size'] for x in manifest['layers']) / 1048576, 1)}), flush=True)
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as pool:
|
||||
futures = [pool.submit(blob, name, headers, desc, args.cache) for desc in [manifest["config"], *manifest["layers"]]]
|
||||
for future in futures:
|
||||
future.result()
|
||||
export_image(record, args.cache, args.output)
|
||||
records.append(record)
|
||||
(args.output / "IMAGE_MANIFEST.json").write_text(json.dumps(records, indent=2) + "\n", encoding="utf-8")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,132 @@
|
||||
"""Run the actual 1.065 API adapter, isolated from bot/scheduler startup.
|
||||
|
||||
Run with the installed Hermes Python, not the relay Python. No installer code
|
||||
or existing profiles are edited. Only a separate loopback listener is started.
|
||||
"""
|
||||
import argparse
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import secrets
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def prepare_environment(install_root, profile, port, config_path):
|
||||
if not profile or any(c not in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-" for c in profile):
|
||||
raise ValueError("invalid profile")
|
||||
source = install_root / "hermes-agent"
|
||||
home = install_root if profile == "default" else install_root / "profiles" / profile
|
||||
if not (source / "gateway/platforms/api_server.py").is_file() or not (home / "config.yaml").is_file():
|
||||
raise RuntimeError("installation/profile does not match installer 1.065 layout")
|
||||
# Environment must be selected before importing any Hermes module (global caches).
|
||||
os.environ["HERMES_HOME"] = str(home.resolve())
|
||||
os.environ["API_SERVER_HOST"] = "127.0.0.1"
|
||||
os.environ["API_SERVER_PORT"] = str(port)
|
||||
os.environ["API_SERVER_CORS_ORIGINS"] = ""
|
||||
os.environ["HERMES_SKIP_CWD_MIGRATION"] = "1"
|
||||
sys.path.insert(0, str(source.resolve()))
|
||||
# Use the package's own dotenv support to reuse the real profile model credentials.
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv(install_root / ".env", override=False)
|
||||
if home != install_root:
|
||||
load_dotenv(home / ".env", override=True)
|
||||
if config_path.exists():
|
||||
data = json.loads(config_path.read_text(encoding="utf-8-sig"))
|
||||
if data.get("url") != f"http://127.0.0.1:{port}" or data.get("mode") != "text-only" or len(data.get("api_key", "")) < 32:
|
||||
raise ValueError("existing local adapter config does not match listener")
|
||||
key = data["api_key"]
|
||||
else:
|
||||
key = secrets.token_hex(32)
|
||||
data = {"url": f"http://127.0.0.1:{port}", "api_key": key, "mode": "text-only"}
|
||||
config_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with config_path.open("x", encoding="utf-8") as out:
|
||||
json.dump(data, out, indent=2)
|
||||
if os.name != "nt":
|
||||
config_path.chmod(0o600)
|
||||
os.environ["API_SERVER_KEY"] = key
|
||||
return key
|
||||
|
||||
|
||||
def restricted_adapter_class(native_class, tools_config):
|
||||
original_tools = tools_config._get_platform_tools
|
||||
def without_remote_tools(config, platform, **kwargs):
|
||||
if platform == "api_server":
|
||||
return set()
|
||||
return original_tools(config, platform, **kwargs)
|
||||
tools_config._get_platform_tools = without_remote_tools
|
||||
|
||||
class RestrictedNativeAPI(native_class):
|
||||
def _wire_plugin_handlers(self, app):
|
||||
# Native connect() normally appends plugin HTTP handlers after the
|
||||
# route table. Do not let that reopen the restricted companion API.
|
||||
return None
|
||||
|
||||
def _create_agent(self, *args, **kwargs):
|
||||
agent = super()._create_agent(*args, **kwargs)
|
||||
# Model/tool construction is native. Fail BEFORE run_conversation if
|
||||
# any plugin/recovery path silently reintroduced tools.
|
||||
if getattr(agent, "tools", None) or getattr(agent, "valid_tool_names", None):
|
||||
raise RuntimeError("text-only relay listener refuses an Agent with tools")
|
||||
return agent
|
||||
|
||||
def _http_route_table(self):
|
||||
allowed = {("GET", "/health"), ("GET", "/v1/models"),
|
||||
("GET", "/v1/capabilities"), ("POST", "/v1/chat/completions")}
|
||||
return [(method, path, handler) for method, path, handler in super()._http_route_table()
|
||||
if (method, path) in allowed]
|
||||
|
||||
async def _handle_capabilities(self, request):
|
||||
response = await super()._handle_capabilities(request)
|
||||
data = json.loads(response.body)
|
||||
data["hainuo_relay_policy"] = {"mode": "text-only", "tools": "disabled", "contract": "hainuo-1065-v1"}
|
||||
# Do not advertise native admin/session/tool operations removed above.
|
||||
endpoints = data.get("endpoints", {})
|
||||
data["endpoints"] = {k: v for k, v in endpoints.items() if k in {"health", "models", "capabilities", "chat_completions"}}
|
||||
data["features"] = {"text_only": True, "tools": False, "streaming": False}
|
||||
from aiohttp import web
|
||||
return web.json_response(data)
|
||||
|
||||
return RestrictedNativeAPI
|
||||
|
||||
|
||||
async def serve_native(port, key):
|
||||
from gateway.config import PlatformConfig
|
||||
from gateway.platforms.api_server import APIServerAdapter
|
||||
from hermes_cli import tools_config
|
||||
adapter_class = restricted_adapter_class(APIServerAdapter, tools_config)
|
||||
adapter = adapter_class(PlatformConfig(enabled=True, extra={"host": "127.0.0.1", "port": port,
|
||||
"key": key, "model_name": "hainuo-relay-text"}))
|
||||
try:
|
||||
if not await adapter.connect():
|
||||
raise RuntimeError("native API did not start; verify port and native installation")
|
||||
print(f"海诺原生API已启动:http://127.0.0.1:{port}(纯文本,无工具;凭据未回显)", flush=True)
|
||||
await asyncio.Event().wait()
|
||||
finally:
|
||||
await adapter.disconnect()
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--install-root", type=Path, required=True)
|
||||
parser.add_argument("--profile", default="hainuo")
|
||||
parser.add_argument("--port", type=int, default=8643)
|
||||
parser.add_argument("--config", type=Path, required=True)
|
||||
args = parser.parse_args()
|
||||
if not 1024 <= args.port <= 65535:
|
||||
raise ValueError("invalid port")
|
||||
# Native logs can contain provider/user content; this companion emits status only.
|
||||
logging.disable(logging.CRITICAL)
|
||||
key = prepare_environment(args.install_root.resolve(), args.profile, args.port, args.config.resolve())
|
||||
try:
|
||||
asyncio.run(serve_native(args.port, key))
|
||||
except KeyboardInterrupt:
|
||||
print("海诺原生API已停止。")
|
||||
except Exception as error:
|
||||
print(f"原生API启动失败({type(error).__name__});未输出凭据。请使用安装包自带Python,并核对安装根目录与Profile。", file=sys.stderr)
|
||||
raise SystemExit(1) from None
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,42 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
project_dir="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
bundle_dir="$(dirname "$project_dir")"
|
||||
cd "$project_dir"
|
||||
case "$(uname -m)" in
|
||||
aarch64|arm64) ;;
|
||||
*) echo 'This bundle is only for Linux ARM64.'; exit 1 ;;
|
||||
esac
|
||||
docker info >/dev/null
|
||||
docker compose version
|
||||
test -d "$bundle_dir/images" || { echo 'Use the complete offline bundle, including images/.'; exit 1; }
|
||||
test -d wheelhouse || { echo 'Missing ARM64 wheelhouse.'; exit 1; }
|
||||
if [ ! -f .env ] && [ "$#" -ne 1 ]; then
|
||||
echo 'Usage: bash install.sh relay.your-company-domain'
|
||||
exit 1
|
||||
fi
|
||||
if [ -f "$bundle_dir/SHA256SUMS" ]; then
|
||||
(cd "$bundle_dir" && sha256sum -c SHA256SUMS)
|
||||
fi
|
||||
for image_file in "$bundle_dir"/images/*.tar; do
|
||||
docker load -i "$image_file"
|
||||
done
|
||||
# Loaded Python base + local ARM64 wheels only. No apt/pip download or remote build frontend.
|
||||
docker build --network none --pull=false -f Dockerfile.offline -t hainuo-relay:0.1.0 .
|
||||
if [ ! -f .env ]; then
|
||||
docker run --rm --user "$(id -u):$(id -g)" -v "$project_dir:/work" -w /work \
|
||||
hainuo-relay:0.1.0 init --hostname "$1"
|
||||
fi
|
||||
if [ "$(id -u)" -eq 0 ]; then
|
||||
chown -R 10001:10001 runtime/connector
|
||||
chmod 700 runtime/connector
|
||||
chmod 600 runtime/connector/connector.key
|
||||
else
|
||||
sudo chown -R 10001:10001 runtime/connector
|
||||
sudo chmod 700 runtime/connector
|
||||
sudo chmod 600 runtime/connector/connector.key
|
||||
fi
|
||||
docker compose -f compose.yaml -f compose.offline.yaml up -d --no-build --pull never
|
||||
bash scripts/verify_deployment.sh
|
||||
echo 'Offline service installation finished. Configure real TLS/platform settings for acceptance.'
|
||||
echo 'Keep operator/ca.key on the offline signing workstation; do not share .env or private keys.'
|
||||
@@ -0,0 +1,30 @@
|
||||
"""Package only source/deployment/tests; never include keys, .env or runtime data."""
|
||||
import hashlib
|
||||
import json
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
output = root.parent / "海诺中继服务_0.1.0_源码部署包.zip"
|
||||
allowed_dirs = {"relay", "scripts", "tests", "docs", "deploy"}
|
||||
allowed_files = {"README.md", "pyproject.toml", "requirements.lock", "Dockerfile", "Dockerfile.offline", ".dockerignore",
|
||||
".gitignore", "compose.yaml", "compose.acceptance.yaml", "compose.offline.yaml"}
|
||||
items = []
|
||||
for path in root.rglob("*"):
|
||||
if not path.is_file():
|
||||
continue
|
||||
relative = path.relative_to(root)
|
||||
if "__pycache__" in relative.parts or ".pytest_cache" in relative.parts:
|
||||
continue
|
||||
if relative.parts[0] not in allowed_dirs and relative.as_posix() not in allowed_files:
|
||||
continue
|
||||
items.append((relative, path))
|
||||
manifest = {relative.as_posix(): hashlib.sha256(path.read_bytes()).hexdigest() for relative, path in sorted(items)}
|
||||
(root / "SOURCE_MANIFEST.json").write_text(json.dumps(manifest, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
||||
with zipfile.ZipFile(output, "w", zipfile.ZIP_DEFLATED) as archive:
|
||||
for relative, path in sorted(items):
|
||||
archive.write(path, "hainuo-relay/" + relative.as_posix())
|
||||
archive.write(root / "SOURCE_MANIFEST.json", "hainuo-relay/SOURCE_MANIFEST.json")
|
||||
digest = hashlib.sha256(output.read_bytes()).hexdigest()
|
||||
output.with_suffix(".zip.sha256").write_text(digest + "\n", encoding="ascii")
|
||||
print(json.dumps({"archive": str(output), "files": len(items) + 1, "sha256": digest}, ensure_ascii=False))
|
||||
@@ -0,0 +1,86 @@
|
||||
"""Assemble an allowlisted offline delivery, excluding credentials and runtime data."""
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tarfile
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
workspace = root.parent
|
||||
bundle = workspace / "hainuo-offline-arm64"
|
||||
images = bundle / "images"
|
||||
licenses = bundle / "licenses"
|
||||
records = json.loads((images / "IMAGE_MANIFEST.json").read_text(encoding="utf-8"))
|
||||
assert {x["name"] for x in records} == {"python", "postgres", "redis", "caddy"}
|
||||
assert json.loads((images / "VERIFICATION.json").read_text())["status"] == "passed"
|
||||
subprocess.run([sys.executable, str(root / "scripts/package.py")], check=True)
|
||||
included = []
|
||||
with zipfile.ZipFile(workspace / "海诺中继服务_0.1.0_源码部署包.zip") as source:
|
||||
for member in source.infolist():
|
||||
relative = Path(member.filename)
|
||||
assert relative.parts[0] == "hainuo-relay" and ".." not in relative.parts
|
||||
destination = bundle / relative
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
destination.write_bytes(source.read(member))
|
||||
included.append(destination)
|
||||
for path in sorted((workspace / ".arm64-wheelcheck").glob("*.whl")):
|
||||
destination = bundle / "hainuo-relay/wheelhouse" / path.name
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copyfile(path, destination)
|
||||
included.append(destination)
|
||||
for record in records:
|
||||
assert Path(record["archive"]).name == record["archive"]
|
||||
included.append(images / record["archive"])
|
||||
included += [images / "IMAGE_MANIFEST.json", images / "VERIFICATION.json"]
|
||||
caddy = json.loads((licenses / "caddy-license.json").read_text())
|
||||
assert caddy["license"]["spdx_id"] == "Apache-2.0"
|
||||
(licenses / "CADDY-LICENSE.txt").write_bytes(base64.b64decode(caddy["content"]))
|
||||
allowed_licenses = {"redis-licensing.html", "postgresql-license.html", "python-license.html", "COPYING",
|
||||
"LICENSE.txt", "AGPL-3.0.txt", "caddy-license.json", "CADDY-LICENSE.txt"}
|
||||
for path in licenses.iterdir():
|
||||
if path.name in allowed_licenses or (path.name.startswith("redis-8.2.") and path.name.endswith("-source.tar.gz")):
|
||||
included.append(path)
|
||||
wrapper = bundle / "install.sh"
|
||||
wrapper.write_text('#!/usr/bin/env bash\nset -euo pipefail\ncd "$(dirname "$0")"\nexec bash hainuo-relay/scripts/install_offline.sh "$@"\n', encoding="utf-8", newline="\n")
|
||||
included.append(wrapper)
|
||||
readme = bundle / "README.txt"
|
||||
readme.write_text("海诺中继 Linux ARM64 官方镜像离线部署包\n\n"
|
||||
"宿主机需要预装Docker Engine和Compose v2。\n"
|
||||
"在本目录执行:bash install.sh relay.实际公司域名\n"
|
||||
"脚本先校验SHA256,再docker load四个官方镜像,离线构建本项目服务并启动。\n"
|
||||
"安装阶段不访问镜像仓库/PyPI;业务仍需连通海能Work和员工PC。\n"
|
||||
"Python 3.12.15 / PostgreSQL 17.11 / Redis 8.2.10 / Caddy 2.11.7,全部linux/arm64。\n"
|
||||
"Redis选择AGPLv3;其他项目许可和Redis对应版本源码见licenses/。\n"
|
||||
"精确上游摘要与下载来源见images/IMAGE_MANIFEST.json,校验记录见VERIFICATION.json。\n"
|
||||
"本项目服务镜像由源码在目标机本地构建,不是声称来自某官方仓库的海诺镜像。\n"
|
||||
"本地无Docker daemon,已验证镜像内容及摘要,未声称已在目标机运行通过。\n"
|
||||
"完整部署、真实1.065适配边界和TLS配置见hainuo-relay/docs/。\n", encoding="utf-8")
|
||||
included.append(readme)
|
||||
hashes = []
|
||||
for path in sorted(set(included)):
|
||||
value = hashlib.sha256()
|
||||
with path.open("rb") as source:
|
||||
for data in iter(lambda: source.read(4 * 1024 * 1024), b""):
|
||||
value.update(data)
|
||||
hashes.append(value.hexdigest() + " " + path.relative_to(bundle).as_posix())
|
||||
checksum = bundle / "SHA256SUMS"
|
||||
checksum.write_text("\n".join(hashes) + "\n", encoding="utf-8", newline="\n")
|
||||
included.append(checksum)
|
||||
output = workspace / "hainuo-relay-offline-arm64-0.1.0.tar.gz"
|
||||
print("Packaging verified images and offline dependencies...", flush=True)
|
||||
with tarfile.open(output, "w:gz", compresslevel=3) as archive:
|
||||
for path in sorted(set(included)):
|
||||
arcname = "hainuo-offline-arm64/" + path.relative_to(bundle).as_posix()
|
||||
assert "/runtime/" not in arcname and "/operator/" not in arcname and not arcname.endswith("/.env")
|
||||
archive.add(path, arcname=arcname, recursive=False)
|
||||
value = hashlib.sha256()
|
||||
with output.open("rb") as source:
|
||||
for data in iter(lambda: source.read(4 * 1024 * 1024), b""):
|
||||
value.update(data)
|
||||
output.with_suffix(output.suffix + ".sha256").write_text(value.hexdigest() + "\n", encoding="ascii")
|
||||
print(json.dumps({"archive": str(output), "bytes": output.stat().st_size, "sha256": value.hexdigest(),
|
||||
"files": len(set(included))}, ensure_ascii=False))
|
||||
@@ -0,0 +1,25 @@
|
||||
"""Create an optional ARM64 Python dependency bundle, not container base images."""
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--wheel-directory", type=Path, required=True)
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
args = parser.parse_args()
|
||||
wheels = sorted(args.wheel_directory.glob("*.whl"))
|
||||
if not wheels or any("win_" in x.name or "x86_64" in x.name for x in wheels):
|
||||
raise SystemExit("expected only ARM64 or platform-independent wheels")
|
||||
manifest = {p.name: hashlib.sha256(p.read_bytes()).hexdigest() for p in wheels}
|
||||
with zipfile.ZipFile(args.output, "w", zipfile.ZIP_DEFLATED) as archive:
|
||||
for path in wheels:
|
||||
archive.write(path, "wheelhouse/" + path.name)
|
||||
archive.writestr("wheelhouse/SHA256SUMS.json", json.dumps(manifest, indent=2) + "\n")
|
||||
archive.writestr("README.txt", "Linux ARM64, CPython 3.12 dependency wheels.\n"
|
||||
"python -m pip install --no-index --find-links wheelhouse -r hainuo-relay/requirements.lock\n"
|
||||
"Docker base images are NOT included. ARM64 runtime and kernel compatibility still require target verification.\n")
|
||||
digest = hashlib.sha256(args.output.read_bytes()).hexdigest()
|
||||
args.output.with_suffix(".zip.sha256").write_text(digest + "\n", encoding="ascii")
|
||||
print(json.dumps({"archive": str(args.output), "wheels": len(wheels), "sha256": digest}, ensure_ascii=False))
|
||||
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
printf 'Architecture: '; uname -m
|
||||
printf 'Page size: '; getconf PAGESIZE
|
||||
docker version --format '{{.Server.Version}}'
|
||||
docker compose version
|
||||
test -f .env || { echo 'Run init first'; exit 1; }
|
||||
test -f runtime/tls/server.pem
|
||||
test -f runtime/connector/connector.key
|
||||
test -f runtime/trust/ca.pem
|
||||
docker compose config --quiet
|
||||
echo 'Preflight passed. This does not confirm platform/PC connectivity.'
|
||||
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
test -f .env || { echo 'Missing existing .env'; exit 1; }
|
||||
test -d wheelhouse || { echo 'Missing existing wheelhouse'; exit 1; }
|
||||
backup_tag="hainuo-relay:before-feishu-$(date +%Y%m%d-%H%M%S)"
|
||||
docker image tag hainuo-relay:0.1.0 "$backup_tag"
|
||||
echo "Rollback image: $backup_tag"
|
||||
docker build --network none --pull=false -f Dockerfile.offline -t hainuo-relay:0.1.0 .
|
||||
# No App Secret is embedded here, in the image, or in process arguments.
|
||||
docker run --rm -it --user 0:0 --entrypoint python \
|
||||
--mount "type=bind,source=$PWD,target=/work" \
|
||||
hainuo-relay:0.1.0 /work/scripts/configure_feishu_longconn.py --root /work
|
||||
cat > compose.work.yaml <<'YAML'
|
||||
services:
|
||||
connector:
|
||||
environment:
|
||||
HN_FEISHU_EVENT_SOURCE: longconn
|
||||
HN_OAUTH_ENABLED: "false"
|
||||
HN_REQUEST_TIMEOUT: "180"
|
||||
longconn:
|
||||
environment:
|
||||
HN_FEISHU_EVENT_SOURCE: longconn
|
||||
YAML
|
||||
args=(-f compose.yaml -f compose.offline.yaml)
|
||||
for extra in compose.edge-fix.yaml compose.native-api.yaml compose.h5.yaml; do
|
||||
if [ -f "$extra" ]; then args+=(-f "$extra"); fi
|
||||
done
|
||||
args+=(-f compose.work.yaml)
|
||||
docker compose "${args[@]}" --profile longconn config --quiet
|
||||
docker compose "${args[@]}" --profile longconn up -d --no-build --pull never --no-deps --force-recreate connector
|
||||
for attempt in {1..30}; do
|
||||
if docker compose "${args[@]}" exec -T connector python -c \
|
||||
"import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/ready',timeout=2)" 2>/dev/null; then
|
||||
docker compose "${args[@]}" --profile longconn up -d --no-build --pull never --no-deps --force-recreate longconn
|
||||
docker compose "${args[@]}" --profile longconn ps connector longconn
|
||||
echo 'Next: docker compose logs --tail 30 longconn; look for platform_connected.'
|
||||
exit 0
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
echo 'Connector not ready; inspect docker compose logs connector. Do not post .env contents.'
|
||||
exit 1
|
||||
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
bash scripts/preflight.sh
|
||||
# Dedicated container identity; do not change operator/ ownership or mount its CA key.
|
||||
if [ "$(id -u)" -eq 0 ]; then
|
||||
chown -R 10001:10001 runtime/connector
|
||||
chmod 700 runtime/connector
|
||||
chmod 600 runtime/connector/connector.key
|
||||
else
|
||||
sudo chown -R 10001:10001 runtime/connector
|
||||
sudo chmod 700 runtime/connector
|
||||
sudo chmod 600 runtime/connector/connector.key
|
||||
fi
|
||||
docker compose build connector
|
||||
docker compose up -d
|
||||
docker compose ps
|
||||
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
test -f .env || { echo 'Run inside the existing deployment; .env missing.'; exit 1; }
|
||||
test -d wheelhouse || { echo 'Existing offline wheelhouse missing.'; exit 1; }
|
||||
backup_tag="hainuo-relay:before-h5-$(date +%Y%m%d-%H%M%S)"
|
||||
docker image tag hainuo-relay:0.1.0 "$backup_tag"
|
||||
echo "Rollback image: $backup_tag"
|
||||
# Offline dependencies are unchanged; do not pull images or initialize a new database.
|
||||
docker build --network none --pull=false -f Dockerfile.offline -t hainuo-relay:0.1.0 .
|
||||
cat > compose.h5.yaml <<'YAML'
|
||||
services:
|
||||
connector:
|
||||
environment:
|
||||
HN_REQUEST_TIMEOUT: "180"
|
||||
YAML
|
||||
args=(-f compose.yaml -f compose.offline.yaml)
|
||||
if [ -f compose.edge-fix.yaml ]; then args+=(-f compose.edge-fix.yaml); fi
|
||||
args+=(-f compose.h5.yaml)
|
||||
docker compose "${args[@]}" config --quiet
|
||||
docker compose "${args[@]}" up -d --no-build --pull never --no-deps --force-recreate connector
|
||||
for attempt in {1..30}; do
|
||||
if docker compose "${args[@]}" exec -T connector python -c \
|
||||
"import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/ready',timeout=2)" 2>/dev/null; then
|
||||
docker compose "${args[@]}" exec -T connector python -c \
|
||||
"import urllib.request; t=urllib.request.urlopen('http://127.0.0.1:8000/h5').read(); assert b'pair-form' in t; print('H5 pairing/chat revision is ready')"
|
||||
exit 0
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
echo 'Connector not ready. Read: sudo docker compose logs --tail 80 connector'
|
||||
exit 1
|
||||
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
root=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)
|
||||
cd "$root"
|
||||
stamp=$(date +%Y%m%d-%H%M%S)
|
||||
docker image tag hainuo-relay:0.1.0 "hainuo-relay:before-userapi-$stamp"
|
||||
docker build --network none --pull=false -f Dockerfile.offline -t hainuo-relay:0.1.0 .
|
||||
args=(-f compose.yaml)
|
||||
for override in compose.offline.yaml compose.edge-fix.yaml compose.h5.yaml compose.work.yaml compose.feishu.yaml compose.autoreg.yaml; do
|
||||
if [[ -f "$override" ]]; then args+=(-f "$override"); fi
|
||||
done
|
||||
args+=(-f compose.userapi.yaml)
|
||||
docker compose "${args[@]}" up -d --no-build --pull never --no-deps --force-recreate connector router
|
||||
if [[ -n "$(docker compose -f compose.yaml ps -q longconn)" ]]; then
|
||||
docker compose "${args[@]}" --profile longconn up -d --no-build --pull never --no-deps --force-recreate longconn
|
||||
fi
|
||||
docker compose "${args[@]}" ps
|
||||
echo "Previous image: hainuo-relay:before-userapi-$stamp"
|
||||
echo 'Keep compose.autoreg.yaml and compose.userapi.yaml in future application recreations.'
|
||||
echo 'Configure platform callback/scopes, then each user completes personal authorization.'
|
||||
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
root=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)
|
||||
cd "$root"
|
||||
test -f .env && test -d wheelhouse && test -f Dockerfile.offline
|
||||
stamp=$(date +%Y%m%d-%H%M%S)
|
||||
# Enable automatic issuance using the current test CA, never generate a new CA.
|
||||
# Only the internal issuer container receives this key; no host port is published.
|
||||
if [[ ! -f runtime/issuer/ca.key ]]; then
|
||||
test -f operator/ca.key || { echo 'Existing CA key missing: provide the original CA to the internal issuer first.' >&2; exit 1; }
|
||||
install -d -m 700 -o 10001 -g 10001 runtime/issuer
|
||||
install -m 400 -o 10001 -g 10001 operator/ca.key runtime/issuer/ca.key
|
||||
fi
|
||||
docker image tag hainuo-relay:0.1.0 "hainuo-relay:before-work1067-$stamp"
|
||||
docker build --network none --pull=false -f Dockerfile.offline -t hainuo-relay:0.1.0 .
|
||||
args=(-f compose.yaml)
|
||||
for override in compose.offline.yaml compose.edge-fix.yaml compose.h5.yaml compose.work.yaml compose.feishu.yaml; do
|
||||
if [[ -f "$override" ]]; then args+=(-f "$override"); fi
|
||||
done
|
||||
args+=(-f compose.autoreg.yaml)
|
||||
# Bring up signer and verify it before changing the serving connector.
|
||||
docker compose "${args[@]}" up -d --no-build --pull never --no-deps --force-recreate enrollment-signer
|
||||
docker compose "${args[@]}" exec -T enrollment-signer python -c 'import time,urllib.request
|
||||
for attempt in range(30):
|
||||
try:
|
||||
urllib.request.urlopen("http://127.0.0.1:8000/ready", timeout=2)
|
||||
print("Automatic certificate issuer ready")
|
||||
break
|
||||
except Exception:
|
||||
time.sleep(1)
|
||||
else:
|
||||
raise SystemExit("Issuer not ready; connector unchanged")'
|
||||
docker compose "${args[@]}" up -d --no-build --pull never --no-deps --force-recreate connector router
|
||||
if [[ -n "$(docker compose -f compose.yaml ps -q longconn)" ]]; then
|
||||
docker compose "${args[@]}" --profile longconn up -d --no-build --pull never --no-deps --force-recreate longconn
|
||||
fi
|
||||
docker compose "${args[@]}" ps
|
||||
echo "Previous image: hainuo-relay:before-work1067-$stamp"
|
||||
echo 'Automatic registration enabled. New PCs only need a Work binding code; no per-PC approval command.'
|
||||
echo 'Keep -f compose.autoreg.yaml in future compose up commands.'
|
||||
@@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose exec -T connector python -c \
|
||||
"import urllib.request; import json; print(json.load(urllib.request.urlopen('http://127.0.0.1:8000/ready')))"
|
||||
docker compose exec -T router python -c \
|
||||
"import urllib.request; import json; print(json.load(urllib.request.urlopen('http://127.0.0.1:8000/ready')))"
|
||||
docker compose exec -T connector python -m relay.cli status --url http://127.0.0.1:8000
|
||||
echo 'Services and metadata query passed. Still run the two-PC acceptance procedure.'
|
||||
@@ -0,0 +1,47 @@
|
||||
"""Verify the Docker archive identity and each layer's uncompressed rootfs digest."""
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import tarfile
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def stream_sha(source):
|
||||
value = hashlib.sha256()
|
||||
for chunk in iter(lambda: source.read(4 * 1024 * 1024), b""):
|
||||
value.update(chunk)
|
||||
return value.hexdigest()
|
||||
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser()
|
||||
p.add_argument("directory", type=Path)
|
||||
args = p.parse_args()
|
||||
records = json.loads((args.directory / "IMAGE_MANIFEST.json").read_text(encoding="utf-8"))
|
||||
output = []
|
||||
for record in records:
|
||||
path = args.directory / record["archive"]
|
||||
with path.open("rb") as source:
|
||||
assert stream_sha(source) == record["archive_sha256"], "archive checksum mismatch"
|
||||
with tarfile.open(path) as archive:
|
||||
manifest = json.load(archive.extractfile("manifest.json"))[0]
|
||||
config_bytes = archive.extractfile(manifest["Config"]).read()
|
||||
assert "sha256:" + hashlib.sha256(config_bytes).hexdigest() == record["config"]["digest"]
|
||||
cfg = json.loads(config_bytes)
|
||||
assert cfg["architecture"] == "arm64" and cfg["os"] == "linux"
|
||||
assert manifest["RepoTags"] == [record["name"] + ":" + record["tag"]]
|
||||
assert len(manifest["Layers"]) == len(cfg["rootfs"]["diff_ids"])
|
||||
for name, expected in zip(manifest["Layers"], cfg["rootfs"]["diff_ids"]):
|
||||
assert "sha256:" + stream_sha(archive.extractfile(name)) == expected, "rootfs checksum mismatch"
|
||||
versions = {x.split("=", 1)[0]: x.split("=", 1)[1] for x in cfg.get("config", {}).get("Env", [])
|
||||
if x.startswith(("PYTHON_VERSION=", "REDIS_VERSION=", "PG_VERSION=", "CADDY_VERSION="))}
|
||||
output.append({"image": manifest["RepoTags"][0], "platform": "linux/arm64", "versions": versions,
|
||||
"layers_verified": len(manifest["Layers"]), "archive_bytes": path.stat().st_size})
|
||||
report = {"status": "passed", "checks": "archive/config/rootfs SHA256 + linux/arm64 + tags", "images": output,
|
||||
"runtime_test": "not run; local Docker daemon unavailable"}
|
||||
(args.directory / "VERIFICATION.json").write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8")
|
||||
print(json.dumps(report, indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,116 @@
|
||||
import asyncio
|
||||
import contextlib
|
||||
import json
|
||||
import socket
|
||||
|
||||
import fakeredis.aioredis
|
||||
import httpx
|
||||
import pytest_asyncio
|
||||
import uvicorn
|
||||
|
||||
from relay.cli import device_request, init_project, issue
|
||||
from relay.config import Settings
|
||||
from relay.connector import create_app as connector_app
|
||||
from relay.router import create_app as router_app
|
||||
|
||||
|
||||
class PlatformStub:
|
||||
def __init__(self):
|
||||
self.replies = []
|
||||
self.exchanges = 0
|
||||
self.refreshes = 0
|
||||
|
||||
async def exchange(self, code):
|
||||
self.exchanges += 1
|
||||
await asyncio.sleep(0.01)
|
||||
return {"user_access_token": "fake-user-token", "refresh_token": "fake-refresh-token",
|
||||
"expires_in": 6900, "refresh_expires_in": 2592000, "open_id": "ou_test"}
|
||||
|
||||
async def refresh(self, token):
|
||||
self.refreshes += 1
|
||||
await asyncio.sleep(0.01)
|
||||
return {"user_access_token": "new-fake-token", "refresh_token": "rotated-fake-token",
|
||||
"expires_in": 7200, "open_id": "ou_test"}
|
||||
|
||||
async def user_info(self, token):
|
||||
return {"open_id": "ou_test"}
|
||||
|
||||
async def reply(self, message, text):
|
||||
self.replies.append((message, text))
|
||||
|
||||
async def close(self):
|
||||
pass
|
||||
|
||||
|
||||
async def serve(app):
|
||||
sock = socket.socket()
|
||||
sock.bind(("127.0.0.1", 0))
|
||||
sock.listen(128)
|
||||
port = sock.getsockname()[1]
|
||||
server = uvicorn.Server(uvicorn.Config(app, log_level="critical", access_log=False, lifespan="on"))
|
||||
task = asyncio.create_task(server.serve(sockets=[sock]))
|
||||
for _ in range(200):
|
||||
if server.started:
|
||||
return server, task, port
|
||||
if task.done():
|
||||
task.result()
|
||||
raise RuntimeError("server failed")
|
||||
await asyncio.sleep(0.01)
|
||||
raise RuntimeError("server startup timeout")
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def env(tmp_path):
|
||||
init_project(tmp_path, "localhost")
|
||||
settings = Settings(environment="test", admin_secret="a" * 40, internal_secret="b" * 40,
|
||||
data_dir=tmp_path / "runtime/connector", ca_cert=tmp_path / "runtime/trust/ca.pem",
|
||||
connector_key=tmp_path / "runtime/connector/connector.key",
|
||||
connector_public_key=tmp_path / "runtime/trust/connector.pub",
|
||||
feishu_enabled=True, feishu_app_id="cli_test", feishu_app_secret="fake-app-secret",
|
||||
feishu_encrypt_key="fake-encrypt-key", feishu_verification_token="fake-verification",
|
||||
public_url="http://localhost", router_url="ws://localhost/device/ws", request_timeout=2)
|
||||
redis = fakeredis.aioredis.FakeRedis(decode_responses=True)
|
||||
platform = PlatformStub()
|
||||
connector = connector_app(settings, redis=redis, platform=platform)
|
||||
router = router_app(settings, redis=redis)
|
||||
cs, ct, cp = await serve(connector)
|
||||
rs, rt, rp = await serve(router)
|
||||
client = httpx.AsyncClient(base_url=f"http://127.0.0.1:{cp}", timeout=10)
|
||||
state = {"settings": settings, "redis": redis, "platform": platform, "connector": connector,
|
||||
"router": router, "client": client, "root": tmp_path,
|
||||
"connector_url": f"http://127.0.0.1:{cp}", "router_url": f"ws://127.0.0.1:{rp}/device/ws",
|
||||
"admin": {"X-Admin-Secret": settings.admin_secret}, "tasks": []}
|
||||
yield state
|
||||
for task in state["tasks"]:
|
||||
task.cancel()
|
||||
await asyncio.gather(*state["tasks"], return_exceptions=True)
|
||||
await client.aclose()
|
||||
cs.should_exit = rs.should_exit = True
|
||||
await asyncio.wait_for(asyncio.gather(ct, rt), timeout=10)
|
||||
await redis.aclose()
|
||||
|
||||
|
||||
async def provision(env, owner="ou_test"):
|
||||
directory = env["root"] / owner
|
||||
device_request(directory)
|
||||
output = env["root"] / (owner + "-issued")
|
||||
issue(env["root"], directory / "request.json", output)
|
||||
for name in ["device.pem", "ca.pem", "connector.pub", "device.json"]:
|
||||
(directory / name).write_bytes((output / name).read_bytes())
|
||||
registration = json.loads((output / "registration.json").read_text())
|
||||
response = await env["client"].post("/admin/devices", json=registration, headers=env["admin"])
|
||||
assert response.status_code == 201, response.text
|
||||
response = await env["client"].post("/admin/test/binding-code", json={"open_id": owner, "text": "ignored"}, headers=env["admin"])
|
||||
code = response.json()["message"].split(":")[-1]
|
||||
from relay.device import bind_device
|
||||
await bind_device(directory, env["connector_url"], code)
|
||||
return directory, registration["host_id"]
|
||||
|
||||
|
||||
async def connect_device(env, directory, root=None):
|
||||
from relay.device import run_device
|
||||
connected = asyncio.Event()
|
||||
task = asyncio.create_task(run_device(directory, env["router_url"], root=root, connected_event=connected))
|
||||
env["tasks"].append(task)
|
||||
await asyncio.wait_for(connected.wait(), 5)
|
||||
return task
|
||||
@@ -0,0 +1,24 @@
|
||||
import json
|
||||
import httpx
|
||||
import pytest
|
||||
from relay.device import LocalAgent
|
||||
|
||||
|
||||
async def test_local_agent_contract_and_no_redirect():
|
||||
requests = []
|
||||
def handler(request):
|
||||
requests.append(json.loads(request.content))
|
||||
return httpx.Response(200, json={"result": "本机助手结果"})
|
||||
agent = LocalAgent("http://127.0.0.1:8765/relay/read-only", transport=httpx.MockTransport(handler))
|
||||
assert await agent.execute("整理测试文件", "ou_test", "turn") == "本机助手结果"
|
||||
assert requests[0] == {"text": "整理测试文件", "open_id": "ou_test", "turn_id": "turn", "channel": "im", "read_only": True}
|
||||
await agent.close()
|
||||
agent = LocalAgent("http://127.0.0.1:8765/relay/read-only", transport=httpx.MockTransport(
|
||||
lambda request: httpx.Response(302, headers={"Location": "https://evil.invalid"})))
|
||||
assert "未返回成功" in await agent.execute("text", "owner", "turn")
|
||||
await agent.close()
|
||||
|
||||
|
||||
def test_agent_rejects_nonlocal_endpoint():
|
||||
with pytest.raises(ValueError):
|
||||
LocalAgent("https://external.example.invalid/run")
|
||||
@@ -0,0 +1,141 @@
|
||||
import asyncio
|
||||
import hashlib
|
||||
import json
|
||||
import secrets
|
||||
import time
|
||||
|
||||
import pytest
|
||||
from cryptography.hazmat.primitives import padding
|
||||
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
||||
from websockets.asyncio.client import connect
|
||||
|
||||
from relay.crypto import load_private, sign
|
||||
from relay.device import DeviceJournal
|
||||
from relay.config import Settings
|
||||
from .conftest import provision, connect_device
|
||||
from .test_integration import dispatch, event
|
||||
|
||||
|
||||
async def test_expired_and_capacity_oauth(env):
|
||||
client = env["client"]
|
||||
data = (await client.post("/oauth/start", json={})).json()
|
||||
env["connector"].state.oauth.pending[data["state"]].expires = time.monotonic() - 1
|
||||
assert (await client.get("/oauth/cb", params={"state": data["state"], "code": "x"})).status_code == 400
|
||||
env["settings"].max_pending = 1
|
||||
assert (await client.post("/oauth/start", json={})).status_code == 201
|
||||
assert (await client.post("/oauth/start", json={})).status_code == 503
|
||||
|
||||
|
||||
async def test_binding_wrong_signature_does_not_consume(env):
|
||||
# Provision one device but remove initial binding to exercise a second code.
|
||||
directory, host = await provision(env)
|
||||
env["connector"].state.store.unbind("ou_test")
|
||||
response = await env["client"].post("/admin/test/binding-code", headers=env["admin"],
|
||||
json={"open_id": "ou_test", "text": "ignored"})
|
||||
code = response.json()["message"].split(":")[-1]
|
||||
proof = sign({"host_id": host, "code": code, "ts": int(time.time())}, load_private(directory / "device.key"))
|
||||
damaged = {**proof, "signature": "AAAA"}
|
||||
assert (await env["client"].post("/device/bind", json={"host_id": host, "proof": damaged})).status_code == 403
|
||||
assert (await env["client"].post("/device/bind", json={"host_id": host, "proof": proof})).status_code == 200
|
||||
assert (await env["client"].post("/device/bind", json={"host_id": host, "proof": proof})).status_code == 404
|
||||
|
||||
|
||||
async def test_timeout_is_unknown_and_cannot_reexecute(env):
|
||||
directory, host = await provision(env)
|
||||
# A live session is required: pause reply processing to reproduce an ambiguous result.
|
||||
await connect_device(env, directory)
|
||||
env["connector"].state.reply_task.cancel()
|
||||
await asyncio.sleep(0)
|
||||
result = await dispatch(env, "ou_test", "/回显 delayed", "timeout-turn")
|
||||
assert result.json()["status"] == "unknown"
|
||||
assert (await dispatch(env, "ou_test", "/回显 retried", "timeout-turn")).json()["status"] == "duplicate"
|
||||
assert (await env["client"].get("/ready")).status_code == 503
|
||||
|
||||
|
||||
async def test_duplicate_device_connection_rejected(env):
|
||||
from relay.crypto import raw_public
|
||||
from cryptography.hazmat.primitives.asymmetric import x25519
|
||||
directory, host = await provision(env)
|
||||
await connect_device(env, directory)
|
||||
async with connect(env["router_url"], proxy=None) as ws:
|
||||
challenge = json.loads(await ws.recv())["challenge"]
|
||||
proof = sign({"host_id": host, "session_id": secrets.token_hex(16), "challenge": challenge,
|
||||
"kx": raw_public(x25519.X25519PrivateKey.generate().public_key()), "ts": int(time.time())},
|
||||
load_private(directory / "device.key"))
|
||||
await ws.send(json.dumps({"host_id": host, "cert": (directory / "device.pem").read_text(), "proof": proof}))
|
||||
from websockets.exceptions import ConnectionClosed
|
||||
with pytest.raises(ConnectionClosed):
|
||||
await ws.recv()
|
||||
assert (await dispatch(env, "ou_test", "/回显 original")).json()["result"] == "original"
|
||||
|
||||
|
||||
async def test_encrypted_webhook_and_stale_signature(env):
|
||||
directory, _ = await provision(env)
|
||||
await connect_device(env, directory)
|
||||
s = env["settings"]
|
||||
raw_event = json.dumps(event(env, "om_encrypted"), ensure_ascii=False).encode()
|
||||
pad = padding.PKCS7(128).padder()
|
||||
padded = pad.update(raw_event) + pad.finalize()
|
||||
iv = secrets.token_bytes(16)
|
||||
enc = Cipher(algorithms.AES(hashlib.sha256(s.feishu_encrypt_key.encode()).digest()), modes.CBC(iv)).encryptor()
|
||||
import base64
|
||||
raw = json.dumps({"encrypt": base64.b64encode(iv + enc.update(padded) + enc.finalize()).decode()}).encode()
|
||||
timestamp, nonce = str(int(time.time())), "encrypted-nonce"
|
||||
headers = {"X-Lark-Request-Timestamp": timestamp, "X-Lark-Request-Nonce": nonce,
|
||||
"X-Lark-Signature": hashlib.sha256((timestamp + nonce + s.feishu_encrypt_key).encode() + raw).hexdigest()}
|
||||
assert (await env["client"].post("/feishu/event", content=raw, headers=headers)).status_code == 200
|
||||
for _ in range(100):
|
||||
if env["platform"].replies:
|
||||
break
|
||||
await asyncio.sleep(0.01)
|
||||
assert env["platform"].replies == [("om_encrypted", "平台事件")]
|
||||
headers["X-Lark-Request-Timestamp"] = str(int(time.time()) - 600)
|
||||
assert (await env["client"].post("/feishu/event", content=raw, headers=headers)).status_code == 403
|
||||
|
||||
|
||||
async def test_h5_and_whoami_only_returns_owned_static_domain(env):
|
||||
await provision(env)
|
||||
env["settings"].device_domain = "pc.example.invalid"
|
||||
response = await env["client"].get("/h5")
|
||||
assert response.status_code == 200
|
||||
assert "script-src 'self'" in response.headers["content-security-policy"]
|
||||
assert (await env["client"].get("/assets/app.js")).status_code == 200
|
||||
assert (await env["client"].get("/api/whoami")).status_code == 401
|
||||
response = await env["client"].get("/api/whoami?target=https://evil.invalid", headers={"Authorization": "Bearer fake-user"})
|
||||
assert response.status_code == 200
|
||||
assert response.json()["url"].endswith(".pc.example.invalid/")
|
||||
assert response.json()["device_login_required"] is True
|
||||
|
||||
|
||||
async def test_proxy_blocks_auth_endpoints_absolute_urls_and_traversal(env):
|
||||
env["settings"].proxy_enabled = True
|
||||
for target in ["https://evil.invalid/", "//evil.invalid/", "/open-apis/auth/v3/tenant_access_token/internal",
|
||||
"/open-apis/authen/v1/user_info", "/open-apis/drive/v1/files/%2e%2e/auth", "/open-apis/drive/v1/files%0d%0aX:evil"]:
|
||||
result = await env["client"].get("/proxy", headers={"Authorization": "Bearer fake-user", "X-Relay-Target": target})
|
||||
assert result.status_code == 403, target
|
||||
|
||||
|
||||
def test_device_journal_survives_restart(tmp_path):
|
||||
path = tmp_path / "journal.sqlite"
|
||||
journal = DeviceJournal(path)
|
||||
assert journal.consume("turn")
|
||||
journal.close()
|
||||
journal = DeviceJournal(path)
|
||||
assert not journal.consume("turn")
|
||||
journal.close()
|
||||
|
||||
|
||||
def test_production_fails_closed():
|
||||
with pytest.raises(ValueError):
|
||||
Settings(environment="production", admin_secret="short", internal_secret="short")
|
||||
with pytest.raises(ValueError):
|
||||
Settings(environment="production", admin_secret="a" * 40, internal_secret="b" * 40,
|
||||
public_url="http://localhost")
|
||||
|
||||
|
||||
async def test_validation_errors_do_not_echo_tokens(env):
|
||||
token = "fake-confidential-token-" * 300
|
||||
response = await env["client"].post("/oauth/refresh", json={"refresh_token": token})
|
||||
assert response.status_code == 422
|
||||
assert "fake-confidential" not in response.text
|
||||
assert response.json() == {"detail": "invalid request parameters"}
|
||||
@@ -0,0 +1,38 @@
|
||||
import copy
|
||||
import time
|
||||
|
||||
import pytest
|
||||
from cryptography.exceptions import InvalidSignature, InvalidTag
|
||||
from cryptography.hazmat.primitives.asymmetric import ed25519, x25519
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from relay.crypto import (issue_ca, issue_device, raw_public, seal, open_envelope, verify_device)
|
||||
|
||||
|
||||
def test_encrypted_and_authenticated():
|
||||
signer = ed25519.Ed25519PrivateKey.generate()
|
||||
recipient = x25519.X25519PrivateKey.generate()
|
||||
envelope = seal({"text": "秘密合同"}, raw_public(recipient.public_key()), signer, host_id="host", session_id="session")
|
||||
assert "秘密" not in str(envelope)
|
||||
assert open_envelope(envelope, recipient, signer.public_key(), host_id="host", session_id="session")["text"] == "秘密合同"
|
||||
damaged = copy.deepcopy(envelope)
|
||||
damaged["ciphertext"] = damaged["ciphertext"][:-4] + "AAAA"
|
||||
with pytest.raises(InvalidSignature):
|
||||
open_envelope(damaged, recipient, signer.public_key(), host_id="host", session_id="session")
|
||||
with pytest.raises(ValueError):
|
||||
open_envelope(envelope, recipient, signer.public_key(), host_id="other", session_id="session")
|
||||
# Long-term signing key alone cannot recover a recorded session's data.
|
||||
with pytest.raises(InvalidTag):
|
||||
open_envelope(envelope, x25519.X25519PrivateKey.generate(), signer.public_key(), host_id="host", session_id="session")
|
||||
|
||||
|
||||
def test_certificate_identity_and_trust():
|
||||
ca_key, ca = issue_ca()
|
||||
device = ed25519.Ed25519PrivateKey.generate()
|
||||
pem = issue_device(ca_key, ca, "a" * 32, device.public_key()).public_bytes(serialization.Encoding.PEM).decode()
|
||||
ca_pem = ca.public_bytes(serialization.Encoding.PEM)
|
||||
verify_device(pem, ca_pem, "a" * 32)
|
||||
with pytest.raises(ValueError):
|
||||
verify_device(pem, ca_pem, "b" * 32)
|
||||
_, other_ca = issue_ca()
|
||||
with pytest.raises((ValueError, InvalidSignature)):
|
||||
verify_device(pem, other_ca.public_bytes(serialization.Encoding.PEM), "a" * 32)
|
||||
@@ -0,0 +1,85 @@
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from relay.config import Settings
|
||||
from .conftest import provision
|
||||
|
||||
|
||||
def setup_module_source():
|
||||
path = Path(__file__).resolve().parents[1] / "scripts/configure_feishu_longconn.py"
|
||||
spec = importlib.util.spec_from_file_location("feishu_setup_test", path)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
def test_longconn_does_not_require_webhook_secrets():
|
||||
settings = Settings(environment="test", admin_secret="a" * 40, internal_secret="b" * 40,
|
||||
feishu_enabled=True, feishu_app_id="cli_test", feishu_app_secret="fake-secret")
|
||||
assert settings.feishu_event_source == "longconn"
|
||||
with pytest.raises(ValueError):
|
||||
Settings(environment="test", admin_secret="a" * 40, internal_secret="b" * 40,
|
||||
feishu_enabled=True, feishu_app_id="cli_test", feishu_app_secret="fake-secret", feishu_event_source="webhook")
|
||||
|
||||
|
||||
def test_setup_checks_private_origin_and_never_returns_tokens():
|
||||
module = setup_module_source()
|
||||
seen = []
|
||||
def handler(request):
|
||||
seen.append(request)
|
||||
assert request.url.host == "open.w.cnooc.com.cn"
|
||||
if request.url.path.endswith("app_access_token/internal"):
|
||||
return httpx.Response(200, json={"code": 0, "app_access_token": "fake-sensitive-app-token"})
|
||||
if request.url.path.endswith("tenant_access_token/internal"):
|
||||
return httpx.Response(200, json={"code": 0, "tenant_access_token": "fake-sensitive-tenant-token"})
|
||||
return httpx.Response(200, json={"code": 0, "data": {"URL": "wss://ws.private.invalid/path?ticket=fake-sensitive-ticket"}})
|
||||
with httpx.Client(transport=httpx.MockTransport(handler), follow_redirects=False) as client:
|
||||
result = module.check_platform("https://open.w.cnooc.com.cn", "cli_test", "fake-secret", client)
|
||||
assert len(seen) == 3
|
||||
assert "fake-sensitive" not in str(result)
|
||||
assert "ws.private.invalid" in str(result)
|
||||
|
||||
|
||||
def test_setup_refuses_redirect_and_suppresses_response_body():
|
||||
module = setup_module_source()
|
||||
with httpx.Client(transport=httpx.MockTransport(lambda request: httpx.Response(302, headers={"Location": "https://evil.invalid"}, text="fake-secret")), follow_redirects=False) as client:
|
||||
with pytest.raises(RuntimeError) as error:
|
||||
module.check_platform("https://open.w.cnooc.com.cn", "cli_test", "fake-secret", client)
|
||||
assert "302" in str(error.value) and "fake-secret" not in str(error.value)
|
||||
|
||||
|
||||
def test_env_update_preserves_other_settings_and_backs_up(tmp_path):
|
||||
module = setup_module_source()
|
||||
path = tmp_path / ".env"
|
||||
path.write_text("# existing\nHN_ADMIN_SECRET=fake-admin\nHN_FEISHU_ENABLED=false\nHN_FEISHU_ENABLED=false\nHN_PUBLIC_URL=https://10.77.76.13\n")
|
||||
module.update_env(path, {"HN_FEISHU_ENABLED": "true", "HN_FEISHU_APP_SECRET": "fake-platform-secret", "HN_OAUTH_ENABLED": "false"})
|
||||
value = path.read_text()
|
||||
assert "HN_ADMIN_SECRET=fake-admin" in value and "HN_PUBLIC_URL=https://10.77.76.13" in value
|
||||
assert value.count("HN_FEISHU_ENABLED=") == 1
|
||||
assert "HN_FEISHU_APP_SECRET=fake-platform-secret" in value
|
||||
assert len(list(tmp_path.glob(".env.before-feishu-*"))) == 1
|
||||
|
||||
|
||||
async def test_admin_can_unbind_test_identity_without_revoking_device(env):
|
||||
directory, host = await provision(env)
|
||||
result = await env["client"].post(f"/admin/devices/{host}/unbind", headers=env["admin"])
|
||||
assert result.status_code == 200
|
||||
device = env["connector"].state.store.device(host)
|
||||
assert device["owner"] is None and device["revoked"] == 0
|
||||
assert await env["redis"].get("hn:web-generation:" + host) == "2"
|
||||
from relay.device import bind_device
|
||||
code_result = await env["client"].post("/admin/test/binding-code", headers=env["admin"], json={"open_id": "ou_real", "text": "ignored"})
|
||||
code = code_result.json()["message"].split(":")[-1]
|
||||
await bind_device(directory, env["connector_url"], code)
|
||||
assert env["connector"].state.store.device(host)["owner"] == "ou_real"
|
||||
|
||||
|
||||
async def test_unconfigured_webhook_and_unverified_oauth_are_closed(env):
|
||||
env["settings"].feishu_encrypt_key = ""
|
||||
env["settings"].feishu_verification_token = ""
|
||||
env["settings"].oauth_enabled = False
|
||||
assert (await env["client"].post("/feishu/event", json={})).status_code == 503
|
||||
assert (await env["client"].post("/oauth/start", json={})).status_code == 503
|
||||
@@ -0,0 +1,150 @@
|
||||
import asyncio
|
||||
import importlib.util
|
||||
import json
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from relay.hainuo1065 import Hainuo1065Agent, NativeAPIError, read_native_config
|
||||
from .conftest import provision
|
||||
from .test_integration import dispatch
|
||||
|
||||
|
||||
POLICY = {"mode": "text-only", "tools": "disabled", "contract": "hainuo-1065-v1"}
|
||||
|
||||
|
||||
def config(tmp_path, url="http://127.0.0.1:8643"):
|
||||
path = tmp_path / "native.json"
|
||||
path.write_text(json.dumps({"url": url, "api_key": "fake-local-key-" * 4, "mode": "text-only"}))
|
||||
return path
|
||||
|
||||
|
||||
def native_handler(seen, *, policy=POLICY, completion=None):
|
||||
def handler(request):
|
||||
seen.append(request)
|
||||
assert request.headers["Authorization"].startswith("Bearer fake-local-key-")
|
||||
if request.url.path == "/health":
|
||||
return httpx.Response(200, json={"status": "ok", "platform": "hermes-agent", "version": "package-fixture"})
|
||||
if request.url.path == "/v1/capabilities":
|
||||
return httpx.Response(200, json={"hainuo_relay_policy": policy})
|
||||
if request.url.path == "/v1/models":
|
||||
return httpx.Response(200, json={"data": [{"id": "hainuo-relay-text"}]})
|
||||
assert request.url.path == "/v1/chat/completions"
|
||||
return httpx.Response(200, json=completion or {"choices": [{"message": {"role": "assistant", "content": "真实API格式结果"}, "finish_reason": "stop"}]})
|
||||
return handler
|
||||
|
||||
|
||||
async def test_native_api_request_fields_idempotency_and_result(tmp_path):
|
||||
seen = []
|
||||
adapter = Hainuo1065Agent(config(tmp_path), transport=httpx.MockTransport(native_handler(seen)))
|
||||
assert await adapter.execute("只回复联调通过", "ou_a", "turn_1") == "真实API格式结果"
|
||||
post = seen[-1]
|
||||
assert json.loads(post.content) == {"model": "hainuo-relay-text", "stream": False,
|
||||
"messages": [{"role": "user", "content": "只回复联调通过"}]}
|
||||
assert post.headers["Idempotency-Key"].startswith("hn-")
|
||||
idem = post.headers["Idempotency-Key"]
|
||||
await adapter.execute("只回复联调通过", "ou_a", "turn_1")
|
||||
assert seen[-1].headers["Idempotency-Key"] == idem
|
||||
await adapter.execute("只回复联调通过", "ou_b", "turn_1")
|
||||
assert seen[-1].headers["Idempotency-Key"] != idem
|
||||
await adapter.close()
|
||||
|
||||
|
||||
async def test_refuses_stock_unrestricted_api(tmp_path):
|
||||
seen = []
|
||||
adapter = Hainuo1065Agent(config(tmp_path), transport=httpx.MockTransport(native_handler(seen, policy={})))
|
||||
result = await adapter.execute("anything", "owner", "turn")
|
||||
assert "policy missing" in result
|
||||
assert all(request.method != "POST" for request in seen)
|
||||
await adapter.close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("completion", [
|
||||
{"choices": []},
|
||||
{"choices": [{"message": {"content": "partial"}, "finish_reason": "length"}]},
|
||||
{"choices": [{"message": {"content": "tool", "tool_calls": [{"id": "x"}]}, "finish_reason": "stop"}]},
|
||||
])
|
||||
async def test_incomplete_native_results_fail_closed(tmp_path, completion):
|
||||
adapter = Hainuo1065Agent(config(tmp_path), transport=httpx.MockTransport(native_handler([], completion=completion)))
|
||||
assert "未完成请求" in await adapter.execute("anything", "owner", "turn")
|
||||
await adapter.close()
|
||||
|
||||
|
||||
async def test_native_redirect_no_retry_and_no_error_body_leak(tmp_path):
|
||||
seen = []
|
||||
def handler(request):
|
||||
seen.append(request)
|
||||
return httpx.Response(302, headers={"Location": "https://outside.invalid/"}, text="fake-confidential-body")
|
||||
adapter = Hainuo1065Agent(config(tmp_path), transport=httpx.MockTransport(handler))
|
||||
result = await adapter.execute("anything", "owner", "turn")
|
||||
assert "fake-confidential" not in result and "HTTP 302" in result
|
||||
assert len(seen) == 1
|
||||
await adapter.close()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("url", ["http://example.invalid:8643", "http://localhost:8643", "https://127.0.0.1:8643", "http://127.0.0.1:8643/p/other", "http://user:pass@127.0.0.1:8643"])
|
||||
def test_native_target_cannot_be_remote_or_profile_switched(tmp_path, url):
|
||||
with pytest.raises(ValueError):
|
||||
read_native_config(config(tmp_path, url))
|
||||
|
||||
|
||||
def test_native_launcher_enforces_no_tools_and_endpoint_whitelist():
|
||||
script = Path(__file__).resolve().parents[1] / "scripts/hainuo_native_api.py"
|
||||
spec = importlib.util.spec_from_file_location("native_launcher_test", script)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
class Native:
|
||||
def _create_agent(self):
|
||||
return SimpleNamespace(tools=[{"name": "terminal"}], valid_tool_names={"terminal"})
|
||||
def _http_route_table(self):
|
||||
return [("GET", "/health", None), ("POST", "/v1/chat/completions", None),
|
||||
("DELETE", "/api/sessions/x", None), ("POST", "/api/jobs", None)]
|
||||
tools = SimpleNamespace(_get_platform_tools=lambda config, platform, **kwargs: {"terminal"})
|
||||
Restricted = module.restricted_adapter_class(Native, tools)
|
||||
assert tools._get_platform_tools({}, "api_server") == set()
|
||||
assert tools._get_platform_tools({}, "feishu") == {"terminal"}
|
||||
assert len(Restricted()._http_route_table()) == 2
|
||||
assert Restricted()._wire_plugin_handlers(object()) is None
|
||||
with pytest.raises(RuntimeError, match="refuses an Agent with tools"):
|
||||
Restricted()._create_agent()
|
||||
|
||||
|
||||
async def test_encrypted_ecs_device_native_http_and_return(env):
|
||||
"""Real socket chain; native API is a contract fixture, not installed Hermes."""
|
||||
from fastapi import FastAPI, Request
|
||||
from .conftest import serve
|
||||
from relay.device import run_device
|
||||
app = FastAPI()
|
||||
seen = []
|
||||
@app.get("/health")
|
||||
async def health():
|
||||
return {"status": "ok", "platform": "hermes-agent", "version": "contract-fixture"}
|
||||
@app.get("/v1/capabilities")
|
||||
async def capabilities():
|
||||
return {"hainuo_relay_policy": POLICY}
|
||||
@app.get("/v1/models")
|
||||
async def models():
|
||||
return {"data": [{"id": "hainuo-relay-text"}]}
|
||||
@app.post("/v1/chat/completions")
|
||||
async def chat(request: Request):
|
||||
seen.append(await request.json())
|
||||
assert request.headers.get("Idempotency-Key")
|
||||
return {"choices": [{"message": {"content": "本机API对接成功"}, "finish_reason": "stop"}]}
|
||||
server, server_task, port = await serve(app)
|
||||
directory, _ = await provision(env)
|
||||
cfg = config(env["root"], f"http://127.0.0.1:{port}")
|
||||
connected = asyncio.Event()
|
||||
device = asyncio.create_task(run_device(directory, env["router_url"], connected_event=connected, hainuo_config=cfg))
|
||||
env["tasks"].append(device)
|
||||
try:
|
||||
await asyncio.wait_for(connected.wait(), 5)
|
||||
response = await dispatch(env, "ou_test", "/助手 只回复本机API对接成功", "native-turn")
|
||||
assert response.json()["result"] == "本机API对接成功"
|
||||
assert seen[0]["messages"][0]["content"] == "只回复本机API对接成功"
|
||||
assert (await dispatch(env, "ou_test", "/助手 repeat", "native-turn")).json()["status"] == "duplicate"
|
||||
assert len(seen) == 1
|
||||
finally:
|
||||
server.should_exit = True
|
||||
await asyncio.wait_for(server_task, 5)
|
||||
@@ -0,0 +1,38 @@
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
import tarfile
|
||||
|
||||
from scripts.fetch_official_images import export_image, digest_file
|
||||
|
||||
|
||||
def test_offline_docker_archive_preserves_config_and_rootfs(tmp_path):
|
||||
cache, output = tmp_path / "cache", tmp_path / "out"
|
||||
cache.mkdir()
|
||||
output.mkdir()
|
||||
layer = io.BytesIO()
|
||||
with tarfile.open(fileobj=layer, mode="w") as tar:
|
||||
content = b"hello ARM64\n"
|
||||
info = tarfile.TarInfo("test.txt")
|
||||
info.size, info.mode = len(content), 0o644
|
||||
tar.addfile(info, io.BytesIO(content))
|
||||
raw = layer.getvalue()
|
||||
raw_digest = hashlib.sha256(raw).hexdigest()
|
||||
import gzip
|
||||
compressed = gzip.compress(raw)
|
||||
blob_digest = hashlib.sha256(compressed).hexdigest()
|
||||
(cache / (blob_digest + ".blob")).write_bytes(compressed)
|
||||
config = json.dumps({"architecture": "arm64", "os": "linux", "config": {"Env": []},
|
||||
"rootfs": {"type": "layers", "diff_ids": ["sha256:" + raw_digest]}}).encode()
|
||||
config_digest = hashlib.sha256(config).hexdigest()
|
||||
(cache / (config_digest + ".blob")).write_bytes(config)
|
||||
record = {"name": "test", "tag": "arm64", "config": {"digest": "sha256:" + config_digest},
|
||||
"layers": [{"digest": "sha256:" + blob_digest}]}
|
||||
export_image(record, cache, output)
|
||||
path = output / record["archive"]
|
||||
assert record["archive_sha256"] == digest_file(path)
|
||||
with tarfile.open(path) as tar:
|
||||
manifest = json.load(tar.extractfile("manifest.json"))[0]
|
||||
assert manifest["RepoTags"] == ["test:arm64"]
|
||||
assert tar.extractfile(manifest["Config"]).read() == config
|
||||
assert tar.extractfile(manifest["Layers"][0]).read() == raw
|
||||
@@ -0,0 +1,143 @@
|
||||
import asyncio
|
||||
import hashlib
|
||||
import json
|
||||
import time
|
||||
import uuid
|
||||
|
||||
from relay.crypto import load_private, sign
|
||||
from .conftest import provision, connect_device
|
||||
|
||||
|
||||
async def dispatch(env, owner, text, turn=None):
|
||||
return await env["client"].post("/admin/test/dispatch", headers=env["admin"],
|
||||
json={"open_id": owner, "text": text, "turn_id": turn or uuid.uuid4().hex})
|
||||
|
||||
|
||||
async def test_two_devices_encrypted_isolation_and_dedup(env):
|
||||
a, ah = await provision(env, "ou_a")
|
||||
b, bh = await provision(env, "ou_b")
|
||||
await connect_device(env, a)
|
||||
await connect_device(env, b)
|
||||
responses = await asyncio.gather(dispatch(env, "ou_a", "/回显 A的唯一结果"), dispatch(env, "ou_b", "/回显 B的唯一结果"))
|
||||
assert [x.json()["result"] for x in responses] == ["A的唯一结果", "B的唯一结果"]
|
||||
first = await dispatch(env, "ou_a", "/回显 once", "same-turn")
|
||||
again = await dispatch(env, "ou_a", "/回显 twice", "same-turn")
|
||||
assert first.json()["status"] == "completed"
|
||||
assert again.json()["status"] == "duplicate"
|
||||
# Directory compromise cannot silently substitute B's certificate for A.
|
||||
presence_b = await env["redis"].get("hn:online:" + bh)
|
||||
await env["redis"].set("hn:online:" + ah, presence_b)
|
||||
assert (await dispatch(env, "ou_a", "/回显 leaked")).status_code == 409
|
||||
|
||||
|
||||
async def test_offline_revoke_and_persistence(env):
|
||||
directory, host = await provision(env)
|
||||
assert (await dispatch(env, "ou_test", "/主机名")).json()["status"] == "offline"
|
||||
await connect_device(env, directory)
|
||||
assert (await dispatch(env, "ou_test", "/主机名")).json()["status"] == "completed"
|
||||
result = await env["client"].post(f"/admin/devices/{host}/revoke", headers=env["admin"])
|
||||
assert result.status_code == 200
|
||||
assert (await dispatch(env, "ou_test", "/主机名")).json()["status"] == "unavailable"
|
||||
await asyncio.sleep(0.05)
|
||||
assert not await env["redis"].exists("hn:online:" + host)
|
||||
from relay.store import Store
|
||||
db = Store(env["settings"].data_dir / "metadata.sqlite")
|
||||
assert db.device(host)["revoked"] == 1
|
||||
db.close()
|
||||
|
||||
|
||||
async def test_device_read_boundary(env):
|
||||
directory, host = await provision(env)
|
||||
root = env["root"] / "test-files"
|
||||
root.mkdir()
|
||||
(root / "allowed.txt").write_text("授权内容", encoding="utf-8")
|
||||
(env["root"] / "outside.txt").write_text("不该读到", encoding="utf-8")
|
||||
await connect_device(env, directory, root)
|
||||
assert (await dispatch(env, "ou_test", "/读取 allowed.txt")).json()["result"] == "授权内容"
|
||||
assert "拒绝" in (await dispatch(env, "ou_test", "/读取 ../outside.txt")).json()["result"]
|
||||
assert "只读诊断" in (await dispatch(env, "ou_test", "rm -rf /")).json()["result"]
|
||||
|
||||
|
||||
async def test_binding_requires_device_proof_and_one_time_code(env):
|
||||
directory, host = await provision(env)
|
||||
response = await env["client"].post("/device/bind", json={"host_id": host, "proof": {}})
|
||||
assert response.status_code == 403
|
||||
response = await env["client"].post("/admin/devices", json={"host_id": host, "cert": "invalid"})
|
||||
assert response.status_code == 401
|
||||
|
||||
|
||||
async def test_oauth_wrong_secret_does_not_destroy_and_claim_once(env):
|
||||
client = env["client"]
|
||||
start = await client.post("/oauth/start?state=attacker", json={})
|
||||
assert start.status_code == 201
|
||||
data = start.json()
|
||||
assert data["state"] != "attacker"
|
||||
assert data["claim_secret"] not in data["authorization_url"]
|
||||
cb = await client.get("/oauth/cb", params={"state": data["state"], "code": "fake-code"})
|
||||
assert cb.status_code == 200
|
||||
bad = await client.get("/oauth/claim", params={"state": data["state"]}, headers={"X-OAuth-Claim-Secret": "bad"})
|
||||
assert bad.status_code == 404
|
||||
good = await client.get("/oauth/claim", params={"state": data["state"]}, headers={"X-OAuth-Claim-Secret": data["claim_secret"]})
|
||||
assert good.status_code == 200
|
||||
assert good.json()["expires_in"] == 6900
|
||||
assert "code" not in good.json()
|
||||
assert good.headers["cache-control"] == "no-store"
|
||||
assert (await client.get("/oauth/claim", params={"state": data["state"]}, headers={"X-OAuth-Claim-Secret": data["claim_secret"]})).status_code == 404
|
||||
assert (await client.get("/oauth/cb?state=unknown&code=x")).status_code == 400
|
||||
|
||||
|
||||
async def test_oauth_concurrent_callbacks_claims_and_refresh(env):
|
||||
client = env["client"]
|
||||
data = (await client.post("/oauth/start", json={})).json()
|
||||
responses = await asyncio.gather(*[client.get("/oauth/cb", params={"state": data["state"], "code": "same"}) for _ in range(2)])
|
||||
assert sorted(x.status_code for x in responses) == [200, 400]
|
||||
assert env["platform"].exchanges == 1
|
||||
responses = await asyncio.gather(*[client.get("/oauth/claim", params={"state": data["state"]}, headers={"X-OAuth-Claim-Secret": data["claim_secret"]}) for _ in range(2)])
|
||||
assert sorted(x.status_code for x in responses) == [200, 404]
|
||||
responses = await asyncio.gather(*[client.post("/oauth/refresh", json={"refresh_token": "same-fake-refresh"}) for _ in range(2)])
|
||||
assert sorted(x.status_code for x in responses) == [200, 409]
|
||||
assert env["platform"].refreshes == 1
|
||||
|
||||
|
||||
async def test_body_limit_proxy_disabled_and_rate_limit(env):
|
||||
client = env["client"]
|
||||
assert (await client.post("/oauth/start", content=b"x" * 300000)).status_code == 413
|
||||
assert (await client.get("/proxy", headers={"X-Relay-Target": "https://evil.invalid"})).status_code == 404
|
||||
statuses = [(await client.post("/oauth/start", json={})).status_code for _ in range(21)]
|
||||
assert statuses[-1] == 429
|
||||
|
||||
|
||||
def event(env, message_id="om_test", text="/回显 平台事件", chat_type="p2p"):
|
||||
return {"header": {"event_type": "im.message.receive_v1", "app_id": env["settings"].feishu_app_id,
|
||||
"token": env["settings"].feishu_verification_token},
|
||||
"event": {"sender": {"sender_type": "user", "sender_id": {"open_id": "ou_test"}},
|
||||
"message": {"message_id": message_id, "chat_type": chat_type, "message_type": "text",
|
||||
"content": json.dumps({"text": text}, ensure_ascii=False)}}}
|
||||
|
||||
|
||||
async def test_webhook_signature_event_routing_and_replay(env):
|
||||
directory, _ = await provision(env)
|
||||
await connect_device(env, directory)
|
||||
raw = json.dumps(event(env), ensure_ascii=False).encode()
|
||||
timestamp, nonce = str(int(time.time())), "nonce"
|
||||
signature = hashlib.sha256((timestamp + nonce + env["settings"].feishu_encrypt_key).encode() + raw).hexdigest()
|
||||
headers = {"X-Lark-Request-Timestamp": timestamp, "X-Lark-Request-Nonce": nonce, "X-Lark-Signature": signature}
|
||||
assert (await env["client"].post("/feishu/event", content=raw)).status_code == 403
|
||||
for _ in range(2):
|
||||
assert (await env["client"].post("/feishu/event", content=raw, headers=headers)).status_code == 200
|
||||
for _ in range(100):
|
||||
if env["platform"].replies:
|
||||
break
|
||||
await asyncio.sleep(0.02)
|
||||
assert env["platform"].replies == [("om_test", "平台事件")]
|
||||
# No transcript or credentials persisted in either connector metadata or device journal.
|
||||
for db in [env["settings"].data_dir / "metadata.sqlite", directory / "journal.sqlite"]:
|
||||
assert "平台事件".encode() not in db.read_bytes()
|
||||
|
||||
|
||||
async def test_group_events_ignored(env):
|
||||
result = await env["client"].post("/internal/events", json=event(env, chat_type="group"),
|
||||
headers={"X-Internal-Secret": env["settings"].internal_secret})
|
||||
assert result.status_code == 200
|
||||
await asyncio.sleep(0.05)
|
||||
assert env["platform"].replies == []
|
||||
@@ -0,0 +1,36 @@
|
||||
import httpx
|
||||
import pytest
|
||||
from relay.platform import Feishu, PlatformError
|
||||
|
||||
|
||||
async def test_private_platform_domain_and_reply_target(env):
|
||||
seen = []
|
||||
def handler(request):
|
||||
seen.append(request)
|
||||
if request.url.path.endswith("app_access_token/internal"):
|
||||
return httpx.Response(200, json={"code": 0, "app_access_token": "fake-app-token", "expire": 7200})
|
||||
if request.url.path.endswith("tenant_access_token/internal"):
|
||||
return httpx.Response(200, json={"code": 0, "tenant_access_token": "fake-tenant-token", "expire": 7200})
|
||||
if request.url.path.endswith("oidc/access_token"):
|
||||
return httpx.Response(200, json={"code": 0, "data": {"access_token": "fake-user-token",
|
||||
"refresh_token": "fake-refresh", "expires_in": 6900, "open_id": "ou_test"}})
|
||||
return httpx.Response(200, json={"code": 0, "data": {}})
|
||||
env["settings"].feishu_api_base = "https://private.example.invalid"
|
||||
f = Feishu(env["settings"], transport=httpx.MockTransport(handler))
|
||||
result = await f.exchange("fake-code")
|
||||
assert result["expires_in"] == 6900
|
||||
await f.reply("om_original", "你好")
|
||||
assert all(x.url.host == "private.example.invalid" for x in seen)
|
||||
assert seen[-1].url.path == "/open-apis/im/v1/messages/om_original/reply"
|
||||
assert seen[-1].headers["Authorization"] == "Bearer fake-tenant-token"
|
||||
assert len([x for x in seen if x.url.path.endswith("app_access_token/internal")]) == 1
|
||||
await f.close()
|
||||
|
||||
|
||||
async def test_platform_errors_never_echo_credentials(env):
|
||||
f = Feishu(env["settings"], transport=httpx.MockTransport(
|
||||
lambda request: httpx.Response(400, text="fake-secret-that-must-not-appear")))
|
||||
with pytest.raises(PlatformError) as error:
|
||||
await f.app_token()
|
||||
assert "fake-secret" not in str(error.value)
|
||||
await f.close()
|
||||
@@ -0,0 +1,33 @@
|
||||
"""Opt-in real PostgreSQL verification in an isolated, newly created schema."""
|
||||
import os
|
||||
import uuid
|
||||
import psycopg
|
||||
import pytest
|
||||
from psycopg import sql
|
||||
from relay.store import Store
|
||||
|
||||
|
||||
@pytest.mark.skipif(not os.environ.get("HN_TEST_DATABASE_URL"), reason="real PostgreSQL URL not supplied")
|
||||
def test_postgres_metadata_and_duplicate_claim(tmp_path):
|
||||
url = os.environ["HN_TEST_DATABASE_URL"]
|
||||
schema = "hn_test_" + uuid.uuid4().hex
|
||||
conn = psycopg.connect(url, autocommit=True)
|
||||
conn.execute(sql.SQL("CREATE SCHEMA {}").format(sql.Identifier(schema)))
|
||||
from psycopg.conninfo import make_conninfo
|
||||
isolated = make_conninfo(url, options=f"-c search_path={schema}")
|
||||
store = None
|
||||
try:
|
||||
store = Store(tmp_path / "unused.sqlite", isolated)
|
||||
store.add_device("host", "cert", "fingerprint")
|
||||
store.bind("host", "owner")
|
||||
assert store.by_owner("owner")["host_id"] == "host"
|
||||
assert store.begin_turn("turn", "owner", "host")
|
||||
assert not store.begin_turn("turn", "owner", "host")
|
||||
store.audit("owner", "instruction", "host", "completed")
|
||||
store.revoke("host")
|
||||
assert not store.by_owner("owner")
|
||||
finally:
|
||||
if store:
|
||||
store.close()
|
||||
conn.execute(sql.SQL("DROP SCHEMA {} CASCADE").format(sql.Identifier(schema)))
|
||||
conn.close()
|
||||
@@ -0,0 +1,44 @@
|
||||
import asyncio
|
||||
import json
|
||||
import secrets
|
||||
|
||||
from fastapi import FastAPI
|
||||
|
||||
from relay.device import run_device
|
||||
from .conftest import provision, serve
|
||||
from .test_hainuo1065 import POLICY, config
|
||||
from .test_web_session import browser
|
||||
|
||||
|
||||
async def test_h5_cookie_to_encrypted_device_to_native_http_result(env):
|
||||
"""Real loopback HTTP + WS, with a native model contract fixture."""
|
||||
app = FastAPI()
|
||||
@app.get("/health")
|
||||
async def health():
|
||||
return {"status": "ok", "platform": "hermes-agent"}
|
||||
@app.get("/v1/capabilities")
|
||||
async def capabilities():
|
||||
return {"hainuo_relay_policy": POLICY}
|
||||
@app.get("/v1/models")
|
||||
async def models():
|
||||
return {"data": [{"id": "hainuo-relay-text"}]}
|
||||
@app.post("/v1/chat/completions")
|
||||
async def chat():
|
||||
return {"choices": [{"message": {"content": "42"}, "finish_reason": "stop"}]}
|
||||
server, task, port = await serve(app)
|
||||
directory, host = await provision(env)
|
||||
cfg = config(env["root"], f"http://127.0.0.1:{port}")
|
||||
connected = asyncio.Event()
|
||||
device = asyncio.create_task(run_device(directory, env["router_url"], connected_event=connected, hainuo_config=cfg))
|
||||
env["tasks"].append(device)
|
||||
try:
|
||||
await asyncio.wait_for(connected.wait(), 5)
|
||||
headers = await browser(env, directory, host)
|
||||
response = await env["client"].post("/web/chat", headers=headers,
|
||||
json={"text": "17加25", "request_id": secrets.token_hex(16)})
|
||||
assert response.status_code == 200
|
||||
assert response.json()["status"] == "completed"
|
||||
assert response.json()["result"] == "42"
|
||||
finally:
|
||||
server.should_exit = True
|
||||
await asyncio.wait_for(task, 5)
|
||||
@@ -0,0 +1,141 @@
|
||||
import asyncio
|
||||
import json
|
||||
import secrets
|
||||
import time
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
||||
from relay.crypto import load_private, sign
|
||||
from relay.web_session import COOKIE, digest
|
||||
from .conftest import provision, connect_device
|
||||
|
||||
|
||||
async def issue(env, directory, host):
|
||||
config = json.loads((directory / "device.json").read_text())
|
||||
proof = sign({"host_id": host, "owner": config["owner"], "purpose": "h5-pair-v1",
|
||||
"nonce": secrets.token_hex(16), "ts": int(time.time())}, load_private(directory / "device.key"))
|
||||
response = await env["client"].post("/device/web-pair", json={"host_id": host, "proof": proof})
|
||||
assert response.status_code == 200, response.text
|
||||
return response.json(), proof
|
||||
|
||||
|
||||
async def claim(env, code):
|
||||
return await env["client"].post("/web/claim", json={"code": code}, headers={"Origin": env["settings"].public_url})
|
||||
|
||||
|
||||
def cookie(response):
|
||||
return {"Cookie": COOKIE + "=" + response.cookies.get(COOKIE)}
|
||||
|
||||
|
||||
async def browser(env, directory, host):
|
||||
issued, _ = await issue(env, directory, host)
|
||||
response = await claim(env, issued["code"])
|
||||
assert response.status_code == 200, response.text
|
||||
return {**cookie(response), "Origin": env["settings"].public_url}
|
||||
|
||||
|
||||
async def test_browser_pairing_one_time_signed_and_secure_cookie(env):
|
||||
directory, host = await provision(env)
|
||||
issued, proof = await issue(env, directory, host)
|
||||
assert "signature" in issued
|
||||
# The anonymous browser can neither name an owner nor invent a device signature.
|
||||
assert (await env["client"].post("/device/web-pair", json={"host_id": host, "proof": {}})).status_code == 403
|
||||
assert (await env["client"].post("/device/web-pair", json={"host_id": host, "proof": proof})).status_code == 409
|
||||
response = await claim(env, issued["code"])
|
||||
assert response.status_code == 200
|
||||
header = response.headers["set-cookie"].lower()
|
||||
assert "secure" in header and "httponly" in header and "samesite=strict" in header
|
||||
assert "code" not in response.json() and "owner" not in response.json()
|
||||
assert (await claim(env, issued["code"])).status_code == 404
|
||||
assert (await env["client"].get("/web/session")).status_code == 401 # Secure cookies never sent over test HTTP.
|
||||
session = await env["client"].get("/web/session", headers=cookie(response))
|
||||
assert session.status_code == 200 and session.json()["host_id"] == host
|
||||
assert session.headers["cache-control"] == "no-store"
|
||||
|
||||
|
||||
async def test_cross_origin_cannot_consume_pair_code_or_send_chat(env):
|
||||
directory, host = await provision(env)
|
||||
issued, _ = await issue(env, directory, host)
|
||||
for origin in [None, "https://evil.invalid", "null", "http://localhost.evil.invalid"]:
|
||||
headers = {} if origin is None else {"Origin": origin}
|
||||
result = await env["client"].post("/web/claim", json={"code": issued["code"]}, headers=headers)
|
||||
assert result.status_code == 403
|
||||
response = await claim(env, issued["code"])
|
||||
assert response.status_code == 200
|
||||
result = await env["client"].post("/web/chat", headers={**cookie(response), "Origin": "https://evil.invalid"},
|
||||
json={"text": "hello", "request_id": secrets.token_hex(16)})
|
||||
assert result.status_code == 403
|
||||
|
||||
|
||||
async def test_web_devices_are_isolated_and_chat_deduplicates(env):
|
||||
a, ah = await provision(env, "ou_a")
|
||||
b, bh = await provision(env, "ou_b")
|
||||
await connect_device(env, a)
|
||||
await connect_device(env, b)
|
||||
a_headers = await browser(env, a, ah)
|
||||
b_headers = await browser(env, b, bh)
|
||||
rid = secrets.token_hex(16)
|
||||
a_result, b_result = await asyncio.gather(
|
||||
env["client"].post("/web/chat", headers=a_headers, json={"text": "/回显 A独立回复", "request_id": rid}),
|
||||
env["client"].post("/web/chat", headers=b_headers, json={"text": "/回显 B独立回复", "request_id": rid}))
|
||||
assert a_result.json()["result"] == "A独立回复"
|
||||
assert b_result.json()["result"] == "B独立回复"
|
||||
again = await env["client"].post("/web/chat", headers=a_headers, json={"text": "/回显 repeat", "request_id": rid})
|
||||
assert again.json()["status"] == "duplicate"
|
||||
# Redis contains only pairing/session metadata, never the plaintext chat transcript.
|
||||
for key in await env["redis"].keys("hn:web*"):
|
||||
value = await env["redis"].get(key)
|
||||
assert "独立回复" not in str(value)
|
||||
|
||||
|
||||
async def test_logout_expiry_and_revoke_invalidate_browser(env):
|
||||
directory, host = await provision(env)
|
||||
headers = await browser(env, directory, host)
|
||||
assert (await env["client"].post("/web/logout", headers=headers)).status_code == 200
|
||||
assert (await env["client"].get("/web/session", headers=headers)).status_code == 401
|
||||
headers = await browser(env, directory, host)
|
||||
await env["redis"].delete("hn:web-session:" + digest(headers["Cookie"].split("=", 1)[1]))
|
||||
assert (await env["client"].get("/web/session", headers=headers)).status_code == 401
|
||||
headers = await browser(env, directory, host)
|
||||
await env["client"].post(f"/admin/devices/{host}/revoke", headers=env["admin"])
|
||||
assert (await env["client"].get("/web/session", headers=headers)).status_code == 401
|
||||
|
||||
|
||||
async def test_binding_generation_invalidates_old_codes_and_sessions(env):
|
||||
directory, host = await provision(env)
|
||||
issued, _ = await issue(env, directory, host)
|
||||
headers = await browser(env, directory, host)
|
||||
await env["redis"].incr("hn:web-generation:" + host)
|
||||
assert (await claim(env, issued["code"])).status_code == 401
|
||||
assert (await env["client"].get("/web/session", headers=headers)).status_code == 401
|
||||
|
||||
|
||||
async def test_pair_code_expiry_and_anonymous_chat_denied(env):
|
||||
directory, host = await provision(env)
|
||||
issued, _ = await issue(env, directory, host)
|
||||
await env["redis"].delete("hn:web-code:" + digest(issued["code"]))
|
||||
assert (await claim(env, issued["code"])).status_code == 404
|
||||
assert (await env["client"].post("/web/chat", headers={"Origin": env["settings"].public_url},
|
||||
json={"text": "hello", "request_id": secrets.token_hex(16)})).status_code == 401
|
||||
|
||||
|
||||
async def test_unprefixed_question_uses_native_agent_prefix(env):
|
||||
directory, host = await provision(env)
|
||||
headers = await browser(env, directory, host)
|
||||
seen = []
|
||||
async def capture(owner, text, turn):
|
||||
seen.append((owner, text))
|
||||
return {"status": "completed", "result": "42", "turn_id": turn}
|
||||
env["connector"].state.broker.dispatch = capture
|
||||
response = await env["client"].post("/web/chat", headers=headers,
|
||||
json={"text": "17加25", "request_id": secrets.token_hex(16), "open_id": "attacker"})
|
||||
assert response.json()["result"] == "42"
|
||||
assert seen == [("ou_test", "/助手 17加25")]
|
||||
|
||||
|
||||
async def test_concurrent_claim_exactly_one_browser(env):
|
||||
directory, host = await provision(env)
|
||||
issued, _ = await issue(env, directory, host)
|
||||
results = await asyncio.gather(claim(env, issued["code"]), claim(env, issued["code"]))
|
||||
assert sorted(x.status_code for x in results) == [200, 404]
|
||||
Reference in New Issue
Block a user