commit 011a7797ee72d019b28ef1d968de5fe42ca8e200 Author: ex_tianzy Date: Tue Oct 27 17:26:58 2026 +0800 Import ECS deployed relay source and historical patches (2026-10-27) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..79df8b0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +__pycache__/ +*.pyc +*.log +.env +.env.* +*.key +*.dpapi +*.p12 +*.pfx +*.sqlite +*.sqlite3 +device.pem +device.json +hainuo-native.json +registration.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..eca2e7d --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# ECS 中继服务源码快照 + +本仓库保存 2026-10-27 从 ECS 导出的解压后源码,共 316 个原始文件,包括运行容器源码、主机源码、部署参考文件和历史补丁源码。 + +## 来源与校验 + +- 原始归档:`hainuo-ecs-source-20261027-165115-qb1aPw.tar.gz` +- SHA256:`ac3bda5b7835b5d9c4be83c64244554e41c4bddcb94b7cf8adec43bf2efe7e9c` +- 上传前在 Windows 本地验证归档 SHA256、路径安全性和文件类型。 +- `SOURCE_SHA256SUMS.json` 记录 316 个原始文件的 SHA256。 +- 压缩包和外部校验文件没有作为源码提交。 + +## 目录说明 + +| 路径 | 内容 | +| --- | --- | +| `container-code/` | 从各运行容器中导出的应用源码;服务与镜像信息见 `deployed-services.tsv` | +| `host-code/` | ECS 主机工作目录中的源码,可能与容器内版本不同 | +| `deployment-source/` | 已安装的脚本、文档、测试及脱敏部署配置 | +| `patch-source/` | 历史补丁包中的源码;存在于此处不代表全部已部署 | +| `patch-inventory.json` | 历史补丁来源与校验记录 | + +## 使用范围 + +这是源码交接快照,不是可直接部署的离线交付包。Compose 中的敏感配置已脱敏;依赖二进制、离线镜像、数据库、运行时配置、证书私钥、设备身份和用户令牌不包含在内。新环境部署需要另行配置应用凭据、域名及证书、数据存储和依赖,并进行业务验收。 + +本次导入保留原始源码,不合并不同来源的版本,不修改 ECS 服务,也不包含仅安装在 Windows 客户端上的补丁。 + +## 上传前检查 + +敏感文件名及 PEM 私钥标记扫描未发现命中。凭据赋值格式扫描有 6 处命中,人工核对为测试中的 `fake-app-secret` / `fake-secret` 示例,不是运行凭据。这些检查不能保证识别所有形式的硬编码敏感信息。 diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..5f4c91d --- /dev/null +++ b/README.txt @@ -0,0 +1,17 @@ +ECS service source export + +container-code//relay is copied from each deployed container, not rebuilt +from a local workstation checkout. deployed-services.tsv records image IDs and state. +host-code/ is the separate host workspace source; it may differ from container code. + +No docker inspect environment dump, .env, runtime/, operator/, data volumes, +certificate private keys, device identities or user token files are exported. +deployment-source/ includes installed update scripts and sanitized compose files. +patch-source/ includes source-only historical ECS update archives found under +/home/cnooc; inventory records hashes, not proof that every archive is deployed. +Sanitized compose files redact values and are NOT directly deployable backups. +Dependency binaries and live environment values are not included. +This is source evidence, not a complete disaster-recovery backup. +PC-only patches are not on ECS unless separately uploaded; this export does not +pretend to include patches installed only on Windows clients. +Custom secrets hardcoded in source cannot be ruled out by filename exclusions. diff --git a/SOURCE_SHA256SUMS.json b/SOURCE_SHA256SUMS.json new file mode 100644 index 0000000..9ea1888 --- /dev/null +++ b/SOURCE_SHA256SUMS.json @@ -0,0 +1,318 @@ +{ + "README.txt": "23c1445b1746a11c2f53fbd8bd642a199dbd32b8a38b82aaddc92cd43922e9df", + "host-code/relay/process_lock.py": "55967814dad34e8278dca985ebaa224bf5baa7bdf08569624b1689c05c4e1bc7", + "host-code/relay/web_session.py": "32d0cb4e441a5ef0836d3bf7160dcd2b3ba54eb5e406ce290777a5e4b9c95616", + "host-code/relay/longconn.py": "2f8c0b1f03644f679de5497a38c8a5616153544bc0996b8ff067d551b8f661de", + "host-code/relay/device.py": "9632c97fe2aa33633b738cce492b1d39f7fa284c12794c01625403137d70bf1d", + "host-code/relay/router.py": "acaa02ce49bc94792a001cf6d4da65258bd0e2739839d0904f3c84cabc908297", + "host-code/relay/windows_keys.py": "d2d2ef0c49f97c1b2a4ba109e0bfea65d52bf0a7b0d5bf5d1267ea4b38f913a8", + "host-code/relay/crypto.py": "d8bdf71a76f27f5366cc4cdd80ab71a31ba2eb5c7d124ac8fd7538ab3b1a56bf", + "host-code/relay/hainuo1065.py": "5cde2a76cd6e62f30c27fbce5360b0c49433a13c246c80527fadc6a441cc8004", + "host-code/relay/enrollment_signer.py": "43ac844f9c6bdaac10901c1e6923a9e7bca0da0a4e997dbcf33eb5209b3237fd", + "host-code/relay/common.py": "27426e3e23554dc8417052d79b08570bd4abfbb52140d1a984d2726160adc5c9", + "host-code/relay/platform.py": "e16bb840ea04d8f3c797ad893a6624c8bb52e60e1c5d7adb3c4d344338a593a0", + "host-code/relay/work_tools.py": "906ae536c4d203f0ce9c8eae56597ab77041aac1b86ec23f8791b9b4200e4a05", + "host-code/relay/work_client.py": "88e2785670b31fbb369d1eee0f46ad3812e71fb2f96aee61cfd085574fa4819e", + "host-code/relay/work_api.py": "82713bebed9d675ddbc98b06345a6e42062fd94bc10a7d9af91ebdf15cee7c5d", + "host-code/relay/cli.py": "07c198811fb239b0fdd9b1c84966dfd05da4214a0035bddbf581147f19af859d", + "host-code/relay/connector.py": "942403c1062feee25e0ee64a79661d1994e6dbc697fc48425010fafbf6aa2b8e", + "host-code/relay/web/index.html": "6366aae3fea4f8253479b2400723b9b8ba85bc6fe65a7e083dd646e33eefc7bb", + "host-code/relay/web/app.js": "f0fd6ebf9fde92013ec7e1d97ea37dc7de1c511636f99f6692f92d11766a767e", + "host-code/relay/web/style.css": "6b5f11d17844c86aa9dd976db560b9331f027a5a4abccee69c33859b7ab06118", + "host-code/relay/enrollment.py": "a8bc40a51179b20be030b3b483a1b161f4fa0b87d1ac29a02aeb91702f2b9560", + "host-code/relay/__init__.py": "49c2aefb6d6d832dce26493eeac743f6db3e6f3fb662250e66c862475d46ce1e", + "host-code/relay/config.py": "fd858ea00f6f37bd28c51d5d7589e27237c1bfbd7e6b71570e00414d3318cd33", + "host-code/relay/oauth.py": "10b31d0a6f898358c33ea3d51ee02aaf17c9863fc15746d5da9d34d76815d4bf", + "host-code/relay/store.py": "861a48760b645be7c640d22aa49540e31c9edfb890eb4b827741a3d57e6ad515", + "host-code/relay/client_manage.py": "d0762787107d4ced46b6f797cfc61b987b2ea3e51556887d69107dd2acbfdf2c", + "host-code/Dockerfile.offline": "fdd3422f24ca2ed7236004c8275cf6dac1dfe1a7f4316e2d4ca67cc33bf7288c", + "host-code/pyproject.toml": "70b6fec01c6b097c898a235fe06795a5c2f4b0fec783c04ef536047c260dffe8", + "host-code/README.md": "310e65fec0545768db71b72e97b89dd66297fefef50a2787544fd7ac50c4a889", + "host-code/requirements.lock": "0d11397a270cac40d730b193f002a80800169625e3fe2bc7f755a793991c13a8", + "host-code/Dockerfile": "9e9d3ed4b38e63fb0aac789fcfa3a07f8e96f28fb0d3c2c8980e33138aa7e628", + "deployed-services.tsv": "d28522ee493db2c5d152eb0b47d9407df7ff1da22a34794dbb9c5720c6881d14", + "patch-inventory.json": "32b74e180fa5b314c0b6d14b5fc8fae298831d3aa1cedd61571d2b389adb13e7", + "patch-source/951d53cd16a9de5f/relay/process_lock.py": "55967814dad34e8278dca985ebaa224bf5baa7bdf08569624b1689c05c4e1bc7", + "patch-source/951d53cd16a9de5f/relay/web_session.py": "32d0cb4e441a5ef0836d3bf7160dcd2b3ba54eb5e406ce290777a5e4b9c95616", + "patch-source/951d53cd16a9de5f/relay/longconn.py": "6cb1f73842eafe39fd3e16f06d34158760b5196383300cfc7aac4c129c49387c", + "patch-source/951d53cd16a9de5f/relay/device.py": "9632c97fe2aa33633b738cce492b1d39f7fa284c12794c01625403137d70bf1d", + "patch-source/951d53cd16a9de5f/relay/router.py": "acaa02ce49bc94792a001cf6d4da65258bd0e2739839d0904f3c84cabc908297", + "patch-source/951d53cd16a9de5f/relay/windows_keys.py": "d2d2ef0c49f97c1b2a4ba109e0bfea65d52bf0a7b0d5bf5d1267ea4b38f913a8", + "patch-source/951d53cd16a9de5f/relay/crypto.py": "d8bdf71a76f27f5366cc4cdd80ab71a31ba2eb5c7d124ac8fd7538ab3b1a56bf", + "patch-source/951d53cd16a9de5f/relay/hainuo1065.py": "5cde2a76cd6e62f30c27fbce5360b0c49433a13c246c80527fadc6a441cc8004", + "patch-source/951d53cd16a9de5f/relay/enrollment_signer.py": "43ac844f9c6bdaac10901c1e6923a9e7bca0da0a4e997dbcf33eb5209b3237fd", + "patch-source/951d53cd16a9de5f/relay/common.py": "27426e3e23554dc8417052d79b08570bd4abfbb52140d1a984d2726160adc5c9", + "patch-source/951d53cd16a9de5f/relay/platform.py": "161b0cd454a00b388a4a65f6a60540b0dcd945a57c1a2e4ab0e538ef5a433ff8", + "patch-source/951d53cd16a9de5f/relay/cli.py": "07c198811fb239b0fdd9b1c84966dfd05da4214a0035bddbf581147f19af859d", + "patch-source/951d53cd16a9de5f/relay/connector.py": "3ea9f5aa18093b30593069a11a134036cb67c1c7dacb2e5c2b0dad985ab8938d", + "patch-source/951d53cd16a9de5f/relay/enrollment.py": "a8bc40a51179b20be030b3b483a1b161f4fa0b87d1ac29a02aeb91702f2b9560", + "patch-source/951d53cd16a9de5f/relay/__init__.py": "49c2aefb6d6d832dce26493eeac743f6db3e6f3fb662250e66c862475d46ce1e", + "patch-source/951d53cd16a9de5f/relay/config.py": "789f4a42fce45f11a85c4a8aa64aa3a2963ad5499827064ad67212d48e53eb2e", + "patch-source/951d53cd16a9de5f/relay/oauth.py": "132d9064d79c44db4240c206acb2d5fbadb7f94d7445073b3cbfefbafaa3f95d", + "patch-source/951d53cd16a9de5f/relay/store.py": "861a48760b645be7c640d22aa49540e31c9edfb890eb4b827741a3d57e6ad515", + "patch-source/951d53cd16a9de5f/relay/client_manage.py": "3bdb5805d1ef249999bf8d9d49014b78606b00cea774f2442729f275c62676cd", + "patch-source/951d53cd16a9de5f/Dockerfile.offline": "fdd3422f24ca2ed7236004c8275cf6dac1dfe1a7f4316e2d4ca67cc33bf7288c", + "patch-source/951d53cd16a9de5f/scripts/update-work1067.sh": "000a8e671a8b3202689a1f827342c41728b75d1245a706042fa8fc5414c85d1c", + "patch-source/951d53cd16a9de5f/compose.autoreg.yaml": "5bc08394bcf5c1032142ec37db48710b8c50511c682a563384eca18d80f5fd1d", + "patch-source/a8137fe7e675b0a9/hainuo-relay/relay/web_session.py": "32d0cb4e441a5ef0836d3bf7160dcd2b3ba54eb5e406ce290777a5e4b9c95616", + "patch-source/a8137fe7e675b0a9/hainuo-relay/relay/longconn.py": "6cb1f73842eafe39fd3e16f06d34158760b5196383300cfc7aac4c129c49387c", + "patch-source/a8137fe7e675b0a9/hainuo-relay/relay/device.py": "4acf39c2b73a9d85a418829a088522a6848d3542a8939b5d57228e6a44441339", + "patch-source/a8137fe7e675b0a9/hainuo-relay/relay/router.py": "acaa02ce49bc94792a001cf6d4da65258bd0e2739839d0904f3c84cabc908297", + "patch-source/a8137fe7e675b0a9/hainuo-relay/relay/crypto.py": "63e3d8e3d206d0bdb209683abd1ac79e29474273530f018b1d88b5a39d12d780", + "patch-source/a8137fe7e675b0a9/hainuo-relay/relay/hainuo1065.py": "5cde2a76cd6e62f30c27fbce5360b0c49433a13c246c80527fadc6a441cc8004", + "patch-source/a8137fe7e675b0a9/hainuo-relay/relay/common.py": "27426e3e23554dc8417052d79b08570bd4abfbb52140d1a984d2726160adc5c9", + "patch-source/a8137fe7e675b0a9/hainuo-relay/relay/platform.py": "161b0cd454a00b388a4a65f6a60540b0dcd945a57c1a2e4ab0e538ef5a433ff8", + "patch-source/a8137fe7e675b0a9/hainuo-relay/relay/cli.py": "07c198811fb239b0fdd9b1c84966dfd05da4214a0035bddbf581147f19af859d", + "patch-source/a8137fe7e675b0a9/hainuo-relay/relay/connector.py": "d1922c0d9aa6d54bcfcfc83a7b1f39d1c469c8bd38385febb0e88fbfa10378e3", + "patch-source/a8137fe7e675b0a9/hainuo-relay/relay/web/index.html": "6366aae3fea4f8253479b2400723b9b8ba85bc6fe65a7e083dd646e33eefc7bb", + "patch-source/a8137fe7e675b0a9/hainuo-relay/relay/web/app.js": "f0fd6ebf9fde92013ec7e1d97ea37dc7de1c511636f99f6692f92d11766a767e", + "patch-source/a8137fe7e675b0a9/hainuo-relay/relay/web/style.css": "6b5f11d17844c86aa9dd976db560b9331f027a5a4abccee69c33859b7ab06118", + "patch-source/a8137fe7e675b0a9/hainuo-relay/relay/__init__.py": "49c2aefb6d6d832dce26493eeac743f6db3e6f3fb662250e66c862475d46ce1e", + "patch-source/a8137fe7e675b0a9/hainuo-relay/relay/config.py": "2b6df3d2f437ca62171fbc7e672c04eb4f47769e15cef26c1291f8160d27fdab", + "patch-source/a8137fe7e675b0a9/hainuo-relay/relay/oauth.py": "132d9064d79c44db4240c206acb2d5fbadb7f94d7445073b3cbfefbafaa3f95d", + "patch-source/a8137fe7e675b0a9/hainuo-relay/relay/store.py": "861a48760b645be7c640d22aa49540e31c9edfb890eb4b827741a3d57e6ad515", + "patch-source/a8137fe7e675b0a9/hainuo-relay/tests/test_feishu_longconn_setup.py": "17af4efc2b05fea62dec051c2e4ab145184b97d18ce2f904c44ed162b103bcb0", + "patch-source/a8137fe7e675b0a9/hainuo-relay/Dockerfile.offline": "fdd3422f24ca2ed7236004c8275cf6dac1dfe1a7f4316e2d4ca67cc33bf7288c", + "patch-source/a8137fe7e675b0a9/hainuo-relay/scripts/start-feishu-longconn.sh": "f07765ea2b649373b0da781902c9bec631d4812f7ae977aea8163ed6cecafc97", + "patch-source/a8137fe7e675b0a9/hainuo-relay/scripts/configure_feishu_longconn.py": "9c81a768bfc97ab678ad37fac09530604d327f46893274e1b9e1b3c4edafdcce", + "patch-source/a8137fe7e675b0a9/hainuo-relay/docs/FEISHU_PRIVATE_LONGCONN.md": "28c80cd29cbf60af518fd98e4dfefa702d45e563c570cb5722cb5aa73a0d8398", + "patch-source/02ffd84040a628b7/relay/process_lock.py": "55967814dad34e8278dca985ebaa224bf5baa7bdf08569624b1689c05c4e1bc7", + "patch-source/02ffd84040a628b7/relay/web_session.py": "32d0cb4e441a5ef0836d3bf7160dcd2b3ba54eb5e406ce290777a5e4b9c95616", + "patch-source/02ffd84040a628b7/relay/longconn.py": "6cb1f73842eafe39fd3e16f06d34158760b5196383300cfc7aac4c129c49387c", + "patch-source/02ffd84040a628b7/relay/device.py": "9632c97fe2aa33633b738cce492b1d39f7fa284c12794c01625403137d70bf1d", + "patch-source/02ffd84040a628b7/relay/router.py": "acaa02ce49bc94792a001cf6d4da65258bd0e2739839d0904f3c84cabc908297", + "patch-source/02ffd84040a628b7/relay/windows_keys.py": "d2d2ef0c49f97c1b2a4ba109e0bfea65d52bf0a7b0d5bf5d1267ea4b38f913a8", + "patch-source/02ffd84040a628b7/relay/crypto.py": "d8bdf71a76f27f5366cc4cdd80ab71a31ba2eb5c7d124ac8fd7538ab3b1a56bf", + "patch-source/02ffd84040a628b7/relay/hainuo1065.py": "5cde2a76cd6e62f30c27fbce5360b0c49433a13c246c80527fadc6a441cc8004", + "patch-source/02ffd84040a628b7/relay/enrollment_signer.py": "43ac844f9c6bdaac10901c1e6923a9e7bca0da0a4e997dbcf33eb5209b3237fd", + "patch-source/02ffd84040a628b7/relay/common.py": "27426e3e23554dc8417052d79b08570bd4abfbb52140d1a984d2726160adc5c9", + "patch-source/02ffd84040a628b7/relay/platform.py": "e16bb840ea04d8f3c797ad893a6624c8bb52e60e1c5d7adb3c4d344338a593a0", + "patch-source/02ffd84040a628b7/relay/work_tools.py": "906ae536c4d203f0ce9c8eae56597ab77041aac1b86ec23f8791b9b4200e4a05", + "patch-source/02ffd84040a628b7/relay/work_client.py": "88e2785670b31fbb369d1eee0f46ad3812e71fb2f96aee61cfd085574fa4819e", + "patch-source/02ffd84040a628b7/relay/work_api.py": "7ced23ad5b894fc01e727a4d411edc0f5e8115418aa20c1d9b02c5ce9b4c17b6", + "patch-source/02ffd84040a628b7/relay/cli.py": "07c198811fb239b0fdd9b1c84966dfd05da4214a0035bddbf581147f19af859d", + "patch-source/02ffd84040a628b7/relay/connector.py": "942403c1062feee25e0ee64a79661d1994e6dbc697fc48425010fafbf6aa2b8e", + "patch-source/02ffd84040a628b7/relay/enrollment.py": "a8bc40a51179b20be030b3b483a1b161f4fa0b87d1ac29a02aeb91702f2b9560", + "patch-source/02ffd84040a628b7/relay/__init__.py": "49c2aefb6d6d832dce26493eeac743f6db3e6f3fb662250e66c862475d46ce1e", + "patch-source/02ffd84040a628b7/relay/config.py": "fd858ea00f6f37bd28c51d5d7589e27237c1bfbd7e6b71570e00414d3318cd33", + "patch-source/02ffd84040a628b7/relay/oauth.py": "10b31d0a6f898358c33ea3d51ee02aaf17c9863fc15746d5da9d34d76815d4bf", + "patch-source/02ffd84040a628b7/relay/store.py": "861a48760b645be7c640d22aa49540e31c9edfb890eb4b827741a3d57e6ad515", + "patch-source/02ffd84040a628b7/relay/client_manage.py": "d0762787107d4ced46b6f797cfc61b987b2ea3e51556887d69107dd2acbfdf2c", + "patch-source/02ffd84040a628b7/compose.userapi.yaml": "c08a9e8439f11415e8391a8b7c48e84bf8908a0ef8f6c99ecd2da7d31c7e346d", + "patch-source/02ffd84040a628b7/Dockerfile.offline": "fdd3422f24ca2ed7236004c8275cf6dac1dfe1a7f4316e2d4ca67cc33bf7288c", + "patch-source/02ffd84040a628b7/scripts/update-userapi.sh": "870ea9032da931483fec76d8588f0f5541a9b69008ee61a2258e87fedbb8217d", + "patch-source/0de3db40fc2044b0/hainuo-relay/relay/web_session.py": "32d0cb4e441a5ef0836d3bf7160dcd2b3ba54eb5e406ce290777a5e4b9c95616", + "patch-source/0de3db40fc2044b0/hainuo-relay/relay/longconn.py": "2576a6d9209bb0f64a02f2ecda89a906ca8cd2039fbf228db0e1f256d9589d47", + "patch-source/0de3db40fc2044b0/hainuo-relay/relay/device.py": "4acf39c2b73a9d85a418829a088522a6848d3542a8939b5d57228e6a44441339", + "patch-source/0de3db40fc2044b0/hainuo-relay/relay/router.py": "acaa02ce49bc94792a001cf6d4da65258bd0e2739839d0904f3c84cabc908297", + "patch-source/0de3db40fc2044b0/hainuo-relay/relay/crypto.py": "63e3d8e3d206d0bdb209683abd1ac79e29474273530f018b1d88b5a39d12d780", + "patch-source/0de3db40fc2044b0/hainuo-relay/relay/hainuo1065.py": "08c648d7bcae9182779c6311115bbc55c35312717c7753ac0133228ca1c45a8f", + "patch-source/0de3db40fc2044b0/hainuo-relay/relay/common.py": "27426e3e23554dc8417052d79b08570bd4abfbb52140d1a984d2726160adc5c9", + "patch-source/0de3db40fc2044b0/hainuo-relay/relay/platform.py": "161b0cd454a00b388a4a65f6a60540b0dcd945a57c1a2e4ab0e538ef5a433ff8", + "patch-source/0de3db40fc2044b0/hainuo-relay/relay/cli.py": "1cef185141621a8a51e166f7cb979b095b8ccc4d348f83438830563d67a3ee70", + "patch-source/0de3db40fc2044b0/hainuo-relay/relay/connector.py": "7672af06cf950065bc606260f56d357bb7e09ab1899d185283dd7f464b62745a", + "patch-source/0de3db40fc2044b0/hainuo-relay/relay/web/index.html": "6366aae3fea4f8253479b2400723b9b8ba85bc6fe65a7e083dd646e33eefc7bb", + "patch-source/0de3db40fc2044b0/hainuo-relay/relay/web/app.js": "f0fd6ebf9fde92013ec7e1d97ea37dc7de1c511636f99f6692f92d11766a767e", + "patch-source/0de3db40fc2044b0/hainuo-relay/relay/web/style.css": "6b5f11d17844c86aa9dd976db560b9331f027a5a4abccee69c33859b7ab06118", + "patch-source/0de3db40fc2044b0/hainuo-relay/relay/__init__.py": "49c2aefb6d6d832dce26493eeac743f6db3e6f3fb662250e66c862475d46ce1e", + "patch-source/0de3db40fc2044b0/hainuo-relay/relay/config.py": "cef0894a9659e9bf70cf8831e9955e3eb345a722ef4e447780e205a061341131", + "patch-source/0de3db40fc2044b0/hainuo-relay/relay/oauth.py": "c92d8163251302afd9ff6d46999fa8daa6413715350d8c42eab83835dec3c03a", + "patch-source/0de3db40fc2044b0/hainuo-relay/relay/store.py": "861a48760b645be7c640d22aa49540e31c9edfb890eb4b827741a3d57e6ad515", + "patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_web_session.py": "15008f4ecbd8dc1d6c4b3ff7f10534eb753a65bde0eae9dbf8017313a293bda1", + "patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_platform.py": "e09058eea66a6236da68b73ce3114f8adca4abf89c76f91a0b2682794d365387", + "patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_web_native_chain.py": "01344733415918b739e92f7f50a43ccaf0c3e73b68be775272450f8a889ae174", + "patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_boundaries.py": "d446b939c2eaaa56cdf86edaa26001a712a16d0b0051dfff6f539a023aae340a", + "patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_agent.py": "964c9428aa3cdc6e7978fbfb21ccaf5208b5711df780f8c5cc5c5b6f5454f39d", + "patch-source/0de3db40fc2044b0/hainuo-relay/tests/conftest.py": "441737a6c9ccbfd5fe1692919abb271ea8d1bab1897ffe3ba81a3e1ac0baf6cd", + "patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_crypto.py": "d6aa44abe38b4f22ada7abe9e6da0a42ea20aebe916b3baf00022cd936671005", + "patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_hainuo1065.py": "03fff97d432a83561071af01c173045bf8ac58cbdb75ecca57e931ba977377e2", + "patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_image_archive.py": "01f3062f6f9068d08ba11e45a0ce63efd611bd0f18688cb60ee335095ab3ad51", + "patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_integration.py": "2b3c475e460805d270690e869e0d840b2978123e135397658a95600ebc4c759b", + "patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_postgres.py": "6929326d76ec07d39840e8207d80ab535877010d0c4930d0c2092cbf33dd501d", + "patch-source/0de3db40fc2044b0/hainuo-relay/tests/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "patch-source/0de3db40fc2044b0/hainuo-relay/Dockerfile.offline": "fdd3422f24ca2ed7236004c8275cf6dac1dfe1a7f4316e2d4ca67cc33bf7288c", + "patch-source/0de3db40fc2044b0/hainuo-relay/scripts/hainuo_native_api.py": "475564cf6baf5f64b542efc67e843be4bf9ac6b1f7f96aa3448ee91ce5c11fe6", + "patch-source/0de3db40fc2044b0/hainuo-relay/scripts/update-h5.sh": "76086172ddf00ac40c468a65b9515ce2cc99d52aea45795e2dc35477465fec7a", + "patch-source/0de3db40fc2044b0/hainuo-relay/docs/H5_PAIR_CHAT.md": "57df400ac9bd42f2c1e09ac75c59cdea45f4a7ce72720dfe615c7f66c058cbc5", + "patch-source/51c062dff81e268e/relay/process_lock.py": "55967814dad34e8278dca985ebaa224bf5baa7bdf08569624b1689c05c4e1bc7", + "patch-source/51c062dff81e268e/relay/web_session.py": "32d0cb4e441a5ef0836d3bf7160dcd2b3ba54eb5e406ce290777a5e4b9c95616", + "patch-source/51c062dff81e268e/relay/longconn.py": "6cb1f73842eafe39fd3e16f06d34158760b5196383300cfc7aac4c129c49387c", + "patch-source/51c062dff81e268e/relay/device.py": "9632c97fe2aa33633b738cce492b1d39f7fa284c12794c01625403137d70bf1d", + "patch-source/51c062dff81e268e/relay/router.py": "acaa02ce49bc94792a001cf6d4da65258bd0e2739839d0904f3c84cabc908297", + "patch-source/51c062dff81e268e/relay/windows_keys.py": "d2d2ef0c49f97c1b2a4ba109e0bfea65d52bf0a7b0d5bf5d1267ea4b38f913a8", + "patch-source/51c062dff81e268e/relay/crypto.py": "d8bdf71a76f27f5366cc4cdd80ab71a31ba2eb5c7d124ac8fd7538ab3b1a56bf", + "patch-source/51c062dff81e268e/relay/hainuo1065.py": "5cde2a76cd6e62f30c27fbce5360b0c49433a13c246c80527fadc6a441cc8004", + "patch-source/51c062dff81e268e/relay/enrollment_signer.py": "43ac844f9c6bdaac10901c1e6923a9e7bca0da0a4e997dbcf33eb5209b3237fd", + "patch-source/51c062dff81e268e/relay/common.py": "27426e3e23554dc8417052d79b08570bd4abfbb52140d1a984d2726160adc5c9", + "patch-source/51c062dff81e268e/relay/platform.py": "161b0cd454a00b388a4a65f6a60540b0dcd945a57c1a2e4ab0e538ef5a433ff8", + "patch-source/51c062dff81e268e/relay/cli.py": "07c198811fb239b0fdd9b1c84966dfd05da4214a0035bddbf581147f19af859d", + "patch-source/51c062dff81e268e/relay/connector.py": "7abcddb202f839ae174a804c1c9b015d83e7645d07de0abacdc827cf7d705738", + "patch-source/51c062dff81e268e/relay/enrollment.py": "a8bc40a51179b20be030b3b483a1b161f4fa0b87d1ac29a02aeb91702f2b9560", + "patch-source/51c062dff81e268e/relay/__init__.py": "49c2aefb6d6d832dce26493eeac743f6db3e6f3fb662250e66c862475d46ce1e", + "patch-source/51c062dff81e268e/relay/config.py": "789f4a42fce45f11a85c4a8aa64aa3a2963ad5499827064ad67212d48e53eb2e", + "patch-source/51c062dff81e268e/relay/oauth.py": "132d9064d79c44db4240c206acb2d5fbadb7f94d7445073b3cbfefbafaa3f95d", + "patch-source/51c062dff81e268e/relay/store.py": "861a48760b645be7c640d22aa49540e31c9edfb890eb4b827741a3d57e6ad515", + "patch-source/51c062dff81e268e/relay/client_manage.py": "643e4a7db9bcad2207052eb87f9fd8653d07b324742b6955b98e08cec13f4560", + "patch-source/51c062dff81e268e/Dockerfile.offline": "fdd3422f24ca2ed7236004c8275cf6dac1dfe1a7f4316e2d4ca67cc33bf7288c", + "patch-source/51c062dff81e268e/scripts/update-work1067.sh": "000a8e671a8b3202689a1f827342c41728b75d1245a706042fa8fc5414c85d1c", + "patch-source/51c062dff81e268e/compose.autoreg.yaml": "5bc08394bcf5c1032142ec37db48710b8c50511c682a563384eca18d80f5fd1d", + "container-code/enrollment-signer/relay/process_lock.py": "55967814dad34e8278dca985ebaa224bf5baa7bdf08569624b1689c05c4e1bc7", + "container-code/enrollment-signer/relay/web_session.py": "32d0cb4e441a5ef0836d3bf7160dcd2b3ba54eb5e406ce290777a5e4b9c95616", + "container-code/enrollment-signer/relay/longconn.py": "6cb1f73842eafe39fd3e16f06d34158760b5196383300cfc7aac4c129c49387c", + "container-code/enrollment-signer/relay/device.py": "9632c97fe2aa33633b738cce492b1d39f7fa284c12794c01625403137d70bf1d", + "container-code/enrollment-signer/relay/router.py": "acaa02ce49bc94792a001cf6d4da65258bd0e2739839d0904f3c84cabc908297", + "container-code/enrollment-signer/relay/windows_keys.py": "d2d2ef0c49f97c1b2a4ba109e0bfea65d52bf0a7b0d5bf5d1267ea4b38f913a8", + "container-code/enrollment-signer/relay/crypto.py": "d8bdf71a76f27f5366cc4cdd80ab71a31ba2eb5c7d124ac8fd7538ab3b1a56bf", + "container-code/enrollment-signer/relay/hainuo1065.py": "5cde2a76cd6e62f30c27fbce5360b0c49433a13c246c80527fadc6a441cc8004", + "container-code/enrollment-signer/relay/enrollment_signer.py": "43ac844f9c6bdaac10901c1e6923a9e7bca0da0a4e997dbcf33eb5209b3237fd", + "container-code/enrollment-signer/relay/common.py": "27426e3e23554dc8417052d79b08570bd4abfbb52140d1a984d2726160adc5c9", + "container-code/enrollment-signer/relay/platform.py": "161b0cd454a00b388a4a65f6a60540b0dcd945a57c1a2e4ab0e538ef5a433ff8", + "container-code/enrollment-signer/relay/cli.py": "07c198811fb239b0fdd9b1c84966dfd05da4214a0035bddbf581147f19af859d", + "container-code/enrollment-signer/relay/connector.py": "7abcddb202f839ae174a804c1c9b015d83e7645d07de0abacdc827cf7d705738", + "container-code/enrollment-signer/relay/web/index.html": "6366aae3fea4f8253479b2400723b9b8ba85bc6fe65a7e083dd646e33eefc7bb", + "container-code/enrollment-signer/relay/web/app.js": "f0fd6ebf9fde92013ec7e1d97ea37dc7de1c511636f99f6692f92d11766a767e", + "container-code/enrollment-signer/relay/web/style.css": "6b5f11d17844c86aa9dd976db560b9331f027a5a4abccee69c33859b7ab06118", + "container-code/enrollment-signer/relay/enrollment.py": "a8bc40a51179b20be030b3b483a1b161f4fa0b87d1ac29a02aeb91702f2b9560", + "container-code/enrollment-signer/relay/__init__.py": "49c2aefb6d6d832dce26493eeac743f6db3e6f3fb662250e66c862475d46ce1e", + "container-code/enrollment-signer/relay/config.py": "789f4a42fce45f11a85c4a8aa64aa3a2963ad5499827064ad67212d48e53eb2e", + "container-code/enrollment-signer/relay/oauth.py": "132d9064d79c44db4240c206acb2d5fbadb7f94d7445073b3cbfefbafaa3f95d", + "container-code/enrollment-signer/relay/store.py": "861a48760b645be7c640d22aa49540e31c9edfb890eb4b827741a3d57e6ad515", + "container-code/enrollment-signer/relay/client_manage.py": "643e4a7db9bcad2207052eb87f9fd8653d07b324742b6955b98e08cec13f4560", + "container-code/enrollment-signer/pyproject.toml": "70b6fec01c6b097c898a235fe06795a5c2f4b0fec783c04ef536047c260dffe8", + "container-code/enrollment-signer/requirements.lock": "0d11397a270cac40d730b193f002a80800169625e3fe2bc7f755a793991c13a8", + "container-code/enrollment-signer/image-id.txt": "2c6182b1b0caf1e0f2714d9e09338d16ad19bd01faea2951732c1bb7b3797109", + "container-code/connector/relay/process_lock.py": "55967814dad34e8278dca985ebaa224bf5baa7bdf08569624b1689c05c4e1bc7", + "container-code/connector/relay/web_session.py": "32d0cb4e441a5ef0836d3bf7160dcd2b3ba54eb5e406ce290777a5e4b9c95616", + "container-code/connector/relay/longconn.py": "2f8c0b1f03644f679de5497a38c8a5616153544bc0996b8ff067d551b8f661de", + "container-code/connector/relay/device.py": "9632c97fe2aa33633b738cce492b1d39f7fa284c12794c01625403137d70bf1d", + "container-code/connector/relay/router.py": "acaa02ce49bc94792a001cf6d4da65258bd0e2739839d0904f3c84cabc908297", + "container-code/connector/relay/windows_keys.py": "d2d2ef0c49f97c1b2a4ba109e0bfea65d52bf0a7b0d5bf5d1267ea4b38f913a8", + "container-code/connector/relay/crypto.py": "d8bdf71a76f27f5366cc4cdd80ab71a31ba2eb5c7d124ac8fd7538ab3b1a56bf", + "container-code/connector/relay/hainuo1065.py": "5cde2a76cd6e62f30c27fbce5360b0c49433a13c246c80527fadc6a441cc8004", + "container-code/connector/relay/enrollment_signer.py": "43ac844f9c6bdaac10901c1e6923a9e7bca0da0a4e997dbcf33eb5209b3237fd", + "container-code/connector/relay/common.py": "27426e3e23554dc8417052d79b08570bd4abfbb52140d1a984d2726160adc5c9", + "container-code/connector/relay/platform.py": "e16bb840ea04d8f3c797ad893a6624c8bb52e60e1c5d7adb3c4d344338a593a0", + "container-code/connector/relay/work_tools.py": "906ae536c4d203f0ce9c8eae56597ab77041aac1b86ec23f8791b9b4200e4a05", + "container-code/connector/relay/work_client.py": "88e2785670b31fbb369d1eee0f46ad3812e71fb2f96aee61cfd085574fa4819e", + "container-code/connector/relay/work_api.py": "82713bebed9d675ddbc98b06345a6e42062fd94bc10a7d9af91ebdf15cee7c5d", + "container-code/connector/relay/cli.py": "07c198811fb239b0fdd9b1c84966dfd05da4214a0035bddbf581147f19af859d", + "container-code/connector/relay/connector.py": "942403c1062feee25e0ee64a79661d1994e6dbc697fc48425010fafbf6aa2b8e", + "container-code/connector/relay/web/index.html": "6366aae3fea4f8253479b2400723b9b8ba85bc6fe65a7e083dd646e33eefc7bb", + "container-code/connector/relay/web/app.js": "f0fd6ebf9fde92013ec7e1d97ea37dc7de1c511636f99f6692f92d11766a767e", + "container-code/connector/relay/web/style.css": "6b5f11d17844c86aa9dd976db560b9331f027a5a4abccee69c33859b7ab06118", + "container-code/connector/relay/enrollment.py": "a8bc40a51179b20be030b3b483a1b161f4fa0b87d1ac29a02aeb91702f2b9560", + "container-code/connector/relay/__init__.py": "49c2aefb6d6d832dce26493eeac743f6db3e6f3fb662250e66c862475d46ce1e", + "container-code/connector/relay/config.py": "fd858ea00f6f37bd28c51d5d7589e27237c1bfbd7e6b71570e00414d3318cd33", + "container-code/connector/relay/oauth.py": "10b31d0a6f898358c33ea3d51ee02aaf17c9863fc15746d5da9d34d76815d4bf", + "container-code/connector/relay/store.py": "861a48760b645be7c640d22aa49540e31c9edfb890eb4b827741a3d57e6ad515", + "container-code/connector/relay/client_manage.py": "d0762787107d4ced46b6f797cfc61b987b2ea3e51556887d69107dd2acbfdf2c", + "container-code/connector/pyproject.toml": "70b6fec01c6b097c898a235fe06795a5c2f4b0fec783c04ef536047c260dffe8", + "container-code/connector/requirements.lock": "0d11397a270cac40d730b193f002a80800169625e3fe2bc7f755a793991c13a8", + "container-code/connector/image-id.txt": "f2fcd0651c9375bc486647b44cf1854a7581e818a849b0acef2dbb8fe2b41842", + "container-code/longconn/relay/process_lock.py": "55967814dad34e8278dca985ebaa224bf5baa7bdf08569624b1689c05c4e1bc7", + "container-code/longconn/relay/web_session.py": "32d0cb4e441a5ef0836d3bf7160dcd2b3ba54eb5e406ce290777a5e4b9c95616", + "container-code/longconn/relay/longconn.py": "2f8c0b1f03644f679de5497a38c8a5616153544bc0996b8ff067d551b8f661de", + "container-code/longconn/relay/device.py": "9632c97fe2aa33633b738cce492b1d39f7fa284c12794c01625403137d70bf1d", + "container-code/longconn/relay/router.py": "acaa02ce49bc94792a001cf6d4da65258bd0e2739839d0904f3c84cabc908297", + "container-code/longconn/relay/windows_keys.py": "d2d2ef0c49f97c1b2a4ba109e0bfea65d52bf0a7b0d5bf5d1267ea4b38f913a8", + "container-code/longconn/relay/crypto.py": "d8bdf71a76f27f5366cc4cdd80ab71a31ba2eb5c7d124ac8fd7538ab3b1a56bf", + "container-code/longconn/relay/hainuo1065.py": "5cde2a76cd6e62f30c27fbce5360b0c49433a13c246c80527fadc6a441cc8004", + "container-code/longconn/relay/enrollment_signer.py": "43ac844f9c6bdaac10901c1e6923a9e7bca0da0a4e997dbcf33eb5209b3237fd", + "container-code/longconn/relay/common.py": "27426e3e23554dc8417052d79b08570bd4abfbb52140d1a984d2726160adc5c9", + "container-code/longconn/relay/platform.py": "e16bb840ea04d8f3c797ad893a6624c8bb52e60e1c5d7adb3c4d344338a593a0", + "container-code/longconn/relay/work_tools.py": "906ae536c4d203f0ce9c8eae56597ab77041aac1b86ec23f8791b9b4200e4a05", + "container-code/longconn/relay/work_client.py": "88e2785670b31fbb369d1eee0f46ad3812e71fb2f96aee61cfd085574fa4819e", + "container-code/longconn/relay/work_api.py": "82713bebed9d675ddbc98b06345a6e42062fd94bc10a7d9af91ebdf15cee7c5d", + "container-code/longconn/relay/cli.py": "07c198811fb239b0fdd9b1c84966dfd05da4214a0035bddbf581147f19af859d", + "container-code/longconn/relay/connector.py": "942403c1062feee25e0ee64a79661d1994e6dbc697fc48425010fafbf6aa2b8e", + "container-code/longconn/relay/web/index.html": "6366aae3fea4f8253479b2400723b9b8ba85bc6fe65a7e083dd646e33eefc7bb", + "container-code/longconn/relay/web/app.js": "f0fd6ebf9fde92013ec7e1d97ea37dc7de1c511636f99f6692f92d11766a767e", + "container-code/longconn/relay/web/style.css": "6b5f11d17844c86aa9dd976db560b9331f027a5a4abccee69c33859b7ab06118", + "container-code/longconn/relay/enrollment.py": "a8bc40a51179b20be030b3b483a1b161f4fa0b87d1ac29a02aeb91702f2b9560", + "container-code/longconn/relay/__init__.py": "49c2aefb6d6d832dce26493eeac743f6db3e6f3fb662250e66c862475d46ce1e", + "container-code/longconn/relay/config.py": "fd858ea00f6f37bd28c51d5d7589e27237c1bfbd7e6b71570e00414d3318cd33", + "container-code/longconn/relay/oauth.py": "10b31d0a6f898358c33ea3d51ee02aaf17c9863fc15746d5da9d34d76815d4bf", + "container-code/longconn/relay/store.py": "861a48760b645be7c640d22aa49540e31c9edfb890eb4b827741a3d57e6ad515", + "container-code/longconn/relay/client_manage.py": "d0762787107d4ced46b6f797cfc61b987b2ea3e51556887d69107dd2acbfdf2c", + "container-code/longconn/pyproject.toml": "70b6fec01c6b097c898a235fe06795a5c2f4b0fec783c04ef536047c260dffe8", + "container-code/longconn/requirements.lock": "0d11397a270cac40d730b193f002a80800169625e3fe2bc7f755a793991c13a8", + "container-code/longconn/image-id.txt": "f2fcd0651c9375bc486647b44cf1854a7581e818a849b0acef2dbb8fe2b41842", + "container-code/router/relay/process_lock.py": "55967814dad34e8278dca985ebaa224bf5baa7bdf08569624b1689c05c4e1bc7", + "container-code/router/relay/web_session.py": "32d0cb4e441a5ef0836d3bf7160dcd2b3ba54eb5e406ce290777a5e4b9c95616", + "container-code/router/relay/longconn.py": "2f8c0b1f03644f679de5497a38c8a5616153544bc0996b8ff067d551b8f661de", + "container-code/router/relay/device.py": "9632c97fe2aa33633b738cce492b1d39f7fa284c12794c01625403137d70bf1d", + "container-code/router/relay/router.py": "acaa02ce49bc94792a001cf6d4da65258bd0e2739839d0904f3c84cabc908297", + "container-code/router/relay/windows_keys.py": "d2d2ef0c49f97c1b2a4ba109e0bfea65d52bf0a7b0d5bf5d1267ea4b38f913a8", + "container-code/router/relay/crypto.py": "d8bdf71a76f27f5366cc4cdd80ab71a31ba2eb5c7d124ac8fd7538ab3b1a56bf", + "container-code/router/relay/hainuo1065.py": "5cde2a76cd6e62f30c27fbce5360b0c49433a13c246c80527fadc6a441cc8004", + "container-code/router/relay/enrollment_signer.py": "43ac844f9c6bdaac10901c1e6923a9e7bca0da0a4e997dbcf33eb5209b3237fd", + "container-code/router/relay/common.py": "27426e3e23554dc8417052d79b08570bd4abfbb52140d1a984d2726160adc5c9", + "container-code/router/relay/platform.py": "e16bb840ea04d8f3c797ad893a6624c8bb52e60e1c5d7adb3c4d344338a593a0", + "container-code/router/relay/work_tools.py": "906ae536c4d203f0ce9c8eae56597ab77041aac1b86ec23f8791b9b4200e4a05", + "container-code/router/relay/work_client.py": "88e2785670b31fbb369d1eee0f46ad3812e71fb2f96aee61cfd085574fa4819e", + "container-code/router/relay/work_api.py": "82713bebed9d675ddbc98b06345a6e42062fd94bc10a7d9af91ebdf15cee7c5d", + "container-code/router/relay/cli.py": "07c198811fb239b0fdd9b1c84966dfd05da4214a0035bddbf581147f19af859d", + "container-code/router/relay/connector.py": "942403c1062feee25e0ee64a79661d1994e6dbc697fc48425010fafbf6aa2b8e", + "container-code/router/relay/web/index.html": "6366aae3fea4f8253479b2400723b9b8ba85bc6fe65a7e083dd646e33eefc7bb", + "container-code/router/relay/web/app.js": "f0fd6ebf9fde92013ec7e1d97ea37dc7de1c511636f99f6692f92d11766a767e", + "container-code/router/relay/web/style.css": "6b5f11d17844c86aa9dd976db560b9331f027a5a4abccee69c33859b7ab06118", + "container-code/router/relay/enrollment.py": "a8bc40a51179b20be030b3b483a1b161f4fa0b87d1ac29a02aeb91702f2b9560", + "container-code/router/relay/__init__.py": "49c2aefb6d6d832dce26493eeac743f6db3e6f3fb662250e66c862475d46ce1e", + "container-code/router/relay/config.py": "fd858ea00f6f37bd28c51d5d7589e27237c1bfbd7e6b71570e00414d3318cd33", + "container-code/router/relay/oauth.py": "10b31d0a6f898358c33ea3d51ee02aaf17c9863fc15746d5da9d34d76815d4bf", + "container-code/router/relay/store.py": "861a48760b645be7c640d22aa49540e31c9edfb890eb4b827741a3d57e6ad515", + "container-code/router/relay/client_manage.py": "d0762787107d4ced46b6f797cfc61b987b2ea3e51556887d69107dd2acbfdf2c", + "container-code/router/pyproject.toml": "70b6fec01c6b097c898a235fe06795a5c2f4b0fec783c04ef536047c260dffe8", + "container-code/router/requirements.lock": "0d11397a270cac40d730b193f002a80800169625e3fe2bc7f755a793991c13a8", + "container-code/router/image-id.txt": "f2fcd0651c9375bc486647b44cf1854a7581e818a849b0acef2dbb8fe2b41842", + "deployment-source/compose.userapi.yaml": "c08a9e8439f11415e8391a8b7c48e84bf8908a0ef8f6c99ecd2da7d31c7e346d", + "deployment-source/tests/test_web_session.py": "15008f4ecbd8dc1d6c4b3ff7f10534eb753a65bde0eae9dbf8017313a293bda1", + "deployment-source/tests/test_platform.py": "e09058eea66a6236da68b73ce3114f8adca4abf89c76f91a0b2682794d365387", + "deployment-source/tests/test_web_native_chain.py": "01344733415918b739e92f7f50a43ccaf0c3e73b68be775272450f8a889ae174", + "deployment-source/tests/test_boundaries.py": "d446b939c2eaaa56cdf86edaa26001a712a16d0b0051dfff6f539a023aae340a", + "deployment-source/tests/test_agent.py": "964c9428aa3cdc6e7978fbfb21ccaf5208b5711df780f8c5cc5c5b6f5454f39d", + "deployment-source/tests/conftest.py": "441737a6c9ccbfd5fe1692919abb271ea8d1bab1897ffe3ba81a3e1ac0baf6cd", + "deployment-source/tests/test_crypto.py": "d6aa44abe38b4f22ada7abe9e6da0a42ea20aebe916b3baf00022cd936671005", + "deployment-source/tests/test_hainuo1065.py": "03fff97d432a83561071af01c173045bf8ac58cbdb75ecca57e931ba977377e2", + "deployment-source/tests/test_feishu_longconn_setup.py": "17af4efc2b05fea62dec051c2e4ab145184b97d18ce2f904c44ed162b103bcb0", + "deployment-source/tests/test_image_archive.py": "01f3062f6f9068d08ba11e45a0ce63efd611bd0f18688cb60ee335095ab3ad51", + "deployment-source/tests/test_integration.py": "2b3c475e460805d270690e869e0d840b2978123e135397658a95600ebc4c759b", + "deployment-source/tests/test_postgres.py": "6929326d76ec07d39840e8207d80ab535877010d0c4930d0c2092cbf33dd501d", + "deployment-source/tests/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "deployment-source/compose.work.yaml": "6017b8d251d83567635c31b521e1e0d38a993f74e3927d1c367e0801a4d8cd80", + "deployment-source/deploy/Caddyfile": "caad7c776841b8da8be465fbedffcfbbf10c5291c642ec0cbf9e9fa44e3b0dd0", + "deployment-source/deploy/hainuo-relay@.service": "a8813fe6eee6578096860cae00f952124d985ff197027bfb47ba55af782a861c", + "deployment-source/compose.h5.yaml": "9a3b36d7a3c297a7cd71c1d780b6ed2929122b777ff146891741fa4d3a11c1fa", + "deployment-source/compose.yaml": "ed4d3dedff71f59453aca2e29f7c77d226c3665ee2252fba58c3bf384b3c2334", + "deployment-source/scripts/fetch_official_images.py": "348f96c195ab2ba22db327ef8343fe23f5219a760b42f7d3d3361f8ed202cb0f", + "deployment-source/scripts/install_offline.sh": "7986fed666310370a17731d8c41dbbd9b3b87ea1b6e586c4f9f0e3a670744923", + "deployment-source/scripts/hainuo_native_api.py": "475564cf6baf5f64b542efc67e843be4bf9ac6b1f7f96aa3448ee91ce5c11fe6", + "deployment-source/scripts/package_offline.py": "987821133c3e61b45247a837eb8c4cd26fffc554a54059906d94960e82c79811", + "deployment-source/scripts/package.py": "3da72da9db341d9f9f56a3190c1a32218128140a34d88d109edaa06ac8782f6b", + "deployment-source/scripts/verify_deployment.sh": "dce297f4bd5ec0cb823bbc39f4c57fa49914adf7b1be10ef9825639d49affc8a", + "deployment-source/scripts/package_wheels.py": "5d02645537fb03d2e1b7148e54eeb405bf565e6fdc23905b8b1abc2a94fe54eb", + "deployment-source/scripts/verify_image_archives.py": "d046223e36ce92720610dbcdc610911496b238f4f8d9d6ea60469f4e30715e9f", + "deployment-source/scripts/update-work1067.sh": "000a8e671a8b3202689a1f827342c41728b75d1245a706042fa8fc5414c85d1c", + "deployment-source/scripts/start.sh": "859d72719cbf2ef56b193b6b37dbc914cba1de3eb1aee48439442115763ebd62", + "deployment-source/scripts/start-feishu-longconn.sh": "f07765ea2b649373b0da781902c9bec631d4812f7ae977aea8163ed6cecafc97", + "deployment-source/scripts/bootstrap.sh": "9f5797c89986ca323cb9a30bd572b2f8f43fd477a89dfa12d6d564bfeb26f14e", + "deployment-source/scripts/preflight.sh": "e9019f39269741775384c146a6d2ec065fdaedb10e4f25094627add508551078", + "deployment-source/scripts/check_arm64_images.py": "6a3fba1cd8481fc2634fa8c9e473a8c2d1a5567ab866d2aee08ae2759306163a", + "deployment-source/scripts/configure_feishu_longconn.py": "9c81a768bfc97ab678ad37fac09530604d327f46893274e1b9e1b3c4edafdcce", + "deployment-source/scripts/update-userapi.sh": "870ea9032da931483fec76d8588f0f5541a9b69008ee61a2258e87fedbb8217d", + "deployment-source/scripts/update-h5.sh": "76086172ddf00ac40c468a65b9515ce2cc99d52aea45795e2dc35477465fec7a", + "deployment-source/scripts/backup.py": "dc522eea234a64de37e03bc62effb674ba0471b983715e2cf287b997d5919bc9", + "deployment-source/compose.acceptance.yaml": "630dd83123e6856a640a548658bb054f5953e897ba9f95765d88063668b23f31", + "deployment-source/compose.edge-fix.yaml": "4629e6acf01f59aaeb3d28a127a025069814f4fa5e78629db3142017a42b83c0", + "deployment-source/compose.autoreg.yaml": "5bc08394bcf5c1032142ec37db48710b8c50511c682a563384eca18d80f5fd1d", + "deployment-source/compose.offline.yaml": "7c3ffabaf38365f2dbb7e647acbdfa321df6e6dae7c131e375a5ecbcd721bb5a", + "deployment-source/docs/CHANGES.md": "c94bcf0976bb9f9e103ae042a86d5e0e3d082fb7abc19cc51f714dac5218eb74", + "deployment-source/docs/OFFLINE_IMAGES.md": "ec847886b02aad2805a61c7f6265be08f7f9307c324eb1ec4637b26e9d548237", + "deployment-source/docs/H5_PAIR_CHAT.md": "57df400ac9bd42f2c1e09ac75c59cdea45f4a7ce72720dfe615c7f66c058cbc5", + "deployment-source/docs/PROTOCOL.md": "936d2231d2c502bdef58806cfe3f74ab71954f22c8aa747f4c4c6fb4664f1185", + "deployment-source/docs/FEISHU_PRIVATE_LONGCONN.md": "28c80cd29cbf60af518fd98e4dfefa702d45e563c570cb5722cb5aa73a0d8398", + "deployment-source/docs/TEST_REPORT.md": "5623ca0442151cc4eefdaf20e1945cd61794951c4c3e020c78574282ae287aa1", + "deployment-source/docs/SPLIT_SIX_NODES.md": "cfc65d09118ab9d76ed77215de4ed120b62ca023df31f53d6148efcc17a3a44f", + "deployment-source/docs/DEPLOY_LINUX_ARM64.md": "700199ce2e2eb1ce528d7d9df1cff16b02d58e23dfc468173514b9eac84c13f4", + "deployment-source/docs/ACCEPTANCE.md": "44246c79de4c5d854fd6483d486350124535fa51f88045a0281cb4e82d40a721" +} diff --git a/container-code/connector/image-id.txt b/container-code/connector/image-id.txt new file mode 100644 index 0000000..b63734c --- /dev/null +++ b/container-code/connector/image-id.txt @@ -0,0 +1 @@ +sha256:90324bc252d99afc3ba068e9b416eec1ff31e9302716d1c08a525a45ad0ef011 diff --git a/container-code/connector/pyproject.toml b/container-code/connector/pyproject.toml new file mode 100644 index 0000000..3bf5e2c --- /dev/null +++ b/container-code/connector/pyproject.toml @@ -0,0 +1,32 @@ +[build-system] +requires = ["setuptools>=75"] +build-backend = "setuptools.build_meta" + +[project] +name = "hainuo-relay" +version = "0.1.0" +description = "Hainuo isolated connector and encrypted device router" +requires-python = ">=3.12" +dependencies = [ + "fastapi==0.119.1", "uvicorn==0.38.0", "httpx==0.28.1", + "redis==6.4.0", "cryptography==46.0.3", "websockets==15.0.1", + "pydantic-settings==2.11.0", + "psycopg[binary]==3.2.12", +] + +[project.optional-dependencies] +test = ["pytest==8.4.2", "pytest-asyncio==1.2.0", "fakeredis==2.32.0"] +longconn = ["lark-oapi==1.5.3"] + +[project.scripts] +hainuo-relay = "relay.cli:main" + +[tool.setuptools.packages.find] +include = ["relay*"] + +[tool.setuptools.package-data] +relay = ["web/*.html", "web/*.js", "web/*.css"] + +[tool.pytest.ini_options] +asyncio_mode = "auto" +testpaths = ["tests"] diff --git a/container-code/connector/relay/__init__.py b/container-code/connector/relay/__init__.py new file mode 100644 index 0000000..32754a7 --- /dev/null +++ b/container-code/connector/relay/__init__.py @@ -0,0 +1 @@ +"""Hainuo relay 0.1.0.""" diff --git a/container-code/connector/relay/cli.py b/container-code/connector/relay/cli.py new file mode 100644 index 0000000..fad7979 --- /dev/null +++ b/container-code/connector/relay/cli.py @@ -0,0 +1,216 @@ +import argparse +import asyncio +import json +import os +import secrets +import ssl +import uuid +from datetime import datetime, timedelta, timezone +from pathlib import Path + +import httpx +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ed25519 +from cryptography.x509.oid import NameOID + +from .crypto import issue_ca, issue_device, load_private, pem_private + + +def write_new(path, data, private=False): + path.parent.mkdir(parents=True, exist_ok=True) + with path.open("xb") as f: + f.write(data if isinstance(data, bytes) else data.encode()) + if private and os.name != "nt": + path.chmod(0o600) + + +def init_project(root: Path, hostname: str): + if (root / ".env").exists(): + raise RuntimeError(".env exists; init never overwrites credentials") + ca_key, ca = issue_ca() + signer = ed25519.Ed25519PrivateKey.generate() + write_new(root / "operator/ca.key", pem_private(ca_key), True) + write_new(root / "runtime/trust/ca.pem", ca.public_bytes(serialization.Encoding.PEM)) + write_new(root / "runtime/connector/connector.key", pem_private(signer), True) + write_new(root / "runtime/trust/connector.pub", signer.public_key().public_bytes( + serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo)) + tls_key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, hostname)]) + now = datetime.now(timezone.utc) + tls = (x509.CertificateBuilder().subject_name(name).issuer_name(name).public_key(tls_key.public_key()) + .serial_number(x509.random_serial_number()).not_valid_before(now - timedelta(minutes=5)) + .not_valid_after(now + timedelta(days=30)) + .add_extension(x509.SubjectAlternativeName([x509.DNSName(hostname)]), critical=False) + .sign(tls_key, algorithm=None)) + write_new(root / "runtime/tls/server.key", pem_private(tls_key), True) + write_new(root / "runtime/tls/server.pem", tls.public_bytes(serialization.Encoding.PEM)) + redis_secret = secrets.token_hex(32) + db_secret = secrets.token_hex(32) + env = (f"HN_ENVIRONMENT=development\nHN_PUBLIC_HOST={hostname}\nHN_PUBLIC_URL=https://{hostname}\n" + f"HN_ROUTER_URL=wss://{hostname}/device/ws\nHN_ADMIN_SECRET={secrets.token_hex(32)}\n" + f"HN_INTERNAL_SECRET={secrets.token_hex(32)}\nHN_REDIS_PASSWORD={redis_secret}\n" + f"HN_REDIS_URL=redis://:{redis_secret}@redis:6379/0\n" + f"HN_POSTGRES_PASSWORD={db_secret}\nHN_DATABASE_URL=postgresql://hainuo:{db_secret}@postgres:5432/hainuo\n" + "HN_FEISHU_ENABLED=false\nHN_FEISHU_API_BASE=https://open.feishu.cn\n" + "HN_FEISHU_AUTHORIZE_URL=https://open.feishu.cn/open-apis/authen/v1/authorize\n" + "HN_FEISHU_APP_ID=\nHN_FEISHU_APP_SECRET=\nHN_FEISHU_ENCRYPT_KEY=\n" + "HN_FEISHU_VERIFICATION_TOKEN=\nHN_PROXY_ENABLED=false\nHN_DEVICE_DOMAIN=\n") + write_new(root / ".env", env, True) + print("已生成配置、连接器密钥、设备CA及临时TLS证书;未输出凭据。") + print("operator/ca.key 仅保留在签发工作站,不要上传到ECS。临时TLS证书上线前须替换。") + + +def device_request(directory): + host = uuid.uuid4().hex + private = ed25519.Ed25519PrivateKey.generate() + write_new(directory / "device.key", pem_private(private), True) + public = private.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode() + write_new(directory / "request.json", json.dumps({"host_id": host, "public_key": public}, indent=2)) + print("已在本机生成私钥和 request.json;仅将request.json交给签发工作站。") + + +def issue(root, request_file, output): + request = json.loads(request_file.read_text(encoding="utf-8")) + host = request["host_id"] + if len(host) != 32 or any(c not in "0123456789abcdef" for c in host): + raise ValueError("invalid host identity") + ca = x509.load_pem_x509_certificate((root / "runtime/trust/ca.pem").read_bytes()) + public = serialization.load_pem_public_key(request["public_key"].encode()) + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("Ed25519 required") + cert = issue_device(load_private(root / "operator/ca.key"), ca, host, public) + pem = cert.public_bytes(serialization.Encoding.PEM) + write_new(output / "device.pem", pem) + write_new(output / "ca.pem", (root / "runtime/trust/ca.pem").read_bytes()) + write_new(output / "connector.pub", (root / "runtime/trust/connector.pub").read_bytes()) + write_new(output / "device.json", json.dumps({"host_id": host, "owner": None}, indent=2)) + write_new(output / "registration.json", json.dumps({"host_id": host, "cert": pem.decode()}, indent=2)) + print("已签发设备证书;将device.pem、ca.pem、connector.pub、device.json交回原设备。") + + +def main(): + parser = argparse.ArgumentParser(description="海诺中继部署和设备工具") + sub = parser.add_subparsers(dest="command", required=True) + p = sub.add_parser("init") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--hostname", required=True) + p = sub.add_parser("serve") + p.add_argument("role", choices=["connector", "router"]) + p.add_argument("--host", default="127.0.0.1") + p.add_argument("--port", type=int, default=8000) + p = sub.add_parser("device-request") + p.add_argument("--directory", type=Path, required=True) + p = sub.add_parser("issue-device") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--request", type=Path, required=True) + p.add_argument("--output", type=Path, required=True) + p = sub.add_parser("register") + p.add_argument("--registration", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("bind") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("device") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p.add_argument("--test-root", type=Path) + p.add_argument("--agent-url", help="Explicit loopback read-only Agent adapter endpoint") + p.add_argument("--hainuo-config", type=Path, help="Native installer-1.065 API adapter config; alternative to --agent-url") + p = sub.add_parser("hainuo-probe") + p.add_argument("--config", type=Path, required=True) + p = sub.add_parser("web-pair") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + for command in ("status", "binding-code", "dispatch", "revoke", "unbind", "reconcile"): + p = sub.add_parser(command) + p.add_argument("--url", default="http://127.0.0.1:18081") + p.add_argument("--tls-ca", type=Path) + if command in {"binding-code", "dispatch"}: + p.add_argument("--owner", required=True) + if command == "dispatch": + p.add_argument("--text", required=True) + p.add_argument("--turn-id", default=None) + if command in {"revoke", "unbind"}: + p.add_argument("--host-id", required=True) + args = parser.parse_args() + if args.command == "init": + init_project(args.root, args.hostname) + elif args.command == "serve": + import uvicorn + uvicorn.run(f"relay.{args.role}:create_app", factory=True, host=args.host, port=args.port, + workers=1, access_log=False, proxy_headers=False, + ws_max_size=262144, ws_max_queue=16, log_level="warning") + elif args.command == "device-request": + device_request(args.directory) + elif args.command == "issue-device": + issue(args.root, args.request, args.output) + elif args.command == "register": + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, follow_redirects=False, timeout=15) as client: + response = client.post(args.url.rstrip("/") + "/admin/devices", + headers={"X-Admin-Secret": secret}, + json=json.loads(args.registration.read_text(encoding="utf-8"))) + if response.status_code != 201: + raise RuntimeError(f"register failed: HTTP {response.status_code}") + print("设备登记成功。") + elif args.command == "bind": + import getpass + from .device import bind_device + code = getpass.getpass("Binding code: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + asyncio.run(bind_device(args.directory, args.url, code, tls_verify=context)) + print("绑定成功。") + elif args.command == "device": + from .device import run_device + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else None + asyncio.run(run_device(args.directory, args.url, root=args.test_root, ssl_context=context, + agent_url=args.agent_url, hainuo_config=args.hainuo_config)) + elif args.command == "hainuo-probe": + from .hainuo1065 import Hainuo1065Agent + async def probe(): + adapter = Hainuo1065Agent(args.config) + try: + print(json.dumps(await adapter.probe(), ensure_ascii=False, indent=2)) + finally: + await adapter.close() + asyncio.run(probe()) + elif args.command == "web-pair": + from .device import pair_browser + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + result = asyncio.run(pair_browser(args.directory, args.url, tls_verify=context)) + print("网页地址:" + result["url"]) + print("一次性网页配对码(120秒有效):" + result["code"]) + else: + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, timeout=200, follow_redirects=False) as client: + paths = {"status": "/admin/devices", "binding-code": "/admin/test/binding-code", + "dispatch": "/admin/test/dispatch", "reconcile": "/admin/reconcile"} + if args.command in {"revoke", "unbind"}: + if len(args.host_id) != 32 or any(c not in "0123456789abcdef" for c in args.host_id): + raise ValueError("invalid host identity") + path = f"/admin/devices/{args.host_id}/{args.command}" + else: + path = paths[args.command] + body = None + if args.command in {"binding-code", "dispatch"}: + body = {"open_id": args.owner, "text": getattr(args, "text", "binding")} + if getattr(args, "turn_id", None): + body["turn_id"] = args.turn_id + response = client.request("GET" if args.command == "status" else "POST", args.url.rstrip("/") + path, + headers={"X-Admin-Secret": secret}, json=body) + if response.status_code != 200: + raise RuntimeError(f"operation failed: HTTP {response.status_code}") + print(json.dumps(response.json(), ensure_ascii=False, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/container-code/connector/relay/client_manage.py b/container-code/connector/relay/client_manage.py new file mode 100644 index 0000000..6b543fe --- /dev/null +++ b/container-code/connector/relay/client_manage.py @@ -0,0 +1,293 @@ +"""Per-user Windows connection companion. No platform/application secrets.""" +import argparse +import asyncio +import json +import os +import secrets +import socket +import ssl +import subprocess +import sys +import time +import uuid +from pathlib import Path + +import httpx +import psutil +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey +from .crypto import load_private, load_public, pem_private, raw_public, sign, verified, verify_device +from .windows_keys import protect + +BASE = Path(__file__).resolve().parent.parent +URL = "https://10.77.76.13" + + +def save(path, value): + temp = path.with_suffix(path.suffix + ".tmp") + temp.write_text(json.dumps(value, ensure_ascii=False, indent=2), encoding="utf-8") + os.replace(temp, path) + + +def secure_directory(directory): + directory.mkdir(parents=True, exist_ok=True) + # SID avoids localized account names. Fail closed if ACL protection fails. + result = subprocess.run(["whoami", "/user", "/fo", "csv", "/nh"], capture_output=True, text=True, check=True) + import csv + sid = list(csv.reader([result.stdout.strip()]))[0][1] + subprocess.run(["icacls", str(directory), "/inheritance:r", "/grant:r", f"*{sid}:(OI)(CI)F", "*S-1-5-18:(OI)(CI)F"], + capture_output=True, check=True, creationflags=subprocess.CREATE_NO_WINDOW) + + +def prepare(directory): + secure_directory(directory) + for name in ("server.pem", "ca.pem", "connector.pub"): + target = directory / name + if not target.exists(): + target.write_bytes((BASE / "trust" / name).read_bytes()) + if not (directory / "device.json").exists(): + if (directory / "device.key.dpapi").exists() or (directory / "device.key").exists(): + raise RuntimeError("发现不完整的设备身份,请保留目录并联系管理员恢复。") + private = Ed25519PrivateKey.generate() + (directory / "device.key.dpapi").write_bytes(protect(pem_private(private))) + save(directory / "device.json", {"host_id": uuid.uuid4().hex, "owner": None}) + + +def tls(directory): + return ssl.create_default_context(cafile=str(directory / "server.pem")) + + +def import_existing(directory, source): + """Explicit migration; only public trust and this PC's identity, never app keys.""" + if source.resolve() == directory.resolve(): + raise ValueError("请选择旧设备目录。") + if any((directory / name).exists() for name in ("device.json", "device.key.dpapi", "device.key")): + raise RuntimeError("本目录已有设备身份。请使用已有身份继续,不能用导入覆盖。") + config = json.loads((source / "device.json").read_text(encoding="utf-8-sig")) + for name in ("server.pem", "ca.pem", "connector.pub"): + if (source / name).read_bytes().strip() != (BASE / "trust" / name).read_bytes().strip(): + raise ValueError("旧设备的服务信任与本安装包不符,请联系管理员。") + cert = (source / "device.pem").read_text(encoding="utf-8") + public = verify_device(cert, (source / "ca.pem").read_bytes(), config["host_id"]) + private = load_private(source / "device.key") + if raw_public(public) != raw_public(private.public_key()): + raise ValueError("设备证书与本机私钥不匹配。") + secure_directory(directory) + for name in ("device.pem", "server.pem", "ca.pem", "connector.pub"): + (directory / name).write_bytes((source / name).read_bytes()) + (directory / "device.key.dpapi").write_bytes(protect(pem_private(private))) + save(directory / "device.json", config) + # Preserve local replay journal using SQLite's consistent backup API. + if (source / "journal.sqlite").is_file(): + import sqlite3 + with sqlite3.connect(source / "journal.sqlite") as original, sqlite3.connect(directory / "journal.sqlite") as target: + original.backup(target) + return {"message": "旧设备导入成功,Work绑定已保留。请先停止旧PowerShell桥接窗口,再点击启动连接。"} + + +def enrollment(directory): + prepare(directory) + cfg = json.loads((directory / "device.json").read_text()) + if (directory / "device.pem").exists(): + verify_device((directory / "device.pem").read_text(), (directory / "ca.pem").read_bytes(), cfg["host_id"]) + return {"message": "设备已注册,可在Work获取绑定码后绑定。已有绑定可直接启动连接。"} + with httpx.Client(verify=tls(directory), trust_env=False, timeout=15) as http: + key = load_private(directory / "device.key") + proof = sign({"host_id": cfg["host_id"], "purpose": "device-enroll-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + response = http.post(URL + "/device/register", json={"host_id": cfg["host_id"], "proof": proof, + "public_key": key.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode()}) + if response.status_code not in {200, 201}: + raise RuntimeError("自动注册失败:HTTP " + str(response.status_code) + ";请确认ECS已安装1.067自动注册更新。") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt.get("status") != "registered" or receipt["host_id"] != cfg["host_id"]: + raise ValueError("注册回执身份不符") + public = verify_device(receipt["cert"], (directory / "ca.pem").read_bytes(), cfg["host_id"]) + if raw_public(public) != raw_public(key.public_key()): + raise ValueError("注册证书公钥不符") + temp = directory / "device.pem.tmp" + temp.write_text(receipt["cert"], encoding="utf-8") + os.replace(temp, directory / "device.pem") + (directory / "enrollment.json").unlink(missing_ok=True) + return {"message": "设备已自动注册,无需管理员审批。请在Work获取绑定码,填写后点击绑定并连接。"} + + +def managed_processes(directory): + """Only processes with our absolute entry path AND exact device directory.""" + state = directory / "processes.json" + if not state.exists(): + return [] + result = [] + for item in json.loads(state.read_text()).get("processes", []): + try: + proc = psutil.Process(item["pid"]) + command = proc.cmdline() + if (abs(proc.create_time() - item["created"]) < 0.1 and str(BASE / "entry.py") in command + and str(directory) in command): + result.append(proc) + except psutil.Error: + pass + return result + + +def stop(directory): + procs = managed_processes(directory) + # Supervisor owns and terminates only its own child handles in its finally block. + (directory / "stop.flag").touch() + _, alive = psutil.wait_procs(procs, timeout=8) + for proc in alive: + proc.terminate() + return {"message": "已停止本安装包启动的连接进程。手工启动的旧桥接请在原窗口按Ctrl+C停止。"} + + +def start(directory, root): + from .process_lock import DeviceProcessLock + with DeviceProcessLock(directory / "control"): + return _start(directory, root) + + +def _start(directory, root): + cfg = json.loads((directory / "device.json").read_text()) + if not cfg.get("owner") or not (directory / "device.pem").exists(): + raise RuntimeError("请先完成设备准入和Work绑定。") + if managed_processes(directory): + return {"message": "连接进程已运行,请点击状态查看。"} + (directory / "stop.flag").unlink(missing_ok=True) + log = (directory / "bridge.log").open("a", encoding="utf-8") + proc = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "supervise", "--directory", str(directory), "--root", str(root)], + stdout=log, stderr=log, creationflags=subprocess.CREATE_NO_WINDOW) + log.close() + save(directory / "processes.json", {"processes": [{"pid": proc.pid, "created": psutil.Process(proc.pid).create_time()}]}) + return {"message": "连接进程已启动;约30秒后点击状态,再到Work发送 /主机名 验证。"} + + +def supervise(directory, root): + from .process_lock import DeviceProcessLock + lockdir = directory / "supervisor" + with DeviceProcessLock(lockdir): + children = [] + def record_children(): + processes = [psutil.Process(os.getpid())] + [psutil.Process(p.pid) for p in children if p.poll() is None] + save(directory / "processes.json", {"processes": [{"pid": p.pid, "created": p.create_time()} for p in processes]}) + try: + # Check local bind availability without an HTTP connection. Windows + # filtering can time out connections to a closed port rather than + # refuse them; that must not prevent starting our own listener. + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as port_probe: + try: + if os.name == "nt": + port_probe.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1) + port_probe.bind(("127.0.0.1", 18643)) + except OSError: + raise RuntimeError("原生API端口18643被占用或不允许绑定,请先检查旧连接。") from None + native = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "native", "--directory", str(directory), "--root", str(root)]) + children.append(native) + record_children() + for _ in range(90): + if (directory / "stop.flag").exists(): + return + if native.poll() is not None: + raise RuntimeError("本机原生API启动失败,请检查安装配置。") + try: + from .hainuo1065 import Hainuo1065Agent + async def probe(): + agent = Hainuo1065Agent(directory / "hainuo-native.json") + try: + return await agent.probe() + finally: + await agent.close() + asyncio.run(probe()) + break + except Exception: + time.sleep(1) + else: + raise RuntimeError("本机原生API未就绪。") + bridge = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "bridge", "--directory", str(directory), "--root", str(root)]) + children.append(bridge) + record_children() + while not (directory / "stop.flag").exists(): + if any(proc.poll() is not None for proc in children): + raise RuntimeError("连接子进程已退出,请查看状态后重新启动。") + time.sleep(0.5) + finally: + for proc in reversed(children): + if proc.poll() is None: + proc.terminate() + try: + proc.wait(timeout=5) + except subprocess.TimeoutExpired: + proc.kill() + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("action", choices=["authorize", "import", "enroll", "bind", "start", "stop", "status", "pair", "supervise", "native", "bridge"]) + parser.add_argument("--directory", type=Path, required=True) + parser.add_argument("--root", type=Path, required=True) + args = parser.parse_args() + directory, root = args.directory.resolve(), args.root.resolve() + directory.mkdir(parents=True, exist_ok=True) + try: + if args.action == "authorize": + from .work_client import WorkClient + client = WorkClient(directory) + try: + result = client.status() + result["message"] += ("\n" + result["authorization_url"]) if result.get("authorization_url") else "" + finally: + client.close() + elif args.action == "import": + result = import_existing(directory, Path(sys.stdin.readline().strip())) + elif args.action == "enroll": + result = enrollment(directory) + elif args.action == "bind": + from .device import bind_device + code = sys.stdin.readline().strip() + if not code: + raise ValueError("请填写Work机器人新生成的绑定码。") + enrollment(directory) + stop(directory) + asyncio.run(bind_device(directory, URL, code, tls_verify=tls(directory))) + result = start(directory, root) + result["message"] = "Work绑定成功。" + result["message"] + elif args.action == "start": + result = start(directory, root) + elif args.action == "stop": + result = stop(directory) + elif args.action == "pair": + from .device import pair_browser + receipt = asyncio.run(pair_browser(directory, URL, tls_verify=tls(directory))) + result = {"message": "网页:" + receipt["url"] + "\n一次性配对码(120秒):" + receipt["code"]} + elif args.action == "status": + cfg = json.loads((directory / "device.json").read_text()) if (directory / "device.json").exists() else {} + log = directory / "bridge.log" + tail = log.read_bytes()[-6000:].decode("utf-8", errors="replace") if log.exists() else "" + result = {"message": "设备:" + cfg.get("host_id", "未申请") + "\nWork身份:" + (cfg.get("owner") or "未绑定") + + "\n连接进程:" + ("运行中(请在Work发送 /主机名 确认当前链路)" if managed_processes(directory) else "未运行") + "\n" + tail[-2500:]} + elif args.action == "supervise": + return supervise(directory, root) + elif args.action == "native": + os.environ["HN_WORK_DEVICE_DIR"] = str(directory) + import importlib.util + spec = importlib.util.spec_from_file_location("native_companion", BASE / "hainuo_native_api.py") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + module.enable_profile_tools(root, "hainuo") + key = module.prepare_environment(root, "hainuo", 18643, directory / "hainuo-native.json", enable_tools=True) + import logging + logging.disable(logging.CRITICAL) + return asyncio.run(module.serve_native(18643, key, enable_tools=True)) + else: + from .device import run_device + return asyncio.run(run_device(directory, "wss://10.77.76.13/device/ws", ssl_context=tls(directory), hainuo_config=directory / "hainuo-native.json")) + print(json.dumps(result, ensure_ascii=False)) + except KeyboardInterrupt: + pass + except Exception as error: + # Native exception bodies may include URLs or secrets; emit bounded known messages only. + message = str(error) if isinstance(error, (RuntimeError, ValueError)) else type(error).__name__ + ";请检查网络、证书、安装目录。" + print(json.dumps({"message": message[:600], "error": True}, ensure_ascii=False)) + raise SystemExit(1) from None + + +if __name__ == "__main__": + main() diff --git a/container-code/connector/relay/common.py b/container-code/connector/relay/common.py new file mode 100644 index 0000000..c112b99 --- /dev/null +++ b/container-code/connector/relay/common.py @@ -0,0 +1,61 @@ +import hashlib +import hmac +import json +import time +from fastapi import HTTPException, Request +from starlette.responses import JSONResponse + + +def require_secret(actual, expected): + if not actual or not hmac.compare_digest(actual, expected): + raise HTTPException(401, "unauthorized") + + +async def rate_limit(redis, bucket: str, limit=60, seconds=60): + # Atomic counter + expiry: a crash cannot leave a permanent bucket. + key = "hn:rate:" + hashlib.sha256(bucket.encode()).hexdigest() + ":" + str(int(time.time()) // seconds) + pipe = redis.pipeline(transaction=True) + pipe.incr(key) + pipe.expire(key, seconds * 2) + result = (await pipe.execute())[0] + if int(result) > limit: + raise HTTPException(429, "rate limited", headers={"Retry-After": str(seconds)}) + + +class BodyLimitMiddleware: + def __init__(self, app, max_body=262144): + self.app, self.max_body = app, max_body + + async def __call__(self, scope, receive, send): + if scope["type"] != "http": + return await self.app(scope, receive, send) + messages, size = [], 0 + while True: + message = await receive() + if message["type"] == "http.disconnect": + return + size += len(message.get("body", b"")) + if size > self.max_body: + return await JSONResponse({"detail": "body too large"}, status_code=413)(scope, receive, send) + messages.append(message) + if not message.get("more_body", False): + break + async def buffered(): + return messages.pop(0) if messages else await receive() + await self.app(scope, buffered, send) + + +def no_cache(response): + response.headers["Cache-Control"] = "no-store" + response.headers["X-Content-Type-Options"] = "nosniff" + response.headers["Referrer-Policy"] = "no-referrer" + return response + + +async def json_pub(redis, channel, data): + return await redis.publish(channel, json.dumps(data, separators=(",", ":"))) + + +def peer(request: Request): + # Do not trust arbitrary X-Forwarded-For; configure uvicorn trusted proxies explicitly. + return request.client.host if request.client else "unknown" diff --git a/container-code/connector/relay/config.py b/container-code/connector/relay/config.py new file mode 100644 index 0000000..c4c32ac --- /dev/null +++ b/container-code/connector/relay/config.py @@ -0,0 +1,66 @@ +from pathlib import Path +from pydantic import model_validator +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + model_config = SettingsConfigDict(env_prefix="HN_", extra="ignore", hide_input_in_errors=True) + environment: str = "production" + role: str = "connector" + redis_url: str = "redis://127.0.0.1:6379/0" + data_dir: Path = Path("runtime/connector") + database_url: str = "" + admin_secret: str = "" + internal_secret: str = "" + ca_cert: Path = Path("runtime/pki/ca.pem") + connector_key: Path = Path("runtime/pki/connector.key") + connector_public_key: Path = Path("runtime/pki/connector.pub") + public_url: str = "https://relay.example.invalid" + router_url: str = "wss://relay.example.invalid/device/ws" + device_domain: str = "" + feishu_api_base: str = "https://open.feishu.cn" + feishu_authorize_url: str = "https://open.feishu.cn/open-apis/authen/v1/authorize" + feishu_oauth_scopes: str = "" + feishu_app_id: str = "" + feishu_app_secret: str = "" + feishu_encrypt_key: str = "" + feishu_verification_token: str = "" + feishu_enabled: bool = False + oauth_enabled: bool = True + feishu_event_source: str = "longconn" + proxy_enabled: bool = False + # A fixed allowlist, never a user supplied upstream URL. + proxy_paths: str = "/open-apis/drive/v1/files,/open-apis/docx/v1/documents" + request_timeout: int = 45 + max_body: int = 262144 + max_pending: int = 1000 + max_devices: int = 1000 + max_connections: int = 1000 + max_result: int = 16000 + allow_insecure_local: bool = False + enrollment_signer_url: str = "" + + @model_validator(mode="after") + def validate_runtime(self): + if self.environment not in {"production", "test", "development"}: + raise ValueError("invalid environment") + if self.role == "connector" and min(len(self.admin_secret), len(self.internal_secret)) < 32: + raise ValueError("admin/internal secrets must each be at least 32 characters") + if self.role == "longconn" and len(self.internal_secret) < 32: + raise ValueError("internal credential must be at least 32 characters") + if self.role == "connector" and self.admin_secret == self.internal_secret: + raise ValueError("admin and internal credentials must be different") + if self.environment == "production": + if self.allow_insecure_local or not self.public_url.startswith("https://"): + raise ValueError("production requires HTTPS") + if not self.router_url.startswith("wss://"): + raise ValueError("production requires WSS") + if not self.feishu_api_base.startswith("https://"): + raise ValueError("platform API requires HTTPS") + if self.feishu_event_source not in {"longconn", "webhook"}: + raise ValueError("invalid platform event source") + if self.feishu_enabled and not all((self.feishu_app_id, self.feishu_app_secret)): + raise ValueError("platform app ID and secret are required") + if self.feishu_enabled and self.feishu_event_source == "webhook" and not all((self.feishu_encrypt_key, self.feishu_verification_token)): + raise ValueError("webhook requires encryption key and verification token") + return self diff --git a/container-code/connector/relay/connector.py b/container-code/connector/relay/connector.py new file mode 100644 index 0000000..c01f3b6 --- /dev/null +++ b/container-code/connector/relay/connector.py @@ -0,0 +1,506 @@ +import asyncio +import base64 +import contextlib +import hashlib +import hmac +import json +import re +import secrets +import sqlite3 +import time +import uuid +from contextlib import asynccontextmanager +from dataclasses import dataclass +from urllib.parse import unquote, urlsplit + +from cryptography.hazmat.primitives import hashes, padding +from cryptography.hazmat.primitives.asymmetric import x25519 +from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes +from fastapi import FastAPI, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from fastapi.exceptions import RequestValidationError +from pydantic import BaseModel, Field +from redis.asyncio import Redis + +from .common import BodyLimitMiddleware, no_cache, peer, rate_limit, require_secret +from .config import Settings +from .crypto import (fingerprint, load_private, open_envelope, raw_public, seal, + sign, verified, verify_device) +from .oauth import OAuth +from .platform import Feishu, PlatformError +from .store import Store, INTEGRITY_ERRORS + + +class Register(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + cert: str = Field(max_length=10000) + + +class Bind(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Instruction(BaseModel): + open_id: str = Field(min_length=1, max_length=128) + text: str = Field(min_length=1, max_length=8000) + # Stable identity chosen by upstream, not generated again on retries. + turn_id: str = Field(default_factory=lambda: uuid.uuid4().hex, pattern=r"^[A-Za-z0-9_-]{1,128}$") + + +@dataclass +class Pending: + future: asyncio.Future + private: object + public: object + host: str + session: str + + +class Broker: + def __init__(self, settings, redis, store): + self.s, self.r, self.store = settings, redis, store + self.signer = load_private(settings.connector_key) + self.pending: dict[str, Pending] = {} + + async def replies(self, pub): + async for message in pub.listen(): + if message["type"] != "message": + continue + try: + data = json.loads(message["data"]) + item = self.pending.get(data["turn_id"]) + if not item or item.future.done(): + continue + plain = open_envelope(data["envelope"], item.private, item.public, + host_id=item.host, session_id=item.session) + if plain["turn_id"] != data["turn_id"] or plain.get("type") != "result": + continue + result = plain.get("result", "") + if not isinstance(result, str) or len(result) > self.s.max_result: + continue + item.future.set_result({"status": "completed", "turn_id": data["turn_id"], "result": result}) + except Exception: + # Invalid packets never consume a pending result; do not log packet contents. + continue + + async def dispatch(self, owner, text, turn): + device = self.store.by_owner(owner) + if not device: + return {"status": "unavailable", "turn_id": turn, "result": "未绑定设备或设备不可用。"} + host = device["host_id"] + await rate_limit(self.r, "dispatch:" + owner, 30) + if len(self.pending) >= self.s.max_pending: + raise HTTPException(503, "too many in-flight requests") + presence = await self.r.get("hn:online:" + host) + if not presence or await self.r.exists("hn:revoked:" + host): + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线,请开机并连接网络。"} + try: + presence = json.loads(presence) + if fingerprint(presence["cert"]) != device["fingerprint"]: + raise ValueError("certificate changed") + public = verify_device(presence["cert"], self.s.ca_cert.read_bytes(), host) + proof = verified(presence["proof"], public) + if proof["host_id"] != host or proof["challenge"] != presence["challenge"]: + raise ValueError("invalid device session") + session = proof["session_id"] + except Exception: + self.store.audit(owner, "dispatch", host, "identity_rejected") + raise HTTPException(409, "device identity rejected") from None + if not self.store.begin_turn(turn, owner, host): + return {"status": "duplicate", "turn_id": turn, "result": "该请求已接收,不会重复执行。"} + private = x25519.X25519PrivateKey.generate() + future = asyncio.get_running_loop().create_future() + self.pending[turn] = Pending(future, private, public, host, session) + envelope = seal({"type": "instruction", "turn_id": turn, "owner": owner, + "text": text, "read_only": True, "channel": "im", + "reply_key": raw_public(private.public_key())}, proof["kx"], self.signer, + host_id=host, session_id=session) + key = f"hn:inflight:{host}:{turn}" + try: + await self.r.set(key, session, ex=self.s.request_timeout + 5) + count = await self.r.publish("hn:down:" + host, json.dumps({"type": "instruction", "envelope": envelope})) + if not count: + self.store.finish(turn, "offline") + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线。"} + result = await asyncio.wait_for(future, timeout=self.s.request_timeout) + self.store.finish(turn, "completed") + self.store.audit(owner, "instruction", host, "completed") + return result + except asyncio.TimeoutError: + self.store.finish(turn, "unknown") + return {"status": "unknown", "turn_id": turn, + "result": "等待结果超时,执行状态未知;不要直接重发有副作用的任务。"} + finally: + self.pending.pop(turn, None) + await self.r.delete(key) + + +def create_app(settings=None, redis=None, platform=None): + s = settings or Settings() + r = redis or Redis.from_url(s.redis_url, decode_responses=True) + store = Store(s.data_dir / "metadata.sqlite", s.database_url) + f = platform or Feishu(s) + broker = Broker(s, r, store) + oauth = OAuth(s, r, f) + oauth.store = store + background = set() + + def spawn(coro): + task = asyncio.create_task(coro) + background.add(task) + task.add_done_callback(background.discard) + # Retrieve errors without displaying possibly sensitive exception text. + task.add_done_callback(lambda t: t.exception() if not t.cancelled() else None) + + @asynccontextmanager + async def lifespan(app): + await r.ping() + for row in store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall(): + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + pub = r.pubsub() + await pub.subscribe("hn:replies") + reply_task = asyncio.create_task(broker.replies(pub)) + app.state.reply_task = reply_task + async def sweep(): + while True: + await asyncio.sleep(30) + async with oauth.lock: + oauth.prune() + sweep_task = asyncio.create_task(sweep()) + try: + yield + finally: + for task in [reply_task, sweep_task, *background]: + task.cancel() + await asyncio.gather(reply_task, sweep_task, *background, return_exceptions=True) + await pub.aclose() + await f.close() + store.close() + if redis is None: + await r.aclose() + + app = FastAPI(title="Hainuo B1 connector", lifespan=lifespan, docs_url=None, redoc_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=s.max_body) + app.include_router(oauth.router) + from .web_session import WebSessions + web_sessions = WebSessions(s, r, store, broker, f) + app.include_router(web_sessions.router) + app.state.store, app.state.broker, app.state.oauth = store, broker, oauth + app.state.platform, app.state.redis = f, r + + @app.exception_handler(RequestValidationError) + async def validation_error(request, exc): + # FastAPI's default error response includes invalid input values (possibly tokens). + return JSONResponse({"detail": "invalid request parameters"}, status_code=422) + + @app.middleware("http") + async def secure_response(request, call_next): + return no_cache(await call_next(request)) + + def admin(request): + require_secret(request.headers.get("X-Admin-Secret"), s.admin_secret) + + from .enrollment import enrollment_router + app.include_router(enrollment_router(s, r, store, broker.signer, admin)) + from .work_api import router as work_router + app.include_router(work_router(s, r, store, f)) + + @app.get("/health") + async def health(): + return {"status": "alive", "role": "connector", "version": "0.1.0"} + + @app.get("/ready") + async def ready(): + await r.ping() + store.db.execute("SELECT 1") + if not getattr(app.state, "reply_task", None) or app.state.reply_task.done(): + raise HTTPException(503, "reply subscriber unavailable") + return {"status": "ready"} + + @app.post("/admin/devices", status_code=201) + async def register(body: Register, request: Request): + admin(request) + try: + verify_device(body.cert, s.ca_cert.read_bytes(), body.host_id) + except Exception: + raise HTTPException(400, "invalid device certificate") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= s.max_devices: + raise HTTPException(503, "device capacity reached") + try: + fp = fingerprint(body.cert) + store.add_device(body.host_id, body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "device already exists") from None + await r.set("hn:device:" + body.host_id, fp) + store.audit("admin", "register", body.host_id, "success") + return {"host_id": body.host_id, "fingerprint": fp} + + @app.get("/admin/devices") + async def devices(request: Request): + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,owner,revoked FROM devices ORDER BY host_id").fetchall() + return {"devices": [{**dict(row), "online": bool(await r.exists("hn:online:" + row["host_id"]))} for row in rows]} + + @app.post("/admin/devices/{host}/revoke") + async def revoke(host: str, request: Request): + admin(request) + if not store.device(host): + raise HTTPException(404, "device not found") + # Persistent source of truth first; router block + disconnect follows. + store.revoke(host) + await r.incr("hn:web-generation:" + host) + await r.set("hn:revoked:" + host, "1") + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "revoke") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "revoked", "result": "设备已停用,结果已丢弃。"}) + store.audit("admin", "revoke", host, "success") + return {"status": "revoked"} + + @app.post("/admin/reconcile") + async def reconcile(request: Request): + """Rebuild router admission metadata after Redis recovery; no dialogue data.""" + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall() + for row in rows: + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + return {"devices": len(rows)} + + @app.post("/admin/devices/{host}/unbind") + async def admin_unbind(host: str, request: Request): + admin(request) + device = store.device(host) + if not device: + raise HTTPException(404, "device not found") + if device["owner"]: + store.unbind(device["owner"]) + await r.incr("hn:web-generation:" + host) + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "unbind") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + store.audit("admin", "unbind", host, "success") + return {"status": "unbound", "host_id": host, "revoked": bool(device["revoked"])} + + async def make_binding_code(owner): + if store.by_owner(owner): + return "你已绑定电脑,请先发送 /解绑。" + await rate_limit(r, "binding:" + owner, 5) + code = secrets.token_urlsafe(12) + await r.set("hn:bind:" + hashlib.sha256(code.encode()).hexdigest(), owner, ex=300) + return "请在本人电脑的桥接客户端输入绑定码(5分钟有效):" + code + + @app.post("/device/bind") + async def bind(body: Bind, request: Request): + await rate_limit(r, "bind:" + peer(request), 20) + device = store.device(body.host_id) + if not device or device["revoked"]: + raise HTTPException(404, "device unavailable") + try: + public = verify_device(device["cert"], s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, public) + if proof["host_id"] != body.host_id or abs(time.time() - proof["ts"]) > 90: + raise ValueError("invalid proof") + code = proof["code"] + if not isinstance(code, str) or len(code) > 128: + raise ValueError("invalid code") + except Exception: + raise HTTPException(403, "invalid device proof") from None + key = "hn:bind:" + hashlib.sha256(code.encode()).hexdigest() + owner = await r.get(key) + if not owner: + raise HTTPException(404, "binding code expired or consumed") + # GETDEL is atomic across connectors. Invalid device proofs never reach this point. + owner = await r.getdel(key) + if not owner: + raise HTTPException(404, "binding code consumed") + try: + store.bind(body.host_id, owner) + except (*INTEGRITY_ERRORS, ValueError): + raise HTTPException(409, "owner/device already bound; request new code") from None + await r.incr("hn:web-generation:" + body.host_id) + store.audit(owner, "bind", body.host_id, "success") + return sign({"host_id": body.host_id, "owner": owner, "ts": int(time.time())}, broker.signer) + + @app.post("/admin/test/binding-code") + async def test_code(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return {"message": await make_binding_code(body.open_id)} + + @app.post("/admin/test/dispatch") + async def test_dispatch(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return await broker.dispatch(body.open_id, body.text, body.turn_id) + + async def handle_event(data): + header, event = data.get("header", {}), data.get("event", {}) + if header.get("event_type") != "im.message.receive_v1": + return + message, sender = event.get("message", {}), event.get("sender", {}) + owner = sender.get("sender_id", {}).get("open_id") + message_id = message.get("message_id", "") + if (message.get("chat_type") != "p2p" or message.get("message_type") != "text" + or sender.get("sender_type") != "user" or not owner + or not re.fullmatch(r"[A-Za-z0-9_-]{1,128}", message_id)): + return + if header.get("app_id") != s.feishu_app_id: + return + try: + text = json.loads(message["content"])["text"].strip() + if not text or len(text) > 8000: + return + # Persistent receipt BEFORE any side effect. Restarts do not execute a delivery twice. + event_turn = "event_" + hashlib.sha256(message_id.encode()).hexdigest() + if not store.begin_turn(event_turn, owner, "event"): + return + if text == "/绑定": + result = await make_binding_code(owner) + elif text == "/解绑": + old = store.by_owner(owner) + store.unbind(owner) + if old: + await r.incr("hn:web-generation:" + old["host_id"]) + await r.publish("hn:kill:" + old["host_id"], "unbind") + for item in broker.pending.values(): + if item.host == old["host_id"] and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + result = "已解绑;重新绑定后请更新本机绑定信息。" + elif text == "/主机名" or text.startswith("/回显 "): + result = (await broker.dispatch(owner, text, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + else: + instruction = text if text.startswith("/助手 ") else "/助手 " + text + result = (await broker.dispatch(owner, instruction, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + await f.reply(message_id, result[:s.max_result]) + store.finish(event_turn, "replied") + except Exception: + store.audit(owner, "event", message_id, "failed_or_unknown") + + @app.post("/internal/events") + async def internal_event(request: Request): + require_secret(request.headers.get("X-Internal-Secret"), s.internal_secret) + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(await request.json())) + return {"accepted": True} + + @app.post("/feishu/event") + async def webhook(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if not s.feishu_encrypt_key or not s.feishu_verification_token: + raise HTTPException(503, "webhook credentials not configured; use selected event source") + await rate_limit(r, "webhook:" + peer(request), 600) + raw = await request.body() + timestamp, nonce, signature = (request.headers.get(x, "") for x in + ("X-Lark-Request-Timestamp", "X-Lark-Request-Nonce", "X-Lark-Signature")) + try: + if abs(time.time() - int(timestamp)) > 300 or not nonce or len(nonce) > 128: + raise ValueError("stale request") + expected = hashlib.sha256((timestamp + nonce + s.feishu_encrypt_key).encode() + raw).hexdigest() + if not hmac.compare_digest(expected, signature): + raise ValueError("bad signature") + data = json.loads(raw) + if "encrypt" in data: + cipher = base64.b64decode(data["encrypt"], validate=True) + decryptor = Cipher(algorithms.AES(hashlib.sha256(s.feishu_encrypt_key.encode()).digest()), + modes.CBC(cipher[:16])).decryptor() + padded = decryptor.update(cipher[16:]) + decryptor.finalize() + unpadder = padding.PKCS7(128).unpadder() + data = json.loads(unpadder.update(padded) + unpadder.finalize()) + token = data.get("token", data.get("header", {}).get("token", "")) + if not hmac.compare_digest(token, s.feishu_verification_token): + raise ValueError("invalid verification token") + except Exception: + raise HTTPException(403, "invalid event authentication") from None + if data.get("type") == "url_verification": + return {"challenge": data.get("challenge", "")} + # Same delivery is ACKed again; durable message_id receipt handles execution deduplication. + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(data)) + return {"code": 0} + + @app.get("/api/whoami") + async def whoami(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(r, "whoami:" + peer(request)) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await f.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "no device bound") + if not s.device_domain or not re.fullmatch(r"[a-zA-Z0-9.-]+", s.device_domain): + raise HTTPException(503, "device web domain not configured") + return {"host_id": device["host_id"], "url": f"https://h-{device['host_id']}.{s.device_domain}/", + "online": bool(await r.exists("hn:online:" + device["host_id"])), + "device_login_required": True} + + @app.get("/h5", response_class=HTMLResponse) + async def h5(): + from importlib.resources import files + return HTMLResponse(files("relay").joinpath("web/index.html").read_text(encoding="utf-8"), + headers={"Content-Security-Policy": "default-src 'self'; script-src 'self'; style-src 'self'; frame-ancestors 'self'; base-uri 'none'; form-action 'self'"}) + + @app.get("/assets/{name}") + async def asset(name: str): + from importlib.resources import files + from starlette.responses import Response + if name not in {"app.js", "style.css"}: + raise HTTPException(404, "not found") + return Response(files("relay").joinpath("web/" + name).read_bytes(), + media_type="text/javascript" if name.endswith(".js") else "text/css") + + @app.get("/proxy") + async def proxy(request: Request): + if not s.proxy_enabled or not s.feishu_enabled: + raise HTTPException(404, "not found") + auth = request.headers.get("Authorization", "") + target = request.headers.get("X-Relay-Target", "") + decoded = unquote(target) + parts = urlsplit(target) + allowed = s.proxy_paths.split(",") + if (not auth.startswith("Bearer ") or len(auth) > 8192 or len(target) > 4096 + or parts.scheme or parts.netloc or parts.fragment or target.startswith("//") + or any(x in decoded for x in ("..", "\\", "\r", "\n", "#", "%")) + or not any(parts.path == x or parts.path.startswith(x + "/") for x in allowed)): + raise HTTPException(403, "target not allowed") + try: + info = await f.user_info(auth[7:]) + await rate_limit(r, "proxy:" + info["open_id"], 60) + # Buffer bounded response; no disk/cache, redirects never followed. + async with f.http.stream("GET", target, headers={"Authorization": auth}) as response: + if response.status_code in {301, 302, 303, 307, 308}: + raise HTTPException(502, "upstream redirect refused") + chunks, size = [], 0 + async for chunk in response.aiter_bytes(): + size += len(chunk) + if size > 2 * 1024 * 1024: + raise HTTPException(502, "response too large") + chunks.append(chunk) + from starlette.responses import Response + store.audit(info["open_id"], "proxy", parts.path, str(response.status_code)) + return Response(b"".join(chunks), status_code=response.status_code, + media_type=response.headers.get("content-type", "application/json")) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + + return app diff --git a/container-code/connector/relay/crypto.py b/container-code/connector/relay/crypto.py new file mode 100644 index 0000000..dd962e5 --- /dev/null +++ b/container-code/connector/relay/crypto.py @@ -0,0 +1,140 @@ +"""Signed ephemeral X25519 envelopes. The router never receives plaintext keys.""" +import base64 +import hashlib +import json +import secrets +import time +from datetime import datetime, timedelta, timezone + +from cryptography import x509 +from cryptography.hazmat.primitives import hashes, serialization +from cryptography.hazmat.primitives.asymmetric import ed25519, x25519 +from cryptography.hazmat.primitives.ciphers.aead import AESGCM +from cryptography.hazmat.primitives.kdf.hkdf import HKDF +from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID + + +def b64(data: bytes) -> str: + return base64.urlsafe_b64encode(data).decode() + + +def unb64(data: str) -> bytes: + return base64.b64decode(data.encode(), altchars=b"-_", validate=True) + + +def canonical(data: dict) -> bytes: + return json.dumps(data, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode() + + +def raw_public(key) -> str: + return b64(key.public_bytes(serialization.Encoding.Raw, serialization.PublicFormat.Raw)) + + +def pem_private(key) -> bytes: + return key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, + serialization.NoEncryption()) + + +def load_private(path): + protected = path.with_name(path.name + ".dpapi") + if protected.is_file(): + from .windows_keys import unprotect + data = unprotect(protected.read_bytes()) + else: + data = path.read_bytes() + return serialization.load_pem_private_key(data, password=None) + + +def load_public(path): + return serialization.load_pem_public_key(path.read_bytes()) + + +def fingerprint(pem: str) -> str: + return x509.load_pem_x509_certificate(pem.encode()).fingerprint(hashes.SHA256()).hex() + + +def verify_device(pem: str, ca_pem: bytes, host_id: str): + """v1 deliberately accepts only directly issued Ed25519 device certificates.""" + cert = x509.load_pem_x509_certificate(pem.encode()) + ca = x509.load_pem_x509_certificate(ca_pem) + now = datetime.now(timezone.utc) + if not (ca.not_valid_before_utc <= now <= ca.not_valid_after_utc): + raise ValueError("expired CA") + if not ca.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("not a CA") + cert.verify_directly_issued_by(ca) + if not (cert.not_valid_before_utc <= now <= cert.not_valid_after_utc): + raise ValueError("expired certificate") + if cert.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("CA cannot be used as device") + if ExtendedKeyUsageOID.CLIENT_AUTH not in cert.extensions.get_extension_for_class(x509.ExtendedKeyUsage).value: + raise ValueError("clientAuth missing") + names = cert.extensions.get_extension_for_class(x509.SubjectAlternativeName).value + if names.get_values_for_type(x509.UniformResourceIdentifier) != [f"urn:hainuo:host:{host_id}"]: + raise ValueError("host identity mismatch") + public = cert.public_key() + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("v1 requires Ed25519 device key") + return public + + +def sign(data: dict, key) -> dict: + return {**data, "signature": b64(key.sign(canonical(data)))} + + +def verified(data: dict, key) -> dict: + body = {k: v for k, v in data.items() if k != "signature"} + key.verify(unb64(data["signature"]), canonical(body)) + return body + + +def seal(payload: dict, recipient_public: str, signer, *, host_id: str, session_id: str) -> dict: + ephemeral = x25519.X25519PrivateKey.generate() + header = {"v": 1, "host_id": host_id, "session_id": session_id, + "ts": int(time.time()), "nonce": b64(secrets.token_bytes(12)), + "ephemeral": raw_public(ephemeral.public_key())} + shared = ephemeral.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(recipient_public))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + cipher = AESGCM(key).encrypt(unb64(header["nonce"]), canonical(payload), canonical(header)) + return sign({**header, "ciphertext": b64(cipher)}, signer) + + +def open_envelope(envelope: dict, recipient_private, sender_public, *, host_id: str, session_id: str) -> dict: + body = verified(envelope, sender_public) + if body["v"] != 1 or body["host_id"] != host_id or body["session_id"] != session_id: + raise ValueError("wrong destination/session") + if abs(time.time() - body["ts"]) > 90: + raise ValueError("expired envelope") + header = {k: v for k, v in body.items() if k != "ciphertext"} + shared = recipient_private.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(body["ephemeral"]))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + plain = AESGCM(key).decrypt(unb64(body["nonce"]), unb64(body["ciphertext"]), canonical(header)) + return json.loads(plain) + + +def issue_ca(): + key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Hainuo device test CA")]) + now = datetime.now(timezone.utc) + cert = (x509.CertificateBuilder().subject_name(name).issuer_name(name) + .public_key(key.public_key()).serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=3650)) + .add_extension(x509.BasicConstraints(ca=True, path_length=0), critical=True) + .add_extension(x509.KeyUsage(False, False, False, False, False, True, True, False, False), critical=True) + .sign(key, algorithm=None)) + return key, cert + + +def issue_device(ca_key, ca_cert, host_id: str, public_key, days=30): + now = datetime.now(timezone.utc) + return (x509.CertificateBuilder() + .subject_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, host_id)])) + .issuer_name(ca_cert.subject).public_key(public_key) + .serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=days)) + .add_extension(x509.BasicConstraints(ca=False, path_length=None), critical=True) + .add_extension(x509.SubjectAlternativeName([x509.UniformResourceIdentifier(f"urn:hainuo:host:{host_id}")]), critical=False) + .add_extension(x509.ExtendedKeyUsage([ExtendedKeyUsageOID.CLIENT_AUTH]), critical=False) + .sign(ca_key, algorithm=None)) diff --git a/container-code/connector/relay/device.py b/container-code/connector/relay/device.py new file mode 100644 index 0000000..a8fe010 --- /dev/null +++ b/container-code/connector/relay/device.py @@ -0,0 +1,216 @@ +"""Reference PC bridge. No shell/PowerShell execution; local read-only diagnostics.""" +import asyncio +import contextlib +import json +import os +import secrets +import socket +import sqlite3 +import time +from pathlib import Path +from urllib.parse import urlsplit + +import httpx +from cryptography.hazmat.primitives.asymmetric import x25519 +from websockets.asyncio.client import connect + +from .crypto import (load_private, load_public, open_envelope, raw_public, seal, sign, + verified, verify_device) + + +class DeviceJournal: + def __init__(self, path): + path.parent.mkdir(parents=True, exist_ok=True) + self.db = sqlite3.connect(path) + self.db.execute("CREATE TABLE IF NOT EXISTS consumed(turn_id TEXT PRIMARY KEY, ts REAL NOT NULL)") + self.db.commit() + + def consume(self, turn): + with self.db: + return self.db.execute("INSERT OR IGNORE INTO consumed VALUES(?,?)", (turn, time.time())).rowcount == 1 + + def close(self): + self.db.close() + + +async def execute_readonly(text, root: Path | None): + if text in {"/主机名", "hostname"}: + return socket.gethostname() + if text.startswith("/回显 "): + return text[4:][:16000] + if text.startswith("/读取 "): + if root is None: + return "未配置测试文件目录。" + base = root.resolve(strict=True) + try: + path = (base / text[4:].strip()).resolve(strict=True) + if path == base or not path.is_relative_to(base) or not path.is_file(): + return "拒绝:文件不在允许的测试目录。" + if path.stat().st_size > 32768: + return "拒绝:测试文件超过32KB。" + return path.read_text(encoding="utf-8")[:16000] + except (OSError, UnicodeError, ValueError): + return "拒绝:文件不存在、不可读或不是UTF-8文本。" + return "当前桥接客户端仅开放只读诊断:/主机名、/回显 内容、/读取 相对路径;尚未接入海诺本地Agent接口。" + + +class LocalAgent: + """Explicit adapter contract, not a claim of compatibility with installer 1.065.""" + def __init__(self, url, transport=None): + parts = urlsplit(url) + if parts.scheme != "http" or parts.hostname not in {"127.0.0.1", "::1"} or parts.username or parts.password or parts.fragment: + raise ValueError("local Agent must use a fixed loopback HTTP endpoint") + self.url = url + self.http = httpx.AsyncClient(timeout=30, follow_redirects=False, transport=transport, trust_env=False) + + async def execute(self, text, owner, turn): + headers = {} + if os.environ.get("HN_LOCAL_AGENT_TOKEN"): + headers["Authorization"] = "Bearer " + os.environ["HN_LOCAL_AGENT_TOKEN"] + try: + async with self.http.stream("POST", self.url, headers=headers, + json={"text": text, "open_id": owner, "turn_id": turn, + "channel": "im", "read_only": True}) as response: + if response.status_code != 200: + return "本机Agent未返回成功结果,请查看本机服务状态。" + body = b"" + async for chunk in response.aiter_bytes(): + body += chunk + if len(body) > 65536: + return "本机Agent结果超过限制。" + result = json.loads(body)["result"] + if not isinstance(result, str): + return "本机Agent响应格式不匹配。" + return result[:16000] + except (httpx.HTTPError, ValueError, KeyError): + return "本机Agent连接失败或响应格式不匹配。" + + async def close(self): + await self.http.aclose() + + +async def bind_device(directory: Path, connector_url: str, code: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + private = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "code": code, "ts": int(time.time())}, private) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as http: + response = await http.post(connector_url.rstrip("/") + "/device/bind", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"binding failed: HTTP {response.status_code}") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt["host_id"] != config["host_id"]: + raise ValueError("wrong binding receipt") + config["owner"] = receipt["owner"] + temp = directory / "device.json.tmp" + temp.write_text(json.dumps(config, ensure_ascii=False, indent=2), encoding="utf-8") + temp.replace(directory / "device.json") + + +async def pair_browser(directory: Path, connector_url: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8-sig")) + if not config.get("owner"): + raise RuntimeError("device must be bound first") + key = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "owner": config["owner"], + "purpose": "h5-pair-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as client: + response = await client.post(connector_url.rstrip("/") + "/device/web-pair", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"browser pairing failed: HTTP {response.status_code}") + result = verified(response.json(), load_public(directory / "connector.pub")) + if result["host_id"] != config["host_id"]: + raise ValueError("wrong pairing receipt") + return result + + +async def run_device(directory: Path, router_url: str, **kwargs): + from .process_lock import DeviceProcessLock + with DeviceProcessLock(directory): + return await _run_device(directory, router_url, **kwargs) + + +async def _run_device(directory: Path, router_url: str, *, root=None, once=False, ssl_context=None, + stop_event=None, connected_event=None, agent_url=None, hainuo_config=None): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + if not config.get("owner"): + raise RuntimeError("bind device before starting") + host, owner = config["host_id"], config["owner"] + key = load_private(directory / "device.key") + connector = load_public(directory / "connector.pub") + cert = (directory / "device.pem").read_text(encoding="utf-8") + verify_device(cert, (directory / "ca.pem").read_bytes(), host) + journal = DeviceJournal(directory / "journal.sqlite") + if agent_url and hainuo_config: + raise ValueError("choose one Agent adapter") + if hainuo_config: + from .hainuo1065 import Hainuo1065Agent + agent = Hainuo1065Agent(hainuo_config) + else: + agent = LocalAgent(agent_url) if agent_url else None + stop_event = stop_event or asyncio.Event() + delay = 1 + try: + while not stop_event.is_set(): + try: + kwargs = {"ssl": ssl_context} if router_url.startswith("wss://") and ssl_context else {} + async with connect(router_url, max_size=262144, open_timeout=10, ping_interval=10, + proxy=None, **kwargs) as ws: + challenge = json.loads(await ws.recv())["challenge"] + ephemeral = x25519.X25519PrivateKey.generate() + session = secrets.token_hex(16) + proof = sign({"host_id": host, "session_id": session, "challenge": challenge, + "kx": raw_public(ephemeral.public_key()), "ts": int(time.time())}, key) + await ws.send(json.dumps({"host_id": host, "cert": cert, "proof": proof})) + accepted = json.loads(await ws.recv()) + if accepted.get("type") != "connected": + raise RuntimeError("device connection rejected") + print("设备连接成功,已绑定本人海能Work身份。", flush=True) + if connected_event: + connected_event.set() + delay = 1 + while not stop_event.is_set(): + try: + raw = await asyncio.wait_for(ws.recv(), timeout=1) + except asyncio.TimeoutError: + continue + data = json.loads(raw) + payload = open_envelope(data["envelope"], ephemeral, connector, + host_id=host, session_id=session) + if payload.get("owner") != owner or payload.get("type") != "instruction": + continue + turn = payload["turn_id"] + if not isinstance(turn, str) or len(turn) > 128: + continue + if not journal.consume(turn): + result = "该请求已处理,拒绝重复执行。" + elif payload.get("read_only") is not True or payload.get("channel") != "im": + result = "拒绝:通道或操作类型不受支持。" + else: + if agent and payload["text"].startswith("/助手 "): + result = await agent.execute(payload["text"][4:], owner, turn) + else: + result = await execute_readonly(payload["text"], root) + reply = seal({"type": "result", "turn_id": turn, "result": result}, + payload["reply_key"], key, host_id=host, session_id=session) + await ws.send(json.dumps({"type": "reply", "turn_id": turn, "envelope": reply})) + if once: + return + except asyncio.CancelledError: + raise + except Exception as error: + if once: + raise + from websockets.exceptions import ConnectionClosed + if isinstance(error, ConnectionClosed) and error.rcvd and error.rcvd.code == 1008: + print("设备被中继拒绝:请检查是否有另一桥接进程、设备准入或证书状态。", flush=True) + # No payloads/URLs/credentials in logs, bounded reconnect backoff + jitter. + print("设备连接中断,等待重连。", flush=True) + with contextlib.suppress(asyncio.TimeoutError): + await asyncio.wait_for(stop_event.wait(), timeout=delay + secrets.randbelow(1000) / 1000) + delay = min(delay * 2, 30) + finally: + journal.close() + if agent: + await agent.close() diff --git a/container-code/connector/relay/enrollment.py b/container-code/connector/relay/enrollment.py new file mode 100644 index 0000000..b4ac77f --- /dev/null +++ b/container-code/connector/relay/enrollment.py @@ -0,0 +1,178 @@ +"""Device registration with key possession proof; Work binding is separate.""" +import json +import secrets +import time +import httpx + +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey +from fastapi import APIRouter, HTTPException, Request +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .crypto import verified, sign, raw_public, verify_device, fingerprint +from .web_session import digest +from fastapi.responses import JSONResponse + + +def validate_enrollment(body): + try: + key = serialization.load_pem_public_key(body.public_key.encode()) + if not isinstance(key, Ed25519PublicKey): + raise ValueError() + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["purpose"] != "device-enroll-v1" + or abs(time.time() - proof["ts"]) > 90 or len(proof["nonce"]) != 32): + raise ValueError() + return key + except Exception: + raise HTTPException(403, "invalid enrollment proof") from None + + +async def request_certificate(settings, body): + async with httpx.AsyncClient(trust_env=False, timeout=15, follow_redirects=False) as http: + response = await http.post(settings.enrollment_signer_url.rstrip("/") + "/issue", + headers={"X-Internal-Secret": settings.internal_secret}, + json=body.model_dump()) + response.raise_for_status() + return response.json()["cert"] + + +class EnrollmentRequest(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + public_key: str = Field(max_length=1024) + proof: dict + + +class EnrollmentApproval(BaseModel): + cert: str = Field(max_length=10000) + + +def enrollment_router(settings, redis, store, signer, admin): + router = APIRouter() + + @router.post("/device/register") + async def automatic_registration(body: EnrollmentRequest, request: Request): + # No human approval: eligibility follows intranet access plus Work app scope. + # Proof/TLS/limits protect identity and resources, and do not add user approval. + await rate_limit(redis, "auto-register-ip:" + peer(request), 120) + key = validate_enrollment(body) + await rate_limit(redis, "auto-register-host:" + body.host_id, 30) + existing = store.device(body.host_id) + + def check_existing(device): + if device["revoked"]: + raise HTTPException(403, "device revoked") + try: + registered = verify_device(device["cert"], settings.ca_cert.read_bytes(), body.host_id) + if raw_public(registered) != raw_public(key): + raise ValueError() + except Exception: + raise HTTPException(409, "existing device identity mismatch or certificate expired") from None + return device["cert"] + + if existing: + cert = check_existing(existing) + else: + if not settings.enrollment_signer_url: + raise HTTPException(503, "automatic registration is not enabled; update ECS first") + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= settings.max_devices: + raise HTTPException(503, "device capacity reached") + await rate_limit(redis, "auto-register-total", settings.max_pending, 3600) + try: + cert = await request_certificate(settings, body) + issued = verify_device(cert, settings.ca_cert.read_bytes(), body.host_id) + if raw_public(issued) != raw_public(key): + raise ValueError() + except Exception: + raise HTTPException(503, "certificate issuer unavailable or invalid response") from None + from .store import INTEGRITY_ERRORS + try: + store.add_device(body.host_id, cert, fingerprint(cert)) + except INTEGRITY_ERRORS: + # A retry/concurrent request must return the original certificate. + cert = check_existing(store.device(body.host_id)) + store.audit("device", "automatic_registration", body.host_id, "success") + await redis.set("hn:device:" + body.host_id, fingerprint(cert)) + return no_cache(JSONResponse(sign({"status": "registered", "host_id": body.host_id, + "cert": cert}, signer), status_code=201 if not existing else 200)) + + @router.post("/device/enroll") + async def request_enrollment(body: EnrollmentRequest, request: Request): + await rate_limit(redis, "enroll-ip:" + peer(request), 10) + validate_enrollment(body) + await rate_limit(redis, "enroll-host:" + body.host_id, 3, 3600) + if store.device(body.host_id): + raise HTTPException(409, "device already registered; recover its original certificate") + # Global rolling capacity bound; prevents anonymous request-table exhaustion. + await rate_limit(redis, "enroll-global", settings.max_pending, 1800) + identifier, claim_secret = secrets.token_hex(16), secrets.token_urlsafe(32) + record = {"host_id": body.host_id, "public_key": body.public_key, + "claim_hash": digest(claim_secret), "created": time.time(), "status": "pending"} + await redis.set("hn:enroll:" + identifier, json.dumps(record), ex=1800) + return no_cache(JSONResponse(sign({"request_id": identifier, "claim_secret": claim_secret, + "host_id": body.host_id, "expires_in": 1800}, signer), status_code=201)) + + @router.get("/device/enroll/{identifier}") + async def claim(identifier: str, request: Request): + await rate_limit(redis, "enroll-poll:" + peer(request), 60) + data = await redis.get("hn:enroll:" + identifier) + if not data: + raise HTTPException(404, "request expired") + record = json.loads(data) + import hmac + if not hmac.compare_digest(record["claim_hash"], digest(request.headers.get("X-Enrollment-Secret", ""))): + raise HTTPException(404, "request unavailable") + if record["status"] == "pending": + return no_cache(JSONResponse({"status": "pending"}, status_code=202)) + device = store.device(record["host_id"]) + if not device or device["revoked"]: + raise HTTPException(403, "device unavailable") + # Recoverable for its original claimant for the request TTL; public certificate only. + return no_cache(JSONResponse(sign({"status": "approved", "host_id": record["host_id"], + "request_id": identifier, "cert": device["cert"]}, signer))) + + @router.get("/admin/enrollments/{identifier}") + async def inspect(identifier: str, request: Request): + admin(request) + encoded = await redis.get("hn:enroll:" + identifier) + if not encoded: + raise HTTPException(404, "request expired") + record = json.loads(encoded) + return {k: v for k, v in record.items() if k != "claim_hash"} + + @router.post("/admin/enrollments/{identifier}/approve") + async def approve(identifier: str, body: EnrollmentApproval, request: Request): + admin(request) + encoded = await redis.get("hn:enroll:" + identifier) + if not encoded: + raise HTTPException(404, "request expired") + record = json.loads(encoded) + try: + public = verify_device(body.cert, settings.ca_cert.read_bytes(), record["host_id"]) + requested = serialization.load_pem_public_key(record["public_key"].encode()) + if raw_public(public) != raw_public(requested): + raise ValueError() + except Exception: + raise HTTPException(400, "certificate does not match enrollment") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= settings.max_devices: + raise HTTPException(503, "device capacity reached") + fp = fingerprint(body.cert) + existing = store.device(record["host_id"]) + if existing and (existing["revoked"] or existing["fingerprint"] != fp): + raise HTTPException(409, "device conflict") + if not existing: + from .store import INTEGRITY_ERRORS + try: + store.add_device(record["host_id"], body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "concurrent device registration") from None + await redis.set("hn:device:" + record["host_id"], fp) + record["status"] = "approved" + remaining = await redis.ttl("hn:enroll:" + identifier) + if remaining < 1: + raise HTTPException(409, "request expired during approval") + await redis.set("hn:enroll:" + identifier, json.dumps(record), ex=remaining) + store.audit("admin", "enrollment_approved", record["host_id"], "success") + return {"status": "approved", "host_id": record["host_id"]} + + return router diff --git a/container-code/connector/relay/enrollment_signer.py b/container-code/connector/relay/enrollment_signer.py new file mode 100644 index 0000000..6089d02 --- /dev/null +++ b/container-code/connector/relay/enrollment_signer.py @@ -0,0 +1,43 @@ +"""Internal automatic CA issuer. No published host port, no application credentials.""" +import os +from pathlib import Path + +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from fastapi import FastAPI, Request +from .common import BodyLimitMiddleware, require_secret +from .crypto import load_private, issue_device, raw_public, verify_device +from .enrollment import EnrollmentRequest, validate_enrollment + + +def create_app(*, ca_key_path=None, ca_cert_path=None, secret=None): + key = load_private(Path(ca_key_path or os.environ.get("HN_ISSUER_CA_KEY", "/issuer/ca.key"))) + ca_bytes = Path(ca_cert_path or os.environ.get("HN_ISSUER_CA_CERT", "/trust/ca.pem")).read_bytes() + ca = x509.load_pem_x509_certificate(ca_bytes) + if raw_public(key.public_key()) != raw_public(ca.public_key()): + raise ValueError("issuer CA key mismatch") + token = secret or os.environ.get("HN_INTERNAL_SECRET", "") + if len(token) < 32: + raise ValueError("issuer requires internal credential") + app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=16384) + + @app.get("/ready") + async def ready(): + return {"status": "ready"} + + @app.post("/issue") + async def issue(body: EnrollmentRequest, request: Request): + require_secret(request.headers.get("X-Internal-Secret"), token) + public = validate_enrollment(body) + cert = issue_device(key, ca, body.host_id, public).public_bytes(serialization.Encoding.PEM).decode() + verify_device(cert, ca_bytes, body.host_id) + return {"cert": cert} + + return app + + +if __name__ == "__main__": + import uvicorn + uvicorn.run("relay.enrollment_signer:create_app", factory=True, host="0.0.0.0", port=8000, + workers=1, access_log=False, proxy_headers=False, log_level="warning") diff --git a/container-code/connector/relay/hainuo1065.py b/container-code/connector/relay/hainuo1065.py new file mode 100644 index 0000000..6f7735b --- /dev/null +++ b/container-code/connector/relay/hainuo1065.py @@ -0,0 +1,107 @@ +"""Adapter for the native API actually found in installer 1.065. + +Loopback only, independent API key, no platform secrets, no redirects or retries. +The included native launcher offers only text inference with no tools. +""" +import hashlib +import json +import re +from pathlib import Path +from urllib.parse import urlsplit + +import httpx + + +class NativeAPIError(RuntimeError): + """Contains a sanitized status only, never response body or credentials.""" + + +def read_native_config(path): + path = Path(path) + data = json.loads(path.read_text(encoding="utf-8-sig")) + url = data.get("url", "") + parts = urlsplit(url) + if (parts.scheme != "http" or parts.hostname != "127.0.0.1" or not parts.port + or parts.username or parts.password or parts.query or parts.fragment + or parts.path not in {"", "/"}): + raise ValueError("native API must be a fixed IPv4 loopback HTTP base URL") + if not isinstance(data.get("api_key"), str) or len(data["api_key"]) < 32: + raise ValueError("native API requires a dedicated key of at least 32 characters") + if data.get("mode") not in {"text-only", "profile-tools"}: + raise ValueError("unsupported native API mode") + data["url"] = url.rstrip("/") + return data + + +class Hainuo1065Agent: + def __init__(self, config_path, transport=None): + self.config = read_native_config(config_path) + self.http = httpx.AsyncClient(base_url=self.config["url"], timeout=120, + follow_redirects=False, trust_env=False, transport=transport) + self.headers = {"Authorization": "Bearer " + self.config["api_key"]} + + async def _json(self, method, path, **kwargs): + try: + async with self.http.stream(method, path, headers={**self.headers, **kwargs.pop("headers", {})}, **kwargs) as response: + if response.status_code != 200: + raise NativeAPIError(f"native API HTTP {response.status_code}") + body = bytearray() + async for chunk in response.aiter_bytes(): + body.extend(chunk) + if len(body) > 262144: + raise NativeAPIError("native API response exceeds limit") + data = json.loads(body) + if not isinstance(data, dict): + raise NativeAPIError("native API response is not an object") + return data + except (httpx.HTTPError, ValueError): + raise NativeAPIError("native API connection/JSON failure") from None + + async def probe(self): + health = await self._json("GET", "/health") + if health.get("platform") != "hermes-agent" or health.get("status") != "ok": + raise NativeAPIError("endpoint is not the native Hermes API") + capabilities = await self._json("GET", "/v1/capabilities") + policy = capabilities.get("hainuo_relay_policy", {}) + # A stock unrestricted API endpoint is deliberately not sufficient. + expected_mode = self.config["mode"] + expected_tools = "profile" if expected_mode == "profile-tools" else "disabled" + if policy != {"mode": expected_mode, "tools": expected_tools, "contract": "hainuo-1065-v1"}: + raise NativeAPIError("native API policy missing; start the supplied restricted launcher") + models = await self._json("GET", "/v1/models") + model = self.config.get("model") + available = models.get("data") + if not isinstance(available, list) or not available or not all(isinstance(x, dict) and isinstance(x.get("id"), str) for x in available): + raise NativeAPIError("native model list invalid") + if model and model not in {x["id"] for x in available}: + raise NativeAPIError("configured model alias unavailable") + self.model = model or available[0]["id"] + return {"status": "ready", "platform": "hermes-agent", "model": self.model, + "mode": expected_mode, "version": health.get("version", "unknown")} + + async def execute(self, text, owner, turn): + try: + # Recheck the actual local service before each inference, including reconnects. + await self.probe() + digest = hashlib.sha256((owner + "\0" + turn).encode()).hexdigest() + data = await self._json("POST", "/v1/chat/completions", + headers={"Idempotency-Key": "hn-" + digest}, + json={"model": self.model, "stream": False, + "messages": [{"role": "user", "content": text}]}) + choices = data.get("choices") + if not isinstance(choices, list) or not choices or not isinstance(choices[0], dict): + raise NativeAPIError("native completion choices missing") + choice = choices[0] + message = choice.get("message", {}) + if (choice.get("finish_reason") != "stop" or message.get("tool_calls") + or not isinstance(message.get("content"), str) + or data.get("hermes", {}).get("failed") + or data.get("hermes", {}).get("partial")): + raise NativeAPIError("native completion incomplete or unexpected tool call") + return message["content"][:16000] + except NativeAPIError as error: + # Safe generated error strings only; never echo upstream model/API errors. + return "海诺原生接口未完成请求:" + str(error) + "。未自动重试。" + + async def close(self): + await self.http.aclose() diff --git a/container-code/connector/relay/longconn.py b/container-code/connector/relay/longconn.py new file mode 100644 index 0000000..6d7be70 --- /dev/null +++ b/container-code/connector/relay/longconn.py @@ -0,0 +1,83 @@ +"""Optional official SDK event source, private platform domain explicitly configured.""" +import json +import logging +import os +import threading +import time + +import httpx +import redis +from .config import Settings + + +def disable_proxy_environment(): + """This dedicated SDK worker must connect directly, including discovery and WS.""" + for key in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "http_proxy", "https_proxy", "all_proxy"): + os.environ.pop(key, None) + os.environ["NO_PROXY"] = "*" + os.environ["no_proxy"] = "*" + + +def main(): + disable_proxy_environment() + import lark_oapi as lark + s = Settings(role="longconn") + if not s.feishu_enabled or s.feishu_event_source != "longconn": + raise RuntimeError("platform is disabled") + # The SDK may log websocket URLs, tokens or event bodies. Disable its logging entirely. + logging.disable(logging.CRITICAL) + r = redis.Redis.from_url(s.redis_url) + lease = r.lock("hn:longconn:leader", timeout=30, thread_local=False) + if not lease.acquire(blocking=False): + raise RuntimeError("another long-connection consumer owns the lease") + url = os.environ.get("HN_CONNECTOR_INTERNAL_URL", "http://connector:8000") + client = httpx.Client(base_url=url, timeout=10, follow_redirects=False, trust_env=False) + done = threading.Event() + + def maintain_lease(): + while not done.wait(8): + try: + lease.extend(30, replace_ttl=True) + except Exception: + # Fail closed. Continuing after lease loss could create two consumers. + os._exit(2) + + threading.Thread(target=maintain_lease, daemon=True).start() + + def received(event): + data = json.loads(lark.JSON.marshal(event)) + response = client.post("/internal/events", headers={"X-Internal-Secret": s.internal_secret}, json=data) + if response.status_code != 200: + raise RuntimeError("connector did not accept event") + print(json.dumps({"event": "platform_event_accepted", "type": data.get("header", {}).get("event_type")}), flush=True) + + handler = lark.EventDispatcherHandler.builder("", "").register_p2_im_message_receive_v1(received).build() + class ObservableClient(lark.ws.Client): + async def _connect(self): + try: + await super()._connect() + if self._conn is not None: + print(json.dumps({"event": "platform_connected", "api_host": s.feishu_api_base}), flush=True) + except Exception as error: + print(json.dumps({"event": "platform_connect_failed", "error_type": type(error).__name__}), flush=True) + raise + + try: + # domain is an actual supported SDK argument; no public-cloud-domain monkey patch. + ObservableClient(s.feishu_app_id, s.feishu_app_secret, event_handler=handler, + domain=s.feishu_api_base.rstrip("/"), log_level=lark.LogLevel.ERROR).start() + finally: + done.set() + client.close() + try: + lease.release() + except redis.exceptions.LockError: + pass + + +if __name__ == "__main__": + try: + main() + except Exception as error: + print(json.dumps({"event": "platform_consumer_stopped", "error_type": type(error).__name__}), flush=True) + raise SystemExit(1) from None diff --git a/container-code/connector/relay/oauth.py b/container-code/connector/relay/oauth.py new file mode 100644 index 0000000..1b35a14 --- /dev/null +++ b/container-code/connector/relay/oauth.py @@ -0,0 +1,144 @@ +import asyncio +import hashlib +import hmac +import secrets +import time +from dataclasses import dataclass +from urllib.parse import urlencode + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .platform import PlatformError + + +@dataclass +class Transaction: + secret_hash: str + expires: float + status: str = "waiting" + result: dict | None = None + owner: str = "" + + +class Empty(BaseModel): + host_id: str = "" + proof: dict = Field(default_factory=dict) + + +class Refresh(BaseModel): + refresh_token: str = Field(min_length=1, max_length=4096) + + +class OAuth: + """A single connector process owns transactions and tokens in memory only.""" + def __init__(self, settings, redis, platform): + self.settings, self.redis, self.platform = settings, redis, platform + self.pending: dict[str, Transaction] = {} + self.lock = asyncio.Lock() + self.router = APIRouter() + self.router.add_api_route("/oauth/start", self.start, methods=["POST"], status_code=201) + self.router.add_api_route("/oauth/cb", self.callback, methods=["GET"]) + self.router.add_api_route("/oauth/claim", self.claim, methods=["GET"]) + self.router.add_api_route("/oauth/pending", self.status, methods=["GET"]) + self.router.add_api_route("/oauth/refresh", self.refresh, methods=["POST"]) + + def prune(self): + now = time.monotonic() + for state in list(self.pending): + if self.pending[state].expires <= now: + del self.pending[state] + + def authenticated(self, state, secret): + self.prune() + item = self.pending.get(state) + digest = hashlib.sha256((secret or "").encode()).hexdigest() + if not item or not hmac.compare_digest(item.secret_hash, digest): + raise HTTPException(404, "transaction not found") + return item + + async def start(self, body: Empty, request: Request): + if not self.settings.feishu_enabled or not self.settings.oauth_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.redis, "oauth:start:" + peer(request), 20) + owner = "" + if body.host_id: + from .work_api import device_owner + owner = await device_owner(self.settings, self.redis, self.store, body.host_id, + body.proof, "work-oauth-v1", {}) + async with self.lock: + self.prune() + if len(self.pending) >= self.settings.max_pending: + raise HTTPException(503, "too many pending transactions") + state, secret = secrets.token_urlsafe(32), secrets.token_urlsafe(32) + self.pending[state] = Transaction(hashlib.sha256(secret.encode()).hexdigest(), time.monotonic() + 600, owner=owner) + redirect = self.settings.public_url.rstrip("/") + "/oauth/cb" + parameters = {"app_id": self.settings.feishu_app_id, "redirect_uri": redirect, "state": state} + if self.settings.feishu_oauth_scopes: + parameters["scope"] = self.settings.feishu_oauth_scopes + url = self.settings.feishu_authorize_url + "?" + urlencode(parameters) + return no_cache(JSONResponse({"state": state, "claim_secret": secret, "authorization_url": url, + "redirect_uri": redirect, "expires_in": 600, + "code_ttl": 60, "poll_interval": 2}, status_code=201)) + + async def callback(self, request: Request, state: str = "", code: str = "", error: str = ""): + await rate_limit(self.redis, "oauth:cb:" + peer(request)) + async with self.lock: + self.prune() + item = self.pending.get(state) + if error and item and item.status == "waiting": + item.status = "failed" + return no_cache(HTMLResponse("

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

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

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

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

连接你的
工作电脑。

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

+
01 / 连接本人设备

输入电脑上的配对码

+

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

+
+
使用海能Work授权

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

+ +

正在检查网页会话…

+
PC需保持开机、联网,并运行原生API和桥接端。试点网页由中继在内存中转发正文,不保存聊天记录;当前页关闭后消息不保留。
+
diff --git a/container-code/connector/relay/web/style.css b/container-code/connector/relay/web/style.css new file mode 100644 index 0000000..a33d19c --- /dev/null +++ b/container-code/connector/relay/web/style.css @@ -0,0 +1,3 @@ +:root{color-scheme:light;font-family:system-ui,-apple-system,"Segoe UI","Microsoft YaHei",sans-serif;color:#17372c;background:#f2f4ed}*{box-sizing:border-box}body{margin:0}main{max-width:820px;margin:0 auto;padding:60px 26px}.mark{font-size:13px;letter-spacing:.16em;color:#557764;font-weight:700}h1{font-size:clamp(42px,8vw,70px);letter-spacing:-.05em;line-height:1.12;font-weight:650;margin:40px 0 22px}.intro{font-size:17px;line-height:1.8;color:#56675a;max-width:520px}.card{background:#fff;border:1px solid #dbe3d8;border-radius:20px;padding:30px;margin:40px 0 24px;box-shadow:0 12px 44px #18372c06}.step{font-size:12px;letter-spacing:.1em;color:#6d806c}h2{font-size:24px;margin:16px 0}.card p{line-height:1.8;color:#5b6c60}button{background:#214e3a;color:white;border:0;border-radius:10px;font:inherit;font-weight:600;padding:16px 20px;width:100%;text-align:left;cursor:pointer;margin-top:16px}button span{float:right}button:disabled{opacity:.5;cursor:wait}button:focus-visible,a:focus-visible{outline:3px solid #d0a349;outline-offset:4px}#status{font-size:13px;margin-bottom:0}#open{display:block;margin-top:22px;color:#214e3a;font-weight:600}#open[hidden]{display:none}footer{color:#768373;font-size:12px;line-height:1.8}@media(max-width:480px){main{padding-top:36px}.card{padding:22px}h1{margin-top:28px}} + +[hidden]{display:none!important}.badge{float:right;font-size:11px;letter-spacing:0;background:#e4ebdf;border-radius:20px;padding:6px 10px}label{display:block;font-size:13px;color:#526c5b;margin:12px 0 8px}input,textarea{font:inherit;width:100%;border:1px solid #ccd8ce;border-radius:10px;padding:14px;color:#17372c;background:#fafcf8;resize:vertical}input:focus,textarea:focus{outline:2px solid #6c987b;outline-offset:2px}details{margin-top:24px;font-size:13px;color:#587561}summary{cursor:pointer}.chat-head{display:flex;align-items:center;justify-content:space-between;gap:20px}.secondary{width:auto;background:#edf2e9;color:#31523f;padding:9px 12px;font-size:12px;margin:0}.hint{font-size:13px}#messages{display:flex;flex-direction:column;gap:12px;margin:22px 0;max-height:420px;overflow:auto}.message{padding:14px 16px;border-radius:12px;line-height:1.8;white-space:pre-wrap;overflow-wrap:anywhere;font-size:15px}.message.user{background:#e5eee2;margin-left:36px}.message.assistant{background:#f2f5ef;margin-right:24px}.message.notice{background:#fbf4e5;color:#735c2e}#status{font-size:13px;color:#5f745f;line-height:1.8}#chat-panel h2{font-size:20px}footer{max-width:640px} diff --git a/container-code/connector/relay/web_session.py b/container-code/connector/relay/web_session.py new file mode 100644 index 0000000..1ac9adf --- /dev/null +++ b/container-code/connector/relay/web_session.py @@ -0,0 +1,183 @@ +"""Pilot H5 pairing and conversation. Credentials never appear in URLs.""" +import hashlib +import json +import secrets +import time +from urllib.parse import urlsplit + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import JSONResponse +from pydantic import BaseModel, Field + +from .common import no_cache, peer, rate_limit +from .crypto import sign, verified, verify_device +from .platform import PlatformError + +COOKIE = "hn_web_session" + + +def digest(value): + return hashlib.sha256(value.encode()).hexdigest() + + +def origin_tuple(value): + try: + parts = urlsplit(value) + if parts.scheme not in {"https", "http"} or not parts.hostname or parts.username or parts.password: + return None + return parts.scheme, parts.hostname.lower(), parts.port or (443 if parts.scheme == "https" else 80) + except ValueError: + return None + + +class PairProof(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Claim(BaseModel): + code: str = Field(min_length=12, max_length=128) + + +class Chat(BaseModel): + text: str = Field(min_length=1, max_length=8000) + request_id: str = Field(pattern=r"^[a-f0-9]{32}$") + + +class WebSessions: + def __init__(self, settings, redis, store, broker, platform): + self.s, self.r, self.store, self.broker, self.platform = settings, redis, store, broker, platform + self.router = APIRouter() + for path, method, handler in [ + ("/device/web-pair", "POST", self.device_pair), + ("/web/claim", "POST", self.claim), + ("/web/session", "GET", self.session), + ("/web/platform-session", "POST", self.platform_login), + ("/web/chat", "POST", self.chat), + ("/web/logout", "POST", self.logout), + ]: + self.router.add_api_route(path, handler, methods=[method]) + + def check_origin(self, request): + actual = origin_tuple(request.headers.get("Origin", "")) + if not actual or actual != origin_tuple(self.s.public_url): + raise HTTPException(403, "same-origin browser request required") + + async def valid_device(self, record): + device = self.store.device(record["host_id"]) + if (not device or device["revoked"] or device["owner"] != record["owner"] + or device["fingerprint"] != record["fingerprint"]): + raise HTTPException(401, "device binding changed; pair again") + generation = await self.r.get("hn:web-generation:" + record["host_id"]) or "0" + if record.get("generation") != generation: + raise HTTPException(401, "device binding changed; pair again") + return device + + async def device_pair(self, body: PairProof, request: Request): + await rate_limit(self.r, "web-pair-ip:" + peer(request), 20) + device = self.store.device(body.host_id) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "active bound device required") + try: + key = verify_device(device["cert"], self.s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["owner"] != device["owner"] + or proof["purpose"] != "h5-pair-v1" or abs(time.time() - proof["ts"]) > 90 + or not isinstance(proof["nonce"], str) or len(proof["nonce"]) != 32): + raise ValueError("invalid proof") + except Exception: + raise HTTPException(403, "invalid device proof") from None + replay = "hn:web-pair-proof:" + digest(body.host_id + proof["nonce"]) + if not await self.r.set(replay, "used", nx=True, ex=180): + raise HTTPException(409, "pairing request already used") + await rate_limit(self.r, "web-pair-device:" + body.host_id, 5) + code = secrets.token_urlsafe(18) + record = {"host_id": body.host_id, "owner": device["owner"], "fingerprint": device["fingerprint"], + "generation": await self.r.get("hn:web-generation:" + body.host_id) or "0"} + await self.r.set("hn:web-code:" + digest(code), json.dumps(record), ex=120) + self.store.audit(device["owner"], "web_pair_issued", body.host_id, "success") + return no_cache(JSONResponse(sign({"host_id": body.host_id, "code": code, "expires_in": 120, + "url": self.s.public_url.rstrip("/") + "/h5"}, self.broker.signer))) + + async def new_session(self, record): + token = secrets.token_urlsafe(32) + record = {k: record[k] for k in ("host_id", "owner", "fingerprint", "generation")} + await self.r.set("hn:web-session:" + digest(token), json.dumps(record), ex=1800) + response = no_cache(JSONResponse({"status": "paired", "host_id": record["host_id"], "expires_in": 1800})) + response.set_cookie(COOKIE, token, max_age=1800, secure=True, httponly=True, samesite="strict", path="/") + return response + + async def claim(self, body: Claim, request: Request): + self.check_origin(request) + await rate_limit(self.r, "web-claim:" + peer(request), 20) + key = "hn:web-code:" + digest(body.code.strip()) + encoded = await self.r.get(key) + if not encoded: + raise HTTPException(404, "code expired or consumed") + await self.valid_device(json.loads(encoded)) + encoded = await self.r.getdel(key) + if not encoded: + raise HTTPException(404, "code already consumed") + record = json.loads(encoded) + await self.valid_device(record) + self.store.audit(record["owner"], "web_pair_claimed", record["host_id"], "success") + return await self.new_session(record) + + async def authenticated(self, request): + token = request.cookies.get(COOKIE, "") + if not token or len(token) > 128: + raise HTTPException(401, "pair this browser first") + encoded = await self.r.get("hn:web-session:" + digest(token)) + if not encoded: + raise HTTPException(401, "browser session expired") + record = json.loads(encoded) + await self.valid_device(record) + return record + + async def session(self, request: Request): + record = await self.authenticated(request) + return no_cache(JSONResponse({"authenticated": True, "host_id": record["host_id"], + "online": bool(await self.r.exists("hn:online:" + record["host_id"])), + "mode": "pilot-text-only", "relay_sees_content": True})) + + async def platform_login(self, request: Request): + self.check_origin(request) + if not self.s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.r, "web-platform:" + peer(request), 20) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await self.platform.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = self.store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "bind your device first") + return await self.new_session({**device, "generation": await self.r.get("hn:web-generation:" + device["host_id"]) or "0"}) + + async def chat(self, body: Chat, request: Request): + self.check_origin(request) + record = await self.authenticated(request) + await rate_limit(self.r, "web-chat:" + record["owner"], 20) + text = body.text.strip() + if not text: + raise HTTPException(422, "empty message") + # PC accepts only its bound owner and the existing IM read-only channel. + if not (text == "/主机名" or text.startswith(("/回显 ", "/读取 ", "/助手 "))): + text = "/助手 " + text + turn = "web_" + digest(record["owner"] + "\0" + body.request_id) + result = await self.broker.dispatch(record["owner"], text, turn) + # Revoke/unbind during a long model call invalidates the browser immediately. + await self.authenticated(request) + return no_cache(JSONResponse(result)) + + async def logout(self, request: Request): + self.check_origin(request) + token = request.cookies.get(COOKIE, "") + if token and len(token) <= 128: + await self.r.delete("hn:web-session:" + digest(token)) + response = no_cache(JSONResponse({"status": "logged_out"})) + response.delete_cookie(COOKIE, secure=True, httponly=True, samesite="strict", path="/") + return response diff --git a/container-code/connector/relay/windows_keys.py b/container-code/connector/relay/windows_keys.py new file mode 100644 index 0000000..0e84e19 --- /dev/null +++ b/container-code/connector/relay/windows_keys.py @@ -0,0 +1,40 @@ +"""Current-user Windows DPAPI for device keys; never uses machine-wide scope.""" +import ctypes +import os +from ctypes import wintypes + + +class Blob(ctypes.Structure): + _fields_ = [("cbData", wintypes.DWORD), ("pbData", ctypes.POINTER(ctypes.c_ubyte))] + + +def transform(data, decrypt=False): + if os.name != "nt": + raise RuntimeError("DPAPI is supported only on Windows") + buffer = (ctypes.c_ubyte * len(data)).from_buffer_copy(data) + source = Blob(len(data), buffer) + target = Blob() + crypt = ctypes.WinDLL("crypt32", use_last_error=True) + kernel = ctypes.WinDLL("kernel32", use_last_error=True) + if decrypt: + fn = crypt.CryptUnprotectData + args = [ctypes.byref(source), None, None, None, None, 1, ctypes.byref(target)] + else: + fn = crypt.CryptProtectData + args = [ctypes.byref(source), "Hainuo device identity", None, None, None, 1, ctypes.byref(target)] + fn.restype = wintypes.BOOL + if not fn(*args): + raise ctypes.WinError(ctypes.get_last_error()) + try: + return ctypes.string_at(target.pbData, target.cbData) + finally: + kernel.LocalFree.argtypes = [ctypes.c_void_p] + kernel.LocalFree(target.pbData) + + +def protect(data): + return transform(data) + + +def unprotect(data): + return transform(data, decrypt=True) diff --git a/container-code/connector/relay/work_api.py b/container-code/connector/relay/work_api.py new file mode 100644 index 0000000..96a6e49 --- /dev/null +++ b/container-code/connector/relay/work_api.py @@ -0,0 +1,152 @@ +"""Device-authenticated USER-token business gateway; no tenant-token fallback.""" +import hashlib +import json +import re +import time +from urllib.parse import unquote + +import httpx +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import JSONResponse +from pydantic import BaseModel, Field +from .common import no_cache, rate_limit +from .crypto import canonical, verified, verify_device +from .store import INTEGRITY_ERRORS + + +class DeviceProof(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class BusinessCall(DeviceProof): + method: str = Field(pattern=r"^(GET|POST|PATCH|DELETE)$") + path: str = Field(max_length=1500) + params: dict = Field(default_factory=dict) + body: dict = Field(default_factory=dict) + operation_id: str = Field(default="", max_length=100) + + +def business_allowed(method, path): + if any(x in unquote(path) for x in ("..", "\\", "%", "?", "#", "\r", "\n")): + return False + seg = r"[A-Za-z0-9_.@-]+" + cal = r"/open-apis/calendar/v4/calendars" + rules = { + "GET": [cal, cal + "/" + seg, cal + "/" + seg + "/events(?:/" + seg + ")?(?:/attendees)?", + r"/open-apis/contact/v3/users/" + seg, r"/open-apis/search/v1/user", + r"/open-apis/(?:drive/v1/files|docx/v1/documents|wiki/v2/spaces|bitable/v1/apps)(?:/" + seg + ")*"], + "POST": [cal + "/primary", cal + "/" + seg + "/events(?:/" + seg + "/attendees)?"], + "PATCH": [cal + "/" + seg + "/events/" + seg], + "DELETE": [cal + "/" + seg + "/events/" + seg, cal + "/" + seg + "/events/" + seg + "/attendees/" + seg], + } + return any(re.fullmatch(p, path) for p in rules.get(method, [])) + + +async def device_owner(settings, redis, store, host, proof, purpose, payload): + device = store.device(host) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "active bound device required") + try: + key = verify_device(device["cert"], settings.ca_cert.read_bytes(), host) + data = verified(proof, key) + if (data["host_id"] != host or data["purpose"] != purpose or abs(time.time() - data["ts"]) > 90 + or not re.fullmatch(r"[a-f0-9]{32}", data["nonce"]) + or data["payload_hash"] != hashlib.sha256(canonical(payload)).hexdigest()): + raise ValueError() + except Exception: + raise HTTPException(403, "invalid device request proof") from None + if not await redis.set("hn:work-proof:" + host + ":" + data["nonce"], "1", nx=True, ex=180): + raise HTTPException(409, "device request replayed") + return device["owner"] + + +def router(settings, redis, store, platform): + routes = APIRouter() + store.db.execute("""CREATE TABLE IF NOT EXISTS business_operations( + owner TEXT NOT NULL, operation_id TEXT NOT NULL, request_hash TEXT NOT NULL, + status TEXT NOT NULL, result_meta TEXT NOT NULL, created DOUBLE PRECISION NOT NULL, + PRIMARY KEY(owner,operation_id))""") + if not settings.database_url: + store.db.commit() + + @routes.post("/work/api") + async def call(body: BusinessCall, request: Request): + if not settings.feishu_enabled: + raise HTTPException(503, "platform disabled") + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or not 10 < len(auth) <= 8192: + raise HTTPException(401, "user access token required") + if not business_allowed(body.method, body.path): + raise HTTPException(403, "business method/path not supported") + payload = {"method": body.method, "path": body.path, "params": body.params, "body": body.body, + "operation_id": body.operation_id, "token_hash": hashlib.sha256(auth[7:].encode()).hexdigest()} + owner = await device_owner(settings, redis, store, body.host_id, body.proof, "work-api-v1", payload) + await rate_limit(redis, "work-api:" + owner, 60) + try: + info = await platform.user_info(auth[7:]) + except Exception: + raise HTTPException(401, "user token expired or invalid; refresh or authorize") from None + if info.get("open_id") != owner: + raise HTTPException(403, "token user does not match bound device owner") + write = body.method != "GET" and not body.path.endswith(("/primary", "/batch_get_id")) + digest = hashlib.sha256(canonical({k: v for k, v in payload.items() if k != "token_hash"})).hexdigest() + if write: + if not re.fullmatch(r"[A-Za-z0-9_-]{8,100}", body.operation_id): + raise HTTPException(422, "write requires stable operation_id") + try: + with store.db: + store.db.execute("INSERT INTO business_operations VALUES(?,?,?,?,?,?)", + (owner, body.operation_id, digest, "inflight", "{}", time.time())) + except INTEGRITY_ERRORS: + row = dict(store.db.execute("SELECT * FROM business_operations WHERE owner=? AND operation_id=?", + (owner, body.operation_id)).fetchone()) + if row["request_hash"] != digest: + raise HTTPException(409, "operation_id reused with different request") + return no_cache(JSONResponse({"status": "duplicate", "operation_id": body.operation_id, + "previous_status": row["status"], **json.loads(row["result_meta"])})) + params = dict(body.params) + if write and body.method == "POST" and body.path.endswith("/events"): + params["idempotency_key"] = hashlib.sha256((owner + body.operation_id).encode()).hexdigest() + try: + async with platform.http.stream(body.method, body.path, params=params, + json=body.body if body.method != "GET" else None, + headers={"Authorization": auth}) as upstream: + raw = bytearray() + async for chunk in upstream.aiter_bytes(): + raw.extend(chunk) + if len(raw) > 2 * 1024 * 1024: + raise ValueError() + data = json.loads(raw) + if not isinstance(data, dict): + raise ValueError() + code = data.get("code", 0) + if upstream.status_code != 200 or code != 0: + status = "platform_rejected" + def safe_diagnostic(value): + text = str(value) + for secret in (auth, auth[7:], settings.feishu_app_secret, settings.admin_secret, settings.internal_secret): + if secret: + text = text.replace(secret, "[redacted]") + return text[:2000] + detail = data.get("error") or {} + result = {"status": status, "http_status": upstream.status_code, "platform_code": code, + "message": "平台拒绝:请核对用户授权、应用scope、资源权限或接口版本。", + "platform_message": safe_diagnostic(data.get("msg", data.get("message", ""))), + "platform_log_id": safe_diagnostic(detail.get("log_id", "")) if isinstance(detail, dict) else "", + "permission_details": safe_diagnostic(detail.get("permission_violations", [])) if isinstance(detail, dict) else ""} + else: + status, result = "completed", {"status": "completed", "data": data.get("data", {})} + except (httpx.HTTPError, ValueError): + status = "unknown" if write else "failed" + result = {"status": status, "message": "平台请求未取得明确结果;写操作不要换新ID重试,请先核对日历。"} + if write: + event = result.get("data", {}).get("event", {}) + meta = {k: event[k] for k in ("event_id", "app_link") if k in event} + with store.db: + store.db.execute("UPDATE business_operations SET status=?,result_meta=? WHERE owner=? AND operation_id=?", + (status, json.dumps(meta), owner, body.operation_id)) + store.audit(owner, "work_api_" + body.method.lower(), body.path, status) + return no_cache(JSONResponse(result)) + + return routes diff --git a/container-code/connector/relay/work_client.py b/container-code/connector/relay/work_client.py new file mode 100644 index 0000000..4cc90b3 --- /dev/null +++ b/container-code/connector/relay/work_client.py @@ -0,0 +1,190 @@ +"""PC-side user OAuth and calendar operations. Tokens are DPAPI-protected only here.""" +import hashlib +import json +import secrets +import ssl +import time +from datetime import datetime +from pathlib import Path +from urllib.parse import quote, urlsplit + +import httpx +from .crypto import canonical, load_private, sign +from .windows_keys import protect, unprotect +from .process_lock import DeviceProcessLock + + +class WorkError(RuntimeError): + pass + + +class AuthorizationRequired(WorkError): + def __init__(self, url): + self.url = url + super().__init__("需要本人Work授权,请打开授权链接,完成后重新发起原任务;尚未执行平台写操作。") + + +def atomic_secret(path, data): + temp = path.with_suffix(".tmp") + temp.write_bytes(protect(json.dumps(data, ensure_ascii=False).encode())) + temp.replace(path) + + +class WorkClient: + def __init__(self, directory, url="https://10.77.76.13", *, transport=None): + self.directory = Path(directory) + self.config = json.loads((self.directory / "device.json").read_text(encoding="utf-8-sig")) + if not self.config.get("owner"): + raise WorkError("请先绑定本人Work身份。") + context = ssl.create_default_context(cafile=str(self.directory / "server.pem")) + self.http = httpx.Client(base_url=url.rstrip("/"), verify=context, trust_env=False, + timeout=25, follow_redirects=False, transport=transport) + self.owner = self.config["owner"] + self.path = self.directory / "work-user-tokens.dpapi" + self.pending = self.directory / "work-oauth-pending.dpapi" + + def close(self): + self.http.close() + + def read(self, path): + return json.loads(unprotect(path.read_bytes())) + + def proof(self, purpose, payload): + return sign({"host_id": self.config["host_id"], "purpose": purpose, "ts": int(time.time()), + "nonce": secrets.token_hex(16), "payload_hash": hashlib.sha256(canonical(payload)).hexdigest()}, + load_private(self.directory / "device.key")) + + def authorize(self): + if self.pending.exists(): + data = self.read(self.pending) + if data.get("owner") == self.owner and data["deadline"] > time.time(): + return data["authorization_url"] + response = self.http.post("/oauth/start", json={"host_id": self.config["host_id"], + "proof": self.proof("work-oauth-v1", {})}) + if response.status_code != 201: + raise WorkError("授权事务创建失败(HTTP " + str(response.status_code) + "),请检查ECS平台配置。") + data = response.json() + if urlsplit(data["authorization_url"]).scheme != "https": + raise WorkError("授权地址必须使用HTTPS。") + data.update(owner=self.owner, deadline=time.time() + data["expires_in"]) + atomic_secret(self.pending, data) + return data["authorization_url"] + + def save_tokens(self, data): + if data.get("open_id") != self.owner or not data.get("user_access_token"): + raise WorkError("授权账号与本机Work绑定账号不一致,请使用本人账号重新授权。") + now = time.time() + data = {**data, "expires_at": now + int(data["expires_in"]), + "refresh_expires_at": now + int(data.get("refresh_expires_in", 0)), "needs_authorization": False} + atomic_secret(self.path, data) + return data + + def access_token(self): + # Shared across GUI and Agent calls; never rotate the same token concurrently. + with DeviceProcessLock(self.directory / "work-token-lock"): + if self.path.exists(): + data = self.read(self.path) + if data.get("open_id") == self.owner and not data.get("needs_authorization"): + if data["expires_at"] > time.time() + 120: + return data["user_access_token"] + if data.get("refresh_token") and data.get("refresh_expires_at", 0) > time.time(): + # Mark before network; ambiguous failure must not reuse a rotating refresh token. + atomic_secret(self.path, {**data, "needs_authorization": True}) + try: + response = self.http.post("/oauth/refresh", json={"refresh_token": data["refresh_token"]}) + if response.status_code != 200: + raise WorkError("刷新失败") + data = self.save_tokens(response.json()) + return data["user_access_token"] + except (httpx.HTTPError, ValueError, WorkError): + raise AuthorizationRequired(self.authorize()) from None + if self.pending.exists(): + pending = self.read(self.pending) + if pending.get("owner") == self.owner and pending["deadline"] > time.time(): + response = self.http.get("/oauth/claim", params={"state": pending["state"]}, + headers={"X-OAuth-Claim-Secret": pending["claim_secret"]}) + if response.status_code == 200: + data = self.save_tokens(response.json()) + self.pending.unlink(missing_ok=True) + return data["user_access_token"] + if response.status_code not in {202}: + self.pending.unlink(missing_ok=True) + raise AuthorizationRequired(self.authorize()) + + def status(self): + try: + token = self.access_token() + return {"authorized": True, "owner": self.owner, "message": "本人用户授权可用;令牌未输出。", + "local_time": datetime.now().astimezone().isoformat()} + except AuthorizationRequired as error: + return {"authorized": False, "authorization_url": error.url, "message": str(error)} + + def api(self, method, path, params=None, body=None, operation_id=""): + token = self.access_token() + payload = {"method": method, "path": path, "params": params or {}, "body": body or {}, + "operation_id": operation_id, "token_hash": hashlib.sha256(token.encode()).hexdigest()} + proof = self.proof("work-api-v1", payload) + wire = {k: v for k, v in payload.items() if k != "token_hash"} + response = self.http.post("/work/api", headers={"Authorization": "Bearer " + token}, + json={**wire, "host_id": self.config["host_id"], "proof": proof}) + if response.status_code == 401: + # Do not repeat a business write automatically, even when auth fails. + if self.path.exists(): + data = self.read(self.path) + data["expires_at"] = 0 + atomic_secret(self.path, data) + raise WorkError("用户令牌已失效。本次未重试,请检查授权后再核对操作状态。") + if response.status_code != 200: + raise WorkError("平台代理失败(HTTP " + str(response.status_code) + ");未重试。") + return response.json() + + def primary_calendar(self): + result = self.api("POST", "/open-apis/calendar/v4/calendars/primary", {"user_id_type": "open_id"}) + if result.get("status") != "completed": + return result + calendars = result.get("data", {}).get("calendars", []) + own = [c["calendar"] for c in calendars if c.get("user_id") == self.owner and c.get("calendar")] + if not own and len(calendars) == 1 and not calendars[0].get("user_id") and calendars[0].get("calendar"): + own = [calendars[0]["calendar"]] + if len(own) != 1: + raise WorkError("没有唯一确定的本人主日历,请先查询日历列表并明确选择。") + return {"status": "completed", "calendar": own[0]} + + def create_event(self, summary, start_iso, end_iso, attendee_open_ids, operation_id, description="", calendar_id=""): + import re + if not re.fullmatch(r"[A-Za-z0-9_-]{8,80}", operation_id): + raise WorkError("创建日程需8到80位稳定operation_id,重试不得换ID。") + if not summary.strip() or len(summary) > 1000: + raise WorkError("日程主题不能为空或超过1000字符。") + start, end = datetime.fromisoformat(start_iso), datetime.fromisoformat(end_iso) + if start.tzinfo is None or end.tzinfo is None or end <= start: + raise WorkError("起止时间必须包含时区且结束晚于开始;不要猜测时区或时长。") + if any(not isinstance(x, str) or not x.startswith("ou_") for x in attendee_open_ids): + raise WorkError("参会人必须为当前应用身份域的open_id,请先搜索并确认,不能把姓名/工号直接当ID。") + if not calendar_id: + primary = self.primary_calendar() + if primary.get("status") != "completed": + return primary + calendar_id = primary["calendar"]["calendar_id"] + path = "/open-apis/calendar/v4/calendars/" + quote(calendar_id, safe="") + "/events" + body = {"summary": summary, "description": description, "need_notification": True, + "start_time": {"timestamp": str(int(start.timestamp())), "timezone": "Asia/Shanghai"}, + "end_time": {"timestamp": str(int(end.timestamp())), "timezone": "Asia/Shanghai"}} + result = self.api("POST", path, {"user_id_type": "open_id"}, body, operation_id) + event = result.get("data", {}).get("event", {}) + if result.get("status") == "duplicate" and result.get("previous_status") == "completed": + event = {"event_id": result.get("event_id"), "app_link": result.get("app_link")} + elif result.get("status") != "completed": + return result + if not event.get("event_id"): + raise WorkError("平台未返回event_id,不能认定创建成功或继续邀请。") + ids = sorted(set([self.owner] + list(attendee_open_ids))) + invite = self.api("POST", path + "/" + quote(event["event_id"], safe="") + "/attendees", + {"user_id_type": "open_id"}, + {"attendees": [{"type": "user", "user_id": x} for x in ids], "need_notification": True}, + operation_id + "_attendees") + invited = invite.get("status") == "completed" or (invite.get("status") == "duplicate" and invite.get("previous_status") == "completed") + return {"status": "completed" if invited else "partial", "event_created": True, + "calendar_id": calendar_id, "event_id": event["event_id"], "app_link": event.get("app_link"), + "attendees_invited": invited, "invite_result": invite, + "message": "日程已创建并提交参会人邀请;不代表对方已接受。" if invited else "日程已创建,但邀请未完成;保留原操作ID和event_id处理,勿重新创建日程。"} diff --git a/container-code/connector/relay/work_tools.py b/container-code/connector/relay/work_tools.py new file mode 100644 index 0000000..b1c6da4 --- /dev/null +++ b/container-code/connector/relay/work_tools.py @@ -0,0 +1,145 @@ +"""Hermes native tools backed by the authenticated user-token relay.""" +import json +import os +import threading +import time +from pathlib import Path +import httpx +from .work_client import WorkClient, WorkError, AuthorizationRequired + +INSTRUCTIONS = """海能Work平台能力已接入本机专用工具:work_authorization、work_calendar、work_contacts、work_api_request。 +涉及飞书/海能Work日历或文档时优先调用这些工具,勿使用manage_connections寻找公有云连接器,勿索要或搜索App Secret。 +用户token仅由本机安全存储和ECS换取,第一次需要本人打开授权链接。工具返回authorization_required时将链接完整提供给本人,不能声称已经创建。 +当前日期以work_authorization返回的local_time为准。创建日程必须确定带时区的起止时间和真实参会人open_id。 +姓名/工号须用work_contacts搜索核实,多个候选必须澄清,不能猜open_id。平台返回partial表示已创建但邀请未完成,勿再次创建。 +同一写操作的operation_id必须保持不变,网络异常不得换ID重试。日程创建成功、邀请提交成功、对方接受是不同状态。 +""" + +_watching = set() +_watch_lock = threading.Lock() + + +def watch_authorization(directory): + """Claim within the server's 60-second result TTL, without keeping tokens in ECS.""" + name = str(directory) + with _watch_lock: + if name in _watching: + return + _watching.add(name) + def poll(): + client = None + try: + client = WorkClient(directory, os.environ.get("HN_WORK_RELAY_URL", "https://10.77.76.13")) + for _ in range(295): + time.sleep(2) + try: + if client.pending.exists() and client.read(client.pending).get("deadline", 0) <= time.time(): + return + if client.status().get("authorized"): + return + except Exception: + # Another local consumer may hold the short token lock. + continue + except Exception: + pass + finally: + if client: + client.close() + with _watch_lock: + _watching.discard(name) + threading.Thread(target=poll, daemon=True, name="work-user-auth-claim").start() + + +def register(directory): + from tools.registry import registry + import toolsets + definitions = [ + ("work_authorization", "查询或发起本人海能Work用户授权,返回可点击链接和本机时间;不返回token。", { + "type": "object", "properties": {}, "additionalProperties": False}), + ("work_calendar", "本人用户身份的日历:list/primary/list_events/get_event/create_event/invite。写操作operation_id需稳定,起止时间均带时区。", { + "type": "object", "properties": { + "action": {"type": "string", "enum": ["list", "primary", "list_events", "get_event", "create_event", "invite"]}, + "calendar_id": {"type": "string"}, "event_id": {"type": "string"}, + "summary": {"type": "string"}, "description": {"type": "string"}, + "start_iso": {"type": "string", "description": "ISO8601,例如2026-10-27T09:00:00+08:00"}, + "end_iso": {"type": "string"}, "attendee_open_ids": {"type": "array", "items": {"type": "string"}}, + "operation_id": {"type": "string", "description": "8-80位稳定操作ID,同一意图重试不能更换"}, + "params": {"type": "object"}}, "required": ["action"]}), + ("work_contacts", "使用本人user token搜索参会人;query可为姓名或工号,返回候选,不能未经核实猜ID。open_id可读取具体用户信息。", { + "type": "object", "properties": {"query": {"type": "string"}, "open_id": {"type": "string"}}}), + ("work_api_request", "携本人user token代理受支持业务API(日历CRUD/邀请、通讯录查询、文档云盘知识库读取)。禁止认证接口,不支持的路径返回403。写操作必须operation_id。", { + "type": "object", "properties": {"method": {"type": "string", "enum": ["GET", "POST", "PATCH", "DELETE"]}, + "path": {"type": "string"}, "params": {"type": "object"}, "body": {"type": "object"}, + "operation_id": {"type": "string"}}, "required": ["method", "path"]}), + ] + + def handler_for(name): + def handle(args, **kwargs): + client = None + try: + client = WorkClient(directory, os.environ.get("HN_WORK_RELAY_URL", "https://10.77.76.13")) + if name == "work_authorization": + result = client.status() + if not result.get("authorized"): + watch_authorization(directory) + elif name == "work_api_request": + result = client.api(**args) + elif name == "work_contacts": + from urllib.parse import quote + result = client.api("GET", "/open-apis/contact/v3/users/" + quote(args["open_id"], safe=""), + {"user_id_type": "open_id"}) if args.get("open_id") else client.api( + "GET", "/open-apis/search/v1/user", {"query": args.get("query", ""), "page_size": 20}) + else: + result = calendar(client, args) + return json.dumps(result, ensure_ascii=False) + except AuthorizationRequired as error: + watch_authorization(directory) + return json.dumps({"status": "authorization_required", "authorization_url": error.url, "message": str(error)}, ensure_ascii=False) + except (WorkError, ValueError, KeyError) as error: + return json.dumps({"error": str(error)[:500], "status": "failed"}, ensure_ascii=False) + except Exception as error: + return json.dumps({"error": type(error).__name__ + ";操作状态未确认,未自动重试。"}, ensure_ascii=False) + finally: + if client: + client.close() + return handle + + for name, description, parameters in definitions: + registry.register(name=name, toolset="hainuo_work", schema={"name": name, "description": description, "parameters": parameters}, + handler=handler_for(name), description=description) + toolsets.TOOLSETS["hainuo_work"] = {"description": "海能Work本人用户授权及日历业务", "tools": [x[0] for x in definitions], "includes": []} + # Present these four small schemas directly; otherwise Hermes defers custom + # toolsets behind tool_search and can wrongly search a public-cloud connector. + for name, _, _ in definitions: + if name not in toolsets._HERMES_CORE_TOOLS: + toolsets._HERMES_CORE_TOOLS.append(name) + + +def calendar(client, args): + from urllib.parse import quote + action = args["action"] + if action == "primary": + return client.primary_calendar() + if action == "list": + return client.api("GET", "/open-apis/calendar/v4/calendars", args.get("params", {})) + if action == "create_event": + return client.create_event(**{k: v for k, v in args.items() if k != "action" and k != "params"}) + calendar_id = args.get("calendar_id") + if not calendar_id: + primary = client.primary_calendar() + if primary.get("status") != "completed": + return primary + calendar_id = primary["calendar"]["calendar_id"] + base = "/open-apis/calendar/v4/calendars/" + quote(calendar_id, safe="") + "/events" + if action == "list_events": + return client.api("GET", base, args.get("params", {})) + base += "/" + quote(args["event_id"], safe="") + if action == "get_event": + return client.api("GET", base, {"user_id_type": "open_id"}) + if action == "invite": + ids = args.get("attendee_open_ids", []) + if not ids or any(not x.startswith("ou_") for x in ids): + raise WorkError("需要已确认的参会人open_id。") + return client.api("POST", base + "/attendees", {"user_id_type": "open_id"}, + {"attendees": [{"type": "user", "user_id": x} for x in ids], "need_notification": True}, args["operation_id"]) + raise WorkError("不支持的日历操作。") diff --git a/container-code/connector/requirements.lock b/container-code/connector/requirements.lock new file mode 100644 index 0000000..c763938 --- /dev/null +++ b/container-code/connector/requirements.lock @@ -0,0 +1,41 @@ +annotated-types==0.8.0 +anyio==4.15.1 +certifi==2026.10.23 +cffi==2.1.1 +charset-normalizer==3.5.2 +click==8.5.0 +colorama==0.4.6 +cryptography==46.0.3 +fakeredis==2.32.0 +fastapi==0.119.1 +h11==0.16.0 +httpcore==1.0.9 +httpx==0.28.1 +idna==3.20 +iniconfig==2.3.1 +lark-oapi==1.5.3 +packaging==26.3 +pluggy==1.7.0 +psycopg==3.2.12 +psycopg-binary==3.2.12 +pycparser==3.12 +pycryptodome==4.2.0 +pydantic==2.14.1 +pydantic-settings==2.11.0 +pydantic_core==2.50.1 +Pygments==2.21.0 +pytest==8.4.2 +pytest-asyncio==1.2.0 +python-dotenv==1.2.4 +PyYAML==6.0.3 +redis==6.4.0 +requests==2.34.2 +requests-toolbelt==1.0.0 +sortedcontainers==2.4.0 +starlette==0.48.0 +typing-inspection==0.4.4 +typing_extensions==4.16.0 +tzdata==2026.5 +urllib3==2.8.0 +uvicorn==0.38.0 +websockets==15.0.1 diff --git a/container-code/enrollment-signer/image-id.txt b/container-code/enrollment-signer/image-id.txt new file mode 100644 index 0000000..92e3e52 --- /dev/null +++ b/container-code/enrollment-signer/image-id.txt @@ -0,0 +1 @@ +sha256:5c8c389adf40b8b287afeed77742bbc88cb8b7c77fd9d9332a1cca49b08196a7 diff --git a/container-code/enrollment-signer/pyproject.toml b/container-code/enrollment-signer/pyproject.toml new file mode 100644 index 0000000..3bf5e2c --- /dev/null +++ b/container-code/enrollment-signer/pyproject.toml @@ -0,0 +1,32 @@ +[build-system] +requires = ["setuptools>=75"] +build-backend = "setuptools.build_meta" + +[project] +name = "hainuo-relay" +version = "0.1.0" +description = "Hainuo isolated connector and encrypted device router" +requires-python = ">=3.12" +dependencies = [ + "fastapi==0.119.1", "uvicorn==0.38.0", "httpx==0.28.1", + "redis==6.4.0", "cryptography==46.0.3", "websockets==15.0.1", + "pydantic-settings==2.11.0", + "psycopg[binary]==3.2.12", +] + +[project.optional-dependencies] +test = ["pytest==8.4.2", "pytest-asyncio==1.2.0", "fakeredis==2.32.0"] +longconn = ["lark-oapi==1.5.3"] + +[project.scripts] +hainuo-relay = "relay.cli:main" + +[tool.setuptools.packages.find] +include = ["relay*"] + +[tool.setuptools.package-data] +relay = ["web/*.html", "web/*.js", "web/*.css"] + +[tool.pytest.ini_options] +asyncio_mode = "auto" +testpaths = ["tests"] diff --git a/container-code/enrollment-signer/relay/__init__.py b/container-code/enrollment-signer/relay/__init__.py new file mode 100644 index 0000000..32754a7 --- /dev/null +++ b/container-code/enrollment-signer/relay/__init__.py @@ -0,0 +1 @@ +"""Hainuo relay 0.1.0.""" diff --git a/container-code/enrollment-signer/relay/cli.py b/container-code/enrollment-signer/relay/cli.py new file mode 100644 index 0000000..fad7979 --- /dev/null +++ b/container-code/enrollment-signer/relay/cli.py @@ -0,0 +1,216 @@ +import argparse +import asyncio +import json +import os +import secrets +import ssl +import uuid +from datetime import datetime, timedelta, timezone +from pathlib import Path + +import httpx +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ed25519 +from cryptography.x509.oid import NameOID + +from .crypto import issue_ca, issue_device, load_private, pem_private + + +def write_new(path, data, private=False): + path.parent.mkdir(parents=True, exist_ok=True) + with path.open("xb") as f: + f.write(data if isinstance(data, bytes) else data.encode()) + if private and os.name != "nt": + path.chmod(0o600) + + +def init_project(root: Path, hostname: str): + if (root / ".env").exists(): + raise RuntimeError(".env exists; init never overwrites credentials") + ca_key, ca = issue_ca() + signer = ed25519.Ed25519PrivateKey.generate() + write_new(root / "operator/ca.key", pem_private(ca_key), True) + write_new(root / "runtime/trust/ca.pem", ca.public_bytes(serialization.Encoding.PEM)) + write_new(root / "runtime/connector/connector.key", pem_private(signer), True) + write_new(root / "runtime/trust/connector.pub", signer.public_key().public_bytes( + serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo)) + tls_key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, hostname)]) + now = datetime.now(timezone.utc) + tls = (x509.CertificateBuilder().subject_name(name).issuer_name(name).public_key(tls_key.public_key()) + .serial_number(x509.random_serial_number()).not_valid_before(now - timedelta(minutes=5)) + .not_valid_after(now + timedelta(days=30)) + .add_extension(x509.SubjectAlternativeName([x509.DNSName(hostname)]), critical=False) + .sign(tls_key, algorithm=None)) + write_new(root / "runtime/tls/server.key", pem_private(tls_key), True) + write_new(root / "runtime/tls/server.pem", tls.public_bytes(serialization.Encoding.PEM)) + redis_secret = secrets.token_hex(32) + db_secret = secrets.token_hex(32) + env = (f"HN_ENVIRONMENT=development\nHN_PUBLIC_HOST={hostname}\nHN_PUBLIC_URL=https://{hostname}\n" + f"HN_ROUTER_URL=wss://{hostname}/device/ws\nHN_ADMIN_SECRET={secrets.token_hex(32)}\n" + f"HN_INTERNAL_SECRET={secrets.token_hex(32)}\nHN_REDIS_PASSWORD={redis_secret}\n" + f"HN_REDIS_URL=redis://:{redis_secret}@redis:6379/0\n" + f"HN_POSTGRES_PASSWORD={db_secret}\nHN_DATABASE_URL=postgresql://hainuo:{db_secret}@postgres:5432/hainuo\n" + "HN_FEISHU_ENABLED=false\nHN_FEISHU_API_BASE=https://open.feishu.cn\n" + "HN_FEISHU_AUTHORIZE_URL=https://open.feishu.cn/open-apis/authen/v1/authorize\n" + "HN_FEISHU_APP_ID=\nHN_FEISHU_APP_SECRET=\nHN_FEISHU_ENCRYPT_KEY=\n" + "HN_FEISHU_VERIFICATION_TOKEN=\nHN_PROXY_ENABLED=false\nHN_DEVICE_DOMAIN=\n") + write_new(root / ".env", env, True) + print("已生成配置、连接器密钥、设备CA及临时TLS证书;未输出凭据。") + print("operator/ca.key 仅保留在签发工作站,不要上传到ECS。临时TLS证书上线前须替换。") + + +def device_request(directory): + host = uuid.uuid4().hex + private = ed25519.Ed25519PrivateKey.generate() + write_new(directory / "device.key", pem_private(private), True) + public = private.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode() + write_new(directory / "request.json", json.dumps({"host_id": host, "public_key": public}, indent=2)) + print("已在本机生成私钥和 request.json;仅将request.json交给签发工作站。") + + +def issue(root, request_file, output): + request = json.loads(request_file.read_text(encoding="utf-8")) + host = request["host_id"] + if len(host) != 32 or any(c not in "0123456789abcdef" for c in host): + raise ValueError("invalid host identity") + ca = x509.load_pem_x509_certificate((root / "runtime/trust/ca.pem").read_bytes()) + public = serialization.load_pem_public_key(request["public_key"].encode()) + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("Ed25519 required") + cert = issue_device(load_private(root / "operator/ca.key"), ca, host, public) + pem = cert.public_bytes(serialization.Encoding.PEM) + write_new(output / "device.pem", pem) + write_new(output / "ca.pem", (root / "runtime/trust/ca.pem").read_bytes()) + write_new(output / "connector.pub", (root / "runtime/trust/connector.pub").read_bytes()) + write_new(output / "device.json", json.dumps({"host_id": host, "owner": None}, indent=2)) + write_new(output / "registration.json", json.dumps({"host_id": host, "cert": pem.decode()}, indent=2)) + print("已签发设备证书;将device.pem、ca.pem、connector.pub、device.json交回原设备。") + + +def main(): + parser = argparse.ArgumentParser(description="海诺中继部署和设备工具") + sub = parser.add_subparsers(dest="command", required=True) + p = sub.add_parser("init") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--hostname", required=True) + p = sub.add_parser("serve") + p.add_argument("role", choices=["connector", "router"]) + p.add_argument("--host", default="127.0.0.1") + p.add_argument("--port", type=int, default=8000) + p = sub.add_parser("device-request") + p.add_argument("--directory", type=Path, required=True) + p = sub.add_parser("issue-device") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--request", type=Path, required=True) + p.add_argument("--output", type=Path, required=True) + p = sub.add_parser("register") + p.add_argument("--registration", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("bind") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("device") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p.add_argument("--test-root", type=Path) + p.add_argument("--agent-url", help="Explicit loopback read-only Agent adapter endpoint") + p.add_argument("--hainuo-config", type=Path, help="Native installer-1.065 API adapter config; alternative to --agent-url") + p = sub.add_parser("hainuo-probe") + p.add_argument("--config", type=Path, required=True) + p = sub.add_parser("web-pair") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + for command in ("status", "binding-code", "dispatch", "revoke", "unbind", "reconcile"): + p = sub.add_parser(command) + p.add_argument("--url", default="http://127.0.0.1:18081") + p.add_argument("--tls-ca", type=Path) + if command in {"binding-code", "dispatch"}: + p.add_argument("--owner", required=True) + if command == "dispatch": + p.add_argument("--text", required=True) + p.add_argument("--turn-id", default=None) + if command in {"revoke", "unbind"}: + p.add_argument("--host-id", required=True) + args = parser.parse_args() + if args.command == "init": + init_project(args.root, args.hostname) + elif args.command == "serve": + import uvicorn + uvicorn.run(f"relay.{args.role}:create_app", factory=True, host=args.host, port=args.port, + workers=1, access_log=False, proxy_headers=False, + ws_max_size=262144, ws_max_queue=16, log_level="warning") + elif args.command == "device-request": + device_request(args.directory) + elif args.command == "issue-device": + issue(args.root, args.request, args.output) + elif args.command == "register": + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, follow_redirects=False, timeout=15) as client: + response = client.post(args.url.rstrip("/") + "/admin/devices", + headers={"X-Admin-Secret": secret}, + json=json.loads(args.registration.read_text(encoding="utf-8"))) + if response.status_code != 201: + raise RuntimeError(f"register failed: HTTP {response.status_code}") + print("设备登记成功。") + elif args.command == "bind": + import getpass + from .device import bind_device + code = getpass.getpass("Binding code: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + asyncio.run(bind_device(args.directory, args.url, code, tls_verify=context)) + print("绑定成功。") + elif args.command == "device": + from .device import run_device + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else None + asyncio.run(run_device(args.directory, args.url, root=args.test_root, ssl_context=context, + agent_url=args.agent_url, hainuo_config=args.hainuo_config)) + elif args.command == "hainuo-probe": + from .hainuo1065 import Hainuo1065Agent + async def probe(): + adapter = Hainuo1065Agent(args.config) + try: + print(json.dumps(await adapter.probe(), ensure_ascii=False, indent=2)) + finally: + await adapter.close() + asyncio.run(probe()) + elif args.command == "web-pair": + from .device import pair_browser + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + result = asyncio.run(pair_browser(args.directory, args.url, tls_verify=context)) + print("网页地址:" + result["url"]) + print("一次性网页配对码(120秒有效):" + result["code"]) + else: + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, timeout=200, follow_redirects=False) as client: + paths = {"status": "/admin/devices", "binding-code": "/admin/test/binding-code", + "dispatch": "/admin/test/dispatch", "reconcile": "/admin/reconcile"} + if args.command in {"revoke", "unbind"}: + if len(args.host_id) != 32 or any(c not in "0123456789abcdef" for c in args.host_id): + raise ValueError("invalid host identity") + path = f"/admin/devices/{args.host_id}/{args.command}" + else: + path = paths[args.command] + body = None + if args.command in {"binding-code", "dispatch"}: + body = {"open_id": args.owner, "text": getattr(args, "text", "binding")} + if getattr(args, "turn_id", None): + body["turn_id"] = args.turn_id + response = client.request("GET" if args.command == "status" else "POST", args.url.rstrip("/") + path, + headers={"X-Admin-Secret": secret}, json=body) + if response.status_code != 200: + raise RuntimeError(f"operation failed: HTTP {response.status_code}") + print(json.dumps(response.json(), ensure_ascii=False, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/container-code/enrollment-signer/relay/client_manage.py b/container-code/enrollment-signer/relay/client_manage.py new file mode 100644 index 0000000..8fa25e4 --- /dev/null +++ b/container-code/enrollment-signer/relay/client_manage.py @@ -0,0 +1,284 @@ +"""Per-user Windows connection companion. No platform/application secrets.""" +import argparse +import asyncio +import json +import os +import secrets +import socket +import ssl +import subprocess +import sys +import time +import uuid +from pathlib import Path + +import httpx +import psutil +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey +from .crypto import load_private, load_public, pem_private, raw_public, sign, verified, verify_device +from .windows_keys import protect + +BASE = Path(__file__).resolve().parent.parent +URL = "https://10.77.76.13" + + +def save(path, value): + temp = path.with_suffix(path.suffix + ".tmp") + temp.write_text(json.dumps(value, ensure_ascii=False, indent=2), encoding="utf-8") + os.replace(temp, path) + + +def secure_directory(directory): + directory.mkdir(parents=True, exist_ok=True) + # SID avoids localized account names. Fail closed if ACL protection fails. + result = subprocess.run(["whoami", "/user", "/fo", "csv", "/nh"], capture_output=True, text=True, check=True) + import csv + sid = list(csv.reader([result.stdout.strip()]))[0][1] + subprocess.run(["icacls", str(directory), "/inheritance:r", "/grant:r", f"*{sid}:(OI)(CI)F", "*S-1-5-18:(OI)(CI)F"], + capture_output=True, check=True, creationflags=subprocess.CREATE_NO_WINDOW) + + +def prepare(directory): + secure_directory(directory) + for name in ("server.pem", "ca.pem", "connector.pub"): + target = directory / name + if not target.exists(): + target.write_bytes((BASE / "trust" / name).read_bytes()) + if not (directory / "device.json").exists(): + if (directory / "device.key.dpapi").exists() or (directory / "device.key").exists(): + raise RuntimeError("发现不完整的设备身份,请保留目录并联系管理员恢复。") + private = Ed25519PrivateKey.generate() + (directory / "device.key.dpapi").write_bytes(protect(pem_private(private))) + save(directory / "device.json", {"host_id": uuid.uuid4().hex, "owner": None}) + + +def tls(directory): + return ssl.create_default_context(cafile=str(directory / "server.pem")) + + +def import_existing(directory, source): + """Explicit migration; only public trust and this PC's identity, never app keys.""" + if source.resolve() == directory.resolve(): + raise ValueError("请选择旧设备目录。") + if any((directory / name).exists() for name in ("device.json", "device.key.dpapi", "device.key")): + raise RuntimeError("本目录已有设备身份。请使用已有身份继续,不能用导入覆盖。") + config = json.loads((source / "device.json").read_text(encoding="utf-8-sig")) + for name in ("server.pem", "ca.pem", "connector.pub"): + if (source / name).read_bytes().strip() != (BASE / "trust" / name).read_bytes().strip(): + raise ValueError("旧设备的服务信任与本安装包不符,请联系管理员。") + cert = (source / "device.pem").read_text(encoding="utf-8") + public = verify_device(cert, (source / "ca.pem").read_bytes(), config["host_id"]) + private = load_private(source / "device.key") + if raw_public(public) != raw_public(private.public_key()): + raise ValueError("设备证书与本机私钥不匹配。") + secure_directory(directory) + for name in ("device.pem", "server.pem", "ca.pem", "connector.pub"): + (directory / name).write_bytes((source / name).read_bytes()) + (directory / "device.key.dpapi").write_bytes(protect(pem_private(private))) + save(directory / "device.json", config) + # Preserve local replay journal using SQLite's consistent backup API. + if (source / "journal.sqlite").is_file(): + import sqlite3 + with sqlite3.connect(source / "journal.sqlite") as original, sqlite3.connect(directory / "journal.sqlite") as target: + original.backup(target) + return {"message": "旧设备导入成功,Work绑定已保留。请先停止旧PowerShell桥接窗口,再点击启动连接。"} + + +def enrollment(directory): + prepare(directory) + cfg = json.loads((directory / "device.json").read_text()) + if (directory / "device.pem").exists(): + verify_device((directory / "device.pem").read_text(), (directory / "ca.pem").read_bytes(), cfg["host_id"]) + return {"message": "设备已注册,可在Work获取绑定码后绑定。已有绑定可直接启动连接。"} + with httpx.Client(verify=tls(directory), trust_env=False, timeout=15) as http: + key = load_private(directory / "device.key") + proof = sign({"host_id": cfg["host_id"], "purpose": "device-enroll-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + response = http.post(URL + "/device/register", json={"host_id": cfg["host_id"], "proof": proof, + "public_key": key.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode()}) + if response.status_code not in {200, 201}: + raise RuntimeError("自动注册失败:HTTP " + str(response.status_code) + ";请确认ECS已安装1.067自动注册更新。") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt.get("status") != "registered" or receipt["host_id"] != cfg["host_id"]: + raise ValueError("注册回执身份不符") + public = verify_device(receipt["cert"], (directory / "ca.pem").read_bytes(), cfg["host_id"]) + if raw_public(public) != raw_public(key.public_key()): + raise ValueError("注册证书公钥不符") + temp = directory / "device.pem.tmp" + temp.write_text(receipt["cert"], encoding="utf-8") + os.replace(temp, directory / "device.pem") + (directory / "enrollment.json").unlink(missing_ok=True) + return {"message": "设备已自动注册,无需管理员审批。请在Work获取绑定码,填写后点击绑定并连接。"} + + +def managed_processes(directory): + """Only processes with our absolute entry path AND exact device directory.""" + state = directory / "processes.json" + if not state.exists(): + return [] + result = [] + for item in json.loads(state.read_text()).get("processes", []): + try: + proc = psutil.Process(item["pid"]) + command = proc.cmdline() + if (abs(proc.create_time() - item["created"]) < 0.1 and str(BASE / "entry.py") in command + and str(directory) in command): + result.append(proc) + except psutil.Error: + pass + return result + + +def stop(directory): + procs = managed_processes(directory) + # Supervisor owns and terminates only its own child handles in its finally block. + (directory / "stop.flag").touch() + _, alive = psutil.wait_procs(procs, timeout=8) + for proc in alive: + proc.terminate() + return {"message": "已停止本安装包启动的连接进程。手工启动的旧桥接请在原窗口按Ctrl+C停止。"} + + +def start(directory, root): + from .process_lock import DeviceProcessLock + with DeviceProcessLock(directory / "control"): + return _start(directory, root) + + +def _start(directory, root): + cfg = json.loads((directory / "device.json").read_text()) + if not cfg.get("owner") or not (directory / "device.pem").exists(): + raise RuntimeError("请先完成设备准入和Work绑定。") + if managed_processes(directory): + return {"message": "连接进程已运行,请点击状态查看。"} + (directory / "stop.flag").unlink(missing_ok=True) + log = (directory / "bridge.log").open("a", encoding="utf-8") + proc = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "supervise", "--directory", str(directory), "--root", str(root)], + stdout=log, stderr=log, creationflags=subprocess.CREATE_NO_WINDOW) + log.close() + save(directory / "processes.json", {"processes": [{"pid": proc.pid, "created": psutil.Process(proc.pid).create_time()}]}) + return {"message": "连接进程已启动;约30秒后点击状态,再到Work发送 /主机名 验证。"} + + +def supervise(directory, root): + from .process_lock import DeviceProcessLock + lockdir = directory / "supervisor" + with DeviceProcessLock(lockdir): + children = [] + def record_children(): + processes = [psutil.Process(os.getpid())] + [psutil.Process(p.pid) for p in children if p.poll() is None] + save(directory / "processes.json", {"processes": [{"pid": p.pid, "created": p.create_time()} for p in processes]}) + try: + # Check local bind availability without an HTTP connection. Windows + # filtering can time out connections to a closed port rather than + # refuse them; that must not prevent starting our own listener. + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as port_probe: + try: + if os.name == "nt": + port_probe.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1) + port_probe.bind(("127.0.0.1", 18643)) + except OSError: + raise RuntimeError("原生API端口18643被占用或不允许绑定,请先检查旧连接。") from None + native = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "native", "--directory", str(directory), "--root", str(root)]) + children.append(native) + record_children() + for _ in range(90): + if (directory / "stop.flag").exists(): + return + if native.poll() is not None: + raise RuntimeError("本机原生API启动失败,请检查安装配置。") + try: + from .hainuo1065 import Hainuo1065Agent + async def probe(): + agent = Hainuo1065Agent(directory / "hainuo-native.json") + try: + return await agent.probe() + finally: + await agent.close() + asyncio.run(probe()) + break + except Exception: + time.sleep(1) + else: + raise RuntimeError("本机原生API未就绪。") + bridge = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "bridge", "--directory", str(directory), "--root", str(root)]) + children.append(bridge) + record_children() + while not (directory / "stop.flag").exists(): + if any(proc.poll() is not None for proc in children): + raise RuntimeError("连接子进程已退出,请查看状态后重新启动。") + time.sleep(0.5) + finally: + for proc in reversed(children): + if proc.poll() is None: + proc.terminate() + try: + proc.wait(timeout=5) + except subprocess.TimeoutExpired: + proc.kill() + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("action", choices=["import", "enroll", "bind", "start", "stop", "status", "pair", "supervise", "native", "bridge"]) + parser.add_argument("--directory", type=Path, required=True) + parser.add_argument("--root", type=Path, required=True) + args = parser.parse_args() + directory, root = args.directory.resolve(), args.root.resolve() + directory.mkdir(parents=True, exist_ok=True) + try: + if args.action == "import": + result = import_existing(directory, Path(sys.stdin.readline().strip())) + elif args.action == "enroll": + result = enrollment(directory) + elif args.action == "bind": + from .device import bind_device + code = sys.stdin.readline().strip() + if not code: + raise ValueError("请填写Work机器人新生成的绑定码。") + enrollment(directory) + stop(directory) + asyncio.run(bind_device(directory, URL, code, tls_verify=tls(directory))) + result = start(directory, root) + result["message"] = "Work绑定成功。" + result["message"] + elif args.action == "start": + result = start(directory, root) + elif args.action == "stop": + result = stop(directory) + elif args.action == "pair": + from .device import pair_browser + receipt = asyncio.run(pair_browser(directory, URL, tls_verify=tls(directory))) + result = {"message": "网页:" + receipt["url"] + "\n一次性配对码(120秒):" + receipt["code"]} + elif args.action == "status": + cfg = json.loads((directory / "device.json").read_text()) if (directory / "device.json").exists() else {} + log = directory / "bridge.log" + tail = log.read_bytes()[-6000:].decode("utf-8", errors="replace") if log.exists() else "" + result = {"message": "设备:" + cfg.get("host_id", "未申请") + "\nWork身份:" + (cfg.get("owner") or "未绑定") + + "\n连接进程:" + ("运行中(请在Work发送 /主机名 确认当前链路)" if managed_processes(directory) else "未运行") + "\n" + tail[-2500:]} + elif args.action == "supervise": + return supervise(directory, root) + elif args.action == "native": + import importlib.util + spec = importlib.util.spec_from_file_location("native_companion", BASE / "hainuo_native_api.py") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + module.enable_profile_tools(root, "hainuo") + key = module.prepare_environment(root, "hainuo", 18643, directory / "hainuo-native.json", enable_tools=True) + import logging + logging.disable(logging.CRITICAL) + return asyncio.run(module.serve_native(18643, key, enable_tools=True)) + else: + from .device import run_device + return asyncio.run(run_device(directory, "wss://10.77.76.13/device/ws", ssl_context=tls(directory), hainuo_config=directory / "hainuo-native.json")) + print(json.dumps(result, ensure_ascii=False)) + except KeyboardInterrupt: + pass + except Exception as error: + # Native exception bodies may include URLs or secrets; emit bounded known messages only. + message = str(error) if isinstance(error, (RuntimeError, ValueError)) else type(error).__name__ + ";请检查网络、证书、安装目录。" + print(json.dumps({"message": message[:600], "error": True}, ensure_ascii=False)) + raise SystemExit(1) from None + + +if __name__ == "__main__": + main() diff --git a/container-code/enrollment-signer/relay/common.py b/container-code/enrollment-signer/relay/common.py new file mode 100644 index 0000000..c112b99 --- /dev/null +++ b/container-code/enrollment-signer/relay/common.py @@ -0,0 +1,61 @@ +import hashlib +import hmac +import json +import time +from fastapi import HTTPException, Request +from starlette.responses import JSONResponse + + +def require_secret(actual, expected): + if not actual or not hmac.compare_digest(actual, expected): + raise HTTPException(401, "unauthorized") + + +async def rate_limit(redis, bucket: str, limit=60, seconds=60): + # Atomic counter + expiry: a crash cannot leave a permanent bucket. + key = "hn:rate:" + hashlib.sha256(bucket.encode()).hexdigest() + ":" + str(int(time.time()) // seconds) + pipe = redis.pipeline(transaction=True) + pipe.incr(key) + pipe.expire(key, seconds * 2) + result = (await pipe.execute())[0] + if int(result) > limit: + raise HTTPException(429, "rate limited", headers={"Retry-After": str(seconds)}) + + +class BodyLimitMiddleware: + def __init__(self, app, max_body=262144): + self.app, self.max_body = app, max_body + + async def __call__(self, scope, receive, send): + if scope["type"] != "http": + return await self.app(scope, receive, send) + messages, size = [], 0 + while True: + message = await receive() + if message["type"] == "http.disconnect": + return + size += len(message.get("body", b"")) + if size > self.max_body: + return await JSONResponse({"detail": "body too large"}, status_code=413)(scope, receive, send) + messages.append(message) + if not message.get("more_body", False): + break + async def buffered(): + return messages.pop(0) if messages else await receive() + await self.app(scope, buffered, send) + + +def no_cache(response): + response.headers["Cache-Control"] = "no-store" + response.headers["X-Content-Type-Options"] = "nosniff" + response.headers["Referrer-Policy"] = "no-referrer" + return response + + +async def json_pub(redis, channel, data): + return await redis.publish(channel, json.dumps(data, separators=(",", ":"))) + + +def peer(request: Request): + # Do not trust arbitrary X-Forwarded-For; configure uvicorn trusted proxies explicitly. + return request.client.host if request.client else "unknown" diff --git a/container-code/enrollment-signer/relay/config.py b/container-code/enrollment-signer/relay/config.py new file mode 100644 index 0000000..d6ad400 --- /dev/null +++ b/container-code/enrollment-signer/relay/config.py @@ -0,0 +1,65 @@ +from pathlib import Path +from pydantic import model_validator +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + model_config = SettingsConfigDict(env_prefix="HN_", extra="ignore", hide_input_in_errors=True) + environment: str = "production" + role: str = "connector" + redis_url: str = "redis://127.0.0.1:6379/0" + data_dir: Path = Path("runtime/connector") + database_url: str = "" + admin_secret: str = "" + internal_secret: str = "" + ca_cert: Path = Path("runtime/pki/ca.pem") + connector_key: Path = Path("runtime/pki/connector.key") + connector_public_key: Path = Path("runtime/pki/connector.pub") + public_url: str = "https://relay.example.invalid" + router_url: str = "wss://relay.example.invalid/device/ws" + device_domain: str = "" + feishu_api_base: str = "https://open.feishu.cn" + feishu_authorize_url: str = "https://open.feishu.cn/open-apis/authen/v1/authorize" + feishu_app_id: str = "" + feishu_app_secret: str = "" + feishu_encrypt_key: str = "" + feishu_verification_token: str = "" + feishu_enabled: bool = False + oauth_enabled: bool = True + feishu_event_source: str = "longconn" + proxy_enabled: bool = False + # A fixed allowlist, never a user supplied upstream URL. + proxy_paths: str = "/open-apis/drive/v1/files,/open-apis/docx/v1/documents" + request_timeout: int = 45 + max_body: int = 262144 + max_pending: int = 1000 + max_devices: int = 1000 + max_connections: int = 1000 + max_result: int = 16000 + allow_insecure_local: bool = False + enrollment_signer_url: str = "" + + @model_validator(mode="after") + def validate_runtime(self): + if self.environment not in {"production", "test", "development"}: + raise ValueError("invalid environment") + if self.role == "connector" and min(len(self.admin_secret), len(self.internal_secret)) < 32: + raise ValueError("admin/internal secrets must each be at least 32 characters") + if self.role == "longconn" and len(self.internal_secret) < 32: + raise ValueError("internal credential must be at least 32 characters") + if self.role == "connector" and self.admin_secret == self.internal_secret: + raise ValueError("admin and internal credentials must be different") + if self.environment == "production": + if self.allow_insecure_local or not self.public_url.startswith("https://"): + raise ValueError("production requires HTTPS") + if not self.router_url.startswith("wss://"): + raise ValueError("production requires WSS") + if not self.feishu_api_base.startswith("https://"): + raise ValueError("platform API requires HTTPS") + if self.feishu_event_source not in {"longconn", "webhook"}: + raise ValueError("invalid platform event source") + if self.feishu_enabled and not all((self.feishu_app_id, self.feishu_app_secret)): + raise ValueError("platform app ID and secret are required") + if self.feishu_enabled and self.feishu_event_source == "webhook" and not all((self.feishu_encrypt_key, self.feishu_verification_token)): + raise ValueError("webhook requires encryption key and verification token") + return self diff --git a/container-code/enrollment-signer/relay/connector.py b/container-code/enrollment-signer/relay/connector.py new file mode 100644 index 0000000..44a2ee9 --- /dev/null +++ b/container-code/enrollment-signer/relay/connector.py @@ -0,0 +1,503 @@ +import asyncio +import base64 +import contextlib +import hashlib +import hmac +import json +import re +import secrets +import sqlite3 +import time +import uuid +from contextlib import asynccontextmanager +from dataclasses import dataclass +from urllib.parse import unquote, urlsplit + +from cryptography.hazmat.primitives import hashes, padding +from cryptography.hazmat.primitives.asymmetric import x25519 +from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes +from fastapi import FastAPI, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from fastapi.exceptions import RequestValidationError +from pydantic import BaseModel, Field +from redis.asyncio import Redis + +from .common import BodyLimitMiddleware, no_cache, peer, rate_limit, require_secret +from .config import Settings +from .crypto import (fingerprint, load_private, open_envelope, raw_public, seal, + sign, verified, verify_device) +from .oauth import OAuth +from .platform import Feishu, PlatformError +from .store import Store, INTEGRITY_ERRORS + + +class Register(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + cert: str = Field(max_length=10000) + + +class Bind(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Instruction(BaseModel): + open_id: str = Field(min_length=1, max_length=128) + text: str = Field(min_length=1, max_length=8000) + # Stable identity chosen by upstream, not generated again on retries. + turn_id: str = Field(default_factory=lambda: uuid.uuid4().hex, pattern=r"^[A-Za-z0-9_-]{1,128}$") + + +@dataclass +class Pending: + future: asyncio.Future + private: object + public: object + host: str + session: str + + +class Broker: + def __init__(self, settings, redis, store): + self.s, self.r, self.store = settings, redis, store + self.signer = load_private(settings.connector_key) + self.pending: dict[str, Pending] = {} + + async def replies(self, pub): + async for message in pub.listen(): + if message["type"] != "message": + continue + try: + data = json.loads(message["data"]) + item = self.pending.get(data["turn_id"]) + if not item or item.future.done(): + continue + plain = open_envelope(data["envelope"], item.private, item.public, + host_id=item.host, session_id=item.session) + if plain["turn_id"] != data["turn_id"] or plain.get("type") != "result": + continue + result = plain.get("result", "") + if not isinstance(result, str) or len(result) > self.s.max_result: + continue + item.future.set_result({"status": "completed", "turn_id": data["turn_id"], "result": result}) + except Exception: + # Invalid packets never consume a pending result; do not log packet contents. + continue + + async def dispatch(self, owner, text, turn): + device = self.store.by_owner(owner) + if not device: + return {"status": "unavailable", "turn_id": turn, "result": "未绑定设备或设备不可用。"} + host = device["host_id"] + await rate_limit(self.r, "dispatch:" + owner, 30) + if len(self.pending) >= self.s.max_pending: + raise HTTPException(503, "too many in-flight requests") + presence = await self.r.get("hn:online:" + host) + if not presence or await self.r.exists("hn:revoked:" + host): + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线,请开机并连接网络。"} + try: + presence = json.loads(presence) + if fingerprint(presence["cert"]) != device["fingerprint"]: + raise ValueError("certificate changed") + public = verify_device(presence["cert"], self.s.ca_cert.read_bytes(), host) + proof = verified(presence["proof"], public) + if proof["host_id"] != host or proof["challenge"] != presence["challenge"]: + raise ValueError("invalid device session") + session = proof["session_id"] + except Exception: + self.store.audit(owner, "dispatch", host, "identity_rejected") + raise HTTPException(409, "device identity rejected") from None + if not self.store.begin_turn(turn, owner, host): + return {"status": "duplicate", "turn_id": turn, "result": "该请求已接收,不会重复执行。"} + private = x25519.X25519PrivateKey.generate() + future = asyncio.get_running_loop().create_future() + self.pending[turn] = Pending(future, private, public, host, session) + envelope = seal({"type": "instruction", "turn_id": turn, "owner": owner, + "text": text, "read_only": True, "channel": "im", + "reply_key": raw_public(private.public_key())}, proof["kx"], self.signer, + host_id=host, session_id=session) + key = f"hn:inflight:{host}:{turn}" + try: + await self.r.set(key, session, ex=self.s.request_timeout + 5) + count = await self.r.publish("hn:down:" + host, json.dumps({"type": "instruction", "envelope": envelope})) + if not count: + self.store.finish(turn, "offline") + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线。"} + result = await asyncio.wait_for(future, timeout=self.s.request_timeout) + self.store.finish(turn, "completed") + self.store.audit(owner, "instruction", host, "completed") + return result + except asyncio.TimeoutError: + self.store.finish(turn, "unknown") + return {"status": "unknown", "turn_id": turn, + "result": "等待结果超时,执行状态未知;不要直接重发有副作用的任务。"} + finally: + self.pending.pop(turn, None) + await self.r.delete(key) + + +def create_app(settings=None, redis=None, platform=None): + s = settings or Settings() + r = redis or Redis.from_url(s.redis_url, decode_responses=True) + store = Store(s.data_dir / "metadata.sqlite", s.database_url) + f = platform or Feishu(s) + broker = Broker(s, r, store) + oauth = OAuth(s, r, f) + background = set() + + def spawn(coro): + task = asyncio.create_task(coro) + background.add(task) + task.add_done_callback(background.discard) + # Retrieve errors without displaying possibly sensitive exception text. + task.add_done_callback(lambda t: t.exception() if not t.cancelled() else None) + + @asynccontextmanager + async def lifespan(app): + await r.ping() + for row in store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall(): + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + pub = r.pubsub() + await pub.subscribe("hn:replies") + reply_task = asyncio.create_task(broker.replies(pub)) + app.state.reply_task = reply_task + async def sweep(): + while True: + await asyncio.sleep(30) + async with oauth.lock: + oauth.prune() + sweep_task = asyncio.create_task(sweep()) + try: + yield + finally: + for task in [reply_task, sweep_task, *background]: + task.cancel() + await asyncio.gather(reply_task, sweep_task, *background, return_exceptions=True) + await pub.aclose() + await f.close() + store.close() + if redis is None: + await r.aclose() + + app = FastAPI(title="Hainuo B1 connector", lifespan=lifespan, docs_url=None, redoc_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=s.max_body) + app.include_router(oauth.router) + from .web_session import WebSessions + web_sessions = WebSessions(s, r, store, broker, f) + app.include_router(web_sessions.router) + app.state.store, app.state.broker, app.state.oauth = store, broker, oauth + app.state.platform, app.state.redis = f, r + + @app.exception_handler(RequestValidationError) + async def validation_error(request, exc): + # FastAPI's default error response includes invalid input values (possibly tokens). + return JSONResponse({"detail": "invalid request parameters"}, status_code=422) + + @app.middleware("http") + async def secure_response(request, call_next): + return no_cache(await call_next(request)) + + def admin(request): + require_secret(request.headers.get("X-Admin-Secret"), s.admin_secret) + + from .enrollment import enrollment_router + app.include_router(enrollment_router(s, r, store, broker.signer, admin)) + + @app.get("/health") + async def health(): + return {"status": "alive", "role": "connector", "version": "0.1.0"} + + @app.get("/ready") + async def ready(): + await r.ping() + store.db.execute("SELECT 1") + if not getattr(app.state, "reply_task", None) or app.state.reply_task.done(): + raise HTTPException(503, "reply subscriber unavailable") + return {"status": "ready"} + + @app.post("/admin/devices", status_code=201) + async def register(body: Register, request: Request): + admin(request) + try: + verify_device(body.cert, s.ca_cert.read_bytes(), body.host_id) + except Exception: + raise HTTPException(400, "invalid device certificate") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= s.max_devices: + raise HTTPException(503, "device capacity reached") + try: + fp = fingerprint(body.cert) + store.add_device(body.host_id, body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "device already exists") from None + await r.set("hn:device:" + body.host_id, fp) + store.audit("admin", "register", body.host_id, "success") + return {"host_id": body.host_id, "fingerprint": fp} + + @app.get("/admin/devices") + async def devices(request: Request): + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,owner,revoked FROM devices ORDER BY host_id").fetchall() + return {"devices": [{**dict(row), "online": bool(await r.exists("hn:online:" + row["host_id"]))} for row in rows]} + + @app.post("/admin/devices/{host}/revoke") + async def revoke(host: str, request: Request): + admin(request) + if not store.device(host): + raise HTTPException(404, "device not found") + # Persistent source of truth first; router block + disconnect follows. + store.revoke(host) + await r.incr("hn:web-generation:" + host) + await r.set("hn:revoked:" + host, "1") + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "revoke") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "revoked", "result": "设备已停用,结果已丢弃。"}) + store.audit("admin", "revoke", host, "success") + return {"status": "revoked"} + + @app.post("/admin/reconcile") + async def reconcile(request: Request): + """Rebuild router admission metadata after Redis recovery; no dialogue data.""" + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall() + for row in rows: + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + return {"devices": len(rows)} + + @app.post("/admin/devices/{host}/unbind") + async def admin_unbind(host: str, request: Request): + admin(request) + device = store.device(host) + if not device: + raise HTTPException(404, "device not found") + if device["owner"]: + store.unbind(device["owner"]) + await r.incr("hn:web-generation:" + host) + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "unbind") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + store.audit("admin", "unbind", host, "success") + return {"status": "unbound", "host_id": host, "revoked": bool(device["revoked"])} + + async def make_binding_code(owner): + if store.by_owner(owner): + return "你已绑定电脑,请先发送 /解绑。" + await rate_limit(r, "binding:" + owner, 5) + code = secrets.token_urlsafe(12) + await r.set("hn:bind:" + hashlib.sha256(code.encode()).hexdigest(), owner, ex=300) + return "请在本人电脑的桥接客户端输入绑定码(5分钟有效):" + code + + @app.post("/device/bind") + async def bind(body: Bind, request: Request): + await rate_limit(r, "bind:" + peer(request), 20) + device = store.device(body.host_id) + if not device or device["revoked"]: + raise HTTPException(404, "device unavailable") + try: + public = verify_device(device["cert"], s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, public) + if proof["host_id"] != body.host_id or abs(time.time() - proof["ts"]) > 90: + raise ValueError("invalid proof") + code = proof["code"] + if not isinstance(code, str) or len(code) > 128: + raise ValueError("invalid code") + except Exception: + raise HTTPException(403, "invalid device proof") from None + key = "hn:bind:" + hashlib.sha256(code.encode()).hexdigest() + owner = await r.get(key) + if not owner: + raise HTTPException(404, "binding code expired or consumed") + # GETDEL is atomic across connectors. Invalid device proofs never reach this point. + owner = await r.getdel(key) + if not owner: + raise HTTPException(404, "binding code consumed") + try: + store.bind(body.host_id, owner) + except (*INTEGRITY_ERRORS, ValueError): + raise HTTPException(409, "owner/device already bound; request new code") from None + await r.incr("hn:web-generation:" + body.host_id) + store.audit(owner, "bind", body.host_id, "success") + return sign({"host_id": body.host_id, "owner": owner, "ts": int(time.time())}, broker.signer) + + @app.post("/admin/test/binding-code") + async def test_code(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return {"message": await make_binding_code(body.open_id)} + + @app.post("/admin/test/dispatch") + async def test_dispatch(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return await broker.dispatch(body.open_id, body.text, body.turn_id) + + async def handle_event(data): + header, event = data.get("header", {}), data.get("event", {}) + if header.get("event_type") != "im.message.receive_v1": + return + message, sender = event.get("message", {}), event.get("sender", {}) + owner = sender.get("sender_id", {}).get("open_id") + message_id = message.get("message_id", "") + if (message.get("chat_type") != "p2p" or message.get("message_type") != "text" + or sender.get("sender_type") != "user" or not owner + or not re.fullmatch(r"[A-Za-z0-9_-]{1,128}", message_id)): + return + if header.get("app_id") != s.feishu_app_id: + return + try: + text = json.loads(message["content"])["text"].strip() + if not text or len(text) > 8000: + return + # Persistent receipt BEFORE any side effect. Restarts do not execute a delivery twice. + event_turn = "event_" + hashlib.sha256(message_id.encode()).hexdigest() + if not store.begin_turn(event_turn, owner, "event"): + return + if text == "/绑定": + result = await make_binding_code(owner) + elif text == "/解绑": + old = store.by_owner(owner) + store.unbind(owner) + if old: + await r.incr("hn:web-generation:" + old["host_id"]) + await r.publish("hn:kill:" + old["host_id"], "unbind") + for item in broker.pending.values(): + if item.host == old["host_id"] and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + result = "已解绑;重新绑定后请更新本机绑定信息。" + elif text == "/主机名" or text.startswith("/回显 "): + result = (await broker.dispatch(owner, text, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + else: + instruction = text if text.startswith("/助手 ") else "/助手 " + text + result = (await broker.dispatch(owner, instruction, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + await f.reply(message_id, result[:s.max_result]) + store.finish(event_turn, "replied") + except Exception: + store.audit(owner, "event", message_id, "failed_or_unknown") + + @app.post("/internal/events") + async def internal_event(request: Request): + require_secret(request.headers.get("X-Internal-Secret"), s.internal_secret) + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(await request.json())) + return {"accepted": True} + + @app.post("/feishu/event") + async def webhook(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if not s.feishu_encrypt_key or not s.feishu_verification_token: + raise HTTPException(503, "webhook credentials not configured; use selected event source") + await rate_limit(r, "webhook:" + peer(request), 600) + raw = await request.body() + timestamp, nonce, signature = (request.headers.get(x, "") for x in + ("X-Lark-Request-Timestamp", "X-Lark-Request-Nonce", "X-Lark-Signature")) + try: + if abs(time.time() - int(timestamp)) > 300 or not nonce or len(nonce) > 128: + raise ValueError("stale request") + expected = hashlib.sha256((timestamp + nonce + s.feishu_encrypt_key).encode() + raw).hexdigest() + if not hmac.compare_digest(expected, signature): + raise ValueError("bad signature") + data = json.loads(raw) + if "encrypt" in data: + cipher = base64.b64decode(data["encrypt"], validate=True) + decryptor = Cipher(algorithms.AES(hashlib.sha256(s.feishu_encrypt_key.encode()).digest()), + modes.CBC(cipher[:16])).decryptor() + padded = decryptor.update(cipher[16:]) + decryptor.finalize() + unpadder = padding.PKCS7(128).unpadder() + data = json.loads(unpadder.update(padded) + unpadder.finalize()) + token = data.get("token", data.get("header", {}).get("token", "")) + if not hmac.compare_digest(token, s.feishu_verification_token): + raise ValueError("invalid verification token") + except Exception: + raise HTTPException(403, "invalid event authentication") from None + if data.get("type") == "url_verification": + return {"challenge": data.get("challenge", "")} + # Same delivery is ACKed again; durable message_id receipt handles execution deduplication. + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(data)) + return {"code": 0} + + @app.get("/api/whoami") + async def whoami(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(r, "whoami:" + peer(request)) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await f.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "no device bound") + if not s.device_domain or not re.fullmatch(r"[a-zA-Z0-9.-]+", s.device_domain): + raise HTTPException(503, "device web domain not configured") + return {"host_id": device["host_id"], "url": f"https://h-{device['host_id']}.{s.device_domain}/", + "online": bool(await r.exists("hn:online:" + device["host_id"])), + "device_login_required": True} + + @app.get("/h5", response_class=HTMLResponse) + async def h5(): + from importlib.resources import files + return HTMLResponse(files("relay").joinpath("web/index.html").read_text(encoding="utf-8"), + headers={"Content-Security-Policy": "default-src 'self'; script-src 'self'; style-src 'self'; frame-ancestors 'self'; base-uri 'none'; form-action 'self'"}) + + @app.get("/assets/{name}") + async def asset(name: str): + from importlib.resources import files + from starlette.responses import Response + if name not in {"app.js", "style.css"}: + raise HTTPException(404, "not found") + return Response(files("relay").joinpath("web/" + name).read_bytes(), + media_type="text/javascript" if name.endswith(".js") else "text/css") + + @app.get("/proxy") + async def proxy(request: Request): + if not s.proxy_enabled or not s.feishu_enabled: + raise HTTPException(404, "not found") + auth = request.headers.get("Authorization", "") + target = request.headers.get("X-Relay-Target", "") + decoded = unquote(target) + parts = urlsplit(target) + allowed = s.proxy_paths.split(",") + if (not auth.startswith("Bearer ") or len(auth) > 8192 or len(target) > 4096 + or parts.scheme or parts.netloc or parts.fragment or target.startswith("//") + or any(x in decoded for x in ("..", "\\", "\r", "\n", "#", "%")) + or not any(parts.path == x or parts.path.startswith(x + "/") for x in allowed)): + raise HTTPException(403, "target not allowed") + try: + info = await f.user_info(auth[7:]) + await rate_limit(r, "proxy:" + info["open_id"], 60) + # Buffer bounded response; no disk/cache, redirects never followed. + async with f.http.stream("GET", target, headers={"Authorization": auth}) as response: + if response.status_code in {301, 302, 303, 307, 308}: + raise HTTPException(502, "upstream redirect refused") + chunks, size = [], 0 + async for chunk in response.aiter_bytes(): + size += len(chunk) + if size > 2 * 1024 * 1024: + raise HTTPException(502, "response too large") + chunks.append(chunk) + from starlette.responses import Response + store.audit(info["open_id"], "proxy", parts.path, str(response.status_code)) + return Response(b"".join(chunks), status_code=response.status_code, + media_type=response.headers.get("content-type", "application/json")) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + + return app diff --git a/container-code/enrollment-signer/relay/crypto.py b/container-code/enrollment-signer/relay/crypto.py new file mode 100644 index 0000000..dd962e5 --- /dev/null +++ b/container-code/enrollment-signer/relay/crypto.py @@ -0,0 +1,140 @@ +"""Signed ephemeral X25519 envelopes. The router never receives plaintext keys.""" +import base64 +import hashlib +import json +import secrets +import time +from datetime import datetime, timedelta, timezone + +from cryptography import x509 +from cryptography.hazmat.primitives import hashes, serialization +from cryptography.hazmat.primitives.asymmetric import ed25519, x25519 +from cryptography.hazmat.primitives.ciphers.aead import AESGCM +from cryptography.hazmat.primitives.kdf.hkdf import HKDF +from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID + + +def b64(data: bytes) -> str: + return base64.urlsafe_b64encode(data).decode() + + +def unb64(data: str) -> bytes: + return base64.b64decode(data.encode(), altchars=b"-_", validate=True) + + +def canonical(data: dict) -> bytes: + return json.dumps(data, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode() + + +def raw_public(key) -> str: + return b64(key.public_bytes(serialization.Encoding.Raw, serialization.PublicFormat.Raw)) + + +def pem_private(key) -> bytes: + return key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, + serialization.NoEncryption()) + + +def load_private(path): + protected = path.with_name(path.name + ".dpapi") + if protected.is_file(): + from .windows_keys import unprotect + data = unprotect(protected.read_bytes()) + else: + data = path.read_bytes() + return serialization.load_pem_private_key(data, password=None) + + +def load_public(path): + return serialization.load_pem_public_key(path.read_bytes()) + + +def fingerprint(pem: str) -> str: + return x509.load_pem_x509_certificate(pem.encode()).fingerprint(hashes.SHA256()).hex() + + +def verify_device(pem: str, ca_pem: bytes, host_id: str): + """v1 deliberately accepts only directly issued Ed25519 device certificates.""" + cert = x509.load_pem_x509_certificate(pem.encode()) + ca = x509.load_pem_x509_certificate(ca_pem) + now = datetime.now(timezone.utc) + if not (ca.not_valid_before_utc <= now <= ca.not_valid_after_utc): + raise ValueError("expired CA") + if not ca.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("not a CA") + cert.verify_directly_issued_by(ca) + if not (cert.not_valid_before_utc <= now <= cert.not_valid_after_utc): + raise ValueError("expired certificate") + if cert.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("CA cannot be used as device") + if ExtendedKeyUsageOID.CLIENT_AUTH not in cert.extensions.get_extension_for_class(x509.ExtendedKeyUsage).value: + raise ValueError("clientAuth missing") + names = cert.extensions.get_extension_for_class(x509.SubjectAlternativeName).value + if names.get_values_for_type(x509.UniformResourceIdentifier) != [f"urn:hainuo:host:{host_id}"]: + raise ValueError("host identity mismatch") + public = cert.public_key() + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("v1 requires Ed25519 device key") + return public + + +def sign(data: dict, key) -> dict: + return {**data, "signature": b64(key.sign(canonical(data)))} + + +def verified(data: dict, key) -> dict: + body = {k: v for k, v in data.items() if k != "signature"} + key.verify(unb64(data["signature"]), canonical(body)) + return body + + +def seal(payload: dict, recipient_public: str, signer, *, host_id: str, session_id: str) -> dict: + ephemeral = x25519.X25519PrivateKey.generate() + header = {"v": 1, "host_id": host_id, "session_id": session_id, + "ts": int(time.time()), "nonce": b64(secrets.token_bytes(12)), + "ephemeral": raw_public(ephemeral.public_key())} + shared = ephemeral.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(recipient_public))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + cipher = AESGCM(key).encrypt(unb64(header["nonce"]), canonical(payload), canonical(header)) + return sign({**header, "ciphertext": b64(cipher)}, signer) + + +def open_envelope(envelope: dict, recipient_private, sender_public, *, host_id: str, session_id: str) -> dict: + body = verified(envelope, sender_public) + if body["v"] != 1 or body["host_id"] != host_id or body["session_id"] != session_id: + raise ValueError("wrong destination/session") + if abs(time.time() - body["ts"]) > 90: + raise ValueError("expired envelope") + header = {k: v for k, v in body.items() if k != "ciphertext"} + shared = recipient_private.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(body["ephemeral"]))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + plain = AESGCM(key).decrypt(unb64(body["nonce"]), unb64(body["ciphertext"]), canonical(header)) + return json.loads(plain) + + +def issue_ca(): + key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Hainuo device test CA")]) + now = datetime.now(timezone.utc) + cert = (x509.CertificateBuilder().subject_name(name).issuer_name(name) + .public_key(key.public_key()).serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=3650)) + .add_extension(x509.BasicConstraints(ca=True, path_length=0), critical=True) + .add_extension(x509.KeyUsage(False, False, False, False, False, True, True, False, False), critical=True) + .sign(key, algorithm=None)) + return key, cert + + +def issue_device(ca_key, ca_cert, host_id: str, public_key, days=30): + now = datetime.now(timezone.utc) + return (x509.CertificateBuilder() + .subject_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, host_id)])) + .issuer_name(ca_cert.subject).public_key(public_key) + .serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=days)) + .add_extension(x509.BasicConstraints(ca=False, path_length=None), critical=True) + .add_extension(x509.SubjectAlternativeName([x509.UniformResourceIdentifier(f"urn:hainuo:host:{host_id}")]), critical=False) + .add_extension(x509.ExtendedKeyUsage([ExtendedKeyUsageOID.CLIENT_AUTH]), critical=False) + .sign(ca_key, algorithm=None)) diff --git a/container-code/enrollment-signer/relay/device.py b/container-code/enrollment-signer/relay/device.py new file mode 100644 index 0000000..a8fe010 --- /dev/null +++ b/container-code/enrollment-signer/relay/device.py @@ -0,0 +1,216 @@ +"""Reference PC bridge. No shell/PowerShell execution; local read-only diagnostics.""" +import asyncio +import contextlib +import json +import os +import secrets +import socket +import sqlite3 +import time +from pathlib import Path +from urllib.parse import urlsplit + +import httpx +from cryptography.hazmat.primitives.asymmetric import x25519 +from websockets.asyncio.client import connect + +from .crypto import (load_private, load_public, open_envelope, raw_public, seal, sign, + verified, verify_device) + + +class DeviceJournal: + def __init__(self, path): + path.parent.mkdir(parents=True, exist_ok=True) + self.db = sqlite3.connect(path) + self.db.execute("CREATE TABLE IF NOT EXISTS consumed(turn_id TEXT PRIMARY KEY, ts REAL NOT NULL)") + self.db.commit() + + def consume(self, turn): + with self.db: + return self.db.execute("INSERT OR IGNORE INTO consumed VALUES(?,?)", (turn, time.time())).rowcount == 1 + + def close(self): + self.db.close() + + +async def execute_readonly(text, root: Path | None): + if text in {"/主机名", "hostname"}: + return socket.gethostname() + if text.startswith("/回显 "): + return text[4:][:16000] + if text.startswith("/读取 "): + if root is None: + return "未配置测试文件目录。" + base = root.resolve(strict=True) + try: + path = (base / text[4:].strip()).resolve(strict=True) + if path == base or not path.is_relative_to(base) or not path.is_file(): + return "拒绝:文件不在允许的测试目录。" + if path.stat().st_size > 32768: + return "拒绝:测试文件超过32KB。" + return path.read_text(encoding="utf-8")[:16000] + except (OSError, UnicodeError, ValueError): + return "拒绝:文件不存在、不可读或不是UTF-8文本。" + return "当前桥接客户端仅开放只读诊断:/主机名、/回显 内容、/读取 相对路径;尚未接入海诺本地Agent接口。" + + +class LocalAgent: + """Explicit adapter contract, not a claim of compatibility with installer 1.065.""" + def __init__(self, url, transport=None): + parts = urlsplit(url) + if parts.scheme != "http" or parts.hostname not in {"127.0.0.1", "::1"} or parts.username or parts.password or parts.fragment: + raise ValueError("local Agent must use a fixed loopback HTTP endpoint") + self.url = url + self.http = httpx.AsyncClient(timeout=30, follow_redirects=False, transport=transport, trust_env=False) + + async def execute(self, text, owner, turn): + headers = {} + if os.environ.get("HN_LOCAL_AGENT_TOKEN"): + headers["Authorization"] = "Bearer " + os.environ["HN_LOCAL_AGENT_TOKEN"] + try: + async with self.http.stream("POST", self.url, headers=headers, + json={"text": text, "open_id": owner, "turn_id": turn, + "channel": "im", "read_only": True}) as response: + if response.status_code != 200: + return "本机Agent未返回成功结果,请查看本机服务状态。" + body = b"" + async for chunk in response.aiter_bytes(): + body += chunk + if len(body) > 65536: + return "本机Agent结果超过限制。" + result = json.loads(body)["result"] + if not isinstance(result, str): + return "本机Agent响应格式不匹配。" + return result[:16000] + except (httpx.HTTPError, ValueError, KeyError): + return "本机Agent连接失败或响应格式不匹配。" + + async def close(self): + await self.http.aclose() + + +async def bind_device(directory: Path, connector_url: str, code: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + private = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "code": code, "ts": int(time.time())}, private) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as http: + response = await http.post(connector_url.rstrip("/") + "/device/bind", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"binding failed: HTTP {response.status_code}") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt["host_id"] != config["host_id"]: + raise ValueError("wrong binding receipt") + config["owner"] = receipt["owner"] + temp = directory / "device.json.tmp" + temp.write_text(json.dumps(config, ensure_ascii=False, indent=2), encoding="utf-8") + temp.replace(directory / "device.json") + + +async def pair_browser(directory: Path, connector_url: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8-sig")) + if not config.get("owner"): + raise RuntimeError("device must be bound first") + key = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "owner": config["owner"], + "purpose": "h5-pair-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as client: + response = await client.post(connector_url.rstrip("/") + "/device/web-pair", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"browser pairing failed: HTTP {response.status_code}") + result = verified(response.json(), load_public(directory / "connector.pub")) + if result["host_id"] != config["host_id"]: + raise ValueError("wrong pairing receipt") + return result + + +async def run_device(directory: Path, router_url: str, **kwargs): + from .process_lock import DeviceProcessLock + with DeviceProcessLock(directory): + return await _run_device(directory, router_url, **kwargs) + + +async def _run_device(directory: Path, router_url: str, *, root=None, once=False, ssl_context=None, + stop_event=None, connected_event=None, agent_url=None, hainuo_config=None): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + if not config.get("owner"): + raise RuntimeError("bind device before starting") + host, owner = config["host_id"], config["owner"] + key = load_private(directory / "device.key") + connector = load_public(directory / "connector.pub") + cert = (directory / "device.pem").read_text(encoding="utf-8") + verify_device(cert, (directory / "ca.pem").read_bytes(), host) + journal = DeviceJournal(directory / "journal.sqlite") + if agent_url and hainuo_config: + raise ValueError("choose one Agent adapter") + if hainuo_config: + from .hainuo1065 import Hainuo1065Agent + agent = Hainuo1065Agent(hainuo_config) + else: + agent = LocalAgent(agent_url) if agent_url else None + stop_event = stop_event or asyncio.Event() + delay = 1 + try: + while not stop_event.is_set(): + try: + kwargs = {"ssl": ssl_context} if router_url.startswith("wss://") and ssl_context else {} + async with connect(router_url, max_size=262144, open_timeout=10, ping_interval=10, + proxy=None, **kwargs) as ws: + challenge = json.loads(await ws.recv())["challenge"] + ephemeral = x25519.X25519PrivateKey.generate() + session = secrets.token_hex(16) + proof = sign({"host_id": host, "session_id": session, "challenge": challenge, + "kx": raw_public(ephemeral.public_key()), "ts": int(time.time())}, key) + await ws.send(json.dumps({"host_id": host, "cert": cert, "proof": proof})) + accepted = json.loads(await ws.recv()) + if accepted.get("type") != "connected": + raise RuntimeError("device connection rejected") + print("设备连接成功,已绑定本人海能Work身份。", flush=True) + if connected_event: + connected_event.set() + delay = 1 + while not stop_event.is_set(): + try: + raw = await asyncio.wait_for(ws.recv(), timeout=1) + except asyncio.TimeoutError: + continue + data = json.loads(raw) + payload = open_envelope(data["envelope"], ephemeral, connector, + host_id=host, session_id=session) + if payload.get("owner") != owner or payload.get("type") != "instruction": + continue + turn = payload["turn_id"] + if not isinstance(turn, str) or len(turn) > 128: + continue + if not journal.consume(turn): + result = "该请求已处理,拒绝重复执行。" + elif payload.get("read_only") is not True or payload.get("channel") != "im": + result = "拒绝:通道或操作类型不受支持。" + else: + if agent and payload["text"].startswith("/助手 "): + result = await agent.execute(payload["text"][4:], owner, turn) + else: + result = await execute_readonly(payload["text"], root) + reply = seal({"type": "result", "turn_id": turn, "result": result}, + payload["reply_key"], key, host_id=host, session_id=session) + await ws.send(json.dumps({"type": "reply", "turn_id": turn, "envelope": reply})) + if once: + return + except asyncio.CancelledError: + raise + except Exception as error: + if once: + raise + from websockets.exceptions import ConnectionClosed + if isinstance(error, ConnectionClosed) and error.rcvd and error.rcvd.code == 1008: + print("设备被中继拒绝:请检查是否有另一桥接进程、设备准入或证书状态。", flush=True) + # No payloads/URLs/credentials in logs, bounded reconnect backoff + jitter. + print("设备连接中断,等待重连。", flush=True) + with contextlib.suppress(asyncio.TimeoutError): + await asyncio.wait_for(stop_event.wait(), timeout=delay + secrets.randbelow(1000) / 1000) + delay = min(delay * 2, 30) + finally: + journal.close() + if agent: + await agent.close() diff --git a/container-code/enrollment-signer/relay/enrollment.py b/container-code/enrollment-signer/relay/enrollment.py new file mode 100644 index 0000000..b4ac77f --- /dev/null +++ b/container-code/enrollment-signer/relay/enrollment.py @@ -0,0 +1,178 @@ +"""Device registration with key possession proof; Work binding is separate.""" +import json +import secrets +import time +import httpx + +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey +from fastapi import APIRouter, HTTPException, Request +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .crypto import verified, sign, raw_public, verify_device, fingerprint +from .web_session import digest +from fastapi.responses import JSONResponse + + +def validate_enrollment(body): + try: + key = serialization.load_pem_public_key(body.public_key.encode()) + if not isinstance(key, Ed25519PublicKey): + raise ValueError() + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["purpose"] != "device-enroll-v1" + or abs(time.time() - proof["ts"]) > 90 or len(proof["nonce"]) != 32): + raise ValueError() + return key + except Exception: + raise HTTPException(403, "invalid enrollment proof") from None + + +async def request_certificate(settings, body): + async with httpx.AsyncClient(trust_env=False, timeout=15, follow_redirects=False) as http: + response = await http.post(settings.enrollment_signer_url.rstrip("/") + "/issue", + headers={"X-Internal-Secret": settings.internal_secret}, + json=body.model_dump()) + response.raise_for_status() + return response.json()["cert"] + + +class EnrollmentRequest(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + public_key: str = Field(max_length=1024) + proof: dict + + +class EnrollmentApproval(BaseModel): + cert: str = Field(max_length=10000) + + +def enrollment_router(settings, redis, store, signer, admin): + router = APIRouter() + + @router.post("/device/register") + async def automatic_registration(body: EnrollmentRequest, request: Request): + # No human approval: eligibility follows intranet access plus Work app scope. + # Proof/TLS/limits protect identity and resources, and do not add user approval. + await rate_limit(redis, "auto-register-ip:" + peer(request), 120) + key = validate_enrollment(body) + await rate_limit(redis, "auto-register-host:" + body.host_id, 30) + existing = store.device(body.host_id) + + def check_existing(device): + if device["revoked"]: + raise HTTPException(403, "device revoked") + try: + registered = verify_device(device["cert"], settings.ca_cert.read_bytes(), body.host_id) + if raw_public(registered) != raw_public(key): + raise ValueError() + except Exception: + raise HTTPException(409, "existing device identity mismatch or certificate expired") from None + return device["cert"] + + if existing: + cert = check_existing(existing) + else: + if not settings.enrollment_signer_url: + raise HTTPException(503, "automatic registration is not enabled; update ECS first") + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= settings.max_devices: + raise HTTPException(503, "device capacity reached") + await rate_limit(redis, "auto-register-total", settings.max_pending, 3600) + try: + cert = await request_certificate(settings, body) + issued = verify_device(cert, settings.ca_cert.read_bytes(), body.host_id) + if raw_public(issued) != raw_public(key): + raise ValueError() + except Exception: + raise HTTPException(503, "certificate issuer unavailable or invalid response") from None + from .store import INTEGRITY_ERRORS + try: + store.add_device(body.host_id, cert, fingerprint(cert)) + except INTEGRITY_ERRORS: + # A retry/concurrent request must return the original certificate. + cert = check_existing(store.device(body.host_id)) + store.audit("device", "automatic_registration", body.host_id, "success") + await redis.set("hn:device:" + body.host_id, fingerprint(cert)) + return no_cache(JSONResponse(sign({"status": "registered", "host_id": body.host_id, + "cert": cert}, signer), status_code=201 if not existing else 200)) + + @router.post("/device/enroll") + async def request_enrollment(body: EnrollmentRequest, request: Request): + await rate_limit(redis, "enroll-ip:" + peer(request), 10) + validate_enrollment(body) + await rate_limit(redis, "enroll-host:" + body.host_id, 3, 3600) + if store.device(body.host_id): + raise HTTPException(409, "device already registered; recover its original certificate") + # Global rolling capacity bound; prevents anonymous request-table exhaustion. + await rate_limit(redis, "enroll-global", settings.max_pending, 1800) + identifier, claim_secret = secrets.token_hex(16), secrets.token_urlsafe(32) + record = {"host_id": body.host_id, "public_key": body.public_key, + "claim_hash": digest(claim_secret), "created": time.time(), "status": "pending"} + await redis.set("hn:enroll:" + identifier, json.dumps(record), ex=1800) + return no_cache(JSONResponse(sign({"request_id": identifier, "claim_secret": claim_secret, + "host_id": body.host_id, "expires_in": 1800}, signer), status_code=201)) + + @router.get("/device/enroll/{identifier}") + async def claim(identifier: str, request: Request): + await rate_limit(redis, "enroll-poll:" + peer(request), 60) + data = await redis.get("hn:enroll:" + identifier) + if not data: + raise HTTPException(404, "request expired") + record = json.loads(data) + import hmac + if not hmac.compare_digest(record["claim_hash"], digest(request.headers.get("X-Enrollment-Secret", ""))): + raise HTTPException(404, "request unavailable") + if record["status"] == "pending": + return no_cache(JSONResponse({"status": "pending"}, status_code=202)) + device = store.device(record["host_id"]) + if not device or device["revoked"]: + raise HTTPException(403, "device unavailable") + # Recoverable for its original claimant for the request TTL; public certificate only. + return no_cache(JSONResponse(sign({"status": "approved", "host_id": record["host_id"], + "request_id": identifier, "cert": device["cert"]}, signer))) + + @router.get("/admin/enrollments/{identifier}") + async def inspect(identifier: str, request: Request): + admin(request) + encoded = await redis.get("hn:enroll:" + identifier) + if not encoded: + raise HTTPException(404, "request expired") + record = json.loads(encoded) + return {k: v for k, v in record.items() if k != "claim_hash"} + + @router.post("/admin/enrollments/{identifier}/approve") + async def approve(identifier: str, body: EnrollmentApproval, request: Request): + admin(request) + encoded = await redis.get("hn:enroll:" + identifier) + if not encoded: + raise HTTPException(404, "request expired") + record = json.loads(encoded) + try: + public = verify_device(body.cert, settings.ca_cert.read_bytes(), record["host_id"]) + requested = serialization.load_pem_public_key(record["public_key"].encode()) + if raw_public(public) != raw_public(requested): + raise ValueError() + except Exception: + raise HTTPException(400, "certificate does not match enrollment") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= settings.max_devices: + raise HTTPException(503, "device capacity reached") + fp = fingerprint(body.cert) + existing = store.device(record["host_id"]) + if existing and (existing["revoked"] or existing["fingerprint"] != fp): + raise HTTPException(409, "device conflict") + if not existing: + from .store import INTEGRITY_ERRORS + try: + store.add_device(record["host_id"], body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "concurrent device registration") from None + await redis.set("hn:device:" + record["host_id"], fp) + record["status"] = "approved" + remaining = await redis.ttl("hn:enroll:" + identifier) + if remaining < 1: + raise HTTPException(409, "request expired during approval") + await redis.set("hn:enroll:" + identifier, json.dumps(record), ex=remaining) + store.audit("admin", "enrollment_approved", record["host_id"], "success") + return {"status": "approved", "host_id": record["host_id"]} + + return router diff --git a/container-code/enrollment-signer/relay/enrollment_signer.py b/container-code/enrollment-signer/relay/enrollment_signer.py new file mode 100644 index 0000000..6089d02 --- /dev/null +++ b/container-code/enrollment-signer/relay/enrollment_signer.py @@ -0,0 +1,43 @@ +"""Internal automatic CA issuer. No published host port, no application credentials.""" +import os +from pathlib import Path + +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from fastapi import FastAPI, Request +from .common import BodyLimitMiddleware, require_secret +from .crypto import load_private, issue_device, raw_public, verify_device +from .enrollment import EnrollmentRequest, validate_enrollment + + +def create_app(*, ca_key_path=None, ca_cert_path=None, secret=None): + key = load_private(Path(ca_key_path or os.environ.get("HN_ISSUER_CA_KEY", "/issuer/ca.key"))) + ca_bytes = Path(ca_cert_path or os.environ.get("HN_ISSUER_CA_CERT", "/trust/ca.pem")).read_bytes() + ca = x509.load_pem_x509_certificate(ca_bytes) + if raw_public(key.public_key()) != raw_public(ca.public_key()): + raise ValueError("issuer CA key mismatch") + token = secret or os.environ.get("HN_INTERNAL_SECRET", "") + if len(token) < 32: + raise ValueError("issuer requires internal credential") + app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=16384) + + @app.get("/ready") + async def ready(): + return {"status": "ready"} + + @app.post("/issue") + async def issue(body: EnrollmentRequest, request: Request): + require_secret(request.headers.get("X-Internal-Secret"), token) + public = validate_enrollment(body) + cert = issue_device(key, ca, body.host_id, public).public_bytes(serialization.Encoding.PEM).decode() + verify_device(cert, ca_bytes, body.host_id) + return {"cert": cert} + + return app + + +if __name__ == "__main__": + import uvicorn + uvicorn.run("relay.enrollment_signer:create_app", factory=True, host="0.0.0.0", port=8000, + workers=1, access_log=False, proxy_headers=False, log_level="warning") diff --git a/container-code/enrollment-signer/relay/hainuo1065.py b/container-code/enrollment-signer/relay/hainuo1065.py new file mode 100644 index 0000000..6f7735b --- /dev/null +++ b/container-code/enrollment-signer/relay/hainuo1065.py @@ -0,0 +1,107 @@ +"""Adapter for the native API actually found in installer 1.065. + +Loopback only, independent API key, no platform secrets, no redirects or retries. +The included native launcher offers only text inference with no tools. +""" +import hashlib +import json +import re +from pathlib import Path +from urllib.parse import urlsplit + +import httpx + + +class NativeAPIError(RuntimeError): + """Contains a sanitized status only, never response body or credentials.""" + + +def read_native_config(path): + path = Path(path) + data = json.loads(path.read_text(encoding="utf-8-sig")) + url = data.get("url", "") + parts = urlsplit(url) + if (parts.scheme != "http" or parts.hostname != "127.0.0.1" or not parts.port + or parts.username or parts.password or parts.query or parts.fragment + or parts.path not in {"", "/"}): + raise ValueError("native API must be a fixed IPv4 loopback HTTP base URL") + if not isinstance(data.get("api_key"), str) or len(data["api_key"]) < 32: + raise ValueError("native API requires a dedicated key of at least 32 characters") + if data.get("mode") not in {"text-only", "profile-tools"}: + raise ValueError("unsupported native API mode") + data["url"] = url.rstrip("/") + return data + + +class Hainuo1065Agent: + def __init__(self, config_path, transport=None): + self.config = read_native_config(config_path) + self.http = httpx.AsyncClient(base_url=self.config["url"], timeout=120, + follow_redirects=False, trust_env=False, transport=transport) + self.headers = {"Authorization": "Bearer " + self.config["api_key"]} + + async def _json(self, method, path, **kwargs): + try: + async with self.http.stream(method, path, headers={**self.headers, **kwargs.pop("headers", {})}, **kwargs) as response: + if response.status_code != 200: + raise NativeAPIError(f"native API HTTP {response.status_code}") + body = bytearray() + async for chunk in response.aiter_bytes(): + body.extend(chunk) + if len(body) > 262144: + raise NativeAPIError("native API response exceeds limit") + data = json.loads(body) + if not isinstance(data, dict): + raise NativeAPIError("native API response is not an object") + return data + except (httpx.HTTPError, ValueError): + raise NativeAPIError("native API connection/JSON failure") from None + + async def probe(self): + health = await self._json("GET", "/health") + if health.get("platform") != "hermes-agent" or health.get("status") != "ok": + raise NativeAPIError("endpoint is not the native Hermes API") + capabilities = await self._json("GET", "/v1/capabilities") + policy = capabilities.get("hainuo_relay_policy", {}) + # A stock unrestricted API endpoint is deliberately not sufficient. + expected_mode = self.config["mode"] + expected_tools = "profile" if expected_mode == "profile-tools" else "disabled" + if policy != {"mode": expected_mode, "tools": expected_tools, "contract": "hainuo-1065-v1"}: + raise NativeAPIError("native API policy missing; start the supplied restricted launcher") + models = await self._json("GET", "/v1/models") + model = self.config.get("model") + available = models.get("data") + if not isinstance(available, list) or not available or not all(isinstance(x, dict) and isinstance(x.get("id"), str) for x in available): + raise NativeAPIError("native model list invalid") + if model and model not in {x["id"] for x in available}: + raise NativeAPIError("configured model alias unavailable") + self.model = model or available[0]["id"] + return {"status": "ready", "platform": "hermes-agent", "model": self.model, + "mode": expected_mode, "version": health.get("version", "unknown")} + + async def execute(self, text, owner, turn): + try: + # Recheck the actual local service before each inference, including reconnects. + await self.probe() + digest = hashlib.sha256((owner + "\0" + turn).encode()).hexdigest() + data = await self._json("POST", "/v1/chat/completions", + headers={"Idempotency-Key": "hn-" + digest}, + json={"model": self.model, "stream": False, + "messages": [{"role": "user", "content": text}]}) + choices = data.get("choices") + if not isinstance(choices, list) or not choices or not isinstance(choices[0], dict): + raise NativeAPIError("native completion choices missing") + choice = choices[0] + message = choice.get("message", {}) + if (choice.get("finish_reason") != "stop" or message.get("tool_calls") + or not isinstance(message.get("content"), str) + or data.get("hermes", {}).get("failed") + or data.get("hermes", {}).get("partial")): + raise NativeAPIError("native completion incomplete or unexpected tool call") + return message["content"][:16000] + except NativeAPIError as error: + # Safe generated error strings only; never echo upstream model/API errors. + return "海诺原生接口未完成请求:" + str(error) + "。未自动重试。" + + async def close(self): + await self.http.aclose() diff --git a/container-code/enrollment-signer/relay/longconn.py b/container-code/enrollment-signer/relay/longconn.py new file mode 100644 index 0000000..5c12bbf --- /dev/null +++ b/container-code/enrollment-signer/relay/longconn.py @@ -0,0 +1,74 @@ +"""Optional official SDK event source, private platform domain explicitly configured.""" +import json +import logging +import os +import threading +import time + +import httpx +import redis +from .config import Settings + + +def main(): + import lark_oapi as lark + s = Settings(role="longconn") + if not s.feishu_enabled or s.feishu_event_source != "longconn": + raise RuntimeError("platform is disabled") + # The SDK may log websocket URLs, tokens or event bodies. Disable its logging entirely. + logging.disable(logging.CRITICAL) + r = redis.Redis.from_url(s.redis_url) + lease = r.lock("hn:longconn:leader", timeout=30, thread_local=False) + if not lease.acquire(blocking=False): + raise RuntimeError("another long-connection consumer owns the lease") + url = os.environ.get("HN_CONNECTOR_INTERNAL_URL", "http://connector:8000") + client = httpx.Client(base_url=url, timeout=10, follow_redirects=False) + done = threading.Event() + + def maintain_lease(): + while not done.wait(8): + try: + lease.extend(30, replace_ttl=True) + except Exception: + # Fail closed. Continuing after lease loss could create two consumers. + os._exit(2) + + threading.Thread(target=maintain_lease, daemon=True).start() + + def received(event): + data = json.loads(lark.JSON.marshal(event)) + response = client.post("/internal/events", headers={"X-Internal-Secret": s.internal_secret}, json=data) + if response.status_code != 200: + raise RuntimeError("connector did not accept event") + print(json.dumps({"event": "platform_event_accepted", "type": data.get("header", {}).get("event_type")}), flush=True) + + handler = lark.EventDispatcherHandler.builder("", "").register_p2_im_message_receive_v1(received).build() + class ObservableClient(lark.ws.Client): + async def _connect(self): + try: + await super()._connect() + if self._conn is not None: + print(json.dumps({"event": "platform_connected", "api_host": s.feishu_api_base}), flush=True) + except Exception as error: + print(json.dumps({"event": "platform_connect_failed", "error_type": type(error).__name__}), flush=True) + raise + + try: + # domain is an actual supported SDK argument; no public-cloud-domain monkey patch. + ObservableClient(s.feishu_app_id, s.feishu_app_secret, event_handler=handler, + domain=s.feishu_api_base.rstrip("/"), log_level=lark.LogLevel.ERROR).start() + finally: + done.set() + client.close() + try: + lease.release() + except redis.exceptions.LockError: + pass + + +if __name__ == "__main__": + try: + main() + except Exception as error: + print(json.dumps({"event": "platform_consumer_stopped", "error_type": type(error).__name__}), flush=True) + raise SystemExit(1) from None diff --git a/container-code/enrollment-signer/relay/oauth.py b/container-code/enrollment-signer/relay/oauth.py new file mode 100644 index 0000000..b04ce6e --- /dev/null +++ b/container-code/enrollment-signer/relay/oauth.py @@ -0,0 +1,127 @@ +import asyncio +import hashlib +import hmac +import secrets +import time +from dataclasses import dataclass +from urllib.parse import urlencode + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .platform import PlatformError + + +@dataclass +class Transaction: + secret_hash: str + expires: float + status: str = "waiting" + result: dict | None = None + + +class Empty(BaseModel): + pass + + +class Refresh(BaseModel): + refresh_token: str = Field(min_length=1, max_length=4096) + + +class OAuth: + """A single connector process owns transactions and tokens in memory only.""" + def __init__(self, settings, redis, platform): + self.settings, self.redis, self.platform = settings, redis, platform + self.pending: dict[str, Transaction] = {} + self.lock = asyncio.Lock() + self.router = APIRouter() + self.router.add_api_route("/oauth/start", self.start, methods=["POST"], status_code=201) + self.router.add_api_route("/oauth/cb", self.callback, methods=["GET"]) + self.router.add_api_route("/oauth/claim", self.claim, methods=["GET"]) + self.router.add_api_route("/oauth/pending", self.status, methods=["GET"]) + self.router.add_api_route("/oauth/refresh", self.refresh, methods=["POST"]) + + def prune(self): + now = time.monotonic() + for state in list(self.pending): + if self.pending[state].expires <= now: + del self.pending[state] + + def authenticated(self, state, secret): + self.prune() + item = self.pending.get(state) + digest = hashlib.sha256((secret or "").encode()).hexdigest() + if not item or not hmac.compare_digest(item.secret_hash, digest): + raise HTTPException(404, "transaction not found") + return item + + async def start(self, body: Empty, request: Request): + if not self.settings.feishu_enabled or not self.settings.oauth_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.redis, "oauth:start:" + peer(request), 20) + async with self.lock: + self.prune() + if len(self.pending) >= self.settings.max_pending: + raise HTTPException(503, "too many pending transactions") + state, secret = secrets.token_urlsafe(32), secrets.token_urlsafe(32) + self.pending[state] = Transaction(hashlib.sha256(secret.encode()).hexdigest(), time.monotonic() + 600) + redirect = self.settings.public_url.rstrip("/") + "/oauth/cb" + url = self.settings.feishu_authorize_url + "?" + urlencode({"app_id": self.settings.feishu_app_id, + "redirect_uri": redirect, "state": state}) + return no_cache(JSONResponse({"state": state, "claim_secret": secret, "authorization_url": url, + "redirect_uri": redirect, "expires_in": 600, + "code_ttl": 60, "poll_interval": 2}, status_code=201)) + + async def callback(self, request: Request, state: str = "", code: str = ""): + await rate_limit(self.redis, "oauth:cb:" + peer(request)) + async with self.lock: + self.prune() + item = self.pending.get(state) + if not code or len(code) > 4096 or not item or item.status != "waiting": + raise HTTPException(400, "invalid callback") + item.status = "exchanging" + try: + result = await self.platform.exchange(code) + except PlatformError: + async with self.lock: + item.status = "failed" + raise HTTPException(502, "authorization exchange failed") from None + async with self.lock: + if state not in self.pending or item.expires <= time.monotonic(): + self.pending.pop(state, None) + raise HTTPException(400, "authorization expired") + item.result, item.status = result, "ready" + return no_cache(HTMLResponse("

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

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

连接你的
工作电脑。

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

+
01 / 连接本人设备

输入电脑上的配对码

+

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

+
+
使用海能Work授权

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

+ +

正在检查网页会话…

+
PC需保持开机、联网,并运行原生API和桥接端。试点网页由中继在内存中转发正文,不保存聊天记录;当前页关闭后消息不保留。
+
diff --git a/container-code/enrollment-signer/relay/web/style.css b/container-code/enrollment-signer/relay/web/style.css new file mode 100644 index 0000000..a33d19c --- /dev/null +++ b/container-code/enrollment-signer/relay/web/style.css @@ -0,0 +1,3 @@ +:root{color-scheme:light;font-family:system-ui,-apple-system,"Segoe UI","Microsoft YaHei",sans-serif;color:#17372c;background:#f2f4ed}*{box-sizing:border-box}body{margin:0}main{max-width:820px;margin:0 auto;padding:60px 26px}.mark{font-size:13px;letter-spacing:.16em;color:#557764;font-weight:700}h1{font-size:clamp(42px,8vw,70px);letter-spacing:-.05em;line-height:1.12;font-weight:650;margin:40px 0 22px}.intro{font-size:17px;line-height:1.8;color:#56675a;max-width:520px}.card{background:#fff;border:1px solid #dbe3d8;border-radius:20px;padding:30px;margin:40px 0 24px;box-shadow:0 12px 44px #18372c06}.step{font-size:12px;letter-spacing:.1em;color:#6d806c}h2{font-size:24px;margin:16px 0}.card p{line-height:1.8;color:#5b6c60}button{background:#214e3a;color:white;border:0;border-radius:10px;font:inherit;font-weight:600;padding:16px 20px;width:100%;text-align:left;cursor:pointer;margin-top:16px}button span{float:right}button:disabled{opacity:.5;cursor:wait}button:focus-visible,a:focus-visible{outline:3px solid #d0a349;outline-offset:4px}#status{font-size:13px;margin-bottom:0}#open{display:block;margin-top:22px;color:#214e3a;font-weight:600}#open[hidden]{display:none}footer{color:#768373;font-size:12px;line-height:1.8}@media(max-width:480px){main{padding-top:36px}.card{padding:22px}h1{margin-top:28px}} + +[hidden]{display:none!important}.badge{float:right;font-size:11px;letter-spacing:0;background:#e4ebdf;border-radius:20px;padding:6px 10px}label{display:block;font-size:13px;color:#526c5b;margin:12px 0 8px}input,textarea{font:inherit;width:100%;border:1px solid #ccd8ce;border-radius:10px;padding:14px;color:#17372c;background:#fafcf8;resize:vertical}input:focus,textarea:focus{outline:2px solid #6c987b;outline-offset:2px}details{margin-top:24px;font-size:13px;color:#587561}summary{cursor:pointer}.chat-head{display:flex;align-items:center;justify-content:space-between;gap:20px}.secondary{width:auto;background:#edf2e9;color:#31523f;padding:9px 12px;font-size:12px;margin:0}.hint{font-size:13px}#messages{display:flex;flex-direction:column;gap:12px;margin:22px 0;max-height:420px;overflow:auto}.message{padding:14px 16px;border-radius:12px;line-height:1.8;white-space:pre-wrap;overflow-wrap:anywhere;font-size:15px}.message.user{background:#e5eee2;margin-left:36px}.message.assistant{background:#f2f5ef;margin-right:24px}.message.notice{background:#fbf4e5;color:#735c2e}#status{font-size:13px;color:#5f745f;line-height:1.8}#chat-panel h2{font-size:20px}footer{max-width:640px} diff --git a/container-code/enrollment-signer/relay/web_session.py b/container-code/enrollment-signer/relay/web_session.py new file mode 100644 index 0000000..1ac9adf --- /dev/null +++ b/container-code/enrollment-signer/relay/web_session.py @@ -0,0 +1,183 @@ +"""Pilot H5 pairing and conversation. Credentials never appear in URLs.""" +import hashlib +import json +import secrets +import time +from urllib.parse import urlsplit + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import JSONResponse +from pydantic import BaseModel, Field + +from .common import no_cache, peer, rate_limit +from .crypto import sign, verified, verify_device +from .platform import PlatformError + +COOKIE = "hn_web_session" + + +def digest(value): + return hashlib.sha256(value.encode()).hexdigest() + + +def origin_tuple(value): + try: + parts = urlsplit(value) + if parts.scheme not in {"https", "http"} or not parts.hostname or parts.username or parts.password: + return None + return parts.scheme, parts.hostname.lower(), parts.port or (443 if parts.scheme == "https" else 80) + except ValueError: + return None + + +class PairProof(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Claim(BaseModel): + code: str = Field(min_length=12, max_length=128) + + +class Chat(BaseModel): + text: str = Field(min_length=1, max_length=8000) + request_id: str = Field(pattern=r"^[a-f0-9]{32}$") + + +class WebSessions: + def __init__(self, settings, redis, store, broker, platform): + self.s, self.r, self.store, self.broker, self.platform = settings, redis, store, broker, platform + self.router = APIRouter() + for path, method, handler in [ + ("/device/web-pair", "POST", self.device_pair), + ("/web/claim", "POST", self.claim), + ("/web/session", "GET", self.session), + ("/web/platform-session", "POST", self.platform_login), + ("/web/chat", "POST", self.chat), + ("/web/logout", "POST", self.logout), + ]: + self.router.add_api_route(path, handler, methods=[method]) + + def check_origin(self, request): + actual = origin_tuple(request.headers.get("Origin", "")) + if not actual or actual != origin_tuple(self.s.public_url): + raise HTTPException(403, "same-origin browser request required") + + async def valid_device(self, record): + device = self.store.device(record["host_id"]) + if (not device or device["revoked"] or device["owner"] != record["owner"] + or device["fingerprint"] != record["fingerprint"]): + raise HTTPException(401, "device binding changed; pair again") + generation = await self.r.get("hn:web-generation:" + record["host_id"]) or "0" + if record.get("generation") != generation: + raise HTTPException(401, "device binding changed; pair again") + return device + + async def device_pair(self, body: PairProof, request: Request): + await rate_limit(self.r, "web-pair-ip:" + peer(request), 20) + device = self.store.device(body.host_id) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "active bound device required") + try: + key = verify_device(device["cert"], self.s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["owner"] != device["owner"] + or proof["purpose"] != "h5-pair-v1" or abs(time.time() - proof["ts"]) > 90 + or not isinstance(proof["nonce"], str) or len(proof["nonce"]) != 32): + raise ValueError("invalid proof") + except Exception: + raise HTTPException(403, "invalid device proof") from None + replay = "hn:web-pair-proof:" + digest(body.host_id + proof["nonce"]) + if not await self.r.set(replay, "used", nx=True, ex=180): + raise HTTPException(409, "pairing request already used") + await rate_limit(self.r, "web-pair-device:" + body.host_id, 5) + code = secrets.token_urlsafe(18) + record = {"host_id": body.host_id, "owner": device["owner"], "fingerprint": device["fingerprint"], + "generation": await self.r.get("hn:web-generation:" + body.host_id) or "0"} + await self.r.set("hn:web-code:" + digest(code), json.dumps(record), ex=120) + self.store.audit(device["owner"], "web_pair_issued", body.host_id, "success") + return no_cache(JSONResponse(sign({"host_id": body.host_id, "code": code, "expires_in": 120, + "url": self.s.public_url.rstrip("/") + "/h5"}, self.broker.signer))) + + async def new_session(self, record): + token = secrets.token_urlsafe(32) + record = {k: record[k] for k in ("host_id", "owner", "fingerprint", "generation")} + await self.r.set("hn:web-session:" + digest(token), json.dumps(record), ex=1800) + response = no_cache(JSONResponse({"status": "paired", "host_id": record["host_id"], "expires_in": 1800})) + response.set_cookie(COOKIE, token, max_age=1800, secure=True, httponly=True, samesite="strict", path="/") + return response + + async def claim(self, body: Claim, request: Request): + self.check_origin(request) + await rate_limit(self.r, "web-claim:" + peer(request), 20) + key = "hn:web-code:" + digest(body.code.strip()) + encoded = await self.r.get(key) + if not encoded: + raise HTTPException(404, "code expired or consumed") + await self.valid_device(json.loads(encoded)) + encoded = await self.r.getdel(key) + if not encoded: + raise HTTPException(404, "code already consumed") + record = json.loads(encoded) + await self.valid_device(record) + self.store.audit(record["owner"], "web_pair_claimed", record["host_id"], "success") + return await self.new_session(record) + + async def authenticated(self, request): + token = request.cookies.get(COOKIE, "") + if not token or len(token) > 128: + raise HTTPException(401, "pair this browser first") + encoded = await self.r.get("hn:web-session:" + digest(token)) + if not encoded: + raise HTTPException(401, "browser session expired") + record = json.loads(encoded) + await self.valid_device(record) + return record + + async def session(self, request: Request): + record = await self.authenticated(request) + return no_cache(JSONResponse({"authenticated": True, "host_id": record["host_id"], + "online": bool(await self.r.exists("hn:online:" + record["host_id"])), + "mode": "pilot-text-only", "relay_sees_content": True})) + + async def platform_login(self, request: Request): + self.check_origin(request) + if not self.s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.r, "web-platform:" + peer(request), 20) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await self.platform.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = self.store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "bind your device first") + return await self.new_session({**device, "generation": await self.r.get("hn:web-generation:" + device["host_id"]) or "0"}) + + async def chat(self, body: Chat, request: Request): + self.check_origin(request) + record = await self.authenticated(request) + await rate_limit(self.r, "web-chat:" + record["owner"], 20) + text = body.text.strip() + if not text: + raise HTTPException(422, "empty message") + # PC accepts only its bound owner and the existing IM read-only channel. + if not (text == "/主机名" or text.startswith(("/回显 ", "/读取 ", "/助手 "))): + text = "/助手 " + text + turn = "web_" + digest(record["owner"] + "\0" + body.request_id) + result = await self.broker.dispatch(record["owner"], text, turn) + # Revoke/unbind during a long model call invalidates the browser immediately. + await self.authenticated(request) + return no_cache(JSONResponse(result)) + + async def logout(self, request: Request): + self.check_origin(request) + token = request.cookies.get(COOKIE, "") + if token and len(token) <= 128: + await self.r.delete("hn:web-session:" + digest(token)) + response = no_cache(JSONResponse({"status": "logged_out"})) + response.delete_cookie(COOKIE, secure=True, httponly=True, samesite="strict", path="/") + return response diff --git a/container-code/enrollment-signer/relay/windows_keys.py b/container-code/enrollment-signer/relay/windows_keys.py new file mode 100644 index 0000000..0e84e19 --- /dev/null +++ b/container-code/enrollment-signer/relay/windows_keys.py @@ -0,0 +1,40 @@ +"""Current-user Windows DPAPI for device keys; never uses machine-wide scope.""" +import ctypes +import os +from ctypes import wintypes + + +class Blob(ctypes.Structure): + _fields_ = [("cbData", wintypes.DWORD), ("pbData", ctypes.POINTER(ctypes.c_ubyte))] + + +def transform(data, decrypt=False): + if os.name != "nt": + raise RuntimeError("DPAPI is supported only on Windows") + buffer = (ctypes.c_ubyte * len(data)).from_buffer_copy(data) + source = Blob(len(data), buffer) + target = Blob() + crypt = ctypes.WinDLL("crypt32", use_last_error=True) + kernel = ctypes.WinDLL("kernel32", use_last_error=True) + if decrypt: + fn = crypt.CryptUnprotectData + args = [ctypes.byref(source), None, None, None, None, 1, ctypes.byref(target)] + else: + fn = crypt.CryptProtectData + args = [ctypes.byref(source), "Hainuo device identity", None, None, None, 1, ctypes.byref(target)] + fn.restype = wintypes.BOOL + if not fn(*args): + raise ctypes.WinError(ctypes.get_last_error()) + try: + return ctypes.string_at(target.pbData, target.cbData) + finally: + kernel.LocalFree.argtypes = [ctypes.c_void_p] + kernel.LocalFree(target.pbData) + + +def protect(data): + return transform(data) + + +def unprotect(data): + return transform(data, decrypt=True) diff --git a/container-code/enrollment-signer/requirements.lock b/container-code/enrollment-signer/requirements.lock new file mode 100644 index 0000000..c763938 --- /dev/null +++ b/container-code/enrollment-signer/requirements.lock @@ -0,0 +1,41 @@ +annotated-types==0.8.0 +anyio==4.15.1 +certifi==2026.10.23 +cffi==2.1.1 +charset-normalizer==3.5.2 +click==8.5.0 +colorama==0.4.6 +cryptography==46.0.3 +fakeredis==2.32.0 +fastapi==0.119.1 +h11==0.16.0 +httpcore==1.0.9 +httpx==0.28.1 +idna==3.20 +iniconfig==2.3.1 +lark-oapi==1.5.3 +packaging==26.3 +pluggy==1.7.0 +psycopg==3.2.12 +psycopg-binary==3.2.12 +pycparser==3.12 +pycryptodome==4.2.0 +pydantic==2.14.1 +pydantic-settings==2.11.0 +pydantic_core==2.50.1 +Pygments==2.21.0 +pytest==8.4.2 +pytest-asyncio==1.2.0 +python-dotenv==1.2.4 +PyYAML==6.0.3 +redis==6.4.0 +requests==2.34.2 +requests-toolbelt==1.0.0 +sortedcontainers==2.4.0 +starlette==0.48.0 +typing-inspection==0.4.4 +typing_extensions==4.16.0 +tzdata==2026.5 +urllib3==2.8.0 +uvicorn==0.38.0 +websockets==15.0.1 diff --git a/container-code/longconn/image-id.txt b/container-code/longconn/image-id.txt new file mode 100644 index 0000000..b63734c --- /dev/null +++ b/container-code/longconn/image-id.txt @@ -0,0 +1 @@ +sha256:90324bc252d99afc3ba068e9b416eec1ff31e9302716d1c08a525a45ad0ef011 diff --git a/container-code/longconn/pyproject.toml b/container-code/longconn/pyproject.toml new file mode 100644 index 0000000..3bf5e2c --- /dev/null +++ b/container-code/longconn/pyproject.toml @@ -0,0 +1,32 @@ +[build-system] +requires = ["setuptools>=75"] +build-backend = "setuptools.build_meta" + +[project] +name = "hainuo-relay" +version = "0.1.0" +description = "Hainuo isolated connector and encrypted device router" +requires-python = ">=3.12" +dependencies = [ + "fastapi==0.119.1", "uvicorn==0.38.0", "httpx==0.28.1", + "redis==6.4.0", "cryptography==46.0.3", "websockets==15.0.1", + "pydantic-settings==2.11.0", + "psycopg[binary]==3.2.12", +] + +[project.optional-dependencies] +test = ["pytest==8.4.2", "pytest-asyncio==1.2.0", "fakeredis==2.32.0"] +longconn = ["lark-oapi==1.5.3"] + +[project.scripts] +hainuo-relay = "relay.cli:main" + +[tool.setuptools.packages.find] +include = ["relay*"] + +[tool.setuptools.package-data] +relay = ["web/*.html", "web/*.js", "web/*.css"] + +[tool.pytest.ini_options] +asyncio_mode = "auto" +testpaths = ["tests"] diff --git a/container-code/longconn/relay/__init__.py b/container-code/longconn/relay/__init__.py new file mode 100644 index 0000000..32754a7 --- /dev/null +++ b/container-code/longconn/relay/__init__.py @@ -0,0 +1 @@ +"""Hainuo relay 0.1.0.""" diff --git a/container-code/longconn/relay/cli.py b/container-code/longconn/relay/cli.py new file mode 100644 index 0000000..fad7979 --- /dev/null +++ b/container-code/longconn/relay/cli.py @@ -0,0 +1,216 @@ +import argparse +import asyncio +import json +import os +import secrets +import ssl +import uuid +from datetime import datetime, timedelta, timezone +from pathlib import Path + +import httpx +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ed25519 +from cryptography.x509.oid import NameOID + +from .crypto import issue_ca, issue_device, load_private, pem_private + + +def write_new(path, data, private=False): + path.parent.mkdir(parents=True, exist_ok=True) + with path.open("xb") as f: + f.write(data if isinstance(data, bytes) else data.encode()) + if private and os.name != "nt": + path.chmod(0o600) + + +def init_project(root: Path, hostname: str): + if (root / ".env").exists(): + raise RuntimeError(".env exists; init never overwrites credentials") + ca_key, ca = issue_ca() + signer = ed25519.Ed25519PrivateKey.generate() + write_new(root / "operator/ca.key", pem_private(ca_key), True) + write_new(root / "runtime/trust/ca.pem", ca.public_bytes(serialization.Encoding.PEM)) + write_new(root / "runtime/connector/connector.key", pem_private(signer), True) + write_new(root / "runtime/trust/connector.pub", signer.public_key().public_bytes( + serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo)) + tls_key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, hostname)]) + now = datetime.now(timezone.utc) + tls = (x509.CertificateBuilder().subject_name(name).issuer_name(name).public_key(tls_key.public_key()) + .serial_number(x509.random_serial_number()).not_valid_before(now - timedelta(minutes=5)) + .not_valid_after(now + timedelta(days=30)) + .add_extension(x509.SubjectAlternativeName([x509.DNSName(hostname)]), critical=False) + .sign(tls_key, algorithm=None)) + write_new(root / "runtime/tls/server.key", pem_private(tls_key), True) + write_new(root / "runtime/tls/server.pem", tls.public_bytes(serialization.Encoding.PEM)) + redis_secret = secrets.token_hex(32) + db_secret = secrets.token_hex(32) + env = (f"HN_ENVIRONMENT=development\nHN_PUBLIC_HOST={hostname}\nHN_PUBLIC_URL=https://{hostname}\n" + f"HN_ROUTER_URL=wss://{hostname}/device/ws\nHN_ADMIN_SECRET={secrets.token_hex(32)}\n" + f"HN_INTERNAL_SECRET={secrets.token_hex(32)}\nHN_REDIS_PASSWORD={redis_secret}\n" + f"HN_REDIS_URL=redis://:{redis_secret}@redis:6379/0\n" + f"HN_POSTGRES_PASSWORD={db_secret}\nHN_DATABASE_URL=postgresql://hainuo:{db_secret}@postgres:5432/hainuo\n" + "HN_FEISHU_ENABLED=false\nHN_FEISHU_API_BASE=https://open.feishu.cn\n" + "HN_FEISHU_AUTHORIZE_URL=https://open.feishu.cn/open-apis/authen/v1/authorize\n" + "HN_FEISHU_APP_ID=\nHN_FEISHU_APP_SECRET=\nHN_FEISHU_ENCRYPT_KEY=\n" + "HN_FEISHU_VERIFICATION_TOKEN=\nHN_PROXY_ENABLED=false\nHN_DEVICE_DOMAIN=\n") + write_new(root / ".env", env, True) + print("已生成配置、连接器密钥、设备CA及临时TLS证书;未输出凭据。") + print("operator/ca.key 仅保留在签发工作站,不要上传到ECS。临时TLS证书上线前须替换。") + + +def device_request(directory): + host = uuid.uuid4().hex + private = ed25519.Ed25519PrivateKey.generate() + write_new(directory / "device.key", pem_private(private), True) + public = private.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode() + write_new(directory / "request.json", json.dumps({"host_id": host, "public_key": public}, indent=2)) + print("已在本机生成私钥和 request.json;仅将request.json交给签发工作站。") + + +def issue(root, request_file, output): + request = json.loads(request_file.read_text(encoding="utf-8")) + host = request["host_id"] + if len(host) != 32 or any(c not in "0123456789abcdef" for c in host): + raise ValueError("invalid host identity") + ca = x509.load_pem_x509_certificate((root / "runtime/trust/ca.pem").read_bytes()) + public = serialization.load_pem_public_key(request["public_key"].encode()) + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("Ed25519 required") + cert = issue_device(load_private(root / "operator/ca.key"), ca, host, public) + pem = cert.public_bytes(serialization.Encoding.PEM) + write_new(output / "device.pem", pem) + write_new(output / "ca.pem", (root / "runtime/trust/ca.pem").read_bytes()) + write_new(output / "connector.pub", (root / "runtime/trust/connector.pub").read_bytes()) + write_new(output / "device.json", json.dumps({"host_id": host, "owner": None}, indent=2)) + write_new(output / "registration.json", json.dumps({"host_id": host, "cert": pem.decode()}, indent=2)) + print("已签发设备证书;将device.pem、ca.pem、connector.pub、device.json交回原设备。") + + +def main(): + parser = argparse.ArgumentParser(description="海诺中继部署和设备工具") + sub = parser.add_subparsers(dest="command", required=True) + p = sub.add_parser("init") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--hostname", required=True) + p = sub.add_parser("serve") + p.add_argument("role", choices=["connector", "router"]) + p.add_argument("--host", default="127.0.0.1") + p.add_argument("--port", type=int, default=8000) + p = sub.add_parser("device-request") + p.add_argument("--directory", type=Path, required=True) + p = sub.add_parser("issue-device") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--request", type=Path, required=True) + p.add_argument("--output", type=Path, required=True) + p = sub.add_parser("register") + p.add_argument("--registration", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("bind") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("device") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p.add_argument("--test-root", type=Path) + p.add_argument("--agent-url", help="Explicit loopback read-only Agent adapter endpoint") + p.add_argument("--hainuo-config", type=Path, help="Native installer-1.065 API adapter config; alternative to --agent-url") + p = sub.add_parser("hainuo-probe") + p.add_argument("--config", type=Path, required=True) + p = sub.add_parser("web-pair") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + for command in ("status", "binding-code", "dispatch", "revoke", "unbind", "reconcile"): + p = sub.add_parser(command) + p.add_argument("--url", default="http://127.0.0.1:18081") + p.add_argument("--tls-ca", type=Path) + if command in {"binding-code", "dispatch"}: + p.add_argument("--owner", required=True) + if command == "dispatch": + p.add_argument("--text", required=True) + p.add_argument("--turn-id", default=None) + if command in {"revoke", "unbind"}: + p.add_argument("--host-id", required=True) + args = parser.parse_args() + if args.command == "init": + init_project(args.root, args.hostname) + elif args.command == "serve": + import uvicorn + uvicorn.run(f"relay.{args.role}:create_app", factory=True, host=args.host, port=args.port, + workers=1, access_log=False, proxy_headers=False, + ws_max_size=262144, ws_max_queue=16, log_level="warning") + elif args.command == "device-request": + device_request(args.directory) + elif args.command == "issue-device": + issue(args.root, args.request, args.output) + elif args.command == "register": + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, follow_redirects=False, timeout=15) as client: + response = client.post(args.url.rstrip("/") + "/admin/devices", + headers={"X-Admin-Secret": secret}, + json=json.loads(args.registration.read_text(encoding="utf-8"))) + if response.status_code != 201: + raise RuntimeError(f"register failed: HTTP {response.status_code}") + print("设备登记成功。") + elif args.command == "bind": + import getpass + from .device import bind_device + code = getpass.getpass("Binding code: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + asyncio.run(bind_device(args.directory, args.url, code, tls_verify=context)) + print("绑定成功。") + elif args.command == "device": + from .device import run_device + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else None + asyncio.run(run_device(args.directory, args.url, root=args.test_root, ssl_context=context, + agent_url=args.agent_url, hainuo_config=args.hainuo_config)) + elif args.command == "hainuo-probe": + from .hainuo1065 import Hainuo1065Agent + async def probe(): + adapter = Hainuo1065Agent(args.config) + try: + print(json.dumps(await adapter.probe(), ensure_ascii=False, indent=2)) + finally: + await adapter.close() + asyncio.run(probe()) + elif args.command == "web-pair": + from .device import pair_browser + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + result = asyncio.run(pair_browser(args.directory, args.url, tls_verify=context)) + print("网页地址:" + result["url"]) + print("一次性网页配对码(120秒有效):" + result["code"]) + else: + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, timeout=200, follow_redirects=False) as client: + paths = {"status": "/admin/devices", "binding-code": "/admin/test/binding-code", + "dispatch": "/admin/test/dispatch", "reconcile": "/admin/reconcile"} + if args.command in {"revoke", "unbind"}: + if len(args.host_id) != 32 or any(c not in "0123456789abcdef" for c in args.host_id): + raise ValueError("invalid host identity") + path = f"/admin/devices/{args.host_id}/{args.command}" + else: + path = paths[args.command] + body = None + if args.command in {"binding-code", "dispatch"}: + body = {"open_id": args.owner, "text": getattr(args, "text", "binding")} + if getattr(args, "turn_id", None): + body["turn_id"] = args.turn_id + response = client.request("GET" if args.command == "status" else "POST", args.url.rstrip("/") + path, + headers={"X-Admin-Secret": secret}, json=body) + if response.status_code != 200: + raise RuntimeError(f"operation failed: HTTP {response.status_code}") + print(json.dumps(response.json(), ensure_ascii=False, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/container-code/longconn/relay/client_manage.py b/container-code/longconn/relay/client_manage.py new file mode 100644 index 0000000..6b543fe --- /dev/null +++ b/container-code/longconn/relay/client_manage.py @@ -0,0 +1,293 @@ +"""Per-user Windows connection companion. No platform/application secrets.""" +import argparse +import asyncio +import json +import os +import secrets +import socket +import ssl +import subprocess +import sys +import time +import uuid +from pathlib import Path + +import httpx +import psutil +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey +from .crypto import load_private, load_public, pem_private, raw_public, sign, verified, verify_device +from .windows_keys import protect + +BASE = Path(__file__).resolve().parent.parent +URL = "https://10.77.76.13" + + +def save(path, value): + temp = path.with_suffix(path.suffix + ".tmp") + temp.write_text(json.dumps(value, ensure_ascii=False, indent=2), encoding="utf-8") + os.replace(temp, path) + + +def secure_directory(directory): + directory.mkdir(parents=True, exist_ok=True) + # SID avoids localized account names. Fail closed if ACL protection fails. + result = subprocess.run(["whoami", "/user", "/fo", "csv", "/nh"], capture_output=True, text=True, check=True) + import csv + sid = list(csv.reader([result.stdout.strip()]))[0][1] + subprocess.run(["icacls", str(directory), "/inheritance:r", "/grant:r", f"*{sid}:(OI)(CI)F", "*S-1-5-18:(OI)(CI)F"], + capture_output=True, check=True, creationflags=subprocess.CREATE_NO_WINDOW) + + +def prepare(directory): + secure_directory(directory) + for name in ("server.pem", "ca.pem", "connector.pub"): + target = directory / name + if not target.exists(): + target.write_bytes((BASE / "trust" / name).read_bytes()) + if not (directory / "device.json").exists(): + if (directory / "device.key.dpapi").exists() or (directory / "device.key").exists(): + raise RuntimeError("发现不完整的设备身份,请保留目录并联系管理员恢复。") + private = Ed25519PrivateKey.generate() + (directory / "device.key.dpapi").write_bytes(protect(pem_private(private))) + save(directory / "device.json", {"host_id": uuid.uuid4().hex, "owner": None}) + + +def tls(directory): + return ssl.create_default_context(cafile=str(directory / "server.pem")) + + +def import_existing(directory, source): + """Explicit migration; only public trust and this PC's identity, never app keys.""" + if source.resolve() == directory.resolve(): + raise ValueError("请选择旧设备目录。") + if any((directory / name).exists() for name in ("device.json", "device.key.dpapi", "device.key")): + raise RuntimeError("本目录已有设备身份。请使用已有身份继续,不能用导入覆盖。") + config = json.loads((source / "device.json").read_text(encoding="utf-8-sig")) + for name in ("server.pem", "ca.pem", "connector.pub"): + if (source / name).read_bytes().strip() != (BASE / "trust" / name).read_bytes().strip(): + raise ValueError("旧设备的服务信任与本安装包不符,请联系管理员。") + cert = (source / "device.pem").read_text(encoding="utf-8") + public = verify_device(cert, (source / "ca.pem").read_bytes(), config["host_id"]) + private = load_private(source / "device.key") + if raw_public(public) != raw_public(private.public_key()): + raise ValueError("设备证书与本机私钥不匹配。") + secure_directory(directory) + for name in ("device.pem", "server.pem", "ca.pem", "connector.pub"): + (directory / name).write_bytes((source / name).read_bytes()) + (directory / "device.key.dpapi").write_bytes(protect(pem_private(private))) + save(directory / "device.json", config) + # Preserve local replay journal using SQLite's consistent backup API. + if (source / "journal.sqlite").is_file(): + import sqlite3 + with sqlite3.connect(source / "journal.sqlite") as original, sqlite3.connect(directory / "journal.sqlite") as target: + original.backup(target) + return {"message": "旧设备导入成功,Work绑定已保留。请先停止旧PowerShell桥接窗口,再点击启动连接。"} + + +def enrollment(directory): + prepare(directory) + cfg = json.loads((directory / "device.json").read_text()) + if (directory / "device.pem").exists(): + verify_device((directory / "device.pem").read_text(), (directory / "ca.pem").read_bytes(), cfg["host_id"]) + return {"message": "设备已注册,可在Work获取绑定码后绑定。已有绑定可直接启动连接。"} + with httpx.Client(verify=tls(directory), trust_env=False, timeout=15) as http: + key = load_private(directory / "device.key") + proof = sign({"host_id": cfg["host_id"], "purpose": "device-enroll-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + response = http.post(URL + "/device/register", json={"host_id": cfg["host_id"], "proof": proof, + "public_key": key.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode()}) + if response.status_code not in {200, 201}: + raise RuntimeError("自动注册失败:HTTP " + str(response.status_code) + ";请确认ECS已安装1.067自动注册更新。") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt.get("status") != "registered" or receipt["host_id"] != cfg["host_id"]: + raise ValueError("注册回执身份不符") + public = verify_device(receipt["cert"], (directory / "ca.pem").read_bytes(), cfg["host_id"]) + if raw_public(public) != raw_public(key.public_key()): + raise ValueError("注册证书公钥不符") + temp = directory / "device.pem.tmp" + temp.write_text(receipt["cert"], encoding="utf-8") + os.replace(temp, directory / "device.pem") + (directory / "enrollment.json").unlink(missing_ok=True) + return {"message": "设备已自动注册,无需管理员审批。请在Work获取绑定码,填写后点击绑定并连接。"} + + +def managed_processes(directory): + """Only processes with our absolute entry path AND exact device directory.""" + state = directory / "processes.json" + if not state.exists(): + return [] + result = [] + for item in json.loads(state.read_text()).get("processes", []): + try: + proc = psutil.Process(item["pid"]) + command = proc.cmdline() + if (abs(proc.create_time() - item["created"]) < 0.1 and str(BASE / "entry.py") in command + and str(directory) in command): + result.append(proc) + except psutil.Error: + pass + return result + + +def stop(directory): + procs = managed_processes(directory) + # Supervisor owns and terminates only its own child handles in its finally block. + (directory / "stop.flag").touch() + _, alive = psutil.wait_procs(procs, timeout=8) + for proc in alive: + proc.terminate() + return {"message": "已停止本安装包启动的连接进程。手工启动的旧桥接请在原窗口按Ctrl+C停止。"} + + +def start(directory, root): + from .process_lock import DeviceProcessLock + with DeviceProcessLock(directory / "control"): + return _start(directory, root) + + +def _start(directory, root): + cfg = json.loads((directory / "device.json").read_text()) + if not cfg.get("owner") or not (directory / "device.pem").exists(): + raise RuntimeError("请先完成设备准入和Work绑定。") + if managed_processes(directory): + return {"message": "连接进程已运行,请点击状态查看。"} + (directory / "stop.flag").unlink(missing_ok=True) + log = (directory / "bridge.log").open("a", encoding="utf-8") + proc = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "supervise", "--directory", str(directory), "--root", str(root)], + stdout=log, stderr=log, creationflags=subprocess.CREATE_NO_WINDOW) + log.close() + save(directory / "processes.json", {"processes": [{"pid": proc.pid, "created": psutil.Process(proc.pid).create_time()}]}) + return {"message": "连接进程已启动;约30秒后点击状态,再到Work发送 /主机名 验证。"} + + +def supervise(directory, root): + from .process_lock import DeviceProcessLock + lockdir = directory / "supervisor" + with DeviceProcessLock(lockdir): + children = [] + def record_children(): + processes = [psutil.Process(os.getpid())] + [psutil.Process(p.pid) for p in children if p.poll() is None] + save(directory / "processes.json", {"processes": [{"pid": p.pid, "created": p.create_time()} for p in processes]}) + try: + # Check local bind availability without an HTTP connection. Windows + # filtering can time out connections to a closed port rather than + # refuse them; that must not prevent starting our own listener. + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as port_probe: + try: + if os.name == "nt": + port_probe.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1) + port_probe.bind(("127.0.0.1", 18643)) + except OSError: + raise RuntimeError("原生API端口18643被占用或不允许绑定,请先检查旧连接。") from None + native = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "native", "--directory", str(directory), "--root", str(root)]) + children.append(native) + record_children() + for _ in range(90): + if (directory / "stop.flag").exists(): + return + if native.poll() is not None: + raise RuntimeError("本机原生API启动失败,请检查安装配置。") + try: + from .hainuo1065 import Hainuo1065Agent + async def probe(): + agent = Hainuo1065Agent(directory / "hainuo-native.json") + try: + return await agent.probe() + finally: + await agent.close() + asyncio.run(probe()) + break + except Exception: + time.sleep(1) + else: + raise RuntimeError("本机原生API未就绪。") + bridge = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "bridge", "--directory", str(directory), "--root", str(root)]) + children.append(bridge) + record_children() + while not (directory / "stop.flag").exists(): + if any(proc.poll() is not None for proc in children): + raise RuntimeError("连接子进程已退出,请查看状态后重新启动。") + time.sleep(0.5) + finally: + for proc in reversed(children): + if proc.poll() is None: + proc.terminate() + try: + proc.wait(timeout=5) + except subprocess.TimeoutExpired: + proc.kill() + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("action", choices=["authorize", "import", "enroll", "bind", "start", "stop", "status", "pair", "supervise", "native", "bridge"]) + parser.add_argument("--directory", type=Path, required=True) + parser.add_argument("--root", type=Path, required=True) + args = parser.parse_args() + directory, root = args.directory.resolve(), args.root.resolve() + directory.mkdir(parents=True, exist_ok=True) + try: + if args.action == "authorize": + from .work_client import WorkClient + client = WorkClient(directory) + try: + result = client.status() + result["message"] += ("\n" + result["authorization_url"]) if result.get("authorization_url") else "" + finally: + client.close() + elif args.action == "import": + result = import_existing(directory, Path(sys.stdin.readline().strip())) + elif args.action == "enroll": + result = enrollment(directory) + elif args.action == "bind": + from .device import bind_device + code = sys.stdin.readline().strip() + if not code: + raise ValueError("请填写Work机器人新生成的绑定码。") + enrollment(directory) + stop(directory) + asyncio.run(bind_device(directory, URL, code, tls_verify=tls(directory))) + result = start(directory, root) + result["message"] = "Work绑定成功。" + result["message"] + elif args.action == "start": + result = start(directory, root) + elif args.action == "stop": + result = stop(directory) + elif args.action == "pair": + from .device import pair_browser + receipt = asyncio.run(pair_browser(directory, URL, tls_verify=tls(directory))) + result = {"message": "网页:" + receipt["url"] + "\n一次性配对码(120秒):" + receipt["code"]} + elif args.action == "status": + cfg = json.loads((directory / "device.json").read_text()) if (directory / "device.json").exists() else {} + log = directory / "bridge.log" + tail = log.read_bytes()[-6000:].decode("utf-8", errors="replace") if log.exists() else "" + result = {"message": "设备:" + cfg.get("host_id", "未申请") + "\nWork身份:" + (cfg.get("owner") or "未绑定") + + "\n连接进程:" + ("运行中(请在Work发送 /主机名 确认当前链路)" if managed_processes(directory) else "未运行") + "\n" + tail[-2500:]} + elif args.action == "supervise": + return supervise(directory, root) + elif args.action == "native": + os.environ["HN_WORK_DEVICE_DIR"] = str(directory) + import importlib.util + spec = importlib.util.spec_from_file_location("native_companion", BASE / "hainuo_native_api.py") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + module.enable_profile_tools(root, "hainuo") + key = module.prepare_environment(root, "hainuo", 18643, directory / "hainuo-native.json", enable_tools=True) + import logging + logging.disable(logging.CRITICAL) + return asyncio.run(module.serve_native(18643, key, enable_tools=True)) + else: + from .device import run_device + return asyncio.run(run_device(directory, "wss://10.77.76.13/device/ws", ssl_context=tls(directory), hainuo_config=directory / "hainuo-native.json")) + print(json.dumps(result, ensure_ascii=False)) + except KeyboardInterrupt: + pass + except Exception as error: + # Native exception bodies may include URLs or secrets; emit bounded known messages only. + message = str(error) if isinstance(error, (RuntimeError, ValueError)) else type(error).__name__ + ";请检查网络、证书、安装目录。" + print(json.dumps({"message": message[:600], "error": True}, ensure_ascii=False)) + raise SystemExit(1) from None + + +if __name__ == "__main__": + main() diff --git a/container-code/longconn/relay/common.py b/container-code/longconn/relay/common.py new file mode 100644 index 0000000..c112b99 --- /dev/null +++ b/container-code/longconn/relay/common.py @@ -0,0 +1,61 @@ +import hashlib +import hmac +import json +import time +from fastapi import HTTPException, Request +from starlette.responses import JSONResponse + + +def require_secret(actual, expected): + if not actual or not hmac.compare_digest(actual, expected): + raise HTTPException(401, "unauthorized") + + +async def rate_limit(redis, bucket: str, limit=60, seconds=60): + # Atomic counter + expiry: a crash cannot leave a permanent bucket. + key = "hn:rate:" + hashlib.sha256(bucket.encode()).hexdigest() + ":" + str(int(time.time()) // seconds) + pipe = redis.pipeline(transaction=True) + pipe.incr(key) + pipe.expire(key, seconds * 2) + result = (await pipe.execute())[0] + if int(result) > limit: + raise HTTPException(429, "rate limited", headers={"Retry-After": str(seconds)}) + + +class BodyLimitMiddleware: + def __init__(self, app, max_body=262144): + self.app, self.max_body = app, max_body + + async def __call__(self, scope, receive, send): + if scope["type"] != "http": + return await self.app(scope, receive, send) + messages, size = [], 0 + while True: + message = await receive() + if message["type"] == "http.disconnect": + return + size += len(message.get("body", b"")) + if size > self.max_body: + return await JSONResponse({"detail": "body too large"}, status_code=413)(scope, receive, send) + messages.append(message) + if not message.get("more_body", False): + break + async def buffered(): + return messages.pop(0) if messages else await receive() + await self.app(scope, buffered, send) + + +def no_cache(response): + response.headers["Cache-Control"] = "no-store" + response.headers["X-Content-Type-Options"] = "nosniff" + response.headers["Referrer-Policy"] = "no-referrer" + return response + + +async def json_pub(redis, channel, data): + return await redis.publish(channel, json.dumps(data, separators=(",", ":"))) + + +def peer(request: Request): + # Do not trust arbitrary X-Forwarded-For; configure uvicorn trusted proxies explicitly. + return request.client.host if request.client else "unknown" diff --git a/container-code/longconn/relay/config.py b/container-code/longconn/relay/config.py new file mode 100644 index 0000000..c4c32ac --- /dev/null +++ b/container-code/longconn/relay/config.py @@ -0,0 +1,66 @@ +from pathlib import Path +from pydantic import model_validator +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + model_config = SettingsConfigDict(env_prefix="HN_", extra="ignore", hide_input_in_errors=True) + environment: str = "production" + role: str = "connector" + redis_url: str = "redis://127.0.0.1:6379/0" + data_dir: Path = Path("runtime/connector") + database_url: str = "" + admin_secret: str = "" + internal_secret: str = "" + ca_cert: Path = Path("runtime/pki/ca.pem") + connector_key: Path = Path("runtime/pki/connector.key") + connector_public_key: Path = Path("runtime/pki/connector.pub") + public_url: str = "https://relay.example.invalid" + router_url: str = "wss://relay.example.invalid/device/ws" + device_domain: str = "" + feishu_api_base: str = "https://open.feishu.cn" + feishu_authorize_url: str = "https://open.feishu.cn/open-apis/authen/v1/authorize" + feishu_oauth_scopes: str = "" + feishu_app_id: str = "" + feishu_app_secret: str = "" + feishu_encrypt_key: str = "" + feishu_verification_token: str = "" + feishu_enabled: bool = False + oauth_enabled: bool = True + feishu_event_source: str = "longconn" + proxy_enabled: bool = False + # A fixed allowlist, never a user supplied upstream URL. + proxy_paths: str = "/open-apis/drive/v1/files,/open-apis/docx/v1/documents" + request_timeout: int = 45 + max_body: int = 262144 + max_pending: int = 1000 + max_devices: int = 1000 + max_connections: int = 1000 + max_result: int = 16000 + allow_insecure_local: bool = False + enrollment_signer_url: str = "" + + @model_validator(mode="after") + def validate_runtime(self): + if self.environment not in {"production", "test", "development"}: + raise ValueError("invalid environment") + if self.role == "connector" and min(len(self.admin_secret), len(self.internal_secret)) < 32: + raise ValueError("admin/internal secrets must each be at least 32 characters") + if self.role == "longconn" and len(self.internal_secret) < 32: + raise ValueError("internal credential must be at least 32 characters") + if self.role == "connector" and self.admin_secret == self.internal_secret: + raise ValueError("admin and internal credentials must be different") + if self.environment == "production": + if self.allow_insecure_local or not self.public_url.startswith("https://"): + raise ValueError("production requires HTTPS") + if not self.router_url.startswith("wss://"): + raise ValueError("production requires WSS") + if not self.feishu_api_base.startswith("https://"): + raise ValueError("platform API requires HTTPS") + if self.feishu_event_source not in {"longconn", "webhook"}: + raise ValueError("invalid platform event source") + if self.feishu_enabled and not all((self.feishu_app_id, self.feishu_app_secret)): + raise ValueError("platform app ID and secret are required") + if self.feishu_enabled and self.feishu_event_source == "webhook" and not all((self.feishu_encrypt_key, self.feishu_verification_token)): + raise ValueError("webhook requires encryption key and verification token") + return self diff --git a/container-code/longconn/relay/connector.py b/container-code/longconn/relay/connector.py new file mode 100644 index 0000000..c01f3b6 --- /dev/null +++ b/container-code/longconn/relay/connector.py @@ -0,0 +1,506 @@ +import asyncio +import base64 +import contextlib +import hashlib +import hmac +import json +import re +import secrets +import sqlite3 +import time +import uuid +from contextlib import asynccontextmanager +from dataclasses import dataclass +from urllib.parse import unquote, urlsplit + +from cryptography.hazmat.primitives import hashes, padding +from cryptography.hazmat.primitives.asymmetric import x25519 +from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes +from fastapi import FastAPI, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from fastapi.exceptions import RequestValidationError +from pydantic import BaseModel, Field +from redis.asyncio import Redis + +from .common import BodyLimitMiddleware, no_cache, peer, rate_limit, require_secret +from .config import Settings +from .crypto import (fingerprint, load_private, open_envelope, raw_public, seal, + sign, verified, verify_device) +from .oauth import OAuth +from .platform import Feishu, PlatformError +from .store import Store, INTEGRITY_ERRORS + + +class Register(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + cert: str = Field(max_length=10000) + + +class Bind(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Instruction(BaseModel): + open_id: str = Field(min_length=1, max_length=128) + text: str = Field(min_length=1, max_length=8000) + # Stable identity chosen by upstream, not generated again on retries. + turn_id: str = Field(default_factory=lambda: uuid.uuid4().hex, pattern=r"^[A-Za-z0-9_-]{1,128}$") + + +@dataclass +class Pending: + future: asyncio.Future + private: object + public: object + host: str + session: str + + +class Broker: + def __init__(self, settings, redis, store): + self.s, self.r, self.store = settings, redis, store + self.signer = load_private(settings.connector_key) + self.pending: dict[str, Pending] = {} + + async def replies(self, pub): + async for message in pub.listen(): + if message["type"] != "message": + continue + try: + data = json.loads(message["data"]) + item = self.pending.get(data["turn_id"]) + if not item or item.future.done(): + continue + plain = open_envelope(data["envelope"], item.private, item.public, + host_id=item.host, session_id=item.session) + if plain["turn_id"] != data["turn_id"] or plain.get("type") != "result": + continue + result = plain.get("result", "") + if not isinstance(result, str) or len(result) > self.s.max_result: + continue + item.future.set_result({"status": "completed", "turn_id": data["turn_id"], "result": result}) + except Exception: + # Invalid packets never consume a pending result; do not log packet contents. + continue + + async def dispatch(self, owner, text, turn): + device = self.store.by_owner(owner) + if not device: + return {"status": "unavailable", "turn_id": turn, "result": "未绑定设备或设备不可用。"} + host = device["host_id"] + await rate_limit(self.r, "dispatch:" + owner, 30) + if len(self.pending) >= self.s.max_pending: + raise HTTPException(503, "too many in-flight requests") + presence = await self.r.get("hn:online:" + host) + if not presence or await self.r.exists("hn:revoked:" + host): + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线,请开机并连接网络。"} + try: + presence = json.loads(presence) + if fingerprint(presence["cert"]) != device["fingerprint"]: + raise ValueError("certificate changed") + public = verify_device(presence["cert"], self.s.ca_cert.read_bytes(), host) + proof = verified(presence["proof"], public) + if proof["host_id"] != host or proof["challenge"] != presence["challenge"]: + raise ValueError("invalid device session") + session = proof["session_id"] + except Exception: + self.store.audit(owner, "dispatch", host, "identity_rejected") + raise HTTPException(409, "device identity rejected") from None + if not self.store.begin_turn(turn, owner, host): + return {"status": "duplicate", "turn_id": turn, "result": "该请求已接收,不会重复执行。"} + private = x25519.X25519PrivateKey.generate() + future = asyncio.get_running_loop().create_future() + self.pending[turn] = Pending(future, private, public, host, session) + envelope = seal({"type": "instruction", "turn_id": turn, "owner": owner, + "text": text, "read_only": True, "channel": "im", + "reply_key": raw_public(private.public_key())}, proof["kx"], self.signer, + host_id=host, session_id=session) + key = f"hn:inflight:{host}:{turn}" + try: + await self.r.set(key, session, ex=self.s.request_timeout + 5) + count = await self.r.publish("hn:down:" + host, json.dumps({"type": "instruction", "envelope": envelope})) + if not count: + self.store.finish(turn, "offline") + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线。"} + result = await asyncio.wait_for(future, timeout=self.s.request_timeout) + self.store.finish(turn, "completed") + self.store.audit(owner, "instruction", host, "completed") + return result + except asyncio.TimeoutError: + self.store.finish(turn, "unknown") + return {"status": "unknown", "turn_id": turn, + "result": "等待结果超时,执行状态未知;不要直接重发有副作用的任务。"} + finally: + self.pending.pop(turn, None) + await self.r.delete(key) + + +def create_app(settings=None, redis=None, platform=None): + s = settings or Settings() + r = redis or Redis.from_url(s.redis_url, decode_responses=True) + store = Store(s.data_dir / "metadata.sqlite", s.database_url) + f = platform or Feishu(s) + broker = Broker(s, r, store) + oauth = OAuth(s, r, f) + oauth.store = store + background = set() + + def spawn(coro): + task = asyncio.create_task(coro) + background.add(task) + task.add_done_callback(background.discard) + # Retrieve errors without displaying possibly sensitive exception text. + task.add_done_callback(lambda t: t.exception() if not t.cancelled() else None) + + @asynccontextmanager + async def lifespan(app): + await r.ping() + for row in store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall(): + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + pub = r.pubsub() + await pub.subscribe("hn:replies") + reply_task = asyncio.create_task(broker.replies(pub)) + app.state.reply_task = reply_task + async def sweep(): + while True: + await asyncio.sleep(30) + async with oauth.lock: + oauth.prune() + sweep_task = asyncio.create_task(sweep()) + try: + yield + finally: + for task in [reply_task, sweep_task, *background]: + task.cancel() + await asyncio.gather(reply_task, sweep_task, *background, return_exceptions=True) + await pub.aclose() + await f.close() + store.close() + if redis is None: + await r.aclose() + + app = FastAPI(title="Hainuo B1 connector", lifespan=lifespan, docs_url=None, redoc_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=s.max_body) + app.include_router(oauth.router) + from .web_session import WebSessions + web_sessions = WebSessions(s, r, store, broker, f) + app.include_router(web_sessions.router) + app.state.store, app.state.broker, app.state.oauth = store, broker, oauth + app.state.platform, app.state.redis = f, r + + @app.exception_handler(RequestValidationError) + async def validation_error(request, exc): + # FastAPI's default error response includes invalid input values (possibly tokens). + return JSONResponse({"detail": "invalid request parameters"}, status_code=422) + + @app.middleware("http") + async def secure_response(request, call_next): + return no_cache(await call_next(request)) + + def admin(request): + require_secret(request.headers.get("X-Admin-Secret"), s.admin_secret) + + from .enrollment import enrollment_router + app.include_router(enrollment_router(s, r, store, broker.signer, admin)) + from .work_api import router as work_router + app.include_router(work_router(s, r, store, f)) + + @app.get("/health") + async def health(): + return {"status": "alive", "role": "connector", "version": "0.1.0"} + + @app.get("/ready") + async def ready(): + await r.ping() + store.db.execute("SELECT 1") + if not getattr(app.state, "reply_task", None) or app.state.reply_task.done(): + raise HTTPException(503, "reply subscriber unavailable") + return {"status": "ready"} + + @app.post("/admin/devices", status_code=201) + async def register(body: Register, request: Request): + admin(request) + try: + verify_device(body.cert, s.ca_cert.read_bytes(), body.host_id) + except Exception: + raise HTTPException(400, "invalid device certificate") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= s.max_devices: + raise HTTPException(503, "device capacity reached") + try: + fp = fingerprint(body.cert) + store.add_device(body.host_id, body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "device already exists") from None + await r.set("hn:device:" + body.host_id, fp) + store.audit("admin", "register", body.host_id, "success") + return {"host_id": body.host_id, "fingerprint": fp} + + @app.get("/admin/devices") + async def devices(request: Request): + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,owner,revoked FROM devices ORDER BY host_id").fetchall() + return {"devices": [{**dict(row), "online": bool(await r.exists("hn:online:" + row["host_id"]))} for row in rows]} + + @app.post("/admin/devices/{host}/revoke") + async def revoke(host: str, request: Request): + admin(request) + if not store.device(host): + raise HTTPException(404, "device not found") + # Persistent source of truth first; router block + disconnect follows. + store.revoke(host) + await r.incr("hn:web-generation:" + host) + await r.set("hn:revoked:" + host, "1") + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "revoke") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "revoked", "result": "设备已停用,结果已丢弃。"}) + store.audit("admin", "revoke", host, "success") + return {"status": "revoked"} + + @app.post("/admin/reconcile") + async def reconcile(request: Request): + """Rebuild router admission metadata after Redis recovery; no dialogue data.""" + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall() + for row in rows: + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + return {"devices": len(rows)} + + @app.post("/admin/devices/{host}/unbind") + async def admin_unbind(host: str, request: Request): + admin(request) + device = store.device(host) + if not device: + raise HTTPException(404, "device not found") + if device["owner"]: + store.unbind(device["owner"]) + await r.incr("hn:web-generation:" + host) + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "unbind") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + store.audit("admin", "unbind", host, "success") + return {"status": "unbound", "host_id": host, "revoked": bool(device["revoked"])} + + async def make_binding_code(owner): + if store.by_owner(owner): + return "你已绑定电脑,请先发送 /解绑。" + await rate_limit(r, "binding:" + owner, 5) + code = secrets.token_urlsafe(12) + await r.set("hn:bind:" + hashlib.sha256(code.encode()).hexdigest(), owner, ex=300) + return "请在本人电脑的桥接客户端输入绑定码(5分钟有效):" + code + + @app.post("/device/bind") + async def bind(body: Bind, request: Request): + await rate_limit(r, "bind:" + peer(request), 20) + device = store.device(body.host_id) + if not device or device["revoked"]: + raise HTTPException(404, "device unavailable") + try: + public = verify_device(device["cert"], s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, public) + if proof["host_id"] != body.host_id or abs(time.time() - proof["ts"]) > 90: + raise ValueError("invalid proof") + code = proof["code"] + if not isinstance(code, str) or len(code) > 128: + raise ValueError("invalid code") + except Exception: + raise HTTPException(403, "invalid device proof") from None + key = "hn:bind:" + hashlib.sha256(code.encode()).hexdigest() + owner = await r.get(key) + if not owner: + raise HTTPException(404, "binding code expired or consumed") + # GETDEL is atomic across connectors. Invalid device proofs never reach this point. + owner = await r.getdel(key) + if not owner: + raise HTTPException(404, "binding code consumed") + try: + store.bind(body.host_id, owner) + except (*INTEGRITY_ERRORS, ValueError): + raise HTTPException(409, "owner/device already bound; request new code") from None + await r.incr("hn:web-generation:" + body.host_id) + store.audit(owner, "bind", body.host_id, "success") + return sign({"host_id": body.host_id, "owner": owner, "ts": int(time.time())}, broker.signer) + + @app.post("/admin/test/binding-code") + async def test_code(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return {"message": await make_binding_code(body.open_id)} + + @app.post("/admin/test/dispatch") + async def test_dispatch(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return await broker.dispatch(body.open_id, body.text, body.turn_id) + + async def handle_event(data): + header, event = data.get("header", {}), data.get("event", {}) + if header.get("event_type") != "im.message.receive_v1": + return + message, sender = event.get("message", {}), event.get("sender", {}) + owner = sender.get("sender_id", {}).get("open_id") + message_id = message.get("message_id", "") + if (message.get("chat_type") != "p2p" or message.get("message_type") != "text" + or sender.get("sender_type") != "user" or not owner + or not re.fullmatch(r"[A-Za-z0-9_-]{1,128}", message_id)): + return + if header.get("app_id") != s.feishu_app_id: + return + try: + text = json.loads(message["content"])["text"].strip() + if not text or len(text) > 8000: + return + # Persistent receipt BEFORE any side effect. Restarts do not execute a delivery twice. + event_turn = "event_" + hashlib.sha256(message_id.encode()).hexdigest() + if not store.begin_turn(event_turn, owner, "event"): + return + if text == "/绑定": + result = await make_binding_code(owner) + elif text == "/解绑": + old = store.by_owner(owner) + store.unbind(owner) + if old: + await r.incr("hn:web-generation:" + old["host_id"]) + await r.publish("hn:kill:" + old["host_id"], "unbind") + for item in broker.pending.values(): + if item.host == old["host_id"] and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + result = "已解绑;重新绑定后请更新本机绑定信息。" + elif text == "/主机名" or text.startswith("/回显 "): + result = (await broker.dispatch(owner, text, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + else: + instruction = text if text.startswith("/助手 ") else "/助手 " + text + result = (await broker.dispatch(owner, instruction, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + await f.reply(message_id, result[:s.max_result]) + store.finish(event_turn, "replied") + except Exception: + store.audit(owner, "event", message_id, "failed_or_unknown") + + @app.post("/internal/events") + async def internal_event(request: Request): + require_secret(request.headers.get("X-Internal-Secret"), s.internal_secret) + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(await request.json())) + return {"accepted": True} + + @app.post("/feishu/event") + async def webhook(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if not s.feishu_encrypt_key or not s.feishu_verification_token: + raise HTTPException(503, "webhook credentials not configured; use selected event source") + await rate_limit(r, "webhook:" + peer(request), 600) + raw = await request.body() + timestamp, nonce, signature = (request.headers.get(x, "") for x in + ("X-Lark-Request-Timestamp", "X-Lark-Request-Nonce", "X-Lark-Signature")) + try: + if abs(time.time() - int(timestamp)) > 300 or not nonce or len(nonce) > 128: + raise ValueError("stale request") + expected = hashlib.sha256((timestamp + nonce + s.feishu_encrypt_key).encode() + raw).hexdigest() + if not hmac.compare_digest(expected, signature): + raise ValueError("bad signature") + data = json.loads(raw) + if "encrypt" in data: + cipher = base64.b64decode(data["encrypt"], validate=True) + decryptor = Cipher(algorithms.AES(hashlib.sha256(s.feishu_encrypt_key.encode()).digest()), + modes.CBC(cipher[:16])).decryptor() + padded = decryptor.update(cipher[16:]) + decryptor.finalize() + unpadder = padding.PKCS7(128).unpadder() + data = json.loads(unpadder.update(padded) + unpadder.finalize()) + token = data.get("token", data.get("header", {}).get("token", "")) + if not hmac.compare_digest(token, s.feishu_verification_token): + raise ValueError("invalid verification token") + except Exception: + raise HTTPException(403, "invalid event authentication") from None + if data.get("type") == "url_verification": + return {"challenge": data.get("challenge", "")} + # Same delivery is ACKed again; durable message_id receipt handles execution deduplication. + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(data)) + return {"code": 0} + + @app.get("/api/whoami") + async def whoami(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(r, "whoami:" + peer(request)) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await f.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "no device bound") + if not s.device_domain or not re.fullmatch(r"[a-zA-Z0-9.-]+", s.device_domain): + raise HTTPException(503, "device web domain not configured") + return {"host_id": device["host_id"], "url": f"https://h-{device['host_id']}.{s.device_domain}/", + "online": bool(await r.exists("hn:online:" + device["host_id"])), + "device_login_required": True} + + @app.get("/h5", response_class=HTMLResponse) + async def h5(): + from importlib.resources import files + return HTMLResponse(files("relay").joinpath("web/index.html").read_text(encoding="utf-8"), + headers={"Content-Security-Policy": "default-src 'self'; script-src 'self'; style-src 'self'; frame-ancestors 'self'; base-uri 'none'; form-action 'self'"}) + + @app.get("/assets/{name}") + async def asset(name: str): + from importlib.resources import files + from starlette.responses import Response + if name not in {"app.js", "style.css"}: + raise HTTPException(404, "not found") + return Response(files("relay").joinpath("web/" + name).read_bytes(), + media_type="text/javascript" if name.endswith(".js") else "text/css") + + @app.get("/proxy") + async def proxy(request: Request): + if not s.proxy_enabled or not s.feishu_enabled: + raise HTTPException(404, "not found") + auth = request.headers.get("Authorization", "") + target = request.headers.get("X-Relay-Target", "") + decoded = unquote(target) + parts = urlsplit(target) + allowed = s.proxy_paths.split(",") + if (not auth.startswith("Bearer ") or len(auth) > 8192 or len(target) > 4096 + or parts.scheme or parts.netloc or parts.fragment or target.startswith("//") + or any(x in decoded for x in ("..", "\\", "\r", "\n", "#", "%")) + or not any(parts.path == x or parts.path.startswith(x + "/") for x in allowed)): + raise HTTPException(403, "target not allowed") + try: + info = await f.user_info(auth[7:]) + await rate_limit(r, "proxy:" + info["open_id"], 60) + # Buffer bounded response; no disk/cache, redirects never followed. + async with f.http.stream("GET", target, headers={"Authorization": auth}) as response: + if response.status_code in {301, 302, 303, 307, 308}: + raise HTTPException(502, "upstream redirect refused") + chunks, size = [], 0 + async for chunk in response.aiter_bytes(): + size += len(chunk) + if size > 2 * 1024 * 1024: + raise HTTPException(502, "response too large") + chunks.append(chunk) + from starlette.responses import Response + store.audit(info["open_id"], "proxy", parts.path, str(response.status_code)) + return Response(b"".join(chunks), status_code=response.status_code, + media_type=response.headers.get("content-type", "application/json")) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + + return app diff --git a/container-code/longconn/relay/crypto.py b/container-code/longconn/relay/crypto.py new file mode 100644 index 0000000..dd962e5 --- /dev/null +++ b/container-code/longconn/relay/crypto.py @@ -0,0 +1,140 @@ +"""Signed ephemeral X25519 envelopes. The router never receives plaintext keys.""" +import base64 +import hashlib +import json +import secrets +import time +from datetime import datetime, timedelta, timezone + +from cryptography import x509 +from cryptography.hazmat.primitives import hashes, serialization +from cryptography.hazmat.primitives.asymmetric import ed25519, x25519 +from cryptography.hazmat.primitives.ciphers.aead import AESGCM +from cryptography.hazmat.primitives.kdf.hkdf import HKDF +from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID + + +def b64(data: bytes) -> str: + return base64.urlsafe_b64encode(data).decode() + + +def unb64(data: str) -> bytes: + return base64.b64decode(data.encode(), altchars=b"-_", validate=True) + + +def canonical(data: dict) -> bytes: + return json.dumps(data, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode() + + +def raw_public(key) -> str: + return b64(key.public_bytes(serialization.Encoding.Raw, serialization.PublicFormat.Raw)) + + +def pem_private(key) -> bytes: + return key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, + serialization.NoEncryption()) + + +def load_private(path): + protected = path.with_name(path.name + ".dpapi") + if protected.is_file(): + from .windows_keys import unprotect + data = unprotect(protected.read_bytes()) + else: + data = path.read_bytes() + return serialization.load_pem_private_key(data, password=None) + + +def load_public(path): + return serialization.load_pem_public_key(path.read_bytes()) + + +def fingerprint(pem: str) -> str: + return x509.load_pem_x509_certificate(pem.encode()).fingerprint(hashes.SHA256()).hex() + + +def verify_device(pem: str, ca_pem: bytes, host_id: str): + """v1 deliberately accepts only directly issued Ed25519 device certificates.""" + cert = x509.load_pem_x509_certificate(pem.encode()) + ca = x509.load_pem_x509_certificate(ca_pem) + now = datetime.now(timezone.utc) + if not (ca.not_valid_before_utc <= now <= ca.not_valid_after_utc): + raise ValueError("expired CA") + if not ca.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("not a CA") + cert.verify_directly_issued_by(ca) + if not (cert.not_valid_before_utc <= now <= cert.not_valid_after_utc): + raise ValueError("expired certificate") + if cert.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("CA cannot be used as device") + if ExtendedKeyUsageOID.CLIENT_AUTH not in cert.extensions.get_extension_for_class(x509.ExtendedKeyUsage).value: + raise ValueError("clientAuth missing") + names = cert.extensions.get_extension_for_class(x509.SubjectAlternativeName).value + if names.get_values_for_type(x509.UniformResourceIdentifier) != [f"urn:hainuo:host:{host_id}"]: + raise ValueError("host identity mismatch") + public = cert.public_key() + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("v1 requires Ed25519 device key") + return public + + +def sign(data: dict, key) -> dict: + return {**data, "signature": b64(key.sign(canonical(data)))} + + +def verified(data: dict, key) -> dict: + body = {k: v for k, v in data.items() if k != "signature"} + key.verify(unb64(data["signature"]), canonical(body)) + return body + + +def seal(payload: dict, recipient_public: str, signer, *, host_id: str, session_id: str) -> dict: + ephemeral = x25519.X25519PrivateKey.generate() + header = {"v": 1, "host_id": host_id, "session_id": session_id, + "ts": int(time.time()), "nonce": b64(secrets.token_bytes(12)), + "ephemeral": raw_public(ephemeral.public_key())} + shared = ephemeral.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(recipient_public))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + cipher = AESGCM(key).encrypt(unb64(header["nonce"]), canonical(payload), canonical(header)) + return sign({**header, "ciphertext": b64(cipher)}, signer) + + +def open_envelope(envelope: dict, recipient_private, sender_public, *, host_id: str, session_id: str) -> dict: + body = verified(envelope, sender_public) + if body["v"] != 1 or body["host_id"] != host_id or body["session_id"] != session_id: + raise ValueError("wrong destination/session") + if abs(time.time() - body["ts"]) > 90: + raise ValueError("expired envelope") + header = {k: v for k, v in body.items() if k != "ciphertext"} + shared = recipient_private.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(body["ephemeral"]))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + plain = AESGCM(key).decrypt(unb64(body["nonce"]), unb64(body["ciphertext"]), canonical(header)) + return json.loads(plain) + + +def issue_ca(): + key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Hainuo device test CA")]) + now = datetime.now(timezone.utc) + cert = (x509.CertificateBuilder().subject_name(name).issuer_name(name) + .public_key(key.public_key()).serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=3650)) + .add_extension(x509.BasicConstraints(ca=True, path_length=0), critical=True) + .add_extension(x509.KeyUsage(False, False, False, False, False, True, True, False, False), critical=True) + .sign(key, algorithm=None)) + return key, cert + + +def issue_device(ca_key, ca_cert, host_id: str, public_key, days=30): + now = datetime.now(timezone.utc) + return (x509.CertificateBuilder() + .subject_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, host_id)])) + .issuer_name(ca_cert.subject).public_key(public_key) + .serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=days)) + .add_extension(x509.BasicConstraints(ca=False, path_length=None), critical=True) + .add_extension(x509.SubjectAlternativeName([x509.UniformResourceIdentifier(f"urn:hainuo:host:{host_id}")]), critical=False) + .add_extension(x509.ExtendedKeyUsage([ExtendedKeyUsageOID.CLIENT_AUTH]), critical=False) + .sign(ca_key, algorithm=None)) diff --git a/container-code/longconn/relay/device.py b/container-code/longconn/relay/device.py new file mode 100644 index 0000000..a8fe010 --- /dev/null +++ b/container-code/longconn/relay/device.py @@ -0,0 +1,216 @@ +"""Reference PC bridge. No shell/PowerShell execution; local read-only diagnostics.""" +import asyncio +import contextlib +import json +import os +import secrets +import socket +import sqlite3 +import time +from pathlib import Path +from urllib.parse import urlsplit + +import httpx +from cryptography.hazmat.primitives.asymmetric import x25519 +from websockets.asyncio.client import connect + +from .crypto import (load_private, load_public, open_envelope, raw_public, seal, sign, + verified, verify_device) + + +class DeviceJournal: + def __init__(self, path): + path.parent.mkdir(parents=True, exist_ok=True) + self.db = sqlite3.connect(path) + self.db.execute("CREATE TABLE IF NOT EXISTS consumed(turn_id TEXT PRIMARY KEY, ts REAL NOT NULL)") + self.db.commit() + + def consume(self, turn): + with self.db: + return self.db.execute("INSERT OR IGNORE INTO consumed VALUES(?,?)", (turn, time.time())).rowcount == 1 + + def close(self): + self.db.close() + + +async def execute_readonly(text, root: Path | None): + if text in {"/主机名", "hostname"}: + return socket.gethostname() + if text.startswith("/回显 "): + return text[4:][:16000] + if text.startswith("/读取 "): + if root is None: + return "未配置测试文件目录。" + base = root.resolve(strict=True) + try: + path = (base / text[4:].strip()).resolve(strict=True) + if path == base or not path.is_relative_to(base) or not path.is_file(): + return "拒绝:文件不在允许的测试目录。" + if path.stat().st_size > 32768: + return "拒绝:测试文件超过32KB。" + return path.read_text(encoding="utf-8")[:16000] + except (OSError, UnicodeError, ValueError): + return "拒绝:文件不存在、不可读或不是UTF-8文本。" + return "当前桥接客户端仅开放只读诊断:/主机名、/回显 内容、/读取 相对路径;尚未接入海诺本地Agent接口。" + + +class LocalAgent: + """Explicit adapter contract, not a claim of compatibility with installer 1.065.""" + def __init__(self, url, transport=None): + parts = urlsplit(url) + if parts.scheme != "http" or parts.hostname not in {"127.0.0.1", "::1"} or parts.username or parts.password or parts.fragment: + raise ValueError("local Agent must use a fixed loopback HTTP endpoint") + self.url = url + self.http = httpx.AsyncClient(timeout=30, follow_redirects=False, transport=transport, trust_env=False) + + async def execute(self, text, owner, turn): + headers = {} + if os.environ.get("HN_LOCAL_AGENT_TOKEN"): + headers["Authorization"] = "Bearer " + os.environ["HN_LOCAL_AGENT_TOKEN"] + try: + async with self.http.stream("POST", self.url, headers=headers, + json={"text": text, "open_id": owner, "turn_id": turn, + "channel": "im", "read_only": True}) as response: + if response.status_code != 200: + return "本机Agent未返回成功结果,请查看本机服务状态。" + body = b"" + async for chunk in response.aiter_bytes(): + body += chunk + if len(body) > 65536: + return "本机Agent结果超过限制。" + result = json.loads(body)["result"] + if not isinstance(result, str): + return "本机Agent响应格式不匹配。" + return result[:16000] + except (httpx.HTTPError, ValueError, KeyError): + return "本机Agent连接失败或响应格式不匹配。" + + async def close(self): + await self.http.aclose() + + +async def bind_device(directory: Path, connector_url: str, code: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + private = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "code": code, "ts": int(time.time())}, private) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as http: + response = await http.post(connector_url.rstrip("/") + "/device/bind", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"binding failed: HTTP {response.status_code}") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt["host_id"] != config["host_id"]: + raise ValueError("wrong binding receipt") + config["owner"] = receipt["owner"] + temp = directory / "device.json.tmp" + temp.write_text(json.dumps(config, ensure_ascii=False, indent=2), encoding="utf-8") + temp.replace(directory / "device.json") + + +async def pair_browser(directory: Path, connector_url: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8-sig")) + if not config.get("owner"): + raise RuntimeError("device must be bound first") + key = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "owner": config["owner"], + "purpose": "h5-pair-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as client: + response = await client.post(connector_url.rstrip("/") + "/device/web-pair", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"browser pairing failed: HTTP {response.status_code}") + result = verified(response.json(), load_public(directory / "connector.pub")) + if result["host_id"] != config["host_id"]: + raise ValueError("wrong pairing receipt") + return result + + +async def run_device(directory: Path, router_url: str, **kwargs): + from .process_lock import DeviceProcessLock + with DeviceProcessLock(directory): + return await _run_device(directory, router_url, **kwargs) + + +async def _run_device(directory: Path, router_url: str, *, root=None, once=False, ssl_context=None, + stop_event=None, connected_event=None, agent_url=None, hainuo_config=None): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + if not config.get("owner"): + raise RuntimeError("bind device before starting") + host, owner = config["host_id"], config["owner"] + key = load_private(directory / "device.key") + connector = load_public(directory / "connector.pub") + cert = (directory / "device.pem").read_text(encoding="utf-8") + verify_device(cert, (directory / "ca.pem").read_bytes(), host) + journal = DeviceJournal(directory / "journal.sqlite") + if agent_url and hainuo_config: + raise ValueError("choose one Agent adapter") + if hainuo_config: + from .hainuo1065 import Hainuo1065Agent + agent = Hainuo1065Agent(hainuo_config) + else: + agent = LocalAgent(agent_url) if agent_url else None + stop_event = stop_event or asyncio.Event() + delay = 1 + try: + while not stop_event.is_set(): + try: + kwargs = {"ssl": ssl_context} if router_url.startswith("wss://") and ssl_context else {} + async with connect(router_url, max_size=262144, open_timeout=10, ping_interval=10, + proxy=None, **kwargs) as ws: + challenge = json.loads(await ws.recv())["challenge"] + ephemeral = x25519.X25519PrivateKey.generate() + session = secrets.token_hex(16) + proof = sign({"host_id": host, "session_id": session, "challenge": challenge, + "kx": raw_public(ephemeral.public_key()), "ts": int(time.time())}, key) + await ws.send(json.dumps({"host_id": host, "cert": cert, "proof": proof})) + accepted = json.loads(await ws.recv()) + if accepted.get("type") != "connected": + raise RuntimeError("device connection rejected") + print("设备连接成功,已绑定本人海能Work身份。", flush=True) + if connected_event: + connected_event.set() + delay = 1 + while not stop_event.is_set(): + try: + raw = await asyncio.wait_for(ws.recv(), timeout=1) + except asyncio.TimeoutError: + continue + data = json.loads(raw) + payload = open_envelope(data["envelope"], ephemeral, connector, + host_id=host, session_id=session) + if payload.get("owner") != owner or payload.get("type") != "instruction": + continue + turn = payload["turn_id"] + if not isinstance(turn, str) or len(turn) > 128: + continue + if not journal.consume(turn): + result = "该请求已处理,拒绝重复执行。" + elif payload.get("read_only") is not True or payload.get("channel") != "im": + result = "拒绝:通道或操作类型不受支持。" + else: + if agent and payload["text"].startswith("/助手 "): + result = await agent.execute(payload["text"][4:], owner, turn) + else: + result = await execute_readonly(payload["text"], root) + reply = seal({"type": "result", "turn_id": turn, "result": result}, + payload["reply_key"], key, host_id=host, session_id=session) + await ws.send(json.dumps({"type": "reply", "turn_id": turn, "envelope": reply})) + if once: + return + except asyncio.CancelledError: + raise + except Exception as error: + if once: + raise + from websockets.exceptions import ConnectionClosed + if isinstance(error, ConnectionClosed) and error.rcvd and error.rcvd.code == 1008: + print("设备被中继拒绝:请检查是否有另一桥接进程、设备准入或证书状态。", flush=True) + # No payloads/URLs/credentials in logs, bounded reconnect backoff + jitter. + print("设备连接中断,等待重连。", flush=True) + with contextlib.suppress(asyncio.TimeoutError): + await asyncio.wait_for(stop_event.wait(), timeout=delay + secrets.randbelow(1000) / 1000) + delay = min(delay * 2, 30) + finally: + journal.close() + if agent: + await agent.close() diff --git a/container-code/longconn/relay/enrollment.py b/container-code/longconn/relay/enrollment.py new file mode 100644 index 0000000..b4ac77f --- /dev/null +++ b/container-code/longconn/relay/enrollment.py @@ -0,0 +1,178 @@ +"""Device registration with key possession proof; Work binding is separate.""" +import json +import secrets +import time +import httpx + +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey +from fastapi import APIRouter, HTTPException, Request +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .crypto import verified, sign, raw_public, verify_device, fingerprint +from .web_session import digest +from fastapi.responses import JSONResponse + + +def validate_enrollment(body): + try: + key = serialization.load_pem_public_key(body.public_key.encode()) + if not isinstance(key, Ed25519PublicKey): + raise ValueError() + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["purpose"] != "device-enroll-v1" + or abs(time.time() - proof["ts"]) > 90 or len(proof["nonce"]) != 32): + raise ValueError() + return key + except Exception: + raise HTTPException(403, "invalid enrollment proof") from None + + +async def request_certificate(settings, body): + async with httpx.AsyncClient(trust_env=False, timeout=15, follow_redirects=False) as http: + response = await http.post(settings.enrollment_signer_url.rstrip("/") + "/issue", + headers={"X-Internal-Secret": settings.internal_secret}, + json=body.model_dump()) + response.raise_for_status() + return response.json()["cert"] + + +class EnrollmentRequest(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + public_key: str = Field(max_length=1024) + proof: dict + + +class EnrollmentApproval(BaseModel): + cert: str = Field(max_length=10000) + + +def enrollment_router(settings, redis, store, signer, admin): + router = APIRouter() + + @router.post("/device/register") + async def automatic_registration(body: EnrollmentRequest, request: Request): + # No human approval: eligibility follows intranet access plus Work app scope. + # Proof/TLS/limits protect identity and resources, and do not add user approval. + await rate_limit(redis, "auto-register-ip:" + peer(request), 120) + key = validate_enrollment(body) + await rate_limit(redis, "auto-register-host:" + body.host_id, 30) + existing = store.device(body.host_id) + + def check_existing(device): + if device["revoked"]: + raise HTTPException(403, "device revoked") + try: + registered = verify_device(device["cert"], settings.ca_cert.read_bytes(), body.host_id) + if raw_public(registered) != raw_public(key): + raise ValueError() + except Exception: + raise HTTPException(409, "existing device identity mismatch or certificate expired") from None + return device["cert"] + + if existing: + cert = check_existing(existing) + else: + if not settings.enrollment_signer_url: + raise HTTPException(503, "automatic registration is not enabled; update ECS first") + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= settings.max_devices: + raise HTTPException(503, "device capacity reached") + await rate_limit(redis, "auto-register-total", settings.max_pending, 3600) + try: + cert = await request_certificate(settings, body) + issued = verify_device(cert, settings.ca_cert.read_bytes(), body.host_id) + if raw_public(issued) != raw_public(key): + raise ValueError() + except Exception: + raise HTTPException(503, "certificate issuer unavailable or invalid response") from None + from .store import INTEGRITY_ERRORS + try: + store.add_device(body.host_id, cert, fingerprint(cert)) + except INTEGRITY_ERRORS: + # A retry/concurrent request must return the original certificate. + cert = check_existing(store.device(body.host_id)) + store.audit("device", "automatic_registration", body.host_id, "success") + await redis.set("hn:device:" + body.host_id, fingerprint(cert)) + return no_cache(JSONResponse(sign({"status": "registered", "host_id": body.host_id, + "cert": cert}, signer), status_code=201 if not existing else 200)) + + @router.post("/device/enroll") + async def request_enrollment(body: EnrollmentRequest, request: Request): + await rate_limit(redis, "enroll-ip:" + peer(request), 10) + validate_enrollment(body) + await rate_limit(redis, "enroll-host:" + body.host_id, 3, 3600) + if store.device(body.host_id): + raise HTTPException(409, "device already registered; recover its original certificate") + # Global rolling capacity bound; prevents anonymous request-table exhaustion. + await rate_limit(redis, "enroll-global", settings.max_pending, 1800) + identifier, claim_secret = secrets.token_hex(16), secrets.token_urlsafe(32) + record = {"host_id": body.host_id, "public_key": body.public_key, + "claim_hash": digest(claim_secret), "created": time.time(), "status": "pending"} + await redis.set("hn:enroll:" + identifier, json.dumps(record), ex=1800) + return no_cache(JSONResponse(sign({"request_id": identifier, "claim_secret": claim_secret, + "host_id": body.host_id, "expires_in": 1800}, signer), status_code=201)) + + @router.get("/device/enroll/{identifier}") + async def claim(identifier: str, request: Request): + await rate_limit(redis, "enroll-poll:" + peer(request), 60) + data = await redis.get("hn:enroll:" + identifier) + if not data: + raise HTTPException(404, "request expired") + record = json.loads(data) + import hmac + if not hmac.compare_digest(record["claim_hash"], digest(request.headers.get("X-Enrollment-Secret", ""))): + raise HTTPException(404, "request unavailable") + if record["status"] == "pending": + return no_cache(JSONResponse({"status": "pending"}, status_code=202)) + device = store.device(record["host_id"]) + if not device or device["revoked"]: + raise HTTPException(403, "device unavailable") + # Recoverable for its original claimant for the request TTL; public certificate only. + return no_cache(JSONResponse(sign({"status": "approved", "host_id": record["host_id"], + "request_id": identifier, "cert": device["cert"]}, signer))) + + @router.get("/admin/enrollments/{identifier}") + async def inspect(identifier: str, request: Request): + admin(request) + encoded = await redis.get("hn:enroll:" + identifier) + if not encoded: + raise HTTPException(404, "request expired") + record = json.loads(encoded) + return {k: v for k, v in record.items() if k != "claim_hash"} + + @router.post("/admin/enrollments/{identifier}/approve") + async def approve(identifier: str, body: EnrollmentApproval, request: Request): + admin(request) + encoded = await redis.get("hn:enroll:" + identifier) + if not encoded: + raise HTTPException(404, "request expired") + record = json.loads(encoded) + try: + public = verify_device(body.cert, settings.ca_cert.read_bytes(), record["host_id"]) + requested = serialization.load_pem_public_key(record["public_key"].encode()) + if raw_public(public) != raw_public(requested): + raise ValueError() + except Exception: + raise HTTPException(400, "certificate does not match enrollment") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= settings.max_devices: + raise HTTPException(503, "device capacity reached") + fp = fingerprint(body.cert) + existing = store.device(record["host_id"]) + if existing and (existing["revoked"] or existing["fingerprint"] != fp): + raise HTTPException(409, "device conflict") + if not existing: + from .store import INTEGRITY_ERRORS + try: + store.add_device(record["host_id"], body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "concurrent device registration") from None + await redis.set("hn:device:" + record["host_id"], fp) + record["status"] = "approved" + remaining = await redis.ttl("hn:enroll:" + identifier) + if remaining < 1: + raise HTTPException(409, "request expired during approval") + await redis.set("hn:enroll:" + identifier, json.dumps(record), ex=remaining) + store.audit("admin", "enrollment_approved", record["host_id"], "success") + return {"status": "approved", "host_id": record["host_id"]} + + return router diff --git a/container-code/longconn/relay/enrollment_signer.py b/container-code/longconn/relay/enrollment_signer.py new file mode 100644 index 0000000..6089d02 --- /dev/null +++ b/container-code/longconn/relay/enrollment_signer.py @@ -0,0 +1,43 @@ +"""Internal automatic CA issuer. No published host port, no application credentials.""" +import os +from pathlib import Path + +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from fastapi import FastAPI, Request +from .common import BodyLimitMiddleware, require_secret +from .crypto import load_private, issue_device, raw_public, verify_device +from .enrollment import EnrollmentRequest, validate_enrollment + + +def create_app(*, ca_key_path=None, ca_cert_path=None, secret=None): + key = load_private(Path(ca_key_path or os.environ.get("HN_ISSUER_CA_KEY", "/issuer/ca.key"))) + ca_bytes = Path(ca_cert_path or os.environ.get("HN_ISSUER_CA_CERT", "/trust/ca.pem")).read_bytes() + ca = x509.load_pem_x509_certificate(ca_bytes) + if raw_public(key.public_key()) != raw_public(ca.public_key()): + raise ValueError("issuer CA key mismatch") + token = secret or os.environ.get("HN_INTERNAL_SECRET", "") + if len(token) < 32: + raise ValueError("issuer requires internal credential") + app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=16384) + + @app.get("/ready") + async def ready(): + return {"status": "ready"} + + @app.post("/issue") + async def issue(body: EnrollmentRequest, request: Request): + require_secret(request.headers.get("X-Internal-Secret"), token) + public = validate_enrollment(body) + cert = issue_device(key, ca, body.host_id, public).public_bytes(serialization.Encoding.PEM).decode() + verify_device(cert, ca_bytes, body.host_id) + return {"cert": cert} + + return app + + +if __name__ == "__main__": + import uvicorn + uvicorn.run("relay.enrollment_signer:create_app", factory=True, host="0.0.0.0", port=8000, + workers=1, access_log=False, proxy_headers=False, log_level="warning") diff --git a/container-code/longconn/relay/hainuo1065.py b/container-code/longconn/relay/hainuo1065.py new file mode 100644 index 0000000..6f7735b --- /dev/null +++ b/container-code/longconn/relay/hainuo1065.py @@ -0,0 +1,107 @@ +"""Adapter for the native API actually found in installer 1.065. + +Loopback only, independent API key, no platform secrets, no redirects or retries. +The included native launcher offers only text inference with no tools. +""" +import hashlib +import json +import re +from pathlib import Path +from urllib.parse import urlsplit + +import httpx + + +class NativeAPIError(RuntimeError): + """Contains a sanitized status only, never response body or credentials.""" + + +def read_native_config(path): + path = Path(path) + data = json.loads(path.read_text(encoding="utf-8-sig")) + url = data.get("url", "") + parts = urlsplit(url) + if (parts.scheme != "http" or parts.hostname != "127.0.0.1" or not parts.port + or parts.username or parts.password or parts.query or parts.fragment + or parts.path not in {"", "/"}): + raise ValueError("native API must be a fixed IPv4 loopback HTTP base URL") + if not isinstance(data.get("api_key"), str) or len(data["api_key"]) < 32: + raise ValueError("native API requires a dedicated key of at least 32 characters") + if data.get("mode") not in {"text-only", "profile-tools"}: + raise ValueError("unsupported native API mode") + data["url"] = url.rstrip("/") + return data + + +class Hainuo1065Agent: + def __init__(self, config_path, transport=None): + self.config = read_native_config(config_path) + self.http = httpx.AsyncClient(base_url=self.config["url"], timeout=120, + follow_redirects=False, trust_env=False, transport=transport) + self.headers = {"Authorization": "Bearer " + self.config["api_key"]} + + async def _json(self, method, path, **kwargs): + try: + async with self.http.stream(method, path, headers={**self.headers, **kwargs.pop("headers", {})}, **kwargs) as response: + if response.status_code != 200: + raise NativeAPIError(f"native API HTTP {response.status_code}") + body = bytearray() + async for chunk in response.aiter_bytes(): + body.extend(chunk) + if len(body) > 262144: + raise NativeAPIError("native API response exceeds limit") + data = json.loads(body) + if not isinstance(data, dict): + raise NativeAPIError("native API response is not an object") + return data + except (httpx.HTTPError, ValueError): + raise NativeAPIError("native API connection/JSON failure") from None + + async def probe(self): + health = await self._json("GET", "/health") + if health.get("platform") != "hermes-agent" or health.get("status") != "ok": + raise NativeAPIError("endpoint is not the native Hermes API") + capabilities = await self._json("GET", "/v1/capabilities") + policy = capabilities.get("hainuo_relay_policy", {}) + # A stock unrestricted API endpoint is deliberately not sufficient. + expected_mode = self.config["mode"] + expected_tools = "profile" if expected_mode == "profile-tools" else "disabled" + if policy != {"mode": expected_mode, "tools": expected_tools, "contract": "hainuo-1065-v1"}: + raise NativeAPIError("native API policy missing; start the supplied restricted launcher") + models = await self._json("GET", "/v1/models") + model = self.config.get("model") + available = models.get("data") + if not isinstance(available, list) or not available or not all(isinstance(x, dict) and isinstance(x.get("id"), str) for x in available): + raise NativeAPIError("native model list invalid") + if model and model not in {x["id"] for x in available}: + raise NativeAPIError("configured model alias unavailable") + self.model = model or available[0]["id"] + return {"status": "ready", "platform": "hermes-agent", "model": self.model, + "mode": expected_mode, "version": health.get("version", "unknown")} + + async def execute(self, text, owner, turn): + try: + # Recheck the actual local service before each inference, including reconnects. + await self.probe() + digest = hashlib.sha256((owner + "\0" + turn).encode()).hexdigest() + data = await self._json("POST", "/v1/chat/completions", + headers={"Idempotency-Key": "hn-" + digest}, + json={"model": self.model, "stream": False, + "messages": [{"role": "user", "content": text}]}) + choices = data.get("choices") + if not isinstance(choices, list) or not choices or not isinstance(choices[0], dict): + raise NativeAPIError("native completion choices missing") + choice = choices[0] + message = choice.get("message", {}) + if (choice.get("finish_reason") != "stop" or message.get("tool_calls") + or not isinstance(message.get("content"), str) + or data.get("hermes", {}).get("failed") + or data.get("hermes", {}).get("partial")): + raise NativeAPIError("native completion incomplete or unexpected tool call") + return message["content"][:16000] + except NativeAPIError as error: + # Safe generated error strings only; never echo upstream model/API errors. + return "海诺原生接口未完成请求:" + str(error) + "。未自动重试。" + + async def close(self): + await self.http.aclose() diff --git a/container-code/longconn/relay/longconn.py b/container-code/longconn/relay/longconn.py new file mode 100644 index 0000000..6d7be70 --- /dev/null +++ b/container-code/longconn/relay/longconn.py @@ -0,0 +1,83 @@ +"""Optional official SDK event source, private platform domain explicitly configured.""" +import json +import logging +import os +import threading +import time + +import httpx +import redis +from .config import Settings + + +def disable_proxy_environment(): + """This dedicated SDK worker must connect directly, including discovery and WS.""" + for key in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "http_proxy", "https_proxy", "all_proxy"): + os.environ.pop(key, None) + os.environ["NO_PROXY"] = "*" + os.environ["no_proxy"] = "*" + + +def main(): + disable_proxy_environment() + import lark_oapi as lark + s = Settings(role="longconn") + if not s.feishu_enabled or s.feishu_event_source != "longconn": + raise RuntimeError("platform is disabled") + # The SDK may log websocket URLs, tokens or event bodies. Disable its logging entirely. + logging.disable(logging.CRITICAL) + r = redis.Redis.from_url(s.redis_url) + lease = r.lock("hn:longconn:leader", timeout=30, thread_local=False) + if not lease.acquire(blocking=False): + raise RuntimeError("another long-connection consumer owns the lease") + url = os.environ.get("HN_CONNECTOR_INTERNAL_URL", "http://connector:8000") + client = httpx.Client(base_url=url, timeout=10, follow_redirects=False, trust_env=False) + done = threading.Event() + + def maintain_lease(): + while not done.wait(8): + try: + lease.extend(30, replace_ttl=True) + except Exception: + # Fail closed. Continuing after lease loss could create two consumers. + os._exit(2) + + threading.Thread(target=maintain_lease, daemon=True).start() + + def received(event): + data = json.loads(lark.JSON.marshal(event)) + response = client.post("/internal/events", headers={"X-Internal-Secret": s.internal_secret}, json=data) + if response.status_code != 200: + raise RuntimeError("connector did not accept event") + print(json.dumps({"event": "platform_event_accepted", "type": data.get("header", {}).get("event_type")}), flush=True) + + handler = lark.EventDispatcherHandler.builder("", "").register_p2_im_message_receive_v1(received).build() + class ObservableClient(lark.ws.Client): + async def _connect(self): + try: + await super()._connect() + if self._conn is not None: + print(json.dumps({"event": "platform_connected", "api_host": s.feishu_api_base}), flush=True) + except Exception as error: + print(json.dumps({"event": "platform_connect_failed", "error_type": type(error).__name__}), flush=True) + raise + + try: + # domain is an actual supported SDK argument; no public-cloud-domain monkey patch. + ObservableClient(s.feishu_app_id, s.feishu_app_secret, event_handler=handler, + domain=s.feishu_api_base.rstrip("/"), log_level=lark.LogLevel.ERROR).start() + finally: + done.set() + client.close() + try: + lease.release() + except redis.exceptions.LockError: + pass + + +if __name__ == "__main__": + try: + main() + except Exception as error: + print(json.dumps({"event": "platform_consumer_stopped", "error_type": type(error).__name__}), flush=True) + raise SystemExit(1) from None diff --git a/container-code/longconn/relay/oauth.py b/container-code/longconn/relay/oauth.py new file mode 100644 index 0000000..1b35a14 --- /dev/null +++ b/container-code/longconn/relay/oauth.py @@ -0,0 +1,144 @@ +import asyncio +import hashlib +import hmac +import secrets +import time +from dataclasses import dataclass +from urllib.parse import urlencode + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .platform import PlatformError + + +@dataclass +class Transaction: + secret_hash: str + expires: float + status: str = "waiting" + result: dict | None = None + owner: str = "" + + +class Empty(BaseModel): + host_id: str = "" + proof: dict = Field(default_factory=dict) + + +class Refresh(BaseModel): + refresh_token: str = Field(min_length=1, max_length=4096) + + +class OAuth: + """A single connector process owns transactions and tokens in memory only.""" + def __init__(self, settings, redis, platform): + self.settings, self.redis, self.platform = settings, redis, platform + self.pending: dict[str, Transaction] = {} + self.lock = asyncio.Lock() + self.router = APIRouter() + self.router.add_api_route("/oauth/start", self.start, methods=["POST"], status_code=201) + self.router.add_api_route("/oauth/cb", self.callback, methods=["GET"]) + self.router.add_api_route("/oauth/claim", self.claim, methods=["GET"]) + self.router.add_api_route("/oauth/pending", self.status, methods=["GET"]) + self.router.add_api_route("/oauth/refresh", self.refresh, methods=["POST"]) + + def prune(self): + now = time.monotonic() + for state in list(self.pending): + if self.pending[state].expires <= now: + del self.pending[state] + + def authenticated(self, state, secret): + self.prune() + item = self.pending.get(state) + digest = hashlib.sha256((secret or "").encode()).hexdigest() + if not item or not hmac.compare_digest(item.secret_hash, digest): + raise HTTPException(404, "transaction not found") + return item + + async def start(self, body: Empty, request: Request): + if not self.settings.feishu_enabled or not self.settings.oauth_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.redis, "oauth:start:" + peer(request), 20) + owner = "" + if body.host_id: + from .work_api import device_owner + owner = await device_owner(self.settings, self.redis, self.store, body.host_id, + body.proof, "work-oauth-v1", {}) + async with self.lock: + self.prune() + if len(self.pending) >= self.settings.max_pending: + raise HTTPException(503, "too many pending transactions") + state, secret = secrets.token_urlsafe(32), secrets.token_urlsafe(32) + self.pending[state] = Transaction(hashlib.sha256(secret.encode()).hexdigest(), time.monotonic() + 600, owner=owner) + redirect = self.settings.public_url.rstrip("/") + "/oauth/cb" + parameters = {"app_id": self.settings.feishu_app_id, "redirect_uri": redirect, "state": state} + if self.settings.feishu_oauth_scopes: + parameters["scope"] = self.settings.feishu_oauth_scopes + url = self.settings.feishu_authorize_url + "?" + urlencode(parameters) + return no_cache(JSONResponse({"state": state, "claim_secret": secret, "authorization_url": url, + "redirect_uri": redirect, "expires_in": 600, + "code_ttl": 60, "poll_interval": 2}, status_code=201)) + + async def callback(self, request: Request, state: str = "", code: str = "", error: str = ""): + await rate_limit(self.redis, "oauth:cb:" + peer(request)) + async with self.lock: + self.prune() + item = self.pending.get(state) + if error and item and item.status == "waiting": + item.status = "failed" + return no_cache(HTMLResponse("

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

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

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

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

连接你的
工作电脑。

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

+
01 / 连接本人设备

输入电脑上的配对码

+

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

+
+
使用海能Work授权

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

+ +

正在检查网页会话…

+
PC需保持开机、联网,并运行原生API和桥接端。试点网页由中继在内存中转发正文,不保存聊天记录;当前页关闭后消息不保留。
+
diff --git a/container-code/longconn/relay/web/style.css b/container-code/longconn/relay/web/style.css new file mode 100644 index 0000000..a33d19c --- /dev/null +++ b/container-code/longconn/relay/web/style.css @@ -0,0 +1,3 @@ +:root{color-scheme:light;font-family:system-ui,-apple-system,"Segoe UI","Microsoft YaHei",sans-serif;color:#17372c;background:#f2f4ed}*{box-sizing:border-box}body{margin:0}main{max-width:820px;margin:0 auto;padding:60px 26px}.mark{font-size:13px;letter-spacing:.16em;color:#557764;font-weight:700}h1{font-size:clamp(42px,8vw,70px);letter-spacing:-.05em;line-height:1.12;font-weight:650;margin:40px 0 22px}.intro{font-size:17px;line-height:1.8;color:#56675a;max-width:520px}.card{background:#fff;border:1px solid #dbe3d8;border-radius:20px;padding:30px;margin:40px 0 24px;box-shadow:0 12px 44px #18372c06}.step{font-size:12px;letter-spacing:.1em;color:#6d806c}h2{font-size:24px;margin:16px 0}.card p{line-height:1.8;color:#5b6c60}button{background:#214e3a;color:white;border:0;border-radius:10px;font:inherit;font-weight:600;padding:16px 20px;width:100%;text-align:left;cursor:pointer;margin-top:16px}button span{float:right}button:disabled{opacity:.5;cursor:wait}button:focus-visible,a:focus-visible{outline:3px solid #d0a349;outline-offset:4px}#status{font-size:13px;margin-bottom:0}#open{display:block;margin-top:22px;color:#214e3a;font-weight:600}#open[hidden]{display:none}footer{color:#768373;font-size:12px;line-height:1.8}@media(max-width:480px){main{padding-top:36px}.card{padding:22px}h1{margin-top:28px}} + +[hidden]{display:none!important}.badge{float:right;font-size:11px;letter-spacing:0;background:#e4ebdf;border-radius:20px;padding:6px 10px}label{display:block;font-size:13px;color:#526c5b;margin:12px 0 8px}input,textarea{font:inherit;width:100%;border:1px solid #ccd8ce;border-radius:10px;padding:14px;color:#17372c;background:#fafcf8;resize:vertical}input:focus,textarea:focus{outline:2px solid #6c987b;outline-offset:2px}details{margin-top:24px;font-size:13px;color:#587561}summary{cursor:pointer}.chat-head{display:flex;align-items:center;justify-content:space-between;gap:20px}.secondary{width:auto;background:#edf2e9;color:#31523f;padding:9px 12px;font-size:12px;margin:0}.hint{font-size:13px}#messages{display:flex;flex-direction:column;gap:12px;margin:22px 0;max-height:420px;overflow:auto}.message{padding:14px 16px;border-radius:12px;line-height:1.8;white-space:pre-wrap;overflow-wrap:anywhere;font-size:15px}.message.user{background:#e5eee2;margin-left:36px}.message.assistant{background:#f2f5ef;margin-right:24px}.message.notice{background:#fbf4e5;color:#735c2e}#status{font-size:13px;color:#5f745f;line-height:1.8}#chat-panel h2{font-size:20px}footer{max-width:640px} diff --git a/container-code/longconn/relay/web_session.py b/container-code/longconn/relay/web_session.py new file mode 100644 index 0000000..1ac9adf --- /dev/null +++ b/container-code/longconn/relay/web_session.py @@ -0,0 +1,183 @@ +"""Pilot H5 pairing and conversation. Credentials never appear in URLs.""" +import hashlib +import json +import secrets +import time +from urllib.parse import urlsplit + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import JSONResponse +from pydantic import BaseModel, Field + +from .common import no_cache, peer, rate_limit +from .crypto import sign, verified, verify_device +from .platform import PlatformError + +COOKIE = "hn_web_session" + + +def digest(value): + return hashlib.sha256(value.encode()).hexdigest() + + +def origin_tuple(value): + try: + parts = urlsplit(value) + if parts.scheme not in {"https", "http"} or not parts.hostname or parts.username or parts.password: + return None + return parts.scheme, parts.hostname.lower(), parts.port or (443 if parts.scheme == "https" else 80) + except ValueError: + return None + + +class PairProof(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Claim(BaseModel): + code: str = Field(min_length=12, max_length=128) + + +class Chat(BaseModel): + text: str = Field(min_length=1, max_length=8000) + request_id: str = Field(pattern=r"^[a-f0-9]{32}$") + + +class WebSessions: + def __init__(self, settings, redis, store, broker, platform): + self.s, self.r, self.store, self.broker, self.platform = settings, redis, store, broker, platform + self.router = APIRouter() + for path, method, handler in [ + ("/device/web-pair", "POST", self.device_pair), + ("/web/claim", "POST", self.claim), + ("/web/session", "GET", self.session), + ("/web/platform-session", "POST", self.platform_login), + ("/web/chat", "POST", self.chat), + ("/web/logout", "POST", self.logout), + ]: + self.router.add_api_route(path, handler, methods=[method]) + + def check_origin(self, request): + actual = origin_tuple(request.headers.get("Origin", "")) + if not actual or actual != origin_tuple(self.s.public_url): + raise HTTPException(403, "same-origin browser request required") + + async def valid_device(self, record): + device = self.store.device(record["host_id"]) + if (not device or device["revoked"] or device["owner"] != record["owner"] + or device["fingerprint"] != record["fingerprint"]): + raise HTTPException(401, "device binding changed; pair again") + generation = await self.r.get("hn:web-generation:" + record["host_id"]) or "0" + if record.get("generation") != generation: + raise HTTPException(401, "device binding changed; pair again") + return device + + async def device_pair(self, body: PairProof, request: Request): + await rate_limit(self.r, "web-pair-ip:" + peer(request), 20) + device = self.store.device(body.host_id) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "active bound device required") + try: + key = verify_device(device["cert"], self.s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["owner"] != device["owner"] + or proof["purpose"] != "h5-pair-v1" or abs(time.time() - proof["ts"]) > 90 + or not isinstance(proof["nonce"], str) or len(proof["nonce"]) != 32): + raise ValueError("invalid proof") + except Exception: + raise HTTPException(403, "invalid device proof") from None + replay = "hn:web-pair-proof:" + digest(body.host_id + proof["nonce"]) + if not await self.r.set(replay, "used", nx=True, ex=180): + raise HTTPException(409, "pairing request already used") + await rate_limit(self.r, "web-pair-device:" + body.host_id, 5) + code = secrets.token_urlsafe(18) + record = {"host_id": body.host_id, "owner": device["owner"], "fingerprint": device["fingerprint"], + "generation": await self.r.get("hn:web-generation:" + body.host_id) or "0"} + await self.r.set("hn:web-code:" + digest(code), json.dumps(record), ex=120) + self.store.audit(device["owner"], "web_pair_issued", body.host_id, "success") + return no_cache(JSONResponse(sign({"host_id": body.host_id, "code": code, "expires_in": 120, + "url": self.s.public_url.rstrip("/") + "/h5"}, self.broker.signer))) + + async def new_session(self, record): + token = secrets.token_urlsafe(32) + record = {k: record[k] for k in ("host_id", "owner", "fingerprint", "generation")} + await self.r.set("hn:web-session:" + digest(token), json.dumps(record), ex=1800) + response = no_cache(JSONResponse({"status": "paired", "host_id": record["host_id"], "expires_in": 1800})) + response.set_cookie(COOKIE, token, max_age=1800, secure=True, httponly=True, samesite="strict", path="/") + return response + + async def claim(self, body: Claim, request: Request): + self.check_origin(request) + await rate_limit(self.r, "web-claim:" + peer(request), 20) + key = "hn:web-code:" + digest(body.code.strip()) + encoded = await self.r.get(key) + if not encoded: + raise HTTPException(404, "code expired or consumed") + await self.valid_device(json.loads(encoded)) + encoded = await self.r.getdel(key) + if not encoded: + raise HTTPException(404, "code already consumed") + record = json.loads(encoded) + await self.valid_device(record) + self.store.audit(record["owner"], "web_pair_claimed", record["host_id"], "success") + return await self.new_session(record) + + async def authenticated(self, request): + token = request.cookies.get(COOKIE, "") + if not token or len(token) > 128: + raise HTTPException(401, "pair this browser first") + encoded = await self.r.get("hn:web-session:" + digest(token)) + if not encoded: + raise HTTPException(401, "browser session expired") + record = json.loads(encoded) + await self.valid_device(record) + return record + + async def session(self, request: Request): + record = await self.authenticated(request) + return no_cache(JSONResponse({"authenticated": True, "host_id": record["host_id"], + "online": bool(await self.r.exists("hn:online:" + record["host_id"])), + "mode": "pilot-text-only", "relay_sees_content": True})) + + async def platform_login(self, request: Request): + self.check_origin(request) + if not self.s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.r, "web-platform:" + peer(request), 20) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await self.platform.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = self.store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "bind your device first") + return await self.new_session({**device, "generation": await self.r.get("hn:web-generation:" + device["host_id"]) or "0"}) + + async def chat(self, body: Chat, request: Request): + self.check_origin(request) + record = await self.authenticated(request) + await rate_limit(self.r, "web-chat:" + record["owner"], 20) + text = body.text.strip() + if not text: + raise HTTPException(422, "empty message") + # PC accepts only its bound owner and the existing IM read-only channel. + if not (text == "/主机名" or text.startswith(("/回显 ", "/读取 ", "/助手 "))): + text = "/助手 " + text + turn = "web_" + digest(record["owner"] + "\0" + body.request_id) + result = await self.broker.dispatch(record["owner"], text, turn) + # Revoke/unbind during a long model call invalidates the browser immediately. + await self.authenticated(request) + return no_cache(JSONResponse(result)) + + async def logout(self, request: Request): + self.check_origin(request) + token = request.cookies.get(COOKIE, "") + if token and len(token) <= 128: + await self.r.delete("hn:web-session:" + digest(token)) + response = no_cache(JSONResponse({"status": "logged_out"})) + response.delete_cookie(COOKIE, secure=True, httponly=True, samesite="strict", path="/") + return response diff --git a/container-code/longconn/relay/windows_keys.py b/container-code/longconn/relay/windows_keys.py new file mode 100644 index 0000000..0e84e19 --- /dev/null +++ b/container-code/longconn/relay/windows_keys.py @@ -0,0 +1,40 @@ +"""Current-user Windows DPAPI for device keys; never uses machine-wide scope.""" +import ctypes +import os +from ctypes import wintypes + + +class Blob(ctypes.Structure): + _fields_ = [("cbData", wintypes.DWORD), ("pbData", ctypes.POINTER(ctypes.c_ubyte))] + + +def transform(data, decrypt=False): + if os.name != "nt": + raise RuntimeError("DPAPI is supported only on Windows") + buffer = (ctypes.c_ubyte * len(data)).from_buffer_copy(data) + source = Blob(len(data), buffer) + target = Blob() + crypt = ctypes.WinDLL("crypt32", use_last_error=True) + kernel = ctypes.WinDLL("kernel32", use_last_error=True) + if decrypt: + fn = crypt.CryptUnprotectData + args = [ctypes.byref(source), None, None, None, None, 1, ctypes.byref(target)] + else: + fn = crypt.CryptProtectData + args = [ctypes.byref(source), "Hainuo device identity", None, None, None, 1, ctypes.byref(target)] + fn.restype = wintypes.BOOL + if not fn(*args): + raise ctypes.WinError(ctypes.get_last_error()) + try: + return ctypes.string_at(target.pbData, target.cbData) + finally: + kernel.LocalFree.argtypes = [ctypes.c_void_p] + kernel.LocalFree(target.pbData) + + +def protect(data): + return transform(data) + + +def unprotect(data): + return transform(data, decrypt=True) diff --git a/container-code/longconn/relay/work_api.py b/container-code/longconn/relay/work_api.py new file mode 100644 index 0000000..96a6e49 --- /dev/null +++ b/container-code/longconn/relay/work_api.py @@ -0,0 +1,152 @@ +"""Device-authenticated USER-token business gateway; no tenant-token fallback.""" +import hashlib +import json +import re +import time +from urllib.parse import unquote + +import httpx +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import JSONResponse +from pydantic import BaseModel, Field +from .common import no_cache, rate_limit +from .crypto import canonical, verified, verify_device +from .store import INTEGRITY_ERRORS + + +class DeviceProof(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class BusinessCall(DeviceProof): + method: str = Field(pattern=r"^(GET|POST|PATCH|DELETE)$") + path: str = Field(max_length=1500) + params: dict = Field(default_factory=dict) + body: dict = Field(default_factory=dict) + operation_id: str = Field(default="", max_length=100) + + +def business_allowed(method, path): + if any(x in unquote(path) for x in ("..", "\\", "%", "?", "#", "\r", "\n")): + return False + seg = r"[A-Za-z0-9_.@-]+" + cal = r"/open-apis/calendar/v4/calendars" + rules = { + "GET": [cal, cal + "/" + seg, cal + "/" + seg + "/events(?:/" + seg + ")?(?:/attendees)?", + r"/open-apis/contact/v3/users/" + seg, r"/open-apis/search/v1/user", + r"/open-apis/(?:drive/v1/files|docx/v1/documents|wiki/v2/spaces|bitable/v1/apps)(?:/" + seg + ")*"], + "POST": [cal + "/primary", cal + "/" + seg + "/events(?:/" + seg + "/attendees)?"], + "PATCH": [cal + "/" + seg + "/events/" + seg], + "DELETE": [cal + "/" + seg + "/events/" + seg, cal + "/" + seg + "/events/" + seg + "/attendees/" + seg], + } + return any(re.fullmatch(p, path) for p in rules.get(method, [])) + + +async def device_owner(settings, redis, store, host, proof, purpose, payload): + device = store.device(host) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "active bound device required") + try: + key = verify_device(device["cert"], settings.ca_cert.read_bytes(), host) + data = verified(proof, key) + if (data["host_id"] != host or data["purpose"] != purpose or abs(time.time() - data["ts"]) > 90 + or not re.fullmatch(r"[a-f0-9]{32}", data["nonce"]) + or data["payload_hash"] != hashlib.sha256(canonical(payload)).hexdigest()): + raise ValueError() + except Exception: + raise HTTPException(403, "invalid device request proof") from None + if not await redis.set("hn:work-proof:" + host + ":" + data["nonce"], "1", nx=True, ex=180): + raise HTTPException(409, "device request replayed") + return device["owner"] + + +def router(settings, redis, store, platform): + routes = APIRouter() + store.db.execute("""CREATE TABLE IF NOT EXISTS business_operations( + owner TEXT NOT NULL, operation_id TEXT NOT NULL, request_hash TEXT NOT NULL, + status TEXT NOT NULL, result_meta TEXT NOT NULL, created DOUBLE PRECISION NOT NULL, + PRIMARY KEY(owner,operation_id))""") + if not settings.database_url: + store.db.commit() + + @routes.post("/work/api") + async def call(body: BusinessCall, request: Request): + if not settings.feishu_enabled: + raise HTTPException(503, "platform disabled") + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or not 10 < len(auth) <= 8192: + raise HTTPException(401, "user access token required") + if not business_allowed(body.method, body.path): + raise HTTPException(403, "business method/path not supported") + payload = {"method": body.method, "path": body.path, "params": body.params, "body": body.body, + "operation_id": body.operation_id, "token_hash": hashlib.sha256(auth[7:].encode()).hexdigest()} + owner = await device_owner(settings, redis, store, body.host_id, body.proof, "work-api-v1", payload) + await rate_limit(redis, "work-api:" + owner, 60) + try: + info = await platform.user_info(auth[7:]) + except Exception: + raise HTTPException(401, "user token expired or invalid; refresh or authorize") from None + if info.get("open_id") != owner: + raise HTTPException(403, "token user does not match bound device owner") + write = body.method != "GET" and not body.path.endswith(("/primary", "/batch_get_id")) + digest = hashlib.sha256(canonical({k: v for k, v in payload.items() if k != "token_hash"})).hexdigest() + if write: + if not re.fullmatch(r"[A-Za-z0-9_-]{8,100}", body.operation_id): + raise HTTPException(422, "write requires stable operation_id") + try: + with store.db: + store.db.execute("INSERT INTO business_operations VALUES(?,?,?,?,?,?)", + (owner, body.operation_id, digest, "inflight", "{}", time.time())) + except INTEGRITY_ERRORS: + row = dict(store.db.execute("SELECT * FROM business_operations WHERE owner=? AND operation_id=?", + (owner, body.operation_id)).fetchone()) + if row["request_hash"] != digest: + raise HTTPException(409, "operation_id reused with different request") + return no_cache(JSONResponse({"status": "duplicate", "operation_id": body.operation_id, + "previous_status": row["status"], **json.loads(row["result_meta"])})) + params = dict(body.params) + if write and body.method == "POST" and body.path.endswith("/events"): + params["idempotency_key"] = hashlib.sha256((owner + body.operation_id).encode()).hexdigest() + try: + async with platform.http.stream(body.method, body.path, params=params, + json=body.body if body.method != "GET" else None, + headers={"Authorization": auth}) as upstream: + raw = bytearray() + async for chunk in upstream.aiter_bytes(): + raw.extend(chunk) + if len(raw) > 2 * 1024 * 1024: + raise ValueError() + data = json.loads(raw) + if not isinstance(data, dict): + raise ValueError() + code = data.get("code", 0) + if upstream.status_code != 200 or code != 0: + status = "platform_rejected" + def safe_diagnostic(value): + text = str(value) + for secret in (auth, auth[7:], settings.feishu_app_secret, settings.admin_secret, settings.internal_secret): + if secret: + text = text.replace(secret, "[redacted]") + return text[:2000] + detail = data.get("error") or {} + result = {"status": status, "http_status": upstream.status_code, "platform_code": code, + "message": "平台拒绝:请核对用户授权、应用scope、资源权限或接口版本。", + "platform_message": safe_diagnostic(data.get("msg", data.get("message", ""))), + "platform_log_id": safe_diagnostic(detail.get("log_id", "")) if isinstance(detail, dict) else "", + "permission_details": safe_diagnostic(detail.get("permission_violations", [])) if isinstance(detail, dict) else ""} + else: + status, result = "completed", {"status": "completed", "data": data.get("data", {})} + except (httpx.HTTPError, ValueError): + status = "unknown" if write else "failed" + result = {"status": status, "message": "平台请求未取得明确结果;写操作不要换新ID重试,请先核对日历。"} + if write: + event = result.get("data", {}).get("event", {}) + meta = {k: event[k] for k in ("event_id", "app_link") if k in event} + with store.db: + store.db.execute("UPDATE business_operations SET status=?,result_meta=? WHERE owner=? AND operation_id=?", + (status, json.dumps(meta), owner, body.operation_id)) + store.audit(owner, "work_api_" + body.method.lower(), body.path, status) + return no_cache(JSONResponse(result)) + + return routes diff --git a/container-code/longconn/relay/work_client.py b/container-code/longconn/relay/work_client.py new file mode 100644 index 0000000..4cc90b3 --- /dev/null +++ b/container-code/longconn/relay/work_client.py @@ -0,0 +1,190 @@ +"""PC-side user OAuth and calendar operations. Tokens are DPAPI-protected only here.""" +import hashlib +import json +import secrets +import ssl +import time +from datetime import datetime +from pathlib import Path +from urllib.parse import quote, urlsplit + +import httpx +from .crypto import canonical, load_private, sign +from .windows_keys import protect, unprotect +from .process_lock import DeviceProcessLock + + +class WorkError(RuntimeError): + pass + + +class AuthorizationRequired(WorkError): + def __init__(self, url): + self.url = url + super().__init__("需要本人Work授权,请打开授权链接,完成后重新发起原任务;尚未执行平台写操作。") + + +def atomic_secret(path, data): + temp = path.with_suffix(".tmp") + temp.write_bytes(protect(json.dumps(data, ensure_ascii=False).encode())) + temp.replace(path) + + +class WorkClient: + def __init__(self, directory, url="https://10.77.76.13", *, transport=None): + self.directory = Path(directory) + self.config = json.loads((self.directory / "device.json").read_text(encoding="utf-8-sig")) + if not self.config.get("owner"): + raise WorkError("请先绑定本人Work身份。") + context = ssl.create_default_context(cafile=str(self.directory / "server.pem")) + self.http = httpx.Client(base_url=url.rstrip("/"), verify=context, trust_env=False, + timeout=25, follow_redirects=False, transport=transport) + self.owner = self.config["owner"] + self.path = self.directory / "work-user-tokens.dpapi" + self.pending = self.directory / "work-oauth-pending.dpapi" + + def close(self): + self.http.close() + + def read(self, path): + return json.loads(unprotect(path.read_bytes())) + + def proof(self, purpose, payload): + return sign({"host_id": self.config["host_id"], "purpose": purpose, "ts": int(time.time()), + "nonce": secrets.token_hex(16), "payload_hash": hashlib.sha256(canonical(payload)).hexdigest()}, + load_private(self.directory / "device.key")) + + def authorize(self): + if self.pending.exists(): + data = self.read(self.pending) + if data.get("owner") == self.owner and data["deadline"] > time.time(): + return data["authorization_url"] + response = self.http.post("/oauth/start", json={"host_id": self.config["host_id"], + "proof": self.proof("work-oauth-v1", {})}) + if response.status_code != 201: + raise WorkError("授权事务创建失败(HTTP " + str(response.status_code) + "),请检查ECS平台配置。") + data = response.json() + if urlsplit(data["authorization_url"]).scheme != "https": + raise WorkError("授权地址必须使用HTTPS。") + data.update(owner=self.owner, deadline=time.time() + data["expires_in"]) + atomic_secret(self.pending, data) + return data["authorization_url"] + + def save_tokens(self, data): + if data.get("open_id") != self.owner or not data.get("user_access_token"): + raise WorkError("授权账号与本机Work绑定账号不一致,请使用本人账号重新授权。") + now = time.time() + data = {**data, "expires_at": now + int(data["expires_in"]), + "refresh_expires_at": now + int(data.get("refresh_expires_in", 0)), "needs_authorization": False} + atomic_secret(self.path, data) + return data + + def access_token(self): + # Shared across GUI and Agent calls; never rotate the same token concurrently. + with DeviceProcessLock(self.directory / "work-token-lock"): + if self.path.exists(): + data = self.read(self.path) + if data.get("open_id") == self.owner and not data.get("needs_authorization"): + if data["expires_at"] > time.time() + 120: + return data["user_access_token"] + if data.get("refresh_token") and data.get("refresh_expires_at", 0) > time.time(): + # Mark before network; ambiguous failure must not reuse a rotating refresh token. + atomic_secret(self.path, {**data, "needs_authorization": True}) + try: + response = self.http.post("/oauth/refresh", json={"refresh_token": data["refresh_token"]}) + if response.status_code != 200: + raise WorkError("刷新失败") + data = self.save_tokens(response.json()) + return data["user_access_token"] + except (httpx.HTTPError, ValueError, WorkError): + raise AuthorizationRequired(self.authorize()) from None + if self.pending.exists(): + pending = self.read(self.pending) + if pending.get("owner") == self.owner and pending["deadline"] > time.time(): + response = self.http.get("/oauth/claim", params={"state": pending["state"]}, + headers={"X-OAuth-Claim-Secret": pending["claim_secret"]}) + if response.status_code == 200: + data = self.save_tokens(response.json()) + self.pending.unlink(missing_ok=True) + return data["user_access_token"] + if response.status_code not in {202}: + self.pending.unlink(missing_ok=True) + raise AuthorizationRequired(self.authorize()) + + def status(self): + try: + token = self.access_token() + return {"authorized": True, "owner": self.owner, "message": "本人用户授权可用;令牌未输出。", + "local_time": datetime.now().astimezone().isoformat()} + except AuthorizationRequired as error: + return {"authorized": False, "authorization_url": error.url, "message": str(error)} + + def api(self, method, path, params=None, body=None, operation_id=""): + token = self.access_token() + payload = {"method": method, "path": path, "params": params or {}, "body": body or {}, + "operation_id": operation_id, "token_hash": hashlib.sha256(token.encode()).hexdigest()} + proof = self.proof("work-api-v1", payload) + wire = {k: v for k, v in payload.items() if k != "token_hash"} + response = self.http.post("/work/api", headers={"Authorization": "Bearer " + token}, + json={**wire, "host_id": self.config["host_id"], "proof": proof}) + if response.status_code == 401: + # Do not repeat a business write automatically, even when auth fails. + if self.path.exists(): + data = self.read(self.path) + data["expires_at"] = 0 + atomic_secret(self.path, data) + raise WorkError("用户令牌已失效。本次未重试,请检查授权后再核对操作状态。") + if response.status_code != 200: + raise WorkError("平台代理失败(HTTP " + str(response.status_code) + ");未重试。") + return response.json() + + def primary_calendar(self): + result = self.api("POST", "/open-apis/calendar/v4/calendars/primary", {"user_id_type": "open_id"}) + if result.get("status") != "completed": + return result + calendars = result.get("data", {}).get("calendars", []) + own = [c["calendar"] for c in calendars if c.get("user_id") == self.owner and c.get("calendar")] + if not own and len(calendars) == 1 and not calendars[0].get("user_id") and calendars[0].get("calendar"): + own = [calendars[0]["calendar"]] + if len(own) != 1: + raise WorkError("没有唯一确定的本人主日历,请先查询日历列表并明确选择。") + return {"status": "completed", "calendar": own[0]} + + def create_event(self, summary, start_iso, end_iso, attendee_open_ids, operation_id, description="", calendar_id=""): + import re + if not re.fullmatch(r"[A-Za-z0-9_-]{8,80}", operation_id): + raise WorkError("创建日程需8到80位稳定operation_id,重试不得换ID。") + if not summary.strip() or len(summary) > 1000: + raise WorkError("日程主题不能为空或超过1000字符。") + start, end = datetime.fromisoformat(start_iso), datetime.fromisoformat(end_iso) + if start.tzinfo is None or end.tzinfo is None or end <= start: + raise WorkError("起止时间必须包含时区且结束晚于开始;不要猜测时区或时长。") + if any(not isinstance(x, str) or not x.startswith("ou_") for x in attendee_open_ids): + raise WorkError("参会人必须为当前应用身份域的open_id,请先搜索并确认,不能把姓名/工号直接当ID。") + if not calendar_id: + primary = self.primary_calendar() + if primary.get("status") != "completed": + return primary + calendar_id = primary["calendar"]["calendar_id"] + path = "/open-apis/calendar/v4/calendars/" + quote(calendar_id, safe="") + "/events" + body = {"summary": summary, "description": description, "need_notification": True, + "start_time": {"timestamp": str(int(start.timestamp())), "timezone": "Asia/Shanghai"}, + "end_time": {"timestamp": str(int(end.timestamp())), "timezone": "Asia/Shanghai"}} + result = self.api("POST", path, {"user_id_type": "open_id"}, body, operation_id) + event = result.get("data", {}).get("event", {}) + if result.get("status") == "duplicate" and result.get("previous_status") == "completed": + event = {"event_id": result.get("event_id"), "app_link": result.get("app_link")} + elif result.get("status") != "completed": + return result + if not event.get("event_id"): + raise WorkError("平台未返回event_id,不能认定创建成功或继续邀请。") + ids = sorted(set([self.owner] + list(attendee_open_ids))) + invite = self.api("POST", path + "/" + quote(event["event_id"], safe="") + "/attendees", + {"user_id_type": "open_id"}, + {"attendees": [{"type": "user", "user_id": x} for x in ids], "need_notification": True}, + operation_id + "_attendees") + invited = invite.get("status") == "completed" or (invite.get("status") == "duplicate" and invite.get("previous_status") == "completed") + return {"status": "completed" if invited else "partial", "event_created": True, + "calendar_id": calendar_id, "event_id": event["event_id"], "app_link": event.get("app_link"), + "attendees_invited": invited, "invite_result": invite, + "message": "日程已创建并提交参会人邀请;不代表对方已接受。" if invited else "日程已创建,但邀请未完成;保留原操作ID和event_id处理,勿重新创建日程。"} diff --git a/container-code/longconn/relay/work_tools.py b/container-code/longconn/relay/work_tools.py new file mode 100644 index 0000000..b1c6da4 --- /dev/null +++ b/container-code/longconn/relay/work_tools.py @@ -0,0 +1,145 @@ +"""Hermes native tools backed by the authenticated user-token relay.""" +import json +import os +import threading +import time +from pathlib import Path +import httpx +from .work_client import WorkClient, WorkError, AuthorizationRequired + +INSTRUCTIONS = """海能Work平台能力已接入本机专用工具:work_authorization、work_calendar、work_contacts、work_api_request。 +涉及飞书/海能Work日历或文档时优先调用这些工具,勿使用manage_connections寻找公有云连接器,勿索要或搜索App Secret。 +用户token仅由本机安全存储和ECS换取,第一次需要本人打开授权链接。工具返回authorization_required时将链接完整提供给本人,不能声称已经创建。 +当前日期以work_authorization返回的local_time为准。创建日程必须确定带时区的起止时间和真实参会人open_id。 +姓名/工号须用work_contacts搜索核实,多个候选必须澄清,不能猜open_id。平台返回partial表示已创建但邀请未完成,勿再次创建。 +同一写操作的operation_id必须保持不变,网络异常不得换ID重试。日程创建成功、邀请提交成功、对方接受是不同状态。 +""" + +_watching = set() +_watch_lock = threading.Lock() + + +def watch_authorization(directory): + """Claim within the server's 60-second result TTL, without keeping tokens in ECS.""" + name = str(directory) + with _watch_lock: + if name in _watching: + return + _watching.add(name) + def poll(): + client = None + try: + client = WorkClient(directory, os.environ.get("HN_WORK_RELAY_URL", "https://10.77.76.13")) + for _ in range(295): + time.sleep(2) + try: + if client.pending.exists() and client.read(client.pending).get("deadline", 0) <= time.time(): + return + if client.status().get("authorized"): + return + except Exception: + # Another local consumer may hold the short token lock. + continue + except Exception: + pass + finally: + if client: + client.close() + with _watch_lock: + _watching.discard(name) + threading.Thread(target=poll, daemon=True, name="work-user-auth-claim").start() + + +def register(directory): + from tools.registry import registry + import toolsets + definitions = [ + ("work_authorization", "查询或发起本人海能Work用户授权,返回可点击链接和本机时间;不返回token。", { + "type": "object", "properties": {}, "additionalProperties": False}), + ("work_calendar", "本人用户身份的日历:list/primary/list_events/get_event/create_event/invite。写操作operation_id需稳定,起止时间均带时区。", { + "type": "object", "properties": { + "action": {"type": "string", "enum": ["list", "primary", "list_events", "get_event", "create_event", "invite"]}, + "calendar_id": {"type": "string"}, "event_id": {"type": "string"}, + "summary": {"type": "string"}, "description": {"type": "string"}, + "start_iso": {"type": "string", "description": "ISO8601,例如2026-10-27T09:00:00+08:00"}, + "end_iso": {"type": "string"}, "attendee_open_ids": {"type": "array", "items": {"type": "string"}}, + "operation_id": {"type": "string", "description": "8-80位稳定操作ID,同一意图重试不能更换"}, + "params": {"type": "object"}}, "required": ["action"]}), + ("work_contacts", "使用本人user token搜索参会人;query可为姓名或工号,返回候选,不能未经核实猜ID。open_id可读取具体用户信息。", { + "type": "object", "properties": {"query": {"type": "string"}, "open_id": {"type": "string"}}}), + ("work_api_request", "携本人user token代理受支持业务API(日历CRUD/邀请、通讯录查询、文档云盘知识库读取)。禁止认证接口,不支持的路径返回403。写操作必须operation_id。", { + "type": "object", "properties": {"method": {"type": "string", "enum": ["GET", "POST", "PATCH", "DELETE"]}, + "path": {"type": "string"}, "params": {"type": "object"}, "body": {"type": "object"}, + "operation_id": {"type": "string"}}, "required": ["method", "path"]}), + ] + + def handler_for(name): + def handle(args, **kwargs): + client = None + try: + client = WorkClient(directory, os.environ.get("HN_WORK_RELAY_URL", "https://10.77.76.13")) + if name == "work_authorization": + result = client.status() + if not result.get("authorized"): + watch_authorization(directory) + elif name == "work_api_request": + result = client.api(**args) + elif name == "work_contacts": + from urllib.parse import quote + result = client.api("GET", "/open-apis/contact/v3/users/" + quote(args["open_id"], safe=""), + {"user_id_type": "open_id"}) if args.get("open_id") else client.api( + "GET", "/open-apis/search/v1/user", {"query": args.get("query", ""), "page_size": 20}) + else: + result = calendar(client, args) + return json.dumps(result, ensure_ascii=False) + except AuthorizationRequired as error: + watch_authorization(directory) + return json.dumps({"status": "authorization_required", "authorization_url": error.url, "message": str(error)}, ensure_ascii=False) + except (WorkError, ValueError, KeyError) as error: + return json.dumps({"error": str(error)[:500], "status": "failed"}, ensure_ascii=False) + except Exception as error: + return json.dumps({"error": type(error).__name__ + ";操作状态未确认,未自动重试。"}, ensure_ascii=False) + finally: + if client: + client.close() + return handle + + for name, description, parameters in definitions: + registry.register(name=name, toolset="hainuo_work", schema={"name": name, "description": description, "parameters": parameters}, + handler=handler_for(name), description=description) + toolsets.TOOLSETS["hainuo_work"] = {"description": "海能Work本人用户授权及日历业务", "tools": [x[0] for x in definitions], "includes": []} + # Present these four small schemas directly; otherwise Hermes defers custom + # toolsets behind tool_search and can wrongly search a public-cloud connector. + for name, _, _ in definitions: + if name not in toolsets._HERMES_CORE_TOOLS: + toolsets._HERMES_CORE_TOOLS.append(name) + + +def calendar(client, args): + from urllib.parse import quote + action = args["action"] + if action == "primary": + return client.primary_calendar() + if action == "list": + return client.api("GET", "/open-apis/calendar/v4/calendars", args.get("params", {})) + if action == "create_event": + return client.create_event(**{k: v for k, v in args.items() if k != "action" and k != "params"}) + calendar_id = args.get("calendar_id") + if not calendar_id: + primary = client.primary_calendar() + if primary.get("status") != "completed": + return primary + calendar_id = primary["calendar"]["calendar_id"] + base = "/open-apis/calendar/v4/calendars/" + quote(calendar_id, safe="") + "/events" + if action == "list_events": + return client.api("GET", base, args.get("params", {})) + base += "/" + quote(args["event_id"], safe="") + if action == "get_event": + return client.api("GET", base, {"user_id_type": "open_id"}) + if action == "invite": + ids = args.get("attendee_open_ids", []) + if not ids or any(not x.startswith("ou_") for x in ids): + raise WorkError("需要已确认的参会人open_id。") + return client.api("POST", base + "/attendees", {"user_id_type": "open_id"}, + {"attendees": [{"type": "user", "user_id": x} for x in ids], "need_notification": True}, args["operation_id"]) + raise WorkError("不支持的日历操作。") diff --git a/container-code/longconn/requirements.lock b/container-code/longconn/requirements.lock new file mode 100644 index 0000000..c763938 --- /dev/null +++ b/container-code/longconn/requirements.lock @@ -0,0 +1,41 @@ +annotated-types==0.8.0 +anyio==4.15.1 +certifi==2026.10.23 +cffi==2.1.1 +charset-normalizer==3.5.2 +click==8.5.0 +colorama==0.4.6 +cryptography==46.0.3 +fakeredis==2.32.0 +fastapi==0.119.1 +h11==0.16.0 +httpcore==1.0.9 +httpx==0.28.1 +idna==3.20 +iniconfig==2.3.1 +lark-oapi==1.5.3 +packaging==26.3 +pluggy==1.7.0 +psycopg==3.2.12 +psycopg-binary==3.2.12 +pycparser==3.12 +pycryptodome==4.2.0 +pydantic==2.14.1 +pydantic-settings==2.11.0 +pydantic_core==2.50.1 +Pygments==2.21.0 +pytest==8.4.2 +pytest-asyncio==1.2.0 +python-dotenv==1.2.4 +PyYAML==6.0.3 +redis==6.4.0 +requests==2.34.2 +requests-toolbelt==1.0.0 +sortedcontainers==2.4.0 +starlette==0.48.0 +typing-inspection==0.4.4 +typing_extensions==4.16.0 +tzdata==2026.5 +urllib3==2.8.0 +uvicorn==0.38.0 +websockets==15.0.1 diff --git a/container-code/router/image-id.txt b/container-code/router/image-id.txt new file mode 100644 index 0000000..b63734c --- /dev/null +++ b/container-code/router/image-id.txt @@ -0,0 +1 @@ +sha256:90324bc252d99afc3ba068e9b416eec1ff31e9302716d1c08a525a45ad0ef011 diff --git a/container-code/router/pyproject.toml b/container-code/router/pyproject.toml new file mode 100644 index 0000000..3bf5e2c --- /dev/null +++ b/container-code/router/pyproject.toml @@ -0,0 +1,32 @@ +[build-system] +requires = ["setuptools>=75"] +build-backend = "setuptools.build_meta" + +[project] +name = "hainuo-relay" +version = "0.1.0" +description = "Hainuo isolated connector and encrypted device router" +requires-python = ">=3.12" +dependencies = [ + "fastapi==0.119.1", "uvicorn==0.38.0", "httpx==0.28.1", + "redis==6.4.0", "cryptography==46.0.3", "websockets==15.0.1", + "pydantic-settings==2.11.0", + "psycopg[binary]==3.2.12", +] + +[project.optional-dependencies] +test = ["pytest==8.4.2", "pytest-asyncio==1.2.0", "fakeredis==2.32.0"] +longconn = ["lark-oapi==1.5.3"] + +[project.scripts] +hainuo-relay = "relay.cli:main" + +[tool.setuptools.packages.find] +include = ["relay*"] + +[tool.setuptools.package-data] +relay = ["web/*.html", "web/*.js", "web/*.css"] + +[tool.pytest.ini_options] +asyncio_mode = "auto" +testpaths = ["tests"] diff --git a/container-code/router/relay/__init__.py b/container-code/router/relay/__init__.py new file mode 100644 index 0000000..32754a7 --- /dev/null +++ b/container-code/router/relay/__init__.py @@ -0,0 +1 @@ +"""Hainuo relay 0.1.0.""" diff --git a/container-code/router/relay/cli.py b/container-code/router/relay/cli.py new file mode 100644 index 0000000..fad7979 --- /dev/null +++ b/container-code/router/relay/cli.py @@ -0,0 +1,216 @@ +import argparse +import asyncio +import json +import os +import secrets +import ssl +import uuid +from datetime import datetime, timedelta, timezone +from pathlib import Path + +import httpx +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ed25519 +from cryptography.x509.oid import NameOID + +from .crypto import issue_ca, issue_device, load_private, pem_private + + +def write_new(path, data, private=False): + path.parent.mkdir(parents=True, exist_ok=True) + with path.open("xb") as f: + f.write(data if isinstance(data, bytes) else data.encode()) + if private and os.name != "nt": + path.chmod(0o600) + + +def init_project(root: Path, hostname: str): + if (root / ".env").exists(): + raise RuntimeError(".env exists; init never overwrites credentials") + ca_key, ca = issue_ca() + signer = ed25519.Ed25519PrivateKey.generate() + write_new(root / "operator/ca.key", pem_private(ca_key), True) + write_new(root / "runtime/trust/ca.pem", ca.public_bytes(serialization.Encoding.PEM)) + write_new(root / "runtime/connector/connector.key", pem_private(signer), True) + write_new(root / "runtime/trust/connector.pub", signer.public_key().public_bytes( + serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo)) + tls_key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, hostname)]) + now = datetime.now(timezone.utc) + tls = (x509.CertificateBuilder().subject_name(name).issuer_name(name).public_key(tls_key.public_key()) + .serial_number(x509.random_serial_number()).not_valid_before(now - timedelta(minutes=5)) + .not_valid_after(now + timedelta(days=30)) + .add_extension(x509.SubjectAlternativeName([x509.DNSName(hostname)]), critical=False) + .sign(tls_key, algorithm=None)) + write_new(root / "runtime/tls/server.key", pem_private(tls_key), True) + write_new(root / "runtime/tls/server.pem", tls.public_bytes(serialization.Encoding.PEM)) + redis_secret = secrets.token_hex(32) + db_secret = secrets.token_hex(32) + env = (f"HN_ENVIRONMENT=development\nHN_PUBLIC_HOST={hostname}\nHN_PUBLIC_URL=https://{hostname}\n" + f"HN_ROUTER_URL=wss://{hostname}/device/ws\nHN_ADMIN_SECRET={secrets.token_hex(32)}\n" + f"HN_INTERNAL_SECRET={secrets.token_hex(32)}\nHN_REDIS_PASSWORD={redis_secret}\n" + f"HN_REDIS_URL=redis://:{redis_secret}@redis:6379/0\n" + f"HN_POSTGRES_PASSWORD={db_secret}\nHN_DATABASE_URL=postgresql://hainuo:{db_secret}@postgres:5432/hainuo\n" + "HN_FEISHU_ENABLED=false\nHN_FEISHU_API_BASE=https://open.feishu.cn\n" + "HN_FEISHU_AUTHORIZE_URL=https://open.feishu.cn/open-apis/authen/v1/authorize\n" + "HN_FEISHU_APP_ID=\nHN_FEISHU_APP_SECRET=\nHN_FEISHU_ENCRYPT_KEY=\n" + "HN_FEISHU_VERIFICATION_TOKEN=\nHN_PROXY_ENABLED=false\nHN_DEVICE_DOMAIN=\n") + write_new(root / ".env", env, True) + print("已生成配置、连接器密钥、设备CA及临时TLS证书;未输出凭据。") + print("operator/ca.key 仅保留在签发工作站,不要上传到ECS。临时TLS证书上线前须替换。") + + +def device_request(directory): + host = uuid.uuid4().hex + private = ed25519.Ed25519PrivateKey.generate() + write_new(directory / "device.key", pem_private(private), True) + public = private.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode() + write_new(directory / "request.json", json.dumps({"host_id": host, "public_key": public}, indent=2)) + print("已在本机生成私钥和 request.json;仅将request.json交给签发工作站。") + + +def issue(root, request_file, output): + request = json.loads(request_file.read_text(encoding="utf-8")) + host = request["host_id"] + if len(host) != 32 or any(c not in "0123456789abcdef" for c in host): + raise ValueError("invalid host identity") + ca = x509.load_pem_x509_certificate((root / "runtime/trust/ca.pem").read_bytes()) + public = serialization.load_pem_public_key(request["public_key"].encode()) + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("Ed25519 required") + cert = issue_device(load_private(root / "operator/ca.key"), ca, host, public) + pem = cert.public_bytes(serialization.Encoding.PEM) + write_new(output / "device.pem", pem) + write_new(output / "ca.pem", (root / "runtime/trust/ca.pem").read_bytes()) + write_new(output / "connector.pub", (root / "runtime/trust/connector.pub").read_bytes()) + write_new(output / "device.json", json.dumps({"host_id": host, "owner": None}, indent=2)) + write_new(output / "registration.json", json.dumps({"host_id": host, "cert": pem.decode()}, indent=2)) + print("已签发设备证书;将device.pem、ca.pem、connector.pub、device.json交回原设备。") + + +def main(): + parser = argparse.ArgumentParser(description="海诺中继部署和设备工具") + sub = parser.add_subparsers(dest="command", required=True) + p = sub.add_parser("init") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--hostname", required=True) + p = sub.add_parser("serve") + p.add_argument("role", choices=["connector", "router"]) + p.add_argument("--host", default="127.0.0.1") + p.add_argument("--port", type=int, default=8000) + p = sub.add_parser("device-request") + p.add_argument("--directory", type=Path, required=True) + p = sub.add_parser("issue-device") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--request", type=Path, required=True) + p.add_argument("--output", type=Path, required=True) + p = sub.add_parser("register") + p.add_argument("--registration", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("bind") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("device") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p.add_argument("--test-root", type=Path) + p.add_argument("--agent-url", help="Explicit loopback read-only Agent adapter endpoint") + p.add_argument("--hainuo-config", type=Path, help="Native installer-1.065 API adapter config; alternative to --agent-url") + p = sub.add_parser("hainuo-probe") + p.add_argument("--config", type=Path, required=True) + p = sub.add_parser("web-pair") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + for command in ("status", "binding-code", "dispatch", "revoke", "unbind", "reconcile"): + p = sub.add_parser(command) + p.add_argument("--url", default="http://127.0.0.1:18081") + p.add_argument("--tls-ca", type=Path) + if command in {"binding-code", "dispatch"}: + p.add_argument("--owner", required=True) + if command == "dispatch": + p.add_argument("--text", required=True) + p.add_argument("--turn-id", default=None) + if command in {"revoke", "unbind"}: + p.add_argument("--host-id", required=True) + args = parser.parse_args() + if args.command == "init": + init_project(args.root, args.hostname) + elif args.command == "serve": + import uvicorn + uvicorn.run(f"relay.{args.role}:create_app", factory=True, host=args.host, port=args.port, + workers=1, access_log=False, proxy_headers=False, + ws_max_size=262144, ws_max_queue=16, log_level="warning") + elif args.command == "device-request": + device_request(args.directory) + elif args.command == "issue-device": + issue(args.root, args.request, args.output) + elif args.command == "register": + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, follow_redirects=False, timeout=15) as client: + response = client.post(args.url.rstrip("/") + "/admin/devices", + headers={"X-Admin-Secret": secret}, + json=json.loads(args.registration.read_text(encoding="utf-8"))) + if response.status_code != 201: + raise RuntimeError(f"register failed: HTTP {response.status_code}") + print("设备登记成功。") + elif args.command == "bind": + import getpass + from .device import bind_device + code = getpass.getpass("Binding code: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + asyncio.run(bind_device(args.directory, args.url, code, tls_verify=context)) + print("绑定成功。") + elif args.command == "device": + from .device import run_device + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else None + asyncio.run(run_device(args.directory, args.url, root=args.test_root, ssl_context=context, + agent_url=args.agent_url, hainuo_config=args.hainuo_config)) + elif args.command == "hainuo-probe": + from .hainuo1065 import Hainuo1065Agent + async def probe(): + adapter = Hainuo1065Agent(args.config) + try: + print(json.dumps(await adapter.probe(), ensure_ascii=False, indent=2)) + finally: + await adapter.close() + asyncio.run(probe()) + elif args.command == "web-pair": + from .device import pair_browser + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + result = asyncio.run(pair_browser(args.directory, args.url, tls_verify=context)) + print("网页地址:" + result["url"]) + print("一次性网页配对码(120秒有效):" + result["code"]) + else: + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, timeout=200, follow_redirects=False) as client: + paths = {"status": "/admin/devices", "binding-code": "/admin/test/binding-code", + "dispatch": "/admin/test/dispatch", "reconcile": "/admin/reconcile"} + if args.command in {"revoke", "unbind"}: + if len(args.host_id) != 32 or any(c not in "0123456789abcdef" for c in args.host_id): + raise ValueError("invalid host identity") + path = f"/admin/devices/{args.host_id}/{args.command}" + else: + path = paths[args.command] + body = None + if args.command in {"binding-code", "dispatch"}: + body = {"open_id": args.owner, "text": getattr(args, "text", "binding")} + if getattr(args, "turn_id", None): + body["turn_id"] = args.turn_id + response = client.request("GET" if args.command == "status" else "POST", args.url.rstrip("/") + path, + headers={"X-Admin-Secret": secret}, json=body) + if response.status_code != 200: + raise RuntimeError(f"operation failed: HTTP {response.status_code}") + print(json.dumps(response.json(), ensure_ascii=False, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/container-code/router/relay/client_manage.py b/container-code/router/relay/client_manage.py new file mode 100644 index 0000000..6b543fe --- /dev/null +++ b/container-code/router/relay/client_manage.py @@ -0,0 +1,293 @@ +"""Per-user Windows connection companion. No platform/application secrets.""" +import argparse +import asyncio +import json +import os +import secrets +import socket +import ssl +import subprocess +import sys +import time +import uuid +from pathlib import Path + +import httpx +import psutil +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey +from .crypto import load_private, load_public, pem_private, raw_public, sign, verified, verify_device +from .windows_keys import protect + +BASE = Path(__file__).resolve().parent.parent +URL = "https://10.77.76.13" + + +def save(path, value): + temp = path.with_suffix(path.suffix + ".tmp") + temp.write_text(json.dumps(value, ensure_ascii=False, indent=2), encoding="utf-8") + os.replace(temp, path) + + +def secure_directory(directory): + directory.mkdir(parents=True, exist_ok=True) + # SID avoids localized account names. Fail closed if ACL protection fails. + result = subprocess.run(["whoami", "/user", "/fo", "csv", "/nh"], capture_output=True, text=True, check=True) + import csv + sid = list(csv.reader([result.stdout.strip()]))[0][1] + subprocess.run(["icacls", str(directory), "/inheritance:r", "/grant:r", f"*{sid}:(OI)(CI)F", "*S-1-5-18:(OI)(CI)F"], + capture_output=True, check=True, creationflags=subprocess.CREATE_NO_WINDOW) + + +def prepare(directory): + secure_directory(directory) + for name in ("server.pem", "ca.pem", "connector.pub"): + target = directory / name + if not target.exists(): + target.write_bytes((BASE / "trust" / name).read_bytes()) + if not (directory / "device.json").exists(): + if (directory / "device.key.dpapi").exists() or (directory / "device.key").exists(): + raise RuntimeError("发现不完整的设备身份,请保留目录并联系管理员恢复。") + private = Ed25519PrivateKey.generate() + (directory / "device.key.dpapi").write_bytes(protect(pem_private(private))) + save(directory / "device.json", {"host_id": uuid.uuid4().hex, "owner": None}) + + +def tls(directory): + return ssl.create_default_context(cafile=str(directory / "server.pem")) + + +def import_existing(directory, source): + """Explicit migration; only public trust and this PC's identity, never app keys.""" + if source.resolve() == directory.resolve(): + raise ValueError("请选择旧设备目录。") + if any((directory / name).exists() for name in ("device.json", "device.key.dpapi", "device.key")): + raise RuntimeError("本目录已有设备身份。请使用已有身份继续,不能用导入覆盖。") + config = json.loads((source / "device.json").read_text(encoding="utf-8-sig")) + for name in ("server.pem", "ca.pem", "connector.pub"): + if (source / name).read_bytes().strip() != (BASE / "trust" / name).read_bytes().strip(): + raise ValueError("旧设备的服务信任与本安装包不符,请联系管理员。") + cert = (source / "device.pem").read_text(encoding="utf-8") + public = verify_device(cert, (source / "ca.pem").read_bytes(), config["host_id"]) + private = load_private(source / "device.key") + if raw_public(public) != raw_public(private.public_key()): + raise ValueError("设备证书与本机私钥不匹配。") + secure_directory(directory) + for name in ("device.pem", "server.pem", "ca.pem", "connector.pub"): + (directory / name).write_bytes((source / name).read_bytes()) + (directory / "device.key.dpapi").write_bytes(protect(pem_private(private))) + save(directory / "device.json", config) + # Preserve local replay journal using SQLite's consistent backup API. + if (source / "journal.sqlite").is_file(): + import sqlite3 + with sqlite3.connect(source / "journal.sqlite") as original, sqlite3.connect(directory / "journal.sqlite") as target: + original.backup(target) + return {"message": "旧设备导入成功,Work绑定已保留。请先停止旧PowerShell桥接窗口,再点击启动连接。"} + + +def enrollment(directory): + prepare(directory) + cfg = json.loads((directory / "device.json").read_text()) + if (directory / "device.pem").exists(): + verify_device((directory / "device.pem").read_text(), (directory / "ca.pem").read_bytes(), cfg["host_id"]) + return {"message": "设备已注册,可在Work获取绑定码后绑定。已有绑定可直接启动连接。"} + with httpx.Client(verify=tls(directory), trust_env=False, timeout=15) as http: + key = load_private(directory / "device.key") + proof = sign({"host_id": cfg["host_id"], "purpose": "device-enroll-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + response = http.post(URL + "/device/register", json={"host_id": cfg["host_id"], "proof": proof, + "public_key": key.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode()}) + if response.status_code not in {200, 201}: + raise RuntimeError("自动注册失败:HTTP " + str(response.status_code) + ";请确认ECS已安装1.067自动注册更新。") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt.get("status") != "registered" or receipt["host_id"] != cfg["host_id"]: + raise ValueError("注册回执身份不符") + public = verify_device(receipt["cert"], (directory / "ca.pem").read_bytes(), cfg["host_id"]) + if raw_public(public) != raw_public(key.public_key()): + raise ValueError("注册证书公钥不符") + temp = directory / "device.pem.tmp" + temp.write_text(receipt["cert"], encoding="utf-8") + os.replace(temp, directory / "device.pem") + (directory / "enrollment.json").unlink(missing_ok=True) + return {"message": "设备已自动注册,无需管理员审批。请在Work获取绑定码,填写后点击绑定并连接。"} + + +def managed_processes(directory): + """Only processes with our absolute entry path AND exact device directory.""" + state = directory / "processes.json" + if not state.exists(): + return [] + result = [] + for item in json.loads(state.read_text()).get("processes", []): + try: + proc = psutil.Process(item["pid"]) + command = proc.cmdline() + if (abs(proc.create_time() - item["created"]) < 0.1 and str(BASE / "entry.py") in command + and str(directory) in command): + result.append(proc) + except psutil.Error: + pass + return result + + +def stop(directory): + procs = managed_processes(directory) + # Supervisor owns and terminates only its own child handles in its finally block. + (directory / "stop.flag").touch() + _, alive = psutil.wait_procs(procs, timeout=8) + for proc in alive: + proc.terminate() + return {"message": "已停止本安装包启动的连接进程。手工启动的旧桥接请在原窗口按Ctrl+C停止。"} + + +def start(directory, root): + from .process_lock import DeviceProcessLock + with DeviceProcessLock(directory / "control"): + return _start(directory, root) + + +def _start(directory, root): + cfg = json.loads((directory / "device.json").read_text()) + if not cfg.get("owner") or not (directory / "device.pem").exists(): + raise RuntimeError("请先完成设备准入和Work绑定。") + if managed_processes(directory): + return {"message": "连接进程已运行,请点击状态查看。"} + (directory / "stop.flag").unlink(missing_ok=True) + log = (directory / "bridge.log").open("a", encoding="utf-8") + proc = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "supervise", "--directory", str(directory), "--root", str(root)], + stdout=log, stderr=log, creationflags=subprocess.CREATE_NO_WINDOW) + log.close() + save(directory / "processes.json", {"processes": [{"pid": proc.pid, "created": psutil.Process(proc.pid).create_time()}]}) + return {"message": "连接进程已启动;约30秒后点击状态,再到Work发送 /主机名 验证。"} + + +def supervise(directory, root): + from .process_lock import DeviceProcessLock + lockdir = directory / "supervisor" + with DeviceProcessLock(lockdir): + children = [] + def record_children(): + processes = [psutil.Process(os.getpid())] + [psutil.Process(p.pid) for p in children if p.poll() is None] + save(directory / "processes.json", {"processes": [{"pid": p.pid, "created": p.create_time()} for p in processes]}) + try: + # Check local bind availability without an HTTP connection. Windows + # filtering can time out connections to a closed port rather than + # refuse them; that must not prevent starting our own listener. + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as port_probe: + try: + if os.name == "nt": + port_probe.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1) + port_probe.bind(("127.0.0.1", 18643)) + except OSError: + raise RuntimeError("原生API端口18643被占用或不允许绑定,请先检查旧连接。") from None + native = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "native", "--directory", str(directory), "--root", str(root)]) + children.append(native) + record_children() + for _ in range(90): + if (directory / "stop.flag").exists(): + return + if native.poll() is not None: + raise RuntimeError("本机原生API启动失败,请检查安装配置。") + try: + from .hainuo1065 import Hainuo1065Agent + async def probe(): + agent = Hainuo1065Agent(directory / "hainuo-native.json") + try: + return await agent.probe() + finally: + await agent.close() + asyncio.run(probe()) + break + except Exception: + time.sleep(1) + else: + raise RuntimeError("本机原生API未就绪。") + bridge = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "bridge", "--directory", str(directory), "--root", str(root)]) + children.append(bridge) + record_children() + while not (directory / "stop.flag").exists(): + if any(proc.poll() is not None for proc in children): + raise RuntimeError("连接子进程已退出,请查看状态后重新启动。") + time.sleep(0.5) + finally: + for proc in reversed(children): + if proc.poll() is None: + proc.terminate() + try: + proc.wait(timeout=5) + except subprocess.TimeoutExpired: + proc.kill() + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("action", choices=["authorize", "import", "enroll", "bind", "start", "stop", "status", "pair", "supervise", "native", "bridge"]) + parser.add_argument("--directory", type=Path, required=True) + parser.add_argument("--root", type=Path, required=True) + args = parser.parse_args() + directory, root = args.directory.resolve(), args.root.resolve() + directory.mkdir(parents=True, exist_ok=True) + try: + if args.action == "authorize": + from .work_client import WorkClient + client = WorkClient(directory) + try: + result = client.status() + result["message"] += ("\n" + result["authorization_url"]) if result.get("authorization_url") else "" + finally: + client.close() + elif args.action == "import": + result = import_existing(directory, Path(sys.stdin.readline().strip())) + elif args.action == "enroll": + result = enrollment(directory) + elif args.action == "bind": + from .device import bind_device + code = sys.stdin.readline().strip() + if not code: + raise ValueError("请填写Work机器人新生成的绑定码。") + enrollment(directory) + stop(directory) + asyncio.run(bind_device(directory, URL, code, tls_verify=tls(directory))) + result = start(directory, root) + result["message"] = "Work绑定成功。" + result["message"] + elif args.action == "start": + result = start(directory, root) + elif args.action == "stop": + result = stop(directory) + elif args.action == "pair": + from .device import pair_browser + receipt = asyncio.run(pair_browser(directory, URL, tls_verify=tls(directory))) + result = {"message": "网页:" + receipt["url"] + "\n一次性配对码(120秒):" + receipt["code"]} + elif args.action == "status": + cfg = json.loads((directory / "device.json").read_text()) if (directory / "device.json").exists() else {} + log = directory / "bridge.log" + tail = log.read_bytes()[-6000:].decode("utf-8", errors="replace") if log.exists() else "" + result = {"message": "设备:" + cfg.get("host_id", "未申请") + "\nWork身份:" + (cfg.get("owner") or "未绑定") + + "\n连接进程:" + ("运行中(请在Work发送 /主机名 确认当前链路)" if managed_processes(directory) else "未运行") + "\n" + tail[-2500:]} + elif args.action == "supervise": + return supervise(directory, root) + elif args.action == "native": + os.environ["HN_WORK_DEVICE_DIR"] = str(directory) + import importlib.util + spec = importlib.util.spec_from_file_location("native_companion", BASE / "hainuo_native_api.py") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + module.enable_profile_tools(root, "hainuo") + key = module.prepare_environment(root, "hainuo", 18643, directory / "hainuo-native.json", enable_tools=True) + import logging + logging.disable(logging.CRITICAL) + return asyncio.run(module.serve_native(18643, key, enable_tools=True)) + else: + from .device import run_device + return asyncio.run(run_device(directory, "wss://10.77.76.13/device/ws", ssl_context=tls(directory), hainuo_config=directory / "hainuo-native.json")) + print(json.dumps(result, ensure_ascii=False)) + except KeyboardInterrupt: + pass + except Exception as error: + # Native exception bodies may include URLs or secrets; emit bounded known messages only. + message = str(error) if isinstance(error, (RuntimeError, ValueError)) else type(error).__name__ + ";请检查网络、证书、安装目录。" + print(json.dumps({"message": message[:600], "error": True}, ensure_ascii=False)) + raise SystemExit(1) from None + + +if __name__ == "__main__": + main() diff --git a/container-code/router/relay/common.py b/container-code/router/relay/common.py new file mode 100644 index 0000000..c112b99 --- /dev/null +++ b/container-code/router/relay/common.py @@ -0,0 +1,61 @@ +import hashlib +import hmac +import json +import time +from fastapi import HTTPException, Request +from starlette.responses import JSONResponse + + +def require_secret(actual, expected): + if not actual or not hmac.compare_digest(actual, expected): + raise HTTPException(401, "unauthorized") + + +async def rate_limit(redis, bucket: str, limit=60, seconds=60): + # Atomic counter + expiry: a crash cannot leave a permanent bucket. + key = "hn:rate:" + hashlib.sha256(bucket.encode()).hexdigest() + ":" + str(int(time.time()) // seconds) + pipe = redis.pipeline(transaction=True) + pipe.incr(key) + pipe.expire(key, seconds * 2) + result = (await pipe.execute())[0] + if int(result) > limit: + raise HTTPException(429, "rate limited", headers={"Retry-After": str(seconds)}) + + +class BodyLimitMiddleware: + def __init__(self, app, max_body=262144): + self.app, self.max_body = app, max_body + + async def __call__(self, scope, receive, send): + if scope["type"] != "http": + return await self.app(scope, receive, send) + messages, size = [], 0 + while True: + message = await receive() + if message["type"] == "http.disconnect": + return + size += len(message.get("body", b"")) + if size > self.max_body: + return await JSONResponse({"detail": "body too large"}, status_code=413)(scope, receive, send) + messages.append(message) + if not message.get("more_body", False): + break + async def buffered(): + return messages.pop(0) if messages else await receive() + await self.app(scope, buffered, send) + + +def no_cache(response): + response.headers["Cache-Control"] = "no-store" + response.headers["X-Content-Type-Options"] = "nosniff" + response.headers["Referrer-Policy"] = "no-referrer" + return response + + +async def json_pub(redis, channel, data): + return await redis.publish(channel, json.dumps(data, separators=(",", ":"))) + + +def peer(request: Request): + # Do not trust arbitrary X-Forwarded-For; configure uvicorn trusted proxies explicitly. + return request.client.host if request.client else "unknown" diff --git a/container-code/router/relay/config.py b/container-code/router/relay/config.py new file mode 100644 index 0000000..c4c32ac --- /dev/null +++ b/container-code/router/relay/config.py @@ -0,0 +1,66 @@ +from pathlib import Path +from pydantic import model_validator +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + model_config = SettingsConfigDict(env_prefix="HN_", extra="ignore", hide_input_in_errors=True) + environment: str = "production" + role: str = "connector" + redis_url: str = "redis://127.0.0.1:6379/0" + data_dir: Path = Path("runtime/connector") + database_url: str = "" + admin_secret: str = "" + internal_secret: str = "" + ca_cert: Path = Path("runtime/pki/ca.pem") + connector_key: Path = Path("runtime/pki/connector.key") + connector_public_key: Path = Path("runtime/pki/connector.pub") + public_url: str = "https://relay.example.invalid" + router_url: str = "wss://relay.example.invalid/device/ws" + device_domain: str = "" + feishu_api_base: str = "https://open.feishu.cn" + feishu_authorize_url: str = "https://open.feishu.cn/open-apis/authen/v1/authorize" + feishu_oauth_scopes: str = "" + feishu_app_id: str = "" + feishu_app_secret: str = "" + feishu_encrypt_key: str = "" + feishu_verification_token: str = "" + feishu_enabled: bool = False + oauth_enabled: bool = True + feishu_event_source: str = "longconn" + proxy_enabled: bool = False + # A fixed allowlist, never a user supplied upstream URL. + proxy_paths: str = "/open-apis/drive/v1/files,/open-apis/docx/v1/documents" + request_timeout: int = 45 + max_body: int = 262144 + max_pending: int = 1000 + max_devices: int = 1000 + max_connections: int = 1000 + max_result: int = 16000 + allow_insecure_local: bool = False + enrollment_signer_url: str = "" + + @model_validator(mode="after") + def validate_runtime(self): + if self.environment not in {"production", "test", "development"}: + raise ValueError("invalid environment") + if self.role == "connector" and min(len(self.admin_secret), len(self.internal_secret)) < 32: + raise ValueError("admin/internal secrets must each be at least 32 characters") + if self.role == "longconn" and len(self.internal_secret) < 32: + raise ValueError("internal credential must be at least 32 characters") + if self.role == "connector" and self.admin_secret == self.internal_secret: + raise ValueError("admin and internal credentials must be different") + if self.environment == "production": + if self.allow_insecure_local or not self.public_url.startswith("https://"): + raise ValueError("production requires HTTPS") + if not self.router_url.startswith("wss://"): + raise ValueError("production requires WSS") + if not self.feishu_api_base.startswith("https://"): + raise ValueError("platform API requires HTTPS") + if self.feishu_event_source not in {"longconn", "webhook"}: + raise ValueError("invalid platform event source") + if self.feishu_enabled and not all((self.feishu_app_id, self.feishu_app_secret)): + raise ValueError("platform app ID and secret are required") + if self.feishu_enabled and self.feishu_event_source == "webhook" and not all((self.feishu_encrypt_key, self.feishu_verification_token)): + raise ValueError("webhook requires encryption key and verification token") + return self diff --git a/container-code/router/relay/connector.py b/container-code/router/relay/connector.py new file mode 100644 index 0000000..c01f3b6 --- /dev/null +++ b/container-code/router/relay/connector.py @@ -0,0 +1,506 @@ +import asyncio +import base64 +import contextlib +import hashlib +import hmac +import json +import re +import secrets +import sqlite3 +import time +import uuid +from contextlib import asynccontextmanager +from dataclasses import dataclass +from urllib.parse import unquote, urlsplit + +from cryptography.hazmat.primitives import hashes, padding +from cryptography.hazmat.primitives.asymmetric import x25519 +from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes +from fastapi import FastAPI, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from fastapi.exceptions import RequestValidationError +from pydantic import BaseModel, Field +from redis.asyncio import Redis + +from .common import BodyLimitMiddleware, no_cache, peer, rate_limit, require_secret +from .config import Settings +from .crypto import (fingerprint, load_private, open_envelope, raw_public, seal, + sign, verified, verify_device) +from .oauth import OAuth +from .platform import Feishu, PlatformError +from .store import Store, INTEGRITY_ERRORS + + +class Register(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + cert: str = Field(max_length=10000) + + +class Bind(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Instruction(BaseModel): + open_id: str = Field(min_length=1, max_length=128) + text: str = Field(min_length=1, max_length=8000) + # Stable identity chosen by upstream, not generated again on retries. + turn_id: str = Field(default_factory=lambda: uuid.uuid4().hex, pattern=r"^[A-Za-z0-9_-]{1,128}$") + + +@dataclass +class Pending: + future: asyncio.Future + private: object + public: object + host: str + session: str + + +class Broker: + def __init__(self, settings, redis, store): + self.s, self.r, self.store = settings, redis, store + self.signer = load_private(settings.connector_key) + self.pending: dict[str, Pending] = {} + + async def replies(self, pub): + async for message in pub.listen(): + if message["type"] != "message": + continue + try: + data = json.loads(message["data"]) + item = self.pending.get(data["turn_id"]) + if not item or item.future.done(): + continue + plain = open_envelope(data["envelope"], item.private, item.public, + host_id=item.host, session_id=item.session) + if plain["turn_id"] != data["turn_id"] or plain.get("type") != "result": + continue + result = plain.get("result", "") + if not isinstance(result, str) or len(result) > self.s.max_result: + continue + item.future.set_result({"status": "completed", "turn_id": data["turn_id"], "result": result}) + except Exception: + # Invalid packets never consume a pending result; do not log packet contents. + continue + + async def dispatch(self, owner, text, turn): + device = self.store.by_owner(owner) + if not device: + return {"status": "unavailable", "turn_id": turn, "result": "未绑定设备或设备不可用。"} + host = device["host_id"] + await rate_limit(self.r, "dispatch:" + owner, 30) + if len(self.pending) >= self.s.max_pending: + raise HTTPException(503, "too many in-flight requests") + presence = await self.r.get("hn:online:" + host) + if not presence or await self.r.exists("hn:revoked:" + host): + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线,请开机并连接网络。"} + try: + presence = json.loads(presence) + if fingerprint(presence["cert"]) != device["fingerprint"]: + raise ValueError("certificate changed") + public = verify_device(presence["cert"], self.s.ca_cert.read_bytes(), host) + proof = verified(presence["proof"], public) + if proof["host_id"] != host or proof["challenge"] != presence["challenge"]: + raise ValueError("invalid device session") + session = proof["session_id"] + except Exception: + self.store.audit(owner, "dispatch", host, "identity_rejected") + raise HTTPException(409, "device identity rejected") from None + if not self.store.begin_turn(turn, owner, host): + return {"status": "duplicate", "turn_id": turn, "result": "该请求已接收,不会重复执行。"} + private = x25519.X25519PrivateKey.generate() + future = asyncio.get_running_loop().create_future() + self.pending[turn] = Pending(future, private, public, host, session) + envelope = seal({"type": "instruction", "turn_id": turn, "owner": owner, + "text": text, "read_only": True, "channel": "im", + "reply_key": raw_public(private.public_key())}, proof["kx"], self.signer, + host_id=host, session_id=session) + key = f"hn:inflight:{host}:{turn}" + try: + await self.r.set(key, session, ex=self.s.request_timeout + 5) + count = await self.r.publish("hn:down:" + host, json.dumps({"type": "instruction", "envelope": envelope})) + if not count: + self.store.finish(turn, "offline") + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线。"} + result = await asyncio.wait_for(future, timeout=self.s.request_timeout) + self.store.finish(turn, "completed") + self.store.audit(owner, "instruction", host, "completed") + return result + except asyncio.TimeoutError: + self.store.finish(turn, "unknown") + return {"status": "unknown", "turn_id": turn, + "result": "等待结果超时,执行状态未知;不要直接重发有副作用的任务。"} + finally: + self.pending.pop(turn, None) + await self.r.delete(key) + + +def create_app(settings=None, redis=None, platform=None): + s = settings or Settings() + r = redis or Redis.from_url(s.redis_url, decode_responses=True) + store = Store(s.data_dir / "metadata.sqlite", s.database_url) + f = platform or Feishu(s) + broker = Broker(s, r, store) + oauth = OAuth(s, r, f) + oauth.store = store + background = set() + + def spawn(coro): + task = asyncio.create_task(coro) + background.add(task) + task.add_done_callback(background.discard) + # Retrieve errors without displaying possibly sensitive exception text. + task.add_done_callback(lambda t: t.exception() if not t.cancelled() else None) + + @asynccontextmanager + async def lifespan(app): + await r.ping() + for row in store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall(): + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + pub = r.pubsub() + await pub.subscribe("hn:replies") + reply_task = asyncio.create_task(broker.replies(pub)) + app.state.reply_task = reply_task + async def sweep(): + while True: + await asyncio.sleep(30) + async with oauth.lock: + oauth.prune() + sweep_task = asyncio.create_task(sweep()) + try: + yield + finally: + for task in [reply_task, sweep_task, *background]: + task.cancel() + await asyncio.gather(reply_task, sweep_task, *background, return_exceptions=True) + await pub.aclose() + await f.close() + store.close() + if redis is None: + await r.aclose() + + app = FastAPI(title="Hainuo B1 connector", lifespan=lifespan, docs_url=None, redoc_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=s.max_body) + app.include_router(oauth.router) + from .web_session import WebSessions + web_sessions = WebSessions(s, r, store, broker, f) + app.include_router(web_sessions.router) + app.state.store, app.state.broker, app.state.oauth = store, broker, oauth + app.state.platform, app.state.redis = f, r + + @app.exception_handler(RequestValidationError) + async def validation_error(request, exc): + # FastAPI's default error response includes invalid input values (possibly tokens). + return JSONResponse({"detail": "invalid request parameters"}, status_code=422) + + @app.middleware("http") + async def secure_response(request, call_next): + return no_cache(await call_next(request)) + + def admin(request): + require_secret(request.headers.get("X-Admin-Secret"), s.admin_secret) + + from .enrollment import enrollment_router + app.include_router(enrollment_router(s, r, store, broker.signer, admin)) + from .work_api import router as work_router + app.include_router(work_router(s, r, store, f)) + + @app.get("/health") + async def health(): + return {"status": "alive", "role": "connector", "version": "0.1.0"} + + @app.get("/ready") + async def ready(): + await r.ping() + store.db.execute("SELECT 1") + if not getattr(app.state, "reply_task", None) or app.state.reply_task.done(): + raise HTTPException(503, "reply subscriber unavailable") + return {"status": "ready"} + + @app.post("/admin/devices", status_code=201) + async def register(body: Register, request: Request): + admin(request) + try: + verify_device(body.cert, s.ca_cert.read_bytes(), body.host_id) + except Exception: + raise HTTPException(400, "invalid device certificate") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= s.max_devices: + raise HTTPException(503, "device capacity reached") + try: + fp = fingerprint(body.cert) + store.add_device(body.host_id, body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "device already exists") from None + await r.set("hn:device:" + body.host_id, fp) + store.audit("admin", "register", body.host_id, "success") + return {"host_id": body.host_id, "fingerprint": fp} + + @app.get("/admin/devices") + async def devices(request: Request): + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,owner,revoked FROM devices ORDER BY host_id").fetchall() + return {"devices": [{**dict(row), "online": bool(await r.exists("hn:online:" + row["host_id"]))} for row in rows]} + + @app.post("/admin/devices/{host}/revoke") + async def revoke(host: str, request: Request): + admin(request) + if not store.device(host): + raise HTTPException(404, "device not found") + # Persistent source of truth first; router block + disconnect follows. + store.revoke(host) + await r.incr("hn:web-generation:" + host) + await r.set("hn:revoked:" + host, "1") + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "revoke") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "revoked", "result": "设备已停用,结果已丢弃。"}) + store.audit("admin", "revoke", host, "success") + return {"status": "revoked"} + + @app.post("/admin/reconcile") + async def reconcile(request: Request): + """Rebuild router admission metadata after Redis recovery; no dialogue data.""" + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall() + for row in rows: + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + return {"devices": len(rows)} + + @app.post("/admin/devices/{host}/unbind") + async def admin_unbind(host: str, request: Request): + admin(request) + device = store.device(host) + if not device: + raise HTTPException(404, "device not found") + if device["owner"]: + store.unbind(device["owner"]) + await r.incr("hn:web-generation:" + host) + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "unbind") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + store.audit("admin", "unbind", host, "success") + return {"status": "unbound", "host_id": host, "revoked": bool(device["revoked"])} + + async def make_binding_code(owner): + if store.by_owner(owner): + return "你已绑定电脑,请先发送 /解绑。" + await rate_limit(r, "binding:" + owner, 5) + code = secrets.token_urlsafe(12) + await r.set("hn:bind:" + hashlib.sha256(code.encode()).hexdigest(), owner, ex=300) + return "请在本人电脑的桥接客户端输入绑定码(5分钟有效):" + code + + @app.post("/device/bind") + async def bind(body: Bind, request: Request): + await rate_limit(r, "bind:" + peer(request), 20) + device = store.device(body.host_id) + if not device or device["revoked"]: + raise HTTPException(404, "device unavailable") + try: + public = verify_device(device["cert"], s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, public) + if proof["host_id"] != body.host_id or abs(time.time() - proof["ts"]) > 90: + raise ValueError("invalid proof") + code = proof["code"] + if not isinstance(code, str) or len(code) > 128: + raise ValueError("invalid code") + except Exception: + raise HTTPException(403, "invalid device proof") from None + key = "hn:bind:" + hashlib.sha256(code.encode()).hexdigest() + owner = await r.get(key) + if not owner: + raise HTTPException(404, "binding code expired or consumed") + # GETDEL is atomic across connectors. Invalid device proofs never reach this point. + owner = await r.getdel(key) + if not owner: + raise HTTPException(404, "binding code consumed") + try: + store.bind(body.host_id, owner) + except (*INTEGRITY_ERRORS, ValueError): + raise HTTPException(409, "owner/device already bound; request new code") from None + await r.incr("hn:web-generation:" + body.host_id) + store.audit(owner, "bind", body.host_id, "success") + return sign({"host_id": body.host_id, "owner": owner, "ts": int(time.time())}, broker.signer) + + @app.post("/admin/test/binding-code") + async def test_code(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return {"message": await make_binding_code(body.open_id)} + + @app.post("/admin/test/dispatch") + async def test_dispatch(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return await broker.dispatch(body.open_id, body.text, body.turn_id) + + async def handle_event(data): + header, event = data.get("header", {}), data.get("event", {}) + if header.get("event_type") != "im.message.receive_v1": + return + message, sender = event.get("message", {}), event.get("sender", {}) + owner = sender.get("sender_id", {}).get("open_id") + message_id = message.get("message_id", "") + if (message.get("chat_type") != "p2p" or message.get("message_type") != "text" + or sender.get("sender_type") != "user" or not owner + or not re.fullmatch(r"[A-Za-z0-9_-]{1,128}", message_id)): + return + if header.get("app_id") != s.feishu_app_id: + return + try: + text = json.loads(message["content"])["text"].strip() + if not text or len(text) > 8000: + return + # Persistent receipt BEFORE any side effect. Restarts do not execute a delivery twice. + event_turn = "event_" + hashlib.sha256(message_id.encode()).hexdigest() + if not store.begin_turn(event_turn, owner, "event"): + return + if text == "/绑定": + result = await make_binding_code(owner) + elif text == "/解绑": + old = store.by_owner(owner) + store.unbind(owner) + if old: + await r.incr("hn:web-generation:" + old["host_id"]) + await r.publish("hn:kill:" + old["host_id"], "unbind") + for item in broker.pending.values(): + if item.host == old["host_id"] and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + result = "已解绑;重新绑定后请更新本机绑定信息。" + elif text == "/主机名" or text.startswith("/回显 "): + result = (await broker.dispatch(owner, text, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + else: + instruction = text if text.startswith("/助手 ") else "/助手 " + text + result = (await broker.dispatch(owner, instruction, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + await f.reply(message_id, result[:s.max_result]) + store.finish(event_turn, "replied") + except Exception: + store.audit(owner, "event", message_id, "failed_or_unknown") + + @app.post("/internal/events") + async def internal_event(request: Request): + require_secret(request.headers.get("X-Internal-Secret"), s.internal_secret) + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(await request.json())) + return {"accepted": True} + + @app.post("/feishu/event") + async def webhook(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if not s.feishu_encrypt_key or not s.feishu_verification_token: + raise HTTPException(503, "webhook credentials not configured; use selected event source") + await rate_limit(r, "webhook:" + peer(request), 600) + raw = await request.body() + timestamp, nonce, signature = (request.headers.get(x, "") for x in + ("X-Lark-Request-Timestamp", "X-Lark-Request-Nonce", "X-Lark-Signature")) + try: + if abs(time.time() - int(timestamp)) > 300 or not nonce or len(nonce) > 128: + raise ValueError("stale request") + expected = hashlib.sha256((timestamp + nonce + s.feishu_encrypt_key).encode() + raw).hexdigest() + if not hmac.compare_digest(expected, signature): + raise ValueError("bad signature") + data = json.loads(raw) + if "encrypt" in data: + cipher = base64.b64decode(data["encrypt"], validate=True) + decryptor = Cipher(algorithms.AES(hashlib.sha256(s.feishu_encrypt_key.encode()).digest()), + modes.CBC(cipher[:16])).decryptor() + padded = decryptor.update(cipher[16:]) + decryptor.finalize() + unpadder = padding.PKCS7(128).unpadder() + data = json.loads(unpadder.update(padded) + unpadder.finalize()) + token = data.get("token", data.get("header", {}).get("token", "")) + if not hmac.compare_digest(token, s.feishu_verification_token): + raise ValueError("invalid verification token") + except Exception: + raise HTTPException(403, "invalid event authentication") from None + if data.get("type") == "url_verification": + return {"challenge": data.get("challenge", "")} + # Same delivery is ACKed again; durable message_id receipt handles execution deduplication. + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(data)) + return {"code": 0} + + @app.get("/api/whoami") + async def whoami(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(r, "whoami:" + peer(request)) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await f.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "no device bound") + if not s.device_domain or not re.fullmatch(r"[a-zA-Z0-9.-]+", s.device_domain): + raise HTTPException(503, "device web domain not configured") + return {"host_id": device["host_id"], "url": f"https://h-{device['host_id']}.{s.device_domain}/", + "online": bool(await r.exists("hn:online:" + device["host_id"])), + "device_login_required": True} + + @app.get("/h5", response_class=HTMLResponse) + async def h5(): + from importlib.resources import files + return HTMLResponse(files("relay").joinpath("web/index.html").read_text(encoding="utf-8"), + headers={"Content-Security-Policy": "default-src 'self'; script-src 'self'; style-src 'self'; frame-ancestors 'self'; base-uri 'none'; form-action 'self'"}) + + @app.get("/assets/{name}") + async def asset(name: str): + from importlib.resources import files + from starlette.responses import Response + if name not in {"app.js", "style.css"}: + raise HTTPException(404, "not found") + return Response(files("relay").joinpath("web/" + name).read_bytes(), + media_type="text/javascript" if name.endswith(".js") else "text/css") + + @app.get("/proxy") + async def proxy(request: Request): + if not s.proxy_enabled or not s.feishu_enabled: + raise HTTPException(404, "not found") + auth = request.headers.get("Authorization", "") + target = request.headers.get("X-Relay-Target", "") + decoded = unquote(target) + parts = urlsplit(target) + allowed = s.proxy_paths.split(",") + if (not auth.startswith("Bearer ") or len(auth) > 8192 or len(target) > 4096 + or parts.scheme or parts.netloc or parts.fragment or target.startswith("//") + or any(x in decoded for x in ("..", "\\", "\r", "\n", "#", "%")) + or not any(parts.path == x or parts.path.startswith(x + "/") for x in allowed)): + raise HTTPException(403, "target not allowed") + try: + info = await f.user_info(auth[7:]) + await rate_limit(r, "proxy:" + info["open_id"], 60) + # Buffer bounded response; no disk/cache, redirects never followed. + async with f.http.stream("GET", target, headers={"Authorization": auth}) as response: + if response.status_code in {301, 302, 303, 307, 308}: + raise HTTPException(502, "upstream redirect refused") + chunks, size = [], 0 + async for chunk in response.aiter_bytes(): + size += len(chunk) + if size > 2 * 1024 * 1024: + raise HTTPException(502, "response too large") + chunks.append(chunk) + from starlette.responses import Response + store.audit(info["open_id"], "proxy", parts.path, str(response.status_code)) + return Response(b"".join(chunks), status_code=response.status_code, + media_type=response.headers.get("content-type", "application/json")) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + + return app diff --git a/container-code/router/relay/crypto.py b/container-code/router/relay/crypto.py new file mode 100644 index 0000000..dd962e5 --- /dev/null +++ b/container-code/router/relay/crypto.py @@ -0,0 +1,140 @@ +"""Signed ephemeral X25519 envelopes. The router never receives plaintext keys.""" +import base64 +import hashlib +import json +import secrets +import time +from datetime import datetime, timedelta, timezone + +from cryptography import x509 +from cryptography.hazmat.primitives import hashes, serialization +from cryptography.hazmat.primitives.asymmetric import ed25519, x25519 +from cryptography.hazmat.primitives.ciphers.aead import AESGCM +from cryptography.hazmat.primitives.kdf.hkdf import HKDF +from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID + + +def b64(data: bytes) -> str: + return base64.urlsafe_b64encode(data).decode() + + +def unb64(data: str) -> bytes: + return base64.b64decode(data.encode(), altchars=b"-_", validate=True) + + +def canonical(data: dict) -> bytes: + return json.dumps(data, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode() + + +def raw_public(key) -> str: + return b64(key.public_bytes(serialization.Encoding.Raw, serialization.PublicFormat.Raw)) + + +def pem_private(key) -> bytes: + return key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, + serialization.NoEncryption()) + + +def load_private(path): + protected = path.with_name(path.name + ".dpapi") + if protected.is_file(): + from .windows_keys import unprotect + data = unprotect(protected.read_bytes()) + else: + data = path.read_bytes() + return serialization.load_pem_private_key(data, password=None) + + +def load_public(path): + return serialization.load_pem_public_key(path.read_bytes()) + + +def fingerprint(pem: str) -> str: + return x509.load_pem_x509_certificate(pem.encode()).fingerprint(hashes.SHA256()).hex() + + +def verify_device(pem: str, ca_pem: bytes, host_id: str): + """v1 deliberately accepts only directly issued Ed25519 device certificates.""" + cert = x509.load_pem_x509_certificate(pem.encode()) + ca = x509.load_pem_x509_certificate(ca_pem) + now = datetime.now(timezone.utc) + if not (ca.not_valid_before_utc <= now <= ca.not_valid_after_utc): + raise ValueError("expired CA") + if not ca.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("not a CA") + cert.verify_directly_issued_by(ca) + if not (cert.not_valid_before_utc <= now <= cert.not_valid_after_utc): + raise ValueError("expired certificate") + if cert.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("CA cannot be used as device") + if ExtendedKeyUsageOID.CLIENT_AUTH not in cert.extensions.get_extension_for_class(x509.ExtendedKeyUsage).value: + raise ValueError("clientAuth missing") + names = cert.extensions.get_extension_for_class(x509.SubjectAlternativeName).value + if names.get_values_for_type(x509.UniformResourceIdentifier) != [f"urn:hainuo:host:{host_id}"]: + raise ValueError("host identity mismatch") + public = cert.public_key() + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("v1 requires Ed25519 device key") + return public + + +def sign(data: dict, key) -> dict: + return {**data, "signature": b64(key.sign(canonical(data)))} + + +def verified(data: dict, key) -> dict: + body = {k: v for k, v in data.items() if k != "signature"} + key.verify(unb64(data["signature"]), canonical(body)) + return body + + +def seal(payload: dict, recipient_public: str, signer, *, host_id: str, session_id: str) -> dict: + ephemeral = x25519.X25519PrivateKey.generate() + header = {"v": 1, "host_id": host_id, "session_id": session_id, + "ts": int(time.time()), "nonce": b64(secrets.token_bytes(12)), + "ephemeral": raw_public(ephemeral.public_key())} + shared = ephemeral.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(recipient_public))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + cipher = AESGCM(key).encrypt(unb64(header["nonce"]), canonical(payload), canonical(header)) + return sign({**header, "ciphertext": b64(cipher)}, signer) + + +def open_envelope(envelope: dict, recipient_private, sender_public, *, host_id: str, session_id: str) -> dict: + body = verified(envelope, sender_public) + if body["v"] != 1 or body["host_id"] != host_id or body["session_id"] != session_id: + raise ValueError("wrong destination/session") + if abs(time.time() - body["ts"]) > 90: + raise ValueError("expired envelope") + header = {k: v for k, v in body.items() if k != "ciphertext"} + shared = recipient_private.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(body["ephemeral"]))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + plain = AESGCM(key).decrypt(unb64(body["nonce"]), unb64(body["ciphertext"]), canonical(header)) + return json.loads(plain) + + +def issue_ca(): + key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Hainuo device test CA")]) + now = datetime.now(timezone.utc) + cert = (x509.CertificateBuilder().subject_name(name).issuer_name(name) + .public_key(key.public_key()).serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=3650)) + .add_extension(x509.BasicConstraints(ca=True, path_length=0), critical=True) + .add_extension(x509.KeyUsage(False, False, False, False, False, True, True, False, False), critical=True) + .sign(key, algorithm=None)) + return key, cert + + +def issue_device(ca_key, ca_cert, host_id: str, public_key, days=30): + now = datetime.now(timezone.utc) + return (x509.CertificateBuilder() + .subject_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, host_id)])) + .issuer_name(ca_cert.subject).public_key(public_key) + .serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=days)) + .add_extension(x509.BasicConstraints(ca=False, path_length=None), critical=True) + .add_extension(x509.SubjectAlternativeName([x509.UniformResourceIdentifier(f"urn:hainuo:host:{host_id}")]), critical=False) + .add_extension(x509.ExtendedKeyUsage([ExtendedKeyUsageOID.CLIENT_AUTH]), critical=False) + .sign(ca_key, algorithm=None)) diff --git a/container-code/router/relay/device.py b/container-code/router/relay/device.py new file mode 100644 index 0000000..a8fe010 --- /dev/null +++ b/container-code/router/relay/device.py @@ -0,0 +1,216 @@ +"""Reference PC bridge. No shell/PowerShell execution; local read-only diagnostics.""" +import asyncio +import contextlib +import json +import os +import secrets +import socket +import sqlite3 +import time +from pathlib import Path +from urllib.parse import urlsplit + +import httpx +from cryptography.hazmat.primitives.asymmetric import x25519 +from websockets.asyncio.client import connect + +from .crypto import (load_private, load_public, open_envelope, raw_public, seal, sign, + verified, verify_device) + + +class DeviceJournal: + def __init__(self, path): + path.parent.mkdir(parents=True, exist_ok=True) + self.db = sqlite3.connect(path) + self.db.execute("CREATE TABLE IF NOT EXISTS consumed(turn_id TEXT PRIMARY KEY, ts REAL NOT NULL)") + self.db.commit() + + def consume(self, turn): + with self.db: + return self.db.execute("INSERT OR IGNORE INTO consumed VALUES(?,?)", (turn, time.time())).rowcount == 1 + + def close(self): + self.db.close() + + +async def execute_readonly(text, root: Path | None): + if text in {"/主机名", "hostname"}: + return socket.gethostname() + if text.startswith("/回显 "): + return text[4:][:16000] + if text.startswith("/读取 "): + if root is None: + return "未配置测试文件目录。" + base = root.resolve(strict=True) + try: + path = (base / text[4:].strip()).resolve(strict=True) + if path == base or not path.is_relative_to(base) or not path.is_file(): + return "拒绝:文件不在允许的测试目录。" + if path.stat().st_size > 32768: + return "拒绝:测试文件超过32KB。" + return path.read_text(encoding="utf-8")[:16000] + except (OSError, UnicodeError, ValueError): + return "拒绝:文件不存在、不可读或不是UTF-8文本。" + return "当前桥接客户端仅开放只读诊断:/主机名、/回显 内容、/读取 相对路径;尚未接入海诺本地Agent接口。" + + +class LocalAgent: + """Explicit adapter contract, not a claim of compatibility with installer 1.065.""" + def __init__(self, url, transport=None): + parts = urlsplit(url) + if parts.scheme != "http" or parts.hostname not in {"127.0.0.1", "::1"} or parts.username or parts.password or parts.fragment: + raise ValueError("local Agent must use a fixed loopback HTTP endpoint") + self.url = url + self.http = httpx.AsyncClient(timeout=30, follow_redirects=False, transport=transport, trust_env=False) + + async def execute(self, text, owner, turn): + headers = {} + if os.environ.get("HN_LOCAL_AGENT_TOKEN"): + headers["Authorization"] = "Bearer " + os.environ["HN_LOCAL_AGENT_TOKEN"] + try: + async with self.http.stream("POST", self.url, headers=headers, + json={"text": text, "open_id": owner, "turn_id": turn, + "channel": "im", "read_only": True}) as response: + if response.status_code != 200: + return "本机Agent未返回成功结果,请查看本机服务状态。" + body = b"" + async for chunk in response.aiter_bytes(): + body += chunk + if len(body) > 65536: + return "本机Agent结果超过限制。" + result = json.loads(body)["result"] + if not isinstance(result, str): + return "本机Agent响应格式不匹配。" + return result[:16000] + except (httpx.HTTPError, ValueError, KeyError): + return "本机Agent连接失败或响应格式不匹配。" + + async def close(self): + await self.http.aclose() + + +async def bind_device(directory: Path, connector_url: str, code: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + private = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "code": code, "ts": int(time.time())}, private) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as http: + response = await http.post(connector_url.rstrip("/") + "/device/bind", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"binding failed: HTTP {response.status_code}") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt["host_id"] != config["host_id"]: + raise ValueError("wrong binding receipt") + config["owner"] = receipt["owner"] + temp = directory / "device.json.tmp" + temp.write_text(json.dumps(config, ensure_ascii=False, indent=2), encoding="utf-8") + temp.replace(directory / "device.json") + + +async def pair_browser(directory: Path, connector_url: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8-sig")) + if not config.get("owner"): + raise RuntimeError("device must be bound first") + key = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "owner": config["owner"], + "purpose": "h5-pair-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as client: + response = await client.post(connector_url.rstrip("/") + "/device/web-pair", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"browser pairing failed: HTTP {response.status_code}") + result = verified(response.json(), load_public(directory / "connector.pub")) + if result["host_id"] != config["host_id"]: + raise ValueError("wrong pairing receipt") + return result + + +async def run_device(directory: Path, router_url: str, **kwargs): + from .process_lock import DeviceProcessLock + with DeviceProcessLock(directory): + return await _run_device(directory, router_url, **kwargs) + + +async def _run_device(directory: Path, router_url: str, *, root=None, once=False, ssl_context=None, + stop_event=None, connected_event=None, agent_url=None, hainuo_config=None): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + if not config.get("owner"): + raise RuntimeError("bind device before starting") + host, owner = config["host_id"], config["owner"] + key = load_private(directory / "device.key") + connector = load_public(directory / "connector.pub") + cert = (directory / "device.pem").read_text(encoding="utf-8") + verify_device(cert, (directory / "ca.pem").read_bytes(), host) + journal = DeviceJournal(directory / "journal.sqlite") + if agent_url and hainuo_config: + raise ValueError("choose one Agent adapter") + if hainuo_config: + from .hainuo1065 import Hainuo1065Agent + agent = Hainuo1065Agent(hainuo_config) + else: + agent = LocalAgent(agent_url) if agent_url else None + stop_event = stop_event or asyncio.Event() + delay = 1 + try: + while not stop_event.is_set(): + try: + kwargs = {"ssl": ssl_context} if router_url.startswith("wss://") and ssl_context else {} + async with connect(router_url, max_size=262144, open_timeout=10, ping_interval=10, + proxy=None, **kwargs) as ws: + challenge = json.loads(await ws.recv())["challenge"] + ephemeral = x25519.X25519PrivateKey.generate() + session = secrets.token_hex(16) + proof = sign({"host_id": host, "session_id": session, "challenge": challenge, + "kx": raw_public(ephemeral.public_key()), "ts": int(time.time())}, key) + await ws.send(json.dumps({"host_id": host, "cert": cert, "proof": proof})) + accepted = json.loads(await ws.recv()) + if accepted.get("type") != "connected": + raise RuntimeError("device connection rejected") + print("设备连接成功,已绑定本人海能Work身份。", flush=True) + if connected_event: + connected_event.set() + delay = 1 + while not stop_event.is_set(): + try: + raw = await asyncio.wait_for(ws.recv(), timeout=1) + except asyncio.TimeoutError: + continue + data = json.loads(raw) + payload = open_envelope(data["envelope"], ephemeral, connector, + host_id=host, session_id=session) + if payload.get("owner") != owner or payload.get("type") != "instruction": + continue + turn = payload["turn_id"] + if not isinstance(turn, str) or len(turn) > 128: + continue + if not journal.consume(turn): + result = "该请求已处理,拒绝重复执行。" + elif payload.get("read_only") is not True or payload.get("channel") != "im": + result = "拒绝:通道或操作类型不受支持。" + else: + if agent and payload["text"].startswith("/助手 "): + result = await agent.execute(payload["text"][4:], owner, turn) + else: + result = await execute_readonly(payload["text"], root) + reply = seal({"type": "result", "turn_id": turn, "result": result}, + payload["reply_key"], key, host_id=host, session_id=session) + await ws.send(json.dumps({"type": "reply", "turn_id": turn, "envelope": reply})) + if once: + return + except asyncio.CancelledError: + raise + except Exception as error: + if once: + raise + from websockets.exceptions import ConnectionClosed + if isinstance(error, ConnectionClosed) and error.rcvd and error.rcvd.code == 1008: + print("设备被中继拒绝:请检查是否有另一桥接进程、设备准入或证书状态。", flush=True) + # No payloads/URLs/credentials in logs, bounded reconnect backoff + jitter. + print("设备连接中断,等待重连。", flush=True) + with contextlib.suppress(asyncio.TimeoutError): + await asyncio.wait_for(stop_event.wait(), timeout=delay + secrets.randbelow(1000) / 1000) + delay = min(delay * 2, 30) + finally: + journal.close() + if agent: + await agent.close() diff --git a/container-code/router/relay/enrollment.py b/container-code/router/relay/enrollment.py new file mode 100644 index 0000000..b4ac77f --- /dev/null +++ b/container-code/router/relay/enrollment.py @@ -0,0 +1,178 @@ +"""Device registration with key possession proof; Work binding is separate.""" +import json +import secrets +import time +import httpx + +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey +from fastapi import APIRouter, HTTPException, Request +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .crypto import verified, sign, raw_public, verify_device, fingerprint +from .web_session import digest +from fastapi.responses import JSONResponse + + +def validate_enrollment(body): + try: + key = serialization.load_pem_public_key(body.public_key.encode()) + if not isinstance(key, Ed25519PublicKey): + raise ValueError() + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["purpose"] != "device-enroll-v1" + or abs(time.time() - proof["ts"]) > 90 or len(proof["nonce"]) != 32): + raise ValueError() + return key + except Exception: + raise HTTPException(403, "invalid enrollment proof") from None + + +async def request_certificate(settings, body): + async with httpx.AsyncClient(trust_env=False, timeout=15, follow_redirects=False) as http: + response = await http.post(settings.enrollment_signer_url.rstrip("/") + "/issue", + headers={"X-Internal-Secret": settings.internal_secret}, + json=body.model_dump()) + response.raise_for_status() + return response.json()["cert"] + + +class EnrollmentRequest(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + public_key: str = Field(max_length=1024) + proof: dict + + +class EnrollmentApproval(BaseModel): + cert: str = Field(max_length=10000) + + +def enrollment_router(settings, redis, store, signer, admin): + router = APIRouter() + + @router.post("/device/register") + async def automatic_registration(body: EnrollmentRequest, request: Request): + # No human approval: eligibility follows intranet access plus Work app scope. + # Proof/TLS/limits protect identity and resources, and do not add user approval. + await rate_limit(redis, "auto-register-ip:" + peer(request), 120) + key = validate_enrollment(body) + await rate_limit(redis, "auto-register-host:" + body.host_id, 30) + existing = store.device(body.host_id) + + def check_existing(device): + if device["revoked"]: + raise HTTPException(403, "device revoked") + try: + registered = verify_device(device["cert"], settings.ca_cert.read_bytes(), body.host_id) + if raw_public(registered) != raw_public(key): + raise ValueError() + except Exception: + raise HTTPException(409, "existing device identity mismatch or certificate expired") from None + return device["cert"] + + if existing: + cert = check_existing(existing) + else: + if not settings.enrollment_signer_url: + raise HTTPException(503, "automatic registration is not enabled; update ECS first") + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= settings.max_devices: + raise HTTPException(503, "device capacity reached") + await rate_limit(redis, "auto-register-total", settings.max_pending, 3600) + try: + cert = await request_certificate(settings, body) + issued = verify_device(cert, settings.ca_cert.read_bytes(), body.host_id) + if raw_public(issued) != raw_public(key): + raise ValueError() + except Exception: + raise HTTPException(503, "certificate issuer unavailable or invalid response") from None + from .store import INTEGRITY_ERRORS + try: + store.add_device(body.host_id, cert, fingerprint(cert)) + except INTEGRITY_ERRORS: + # A retry/concurrent request must return the original certificate. + cert = check_existing(store.device(body.host_id)) + store.audit("device", "automatic_registration", body.host_id, "success") + await redis.set("hn:device:" + body.host_id, fingerprint(cert)) + return no_cache(JSONResponse(sign({"status": "registered", "host_id": body.host_id, + "cert": cert}, signer), status_code=201 if not existing else 200)) + + @router.post("/device/enroll") + async def request_enrollment(body: EnrollmentRequest, request: Request): + await rate_limit(redis, "enroll-ip:" + peer(request), 10) + validate_enrollment(body) + await rate_limit(redis, "enroll-host:" + body.host_id, 3, 3600) + if store.device(body.host_id): + raise HTTPException(409, "device already registered; recover its original certificate") + # Global rolling capacity bound; prevents anonymous request-table exhaustion. + await rate_limit(redis, "enroll-global", settings.max_pending, 1800) + identifier, claim_secret = secrets.token_hex(16), secrets.token_urlsafe(32) + record = {"host_id": body.host_id, "public_key": body.public_key, + "claim_hash": digest(claim_secret), "created": time.time(), "status": "pending"} + await redis.set("hn:enroll:" + identifier, json.dumps(record), ex=1800) + return no_cache(JSONResponse(sign({"request_id": identifier, "claim_secret": claim_secret, + "host_id": body.host_id, "expires_in": 1800}, signer), status_code=201)) + + @router.get("/device/enroll/{identifier}") + async def claim(identifier: str, request: Request): + await rate_limit(redis, "enroll-poll:" + peer(request), 60) + data = await redis.get("hn:enroll:" + identifier) + if not data: + raise HTTPException(404, "request expired") + record = json.loads(data) + import hmac + if not hmac.compare_digest(record["claim_hash"], digest(request.headers.get("X-Enrollment-Secret", ""))): + raise HTTPException(404, "request unavailable") + if record["status"] == "pending": + return no_cache(JSONResponse({"status": "pending"}, status_code=202)) + device = store.device(record["host_id"]) + if not device or device["revoked"]: + raise HTTPException(403, "device unavailable") + # Recoverable for its original claimant for the request TTL; public certificate only. + return no_cache(JSONResponse(sign({"status": "approved", "host_id": record["host_id"], + "request_id": identifier, "cert": device["cert"]}, signer))) + + @router.get("/admin/enrollments/{identifier}") + async def inspect(identifier: str, request: Request): + admin(request) + encoded = await redis.get("hn:enroll:" + identifier) + if not encoded: + raise HTTPException(404, "request expired") + record = json.loads(encoded) + return {k: v for k, v in record.items() if k != "claim_hash"} + + @router.post("/admin/enrollments/{identifier}/approve") + async def approve(identifier: str, body: EnrollmentApproval, request: Request): + admin(request) + encoded = await redis.get("hn:enroll:" + identifier) + if not encoded: + raise HTTPException(404, "request expired") + record = json.loads(encoded) + try: + public = verify_device(body.cert, settings.ca_cert.read_bytes(), record["host_id"]) + requested = serialization.load_pem_public_key(record["public_key"].encode()) + if raw_public(public) != raw_public(requested): + raise ValueError() + except Exception: + raise HTTPException(400, "certificate does not match enrollment") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= settings.max_devices: + raise HTTPException(503, "device capacity reached") + fp = fingerprint(body.cert) + existing = store.device(record["host_id"]) + if existing and (existing["revoked"] or existing["fingerprint"] != fp): + raise HTTPException(409, "device conflict") + if not existing: + from .store import INTEGRITY_ERRORS + try: + store.add_device(record["host_id"], body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "concurrent device registration") from None + await redis.set("hn:device:" + record["host_id"], fp) + record["status"] = "approved" + remaining = await redis.ttl("hn:enroll:" + identifier) + if remaining < 1: + raise HTTPException(409, "request expired during approval") + await redis.set("hn:enroll:" + identifier, json.dumps(record), ex=remaining) + store.audit("admin", "enrollment_approved", record["host_id"], "success") + return {"status": "approved", "host_id": record["host_id"]} + + return router diff --git a/container-code/router/relay/enrollment_signer.py b/container-code/router/relay/enrollment_signer.py new file mode 100644 index 0000000..6089d02 --- /dev/null +++ b/container-code/router/relay/enrollment_signer.py @@ -0,0 +1,43 @@ +"""Internal automatic CA issuer. No published host port, no application credentials.""" +import os +from pathlib import Path + +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from fastapi import FastAPI, Request +from .common import BodyLimitMiddleware, require_secret +from .crypto import load_private, issue_device, raw_public, verify_device +from .enrollment import EnrollmentRequest, validate_enrollment + + +def create_app(*, ca_key_path=None, ca_cert_path=None, secret=None): + key = load_private(Path(ca_key_path or os.environ.get("HN_ISSUER_CA_KEY", "/issuer/ca.key"))) + ca_bytes = Path(ca_cert_path or os.environ.get("HN_ISSUER_CA_CERT", "/trust/ca.pem")).read_bytes() + ca = x509.load_pem_x509_certificate(ca_bytes) + if raw_public(key.public_key()) != raw_public(ca.public_key()): + raise ValueError("issuer CA key mismatch") + token = secret or os.environ.get("HN_INTERNAL_SECRET", "") + if len(token) < 32: + raise ValueError("issuer requires internal credential") + app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=16384) + + @app.get("/ready") + async def ready(): + return {"status": "ready"} + + @app.post("/issue") + async def issue(body: EnrollmentRequest, request: Request): + require_secret(request.headers.get("X-Internal-Secret"), token) + public = validate_enrollment(body) + cert = issue_device(key, ca, body.host_id, public).public_bytes(serialization.Encoding.PEM).decode() + verify_device(cert, ca_bytes, body.host_id) + return {"cert": cert} + + return app + + +if __name__ == "__main__": + import uvicorn + uvicorn.run("relay.enrollment_signer:create_app", factory=True, host="0.0.0.0", port=8000, + workers=1, access_log=False, proxy_headers=False, log_level="warning") diff --git a/container-code/router/relay/hainuo1065.py b/container-code/router/relay/hainuo1065.py new file mode 100644 index 0000000..6f7735b --- /dev/null +++ b/container-code/router/relay/hainuo1065.py @@ -0,0 +1,107 @@ +"""Adapter for the native API actually found in installer 1.065. + +Loopback only, independent API key, no platform secrets, no redirects or retries. +The included native launcher offers only text inference with no tools. +""" +import hashlib +import json +import re +from pathlib import Path +from urllib.parse import urlsplit + +import httpx + + +class NativeAPIError(RuntimeError): + """Contains a sanitized status only, never response body or credentials.""" + + +def read_native_config(path): + path = Path(path) + data = json.loads(path.read_text(encoding="utf-8-sig")) + url = data.get("url", "") + parts = urlsplit(url) + if (parts.scheme != "http" or parts.hostname != "127.0.0.1" or not parts.port + or parts.username or parts.password or parts.query or parts.fragment + or parts.path not in {"", "/"}): + raise ValueError("native API must be a fixed IPv4 loopback HTTP base URL") + if not isinstance(data.get("api_key"), str) or len(data["api_key"]) < 32: + raise ValueError("native API requires a dedicated key of at least 32 characters") + if data.get("mode") not in {"text-only", "profile-tools"}: + raise ValueError("unsupported native API mode") + data["url"] = url.rstrip("/") + return data + + +class Hainuo1065Agent: + def __init__(self, config_path, transport=None): + self.config = read_native_config(config_path) + self.http = httpx.AsyncClient(base_url=self.config["url"], timeout=120, + follow_redirects=False, trust_env=False, transport=transport) + self.headers = {"Authorization": "Bearer " + self.config["api_key"]} + + async def _json(self, method, path, **kwargs): + try: + async with self.http.stream(method, path, headers={**self.headers, **kwargs.pop("headers", {})}, **kwargs) as response: + if response.status_code != 200: + raise NativeAPIError(f"native API HTTP {response.status_code}") + body = bytearray() + async for chunk in response.aiter_bytes(): + body.extend(chunk) + if len(body) > 262144: + raise NativeAPIError("native API response exceeds limit") + data = json.loads(body) + if not isinstance(data, dict): + raise NativeAPIError("native API response is not an object") + return data + except (httpx.HTTPError, ValueError): + raise NativeAPIError("native API connection/JSON failure") from None + + async def probe(self): + health = await self._json("GET", "/health") + if health.get("platform") != "hermes-agent" or health.get("status") != "ok": + raise NativeAPIError("endpoint is not the native Hermes API") + capabilities = await self._json("GET", "/v1/capabilities") + policy = capabilities.get("hainuo_relay_policy", {}) + # A stock unrestricted API endpoint is deliberately not sufficient. + expected_mode = self.config["mode"] + expected_tools = "profile" if expected_mode == "profile-tools" else "disabled" + if policy != {"mode": expected_mode, "tools": expected_tools, "contract": "hainuo-1065-v1"}: + raise NativeAPIError("native API policy missing; start the supplied restricted launcher") + models = await self._json("GET", "/v1/models") + model = self.config.get("model") + available = models.get("data") + if not isinstance(available, list) or not available or not all(isinstance(x, dict) and isinstance(x.get("id"), str) for x in available): + raise NativeAPIError("native model list invalid") + if model and model not in {x["id"] for x in available}: + raise NativeAPIError("configured model alias unavailable") + self.model = model or available[0]["id"] + return {"status": "ready", "platform": "hermes-agent", "model": self.model, + "mode": expected_mode, "version": health.get("version", "unknown")} + + async def execute(self, text, owner, turn): + try: + # Recheck the actual local service before each inference, including reconnects. + await self.probe() + digest = hashlib.sha256((owner + "\0" + turn).encode()).hexdigest() + data = await self._json("POST", "/v1/chat/completions", + headers={"Idempotency-Key": "hn-" + digest}, + json={"model": self.model, "stream": False, + "messages": [{"role": "user", "content": text}]}) + choices = data.get("choices") + if not isinstance(choices, list) or not choices or not isinstance(choices[0], dict): + raise NativeAPIError("native completion choices missing") + choice = choices[0] + message = choice.get("message", {}) + if (choice.get("finish_reason") != "stop" or message.get("tool_calls") + or not isinstance(message.get("content"), str) + or data.get("hermes", {}).get("failed") + or data.get("hermes", {}).get("partial")): + raise NativeAPIError("native completion incomplete or unexpected tool call") + return message["content"][:16000] + except NativeAPIError as error: + # Safe generated error strings only; never echo upstream model/API errors. + return "海诺原生接口未完成请求:" + str(error) + "。未自动重试。" + + async def close(self): + await self.http.aclose() diff --git a/container-code/router/relay/longconn.py b/container-code/router/relay/longconn.py new file mode 100644 index 0000000..6d7be70 --- /dev/null +++ b/container-code/router/relay/longconn.py @@ -0,0 +1,83 @@ +"""Optional official SDK event source, private platform domain explicitly configured.""" +import json +import logging +import os +import threading +import time + +import httpx +import redis +from .config import Settings + + +def disable_proxy_environment(): + """This dedicated SDK worker must connect directly, including discovery and WS.""" + for key in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "http_proxy", "https_proxy", "all_proxy"): + os.environ.pop(key, None) + os.environ["NO_PROXY"] = "*" + os.environ["no_proxy"] = "*" + + +def main(): + disable_proxy_environment() + import lark_oapi as lark + s = Settings(role="longconn") + if not s.feishu_enabled or s.feishu_event_source != "longconn": + raise RuntimeError("platform is disabled") + # The SDK may log websocket URLs, tokens or event bodies. Disable its logging entirely. + logging.disable(logging.CRITICAL) + r = redis.Redis.from_url(s.redis_url) + lease = r.lock("hn:longconn:leader", timeout=30, thread_local=False) + if not lease.acquire(blocking=False): + raise RuntimeError("another long-connection consumer owns the lease") + url = os.environ.get("HN_CONNECTOR_INTERNAL_URL", "http://connector:8000") + client = httpx.Client(base_url=url, timeout=10, follow_redirects=False, trust_env=False) + done = threading.Event() + + def maintain_lease(): + while not done.wait(8): + try: + lease.extend(30, replace_ttl=True) + except Exception: + # Fail closed. Continuing after lease loss could create two consumers. + os._exit(2) + + threading.Thread(target=maintain_lease, daemon=True).start() + + def received(event): + data = json.loads(lark.JSON.marshal(event)) + response = client.post("/internal/events", headers={"X-Internal-Secret": s.internal_secret}, json=data) + if response.status_code != 200: + raise RuntimeError("connector did not accept event") + print(json.dumps({"event": "platform_event_accepted", "type": data.get("header", {}).get("event_type")}), flush=True) + + handler = lark.EventDispatcherHandler.builder("", "").register_p2_im_message_receive_v1(received).build() + class ObservableClient(lark.ws.Client): + async def _connect(self): + try: + await super()._connect() + if self._conn is not None: + print(json.dumps({"event": "platform_connected", "api_host": s.feishu_api_base}), flush=True) + except Exception as error: + print(json.dumps({"event": "platform_connect_failed", "error_type": type(error).__name__}), flush=True) + raise + + try: + # domain is an actual supported SDK argument; no public-cloud-domain monkey patch. + ObservableClient(s.feishu_app_id, s.feishu_app_secret, event_handler=handler, + domain=s.feishu_api_base.rstrip("/"), log_level=lark.LogLevel.ERROR).start() + finally: + done.set() + client.close() + try: + lease.release() + except redis.exceptions.LockError: + pass + + +if __name__ == "__main__": + try: + main() + except Exception as error: + print(json.dumps({"event": "platform_consumer_stopped", "error_type": type(error).__name__}), flush=True) + raise SystemExit(1) from None diff --git a/container-code/router/relay/oauth.py b/container-code/router/relay/oauth.py new file mode 100644 index 0000000..1b35a14 --- /dev/null +++ b/container-code/router/relay/oauth.py @@ -0,0 +1,144 @@ +import asyncio +import hashlib +import hmac +import secrets +import time +from dataclasses import dataclass +from urllib.parse import urlencode + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .platform import PlatformError + + +@dataclass +class Transaction: + secret_hash: str + expires: float + status: str = "waiting" + result: dict | None = None + owner: str = "" + + +class Empty(BaseModel): + host_id: str = "" + proof: dict = Field(default_factory=dict) + + +class Refresh(BaseModel): + refresh_token: str = Field(min_length=1, max_length=4096) + + +class OAuth: + """A single connector process owns transactions and tokens in memory only.""" + def __init__(self, settings, redis, platform): + self.settings, self.redis, self.platform = settings, redis, platform + self.pending: dict[str, Transaction] = {} + self.lock = asyncio.Lock() + self.router = APIRouter() + self.router.add_api_route("/oauth/start", self.start, methods=["POST"], status_code=201) + self.router.add_api_route("/oauth/cb", self.callback, methods=["GET"]) + self.router.add_api_route("/oauth/claim", self.claim, methods=["GET"]) + self.router.add_api_route("/oauth/pending", self.status, methods=["GET"]) + self.router.add_api_route("/oauth/refresh", self.refresh, methods=["POST"]) + + def prune(self): + now = time.monotonic() + for state in list(self.pending): + if self.pending[state].expires <= now: + del self.pending[state] + + def authenticated(self, state, secret): + self.prune() + item = self.pending.get(state) + digest = hashlib.sha256((secret or "").encode()).hexdigest() + if not item or not hmac.compare_digest(item.secret_hash, digest): + raise HTTPException(404, "transaction not found") + return item + + async def start(self, body: Empty, request: Request): + if not self.settings.feishu_enabled or not self.settings.oauth_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.redis, "oauth:start:" + peer(request), 20) + owner = "" + if body.host_id: + from .work_api import device_owner + owner = await device_owner(self.settings, self.redis, self.store, body.host_id, + body.proof, "work-oauth-v1", {}) + async with self.lock: + self.prune() + if len(self.pending) >= self.settings.max_pending: + raise HTTPException(503, "too many pending transactions") + state, secret = secrets.token_urlsafe(32), secrets.token_urlsafe(32) + self.pending[state] = Transaction(hashlib.sha256(secret.encode()).hexdigest(), time.monotonic() + 600, owner=owner) + redirect = self.settings.public_url.rstrip("/") + "/oauth/cb" + parameters = {"app_id": self.settings.feishu_app_id, "redirect_uri": redirect, "state": state} + if self.settings.feishu_oauth_scopes: + parameters["scope"] = self.settings.feishu_oauth_scopes + url = self.settings.feishu_authorize_url + "?" + urlencode(parameters) + return no_cache(JSONResponse({"state": state, "claim_secret": secret, "authorization_url": url, + "redirect_uri": redirect, "expires_in": 600, + "code_ttl": 60, "poll_interval": 2}, status_code=201)) + + async def callback(self, request: Request, state: str = "", code: str = "", error: str = ""): + await rate_limit(self.redis, "oauth:cb:" + peer(request)) + async with self.lock: + self.prune() + item = self.pending.get(state) + if error and item and item.status == "waiting": + item.status = "failed" + return no_cache(HTMLResponse("

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

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

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

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

连接你的
工作电脑。

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

+
01 / 连接本人设备

输入电脑上的配对码

+

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

+
+
使用海能Work授权

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

+ +

正在检查网页会话…

+
PC需保持开机、联网,并运行原生API和桥接端。试点网页由中继在内存中转发正文,不保存聊天记录;当前页关闭后消息不保留。
+
diff --git a/container-code/router/relay/web/style.css b/container-code/router/relay/web/style.css new file mode 100644 index 0000000..a33d19c --- /dev/null +++ b/container-code/router/relay/web/style.css @@ -0,0 +1,3 @@ +:root{color-scheme:light;font-family:system-ui,-apple-system,"Segoe UI","Microsoft YaHei",sans-serif;color:#17372c;background:#f2f4ed}*{box-sizing:border-box}body{margin:0}main{max-width:820px;margin:0 auto;padding:60px 26px}.mark{font-size:13px;letter-spacing:.16em;color:#557764;font-weight:700}h1{font-size:clamp(42px,8vw,70px);letter-spacing:-.05em;line-height:1.12;font-weight:650;margin:40px 0 22px}.intro{font-size:17px;line-height:1.8;color:#56675a;max-width:520px}.card{background:#fff;border:1px solid #dbe3d8;border-radius:20px;padding:30px;margin:40px 0 24px;box-shadow:0 12px 44px #18372c06}.step{font-size:12px;letter-spacing:.1em;color:#6d806c}h2{font-size:24px;margin:16px 0}.card p{line-height:1.8;color:#5b6c60}button{background:#214e3a;color:white;border:0;border-radius:10px;font:inherit;font-weight:600;padding:16px 20px;width:100%;text-align:left;cursor:pointer;margin-top:16px}button span{float:right}button:disabled{opacity:.5;cursor:wait}button:focus-visible,a:focus-visible{outline:3px solid #d0a349;outline-offset:4px}#status{font-size:13px;margin-bottom:0}#open{display:block;margin-top:22px;color:#214e3a;font-weight:600}#open[hidden]{display:none}footer{color:#768373;font-size:12px;line-height:1.8}@media(max-width:480px){main{padding-top:36px}.card{padding:22px}h1{margin-top:28px}} + +[hidden]{display:none!important}.badge{float:right;font-size:11px;letter-spacing:0;background:#e4ebdf;border-radius:20px;padding:6px 10px}label{display:block;font-size:13px;color:#526c5b;margin:12px 0 8px}input,textarea{font:inherit;width:100%;border:1px solid #ccd8ce;border-radius:10px;padding:14px;color:#17372c;background:#fafcf8;resize:vertical}input:focus,textarea:focus{outline:2px solid #6c987b;outline-offset:2px}details{margin-top:24px;font-size:13px;color:#587561}summary{cursor:pointer}.chat-head{display:flex;align-items:center;justify-content:space-between;gap:20px}.secondary{width:auto;background:#edf2e9;color:#31523f;padding:9px 12px;font-size:12px;margin:0}.hint{font-size:13px}#messages{display:flex;flex-direction:column;gap:12px;margin:22px 0;max-height:420px;overflow:auto}.message{padding:14px 16px;border-radius:12px;line-height:1.8;white-space:pre-wrap;overflow-wrap:anywhere;font-size:15px}.message.user{background:#e5eee2;margin-left:36px}.message.assistant{background:#f2f5ef;margin-right:24px}.message.notice{background:#fbf4e5;color:#735c2e}#status{font-size:13px;color:#5f745f;line-height:1.8}#chat-panel h2{font-size:20px}footer{max-width:640px} diff --git a/container-code/router/relay/web_session.py b/container-code/router/relay/web_session.py new file mode 100644 index 0000000..1ac9adf --- /dev/null +++ b/container-code/router/relay/web_session.py @@ -0,0 +1,183 @@ +"""Pilot H5 pairing and conversation. Credentials never appear in URLs.""" +import hashlib +import json +import secrets +import time +from urllib.parse import urlsplit + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import JSONResponse +from pydantic import BaseModel, Field + +from .common import no_cache, peer, rate_limit +from .crypto import sign, verified, verify_device +from .platform import PlatformError + +COOKIE = "hn_web_session" + + +def digest(value): + return hashlib.sha256(value.encode()).hexdigest() + + +def origin_tuple(value): + try: + parts = urlsplit(value) + if parts.scheme not in {"https", "http"} or not parts.hostname or parts.username or parts.password: + return None + return parts.scheme, parts.hostname.lower(), parts.port or (443 if parts.scheme == "https" else 80) + except ValueError: + return None + + +class PairProof(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Claim(BaseModel): + code: str = Field(min_length=12, max_length=128) + + +class Chat(BaseModel): + text: str = Field(min_length=1, max_length=8000) + request_id: str = Field(pattern=r"^[a-f0-9]{32}$") + + +class WebSessions: + def __init__(self, settings, redis, store, broker, platform): + self.s, self.r, self.store, self.broker, self.platform = settings, redis, store, broker, platform + self.router = APIRouter() + for path, method, handler in [ + ("/device/web-pair", "POST", self.device_pair), + ("/web/claim", "POST", self.claim), + ("/web/session", "GET", self.session), + ("/web/platform-session", "POST", self.platform_login), + ("/web/chat", "POST", self.chat), + ("/web/logout", "POST", self.logout), + ]: + self.router.add_api_route(path, handler, methods=[method]) + + def check_origin(self, request): + actual = origin_tuple(request.headers.get("Origin", "")) + if not actual or actual != origin_tuple(self.s.public_url): + raise HTTPException(403, "same-origin browser request required") + + async def valid_device(self, record): + device = self.store.device(record["host_id"]) + if (not device or device["revoked"] or device["owner"] != record["owner"] + or device["fingerprint"] != record["fingerprint"]): + raise HTTPException(401, "device binding changed; pair again") + generation = await self.r.get("hn:web-generation:" + record["host_id"]) or "0" + if record.get("generation") != generation: + raise HTTPException(401, "device binding changed; pair again") + return device + + async def device_pair(self, body: PairProof, request: Request): + await rate_limit(self.r, "web-pair-ip:" + peer(request), 20) + device = self.store.device(body.host_id) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "active bound device required") + try: + key = verify_device(device["cert"], self.s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["owner"] != device["owner"] + or proof["purpose"] != "h5-pair-v1" or abs(time.time() - proof["ts"]) > 90 + or not isinstance(proof["nonce"], str) or len(proof["nonce"]) != 32): + raise ValueError("invalid proof") + except Exception: + raise HTTPException(403, "invalid device proof") from None + replay = "hn:web-pair-proof:" + digest(body.host_id + proof["nonce"]) + if not await self.r.set(replay, "used", nx=True, ex=180): + raise HTTPException(409, "pairing request already used") + await rate_limit(self.r, "web-pair-device:" + body.host_id, 5) + code = secrets.token_urlsafe(18) + record = {"host_id": body.host_id, "owner": device["owner"], "fingerprint": device["fingerprint"], + "generation": await self.r.get("hn:web-generation:" + body.host_id) or "0"} + await self.r.set("hn:web-code:" + digest(code), json.dumps(record), ex=120) + self.store.audit(device["owner"], "web_pair_issued", body.host_id, "success") + return no_cache(JSONResponse(sign({"host_id": body.host_id, "code": code, "expires_in": 120, + "url": self.s.public_url.rstrip("/") + "/h5"}, self.broker.signer))) + + async def new_session(self, record): + token = secrets.token_urlsafe(32) + record = {k: record[k] for k in ("host_id", "owner", "fingerprint", "generation")} + await self.r.set("hn:web-session:" + digest(token), json.dumps(record), ex=1800) + response = no_cache(JSONResponse({"status": "paired", "host_id": record["host_id"], "expires_in": 1800})) + response.set_cookie(COOKIE, token, max_age=1800, secure=True, httponly=True, samesite="strict", path="/") + return response + + async def claim(self, body: Claim, request: Request): + self.check_origin(request) + await rate_limit(self.r, "web-claim:" + peer(request), 20) + key = "hn:web-code:" + digest(body.code.strip()) + encoded = await self.r.get(key) + if not encoded: + raise HTTPException(404, "code expired or consumed") + await self.valid_device(json.loads(encoded)) + encoded = await self.r.getdel(key) + if not encoded: + raise HTTPException(404, "code already consumed") + record = json.loads(encoded) + await self.valid_device(record) + self.store.audit(record["owner"], "web_pair_claimed", record["host_id"], "success") + return await self.new_session(record) + + async def authenticated(self, request): + token = request.cookies.get(COOKIE, "") + if not token or len(token) > 128: + raise HTTPException(401, "pair this browser first") + encoded = await self.r.get("hn:web-session:" + digest(token)) + if not encoded: + raise HTTPException(401, "browser session expired") + record = json.loads(encoded) + await self.valid_device(record) + return record + + async def session(self, request: Request): + record = await self.authenticated(request) + return no_cache(JSONResponse({"authenticated": True, "host_id": record["host_id"], + "online": bool(await self.r.exists("hn:online:" + record["host_id"])), + "mode": "pilot-text-only", "relay_sees_content": True})) + + async def platform_login(self, request: Request): + self.check_origin(request) + if not self.s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.r, "web-platform:" + peer(request), 20) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await self.platform.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = self.store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "bind your device first") + return await self.new_session({**device, "generation": await self.r.get("hn:web-generation:" + device["host_id"]) or "0"}) + + async def chat(self, body: Chat, request: Request): + self.check_origin(request) + record = await self.authenticated(request) + await rate_limit(self.r, "web-chat:" + record["owner"], 20) + text = body.text.strip() + if not text: + raise HTTPException(422, "empty message") + # PC accepts only its bound owner and the existing IM read-only channel. + if not (text == "/主机名" or text.startswith(("/回显 ", "/读取 ", "/助手 "))): + text = "/助手 " + text + turn = "web_" + digest(record["owner"] + "\0" + body.request_id) + result = await self.broker.dispatch(record["owner"], text, turn) + # Revoke/unbind during a long model call invalidates the browser immediately. + await self.authenticated(request) + return no_cache(JSONResponse(result)) + + async def logout(self, request: Request): + self.check_origin(request) + token = request.cookies.get(COOKIE, "") + if token and len(token) <= 128: + await self.r.delete("hn:web-session:" + digest(token)) + response = no_cache(JSONResponse({"status": "logged_out"})) + response.delete_cookie(COOKIE, secure=True, httponly=True, samesite="strict", path="/") + return response diff --git a/container-code/router/relay/windows_keys.py b/container-code/router/relay/windows_keys.py new file mode 100644 index 0000000..0e84e19 --- /dev/null +++ b/container-code/router/relay/windows_keys.py @@ -0,0 +1,40 @@ +"""Current-user Windows DPAPI for device keys; never uses machine-wide scope.""" +import ctypes +import os +from ctypes import wintypes + + +class Blob(ctypes.Structure): + _fields_ = [("cbData", wintypes.DWORD), ("pbData", ctypes.POINTER(ctypes.c_ubyte))] + + +def transform(data, decrypt=False): + if os.name != "nt": + raise RuntimeError("DPAPI is supported only on Windows") + buffer = (ctypes.c_ubyte * len(data)).from_buffer_copy(data) + source = Blob(len(data), buffer) + target = Blob() + crypt = ctypes.WinDLL("crypt32", use_last_error=True) + kernel = ctypes.WinDLL("kernel32", use_last_error=True) + if decrypt: + fn = crypt.CryptUnprotectData + args = [ctypes.byref(source), None, None, None, None, 1, ctypes.byref(target)] + else: + fn = crypt.CryptProtectData + args = [ctypes.byref(source), "Hainuo device identity", None, None, None, 1, ctypes.byref(target)] + fn.restype = wintypes.BOOL + if not fn(*args): + raise ctypes.WinError(ctypes.get_last_error()) + try: + return ctypes.string_at(target.pbData, target.cbData) + finally: + kernel.LocalFree.argtypes = [ctypes.c_void_p] + kernel.LocalFree(target.pbData) + + +def protect(data): + return transform(data) + + +def unprotect(data): + return transform(data, decrypt=True) diff --git a/container-code/router/relay/work_api.py b/container-code/router/relay/work_api.py new file mode 100644 index 0000000..96a6e49 --- /dev/null +++ b/container-code/router/relay/work_api.py @@ -0,0 +1,152 @@ +"""Device-authenticated USER-token business gateway; no tenant-token fallback.""" +import hashlib +import json +import re +import time +from urllib.parse import unquote + +import httpx +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import JSONResponse +from pydantic import BaseModel, Field +from .common import no_cache, rate_limit +from .crypto import canonical, verified, verify_device +from .store import INTEGRITY_ERRORS + + +class DeviceProof(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class BusinessCall(DeviceProof): + method: str = Field(pattern=r"^(GET|POST|PATCH|DELETE)$") + path: str = Field(max_length=1500) + params: dict = Field(default_factory=dict) + body: dict = Field(default_factory=dict) + operation_id: str = Field(default="", max_length=100) + + +def business_allowed(method, path): + if any(x in unquote(path) for x in ("..", "\\", "%", "?", "#", "\r", "\n")): + return False + seg = r"[A-Za-z0-9_.@-]+" + cal = r"/open-apis/calendar/v4/calendars" + rules = { + "GET": [cal, cal + "/" + seg, cal + "/" + seg + "/events(?:/" + seg + ")?(?:/attendees)?", + r"/open-apis/contact/v3/users/" + seg, r"/open-apis/search/v1/user", + r"/open-apis/(?:drive/v1/files|docx/v1/documents|wiki/v2/spaces|bitable/v1/apps)(?:/" + seg + ")*"], + "POST": [cal + "/primary", cal + "/" + seg + "/events(?:/" + seg + "/attendees)?"], + "PATCH": [cal + "/" + seg + "/events/" + seg], + "DELETE": [cal + "/" + seg + "/events/" + seg, cal + "/" + seg + "/events/" + seg + "/attendees/" + seg], + } + return any(re.fullmatch(p, path) for p in rules.get(method, [])) + + +async def device_owner(settings, redis, store, host, proof, purpose, payload): + device = store.device(host) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "active bound device required") + try: + key = verify_device(device["cert"], settings.ca_cert.read_bytes(), host) + data = verified(proof, key) + if (data["host_id"] != host or data["purpose"] != purpose or abs(time.time() - data["ts"]) > 90 + or not re.fullmatch(r"[a-f0-9]{32}", data["nonce"]) + or data["payload_hash"] != hashlib.sha256(canonical(payload)).hexdigest()): + raise ValueError() + except Exception: + raise HTTPException(403, "invalid device request proof") from None + if not await redis.set("hn:work-proof:" + host + ":" + data["nonce"], "1", nx=True, ex=180): + raise HTTPException(409, "device request replayed") + return device["owner"] + + +def router(settings, redis, store, platform): + routes = APIRouter() + store.db.execute("""CREATE TABLE IF NOT EXISTS business_operations( + owner TEXT NOT NULL, operation_id TEXT NOT NULL, request_hash TEXT NOT NULL, + status TEXT NOT NULL, result_meta TEXT NOT NULL, created DOUBLE PRECISION NOT NULL, + PRIMARY KEY(owner,operation_id))""") + if not settings.database_url: + store.db.commit() + + @routes.post("/work/api") + async def call(body: BusinessCall, request: Request): + if not settings.feishu_enabled: + raise HTTPException(503, "platform disabled") + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or not 10 < len(auth) <= 8192: + raise HTTPException(401, "user access token required") + if not business_allowed(body.method, body.path): + raise HTTPException(403, "business method/path not supported") + payload = {"method": body.method, "path": body.path, "params": body.params, "body": body.body, + "operation_id": body.operation_id, "token_hash": hashlib.sha256(auth[7:].encode()).hexdigest()} + owner = await device_owner(settings, redis, store, body.host_id, body.proof, "work-api-v1", payload) + await rate_limit(redis, "work-api:" + owner, 60) + try: + info = await platform.user_info(auth[7:]) + except Exception: + raise HTTPException(401, "user token expired or invalid; refresh or authorize") from None + if info.get("open_id") != owner: + raise HTTPException(403, "token user does not match bound device owner") + write = body.method != "GET" and not body.path.endswith(("/primary", "/batch_get_id")) + digest = hashlib.sha256(canonical({k: v for k, v in payload.items() if k != "token_hash"})).hexdigest() + if write: + if not re.fullmatch(r"[A-Za-z0-9_-]{8,100}", body.operation_id): + raise HTTPException(422, "write requires stable operation_id") + try: + with store.db: + store.db.execute("INSERT INTO business_operations VALUES(?,?,?,?,?,?)", + (owner, body.operation_id, digest, "inflight", "{}", time.time())) + except INTEGRITY_ERRORS: + row = dict(store.db.execute("SELECT * FROM business_operations WHERE owner=? AND operation_id=?", + (owner, body.operation_id)).fetchone()) + if row["request_hash"] != digest: + raise HTTPException(409, "operation_id reused with different request") + return no_cache(JSONResponse({"status": "duplicate", "operation_id": body.operation_id, + "previous_status": row["status"], **json.loads(row["result_meta"])})) + params = dict(body.params) + if write and body.method == "POST" and body.path.endswith("/events"): + params["idempotency_key"] = hashlib.sha256((owner + body.operation_id).encode()).hexdigest() + try: + async with platform.http.stream(body.method, body.path, params=params, + json=body.body if body.method != "GET" else None, + headers={"Authorization": auth}) as upstream: + raw = bytearray() + async for chunk in upstream.aiter_bytes(): + raw.extend(chunk) + if len(raw) > 2 * 1024 * 1024: + raise ValueError() + data = json.loads(raw) + if not isinstance(data, dict): + raise ValueError() + code = data.get("code", 0) + if upstream.status_code != 200 or code != 0: + status = "platform_rejected" + def safe_diagnostic(value): + text = str(value) + for secret in (auth, auth[7:], settings.feishu_app_secret, settings.admin_secret, settings.internal_secret): + if secret: + text = text.replace(secret, "[redacted]") + return text[:2000] + detail = data.get("error") or {} + result = {"status": status, "http_status": upstream.status_code, "platform_code": code, + "message": "平台拒绝:请核对用户授权、应用scope、资源权限或接口版本。", + "platform_message": safe_diagnostic(data.get("msg", data.get("message", ""))), + "platform_log_id": safe_diagnostic(detail.get("log_id", "")) if isinstance(detail, dict) else "", + "permission_details": safe_diagnostic(detail.get("permission_violations", [])) if isinstance(detail, dict) else ""} + else: + status, result = "completed", {"status": "completed", "data": data.get("data", {})} + except (httpx.HTTPError, ValueError): + status = "unknown" if write else "failed" + result = {"status": status, "message": "平台请求未取得明确结果;写操作不要换新ID重试,请先核对日历。"} + if write: + event = result.get("data", {}).get("event", {}) + meta = {k: event[k] for k in ("event_id", "app_link") if k in event} + with store.db: + store.db.execute("UPDATE business_operations SET status=?,result_meta=? WHERE owner=? AND operation_id=?", + (status, json.dumps(meta), owner, body.operation_id)) + store.audit(owner, "work_api_" + body.method.lower(), body.path, status) + return no_cache(JSONResponse(result)) + + return routes diff --git a/container-code/router/relay/work_client.py b/container-code/router/relay/work_client.py new file mode 100644 index 0000000..4cc90b3 --- /dev/null +++ b/container-code/router/relay/work_client.py @@ -0,0 +1,190 @@ +"""PC-side user OAuth and calendar operations. Tokens are DPAPI-protected only here.""" +import hashlib +import json +import secrets +import ssl +import time +from datetime import datetime +from pathlib import Path +from urllib.parse import quote, urlsplit + +import httpx +from .crypto import canonical, load_private, sign +from .windows_keys import protect, unprotect +from .process_lock import DeviceProcessLock + + +class WorkError(RuntimeError): + pass + + +class AuthorizationRequired(WorkError): + def __init__(self, url): + self.url = url + super().__init__("需要本人Work授权,请打开授权链接,完成后重新发起原任务;尚未执行平台写操作。") + + +def atomic_secret(path, data): + temp = path.with_suffix(".tmp") + temp.write_bytes(protect(json.dumps(data, ensure_ascii=False).encode())) + temp.replace(path) + + +class WorkClient: + def __init__(self, directory, url="https://10.77.76.13", *, transport=None): + self.directory = Path(directory) + self.config = json.loads((self.directory / "device.json").read_text(encoding="utf-8-sig")) + if not self.config.get("owner"): + raise WorkError("请先绑定本人Work身份。") + context = ssl.create_default_context(cafile=str(self.directory / "server.pem")) + self.http = httpx.Client(base_url=url.rstrip("/"), verify=context, trust_env=False, + timeout=25, follow_redirects=False, transport=transport) + self.owner = self.config["owner"] + self.path = self.directory / "work-user-tokens.dpapi" + self.pending = self.directory / "work-oauth-pending.dpapi" + + def close(self): + self.http.close() + + def read(self, path): + return json.loads(unprotect(path.read_bytes())) + + def proof(self, purpose, payload): + return sign({"host_id": self.config["host_id"], "purpose": purpose, "ts": int(time.time()), + "nonce": secrets.token_hex(16), "payload_hash": hashlib.sha256(canonical(payload)).hexdigest()}, + load_private(self.directory / "device.key")) + + def authorize(self): + if self.pending.exists(): + data = self.read(self.pending) + if data.get("owner") == self.owner and data["deadline"] > time.time(): + return data["authorization_url"] + response = self.http.post("/oauth/start", json={"host_id": self.config["host_id"], + "proof": self.proof("work-oauth-v1", {})}) + if response.status_code != 201: + raise WorkError("授权事务创建失败(HTTP " + str(response.status_code) + "),请检查ECS平台配置。") + data = response.json() + if urlsplit(data["authorization_url"]).scheme != "https": + raise WorkError("授权地址必须使用HTTPS。") + data.update(owner=self.owner, deadline=time.time() + data["expires_in"]) + atomic_secret(self.pending, data) + return data["authorization_url"] + + def save_tokens(self, data): + if data.get("open_id") != self.owner or not data.get("user_access_token"): + raise WorkError("授权账号与本机Work绑定账号不一致,请使用本人账号重新授权。") + now = time.time() + data = {**data, "expires_at": now + int(data["expires_in"]), + "refresh_expires_at": now + int(data.get("refresh_expires_in", 0)), "needs_authorization": False} + atomic_secret(self.path, data) + return data + + def access_token(self): + # Shared across GUI and Agent calls; never rotate the same token concurrently. + with DeviceProcessLock(self.directory / "work-token-lock"): + if self.path.exists(): + data = self.read(self.path) + if data.get("open_id") == self.owner and not data.get("needs_authorization"): + if data["expires_at"] > time.time() + 120: + return data["user_access_token"] + if data.get("refresh_token") and data.get("refresh_expires_at", 0) > time.time(): + # Mark before network; ambiguous failure must not reuse a rotating refresh token. + atomic_secret(self.path, {**data, "needs_authorization": True}) + try: + response = self.http.post("/oauth/refresh", json={"refresh_token": data["refresh_token"]}) + if response.status_code != 200: + raise WorkError("刷新失败") + data = self.save_tokens(response.json()) + return data["user_access_token"] + except (httpx.HTTPError, ValueError, WorkError): + raise AuthorizationRequired(self.authorize()) from None + if self.pending.exists(): + pending = self.read(self.pending) + if pending.get("owner") == self.owner and pending["deadline"] > time.time(): + response = self.http.get("/oauth/claim", params={"state": pending["state"]}, + headers={"X-OAuth-Claim-Secret": pending["claim_secret"]}) + if response.status_code == 200: + data = self.save_tokens(response.json()) + self.pending.unlink(missing_ok=True) + return data["user_access_token"] + if response.status_code not in {202}: + self.pending.unlink(missing_ok=True) + raise AuthorizationRequired(self.authorize()) + + def status(self): + try: + token = self.access_token() + return {"authorized": True, "owner": self.owner, "message": "本人用户授权可用;令牌未输出。", + "local_time": datetime.now().astimezone().isoformat()} + except AuthorizationRequired as error: + return {"authorized": False, "authorization_url": error.url, "message": str(error)} + + def api(self, method, path, params=None, body=None, operation_id=""): + token = self.access_token() + payload = {"method": method, "path": path, "params": params or {}, "body": body or {}, + "operation_id": operation_id, "token_hash": hashlib.sha256(token.encode()).hexdigest()} + proof = self.proof("work-api-v1", payload) + wire = {k: v for k, v in payload.items() if k != "token_hash"} + response = self.http.post("/work/api", headers={"Authorization": "Bearer " + token}, + json={**wire, "host_id": self.config["host_id"], "proof": proof}) + if response.status_code == 401: + # Do not repeat a business write automatically, even when auth fails. + if self.path.exists(): + data = self.read(self.path) + data["expires_at"] = 0 + atomic_secret(self.path, data) + raise WorkError("用户令牌已失效。本次未重试,请检查授权后再核对操作状态。") + if response.status_code != 200: + raise WorkError("平台代理失败(HTTP " + str(response.status_code) + ");未重试。") + return response.json() + + def primary_calendar(self): + result = self.api("POST", "/open-apis/calendar/v4/calendars/primary", {"user_id_type": "open_id"}) + if result.get("status") != "completed": + return result + calendars = result.get("data", {}).get("calendars", []) + own = [c["calendar"] for c in calendars if c.get("user_id") == self.owner and c.get("calendar")] + if not own and len(calendars) == 1 and not calendars[0].get("user_id") and calendars[0].get("calendar"): + own = [calendars[0]["calendar"]] + if len(own) != 1: + raise WorkError("没有唯一确定的本人主日历,请先查询日历列表并明确选择。") + return {"status": "completed", "calendar": own[0]} + + def create_event(self, summary, start_iso, end_iso, attendee_open_ids, operation_id, description="", calendar_id=""): + import re + if not re.fullmatch(r"[A-Za-z0-9_-]{8,80}", operation_id): + raise WorkError("创建日程需8到80位稳定operation_id,重试不得换ID。") + if not summary.strip() or len(summary) > 1000: + raise WorkError("日程主题不能为空或超过1000字符。") + start, end = datetime.fromisoformat(start_iso), datetime.fromisoformat(end_iso) + if start.tzinfo is None or end.tzinfo is None or end <= start: + raise WorkError("起止时间必须包含时区且结束晚于开始;不要猜测时区或时长。") + if any(not isinstance(x, str) or not x.startswith("ou_") for x in attendee_open_ids): + raise WorkError("参会人必须为当前应用身份域的open_id,请先搜索并确认,不能把姓名/工号直接当ID。") + if not calendar_id: + primary = self.primary_calendar() + if primary.get("status") != "completed": + return primary + calendar_id = primary["calendar"]["calendar_id"] + path = "/open-apis/calendar/v4/calendars/" + quote(calendar_id, safe="") + "/events" + body = {"summary": summary, "description": description, "need_notification": True, + "start_time": {"timestamp": str(int(start.timestamp())), "timezone": "Asia/Shanghai"}, + "end_time": {"timestamp": str(int(end.timestamp())), "timezone": "Asia/Shanghai"}} + result = self.api("POST", path, {"user_id_type": "open_id"}, body, operation_id) + event = result.get("data", {}).get("event", {}) + if result.get("status") == "duplicate" and result.get("previous_status") == "completed": + event = {"event_id": result.get("event_id"), "app_link": result.get("app_link")} + elif result.get("status") != "completed": + return result + if not event.get("event_id"): + raise WorkError("平台未返回event_id,不能认定创建成功或继续邀请。") + ids = sorted(set([self.owner] + list(attendee_open_ids))) + invite = self.api("POST", path + "/" + quote(event["event_id"], safe="") + "/attendees", + {"user_id_type": "open_id"}, + {"attendees": [{"type": "user", "user_id": x} for x in ids], "need_notification": True}, + operation_id + "_attendees") + invited = invite.get("status") == "completed" or (invite.get("status") == "duplicate" and invite.get("previous_status") == "completed") + return {"status": "completed" if invited else "partial", "event_created": True, + "calendar_id": calendar_id, "event_id": event["event_id"], "app_link": event.get("app_link"), + "attendees_invited": invited, "invite_result": invite, + "message": "日程已创建并提交参会人邀请;不代表对方已接受。" if invited else "日程已创建,但邀请未完成;保留原操作ID和event_id处理,勿重新创建日程。"} diff --git a/container-code/router/relay/work_tools.py b/container-code/router/relay/work_tools.py new file mode 100644 index 0000000..b1c6da4 --- /dev/null +++ b/container-code/router/relay/work_tools.py @@ -0,0 +1,145 @@ +"""Hermes native tools backed by the authenticated user-token relay.""" +import json +import os +import threading +import time +from pathlib import Path +import httpx +from .work_client import WorkClient, WorkError, AuthorizationRequired + +INSTRUCTIONS = """海能Work平台能力已接入本机专用工具:work_authorization、work_calendar、work_contacts、work_api_request。 +涉及飞书/海能Work日历或文档时优先调用这些工具,勿使用manage_connections寻找公有云连接器,勿索要或搜索App Secret。 +用户token仅由本机安全存储和ECS换取,第一次需要本人打开授权链接。工具返回authorization_required时将链接完整提供给本人,不能声称已经创建。 +当前日期以work_authorization返回的local_time为准。创建日程必须确定带时区的起止时间和真实参会人open_id。 +姓名/工号须用work_contacts搜索核实,多个候选必须澄清,不能猜open_id。平台返回partial表示已创建但邀请未完成,勿再次创建。 +同一写操作的operation_id必须保持不变,网络异常不得换ID重试。日程创建成功、邀请提交成功、对方接受是不同状态。 +""" + +_watching = set() +_watch_lock = threading.Lock() + + +def watch_authorization(directory): + """Claim within the server's 60-second result TTL, without keeping tokens in ECS.""" + name = str(directory) + with _watch_lock: + if name in _watching: + return + _watching.add(name) + def poll(): + client = None + try: + client = WorkClient(directory, os.environ.get("HN_WORK_RELAY_URL", "https://10.77.76.13")) + for _ in range(295): + time.sleep(2) + try: + if client.pending.exists() and client.read(client.pending).get("deadline", 0) <= time.time(): + return + if client.status().get("authorized"): + return + except Exception: + # Another local consumer may hold the short token lock. + continue + except Exception: + pass + finally: + if client: + client.close() + with _watch_lock: + _watching.discard(name) + threading.Thread(target=poll, daemon=True, name="work-user-auth-claim").start() + + +def register(directory): + from tools.registry import registry + import toolsets + definitions = [ + ("work_authorization", "查询或发起本人海能Work用户授权,返回可点击链接和本机时间;不返回token。", { + "type": "object", "properties": {}, "additionalProperties": False}), + ("work_calendar", "本人用户身份的日历:list/primary/list_events/get_event/create_event/invite。写操作operation_id需稳定,起止时间均带时区。", { + "type": "object", "properties": { + "action": {"type": "string", "enum": ["list", "primary", "list_events", "get_event", "create_event", "invite"]}, + "calendar_id": {"type": "string"}, "event_id": {"type": "string"}, + "summary": {"type": "string"}, "description": {"type": "string"}, + "start_iso": {"type": "string", "description": "ISO8601,例如2026-10-27T09:00:00+08:00"}, + "end_iso": {"type": "string"}, "attendee_open_ids": {"type": "array", "items": {"type": "string"}}, + "operation_id": {"type": "string", "description": "8-80位稳定操作ID,同一意图重试不能更换"}, + "params": {"type": "object"}}, "required": ["action"]}), + ("work_contacts", "使用本人user token搜索参会人;query可为姓名或工号,返回候选,不能未经核实猜ID。open_id可读取具体用户信息。", { + "type": "object", "properties": {"query": {"type": "string"}, "open_id": {"type": "string"}}}), + ("work_api_request", "携本人user token代理受支持业务API(日历CRUD/邀请、通讯录查询、文档云盘知识库读取)。禁止认证接口,不支持的路径返回403。写操作必须operation_id。", { + "type": "object", "properties": {"method": {"type": "string", "enum": ["GET", "POST", "PATCH", "DELETE"]}, + "path": {"type": "string"}, "params": {"type": "object"}, "body": {"type": "object"}, + "operation_id": {"type": "string"}}, "required": ["method", "path"]}), + ] + + def handler_for(name): + def handle(args, **kwargs): + client = None + try: + client = WorkClient(directory, os.environ.get("HN_WORK_RELAY_URL", "https://10.77.76.13")) + if name == "work_authorization": + result = client.status() + if not result.get("authorized"): + watch_authorization(directory) + elif name == "work_api_request": + result = client.api(**args) + elif name == "work_contacts": + from urllib.parse import quote + result = client.api("GET", "/open-apis/contact/v3/users/" + quote(args["open_id"], safe=""), + {"user_id_type": "open_id"}) if args.get("open_id") else client.api( + "GET", "/open-apis/search/v1/user", {"query": args.get("query", ""), "page_size": 20}) + else: + result = calendar(client, args) + return json.dumps(result, ensure_ascii=False) + except AuthorizationRequired as error: + watch_authorization(directory) + return json.dumps({"status": "authorization_required", "authorization_url": error.url, "message": str(error)}, ensure_ascii=False) + except (WorkError, ValueError, KeyError) as error: + return json.dumps({"error": str(error)[:500], "status": "failed"}, ensure_ascii=False) + except Exception as error: + return json.dumps({"error": type(error).__name__ + ";操作状态未确认,未自动重试。"}, ensure_ascii=False) + finally: + if client: + client.close() + return handle + + for name, description, parameters in definitions: + registry.register(name=name, toolset="hainuo_work", schema={"name": name, "description": description, "parameters": parameters}, + handler=handler_for(name), description=description) + toolsets.TOOLSETS["hainuo_work"] = {"description": "海能Work本人用户授权及日历业务", "tools": [x[0] for x in definitions], "includes": []} + # Present these four small schemas directly; otherwise Hermes defers custom + # toolsets behind tool_search and can wrongly search a public-cloud connector. + for name, _, _ in definitions: + if name not in toolsets._HERMES_CORE_TOOLS: + toolsets._HERMES_CORE_TOOLS.append(name) + + +def calendar(client, args): + from urllib.parse import quote + action = args["action"] + if action == "primary": + return client.primary_calendar() + if action == "list": + return client.api("GET", "/open-apis/calendar/v4/calendars", args.get("params", {})) + if action == "create_event": + return client.create_event(**{k: v for k, v in args.items() if k != "action" and k != "params"}) + calendar_id = args.get("calendar_id") + if not calendar_id: + primary = client.primary_calendar() + if primary.get("status") != "completed": + return primary + calendar_id = primary["calendar"]["calendar_id"] + base = "/open-apis/calendar/v4/calendars/" + quote(calendar_id, safe="") + "/events" + if action == "list_events": + return client.api("GET", base, args.get("params", {})) + base += "/" + quote(args["event_id"], safe="") + if action == "get_event": + return client.api("GET", base, {"user_id_type": "open_id"}) + if action == "invite": + ids = args.get("attendee_open_ids", []) + if not ids or any(not x.startswith("ou_") for x in ids): + raise WorkError("需要已确认的参会人open_id。") + return client.api("POST", base + "/attendees", {"user_id_type": "open_id"}, + {"attendees": [{"type": "user", "user_id": x} for x in ids], "need_notification": True}, args["operation_id"]) + raise WorkError("不支持的日历操作。") diff --git a/container-code/router/requirements.lock b/container-code/router/requirements.lock new file mode 100644 index 0000000..c763938 --- /dev/null +++ b/container-code/router/requirements.lock @@ -0,0 +1,41 @@ +annotated-types==0.8.0 +anyio==4.15.1 +certifi==2026.10.23 +cffi==2.1.1 +charset-normalizer==3.5.2 +click==8.5.0 +colorama==0.4.6 +cryptography==46.0.3 +fakeredis==2.32.0 +fastapi==0.119.1 +h11==0.16.0 +httpcore==1.0.9 +httpx==0.28.1 +idna==3.20 +iniconfig==2.3.1 +lark-oapi==1.5.3 +packaging==26.3 +pluggy==1.7.0 +psycopg==3.2.12 +psycopg-binary==3.2.12 +pycparser==3.12 +pycryptodome==4.2.0 +pydantic==2.14.1 +pydantic-settings==2.11.0 +pydantic_core==2.50.1 +Pygments==2.21.0 +pytest==8.4.2 +pytest-asyncio==1.2.0 +python-dotenv==1.2.4 +PyYAML==6.0.3 +redis==6.4.0 +requests==2.34.2 +requests-toolbelt==1.0.0 +sortedcontainers==2.4.0 +starlette==0.48.0 +typing-inspection==0.4.4 +typing_extensions==4.16.0 +tzdata==2026.5 +urllib3==2.8.0 +uvicorn==0.38.0 +websockets==15.0.1 diff --git a/deployed-services.tsv b/deployed-services.tsv new file mode 100644 index 0000000..7e0a380 --- /dev/null +++ b/deployed-services.tsv @@ -0,0 +1,5 @@ +service container image_id status +connector e4bc619af2ae37683bf0f20df4e650f0e9554b3930835ee130a27095239dac38 sha256:90324bc252d99afc3ba068e9b416eec1ff31e9302716d1c08a525a45ad0ef011 running +router 50ae1e22f807a14e641aa617facbb4f637629decb619c4fdca8e851231f567f1 sha256:90324bc252d99afc3ba068e9b416eec1ff31e9302716d1c08a525a45ad0ef011 running +longconn c8160ada12a5798865c8d222b83ee429874c4b8811b8d5409d243047d87b9607 sha256:90324bc252d99afc3ba068e9b416eec1ff31e9302716d1c08a525a45ad0ef011 running +enrollment-signer bd5c1b5846d3d2139ee557e919af0f43075bfebbaef935223f238cb7e7fc4bb0 sha256:5c8c389adf40b8b287afeed77742bbc88cb8b7c77fd9d9332a1cca49b08196a7 running diff --git a/deployment-source/compose.acceptance.yaml b/deployment-source/compose.acceptance.yaml new file mode 100644 index 0000000..e46f6d2 --- /dev/null +++ b/deployment-source/compose.acceptance.yaml @@ -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 diff --git a/deployment-source/compose.autoreg.yaml b/deployment-source/compose.autoreg.yaml new file mode 100644 index 0000000..9902acb --- /dev/null +++ b/deployment-source/compose.autoreg.yaml @@ -0,0 +1,39 @@ +services: + connector: + environment: + HN_ENROLLMENT_SIGNER_URL: + 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 diff --git a/deployment-source/compose.edge-fix.yaml b/deployment-source/compose.edge-fix.yaml new file mode 100644 index 0000000..1811c76 --- /dev/null +++ b/deployment-source/compose.edge-fix.yaml @@ -0,0 +1,4 @@ +services: + edge: + cap_add: + - NET_BIND_SERVICE diff --git a/deployment-source/compose.h5.yaml b/deployment-source/compose.h5.yaml new file mode 100644 index 0000000..adba6f3 --- /dev/null +++ b/deployment-source/compose.h5.yaml @@ -0,0 +1,4 @@ +services: + connector: + environment: + HN_REQUEST_TIMEOUT: diff --git a/deployment-source/compose.offline.yaml b/deployment-source/compose.offline.yaml new file mode 100644 index 0000000..a7fd06d --- /dev/null +++ b/deployment-source/compose.offline.yaml @@ -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 diff --git a/deployment-source/compose.userapi.yaml b/deployment-source/compose.userapi.yaml new file mode 100644 index 0000000..1caaa41 --- /dev/null +++ b/deployment-source/compose.userapi.yaml @@ -0,0 +1,6 @@ +services: + connector: + environment: + HN_OAUTH_ENABLED: + HN_FEISHU_AUTHORIZE_URL: ${HN_FEISHU_AUTHORIZE_URL} + HN_FEISHU_OAUTH_SCOPES: ${HN_FEISHU_OAUTH_SCOPES} diff --git a/deployment-source/compose.work.yaml b/deployment-source/compose.work.yaml new file mode 100644 index 0000000..59a84cd --- /dev/null +++ b/deployment-source/compose.work.yaml @@ -0,0 +1,9 @@ +services: + connector: + environment: + HN_FEISHU_EVENT_SOURCE: + HN_OAUTH_ENABLED: + HN_REQUEST_TIMEOUT: + longconn: + environment: + HN_FEISHU_EVENT_SOURCE: diff --git a/deployment-source/compose.yaml b/deployment-source/compose.yaml new file mode 100644 index 0000000..4c12b3c --- /dev/null +++ b/deployment-source/compose.yaml @@ -0,0 +1,216 @@ +name: hainuo-relay +services: + postgres: + image: postgres:17-alpine + restart: unless-stopped + environment: + POSTGRES_DB: + POSTGRES_USER: + 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: + HN_DATABASE_URL: ${HN_DATABASE_URL} + HN_CA_CERT: + HN_CONNECTOR_KEY: + HN_PUBLIC_URL: ${HN_PUBLIC_URL} + HN_ROUTER_URL: ${HN_ROUTER_URL} + HN_DEVICE_DOMAIN: ${HN_DEVICE_DOMAIN} + HN_FEISHU_ENABLED: ${HN_FEISHU_ENABLED} + HN_FEISHU_API_BASE: ${HN_FEISHU_API_BASE} + HN_FEISHU_AUTHORIZE_URL: ${HN_FEISHU_AUTHORIZE_URL} + HN_FEISHU_APP_ID: ${HN_FEISHU_APP_ID} + HN_FEISHU_APP_SECRET: ${HN_FEISHU_APP_SECRET} + HN_FEISHU_ENCRYPT_KEY: ${HN_FEISHU_ENCRYPT_KEY} + HN_FEISHU_VERIFICATION_TOKEN: ${HN_FEISHU_VERIFICATION_TOKEN} + HN_PROXY_ENABLED: ${HN_PROXY_ENABLED} + 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: + 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: + 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: {} diff --git a/deployment-source/deploy/Caddyfile b/deployment-source/deploy/Caddyfile new file mode 100644 index 0000000..89ddb25 --- /dev/null +++ b/deployment-source/deploy/Caddyfile @@ -0,0 +1,22 @@ +{ + admin off + auto_https off +} +:8443 { + tls /tls/server.pem /tls/server.key + # No access log: OAuth query strings and credentials must never be logged. + @private path /admin/* /internal/* /ready + respond @private 404 + handle /device/ws { + reverse_proxy router:8000 + } + handle { + reverse_proxy connector:8000 + } + header { + Strict-Transport-Security "max-age=31536000" + X-Content-Type-Options nosniff + Referrer-Policy no-referrer + -Server + } +} diff --git a/deployment-source/deploy/hainuo-relay@.service b/deployment-source/deploy/hainuo-relay@.service new file mode 100644 index 0000000..0898742 --- /dev/null +++ b/deployment-source/deploy/hainuo-relay@.service @@ -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 diff --git a/deployment-source/docs/ACCEPTANCE.md b/deployment-source/docs/ACCEPTANCE.md new file mode 100644 index 0000000..fd13bb8 --- /dev/null +++ b/deployment-source/docs/ACCEPTANCE.md @@ -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私钥保护、读取权限模型、终端准入、证书生命周期、应急停服及目标环境测试。领导允许调整方案,不等于这些验收证据已经存在。 diff --git a/deployment-source/docs/CHANGES.md b/deployment-source/docs/CHANGES.md new file mode 100644 index 0000000..664ad58 --- /dev/null +++ b/deployment-source/docs/CHANGES.md @@ -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、数据库灾备、监控告警与实际容量验证。 + +这些项目没有通过修改文档被标记成“已完成”。领导文档的冻结与审批记录作为历史背景保存;本次实现差异在这里独立登记,不篡改原始文件。 diff --git a/deployment-source/docs/DEPLOY_LINUX_ARM64.md b/deployment-source/docs/DEPLOY_LINUX_ARM64.md new file mode 100644 index 0000000..c8aa2e1 --- /dev/null +++ b/deployment-source/docs/DEPLOY_LINUX_ARM64.md @@ -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九关。 diff --git a/deployment-source/docs/FEISHU_PRIVATE_LONGCONN.md b/deployment-source/docs/FEISHU_PRIVATE_LONGCONN.md new file mode 100644 index 0000000..6fb9c81 --- /dev/null +++ b/deployment-source/docs/FEISHU_PRIVATE_LONGCONN.md @@ -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发到聊天或打进镜像。 diff --git a/deployment-source/docs/H5_PAIR_CHAT.md b/deployment-source/docs/H5_PAIR_CHAT.md new file mode 100644 index 0000000..7ddcab5 --- /dev/null +++ b/deployment-source/docs/H5_PAIR_CHAT.md @@ -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组合链路,覆盖一次配对码、并发消费、跨站拒绝、两账号隔离、去重、退出与过期、绑定变更失效。 diff --git a/deployment-source/docs/OFFLINE_IMAGES.md b/deployment-source/docs/OFFLINE_IMAGES.md new file mode 100644 index 0000000..7f1cd55 --- /dev/null +++ b/deployment-source/docs/OFFLINE_IMAGES.md @@ -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。 diff --git a/deployment-source/docs/PROTOCOL.md b/deployment-source/docs/PROTOCOL.md new file mode 100644 index 0000000..0a466bc --- /dev/null +++ b/deployment-source/docs/PROTOCOL.md @@ -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验证结果。 diff --git a/deployment-source/docs/SPLIT_SIX_NODES.md b/deployment-source/docs/SPLIT_SIX_NODES.md new file mode 100644 index 0000000..f0993a9 --- /dev/null +++ b/deployment-source/docs/SPLIT_SIX_NODES.md @@ -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仍是单点,不承诺全系统无中断;若六台共用一台物理宿主机,也未隔离宿主机故障。 diff --git a/deployment-source/docs/TEST_REPORT.md b/deployment-source/docs/TEST_REPORT.md new file mode 100644 index 0000000..7cbfa18 --- /dev/null +++ b/deployment-source/docs/TEST_REPORT.md @@ -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业务验收通过”。原领导材料中的公网自测属于此前开发侧陈述,不是本次代码的执行证据。 diff --git a/deployment-source/scripts/backup.py b/deployment-source/scripts/backup.py new file mode 100644 index 0000000..2bd98cb --- /dev/null +++ b/deployment-source/scripts/backup.py @@ -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.") diff --git a/deployment-source/scripts/bootstrap.sh b/deployment-source/scripts/bootstrap.sh new file mode 100644 index 0000000..7bd52aa --- /dev/null +++ b/deployment-source/scripts/bootstrap.sh @@ -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.' diff --git a/deployment-source/scripts/check_arm64_images.py b/deployment-source/scripts/check_arm64_images.py new file mode 100644 index 0000000..7c89f49 --- /dev/null +++ b/deployment-source/scripts/check_arm64_images.py @@ -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)) diff --git a/deployment-source/scripts/configure_feishu_longconn.py b/deployment-source/scripts/configure_feishu_longconn.py new file mode 100644 index 0000000..026b899 --- /dev/null +++ b/deployment-source/scripts/configure_feishu_longconn.py @@ -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 diff --git a/deployment-source/scripts/fetch_official_images.py b/deployment-source/scripts/fetch_official_images.py new file mode 100644 index 0000000..1ee7efb --- /dev/null +++ b/deployment-source/scripts/fetch_official_images.py @@ -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() diff --git a/deployment-source/scripts/hainuo_native_api.py b/deployment-source/scripts/hainuo_native_api.py new file mode 100644 index 0000000..44eb4ab --- /dev/null +++ b/deployment-source/scripts/hainuo_native_api.py @@ -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() diff --git a/deployment-source/scripts/install_offline.sh b/deployment-source/scripts/install_offline.sh new file mode 100644 index 0000000..0077c64 --- /dev/null +++ b/deployment-source/scripts/install_offline.sh @@ -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.' diff --git a/deployment-source/scripts/package.py b/deployment-source/scripts/package.py new file mode 100644 index 0000000..98c1093 --- /dev/null +++ b/deployment-source/scripts/package.py @@ -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)) diff --git a/deployment-source/scripts/package_offline.py b/deployment-source/scripts/package_offline.py new file mode 100644 index 0000000..14aef22 --- /dev/null +++ b/deployment-source/scripts/package_offline.py @@ -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)) diff --git a/deployment-source/scripts/package_wheels.py b/deployment-source/scripts/package_wheels.py new file mode 100644 index 0000000..40801a2 --- /dev/null +++ b/deployment-source/scripts/package_wheels.py @@ -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)) diff --git a/deployment-source/scripts/preflight.sh b/deployment-source/scripts/preflight.sh new file mode 100644 index 0000000..3ca8338 --- /dev/null +++ b/deployment-source/scripts/preflight.sh @@ -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.' diff --git a/deployment-source/scripts/start-feishu-longconn.sh b/deployment-source/scripts/start-feishu-longconn.sh new file mode 100644 index 0000000..5dbc2f4 --- /dev/null +++ b/deployment-source/scripts/start-feishu-longconn.sh @@ -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 diff --git a/deployment-source/scripts/start.sh b/deployment-source/scripts/start.sh new file mode 100644 index 0000000..049ccbb --- /dev/null +++ b/deployment-source/scripts/start.sh @@ -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 diff --git a/deployment-source/scripts/update-h5.sh b/deployment-source/scripts/update-h5.sh new file mode 100644 index 0000000..5404874 --- /dev/null +++ b/deployment-source/scripts/update-h5.sh @@ -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 diff --git a/deployment-source/scripts/update-userapi.sh b/deployment-source/scripts/update-userapi.sh new file mode 100644 index 0000000..e4ffc5a --- /dev/null +++ b/deployment-source/scripts/update-userapi.sh @@ -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.' diff --git a/deployment-source/scripts/update-work1067.sh b/deployment-source/scripts/update-work1067.sh new file mode 100644 index 0000000..7c279be --- /dev/null +++ b/deployment-source/scripts/update-work1067.sh @@ -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.' diff --git a/deployment-source/scripts/verify_deployment.sh b/deployment-source/scripts/verify_deployment.sh new file mode 100644 index 0000000..8fd317c --- /dev/null +++ b/deployment-source/scripts/verify_deployment.sh @@ -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.' diff --git a/deployment-source/scripts/verify_image_archives.py b/deployment-source/scripts/verify_image_archives.py new file mode 100644 index 0000000..40757e1 --- /dev/null +++ b/deployment-source/scripts/verify_image_archives.py @@ -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() diff --git a/deployment-source/tests/__init__.py b/deployment-source/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/deployment-source/tests/conftest.py b/deployment-source/tests/conftest.py new file mode 100644 index 0000000..563fb60 --- /dev/null +++ b/deployment-source/tests/conftest.py @@ -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 diff --git a/deployment-source/tests/test_agent.py b/deployment-source/tests/test_agent.py new file mode 100644 index 0000000..9e3ed6e --- /dev/null +++ b/deployment-source/tests/test_agent.py @@ -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") diff --git a/deployment-source/tests/test_boundaries.py b/deployment-source/tests/test_boundaries.py new file mode 100644 index 0000000..7481d5b --- /dev/null +++ b/deployment-source/tests/test_boundaries.py @@ -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"} diff --git a/deployment-source/tests/test_crypto.py b/deployment-source/tests/test_crypto.py new file mode 100644 index 0000000..08f0eea --- /dev/null +++ b/deployment-source/tests/test_crypto.py @@ -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) diff --git a/deployment-source/tests/test_feishu_longconn_setup.py b/deployment-source/tests/test_feishu_longconn_setup.py new file mode 100644 index 0000000..248066f --- /dev/null +++ b/deployment-source/tests/test_feishu_longconn_setup.py @@ -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 diff --git a/deployment-source/tests/test_hainuo1065.py b/deployment-source/tests/test_hainuo1065.py new file mode 100644 index 0000000..cfb59fe --- /dev/null +++ b/deployment-source/tests/test_hainuo1065.py @@ -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) diff --git a/deployment-source/tests/test_image_archive.py b/deployment-source/tests/test_image_archive.py new file mode 100644 index 0000000..faaa0e5 --- /dev/null +++ b/deployment-source/tests/test_image_archive.py @@ -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 diff --git a/deployment-source/tests/test_integration.py b/deployment-source/tests/test_integration.py new file mode 100644 index 0000000..42acc93 --- /dev/null +++ b/deployment-source/tests/test_integration.py @@ -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 == [] diff --git a/deployment-source/tests/test_platform.py b/deployment-source/tests/test_platform.py new file mode 100644 index 0000000..73f5c9a --- /dev/null +++ b/deployment-source/tests/test_platform.py @@ -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() diff --git a/deployment-source/tests/test_postgres.py b/deployment-source/tests/test_postgres.py new file mode 100644 index 0000000..826993b --- /dev/null +++ b/deployment-source/tests/test_postgres.py @@ -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() diff --git a/deployment-source/tests/test_web_native_chain.py b/deployment-source/tests/test_web_native_chain.py new file mode 100644 index 0000000..9e10f4f --- /dev/null +++ b/deployment-source/tests/test_web_native_chain.py @@ -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) diff --git a/deployment-source/tests/test_web_session.py b/deployment-source/tests/test_web_session.py new file mode 100644 index 0000000..efddc77 --- /dev/null +++ b/deployment-source/tests/test_web_session.py @@ -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] diff --git a/host-code/Dockerfile b/host-code/Dockerfile new file mode 100644 index 0000000..cd582a5 --- /dev/null +++ b/host-code/Dockerfile @@ -0,0 +1,8 @@ +FROM python:3.12-slim-bookworm +ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 +WORKDIR /app +COPY requirements.lock pyproject.toml ./ +COPY relay ./relay +RUN pip install --no-cache-dir -r requirements.lock +USER 10001:10001 +ENTRYPOINT ["python", "-m", "relay.cli"] diff --git a/host-code/Dockerfile.offline b/host-code/Dockerfile.offline new file mode 100644 index 0000000..8af3dff --- /dev/null +++ b/host-code/Dockerfile.offline @@ -0,0 +1,10 @@ +FROM python:3.12-slim-bookworm +ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 +WORKDIR /app +COPY wheelhouse /wheels +COPY requirements.lock pyproject.toml ./ +COPY relay ./relay +RUN pip install --no-index --find-links=/wheels --no-cache-dir -r requirements.lock && rm -rf /wheels +RUN chmod -R a+rX /app +USER 10001:10001 +ENTRYPOINT ["python", "-m", "relay.cli"] diff --git a/host-code/README.md b/host-code/README.md new file mode 100644 index 0000000..dbaa9ee --- /dev/null +++ b/host-code/README.md @@ -0,0 +1,84 @@ +# 海诺中继服务 0.1.0 + +田泽远实施版,2026-10-26。根据当前目录的领导方案和接入安全手册独立开发。 + +已补齐官方ARM64镜像离线包:`hainuo-relay-offline-arm64-0.1.0.tar.gz`。包内包含四个官方镜像、41项ARM64依赖及本项目源码,按[离线镜像说明](docs/OFFLINE_IMAGES.md)安装,不需要访问镜像仓库或PyPI。宿主机仍需预先安装Docker Engine/Compose。 + +这是可运行、可测试的**单机联调版本**。默认采用独立容器:入口、B1连接器、B2路由器、PostgreSQL、Redis;可选启用海能Work官方SDK长连接进程。以后拆机沿用相同协议和服务镜像。 + +## 已交付 + +- B1:平台事件接入、私聊过滤、应用校验、消息去重、按人路由、原消息回复。 +- B2:PC主动出站WebSocket、证书身份与挑战应答、单设备单连接、在线租约、断连清理、仅允许在途请求的回复。 +- 设备端:本机生成私钥、公钥证书注册、一次性绑定、连接器签名验证、密文指令处理、持久化去重、断线重连。 +- 内容保护:Ed25519认证、每连接X25519临时接收密钥、每消息临时发送密钥、HKDF-SHA256、AES-256-GCM。B2不接收消息正文或解密密钥。 +- OAuth:服务端state、取件密钥请求头、立即换令牌、错误密钥不销毁、一次领取、并发刷新保护、内存事务过期清理。 +- 平台数据代理:固定上游、GET只读白名单、用户身份验证、拒绝鉴权接口/重定向/路径穿越,默认关闭。 +- H5:响应式入口壳,OAuth授权、查找本人设备、显示在线状态及设备入口。令牌不进入URL或浏览器持久存储。 +- 运维:健康/就绪检查、元数据审计、设备吊销断连、Docker Compose、systemd样例、备份工具、测试用例。 + +## 真实边界 + +1. **不能确认直接兼容1.065安装包。** 只有Inno安装EXE,没有客户端隧道协议或源代码。本版定义独立协议,并附可运行PC桥接端;未运行或修改原安装包。 +2. 桥接端默认仅支持 `/主机名`、`/回显 内容`、`/读取 测试目录内相对路径`,不执行shell命令。提供可选本地HTTP Agent适配器,但必须由海诺本机服务实现文档中的接口;未声称完成1.065实际Agent对接。 +3. **H5壳不是完整远程桌面/聊天产品。** PC网页的TLS透传、设备端会话登录、海能Work JSAPI免登、文件回传与手机实测尚需集成;不会用“能跳到域名”冒充已完成授权。 +4. 本版CA为离线人工签发,设备私钥为文件保护;Windows DPAPI/TPM不可导出、自动续期及集团规模容量不在已验证范围。 +5. 吊销会切断并禁止中继连接,**不会远程停止海诺Windows服务**。 +6. 未连接真实ECS或海能Work,不提供虚构的环境参数。Docker/ARM64运行、真实PostgreSQL、平台与1.065联调必须按验收文档执行。 + +## 先读什么 + +| 文件 | 用途 | +|---|---| +| [Linux ARM64部署](docs/DEPLOY_LINUX_ARM64.md) | 服务器初始化、启动、平台配置、两台PC接入 | +| [接口和设备协议](docs/PROTOCOL.md) | 与海诺客户端开发者对接,含本地Agent契约 | +| [方案调整清单](docs/CHANGES.md) | 相对领导方案改了什么、为何修改 | +| [验收步骤](docs/ACCEPTANCE.md) | 两人两机、安全与故障用例 | +| [六台拆分](docs/SPLIT_SIX_NODES.md) | 服务归属、端口、主备与迁移事项 | +| [测试报告](docs/TEST_REPORT.md) | 本机实测结果与尚未验证项 | + +## 本地运行测试 + +需要Python 3.12: + +```bash +python -m venv .venv +# Linux: source .venv/bin/activate +# Windows PowerShell: .\.venv\Scripts\Activate.ps1 +python -m pip install -r requirements.lock +python -m pip install --no-deps -e . +python -m pytest -q +``` + +测试会启动真实本地HTTP/WebSocket服务,使用FakeRedis和平台桩,不会访问公司服务或执行系统指令。真实PostgreSQL用例需要额外设置 `HN_TEST_DATABASE_URL`,否则明确跳过。 + +## Linux单机部署摘要 + +```bash +# 在解压后的项目目录执行;需要Docker Engine与Compose插件。 +bash scripts/bootstrap.sh relay.your-company-domain +# 编辑.env;替换runtime/tls内的服务器证书;离线保管operator/ca.key。 +bash scripts/start.sh +bash scripts/verify_deployment.sh +``` + +初始化默认 `HN_ENVIRONMENT=development`、平台关闭,允许先用管理员测试入口验证链路。临时自签TLS证书仅供受控测试;正式验收使用公司认可证书。管理员API只通过服务器回环端口18081访问,公网入口屏蔽 `/admin/*` 和 `/internal/*`。 + +## 当前部署关系 + +```mermaid +flowchart TB + IM[海能Work] -->|webhook 或 官方SDK长连接| B1[B1连接器] + B1 -->|密文指令| R[Redis临时消息与在线状态] + R --> B2[B2路由器] + PC[PC桥接端] -->|主动WSS连接| A[HTTPS入口] + A --> B2 + B2 -->|密文回复| R + R --> B1 + B1 -->|回复原消息| IM + B1 --> PG[PostgreSQL 元数据与去重台账] + H5[H5入口壳] --> A + A --> B1 +``` + +公网HTTPS入口目前在边缘代理解密传输层,但PC指令和回复另有B1↔PC应用层加密;它不是原方案阶段B的浏览器TLS透传。这一差异在调整清单中单独说明。 diff --git a/host-code/pyproject.toml b/host-code/pyproject.toml new file mode 100644 index 0000000..3bf5e2c --- /dev/null +++ b/host-code/pyproject.toml @@ -0,0 +1,32 @@ +[build-system] +requires = ["setuptools>=75"] +build-backend = "setuptools.build_meta" + +[project] +name = "hainuo-relay" +version = "0.1.0" +description = "Hainuo isolated connector and encrypted device router" +requires-python = ">=3.12" +dependencies = [ + "fastapi==0.119.1", "uvicorn==0.38.0", "httpx==0.28.1", + "redis==6.4.0", "cryptography==46.0.3", "websockets==15.0.1", + "pydantic-settings==2.11.0", + "psycopg[binary]==3.2.12", +] + +[project.optional-dependencies] +test = ["pytest==8.4.2", "pytest-asyncio==1.2.0", "fakeredis==2.32.0"] +longconn = ["lark-oapi==1.5.3"] + +[project.scripts] +hainuo-relay = "relay.cli:main" + +[tool.setuptools.packages.find] +include = ["relay*"] + +[tool.setuptools.package-data] +relay = ["web/*.html", "web/*.js", "web/*.css"] + +[tool.pytest.ini_options] +asyncio_mode = "auto" +testpaths = ["tests"] diff --git a/host-code/relay/__init__.py b/host-code/relay/__init__.py new file mode 100644 index 0000000..32754a7 --- /dev/null +++ b/host-code/relay/__init__.py @@ -0,0 +1 @@ +"""Hainuo relay 0.1.0.""" diff --git a/host-code/relay/cli.py b/host-code/relay/cli.py new file mode 100644 index 0000000..fad7979 --- /dev/null +++ b/host-code/relay/cli.py @@ -0,0 +1,216 @@ +import argparse +import asyncio +import json +import os +import secrets +import ssl +import uuid +from datetime import datetime, timedelta, timezone +from pathlib import Path + +import httpx +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ed25519 +from cryptography.x509.oid import NameOID + +from .crypto import issue_ca, issue_device, load_private, pem_private + + +def write_new(path, data, private=False): + path.parent.mkdir(parents=True, exist_ok=True) + with path.open("xb") as f: + f.write(data if isinstance(data, bytes) else data.encode()) + if private and os.name != "nt": + path.chmod(0o600) + + +def init_project(root: Path, hostname: str): + if (root / ".env").exists(): + raise RuntimeError(".env exists; init never overwrites credentials") + ca_key, ca = issue_ca() + signer = ed25519.Ed25519PrivateKey.generate() + write_new(root / "operator/ca.key", pem_private(ca_key), True) + write_new(root / "runtime/trust/ca.pem", ca.public_bytes(serialization.Encoding.PEM)) + write_new(root / "runtime/connector/connector.key", pem_private(signer), True) + write_new(root / "runtime/trust/connector.pub", signer.public_key().public_bytes( + serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo)) + tls_key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, hostname)]) + now = datetime.now(timezone.utc) + tls = (x509.CertificateBuilder().subject_name(name).issuer_name(name).public_key(tls_key.public_key()) + .serial_number(x509.random_serial_number()).not_valid_before(now - timedelta(minutes=5)) + .not_valid_after(now + timedelta(days=30)) + .add_extension(x509.SubjectAlternativeName([x509.DNSName(hostname)]), critical=False) + .sign(tls_key, algorithm=None)) + write_new(root / "runtime/tls/server.key", pem_private(tls_key), True) + write_new(root / "runtime/tls/server.pem", tls.public_bytes(serialization.Encoding.PEM)) + redis_secret = secrets.token_hex(32) + db_secret = secrets.token_hex(32) + env = (f"HN_ENVIRONMENT=development\nHN_PUBLIC_HOST={hostname}\nHN_PUBLIC_URL=https://{hostname}\n" + f"HN_ROUTER_URL=wss://{hostname}/device/ws\nHN_ADMIN_SECRET={secrets.token_hex(32)}\n" + f"HN_INTERNAL_SECRET={secrets.token_hex(32)}\nHN_REDIS_PASSWORD={redis_secret}\n" + f"HN_REDIS_URL=redis://:{redis_secret}@redis:6379/0\n" + f"HN_POSTGRES_PASSWORD={db_secret}\nHN_DATABASE_URL=postgresql://hainuo:{db_secret}@postgres:5432/hainuo\n" + "HN_FEISHU_ENABLED=false\nHN_FEISHU_API_BASE=https://open.feishu.cn\n" + "HN_FEISHU_AUTHORIZE_URL=https://open.feishu.cn/open-apis/authen/v1/authorize\n" + "HN_FEISHU_APP_ID=\nHN_FEISHU_APP_SECRET=\nHN_FEISHU_ENCRYPT_KEY=\n" + "HN_FEISHU_VERIFICATION_TOKEN=\nHN_PROXY_ENABLED=false\nHN_DEVICE_DOMAIN=\n") + write_new(root / ".env", env, True) + print("已生成配置、连接器密钥、设备CA及临时TLS证书;未输出凭据。") + print("operator/ca.key 仅保留在签发工作站,不要上传到ECS。临时TLS证书上线前须替换。") + + +def device_request(directory): + host = uuid.uuid4().hex + private = ed25519.Ed25519PrivateKey.generate() + write_new(directory / "device.key", pem_private(private), True) + public = private.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode() + write_new(directory / "request.json", json.dumps({"host_id": host, "public_key": public}, indent=2)) + print("已在本机生成私钥和 request.json;仅将request.json交给签发工作站。") + + +def issue(root, request_file, output): + request = json.loads(request_file.read_text(encoding="utf-8")) + host = request["host_id"] + if len(host) != 32 or any(c not in "0123456789abcdef" for c in host): + raise ValueError("invalid host identity") + ca = x509.load_pem_x509_certificate((root / "runtime/trust/ca.pem").read_bytes()) + public = serialization.load_pem_public_key(request["public_key"].encode()) + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("Ed25519 required") + cert = issue_device(load_private(root / "operator/ca.key"), ca, host, public) + pem = cert.public_bytes(serialization.Encoding.PEM) + write_new(output / "device.pem", pem) + write_new(output / "ca.pem", (root / "runtime/trust/ca.pem").read_bytes()) + write_new(output / "connector.pub", (root / "runtime/trust/connector.pub").read_bytes()) + write_new(output / "device.json", json.dumps({"host_id": host, "owner": None}, indent=2)) + write_new(output / "registration.json", json.dumps({"host_id": host, "cert": pem.decode()}, indent=2)) + print("已签发设备证书;将device.pem、ca.pem、connector.pub、device.json交回原设备。") + + +def main(): + parser = argparse.ArgumentParser(description="海诺中继部署和设备工具") + sub = parser.add_subparsers(dest="command", required=True) + p = sub.add_parser("init") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--hostname", required=True) + p = sub.add_parser("serve") + p.add_argument("role", choices=["connector", "router"]) + p.add_argument("--host", default="127.0.0.1") + p.add_argument("--port", type=int, default=8000) + p = sub.add_parser("device-request") + p.add_argument("--directory", type=Path, required=True) + p = sub.add_parser("issue-device") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--request", type=Path, required=True) + p.add_argument("--output", type=Path, required=True) + p = sub.add_parser("register") + p.add_argument("--registration", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("bind") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("device") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p.add_argument("--test-root", type=Path) + p.add_argument("--agent-url", help="Explicit loopback read-only Agent adapter endpoint") + p.add_argument("--hainuo-config", type=Path, help="Native installer-1.065 API adapter config; alternative to --agent-url") + p = sub.add_parser("hainuo-probe") + p.add_argument("--config", type=Path, required=True) + p = sub.add_parser("web-pair") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + for command in ("status", "binding-code", "dispatch", "revoke", "unbind", "reconcile"): + p = sub.add_parser(command) + p.add_argument("--url", default="http://127.0.0.1:18081") + p.add_argument("--tls-ca", type=Path) + if command in {"binding-code", "dispatch"}: + p.add_argument("--owner", required=True) + if command == "dispatch": + p.add_argument("--text", required=True) + p.add_argument("--turn-id", default=None) + if command in {"revoke", "unbind"}: + p.add_argument("--host-id", required=True) + args = parser.parse_args() + if args.command == "init": + init_project(args.root, args.hostname) + elif args.command == "serve": + import uvicorn + uvicorn.run(f"relay.{args.role}:create_app", factory=True, host=args.host, port=args.port, + workers=1, access_log=False, proxy_headers=False, + ws_max_size=262144, ws_max_queue=16, log_level="warning") + elif args.command == "device-request": + device_request(args.directory) + elif args.command == "issue-device": + issue(args.root, args.request, args.output) + elif args.command == "register": + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, follow_redirects=False, timeout=15) as client: + response = client.post(args.url.rstrip("/") + "/admin/devices", + headers={"X-Admin-Secret": secret}, + json=json.loads(args.registration.read_text(encoding="utf-8"))) + if response.status_code != 201: + raise RuntimeError(f"register failed: HTTP {response.status_code}") + print("设备登记成功。") + elif args.command == "bind": + import getpass + from .device import bind_device + code = getpass.getpass("Binding code: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + asyncio.run(bind_device(args.directory, args.url, code, tls_verify=context)) + print("绑定成功。") + elif args.command == "device": + from .device import run_device + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else None + asyncio.run(run_device(args.directory, args.url, root=args.test_root, ssl_context=context, + agent_url=args.agent_url, hainuo_config=args.hainuo_config)) + elif args.command == "hainuo-probe": + from .hainuo1065 import Hainuo1065Agent + async def probe(): + adapter = Hainuo1065Agent(args.config) + try: + print(json.dumps(await adapter.probe(), ensure_ascii=False, indent=2)) + finally: + await adapter.close() + asyncio.run(probe()) + elif args.command == "web-pair": + from .device import pair_browser + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + result = asyncio.run(pair_browser(args.directory, args.url, tls_verify=context)) + print("网页地址:" + result["url"]) + print("一次性网页配对码(120秒有效):" + result["code"]) + else: + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, timeout=200, follow_redirects=False) as client: + paths = {"status": "/admin/devices", "binding-code": "/admin/test/binding-code", + "dispatch": "/admin/test/dispatch", "reconcile": "/admin/reconcile"} + if args.command in {"revoke", "unbind"}: + if len(args.host_id) != 32 or any(c not in "0123456789abcdef" for c in args.host_id): + raise ValueError("invalid host identity") + path = f"/admin/devices/{args.host_id}/{args.command}" + else: + path = paths[args.command] + body = None + if args.command in {"binding-code", "dispatch"}: + body = {"open_id": args.owner, "text": getattr(args, "text", "binding")} + if getattr(args, "turn_id", None): + body["turn_id"] = args.turn_id + response = client.request("GET" if args.command == "status" else "POST", args.url.rstrip("/") + path, + headers={"X-Admin-Secret": secret}, json=body) + if response.status_code != 200: + raise RuntimeError(f"operation failed: HTTP {response.status_code}") + print(json.dumps(response.json(), ensure_ascii=False, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/host-code/relay/client_manage.py b/host-code/relay/client_manage.py new file mode 100644 index 0000000..6b543fe --- /dev/null +++ b/host-code/relay/client_manage.py @@ -0,0 +1,293 @@ +"""Per-user Windows connection companion. No platform/application secrets.""" +import argparse +import asyncio +import json +import os +import secrets +import socket +import ssl +import subprocess +import sys +import time +import uuid +from pathlib import Path + +import httpx +import psutil +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey +from .crypto import load_private, load_public, pem_private, raw_public, sign, verified, verify_device +from .windows_keys import protect + +BASE = Path(__file__).resolve().parent.parent +URL = "https://10.77.76.13" + + +def save(path, value): + temp = path.with_suffix(path.suffix + ".tmp") + temp.write_text(json.dumps(value, ensure_ascii=False, indent=2), encoding="utf-8") + os.replace(temp, path) + + +def secure_directory(directory): + directory.mkdir(parents=True, exist_ok=True) + # SID avoids localized account names. Fail closed if ACL protection fails. + result = subprocess.run(["whoami", "/user", "/fo", "csv", "/nh"], capture_output=True, text=True, check=True) + import csv + sid = list(csv.reader([result.stdout.strip()]))[0][1] + subprocess.run(["icacls", str(directory), "/inheritance:r", "/grant:r", f"*{sid}:(OI)(CI)F", "*S-1-5-18:(OI)(CI)F"], + capture_output=True, check=True, creationflags=subprocess.CREATE_NO_WINDOW) + + +def prepare(directory): + secure_directory(directory) + for name in ("server.pem", "ca.pem", "connector.pub"): + target = directory / name + if not target.exists(): + target.write_bytes((BASE / "trust" / name).read_bytes()) + if not (directory / "device.json").exists(): + if (directory / "device.key.dpapi").exists() or (directory / "device.key").exists(): + raise RuntimeError("发现不完整的设备身份,请保留目录并联系管理员恢复。") + private = Ed25519PrivateKey.generate() + (directory / "device.key.dpapi").write_bytes(protect(pem_private(private))) + save(directory / "device.json", {"host_id": uuid.uuid4().hex, "owner": None}) + + +def tls(directory): + return ssl.create_default_context(cafile=str(directory / "server.pem")) + + +def import_existing(directory, source): + """Explicit migration; only public trust and this PC's identity, never app keys.""" + if source.resolve() == directory.resolve(): + raise ValueError("请选择旧设备目录。") + if any((directory / name).exists() for name in ("device.json", "device.key.dpapi", "device.key")): + raise RuntimeError("本目录已有设备身份。请使用已有身份继续,不能用导入覆盖。") + config = json.loads((source / "device.json").read_text(encoding="utf-8-sig")) + for name in ("server.pem", "ca.pem", "connector.pub"): + if (source / name).read_bytes().strip() != (BASE / "trust" / name).read_bytes().strip(): + raise ValueError("旧设备的服务信任与本安装包不符,请联系管理员。") + cert = (source / "device.pem").read_text(encoding="utf-8") + public = verify_device(cert, (source / "ca.pem").read_bytes(), config["host_id"]) + private = load_private(source / "device.key") + if raw_public(public) != raw_public(private.public_key()): + raise ValueError("设备证书与本机私钥不匹配。") + secure_directory(directory) + for name in ("device.pem", "server.pem", "ca.pem", "connector.pub"): + (directory / name).write_bytes((source / name).read_bytes()) + (directory / "device.key.dpapi").write_bytes(protect(pem_private(private))) + save(directory / "device.json", config) + # Preserve local replay journal using SQLite's consistent backup API. + if (source / "journal.sqlite").is_file(): + import sqlite3 + with sqlite3.connect(source / "journal.sqlite") as original, sqlite3.connect(directory / "journal.sqlite") as target: + original.backup(target) + return {"message": "旧设备导入成功,Work绑定已保留。请先停止旧PowerShell桥接窗口,再点击启动连接。"} + + +def enrollment(directory): + prepare(directory) + cfg = json.loads((directory / "device.json").read_text()) + if (directory / "device.pem").exists(): + verify_device((directory / "device.pem").read_text(), (directory / "ca.pem").read_bytes(), cfg["host_id"]) + return {"message": "设备已注册,可在Work获取绑定码后绑定。已有绑定可直接启动连接。"} + with httpx.Client(verify=tls(directory), trust_env=False, timeout=15) as http: + key = load_private(directory / "device.key") + proof = sign({"host_id": cfg["host_id"], "purpose": "device-enroll-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + response = http.post(URL + "/device/register", json={"host_id": cfg["host_id"], "proof": proof, + "public_key": key.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode()}) + if response.status_code not in {200, 201}: + raise RuntimeError("自动注册失败:HTTP " + str(response.status_code) + ";请确认ECS已安装1.067自动注册更新。") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt.get("status") != "registered" or receipt["host_id"] != cfg["host_id"]: + raise ValueError("注册回执身份不符") + public = verify_device(receipt["cert"], (directory / "ca.pem").read_bytes(), cfg["host_id"]) + if raw_public(public) != raw_public(key.public_key()): + raise ValueError("注册证书公钥不符") + temp = directory / "device.pem.tmp" + temp.write_text(receipt["cert"], encoding="utf-8") + os.replace(temp, directory / "device.pem") + (directory / "enrollment.json").unlink(missing_ok=True) + return {"message": "设备已自动注册,无需管理员审批。请在Work获取绑定码,填写后点击绑定并连接。"} + + +def managed_processes(directory): + """Only processes with our absolute entry path AND exact device directory.""" + state = directory / "processes.json" + if not state.exists(): + return [] + result = [] + for item in json.loads(state.read_text()).get("processes", []): + try: + proc = psutil.Process(item["pid"]) + command = proc.cmdline() + if (abs(proc.create_time() - item["created"]) < 0.1 and str(BASE / "entry.py") in command + and str(directory) in command): + result.append(proc) + except psutil.Error: + pass + return result + + +def stop(directory): + procs = managed_processes(directory) + # Supervisor owns and terminates only its own child handles in its finally block. + (directory / "stop.flag").touch() + _, alive = psutil.wait_procs(procs, timeout=8) + for proc in alive: + proc.terminate() + return {"message": "已停止本安装包启动的连接进程。手工启动的旧桥接请在原窗口按Ctrl+C停止。"} + + +def start(directory, root): + from .process_lock import DeviceProcessLock + with DeviceProcessLock(directory / "control"): + return _start(directory, root) + + +def _start(directory, root): + cfg = json.loads((directory / "device.json").read_text()) + if not cfg.get("owner") or not (directory / "device.pem").exists(): + raise RuntimeError("请先完成设备准入和Work绑定。") + if managed_processes(directory): + return {"message": "连接进程已运行,请点击状态查看。"} + (directory / "stop.flag").unlink(missing_ok=True) + log = (directory / "bridge.log").open("a", encoding="utf-8") + proc = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "supervise", "--directory", str(directory), "--root", str(root)], + stdout=log, stderr=log, creationflags=subprocess.CREATE_NO_WINDOW) + log.close() + save(directory / "processes.json", {"processes": [{"pid": proc.pid, "created": psutil.Process(proc.pid).create_time()}]}) + return {"message": "连接进程已启动;约30秒后点击状态,再到Work发送 /主机名 验证。"} + + +def supervise(directory, root): + from .process_lock import DeviceProcessLock + lockdir = directory / "supervisor" + with DeviceProcessLock(lockdir): + children = [] + def record_children(): + processes = [psutil.Process(os.getpid())] + [psutil.Process(p.pid) for p in children if p.poll() is None] + save(directory / "processes.json", {"processes": [{"pid": p.pid, "created": p.create_time()} for p in processes]}) + try: + # Check local bind availability without an HTTP connection. Windows + # filtering can time out connections to a closed port rather than + # refuse them; that must not prevent starting our own listener. + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as port_probe: + try: + if os.name == "nt": + port_probe.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1) + port_probe.bind(("127.0.0.1", 18643)) + except OSError: + raise RuntimeError("原生API端口18643被占用或不允许绑定,请先检查旧连接。") from None + native = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "native", "--directory", str(directory), "--root", str(root)]) + children.append(native) + record_children() + for _ in range(90): + if (directory / "stop.flag").exists(): + return + if native.poll() is not None: + raise RuntimeError("本机原生API启动失败,请检查安装配置。") + try: + from .hainuo1065 import Hainuo1065Agent + async def probe(): + agent = Hainuo1065Agent(directory / "hainuo-native.json") + try: + return await agent.probe() + finally: + await agent.close() + asyncio.run(probe()) + break + except Exception: + time.sleep(1) + else: + raise RuntimeError("本机原生API未就绪。") + bridge = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "bridge", "--directory", str(directory), "--root", str(root)]) + children.append(bridge) + record_children() + while not (directory / "stop.flag").exists(): + if any(proc.poll() is not None for proc in children): + raise RuntimeError("连接子进程已退出,请查看状态后重新启动。") + time.sleep(0.5) + finally: + for proc in reversed(children): + if proc.poll() is None: + proc.terminate() + try: + proc.wait(timeout=5) + except subprocess.TimeoutExpired: + proc.kill() + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("action", choices=["authorize", "import", "enroll", "bind", "start", "stop", "status", "pair", "supervise", "native", "bridge"]) + parser.add_argument("--directory", type=Path, required=True) + parser.add_argument("--root", type=Path, required=True) + args = parser.parse_args() + directory, root = args.directory.resolve(), args.root.resolve() + directory.mkdir(parents=True, exist_ok=True) + try: + if args.action == "authorize": + from .work_client import WorkClient + client = WorkClient(directory) + try: + result = client.status() + result["message"] += ("\n" + result["authorization_url"]) if result.get("authorization_url") else "" + finally: + client.close() + elif args.action == "import": + result = import_existing(directory, Path(sys.stdin.readline().strip())) + elif args.action == "enroll": + result = enrollment(directory) + elif args.action == "bind": + from .device import bind_device + code = sys.stdin.readline().strip() + if not code: + raise ValueError("请填写Work机器人新生成的绑定码。") + enrollment(directory) + stop(directory) + asyncio.run(bind_device(directory, URL, code, tls_verify=tls(directory))) + result = start(directory, root) + result["message"] = "Work绑定成功。" + result["message"] + elif args.action == "start": + result = start(directory, root) + elif args.action == "stop": + result = stop(directory) + elif args.action == "pair": + from .device import pair_browser + receipt = asyncio.run(pair_browser(directory, URL, tls_verify=tls(directory))) + result = {"message": "网页:" + receipt["url"] + "\n一次性配对码(120秒):" + receipt["code"]} + elif args.action == "status": + cfg = json.loads((directory / "device.json").read_text()) if (directory / "device.json").exists() else {} + log = directory / "bridge.log" + tail = log.read_bytes()[-6000:].decode("utf-8", errors="replace") if log.exists() else "" + result = {"message": "设备:" + cfg.get("host_id", "未申请") + "\nWork身份:" + (cfg.get("owner") or "未绑定") + + "\n连接进程:" + ("运行中(请在Work发送 /主机名 确认当前链路)" if managed_processes(directory) else "未运行") + "\n" + tail[-2500:]} + elif args.action == "supervise": + return supervise(directory, root) + elif args.action == "native": + os.environ["HN_WORK_DEVICE_DIR"] = str(directory) + import importlib.util + spec = importlib.util.spec_from_file_location("native_companion", BASE / "hainuo_native_api.py") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + module.enable_profile_tools(root, "hainuo") + key = module.prepare_environment(root, "hainuo", 18643, directory / "hainuo-native.json", enable_tools=True) + import logging + logging.disable(logging.CRITICAL) + return asyncio.run(module.serve_native(18643, key, enable_tools=True)) + else: + from .device import run_device + return asyncio.run(run_device(directory, "wss://10.77.76.13/device/ws", ssl_context=tls(directory), hainuo_config=directory / "hainuo-native.json")) + print(json.dumps(result, ensure_ascii=False)) + except KeyboardInterrupt: + pass + except Exception as error: + # Native exception bodies may include URLs or secrets; emit bounded known messages only. + message = str(error) if isinstance(error, (RuntimeError, ValueError)) else type(error).__name__ + ";请检查网络、证书、安装目录。" + print(json.dumps({"message": message[:600], "error": True}, ensure_ascii=False)) + raise SystemExit(1) from None + + +if __name__ == "__main__": + main() diff --git a/host-code/relay/common.py b/host-code/relay/common.py new file mode 100644 index 0000000..c112b99 --- /dev/null +++ b/host-code/relay/common.py @@ -0,0 +1,61 @@ +import hashlib +import hmac +import json +import time +from fastapi import HTTPException, Request +from starlette.responses import JSONResponse + + +def require_secret(actual, expected): + if not actual or not hmac.compare_digest(actual, expected): + raise HTTPException(401, "unauthorized") + + +async def rate_limit(redis, bucket: str, limit=60, seconds=60): + # Atomic counter + expiry: a crash cannot leave a permanent bucket. + key = "hn:rate:" + hashlib.sha256(bucket.encode()).hexdigest() + ":" + str(int(time.time()) // seconds) + pipe = redis.pipeline(transaction=True) + pipe.incr(key) + pipe.expire(key, seconds * 2) + result = (await pipe.execute())[0] + if int(result) > limit: + raise HTTPException(429, "rate limited", headers={"Retry-After": str(seconds)}) + + +class BodyLimitMiddleware: + def __init__(self, app, max_body=262144): + self.app, self.max_body = app, max_body + + async def __call__(self, scope, receive, send): + if scope["type"] != "http": + return await self.app(scope, receive, send) + messages, size = [], 0 + while True: + message = await receive() + if message["type"] == "http.disconnect": + return + size += len(message.get("body", b"")) + if size > self.max_body: + return await JSONResponse({"detail": "body too large"}, status_code=413)(scope, receive, send) + messages.append(message) + if not message.get("more_body", False): + break + async def buffered(): + return messages.pop(0) if messages else await receive() + await self.app(scope, buffered, send) + + +def no_cache(response): + response.headers["Cache-Control"] = "no-store" + response.headers["X-Content-Type-Options"] = "nosniff" + response.headers["Referrer-Policy"] = "no-referrer" + return response + + +async def json_pub(redis, channel, data): + return await redis.publish(channel, json.dumps(data, separators=(",", ":"))) + + +def peer(request: Request): + # Do not trust arbitrary X-Forwarded-For; configure uvicorn trusted proxies explicitly. + return request.client.host if request.client else "unknown" diff --git a/host-code/relay/config.py b/host-code/relay/config.py new file mode 100644 index 0000000..c4c32ac --- /dev/null +++ b/host-code/relay/config.py @@ -0,0 +1,66 @@ +from pathlib import Path +from pydantic import model_validator +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + model_config = SettingsConfigDict(env_prefix="HN_", extra="ignore", hide_input_in_errors=True) + environment: str = "production" + role: str = "connector" + redis_url: str = "redis://127.0.0.1:6379/0" + data_dir: Path = Path("runtime/connector") + database_url: str = "" + admin_secret: str = "" + internal_secret: str = "" + ca_cert: Path = Path("runtime/pki/ca.pem") + connector_key: Path = Path("runtime/pki/connector.key") + connector_public_key: Path = Path("runtime/pki/connector.pub") + public_url: str = "https://relay.example.invalid" + router_url: str = "wss://relay.example.invalid/device/ws" + device_domain: str = "" + feishu_api_base: str = "https://open.feishu.cn" + feishu_authorize_url: str = "https://open.feishu.cn/open-apis/authen/v1/authorize" + feishu_oauth_scopes: str = "" + feishu_app_id: str = "" + feishu_app_secret: str = "" + feishu_encrypt_key: str = "" + feishu_verification_token: str = "" + feishu_enabled: bool = False + oauth_enabled: bool = True + feishu_event_source: str = "longconn" + proxy_enabled: bool = False + # A fixed allowlist, never a user supplied upstream URL. + proxy_paths: str = "/open-apis/drive/v1/files,/open-apis/docx/v1/documents" + request_timeout: int = 45 + max_body: int = 262144 + max_pending: int = 1000 + max_devices: int = 1000 + max_connections: int = 1000 + max_result: int = 16000 + allow_insecure_local: bool = False + enrollment_signer_url: str = "" + + @model_validator(mode="after") + def validate_runtime(self): + if self.environment not in {"production", "test", "development"}: + raise ValueError("invalid environment") + if self.role == "connector" and min(len(self.admin_secret), len(self.internal_secret)) < 32: + raise ValueError("admin/internal secrets must each be at least 32 characters") + if self.role == "longconn" and len(self.internal_secret) < 32: + raise ValueError("internal credential must be at least 32 characters") + if self.role == "connector" and self.admin_secret == self.internal_secret: + raise ValueError("admin and internal credentials must be different") + if self.environment == "production": + if self.allow_insecure_local or not self.public_url.startswith("https://"): + raise ValueError("production requires HTTPS") + if not self.router_url.startswith("wss://"): + raise ValueError("production requires WSS") + if not self.feishu_api_base.startswith("https://"): + raise ValueError("platform API requires HTTPS") + if self.feishu_event_source not in {"longconn", "webhook"}: + raise ValueError("invalid platform event source") + if self.feishu_enabled and not all((self.feishu_app_id, self.feishu_app_secret)): + raise ValueError("platform app ID and secret are required") + if self.feishu_enabled and self.feishu_event_source == "webhook" and not all((self.feishu_encrypt_key, self.feishu_verification_token)): + raise ValueError("webhook requires encryption key and verification token") + return self diff --git a/host-code/relay/connector.py b/host-code/relay/connector.py new file mode 100644 index 0000000..c01f3b6 --- /dev/null +++ b/host-code/relay/connector.py @@ -0,0 +1,506 @@ +import asyncio +import base64 +import contextlib +import hashlib +import hmac +import json +import re +import secrets +import sqlite3 +import time +import uuid +from contextlib import asynccontextmanager +from dataclasses import dataclass +from urllib.parse import unquote, urlsplit + +from cryptography.hazmat.primitives import hashes, padding +from cryptography.hazmat.primitives.asymmetric import x25519 +from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes +from fastapi import FastAPI, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from fastapi.exceptions import RequestValidationError +from pydantic import BaseModel, Field +from redis.asyncio import Redis + +from .common import BodyLimitMiddleware, no_cache, peer, rate_limit, require_secret +from .config import Settings +from .crypto import (fingerprint, load_private, open_envelope, raw_public, seal, + sign, verified, verify_device) +from .oauth import OAuth +from .platform import Feishu, PlatformError +from .store import Store, INTEGRITY_ERRORS + + +class Register(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + cert: str = Field(max_length=10000) + + +class Bind(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Instruction(BaseModel): + open_id: str = Field(min_length=1, max_length=128) + text: str = Field(min_length=1, max_length=8000) + # Stable identity chosen by upstream, not generated again on retries. + turn_id: str = Field(default_factory=lambda: uuid.uuid4().hex, pattern=r"^[A-Za-z0-9_-]{1,128}$") + + +@dataclass +class Pending: + future: asyncio.Future + private: object + public: object + host: str + session: str + + +class Broker: + def __init__(self, settings, redis, store): + self.s, self.r, self.store = settings, redis, store + self.signer = load_private(settings.connector_key) + self.pending: dict[str, Pending] = {} + + async def replies(self, pub): + async for message in pub.listen(): + if message["type"] != "message": + continue + try: + data = json.loads(message["data"]) + item = self.pending.get(data["turn_id"]) + if not item or item.future.done(): + continue + plain = open_envelope(data["envelope"], item.private, item.public, + host_id=item.host, session_id=item.session) + if plain["turn_id"] != data["turn_id"] or plain.get("type") != "result": + continue + result = plain.get("result", "") + if not isinstance(result, str) or len(result) > self.s.max_result: + continue + item.future.set_result({"status": "completed", "turn_id": data["turn_id"], "result": result}) + except Exception: + # Invalid packets never consume a pending result; do not log packet contents. + continue + + async def dispatch(self, owner, text, turn): + device = self.store.by_owner(owner) + if not device: + return {"status": "unavailable", "turn_id": turn, "result": "未绑定设备或设备不可用。"} + host = device["host_id"] + await rate_limit(self.r, "dispatch:" + owner, 30) + if len(self.pending) >= self.s.max_pending: + raise HTTPException(503, "too many in-flight requests") + presence = await self.r.get("hn:online:" + host) + if not presence or await self.r.exists("hn:revoked:" + host): + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线,请开机并连接网络。"} + try: + presence = json.loads(presence) + if fingerprint(presence["cert"]) != device["fingerprint"]: + raise ValueError("certificate changed") + public = verify_device(presence["cert"], self.s.ca_cert.read_bytes(), host) + proof = verified(presence["proof"], public) + if proof["host_id"] != host or proof["challenge"] != presence["challenge"]: + raise ValueError("invalid device session") + session = proof["session_id"] + except Exception: + self.store.audit(owner, "dispatch", host, "identity_rejected") + raise HTTPException(409, "device identity rejected") from None + if not self.store.begin_turn(turn, owner, host): + return {"status": "duplicate", "turn_id": turn, "result": "该请求已接收,不会重复执行。"} + private = x25519.X25519PrivateKey.generate() + future = asyncio.get_running_loop().create_future() + self.pending[turn] = Pending(future, private, public, host, session) + envelope = seal({"type": "instruction", "turn_id": turn, "owner": owner, + "text": text, "read_only": True, "channel": "im", + "reply_key": raw_public(private.public_key())}, proof["kx"], self.signer, + host_id=host, session_id=session) + key = f"hn:inflight:{host}:{turn}" + try: + await self.r.set(key, session, ex=self.s.request_timeout + 5) + count = await self.r.publish("hn:down:" + host, json.dumps({"type": "instruction", "envelope": envelope})) + if not count: + self.store.finish(turn, "offline") + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线。"} + result = await asyncio.wait_for(future, timeout=self.s.request_timeout) + self.store.finish(turn, "completed") + self.store.audit(owner, "instruction", host, "completed") + return result + except asyncio.TimeoutError: + self.store.finish(turn, "unknown") + return {"status": "unknown", "turn_id": turn, + "result": "等待结果超时,执行状态未知;不要直接重发有副作用的任务。"} + finally: + self.pending.pop(turn, None) + await self.r.delete(key) + + +def create_app(settings=None, redis=None, platform=None): + s = settings or Settings() + r = redis or Redis.from_url(s.redis_url, decode_responses=True) + store = Store(s.data_dir / "metadata.sqlite", s.database_url) + f = platform or Feishu(s) + broker = Broker(s, r, store) + oauth = OAuth(s, r, f) + oauth.store = store + background = set() + + def spawn(coro): + task = asyncio.create_task(coro) + background.add(task) + task.add_done_callback(background.discard) + # Retrieve errors without displaying possibly sensitive exception text. + task.add_done_callback(lambda t: t.exception() if not t.cancelled() else None) + + @asynccontextmanager + async def lifespan(app): + await r.ping() + for row in store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall(): + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + pub = r.pubsub() + await pub.subscribe("hn:replies") + reply_task = asyncio.create_task(broker.replies(pub)) + app.state.reply_task = reply_task + async def sweep(): + while True: + await asyncio.sleep(30) + async with oauth.lock: + oauth.prune() + sweep_task = asyncio.create_task(sweep()) + try: + yield + finally: + for task in [reply_task, sweep_task, *background]: + task.cancel() + await asyncio.gather(reply_task, sweep_task, *background, return_exceptions=True) + await pub.aclose() + await f.close() + store.close() + if redis is None: + await r.aclose() + + app = FastAPI(title="Hainuo B1 connector", lifespan=lifespan, docs_url=None, redoc_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=s.max_body) + app.include_router(oauth.router) + from .web_session import WebSessions + web_sessions = WebSessions(s, r, store, broker, f) + app.include_router(web_sessions.router) + app.state.store, app.state.broker, app.state.oauth = store, broker, oauth + app.state.platform, app.state.redis = f, r + + @app.exception_handler(RequestValidationError) + async def validation_error(request, exc): + # FastAPI's default error response includes invalid input values (possibly tokens). + return JSONResponse({"detail": "invalid request parameters"}, status_code=422) + + @app.middleware("http") + async def secure_response(request, call_next): + return no_cache(await call_next(request)) + + def admin(request): + require_secret(request.headers.get("X-Admin-Secret"), s.admin_secret) + + from .enrollment import enrollment_router + app.include_router(enrollment_router(s, r, store, broker.signer, admin)) + from .work_api import router as work_router + app.include_router(work_router(s, r, store, f)) + + @app.get("/health") + async def health(): + return {"status": "alive", "role": "connector", "version": "0.1.0"} + + @app.get("/ready") + async def ready(): + await r.ping() + store.db.execute("SELECT 1") + if not getattr(app.state, "reply_task", None) or app.state.reply_task.done(): + raise HTTPException(503, "reply subscriber unavailable") + return {"status": "ready"} + + @app.post("/admin/devices", status_code=201) + async def register(body: Register, request: Request): + admin(request) + try: + verify_device(body.cert, s.ca_cert.read_bytes(), body.host_id) + except Exception: + raise HTTPException(400, "invalid device certificate") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= s.max_devices: + raise HTTPException(503, "device capacity reached") + try: + fp = fingerprint(body.cert) + store.add_device(body.host_id, body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "device already exists") from None + await r.set("hn:device:" + body.host_id, fp) + store.audit("admin", "register", body.host_id, "success") + return {"host_id": body.host_id, "fingerprint": fp} + + @app.get("/admin/devices") + async def devices(request: Request): + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,owner,revoked FROM devices ORDER BY host_id").fetchall() + return {"devices": [{**dict(row), "online": bool(await r.exists("hn:online:" + row["host_id"]))} for row in rows]} + + @app.post("/admin/devices/{host}/revoke") + async def revoke(host: str, request: Request): + admin(request) + if not store.device(host): + raise HTTPException(404, "device not found") + # Persistent source of truth first; router block + disconnect follows. + store.revoke(host) + await r.incr("hn:web-generation:" + host) + await r.set("hn:revoked:" + host, "1") + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "revoke") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "revoked", "result": "设备已停用,结果已丢弃。"}) + store.audit("admin", "revoke", host, "success") + return {"status": "revoked"} + + @app.post("/admin/reconcile") + async def reconcile(request: Request): + """Rebuild router admission metadata after Redis recovery; no dialogue data.""" + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall() + for row in rows: + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + return {"devices": len(rows)} + + @app.post("/admin/devices/{host}/unbind") + async def admin_unbind(host: str, request: Request): + admin(request) + device = store.device(host) + if not device: + raise HTTPException(404, "device not found") + if device["owner"]: + store.unbind(device["owner"]) + await r.incr("hn:web-generation:" + host) + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "unbind") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + store.audit("admin", "unbind", host, "success") + return {"status": "unbound", "host_id": host, "revoked": bool(device["revoked"])} + + async def make_binding_code(owner): + if store.by_owner(owner): + return "你已绑定电脑,请先发送 /解绑。" + await rate_limit(r, "binding:" + owner, 5) + code = secrets.token_urlsafe(12) + await r.set("hn:bind:" + hashlib.sha256(code.encode()).hexdigest(), owner, ex=300) + return "请在本人电脑的桥接客户端输入绑定码(5分钟有效):" + code + + @app.post("/device/bind") + async def bind(body: Bind, request: Request): + await rate_limit(r, "bind:" + peer(request), 20) + device = store.device(body.host_id) + if not device or device["revoked"]: + raise HTTPException(404, "device unavailable") + try: + public = verify_device(device["cert"], s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, public) + if proof["host_id"] != body.host_id or abs(time.time() - proof["ts"]) > 90: + raise ValueError("invalid proof") + code = proof["code"] + if not isinstance(code, str) or len(code) > 128: + raise ValueError("invalid code") + except Exception: + raise HTTPException(403, "invalid device proof") from None + key = "hn:bind:" + hashlib.sha256(code.encode()).hexdigest() + owner = await r.get(key) + if not owner: + raise HTTPException(404, "binding code expired or consumed") + # GETDEL is atomic across connectors. Invalid device proofs never reach this point. + owner = await r.getdel(key) + if not owner: + raise HTTPException(404, "binding code consumed") + try: + store.bind(body.host_id, owner) + except (*INTEGRITY_ERRORS, ValueError): + raise HTTPException(409, "owner/device already bound; request new code") from None + await r.incr("hn:web-generation:" + body.host_id) + store.audit(owner, "bind", body.host_id, "success") + return sign({"host_id": body.host_id, "owner": owner, "ts": int(time.time())}, broker.signer) + + @app.post("/admin/test/binding-code") + async def test_code(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return {"message": await make_binding_code(body.open_id)} + + @app.post("/admin/test/dispatch") + async def test_dispatch(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return await broker.dispatch(body.open_id, body.text, body.turn_id) + + async def handle_event(data): + header, event = data.get("header", {}), data.get("event", {}) + if header.get("event_type") != "im.message.receive_v1": + return + message, sender = event.get("message", {}), event.get("sender", {}) + owner = sender.get("sender_id", {}).get("open_id") + message_id = message.get("message_id", "") + if (message.get("chat_type") != "p2p" or message.get("message_type") != "text" + or sender.get("sender_type") != "user" or not owner + or not re.fullmatch(r"[A-Za-z0-9_-]{1,128}", message_id)): + return + if header.get("app_id") != s.feishu_app_id: + return + try: + text = json.loads(message["content"])["text"].strip() + if not text or len(text) > 8000: + return + # Persistent receipt BEFORE any side effect. Restarts do not execute a delivery twice. + event_turn = "event_" + hashlib.sha256(message_id.encode()).hexdigest() + if not store.begin_turn(event_turn, owner, "event"): + return + if text == "/绑定": + result = await make_binding_code(owner) + elif text == "/解绑": + old = store.by_owner(owner) + store.unbind(owner) + if old: + await r.incr("hn:web-generation:" + old["host_id"]) + await r.publish("hn:kill:" + old["host_id"], "unbind") + for item in broker.pending.values(): + if item.host == old["host_id"] and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + result = "已解绑;重新绑定后请更新本机绑定信息。" + elif text == "/主机名" or text.startswith("/回显 "): + result = (await broker.dispatch(owner, text, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + else: + instruction = text if text.startswith("/助手 ") else "/助手 " + text + result = (await broker.dispatch(owner, instruction, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + await f.reply(message_id, result[:s.max_result]) + store.finish(event_turn, "replied") + except Exception: + store.audit(owner, "event", message_id, "failed_or_unknown") + + @app.post("/internal/events") + async def internal_event(request: Request): + require_secret(request.headers.get("X-Internal-Secret"), s.internal_secret) + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(await request.json())) + return {"accepted": True} + + @app.post("/feishu/event") + async def webhook(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if not s.feishu_encrypt_key or not s.feishu_verification_token: + raise HTTPException(503, "webhook credentials not configured; use selected event source") + await rate_limit(r, "webhook:" + peer(request), 600) + raw = await request.body() + timestamp, nonce, signature = (request.headers.get(x, "") for x in + ("X-Lark-Request-Timestamp", "X-Lark-Request-Nonce", "X-Lark-Signature")) + try: + if abs(time.time() - int(timestamp)) > 300 or not nonce or len(nonce) > 128: + raise ValueError("stale request") + expected = hashlib.sha256((timestamp + nonce + s.feishu_encrypt_key).encode() + raw).hexdigest() + if not hmac.compare_digest(expected, signature): + raise ValueError("bad signature") + data = json.loads(raw) + if "encrypt" in data: + cipher = base64.b64decode(data["encrypt"], validate=True) + decryptor = Cipher(algorithms.AES(hashlib.sha256(s.feishu_encrypt_key.encode()).digest()), + modes.CBC(cipher[:16])).decryptor() + padded = decryptor.update(cipher[16:]) + decryptor.finalize() + unpadder = padding.PKCS7(128).unpadder() + data = json.loads(unpadder.update(padded) + unpadder.finalize()) + token = data.get("token", data.get("header", {}).get("token", "")) + if not hmac.compare_digest(token, s.feishu_verification_token): + raise ValueError("invalid verification token") + except Exception: + raise HTTPException(403, "invalid event authentication") from None + if data.get("type") == "url_verification": + return {"challenge": data.get("challenge", "")} + # Same delivery is ACKed again; durable message_id receipt handles execution deduplication. + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(data)) + return {"code": 0} + + @app.get("/api/whoami") + async def whoami(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(r, "whoami:" + peer(request)) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await f.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "no device bound") + if not s.device_domain or not re.fullmatch(r"[a-zA-Z0-9.-]+", s.device_domain): + raise HTTPException(503, "device web domain not configured") + return {"host_id": device["host_id"], "url": f"https://h-{device['host_id']}.{s.device_domain}/", + "online": bool(await r.exists("hn:online:" + device["host_id"])), + "device_login_required": True} + + @app.get("/h5", response_class=HTMLResponse) + async def h5(): + from importlib.resources import files + return HTMLResponse(files("relay").joinpath("web/index.html").read_text(encoding="utf-8"), + headers={"Content-Security-Policy": "default-src 'self'; script-src 'self'; style-src 'self'; frame-ancestors 'self'; base-uri 'none'; form-action 'self'"}) + + @app.get("/assets/{name}") + async def asset(name: str): + from importlib.resources import files + from starlette.responses import Response + if name not in {"app.js", "style.css"}: + raise HTTPException(404, "not found") + return Response(files("relay").joinpath("web/" + name).read_bytes(), + media_type="text/javascript" if name.endswith(".js") else "text/css") + + @app.get("/proxy") + async def proxy(request: Request): + if not s.proxy_enabled or not s.feishu_enabled: + raise HTTPException(404, "not found") + auth = request.headers.get("Authorization", "") + target = request.headers.get("X-Relay-Target", "") + decoded = unquote(target) + parts = urlsplit(target) + allowed = s.proxy_paths.split(",") + if (not auth.startswith("Bearer ") or len(auth) > 8192 or len(target) > 4096 + or parts.scheme or parts.netloc or parts.fragment or target.startswith("//") + or any(x in decoded for x in ("..", "\\", "\r", "\n", "#", "%")) + or not any(parts.path == x or parts.path.startswith(x + "/") for x in allowed)): + raise HTTPException(403, "target not allowed") + try: + info = await f.user_info(auth[7:]) + await rate_limit(r, "proxy:" + info["open_id"], 60) + # Buffer bounded response; no disk/cache, redirects never followed. + async with f.http.stream("GET", target, headers={"Authorization": auth}) as response: + if response.status_code in {301, 302, 303, 307, 308}: + raise HTTPException(502, "upstream redirect refused") + chunks, size = [], 0 + async for chunk in response.aiter_bytes(): + size += len(chunk) + if size > 2 * 1024 * 1024: + raise HTTPException(502, "response too large") + chunks.append(chunk) + from starlette.responses import Response + store.audit(info["open_id"], "proxy", parts.path, str(response.status_code)) + return Response(b"".join(chunks), status_code=response.status_code, + media_type=response.headers.get("content-type", "application/json")) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + + return app diff --git a/host-code/relay/crypto.py b/host-code/relay/crypto.py new file mode 100644 index 0000000..dd962e5 --- /dev/null +++ b/host-code/relay/crypto.py @@ -0,0 +1,140 @@ +"""Signed ephemeral X25519 envelopes. The router never receives plaintext keys.""" +import base64 +import hashlib +import json +import secrets +import time +from datetime import datetime, timedelta, timezone + +from cryptography import x509 +from cryptography.hazmat.primitives import hashes, serialization +from cryptography.hazmat.primitives.asymmetric import ed25519, x25519 +from cryptography.hazmat.primitives.ciphers.aead import AESGCM +from cryptography.hazmat.primitives.kdf.hkdf import HKDF +from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID + + +def b64(data: bytes) -> str: + return base64.urlsafe_b64encode(data).decode() + + +def unb64(data: str) -> bytes: + return base64.b64decode(data.encode(), altchars=b"-_", validate=True) + + +def canonical(data: dict) -> bytes: + return json.dumps(data, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode() + + +def raw_public(key) -> str: + return b64(key.public_bytes(serialization.Encoding.Raw, serialization.PublicFormat.Raw)) + + +def pem_private(key) -> bytes: + return key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, + serialization.NoEncryption()) + + +def load_private(path): + protected = path.with_name(path.name + ".dpapi") + if protected.is_file(): + from .windows_keys import unprotect + data = unprotect(protected.read_bytes()) + else: + data = path.read_bytes() + return serialization.load_pem_private_key(data, password=None) + + +def load_public(path): + return serialization.load_pem_public_key(path.read_bytes()) + + +def fingerprint(pem: str) -> str: + return x509.load_pem_x509_certificate(pem.encode()).fingerprint(hashes.SHA256()).hex() + + +def verify_device(pem: str, ca_pem: bytes, host_id: str): + """v1 deliberately accepts only directly issued Ed25519 device certificates.""" + cert = x509.load_pem_x509_certificate(pem.encode()) + ca = x509.load_pem_x509_certificate(ca_pem) + now = datetime.now(timezone.utc) + if not (ca.not_valid_before_utc <= now <= ca.not_valid_after_utc): + raise ValueError("expired CA") + if not ca.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("not a CA") + cert.verify_directly_issued_by(ca) + if not (cert.not_valid_before_utc <= now <= cert.not_valid_after_utc): + raise ValueError("expired certificate") + if cert.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("CA cannot be used as device") + if ExtendedKeyUsageOID.CLIENT_AUTH not in cert.extensions.get_extension_for_class(x509.ExtendedKeyUsage).value: + raise ValueError("clientAuth missing") + names = cert.extensions.get_extension_for_class(x509.SubjectAlternativeName).value + if names.get_values_for_type(x509.UniformResourceIdentifier) != [f"urn:hainuo:host:{host_id}"]: + raise ValueError("host identity mismatch") + public = cert.public_key() + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("v1 requires Ed25519 device key") + return public + + +def sign(data: dict, key) -> dict: + return {**data, "signature": b64(key.sign(canonical(data)))} + + +def verified(data: dict, key) -> dict: + body = {k: v for k, v in data.items() if k != "signature"} + key.verify(unb64(data["signature"]), canonical(body)) + return body + + +def seal(payload: dict, recipient_public: str, signer, *, host_id: str, session_id: str) -> dict: + ephemeral = x25519.X25519PrivateKey.generate() + header = {"v": 1, "host_id": host_id, "session_id": session_id, + "ts": int(time.time()), "nonce": b64(secrets.token_bytes(12)), + "ephemeral": raw_public(ephemeral.public_key())} + shared = ephemeral.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(recipient_public))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + cipher = AESGCM(key).encrypt(unb64(header["nonce"]), canonical(payload), canonical(header)) + return sign({**header, "ciphertext": b64(cipher)}, signer) + + +def open_envelope(envelope: dict, recipient_private, sender_public, *, host_id: str, session_id: str) -> dict: + body = verified(envelope, sender_public) + if body["v"] != 1 or body["host_id"] != host_id or body["session_id"] != session_id: + raise ValueError("wrong destination/session") + if abs(time.time() - body["ts"]) > 90: + raise ValueError("expired envelope") + header = {k: v for k, v in body.items() if k != "ciphertext"} + shared = recipient_private.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(body["ephemeral"]))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + plain = AESGCM(key).decrypt(unb64(body["nonce"]), unb64(body["ciphertext"]), canonical(header)) + return json.loads(plain) + + +def issue_ca(): + key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Hainuo device test CA")]) + now = datetime.now(timezone.utc) + cert = (x509.CertificateBuilder().subject_name(name).issuer_name(name) + .public_key(key.public_key()).serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=3650)) + .add_extension(x509.BasicConstraints(ca=True, path_length=0), critical=True) + .add_extension(x509.KeyUsage(False, False, False, False, False, True, True, False, False), critical=True) + .sign(key, algorithm=None)) + return key, cert + + +def issue_device(ca_key, ca_cert, host_id: str, public_key, days=30): + now = datetime.now(timezone.utc) + return (x509.CertificateBuilder() + .subject_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, host_id)])) + .issuer_name(ca_cert.subject).public_key(public_key) + .serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=days)) + .add_extension(x509.BasicConstraints(ca=False, path_length=None), critical=True) + .add_extension(x509.SubjectAlternativeName([x509.UniformResourceIdentifier(f"urn:hainuo:host:{host_id}")]), critical=False) + .add_extension(x509.ExtendedKeyUsage([ExtendedKeyUsageOID.CLIENT_AUTH]), critical=False) + .sign(ca_key, algorithm=None)) diff --git a/host-code/relay/device.py b/host-code/relay/device.py new file mode 100644 index 0000000..a8fe010 --- /dev/null +++ b/host-code/relay/device.py @@ -0,0 +1,216 @@ +"""Reference PC bridge. No shell/PowerShell execution; local read-only diagnostics.""" +import asyncio +import contextlib +import json +import os +import secrets +import socket +import sqlite3 +import time +from pathlib import Path +from urllib.parse import urlsplit + +import httpx +from cryptography.hazmat.primitives.asymmetric import x25519 +from websockets.asyncio.client import connect + +from .crypto import (load_private, load_public, open_envelope, raw_public, seal, sign, + verified, verify_device) + + +class DeviceJournal: + def __init__(self, path): + path.parent.mkdir(parents=True, exist_ok=True) + self.db = sqlite3.connect(path) + self.db.execute("CREATE TABLE IF NOT EXISTS consumed(turn_id TEXT PRIMARY KEY, ts REAL NOT NULL)") + self.db.commit() + + def consume(self, turn): + with self.db: + return self.db.execute("INSERT OR IGNORE INTO consumed VALUES(?,?)", (turn, time.time())).rowcount == 1 + + def close(self): + self.db.close() + + +async def execute_readonly(text, root: Path | None): + if text in {"/主机名", "hostname"}: + return socket.gethostname() + if text.startswith("/回显 "): + return text[4:][:16000] + if text.startswith("/读取 "): + if root is None: + return "未配置测试文件目录。" + base = root.resolve(strict=True) + try: + path = (base / text[4:].strip()).resolve(strict=True) + if path == base or not path.is_relative_to(base) or not path.is_file(): + return "拒绝:文件不在允许的测试目录。" + if path.stat().st_size > 32768: + return "拒绝:测试文件超过32KB。" + return path.read_text(encoding="utf-8")[:16000] + except (OSError, UnicodeError, ValueError): + return "拒绝:文件不存在、不可读或不是UTF-8文本。" + return "当前桥接客户端仅开放只读诊断:/主机名、/回显 内容、/读取 相对路径;尚未接入海诺本地Agent接口。" + + +class LocalAgent: + """Explicit adapter contract, not a claim of compatibility with installer 1.065.""" + def __init__(self, url, transport=None): + parts = urlsplit(url) + if parts.scheme != "http" or parts.hostname not in {"127.0.0.1", "::1"} or parts.username or parts.password or parts.fragment: + raise ValueError("local Agent must use a fixed loopback HTTP endpoint") + self.url = url + self.http = httpx.AsyncClient(timeout=30, follow_redirects=False, transport=transport, trust_env=False) + + async def execute(self, text, owner, turn): + headers = {} + if os.environ.get("HN_LOCAL_AGENT_TOKEN"): + headers["Authorization"] = "Bearer " + os.environ["HN_LOCAL_AGENT_TOKEN"] + try: + async with self.http.stream("POST", self.url, headers=headers, + json={"text": text, "open_id": owner, "turn_id": turn, + "channel": "im", "read_only": True}) as response: + if response.status_code != 200: + return "本机Agent未返回成功结果,请查看本机服务状态。" + body = b"" + async for chunk in response.aiter_bytes(): + body += chunk + if len(body) > 65536: + return "本机Agent结果超过限制。" + result = json.loads(body)["result"] + if not isinstance(result, str): + return "本机Agent响应格式不匹配。" + return result[:16000] + except (httpx.HTTPError, ValueError, KeyError): + return "本机Agent连接失败或响应格式不匹配。" + + async def close(self): + await self.http.aclose() + + +async def bind_device(directory: Path, connector_url: str, code: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + private = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "code": code, "ts": int(time.time())}, private) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as http: + response = await http.post(connector_url.rstrip("/") + "/device/bind", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"binding failed: HTTP {response.status_code}") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt["host_id"] != config["host_id"]: + raise ValueError("wrong binding receipt") + config["owner"] = receipt["owner"] + temp = directory / "device.json.tmp" + temp.write_text(json.dumps(config, ensure_ascii=False, indent=2), encoding="utf-8") + temp.replace(directory / "device.json") + + +async def pair_browser(directory: Path, connector_url: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8-sig")) + if not config.get("owner"): + raise RuntimeError("device must be bound first") + key = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "owner": config["owner"], + "purpose": "h5-pair-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as client: + response = await client.post(connector_url.rstrip("/") + "/device/web-pair", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"browser pairing failed: HTTP {response.status_code}") + result = verified(response.json(), load_public(directory / "connector.pub")) + if result["host_id"] != config["host_id"]: + raise ValueError("wrong pairing receipt") + return result + + +async def run_device(directory: Path, router_url: str, **kwargs): + from .process_lock import DeviceProcessLock + with DeviceProcessLock(directory): + return await _run_device(directory, router_url, **kwargs) + + +async def _run_device(directory: Path, router_url: str, *, root=None, once=False, ssl_context=None, + stop_event=None, connected_event=None, agent_url=None, hainuo_config=None): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + if not config.get("owner"): + raise RuntimeError("bind device before starting") + host, owner = config["host_id"], config["owner"] + key = load_private(directory / "device.key") + connector = load_public(directory / "connector.pub") + cert = (directory / "device.pem").read_text(encoding="utf-8") + verify_device(cert, (directory / "ca.pem").read_bytes(), host) + journal = DeviceJournal(directory / "journal.sqlite") + if agent_url and hainuo_config: + raise ValueError("choose one Agent adapter") + if hainuo_config: + from .hainuo1065 import Hainuo1065Agent + agent = Hainuo1065Agent(hainuo_config) + else: + agent = LocalAgent(agent_url) if agent_url else None + stop_event = stop_event or asyncio.Event() + delay = 1 + try: + while not stop_event.is_set(): + try: + kwargs = {"ssl": ssl_context} if router_url.startswith("wss://") and ssl_context else {} + async with connect(router_url, max_size=262144, open_timeout=10, ping_interval=10, + proxy=None, **kwargs) as ws: + challenge = json.loads(await ws.recv())["challenge"] + ephemeral = x25519.X25519PrivateKey.generate() + session = secrets.token_hex(16) + proof = sign({"host_id": host, "session_id": session, "challenge": challenge, + "kx": raw_public(ephemeral.public_key()), "ts": int(time.time())}, key) + await ws.send(json.dumps({"host_id": host, "cert": cert, "proof": proof})) + accepted = json.loads(await ws.recv()) + if accepted.get("type") != "connected": + raise RuntimeError("device connection rejected") + print("设备连接成功,已绑定本人海能Work身份。", flush=True) + if connected_event: + connected_event.set() + delay = 1 + while not stop_event.is_set(): + try: + raw = await asyncio.wait_for(ws.recv(), timeout=1) + except asyncio.TimeoutError: + continue + data = json.loads(raw) + payload = open_envelope(data["envelope"], ephemeral, connector, + host_id=host, session_id=session) + if payload.get("owner") != owner or payload.get("type") != "instruction": + continue + turn = payload["turn_id"] + if not isinstance(turn, str) or len(turn) > 128: + continue + if not journal.consume(turn): + result = "该请求已处理,拒绝重复执行。" + elif payload.get("read_only") is not True or payload.get("channel") != "im": + result = "拒绝:通道或操作类型不受支持。" + else: + if agent and payload["text"].startswith("/助手 "): + result = await agent.execute(payload["text"][4:], owner, turn) + else: + result = await execute_readonly(payload["text"], root) + reply = seal({"type": "result", "turn_id": turn, "result": result}, + payload["reply_key"], key, host_id=host, session_id=session) + await ws.send(json.dumps({"type": "reply", "turn_id": turn, "envelope": reply})) + if once: + return + except asyncio.CancelledError: + raise + except Exception as error: + if once: + raise + from websockets.exceptions import ConnectionClosed + if isinstance(error, ConnectionClosed) and error.rcvd and error.rcvd.code == 1008: + print("设备被中继拒绝:请检查是否有另一桥接进程、设备准入或证书状态。", flush=True) + # No payloads/URLs/credentials in logs, bounded reconnect backoff + jitter. + print("设备连接中断,等待重连。", flush=True) + with contextlib.suppress(asyncio.TimeoutError): + await asyncio.wait_for(stop_event.wait(), timeout=delay + secrets.randbelow(1000) / 1000) + delay = min(delay * 2, 30) + finally: + journal.close() + if agent: + await agent.close() diff --git a/host-code/relay/enrollment.py b/host-code/relay/enrollment.py new file mode 100644 index 0000000..b4ac77f --- /dev/null +++ b/host-code/relay/enrollment.py @@ -0,0 +1,178 @@ +"""Device registration with key possession proof; Work binding is separate.""" +import json +import secrets +import time +import httpx + +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey +from fastapi import APIRouter, HTTPException, Request +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .crypto import verified, sign, raw_public, verify_device, fingerprint +from .web_session import digest +from fastapi.responses import JSONResponse + + +def validate_enrollment(body): + try: + key = serialization.load_pem_public_key(body.public_key.encode()) + if not isinstance(key, Ed25519PublicKey): + raise ValueError() + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["purpose"] != "device-enroll-v1" + or abs(time.time() - proof["ts"]) > 90 or len(proof["nonce"]) != 32): + raise ValueError() + return key + except Exception: + raise HTTPException(403, "invalid enrollment proof") from None + + +async def request_certificate(settings, body): + async with httpx.AsyncClient(trust_env=False, timeout=15, follow_redirects=False) as http: + response = await http.post(settings.enrollment_signer_url.rstrip("/") + "/issue", + headers={"X-Internal-Secret": settings.internal_secret}, + json=body.model_dump()) + response.raise_for_status() + return response.json()["cert"] + + +class EnrollmentRequest(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + public_key: str = Field(max_length=1024) + proof: dict + + +class EnrollmentApproval(BaseModel): + cert: str = Field(max_length=10000) + + +def enrollment_router(settings, redis, store, signer, admin): + router = APIRouter() + + @router.post("/device/register") + async def automatic_registration(body: EnrollmentRequest, request: Request): + # No human approval: eligibility follows intranet access plus Work app scope. + # Proof/TLS/limits protect identity and resources, and do not add user approval. + await rate_limit(redis, "auto-register-ip:" + peer(request), 120) + key = validate_enrollment(body) + await rate_limit(redis, "auto-register-host:" + body.host_id, 30) + existing = store.device(body.host_id) + + def check_existing(device): + if device["revoked"]: + raise HTTPException(403, "device revoked") + try: + registered = verify_device(device["cert"], settings.ca_cert.read_bytes(), body.host_id) + if raw_public(registered) != raw_public(key): + raise ValueError() + except Exception: + raise HTTPException(409, "existing device identity mismatch or certificate expired") from None + return device["cert"] + + if existing: + cert = check_existing(existing) + else: + if not settings.enrollment_signer_url: + raise HTTPException(503, "automatic registration is not enabled; update ECS first") + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= settings.max_devices: + raise HTTPException(503, "device capacity reached") + await rate_limit(redis, "auto-register-total", settings.max_pending, 3600) + try: + cert = await request_certificate(settings, body) + issued = verify_device(cert, settings.ca_cert.read_bytes(), body.host_id) + if raw_public(issued) != raw_public(key): + raise ValueError() + except Exception: + raise HTTPException(503, "certificate issuer unavailable or invalid response") from None + from .store import INTEGRITY_ERRORS + try: + store.add_device(body.host_id, cert, fingerprint(cert)) + except INTEGRITY_ERRORS: + # A retry/concurrent request must return the original certificate. + cert = check_existing(store.device(body.host_id)) + store.audit("device", "automatic_registration", body.host_id, "success") + await redis.set("hn:device:" + body.host_id, fingerprint(cert)) + return no_cache(JSONResponse(sign({"status": "registered", "host_id": body.host_id, + "cert": cert}, signer), status_code=201 if not existing else 200)) + + @router.post("/device/enroll") + async def request_enrollment(body: EnrollmentRequest, request: Request): + await rate_limit(redis, "enroll-ip:" + peer(request), 10) + validate_enrollment(body) + await rate_limit(redis, "enroll-host:" + body.host_id, 3, 3600) + if store.device(body.host_id): + raise HTTPException(409, "device already registered; recover its original certificate") + # Global rolling capacity bound; prevents anonymous request-table exhaustion. + await rate_limit(redis, "enroll-global", settings.max_pending, 1800) + identifier, claim_secret = secrets.token_hex(16), secrets.token_urlsafe(32) + record = {"host_id": body.host_id, "public_key": body.public_key, + "claim_hash": digest(claim_secret), "created": time.time(), "status": "pending"} + await redis.set("hn:enroll:" + identifier, json.dumps(record), ex=1800) + return no_cache(JSONResponse(sign({"request_id": identifier, "claim_secret": claim_secret, + "host_id": body.host_id, "expires_in": 1800}, signer), status_code=201)) + + @router.get("/device/enroll/{identifier}") + async def claim(identifier: str, request: Request): + await rate_limit(redis, "enroll-poll:" + peer(request), 60) + data = await redis.get("hn:enroll:" + identifier) + if not data: + raise HTTPException(404, "request expired") + record = json.loads(data) + import hmac + if not hmac.compare_digest(record["claim_hash"], digest(request.headers.get("X-Enrollment-Secret", ""))): + raise HTTPException(404, "request unavailable") + if record["status"] == "pending": + return no_cache(JSONResponse({"status": "pending"}, status_code=202)) + device = store.device(record["host_id"]) + if not device or device["revoked"]: + raise HTTPException(403, "device unavailable") + # Recoverable for its original claimant for the request TTL; public certificate only. + return no_cache(JSONResponse(sign({"status": "approved", "host_id": record["host_id"], + "request_id": identifier, "cert": device["cert"]}, signer))) + + @router.get("/admin/enrollments/{identifier}") + async def inspect(identifier: str, request: Request): + admin(request) + encoded = await redis.get("hn:enroll:" + identifier) + if not encoded: + raise HTTPException(404, "request expired") + record = json.loads(encoded) + return {k: v for k, v in record.items() if k != "claim_hash"} + + @router.post("/admin/enrollments/{identifier}/approve") + async def approve(identifier: str, body: EnrollmentApproval, request: Request): + admin(request) + encoded = await redis.get("hn:enroll:" + identifier) + if not encoded: + raise HTTPException(404, "request expired") + record = json.loads(encoded) + try: + public = verify_device(body.cert, settings.ca_cert.read_bytes(), record["host_id"]) + requested = serialization.load_pem_public_key(record["public_key"].encode()) + if raw_public(public) != raw_public(requested): + raise ValueError() + except Exception: + raise HTTPException(400, "certificate does not match enrollment") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= settings.max_devices: + raise HTTPException(503, "device capacity reached") + fp = fingerprint(body.cert) + existing = store.device(record["host_id"]) + if existing and (existing["revoked"] or existing["fingerprint"] != fp): + raise HTTPException(409, "device conflict") + if not existing: + from .store import INTEGRITY_ERRORS + try: + store.add_device(record["host_id"], body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "concurrent device registration") from None + await redis.set("hn:device:" + record["host_id"], fp) + record["status"] = "approved" + remaining = await redis.ttl("hn:enroll:" + identifier) + if remaining < 1: + raise HTTPException(409, "request expired during approval") + await redis.set("hn:enroll:" + identifier, json.dumps(record), ex=remaining) + store.audit("admin", "enrollment_approved", record["host_id"], "success") + return {"status": "approved", "host_id": record["host_id"]} + + return router diff --git a/host-code/relay/enrollment_signer.py b/host-code/relay/enrollment_signer.py new file mode 100644 index 0000000..6089d02 --- /dev/null +++ b/host-code/relay/enrollment_signer.py @@ -0,0 +1,43 @@ +"""Internal automatic CA issuer. No published host port, no application credentials.""" +import os +from pathlib import Path + +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from fastapi import FastAPI, Request +from .common import BodyLimitMiddleware, require_secret +from .crypto import load_private, issue_device, raw_public, verify_device +from .enrollment import EnrollmentRequest, validate_enrollment + + +def create_app(*, ca_key_path=None, ca_cert_path=None, secret=None): + key = load_private(Path(ca_key_path or os.environ.get("HN_ISSUER_CA_KEY", "/issuer/ca.key"))) + ca_bytes = Path(ca_cert_path or os.environ.get("HN_ISSUER_CA_CERT", "/trust/ca.pem")).read_bytes() + ca = x509.load_pem_x509_certificate(ca_bytes) + if raw_public(key.public_key()) != raw_public(ca.public_key()): + raise ValueError("issuer CA key mismatch") + token = secret or os.environ.get("HN_INTERNAL_SECRET", "") + if len(token) < 32: + raise ValueError("issuer requires internal credential") + app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=16384) + + @app.get("/ready") + async def ready(): + return {"status": "ready"} + + @app.post("/issue") + async def issue(body: EnrollmentRequest, request: Request): + require_secret(request.headers.get("X-Internal-Secret"), token) + public = validate_enrollment(body) + cert = issue_device(key, ca, body.host_id, public).public_bytes(serialization.Encoding.PEM).decode() + verify_device(cert, ca_bytes, body.host_id) + return {"cert": cert} + + return app + + +if __name__ == "__main__": + import uvicorn + uvicorn.run("relay.enrollment_signer:create_app", factory=True, host="0.0.0.0", port=8000, + workers=1, access_log=False, proxy_headers=False, log_level="warning") diff --git a/host-code/relay/hainuo1065.py b/host-code/relay/hainuo1065.py new file mode 100644 index 0000000..6f7735b --- /dev/null +++ b/host-code/relay/hainuo1065.py @@ -0,0 +1,107 @@ +"""Adapter for the native API actually found in installer 1.065. + +Loopback only, independent API key, no platform secrets, no redirects or retries. +The included native launcher offers only text inference with no tools. +""" +import hashlib +import json +import re +from pathlib import Path +from urllib.parse import urlsplit + +import httpx + + +class NativeAPIError(RuntimeError): + """Contains a sanitized status only, never response body or credentials.""" + + +def read_native_config(path): + path = Path(path) + data = json.loads(path.read_text(encoding="utf-8-sig")) + url = data.get("url", "") + parts = urlsplit(url) + if (parts.scheme != "http" or parts.hostname != "127.0.0.1" or not parts.port + or parts.username or parts.password or parts.query or parts.fragment + or parts.path not in {"", "/"}): + raise ValueError("native API must be a fixed IPv4 loopback HTTP base URL") + if not isinstance(data.get("api_key"), str) or len(data["api_key"]) < 32: + raise ValueError("native API requires a dedicated key of at least 32 characters") + if data.get("mode") not in {"text-only", "profile-tools"}: + raise ValueError("unsupported native API mode") + data["url"] = url.rstrip("/") + return data + + +class Hainuo1065Agent: + def __init__(self, config_path, transport=None): + self.config = read_native_config(config_path) + self.http = httpx.AsyncClient(base_url=self.config["url"], timeout=120, + follow_redirects=False, trust_env=False, transport=transport) + self.headers = {"Authorization": "Bearer " + self.config["api_key"]} + + async def _json(self, method, path, **kwargs): + try: + async with self.http.stream(method, path, headers={**self.headers, **kwargs.pop("headers", {})}, **kwargs) as response: + if response.status_code != 200: + raise NativeAPIError(f"native API HTTP {response.status_code}") + body = bytearray() + async for chunk in response.aiter_bytes(): + body.extend(chunk) + if len(body) > 262144: + raise NativeAPIError("native API response exceeds limit") + data = json.loads(body) + if not isinstance(data, dict): + raise NativeAPIError("native API response is not an object") + return data + except (httpx.HTTPError, ValueError): + raise NativeAPIError("native API connection/JSON failure") from None + + async def probe(self): + health = await self._json("GET", "/health") + if health.get("platform") != "hermes-agent" or health.get("status") != "ok": + raise NativeAPIError("endpoint is not the native Hermes API") + capabilities = await self._json("GET", "/v1/capabilities") + policy = capabilities.get("hainuo_relay_policy", {}) + # A stock unrestricted API endpoint is deliberately not sufficient. + expected_mode = self.config["mode"] + expected_tools = "profile" if expected_mode == "profile-tools" else "disabled" + if policy != {"mode": expected_mode, "tools": expected_tools, "contract": "hainuo-1065-v1"}: + raise NativeAPIError("native API policy missing; start the supplied restricted launcher") + models = await self._json("GET", "/v1/models") + model = self.config.get("model") + available = models.get("data") + if not isinstance(available, list) or not available or not all(isinstance(x, dict) and isinstance(x.get("id"), str) for x in available): + raise NativeAPIError("native model list invalid") + if model and model not in {x["id"] for x in available}: + raise NativeAPIError("configured model alias unavailable") + self.model = model or available[0]["id"] + return {"status": "ready", "platform": "hermes-agent", "model": self.model, + "mode": expected_mode, "version": health.get("version", "unknown")} + + async def execute(self, text, owner, turn): + try: + # Recheck the actual local service before each inference, including reconnects. + await self.probe() + digest = hashlib.sha256((owner + "\0" + turn).encode()).hexdigest() + data = await self._json("POST", "/v1/chat/completions", + headers={"Idempotency-Key": "hn-" + digest}, + json={"model": self.model, "stream": False, + "messages": [{"role": "user", "content": text}]}) + choices = data.get("choices") + if not isinstance(choices, list) or not choices or not isinstance(choices[0], dict): + raise NativeAPIError("native completion choices missing") + choice = choices[0] + message = choice.get("message", {}) + if (choice.get("finish_reason") != "stop" or message.get("tool_calls") + or not isinstance(message.get("content"), str) + or data.get("hermes", {}).get("failed") + or data.get("hermes", {}).get("partial")): + raise NativeAPIError("native completion incomplete or unexpected tool call") + return message["content"][:16000] + except NativeAPIError as error: + # Safe generated error strings only; never echo upstream model/API errors. + return "海诺原生接口未完成请求:" + str(error) + "。未自动重试。" + + async def close(self): + await self.http.aclose() diff --git a/host-code/relay/longconn.py b/host-code/relay/longconn.py new file mode 100644 index 0000000..6d7be70 --- /dev/null +++ b/host-code/relay/longconn.py @@ -0,0 +1,83 @@ +"""Optional official SDK event source, private platform domain explicitly configured.""" +import json +import logging +import os +import threading +import time + +import httpx +import redis +from .config import Settings + + +def disable_proxy_environment(): + """This dedicated SDK worker must connect directly, including discovery and WS.""" + for key in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "http_proxy", "https_proxy", "all_proxy"): + os.environ.pop(key, None) + os.environ["NO_PROXY"] = "*" + os.environ["no_proxy"] = "*" + + +def main(): + disable_proxy_environment() + import lark_oapi as lark + s = Settings(role="longconn") + if not s.feishu_enabled or s.feishu_event_source != "longconn": + raise RuntimeError("platform is disabled") + # The SDK may log websocket URLs, tokens or event bodies. Disable its logging entirely. + logging.disable(logging.CRITICAL) + r = redis.Redis.from_url(s.redis_url) + lease = r.lock("hn:longconn:leader", timeout=30, thread_local=False) + if not lease.acquire(blocking=False): + raise RuntimeError("another long-connection consumer owns the lease") + url = os.environ.get("HN_CONNECTOR_INTERNAL_URL", "http://connector:8000") + client = httpx.Client(base_url=url, timeout=10, follow_redirects=False, trust_env=False) + done = threading.Event() + + def maintain_lease(): + while not done.wait(8): + try: + lease.extend(30, replace_ttl=True) + except Exception: + # Fail closed. Continuing after lease loss could create two consumers. + os._exit(2) + + threading.Thread(target=maintain_lease, daemon=True).start() + + def received(event): + data = json.loads(lark.JSON.marshal(event)) + response = client.post("/internal/events", headers={"X-Internal-Secret": s.internal_secret}, json=data) + if response.status_code != 200: + raise RuntimeError("connector did not accept event") + print(json.dumps({"event": "platform_event_accepted", "type": data.get("header", {}).get("event_type")}), flush=True) + + handler = lark.EventDispatcherHandler.builder("", "").register_p2_im_message_receive_v1(received).build() + class ObservableClient(lark.ws.Client): + async def _connect(self): + try: + await super()._connect() + if self._conn is not None: + print(json.dumps({"event": "platform_connected", "api_host": s.feishu_api_base}), flush=True) + except Exception as error: + print(json.dumps({"event": "platform_connect_failed", "error_type": type(error).__name__}), flush=True) + raise + + try: + # domain is an actual supported SDK argument; no public-cloud-domain monkey patch. + ObservableClient(s.feishu_app_id, s.feishu_app_secret, event_handler=handler, + domain=s.feishu_api_base.rstrip("/"), log_level=lark.LogLevel.ERROR).start() + finally: + done.set() + client.close() + try: + lease.release() + except redis.exceptions.LockError: + pass + + +if __name__ == "__main__": + try: + main() + except Exception as error: + print(json.dumps({"event": "platform_consumer_stopped", "error_type": type(error).__name__}), flush=True) + raise SystemExit(1) from None diff --git a/host-code/relay/oauth.py b/host-code/relay/oauth.py new file mode 100644 index 0000000..1b35a14 --- /dev/null +++ b/host-code/relay/oauth.py @@ -0,0 +1,144 @@ +import asyncio +import hashlib +import hmac +import secrets +import time +from dataclasses import dataclass +from urllib.parse import urlencode + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .platform import PlatformError + + +@dataclass +class Transaction: + secret_hash: str + expires: float + status: str = "waiting" + result: dict | None = None + owner: str = "" + + +class Empty(BaseModel): + host_id: str = "" + proof: dict = Field(default_factory=dict) + + +class Refresh(BaseModel): + refresh_token: str = Field(min_length=1, max_length=4096) + + +class OAuth: + """A single connector process owns transactions and tokens in memory only.""" + def __init__(self, settings, redis, platform): + self.settings, self.redis, self.platform = settings, redis, platform + self.pending: dict[str, Transaction] = {} + self.lock = asyncio.Lock() + self.router = APIRouter() + self.router.add_api_route("/oauth/start", self.start, methods=["POST"], status_code=201) + self.router.add_api_route("/oauth/cb", self.callback, methods=["GET"]) + self.router.add_api_route("/oauth/claim", self.claim, methods=["GET"]) + self.router.add_api_route("/oauth/pending", self.status, methods=["GET"]) + self.router.add_api_route("/oauth/refresh", self.refresh, methods=["POST"]) + + def prune(self): + now = time.monotonic() + for state in list(self.pending): + if self.pending[state].expires <= now: + del self.pending[state] + + def authenticated(self, state, secret): + self.prune() + item = self.pending.get(state) + digest = hashlib.sha256((secret or "").encode()).hexdigest() + if not item or not hmac.compare_digest(item.secret_hash, digest): + raise HTTPException(404, "transaction not found") + return item + + async def start(self, body: Empty, request: Request): + if not self.settings.feishu_enabled or not self.settings.oauth_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.redis, "oauth:start:" + peer(request), 20) + owner = "" + if body.host_id: + from .work_api import device_owner + owner = await device_owner(self.settings, self.redis, self.store, body.host_id, + body.proof, "work-oauth-v1", {}) + async with self.lock: + self.prune() + if len(self.pending) >= self.settings.max_pending: + raise HTTPException(503, "too many pending transactions") + state, secret = secrets.token_urlsafe(32), secrets.token_urlsafe(32) + self.pending[state] = Transaction(hashlib.sha256(secret.encode()).hexdigest(), time.monotonic() + 600, owner=owner) + redirect = self.settings.public_url.rstrip("/") + "/oauth/cb" + parameters = {"app_id": self.settings.feishu_app_id, "redirect_uri": redirect, "state": state} + if self.settings.feishu_oauth_scopes: + parameters["scope"] = self.settings.feishu_oauth_scopes + url = self.settings.feishu_authorize_url + "?" + urlencode(parameters) + return no_cache(JSONResponse({"state": state, "claim_secret": secret, "authorization_url": url, + "redirect_uri": redirect, "expires_in": 600, + "code_ttl": 60, "poll_interval": 2}, status_code=201)) + + async def callback(self, request: Request, state: str = "", code: str = "", error: str = ""): + await rate_limit(self.redis, "oauth:cb:" + peer(request)) + async with self.lock: + self.prune() + item = self.pending.get(state) + if error and item and item.status == "waiting": + item.status = "failed" + return no_cache(HTMLResponse("

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

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

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

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

连接你的
工作电脑。

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

+
01 / 连接本人设备

输入电脑上的配对码

+

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

+
+
使用海能Work授权

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

+ +

正在检查网页会话…

+
PC需保持开机、联网,并运行原生API和桥接端。试点网页由中继在内存中转发正文,不保存聊天记录;当前页关闭后消息不保留。
+
diff --git a/host-code/relay/web/style.css b/host-code/relay/web/style.css new file mode 100644 index 0000000..a33d19c --- /dev/null +++ b/host-code/relay/web/style.css @@ -0,0 +1,3 @@ +:root{color-scheme:light;font-family:system-ui,-apple-system,"Segoe UI","Microsoft YaHei",sans-serif;color:#17372c;background:#f2f4ed}*{box-sizing:border-box}body{margin:0}main{max-width:820px;margin:0 auto;padding:60px 26px}.mark{font-size:13px;letter-spacing:.16em;color:#557764;font-weight:700}h1{font-size:clamp(42px,8vw,70px);letter-spacing:-.05em;line-height:1.12;font-weight:650;margin:40px 0 22px}.intro{font-size:17px;line-height:1.8;color:#56675a;max-width:520px}.card{background:#fff;border:1px solid #dbe3d8;border-radius:20px;padding:30px;margin:40px 0 24px;box-shadow:0 12px 44px #18372c06}.step{font-size:12px;letter-spacing:.1em;color:#6d806c}h2{font-size:24px;margin:16px 0}.card p{line-height:1.8;color:#5b6c60}button{background:#214e3a;color:white;border:0;border-radius:10px;font:inherit;font-weight:600;padding:16px 20px;width:100%;text-align:left;cursor:pointer;margin-top:16px}button span{float:right}button:disabled{opacity:.5;cursor:wait}button:focus-visible,a:focus-visible{outline:3px solid #d0a349;outline-offset:4px}#status{font-size:13px;margin-bottom:0}#open{display:block;margin-top:22px;color:#214e3a;font-weight:600}#open[hidden]{display:none}footer{color:#768373;font-size:12px;line-height:1.8}@media(max-width:480px){main{padding-top:36px}.card{padding:22px}h1{margin-top:28px}} + +[hidden]{display:none!important}.badge{float:right;font-size:11px;letter-spacing:0;background:#e4ebdf;border-radius:20px;padding:6px 10px}label{display:block;font-size:13px;color:#526c5b;margin:12px 0 8px}input,textarea{font:inherit;width:100%;border:1px solid #ccd8ce;border-radius:10px;padding:14px;color:#17372c;background:#fafcf8;resize:vertical}input:focus,textarea:focus{outline:2px solid #6c987b;outline-offset:2px}details{margin-top:24px;font-size:13px;color:#587561}summary{cursor:pointer}.chat-head{display:flex;align-items:center;justify-content:space-between;gap:20px}.secondary{width:auto;background:#edf2e9;color:#31523f;padding:9px 12px;font-size:12px;margin:0}.hint{font-size:13px}#messages{display:flex;flex-direction:column;gap:12px;margin:22px 0;max-height:420px;overflow:auto}.message{padding:14px 16px;border-radius:12px;line-height:1.8;white-space:pre-wrap;overflow-wrap:anywhere;font-size:15px}.message.user{background:#e5eee2;margin-left:36px}.message.assistant{background:#f2f5ef;margin-right:24px}.message.notice{background:#fbf4e5;color:#735c2e}#status{font-size:13px;color:#5f745f;line-height:1.8}#chat-panel h2{font-size:20px}footer{max-width:640px} diff --git a/host-code/relay/web_session.py b/host-code/relay/web_session.py new file mode 100644 index 0000000..1ac9adf --- /dev/null +++ b/host-code/relay/web_session.py @@ -0,0 +1,183 @@ +"""Pilot H5 pairing and conversation. Credentials never appear in URLs.""" +import hashlib +import json +import secrets +import time +from urllib.parse import urlsplit + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import JSONResponse +from pydantic import BaseModel, Field + +from .common import no_cache, peer, rate_limit +from .crypto import sign, verified, verify_device +from .platform import PlatformError + +COOKIE = "hn_web_session" + + +def digest(value): + return hashlib.sha256(value.encode()).hexdigest() + + +def origin_tuple(value): + try: + parts = urlsplit(value) + if parts.scheme not in {"https", "http"} or not parts.hostname or parts.username or parts.password: + return None + return parts.scheme, parts.hostname.lower(), parts.port or (443 if parts.scheme == "https" else 80) + except ValueError: + return None + + +class PairProof(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Claim(BaseModel): + code: str = Field(min_length=12, max_length=128) + + +class Chat(BaseModel): + text: str = Field(min_length=1, max_length=8000) + request_id: str = Field(pattern=r"^[a-f0-9]{32}$") + + +class WebSessions: + def __init__(self, settings, redis, store, broker, platform): + self.s, self.r, self.store, self.broker, self.platform = settings, redis, store, broker, platform + self.router = APIRouter() + for path, method, handler in [ + ("/device/web-pair", "POST", self.device_pair), + ("/web/claim", "POST", self.claim), + ("/web/session", "GET", self.session), + ("/web/platform-session", "POST", self.platform_login), + ("/web/chat", "POST", self.chat), + ("/web/logout", "POST", self.logout), + ]: + self.router.add_api_route(path, handler, methods=[method]) + + def check_origin(self, request): + actual = origin_tuple(request.headers.get("Origin", "")) + if not actual or actual != origin_tuple(self.s.public_url): + raise HTTPException(403, "same-origin browser request required") + + async def valid_device(self, record): + device = self.store.device(record["host_id"]) + if (not device or device["revoked"] or device["owner"] != record["owner"] + or device["fingerprint"] != record["fingerprint"]): + raise HTTPException(401, "device binding changed; pair again") + generation = await self.r.get("hn:web-generation:" + record["host_id"]) or "0" + if record.get("generation") != generation: + raise HTTPException(401, "device binding changed; pair again") + return device + + async def device_pair(self, body: PairProof, request: Request): + await rate_limit(self.r, "web-pair-ip:" + peer(request), 20) + device = self.store.device(body.host_id) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "active bound device required") + try: + key = verify_device(device["cert"], self.s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["owner"] != device["owner"] + or proof["purpose"] != "h5-pair-v1" or abs(time.time() - proof["ts"]) > 90 + or not isinstance(proof["nonce"], str) or len(proof["nonce"]) != 32): + raise ValueError("invalid proof") + except Exception: + raise HTTPException(403, "invalid device proof") from None + replay = "hn:web-pair-proof:" + digest(body.host_id + proof["nonce"]) + if not await self.r.set(replay, "used", nx=True, ex=180): + raise HTTPException(409, "pairing request already used") + await rate_limit(self.r, "web-pair-device:" + body.host_id, 5) + code = secrets.token_urlsafe(18) + record = {"host_id": body.host_id, "owner": device["owner"], "fingerprint": device["fingerprint"], + "generation": await self.r.get("hn:web-generation:" + body.host_id) or "0"} + await self.r.set("hn:web-code:" + digest(code), json.dumps(record), ex=120) + self.store.audit(device["owner"], "web_pair_issued", body.host_id, "success") + return no_cache(JSONResponse(sign({"host_id": body.host_id, "code": code, "expires_in": 120, + "url": self.s.public_url.rstrip("/") + "/h5"}, self.broker.signer))) + + async def new_session(self, record): + token = secrets.token_urlsafe(32) + record = {k: record[k] for k in ("host_id", "owner", "fingerprint", "generation")} + await self.r.set("hn:web-session:" + digest(token), json.dumps(record), ex=1800) + response = no_cache(JSONResponse({"status": "paired", "host_id": record["host_id"], "expires_in": 1800})) + response.set_cookie(COOKIE, token, max_age=1800, secure=True, httponly=True, samesite="strict", path="/") + return response + + async def claim(self, body: Claim, request: Request): + self.check_origin(request) + await rate_limit(self.r, "web-claim:" + peer(request), 20) + key = "hn:web-code:" + digest(body.code.strip()) + encoded = await self.r.get(key) + if not encoded: + raise HTTPException(404, "code expired or consumed") + await self.valid_device(json.loads(encoded)) + encoded = await self.r.getdel(key) + if not encoded: + raise HTTPException(404, "code already consumed") + record = json.loads(encoded) + await self.valid_device(record) + self.store.audit(record["owner"], "web_pair_claimed", record["host_id"], "success") + return await self.new_session(record) + + async def authenticated(self, request): + token = request.cookies.get(COOKIE, "") + if not token or len(token) > 128: + raise HTTPException(401, "pair this browser first") + encoded = await self.r.get("hn:web-session:" + digest(token)) + if not encoded: + raise HTTPException(401, "browser session expired") + record = json.loads(encoded) + await self.valid_device(record) + return record + + async def session(self, request: Request): + record = await self.authenticated(request) + return no_cache(JSONResponse({"authenticated": True, "host_id": record["host_id"], + "online": bool(await self.r.exists("hn:online:" + record["host_id"])), + "mode": "pilot-text-only", "relay_sees_content": True})) + + async def platform_login(self, request: Request): + self.check_origin(request) + if not self.s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.r, "web-platform:" + peer(request), 20) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await self.platform.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = self.store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "bind your device first") + return await self.new_session({**device, "generation": await self.r.get("hn:web-generation:" + device["host_id"]) or "0"}) + + async def chat(self, body: Chat, request: Request): + self.check_origin(request) + record = await self.authenticated(request) + await rate_limit(self.r, "web-chat:" + record["owner"], 20) + text = body.text.strip() + if not text: + raise HTTPException(422, "empty message") + # PC accepts only its bound owner and the existing IM read-only channel. + if not (text == "/主机名" or text.startswith(("/回显 ", "/读取 ", "/助手 "))): + text = "/助手 " + text + turn = "web_" + digest(record["owner"] + "\0" + body.request_id) + result = await self.broker.dispatch(record["owner"], text, turn) + # Revoke/unbind during a long model call invalidates the browser immediately. + await self.authenticated(request) + return no_cache(JSONResponse(result)) + + async def logout(self, request: Request): + self.check_origin(request) + token = request.cookies.get(COOKIE, "") + if token and len(token) <= 128: + await self.r.delete("hn:web-session:" + digest(token)) + response = no_cache(JSONResponse({"status": "logged_out"})) + response.delete_cookie(COOKIE, secure=True, httponly=True, samesite="strict", path="/") + return response diff --git a/host-code/relay/windows_keys.py b/host-code/relay/windows_keys.py new file mode 100644 index 0000000..0e84e19 --- /dev/null +++ b/host-code/relay/windows_keys.py @@ -0,0 +1,40 @@ +"""Current-user Windows DPAPI for device keys; never uses machine-wide scope.""" +import ctypes +import os +from ctypes import wintypes + + +class Blob(ctypes.Structure): + _fields_ = [("cbData", wintypes.DWORD), ("pbData", ctypes.POINTER(ctypes.c_ubyte))] + + +def transform(data, decrypt=False): + if os.name != "nt": + raise RuntimeError("DPAPI is supported only on Windows") + buffer = (ctypes.c_ubyte * len(data)).from_buffer_copy(data) + source = Blob(len(data), buffer) + target = Blob() + crypt = ctypes.WinDLL("crypt32", use_last_error=True) + kernel = ctypes.WinDLL("kernel32", use_last_error=True) + if decrypt: + fn = crypt.CryptUnprotectData + args = [ctypes.byref(source), None, None, None, None, 1, ctypes.byref(target)] + else: + fn = crypt.CryptProtectData + args = [ctypes.byref(source), "Hainuo device identity", None, None, None, 1, ctypes.byref(target)] + fn.restype = wintypes.BOOL + if not fn(*args): + raise ctypes.WinError(ctypes.get_last_error()) + try: + return ctypes.string_at(target.pbData, target.cbData) + finally: + kernel.LocalFree.argtypes = [ctypes.c_void_p] + kernel.LocalFree(target.pbData) + + +def protect(data): + return transform(data) + + +def unprotect(data): + return transform(data, decrypt=True) diff --git a/host-code/relay/work_api.py b/host-code/relay/work_api.py new file mode 100644 index 0000000..96a6e49 --- /dev/null +++ b/host-code/relay/work_api.py @@ -0,0 +1,152 @@ +"""Device-authenticated USER-token business gateway; no tenant-token fallback.""" +import hashlib +import json +import re +import time +from urllib.parse import unquote + +import httpx +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import JSONResponse +from pydantic import BaseModel, Field +from .common import no_cache, rate_limit +from .crypto import canonical, verified, verify_device +from .store import INTEGRITY_ERRORS + + +class DeviceProof(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class BusinessCall(DeviceProof): + method: str = Field(pattern=r"^(GET|POST|PATCH|DELETE)$") + path: str = Field(max_length=1500) + params: dict = Field(default_factory=dict) + body: dict = Field(default_factory=dict) + operation_id: str = Field(default="", max_length=100) + + +def business_allowed(method, path): + if any(x in unquote(path) for x in ("..", "\\", "%", "?", "#", "\r", "\n")): + return False + seg = r"[A-Za-z0-9_.@-]+" + cal = r"/open-apis/calendar/v4/calendars" + rules = { + "GET": [cal, cal + "/" + seg, cal + "/" + seg + "/events(?:/" + seg + ")?(?:/attendees)?", + r"/open-apis/contact/v3/users/" + seg, r"/open-apis/search/v1/user", + r"/open-apis/(?:drive/v1/files|docx/v1/documents|wiki/v2/spaces|bitable/v1/apps)(?:/" + seg + ")*"], + "POST": [cal + "/primary", cal + "/" + seg + "/events(?:/" + seg + "/attendees)?"], + "PATCH": [cal + "/" + seg + "/events/" + seg], + "DELETE": [cal + "/" + seg + "/events/" + seg, cal + "/" + seg + "/events/" + seg + "/attendees/" + seg], + } + return any(re.fullmatch(p, path) for p in rules.get(method, [])) + + +async def device_owner(settings, redis, store, host, proof, purpose, payload): + device = store.device(host) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "active bound device required") + try: + key = verify_device(device["cert"], settings.ca_cert.read_bytes(), host) + data = verified(proof, key) + if (data["host_id"] != host or data["purpose"] != purpose or abs(time.time() - data["ts"]) > 90 + or not re.fullmatch(r"[a-f0-9]{32}", data["nonce"]) + or data["payload_hash"] != hashlib.sha256(canonical(payload)).hexdigest()): + raise ValueError() + except Exception: + raise HTTPException(403, "invalid device request proof") from None + if not await redis.set("hn:work-proof:" + host + ":" + data["nonce"], "1", nx=True, ex=180): + raise HTTPException(409, "device request replayed") + return device["owner"] + + +def router(settings, redis, store, platform): + routes = APIRouter() + store.db.execute("""CREATE TABLE IF NOT EXISTS business_operations( + owner TEXT NOT NULL, operation_id TEXT NOT NULL, request_hash TEXT NOT NULL, + status TEXT NOT NULL, result_meta TEXT NOT NULL, created DOUBLE PRECISION NOT NULL, + PRIMARY KEY(owner,operation_id))""") + if not settings.database_url: + store.db.commit() + + @routes.post("/work/api") + async def call(body: BusinessCall, request: Request): + if not settings.feishu_enabled: + raise HTTPException(503, "platform disabled") + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or not 10 < len(auth) <= 8192: + raise HTTPException(401, "user access token required") + if not business_allowed(body.method, body.path): + raise HTTPException(403, "business method/path not supported") + payload = {"method": body.method, "path": body.path, "params": body.params, "body": body.body, + "operation_id": body.operation_id, "token_hash": hashlib.sha256(auth[7:].encode()).hexdigest()} + owner = await device_owner(settings, redis, store, body.host_id, body.proof, "work-api-v1", payload) + await rate_limit(redis, "work-api:" + owner, 60) + try: + info = await platform.user_info(auth[7:]) + except Exception: + raise HTTPException(401, "user token expired or invalid; refresh or authorize") from None + if info.get("open_id") != owner: + raise HTTPException(403, "token user does not match bound device owner") + write = body.method != "GET" and not body.path.endswith(("/primary", "/batch_get_id")) + digest = hashlib.sha256(canonical({k: v for k, v in payload.items() if k != "token_hash"})).hexdigest() + if write: + if not re.fullmatch(r"[A-Za-z0-9_-]{8,100}", body.operation_id): + raise HTTPException(422, "write requires stable operation_id") + try: + with store.db: + store.db.execute("INSERT INTO business_operations VALUES(?,?,?,?,?,?)", + (owner, body.operation_id, digest, "inflight", "{}", time.time())) + except INTEGRITY_ERRORS: + row = dict(store.db.execute("SELECT * FROM business_operations WHERE owner=? AND operation_id=?", + (owner, body.operation_id)).fetchone()) + if row["request_hash"] != digest: + raise HTTPException(409, "operation_id reused with different request") + return no_cache(JSONResponse({"status": "duplicate", "operation_id": body.operation_id, + "previous_status": row["status"], **json.loads(row["result_meta"])})) + params = dict(body.params) + if write and body.method == "POST" and body.path.endswith("/events"): + params["idempotency_key"] = hashlib.sha256((owner + body.operation_id).encode()).hexdigest() + try: + async with platform.http.stream(body.method, body.path, params=params, + json=body.body if body.method != "GET" else None, + headers={"Authorization": auth}) as upstream: + raw = bytearray() + async for chunk in upstream.aiter_bytes(): + raw.extend(chunk) + if len(raw) > 2 * 1024 * 1024: + raise ValueError() + data = json.loads(raw) + if not isinstance(data, dict): + raise ValueError() + code = data.get("code", 0) + if upstream.status_code != 200 or code != 0: + status = "platform_rejected" + def safe_diagnostic(value): + text = str(value) + for secret in (auth, auth[7:], settings.feishu_app_secret, settings.admin_secret, settings.internal_secret): + if secret: + text = text.replace(secret, "[redacted]") + return text[:2000] + detail = data.get("error") or {} + result = {"status": status, "http_status": upstream.status_code, "platform_code": code, + "message": "平台拒绝:请核对用户授权、应用scope、资源权限或接口版本。", + "platform_message": safe_diagnostic(data.get("msg", data.get("message", ""))), + "platform_log_id": safe_diagnostic(detail.get("log_id", "")) if isinstance(detail, dict) else "", + "permission_details": safe_diagnostic(detail.get("permission_violations", [])) if isinstance(detail, dict) else ""} + else: + status, result = "completed", {"status": "completed", "data": data.get("data", {})} + except (httpx.HTTPError, ValueError): + status = "unknown" if write else "failed" + result = {"status": status, "message": "平台请求未取得明确结果;写操作不要换新ID重试,请先核对日历。"} + if write: + event = result.get("data", {}).get("event", {}) + meta = {k: event[k] for k in ("event_id", "app_link") if k in event} + with store.db: + store.db.execute("UPDATE business_operations SET status=?,result_meta=? WHERE owner=? AND operation_id=?", + (status, json.dumps(meta), owner, body.operation_id)) + store.audit(owner, "work_api_" + body.method.lower(), body.path, status) + return no_cache(JSONResponse(result)) + + return routes diff --git a/host-code/relay/work_client.py b/host-code/relay/work_client.py new file mode 100644 index 0000000..4cc90b3 --- /dev/null +++ b/host-code/relay/work_client.py @@ -0,0 +1,190 @@ +"""PC-side user OAuth and calendar operations. Tokens are DPAPI-protected only here.""" +import hashlib +import json +import secrets +import ssl +import time +from datetime import datetime +from pathlib import Path +from urllib.parse import quote, urlsplit + +import httpx +from .crypto import canonical, load_private, sign +from .windows_keys import protect, unprotect +from .process_lock import DeviceProcessLock + + +class WorkError(RuntimeError): + pass + + +class AuthorizationRequired(WorkError): + def __init__(self, url): + self.url = url + super().__init__("需要本人Work授权,请打开授权链接,完成后重新发起原任务;尚未执行平台写操作。") + + +def atomic_secret(path, data): + temp = path.with_suffix(".tmp") + temp.write_bytes(protect(json.dumps(data, ensure_ascii=False).encode())) + temp.replace(path) + + +class WorkClient: + def __init__(self, directory, url="https://10.77.76.13", *, transport=None): + self.directory = Path(directory) + self.config = json.loads((self.directory / "device.json").read_text(encoding="utf-8-sig")) + if not self.config.get("owner"): + raise WorkError("请先绑定本人Work身份。") + context = ssl.create_default_context(cafile=str(self.directory / "server.pem")) + self.http = httpx.Client(base_url=url.rstrip("/"), verify=context, trust_env=False, + timeout=25, follow_redirects=False, transport=transport) + self.owner = self.config["owner"] + self.path = self.directory / "work-user-tokens.dpapi" + self.pending = self.directory / "work-oauth-pending.dpapi" + + def close(self): + self.http.close() + + def read(self, path): + return json.loads(unprotect(path.read_bytes())) + + def proof(self, purpose, payload): + return sign({"host_id": self.config["host_id"], "purpose": purpose, "ts": int(time.time()), + "nonce": secrets.token_hex(16), "payload_hash": hashlib.sha256(canonical(payload)).hexdigest()}, + load_private(self.directory / "device.key")) + + def authorize(self): + if self.pending.exists(): + data = self.read(self.pending) + if data.get("owner") == self.owner and data["deadline"] > time.time(): + return data["authorization_url"] + response = self.http.post("/oauth/start", json={"host_id": self.config["host_id"], + "proof": self.proof("work-oauth-v1", {})}) + if response.status_code != 201: + raise WorkError("授权事务创建失败(HTTP " + str(response.status_code) + "),请检查ECS平台配置。") + data = response.json() + if urlsplit(data["authorization_url"]).scheme != "https": + raise WorkError("授权地址必须使用HTTPS。") + data.update(owner=self.owner, deadline=time.time() + data["expires_in"]) + atomic_secret(self.pending, data) + return data["authorization_url"] + + def save_tokens(self, data): + if data.get("open_id") != self.owner or not data.get("user_access_token"): + raise WorkError("授权账号与本机Work绑定账号不一致,请使用本人账号重新授权。") + now = time.time() + data = {**data, "expires_at": now + int(data["expires_in"]), + "refresh_expires_at": now + int(data.get("refresh_expires_in", 0)), "needs_authorization": False} + atomic_secret(self.path, data) + return data + + def access_token(self): + # Shared across GUI and Agent calls; never rotate the same token concurrently. + with DeviceProcessLock(self.directory / "work-token-lock"): + if self.path.exists(): + data = self.read(self.path) + if data.get("open_id") == self.owner and not data.get("needs_authorization"): + if data["expires_at"] > time.time() + 120: + return data["user_access_token"] + if data.get("refresh_token") and data.get("refresh_expires_at", 0) > time.time(): + # Mark before network; ambiguous failure must not reuse a rotating refresh token. + atomic_secret(self.path, {**data, "needs_authorization": True}) + try: + response = self.http.post("/oauth/refresh", json={"refresh_token": data["refresh_token"]}) + if response.status_code != 200: + raise WorkError("刷新失败") + data = self.save_tokens(response.json()) + return data["user_access_token"] + except (httpx.HTTPError, ValueError, WorkError): + raise AuthorizationRequired(self.authorize()) from None + if self.pending.exists(): + pending = self.read(self.pending) + if pending.get("owner") == self.owner and pending["deadline"] > time.time(): + response = self.http.get("/oauth/claim", params={"state": pending["state"]}, + headers={"X-OAuth-Claim-Secret": pending["claim_secret"]}) + if response.status_code == 200: + data = self.save_tokens(response.json()) + self.pending.unlink(missing_ok=True) + return data["user_access_token"] + if response.status_code not in {202}: + self.pending.unlink(missing_ok=True) + raise AuthorizationRequired(self.authorize()) + + def status(self): + try: + token = self.access_token() + return {"authorized": True, "owner": self.owner, "message": "本人用户授权可用;令牌未输出。", + "local_time": datetime.now().astimezone().isoformat()} + except AuthorizationRequired as error: + return {"authorized": False, "authorization_url": error.url, "message": str(error)} + + def api(self, method, path, params=None, body=None, operation_id=""): + token = self.access_token() + payload = {"method": method, "path": path, "params": params or {}, "body": body or {}, + "operation_id": operation_id, "token_hash": hashlib.sha256(token.encode()).hexdigest()} + proof = self.proof("work-api-v1", payload) + wire = {k: v for k, v in payload.items() if k != "token_hash"} + response = self.http.post("/work/api", headers={"Authorization": "Bearer " + token}, + json={**wire, "host_id": self.config["host_id"], "proof": proof}) + if response.status_code == 401: + # Do not repeat a business write automatically, even when auth fails. + if self.path.exists(): + data = self.read(self.path) + data["expires_at"] = 0 + atomic_secret(self.path, data) + raise WorkError("用户令牌已失效。本次未重试,请检查授权后再核对操作状态。") + if response.status_code != 200: + raise WorkError("平台代理失败(HTTP " + str(response.status_code) + ");未重试。") + return response.json() + + def primary_calendar(self): + result = self.api("POST", "/open-apis/calendar/v4/calendars/primary", {"user_id_type": "open_id"}) + if result.get("status") != "completed": + return result + calendars = result.get("data", {}).get("calendars", []) + own = [c["calendar"] for c in calendars if c.get("user_id") == self.owner and c.get("calendar")] + if not own and len(calendars) == 1 and not calendars[0].get("user_id") and calendars[0].get("calendar"): + own = [calendars[0]["calendar"]] + if len(own) != 1: + raise WorkError("没有唯一确定的本人主日历,请先查询日历列表并明确选择。") + return {"status": "completed", "calendar": own[0]} + + def create_event(self, summary, start_iso, end_iso, attendee_open_ids, operation_id, description="", calendar_id=""): + import re + if not re.fullmatch(r"[A-Za-z0-9_-]{8,80}", operation_id): + raise WorkError("创建日程需8到80位稳定operation_id,重试不得换ID。") + if not summary.strip() or len(summary) > 1000: + raise WorkError("日程主题不能为空或超过1000字符。") + start, end = datetime.fromisoformat(start_iso), datetime.fromisoformat(end_iso) + if start.tzinfo is None or end.tzinfo is None or end <= start: + raise WorkError("起止时间必须包含时区且结束晚于开始;不要猜测时区或时长。") + if any(not isinstance(x, str) or not x.startswith("ou_") for x in attendee_open_ids): + raise WorkError("参会人必须为当前应用身份域的open_id,请先搜索并确认,不能把姓名/工号直接当ID。") + if not calendar_id: + primary = self.primary_calendar() + if primary.get("status") != "completed": + return primary + calendar_id = primary["calendar"]["calendar_id"] + path = "/open-apis/calendar/v4/calendars/" + quote(calendar_id, safe="") + "/events" + body = {"summary": summary, "description": description, "need_notification": True, + "start_time": {"timestamp": str(int(start.timestamp())), "timezone": "Asia/Shanghai"}, + "end_time": {"timestamp": str(int(end.timestamp())), "timezone": "Asia/Shanghai"}} + result = self.api("POST", path, {"user_id_type": "open_id"}, body, operation_id) + event = result.get("data", {}).get("event", {}) + if result.get("status") == "duplicate" and result.get("previous_status") == "completed": + event = {"event_id": result.get("event_id"), "app_link": result.get("app_link")} + elif result.get("status") != "completed": + return result + if not event.get("event_id"): + raise WorkError("平台未返回event_id,不能认定创建成功或继续邀请。") + ids = sorted(set([self.owner] + list(attendee_open_ids))) + invite = self.api("POST", path + "/" + quote(event["event_id"], safe="") + "/attendees", + {"user_id_type": "open_id"}, + {"attendees": [{"type": "user", "user_id": x} for x in ids], "need_notification": True}, + operation_id + "_attendees") + invited = invite.get("status") == "completed" or (invite.get("status") == "duplicate" and invite.get("previous_status") == "completed") + return {"status": "completed" if invited else "partial", "event_created": True, + "calendar_id": calendar_id, "event_id": event["event_id"], "app_link": event.get("app_link"), + "attendees_invited": invited, "invite_result": invite, + "message": "日程已创建并提交参会人邀请;不代表对方已接受。" if invited else "日程已创建,但邀请未完成;保留原操作ID和event_id处理,勿重新创建日程。"} diff --git a/host-code/relay/work_tools.py b/host-code/relay/work_tools.py new file mode 100644 index 0000000..b1c6da4 --- /dev/null +++ b/host-code/relay/work_tools.py @@ -0,0 +1,145 @@ +"""Hermes native tools backed by the authenticated user-token relay.""" +import json +import os +import threading +import time +from pathlib import Path +import httpx +from .work_client import WorkClient, WorkError, AuthorizationRequired + +INSTRUCTIONS = """海能Work平台能力已接入本机专用工具:work_authorization、work_calendar、work_contacts、work_api_request。 +涉及飞书/海能Work日历或文档时优先调用这些工具,勿使用manage_connections寻找公有云连接器,勿索要或搜索App Secret。 +用户token仅由本机安全存储和ECS换取,第一次需要本人打开授权链接。工具返回authorization_required时将链接完整提供给本人,不能声称已经创建。 +当前日期以work_authorization返回的local_time为准。创建日程必须确定带时区的起止时间和真实参会人open_id。 +姓名/工号须用work_contacts搜索核实,多个候选必须澄清,不能猜open_id。平台返回partial表示已创建但邀请未完成,勿再次创建。 +同一写操作的operation_id必须保持不变,网络异常不得换ID重试。日程创建成功、邀请提交成功、对方接受是不同状态。 +""" + +_watching = set() +_watch_lock = threading.Lock() + + +def watch_authorization(directory): + """Claim within the server's 60-second result TTL, without keeping tokens in ECS.""" + name = str(directory) + with _watch_lock: + if name in _watching: + return + _watching.add(name) + def poll(): + client = None + try: + client = WorkClient(directory, os.environ.get("HN_WORK_RELAY_URL", "https://10.77.76.13")) + for _ in range(295): + time.sleep(2) + try: + if client.pending.exists() and client.read(client.pending).get("deadline", 0) <= time.time(): + return + if client.status().get("authorized"): + return + except Exception: + # Another local consumer may hold the short token lock. + continue + except Exception: + pass + finally: + if client: + client.close() + with _watch_lock: + _watching.discard(name) + threading.Thread(target=poll, daemon=True, name="work-user-auth-claim").start() + + +def register(directory): + from tools.registry import registry + import toolsets + definitions = [ + ("work_authorization", "查询或发起本人海能Work用户授权,返回可点击链接和本机时间;不返回token。", { + "type": "object", "properties": {}, "additionalProperties": False}), + ("work_calendar", "本人用户身份的日历:list/primary/list_events/get_event/create_event/invite。写操作operation_id需稳定,起止时间均带时区。", { + "type": "object", "properties": { + "action": {"type": "string", "enum": ["list", "primary", "list_events", "get_event", "create_event", "invite"]}, + "calendar_id": {"type": "string"}, "event_id": {"type": "string"}, + "summary": {"type": "string"}, "description": {"type": "string"}, + "start_iso": {"type": "string", "description": "ISO8601,例如2026-10-27T09:00:00+08:00"}, + "end_iso": {"type": "string"}, "attendee_open_ids": {"type": "array", "items": {"type": "string"}}, + "operation_id": {"type": "string", "description": "8-80位稳定操作ID,同一意图重试不能更换"}, + "params": {"type": "object"}}, "required": ["action"]}), + ("work_contacts", "使用本人user token搜索参会人;query可为姓名或工号,返回候选,不能未经核实猜ID。open_id可读取具体用户信息。", { + "type": "object", "properties": {"query": {"type": "string"}, "open_id": {"type": "string"}}}), + ("work_api_request", "携本人user token代理受支持业务API(日历CRUD/邀请、通讯录查询、文档云盘知识库读取)。禁止认证接口,不支持的路径返回403。写操作必须operation_id。", { + "type": "object", "properties": {"method": {"type": "string", "enum": ["GET", "POST", "PATCH", "DELETE"]}, + "path": {"type": "string"}, "params": {"type": "object"}, "body": {"type": "object"}, + "operation_id": {"type": "string"}}, "required": ["method", "path"]}), + ] + + def handler_for(name): + def handle(args, **kwargs): + client = None + try: + client = WorkClient(directory, os.environ.get("HN_WORK_RELAY_URL", "https://10.77.76.13")) + if name == "work_authorization": + result = client.status() + if not result.get("authorized"): + watch_authorization(directory) + elif name == "work_api_request": + result = client.api(**args) + elif name == "work_contacts": + from urllib.parse import quote + result = client.api("GET", "/open-apis/contact/v3/users/" + quote(args["open_id"], safe=""), + {"user_id_type": "open_id"}) if args.get("open_id") else client.api( + "GET", "/open-apis/search/v1/user", {"query": args.get("query", ""), "page_size": 20}) + else: + result = calendar(client, args) + return json.dumps(result, ensure_ascii=False) + except AuthorizationRequired as error: + watch_authorization(directory) + return json.dumps({"status": "authorization_required", "authorization_url": error.url, "message": str(error)}, ensure_ascii=False) + except (WorkError, ValueError, KeyError) as error: + return json.dumps({"error": str(error)[:500], "status": "failed"}, ensure_ascii=False) + except Exception as error: + return json.dumps({"error": type(error).__name__ + ";操作状态未确认,未自动重试。"}, ensure_ascii=False) + finally: + if client: + client.close() + return handle + + for name, description, parameters in definitions: + registry.register(name=name, toolset="hainuo_work", schema={"name": name, "description": description, "parameters": parameters}, + handler=handler_for(name), description=description) + toolsets.TOOLSETS["hainuo_work"] = {"description": "海能Work本人用户授权及日历业务", "tools": [x[0] for x in definitions], "includes": []} + # Present these four small schemas directly; otherwise Hermes defers custom + # toolsets behind tool_search and can wrongly search a public-cloud connector. + for name, _, _ in definitions: + if name not in toolsets._HERMES_CORE_TOOLS: + toolsets._HERMES_CORE_TOOLS.append(name) + + +def calendar(client, args): + from urllib.parse import quote + action = args["action"] + if action == "primary": + return client.primary_calendar() + if action == "list": + return client.api("GET", "/open-apis/calendar/v4/calendars", args.get("params", {})) + if action == "create_event": + return client.create_event(**{k: v for k, v in args.items() if k != "action" and k != "params"}) + calendar_id = args.get("calendar_id") + if not calendar_id: + primary = client.primary_calendar() + if primary.get("status") != "completed": + return primary + calendar_id = primary["calendar"]["calendar_id"] + base = "/open-apis/calendar/v4/calendars/" + quote(calendar_id, safe="") + "/events" + if action == "list_events": + return client.api("GET", base, args.get("params", {})) + base += "/" + quote(args["event_id"], safe="") + if action == "get_event": + return client.api("GET", base, {"user_id_type": "open_id"}) + if action == "invite": + ids = args.get("attendee_open_ids", []) + if not ids or any(not x.startswith("ou_") for x in ids): + raise WorkError("需要已确认的参会人open_id。") + return client.api("POST", base + "/attendees", {"user_id_type": "open_id"}, + {"attendees": [{"type": "user", "user_id": x} for x in ids], "need_notification": True}, args["operation_id"]) + raise WorkError("不支持的日历操作。") diff --git a/host-code/requirements.lock b/host-code/requirements.lock new file mode 100644 index 0000000..c763938 --- /dev/null +++ b/host-code/requirements.lock @@ -0,0 +1,41 @@ +annotated-types==0.8.0 +anyio==4.15.1 +certifi==2026.10.23 +cffi==2.1.1 +charset-normalizer==3.5.2 +click==8.5.0 +colorama==0.4.6 +cryptography==46.0.3 +fakeredis==2.32.0 +fastapi==0.119.1 +h11==0.16.0 +httpcore==1.0.9 +httpx==0.28.1 +idna==3.20 +iniconfig==2.3.1 +lark-oapi==1.5.3 +packaging==26.3 +pluggy==1.7.0 +psycopg==3.2.12 +psycopg-binary==3.2.12 +pycparser==3.12 +pycryptodome==4.2.0 +pydantic==2.14.1 +pydantic-settings==2.11.0 +pydantic_core==2.50.1 +Pygments==2.21.0 +pytest==8.4.2 +pytest-asyncio==1.2.0 +python-dotenv==1.2.4 +PyYAML==6.0.3 +redis==6.4.0 +requests==2.34.2 +requests-toolbelt==1.0.0 +sortedcontainers==2.4.0 +starlette==0.48.0 +typing-inspection==0.4.4 +typing_extensions==4.16.0 +tzdata==2026.5 +urllib3==2.8.0 +uvicorn==0.38.0 +websockets==15.0.1 diff --git a/patch-inventory.json b/patch-inventory.json new file mode 100644 index 0000000..651ada8 --- /dev/null +++ b/patch-inventory.json @@ -0,0 +1,93 @@ +{ + "deployment_source_files": [ + "scripts/backup.py", + "scripts/bootstrap.sh", + "scripts/check_arm64_images.py", + "scripts/configure_feishu_longconn.py", + "scripts/fetch_official_images.py", + "scripts/hainuo_native_api.py", + "scripts/install_offline.sh", + "scripts/package.py", + "scripts/package_offline.py", + "scripts/package_wheels.py", + "scripts/preflight.sh", + "scripts/start-feishu-longconn.sh", + "scripts/start.sh", + "scripts/update-h5.sh", + "scripts/update-userapi.sh", + "scripts/update-work1067.sh", + "scripts/verify_deployment.sh", + "scripts/verify_image_archives.py", + "deploy/Caddyfile", + "deploy/hainuo-relay@.service", + "docs/ACCEPTANCE.md", + "docs/CHANGES.md", + "docs/DEPLOY_LINUX_ARM64.md", + "docs/FEISHU_PRIVATE_LONGCONN.md", + "docs/H5_PAIR_CHAT.md", + "docs/OFFLINE_IMAGES.md", + "docs/PROTOCOL.md", + "docs/SPLIT_SIX_NODES.md", + "docs/TEST_REPORT.md", + "tests/__init__.py", + "tests/conftest.py", + "tests/test_agent.py", + "tests/test_boundaries.py", + "tests/test_crypto.py", + "tests/test_feishu_longconn_setup.py", + "tests/test_hainuo1065.py", + "tests/test_image_archive.py", + "tests/test_integration.py", + "tests/test_platform.py", + "tests/test_postgres.py", + "tests/test_web_native_chain.py", + "tests/test_web_session.py", + "compose.acceptance.yaml", + "compose.autoreg.yaml", + "compose.edge-fix.yaml", + "compose.h5.yaml", + "compose.offline.yaml", + "compose.userapi.yaml", + "compose.work.yaml", + "compose.yaml" + ], + "patches": [ + { + "original_filename": "海诺Work_完整工具_ECS更新.tar.gz", + "original_sha256": "51c062dff81e268e9250ad5c222b9f91077131a636453229a4507c2d57315525", + "source_members_exported": 22, + "folder": "51c062dff81e268e", + "status": "historical package found on server; not proof of deployment" + }, + { + "original_filename": "海诺Work_用户授权与日历_ECS更新.tar.gz", + "original_sha256": "02ffd84040a628b74bf125191c7692029ca5ec4ed153e9a02eec2383b9e52cda", + "source_members_exported": 25, + "folder": "02ffd84040a628b7", + "status": "historical package found on server; not proof of deployment" + }, + { + "original_filename": "海诺Work绑定_1.067_自动注册_ECS更新.tar.gz", + "original_sha256": "951d53cd16a9de5fd224e18d946bc45eb8ce58f32c82f2218f7fa96301f8e754", + "source_members_exported": 22, + "folder": "951d53cd16a9de5f", + "status": "historical package found on server; not proof of deployment" + }, + { + "original_filename": "hainuo-relay-feishu-update-20261026.tar.gz", + "original_sha256": "a8137fe7e675b0a9c444e3944d634a2c02effaf9277aced173098d6fef41fb76", + "source_members_exported": 22, + "folder": "a8137fe7e675b0a9", + "status": "historical package found on server; not proof of deployment" + }, + { + "original_filename": "hainuo-relay-h5-update-20261026.tar.gz", + "original_sha256": "0de3db40fc2044b022ba42ec02471f34a0e1db45d195a17c5cb186367e533bc1", + "source_members_exported": 33, + "folder": "0de3db40fc2044b0", + "status": "historical package found on server; not proof of deployment" + } + ], + "compose_yaml": "sanitized templates, NOT directly deployable; environment values redacted", + "note": "Only source members copied from patch archives; raw archives and credentials excluded." +} \ No newline at end of file diff --git a/patch-source/02ffd84040a628b7/Dockerfile.offline b/patch-source/02ffd84040a628b7/Dockerfile.offline new file mode 100644 index 0000000..8af3dff --- /dev/null +++ b/patch-source/02ffd84040a628b7/Dockerfile.offline @@ -0,0 +1,10 @@ +FROM python:3.12-slim-bookworm +ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 +WORKDIR /app +COPY wheelhouse /wheels +COPY requirements.lock pyproject.toml ./ +COPY relay ./relay +RUN pip install --no-index --find-links=/wheels --no-cache-dir -r requirements.lock && rm -rf /wheels +RUN chmod -R a+rX /app +USER 10001:10001 +ENTRYPOINT ["python", "-m", "relay.cli"] diff --git a/patch-source/02ffd84040a628b7/compose.userapi.yaml b/patch-source/02ffd84040a628b7/compose.userapi.yaml new file mode 100644 index 0000000..1caaa41 --- /dev/null +++ b/patch-source/02ffd84040a628b7/compose.userapi.yaml @@ -0,0 +1,6 @@ +services: + connector: + environment: + HN_OAUTH_ENABLED: + HN_FEISHU_AUTHORIZE_URL: ${HN_FEISHU_AUTHORIZE_URL} + HN_FEISHU_OAUTH_SCOPES: ${HN_FEISHU_OAUTH_SCOPES} diff --git a/patch-source/02ffd84040a628b7/relay/__init__.py b/patch-source/02ffd84040a628b7/relay/__init__.py new file mode 100644 index 0000000..32754a7 --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/__init__.py @@ -0,0 +1 @@ +"""Hainuo relay 0.1.0.""" diff --git a/patch-source/02ffd84040a628b7/relay/cli.py b/patch-source/02ffd84040a628b7/relay/cli.py new file mode 100644 index 0000000..fad7979 --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/cli.py @@ -0,0 +1,216 @@ +import argparse +import asyncio +import json +import os +import secrets +import ssl +import uuid +from datetime import datetime, timedelta, timezone +from pathlib import Path + +import httpx +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ed25519 +from cryptography.x509.oid import NameOID + +from .crypto import issue_ca, issue_device, load_private, pem_private + + +def write_new(path, data, private=False): + path.parent.mkdir(parents=True, exist_ok=True) + with path.open("xb") as f: + f.write(data if isinstance(data, bytes) else data.encode()) + if private and os.name != "nt": + path.chmod(0o600) + + +def init_project(root: Path, hostname: str): + if (root / ".env").exists(): + raise RuntimeError(".env exists; init never overwrites credentials") + ca_key, ca = issue_ca() + signer = ed25519.Ed25519PrivateKey.generate() + write_new(root / "operator/ca.key", pem_private(ca_key), True) + write_new(root / "runtime/trust/ca.pem", ca.public_bytes(serialization.Encoding.PEM)) + write_new(root / "runtime/connector/connector.key", pem_private(signer), True) + write_new(root / "runtime/trust/connector.pub", signer.public_key().public_bytes( + serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo)) + tls_key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, hostname)]) + now = datetime.now(timezone.utc) + tls = (x509.CertificateBuilder().subject_name(name).issuer_name(name).public_key(tls_key.public_key()) + .serial_number(x509.random_serial_number()).not_valid_before(now - timedelta(minutes=5)) + .not_valid_after(now + timedelta(days=30)) + .add_extension(x509.SubjectAlternativeName([x509.DNSName(hostname)]), critical=False) + .sign(tls_key, algorithm=None)) + write_new(root / "runtime/tls/server.key", pem_private(tls_key), True) + write_new(root / "runtime/tls/server.pem", tls.public_bytes(serialization.Encoding.PEM)) + redis_secret = secrets.token_hex(32) + db_secret = secrets.token_hex(32) + env = (f"HN_ENVIRONMENT=development\nHN_PUBLIC_HOST={hostname}\nHN_PUBLIC_URL=https://{hostname}\n" + f"HN_ROUTER_URL=wss://{hostname}/device/ws\nHN_ADMIN_SECRET={secrets.token_hex(32)}\n" + f"HN_INTERNAL_SECRET={secrets.token_hex(32)}\nHN_REDIS_PASSWORD={redis_secret}\n" + f"HN_REDIS_URL=redis://:{redis_secret}@redis:6379/0\n" + f"HN_POSTGRES_PASSWORD={db_secret}\nHN_DATABASE_URL=postgresql://hainuo:{db_secret}@postgres:5432/hainuo\n" + "HN_FEISHU_ENABLED=false\nHN_FEISHU_API_BASE=https://open.feishu.cn\n" + "HN_FEISHU_AUTHORIZE_URL=https://open.feishu.cn/open-apis/authen/v1/authorize\n" + "HN_FEISHU_APP_ID=\nHN_FEISHU_APP_SECRET=\nHN_FEISHU_ENCRYPT_KEY=\n" + "HN_FEISHU_VERIFICATION_TOKEN=\nHN_PROXY_ENABLED=false\nHN_DEVICE_DOMAIN=\n") + write_new(root / ".env", env, True) + print("已生成配置、连接器密钥、设备CA及临时TLS证书;未输出凭据。") + print("operator/ca.key 仅保留在签发工作站,不要上传到ECS。临时TLS证书上线前须替换。") + + +def device_request(directory): + host = uuid.uuid4().hex + private = ed25519.Ed25519PrivateKey.generate() + write_new(directory / "device.key", pem_private(private), True) + public = private.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode() + write_new(directory / "request.json", json.dumps({"host_id": host, "public_key": public}, indent=2)) + print("已在本机生成私钥和 request.json;仅将request.json交给签发工作站。") + + +def issue(root, request_file, output): + request = json.loads(request_file.read_text(encoding="utf-8")) + host = request["host_id"] + if len(host) != 32 or any(c not in "0123456789abcdef" for c in host): + raise ValueError("invalid host identity") + ca = x509.load_pem_x509_certificate((root / "runtime/trust/ca.pem").read_bytes()) + public = serialization.load_pem_public_key(request["public_key"].encode()) + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("Ed25519 required") + cert = issue_device(load_private(root / "operator/ca.key"), ca, host, public) + pem = cert.public_bytes(serialization.Encoding.PEM) + write_new(output / "device.pem", pem) + write_new(output / "ca.pem", (root / "runtime/trust/ca.pem").read_bytes()) + write_new(output / "connector.pub", (root / "runtime/trust/connector.pub").read_bytes()) + write_new(output / "device.json", json.dumps({"host_id": host, "owner": None}, indent=2)) + write_new(output / "registration.json", json.dumps({"host_id": host, "cert": pem.decode()}, indent=2)) + print("已签发设备证书;将device.pem、ca.pem、connector.pub、device.json交回原设备。") + + +def main(): + parser = argparse.ArgumentParser(description="海诺中继部署和设备工具") + sub = parser.add_subparsers(dest="command", required=True) + p = sub.add_parser("init") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--hostname", required=True) + p = sub.add_parser("serve") + p.add_argument("role", choices=["connector", "router"]) + p.add_argument("--host", default="127.0.0.1") + p.add_argument("--port", type=int, default=8000) + p = sub.add_parser("device-request") + p.add_argument("--directory", type=Path, required=True) + p = sub.add_parser("issue-device") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--request", type=Path, required=True) + p.add_argument("--output", type=Path, required=True) + p = sub.add_parser("register") + p.add_argument("--registration", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("bind") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("device") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p.add_argument("--test-root", type=Path) + p.add_argument("--agent-url", help="Explicit loopback read-only Agent adapter endpoint") + p.add_argument("--hainuo-config", type=Path, help="Native installer-1.065 API adapter config; alternative to --agent-url") + p = sub.add_parser("hainuo-probe") + p.add_argument("--config", type=Path, required=True) + p = sub.add_parser("web-pair") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + for command in ("status", "binding-code", "dispatch", "revoke", "unbind", "reconcile"): + p = sub.add_parser(command) + p.add_argument("--url", default="http://127.0.0.1:18081") + p.add_argument("--tls-ca", type=Path) + if command in {"binding-code", "dispatch"}: + p.add_argument("--owner", required=True) + if command == "dispatch": + p.add_argument("--text", required=True) + p.add_argument("--turn-id", default=None) + if command in {"revoke", "unbind"}: + p.add_argument("--host-id", required=True) + args = parser.parse_args() + if args.command == "init": + init_project(args.root, args.hostname) + elif args.command == "serve": + import uvicorn + uvicorn.run(f"relay.{args.role}:create_app", factory=True, host=args.host, port=args.port, + workers=1, access_log=False, proxy_headers=False, + ws_max_size=262144, ws_max_queue=16, log_level="warning") + elif args.command == "device-request": + device_request(args.directory) + elif args.command == "issue-device": + issue(args.root, args.request, args.output) + elif args.command == "register": + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, follow_redirects=False, timeout=15) as client: + response = client.post(args.url.rstrip("/") + "/admin/devices", + headers={"X-Admin-Secret": secret}, + json=json.loads(args.registration.read_text(encoding="utf-8"))) + if response.status_code != 201: + raise RuntimeError(f"register failed: HTTP {response.status_code}") + print("设备登记成功。") + elif args.command == "bind": + import getpass + from .device import bind_device + code = getpass.getpass("Binding code: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + asyncio.run(bind_device(args.directory, args.url, code, tls_verify=context)) + print("绑定成功。") + elif args.command == "device": + from .device import run_device + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else None + asyncio.run(run_device(args.directory, args.url, root=args.test_root, ssl_context=context, + agent_url=args.agent_url, hainuo_config=args.hainuo_config)) + elif args.command == "hainuo-probe": + from .hainuo1065 import Hainuo1065Agent + async def probe(): + adapter = Hainuo1065Agent(args.config) + try: + print(json.dumps(await adapter.probe(), ensure_ascii=False, indent=2)) + finally: + await adapter.close() + asyncio.run(probe()) + elif args.command == "web-pair": + from .device import pair_browser + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + result = asyncio.run(pair_browser(args.directory, args.url, tls_verify=context)) + print("网页地址:" + result["url"]) + print("一次性网页配对码(120秒有效):" + result["code"]) + else: + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, timeout=200, follow_redirects=False) as client: + paths = {"status": "/admin/devices", "binding-code": "/admin/test/binding-code", + "dispatch": "/admin/test/dispatch", "reconcile": "/admin/reconcile"} + if args.command in {"revoke", "unbind"}: + if len(args.host_id) != 32 or any(c not in "0123456789abcdef" for c in args.host_id): + raise ValueError("invalid host identity") + path = f"/admin/devices/{args.host_id}/{args.command}" + else: + path = paths[args.command] + body = None + if args.command in {"binding-code", "dispatch"}: + body = {"open_id": args.owner, "text": getattr(args, "text", "binding")} + if getattr(args, "turn_id", None): + body["turn_id"] = args.turn_id + response = client.request("GET" if args.command == "status" else "POST", args.url.rstrip("/") + path, + headers={"X-Admin-Secret": secret}, json=body) + if response.status_code != 200: + raise RuntimeError(f"operation failed: HTTP {response.status_code}") + print(json.dumps(response.json(), ensure_ascii=False, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/patch-source/02ffd84040a628b7/relay/client_manage.py b/patch-source/02ffd84040a628b7/relay/client_manage.py new file mode 100644 index 0000000..6b543fe --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/client_manage.py @@ -0,0 +1,293 @@ +"""Per-user Windows connection companion. No platform/application secrets.""" +import argparse +import asyncio +import json +import os +import secrets +import socket +import ssl +import subprocess +import sys +import time +import uuid +from pathlib import Path + +import httpx +import psutil +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey +from .crypto import load_private, load_public, pem_private, raw_public, sign, verified, verify_device +from .windows_keys import protect + +BASE = Path(__file__).resolve().parent.parent +URL = "https://10.77.76.13" + + +def save(path, value): + temp = path.with_suffix(path.suffix + ".tmp") + temp.write_text(json.dumps(value, ensure_ascii=False, indent=2), encoding="utf-8") + os.replace(temp, path) + + +def secure_directory(directory): + directory.mkdir(parents=True, exist_ok=True) + # SID avoids localized account names. Fail closed if ACL protection fails. + result = subprocess.run(["whoami", "/user", "/fo", "csv", "/nh"], capture_output=True, text=True, check=True) + import csv + sid = list(csv.reader([result.stdout.strip()]))[0][1] + subprocess.run(["icacls", str(directory), "/inheritance:r", "/grant:r", f"*{sid}:(OI)(CI)F", "*S-1-5-18:(OI)(CI)F"], + capture_output=True, check=True, creationflags=subprocess.CREATE_NO_WINDOW) + + +def prepare(directory): + secure_directory(directory) + for name in ("server.pem", "ca.pem", "connector.pub"): + target = directory / name + if not target.exists(): + target.write_bytes((BASE / "trust" / name).read_bytes()) + if not (directory / "device.json").exists(): + if (directory / "device.key.dpapi").exists() or (directory / "device.key").exists(): + raise RuntimeError("发现不完整的设备身份,请保留目录并联系管理员恢复。") + private = Ed25519PrivateKey.generate() + (directory / "device.key.dpapi").write_bytes(protect(pem_private(private))) + save(directory / "device.json", {"host_id": uuid.uuid4().hex, "owner": None}) + + +def tls(directory): + return ssl.create_default_context(cafile=str(directory / "server.pem")) + + +def import_existing(directory, source): + """Explicit migration; only public trust and this PC's identity, never app keys.""" + if source.resolve() == directory.resolve(): + raise ValueError("请选择旧设备目录。") + if any((directory / name).exists() for name in ("device.json", "device.key.dpapi", "device.key")): + raise RuntimeError("本目录已有设备身份。请使用已有身份继续,不能用导入覆盖。") + config = json.loads((source / "device.json").read_text(encoding="utf-8-sig")) + for name in ("server.pem", "ca.pem", "connector.pub"): + if (source / name).read_bytes().strip() != (BASE / "trust" / name).read_bytes().strip(): + raise ValueError("旧设备的服务信任与本安装包不符,请联系管理员。") + cert = (source / "device.pem").read_text(encoding="utf-8") + public = verify_device(cert, (source / "ca.pem").read_bytes(), config["host_id"]) + private = load_private(source / "device.key") + if raw_public(public) != raw_public(private.public_key()): + raise ValueError("设备证书与本机私钥不匹配。") + secure_directory(directory) + for name in ("device.pem", "server.pem", "ca.pem", "connector.pub"): + (directory / name).write_bytes((source / name).read_bytes()) + (directory / "device.key.dpapi").write_bytes(protect(pem_private(private))) + save(directory / "device.json", config) + # Preserve local replay journal using SQLite's consistent backup API. + if (source / "journal.sqlite").is_file(): + import sqlite3 + with sqlite3.connect(source / "journal.sqlite") as original, sqlite3.connect(directory / "journal.sqlite") as target: + original.backup(target) + return {"message": "旧设备导入成功,Work绑定已保留。请先停止旧PowerShell桥接窗口,再点击启动连接。"} + + +def enrollment(directory): + prepare(directory) + cfg = json.loads((directory / "device.json").read_text()) + if (directory / "device.pem").exists(): + verify_device((directory / "device.pem").read_text(), (directory / "ca.pem").read_bytes(), cfg["host_id"]) + return {"message": "设备已注册,可在Work获取绑定码后绑定。已有绑定可直接启动连接。"} + with httpx.Client(verify=tls(directory), trust_env=False, timeout=15) as http: + key = load_private(directory / "device.key") + proof = sign({"host_id": cfg["host_id"], "purpose": "device-enroll-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + response = http.post(URL + "/device/register", json={"host_id": cfg["host_id"], "proof": proof, + "public_key": key.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode()}) + if response.status_code not in {200, 201}: + raise RuntimeError("自动注册失败:HTTP " + str(response.status_code) + ";请确认ECS已安装1.067自动注册更新。") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt.get("status") != "registered" or receipt["host_id"] != cfg["host_id"]: + raise ValueError("注册回执身份不符") + public = verify_device(receipt["cert"], (directory / "ca.pem").read_bytes(), cfg["host_id"]) + if raw_public(public) != raw_public(key.public_key()): + raise ValueError("注册证书公钥不符") + temp = directory / "device.pem.tmp" + temp.write_text(receipt["cert"], encoding="utf-8") + os.replace(temp, directory / "device.pem") + (directory / "enrollment.json").unlink(missing_ok=True) + return {"message": "设备已自动注册,无需管理员审批。请在Work获取绑定码,填写后点击绑定并连接。"} + + +def managed_processes(directory): + """Only processes with our absolute entry path AND exact device directory.""" + state = directory / "processes.json" + if not state.exists(): + return [] + result = [] + for item in json.loads(state.read_text()).get("processes", []): + try: + proc = psutil.Process(item["pid"]) + command = proc.cmdline() + if (abs(proc.create_time() - item["created"]) < 0.1 and str(BASE / "entry.py") in command + and str(directory) in command): + result.append(proc) + except psutil.Error: + pass + return result + + +def stop(directory): + procs = managed_processes(directory) + # Supervisor owns and terminates only its own child handles in its finally block. + (directory / "stop.flag").touch() + _, alive = psutil.wait_procs(procs, timeout=8) + for proc in alive: + proc.terminate() + return {"message": "已停止本安装包启动的连接进程。手工启动的旧桥接请在原窗口按Ctrl+C停止。"} + + +def start(directory, root): + from .process_lock import DeviceProcessLock + with DeviceProcessLock(directory / "control"): + return _start(directory, root) + + +def _start(directory, root): + cfg = json.loads((directory / "device.json").read_text()) + if not cfg.get("owner") or not (directory / "device.pem").exists(): + raise RuntimeError("请先完成设备准入和Work绑定。") + if managed_processes(directory): + return {"message": "连接进程已运行,请点击状态查看。"} + (directory / "stop.flag").unlink(missing_ok=True) + log = (directory / "bridge.log").open("a", encoding="utf-8") + proc = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "supervise", "--directory", str(directory), "--root", str(root)], + stdout=log, stderr=log, creationflags=subprocess.CREATE_NO_WINDOW) + log.close() + save(directory / "processes.json", {"processes": [{"pid": proc.pid, "created": psutil.Process(proc.pid).create_time()}]}) + return {"message": "连接进程已启动;约30秒后点击状态,再到Work发送 /主机名 验证。"} + + +def supervise(directory, root): + from .process_lock import DeviceProcessLock + lockdir = directory / "supervisor" + with DeviceProcessLock(lockdir): + children = [] + def record_children(): + processes = [psutil.Process(os.getpid())] + [psutil.Process(p.pid) for p in children if p.poll() is None] + save(directory / "processes.json", {"processes": [{"pid": p.pid, "created": p.create_time()} for p in processes]}) + try: + # Check local bind availability without an HTTP connection. Windows + # filtering can time out connections to a closed port rather than + # refuse them; that must not prevent starting our own listener. + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as port_probe: + try: + if os.name == "nt": + port_probe.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1) + port_probe.bind(("127.0.0.1", 18643)) + except OSError: + raise RuntimeError("原生API端口18643被占用或不允许绑定,请先检查旧连接。") from None + native = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "native", "--directory", str(directory), "--root", str(root)]) + children.append(native) + record_children() + for _ in range(90): + if (directory / "stop.flag").exists(): + return + if native.poll() is not None: + raise RuntimeError("本机原生API启动失败,请检查安装配置。") + try: + from .hainuo1065 import Hainuo1065Agent + async def probe(): + agent = Hainuo1065Agent(directory / "hainuo-native.json") + try: + return await agent.probe() + finally: + await agent.close() + asyncio.run(probe()) + break + except Exception: + time.sleep(1) + else: + raise RuntimeError("本机原生API未就绪。") + bridge = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "bridge", "--directory", str(directory), "--root", str(root)]) + children.append(bridge) + record_children() + while not (directory / "stop.flag").exists(): + if any(proc.poll() is not None for proc in children): + raise RuntimeError("连接子进程已退出,请查看状态后重新启动。") + time.sleep(0.5) + finally: + for proc in reversed(children): + if proc.poll() is None: + proc.terminate() + try: + proc.wait(timeout=5) + except subprocess.TimeoutExpired: + proc.kill() + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("action", choices=["authorize", "import", "enroll", "bind", "start", "stop", "status", "pair", "supervise", "native", "bridge"]) + parser.add_argument("--directory", type=Path, required=True) + parser.add_argument("--root", type=Path, required=True) + args = parser.parse_args() + directory, root = args.directory.resolve(), args.root.resolve() + directory.mkdir(parents=True, exist_ok=True) + try: + if args.action == "authorize": + from .work_client import WorkClient + client = WorkClient(directory) + try: + result = client.status() + result["message"] += ("\n" + result["authorization_url"]) if result.get("authorization_url") else "" + finally: + client.close() + elif args.action == "import": + result = import_existing(directory, Path(sys.stdin.readline().strip())) + elif args.action == "enroll": + result = enrollment(directory) + elif args.action == "bind": + from .device import bind_device + code = sys.stdin.readline().strip() + if not code: + raise ValueError("请填写Work机器人新生成的绑定码。") + enrollment(directory) + stop(directory) + asyncio.run(bind_device(directory, URL, code, tls_verify=tls(directory))) + result = start(directory, root) + result["message"] = "Work绑定成功。" + result["message"] + elif args.action == "start": + result = start(directory, root) + elif args.action == "stop": + result = stop(directory) + elif args.action == "pair": + from .device import pair_browser + receipt = asyncio.run(pair_browser(directory, URL, tls_verify=tls(directory))) + result = {"message": "网页:" + receipt["url"] + "\n一次性配对码(120秒):" + receipt["code"]} + elif args.action == "status": + cfg = json.loads((directory / "device.json").read_text()) if (directory / "device.json").exists() else {} + log = directory / "bridge.log" + tail = log.read_bytes()[-6000:].decode("utf-8", errors="replace") if log.exists() else "" + result = {"message": "设备:" + cfg.get("host_id", "未申请") + "\nWork身份:" + (cfg.get("owner") or "未绑定") + + "\n连接进程:" + ("运行中(请在Work发送 /主机名 确认当前链路)" if managed_processes(directory) else "未运行") + "\n" + tail[-2500:]} + elif args.action == "supervise": + return supervise(directory, root) + elif args.action == "native": + os.environ["HN_WORK_DEVICE_DIR"] = str(directory) + import importlib.util + spec = importlib.util.spec_from_file_location("native_companion", BASE / "hainuo_native_api.py") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + module.enable_profile_tools(root, "hainuo") + key = module.prepare_environment(root, "hainuo", 18643, directory / "hainuo-native.json", enable_tools=True) + import logging + logging.disable(logging.CRITICAL) + return asyncio.run(module.serve_native(18643, key, enable_tools=True)) + else: + from .device import run_device + return asyncio.run(run_device(directory, "wss://10.77.76.13/device/ws", ssl_context=tls(directory), hainuo_config=directory / "hainuo-native.json")) + print(json.dumps(result, ensure_ascii=False)) + except KeyboardInterrupt: + pass + except Exception as error: + # Native exception bodies may include URLs or secrets; emit bounded known messages only. + message = str(error) if isinstance(error, (RuntimeError, ValueError)) else type(error).__name__ + ";请检查网络、证书、安装目录。" + print(json.dumps({"message": message[:600], "error": True}, ensure_ascii=False)) + raise SystemExit(1) from None + + +if __name__ == "__main__": + main() diff --git a/patch-source/02ffd84040a628b7/relay/common.py b/patch-source/02ffd84040a628b7/relay/common.py new file mode 100644 index 0000000..c112b99 --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/common.py @@ -0,0 +1,61 @@ +import hashlib +import hmac +import json +import time +from fastapi import HTTPException, Request +from starlette.responses import JSONResponse + + +def require_secret(actual, expected): + if not actual or not hmac.compare_digest(actual, expected): + raise HTTPException(401, "unauthorized") + + +async def rate_limit(redis, bucket: str, limit=60, seconds=60): + # Atomic counter + expiry: a crash cannot leave a permanent bucket. + key = "hn:rate:" + hashlib.sha256(bucket.encode()).hexdigest() + ":" + str(int(time.time()) // seconds) + pipe = redis.pipeline(transaction=True) + pipe.incr(key) + pipe.expire(key, seconds * 2) + result = (await pipe.execute())[0] + if int(result) > limit: + raise HTTPException(429, "rate limited", headers={"Retry-After": str(seconds)}) + + +class BodyLimitMiddleware: + def __init__(self, app, max_body=262144): + self.app, self.max_body = app, max_body + + async def __call__(self, scope, receive, send): + if scope["type"] != "http": + return await self.app(scope, receive, send) + messages, size = [], 0 + while True: + message = await receive() + if message["type"] == "http.disconnect": + return + size += len(message.get("body", b"")) + if size > self.max_body: + return await JSONResponse({"detail": "body too large"}, status_code=413)(scope, receive, send) + messages.append(message) + if not message.get("more_body", False): + break + async def buffered(): + return messages.pop(0) if messages else await receive() + await self.app(scope, buffered, send) + + +def no_cache(response): + response.headers["Cache-Control"] = "no-store" + response.headers["X-Content-Type-Options"] = "nosniff" + response.headers["Referrer-Policy"] = "no-referrer" + return response + + +async def json_pub(redis, channel, data): + return await redis.publish(channel, json.dumps(data, separators=(",", ":"))) + + +def peer(request: Request): + # Do not trust arbitrary X-Forwarded-For; configure uvicorn trusted proxies explicitly. + return request.client.host if request.client else "unknown" diff --git a/patch-source/02ffd84040a628b7/relay/config.py b/patch-source/02ffd84040a628b7/relay/config.py new file mode 100644 index 0000000..c4c32ac --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/config.py @@ -0,0 +1,66 @@ +from pathlib import Path +from pydantic import model_validator +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + model_config = SettingsConfigDict(env_prefix="HN_", extra="ignore", hide_input_in_errors=True) + environment: str = "production" + role: str = "connector" + redis_url: str = "redis://127.0.0.1:6379/0" + data_dir: Path = Path("runtime/connector") + database_url: str = "" + admin_secret: str = "" + internal_secret: str = "" + ca_cert: Path = Path("runtime/pki/ca.pem") + connector_key: Path = Path("runtime/pki/connector.key") + connector_public_key: Path = Path("runtime/pki/connector.pub") + public_url: str = "https://relay.example.invalid" + router_url: str = "wss://relay.example.invalid/device/ws" + device_domain: str = "" + feishu_api_base: str = "https://open.feishu.cn" + feishu_authorize_url: str = "https://open.feishu.cn/open-apis/authen/v1/authorize" + feishu_oauth_scopes: str = "" + feishu_app_id: str = "" + feishu_app_secret: str = "" + feishu_encrypt_key: str = "" + feishu_verification_token: str = "" + feishu_enabled: bool = False + oauth_enabled: bool = True + feishu_event_source: str = "longconn" + proxy_enabled: bool = False + # A fixed allowlist, never a user supplied upstream URL. + proxy_paths: str = "/open-apis/drive/v1/files,/open-apis/docx/v1/documents" + request_timeout: int = 45 + max_body: int = 262144 + max_pending: int = 1000 + max_devices: int = 1000 + max_connections: int = 1000 + max_result: int = 16000 + allow_insecure_local: bool = False + enrollment_signer_url: str = "" + + @model_validator(mode="after") + def validate_runtime(self): + if self.environment not in {"production", "test", "development"}: + raise ValueError("invalid environment") + if self.role == "connector" and min(len(self.admin_secret), len(self.internal_secret)) < 32: + raise ValueError("admin/internal secrets must each be at least 32 characters") + if self.role == "longconn" and len(self.internal_secret) < 32: + raise ValueError("internal credential must be at least 32 characters") + if self.role == "connector" and self.admin_secret == self.internal_secret: + raise ValueError("admin and internal credentials must be different") + if self.environment == "production": + if self.allow_insecure_local or not self.public_url.startswith("https://"): + raise ValueError("production requires HTTPS") + if not self.router_url.startswith("wss://"): + raise ValueError("production requires WSS") + if not self.feishu_api_base.startswith("https://"): + raise ValueError("platform API requires HTTPS") + if self.feishu_event_source not in {"longconn", "webhook"}: + raise ValueError("invalid platform event source") + if self.feishu_enabled and not all((self.feishu_app_id, self.feishu_app_secret)): + raise ValueError("platform app ID and secret are required") + if self.feishu_enabled and self.feishu_event_source == "webhook" and not all((self.feishu_encrypt_key, self.feishu_verification_token)): + raise ValueError("webhook requires encryption key and verification token") + return self diff --git a/patch-source/02ffd84040a628b7/relay/connector.py b/patch-source/02ffd84040a628b7/relay/connector.py new file mode 100644 index 0000000..c01f3b6 --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/connector.py @@ -0,0 +1,506 @@ +import asyncio +import base64 +import contextlib +import hashlib +import hmac +import json +import re +import secrets +import sqlite3 +import time +import uuid +from contextlib import asynccontextmanager +from dataclasses import dataclass +from urllib.parse import unquote, urlsplit + +from cryptography.hazmat.primitives import hashes, padding +from cryptography.hazmat.primitives.asymmetric import x25519 +from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes +from fastapi import FastAPI, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from fastapi.exceptions import RequestValidationError +from pydantic import BaseModel, Field +from redis.asyncio import Redis + +from .common import BodyLimitMiddleware, no_cache, peer, rate_limit, require_secret +from .config import Settings +from .crypto import (fingerprint, load_private, open_envelope, raw_public, seal, + sign, verified, verify_device) +from .oauth import OAuth +from .platform import Feishu, PlatformError +from .store import Store, INTEGRITY_ERRORS + + +class Register(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + cert: str = Field(max_length=10000) + + +class Bind(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Instruction(BaseModel): + open_id: str = Field(min_length=1, max_length=128) + text: str = Field(min_length=1, max_length=8000) + # Stable identity chosen by upstream, not generated again on retries. + turn_id: str = Field(default_factory=lambda: uuid.uuid4().hex, pattern=r"^[A-Za-z0-9_-]{1,128}$") + + +@dataclass +class Pending: + future: asyncio.Future + private: object + public: object + host: str + session: str + + +class Broker: + def __init__(self, settings, redis, store): + self.s, self.r, self.store = settings, redis, store + self.signer = load_private(settings.connector_key) + self.pending: dict[str, Pending] = {} + + async def replies(self, pub): + async for message in pub.listen(): + if message["type"] != "message": + continue + try: + data = json.loads(message["data"]) + item = self.pending.get(data["turn_id"]) + if not item or item.future.done(): + continue + plain = open_envelope(data["envelope"], item.private, item.public, + host_id=item.host, session_id=item.session) + if plain["turn_id"] != data["turn_id"] or plain.get("type") != "result": + continue + result = plain.get("result", "") + if not isinstance(result, str) or len(result) > self.s.max_result: + continue + item.future.set_result({"status": "completed", "turn_id": data["turn_id"], "result": result}) + except Exception: + # Invalid packets never consume a pending result; do not log packet contents. + continue + + async def dispatch(self, owner, text, turn): + device = self.store.by_owner(owner) + if not device: + return {"status": "unavailable", "turn_id": turn, "result": "未绑定设备或设备不可用。"} + host = device["host_id"] + await rate_limit(self.r, "dispatch:" + owner, 30) + if len(self.pending) >= self.s.max_pending: + raise HTTPException(503, "too many in-flight requests") + presence = await self.r.get("hn:online:" + host) + if not presence or await self.r.exists("hn:revoked:" + host): + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线,请开机并连接网络。"} + try: + presence = json.loads(presence) + if fingerprint(presence["cert"]) != device["fingerprint"]: + raise ValueError("certificate changed") + public = verify_device(presence["cert"], self.s.ca_cert.read_bytes(), host) + proof = verified(presence["proof"], public) + if proof["host_id"] != host or proof["challenge"] != presence["challenge"]: + raise ValueError("invalid device session") + session = proof["session_id"] + except Exception: + self.store.audit(owner, "dispatch", host, "identity_rejected") + raise HTTPException(409, "device identity rejected") from None + if not self.store.begin_turn(turn, owner, host): + return {"status": "duplicate", "turn_id": turn, "result": "该请求已接收,不会重复执行。"} + private = x25519.X25519PrivateKey.generate() + future = asyncio.get_running_loop().create_future() + self.pending[turn] = Pending(future, private, public, host, session) + envelope = seal({"type": "instruction", "turn_id": turn, "owner": owner, + "text": text, "read_only": True, "channel": "im", + "reply_key": raw_public(private.public_key())}, proof["kx"], self.signer, + host_id=host, session_id=session) + key = f"hn:inflight:{host}:{turn}" + try: + await self.r.set(key, session, ex=self.s.request_timeout + 5) + count = await self.r.publish("hn:down:" + host, json.dumps({"type": "instruction", "envelope": envelope})) + if not count: + self.store.finish(turn, "offline") + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线。"} + result = await asyncio.wait_for(future, timeout=self.s.request_timeout) + self.store.finish(turn, "completed") + self.store.audit(owner, "instruction", host, "completed") + return result + except asyncio.TimeoutError: + self.store.finish(turn, "unknown") + return {"status": "unknown", "turn_id": turn, + "result": "等待结果超时,执行状态未知;不要直接重发有副作用的任务。"} + finally: + self.pending.pop(turn, None) + await self.r.delete(key) + + +def create_app(settings=None, redis=None, platform=None): + s = settings or Settings() + r = redis or Redis.from_url(s.redis_url, decode_responses=True) + store = Store(s.data_dir / "metadata.sqlite", s.database_url) + f = platform or Feishu(s) + broker = Broker(s, r, store) + oauth = OAuth(s, r, f) + oauth.store = store + background = set() + + def spawn(coro): + task = asyncio.create_task(coro) + background.add(task) + task.add_done_callback(background.discard) + # Retrieve errors without displaying possibly sensitive exception text. + task.add_done_callback(lambda t: t.exception() if not t.cancelled() else None) + + @asynccontextmanager + async def lifespan(app): + await r.ping() + for row in store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall(): + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + pub = r.pubsub() + await pub.subscribe("hn:replies") + reply_task = asyncio.create_task(broker.replies(pub)) + app.state.reply_task = reply_task + async def sweep(): + while True: + await asyncio.sleep(30) + async with oauth.lock: + oauth.prune() + sweep_task = asyncio.create_task(sweep()) + try: + yield + finally: + for task in [reply_task, sweep_task, *background]: + task.cancel() + await asyncio.gather(reply_task, sweep_task, *background, return_exceptions=True) + await pub.aclose() + await f.close() + store.close() + if redis is None: + await r.aclose() + + app = FastAPI(title="Hainuo B1 connector", lifespan=lifespan, docs_url=None, redoc_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=s.max_body) + app.include_router(oauth.router) + from .web_session import WebSessions + web_sessions = WebSessions(s, r, store, broker, f) + app.include_router(web_sessions.router) + app.state.store, app.state.broker, app.state.oauth = store, broker, oauth + app.state.platform, app.state.redis = f, r + + @app.exception_handler(RequestValidationError) + async def validation_error(request, exc): + # FastAPI's default error response includes invalid input values (possibly tokens). + return JSONResponse({"detail": "invalid request parameters"}, status_code=422) + + @app.middleware("http") + async def secure_response(request, call_next): + return no_cache(await call_next(request)) + + def admin(request): + require_secret(request.headers.get("X-Admin-Secret"), s.admin_secret) + + from .enrollment import enrollment_router + app.include_router(enrollment_router(s, r, store, broker.signer, admin)) + from .work_api import router as work_router + app.include_router(work_router(s, r, store, f)) + + @app.get("/health") + async def health(): + return {"status": "alive", "role": "connector", "version": "0.1.0"} + + @app.get("/ready") + async def ready(): + await r.ping() + store.db.execute("SELECT 1") + if not getattr(app.state, "reply_task", None) or app.state.reply_task.done(): + raise HTTPException(503, "reply subscriber unavailable") + return {"status": "ready"} + + @app.post("/admin/devices", status_code=201) + async def register(body: Register, request: Request): + admin(request) + try: + verify_device(body.cert, s.ca_cert.read_bytes(), body.host_id) + except Exception: + raise HTTPException(400, "invalid device certificate") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= s.max_devices: + raise HTTPException(503, "device capacity reached") + try: + fp = fingerprint(body.cert) + store.add_device(body.host_id, body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "device already exists") from None + await r.set("hn:device:" + body.host_id, fp) + store.audit("admin", "register", body.host_id, "success") + return {"host_id": body.host_id, "fingerprint": fp} + + @app.get("/admin/devices") + async def devices(request: Request): + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,owner,revoked FROM devices ORDER BY host_id").fetchall() + return {"devices": [{**dict(row), "online": bool(await r.exists("hn:online:" + row["host_id"]))} for row in rows]} + + @app.post("/admin/devices/{host}/revoke") + async def revoke(host: str, request: Request): + admin(request) + if not store.device(host): + raise HTTPException(404, "device not found") + # Persistent source of truth first; router block + disconnect follows. + store.revoke(host) + await r.incr("hn:web-generation:" + host) + await r.set("hn:revoked:" + host, "1") + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "revoke") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "revoked", "result": "设备已停用,结果已丢弃。"}) + store.audit("admin", "revoke", host, "success") + return {"status": "revoked"} + + @app.post("/admin/reconcile") + async def reconcile(request: Request): + """Rebuild router admission metadata after Redis recovery; no dialogue data.""" + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall() + for row in rows: + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + return {"devices": len(rows)} + + @app.post("/admin/devices/{host}/unbind") + async def admin_unbind(host: str, request: Request): + admin(request) + device = store.device(host) + if not device: + raise HTTPException(404, "device not found") + if device["owner"]: + store.unbind(device["owner"]) + await r.incr("hn:web-generation:" + host) + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "unbind") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + store.audit("admin", "unbind", host, "success") + return {"status": "unbound", "host_id": host, "revoked": bool(device["revoked"])} + + async def make_binding_code(owner): + if store.by_owner(owner): + return "你已绑定电脑,请先发送 /解绑。" + await rate_limit(r, "binding:" + owner, 5) + code = secrets.token_urlsafe(12) + await r.set("hn:bind:" + hashlib.sha256(code.encode()).hexdigest(), owner, ex=300) + return "请在本人电脑的桥接客户端输入绑定码(5分钟有效):" + code + + @app.post("/device/bind") + async def bind(body: Bind, request: Request): + await rate_limit(r, "bind:" + peer(request), 20) + device = store.device(body.host_id) + if not device or device["revoked"]: + raise HTTPException(404, "device unavailable") + try: + public = verify_device(device["cert"], s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, public) + if proof["host_id"] != body.host_id or abs(time.time() - proof["ts"]) > 90: + raise ValueError("invalid proof") + code = proof["code"] + if not isinstance(code, str) or len(code) > 128: + raise ValueError("invalid code") + except Exception: + raise HTTPException(403, "invalid device proof") from None + key = "hn:bind:" + hashlib.sha256(code.encode()).hexdigest() + owner = await r.get(key) + if not owner: + raise HTTPException(404, "binding code expired or consumed") + # GETDEL is atomic across connectors. Invalid device proofs never reach this point. + owner = await r.getdel(key) + if not owner: + raise HTTPException(404, "binding code consumed") + try: + store.bind(body.host_id, owner) + except (*INTEGRITY_ERRORS, ValueError): + raise HTTPException(409, "owner/device already bound; request new code") from None + await r.incr("hn:web-generation:" + body.host_id) + store.audit(owner, "bind", body.host_id, "success") + return sign({"host_id": body.host_id, "owner": owner, "ts": int(time.time())}, broker.signer) + + @app.post("/admin/test/binding-code") + async def test_code(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return {"message": await make_binding_code(body.open_id)} + + @app.post("/admin/test/dispatch") + async def test_dispatch(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return await broker.dispatch(body.open_id, body.text, body.turn_id) + + async def handle_event(data): + header, event = data.get("header", {}), data.get("event", {}) + if header.get("event_type") != "im.message.receive_v1": + return + message, sender = event.get("message", {}), event.get("sender", {}) + owner = sender.get("sender_id", {}).get("open_id") + message_id = message.get("message_id", "") + if (message.get("chat_type") != "p2p" or message.get("message_type") != "text" + or sender.get("sender_type") != "user" or not owner + or not re.fullmatch(r"[A-Za-z0-9_-]{1,128}", message_id)): + return + if header.get("app_id") != s.feishu_app_id: + return + try: + text = json.loads(message["content"])["text"].strip() + if not text or len(text) > 8000: + return + # Persistent receipt BEFORE any side effect. Restarts do not execute a delivery twice. + event_turn = "event_" + hashlib.sha256(message_id.encode()).hexdigest() + if not store.begin_turn(event_turn, owner, "event"): + return + if text == "/绑定": + result = await make_binding_code(owner) + elif text == "/解绑": + old = store.by_owner(owner) + store.unbind(owner) + if old: + await r.incr("hn:web-generation:" + old["host_id"]) + await r.publish("hn:kill:" + old["host_id"], "unbind") + for item in broker.pending.values(): + if item.host == old["host_id"] and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + result = "已解绑;重新绑定后请更新本机绑定信息。" + elif text == "/主机名" or text.startswith("/回显 "): + result = (await broker.dispatch(owner, text, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + else: + instruction = text if text.startswith("/助手 ") else "/助手 " + text + result = (await broker.dispatch(owner, instruction, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + await f.reply(message_id, result[:s.max_result]) + store.finish(event_turn, "replied") + except Exception: + store.audit(owner, "event", message_id, "failed_or_unknown") + + @app.post("/internal/events") + async def internal_event(request: Request): + require_secret(request.headers.get("X-Internal-Secret"), s.internal_secret) + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(await request.json())) + return {"accepted": True} + + @app.post("/feishu/event") + async def webhook(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if not s.feishu_encrypt_key or not s.feishu_verification_token: + raise HTTPException(503, "webhook credentials not configured; use selected event source") + await rate_limit(r, "webhook:" + peer(request), 600) + raw = await request.body() + timestamp, nonce, signature = (request.headers.get(x, "") for x in + ("X-Lark-Request-Timestamp", "X-Lark-Request-Nonce", "X-Lark-Signature")) + try: + if abs(time.time() - int(timestamp)) > 300 or not nonce or len(nonce) > 128: + raise ValueError("stale request") + expected = hashlib.sha256((timestamp + nonce + s.feishu_encrypt_key).encode() + raw).hexdigest() + if not hmac.compare_digest(expected, signature): + raise ValueError("bad signature") + data = json.loads(raw) + if "encrypt" in data: + cipher = base64.b64decode(data["encrypt"], validate=True) + decryptor = Cipher(algorithms.AES(hashlib.sha256(s.feishu_encrypt_key.encode()).digest()), + modes.CBC(cipher[:16])).decryptor() + padded = decryptor.update(cipher[16:]) + decryptor.finalize() + unpadder = padding.PKCS7(128).unpadder() + data = json.loads(unpadder.update(padded) + unpadder.finalize()) + token = data.get("token", data.get("header", {}).get("token", "")) + if not hmac.compare_digest(token, s.feishu_verification_token): + raise ValueError("invalid verification token") + except Exception: + raise HTTPException(403, "invalid event authentication") from None + if data.get("type") == "url_verification": + return {"challenge": data.get("challenge", "")} + # Same delivery is ACKed again; durable message_id receipt handles execution deduplication. + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(data)) + return {"code": 0} + + @app.get("/api/whoami") + async def whoami(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(r, "whoami:" + peer(request)) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await f.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "no device bound") + if not s.device_domain or not re.fullmatch(r"[a-zA-Z0-9.-]+", s.device_domain): + raise HTTPException(503, "device web domain not configured") + return {"host_id": device["host_id"], "url": f"https://h-{device['host_id']}.{s.device_domain}/", + "online": bool(await r.exists("hn:online:" + device["host_id"])), + "device_login_required": True} + + @app.get("/h5", response_class=HTMLResponse) + async def h5(): + from importlib.resources import files + return HTMLResponse(files("relay").joinpath("web/index.html").read_text(encoding="utf-8"), + headers={"Content-Security-Policy": "default-src 'self'; script-src 'self'; style-src 'self'; frame-ancestors 'self'; base-uri 'none'; form-action 'self'"}) + + @app.get("/assets/{name}") + async def asset(name: str): + from importlib.resources import files + from starlette.responses import Response + if name not in {"app.js", "style.css"}: + raise HTTPException(404, "not found") + return Response(files("relay").joinpath("web/" + name).read_bytes(), + media_type="text/javascript" if name.endswith(".js") else "text/css") + + @app.get("/proxy") + async def proxy(request: Request): + if not s.proxy_enabled or not s.feishu_enabled: + raise HTTPException(404, "not found") + auth = request.headers.get("Authorization", "") + target = request.headers.get("X-Relay-Target", "") + decoded = unquote(target) + parts = urlsplit(target) + allowed = s.proxy_paths.split(",") + if (not auth.startswith("Bearer ") or len(auth) > 8192 or len(target) > 4096 + or parts.scheme or parts.netloc or parts.fragment or target.startswith("//") + or any(x in decoded for x in ("..", "\\", "\r", "\n", "#", "%")) + or not any(parts.path == x or parts.path.startswith(x + "/") for x in allowed)): + raise HTTPException(403, "target not allowed") + try: + info = await f.user_info(auth[7:]) + await rate_limit(r, "proxy:" + info["open_id"], 60) + # Buffer bounded response; no disk/cache, redirects never followed. + async with f.http.stream("GET", target, headers={"Authorization": auth}) as response: + if response.status_code in {301, 302, 303, 307, 308}: + raise HTTPException(502, "upstream redirect refused") + chunks, size = [], 0 + async for chunk in response.aiter_bytes(): + size += len(chunk) + if size > 2 * 1024 * 1024: + raise HTTPException(502, "response too large") + chunks.append(chunk) + from starlette.responses import Response + store.audit(info["open_id"], "proxy", parts.path, str(response.status_code)) + return Response(b"".join(chunks), status_code=response.status_code, + media_type=response.headers.get("content-type", "application/json")) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + + return app diff --git a/patch-source/02ffd84040a628b7/relay/crypto.py b/patch-source/02ffd84040a628b7/relay/crypto.py new file mode 100644 index 0000000..dd962e5 --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/crypto.py @@ -0,0 +1,140 @@ +"""Signed ephemeral X25519 envelopes. The router never receives plaintext keys.""" +import base64 +import hashlib +import json +import secrets +import time +from datetime import datetime, timedelta, timezone + +from cryptography import x509 +from cryptography.hazmat.primitives import hashes, serialization +from cryptography.hazmat.primitives.asymmetric import ed25519, x25519 +from cryptography.hazmat.primitives.ciphers.aead import AESGCM +from cryptography.hazmat.primitives.kdf.hkdf import HKDF +from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID + + +def b64(data: bytes) -> str: + return base64.urlsafe_b64encode(data).decode() + + +def unb64(data: str) -> bytes: + return base64.b64decode(data.encode(), altchars=b"-_", validate=True) + + +def canonical(data: dict) -> bytes: + return json.dumps(data, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode() + + +def raw_public(key) -> str: + return b64(key.public_bytes(serialization.Encoding.Raw, serialization.PublicFormat.Raw)) + + +def pem_private(key) -> bytes: + return key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, + serialization.NoEncryption()) + + +def load_private(path): + protected = path.with_name(path.name + ".dpapi") + if protected.is_file(): + from .windows_keys import unprotect + data = unprotect(protected.read_bytes()) + else: + data = path.read_bytes() + return serialization.load_pem_private_key(data, password=None) + + +def load_public(path): + return serialization.load_pem_public_key(path.read_bytes()) + + +def fingerprint(pem: str) -> str: + return x509.load_pem_x509_certificate(pem.encode()).fingerprint(hashes.SHA256()).hex() + + +def verify_device(pem: str, ca_pem: bytes, host_id: str): + """v1 deliberately accepts only directly issued Ed25519 device certificates.""" + cert = x509.load_pem_x509_certificate(pem.encode()) + ca = x509.load_pem_x509_certificate(ca_pem) + now = datetime.now(timezone.utc) + if not (ca.not_valid_before_utc <= now <= ca.not_valid_after_utc): + raise ValueError("expired CA") + if not ca.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("not a CA") + cert.verify_directly_issued_by(ca) + if not (cert.not_valid_before_utc <= now <= cert.not_valid_after_utc): + raise ValueError("expired certificate") + if cert.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("CA cannot be used as device") + if ExtendedKeyUsageOID.CLIENT_AUTH not in cert.extensions.get_extension_for_class(x509.ExtendedKeyUsage).value: + raise ValueError("clientAuth missing") + names = cert.extensions.get_extension_for_class(x509.SubjectAlternativeName).value + if names.get_values_for_type(x509.UniformResourceIdentifier) != [f"urn:hainuo:host:{host_id}"]: + raise ValueError("host identity mismatch") + public = cert.public_key() + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("v1 requires Ed25519 device key") + return public + + +def sign(data: dict, key) -> dict: + return {**data, "signature": b64(key.sign(canonical(data)))} + + +def verified(data: dict, key) -> dict: + body = {k: v for k, v in data.items() if k != "signature"} + key.verify(unb64(data["signature"]), canonical(body)) + return body + + +def seal(payload: dict, recipient_public: str, signer, *, host_id: str, session_id: str) -> dict: + ephemeral = x25519.X25519PrivateKey.generate() + header = {"v": 1, "host_id": host_id, "session_id": session_id, + "ts": int(time.time()), "nonce": b64(secrets.token_bytes(12)), + "ephemeral": raw_public(ephemeral.public_key())} + shared = ephemeral.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(recipient_public))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + cipher = AESGCM(key).encrypt(unb64(header["nonce"]), canonical(payload), canonical(header)) + return sign({**header, "ciphertext": b64(cipher)}, signer) + + +def open_envelope(envelope: dict, recipient_private, sender_public, *, host_id: str, session_id: str) -> dict: + body = verified(envelope, sender_public) + if body["v"] != 1 or body["host_id"] != host_id or body["session_id"] != session_id: + raise ValueError("wrong destination/session") + if abs(time.time() - body["ts"]) > 90: + raise ValueError("expired envelope") + header = {k: v for k, v in body.items() if k != "ciphertext"} + shared = recipient_private.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(body["ephemeral"]))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + plain = AESGCM(key).decrypt(unb64(body["nonce"]), unb64(body["ciphertext"]), canonical(header)) + return json.loads(plain) + + +def issue_ca(): + key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Hainuo device test CA")]) + now = datetime.now(timezone.utc) + cert = (x509.CertificateBuilder().subject_name(name).issuer_name(name) + .public_key(key.public_key()).serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=3650)) + .add_extension(x509.BasicConstraints(ca=True, path_length=0), critical=True) + .add_extension(x509.KeyUsage(False, False, False, False, False, True, True, False, False), critical=True) + .sign(key, algorithm=None)) + return key, cert + + +def issue_device(ca_key, ca_cert, host_id: str, public_key, days=30): + now = datetime.now(timezone.utc) + return (x509.CertificateBuilder() + .subject_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, host_id)])) + .issuer_name(ca_cert.subject).public_key(public_key) + .serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=days)) + .add_extension(x509.BasicConstraints(ca=False, path_length=None), critical=True) + .add_extension(x509.SubjectAlternativeName([x509.UniformResourceIdentifier(f"urn:hainuo:host:{host_id}")]), critical=False) + .add_extension(x509.ExtendedKeyUsage([ExtendedKeyUsageOID.CLIENT_AUTH]), critical=False) + .sign(ca_key, algorithm=None)) diff --git a/patch-source/02ffd84040a628b7/relay/device.py b/patch-source/02ffd84040a628b7/relay/device.py new file mode 100644 index 0000000..a8fe010 --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/device.py @@ -0,0 +1,216 @@ +"""Reference PC bridge. No shell/PowerShell execution; local read-only diagnostics.""" +import asyncio +import contextlib +import json +import os +import secrets +import socket +import sqlite3 +import time +from pathlib import Path +from urllib.parse import urlsplit + +import httpx +from cryptography.hazmat.primitives.asymmetric import x25519 +from websockets.asyncio.client import connect + +from .crypto import (load_private, load_public, open_envelope, raw_public, seal, sign, + verified, verify_device) + + +class DeviceJournal: + def __init__(self, path): + path.parent.mkdir(parents=True, exist_ok=True) + self.db = sqlite3.connect(path) + self.db.execute("CREATE TABLE IF NOT EXISTS consumed(turn_id TEXT PRIMARY KEY, ts REAL NOT NULL)") + self.db.commit() + + def consume(self, turn): + with self.db: + return self.db.execute("INSERT OR IGNORE INTO consumed VALUES(?,?)", (turn, time.time())).rowcount == 1 + + def close(self): + self.db.close() + + +async def execute_readonly(text, root: Path | None): + if text in {"/主机名", "hostname"}: + return socket.gethostname() + if text.startswith("/回显 "): + return text[4:][:16000] + if text.startswith("/读取 "): + if root is None: + return "未配置测试文件目录。" + base = root.resolve(strict=True) + try: + path = (base / text[4:].strip()).resolve(strict=True) + if path == base or not path.is_relative_to(base) or not path.is_file(): + return "拒绝:文件不在允许的测试目录。" + if path.stat().st_size > 32768: + return "拒绝:测试文件超过32KB。" + return path.read_text(encoding="utf-8")[:16000] + except (OSError, UnicodeError, ValueError): + return "拒绝:文件不存在、不可读或不是UTF-8文本。" + return "当前桥接客户端仅开放只读诊断:/主机名、/回显 内容、/读取 相对路径;尚未接入海诺本地Agent接口。" + + +class LocalAgent: + """Explicit adapter contract, not a claim of compatibility with installer 1.065.""" + def __init__(self, url, transport=None): + parts = urlsplit(url) + if parts.scheme != "http" or parts.hostname not in {"127.0.0.1", "::1"} or parts.username or parts.password or parts.fragment: + raise ValueError("local Agent must use a fixed loopback HTTP endpoint") + self.url = url + self.http = httpx.AsyncClient(timeout=30, follow_redirects=False, transport=transport, trust_env=False) + + async def execute(self, text, owner, turn): + headers = {} + if os.environ.get("HN_LOCAL_AGENT_TOKEN"): + headers["Authorization"] = "Bearer " + os.environ["HN_LOCAL_AGENT_TOKEN"] + try: + async with self.http.stream("POST", self.url, headers=headers, + json={"text": text, "open_id": owner, "turn_id": turn, + "channel": "im", "read_only": True}) as response: + if response.status_code != 200: + return "本机Agent未返回成功结果,请查看本机服务状态。" + body = b"" + async for chunk in response.aiter_bytes(): + body += chunk + if len(body) > 65536: + return "本机Agent结果超过限制。" + result = json.loads(body)["result"] + if not isinstance(result, str): + return "本机Agent响应格式不匹配。" + return result[:16000] + except (httpx.HTTPError, ValueError, KeyError): + return "本机Agent连接失败或响应格式不匹配。" + + async def close(self): + await self.http.aclose() + + +async def bind_device(directory: Path, connector_url: str, code: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + private = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "code": code, "ts": int(time.time())}, private) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as http: + response = await http.post(connector_url.rstrip("/") + "/device/bind", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"binding failed: HTTP {response.status_code}") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt["host_id"] != config["host_id"]: + raise ValueError("wrong binding receipt") + config["owner"] = receipt["owner"] + temp = directory / "device.json.tmp" + temp.write_text(json.dumps(config, ensure_ascii=False, indent=2), encoding="utf-8") + temp.replace(directory / "device.json") + + +async def pair_browser(directory: Path, connector_url: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8-sig")) + if not config.get("owner"): + raise RuntimeError("device must be bound first") + key = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "owner": config["owner"], + "purpose": "h5-pair-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as client: + response = await client.post(connector_url.rstrip("/") + "/device/web-pair", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"browser pairing failed: HTTP {response.status_code}") + result = verified(response.json(), load_public(directory / "connector.pub")) + if result["host_id"] != config["host_id"]: + raise ValueError("wrong pairing receipt") + return result + + +async def run_device(directory: Path, router_url: str, **kwargs): + from .process_lock import DeviceProcessLock + with DeviceProcessLock(directory): + return await _run_device(directory, router_url, **kwargs) + + +async def _run_device(directory: Path, router_url: str, *, root=None, once=False, ssl_context=None, + stop_event=None, connected_event=None, agent_url=None, hainuo_config=None): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + if not config.get("owner"): + raise RuntimeError("bind device before starting") + host, owner = config["host_id"], config["owner"] + key = load_private(directory / "device.key") + connector = load_public(directory / "connector.pub") + cert = (directory / "device.pem").read_text(encoding="utf-8") + verify_device(cert, (directory / "ca.pem").read_bytes(), host) + journal = DeviceJournal(directory / "journal.sqlite") + if agent_url and hainuo_config: + raise ValueError("choose one Agent adapter") + if hainuo_config: + from .hainuo1065 import Hainuo1065Agent + agent = Hainuo1065Agent(hainuo_config) + else: + agent = LocalAgent(agent_url) if agent_url else None + stop_event = stop_event or asyncio.Event() + delay = 1 + try: + while not stop_event.is_set(): + try: + kwargs = {"ssl": ssl_context} if router_url.startswith("wss://") and ssl_context else {} + async with connect(router_url, max_size=262144, open_timeout=10, ping_interval=10, + proxy=None, **kwargs) as ws: + challenge = json.loads(await ws.recv())["challenge"] + ephemeral = x25519.X25519PrivateKey.generate() + session = secrets.token_hex(16) + proof = sign({"host_id": host, "session_id": session, "challenge": challenge, + "kx": raw_public(ephemeral.public_key()), "ts": int(time.time())}, key) + await ws.send(json.dumps({"host_id": host, "cert": cert, "proof": proof})) + accepted = json.loads(await ws.recv()) + if accepted.get("type") != "connected": + raise RuntimeError("device connection rejected") + print("设备连接成功,已绑定本人海能Work身份。", flush=True) + if connected_event: + connected_event.set() + delay = 1 + while not stop_event.is_set(): + try: + raw = await asyncio.wait_for(ws.recv(), timeout=1) + except asyncio.TimeoutError: + continue + data = json.loads(raw) + payload = open_envelope(data["envelope"], ephemeral, connector, + host_id=host, session_id=session) + if payload.get("owner") != owner or payload.get("type") != "instruction": + continue + turn = payload["turn_id"] + if not isinstance(turn, str) or len(turn) > 128: + continue + if not journal.consume(turn): + result = "该请求已处理,拒绝重复执行。" + elif payload.get("read_only") is not True or payload.get("channel") != "im": + result = "拒绝:通道或操作类型不受支持。" + else: + if agent and payload["text"].startswith("/助手 "): + result = await agent.execute(payload["text"][4:], owner, turn) + else: + result = await execute_readonly(payload["text"], root) + reply = seal({"type": "result", "turn_id": turn, "result": result}, + payload["reply_key"], key, host_id=host, session_id=session) + await ws.send(json.dumps({"type": "reply", "turn_id": turn, "envelope": reply})) + if once: + return + except asyncio.CancelledError: + raise + except Exception as error: + if once: + raise + from websockets.exceptions import ConnectionClosed + if isinstance(error, ConnectionClosed) and error.rcvd and error.rcvd.code == 1008: + print("设备被中继拒绝:请检查是否有另一桥接进程、设备准入或证书状态。", flush=True) + # No payloads/URLs/credentials in logs, bounded reconnect backoff + jitter. + print("设备连接中断,等待重连。", flush=True) + with contextlib.suppress(asyncio.TimeoutError): + await asyncio.wait_for(stop_event.wait(), timeout=delay + secrets.randbelow(1000) / 1000) + delay = min(delay * 2, 30) + finally: + journal.close() + if agent: + await agent.close() diff --git a/patch-source/02ffd84040a628b7/relay/enrollment.py b/patch-source/02ffd84040a628b7/relay/enrollment.py new file mode 100644 index 0000000..b4ac77f --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/enrollment.py @@ -0,0 +1,178 @@ +"""Device registration with key possession proof; Work binding is separate.""" +import json +import secrets +import time +import httpx + +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey +from fastapi import APIRouter, HTTPException, Request +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .crypto import verified, sign, raw_public, verify_device, fingerprint +from .web_session import digest +from fastapi.responses import JSONResponse + + +def validate_enrollment(body): + try: + key = serialization.load_pem_public_key(body.public_key.encode()) + if not isinstance(key, Ed25519PublicKey): + raise ValueError() + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["purpose"] != "device-enroll-v1" + or abs(time.time() - proof["ts"]) > 90 or len(proof["nonce"]) != 32): + raise ValueError() + return key + except Exception: + raise HTTPException(403, "invalid enrollment proof") from None + + +async def request_certificate(settings, body): + async with httpx.AsyncClient(trust_env=False, timeout=15, follow_redirects=False) as http: + response = await http.post(settings.enrollment_signer_url.rstrip("/") + "/issue", + headers={"X-Internal-Secret": settings.internal_secret}, + json=body.model_dump()) + response.raise_for_status() + return response.json()["cert"] + + +class EnrollmentRequest(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + public_key: str = Field(max_length=1024) + proof: dict + + +class EnrollmentApproval(BaseModel): + cert: str = Field(max_length=10000) + + +def enrollment_router(settings, redis, store, signer, admin): + router = APIRouter() + + @router.post("/device/register") + async def automatic_registration(body: EnrollmentRequest, request: Request): + # No human approval: eligibility follows intranet access plus Work app scope. + # Proof/TLS/limits protect identity and resources, and do not add user approval. + await rate_limit(redis, "auto-register-ip:" + peer(request), 120) + key = validate_enrollment(body) + await rate_limit(redis, "auto-register-host:" + body.host_id, 30) + existing = store.device(body.host_id) + + def check_existing(device): + if device["revoked"]: + raise HTTPException(403, "device revoked") + try: + registered = verify_device(device["cert"], settings.ca_cert.read_bytes(), body.host_id) + if raw_public(registered) != raw_public(key): + raise ValueError() + except Exception: + raise HTTPException(409, "existing device identity mismatch or certificate expired") from None + return device["cert"] + + if existing: + cert = check_existing(existing) + else: + if not settings.enrollment_signer_url: + raise HTTPException(503, "automatic registration is not enabled; update ECS first") + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= settings.max_devices: + raise HTTPException(503, "device capacity reached") + await rate_limit(redis, "auto-register-total", settings.max_pending, 3600) + try: + cert = await request_certificate(settings, body) + issued = verify_device(cert, settings.ca_cert.read_bytes(), body.host_id) + if raw_public(issued) != raw_public(key): + raise ValueError() + except Exception: + raise HTTPException(503, "certificate issuer unavailable or invalid response") from None + from .store import INTEGRITY_ERRORS + try: + store.add_device(body.host_id, cert, fingerprint(cert)) + except INTEGRITY_ERRORS: + # A retry/concurrent request must return the original certificate. + cert = check_existing(store.device(body.host_id)) + store.audit("device", "automatic_registration", body.host_id, "success") + await redis.set("hn:device:" + body.host_id, fingerprint(cert)) + return no_cache(JSONResponse(sign({"status": "registered", "host_id": body.host_id, + "cert": cert}, signer), status_code=201 if not existing else 200)) + + @router.post("/device/enroll") + async def request_enrollment(body: EnrollmentRequest, request: Request): + await rate_limit(redis, "enroll-ip:" + peer(request), 10) + validate_enrollment(body) + await rate_limit(redis, "enroll-host:" + body.host_id, 3, 3600) + if store.device(body.host_id): + raise HTTPException(409, "device already registered; recover its original certificate") + # Global rolling capacity bound; prevents anonymous request-table exhaustion. + await rate_limit(redis, "enroll-global", settings.max_pending, 1800) + identifier, claim_secret = secrets.token_hex(16), secrets.token_urlsafe(32) + record = {"host_id": body.host_id, "public_key": body.public_key, + "claim_hash": digest(claim_secret), "created": time.time(), "status": "pending"} + await redis.set("hn:enroll:" + identifier, json.dumps(record), ex=1800) + return no_cache(JSONResponse(sign({"request_id": identifier, "claim_secret": claim_secret, + "host_id": body.host_id, "expires_in": 1800}, signer), status_code=201)) + + @router.get("/device/enroll/{identifier}") + async def claim(identifier: str, request: Request): + await rate_limit(redis, "enroll-poll:" + peer(request), 60) + data = await redis.get("hn:enroll:" + identifier) + if not data: + raise HTTPException(404, "request expired") + record = json.loads(data) + import hmac + if not hmac.compare_digest(record["claim_hash"], digest(request.headers.get("X-Enrollment-Secret", ""))): + raise HTTPException(404, "request unavailable") + if record["status"] == "pending": + return no_cache(JSONResponse({"status": "pending"}, status_code=202)) + device = store.device(record["host_id"]) + if not device or device["revoked"]: + raise HTTPException(403, "device unavailable") + # Recoverable for its original claimant for the request TTL; public certificate only. + return no_cache(JSONResponse(sign({"status": "approved", "host_id": record["host_id"], + "request_id": identifier, "cert": device["cert"]}, signer))) + + @router.get("/admin/enrollments/{identifier}") + async def inspect(identifier: str, request: Request): + admin(request) + encoded = await redis.get("hn:enroll:" + identifier) + if not encoded: + raise HTTPException(404, "request expired") + record = json.loads(encoded) + return {k: v for k, v in record.items() if k != "claim_hash"} + + @router.post("/admin/enrollments/{identifier}/approve") + async def approve(identifier: str, body: EnrollmentApproval, request: Request): + admin(request) + encoded = await redis.get("hn:enroll:" + identifier) + if not encoded: + raise HTTPException(404, "request expired") + record = json.loads(encoded) + try: + public = verify_device(body.cert, settings.ca_cert.read_bytes(), record["host_id"]) + requested = serialization.load_pem_public_key(record["public_key"].encode()) + if raw_public(public) != raw_public(requested): + raise ValueError() + except Exception: + raise HTTPException(400, "certificate does not match enrollment") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= settings.max_devices: + raise HTTPException(503, "device capacity reached") + fp = fingerprint(body.cert) + existing = store.device(record["host_id"]) + if existing and (existing["revoked"] or existing["fingerprint"] != fp): + raise HTTPException(409, "device conflict") + if not existing: + from .store import INTEGRITY_ERRORS + try: + store.add_device(record["host_id"], body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "concurrent device registration") from None + await redis.set("hn:device:" + record["host_id"], fp) + record["status"] = "approved" + remaining = await redis.ttl("hn:enroll:" + identifier) + if remaining < 1: + raise HTTPException(409, "request expired during approval") + await redis.set("hn:enroll:" + identifier, json.dumps(record), ex=remaining) + store.audit("admin", "enrollment_approved", record["host_id"], "success") + return {"status": "approved", "host_id": record["host_id"]} + + return router diff --git a/patch-source/02ffd84040a628b7/relay/enrollment_signer.py b/patch-source/02ffd84040a628b7/relay/enrollment_signer.py new file mode 100644 index 0000000..6089d02 --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/enrollment_signer.py @@ -0,0 +1,43 @@ +"""Internal automatic CA issuer. No published host port, no application credentials.""" +import os +from pathlib import Path + +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from fastapi import FastAPI, Request +from .common import BodyLimitMiddleware, require_secret +from .crypto import load_private, issue_device, raw_public, verify_device +from .enrollment import EnrollmentRequest, validate_enrollment + + +def create_app(*, ca_key_path=None, ca_cert_path=None, secret=None): + key = load_private(Path(ca_key_path or os.environ.get("HN_ISSUER_CA_KEY", "/issuer/ca.key"))) + ca_bytes = Path(ca_cert_path or os.environ.get("HN_ISSUER_CA_CERT", "/trust/ca.pem")).read_bytes() + ca = x509.load_pem_x509_certificate(ca_bytes) + if raw_public(key.public_key()) != raw_public(ca.public_key()): + raise ValueError("issuer CA key mismatch") + token = secret or os.environ.get("HN_INTERNAL_SECRET", "") + if len(token) < 32: + raise ValueError("issuer requires internal credential") + app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=16384) + + @app.get("/ready") + async def ready(): + return {"status": "ready"} + + @app.post("/issue") + async def issue(body: EnrollmentRequest, request: Request): + require_secret(request.headers.get("X-Internal-Secret"), token) + public = validate_enrollment(body) + cert = issue_device(key, ca, body.host_id, public).public_bytes(serialization.Encoding.PEM).decode() + verify_device(cert, ca_bytes, body.host_id) + return {"cert": cert} + + return app + + +if __name__ == "__main__": + import uvicorn + uvicorn.run("relay.enrollment_signer:create_app", factory=True, host="0.0.0.0", port=8000, + workers=1, access_log=False, proxy_headers=False, log_level="warning") diff --git a/patch-source/02ffd84040a628b7/relay/hainuo1065.py b/patch-source/02ffd84040a628b7/relay/hainuo1065.py new file mode 100644 index 0000000..6f7735b --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/hainuo1065.py @@ -0,0 +1,107 @@ +"""Adapter for the native API actually found in installer 1.065. + +Loopback only, independent API key, no platform secrets, no redirects or retries. +The included native launcher offers only text inference with no tools. +""" +import hashlib +import json +import re +from pathlib import Path +from urllib.parse import urlsplit + +import httpx + + +class NativeAPIError(RuntimeError): + """Contains a sanitized status only, never response body or credentials.""" + + +def read_native_config(path): + path = Path(path) + data = json.loads(path.read_text(encoding="utf-8-sig")) + url = data.get("url", "") + parts = urlsplit(url) + if (parts.scheme != "http" or parts.hostname != "127.0.0.1" or not parts.port + or parts.username or parts.password or parts.query or parts.fragment + or parts.path not in {"", "/"}): + raise ValueError("native API must be a fixed IPv4 loopback HTTP base URL") + if not isinstance(data.get("api_key"), str) or len(data["api_key"]) < 32: + raise ValueError("native API requires a dedicated key of at least 32 characters") + if data.get("mode") not in {"text-only", "profile-tools"}: + raise ValueError("unsupported native API mode") + data["url"] = url.rstrip("/") + return data + + +class Hainuo1065Agent: + def __init__(self, config_path, transport=None): + self.config = read_native_config(config_path) + self.http = httpx.AsyncClient(base_url=self.config["url"], timeout=120, + follow_redirects=False, trust_env=False, transport=transport) + self.headers = {"Authorization": "Bearer " + self.config["api_key"]} + + async def _json(self, method, path, **kwargs): + try: + async with self.http.stream(method, path, headers={**self.headers, **kwargs.pop("headers", {})}, **kwargs) as response: + if response.status_code != 200: + raise NativeAPIError(f"native API HTTP {response.status_code}") + body = bytearray() + async for chunk in response.aiter_bytes(): + body.extend(chunk) + if len(body) > 262144: + raise NativeAPIError("native API response exceeds limit") + data = json.loads(body) + if not isinstance(data, dict): + raise NativeAPIError("native API response is not an object") + return data + except (httpx.HTTPError, ValueError): + raise NativeAPIError("native API connection/JSON failure") from None + + async def probe(self): + health = await self._json("GET", "/health") + if health.get("platform") != "hermes-agent" or health.get("status") != "ok": + raise NativeAPIError("endpoint is not the native Hermes API") + capabilities = await self._json("GET", "/v1/capabilities") + policy = capabilities.get("hainuo_relay_policy", {}) + # A stock unrestricted API endpoint is deliberately not sufficient. + expected_mode = self.config["mode"] + expected_tools = "profile" if expected_mode == "profile-tools" else "disabled" + if policy != {"mode": expected_mode, "tools": expected_tools, "contract": "hainuo-1065-v1"}: + raise NativeAPIError("native API policy missing; start the supplied restricted launcher") + models = await self._json("GET", "/v1/models") + model = self.config.get("model") + available = models.get("data") + if not isinstance(available, list) or not available or not all(isinstance(x, dict) and isinstance(x.get("id"), str) for x in available): + raise NativeAPIError("native model list invalid") + if model and model not in {x["id"] for x in available}: + raise NativeAPIError("configured model alias unavailable") + self.model = model or available[0]["id"] + return {"status": "ready", "platform": "hermes-agent", "model": self.model, + "mode": expected_mode, "version": health.get("version", "unknown")} + + async def execute(self, text, owner, turn): + try: + # Recheck the actual local service before each inference, including reconnects. + await self.probe() + digest = hashlib.sha256((owner + "\0" + turn).encode()).hexdigest() + data = await self._json("POST", "/v1/chat/completions", + headers={"Idempotency-Key": "hn-" + digest}, + json={"model": self.model, "stream": False, + "messages": [{"role": "user", "content": text}]}) + choices = data.get("choices") + if not isinstance(choices, list) or not choices or not isinstance(choices[0], dict): + raise NativeAPIError("native completion choices missing") + choice = choices[0] + message = choice.get("message", {}) + if (choice.get("finish_reason") != "stop" or message.get("tool_calls") + or not isinstance(message.get("content"), str) + or data.get("hermes", {}).get("failed") + or data.get("hermes", {}).get("partial")): + raise NativeAPIError("native completion incomplete or unexpected tool call") + return message["content"][:16000] + except NativeAPIError as error: + # Safe generated error strings only; never echo upstream model/API errors. + return "海诺原生接口未完成请求:" + str(error) + "。未自动重试。" + + async def close(self): + await self.http.aclose() diff --git a/patch-source/02ffd84040a628b7/relay/longconn.py b/patch-source/02ffd84040a628b7/relay/longconn.py new file mode 100644 index 0000000..5c12bbf --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/longconn.py @@ -0,0 +1,74 @@ +"""Optional official SDK event source, private platform domain explicitly configured.""" +import json +import logging +import os +import threading +import time + +import httpx +import redis +from .config import Settings + + +def main(): + import lark_oapi as lark + s = Settings(role="longconn") + if not s.feishu_enabled or s.feishu_event_source != "longconn": + raise RuntimeError("platform is disabled") + # The SDK may log websocket URLs, tokens or event bodies. Disable its logging entirely. + logging.disable(logging.CRITICAL) + r = redis.Redis.from_url(s.redis_url) + lease = r.lock("hn:longconn:leader", timeout=30, thread_local=False) + if not lease.acquire(blocking=False): + raise RuntimeError("another long-connection consumer owns the lease") + url = os.environ.get("HN_CONNECTOR_INTERNAL_URL", "http://connector:8000") + client = httpx.Client(base_url=url, timeout=10, follow_redirects=False) + done = threading.Event() + + def maintain_lease(): + while not done.wait(8): + try: + lease.extend(30, replace_ttl=True) + except Exception: + # Fail closed. Continuing after lease loss could create two consumers. + os._exit(2) + + threading.Thread(target=maintain_lease, daemon=True).start() + + def received(event): + data = json.loads(lark.JSON.marshal(event)) + response = client.post("/internal/events", headers={"X-Internal-Secret": s.internal_secret}, json=data) + if response.status_code != 200: + raise RuntimeError("connector did not accept event") + print(json.dumps({"event": "platform_event_accepted", "type": data.get("header", {}).get("event_type")}), flush=True) + + handler = lark.EventDispatcherHandler.builder("", "").register_p2_im_message_receive_v1(received).build() + class ObservableClient(lark.ws.Client): + async def _connect(self): + try: + await super()._connect() + if self._conn is not None: + print(json.dumps({"event": "platform_connected", "api_host": s.feishu_api_base}), flush=True) + except Exception as error: + print(json.dumps({"event": "platform_connect_failed", "error_type": type(error).__name__}), flush=True) + raise + + try: + # domain is an actual supported SDK argument; no public-cloud-domain monkey patch. + ObservableClient(s.feishu_app_id, s.feishu_app_secret, event_handler=handler, + domain=s.feishu_api_base.rstrip("/"), log_level=lark.LogLevel.ERROR).start() + finally: + done.set() + client.close() + try: + lease.release() + except redis.exceptions.LockError: + pass + + +if __name__ == "__main__": + try: + main() + except Exception as error: + print(json.dumps({"event": "platform_consumer_stopped", "error_type": type(error).__name__}), flush=True) + raise SystemExit(1) from None diff --git a/patch-source/02ffd84040a628b7/relay/oauth.py b/patch-source/02ffd84040a628b7/relay/oauth.py new file mode 100644 index 0000000..1b35a14 --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/oauth.py @@ -0,0 +1,144 @@ +import asyncio +import hashlib +import hmac +import secrets +import time +from dataclasses import dataclass +from urllib.parse import urlencode + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .platform import PlatformError + + +@dataclass +class Transaction: + secret_hash: str + expires: float + status: str = "waiting" + result: dict | None = None + owner: str = "" + + +class Empty(BaseModel): + host_id: str = "" + proof: dict = Field(default_factory=dict) + + +class Refresh(BaseModel): + refresh_token: str = Field(min_length=1, max_length=4096) + + +class OAuth: + """A single connector process owns transactions and tokens in memory only.""" + def __init__(self, settings, redis, platform): + self.settings, self.redis, self.platform = settings, redis, platform + self.pending: dict[str, Transaction] = {} + self.lock = asyncio.Lock() + self.router = APIRouter() + self.router.add_api_route("/oauth/start", self.start, methods=["POST"], status_code=201) + self.router.add_api_route("/oauth/cb", self.callback, methods=["GET"]) + self.router.add_api_route("/oauth/claim", self.claim, methods=["GET"]) + self.router.add_api_route("/oauth/pending", self.status, methods=["GET"]) + self.router.add_api_route("/oauth/refresh", self.refresh, methods=["POST"]) + + def prune(self): + now = time.monotonic() + for state in list(self.pending): + if self.pending[state].expires <= now: + del self.pending[state] + + def authenticated(self, state, secret): + self.prune() + item = self.pending.get(state) + digest = hashlib.sha256((secret or "").encode()).hexdigest() + if not item or not hmac.compare_digest(item.secret_hash, digest): + raise HTTPException(404, "transaction not found") + return item + + async def start(self, body: Empty, request: Request): + if not self.settings.feishu_enabled or not self.settings.oauth_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.redis, "oauth:start:" + peer(request), 20) + owner = "" + if body.host_id: + from .work_api import device_owner + owner = await device_owner(self.settings, self.redis, self.store, body.host_id, + body.proof, "work-oauth-v1", {}) + async with self.lock: + self.prune() + if len(self.pending) >= self.settings.max_pending: + raise HTTPException(503, "too many pending transactions") + state, secret = secrets.token_urlsafe(32), secrets.token_urlsafe(32) + self.pending[state] = Transaction(hashlib.sha256(secret.encode()).hexdigest(), time.monotonic() + 600, owner=owner) + redirect = self.settings.public_url.rstrip("/") + "/oauth/cb" + parameters = {"app_id": self.settings.feishu_app_id, "redirect_uri": redirect, "state": state} + if self.settings.feishu_oauth_scopes: + parameters["scope"] = self.settings.feishu_oauth_scopes + url = self.settings.feishu_authorize_url + "?" + urlencode(parameters) + return no_cache(JSONResponse({"state": state, "claim_secret": secret, "authorization_url": url, + "redirect_uri": redirect, "expires_in": 600, + "code_ttl": 60, "poll_interval": 2}, status_code=201)) + + async def callback(self, request: Request, state: str = "", code: str = "", error: str = ""): + await rate_limit(self.redis, "oauth:cb:" + peer(request)) + async with self.lock: + self.prune() + item = self.pending.get(state) + if error and item and item.status == "waiting": + item.status = "failed" + return no_cache(HTMLResponse("

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

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

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

")) + + async def claim(self, request: Request, state: str): + await rate_limit(self.redis, "oauth:claim:" + peer(request)) + async with self.lock: + item = self.authenticated(state, request.headers.get("X-OAuth-Claim-Secret")) + if item.status == "failed": + del self.pending[state] + raise HTTPException(502, "authorization failed") + if item.status != "ready": + return no_cache(JSONResponse({"status": "waiting", "retry_after": 2}, status_code=202)) + del self.pending[state] + return no_cache(JSONResponse({"status": "claimed", **item.result})) + + async def status(self, request: Request, state: str): + await rate_limit(self.redis, "oauth:pending:" + peer(request)) + async with self.lock: + item = self.authenticated(state, request.headers.get("X-OAuth-Claim-Secret")) + return no_cache(JSONResponse({"status": item.status})) + + async def refresh(self, body: Refresh, request: Request): + if not self.settings.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.redis, "oauth:refresh:" + peer(request), 20) + # Lock and consume before the upstream call; ambiguous failures never retry an old refresh token. + digest = hashlib.sha256(body.refresh_token.encode()).hexdigest() + if not await self.redis.set("hn:refresh:" + digest, "used", nx=True, ex=2592000): + raise HTTPException(409, "refresh already attempted; use returned token or reauthorize") + try: + result = await self.platform.refresh(body.refresh_token) + except PlatformError: + raise HTTPException(502, "refresh failed; reauthorize") from None + return no_cache(JSONResponse(result)) diff --git a/patch-source/02ffd84040a628b7/relay/platform.py b/patch-source/02ffd84040a628b7/relay/platform.py new file mode 100644 index 0000000..a43a181 --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/platform.py @@ -0,0 +1,92 @@ +import asyncio +import json +import time +import httpx + + +class PlatformError(Exception): + pass + + +class Feishu: + def __init__(self, settings, transport=None): + self.settings = settings + self.http = httpx.AsyncClient(base_url=settings.feishu_api_base.rstrip("/"), + timeout=15, follow_redirects=False, transport=transport, trust_env=False) + self._token = None + self._expires = 0 + self._tenant_token = None + self._tenant_expires = 0 + self._lock = asyncio.Lock() + + async def close(self): + await self.http.aclose() + + async def _json(self, method, path, **kwargs): + try: + response = await self.http.request(method, path, **kwargs) + response.raise_for_status() + data = response.json() + if data.get("code", 0) != 0: + raise PlatformError("platform rejected request") + return data + except (httpx.HTTPError, ValueError, KeyError) as exc: + # Never propagate upstream bodies or credentials to caller/logs. + raise PlatformError("platform request failed") from None + + async def app_token(self): + async with self._lock: + if self._token and self._expires > time.monotonic() + 1800: + return self._token + data = await self._json("POST", "/open-apis/auth/v3/app_access_token/internal", + json={"app_id": self.settings.feishu_app_id, + "app_secret": self.settings.feishu_app_secret}) + self._token = data["app_access_token"] + self._expires = time.monotonic() + int(data.get("expire", 7200)) + return self._token + + async def exchange(self, code): + token = await self.app_token() + data = await self._json("POST", "/open-apis/authen/v1/oidc/access_token", + headers={"Authorization": f"Bearer {token}"}, + json={"grant_type": "authorization_code", "code": code}) + return self.normalize(data["data"]) + + async def tenant_token(self): + async with self._lock: + if self._tenant_token and self._tenant_expires > time.monotonic() + 1800: + return self._tenant_token + data = await self._json("POST", "/open-apis/auth/v3/tenant_access_token/internal", + json={"app_id": self.settings.feishu_app_id, + "app_secret": self.settings.feishu_app_secret}) + self._tenant_token = data["tenant_access_token"] + self._tenant_expires = time.monotonic() + int(data.get("expire", 7200)) + return self._tenant_token + + async def refresh(self, refresh_token): + token = await self.app_token() + data = await self._json("POST", "/open-apis/authen/v1/oidc/refresh_access_token", + headers={"Authorization": f"Bearer {token}"}, + json={"grant_type": "refresh_token", "refresh_token": refresh_token}) + result = self.normalize(data["data"]) + info = await self.user_info(result["user_access_token"]) + result["open_id"] = info["open_id"] + return result + + @staticmethod + def normalize(data): + return {"user_access_token": data["access_token"], "refresh_token": data.get("refresh_token", ""), + "expires_in": data.get("expires_in", data.get("expire", 0)), + "refresh_expires_in": data.get("refresh_expires_in", 0), "open_id": data.get("open_id", "")} + + async def user_info(self, token): + data = await self._json("GET", "/open-apis/authen/v1/user_info", + headers={"Authorization": f"Bearer {token}"}) + return data["data"] + + async def reply(self, message_id, text): + # Reply to the original message only: no caller-controlled recipient. + token = await self.tenant_token() + await self._json("POST", f"/open-apis/im/v1/messages/{message_id}/reply", + headers={"Authorization": f"Bearer {token}"}, + json={"msg_type": "text", "content": json.dumps({"text": text}, ensure_ascii=False)}) diff --git a/patch-source/02ffd84040a628b7/relay/process_lock.py b/patch-source/02ffd84040a628b7/relay/process_lock.py new file mode 100644 index 0000000..c37da53 --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/process_lock.py @@ -0,0 +1,46 @@ +"""Per-device advisory process lock: same-user GUI and CLI cannot race.""" +import os +from pathlib import Path + + +class DeviceAlreadyRunning(RuntimeError): + pass + + +class DeviceProcessLock: + def __init__(self, directory): + self.path = Path(directory) / "bridge.lock" + self.file = None + + def __enter__(self): + self.path.parent.mkdir(parents=True, exist_ok=True) + self.file = self.path.open("a+b") + self.file.seek(0, 2) + if self.file.tell() == 0: + self.file.write(b"0") + self.file.flush() + self.file.seek(0) + try: + if os.name == "nt": + import msvcrt + msvcrt.locking(self.file.fileno(), msvcrt.LK_NBLCK, 1) + else: + import fcntl + fcntl.flock(self.file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) + except OSError: + self.file.close() + self.file = None + raise DeviceAlreadyRunning("该设备已有桥接进程运行,请勿重复启动。") from None + return self + + def __exit__(self, *args): + if self.file: + self.file.seek(0) + if os.name == "nt": + import msvcrt + msvcrt.locking(self.file.fileno(), msvcrt.LK_UNLCK, 1) + else: + import fcntl + fcntl.flock(self.file.fileno(), fcntl.LOCK_UN) + self.file.close() + self.file = None diff --git a/patch-source/02ffd84040a628b7/relay/router.py b/patch-source/02ffd84040a628b7/relay/router.py new file mode 100644 index 0000000..e8d91e1 --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/router.py @@ -0,0 +1,167 @@ +import asyncio +import contextlib +import json +import secrets +import time +from contextlib import asynccontextmanager + +from fastapi import FastAPI, WebSocket, WebSocketDisconnect +from redis.asyncio import Redis +from redis.exceptions import WatchError + +from .config import Settings +from .crypto import fingerprint, raw_public, verified, verify_device + + +async def compare_delete(redis, key, expected): + async with redis.pipeline() as pipe: + try: + await pipe.watch(key) + if await pipe.get(key) != expected: + return False + pipe.multi() + pipe.delete(key) + await pipe.execute() + return True + except WatchError: + return False + + +async def compare_expire(redis, key, expected, ttl): + async with redis.pipeline() as pipe: + try: + await pipe.watch(key) + if await pipe.get(key) != expected: + return False + pipe.multi() + pipe.expire(key, ttl) + await pipe.execute() + return True + except WatchError: + return False + + +def create_app(settings=None, redis=None): + s = settings or Settings(role="router") + r = redis or Redis.from_url(s.redis_url, decode_responses=True) + ca = s.ca_cert.read_bytes() + active = set() + + @asynccontextmanager + async def lifespan(app): + await r.ping() + yield + if redis is None: + await r.aclose() + + app = FastAPI(title="Hainuo B2 router", lifespan=lifespan, docs_url=None, redoc_url=None) + app.state.redis = r + + @app.get("/health") + async def health(): + return {"status": "alive", "role": "router"} + + @app.get("/ready") + async def ready(): + await r.ping() + return {"status": "ready", "connections": len(active)} + + @app.websocket("/device/ws") + async def device(ws: WebSocket): + from .common import rate_limit + from fastapi import HTTPException + await ws.accept() + if len(active) >= s.max_connections: + await ws.close(1013) + return + ip = ws.client.host if ws.client else "unknown" + try: + await rate_limit(r, "ws:" + ip, 30) + except HTTPException: + await ws.close(1008) + return + challenge = secrets.token_urlsafe(32) + host = session = presence = None + tasks = [] + pub = None + try: + await ws.send_json({"type": "challenge", "challenge": challenge}) + hello = await asyncio.wait_for(ws.receive_json(), timeout=10) + if len(json.dumps(hello)) > 20000: + raise ValueError("oversized hello") + host = hello["host_id"] + if not isinstance(host, str) or len(host) != 32 or any(c not in "0123456789abcdef" for c in host): + raise ValueError("invalid host") + public = verify_device(hello["cert"], ca, host) + proof = verified(hello["proof"], public) + if proof["challenge"] != challenge or proof["host_id"] != host or abs(time.time() - proof["ts"]) > 90: + raise ValueError("invalid proof") + if len(proof["kx"]) > 64 or len(proof["session_id"]) > 64: + raise ValueError("invalid session") + fp = fingerprint(hello["cert"]) + if await r.get("hn:device:" + host) != fp or await r.exists("hn:revoked:" + host): + raise ValueError("unregistered/revoked") + session = proof["session_id"] + presence = json.dumps({"cert": hello["cert"], "proof": hello["proof"], "challenge": challenge}) + if not await r.set("hn:online:" + host, presence, nx=True, ex=30): + raise ValueError("already connected") + active.add(session) + pub = r.pubsub() + await pub.subscribe("hn:down:" + host, "hn:kill:" + host) + await ws.send_json({"type": "connected", "session_id": session}) + + async def downstream(): + async for msg in pub.listen(): + if msg["type"] != "message": + continue + if msg["channel"] == "hn:kill:" + host: + await ws.close(1008) + return + await ws.send_text(msg["data"]) + + async def heartbeat(): + while True: + await asyncio.sleep(8) + if await r.exists("hn:revoked:" + host) or not await compare_expire(r, "hn:online:" + host, presence, 30): + await ws.close(1008) + return + + async def upstream(): + while True: + text = await ws.receive_text() + if len(text.encode()) > s.max_body: + raise ValueError("oversized frame") + data = json.loads(text) + if data.get("type") == "ping": + continue + await rate_limit(r, "ws:reply:" + host, 120) + turn = data.get("turn_id", "") + if data.get("type") != "reply" or await r.get(f"hn:inflight:{host}:{turn}") != session: + raise ValueError("unsolicited reply") + envelope = data["envelope"] + verified(envelope, public) + if envelope["host_id"] != host or envelope["session_id"] != session: + raise ValueError("wrong reply destination") + await r.publish("hn:replies", json.dumps(data)) + + tasks = [asyncio.create_task(x()) for x in (downstream, heartbeat, upstream)] + done, _ = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED) + for task in done: + task.result() + except (WebSocketDisconnect, asyncio.TimeoutError, ValueError, KeyError, TypeError): + with contextlib.suppress(Exception): + await ws.close(1008) + except Exception: + with contextlib.suppress(Exception): + await ws.close(1011) + finally: + for task in tasks: + task.cancel() + await asyncio.gather(*tasks, return_exceptions=True) + if pub: + await pub.aclose() + if presence: + await compare_delete(r, "hn:online:" + host, presence) + active.discard(session) + + return app diff --git a/patch-source/02ffd84040a628b7/relay/store.py b/patch-source/02ffd84040a628b7/relay/store.py new file mode 100644 index 0000000..9d6f8ef --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/store.py @@ -0,0 +1,97 @@ +"""Connector-owned persistent metadata. Never stores message/file/token bodies.""" +import sqlite3 +import time +import psycopg +from psycopg.rows import dict_row + +INTEGRITY_ERRORS = (sqlite3.IntegrityError, psycopg.IntegrityError) + + +class Postgres: + def __init__(self, url): + self.url = url + self.conn = psycopg.connect(url, autocommit=True, row_factory=dict_row) + self.transaction = None + + def execute(self, sql, params=()): + if self.conn.closed or self.conn.broken: + self.conn = psycopg.connect(self.url, autocommit=True, row_factory=dict_row) + return self.conn.execute(sql.replace("?", "%s"), params) + + def __enter__(self): + self.transaction = self.conn.transaction() + self.transaction.__enter__() + return self + + def __exit__(self, *args): + return self.transaction.__exit__(*args) + + def close(self): + self.conn.close() + + +class Store: + def __init__(self, path, database_url=""): + path.parent.mkdir(parents=True, exist_ok=True) + schema = """ + CREATE TABLE IF NOT EXISTS devices( + host_id TEXT PRIMARY KEY, cert TEXT NOT NULL, fingerprint TEXT NOT NULL, + owner TEXT UNIQUE, revoked INTEGER NOT NULL DEFAULT 0); + CREATE TABLE IF NOT EXISTS turns( + turn_id TEXT PRIMARY KEY, owner TEXT NOT NULL, host_id TEXT NOT NULL, + status TEXT NOT NULL, created DOUBLE PRECISION NOT NULL); + CREATE TABLE IF NOT EXISTS audit( + id INTEGER PRIMARY KEY, ts DOUBLE PRECISION NOT NULL, actor TEXT NOT NULL, + action TEXT NOT NULL, resource TEXT NOT NULL, result TEXT NOT NULL); + """ + if database_url: + self.db = Postgres(database_url) + self.db.conn.execute(schema.replace("id INTEGER PRIMARY KEY", "id BIGSERIAL PRIMARY KEY")) + else: + self.db = sqlite3.connect(path, check_same_thread=False) + self.db.row_factory = sqlite3.Row + self.db.executescript("PRAGMA journal_mode=WAL;" + schema) + + def device(self, host): + row = self.db.execute("SELECT * FROM devices WHERE host_id=?", (host,)).fetchone() + return dict(row) if row else None + + def by_owner(self, owner): + row = self.db.execute("SELECT * FROM devices WHERE owner=? AND revoked=0", (owner,)).fetchone() + return dict(row) if row else None + + def add_device(self, host, cert, fp): + with self.db: + self.db.execute("INSERT INTO devices(host_id,cert,fingerprint) VALUES(?,?,?)", (host, cert, fp)) + + def bind(self, host, owner): + with self.db: + # Conditional update also protects against two connectors racing for the same device. + count = self.db.execute("UPDATE devices SET owner=? WHERE host_id=? AND owner IS NULL AND revoked=0", (owner, host)).rowcount + if count != 1: + raise ValueError("device unavailable or already bound") + + def unbind(self, owner): + with self.db: + self.db.execute("UPDATE devices SET owner=NULL WHERE owner=?", (owner,)) + + def revoke(self, host): + with self.db: + self.db.execute("UPDATE devices SET revoked=1 WHERE host_id=?", (host,)) + + def begin_turn(self, turn, owner, host): + with self.db: + return self.db.execute("INSERT INTO turns VALUES(?,?,?,?,?) ON CONFLICT(turn_id) DO NOTHING", + (turn, owner, host, "inflight", time.time())).rowcount == 1 + + def finish(self, turn, status): + with self.db: + self.db.execute("UPDATE turns SET status=? WHERE turn_id=?", (status, turn)) + + def audit(self, actor, action, resource, result): + with self.db: + self.db.execute("INSERT INTO audit(ts,actor,action,resource,result) VALUES(?,?,?,?,?)", + (time.time(), actor, action, resource, result)) + + def close(self): + self.db.close() diff --git a/patch-source/02ffd84040a628b7/relay/web_session.py b/patch-source/02ffd84040a628b7/relay/web_session.py new file mode 100644 index 0000000..1ac9adf --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/web_session.py @@ -0,0 +1,183 @@ +"""Pilot H5 pairing and conversation. Credentials never appear in URLs.""" +import hashlib +import json +import secrets +import time +from urllib.parse import urlsplit + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import JSONResponse +from pydantic import BaseModel, Field + +from .common import no_cache, peer, rate_limit +from .crypto import sign, verified, verify_device +from .platform import PlatformError + +COOKIE = "hn_web_session" + + +def digest(value): + return hashlib.sha256(value.encode()).hexdigest() + + +def origin_tuple(value): + try: + parts = urlsplit(value) + if parts.scheme not in {"https", "http"} or not parts.hostname or parts.username or parts.password: + return None + return parts.scheme, parts.hostname.lower(), parts.port or (443 if parts.scheme == "https" else 80) + except ValueError: + return None + + +class PairProof(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Claim(BaseModel): + code: str = Field(min_length=12, max_length=128) + + +class Chat(BaseModel): + text: str = Field(min_length=1, max_length=8000) + request_id: str = Field(pattern=r"^[a-f0-9]{32}$") + + +class WebSessions: + def __init__(self, settings, redis, store, broker, platform): + self.s, self.r, self.store, self.broker, self.platform = settings, redis, store, broker, platform + self.router = APIRouter() + for path, method, handler in [ + ("/device/web-pair", "POST", self.device_pair), + ("/web/claim", "POST", self.claim), + ("/web/session", "GET", self.session), + ("/web/platform-session", "POST", self.platform_login), + ("/web/chat", "POST", self.chat), + ("/web/logout", "POST", self.logout), + ]: + self.router.add_api_route(path, handler, methods=[method]) + + def check_origin(self, request): + actual = origin_tuple(request.headers.get("Origin", "")) + if not actual or actual != origin_tuple(self.s.public_url): + raise HTTPException(403, "same-origin browser request required") + + async def valid_device(self, record): + device = self.store.device(record["host_id"]) + if (not device or device["revoked"] or device["owner"] != record["owner"] + or device["fingerprint"] != record["fingerprint"]): + raise HTTPException(401, "device binding changed; pair again") + generation = await self.r.get("hn:web-generation:" + record["host_id"]) or "0" + if record.get("generation") != generation: + raise HTTPException(401, "device binding changed; pair again") + return device + + async def device_pair(self, body: PairProof, request: Request): + await rate_limit(self.r, "web-pair-ip:" + peer(request), 20) + device = self.store.device(body.host_id) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "active bound device required") + try: + key = verify_device(device["cert"], self.s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["owner"] != device["owner"] + or proof["purpose"] != "h5-pair-v1" or abs(time.time() - proof["ts"]) > 90 + or not isinstance(proof["nonce"], str) or len(proof["nonce"]) != 32): + raise ValueError("invalid proof") + except Exception: + raise HTTPException(403, "invalid device proof") from None + replay = "hn:web-pair-proof:" + digest(body.host_id + proof["nonce"]) + if not await self.r.set(replay, "used", nx=True, ex=180): + raise HTTPException(409, "pairing request already used") + await rate_limit(self.r, "web-pair-device:" + body.host_id, 5) + code = secrets.token_urlsafe(18) + record = {"host_id": body.host_id, "owner": device["owner"], "fingerprint": device["fingerprint"], + "generation": await self.r.get("hn:web-generation:" + body.host_id) or "0"} + await self.r.set("hn:web-code:" + digest(code), json.dumps(record), ex=120) + self.store.audit(device["owner"], "web_pair_issued", body.host_id, "success") + return no_cache(JSONResponse(sign({"host_id": body.host_id, "code": code, "expires_in": 120, + "url": self.s.public_url.rstrip("/") + "/h5"}, self.broker.signer))) + + async def new_session(self, record): + token = secrets.token_urlsafe(32) + record = {k: record[k] for k in ("host_id", "owner", "fingerprint", "generation")} + await self.r.set("hn:web-session:" + digest(token), json.dumps(record), ex=1800) + response = no_cache(JSONResponse({"status": "paired", "host_id": record["host_id"], "expires_in": 1800})) + response.set_cookie(COOKIE, token, max_age=1800, secure=True, httponly=True, samesite="strict", path="/") + return response + + async def claim(self, body: Claim, request: Request): + self.check_origin(request) + await rate_limit(self.r, "web-claim:" + peer(request), 20) + key = "hn:web-code:" + digest(body.code.strip()) + encoded = await self.r.get(key) + if not encoded: + raise HTTPException(404, "code expired or consumed") + await self.valid_device(json.loads(encoded)) + encoded = await self.r.getdel(key) + if not encoded: + raise HTTPException(404, "code already consumed") + record = json.loads(encoded) + await self.valid_device(record) + self.store.audit(record["owner"], "web_pair_claimed", record["host_id"], "success") + return await self.new_session(record) + + async def authenticated(self, request): + token = request.cookies.get(COOKIE, "") + if not token or len(token) > 128: + raise HTTPException(401, "pair this browser first") + encoded = await self.r.get("hn:web-session:" + digest(token)) + if not encoded: + raise HTTPException(401, "browser session expired") + record = json.loads(encoded) + await self.valid_device(record) + return record + + async def session(self, request: Request): + record = await self.authenticated(request) + return no_cache(JSONResponse({"authenticated": True, "host_id": record["host_id"], + "online": bool(await self.r.exists("hn:online:" + record["host_id"])), + "mode": "pilot-text-only", "relay_sees_content": True})) + + async def platform_login(self, request: Request): + self.check_origin(request) + if not self.s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.r, "web-platform:" + peer(request), 20) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await self.platform.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = self.store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "bind your device first") + return await self.new_session({**device, "generation": await self.r.get("hn:web-generation:" + device["host_id"]) or "0"}) + + async def chat(self, body: Chat, request: Request): + self.check_origin(request) + record = await self.authenticated(request) + await rate_limit(self.r, "web-chat:" + record["owner"], 20) + text = body.text.strip() + if not text: + raise HTTPException(422, "empty message") + # PC accepts only its bound owner and the existing IM read-only channel. + if not (text == "/主机名" or text.startswith(("/回显 ", "/读取 ", "/助手 "))): + text = "/助手 " + text + turn = "web_" + digest(record["owner"] + "\0" + body.request_id) + result = await self.broker.dispatch(record["owner"], text, turn) + # Revoke/unbind during a long model call invalidates the browser immediately. + await self.authenticated(request) + return no_cache(JSONResponse(result)) + + async def logout(self, request: Request): + self.check_origin(request) + token = request.cookies.get(COOKIE, "") + if token and len(token) <= 128: + await self.r.delete("hn:web-session:" + digest(token)) + response = no_cache(JSONResponse({"status": "logged_out"})) + response.delete_cookie(COOKIE, secure=True, httponly=True, samesite="strict", path="/") + return response diff --git a/patch-source/02ffd84040a628b7/relay/windows_keys.py b/patch-source/02ffd84040a628b7/relay/windows_keys.py new file mode 100644 index 0000000..0e84e19 --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/windows_keys.py @@ -0,0 +1,40 @@ +"""Current-user Windows DPAPI for device keys; never uses machine-wide scope.""" +import ctypes +import os +from ctypes import wintypes + + +class Blob(ctypes.Structure): + _fields_ = [("cbData", wintypes.DWORD), ("pbData", ctypes.POINTER(ctypes.c_ubyte))] + + +def transform(data, decrypt=False): + if os.name != "nt": + raise RuntimeError("DPAPI is supported only on Windows") + buffer = (ctypes.c_ubyte * len(data)).from_buffer_copy(data) + source = Blob(len(data), buffer) + target = Blob() + crypt = ctypes.WinDLL("crypt32", use_last_error=True) + kernel = ctypes.WinDLL("kernel32", use_last_error=True) + if decrypt: + fn = crypt.CryptUnprotectData + args = [ctypes.byref(source), None, None, None, None, 1, ctypes.byref(target)] + else: + fn = crypt.CryptProtectData + args = [ctypes.byref(source), "Hainuo device identity", None, None, None, 1, ctypes.byref(target)] + fn.restype = wintypes.BOOL + if not fn(*args): + raise ctypes.WinError(ctypes.get_last_error()) + try: + return ctypes.string_at(target.pbData, target.cbData) + finally: + kernel.LocalFree.argtypes = [ctypes.c_void_p] + kernel.LocalFree(target.pbData) + + +def protect(data): + return transform(data) + + +def unprotect(data): + return transform(data, decrypt=True) diff --git a/patch-source/02ffd84040a628b7/relay/work_api.py b/patch-source/02ffd84040a628b7/relay/work_api.py new file mode 100644 index 0000000..536ae2a --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/work_api.py @@ -0,0 +1,142 @@ +"""Device-authenticated USER-token business gateway; no tenant-token fallback.""" +import hashlib +import json +import re +import time +from urllib.parse import unquote + +import httpx +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import JSONResponse +from pydantic import BaseModel, Field +from .common import no_cache, rate_limit +from .crypto import canonical, verified, verify_device +from .store import INTEGRITY_ERRORS + + +class DeviceProof(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class BusinessCall(DeviceProof): + method: str = Field(pattern=r"^(GET|POST|PATCH|DELETE)$") + path: str = Field(max_length=1500) + params: dict = Field(default_factory=dict) + body: dict = Field(default_factory=dict) + operation_id: str = Field(default="", max_length=100) + + +def business_allowed(method, path): + if any(x in unquote(path) for x in ("..", "\\", "%", "?", "#", "\r", "\n")): + return False + seg = r"[A-Za-z0-9_.@-]+" + cal = r"/open-apis/calendar/v4/calendars" + rules = { + "GET": [cal, cal + "/" + seg, cal + "/" + seg + "/events(?:/" + seg + ")?(?:/attendees)?", + r"/open-apis/contact/v3/users/" + seg, r"/open-apis/search/v1/user", + r"/open-apis/(?:drive/v1/files|docx/v1/documents|wiki/v2/spaces|bitable/v1/apps)(?:/" + seg + ")*"], + "POST": [cal + "/primary", cal + "/" + seg + "/events(?:/" + seg + "/attendees)?"], + "PATCH": [cal + "/" + seg + "/events/" + seg], + "DELETE": [cal + "/" + seg + "/events/" + seg, cal + "/" + seg + "/events/" + seg + "/attendees/" + seg], + } + return any(re.fullmatch(p, path) for p in rules.get(method, [])) + + +async def device_owner(settings, redis, store, host, proof, purpose, payload): + device = store.device(host) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "active bound device required") + try: + key = verify_device(device["cert"], settings.ca_cert.read_bytes(), host) + data = verified(proof, key) + if (data["host_id"] != host or data["purpose"] != purpose or abs(time.time() - data["ts"]) > 90 + or not re.fullmatch(r"[a-f0-9]{32}", data["nonce"]) + or data["payload_hash"] != hashlib.sha256(canonical(payload)).hexdigest()): + raise ValueError() + except Exception: + raise HTTPException(403, "invalid device request proof") from None + if not await redis.set("hn:work-proof:" + host + ":" + data["nonce"], "1", nx=True, ex=180): + raise HTTPException(409, "device request replayed") + return device["owner"] + + +def router(settings, redis, store, platform): + routes = APIRouter() + store.db.execute("""CREATE TABLE IF NOT EXISTS business_operations( + owner TEXT NOT NULL, operation_id TEXT NOT NULL, request_hash TEXT NOT NULL, + status TEXT NOT NULL, result_meta TEXT NOT NULL, created DOUBLE PRECISION NOT NULL, + PRIMARY KEY(owner,operation_id))""") + if not settings.database_url: + store.db.commit() + + @routes.post("/work/api") + async def call(body: BusinessCall, request: Request): + if not settings.feishu_enabled: + raise HTTPException(503, "platform disabled") + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or not 10 < len(auth) <= 8192: + raise HTTPException(401, "user access token required") + if not business_allowed(body.method, body.path): + raise HTTPException(403, "business method/path not supported") + payload = {"method": body.method, "path": body.path, "params": body.params, "body": body.body, + "operation_id": body.operation_id, "token_hash": hashlib.sha256(auth[7:].encode()).hexdigest()} + owner = await device_owner(settings, redis, store, body.host_id, body.proof, "work-api-v1", payload) + await rate_limit(redis, "work-api:" + owner, 60) + try: + info = await platform.user_info(auth[7:]) + except Exception: + raise HTTPException(401, "user token expired or invalid; refresh or authorize") from None + if info.get("open_id") != owner: + raise HTTPException(403, "token user does not match bound device owner") + write = body.method != "GET" and not body.path.endswith(("/primary", "/batch_get_id")) + digest = hashlib.sha256(canonical({k: v for k, v in payload.items() if k != "token_hash"})).hexdigest() + if write: + if not re.fullmatch(r"[A-Za-z0-9_-]{8,100}", body.operation_id): + raise HTTPException(422, "write requires stable operation_id") + try: + with store.db: + store.db.execute("INSERT INTO business_operations VALUES(?,?,?,?,?,?)", + (owner, body.operation_id, digest, "inflight", "{}", time.time())) + except INTEGRITY_ERRORS: + row = dict(store.db.execute("SELECT * FROM business_operations WHERE owner=? AND operation_id=?", + (owner, body.operation_id)).fetchone()) + if row["request_hash"] != digest: + raise HTTPException(409, "operation_id reused with different request") + return no_cache(JSONResponse({"status": "duplicate", "operation_id": body.operation_id, + "previous_status": row["status"], **json.loads(row["result_meta"])})) + params = dict(body.params) + if write and body.method == "POST" and body.path.endswith("/events"): + params["idempotency_key"] = hashlib.sha256((owner + body.operation_id).encode()).hexdigest() + try: + async with platform.http.stream(body.method, body.path, params=params, + json=body.body if body.method != "GET" else None, + headers={"Authorization": auth}) as upstream: + raw = bytearray() + async for chunk in upstream.aiter_bytes(): + raw.extend(chunk) + if len(raw) > 2 * 1024 * 1024: + raise ValueError() + data = json.loads(raw) + if not isinstance(data, dict): + raise ValueError() + code = data.get("code", 0) + if upstream.status_code != 200 or code != 0: + status = "platform_rejected" + result = {"status": status, "http_status": upstream.status_code, "platform_code": code, + "message": "平台拒绝:请核对用户授权、应用scope、资源权限或接口版本。"} + else: + status, result = "completed", {"status": "completed", "data": data.get("data", {})} + except (httpx.HTTPError, ValueError): + status = "unknown" if write else "failed" + result = {"status": status, "message": "平台请求未取得明确结果;写操作不要换新ID重试,请先核对日历。"} + if write: + event = result.get("data", {}).get("event", {}) + meta = {k: event[k] for k in ("event_id", "app_link") if k in event} + with store.db: + store.db.execute("UPDATE business_operations SET status=?,result_meta=? WHERE owner=? AND operation_id=?", + (status, json.dumps(meta), owner, body.operation_id)) + store.audit(owner, "work_api_" + body.method.lower(), body.path, status) + return no_cache(JSONResponse(result)) + + return routes diff --git a/patch-source/02ffd84040a628b7/relay/work_client.py b/patch-source/02ffd84040a628b7/relay/work_client.py new file mode 100644 index 0000000..4cc90b3 --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/work_client.py @@ -0,0 +1,190 @@ +"""PC-side user OAuth and calendar operations. Tokens are DPAPI-protected only here.""" +import hashlib +import json +import secrets +import ssl +import time +from datetime import datetime +from pathlib import Path +from urllib.parse import quote, urlsplit + +import httpx +from .crypto import canonical, load_private, sign +from .windows_keys import protect, unprotect +from .process_lock import DeviceProcessLock + + +class WorkError(RuntimeError): + pass + + +class AuthorizationRequired(WorkError): + def __init__(self, url): + self.url = url + super().__init__("需要本人Work授权,请打开授权链接,完成后重新发起原任务;尚未执行平台写操作。") + + +def atomic_secret(path, data): + temp = path.with_suffix(".tmp") + temp.write_bytes(protect(json.dumps(data, ensure_ascii=False).encode())) + temp.replace(path) + + +class WorkClient: + def __init__(self, directory, url="https://10.77.76.13", *, transport=None): + self.directory = Path(directory) + self.config = json.loads((self.directory / "device.json").read_text(encoding="utf-8-sig")) + if not self.config.get("owner"): + raise WorkError("请先绑定本人Work身份。") + context = ssl.create_default_context(cafile=str(self.directory / "server.pem")) + self.http = httpx.Client(base_url=url.rstrip("/"), verify=context, trust_env=False, + timeout=25, follow_redirects=False, transport=transport) + self.owner = self.config["owner"] + self.path = self.directory / "work-user-tokens.dpapi" + self.pending = self.directory / "work-oauth-pending.dpapi" + + def close(self): + self.http.close() + + def read(self, path): + return json.loads(unprotect(path.read_bytes())) + + def proof(self, purpose, payload): + return sign({"host_id": self.config["host_id"], "purpose": purpose, "ts": int(time.time()), + "nonce": secrets.token_hex(16), "payload_hash": hashlib.sha256(canonical(payload)).hexdigest()}, + load_private(self.directory / "device.key")) + + def authorize(self): + if self.pending.exists(): + data = self.read(self.pending) + if data.get("owner") == self.owner and data["deadline"] > time.time(): + return data["authorization_url"] + response = self.http.post("/oauth/start", json={"host_id": self.config["host_id"], + "proof": self.proof("work-oauth-v1", {})}) + if response.status_code != 201: + raise WorkError("授权事务创建失败(HTTP " + str(response.status_code) + "),请检查ECS平台配置。") + data = response.json() + if urlsplit(data["authorization_url"]).scheme != "https": + raise WorkError("授权地址必须使用HTTPS。") + data.update(owner=self.owner, deadline=time.time() + data["expires_in"]) + atomic_secret(self.pending, data) + return data["authorization_url"] + + def save_tokens(self, data): + if data.get("open_id") != self.owner or not data.get("user_access_token"): + raise WorkError("授权账号与本机Work绑定账号不一致,请使用本人账号重新授权。") + now = time.time() + data = {**data, "expires_at": now + int(data["expires_in"]), + "refresh_expires_at": now + int(data.get("refresh_expires_in", 0)), "needs_authorization": False} + atomic_secret(self.path, data) + return data + + def access_token(self): + # Shared across GUI and Agent calls; never rotate the same token concurrently. + with DeviceProcessLock(self.directory / "work-token-lock"): + if self.path.exists(): + data = self.read(self.path) + if data.get("open_id") == self.owner and not data.get("needs_authorization"): + if data["expires_at"] > time.time() + 120: + return data["user_access_token"] + if data.get("refresh_token") and data.get("refresh_expires_at", 0) > time.time(): + # Mark before network; ambiguous failure must not reuse a rotating refresh token. + atomic_secret(self.path, {**data, "needs_authorization": True}) + try: + response = self.http.post("/oauth/refresh", json={"refresh_token": data["refresh_token"]}) + if response.status_code != 200: + raise WorkError("刷新失败") + data = self.save_tokens(response.json()) + return data["user_access_token"] + except (httpx.HTTPError, ValueError, WorkError): + raise AuthorizationRequired(self.authorize()) from None + if self.pending.exists(): + pending = self.read(self.pending) + if pending.get("owner") == self.owner and pending["deadline"] > time.time(): + response = self.http.get("/oauth/claim", params={"state": pending["state"]}, + headers={"X-OAuth-Claim-Secret": pending["claim_secret"]}) + if response.status_code == 200: + data = self.save_tokens(response.json()) + self.pending.unlink(missing_ok=True) + return data["user_access_token"] + if response.status_code not in {202}: + self.pending.unlink(missing_ok=True) + raise AuthorizationRequired(self.authorize()) + + def status(self): + try: + token = self.access_token() + return {"authorized": True, "owner": self.owner, "message": "本人用户授权可用;令牌未输出。", + "local_time": datetime.now().astimezone().isoformat()} + except AuthorizationRequired as error: + return {"authorized": False, "authorization_url": error.url, "message": str(error)} + + def api(self, method, path, params=None, body=None, operation_id=""): + token = self.access_token() + payload = {"method": method, "path": path, "params": params or {}, "body": body or {}, + "operation_id": operation_id, "token_hash": hashlib.sha256(token.encode()).hexdigest()} + proof = self.proof("work-api-v1", payload) + wire = {k: v for k, v in payload.items() if k != "token_hash"} + response = self.http.post("/work/api", headers={"Authorization": "Bearer " + token}, + json={**wire, "host_id": self.config["host_id"], "proof": proof}) + if response.status_code == 401: + # Do not repeat a business write automatically, even when auth fails. + if self.path.exists(): + data = self.read(self.path) + data["expires_at"] = 0 + atomic_secret(self.path, data) + raise WorkError("用户令牌已失效。本次未重试,请检查授权后再核对操作状态。") + if response.status_code != 200: + raise WorkError("平台代理失败(HTTP " + str(response.status_code) + ");未重试。") + return response.json() + + def primary_calendar(self): + result = self.api("POST", "/open-apis/calendar/v4/calendars/primary", {"user_id_type": "open_id"}) + if result.get("status") != "completed": + return result + calendars = result.get("data", {}).get("calendars", []) + own = [c["calendar"] for c in calendars if c.get("user_id") == self.owner and c.get("calendar")] + if not own and len(calendars) == 1 and not calendars[0].get("user_id") and calendars[0].get("calendar"): + own = [calendars[0]["calendar"]] + if len(own) != 1: + raise WorkError("没有唯一确定的本人主日历,请先查询日历列表并明确选择。") + return {"status": "completed", "calendar": own[0]} + + def create_event(self, summary, start_iso, end_iso, attendee_open_ids, operation_id, description="", calendar_id=""): + import re + if not re.fullmatch(r"[A-Za-z0-9_-]{8,80}", operation_id): + raise WorkError("创建日程需8到80位稳定operation_id,重试不得换ID。") + if not summary.strip() or len(summary) > 1000: + raise WorkError("日程主题不能为空或超过1000字符。") + start, end = datetime.fromisoformat(start_iso), datetime.fromisoformat(end_iso) + if start.tzinfo is None or end.tzinfo is None or end <= start: + raise WorkError("起止时间必须包含时区且结束晚于开始;不要猜测时区或时长。") + if any(not isinstance(x, str) or not x.startswith("ou_") for x in attendee_open_ids): + raise WorkError("参会人必须为当前应用身份域的open_id,请先搜索并确认,不能把姓名/工号直接当ID。") + if not calendar_id: + primary = self.primary_calendar() + if primary.get("status") != "completed": + return primary + calendar_id = primary["calendar"]["calendar_id"] + path = "/open-apis/calendar/v4/calendars/" + quote(calendar_id, safe="") + "/events" + body = {"summary": summary, "description": description, "need_notification": True, + "start_time": {"timestamp": str(int(start.timestamp())), "timezone": "Asia/Shanghai"}, + "end_time": {"timestamp": str(int(end.timestamp())), "timezone": "Asia/Shanghai"}} + result = self.api("POST", path, {"user_id_type": "open_id"}, body, operation_id) + event = result.get("data", {}).get("event", {}) + if result.get("status") == "duplicate" and result.get("previous_status") == "completed": + event = {"event_id": result.get("event_id"), "app_link": result.get("app_link")} + elif result.get("status") != "completed": + return result + if not event.get("event_id"): + raise WorkError("平台未返回event_id,不能认定创建成功或继续邀请。") + ids = sorted(set([self.owner] + list(attendee_open_ids))) + invite = self.api("POST", path + "/" + quote(event["event_id"], safe="") + "/attendees", + {"user_id_type": "open_id"}, + {"attendees": [{"type": "user", "user_id": x} for x in ids], "need_notification": True}, + operation_id + "_attendees") + invited = invite.get("status") == "completed" or (invite.get("status") == "duplicate" and invite.get("previous_status") == "completed") + return {"status": "completed" if invited else "partial", "event_created": True, + "calendar_id": calendar_id, "event_id": event["event_id"], "app_link": event.get("app_link"), + "attendees_invited": invited, "invite_result": invite, + "message": "日程已创建并提交参会人邀请;不代表对方已接受。" if invited else "日程已创建,但邀请未完成;保留原操作ID和event_id处理,勿重新创建日程。"} diff --git a/patch-source/02ffd84040a628b7/relay/work_tools.py b/patch-source/02ffd84040a628b7/relay/work_tools.py new file mode 100644 index 0000000..b1c6da4 --- /dev/null +++ b/patch-source/02ffd84040a628b7/relay/work_tools.py @@ -0,0 +1,145 @@ +"""Hermes native tools backed by the authenticated user-token relay.""" +import json +import os +import threading +import time +from pathlib import Path +import httpx +from .work_client import WorkClient, WorkError, AuthorizationRequired + +INSTRUCTIONS = """海能Work平台能力已接入本机专用工具:work_authorization、work_calendar、work_contacts、work_api_request。 +涉及飞书/海能Work日历或文档时优先调用这些工具,勿使用manage_connections寻找公有云连接器,勿索要或搜索App Secret。 +用户token仅由本机安全存储和ECS换取,第一次需要本人打开授权链接。工具返回authorization_required时将链接完整提供给本人,不能声称已经创建。 +当前日期以work_authorization返回的local_time为准。创建日程必须确定带时区的起止时间和真实参会人open_id。 +姓名/工号须用work_contacts搜索核实,多个候选必须澄清,不能猜open_id。平台返回partial表示已创建但邀请未完成,勿再次创建。 +同一写操作的operation_id必须保持不变,网络异常不得换ID重试。日程创建成功、邀请提交成功、对方接受是不同状态。 +""" + +_watching = set() +_watch_lock = threading.Lock() + + +def watch_authorization(directory): + """Claim within the server's 60-second result TTL, without keeping tokens in ECS.""" + name = str(directory) + with _watch_lock: + if name in _watching: + return + _watching.add(name) + def poll(): + client = None + try: + client = WorkClient(directory, os.environ.get("HN_WORK_RELAY_URL", "https://10.77.76.13")) + for _ in range(295): + time.sleep(2) + try: + if client.pending.exists() and client.read(client.pending).get("deadline", 0) <= time.time(): + return + if client.status().get("authorized"): + return + except Exception: + # Another local consumer may hold the short token lock. + continue + except Exception: + pass + finally: + if client: + client.close() + with _watch_lock: + _watching.discard(name) + threading.Thread(target=poll, daemon=True, name="work-user-auth-claim").start() + + +def register(directory): + from tools.registry import registry + import toolsets + definitions = [ + ("work_authorization", "查询或发起本人海能Work用户授权,返回可点击链接和本机时间;不返回token。", { + "type": "object", "properties": {}, "additionalProperties": False}), + ("work_calendar", "本人用户身份的日历:list/primary/list_events/get_event/create_event/invite。写操作operation_id需稳定,起止时间均带时区。", { + "type": "object", "properties": { + "action": {"type": "string", "enum": ["list", "primary", "list_events", "get_event", "create_event", "invite"]}, + "calendar_id": {"type": "string"}, "event_id": {"type": "string"}, + "summary": {"type": "string"}, "description": {"type": "string"}, + "start_iso": {"type": "string", "description": "ISO8601,例如2026-10-27T09:00:00+08:00"}, + "end_iso": {"type": "string"}, "attendee_open_ids": {"type": "array", "items": {"type": "string"}}, + "operation_id": {"type": "string", "description": "8-80位稳定操作ID,同一意图重试不能更换"}, + "params": {"type": "object"}}, "required": ["action"]}), + ("work_contacts", "使用本人user token搜索参会人;query可为姓名或工号,返回候选,不能未经核实猜ID。open_id可读取具体用户信息。", { + "type": "object", "properties": {"query": {"type": "string"}, "open_id": {"type": "string"}}}), + ("work_api_request", "携本人user token代理受支持业务API(日历CRUD/邀请、通讯录查询、文档云盘知识库读取)。禁止认证接口,不支持的路径返回403。写操作必须operation_id。", { + "type": "object", "properties": {"method": {"type": "string", "enum": ["GET", "POST", "PATCH", "DELETE"]}, + "path": {"type": "string"}, "params": {"type": "object"}, "body": {"type": "object"}, + "operation_id": {"type": "string"}}, "required": ["method", "path"]}), + ] + + def handler_for(name): + def handle(args, **kwargs): + client = None + try: + client = WorkClient(directory, os.environ.get("HN_WORK_RELAY_URL", "https://10.77.76.13")) + if name == "work_authorization": + result = client.status() + if not result.get("authorized"): + watch_authorization(directory) + elif name == "work_api_request": + result = client.api(**args) + elif name == "work_contacts": + from urllib.parse import quote + result = client.api("GET", "/open-apis/contact/v3/users/" + quote(args["open_id"], safe=""), + {"user_id_type": "open_id"}) if args.get("open_id") else client.api( + "GET", "/open-apis/search/v1/user", {"query": args.get("query", ""), "page_size": 20}) + else: + result = calendar(client, args) + return json.dumps(result, ensure_ascii=False) + except AuthorizationRequired as error: + watch_authorization(directory) + return json.dumps({"status": "authorization_required", "authorization_url": error.url, "message": str(error)}, ensure_ascii=False) + except (WorkError, ValueError, KeyError) as error: + return json.dumps({"error": str(error)[:500], "status": "failed"}, ensure_ascii=False) + except Exception as error: + return json.dumps({"error": type(error).__name__ + ";操作状态未确认,未自动重试。"}, ensure_ascii=False) + finally: + if client: + client.close() + return handle + + for name, description, parameters in definitions: + registry.register(name=name, toolset="hainuo_work", schema={"name": name, "description": description, "parameters": parameters}, + handler=handler_for(name), description=description) + toolsets.TOOLSETS["hainuo_work"] = {"description": "海能Work本人用户授权及日历业务", "tools": [x[0] for x in definitions], "includes": []} + # Present these four small schemas directly; otherwise Hermes defers custom + # toolsets behind tool_search and can wrongly search a public-cloud connector. + for name, _, _ in definitions: + if name not in toolsets._HERMES_CORE_TOOLS: + toolsets._HERMES_CORE_TOOLS.append(name) + + +def calendar(client, args): + from urllib.parse import quote + action = args["action"] + if action == "primary": + return client.primary_calendar() + if action == "list": + return client.api("GET", "/open-apis/calendar/v4/calendars", args.get("params", {})) + if action == "create_event": + return client.create_event(**{k: v for k, v in args.items() if k != "action" and k != "params"}) + calendar_id = args.get("calendar_id") + if not calendar_id: + primary = client.primary_calendar() + if primary.get("status") != "completed": + return primary + calendar_id = primary["calendar"]["calendar_id"] + base = "/open-apis/calendar/v4/calendars/" + quote(calendar_id, safe="") + "/events" + if action == "list_events": + return client.api("GET", base, args.get("params", {})) + base += "/" + quote(args["event_id"], safe="") + if action == "get_event": + return client.api("GET", base, {"user_id_type": "open_id"}) + if action == "invite": + ids = args.get("attendee_open_ids", []) + if not ids or any(not x.startswith("ou_") for x in ids): + raise WorkError("需要已确认的参会人open_id。") + return client.api("POST", base + "/attendees", {"user_id_type": "open_id"}, + {"attendees": [{"type": "user", "user_id": x} for x in ids], "need_notification": True}, args["operation_id"]) + raise WorkError("不支持的日历操作。") diff --git a/patch-source/02ffd84040a628b7/scripts/update-userapi.sh b/patch-source/02ffd84040a628b7/scripts/update-userapi.sh new file mode 100644 index 0000000..e4ffc5a --- /dev/null +++ b/patch-source/02ffd84040a628b7/scripts/update-userapi.sh @@ -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.' diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/Dockerfile.offline b/patch-source/0de3db40fc2044b0/hainuo-relay/Dockerfile.offline new file mode 100644 index 0000000..8af3dff --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/Dockerfile.offline @@ -0,0 +1,10 @@ +FROM python:3.12-slim-bookworm +ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 +WORKDIR /app +COPY wheelhouse /wheels +COPY requirements.lock pyproject.toml ./ +COPY relay ./relay +RUN pip install --no-index --find-links=/wheels --no-cache-dir -r requirements.lock && rm -rf /wheels +RUN chmod -R a+rX /app +USER 10001:10001 +ENTRYPOINT ["python", "-m", "relay.cli"] diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/docs/H5_PAIR_CHAT.md b/patch-source/0de3db40fc2044b0/hainuo-relay/docs/H5_PAIR_CHAT.md new file mode 100644 index 0000000..7ddcab5 --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/docs/H5_PAIR_CHAT.md @@ -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组合链路,覆盖一次配对码、并发消费、跨站拒绝、两账号隔离、去重、退出与过期、绑定变更失效。 diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/relay/__init__.py b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/__init__.py new file mode 100644 index 0000000..32754a7 --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/__init__.py @@ -0,0 +1 @@ +"""Hainuo relay 0.1.0.""" diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/relay/cli.py b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/cli.py new file mode 100644 index 0000000..4efe80c --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/cli.py @@ -0,0 +1,216 @@ +import argparse +import asyncio +import json +import os +import secrets +import ssl +import uuid +from datetime import datetime, timedelta, timezone +from pathlib import Path + +import httpx +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ed25519 +from cryptography.x509.oid import NameOID + +from .crypto import issue_ca, issue_device, load_private, pem_private + + +def write_new(path, data, private=False): + path.parent.mkdir(parents=True, exist_ok=True) + with path.open("xb") as f: + f.write(data if isinstance(data, bytes) else data.encode()) + if private and os.name != "nt": + path.chmod(0o600) + + +def init_project(root: Path, hostname: str): + if (root / ".env").exists(): + raise RuntimeError(".env exists; init never overwrites credentials") + ca_key, ca = issue_ca() + signer = ed25519.Ed25519PrivateKey.generate() + write_new(root / "operator/ca.key", pem_private(ca_key), True) + write_new(root / "runtime/trust/ca.pem", ca.public_bytes(serialization.Encoding.PEM)) + write_new(root / "runtime/connector/connector.key", pem_private(signer), True) + write_new(root / "runtime/trust/connector.pub", signer.public_key().public_bytes( + serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo)) + tls_key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, hostname)]) + now = datetime.now(timezone.utc) + tls = (x509.CertificateBuilder().subject_name(name).issuer_name(name).public_key(tls_key.public_key()) + .serial_number(x509.random_serial_number()).not_valid_before(now - timedelta(minutes=5)) + .not_valid_after(now + timedelta(days=30)) + .add_extension(x509.SubjectAlternativeName([x509.DNSName(hostname)]), critical=False) + .sign(tls_key, algorithm=None)) + write_new(root / "runtime/tls/server.key", pem_private(tls_key), True) + write_new(root / "runtime/tls/server.pem", tls.public_bytes(serialization.Encoding.PEM)) + redis_secret = secrets.token_hex(32) + db_secret = secrets.token_hex(32) + env = (f"HN_ENVIRONMENT=development\nHN_PUBLIC_HOST={hostname}\nHN_PUBLIC_URL=https://{hostname}\n" + f"HN_ROUTER_URL=wss://{hostname}/device/ws\nHN_ADMIN_SECRET={secrets.token_hex(32)}\n" + f"HN_INTERNAL_SECRET={secrets.token_hex(32)}\nHN_REDIS_PASSWORD={redis_secret}\n" + f"HN_REDIS_URL=redis://:{redis_secret}@redis:6379/0\n" + f"HN_POSTGRES_PASSWORD={db_secret}\nHN_DATABASE_URL=postgresql://hainuo:{db_secret}@postgres:5432/hainuo\n" + "HN_FEISHU_ENABLED=false\nHN_FEISHU_API_BASE=https://open.feishu.cn\n" + "HN_FEISHU_AUTHORIZE_URL=https://open.feishu.cn/open-apis/authen/v1/authorize\n" + "HN_FEISHU_APP_ID=\nHN_FEISHU_APP_SECRET=\nHN_FEISHU_ENCRYPT_KEY=\n" + "HN_FEISHU_VERIFICATION_TOKEN=\nHN_PROXY_ENABLED=false\nHN_DEVICE_DOMAIN=\n") + write_new(root / ".env", env, True) + print("已生成配置、连接器密钥、设备CA及临时TLS证书;未输出凭据。") + print("operator/ca.key 仅保留在签发工作站,不要上传到ECS。临时TLS证书上线前须替换。") + + +def device_request(directory): + host = uuid.uuid4().hex + private = ed25519.Ed25519PrivateKey.generate() + write_new(directory / "device.key", pem_private(private), True) + public = private.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode() + write_new(directory / "request.json", json.dumps({"host_id": host, "public_key": public}, indent=2)) + print("已在本机生成私钥和 request.json;仅将request.json交给签发工作站。") + + +def issue(root, request_file, output): + request = json.loads(request_file.read_text(encoding="utf-8")) + host = request["host_id"] + if len(host) != 32 or any(c not in "0123456789abcdef" for c in host): + raise ValueError("invalid host identity") + ca = x509.load_pem_x509_certificate((root / "runtime/trust/ca.pem").read_bytes()) + public = serialization.load_pem_public_key(request["public_key"].encode()) + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("Ed25519 required") + cert = issue_device(load_private(root / "operator/ca.key"), ca, host, public) + pem = cert.public_bytes(serialization.Encoding.PEM) + write_new(output / "device.pem", pem) + write_new(output / "ca.pem", (root / "runtime/trust/ca.pem").read_bytes()) + write_new(output / "connector.pub", (root / "runtime/trust/connector.pub").read_bytes()) + write_new(output / "device.json", json.dumps({"host_id": host, "owner": None}, indent=2)) + write_new(output / "registration.json", json.dumps({"host_id": host, "cert": pem.decode()}, indent=2)) + print("已签发设备证书;将device.pem、ca.pem、connector.pub、device.json交回原设备。") + + +def main(): + parser = argparse.ArgumentParser(description="海诺中继部署和设备工具") + sub = parser.add_subparsers(dest="command", required=True) + p = sub.add_parser("init") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--hostname", required=True) + p = sub.add_parser("serve") + p.add_argument("role", choices=["connector", "router"]) + p.add_argument("--host", default="127.0.0.1") + p.add_argument("--port", type=int, default=8000) + p = sub.add_parser("device-request") + p.add_argument("--directory", type=Path, required=True) + p = sub.add_parser("issue-device") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--request", type=Path, required=True) + p.add_argument("--output", type=Path, required=True) + p = sub.add_parser("register") + p.add_argument("--registration", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("bind") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("device") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p.add_argument("--test-root", type=Path) + p.add_argument("--agent-url", help="Explicit loopback read-only Agent adapter endpoint") + p.add_argument("--hainuo-config", type=Path, help="Native installer-1.065 API adapter config; alternative to --agent-url") + p = sub.add_parser("hainuo-probe") + p.add_argument("--config", type=Path, required=True) + p = sub.add_parser("web-pair") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + for command in ("status", "binding-code", "dispatch", "revoke", "reconcile"): + p = sub.add_parser(command) + p.add_argument("--url", default="http://127.0.0.1:18081") + p.add_argument("--tls-ca", type=Path) + if command in {"binding-code", "dispatch"}: + p.add_argument("--owner", required=True) + if command == "dispatch": + p.add_argument("--text", required=True) + p.add_argument("--turn-id", default=None) + if command == "revoke": + p.add_argument("--host-id", required=True) + args = parser.parse_args() + if args.command == "init": + init_project(args.root, args.hostname) + elif args.command == "serve": + import uvicorn + uvicorn.run(f"relay.{args.role}:create_app", factory=True, host=args.host, port=args.port, + workers=1, access_log=False, proxy_headers=False, + ws_max_size=262144, ws_max_queue=16, log_level="warning") + elif args.command == "device-request": + device_request(args.directory) + elif args.command == "issue-device": + issue(args.root, args.request, args.output) + elif args.command == "register": + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, follow_redirects=False, timeout=15) as client: + response = client.post(args.url.rstrip("/") + "/admin/devices", + headers={"X-Admin-Secret": secret}, + json=json.loads(args.registration.read_text(encoding="utf-8"))) + if response.status_code != 201: + raise RuntimeError(f"register failed: HTTP {response.status_code}") + print("设备登记成功。") + elif args.command == "bind": + import getpass + from .device import bind_device + code = getpass.getpass("Binding code: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + asyncio.run(bind_device(args.directory, args.url, code, tls_verify=context)) + print("绑定成功。") + elif args.command == "device": + from .device import run_device + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else None + asyncio.run(run_device(args.directory, args.url, root=args.test_root, ssl_context=context, + agent_url=args.agent_url, hainuo_config=args.hainuo_config)) + elif args.command == "hainuo-probe": + from .hainuo1065 import Hainuo1065Agent + async def probe(): + adapter = Hainuo1065Agent(args.config) + try: + print(json.dumps(await adapter.probe(), ensure_ascii=False, indent=2)) + finally: + await adapter.close() + asyncio.run(probe()) + elif args.command == "web-pair": + from .device import pair_browser + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + result = asyncio.run(pair_browser(args.directory, args.url, tls_verify=context)) + print("网页地址:" + result["url"]) + print("一次性网页配对码(120秒有效):" + result["code"]) + else: + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, timeout=200, follow_redirects=False) as client: + paths = {"status": "/admin/devices", "binding-code": "/admin/test/binding-code", + "dispatch": "/admin/test/dispatch", "reconcile": "/admin/reconcile"} + if args.command == "revoke": + if len(args.host_id) != 32 or any(c not in "0123456789abcdef" for c in args.host_id): + raise ValueError("invalid host identity") + path = f"/admin/devices/{args.host_id}/revoke" + else: + path = paths[args.command] + body = None + if args.command in {"binding-code", "dispatch"}: + body = {"open_id": args.owner, "text": getattr(args, "text", "binding")} + if getattr(args, "turn_id", None): + body["turn_id"] = args.turn_id + response = client.request("GET" if args.command == "status" else "POST", args.url.rstrip("/") + path, + headers={"X-Admin-Secret": secret}, json=body) + if response.status_code != 200: + raise RuntimeError(f"operation failed: HTTP {response.status_code}") + print(json.dumps(response.json(), ensure_ascii=False, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/relay/common.py b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/common.py new file mode 100644 index 0000000..c112b99 --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/common.py @@ -0,0 +1,61 @@ +import hashlib +import hmac +import json +import time +from fastapi import HTTPException, Request +from starlette.responses import JSONResponse + + +def require_secret(actual, expected): + if not actual or not hmac.compare_digest(actual, expected): + raise HTTPException(401, "unauthorized") + + +async def rate_limit(redis, bucket: str, limit=60, seconds=60): + # Atomic counter + expiry: a crash cannot leave a permanent bucket. + key = "hn:rate:" + hashlib.sha256(bucket.encode()).hexdigest() + ":" + str(int(time.time()) // seconds) + pipe = redis.pipeline(transaction=True) + pipe.incr(key) + pipe.expire(key, seconds * 2) + result = (await pipe.execute())[0] + if int(result) > limit: + raise HTTPException(429, "rate limited", headers={"Retry-After": str(seconds)}) + + +class BodyLimitMiddleware: + def __init__(self, app, max_body=262144): + self.app, self.max_body = app, max_body + + async def __call__(self, scope, receive, send): + if scope["type"] != "http": + return await self.app(scope, receive, send) + messages, size = [], 0 + while True: + message = await receive() + if message["type"] == "http.disconnect": + return + size += len(message.get("body", b"")) + if size > self.max_body: + return await JSONResponse({"detail": "body too large"}, status_code=413)(scope, receive, send) + messages.append(message) + if not message.get("more_body", False): + break + async def buffered(): + return messages.pop(0) if messages else await receive() + await self.app(scope, buffered, send) + + +def no_cache(response): + response.headers["Cache-Control"] = "no-store" + response.headers["X-Content-Type-Options"] = "nosniff" + response.headers["Referrer-Policy"] = "no-referrer" + return response + + +async def json_pub(redis, channel, data): + return await redis.publish(channel, json.dumps(data, separators=(",", ":"))) + + +def peer(request: Request): + # Do not trust arbitrary X-Forwarded-For; configure uvicorn trusted proxies explicitly. + return request.client.host if request.client else "unknown" diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/relay/config.py b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/config.py new file mode 100644 index 0000000..6aa0620 --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/config.py @@ -0,0 +1,60 @@ +from pathlib import Path +from pydantic import model_validator +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + model_config = SettingsConfigDict(env_prefix="HN_", extra="ignore", hide_input_in_errors=True) + environment: str = "production" + role: str = "connector" + redis_url: str = "redis://127.0.0.1:6379/0" + data_dir: Path = Path("runtime/connector") + database_url: str = "" + admin_secret: str = "" + internal_secret: str = "" + ca_cert: Path = Path("runtime/pki/ca.pem") + connector_key: Path = Path("runtime/pki/connector.key") + connector_public_key: Path = Path("runtime/pki/connector.pub") + public_url: str = "https://relay.example.invalid" + router_url: str = "wss://relay.example.invalid/device/ws" + device_domain: str = "" + feishu_api_base: str = "https://open.feishu.cn" + feishu_authorize_url: str = "https://open.feishu.cn/open-apis/authen/v1/authorize" + feishu_app_id: str = "" + feishu_app_secret: str = "" + feishu_encrypt_key: str = "" + feishu_verification_token: str = "" + feishu_enabled: bool = False + proxy_enabled: bool = False + # A fixed allowlist, never a user supplied upstream URL. + proxy_paths: str = "/open-apis/drive/v1/files,/open-apis/docx/v1/documents" + request_timeout: int = 45 + max_body: int = 262144 + max_pending: int = 1000 + max_devices: int = 1000 + max_connections: int = 1000 + max_result: int = 16000 + allow_insecure_local: bool = False + + @model_validator(mode="after") + def validate_runtime(self): + if self.environment not in {"production", "test", "development"}: + raise ValueError("invalid environment") + if self.role == "connector" and min(len(self.admin_secret), len(self.internal_secret)) < 32: + raise ValueError("admin/internal secrets must each be at least 32 characters") + if self.role == "longconn" and len(self.internal_secret) < 32: + raise ValueError("internal credential must be at least 32 characters") + if self.role == "connector" and self.admin_secret == self.internal_secret: + raise ValueError("admin and internal credentials must be different") + if self.environment == "production": + if self.allow_insecure_local or not self.public_url.startswith("https://"): + raise ValueError("production requires HTTPS") + if not self.router_url.startswith("wss://"): + raise ValueError("production requires WSS") + if not self.feishu_api_base.startswith("https://"): + raise ValueError("platform API requires HTTPS") + if self.feishu_enabled and not all((self.feishu_app_id, self.feishu_app_secret, + self.feishu_encrypt_key, + self.feishu_verification_token)): + raise ValueError("complete platform credentials are required") + return self diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/relay/connector.py b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/connector.py new file mode 100644 index 0000000..0f106e9 --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/connector.py @@ -0,0 +1,478 @@ +import asyncio +import base64 +import contextlib +import hashlib +import hmac +import json +import re +import secrets +import sqlite3 +import time +import uuid +from contextlib import asynccontextmanager +from dataclasses import dataclass +from urllib.parse import unquote, urlsplit + +from cryptography.hazmat.primitives import hashes, padding +from cryptography.hazmat.primitives.asymmetric import x25519 +from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes +from fastapi import FastAPI, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from fastapi.exceptions import RequestValidationError +from pydantic import BaseModel, Field +from redis.asyncio import Redis + +from .common import BodyLimitMiddleware, no_cache, peer, rate_limit, require_secret +from .config import Settings +from .crypto import (fingerprint, load_private, open_envelope, raw_public, seal, + sign, verified, verify_device) +from .oauth import OAuth +from .platform import Feishu, PlatformError +from .store import Store, INTEGRITY_ERRORS + + +class Register(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + cert: str = Field(max_length=10000) + + +class Bind(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Instruction(BaseModel): + open_id: str = Field(min_length=1, max_length=128) + text: str = Field(min_length=1, max_length=8000) + # Stable identity chosen by upstream, not generated again on retries. + turn_id: str = Field(default_factory=lambda: uuid.uuid4().hex, pattern=r"^[A-Za-z0-9_-]{1,128}$") + + +@dataclass +class Pending: + future: asyncio.Future + private: object + public: object + host: str + session: str + + +class Broker: + def __init__(self, settings, redis, store): + self.s, self.r, self.store = settings, redis, store + self.signer = load_private(settings.connector_key) + self.pending: dict[str, Pending] = {} + + async def replies(self, pub): + async for message in pub.listen(): + if message["type"] != "message": + continue + try: + data = json.loads(message["data"]) + item = self.pending.get(data["turn_id"]) + if not item or item.future.done(): + continue + plain = open_envelope(data["envelope"], item.private, item.public, + host_id=item.host, session_id=item.session) + if plain["turn_id"] != data["turn_id"] or plain.get("type") != "result": + continue + result = plain.get("result", "") + if not isinstance(result, str) or len(result) > self.s.max_result: + continue + item.future.set_result({"status": "completed", "turn_id": data["turn_id"], "result": result}) + except Exception: + # Invalid packets never consume a pending result; do not log packet contents. + continue + + async def dispatch(self, owner, text, turn): + device = self.store.by_owner(owner) + if not device: + return {"status": "unavailable", "turn_id": turn, "result": "未绑定设备或设备不可用。"} + host = device["host_id"] + await rate_limit(self.r, "dispatch:" + owner, 30) + if len(self.pending) >= self.s.max_pending: + raise HTTPException(503, "too many in-flight requests") + presence = await self.r.get("hn:online:" + host) + if not presence or await self.r.exists("hn:revoked:" + host): + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线,请开机并连接网络。"} + try: + presence = json.loads(presence) + if fingerprint(presence["cert"]) != device["fingerprint"]: + raise ValueError("certificate changed") + public = verify_device(presence["cert"], self.s.ca_cert.read_bytes(), host) + proof = verified(presence["proof"], public) + if proof["host_id"] != host or proof["challenge"] != presence["challenge"]: + raise ValueError("invalid device session") + session = proof["session_id"] + except Exception: + self.store.audit(owner, "dispatch", host, "identity_rejected") + raise HTTPException(409, "device identity rejected") from None + if not self.store.begin_turn(turn, owner, host): + return {"status": "duplicate", "turn_id": turn, "result": "该请求已接收,不会重复执行。"} + private = x25519.X25519PrivateKey.generate() + future = asyncio.get_running_loop().create_future() + self.pending[turn] = Pending(future, private, public, host, session) + envelope = seal({"type": "instruction", "turn_id": turn, "owner": owner, + "text": text, "read_only": True, "channel": "im", + "reply_key": raw_public(private.public_key())}, proof["kx"], self.signer, + host_id=host, session_id=session) + key = f"hn:inflight:{host}:{turn}" + try: + await self.r.set(key, session, ex=self.s.request_timeout + 5) + count = await self.r.publish("hn:down:" + host, json.dumps({"type": "instruction", "envelope": envelope})) + if not count: + self.store.finish(turn, "offline") + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线。"} + result = await asyncio.wait_for(future, timeout=self.s.request_timeout) + self.store.finish(turn, "completed") + self.store.audit(owner, "instruction", host, "completed") + return result + except asyncio.TimeoutError: + self.store.finish(turn, "unknown") + return {"status": "unknown", "turn_id": turn, + "result": "等待结果超时,执行状态未知;不要直接重发有副作用的任务。"} + finally: + self.pending.pop(turn, None) + await self.r.delete(key) + + +def create_app(settings=None, redis=None, platform=None): + s = settings or Settings() + r = redis or Redis.from_url(s.redis_url, decode_responses=True) + store = Store(s.data_dir / "metadata.sqlite", s.database_url) + f = platform or Feishu(s) + broker = Broker(s, r, store) + oauth = OAuth(s, r, f) + background = set() + + def spawn(coro): + task = asyncio.create_task(coro) + background.add(task) + task.add_done_callback(background.discard) + # Retrieve errors without displaying possibly sensitive exception text. + task.add_done_callback(lambda t: t.exception() if not t.cancelled() else None) + + @asynccontextmanager + async def lifespan(app): + await r.ping() + for row in store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall(): + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + pub = r.pubsub() + await pub.subscribe("hn:replies") + reply_task = asyncio.create_task(broker.replies(pub)) + app.state.reply_task = reply_task + async def sweep(): + while True: + await asyncio.sleep(30) + async with oauth.lock: + oauth.prune() + sweep_task = asyncio.create_task(sweep()) + try: + yield + finally: + for task in [reply_task, sweep_task, *background]: + task.cancel() + await asyncio.gather(reply_task, sweep_task, *background, return_exceptions=True) + await pub.aclose() + await f.close() + store.close() + if redis is None: + await r.aclose() + + app = FastAPI(title="Hainuo B1 connector", lifespan=lifespan, docs_url=None, redoc_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=s.max_body) + app.include_router(oauth.router) + from .web_session import WebSessions + web_sessions = WebSessions(s, r, store, broker, f) + app.include_router(web_sessions.router) + app.state.store, app.state.broker, app.state.oauth = store, broker, oauth + app.state.platform, app.state.redis = f, r + + @app.exception_handler(RequestValidationError) + async def validation_error(request, exc): + # FastAPI's default error response includes invalid input values (possibly tokens). + return JSONResponse({"detail": "invalid request parameters"}, status_code=422) + + @app.middleware("http") + async def secure_response(request, call_next): + return no_cache(await call_next(request)) + + def admin(request): + require_secret(request.headers.get("X-Admin-Secret"), s.admin_secret) + + @app.get("/health") + async def health(): + return {"status": "alive", "role": "connector", "version": "0.1.0"} + + @app.get("/ready") + async def ready(): + await r.ping() + store.db.execute("SELECT 1") + if not getattr(app.state, "reply_task", None) or app.state.reply_task.done(): + raise HTTPException(503, "reply subscriber unavailable") + return {"status": "ready"} + + @app.post("/admin/devices", status_code=201) + async def register(body: Register, request: Request): + admin(request) + try: + verify_device(body.cert, s.ca_cert.read_bytes(), body.host_id) + except Exception: + raise HTTPException(400, "invalid device certificate") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= s.max_devices: + raise HTTPException(503, "device capacity reached") + try: + fp = fingerprint(body.cert) + store.add_device(body.host_id, body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "device already exists") from None + await r.set("hn:device:" + body.host_id, fp) + store.audit("admin", "register", body.host_id, "success") + return {"host_id": body.host_id, "fingerprint": fp} + + @app.get("/admin/devices") + async def devices(request: Request): + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,owner,revoked FROM devices ORDER BY host_id").fetchall() + return {"devices": [{**dict(row), "online": bool(await r.exists("hn:online:" + row["host_id"]))} for row in rows]} + + @app.post("/admin/devices/{host}/revoke") + async def revoke(host: str, request: Request): + admin(request) + if not store.device(host): + raise HTTPException(404, "device not found") + # Persistent source of truth first; router block + disconnect follows. + store.revoke(host) + await r.incr("hn:web-generation:" + host) + await r.set("hn:revoked:" + host, "1") + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "revoke") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "revoked", "result": "设备已停用,结果已丢弃。"}) + store.audit("admin", "revoke", host, "success") + return {"status": "revoked"} + + @app.post("/admin/reconcile") + async def reconcile(request: Request): + """Rebuild router admission metadata after Redis recovery; no dialogue data.""" + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall() + for row in rows: + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + return {"devices": len(rows)} + + async def make_binding_code(owner): + if store.by_owner(owner): + return "你已绑定电脑,请先发送 /解绑。" + await rate_limit(r, "binding:" + owner, 5) + code = secrets.token_urlsafe(12) + await r.set("hn:bind:" + hashlib.sha256(code.encode()).hexdigest(), owner, ex=300) + return "请在本人电脑的桥接客户端输入绑定码(5分钟有效):" + code + + @app.post("/device/bind") + async def bind(body: Bind, request: Request): + await rate_limit(r, "bind:" + peer(request), 20) + device = store.device(body.host_id) + if not device or device["revoked"]: + raise HTTPException(404, "device unavailable") + try: + public = verify_device(device["cert"], s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, public) + if proof["host_id"] != body.host_id or abs(time.time() - proof["ts"]) > 90: + raise ValueError("invalid proof") + code = proof["code"] + if not isinstance(code, str) or len(code) > 128: + raise ValueError("invalid code") + except Exception: + raise HTTPException(403, "invalid device proof") from None + key = "hn:bind:" + hashlib.sha256(code.encode()).hexdigest() + owner = await r.get(key) + if not owner: + raise HTTPException(404, "binding code expired or consumed") + # GETDEL is atomic across connectors. Invalid device proofs never reach this point. + owner = await r.getdel(key) + if not owner: + raise HTTPException(404, "binding code consumed") + try: + store.bind(body.host_id, owner) + except (*INTEGRITY_ERRORS, ValueError): + raise HTTPException(409, "owner/device already bound; request new code") from None + await r.incr("hn:web-generation:" + body.host_id) + store.audit(owner, "bind", body.host_id, "success") + return sign({"host_id": body.host_id, "owner": owner, "ts": int(time.time())}, broker.signer) + + @app.post("/admin/test/binding-code") + async def test_code(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return {"message": await make_binding_code(body.open_id)} + + @app.post("/admin/test/dispatch") + async def test_dispatch(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return await broker.dispatch(body.open_id, body.text, body.turn_id) + + async def handle_event(data): + header, event = data.get("header", {}), data.get("event", {}) + if header.get("event_type") != "im.message.receive_v1": + return + message, sender = event.get("message", {}), event.get("sender", {}) + owner = sender.get("sender_id", {}).get("open_id") + message_id = message.get("message_id", "") + if (message.get("chat_type") != "p2p" or message.get("message_type") != "text" + or sender.get("sender_type") != "user" or not owner + or not re.fullmatch(r"[A-Za-z0-9_-]{1,128}", message_id)): + return + if header.get("app_id") != s.feishu_app_id: + return + try: + text = json.loads(message["content"])["text"].strip() + if not text or len(text) > 8000: + return + # Persistent receipt BEFORE any side effect. Restarts do not execute a delivery twice. + event_turn = "event_" + hashlib.sha256(message_id.encode()).hexdigest() + if not store.begin_turn(event_turn, owner, "event"): + return + if text == "/绑定": + result = await make_binding_code(owner) + elif text == "/解绑": + old = store.by_owner(owner) + store.unbind(owner) + if old: + await r.incr("hn:web-generation:" + old["host_id"]) + await r.publish("hn:kill:" + old["host_id"], "unbind") + for item in broker.pending.values(): + if item.host == old["host_id"] and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + result = "已解绑;重新绑定后请更新本机绑定信息。" + else: + result = (await broker.dispatch(owner, text, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + await f.reply(message_id, result[:s.max_result]) + store.finish(event_turn, "replied") + except Exception: + store.audit(owner, "event", message_id, "failed_or_unknown") + + @app.post("/internal/events") + async def internal_event(request: Request): + require_secret(request.headers.get("X-Internal-Secret"), s.internal_secret) + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(await request.json())) + return {"accepted": True} + + @app.post("/feishu/event") + async def webhook(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(r, "webhook:" + peer(request), 600) + raw = await request.body() + timestamp, nonce, signature = (request.headers.get(x, "") for x in + ("X-Lark-Request-Timestamp", "X-Lark-Request-Nonce", "X-Lark-Signature")) + try: + if abs(time.time() - int(timestamp)) > 300 or not nonce or len(nonce) > 128: + raise ValueError("stale request") + expected = hashlib.sha256((timestamp + nonce + s.feishu_encrypt_key).encode() + raw).hexdigest() + if not hmac.compare_digest(expected, signature): + raise ValueError("bad signature") + data = json.loads(raw) + if "encrypt" in data: + cipher = base64.b64decode(data["encrypt"], validate=True) + decryptor = Cipher(algorithms.AES(hashlib.sha256(s.feishu_encrypt_key.encode()).digest()), + modes.CBC(cipher[:16])).decryptor() + padded = decryptor.update(cipher[16:]) + decryptor.finalize() + unpadder = padding.PKCS7(128).unpadder() + data = json.loads(unpadder.update(padded) + unpadder.finalize()) + token = data.get("token", data.get("header", {}).get("token", "")) + if not hmac.compare_digest(token, s.feishu_verification_token): + raise ValueError("invalid verification token") + except Exception: + raise HTTPException(403, "invalid event authentication") from None + if data.get("type") == "url_verification": + return {"challenge": data.get("challenge", "")} + # Same delivery is ACKed again; durable message_id receipt handles execution deduplication. + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(data)) + return {"code": 0} + + @app.get("/api/whoami") + async def whoami(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(r, "whoami:" + peer(request)) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await f.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "no device bound") + if not s.device_domain or not re.fullmatch(r"[a-zA-Z0-9.-]+", s.device_domain): + raise HTTPException(503, "device web domain not configured") + return {"host_id": device["host_id"], "url": f"https://h-{device['host_id']}.{s.device_domain}/", + "online": bool(await r.exists("hn:online:" + device["host_id"])), + "device_login_required": True} + + @app.get("/h5", response_class=HTMLResponse) + async def h5(): + from importlib.resources import files + return HTMLResponse(files("relay").joinpath("web/index.html").read_text(encoding="utf-8"), + headers={"Content-Security-Policy": "default-src 'self'; script-src 'self'; style-src 'self'; frame-ancestors 'self'; base-uri 'none'; form-action 'self'"}) + + @app.get("/assets/{name}") + async def asset(name: str): + from importlib.resources import files + from starlette.responses import Response + if name not in {"app.js", "style.css"}: + raise HTTPException(404, "not found") + return Response(files("relay").joinpath("web/" + name).read_bytes(), + media_type="text/javascript" if name.endswith(".js") else "text/css") + + @app.get("/proxy") + async def proxy(request: Request): + if not s.proxy_enabled or not s.feishu_enabled: + raise HTTPException(404, "not found") + auth = request.headers.get("Authorization", "") + target = request.headers.get("X-Relay-Target", "") + decoded = unquote(target) + parts = urlsplit(target) + allowed = s.proxy_paths.split(",") + if (not auth.startswith("Bearer ") or len(auth) > 8192 or len(target) > 4096 + or parts.scheme or parts.netloc or parts.fragment or target.startswith("//") + or any(x in decoded for x in ("..", "\\", "\r", "\n", "#", "%")) + or not any(parts.path == x or parts.path.startswith(x + "/") for x in allowed)): + raise HTTPException(403, "target not allowed") + try: + info = await f.user_info(auth[7:]) + await rate_limit(r, "proxy:" + info["open_id"], 60) + # Buffer bounded response; no disk/cache, redirects never followed. + async with f.http.stream("GET", target, headers={"Authorization": auth}) as response: + if response.status_code in {301, 302, 303, 307, 308}: + raise HTTPException(502, "upstream redirect refused") + chunks, size = [], 0 + async for chunk in response.aiter_bytes(): + size += len(chunk) + if size > 2 * 1024 * 1024: + raise HTTPException(502, "response too large") + chunks.append(chunk) + from starlette.responses import Response + store.audit(info["open_id"], "proxy", parts.path, str(response.status_code)) + return Response(b"".join(chunks), status_code=response.status_code, + media_type=response.headers.get("content-type", "application/json")) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + + return app diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/relay/crypto.py b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/crypto.py new file mode 100644 index 0000000..e4d7afd --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/crypto.py @@ -0,0 +1,134 @@ +"""Signed ephemeral X25519 envelopes. The router never receives plaintext keys.""" +import base64 +import hashlib +import json +import secrets +import time +from datetime import datetime, timedelta, timezone + +from cryptography import x509 +from cryptography.hazmat.primitives import hashes, serialization +from cryptography.hazmat.primitives.asymmetric import ed25519, x25519 +from cryptography.hazmat.primitives.ciphers.aead import AESGCM +from cryptography.hazmat.primitives.kdf.hkdf import HKDF +from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID + + +def b64(data: bytes) -> str: + return base64.urlsafe_b64encode(data).decode() + + +def unb64(data: str) -> bytes: + return base64.b64decode(data.encode(), altchars=b"-_", validate=True) + + +def canonical(data: dict) -> bytes: + return json.dumps(data, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode() + + +def raw_public(key) -> str: + return b64(key.public_bytes(serialization.Encoding.Raw, serialization.PublicFormat.Raw)) + + +def pem_private(key) -> bytes: + return key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, + serialization.NoEncryption()) + + +def load_private(path): + return serialization.load_pem_private_key(path.read_bytes(), password=None) + + +def load_public(path): + return serialization.load_pem_public_key(path.read_bytes()) + + +def fingerprint(pem: str) -> str: + return x509.load_pem_x509_certificate(pem.encode()).fingerprint(hashes.SHA256()).hex() + + +def verify_device(pem: str, ca_pem: bytes, host_id: str): + """v1 deliberately accepts only directly issued Ed25519 device certificates.""" + cert = x509.load_pem_x509_certificate(pem.encode()) + ca = x509.load_pem_x509_certificate(ca_pem) + now = datetime.now(timezone.utc) + if not (ca.not_valid_before_utc <= now <= ca.not_valid_after_utc): + raise ValueError("expired CA") + if not ca.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("not a CA") + cert.verify_directly_issued_by(ca) + if not (cert.not_valid_before_utc <= now <= cert.not_valid_after_utc): + raise ValueError("expired certificate") + if cert.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("CA cannot be used as device") + if ExtendedKeyUsageOID.CLIENT_AUTH not in cert.extensions.get_extension_for_class(x509.ExtendedKeyUsage).value: + raise ValueError("clientAuth missing") + names = cert.extensions.get_extension_for_class(x509.SubjectAlternativeName).value + if names.get_values_for_type(x509.UniformResourceIdentifier) != [f"urn:hainuo:host:{host_id}"]: + raise ValueError("host identity mismatch") + public = cert.public_key() + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("v1 requires Ed25519 device key") + return public + + +def sign(data: dict, key) -> dict: + return {**data, "signature": b64(key.sign(canonical(data)))} + + +def verified(data: dict, key) -> dict: + body = {k: v for k, v in data.items() if k != "signature"} + key.verify(unb64(data["signature"]), canonical(body)) + return body + + +def seal(payload: dict, recipient_public: str, signer, *, host_id: str, session_id: str) -> dict: + ephemeral = x25519.X25519PrivateKey.generate() + header = {"v": 1, "host_id": host_id, "session_id": session_id, + "ts": int(time.time()), "nonce": b64(secrets.token_bytes(12)), + "ephemeral": raw_public(ephemeral.public_key())} + shared = ephemeral.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(recipient_public))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + cipher = AESGCM(key).encrypt(unb64(header["nonce"]), canonical(payload), canonical(header)) + return sign({**header, "ciphertext": b64(cipher)}, signer) + + +def open_envelope(envelope: dict, recipient_private, sender_public, *, host_id: str, session_id: str) -> dict: + body = verified(envelope, sender_public) + if body["v"] != 1 or body["host_id"] != host_id or body["session_id"] != session_id: + raise ValueError("wrong destination/session") + if abs(time.time() - body["ts"]) > 90: + raise ValueError("expired envelope") + header = {k: v for k, v in body.items() if k != "ciphertext"} + shared = recipient_private.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(body["ephemeral"]))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + plain = AESGCM(key).decrypt(unb64(body["nonce"]), unb64(body["ciphertext"]), canonical(header)) + return json.loads(plain) + + +def issue_ca(): + key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Hainuo device test CA")]) + now = datetime.now(timezone.utc) + cert = (x509.CertificateBuilder().subject_name(name).issuer_name(name) + .public_key(key.public_key()).serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=3650)) + .add_extension(x509.BasicConstraints(ca=True, path_length=0), critical=True) + .add_extension(x509.KeyUsage(False, False, False, False, False, True, True, False, False), critical=True) + .sign(key, algorithm=None)) + return key, cert + + +def issue_device(ca_key, ca_cert, host_id: str, public_key, days=30): + now = datetime.now(timezone.utc) + return (x509.CertificateBuilder() + .subject_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, host_id)])) + .issuer_name(ca_cert.subject).public_key(public_key) + .serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=days)) + .add_extension(x509.BasicConstraints(ca=False, path_length=None), critical=True) + .add_extension(x509.SubjectAlternativeName([x509.UniformResourceIdentifier(f"urn:hainuo:host:{host_id}")]), critical=False) + .add_extension(x509.ExtendedKeyUsage([ExtendedKeyUsageOID.CLIENT_AUTH]), critical=False) + .sign(ca_key, algorithm=None)) diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/relay/device.py b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/device.py new file mode 100644 index 0000000..31f9875 --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/device.py @@ -0,0 +1,206 @@ +"""Reference PC bridge. No shell/PowerShell execution; local read-only diagnostics.""" +import asyncio +import contextlib +import json +import os +import secrets +import socket +import sqlite3 +import time +from pathlib import Path +from urllib.parse import urlsplit + +import httpx +from cryptography.hazmat.primitives.asymmetric import x25519 +from websockets.asyncio.client import connect + +from .crypto import (load_private, load_public, open_envelope, raw_public, seal, sign, + verified, verify_device) + + +class DeviceJournal: + def __init__(self, path): + path.parent.mkdir(parents=True, exist_ok=True) + self.db = sqlite3.connect(path) + self.db.execute("CREATE TABLE IF NOT EXISTS consumed(turn_id TEXT PRIMARY KEY, ts REAL NOT NULL)") + self.db.commit() + + def consume(self, turn): + with self.db: + return self.db.execute("INSERT OR IGNORE INTO consumed VALUES(?,?)", (turn, time.time())).rowcount == 1 + + def close(self): + self.db.close() + + +async def execute_readonly(text, root: Path | None): + if text in {"/主机名", "hostname"}: + return socket.gethostname() + if text.startswith("/回显 "): + return text[4:][:16000] + if text.startswith("/读取 "): + if root is None: + return "未配置测试文件目录。" + base = root.resolve(strict=True) + try: + path = (base / text[4:].strip()).resolve(strict=True) + if path == base or not path.is_relative_to(base) or not path.is_file(): + return "拒绝:文件不在允许的测试目录。" + if path.stat().st_size > 32768: + return "拒绝:测试文件超过32KB。" + return path.read_text(encoding="utf-8")[:16000] + except (OSError, UnicodeError, ValueError): + return "拒绝:文件不存在、不可读或不是UTF-8文本。" + return "当前桥接客户端仅开放只读诊断:/主机名、/回显 内容、/读取 相对路径;尚未接入海诺本地Agent接口。" + + +class LocalAgent: + """Explicit adapter contract, not a claim of compatibility with installer 1.065.""" + def __init__(self, url, transport=None): + parts = urlsplit(url) + if parts.scheme != "http" or parts.hostname not in {"127.0.0.1", "::1"} or parts.username or parts.password or parts.fragment: + raise ValueError("local Agent must use a fixed loopback HTTP endpoint") + self.url = url + self.http = httpx.AsyncClient(timeout=30, follow_redirects=False, transport=transport, trust_env=False) + + async def execute(self, text, owner, turn): + headers = {} + if os.environ.get("HN_LOCAL_AGENT_TOKEN"): + headers["Authorization"] = "Bearer " + os.environ["HN_LOCAL_AGENT_TOKEN"] + try: + async with self.http.stream("POST", self.url, headers=headers, + json={"text": text, "open_id": owner, "turn_id": turn, + "channel": "im", "read_only": True}) as response: + if response.status_code != 200: + return "本机Agent未返回成功结果,请查看本机服务状态。" + body = b"" + async for chunk in response.aiter_bytes(): + body += chunk + if len(body) > 65536: + return "本机Agent结果超过限制。" + result = json.loads(body)["result"] + if not isinstance(result, str): + return "本机Agent响应格式不匹配。" + return result[:16000] + except (httpx.HTTPError, ValueError, KeyError): + return "本机Agent连接失败或响应格式不匹配。" + + async def close(self): + await self.http.aclose() + + +async def bind_device(directory: Path, connector_url: str, code: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + private = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "code": code, "ts": int(time.time())}, private) + async with httpx.AsyncClient(verify=tls_verify, follow_redirects=False, timeout=15) as http: + response = await http.post(connector_url.rstrip("/") + "/device/bind", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"binding failed: HTTP {response.status_code}") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt["host_id"] != config["host_id"]: + raise ValueError("wrong binding receipt") + config["owner"] = receipt["owner"] + temp = directory / "device.json.tmp" + temp.write_text(json.dumps(config, ensure_ascii=False, indent=2), encoding="utf-8") + temp.replace(directory / "device.json") + + +async def pair_browser(directory: Path, connector_url: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8-sig")) + if not config.get("owner"): + raise RuntimeError("device must be bound first") + key = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "owner": config["owner"], + "purpose": "h5-pair-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as client: + response = await client.post(connector_url.rstrip("/") + "/device/web-pair", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"browser pairing failed: HTTP {response.status_code}") + result = verified(response.json(), load_public(directory / "connector.pub")) + if result["host_id"] != config["host_id"]: + raise ValueError("wrong pairing receipt") + return result + + +async def run_device(directory: Path, router_url: str, *, root=None, once=False, ssl_context=None, + stop_event=None, connected_event=None, agent_url=None, hainuo_config=None): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + if not config.get("owner"): + raise RuntimeError("bind device before starting") + host, owner = config["host_id"], config["owner"] + key = load_private(directory / "device.key") + connector = load_public(directory / "connector.pub") + cert = (directory / "device.pem").read_text(encoding="utf-8") + verify_device(cert, (directory / "ca.pem").read_bytes(), host) + journal = DeviceJournal(directory / "journal.sqlite") + if agent_url and hainuo_config: + raise ValueError("choose one Agent adapter") + if hainuo_config: + from .hainuo1065 import Hainuo1065Agent + agent = Hainuo1065Agent(hainuo_config) + else: + agent = LocalAgent(agent_url) if agent_url else None + stop_event = stop_event or asyncio.Event() + delay = 1 + try: + while not stop_event.is_set(): + try: + kwargs = {"ssl": ssl_context} if router_url.startswith("wss://") and ssl_context else {} + async with connect(router_url, max_size=262144, open_timeout=10, ping_interval=10, + proxy=None, **kwargs) as ws: + challenge = json.loads(await ws.recv())["challenge"] + ephemeral = x25519.X25519PrivateKey.generate() + session = secrets.token_hex(16) + proof = sign({"host_id": host, "session_id": session, "challenge": challenge, + "kx": raw_public(ephemeral.public_key()), "ts": int(time.time())}, key) + await ws.send(json.dumps({"host_id": host, "cert": cert, "proof": proof})) + accepted = json.loads(await ws.recv()) + if accepted.get("type") != "connected": + raise RuntimeError("device connection rejected") + if connected_event: + connected_event.set() + delay = 1 + while not stop_event.is_set(): + try: + raw = await asyncio.wait_for(ws.recv(), timeout=1) + except asyncio.TimeoutError: + continue + data = json.loads(raw) + payload = open_envelope(data["envelope"], ephemeral, connector, + host_id=host, session_id=session) + if payload.get("owner") != owner or payload.get("type") != "instruction": + continue + turn = payload["turn_id"] + if not isinstance(turn, str) or len(turn) > 128: + continue + if not journal.consume(turn): + result = "该请求已处理,拒绝重复执行。" + elif payload.get("read_only") is not True or payload.get("channel") != "im": + result = "拒绝:通道或操作类型不受支持。" + else: + if agent and payload["text"].startswith("/助手 "): + result = await agent.execute(payload["text"][4:], owner, turn) + else: + result = await execute_readonly(payload["text"], root) + reply = seal({"type": "result", "turn_id": turn, "result": result}, + payload["reply_key"], key, host_id=host, session_id=session) + await ws.send(json.dumps({"type": "reply", "turn_id": turn, "envelope": reply})) + if once: + return + except asyncio.CancelledError: + raise + except Exception: + if once: + raise + # No payloads/URLs/credentials in logs, bounded reconnect backoff + jitter. + print("设备连接中断,等待重连。", flush=True) + with contextlib.suppress(asyncio.TimeoutError): + await asyncio.wait_for(stop_event.wait(), timeout=delay + secrets.randbelow(1000) / 1000) + delay = min(delay * 2, 30) + finally: + journal.close() + if agent: + await agent.close() diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/relay/hainuo1065.py b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/hainuo1065.py new file mode 100644 index 0000000..b6a0f6b --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/hainuo1065.py @@ -0,0 +1,105 @@ +"""Adapter for the native API actually found in installer 1.065. + +Loopback only, independent API key, no platform secrets, no redirects or retries. +The included native launcher offers only text inference with no tools. +""" +import hashlib +import json +import re +from pathlib import Path +from urllib.parse import urlsplit + +import httpx + + +class NativeAPIError(RuntimeError): + """Contains a sanitized status only, never response body or credentials.""" + + +def read_native_config(path): + path = Path(path) + data = json.loads(path.read_text(encoding="utf-8-sig")) + url = data.get("url", "") + parts = urlsplit(url) + if (parts.scheme != "http" or parts.hostname != "127.0.0.1" or not parts.port + or parts.username or parts.password or parts.query or parts.fragment + or parts.path not in {"", "/"}): + raise ValueError("native API must be a fixed IPv4 loopback HTTP base URL") + if not isinstance(data.get("api_key"), str) or len(data["api_key"]) < 32: + raise ValueError("native API requires a dedicated key of at least 32 characters") + if data.get("mode") != "text-only": + raise ValueError("only verified text-only mode is supported") + data["url"] = url.rstrip("/") + return data + + +class Hainuo1065Agent: + def __init__(self, config_path, transport=None): + self.config = read_native_config(config_path) + self.http = httpx.AsyncClient(base_url=self.config["url"], timeout=120, + follow_redirects=False, trust_env=False, transport=transport) + self.headers = {"Authorization": "Bearer " + self.config["api_key"]} + + async def _json(self, method, path, **kwargs): + try: + async with self.http.stream(method, path, headers={**self.headers, **kwargs.pop("headers", {})}, **kwargs) as response: + if response.status_code != 200: + raise NativeAPIError(f"native API HTTP {response.status_code}") + body = bytearray() + async for chunk in response.aiter_bytes(): + body.extend(chunk) + if len(body) > 262144: + raise NativeAPIError("native API response exceeds limit") + data = json.loads(body) + if not isinstance(data, dict): + raise NativeAPIError("native API response is not an object") + return data + except (httpx.HTTPError, ValueError): + raise NativeAPIError("native API connection/JSON failure") from None + + async def probe(self): + health = await self._json("GET", "/health") + if health.get("platform") != "hermes-agent" or health.get("status") != "ok": + raise NativeAPIError("endpoint is not the native Hermes API") + capabilities = await self._json("GET", "/v1/capabilities") + policy = capabilities.get("hainuo_relay_policy", {}) + # A stock unrestricted API endpoint is deliberately not sufficient. + if policy != {"mode": "text-only", "tools": "disabled", "contract": "hainuo-1065-v1"}: + raise NativeAPIError("native API policy missing; start the supplied restricted launcher") + models = await self._json("GET", "/v1/models") + model = self.config.get("model") + available = models.get("data") + if not isinstance(available, list) or not available or not all(isinstance(x, dict) and isinstance(x.get("id"), str) for x in available): + raise NativeAPIError("native model list invalid") + if model and model not in {x["id"] for x in available}: + raise NativeAPIError("configured model alias unavailable") + self.model = model or available[0]["id"] + return {"status": "ready", "platform": "hermes-agent", "model": self.model, + "mode": "text-only", "version": health.get("version", "unknown")} + + async def execute(self, text, owner, turn): + try: + # Recheck the actual local service before each inference, including reconnects. + await self.probe() + digest = hashlib.sha256((owner + "\0" + turn).encode()).hexdigest() + data = await self._json("POST", "/v1/chat/completions", + headers={"Idempotency-Key": "hn-" + digest}, + json={"model": self.model, "stream": False, + "messages": [{"role": "user", "content": text}]}) + choices = data.get("choices") + if not isinstance(choices, list) or not choices or not isinstance(choices[0], dict): + raise NativeAPIError("native completion choices missing") + choice = choices[0] + message = choice.get("message", {}) + if (choice.get("finish_reason") != "stop" or message.get("tool_calls") + or not isinstance(message.get("content"), str) + or data.get("hermes", {}).get("failed") + or data.get("hermes", {}).get("partial")): + raise NativeAPIError("native completion incomplete or unexpected tool call") + return message["content"][:16000] + except NativeAPIError as error: + # Safe generated error strings only; never echo upstream model/API errors. + return "海诺原生接口未完成请求:" + str(error) + "。未自动重试。" + + async def close(self): + await self.http.aclose() diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/relay/longconn.py b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/longconn.py new file mode 100644 index 0000000..bf9ec4a --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/longconn.py @@ -0,0 +1,59 @@ +"""Optional official SDK event source, private platform domain explicitly configured.""" +import json +import logging +import os +import threading +import time + +import httpx +import redis +from .config import Settings + + +def main(): + import lark_oapi as lark + s = Settings(role="longconn") + if not s.feishu_enabled: + raise RuntimeError("platform is disabled") + # The SDK may log websocket URLs, tokens or event bodies. Disable its logging entirely. + logging.disable(logging.CRITICAL) + r = redis.Redis.from_url(s.redis_url) + lease = r.lock("hn:longconn:leader", timeout=30, thread_local=False) + if not lease.acquire(blocking=False): + raise RuntimeError("another long-connection consumer owns the lease") + url = os.environ.get("HN_CONNECTOR_INTERNAL_URL", "http://connector:8000") + client = httpx.Client(base_url=url, timeout=10, follow_redirects=False) + done = threading.Event() + + def maintain_lease(): + while not done.wait(8): + try: + lease.extend(30, replace_ttl=True) + except Exception: + # Fail closed. Continuing after lease loss could create two consumers. + os._exit(2) + + threading.Thread(target=maintain_lease, daemon=True).start() + + def received(event): + data = json.loads(lark.JSON.marshal(event)) + response = client.post("/internal/events", headers={"X-Internal-Secret": s.internal_secret}, json=data) + if response.status_code != 200: + raise RuntimeError("connector did not accept event") + + handler = lark.EventDispatcherHandler.builder("", "").register_p2_im_message_receive_v1(received).build() + try: + # domain is an actual supported SDK argument; no public-cloud-domain monkey patch. + lark.ws.Client(s.feishu_app_id, s.feishu_app_secret, event_handler=handler, + domain=s.feishu_api_base.rstrip("/"), log_level=lark.LogLevel.ERROR).start() + finally: + done.set() + client.close() + try: + lease.release() + except redis.exceptions.LockError: + pass + + +if __name__ == "__main__": + main() diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/relay/oauth.py b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/oauth.py new file mode 100644 index 0000000..e404e22 --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/oauth.py @@ -0,0 +1,127 @@ +import asyncio +import hashlib +import hmac +import secrets +import time +from dataclasses import dataclass +from urllib.parse import urlencode + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .platform import PlatformError + + +@dataclass +class Transaction: + secret_hash: str + expires: float + status: str = "waiting" + result: dict | None = None + + +class Empty(BaseModel): + pass + + +class Refresh(BaseModel): + refresh_token: str = Field(min_length=1, max_length=4096) + + +class OAuth: + """A single connector process owns transactions and tokens in memory only.""" + def __init__(self, settings, redis, platform): + self.settings, self.redis, self.platform = settings, redis, platform + self.pending: dict[str, Transaction] = {} + self.lock = asyncio.Lock() + self.router = APIRouter() + self.router.add_api_route("/oauth/start", self.start, methods=["POST"], status_code=201) + self.router.add_api_route("/oauth/cb", self.callback, methods=["GET"]) + self.router.add_api_route("/oauth/claim", self.claim, methods=["GET"]) + self.router.add_api_route("/oauth/pending", self.status, methods=["GET"]) + self.router.add_api_route("/oauth/refresh", self.refresh, methods=["POST"]) + + def prune(self): + now = time.monotonic() + for state in list(self.pending): + if self.pending[state].expires <= now: + del self.pending[state] + + def authenticated(self, state, secret): + self.prune() + item = self.pending.get(state) + digest = hashlib.sha256((secret or "").encode()).hexdigest() + if not item or not hmac.compare_digest(item.secret_hash, digest): + raise HTTPException(404, "transaction not found") + return item + + async def start(self, body: Empty, request: Request): + if not self.settings.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.redis, "oauth:start:" + peer(request), 20) + async with self.lock: + self.prune() + if len(self.pending) >= self.settings.max_pending: + raise HTTPException(503, "too many pending transactions") + state, secret = secrets.token_urlsafe(32), secrets.token_urlsafe(32) + self.pending[state] = Transaction(hashlib.sha256(secret.encode()).hexdigest(), time.monotonic() + 600) + redirect = self.settings.public_url.rstrip("/") + "/oauth/cb" + url = self.settings.feishu_authorize_url + "?" + urlencode({"app_id": self.settings.feishu_app_id, + "redirect_uri": redirect, "state": state}) + return no_cache(JSONResponse({"state": state, "claim_secret": secret, "authorization_url": url, + "redirect_uri": redirect, "expires_in": 600, + "code_ttl": 60, "poll_interval": 2}, status_code=201)) + + async def callback(self, request: Request, state: str = "", code: str = ""): + await rate_limit(self.redis, "oauth:cb:" + peer(request)) + async with self.lock: + self.prune() + item = self.pending.get(state) + if not code or len(code) > 4096 or not item or item.status != "waiting": + raise HTTPException(400, "invalid callback") + item.status = "exchanging" + try: + result = await self.platform.exchange(code) + except PlatformError: + async with self.lock: + item.status = "failed" + raise HTTPException(502, "authorization exchange failed") from None + async with self.lock: + if state not in self.pending or item.expires <= time.monotonic(): + self.pending.pop(state, None) + raise HTTPException(400, "authorization expired") + item.result, item.status = result, "ready" + return no_cache(HTMLResponse("

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

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

连接你的
工作电脑。

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

+
01 / 连接本人设备

输入电脑上的配对码

+

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

+
+
使用海能Work授权

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

+ +

正在检查网页会话…

+
PC需保持开机、联网,并运行原生API和桥接端。试点网页由中继在内存中转发正文,不保存聊天记录;当前页关闭后消息不保留。
+
diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/relay/web/style.css b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/web/style.css new file mode 100644 index 0000000..a33d19c --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/web/style.css @@ -0,0 +1,3 @@ +:root{color-scheme:light;font-family:system-ui,-apple-system,"Segoe UI","Microsoft YaHei",sans-serif;color:#17372c;background:#f2f4ed}*{box-sizing:border-box}body{margin:0}main{max-width:820px;margin:0 auto;padding:60px 26px}.mark{font-size:13px;letter-spacing:.16em;color:#557764;font-weight:700}h1{font-size:clamp(42px,8vw,70px);letter-spacing:-.05em;line-height:1.12;font-weight:650;margin:40px 0 22px}.intro{font-size:17px;line-height:1.8;color:#56675a;max-width:520px}.card{background:#fff;border:1px solid #dbe3d8;border-radius:20px;padding:30px;margin:40px 0 24px;box-shadow:0 12px 44px #18372c06}.step{font-size:12px;letter-spacing:.1em;color:#6d806c}h2{font-size:24px;margin:16px 0}.card p{line-height:1.8;color:#5b6c60}button{background:#214e3a;color:white;border:0;border-radius:10px;font:inherit;font-weight:600;padding:16px 20px;width:100%;text-align:left;cursor:pointer;margin-top:16px}button span{float:right}button:disabled{opacity:.5;cursor:wait}button:focus-visible,a:focus-visible{outline:3px solid #d0a349;outline-offset:4px}#status{font-size:13px;margin-bottom:0}#open{display:block;margin-top:22px;color:#214e3a;font-weight:600}#open[hidden]{display:none}footer{color:#768373;font-size:12px;line-height:1.8}@media(max-width:480px){main{padding-top:36px}.card{padding:22px}h1{margin-top:28px}} + +[hidden]{display:none!important}.badge{float:right;font-size:11px;letter-spacing:0;background:#e4ebdf;border-radius:20px;padding:6px 10px}label{display:block;font-size:13px;color:#526c5b;margin:12px 0 8px}input,textarea{font:inherit;width:100%;border:1px solid #ccd8ce;border-radius:10px;padding:14px;color:#17372c;background:#fafcf8;resize:vertical}input:focus,textarea:focus{outline:2px solid #6c987b;outline-offset:2px}details{margin-top:24px;font-size:13px;color:#587561}summary{cursor:pointer}.chat-head{display:flex;align-items:center;justify-content:space-between;gap:20px}.secondary{width:auto;background:#edf2e9;color:#31523f;padding:9px 12px;font-size:12px;margin:0}.hint{font-size:13px}#messages{display:flex;flex-direction:column;gap:12px;margin:22px 0;max-height:420px;overflow:auto}.message{padding:14px 16px;border-radius:12px;line-height:1.8;white-space:pre-wrap;overflow-wrap:anywhere;font-size:15px}.message.user{background:#e5eee2;margin-left:36px}.message.assistant{background:#f2f5ef;margin-right:24px}.message.notice{background:#fbf4e5;color:#735c2e}#status{font-size:13px;color:#5f745f;line-height:1.8}#chat-panel h2{font-size:20px}footer{max-width:640px} diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/relay/web_session.py b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/web_session.py new file mode 100644 index 0000000..1ac9adf --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/relay/web_session.py @@ -0,0 +1,183 @@ +"""Pilot H5 pairing and conversation. Credentials never appear in URLs.""" +import hashlib +import json +import secrets +import time +from urllib.parse import urlsplit + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import JSONResponse +from pydantic import BaseModel, Field + +from .common import no_cache, peer, rate_limit +from .crypto import sign, verified, verify_device +from .platform import PlatformError + +COOKIE = "hn_web_session" + + +def digest(value): + return hashlib.sha256(value.encode()).hexdigest() + + +def origin_tuple(value): + try: + parts = urlsplit(value) + if parts.scheme not in {"https", "http"} or not parts.hostname or parts.username or parts.password: + return None + return parts.scheme, parts.hostname.lower(), parts.port or (443 if parts.scheme == "https" else 80) + except ValueError: + return None + + +class PairProof(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Claim(BaseModel): + code: str = Field(min_length=12, max_length=128) + + +class Chat(BaseModel): + text: str = Field(min_length=1, max_length=8000) + request_id: str = Field(pattern=r"^[a-f0-9]{32}$") + + +class WebSessions: + def __init__(self, settings, redis, store, broker, platform): + self.s, self.r, self.store, self.broker, self.platform = settings, redis, store, broker, platform + self.router = APIRouter() + for path, method, handler in [ + ("/device/web-pair", "POST", self.device_pair), + ("/web/claim", "POST", self.claim), + ("/web/session", "GET", self.session), + ("/web/platform-session", "POST", self.platform_login), + ("/web/chat", "POST", self.chat), + ("/web/logout", "POST", self.logout), + ]: + self.router.add_api_route(path, handler, methods=[method]) + + def check_origin(self, request): + actual = origin_tuple(request.headers.get("Origin", "")) + if not actual or actual != origin_tuple(self.s.public_url): + raise HTTPException(403, "same-origin browser request required") + + async def valid_device(self, record): + device = self.store.device(record["host_id"]) + if (not device or device["revoked"] or device["owner"] != record["owner"] + or device["fingerprint"] != record["fingerprint"]): + raise HTTPException(401, "device binding changed; pair again") + generation = await self.r.get("hn:web-generation:" + record["host_id"]) or "0" + if record.get("generation") != generation: + raise HTTPException(401, "device binding changed; pair again") + return device + + async def device_pair(self, body: PairProof, request: Request): + await rate_limit(self.r, "web-pair-ip:" + peer(request), 20) + device = self.store.device(body.host_id) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "active bound device required") + try: + key = verify_device(device["cert"], self.s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["owner"] != device["owner"] + or proof["purpose"] != "h5-pair-v1" or abs(time.time() - proof["ts"]) > 90 + or not isinstance(proof["nonce"], str) or len(proof["nonce"]) != 32): + raise ValueError("invalid proof") + except Exception: + raise HTTPException(403, "invalid device proof") from None + replay = "hn:web-pair-proof:" + digest(body.host_id + proof["nonce"]) + if not await self.r.set(replay, "used", nx=True, ex=180): + raise HTTPException(409, "pairing request already used") + await rate_limit(self.r, "web-pair-device:" + body.host_id, 5) + code = secrets.token_urlsafe(18) + record = {"host_id": body.host_id, "owner": device["owner"], "fingerprint": device["fingerprint"], + "generation": await self.r.get("hn:web-generation:" + body.host_id) or "0"} + await self.r.set("hn:web-code:" + digest(code), json.dumps(record), ex=120) + self.store.audit(device["owner"], "web_pair_issued", body.host_id, "success") + return no_cache(JSONResponse(sign({"host_id": body.host_id, "code": code, "expires_in": 120, + "url": self.s.public_url.rstrip("/") + "/h5"}, self.broker.signer))) + + async def new_session(self, record): + token = secrets.token_urlsafe(32) + record = {k: record[k] for k in ("host_id", "owner", "fingerprint", "generation")} + await self.r.set("hn:web-session:" + digest(token), json.dumps(record), ex=1800) + response = no_cache(JSONResponse({"status": "paired", "host_id": record["host_id"], "expires_in": 1800})) + response.set_cookie(COOKIE, token, max_age=1800, secure=True, httponly=True, samesite="strict", path="/") + return response + + async def claim(self, body: Claim, request: Request): + self.check_origin(request) + await rate_limit(self.r, "web-claim:" + peer(request), 20) + key = "hn:web-code:" + digest(body.code.strip()) + encoded = await self.r.get(key) + if not encoded: + raise HTTPException(404, "code expired or consumed") + await self.valid_device(json.loads(encoded)) + encoded = await self.r.getdel(key) + if not encoded: + raise HTTPException(404, "code already consumed") + record = json.loads(encoded) + await self.valid_device(record) + self.store.audit(record["owner"], "web_pair_claimed", record["host_id"], "success") + return await self.new_session(record) + + async def authenticated(self, request): + token = request.cookies.get(COOKIE, "") + if not token or len(token) > 128: + raise HTTPException(401, "pair this browser first") + encoded = await self.r.get("hn:web-session:" + digest(token)) + if not encoded: + raise HTTPException(401, "browser session expired") + record = json.loads(encoded) + await self.valid_device(record) + return record + + async def session(self, request: Request): + record = await self.authenticated(request) + return no_cache(JSONResponse({"authenticated": True, "host_id": record["host_id"], + "online": bool(await self.r.exists("hn:online:" + record["host_id"])), + "mode": "pilot-text-only", "relay_sees_content": True})) + + async def platform_login(self, request: Request): + self.check_origin(request) + if not self.s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.r, "web-platform:" + peer(request), 20) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await self.platform.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = self.store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "bind your device first") + return await self.new_session({**device, "generation": await self.r.get("hn:web-generation:" + device["host_id"]) or "0"}) + + async def chat(self, body: Chat, request: Request): + self.check_origin(request) + record = await self.authenticated(request) + await rate_limit(self.r, "web-chat:" + record["owner"], 20) + text = body.text.strip() + if not text: + raise HTTPException(422, "empty message") + # PC accepts only its bound owner and the existing IM read-only channel. + if not (text == "/主机名" or text.startswith(("/回显 ", "/读取 ", "/助手 "))): + text = "/助手 " + text + turn = "web_" + digest(record["owner"] + "\0" + body.request_id) + result = await self.broker.dispatch(record["owner"], text, turn) + # Revoke/unbind during a long model call invalidates the browser immediately. + await self.authenticated(request) + return no_cache(JSONResponse(result)) + + async def logout(self, request: Request): + self.check_origin(request) + token = request.cookies.get(COOKIE, "") + if token and len(token) <= 128: + await self.r.delete("hn:web-session:" + digest(token)) + response = no_cache(JSONResponse({"status": "logged_out"})) + response.delete_cookie(COOKIE, secure=True, httponly=True, samesite="strict", path="/") + return response diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/scripts/hainuo_native_api.py b/patch-source/0de3db40fc2044b0/hainuo-relay/scripts/hainuo_native_api.py new file mode 100644 index 0000000..44eb4ab --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/scripts/hainuo_native_api.py @@ -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() diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/scripts/update-h5.sh b/patch-source/0de3db40fc2044b0/hainuo-relay/scripts/update-h5.sh new file mode 100644 index 0000000..5404874 --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/scripts/update-h5.sh @@ -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 diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/tests/__init__.py b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/tests/conftest.py b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/conftest.py new file mode 100644 index 0000000..563fb60 --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/conftest.py @@ -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 diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_agent.py b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_agent.py new file mode 100644 index 0000000..9e3ed6e --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_agent.py @@ -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") diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_boundaries.py b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_boundaries.py new file mode 100644 index 0000000..7481d5b --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_boundaries.py @@ -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"} diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_crypto.py b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_crypto.py new file mode 100644 index 0000000..08f0eea --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_crypto.py @@ -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) diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_hainuo1065.py b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_hainuo1065.py new file mode 100644 index 0000000..cfb59fe --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_hainuo1065.py @@ -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) diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_image_archive.py b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_image_archive.py new file mode 100644 index 0000000..faaa0e5 --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_image_archive.py @@ -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 diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_integration.py b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_integration.py new file mode 100644 index 0000000..42acc93 --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_integration.py @@ -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 == [] diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_platform.py b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_platform.py new file mode 100644 index 0000000..73f5c9a --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_platform.py @@ -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() diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_postgres.py b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_postgres.py new file mode 100644 index 0000000..826993b --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_postgres.py @@ -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() diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_web_native_chain.py b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_web_native_chain.py new file mode 100644 index 0000000..9e10f4f --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_web_native_chain.py @@ -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) diff --git a/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_web_session.py b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_web_session.py new file mode 100644 index 0000000..efddc77 --- /dev/null +++ b/patch-source/0de3db40fc2044b0/hainuo-relay/tests/test_web_session.py @@ -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] diff --git a/patch-source/51c062dff81e268e/Dockerfile.offline b/patch-source/51c062dff81e268e/Dockerfile.offline new file mode 100644 index 0000000..8af3dff --- /dev/null +++ b/patch-source/51c062dff81e268e/Dockerfile.offline @@ -0,0 +1,10 @@ +FROM python:3.12-slim-bookworm +ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 +WORKDIR /app +COPY wheelhouse /wheels +COPY requirements.lock pyproject.toml ./ +COPY relay ./relay +RUN pip install --no-index --find-links=/wheels --no-cache-dir -r requirements.lock && rm -rf /wheels +RUN chmod -R a+rX /app +USER 10001:10001 +ENTRYPOINT ["python", "-m", "relay.cli"] diff --git a/patch-source/51c062dff81e268e/compose.autoreg.yaml b/patch-source/51c062dff81e268e/compose.autoreg.yaml new file mode 100644 index 0000000..9902acb --- /dev/null +++ b/patch-source/51c062dff81e268e/compose.autoreg.yaml @@ -0,0 +1,39 @@ +services: + connector: + environment: + HN_ENROLLMENT_SIGNER_URL: + 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 diff --git a/patch-source/51c062dff81e268e/relay/__init__.py b/patch-source/51c062dff81e268e/relay/__init__.py new file mode 100644 index 0000000..32754a7 --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/__init__.py @@ -0,0 +1 @@ +"""Hainuo relay 0.1.0.""" diff --git a/patch-source/51c062dff81e268e/relay/cli.py b/patch-source/51c062dff81e268e/relay/cli.py new file mode 100644 index 0000000..fad7979 --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/cli.py @@ -0,0 +1,216 @@ +import argparse +import asyncio +import json +import os +import secrets +import ssl +import uuid +from datetime import datetime, timedelta, timezone +from pathlib import Path + +import httpx +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ed25519 +from cryptography.x509.oid import NameOID + +from .crypto import issue_ca, issue_device, load_private, pem_private + + +def write_new(path, data, private=False): + path.parent.mkdir(parents=True, exist_ok=True) + with path.open("xb") as f: + f.write(data if isinstance(data, bytes) else data.encode()) + if private and os.name != "nt": + path.chmod(0o600) + + +def init_project(root: Path, hostname: str): + if (root / ".env").exists(): + raise RuntimeError(".env exists; init never overwrites credentials") + ca_key, ca = issue_ca() + signer = ed25519.Ed25519PrivateKey.generate() + write_new(root / "operator/ca.key", pem_private(ca_key), True) + write_new(root / "runtime/trust/ca.pem", ca.public_bytes(serialization.Encoding.PEM)) + write_new(root / "runtime/connector/connector.key", pem_private(signer), True) + write_new(root / "runtime/trust/connector.pub", signer.public_key().public_bytes( + serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo)) + tls_key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, hostname)]) + now = datetime.now(timezone.utc) + tls = (x509.CertificateBuilder().subject_name(name).issuer_name(name).public_key(tls_key.public_key()) + .serial_number(x509.random_serial_number()).not_valid_before(now - timedelta(minutes=5)) + .not_valid_after(now + timedelta(days=30)) + .add_extension(x509.SubjectAlternativeName([x509.DNSName(hostname)]), critical=False) + .sign(tls_key, algorithm=None)) + write_new(root / "runtime/tls/server.key", pem_private(tls_key), True) + write_new(root / "runtime/tls/server.pem", tls.public_bytes(serialization.Encoding.PEM)) + redis_secret = secrets.token_hex(32) + db_secret = secrets.token_hex(32) + env = (f"HN_ENVIRONMENT=development\nHN_PUBLIC_HOST={hostname}\nHN_PUBLIC_URL=https://{hostname}\n" + f"HN_ROUTER_URL=wss://{hostname}/device/ws\nHN_ADMIN_SECRET={secrets.token_hex(32)}\n" + f"HN_INTERNAL_SECRET={secrets.token_hex(32)}\nHN_REDIS_PASSWORD={redis_secret}\n" + f"HN_REDIS_URL=redis://:{redis_secret}@redis:6379/0\n" + f"HN_POSTGRES_PASSWORD={db_secret}\nHN_DATABASE_URL=postgresql://hainuo:{db_secret}@postgres:5432/hainuo\n" + "HN_FEISHU_ENABLED=false\nHN_FEISHU_API_BASE=https://open.feishu.cn\n" + "HN_FEISHU_AUTHORIZE_URL=https://open.feishu.cn/open-apis/authen/v1/authorize\n" + "HN_FEISHU_APP_ID=\nHN_FEISHU_APP_SECRET=\nHN_FEISHU_ENCRYPT_KEY=\n" + "HN_FEISHU_VERIFICATION_TOKEN=\nHN_PROXY_ENABLED=false\nHN_DEVICE_DOMAIN=\n") + write_new(root / ".env", env, True) + print("已生成配置、连接器密钥、设备CA及临时TLS证书;未输出凭据。") + print("operator/ca.key 仅保留在签发工作站,不要上传到ECS。临时TLS证书上线前须替换。") + + +def device_request(directory): + host = uuid.uuid4().hex + private = ed25519.Ed25519PrivateKey.generate() + write_new(directory / "device.key", pem_private(private), True) + public = private.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode() + write_new(directory / "request.json", json.dumps({"host_id": host, "public_key": public}, indent=2)) + print("已在本机生成私钥和 request.json;仅将request.json交给签发工作站。") + + +def issue(root, request_file, output): + request = json.loads(request_file.read_text(encoding="utf-8")) + host = request["host_id"] + if len(host) != 32 or any(c not in "0123456789abcdef" for c in host): + raise ValueError("invalid host identity") + ca = x509.load_pem_x509_certificate((root / "runtime/trust/ca.pem").read_bytes()) + public = serialization.load_pem_public_key(request["public_key"].encode()) + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("Ed25519 required") + cert = issue_device(load_private(root / "operator/ca.key"), ca, host, public) + pem = cert.public_bytes(serialization.Encoding.PEM) + write_new(output / "device.pem", pem) + write_new(output / "ca.pem", (root / "runtime/trust/ca.pem").read_bytes()) + write_new(output / "connector.pub", (root / "runtime/trust/connector.pub").read_bytes()) + write_new(output / "device.json", json.dumps({"host_id": host, "owner": None}, indent=2)) + write_new(output / "registration.json", json.dumps({"host_id": host, "cert": pem.decode()}, indent=2)) + print("已签发设备证书;将device.pem、ca.pem、connector.pub、device.json交回原设备。") + + +def main(): + parser = argparse.ArgumentParser(description="海诺中继部署和设备工具") + sub = parser.add_subparsers(dest="command", required=True) + p = sub.add_parser("init") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--hostname", required=True) + p = sub.add_parser("serve") + p.add_argument("role", choices=["connector", "router"]) + p.add_argument("--host", default="127.0.0.1") + p.add_argument("--port", type=int, default=8000) + p = sub.add_parser("device-request") + p.add_argument("--directory", type=Path, required=True) + p = sub.add_parser("issue-device") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--request", type=Path, required=True) + p.add_argument("--output", type=Path, required=True) + p = sub.add_parser("register") + p.add_argument("--registration", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("bind") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("device") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p.add_argument("--test-root", type=Path) + p.add_argument("--agent-url", help="Explicit loopback read-only Agent adapter endpoint") + p.add_argument("--hainuo-config", type=Path, help="Native installer-1.065 API adapter config; alternative to --agent-url") + p = sub.add_parser("hainuo-probe") + p.add_argument("--config", type=Path, required=True) + p = sub.add_parser("web-pair") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + for command in ("status", "binding-code", "dispatch", "revoke", "unbind", "reconcile"): + p = sub.add_parser(command) + p.add_argument("--url", default="http://127.0.0.1:18081") + p.add_argument("--tls-ca", type=Path) + if command in {"binding-code", "dispatch"}: + p.add_argument("--owner", required=True) + if command == "dispatch": + p.add_argument("--text", required=True) + p.add_argument("--turn-id", default=None) + if command in {"revoke", "unbind"}: + p.add_argument("--host-id", required=True) + args = parser.parse_args() + if args.command == "init": + init_project(args.root, args.hostname) + elif args.command == "serve": + import uvicorn + uvicorn.run(f"relay.{args.role}:create_app", factory=True, host=args.host, port=args.port, + workers=1, access_log=False, proxy_headers=False, + ws_max_size=262144, ws_max_queue=16, log_level="warning") + elif args.command == "device-request": + device_request(args.directory) + elif args.command == "issue-device": + issue(args.root, args.request, args.output) + elif args.command == "register": + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, follow_redirects=False, timeout=15) as client: + response = client.post(args.url.rstrip("/") + "/admin/devices", + headers={"X-Admin-Secret": secret}, + json=json.loads(args.registration.read_text(encoding="utf-8"))) + if response.status_code != 201: + raise RuntimeError(f"register failed: HTTP {response.status_code}") + print("设备登记成功。") + elif args.command == "bind": + import getpass + from .device import bind_device + code = getpass.getpass("Binding code: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + asyncio.run(bind_device(args.directory, args.url, code, tls_verify=context)) + print("绑定成功。") + elif args.command == "device": + from .device import run_device + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else None + asyncio.run(run_device(args.directory, args.url, root=args.test_root, ssl_context=context, + agent_url=args.agent_url, hainuo_config=args.hainuo_config)) + elif args.command == "hainuo-probe": + from .hainuo1065 import Hainuo1065Agent + async def probe(): + adapter = Hainuo1065Agent(args.config) + try: + print(json.dumps(await adapter.probe(), ensure_ascii=False, indent=2)) + finally: + await adapter.close() + asyncio.run(probe()) + elif args.command == "web-pair": + from .device import pair_browser + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + result = asyncio.run(pair_browser(args.directory, args.url, tls_verify=context)) + print("网页地址:" + result["url"]) + print("一次性网页配对码(120秒有效):" + result["code"]) + else: + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, timeout=200, follow_redirects=False) as client: + paths = {"status": "/admin/devices", "binding-code": "/admin/test/binding-code", + "dispatch": "/admin/test/dispatch", "reconcile": "/admin/reconcile"} + if args.command in {"revoke", "unbind"}: + if len(args.host_id) != 32 or any(c not in "0123456789abcdef" for c in args.host_id): + raise ValueError("invalid host identity") + path = f"/admin/devices/{args.host_id}/{args.command}" + else: + path = paths[args.command] + body = None + if args.command in {"binding-code", "dispatch"}: + body = {"open_id": args.owner, "text": getattr(args, "text", "binding")} + if getattr(args, "turn_id", None): + body["turn_id"] = args.turn_id + response = client.request("GET" if args.command == "status" else "POST", args.url.rstrip("/") + path, + headers={"X-Admin-Secret": secret}, json=body) + if response.status_code != 200: + raise RuntimeError(f"operation failed: HTTP {response.status_code}") + print(json.dumps(response.json(), ensure_ascii=False, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/patch-source/51c062dff81e268e/relay/client_manage.py b/patch-source/51c062dff81e268e/relay/client_manage.py new file mode 100644 index 0000000..8fa25e4 --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/client_manage.py @@ -0,0 +1,284 @@ +"""Per-user Windows connection companion. No platform/application secrets.""" +import argparse +import asyncio +import json +import os +import secrets +import socket +import ssl +import subprocess +import sys +import time +import uuid +from pathlib import Path + +import httpx +import psutil +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey +from .crypto import load_private, load_public, pem_private, raw_public, sign, verified, verify_device +from .windows_keys import protect + +BASE = Path(__file__).resolve().parent.parent +URL = "https://10.77.76.13" + + +def save(path, value): + temp = path.with_suffix(path.suffix + ".tmp") + temp.write_text(json.dumps(value, ensure_ascii=False, indent=2), encoding="utf-8") + os.replace(temp, path) + + +def secure_directory(directory): + directory.mkdir(parents=True, exist_ok=True) + # SID avoids localized account names. Fail closed if ACL protection fails. + result = subprocess.run(["whoami", "/user", "/fo", "csv", "/nh"], capture_output=True, text=True, check=True) + import csv + sid = list(csv.reader([result.stdout.strip()]))[0][1] + subprocess.run(["icacls", str(directory), "/inheritance:r", "/grant:r", f"*{sid}:(OI)(CI)F", "*S-1-5-18:(OI)(CI)F"], + capture_output=True, check=True, creationflags=subprocess.CREATE_NO_WINDOW) + + +def prepare(directory): + secure_directory(directory) + for name in ("server.pem", "ca.pem", "connector.pub"): + target = directory / name + if not target.exists(): + target.write_bytes((BASE / "trust" / name).read_bytes()) + if not (directory / "device.json").exists(): + if (directory / "device.key.dpapi").exists() or (directory / "device.key").exists(): + raise RuntimeError("发现不完整的设备身份,请保留目录并联系管理员恢复。") + private = Ed25519PrivateKey.generate() + (directory / "device.key.dpapi").write_bytes(protect(pem_private(private))) + save(directory / "device.json", {"host_id": uuid.uuid4().hex, "owner": None}) + + +def tls(directory): + return ssl.create_default_context(cafile=str(directory / "server.pem")) + + +def import_existing(directory, source): + """Explicit migration; only public trust and this PC's identity, never app keys.""" + if source.resolve() == directory.resolve(): + raise ValueError("请选择旧设备目录。") + if any((directory / name).exists() for name in ("device.json", "device.key.dpapi", "device.key")): + raise RuntimeError("本目录已有设备身份。请使用已有身份继续,不能用导入覆盖。") + config = json.loads((source / "device.json").read_text(encoding="utf-8-sig")) + for name in ("server.pem", "ca.pem", "connector.pub"): + if (source / name).read_bytes().strip() != (BASE / "trust" / name).read_bytes().strip(): + raise ValueError("旧设备的服务信任与本安装包不符,请联系管理员。") + cert = (source / "device.pem").read_text(encoding="utf-8") + public = verify_device(cert, (source / "ca.pem").read_bytes(), config["host_id"]) + private = load_private(source / "device.key") + if raw_public(public) != raw_public(private.public_key()): + raise ValueError("设备证书与本机私钥不匹配。") + secure_directory(directory) + for name in ("device.pem", "server.pem", "ca.pem", "connector.pub"): + (directory / name).write_bytes((source / name).read_bytes()) + (directory / "device.key.dpapi").write_bytes(protect(pem_private(private))) + save(directory / "device.json", config) + # Preserve local replay journal using SQLite's consistent backup API. + if (source / "journal.sqlite").is_file(): + import sqlite3 + with sqlite3.connect(source / "journal.sqlite") as original, sqlite3.connect(directory / "journal.sqlite") as target: + original.backup(target) + return {"message": "旧设备导入成功,Work绑定已保留。请先停止旧PowerShell桥接窗口,再点击启动连接。"} + + +def enrollment(directory): + prepare(directory) + cfg = json.loads((directory / "device.json").read_text()) + if (directory / "device.pem").exists(): + verify_device((directory / "device.pem").read_text(), (directory / "ca.pem").read_bytes(), cfg["host_id"]) + return {"message": "设备已注册,可在Work获取绑定码后绑定。已有绑定可直接启动连接。"} + with httpx.Client(verify=tls(directory), trust_env=False, timeout=15) as http: + key = load_private(directory / "device.key") + proof = sign({"host_id": cfg["host_id"], "purpose": "device-enroll-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + response = http.post(URL + "/device/register", json={"host_id": cfg["host_id"], "proof": proof, + "public_key": key.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode()}) + if response.status_code not in {200, 201}: + raise RuntimeError("自动注册失败:HTTP " + str(response.status_code) + ";请确认ECS已安装1.067自动注册更新。") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt.get("status") != "registered" or receipt["host_id"] != cfg["host_id"]: + raise ValueError("注册回执身份不符") + public = verify_device(receipt["cert"], (directory / "ca.pem").read_bytes(), cfg["host_id"]) + if raw_public(public) != raw_public(key.public_key()): + raise ValueError("注册证书公钥不符") + temp = directory / "device.pem.tmp" + temp.write_text(receipt["cert"], encoding="utf-8") + os.replace(temp, directory / "device.pem") + (directory / "enrollment.json").unlink(missing_ok=True) + return {"message": "设备已自动注册,无需管理员审批。请在Work获取绑定码,填写后点击绑定并连接。"} + + +def managed_processes(directory): + """Only processes with our absolute entry path AND exact device directory.""" + state = directory / "processes.json" + if not state.exists(): + return [] + result = [] + for item in json.loads(state.read_text()).get("processes", []): + try: + proc = psutil.Process(item["pid"]) + command = proc.cmdline() + if (abs(proc.create_time() - item["created"]) < 0.1 and str(BASE / "entry.py") in command + and str(directory) in command): + result.append(proc) + except psutil.Error: + pass + return result + + +def stop(directory): + procs = managed_processes(directory) + # Supervisor owns and terminates only its own child handles in its finally block. + (directory / "stop.flag").touch() + _, alive = psutil.wait_procs(procs, timeout=8) + for proc in alive: + proc.terminate() + return {"message": "已停止本安装包启动的连接进程。手工启动的旧桥接请在原窗口按Ctrl+C停止。"} + + +def start(directory, root): + from .process_lock import DeviceProcessLock + with DeviceProcessLock(directory / "control"): + return _start(directory, root) + + +def _start(directory, root): + cfg = json.loads((directory / "device.json").read_text()) + if not cfg.get("owner") or not (directory / "device.pem").exists(): + raise RuntimeError("请先完成设备准入和Work绑定。") + if managed_processes(directory): + return {"message": "连接进程已运行,请点击状态查看。"} + (directory / "stop.flag").unlink(missing_ok=True) + log = (directory / "bridge.log").open("a", encoding="utf-8") + proc = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "supervise", "--directory", str(directory), "--root", str(root)], + stdout=log, stderr=log, creationflags=subprocess.CREATE_NO_WINDOW) + log.close() + save(directory / "processes.json", {"processes": [{"pid": proc.pid, "created": psutil.Process(proc.pid).create_time()}]}) + return {"message": "连接进程已启动;约30秒后点击状态,再到Work发送 /主机名 验证。"} + + +def supervise(directory, root): + from .process_lock import DeviceProcessLock + lockdir = directory / "supervisor" + with DeviceProcessLock(lockdir): + children = [] + def record_children(): + processes = [psutil.Process(os.getpid())] + [psutil.Process(p.pid) for p in children if p.poll() is None] + save(directory / "processes.json", {"processes": [{"pid": p.pid, "created": p.create_time()} for p in processes]}) + try: + # Check local bind availability without an HTTP connection. Windows + # filtering can time out connections to a closed port rather than + # refuse them; that must not prevent starting our own listener. + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as port_probe: + try: + if os.name == "nt": + port_probe.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1) + port_probe.bind(("127.0.0.1", 18643)) + except OSError: + raise RuntimeError("原生API端口18643被占用或不允许绑定,请先检查旧连接。") from None + native = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "native", "--directory", str(directory), "--root", str(root)]) + children.append(native) + record_children() + for _ in range(90): + if (directory / "stop.flag").exists(): + return + if native.poll() is not None: + raise RuntimeError("本机原生API启动失败,请检查安装配置。") + try: + from .hainuo1065 import Hainuo1065Agent + async def probe(): + agent = Hainuo1065Agent(directory / "hainuo-native.json") + try: + return await agent.probe() + finally: + await agent.close() + asyncio.run(probe()) + break + except Exception: + time.sleep(1) + else: + raise RuntimeError("本机原生API未就绪。") + bridge = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "bridge", "--directory", str(directory), "--root", str(root)]) + children.append(bridge) + record_children() + while not (directory / "stop.flag").exists(): + if any(proc.poll() is not None for proc in children): + raise RuntimeError("连接子进程已退出,请查看状态后重新启动。") + time.sleep(0.5) + finally: + for proc in reversed(children): + if proc.poll() is None: + proc.terminate() + try: + proc.wait(timeout=5) + except subprocess.TimeoutExpired: + proc.kill() + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("action", choices=["import", "enroll", "bind", "start", "stop", "status", "pair", "supervise", "native", "bridge"]) + parser.add_argument("--directory", type=Path, required=True) + parser.add_argument("--root", type=Path, required=True) + args = parser.parse_args() + directory, root = args.directory.resolve(), args.root.resolve() + directory.mkdir(parents=True, exist_ok=True) + try: + if args.action == "import": + result = import_existing(directory, Path(sys.stdin.readline().strip())) + elif args.action == "enroll": + result = enrollment(directory) + elif args.action == "bind": + from .device import bind_device + code = sys.stdin.readline().strip() + if not code: + raise ValueError("请填写Work机器人新生成的绑定码。") + enrollment(directory) + stop(directory) + asyncio.run(bind_device(directory, URL, code, tls_verify=tls(directory))) + result = start(directory, root) + result["message"] = "Work绑定成功。" + result["message"] + elif args.action == "start": + result = start(directory, root) + elif args.action == "stop": + result = stop(directory) + elif args.action == "pair": + from .device import pair_browser + receipt = asyncio.run(pair_browser(directory, URL, tls_verify=tls(directory))) + result = {"message": "网页:" + receipt["url"] + "\n一次性配对码(120秒):" + receipt["code"]} + elif args.action == "status": + cfg = json.loads((directory / "device.json").read_text()) if (directory / "device.json").exists() else {} + log = directory / "bridge.log" + tail = log.read_bytes()[-6000:].decode("utf-8", errors="replace") if log.exists() else "" + result = {"message": "设备:" + cfg.get("host_id", "未申请") + "\nWork身份:" + (cfg.get("owner") or "未绑定") + + "\n连接进程:" + ("运行中(请在Work发送 /主机名 确认当前链路)" if managed_processes(directory) else "未运行") + "\n" + tail[-2500:]} + elif args.action == "supervise": + return supervise(directory, root) + elif args.action == "native": + import importlib.util + spec = importlib.util.spec_from_file_location("native_companion", BASE / "hainuo_native_api.py") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + module.enable_profile_tools(root, "hainuo") + key = module.prepare_environment(root, "hainuo", 18643, directory / "hainuo-native.json", enable_tools=True) + import logging + logging.disable(logging.CRITICAL) + return asyncio.run(module.serve_native(18643, key, enable_tools=True)) + else: + from .device import run_device + return asyncio.run(run_device(directory, "wss://10.77.76.13/device/ws", ssl_context=tls(directory), hainuo_config=directory / "hainuo-native.json")) + print(json.dumps(result, ensure_ascii=False)) + except KeyboardInterrupt: + pass + except Exception as error: + # Native exception bodies may include URLs or secrets; emit bounded known messages only. + message = str(error) if isinstance(error, (RuntimeError, ValueError)) else type(error).__name__ + ";请检查网络、证书、安装目录。" + print(json.dumps({"message": message[:600], "error": True}, ensure_ascii=False)) + raise SystemExit(1) from None + + +if __name__ == "__main__": + main() diff --git a/patch-source/51c062dff81e268e/relay/common.py b/patch-source/51c062dff81e268e/relay/common.py new file mode 100644 index 0000000..c112b99 --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/common.py @@ -0,0 +1,61 @@ +import hashlib +import hmac +import json +import time +from fastapi import HTTPException, Request +from starlette.responses import JSONResponse + + +def require_secret(actual, expected): + if not actual or not hmac.compare_digest(actual, expected): + raise HTTPException(401, "unauthorized") + + +async def rate_limit(redis, bucket: str, limit=60, seconds=60): + # Atomic counter + expiry: a crash cannot leave a permanent bucket. + key = "hn:rate:" + hashlib.sha256(bucket.encode()).hexdigest() + ":" + str(int(time.time()) // seconds) + pipe = redis.pipeline(transaction=True) + pipe.incr(key) + pipe.expire(key, seconds * 2) + result = (await pipe.execute())[0] + if int(result) > limit: + raise HTTPException(429, "rate limited", headers={"Retry-After": str(seconds)}) + + +class BodyLimitMiddleware: + def __init__(self, app, max_body=262144): + self.app, self.max_body = app, max_body + + async def __call__(self, scope, receive, send): + if scope["type"] != "http": + return await self.app(scope, receive, send) + messages, size = [], 0 + while True: + message = await receive() + if message["type"] == "http.disconnect": + return + size += len(message.get("body", b"")) + if size > self.max_body: + return await JSONResponse({"detail": "body too large"}, status_code=413)(scope, receive, send) + messages.append(message) + if not message.get("more_body", False): + break + async def buffered(): + return messages.pop(0) if messages else await receive() + await self.app(scope, buffered, send) + + +def no_cache(response): + response.headers["Cache-Control"] = "no-store" + response.headers["X-Content-Type-Options"] = "nosniff" + response.headers["Referrer-Policy"] = "no-referrer" + return response + + +async def json_pub(redis, channel, data): + return await redis.publish(channel, json.dumps(data, separators=(",", ":"))) + + +def peer(request: Request): + # Do not trust arbitrary X-Forwarded-For; configure uvicorn trusted proxies explicitly. + return request.client.host if request.client else "unknown" diff --git a/patch-source/51c062dff81e268e/relay/config.py b/patch-source/51c062dff81e268e/relay/config.py new file mode 100644 index 0000000..d6ad400 --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/config.py @@ -0,0 +1,65 @@ +from pathlib import Path +from pydantic import model_validator +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + model_config = SettingsConfigDict(env_prefix="HN_", extra="ignore", hide_input_in_errors=True) + environment: str = "production" + role: str = "connector" + redis_url: str = "redis://127.0.0.1:6379/0" + data_dir: Path = Path("runtime/connector") + database_url: str = "" + admin_secret: str = "" + internal_secret: str = "" + ca_cert: Path = Path("runtime/pki/ca.pem") + connector_key: Path = Path("runtime/pki/connector.key") + connector_public_key: Path = Path("runtime/pki/connector.pub") + public_url: str = "https://relay.example.invalid" + router_url: str = "wss://relay.example.invalid/device/ws" + device_domain: str = "" + feishu_api_base: str = "https://open.feishu.cn" + feishu_authorize_url: str = "https://open.feishu.cn/open-apis/authen/v1/authorize" + feishu_app_id: str = "" + feishu_app_secret: str = "" + feishu_encrypt_key: str = "" + feishu_verification_token: str = "" + feishu_enabled: bool = False + oauth_enabled: bool = True + feishu_event_source: str = "longconn" + proxy_enabled: bool = False + # A fixed allowlist, never a user supplied upstream URL. + proxy_paths: str = "/open-apis/drive/v1/files,/open-apis/docx/v1/documents" + request_timeout: int = 45 + max_body: int = 262144 + max_pending: int = 1000 + max_devices: int = 1000 + max_connections: int = 1000 + max_result: int = 16000 + allow_insecure_local: bool = False + enrollment_signer_url: str = "" + + @model_validator(mode="after") + def validate_runtime(self): + if self.environment not in {"production", "test", "development"}: + raise ValueError("invalid environment") + if self.role == "connector" and min(len(self.admin_secret), len(self.internal_secret)) < 32: + raise ValueError("admin/internal secrets must each be at least 32 characters") + if self.role == "longconn" and len(self.internal_secret) < 32: + raise ValueError("internal credential must be at least 32 characters") + if self.role == "connector" and self.admin_secret == self.internal_secret: + raise ValueError("admin and internal credentials must be different") + if self.environment == "production": + if self.allow_insecure_local or not self.public_url.startswith("https://"): + raise ValueError("production requires HTTPS") + if not self.router_url.startswith("wss://"): + raise ValueError("production requires WSS") + if not self.feishu_api_base.startswith("https://"): + raise ValueError("platform API requires HTTPS") + if self.feishu_event_source not in {"longconn", "webhook"}: + raise ValueError("invalid platform event source") + if self.feishu_enabled and not all((self.feishu_app_id, self.feishu_app_secret)): + raise ValueError("platform app ID and secret are required") + if self.feishu_enabled and self.feishu_event_source == "webhook" and not all((self.feishu_encrypt_key, self.feishu_verification_token)): + raise ValueError("webhook requires encryption key and verification token") + return self diff --git a/patch-source/51c062dff81e268e/relay/connector.py b/patch-source/51c062dff81e268e/relay/connector.py new file mode 100644 index 0000000..44a2ee9 --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/connector.py @@ -0,0 +1,503 @@ +import asyncio +import base64 +import contextlib +import hashlib +import hmac +import json +import re +import secrets +import sqlite3 +import time +import uuid +from contextlib import asynccontextmanager +from dataclasses import dataclass +from urllib.parse import unquote, urlsplit + +from cryptography.hazmat.primitives import hashes, padding +from cryptography.hazmat.primitives.asymmetric import x25519 +from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes +from fastapi import FastAPI, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from fastapi.exceptions import RequestValidationError +from pydantic import BaseModel, Field +from redis.asyncio import Redis + +from .common import BodyLimitMiddleware, no_cache, peer, rate_limit, require_secret +from .config import Settings +from .crypto import (fingerprint, load_private, open_envelope, raw_public, seal, + sign, verified, verify_device) +from .oauth import OAuth +from .platform import Feishu, PlatformError +from .store import Store, INTEGRITY_ERRORS + + +class Register(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + cert: str = Field(max_length=10000) + + +class Bind(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Instruction(BaseModel): + open_id: str = Field(min_length=1, max_length=128) + text: str = Field(min_length=1, max_length=8000) + # Stable identity chosen by upstream, not generated again on retries. + turn_id: str = Field(default_factory=lambda: uuid.uuid4().hex, pattern=r"^[A-Za-z0-9_-]{1,128}$") + + +@dataclass +class Pending: + future: asyncio.Future + private: object + public: object + host: str + session: str + + +class Broker: + def __init__(self, settings, redis, store): + self.s, self.r, self.store = settings, redis, store + self.signer = load_private(settings.connector_key) + self.pending: dict[str, Pending] = {} + + async def replies(self, pub): + async for message in pub.listen(): + if message["type"] != "message": + continue + try: + data = json.loads(message["data"]) + item = self.pending.get(data["turn_id"]) + if not item or item.future.done(): + continue + plain = open_envelope(data["envelope"], item.private, item.public, + host_id=item.host, session_id=item.session) + if plain["turn_id"] != data["turn_id"] or plain.get("type") != "result": + continue + result = plain.get("result", "") + if not isinstance(result, str) or len(result) > self.s.max_result: + continue + item.future.set_result({"status": "completed", "turn_id": data["turn_id"], "result": result}) + except Exception: + # Invalid packets never consume a pending result; do not log packet contents. + continue + + async def dispatch(self, owner, text, turn): + device = self.store.by_owner(owner) + if not device: + return {"status": "unavailable", "turn_id": turn, "result": "未绑定设备或设备不可用。"} + host = device["host_id"] + await rate_limit(self.r, "dispatch:" + owner, 30) + if len(self.pending) >= self.s.max_pending: + raise HTTPException(503, "too many in-flight requests") + presence = await self.r.get("hn:online:" + host) + if not presence or await self.r.exists("hn:revoked:" + host): + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线,请开机并连接网络。"} + try: + presence = json.loads(presence) + if fingerprint(presence["cert"]) != device["fingerprint"]: + raise ValueError("certificate changed") + public = verify_device(presence["cert"], self.s.ca_cert.read_bytes(), host) + proof = verified(presence["proof"], public) + if proof["host_id"] != host or proof["challenge"] != presence["challenge"]: + raise ValueError("invalid device session") + session = proof["session_id"] + except Exception: + self.store.audit(owner, "dispatch", host, "identity_rejected") + raise HTTPException(409, "device identity rejected") from None + if not self.store.begin_turn(turn, owner, host): + return {"status": "duplicate", "turn_id": turn, "result": "该请求已接收,不会重复执行。"} + private = x25519.X25519PrivateKey.generate() + future = asyncio.get_running_loop().create_future() + self.pending[turn] = Pending(future, private, public, host, session) + envelope = seal({"type": "instruction", "turn_id": turn, "owner": owner, + "text": text, "read_only": True, "channel": "im", + "reply_key": raw_public(private.public_key())}, proof["kx"], self.signer, + host_id=host, session_id=session) + key = f"hn:inflight:{host}:{turn}" + try: + await self.r.set(key, session, ex=self.s.request_timeout + 5) + count = await self.r.publish("hn:down:" + host, json.dumps({"type": "instruction", "envelope": envelope})) + if not count: + self.store.finish(turn, "offline") + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线。"} + result = await asyncio.wait_for(future, timeout=self.s.request_timeout) + self.store.finish(turn, "completed") + self.store.audit(owner, "instruction", host, "completed") + return result + except asyncio.TimeoutError: + self.store.finish(turn, "unknown") + return {"status": "unknown", "turn_id": turn, + "result": "等待结果超时,执行状态未知;不要直接重发有副作用的任务。"} + finally: + self.pending.pop(turn, None) + await self.r.delete(key) + + +def create_app(settings=None, redis=None, platform=None): + s = settings or Settings() + r = redis or Redis.from_url(s.redis_url, decode_responses=True) + store = Store(s.data_dir / "metadata.sqlite", s.database_url) + f = platform or Feishu(s) + broker = Broker(s, r, store) + oauth = OAuth(s, r, f) + background = set() + + def spawn(coro): + task = asyncio.create_task(coro) + background.add(task) + task.add_done_callback(background.discard) + # Retrieve errors without displaying possibly sensitive exception text. + task.add_done_callback(lambda t: t.exception() if not t.cancelled() else None) + + @asynccontextmanager + async def lifespan(app): + await r.ping() + for row in store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall(): + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + pub = r.pubsub() + await pub.subscribe("hn:replies") + reply_task = asyncio.create_task(broker.replies(pub)) + app.state.reply_task = reply_task + async def sweep(): + while True: + await asyncio.sleep(30) + async with oauth.lock: + oauth.prune() + sweep_task = asyncio.create_task(sweep()) + try: + yield + finally: + for task in [reply_task, sweep_task, *background]: + task.cancel() + await asyncio.gather(reply_task, sweep_task, *background, return_exceptions=True) + await pub.aclose() + await f.close() + store.close() + if redis is None: + await r.aclose() + + app = FastAPI(title="Hainuo B1 connector", lifespan=lifespan, docs_url=None, redoc_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=s.max_body) + app.include_router(oauth.router) + from .web_session import WebSessions + web_sessions = WebSessions(s, r, store, broker, f) + app.include_router(web_sessions.router) + app.state.store, app.state.broker, app.state.oauth = store, broker, oauth + app.state.platform, app.state.redis = f, r + + @app.exception_handler(RequestValidationError) + async def validation_error(request, exc): + # FastAPI's default error response includes invalid input values (possibly tokens). + return JSONResponse({"detail": "invalid request parameters"}, status_code=422) + + @app.middleware("http") + async def secure_response(request, call_next): + return no_cache(await call_next(request)) + + def admin(request): + require_secret(request.headers.get("X-Admin-Secret"), s.admin_secret) + + from .enrollment import enrollment_router + app.include_router(enrollment_router(s, r, store, broker.signer, admin)) + + @app.get("/health") + async def health(): + return {"status": "alive", "role": "connector", "version": "0.1.0"} + + @app.get("/ready") + async def ready(): + await r.ping() + store.db.execute("SELECT 1") + if not getattr(app.state, "reply_task", None) or app.state.reply_task.done(): + raise HTTPException(503, "reply subscriber unavailable") + return {"status": "ready"} + + @app.post("/admin/devices", status_code=201) + async def register(body: Register, request: Request): + admin(request) + try: + verify_device(body.cert, s.ca_cert.read_bytes(), body.host_id) + except Exception: + raise HTTPException(400, "invalid device certificate") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= s.max_devices: + raise HTTPException(503, "device capacity reached") + try: + fp = fingerprint(body.cert) + store.add_device(body.host_id, body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "device already exists") from None + await r.set("hn:device:" + body.host_id, fp) + store.audit("admin", "register", body.host_id, "success") + return {"host_id": body.host_id, "fingerprint": fp} + + @app.get("/admin/devices") + async def devices(request: Request): + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,owner,revoked FROM devices ORDER BY host_id").fetchall() + return {"devices": [{**dict(row), "online": bool(await r.exists("hn:online:" + row["host_id"]))} for row in rows]} + + @app.post("/admin/devices/{host}/revoke") + async def revoke(host: str, request: Request): + admin(request) + if not store.device(host): + raise HTTPException(404, "device not found") + # Persistent source of truth first; router block + disconnect follows. + store.revoke(host) + await r.incr("hn:web-generation:" + host) + await r.set("hn:revoked:" + host, "1") + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "revoke") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "revoked", "result": "设备已停用,结果已丢弃。"}) + store.audit("admin", "revoke", host, "success") + return {"status": "revoked"} + + @app.post("/admin/reconcile") + async def reconcile(request: Request): + """Rebuild router admission metadata after Redis recovery; no dialogue data.""" + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall() + for row in rows: + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + return {"devices": len(rows)} + + @app.post("/admin/devices/{host}/unbind") + async def admin_unbind(host: str, request: Request): + admin(request) + device = store.device(host) + if not device: + raise HTTPException(404, "device not found") + if device["owner"]: + store.unbind(device["owner"]) + await r.incr("hn:web-generation:" + host) + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "unbind") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + store.audit("admin", "unbind", host, "success") + return {"status": "unbound", "host_id": host, "revoked": bool(device["revoked"])} + + async def make_binding_code(owner): + if store.by_owner(owner): + return "你已绑定电脑,请先发送 /解绑。" + await rate_limit(r, "binding:" + owner, 5) + code = secrets.token_urlsafe(12) + await r.set("hn:bind:" + hashlib.sha256(code.encode()).hexdigest(), owner, ex=300) + return "请在本人电脑的桥接客户端输入绑定码(5分钟有效):" + code + + @app.post("/device/bind") + async def bind(body: Bind, request: Request): + await rate_limit(r, "bind:" + peer(request), 20) + device = store.device(body.host_id) + if not device or device["revoked"]: + raise HTTPException(404, "device unavailable") + try: + public = verify_device(device["cert"], s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, public) + if proof["host_id"] != body.host_id or abs(time.time() - proof["ts"]) > 90: + raise ValueError("invalid proof") + code = proof["code"] + if not isinstance(code, str) or len(code) > 128: + raise ValueError("invalid code") + except Exception: + raise HTTPException(403, "invalid device proof") from None + key = "hn:bind:" + hashlib.sha256(code.encode()).hexdigest() + owner = await r.get(key) + if not owner: + raise HTTPException(404, "binding code expired or consumed") + # GETDEL is atomic across connectors. Invalid device proofs never reach this point. + owner = await r.getdel(key) + if not owner: + raise HTTPException(404, "binding code consumed") + try: + store.bind(body.host_id, owner) + except (*INTEGRITY_ERRORS, ValueError): + raise HTTPException(409, "owner/device already bound; request new code") from None + await r.incr("hn:web-generation:" + body.host_id) + store.audit(owner, "bind", body.host_id, "success") + return sign({"host_id": body.host_id, "owner": owner, "ts": int(time.time())}, broker.signer) + + @app.post("/admin/test/binding-code") + async def test_code(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return {"message": await make_binding_code(body.open_id)} + + @app.post("/admin/test/dispatch") + async def test_dispatch(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return await broker.dispatch(body.open_id, body.text, body.turn_id) + + async def handle_event(data): + header, event = data.get("header", {}), data.get("event", {}) + if header.get("event_type") != "im.message.receive_v1": + return + message, sender = event.get("message", {}), event.get("sender", {}) + owner = sender.get("sender_id", {}).get("open_id") + message_id = message.get("message_id", "") + if (message.get("chat_type") != "p2p" or message.get("message_type") != "text" + or sender.get("sender_type") != "user" or not owner + or not re.fullmatch(r"[A-Za-z0-9_-]{1,128}", message_id)): + return + if header.get("app_id") != s.feishu_app_id: + return + try: + text = json.loads(message["content"])["text"].strip() + if not text or len(text) > 8000: + return + # Persistent receipt BEFORE any side effect. Restarts do not execute a delivery twice. + event_turn = "event_" + hashlib.sha256(message_id.encode()).hexdigest() + if not store.begin_turn(event_turn, owner, "event"): + return + if text == "/绑定": + result = await make_binding_code(owner) + elif text == "/解绑": + old = store.by_owner(owner) + store.unbind(owner) + if old: + await r.incr("hn:web-generation:" + old["host_id"]) + await r.publish("hn:kill:" + old["host_id"], "unbind") + for item in broker.pending.values(): + if item.host == old["host_id"] and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + result = "已解绑;重新绑定后请更新本机绑定信息。" + elif text == "/主机名" or text.startswith("/回显 "): + result = (await broker.dispatch(owner, text, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + else: + instruction = text if text.startswith("/助手 ") else "/助手 " + text + result = (await broker.dispatch(owner, instruction, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + await f.reply(message_id, result[:s.max_result]) + store.finish(event_turn, "replied") + except Exception: + store.audit(owner, "event", message_id, "failed_or_unknown") + + @app.post("/internal/events") + async def internal_event(request: Request): + require_secret(request.headers.get("X-Internal-Secret"), s.internal_secret) + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(await request.json())) + return {"accepted": True} + + @app.post("/feishu/event") + async def webhook(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if not s.feishu_encrypt_key or not s.feishu_verification_token: + raise HTTPException(503, "webhook credentials not configured; use selected event source") + await rate_limit(r, "webhook:" + peer(request), 600) + raw = await request.body() + timestamp, nonce, signature = (request.headers.get(x, "") for x in + ("X-Lark-Request-Timestamp", "X-Lark-Request-Nonce", "X-Lark-Signature")) + try: + if abs(time.time() - int(timestamp)) > 300 or not nonce or len(nonce) > 128: + raise ValueError("stale request") + expected = hashlib.sha256((timestamp + nonce + s.feishu_encrypt_key).encode() + raw).hexdigest() + if not hmac.compare_digest(expected, signature): + raise ValueError("bad signature") + data = json.loads(raw) + if "encrypt" in data: + cipher = base64.b64decode(data["encrypt"], validate=True) + decryptor = Cipher(algorithms.AES(hashlib.sha256(s.feishu_encrypt_key.encode()).digest()), + modes.CBC(cipher[:16])).decryptor() + padded = decryptor.update(cipher[16:]) + decryptor.finalize() + unpadder = padding.PKCS7(128).unpadder() + data = json.loads(unpadder.update(padded) + unpadder.finalize()) + token = data.get("token", data.get("header", {}).get("token", "")) + if not hmac.compare_digest(token, s.feishu_verification_token): + raise ValueError("invalid verification token") + except Exception: + raise HTTPException(403, "invalid event authentication") from None + if data.get("type") == "url_verification": + return {"challenge": data.get("challenge", "")} + # Same delivery is ACKed again; durable message_id receipt handles execution deduplication. + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(data)) + return {"code": 0} + + @app.get("/api/whoami") + async def whoami(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(r, "whoami:" + peer(request)) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await f.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "no device bound") + if not s.device_domain or not re.fullmatch(r"[a-zA-Z0-9.-]+", s.device_domain): + raise HTTPException(503, "device web domain not configured") + return {"host_id": device["host_id"], "url": f"https://h-{device['host_id']}.{s.device_domain}/", + "online": bool(await r.exists("hn:online:" + device["host_id"])), + "device_login_required": True} + + @app.get("/h5", response_class=HTMLResponse) + async def h5(): + from importlib.resources import files + return HTMLResponse(files("relay").joinpath("web/index.html").read_text(encoding="utf-8"), + headers={"Content-Security-Policy": "default-src 'self'; script-src 'self'; style-src 'self'; frame-ancestors 'self'; base-uri 'none'; form-action 'self'"}) + + @app.get("/assets/{name}") + async def asset(name: str): + from importlib.resources import files + from starlette.responses import Response + if name not in {"app.js", "style.css"}: + raise HTTPException(404, "not found") + return Response(files("relay").joinpath("web/" + name).read_bytes(), + media_type="text/javascript" if name.endswith(".js") else "text/css") + + @app.get("/proxy") + async def proxy(request: Request): + if not s.proxy_enabled or not s.feishu_enabled: + raise HTTPException(404, "not found") + auth = request.headers.get("Authorization", "") + target = request.headers.get("X-Relay-Target", "") + decoded = unquote(target) + parts = urlsplit(target) + allowed = s.proxy_paths.split(",") + if (not auth.startswith("Bearer ") or len(auth) > 8192 or len(target) > 4096 + or parts.scheme or parts.netloc or parts.fragment or target.startswith("//") + or any(x in decoded for x in ("..", "\\", "\r", "\n", "#", "%")) + or not any(parts.path == x or parts.path.startswith(x + "/") for x in allowed)): + raise HTTPException(403, "target not allowed") + try: + info = await f.user_info(auth[7:]) + await rate_limit(r, "proxy:" + info["open_id"], 60) + # Buffer bounded response; no disk/cache, redirects never followed. + async with f.http.stream("GET", target, headers={"Authorization": auth}) as response: + if response.status_code in {301, 302, 303, 307, 308}: + raise HTTPException(502, "upstream redirect refused") + chunks, size = [], 0 + async for chunk in response.aiter_bytes(): + size += len(chunk) + if size > 2 * 1024 * 1024: + raise HTTPException(502, "response too large") + chunks.append(chunk) + from starlette.responses import Response + store.audit(info["open_id"], "proxy", parts.path, str(response.status_code)) + return Response(b"".join(chunks), status_code=response.status_code, + media_type=response.headers.get("content-type", "application/json")) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + + return app diff --git a/patch-source/51c062dff81e268e/relay/crypto.py b/patch-source/51c062dff81e268e/relay/crypto.py new file mode 100644 index 0000000..dd962e5 --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/crypto.py @@ -0,0 +1,140 @@ +"""Signed ephemeral X25519 envelopes. The router never receives plaintext keys.""" +import base64 +import hashlib +import json +import secrets +import time +from datetime import datetime, timedelta, timezone + +from cryptography import x509 +from cryptography.hazmat.primitives import hashes, serialization +from cryptography.hazmat.primitives.asymmetric import ed25519, x25519 +from cryptography.hazmat.primitives.ciphers.aead import AESGCM +from cryptography.hazmat.primitives.kdf.hkdf import HKDF +from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID + + +def b64(data: bytes) -> str: + return base64.urlsafe_b64encode(data).decode() + + +def unb64(data: str) -> bytes: + return base64.b64decode(data.encode(), altchars=b"-_", validate=True) + + +def canonical(data: dict) -> bytes: + return json.dumps(data, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode() + + +def raw_public(key) -> str: + return b64(key.public_bytes(serialization.Encoding.Raw, serialization.PublicFormat.Raw)) + + +def pem_private(key) -> bytes: + return key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, + serialization.NoEncryption()) + + +def load_private(path): + protected = path.with_name(path.name + ".dpapi") + if protected.is_file(): + from .windows_keys import unprotect + data = unprotect(protected.read_bytes()) + else: + data = path.read_bytes() + return serialization.load_pem_private_key(data, password=None) + + +def load_public(path): + return serialization.load_pem_public_key(path.read_bytes()) + + +def fingerprint(pem: str) -> str: + return x509.load_pem_x509_certificate(pem.encode()).fingerprint(hashes.SHA256()).hex() + + +def verify_device(pem: str, ca_pem: bytes, host_id: str): + """v1 deliberately accepts only directly issued Ed25519 device certificates.""" + cert = x509.load_pem_x509_certificate(pem.encode()) + ca = x509.load_pem_x509_certificate(ca_pem) + now = datetime.now(timezone.utc) + if not (ca.not_valid_before_utc <= now <= ca.not_valid_after_utc): + raise ValueError("expired CA") + if not ca.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("not a CA") + cert.verify_directly_issued_by(ca) + if not (cert.not_valid_before_utc <= now <= cert.not_valid_after_utc): + raise ValueError("expired certificate") + if cert.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("CA cannot be used as device") + if ExtendedKeyUsageOID.CLIENT_AUTH not in cert.extensions.get_extension_for_class(x509.ExtendedKeyUsage).value: + raise ValueError("clientAuth missing") + names = cert.extensions.get_extension_for_class(x509.SubjectAlternativeName).value + if names.get_values_for_type(x509.UniformResourceIdentifier) != [f"urn:hainuo:host:{host_id}"]: + raise ValueError("host identity mismatch") + public = cert.public_key() + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("v1 requires Ed25519 device key") + return public + + +def sign(data: dict, key) -> dict: + return {**data, "signature": b64(key.sign(canonical(data)))} + + +def verified(data: dict, key) -> dict: + body = {k: v for k, v in data.items() if k != "signature"} + key.verify(unb64(data["signature"]), canonical(body)) + return body + + +def seal(payload: dict, recipient_public: str, signer, *, host_id: str, session_id: str) -> dict: + ephemeral = x25519.X25519PrivateKey.generate() + header = {"v": 1, "host_id": host_id, "session_id": session_id, + "ts": int(time.time()), "nonce": b64(secrets.token_bytes(12)), + "ephemeral": raw_public(ephemeral.public_key())} + shared = ephemeral.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(recipient_public))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + cipher = AESGCM(key).encrypt(unb64(header["nonce"]), canonical(payload), canonical(header)) + return sign({**header, "ciphertext": b64(cipher)}, signer) + + +def open_envelope(envelope: dict, recipient_private, sender_public, *, host_id: str, session_id: str) -> dict: + body = verified(envelope, sender_public) + if body["v"] != 1 or body["host_id"] != host_id or body["session_id"] != session_id: + raise ValueError("wrong destination/session") + if abs(time.time() - body["ts"]) > 90: + raise ValueError("expired envelope") + header = {k: v for k, v in body.items() if k != "ciphertext"} + shared = recipient_private.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(body["ephemeral"]))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + plain = AESGCM(key).decrypt(unb64(body["nonce"]), unb64(body["ciphertext"]), canonical(header)) + return json.loads(plain) + + +def issue_ca(): + key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Hainuo device test CA")]) + now = datetime.now(timezone.utc) + cert = (x509.CertificateBuilder().subject_name(name).issuer_name(name) + .public_key(key.public_key()).serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=3650)) + .add_extension(x509.BasicConstraints(ca=True, path_length=0), critical=True) + .add_extension(x509.KeyUsage(False, False, False, False, False, True, True, False, False), critical=True) + .sign(key, algorithm=None)) + return key, cert + + +def issue_device(ca_key, ca_cert, host_id: str, public_key, days=30): + now = datetime.now(timezone.utc) + return (x509.CertificateBuilder() + .subject_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, host_id)])) + .issuer_name(ca_cert.subject).public_key(public_key) + .serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=days)) + .add_extension(x509.BasicConstraints(ca=False, path_length=None), critical=True) + .add_extension(x509.SubjectAlternativeName([x509.UniformResourceIdentifier(f"urn:hainuo:host:{host_id}")]), critical=False) + .add_extension(x509.ExtendedKeyUsage([ExtendedKeyUsageOID.CLIENT_AUTH]), critical=False) + .sign(ca_key, algorithm=None)) diff --git a/patch-source/51c062dff81e268e/relay/device.py b/patch-source/51c062dff81e268e/relay/device.py new file mode 100644 index 0000000..a8fe010 --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/device.py @@ -0,0 +1,216 @@ +"""Reference PC bridge. No shell/PowerShell execution; local read-only diagnostics.""" +import asyncio +import contextlib +import json +import os +import secrets +import socket +import sqlite3 +import time +from pathlib import Path +from urllib.parse import urlsplit + +import httpx +from cryptography.hazmat.primitives.asymmetric import x25519 +from websockets.asyncio.client import connect + +from .crypto import (load_private, load_public, open_envelope, raw_public, seal, sign, + verified, verify_device) + + +class DeviceJournal: + def __init__(self, path): + path.parent.mkdir(parents=True, exist_ok=True) + self.db = sqlite3.connect(path) + self.db.execute("CREATE TABLE IF NOT EXISTS consumed(turn_id TEXT PRIMARY KEY, ts REAL NOT NULL)") + self.db.commit() + + def consume(self, turn): + with self.db: + return self.db.execute("INSERT OR IGNORE INTO consumed VALUES(?,?)", (turn, time.time())).rowcount == 1 + + def close(self): + self.db.close() + + +async def execute_readonly(text, root: Path | None): + if text in {"/主机名", "hostname"}: + return socket.gethostname() + if text.startswith("/回显 "): + return text[4:][:16000] + if text.startswith("/读取 "): + if root is None: + return "未配置测试文件目录。" + base = root.resolve(strict=True) + try: + path = (base / text[4:].strip()).resolve(strict=True) + if path == base or not path.is_relative_to(base) or not path.is_file(): + return "拒绝:文件不在允许的测试目录。" + if path.stat().st_size > 32768: + return "拒绝:测试文件超过32KB。" + return path.read_text(encoding="utf-8")[:16000] + except (OSError, UnicodeError, ValueError): + return "拒绝:文件不存在、不可读或不是UTF-8文本。" + return "当前桥接客户端仅开放只读诊断:/主机名、/回显 内容、/读取 相对路径;尚未接入海诺本地Agent接口。" + + +class LocalAgent: + """Explicit adapter contract, not a claim of compatibility with installer 1.065.""" + def __init__(self, url, transport=None): + parts = urlsplit(url) + if parts.scheme != "http" or parts.hostname not in {"127.0.0.1", "::1"} or parts.username or parts.password or parts.fragment: + raise ValueError("local Agent must use a fixed loopback HTTP endpoint") + self.url = url + self.http = httpx.AsyncClient(timeout=30, follow_redirects=False, transport=transport, trust_env=False) + + async def execute(self, text, owner, turn): + headers = {} + if os.environ.get("HN_LOCAL_AGENT_TOKEN"): + headers["Authorization"] = "Bearer " + os.environ["HN_LOCAL_AGENT_TOKEN"] + try: + async with self.http.stream("POST", self.url, headers=headers, + json={"text": text, "open_id": owner, "turn_id": turn, + "channel": "im", "read_only": True}) as response: + if response.status_code != 200: + return "本机Agent未返回成功结果,请查看本机服务状态。" + body = b"" + async for chunk in response.aiter_bytes(): + body += chunk + if len(body) > 65536: + return "本机Agent结果超过限制。" + result = json.loads(body)["result"] + if not isinstance(result, str): + return "本机Agent响应格式不匹配。" + return result[:16000] + except (httpx.HTTPError, ValueError, KeyError): + return "本机Agent连接失败或响应格式不匹配。" + + async def close(self): + await self.http.aclose() + + +async def bind_device(directory: Path, connector_url: str, code: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + private = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "code": code, "ts": int(time.time())}, private) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as http: + response = await http.post(connector_url.rstrip("/") + "/device/bind", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"binding failed: HTTP {response.status_code}") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt["host_id"] != config["host_id"]: + raise ValueError("wrong binding receipt") + config["owner"] = receipt["owner"] + temp = directory / "device.json.tmp" + temp.write_text(json.dumps(config, ensure_ascii=False, indent=2), encoding="utf-8") + temp.replace(directory / "device.json") + + +async def pair_browser(directory: Path, connector_url: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8-sig")) + if not config.get("owner"): + raise RuntimeError("device must be bound first") + key = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "owner": config["owner"], + "purpose": "h5-pair-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as client: + response = await client.post(connector_url.rstrip("/") + "/device/web-pair", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"browser pairing failed: HTTP {response.status_code}") + result = verified(response.json(), load_public(directory / "connector.pub")) + if result["host_id"] != config["host_id"]: + raise ValueError("wrong pairing receipt") + return result + + +async def run_device(directory: Path, router_url: str, **kwargs): + from .process_lock import DeviceProcessLock + with DeviceProcessLock(directory): + return await _run_device(directory, router_url, **kwargs) + + +async def _run_device(directory: Path, router_url: str, *, root=None, once=False, ssl_context=None, + stop_event=None, connected_event=None, agent_url=None, hainuo_config=None): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + if not config.get("owner"): + raise RuntimeError("bind device before starting") + host, owner = config["host_id"], config["owner"] + key = load_private(directory / "device.key") + connector = load_public(directory / "connector.pub") + cert = (directory / "device.pem").read_text(encoding="utf-8") + verify_device(cert, (directory / "ca.pem").read_bytes(), host) + journal = DeviceJournal(directory / "journal.sqlite") + if agent_url and hainuo_config: + raise ValueError("choose one Agent adapter") + if hainuo_config: + from .hainuo1065 import Hainuo1065Agent + agent = Hainuo1065Agent(hainuo_config) + else: + agent = LocalAgent(agent_url) if agent_url else None + stop_event = stop_event or asyncio.Event() + delay = 1 + try: + while not stop_event.is_set(): + try: + kwargs = {"ssl": ssl_context} if router_url.startswith("wss://") and ssl_context else {} + async with connect(router_url, max_size=262144, open_timeout=10, ping_interval=10, + proxy=None, **kwargs) as ws: + challenge = json.loads(await ws.recv())["challenge"] + ephemeral = x25519.X25519PrivateKey.generate() + session = secrets.token_hex(16) + proof = sign({"host_id": host, "session_id": session, "challenge": challenge, + "kx": raw_public(ephemeral.public_key()), "ts": int(time.time())}, key) + await ws.send(json.dumps({"host_id": host, "cert": cert, "proof": proof})) + accepted = json.loads(await ws.recv()) + if accepted.get("type") != "connected": + raise RuntimeError("device connection rejected") + print("设备连接成功,已绑定本人海能Work身份。", flush=True) + if connected_event: + connected_event.set() + delay = 1 + while not stop_event.is_set(): + try: + raw = await asyncio.wait_for(ws.recv(), timeout=1) + except asyncio.TimeoutError: + continue + data = json.loads(raw) + payload = open_envelope(data["envelope"], ephemeral, connector, + host_id=host, session_id=session) + if payload.get("owner") != owner or payload.get("type") != "instruction": + continue + turn = payload["turn_id"] + if not isinstance(turn, str) or len(turn) > 128: + continue + if not journal.consume(turn): + result = "该请求已处理,拒绝重复执行。" + elif payload.get("read_only") is not True or payload.get("channel") != "im": + result = "拒绝:通道或操作类型不受支持。" + else: + if agent and payload["text"].startswith("/助手 "): + result = await agent.execute(payload["text"][4:], owner, turn) + else: + result = await execute_readonly(payload["text"], root) + reply = seal({"type": "result", "turn_id": turn, "result": result}, + payload["reply_key"], key, host_id=host, session_id=session) + await ws.send(json.dumps({"type": "reply", "turn_id": turn, "envelope": reply})) + if once: + return + except asyncio.CancelledError: + raise + except Exception as error: + if once: + raise + from websockets.exceptions import ConnectionClosed + if isinstance(error, ConnectionClosed) and error.rcvd and error.rcvd.code == 1008: + print("设备被中继拒绝:请检查是否有另一桥接进程、设备准入或证书状态。", flush=True) + # No payloads/URLs/credentials in logs, bounded reconnect backoff + jitter. + print("设备连接中断,等待重连。", flush=True) + with contextlib.suppress(asyncio.TimeoutError): + await asyncio.wait_for(stop_event.wait(), timeout=delay + secrets.randbelow(1000) / 1000) + delay = min(delay * 2, 30) + finally: + journal.close() + if agent: + await agent.close() diff --git a/patch-source/51c062dff81e268e/relay/enrollment.py b/patch-source/51c062dff81e268e/relay/enrollment.py new file mode 100644 index 0000000..b4ac77f --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/enrollment.py @@ -0,0 +1,178 @@ +"""Device registration with key possession proof; Work binding is separate.""" +import json +import secrets +import time +import httpx + +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey +from fastapi import APIRouter, HTTPException, Request +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .crypto import verified, sign, raw_public, verify_device, fingerprint +from .web_session import digest +from fastapi.responses import JSONResponse + + +def validate_enrollment(body): + try: + key = serialization.load_pem_public_key(body.public_key.encode()) + if not isinstance(key, Ed25519PublicKey): + raise ValueError() + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["purpose"] != "device-enroll-v1" + or abs(time.time() - proof["ts"]) > 90 or len(proof["nonce"]) != 32): + raise ValueError() + return key + except Exception: + raise HTTPException(403, "invalid enrollment proof") from None + + +async def request_certificate(settings, body): + async with httpx.AsyncClient(trust_env=False, timeout=15, follow_redirects=False) as http: + response = await http.post(settings.enrollment_signer_url.rstrip("/") + "/issue", + headers={"X-Internal-Secret": settings.internal_secret}, + json=body.model_dump()) + response.raise_for_status() + return response.json()["cert"] + + +class EnrollmentRequest(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + public_key: str = Field(max_length=1024) + proof: dict + + +class EnrollmentApproval(BaseModel): + cert: str = Field(max_length=10000) + + +def enrollment_router(settings, redis, store, signer, admin): + router = APIRouter() + + @router.post("/device/register") + async def automatic_registration(body: EnrollmentRequest, request: Request): + # No human approval: eligibility follows intranet access plus Work app scope. + # Proof/TLS/limits protect identity and resources, and do not add user approval. + await rate_limit(redis, "auto-register-ip:" + peer(request), 120) + key = validate_enrollment(body) + await rate_limit(redis, "auto-register-host:" + body.host_id, 30) + existing = store.device(body.host_id) + + def check_existing(device): + if device["revoked"]: + raise HTTPException(403, "device revoked") + try: + registered = verify_device(device["cert"], settings.ca_cert.read_bytes(), body.host_id) + if raw_public(registered) != raw_public(key): + raise ValueError() + except Exception: + raise HTTPException(409, "existing device identity mismatch or certificate expired") from None + return device["cert"] + + if existing: + cert = check_existing(existing) + else: + if not settings.enrollment_signer_url: + raise HTTPException(503, "automatic registration is not enabled; update ECS first") + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= settings.max_devices: + raise HTTPException(503, "device capacity reached") + await rate_limit(redis, "auto-register-total", settings.max_pending, 3600) + try: + cert = await request_certificate(settings, body) + issued = verify_device(cert, settings.ca_cert.read_bytes(), body.host_id) + if raw_public(issued) != raw_public(key): + raise ValueError() + except Exception: + raise HTTPException(503, "certificate issuer unavailable or invalid response") from None + from .store import INTEGRITY_ERRORS + try: + store.add_device(body.host_id, cert, fingerprint(cert)) + except INTEGRITY_ERRORS: + # A retry/concurrent request must return the original certificate. + cert = check_existing(store.device(body.host_id)) + store.audit("device", "automatic_registration", body.host_id, "success") + await redis.set("hn:device:" + body.host_id, fingerprint(cert)) + return no_cache(JSONResponse(sign({"status": "registered", "host_id": body.host_id, + "cert": cert}, signer), status_code=201 if not existing else 200)) + + @router.post("/device/enroll") + async def request_enrollment(body: EnrollmentRequest, request: Request): + await rate_limit(redis, "enroll-ip:" + peer(request), 10) + validate_enrollment(body) + await rate_limit(redis, "enroll-host:" + body.host_id, 3, 3600) + if store.device(body.host_id): + raise HTTPException(409, "device already registered; recover its original certificate") + # Global rolling capacity bound; prevents anonymous request-table exhaustion. + await rate_limit(redis, "enroll-global", settings.max_pending, 1800) + identifier, claim_secret = secrets.token_hex(16), secrets.token_urlsafe(32) + record = {"host_id": body.host_id, "public_key": body.public_key, + "claim_hash": digest(claim_secret), "created": time.time(), "status": "pending"} + await redis.set("hn:enroll:" + identifier, json.dumps(record), ex=1800) + return no_cache(JSONResponse(sign({"request_id": identifier, "claim_secret": claim_secret, + "host_id": body.host_id, "expires_in": 1800}, signer), status_code=201)) + + @router.get("/device/enroll/{identifier}") + async def claim(identifier: str, request: Request): + await rate_limit(redis, "enroll-poll:" + peer(request), 60) + data = await redis.get("hn:enroll:" + identifier) + if not data: + raise HTTPException(404, "request expired") + record = json.loads(data) + import hmac + if not hmac.compare_digest(record["claim_hash"], digest(request.headers.get("X-Enrollment-Secret", ""))): + raise HTTPException(404, "request unavailable") + if record["status"] == "pending": + return no_cache(JSONResponse({"status": "pending"}, status_code=202)) + device = store.device(record["host_id"]) + if not device or device["revoked"]: + raise HTTPException(403, "device unavailable") + # Recoverable for its original claimant for the request TTL; public certificate only. + return no_cache(JSONResponse(sign({"status": "approved", "host_id": record["host_id"], + "request_id": identifier, "cert": device["cert"]}, signer))) + + @router.get("/admin/enrollments/{identifier}") + async def inspect(identifier: str, request: Request): + admin(request) + encoded = await redis.get("hn:enroll:" + identifier) + if not encoded: + raise HTTPException(404, "request expired") + record = json.loads(encoded) + return {k: v for k, v in record.items() if k != "claim_hash"} + + @router.post("/admin/enrollments/{identifier}/approve") + async def approve(identifier: str, body: EnrollmentApproval, request: Request): + admin(request) + encoded = await redis.get("hn:enroll:" + identifier) + if not encoded: + raise HTTPException(404, "request expired") + record = json.loads(encoded) + try: + public = verify_device(body.cert, settings.ca_cert.read_bytes(), record["host_id"]) + requested = serialization.load_pem_public_key(record["public_key"].encode()) + if raw_public(public) != raw_public(requested): + raise ValueError() + except Exception: + raise HTTPException(400, "certificate does not match enrollment") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= settings.max_devices: + raise HTTPException(503, "device capacity reached") + fp = fingerprint(body.cert) + existing = store.device(record["host_id"]) + if existing and (existing["revoked"] or existing["fingerprint"] != fp): + raise HTTPException(409, "device conflict") + if not existing: + from .store import INTEGRITY_ERRORS + try: + store.add_device(record["host_id"], body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "concurrent device registration") from None + await redis.set("hn:device:" + record["host_id"], fp) + record["status"] = "approved" + remaining = await redis.ttl("hn:enroll:" + identifier) + if remaining < 1: + raise HTTPException(409, "request expired during approval") + await redis.set("hn:enroll:" + identifier, json.dumps(record), ex=remaining) + store.audit("admin", "enrollment_approved", record["host_id"], "success") + return {"status": "approved", "host_id": record["host_id"]} + + return router diff --git a/patch-source/51c062dff81e268e/relay/enrollment_signer.py b/patch-source/51c062dff81e268e/relay/enrollment_signer.py new file mode 100644 index 0000000..6089d02 --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/enrollment_signer.py @@ -0,0 +1,43 @@ +"""Internal automatic CA issuer. No published host port, no application credentials.""" +import os +from pathlib import Path + +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from fastapi import FastAPI, Request +from .common import BodyLimitMiddleware, require_secret +from .crypto import load_private, issue_device, raw_public, verify_device +from .enrollment import EnrollmentRequest, validate_enrollment + + +def create_app(*, ca_key_path=None, ca_cert_path=None, secret=None): + key = load_private(Path(ca_key_path or os.environ.get("HN_ISSUER_CA_KEY", "/issuer/ca.key"))) + ca_bytes = Path(ca_cert_path or os.environ.get("HN_ISSUER_CA_CERT", "/trust/ca.pem")).read_bytes() + ca = x509.load_pem_x509_certificate(ca_bytes) + if raw_public(key.public_key()) != raw_public(ca.public_key()): + raise ValueError("issuer CA key mismatch") + token = secret or os.environ.get("HN_INTERNAL_SECRET", "") + if len(token) < 32: + raise ValueError("issuer requires internal credential") + app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=16384) + + @app.get("/ready") + async def ready(): + return {"status": "ready"} + + @app.post("/issue") + async def issue(body: EnrollmentRequest, request: Request): + require_secret(request.headers.get("X-Internal-Secret"), token) + public = validate_enrollment(body) + cert = issue_device(key, ca, body.host_id, public).public_bytes(serialization.Encoding.PEM).decode() + verify_device(cert, ca_bytes, body.host_id) + return {"cert": cert} + + return app + + +if __name__ == "__main__": + import uvicorn + uvicorn.run("relay.enrollment_signer:create_app", factory=True, host="0.0.0.0", port=8000, + workers=1, access_log=False, proxy_headers=False, log_level="warning") diff --git a/patch-source/51c062dff81e268e/relay/hainuo1065.py b/patch-source/51c062dff81e268e/relay/hainuo1065.py new file mode 100644 index 0000000..6f7735b --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/hainuo1065.py @@ -0,0 +1,107 @@ +"""Adapter for the native API actually found in installer 1.065. + +Loopback only, independent API key, no platform secrets, no redirects or retries. +The included native launcher offers only text inference with no tools. +""" +import hashlib +import json +import re +from pathlib import Path +from urllib.parse import urlsplit + +import httpx + + +class NativeAPIError(RuntimeError): + """Contains a sanitized status only, never response body or credentials.""" + + +def read_native_config(path): + path = Path(path) + data = json.loads(path.read_text(encoding="utf-8-sig")) + url = data.get("url", "") + parts = urlsplit(url) + if (parts.scheme != "http" or parts.hostname != "127.0.0.1" or not parts.port + or parts.username or parts.password or parts.query or parts.fragment + or parts.path not in {"", "/"}): + raise ValueError("native API must be a fixed IPv4 loopback HTTP base URL") + if not isinstance(data.get("api_key"), str) or len(data["api_key"]) < 32: + raise ValueError("native API requires a dedicated key of at least 32 characters") + if data.get("mode") not in {"text-only", "profile-tools"}: + raise ValueError("unsupported native API mode") + data["url"] = url.rstrip("/") + return data + + +class Hainuo1065Agent: + def __init__(self, config_path, transport=None): + self.config = read_native_config(config_path) + self.http = httpx.AsyncClient(base_url=self.config["url"], timeout=120, + follow_redirects=False, trust_env=False, transport=transport) + self.headers = {"Authorization": "Bearer " + self.config["api_key"]} + + async def _json(self, method, path, **kwargs): + try: + async with self.http.stream(method, path, headers={**self.headers, **kwargs.pop("headers", {})}, **kwargs) as response: + if response.status_code != 200: + raise NativeAPIError(f"native API HTTP {response.status_code}") + body = bytearray() + async for chunk in response.aiter_bytes(): + body.extend(chunk) + if len(body) > 262144: + raise NativeAPIError("native API response exceeds limit") + data = json.loads(body) + if not isinstance(data, dict): + raise NativeAPIError("native API response is not an object") + return data + except (httpx.HTTPError, ValueError): + raise NativeAPIError("native API connection/JSON failure") from None + + async def probe(self): + health = await self._json("GET", "/health") + if health.get("platform") != "hermes-agent" or health.get("status") != "ok": + raise NativeAPIError("endpoint is not the native Hermes API") + capabilities = await self._json("GET", "/v1/capabilities") + policy = capabilities.get("hainuo_relay_policy", {}) + # A stock unrestricted API endpoint is deliberately not sufficient. + expected_mode = self.config["mode"] + expected_tools = "profile" if expected_mode == "profile-tools" else "disabled" + if policy != {"mode": expected_mode, "tools": expected_tools, "contract": "hainuo-1065-v1"}: + raise NativeAPIError("native API policy missing; start the supplied restricted launcher") + models = await self._json("GET", "/v1/models") + model = self.config.get("model") + available = models.get("data") + if not isinstance(available, list) or not available or not all(isinstance(x, dict) and isinstance(x.get("id"), str) for x in available): + raise NativeAPIError("native model list invalid") + if model and model not in {x["id"] for x in available}: + raise NativeAPIError("configured model alias unavailable") + self.model = model or available[0]["id"] + return {"status": "ready", "platform": "hermes-agent", "model": self.model, + "mode": expected_mode, "version": health.get("version", "unknown")} + + async def execute(self, text, owner, turn): + try: + # Recheck the actual local service before each inference, including reconnects. + await self.probe() + digest = hashlib.sha256((owner + "\0" + turn).encode()).hexdigest() + data = await self._json("POST", "/v1/chat/completions", + headers={"Idempotency-Key": "hn-" + digest}, + json={"model": self.model, "stream": False, + "messages": [{"role": "user", "content": text}]}) + choices = data.get("choices") + if not isinstance(choices, list) or not choices or not isinstance(choices[0], dict): + raise NativeAPIError("native completion choices missing") + choice = choices[0] + message = choice.get("message", {}) + if (choice.get("finish_reason") != "stop" or message.get("tool_calls") + or not isinstance(message.get("content"), str) + or data.get("hermes", {}).get("failed") + or data.get("hermes", {}).get("partial")): + raise NativeAPIError("native completion incomplete or unexpected tool call") + return message["content"][:16000] + except NativeAPIError as error: + # Safe generated error strings only; never echo upstream model/API errors. + return "海诺原生接口未完成请求:" + str(error) + "。未自动重试。" + + async def close(self): + await self.http.aclose() diff --git a/patch-source/51c062dff81e268e/relay/longconn.py b/patch-source/51c062dff81e268e/relay/longconn.py new file mode 100644 index 0000000..5c12bbf --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/longconn.py @@ -0,0 +1,74 @@ +"""Optional official SDK event source, private platform domain explicitly configured.""" +import json +import logging +import os +import threading +import time + +import httpx +import redis +from .config import Settings + + +def main(): + import lark_oapi as lark + s = Settings(role="longconn") + if not s.feishu_enabled or s.feishu_event_source != "longconn": + raise RuntimeError("platform is disabled") + # The SDK may log websocket URLs, tokens or event bodies. Disable its logging entirely. + logging.disable(logging.CRITICAL) + r = redis.Redis.from_url(s.redis_url) + lease = r.lock("hn:longconn:leader", timeout=30, thread_local=False) + if not lease.acquire(blocking=False): + raise RuntimeError("another long-connection consumer owns the lease") + url = os.environ.get("HN_CONNECTOR_INTERNAL_URL", "http://connector:8000") + client = httpx.Client(base_url=url, timeout=10, follow_redirects=False) + done = threading.Event() + + def maintain_lease(): + while not done.wait(8): + try: + lease.extend(30, replace_ttl=True) + except Exception: + # Fail closed. Continuing after lease loss could create two consumers. + os._exit(2) + + threading.Thread(target=maintain_lease, daemon=True).start() + + def received(event): + data = json.loads(lark.JSON.marshal(event)) + response = client.post("/internal/events", headers={"X-Internal-Secret": s.internal_secret}, json=data) + if response.status_code != 200: + raise RuntimeError("connector did not accept event") + print(json.dumps({"event": "platform_event_accepted", "type": data.get("header", {}).get("event_type")}), flush=True) + + handler = lark.EventDispatcherHandler.builder("", "").register_p2_im_message_receive_v1(received).build() + class ObservableClient(lark.ws.Client): + async def _connect(self): + try: + await super()._connect() + if self._conn is not None: + print(json.dumps({"event": "platform_connected", "api_host": s.feishu_api_base}), flush=True) + except Exception as error: + print(json.dumps({"event": "platform_connect_failed", "error_type": type(error).__name__}), flush=True) + raise + + try: + # domain is an actual supported SDK argument; no public-cloud-domain monkey patch. + ObservableClient(s.feishu_app_id, s.feishu_app_secret, event_handler=handler, + domain=s.feishu_api_base.rstrip("/"), log_level=lark.LogLevel.ERROR).start() + finally: + done.set() + client.close() + try: + lease.release() + except redis.exceptions.LockError: + pass + + +if __name__ == "__main__": + try: + main() + except Exception as error: + print(json.dumps({"event": "platform_consumer_stopped", "error_type": type(error).__name__}), flush=True) + raise SystemExit(1) from None diff --git a/patch-source/51c062dff81e268e/relay/oauth.py b/patch-source/51c062dff81e268e/relay/oauth.py new file mode 100644 index 0000000..b04ce6e --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/oauth.py @@ -0,0 +1,127 @@ +import asyncio +import hashlib +import hmac +import secrets +import time +from dataclasses import dataclass +from urllib.parse import urlencode + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .platform import PlatformError + + +@dataclass +class Transaction: + secret_hash: str + expires: float + status: str = "waiting" + result: dict | None = None + + +class Empty(BaseModel): + pass + + +class Refresh(BaseModel): + refresh_token: str = Field(min_length=1, max_length=4096) + + +class OAuth: + """A single connector process owns transactions and tokens in memory only.""" + def __init__(self, settings, redis, platform): + self.settings, self.redis, self.platform = settings, redis, platform + self.pending: dict[str, Transaction] = {} + self.lock = asyncio.Lock() + self.router = APIRouter() + self.router.add_api_route("/oauth/start", self.start, methods=["POST"], status_code=201) + self.router.add_api_route("/oauth/cb", self.callback, methods=["GET"]) + self.router.add_api_route("/oauth/claim", self.claim, methods=["GET"]) + self.router.add_api_route("/oauth/pending", self.status, methods=["GET"]) + self.router.add_api_route("/oauth/refresh", self.refresh, methods=["POST"]) + + def prune(self): + now = time.monotonic() + for state in list(self.pending): + if self.pending[state].expires <= now: + del self.pending[state] + + def authenticated(self, state, secret): + self.prune() + item = self.pending.get(state) + digest = hashlib.sha256((secret or "").encode()).hexdigest() + if not item or not hmac.compare_digest(item.secret_hash, digest): + raise HTTPException(404, "transaction not found") + return item + + async def start(self, body: Empty, request: Request): + if not self.settings.feishu_enabled or not self.settings.oauth_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.redis, "oauth:start:" + peer(request), 20) + async with self.lock: + self.prune() + if len(self.pending) >= self.settings.max_pending: + raise HTTPException(503, "too many pending transactions") + state, secret = secrets.token_urlsafe(32), secrets.token_urlsafe(32) + self.pending[state] = Transaction(hashlib.sha256(secret.encode()).hexdigest(), time.monotonic() + 600) + redirect = self.settings.public_url.rstrip("/") + "/oauth/cb" + url = self.settings.feishu_authorize_url + "?" + urlencode({"app_id": self.settings.feishu_app_id, + "redirect_uri": redirect, "state": state}) + return no_cache(JSONResponse({"state": state, "claim_secret": secret, "authorization_url": url, + "redirect_uri": redirect, "expires_in": 600, + "code_ttl": 60, "poll_interval": 2}, status_code=201)) + + async def callback(self, request: Request, state: str = "", code: str = ""): + await rate_limit(self.redis, "oauth:cb:" + peer(request)) + async with self.lock: + self.prune() + item = self.pending.get(state) + if not code or len(code) > 4096 or not item or item.status != "waiting": + raise HTTPException(400, "invalid callback") + item.status = "exchanging" + try: + result = await self.platform.exchange(code) + except PlatformError: + async with self.lock: + item.status = "failed" + raise HTTPException(502, "authorization exchange failed") from None + async with self.lock: + if state not in self.pending or item.expires <= time.monotonic(): + self.pending.pop(state, None) + raise HTTPException(400, "authorization expired") + item.result, item.status = result, "ready" + return no_cache(HTMLResponse("

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

")) + + async def claim(self, request: Request, state: str): + await rate_limit(self.redis, "oauth:claim:" + peer(request)) + async with self.lock: + item = self.authenticated(state, request.headers.get("X-OAuth-Claim-Secret")) + if item.status == "failed": + del self.pending[state] + raise HTTPException(502, "authorization failed") + if item.status != "ready": + return no_cache(JSONResponse({"status": "waiting", "retry_after": 2}, status_code=202)) + del self.pending[state] + return no_cache(JSONResponse({"status": "claimed", **item.result})) + + async def status(self, request: Request, state: str): + await rate_limit(self.redis, "oauth:pending:" + peer(request)) + async with self.lock: + item = self.authenticated(state, request.headers.get("X-OAuth-Claim-Secret")) + return no_cache(JSONResponse({"status": item.status})) + + async def refresh(self, body: Refresh, request: Request): + if not self.settings.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.redis, "oauth:refresh:" + peer(request), 20) + # Lock and consume before the upstream call; ambiguous failures never retry an old refresh token. + digest = hashlib.sha256(body.refresh_token.encode()).hexdigest() + if not await self.redis.set("hn:refresh:" + digest, "used", nx=True, ex=2592000): + raise HTTPException(409, "refresh already attempted; use returned token or reauthorize") + try: + result = await self.platform.refresh(body.refresh_token) + except PlatformError: + raise HTTPException(502, "refresh failed; reauthorize") from None + return no_cache(JSONResponse(result)) diff --git a/patch-source/51c062dff81e268e/relay/platform.py b/patch-source/51c062dff81e268e/relay/platform.py new file mode 100644 index 0000000..61c5193 --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/platform.py @@ -0,0 +1,89 @@ +import asyncio +import json +import time +import httpx + + +class PlatformError(Exception): + pass + + +class Feishu: + def __init__(self, settings, transport=None): + self.settings = settings + self.http = httpx.AsyncClient(base_url=settings.feishu_api_base.rstrip("/"), + timeout=15, follow_redirects=False, transport=transport) + self._token = None + self._expires = 0 + self._tenant_token = None + self._tenant_expires = 0 + self._lock = asyncio.Lock() + + async def close(self): + await self.http.aclose() + + async def _json(self, method, path, **kwargs): + try: + response = await self.http.request(method, path, **kwargs) + response.raise_for_status() + data = response.json() + if data.get("code", 0) != 0: + raise PlatformError("platform rejected request") + return data + except (httpx.HTTPError, ValueError, KeyError) as exc: + # Never propagate upstream bodies or credentials to caller/logs. + raise PlatformError("platform request failed") from None + + async def app_token(self): + async with self._lock: + if self._token and self._expires > time.monotonic() + 1800: + return self._token + data = await self._json("POST", "/open-apis/auth/v3/app_access_token/internal", + json={"app_id": self.settings.feishu_app_id, + "app_secret": self.settings.feishu_app_secret}) + self._token = data["app_access_token"] + self._expires = time.monotonic() + int(data.get("expire", 7200)) + return self._token + + async def exchange(self, code): + token = await self.app_token() + data = await self._json("POST", "/open-apis/authen/v1/oidc/access_token", + headers={"Authorization": f"Bearer {token}"}, + json={"grant_type": "authorization_code", "code": code}) + return self.normalize(data["data"]) + + async def tenant_token(self): + async with self._lock: + if self._tenant_token and self._tenant_expires > time.monotonic() + 1800: + return self._tenant_token + data = await self._json("POST", "/open-apis/auth/v3/tenant_access_token/internal", + json={"app_id": self.settings.feishu_app_id, + "app_secret": self.settings.feishu_app_secret}) + self._tenant_token = data["tenant_access_token"] + self._tenant_expires = time.monotonic() + int(data.get("expire", 7200)) + return self._tenant_token + + async def refresh(self, refresh_token): + token = await self.app_token() + data = await self._json("POST", "/open-apis/authen/v1/oidc/refresh_access_token", + headers={"Authorization": f"Bearer {token}"}, + json={"grant_type": "refresh_token", "refresh_token": refresh_token}) + return self.normalize(data["data"]) + + @staticmethod + def normalize(data): + return {"user_access_token": data["access_token"], "refresh_token": data.get("refresh_token", ""), + "expires_in": data.get("expires_in", data.get("expire", 0)), + "refresh_expires_in": data.get("refresh_expires_in", 0), "open_id": data["open_id"]} + + async def user_info(self, token): + data = await self._json("GET", "/open-apis/authen/v1/user_info", + headers={"Authorization": f"Bearer {token}"}) + return data["data"] + + async def reply(self, message_id, text): + # Reply to the original message only: no caller-controlled recipient. + token = await self.tenant_token() + await self._json("POST", f"/open-apis/im/v1/messages/{message_id}/reply", + headers={"Authorization": f"Bearer {token}"}, + json={"msg_type": "text", "content": json.dumps({"text": text}, ensure_ascii=False)}) diff --git a/patch-source/51c062dff81e268e/relay/process_lock.py b/patch-source/51c062dff81e268e/relay/process_lock.py new file mode 100644 index 0000000..c37da53 --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/process_lock.py @@ -0,0 +1,46 @@ +"""Per-device advisory process lock: same-user GUI and CLI cannot race.""" +import os +from pathlib import Path + + +class DeviceAlreadyRunning(RuntimeError): + pass + + +class DeviceProcessLock: + def __init__(self, directory): + self.path = Path(directory) / "bridge.lock" + self.file = None + + def __enter__(self): + self.path.parent.mkdir(parents=True, exist_ok=True) + self.file = self.path.open("a+b") + self.file.seek(0, 2) + if self.file.tell() == 0: + self.file.write(b"0") + self.file.flush() + self.file.seek(0) + try: + if os.name == "nt": + import msvcrt + msvcrt.locking(self.file.fileno(), msvcrt.LK_NBLCK, 1) + else: + import fcntl + fcntl.flock(self.file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) + except OSError: + self.file.close() + self.file = None + raise DeviceAlreadyRunning("该设备已有桥接进程运行,请勿重复启动。") from None + return self + + def __exit__(self, *args): + if self.file: + self.file.seek(0) + if os.name == "nt": + import msvcrt + msvcrt.locking(self.file.fileno(), msvcrt.LK_UNLCK, 1) + else: + import fcntl + fcntl.flock(self.file.fileno(), fcntl.LOCK_UN) + self.file.close() + self.file = None diff --git a/patch-source/51c062dff81e268e/relay/router.py b/patch-source/51c062dff81e268e/relay/router.py new file mode 100644 index 0000000..e8d91e1 --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/router.py @@ -0,0 +1,167 @@ +import asyncio +import contextlib +import json +import secrets +import time +from contextlib import asynccontextmanager + +from fastapi import FastAPI, WebSocket, WebSocketDisconnect +from redis.asyncio import Redis +from redis.exceptions import WatchError + +from .config import Settings +from .crypto import fingerprint, raw_public, verified, verify_device + + +async def compare_delete(redis, key, expected): + async with redis.pipeline() as pipe: + try: + await pipe.watch(key) + if await pipe.get(key) != expected: + return False + pipe.multi() + pipe.delete(key) + await pipe.execute() + return True + except WatchError: + return False + + +async def compare_expire(redis, key, expected, ttl): + async with redis.pipeline() as pipe: + try: + await pipe.watch(key) + if await pipe.get(key) != expected: + return False + pipe.multi() + pipe.expire(key, ttl) + await pipe.execute() + return True + except WatchError: + return False + + +def create_app(settings=None, redis=None): + s = settings or Settings(role="router") + r = redis or Redis.from_url(s.redis_url, decode_responses=True) + ca = s.ca_cert.read_bytes() + active = set() + + @asynccontextmanager + async def lifespan(app): + await r.ping() + yield + if redis is None: + await r.aclose() + + app = FastAPI(title="Hainuo B2 router", lifespan=lifespan, docs_url=None, redoc_url=None) + app.state.redis = r + + @app.get("/health") + async def health(): + return {"status": "alive", "role": "router"} + + @app.get("/ready") + async def ready(): + await r.ping() + return {"status": "ready", "connections": len(active)} + + @app.websocket("/device/ws") + async def device(ws: WebSocket): + from .common import rate_limit + from fastapi import HTTPException + await ws.accept() + if len(active) >= s.max_connections: + await ws.close(1013) + return + ip = ws.client.host if ws.client else "unknown" + try: + await rate_limit(r, "ws:" + ip, 30) + except HTTPException: + await ws.close(1008) + return + challenge = secrets.token_urlsafe(32) + host = session = presence = None + tasks = [] + pub = None + try: + await ws.send_json({"type": "challenge", "challenge": challenge}) + hello = await asyncio.wait_for(ws.receive_json(), timeout=10) + if len(json.dumps(hello)) > 20000: + raise ValueError("oversized hello") + host = hello["host_id"] + if not isinstance(host, str) or len(host) != 32 or any(c not in "0123456789abcdef" for c in host): + raise ValueError("invalid host") + public = verify_device(hello["cert"], ca, host) + proof = verified(hello["proof"], public) + if proof["challenge"] != challenge or proof["host_id"] != host or abs(time.time() - proof["ts"]) > 90: + raise ValueError("invalid proof") + if len(proof["kx"]) > 64 or len(proof["session_id"]) > 64: + raise ValueError("invalid session") + fp = fingerprint(hello["cert"]) + if await r.get("hn:device:" + host) != fp or await r.exists("hn:revoked:" + host): + raise ValueError("unregistered/revoked") + session = proof["session_id"] + presence = json.dumps({"cert": hello["cert"], "proof": hello["proof"], "challenge": challenge}) + if not await r.set("hn:online:" + host, presence, nx=True, ex=30): + raise ValueError("already connected") + active.add(session) + pub = r.pubsub() + await pub.subscribe("hn:down:" + host, "hn:kill:" + host) + await ws.send_json({"type": "connected", "session_id": session}) + + async def downstream(): + async for msg in pub.listen(): + if msg["type"] != "message": + continue + if msg["channel"] == "hn:kill:" + host: + await ws.close(1008) + return + await ws.send_text(msg["data"]) + + async def heartbeat(): + while True: + await asyncio.sleep(8) + if await r.exists("hn:revoked:" + host) or not await compare_expire(r, "hn:online:" + host, presence, 30): + await ws.close(1008) + return + + async def upstream(): + while True: + text = await ws.receive_text() + if len(text.encode()) > s.max_body: + raise ValueError("oversized frame") + data = json.loads(text) + if data.get("type") == "ping": + continue + await rate_limit(r, "ws:reply:" + host, 120) + turn = data.get("turn_id", "") + if data.get("type") != "reply" or await r.get(f"hn:inflight:{host}:{turn}") != session: + raise ValueError("unsolicited reply") + envelope = data["envelope"] + verified(envelope, public) + if envelope["host_id"] != host or envelope["session_id"] != session: + raise ValueError("wrong reply destination") + await r.publish("hn:replies", json.dumps(data)) + + tasks = [asyncio.create_task(x()) for x in (downstream, heartbeat, upstream)] + done, _ = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED) + for task in done: + task.result() + except (WebSocketDisconnect, asyncio.TimeoutError, ValueError, KeyError, TypeError): + with contextlib.suppress(Exception): + await ws.close(1008) + except Exception: + with contextlib.suppress(Exception): + await ws.close(1011) + finally: + for task in tasks: + task.cancel() + await asyncio.gather(*tasks, return_exceptions=True) + if pub: + await pub.aclose() + if presence: + await compare_delete(r, "hn:online:" + host, presence) + active.discard(session) + + return app diff --git a/patch-source/51c062dff81e268e/relay/store.py b/patch-source/51c062dff81e268e/relay/store.py new file mode 100644 index 0000000..9d6f8ef --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/store.py @@ -0,0 +1,97 @@ +"""Connector-owned persistent metadata. Never stores message/file/token bodies.""" +import sqlite3 +import time +import psycopg +from psycopg.rows import dict_row + +INTEGRITY_ERRORS = (sqlite3.IntegrityError, psycopg.IntegrityError) + + +class Postgres: + def __init__(self, url): + self.url = url + self.conn = psycopg.connect(url, autocommit=True, row_factory=dict_row) + self.transaction = None + + def execute(self, sql, params=()): + if self.conn.closed or self.conn.broken: + self.conn = psycopg.connect(self.url, autocommit=True, row_factory=dict_row) + return self.conn.execute(sql.replace("?", "%s"), params) + + def __enter__(self): + self.transaction = self.conn.transaction() + self.transaction.__enter__() + return self + + def __exit__(self, *args): + return self.transaction.__exit__(*args) + + def close(self): + self.conn.close() + + +class Store: + def __init__(self, path, database_url=""): + path.parent.mkdir(parents=True, exist_ok=True) + schema = """ + CREATE TABLE IF NOT EXISTS devices( + host_id TEXT PRIMARY KEY, cert TEXT NOT NULL, fingerprint TEXT NOT NULL, + owner TEXT UNIQUE, revoked INTEGER NOT NULL DEFAULT 0); + CREATE TABLE IF NOT EXISTS turns( + turn_id TEXT PRIMARY KEY, owner TEXT NOT NULL, host_id TEXT NOT NULL, + status TEXT NOT NULL, created DOUBLE PRECISION NOT NULL); + CREATE TABLE IF NOT EXISTS audit( + id INTEGER PRIMARY KEY, ts DOUBLE PRECISION NOT NULL, actor TEXT NOT NULL, + action TEXT NOT NULL, resource TEXT NOT NULL, result TEXT NOT NULL); + """ + if database_url: + self.db = Postgres(database_url) + self.db.conn.execute(schema.replace("id INTEGER PRIMARY KEY", "id BIGSERIAL PRIMARY KEY")) + else: + self.db = sqlite3.connect(path, check_same_thread=False) + self.db.row_factory = sqlite3.Row + self.db.executescript("PRAGMA journal_mode=WAL;" + schema) + + def device(self, host): + row = self.db.execute("SELECT * FROM devices WHERE host_id=?", (host,)).fetchone() + return dict(row) if row else None + + def by_owner(self, owner): + row = self.db.execute("SELECT * FROM devices WHERE owner=? AND revoked=0", (owner,)).fetchone() + return dict(row) if row else None + + def add_device(self, host, cert, fp): + with self.db: + self.db.execute("INSERT INTO devices(host_id,cert,fingerprint) VALUES(?,?,?)", (host, cert, fp)) + + def bind(self, host, owner): + with self.db: + # Conditional update also protects against two connectors racing for the same device. + count = self.db.execute("UPDATE devices SET owner=? WHERE host_id=? AND owner IS NULL AND revoked=0", (owner, host)).rowcount + if count != 1: + raise ValueError("device unavailable or already bound") + + def unbind(self, owner): + with self.db: + self.db.execute("UPDATE devices SET owner=NULL WHERE owner=?", (owner,)) + + def revoke(self, host): + with self.db: + self.db.execute("UPDATE devices SET revoked=1 WHERE host_id=?", (host,)) + + def begin_turn(self, turn, owner, host): + with self.db: + return self.db.execute("INSERT INTO turns VALUES(?,?,?,?,?) ON CONFLICT(turn_id) DO NOTHING", + (turn, owner, host, "inflight", time.time())).rowcount == 1 + + def finish(self, turn, status): + with self.db: + self.db.execute("UPDATE turns SET status=? WHERE turn_id=?", (status, turn)) + + def audit(self, actor, action, resource, result): + with self.db: + self.db.execute("INSERT INTO audit(ts,actor,action,resource,result) VALUES(?,?,?,?,?)", + (time.time(), actor, action, resource, result)) + + def close(self): + self.db.close() diff --git a/patch-source/51c062dff81e268e/relay/web_session.py b/patch-source/51c062dff81e268e/relay/web_session.py new file mode 100644 index 0000000..1ac9adf --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/web_session.py @@ -0,0 +1,183 @@ +"""Pilot H5 pairing and conversation. Credentials never appear in URLs.""" +import hashlib +import json +import secrets +import time +from urllib.parse import urlsplit + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import JSONResponse +from pydantic import BaseModel, Field + +from .common import no_cache, peer, rate_limit +from .crypto import sign, verified, verify_device +from .platform import PlatformError + +COOKIE = "hn_web_session" + + +def digest(value): + return hashlib.sha256(value.encode()).hexdigest() + + +def origin_tuple(value): + try: + parts = urlsplit(value) + if parts.scheme not in {"https", "http"} or not parts.hostname or parts.username or parts.password: + return None + return parts.scheme, parts.hostname.lower(), parts.port or (443 if parts.scheme == "https" else 80) + except ValueError: + return None + + +class PairProof(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Claim(BaseModel): + code: str = Field(min_length=12, max_length=128) + + +class Chat(BaseModel): + text: str = Field(min_length=1, max_length=8000) + request_id: str = Field(pattern=r"^[a-f0-9]{32}$") + + +class WebSessions: + def __init__(self, settings, redis, store, broker, platform): + self.s, self.r, self.store, self.broker, self.platform = settings, redis, store, broker, platform + self.router = APIRouter() + for path, method, handler in [ + ("/device/web-pair", "POST", self.device_pair), + ("/web/claim", "POST", self.claim), + ("/web/session", "GET", self.session), + ("/web/platform-session", "POST", self.platform_login), + ("/web/chat", "POST", self.chat), + ("/web/logout", "POST", self.logout), + ]: + self.router.add_api_route(path, handler, methods=[method]) + + def check_origin(self, request): + actual = origin_tuple(request.headers.get("Origin", "")) + if not actual or actual != origin_tuple(self.s.public_url): + raise HTTPException(403, "same-origin browser request required") + + async def valid_device(self, record): + device = self.store.device(record["host_id"]) + if (not device or device["revoked"] or device["owner"] != record["owner"] + or device["fingerprint"] != record["fingerprint"]): + raise HTTPException(401, "device binding changed; pair again") + generation = await self.r.get("hn:web-generation:" + record["host_id"]) or "0" + if record.get("generation") != generation: + raise HTTPException(401, "device binding changed; pair again") + return device + + async def device_pair(self, body: PairProof, request: Request): + await rate_limit(self.r, "web-pair-ip:" + peer(request), 20) + device = self.store.device(body.host_id) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "active bound device required") + try: + key = verify_device(device["cert"], self.s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["owner"] != device["owner"] + or proof["purpose"] != "h5-pair-v1" or abs(time.time() - proof["ts"]) > 90 + or not isinstance(proof["nonce"], str) or len(proof["nonce"]) != 32): + raise ValueError("invalid proof") + except Exception: + raise HTTPException(403, "invalid device proof") from None + replay = "hn:web-pair-proof:" + digest(body.host_id + proof["nonce"]) + if not await self.r.set(replay, "used", nx=True, ex=180): + raise HTTPException(409, "pairing request already used") + await rate_limit(self.r, "web-pair-device:" + body.host_id, 5) + code = secrets.token_urlsafe(18) + record = {"host_id": body.host_id, "owner": device["owner"], "fingerprint": device["fingerprint"], + "generation": await self.r.get("hn:web-generation:" + body.host_id) or "0"} + await self.r.set("hn:web-code:" + digest(code), json.dumps(record), ex=120) + self.store.audit(device["owner"], "web_pair_issued", body.host_id, "success") + return no_cache(JSONResponse(sign({"host_id": body.host_id, "code": code, "expires_in": 120, + "url": self.s.public_url.rstrip("/") + "/h5"}, self.broker.signer))) + + async def new_session(self, record): + token = secrets.token_urlsafe(32) + record = {k: record[k] for k in ("host_id", "owner", "fingerprint", "generation")} + await self.r.set("hn:web-session:" + digest(token), json.dumps(record), ex=1800) + response = no_cache(JSONResponse({"status": "paired", "host_id": record["host_id"], "expires_in": 1800})) + response.set_cookie(COOKIE, token, max_age=1800, secure=True, httponly=True, samesite="strict", path="/") + return response + + async def claim(self, body: Claim, request: Request): + self.check_origin(request) + await rate_limit(self.r, "web-claim:" + peer(request), 20) + key = "hn:web-code:" + digest(body.code.strip()) + encoded = await self.r.get(key) + if not encoded: + raise HTTPException(404, "code expired or consumed") + await self.valid_device(json.loads(encoded)) + encoded = await self.r.getdel(key) + if not encoded: + raise HTTPException(404, "code already consumed") + record = json.loads(encoded) + await self.valid_device(record) + self.store.audit(record["owner"], "web_pair_claimed", record["host_id"], "success") + return await self.new_session(record) + + async def authenticated(self, request): + token = request.cookies.get(COOKIE, "") + if not token or len(token) > 128: + raise HTTPException(401, "pair this browser first") + encoded = await self.r.get("hn:web-session:" + digest(token)) + if not encoded: + raise HTTPException(401, "browser session expired") + record = json.loads(encoded) + await self.valid_device(record) + return record + + async def session(self, request: Request): + record = await self.authenticated(request) + return no_cache(JSONResponse({"authenticated": True, "host_id": record["host_id"], + "online": bool(await self.r.exists("hn:online:" + record["host_id"])), + "mode": "pilot-text-only", "relay_sees_content": True})) + + async def platform_login(self, request: Request): + self.check_origin(request) + if not self.s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.r, "web-platform:" + peer(request), 20) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await self.platform.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = self.store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "bind your device first") + return await self.new_session({**device, "generation": await self.r.get("hn:web-generation:" + device["host_id"]) or "0"}) + + async def chat(self, body: Chat, request: Request): + self.check_origin(request) + record = await self.authenticated(request) + await rate_limit(self.r, "web-chat:" + record["owner"], 20) + text = body.text.strip() + if not text: + raise HTTPException(422, "empty message") + # PC accepts only its bound owner and the existing IM read-only channel. + if not (text == "/主机名" or text.startswith(("/回显 ", "/读取 ", "/助手 "))): + text = "/助手 " + text + turn = "web_" + digest(record["owner"] + "\0" + body.request_id) + result = await self.broker.dispatch(record["owner"], text, turn) + # Revoke/unbind during a long model call invalidates the browser immediately. + await self.authenticated(request) + return no_cache(JSONResponse(result)) + + async def logout(self, request: Request): + self.check_origin(request) + token = request.cookies.get(COOKIE, "") + if token and len(token) <= 128: + await self.r.delete("hn:web-session:" + digest(token)) + response = no_cache(JSONResponse({"status": "logged_out"})) + response.delete_cookie(COOKIE, secure=True, httponly=True, samesite="strict", path="/") + return response diff --git a/patch-source/51c062dff81e268e/relay/windows_keys.py b/patch-source/51c062dff81e268e/relay/windows_keys.py new file mode 100644 index 0000000..0e84e19 --- /dev/null +++ b/patch-source/51c062dff81e268e/relay/windows_keys.py @@ -0,0 +1,40 @@ +"""Current-user Windows DPAPI for device keys; never uses machine-wide scope.""" +import ctypes +import os +from ctypes import wintypes + + +class Blob(ctypes.Structure): + _fields_ = [("cbData", wintypes.DWORD), ("pbData", ctypes.POINTER(ctypes.c_ubyte))] + + +def transform(data, decrypt=False): + if os.name != "nt": + raise RuntimeError("DPAPI is supported only on Windows") + buffer = (ctypes.c_ubyte * len(data)).from_buffer_copy(data) + source = Blob(len(data), buffer) + target = Blob() + crypt = ctypes.WinDLL("crypt32", use_last_error=True) + kernel = ctypes.WinDLL("kernel32", use_last_error=True) + if decrypt: + fn = crypt.CryptUnprotectData + args = [ctypes.byref(source), None, None, None, None, 1, ctypes.byref(target)] + else: + fn = crypt.CryptProtectData + args = [ctypes.byref(source), "Hainuo device identity", None, None, None, 1, ctypes.byref(target)] + fn.restype = wintypes.BOOL + if not fn(*args): + raise ctypes.WinError(ctypes.get_last_error()) + try: + return ctypes.string_at(target.pbData, target.cbData) + finally: + kernel.LocalFree.argtypes = [ctypes.c_void_p] + kernel.LocalFree(target.pbData) + + +def protect(data): + return transform(data) + + +def unprotect(data): + return transform(data, decrypt=True) diff --git a/patch-source/51c062dff81e268e/scripts/update-work1067.sh b/patch-source/51c062dff81e268e/scripts/update-work1067.sh new file mode 100644 index 0000000..7c279be --- /dev/null +++ b/patch-source/51c062dff81e268e/scripts/update-work1067.sh @@ -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.' diff --git a/patch-source/951d53cd16a9de5f/Dockerfile.offline b/patch-source/951d53cd16a9de5f/Dockerfile.offline new file mode 100644 index 0000000..8af3dff --- /dev/null +++ b/patch-source/951d53cd16a9de5f/Dockerfile.offline @@ -0,0 +1,10 @@ +FROM python:3.12-slim-bookworm +ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 +WORKDIR /app +COPY wheelhouse /wheels +COPY requirements.lock pyproject.toml ./ +COPY relay ./relay +RUN pip install --no-index --find-links=/wheels --no-cache-dir -r requirements.lock && rm -rf /wheels +RUN chmod -R a+rX /app +USER 10001:10001 +ENTRYPOINT ["python", "-m", "relay.cli"] diff --git a/patch-source/951d53cd16a9de5f/compose.autoreg.yaml b/patch-source/951d53cd16a9de5f/compose.autoreg.yaml new file mode 100644 index 0000000..9902acb --- /dev/null +++ b/patch-source/951d53cd16a9de5f/compose.autoreg.yaml @@ -0,0 +1,39 @@ +services: + connector: + environment: + HN_ENROLLMENT_SIGNER_URL: + 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 diff --git a/patch-source/951d53cd16a9de5f/relay/__init__.py b/patch-source/951d53cd16a9de5f/relay/__init__.py new file mode 100644 index 0000000..32754a7 --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/__init__.py @@ -0,0 +1 @@ +"""Hainuo relay 0.1.0.""" diff --git a/patch-source/951d53cd16a9de5f/relay/cli.py b/patch-source/951d53cd16a9de5f/relay/cli.py new file mode 100644 index 0000000..fad7979 --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/cli.py @@ -0,0 +1,216 @@ +import argparse +import asyncio +import json +import os +import secrets +import ssl +import uuid +from datetime import datetime, timedelta, timezone +from pathlib import Path + +import httpx +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ed25519 +from cryptography.x509.oid import NameOID + +from .crypto import issue_ca, issue_device, load_private, pem_private + + +def write_new(path, data, private=False): + path.parent.mkdir(parents=True, exist_ok=True) + with path.open("xb") as f: + f.write(data if isinstance(data, bytes) else data.encode()) + if private and os.name != "nt": + path.chmod(0o600) + + +def init_project(root: Path, hostname: str): + if (root / ".env").exists(): + raise RuntimeError(".env exists; init never overwrites credentials") + ca_key, ca = issue_ca() + signer = ed25519.Ed25519PrivateKey.generate() + write_new(root / "operator/ca.key", pem_private(ca_key), True) + write_new(root / "runtime/trust/ca.pem", ca.public_bytes(serialization.Encoding.PEM)) + write_new(root / "runtime/connector/connector.key", pem_private(signer), True) + write_new(root / "runtime/trust/connector.pub", signer.public_key().public_bytes( + serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo)) + tls_key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, hostname)]) + now = datetime.now(timezone.utc) + tls = (x509.CertificateBuilder().subject_name(name).issuer_name(name).public_key(tls_key.public_key()) + .serial_number(x509.random_serial_number()).not_valid_before(now - timedelta(minutes=5)) + .not_valid_after(now + timedelta(days=30)) + .add_extension(x509.SubjectAlternativeName([x509.DNSName(hostname)]), critical=False) + .sign(tls_key, algorithm=None)) + write_new(root / "runtime/tls/server.key", pem_private(tls_key), True) + write_new(root / "runtime/tls/server.pem", tls.public_bytes(serialization.Encoding.PEM)) + redis_secret = secrets.token_hex(32) + db_secret = secrets.token_hex(32) + env = (f"HN_ENVIRONMENT=development\nHN_PUBLIC_HOST={hostname}\nHN_PUBLIC_URL=https://{hostname}\n" + f"HN_ROUTER_URL=wss://{hostname}/device/ws\nHN_ADMIN_SECRET={secrets.token_hex(32)}\n" + f"HN_INTERNAL_SECRET={secrets.token_hex(32)}\nHN_REDIS_PASSWORD={redis_secret}\n" + f"HN_REDIS_URL=redis://:{redis_secret}@redis:6379/0\n" + f"HN_POSTGRES_PASSWORD={db_secret}\nHN_DATABASE_URL=postgresql://hainuo:{db_secret}@postgres:5432/hainuo\n" + "HN_FEISHU_ENABLED=false\nHN_FEISHU_API_BASE=https://open.feishu.cn\n" + "HN_FEISHU_AUTHORIZE_URL=https://open.feishu.cn/open-apis/authen/v1/authorize\n" + "HN_FEISHU_APP_ID=\nHN_FEISHU_APP_SECRET=\nHN_FEISHU_ENCRYPT_KEY=\n" + "HN_FEISHU_VERIFICATION_TOKEN=\nHN_PROXY_ENABLED=false\nHN_DEVICE_DOMAIN=\n") + write_new(root / ".env", env, True) + print("已生成配置、连接器密钥、设备CA及临时TLS证书;未输出凭据。") + print("operator/ca.key 仅保留在签发工作站,不要上传到ECS。临时TLS证书上线前须替换。") + + +def device_request(directory): + host = uuid.uuid4().hex + private = ed25519.Ed25519PrivateKey.generate() + write_new(directory / "device.key", pem_private(private), True) + public = private.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode() + write_new(directory / "request.json", json.dumps({"host_id": host, "public_key": public}, indent=2)) + print("已在本机生成私钥和 request.json;仅将request.json交给签发工作站。") + + +def issue(root, request_file, output): + request = json.loads(request_file.read_text(encoding="utf-8")) + host = request["host_id"] + if len(host) != 32 or any(c not in "0123456789abcdef" for c in host): + raise ValueError("invalid host identity") + ca = x509.load_pem_x509_certificate((root / "runtime/trust/ca.pem").read_bytes()) + public = serialization.load_pem_public_key(request["public_key"].encode()) + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("Ed25519 required") + cert = issue_device(load_private(root / "operator/ca.key"), ca, host, public) + pem = cert.public_bytes(serialization.Encoding.PEM) + write_new(output / "device.pem", pem) + write_new(output / "ca.pem", (root / "runtime/trust/ca.pem").read_bytes()) + write_new(output / "connector.pub", (root / "runtime/trust/connector.pub").read_bytes()) + write_new(output / "device.json", json.dumps({"host_id": host, "owner": None}, indent=2)) + write_new(output / "registration.json", json.dumps({"host_id": host, "cert": pem.decode()}, indent=2)) + print("已签发设备证书;将device.pem、ca.pem、connector.pub、device.json交回原设备。") + + +def main(): + parser = argparse.ArgumentParser(description="海诺中继部署和设备工具") + sub = parser.add_subparsers(dest="command", required=True) + p = sub.add_parser("init") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--hostname", required=True) + p = sub.add_parser("serve") + p.add_argument("role", choices=["connector", "router"]) + p.add_argument("--host", default="127.0.0.1") + p.add_argument("--port", type=int, default=8000) + p = sub.add_parser("device-request") + p.add_argument("--directory", type=Path, required=True) + p = sub.add_parser("issue-device") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--request", type=Path, required=True) + p.add_argument("--output", type=Path, required=True) + p = sub.add_parser("register") + p.add_argument("--registration", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("bind") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("device") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p.add_argument("--test-root", type=Path) + p.add_argument("--agent-url", help="Explicit loopback read-only Agent adapter endpoint") + p.add_argument("--hainuo-config", type=Path, help="Native installer-1.065 API adapter config; alternative to --agent-url") + p = sub.add_parser("hainuo-probe") + p.add_argument("--config", type=Path, required=True) + p = sub.add_parser("web-pair") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + for command in ("status", "binding-code", "dispatch", "revoke", "unbind", "reconcile"): + p = sub.add_parser(command) + p.add_argument("--url", default="http://127.0.0.1:18081") + p.add_argument("--tls-ca", type=Path) + if command in {"binding-code", "dispatch"}: + p.add_argument("--owner", required=True) + if command == "dispatch": + p.add_argument("--text", required=True) + p.add_argument("--turn-id", default=None) + if command in {"revoke", "unbind"}: + p.add_argument("--host-id", required=True) + args = parser.parse_args() + if args.command == "init": + init_project(args.root, args.hostname) + elif args.command == "serve": + import uvicorn + uvicorn.run(f"relay.{args.role}:create_app", factory=True, host=args.host, port=args.port, + workers=1, access_log=False, proxy_headers=False, + ws_max_size=262144, ws_max_queue=16, log_level="warning") + elif args.command == "device-request": + device_request(args.directory) + elif args.command == "issue-device": + issue(args.root, args.request, args.output) + elif args.command == "register": + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, follow_redirects=False, timeout=15) as client: + response = client.post(args.url.rstrip("/") + "/admin/devices", + headers={"X-Admin-Secret": secret}, + json=json.loads(args.registration.read_text(encoding="utf-8"))) + if response.status_code != 201: + raise RuntimeError(f"register failed: HTTP {response.status_code}") + print("设备登记成功。") + elif args.command == "bind": + import getpass + from .device import bind_device + code = getpass.getpass("Binding code: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + asyncio.run(bind_device(args.directory, args.url, code, tls_verify=context)) + print("绑定成功。") + elif args.command == "device": + from .device import run_device + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else None + asyncio.run(run_device(args.directory, args.url, root=args.test_root, ssl_context=context, + agent_url=args.agent_url, hainuo_config=args.hainuo_config)) + elif args.command == "hainuo-probe": + from .hainuo1065 import Hainuo1065Agent + async def probe(): + adapter = Hainuo1065Agent(args.config) + try: + print(json.dumps(await adapter.probe(), ensure_ascii=False, indent=2)) + finally: + await adapter.close() + asyncio.run(probe()) + elif args.command == "web-pair": + from .device import pair_browser + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + result = asyncio.run(pair_browser(args.directory, args.url, tls_verify=context)) + print("网页地址:" + result["url"]) + print("一次性网页配对码(120秒有效):" + result["code"]) + else: + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, timeout=200, follow_redirects=False) as client: + paths = {"status": "/admin/devices", "binding-code": "/admin/test/binding-code", + "dispatch": "/admin/test/dispatch", "reconcile": "/admin/reconcile"} + if args.command in {"revoke", "unbind"}: + if len(args.host_id) != 32 or any(c not in "0123456789abcdef" for c in args.host_id): + raise ValueError("invalid host identity") + path = f"/admin/devices/{args.host_id}/{args.command}" + else: + path = paths[args.command] + body = None + if args.command in {"binding-code", "dispatch"}: + body = {"open_id": args.owner, "text": getattr(args, "text", "binding")} + if getattr(args, "turn_id", None): + body["turn_id"] = args.turn_id + response = client.request("GET" if args.command == "status" else "POST", args.url.rstrip("/") + path, + headers={"X-Admin-Secret": secret}, json=body) + if response.status_code != 200: + raise RuntimeError(f"operation failed: HTTP {response.status_code}") + print(json.dumps(response.json(), ensure_ascii=False, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/patch-source/951d53cd16a9de5f/relay/client_manage.py b/patch-source/951d53cd16a9de5f/relay/client_manage.py new file mode 100644 index 0000000..02ed999 --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/client_manage.py @@ -0,0 +1,279 @@ +"""Per-user Windows connection companion. No platform/application secrets.""" +import argparse +import asyncio +import json +import os +import secrets +import ssl +import subprocess +import sys +import time +import uuid +from pathlib import Path + +import httpx +import psutil +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey +from .crypto import load_private, load_public, pem_private, raw_public, sign, verified, verify_device +from .windows_keys import protect + +BASE = Path(__file__).resolve().parent.parent +URL = "https://10.77.76.13" + + +def save(path, value): + temp = path.with_suffix(path.suffix + ".tmp") + temp.write_text(json.dumps(value, ensure_ascii=False, indent=2), encoding="utf-8") + os.replace(temp, path) + + +def secure_directory(directory): + directory.mkdir(parents=True, exist_ok=True) + # SID avoids localized account names. Fail closed if ACL protection fails. + result = subprocess.run(["whoami", "/user", "/fo", "csv", "/nh"], capture_output=True, text=True, check=True) + import csv + sid = list(csv.reader([result.stdout.strip()]))[0][1] + subprocess.run(["icacls", str(directory), "/inheritance:r", "/grant:r", f"*{sid}:(OI)(CI)F", "*S-1-5-18:(OI)(CI)F"], + capture_output=True, check=True, creationflags=subprocess.CREATE_NO_WINDOW) + + +def prepare(directory): + secure_directory(directory) + for name in ("server.pem", "ca.pem", "connector.pub"): + target = directory / name + if not target.exists(): + target.write_bytes((BASE / "trust" / name).read_bytes()) + if not (directory / "device.json").exists(): + if (directory / "device.key.dpapi").exists() or (directory / "device.key").exists(): + raise RuntimeError("发现不完整的设备身份,请保留目录并联系管理员恢复。") + private = Ed25519PrivateKey.generate() + (directory / "device.key.dpapi").write_bytes(protect(pem_private(private))) + save(directory / "device.json", {"host_id": uuid.uuid4().hex, "owner": None}) + + +def tls(directory): + return ssl.create_default_context(cafile=str(directory / "server.pem")) + + +def import_existing(directory, source): + """Explicit migration; only public trust and this PC's identity, never app keys.""" + if source.resolve() == directory.resolve(): + raise ValueError("请选择旧设备目录。") + if any((directory / name).exists() for name in ("device.json", "device.key.dpapi", "device.key")): + raise RuntimeError("本目录已有设备身份。请使用已有身份继续,不能用导入覆盖。") + config = json.loads((source / "device.json").read_text(encoding="utf-8-sig")) + for name in ("server.pem", "ca.pem", "connector.pub"): + if (source / name).read_bytes().strip() != (BASE / "trust" / name).read_bytes().strip(): + raise ValueError("旧设备的服务信任与本安装包不符,请联系管理员。") + cert = (source / "device.pem").read_text(encoding="utf-8") + public = verify_device(cert, (source / "ca.pem").read_bytes(), config["host_id"]) + private = load_private(source / "device.key") + if raw_public(public) != raw_public(private.public_key()): + raise ValueError("设备证书与本机私钥不匹配。") + secure_directory(directory) + for name in ("device.pem", "server.pem", "ca.pem", "connector.pub"): + (directory / name).write_bytes((source / name).read_bytes()) + (directory / "device.key.dpapi").write_bytes(protect(pem_private(private))) + save(directory / "device.json", config) + # Preserve local replay journal using SQLite's consistent backup API. + if (source / "journal.sqlite").is_file(): + import sqlite3 + with sqlite3.connect(source / "journal.sqlite") as original, sqlite3.connect(directory / "journal.sqlite") as target: + original.backup(target) + return {"message": "旧设备导入成功,Work绑定已保留。请先停止旧PowerShell桥接窗口,再点击启动连接。"} + + +def enrollment(directory): + prepare(directory) + cfg = json.loads((directory / "device.json").read_text()) + if (directory / "device.pem").exists(): + verify_device((directory / "device.pem").read_text(), (directory / "ca.pem").read_bytes(), cfg["host_id"]) + return {"message": "设备已注册,可在Work获取绑定码后绑定。已有绑定可直接启动连接。"} + with httpx.Client(verify=tls(directory), trust_env=False, timeout=15) as http: + key = load_private(directory / "device.key") + proof = sign({"host_id": cfg["host_id"], "purpose": "device-enroll-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + response = http.post(URL + "/device/register", json={"host_id": cfg["host_id"], "proof": proof, + "public_key": key.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode()}) + if response.status_code not in {200, 201}: + raise RuntimeError("自动注册失败:HTTP " + str(response.status_code) + ";请确认ECS已安装1.067自动注册更新。") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt.get("status") != "registered" or receipt["host_id"] != cfg["host_id"]: + raise ValueError("注册回执身份不符") + public = verify_device(receipt["cert"], (directory / "ca.pem").read_bytes(), cfg["host_id"]) + if raw_public(public) != raw_public(key.public_key()): + raise ValueError("注册证书公钥不符") + temp = directory / "device.pem.tmp" + temp.write_text(receipt["cert"], encoding="utf-8") + os.replace(temp, directory / "device.pem") + (directory / "enrollment.json").unlink(missing_ok=True) + return {"message": "设备已自动注册,无需管理员审批。请在Work获取绑定码,填写后点击绑定并连接。"} + + +def managed_processes(directory): + """Only processes with our absolute entry path AND exact device directory.""" + state = directory / "processes.json" + if not state.exists(): + return [] + result = [] + for item in json.loads(state.read_text()).get("processes", []): + try: + proc = psutil.Process(item["pid"]) + command = proc.cmdline() + if (abs(proc.create_time() - item["created"]) < 0.1 and str(BASE / "entry.py") in command + and str(directory) in command): + result.append(proc) + except psutil.Error: + pass + return result + + +def stop(directory): + procs = managed_processes(directory) + # Supervisor owns and terminates only its own child handles in its finally block. + (directory / "stop.flag").touch() + _, alive = psutil.wait_procs(procs, timeout=8) + for proc in alive: + proc.terminate() + return {"message": "已停止本安装包启动的连接进程。手工启动的旧桥接请在原窗口按Ctrl+C停止。"} + + +def start(directory, root): + from .process_lock import DeviceProcessLock + with DeviceProcessLock(directory / "control"): + return _start(directory, root) + + +def _start(directory, root): + cfg = json.loads((directory / "device.json").read_text()) + if not cfg.get("owner") or not (directory / "device.pem").exists(): + raise RuntimeError("请先完成设备准入和Work绑定。") + if managed_processes(directory): + return {"message": "连接进程已运行,请点击状态查看。"} + (directory / "stop.flag").unlink(missing_ok=True) + log = (directory / "bridge.log").open("a", encoding="utf-8") + proc = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "supervise", "--directory", str(directory), "--root", str(root)], + stdout=log, stderr=log, creationflags=subprocess.CREATE_NO_WINDOW) + log.close() + save(directory / "processes.json", {"processes": [{"pid": proc.pid, "created": psutil.Process(proc.pid).create_time()}]}) + return {"message": "连接进程已启动;约30秒后点击状态,再到Work发送 /主机名 验证。"} + + +def supervise(directory, root): + from .process_lock import DeviceProcessLock + lockdir = directory / "supervisor" + with DeviceProcessLock(lockdir): + children = [] + def record_children(): + processes = [psutil.Process(os.getpid())] + [psutil.Process(p.pid) for p in children if p.poll() is None] + save(directory / "processes.json", {"processes": [{"pid": p.pid, "created": p.create_time()} for p in processes]}) + try: + with httpx.Client(trust_env=False, timeout=1) as client: + try: + client.get("http://127.0.0.1:18643/health") + except httpx.ConnectError: + pass + else: + raise RuntimeError("原生API端口18643已被占用,请先停止旧连接。") + native = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "native", "--directory", str(directory), "--root", str(root)]) + children.append(native) + record_children() + for _ in range(90): + if (directory / "stop.flag").exists(): + return + if native.poll() is not None: + raise RuntimeError("本机原生API启动失败,请检查安装配置。") + try: + from .hainuo1065 import Hainuo1065Agent + async def probe(): + agent = Hainuo1065Agent(directory / "hainuo-native.json") + try: + return await agent.probe() + finally: + await agent.close() + asyncio.run(probe()) + break + except Exception: + time.sleep(1) + else: + raise RuntimeError("本机原生API未就绪。") + bridge = subprocess.Popen([sys.executable, str(BASE / "entry.py"), "bridge", "--directory", str(directory), "--root", str(root)]) + children.append(bridge) + record_children() + while not (directory / "stop.flag").exists(): + if any(proc.poll() is not None for proc in children): + raise RuntimeError("连接子进程已退出,请查看状态后重新启动。") + time.sleep(0.5) + finally: + for proc in reversed(children): + if proc.poll() is None: + proc.terminate() + try: + proc.wait(timeout=5) + except subprocess.TimeoutExpired: + proc.kill() + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("action", choices=["import", "enroll", "bind", "start", "stop", "status", "pair", "supervise", "native", "bridge"]) + parser.add_argument("--directory", type=Path, required=True) + parser.add_argument("--root", type=Path, required=True) + args = parser.parse_args() + directory, root = args.directory.resolve(), args.root.resolve() + directory.mkdir(parents=True, exist_ok=True) + try: + if args.action == "import": + result = import_existing(directory, Path(sys.stdin.readline().strip())) + elif args.action == "enroll": + result = enrollment(directory) + elif args.action == "bind": + from .device import bind_device + code = sys.stdin.readline().strip() + if not code: + raise ValueError("请填写Work机器人新生成的绑定码。") + enrollment(directory) + stop(directory) + asyncio.run(bind_device(directory, URL, code, tls_verify=tls(directory))) + result = start(directory, root) + result["message"] = "Work绑定成功。" + result["message"] + elif args.action == "start": + result = start(directory, root) + elif args.action == "stop": + result = stop(directory) + elif args.action == "pair": + from .device import pair_browser + receipt = asyncio.run(pair_browser(directory, URL, tls_verify=tls(directory))) + result = {"message": "网页:" + receipt["url"] + "\n一次性配对码(120秒):" + receipt["code"]} + elif args.action == "status": + cfg = json.loads((directory / "device.json").read_text()) if (directory / "device.json").exists() else {} + log = directory / "bridge.log" + tail = log.read_bytes()[-6000:].decode("utf-8", errors="replace") if log.exists() else "" + result = {"message": "设备:" + cfg.get("host_id", "未申请") + "\nWork身份:" + (cfg.get("owner") or "未绑定") + + "\n连接进程:" + ("运行中(请在Work发送 /主机名 确认当前链路)" if managed_processes(directory) else "未运行") + "\n" + tail[-2500:]} + elif args.action == "supervise": + return supervise(directory, root) + elif args.action == "native": + import importlib.util + spec = importlib.util.spec_from_file_location("native_companion", BASE / "hainuo_native_api.py") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + key = module.prepare_environment(root, "hainuo", 18643, directory / "hainuo-native.json") + import logging + logging.disable(logging.CRITICAL) + return asyncio.run(module.serve_native(18643, key)) + else: + from .device import run_device + return asyncio.run(run_device(directory, "wss://10.77.76.13/device/ws", ssl_context=tls(directory), hainuo_config=directory / "hainuo-native.json")) + print(json.dumps(result, ensure_ascii=False)) + except KeyboardInterrupt: + pass + except Exception as error: + # Native exception bodies may include URLs or secrets; emit bounded known messages only. + message = str(error) if isinstance(error, (RuntimeError, ValueError)) else type(error).__name__ + ";请检查网络、证书、安装目录。" + print(json.dumps({"message": message[:600], "error": True}, ensure_ascii=False)) + raise SystemExit(1) from None + + +if __name__ == "__main__": + main() diff --git a/patch-source/951d53cd16a9de5f/relay/common.py b/patch-source/951d53cd16a9de5f/relay/common.py new file mode 100644 index 0000000..c112b99 --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/common.py @@ -0,0 +1,61 @@ +import hashlib +import hmac +import json +import time +from fastapi import HTTPException, Request +from starlette.responses import JSONResponse + + +def require_secret(actual, expected): + if not actual or not hmac.compare_digest(actual, expected): + raise HTTPException(401, "unauthorized") + + +async def rate_limit(redis, bucket: str, limit=60, seconds=60): + # Atomic counter + expiry: a crash cannot leave a permanent bucket. + key = "hn:rate:" + hashlib.sha256(bucket.encode()).hexdigest() + ":" + str(int(time.time()) // seconds) + pipe = redis.pipeline(transaction=True) + pipe.incr(key) + pipe.expire(key, seconds * 2) + result = (await pipe.execute())[0] + if int(result) > limit: + raise HTTPException(429, "rate limited", headers={"Retry-After": str(seconds)}) + + +class BodyLimitMiddleware: + def __init__(self, app, max_body=262144): + self.app, self.max_body = app, max_body + + async def __call__(self, scope, receive, send): + if scope["type"] != "http": + return await self.app(scope, receive, send) + messages, size = [], 0 + while True: + message = await receive() + if message["type"] == "http.disconnect": + return + size += len(message.get("body", b"")) + if size > self.max_body: + return await JSONResponse({"detail": "body too large"}, status_code=413)(scope, receive, send) + messages.append(message) + if not message.get("more_body", False): + break + async def buffered(): + return messages.pop(0) if messages else await receive() + await self.app(scope, buffered, send) + + +def no_cache(response): + response.headers["Cache-Control"] = "no-store" + response.headers["X-Content-Type-Options"] = "nosniff" + response.headers["Referrer-Policy"] = "no-referrer" + return response + + +async def json_pub(redis, channel, data): + return await redis.publish(channel, json.dumps(data, separators=(",", ":"))) + + +def peer(request: Request): + # Do not trust arbitrary X-Forwarded-For; configure uvicorn trusted proxies explicitly. + return request.client.host if request.client else "unknown" diff --git a/patch-source/951d53cd16a9de5f/relay/config.py b/patch-source/951d53cd16a9de5f/relay/config.py new file mode 100644 index 0000000..d6ad400 --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/config.py @@ -0,0 +1,65 @@ +from pathlib import Path +from pydantic import model_validator +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + model_config = SettingsConfigDict(env_prefix="HN_", extra="ignore", hide_input_in_errors=True) + environment: str = "production" + role: str = "connector" + redis_url: str = "redis://127.0.0.1:6379/0" + data_dir: Path = Path("runtime/connector") + database_url: str = "" + admin_secret: str = "" + internal_secret: str = "" + ca_cert: Path = Path("runtime/pki/ca.pem") + connector_key: Path = Path("runtime/pki/connector.key") + connector_public_key: Path = Path("runtime/pki/connector.pub") + public_url: str = "https://relay.example.invalid" + router_url: str = "wss://relay.example.invalid/device/ws" + device_domain: str = "" + feishu_api_base: str = "https://open.feishu.cn" + feishu_authorize_url: str = "https://open.feishu.cn/open-apis/authen/v1/authorize" + feishu_app_id: str = "" + feishu_app_secret: str = "" + feishu_encrypt_key: str = "" + feishu_verification_token: str = "" + feishu_enabled: bool = False + oauth_enabled: bool = True + feishu_event_source: str = "longconn" + proxy_enabled: bool = False + # A fixed allowlist, never a user supplied upstream URL. + proxy_paths: str = "/open-apis/drive/v1/files,/open-apis/docx/v1/documents" + request_timeout: int = 45 + max_body: int = 262144 + max_pending: int = 1000 + max_devices: int = 1000 + max_connections: int = 1000 + max_result: int = 16000 + allow_insecure_local: bool = False + enrollment_signer_url: str = "" + + @model_validator(mode="after") + def validate_runtime(self): + if self.environment not in {"production", "test", "development"}: + raise ValueError("invalid environment") + if self.role == "connector" and min(len(self.admin_secret), len(self.internal_secret)) < 32: + raise ValueError("admin/internal secrets must each be at least 32 characters") + if self.role == "longconn" and len(self.internal_secret) < 32: + raise ValueError("internal credential must be at least 32 characters") + if self.role == "connector" and self.admin_secret == self.internal_secret: + raise ValueError("admin and internal credentials must be different") + if self.environment == "production": + if self.allow_insecure_local or not self.public_url.startswith("https://"): + raise ValueError("production requires HTTPS") + if not self.router_url.startswith("wss://"): + raise ValueError("production requires WSS") + if not self.feishu_api_base.startswith("https://"): + raise ValueError("platform API requires HTTPS") + if self.feishu_event_source not in {"longconn", "webhook"}: + raise ValueError("invalid platform event source") + if self.feishu_enabled and not all((self.feishu_app_id, self.feishu_app_secret)): + raise ValueError("platform app ID and secret are required") + if self.feishu_enabled and self.feishu_event_source == "webhook" and not all((self.feishu_encrypt_key, self.feishu_verification_token)): + raise ValueError("webhook requires encryption key and verification token") + return self diff --git a/patch-source/951d53cd16a9de5f/relay/connector.py b/patch-source/951d53cd16a9de5f/relay/connector.py new file mode 100644 index 0000000..bfde462 --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/connector.py @@ -0,0 +1,502 @@ +import asyncio +import base64 +import contextlib +import hashlib +import hmac +import json +import re +import secrets +import sqlite3 +import time +import uuid +from contextlib import asynccontextmanager +from dataclasses import dataclass +from urllib.parse import unquote, urlsplit + +from cryptography.hazmat.primitives import hashes, padding +from cryptography.hazmat.primitives.asymmetric import x25519 +from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes +from fastapi import FastAPI, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from fastapi.exceptions import RequestValidationError +from pydantic import BaseModel, Field +from redis.asyncio import Redis + +from .common import BodyLimitMiddleware, no_cache, peer, rate_limit, require_secret +from .config import Settings +from .crypto import (fingerprint, load_private, open_envelope, raw_public, seal, + sign, verified, verify_device) +from .oauth import OAuth +from .platform import Feishu, PlatformError +from .store import Store, INTEGRITY_ERRORS + + +class Register(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + cert: str = Field(max_length=10000) + + +class Bind(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Instruction(BaseModel): + open_id: str = Field(min_length=1, max_length=128) + text: str = Field(min_length=1, max_length=8000) + # Stable identity chosen by upstream, not generated again on retries. + turn_id: str = Field(default_factory=lambda: uuid.uuid4().hex, pattern=r"^[A-Za-z0-9_-]{1,128}$") + + +@dataclass +class Pending: + future: asyncio.Future + private: object + public: object + host: str + session: str + + +class Broker: + def __init__(self, settings, redis, store): + self.s, self.r, self.store = settings, redis, store + self.signer = load_private(settings.connector_key) + self.pending: dict[str, Pending] = {} + + async def replies(self, pub): + async for message in pub.listen(): + if message["type"] != "message": + continue + try: + data = json.loads(message["data"]) + item = self.pending.get(data["turn_id"]) + if not item or item.future.done(): + continue + plain = open_envelope(data["envelope"], item.private, item.public, + host_id=item.host, session_id=item.session) + if plain["turn_id"] != data["turn_id"] or plain.get("type") != "result": + continue + result = plain.get("result", "") + if not isinstance(result, str) or len(result) > self.s.max_result: + continue + item.future.set_result({"status": "completed", "turn_id": data["turn_id"], "result": result}) + except Exception: + # Invalid packets never consume a pending result; do not log packet contents. + continue + + async def dispatch(self, owner, text, turn): + device = self.store.by_owner(owner) + if not device: + return {"status": "unavailable", "turn_id": turn, "result": "未绑定设备或设备不可用。"} + host = device["host_id"] + await rate_limit(self.r, "dispatch:" + owner, 30) + if len(self.pending) >= self.s.max_pending: + raise HTTPException(503, "too many in-flight requests") + presence = await self.r.get("hn:online:" + host) + if not presence or await self.r.exists("hn:revoked:" + host): + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线,请开机并连接网络。"} + try: + presence = json.loads(presence) + if fingerprint(presence["cert"]) != device["fingerprint"]: + raise ValueError("certificate changed") + public = verify_device(presence["cert"], self.s.ca_cert.read_bytes(), host) + proof = verified(presence["proof"], public) + if proof["host_id"] != host or proof["challenge"] != presence["challenge"]: + raise ValueError("invalid device session") + session = proof["session_id"] + except Exception: + self.store.audit(owner, "dispatch", host, "identity_rejected") + raise HTTPException(409, "device identity rejected") from None + if not self.store.begin_turn(turn, owner, host): + return {"status": "duplicate", "turn_id": turn, "result": "该请求已接收,不会重复执行。"} + private = x25519.X25519PrivateKey.generate() + future = asyncio.get_running_loop().create_future() + self.pending[turn] = Pending(future, private, public, host, session) + envelope = seal({"type": "instruction", "turn_id": turn, "owner": owner, + "text": text, "read_only": True, "channel": "im", + "reply_key": raw_public(private.public_key())}, proof["kx"], self.signer, + host_id=host, session_id=session) + key = f"hn:inflight:{host}:{turn}" + try: + await self.r.set(key, session, ex=self.s.request_timeout + 5) + count = await self.r.publish("hn:down:" + host, json.dumps({"type": "instruction", "envelope": envelope})) + if not count: + self.store.finish(turn, "offline") + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线。"} + result = await asyncio.wait_for(future, timeout=self.s.request_timeout) + self.store.finish(turn, "completed") + self.store.audit(owner, "instruction", host, "completed") + return result + except asyncio.TimeoutError: + self.store.finish(turn, "unknown") + return {"status": "unknown", "turn_id": turn, + "result": "等待结果超时,执行状态未知;不要直接重发有副作用的任务。"} + finally: + self.pending.pop(turn, None) + await self.r.delete(key) + + +def create_app(settings=None, redis=None, platform=None): + s = settings or Settings() + r = redis or Redis.from_url(s.redis_url, decode_responses=True) + store = Store(s.data_dir / "metadata.sqlite", s.database_url) + f = platform or Feishu(s) + broker = Broker(s, r, store) + oauth = OAuth(s, r, f) + background = set() + + def spawn(coro): + task = asyncio.create_task(coro) + background.add(task) + task.add_done_callback(background.discard) + # Retrieve errors without displaying possibly sensitive exception text. + task.add_done_callback(lambda t: t.exception() if not t.cancelled() else None) + + @asynccontextmanager + async def lifespan(app): + await r.ping() + for row in store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall(): + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + pub = r.pubsub() + await pub.subscribe("hn:replies") + reply_task = asyncio.create_task(broker.replies(pub)) + app.state.reply_task = reply_task + async def sweep(): + while True: + await asyncio.sleep(30) + async with oauth.lock: + oauth.prune() + sweep_task = asyncio.create_task(sweep()) + try: + yield + finally: + for task in [reply_task, sweep_task, *background]: + task.cancel() + await asyncio.gather(reply_task, sweep_task, *background, return_exceptions=True) + await pub.aclose() + await f.close() + store.close() + if redis is None: + await r.aclose() + + app = FastAPI(title="Hainuo B1 connector", lifespan=lifespan, docs_url=None, redoc_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=s.max_body) + app.include_router(oauth.router) + from .web_session import WebSessions + web_sessions = WebSessions(s, r, store, broker, f) + app.include_router(web_sessions.router) + app.state.store, app.state.broker, app.state.oauth = store, broker, oauth + app.state.platform, app.state.redis = f, r + + @app.exception_handler(RequestValidationError) + async def validation_error(request, exc): + # FastAPI's default error response includes invalid input values (possibly tokens). + return JSONResponse({"detail": "invalid request parameters"}, status_code=422) + + @app.middleware("http") + async def secure_response(request, call_next): + return no_cache(await call_next(request)) + + def admin(request): + require_secret(request.headers.get("X-Admin-Secret"), s.admin_secret) + + from .enrollment import enrollment_router + app.include_router(enrollment_router(s, r, store, broker.signer, admin)) + + @app.get("/health") + async def health(): + return {"status": "alive", "role": "connector", "version": "0.1.0"} + + @app.get("/ready") + async def ready(): + await r.ping() + store.db.execute("SELECT 1") + if not getattr(app.state, "reply_task", None) or app.state.reply_task.done(): + raise HTTPException(503, "reply subscriber unavailable") + return {"status": "ready"} + + @app.post("/admin/devices", status_code=201) + async def register(body: Register, request: Request): + admin(request) + try: + verify_device(body.cert, s.ca_cert.read_bytes(), body.host_id) + except Exception: + raise HTTPException(400, "invalid device certificate") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= s.max_devices: + raise HTTPException(503, "device capacity reached") + try: + fp = fingerprint(body.cert) + store.add_device(body.host_id, body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "device already exists") from None + await r.set("hn:device:" + body.host_id, fp) + store.audit("admin", "register", body.host_id, "success") + return {"host_id": body.host_id, "fingerprint": fp} + + @app.get("/admin/devices") + async def devices(request: Request): + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,owner,revoked FROM devices ORDER BY host_id").fetchall() + return {"devices": [{**dict(row), "online": bool(await r.exists("hn:online:" + row["host_id"]))} for row in rows]} + + @app.post("/admin/devices/{host}/revoke") + async def revoke(host: str, request: Request): + admin(request) + if not store.device(host): + raise HTTPException(404, "device not found") + # Persistent source of truth first; router block + disconnect follows. + store.revoke(host) + await r.incr("hn:web-generation:" + host) + await r.set("hn:revoked:" + host, "1") + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "revoke") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "revoked", "result": "设备已停用,结果已丢弃。"}) + store.audit("admin", "revoke", host, "success") + return {"status": "revoked"} + + @app.post("/admin/reconcile") + async def reconcile(request: Request): + """Rebuild router admission metadata after Redis recovery; no dialogue data.""" + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall() + for row in rows: + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + return {"devices": len(rows)} + + @app.post("/admin/devices/{host}/unbind") + async def admin_unbind(host: str, request: Request): + admin(request) + device = store.device(host) + if not device: + raise HTTPException(404, "device not found") + if device["owner"]: + store.unbind(device["owner"]) + await r.incr("hn:web-generation:" + host) + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "unbind") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + store.audit("admin", "unbind", host, "success") + return {"status": "unbound", "host_id": host, "revoked": bool(device["revoked"])} + + async def make_binding_code(owner): + if store.by_owner(owner): + return "你已绑定电脑,请先发送 /解绑。" + await rate_limit(r, "binding:" + owner, 5) + code = secrets.token_urlsafe(12) + await r.set("hn:bind:" + hashlib.sha256(code.encode()).hexdigest(), owner, ex=300) + return "请在本人电脑的桥接客户端输入绑定码(5分钟有效):" + code + + @app.post("/device/bind") + async def bind(body: Bind, request: Request): + await rate_limit(r, "bind:" + peer(request), 20) + device = store.device(body.host_id) + if not device or device["revoked"]: + raise HTTPException(404, "device unavailable") + try: + public = verify_device(device["cert"], s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, public) + if proof["host_id"] != body.host_id or abs(time.time() - proof["ts"]) > 90: + raise ValueError("invalid proof") + code = proof["code"] + if not isinstance(code, str) or len(code) > 128: + raise ValueError("invalid code") + except Exception: + raise HTTPException(403, "invalid device proof") from None + key = "hn:bind:" + hashlib.sha256(code.encode()).hexdigest() + owner = await r.get(key) + if not owner: + raise HTTPException(404, "binding code expired or consumed") + # GETDEL is atomic across connectors. Invalid device proofs never reach this point. + owner = await r.getdel(key) + if not owner: + raise HTTPException(404, "binding code consumed") + try: + store.bind(body.host_id, owner) + except (*INTEGRITY_ERRORS, ValueError): + raise HTTPException(409, "owner/device already bound; request new code") from None + await r.incr("hn:web-generation:" + body.host_id) + store.audit(owner, "bind", body.host_id, "success") + return sign({"host_id": body.host_id, "owner": owner, "ts": int(time.time())}, broker.signer) + + @app.post("/admin/test/binding-code") + async def test_code(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return {"message": await make_binding_code(body.open_id)} + + @app.post("/admin/test/dispatch") + async def test_dispatch(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return await broker.dispatch(body.open_id, body.text, body.turn_id) + + async def handle_event(data): + header, event = data.get("header", {}), data.get("event", {}) + if header.get("event_type") != "im.message.receive_v1": + return + message, sender = event.get("message", {}), event.get("sender", {}) + owner = sender.get("sender_id", {}).get("open_id") + message_id = message.get("message_id", "") + if (message.get("chat_type") != "p2p" or message.get("message_type") != "text" + or sender.get("sender_type") != "user" or not owner + or not re.fullmatch(r"[A-Za-z0-9_-]{1,128}", message_id)): + return + if header.get("app_id") != s.feishu_app_id: + return + try: + text = json.loads(message["content"])["text"].strip() + if not text or len(text) > 8000: + return + # Persistent receipt BEFORE any side effect. Restarts do not execute a delivery twice. + event_turn = "event_" + hashlib.sha256(message_id.encode()).hexdigest() + if not store.begin_turn(event_turn, owner, "event"): + return + if text == "/绑定": + result = await make_binding_code(owner) + elif text == "/解绑": + old = store.by_owner(owner) + store.unbind(owner) + if old: + await r.incr("hn:web-generation:" + old["host_id"]) + await r.publish("hn:kill:" + old["host_id"], "unbind") + for item in broker.pending.values(): + if item.host == old["host_id"] and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + result = "已解绑;重新绑定后请更新本机绑定信息。" + elif text == "/主机名" or text.startswith("/回显 "): + result = (await broker.dispatch(owner, text, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + else: + result = "当前为企业身份接入联调:支持 /绑定、/解绑、/主机名、/回显 内容。Agent工具权限验收完成后再开放自然语言任务。" + await f.reply(message_id, result[:s.max_result]) + store.finish(event_turn, "replied") + except Exception: + store.audit(owner, "event", message_id, "failed_or_unknown") + + @app.post("/internal/events") + async def internal_event(request: Request): + require_secret(request.headers.get("X-Internal-Secret"), s.internal_secret) + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(await request.json())) + return {"accepted": True} + + @app.post("/feishu/event") + async def webhook(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if not s.feishu_encrypt_key or not s.feishu_verification_token: + raise HTTPException(503, "webhook credentials not configured; use selected event source") + await rate_limit(r, "webhook:" + peer(request), 600) + raw = await request.body() + timestamp, nonce, signature = (request.headers.get(x, "") for x in + ("X-Lark-Request-Timestamp", "X-Lark-Request-Nonce", "X-Lark-Signature")) + try: + if abs(time.time() - int(timestamp)) > 300 or not nonce or len(nonce) > 128: + raise ValueError("stale request") + expected = hashlib.sha256((timestamp + nonce + s.feishu_encrypt_key).encode() + raw).hexdigest() + if not hmac.compare_digest(expected, signature): + raise ValueError("bad signature") + data = json.loads(raw) + if "encrypt" in data: + cipher = base64.b64decode(data["encrypt"], validate=True) + decryptor = Cipher(algorithms.AES(hashlib.sha256(s.feishu_encrypt_key.encode()).digest()), + modes.CBC(cipher[:16])).decryptor() + padded = decryptor.update(cipher[16:]) + decryptor.finalize() + unpadder = padding.PKCS7(128).unpadder() + data = json.loads(unpadder.update(padded) + unpadder.finalize()) + token = data.get("token", data.get("header", {}).get("token", "")) + if not hmac.compare_digest(token, s.feishu_verification_token): + raise ValueError("invalid verification token") + except Exception: + raise HTTPException(403, "invalid event authentication") from None + if data.get("type") == "url_verification": + return {"challenge": data.get("challenge", "")} + # Same delivery is ACKed again; durable message_id receipt handles execution deduplication. + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(data)) + return {"code": 0} + + @app.get("/api/whoami") + async def whoami(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(r, "whoami:" + peer(request)) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await f.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "no device bound") + if not s.device_domain or not re.fullmatch(r"[a-zA-Z0-9.-]+", s.device_domain): + raise HTTPException(503, "device web domain not configured") + return {"host_id": device["host_id"], "url": f"https://h-{device['host_id']}.{s.device_domain}/", + "online": bool(await r.exists("hn:online:" + device["host_id"])), + "device_login_required": True} + + @app.get("/h5", response_class=HTMLResponse) + async def h5(): + from importlib.resources import files + return HTMLResponse(files("relay").joinpath("web/index.html").read_text(encoding="utf-8"), + headers={"Content-Security-Policy": "default-src 'self'; script-src 'self'; style-src 'self'; frame-ancestors 'self'; base-uri 'none'; form-action 'self'"}) + + @app.get("/assets/{name}") + async def asset(name: str): + from importlib.resources import files + from starlette.responses import Response + if name not in {"app.js", "style.css"}: + raise HTTPException(404, "not found") + return Response(files("relay").joinpath("web/" + name).read_bytes(), + media_type="text/javascript" if name.endswith(".js") else "text/css") + + @app.get("/proxy") + async def proxy(request: Request): + if not s.proxy_enabled or not s.feishu_enabled: + raise HTTPException(404, "not found") + auth = request.headers.get("Authorization", "") + target = request.headers.get("X-Relay-Target", "") + decoded = unquote(target) + parts = urlsplit(target) + allowed = s.proxy_paths.split(",") + if (not auth.startswith("Bearer ") or len(auth) > 8192 or len(target) > 4096 + or parts.scheme or parts.netloc or parts.fragment or target.startswith("//") + or any(x in decoded for x in ("..", "\\", "\r", "\n", "#", "%")) + or not any(parts.path == x or parts.path.startswith(x + "/") for x in allowed)): + raise HTTPException(403, "target not allowed") + try: + info = await f.user_info(auth[7:]) + await rate_limit(r, "proxy:" + info["open_id"], 60) + # Buffer bounded response; no disk/cache, redirects never followed. + async with f.http.stream("GET", target, headers={"Authorization": auth}) as response: + if response.status_code in {301, 302, 303, 307, 308}: + raise HTTPException(502, "upstream redirect refused") + chunks, size = [], 0 + async for chunk in response.aiter_bytes(): + size += len(chunk) + if size > 2 * 1024 * 1024: + raise HTTPException(502, "response too large") + chunks.append(chunk) + from starlette.responses import Response + store.audit(info["open_id"], "proxy", parts.path, str(response.status_code)) + return Response(b"".join(chunks), status_code=response.status_code, + media_type=response.headers.get("content-type", "application/json")) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + + return app diff --git a/patch-source/951d53cd16a9de5f/relay/crypto.py b/patch-source/951d53cd16a9de5f/relay/crypto.py new file mode 100644 index 0000000..dd962e5 --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/crypto.py @@ -0,0 +1,140 @@ +"""Signed ephemeral X25519 envelopes. The router never receives plaintext keys.""" +import base64 +import hashlib +import json +import secrets +import time +from datetime import datetime, timedelta, timezone + +from cryptography import x509 +from cryptography.hazmat.primitives import hashes, serialization +from cryptography.hazmat.primitives.asymmetric import ed25519, x25519 +from cryptography.hazmat.primitives.ciphers.aead import AESGCM +from cryptography.hazmat.primitives.kdf.hkdf import HKDF +from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID + + +def b64(data: bytes) -> str: + return base64.urlsafe_b64encode(data).decode() + + +def unb64(data: str) -> bytes: + return base64.b64decode(data.encode(), altchars=b"-_", validate=True) + + +def canonical(data: dict) -> bytes: + return json.dumps(data, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode() + + +def raw_public(key) -> str: + return b64(key.public_bytes(serialization.Encoding.Raw, serialization.PublicFormat.Raw)) + + +def pem_private(key) -> bytes: + return key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, + serialization.NoEncryption()) + + +def load_private(path): + protected = path.with_name(path.name + ".dpapi") + if protected.is_file(): + from .windows_keys import unprotect + data = unprotect(protected.read_bytes()) + else: + data = path.read_bytes() + return serialization.load_pem_private_key(data, password=None) + + +def load_public(path): + return serialization.load_pem_public_key(path.read_bytes()) + + +def fingerprint(pem: str) -> str: + return x509.load_pem_x509_certificate(pem.encode()).fingerprint(hashes.SHA256()).hex() + + +def verify_device(pem: str, ca_pem: bytes, host_id: str): + """v1 deliberately accepts only directly issued Ed25519 device certificates.""" + cert = x509.load_pem_x509_certificate(pem.encode()) + ca = x509.load_pem_x509_certificate(ca_pem) + now = datetime.now(timezone.utc) + if not (ca.not_valid_before_utc <= now <= ca.not_valid_after_utc): + raise ValueError("expired CA") + if not ca.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("not a CA") + cert.verify_directly_issued_by(ca) + if not (cert.not_valid_before_utc <= now <= cert.not_valid_after_utc): + raise ValueError("expired certificate") + if cert.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("CA cannot be used as device") + if ExtendedKeyUsageOID.CLIENT_AUTH not in cert.extensions.get_extension_for_class(x509.ExtendedKeyUsage).value: + raise ValueError("clientAuth missing") + names = cert.extensions.get_extension_for_class(x509.SubjectAlternativeName).value + if names.get_values_for_type(x509.UniformResourceIdentifier) != [f"urn:hainuo:host:{host_id}"]: + raise ValueError("host identity mismatch") + public = cert.public_key() + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("v1 requires Ed25519 device key") + return public + + +def sign(data: dict, key) -> dict: + return {**data, "signature": b64(key.sign(canonical(data)))} + + +def verified(data: dict, key) -> dict: + body = {k: v for k, v in data.items() if k != "signature"} + key.verify(unb64(data["signature"]), canonical(body)) + return body + + +def seal(payload: dict, recipient_public: str, signer, *, host_id: str, session_id: str) -> dict: + ephemeral = x25519.X25519PrivateKey.generate() + header = {"v": 1, "host_id": host_id, "session_id": session_id, + "ts": int(time.time()), "nonce": b64(secrets.token_bytes(12)), + "ephemeral": raw_public(ephemeral.public_key())} + shared = ephemeral.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(recipient_public))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + cipher = AESGCM(key).encrypt(unb64(header["nonce"]), canonical(payload), canonical(header)) + return sign({**header, "ciphertext": b64(cipher)}, signer) + + +def open_envelope(envelope: dict, recipient_private, sender_public, *, host_id: str, session_id: str) -> dict: + body = verified(envelope, sender_public) + if body["v"] != 1 or body["host_id"] != host_id or body["session_id"] != session_id: + raise ValueError("wrong destination/session") + if abs(time.time() - body["ts"]) > 90: + raise ValueError("expired envelope") + header = {k: v for k, v in body.items() if k != "ciphertext"} + shared = recipient_private.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(body["ephemeral"]))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + plain = AESGCM(key).decrypt(unb64(body["nonce"]), unb64(body["ciphertext"]), canonical(header)) + return json.loads(plain) + + +def issue_ca(): + key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Hainuo device test CA")]) + now = datetime.now(timezone.utc) + cert = (x509.CertificateBuilder().subject_name(name).issuer_name(name) + .public_key(key.public_key()).serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=3650)) + .add_extension(x509.BasicConstraints(ca=True, path_length=0), critical=True) + .add_extension(x509.KeyUsage(False, False, False, False, False, True, True, False, False), critical=True) + .sign(key, algorithm=None)) + return key, cert + + +def issue_device(ca_key, ca_cert, host_id: str, public_key, days=30): + now = datetime.now(timezone.utc) + return (x509.CertificateBuilder() + .subject_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, host_id)])) + .issuer_name(ca_cert.subject).public_key(public_key) + .serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=days)) + .add_extension(x509.BasicConstraints(ca=False, path_length=None), critical=True) + .add_extension(x509.SubjectAlternativeName([x509.UniformResourceIdentifier(f"urn:hainuo:host:{host_id}")]), critical=False) + .add_extension(x509.ExtendedKeyUsage([ExtendedKeyUsageOID.CLIENT_AUTH]), critical=False) + .sign(ca_key, algorithm=None)) diff --git a/patch-source/951d53cd16a9de5f/relay/device.py b/patch-source/951d53cd16a9de5f/relay/device.py new file mode 100644 index 0000000..a8fe010 --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/device.py @@ -0,0 +1,216 @@ +"""Reference PC bridge. No shell/PowerShell execution; local read-only diagnostics.""" +import asyncio +import contextlib +import json +import os +import secrets +import socket +import sqlite3 +import time +from pathlib import Path +from urllib.parse import urlsplit + +import httpx +from cryptography.hazmat.primitives.asymmetric import x25519 +from websockets.asyncio.client import connect + +from .crypto import (load_private, load_public, open_envelope, raw_public, seal, sign, + verified, verify_device) + + +class DeviceJournal: + def __init__(self, path): + path.parent.mkdir(parents=True, exist_ok=True) + self.db = sqlite3.connect(path) + self.db.execute("CREATE TABLE IF NOT EXISTS consumed(turn_id TEXT PRIMARY KEY, ts REAL NOT NULL)") + self.db.commit() + + def consume(self, turn): + with self.db: + return self.db.execute("INSERT OR IGNORE INTO consumed VALUES(?,?)", (turn, time.time())).rowcount == 1 + + def close(self): + self.db.close() + + +async def execute_readonly(text, root: Path | None): + if text in {"/主机名", "hostname"}: + return socket.gethostname() + if text.startswith("/回显 "): + return text[4:][:16000] + if text.startswith("/读取 "): + if root is None: + return "未配置测试文件目录。" + base = root.resolve(strict=True) + try: + path = (base / text[4:].strip()).resolve(strict=True) + if path == base or not path.is_relative_to(base) or not path.is_file(): + return "拒绝:文件不在允许的测试目录。" + if path.stat().st_size > 32768: + return "拒绝:测试文件超过32KB。" + return path.read_text(encoding="utf-8")[:16000] + except (OSError, UnicodeError, ValueError): + return "拒绝:文件不存在、不可读或不是UTF-8文本。" + return "当前桥接客户端仅开放只读诊断:/主机名、/回显 内容、/读取 相对路径;尚未接入海诺本地Agent接口。" + + +class LocalAgent: + """Explicit adapter contract, not a claim of compatibility with installer 1.065.""" + def __init__(self, url, transport=None): + parts = urlsplit(url) + if parts.scheme != "http" or parts.hostname not in {"127.0.0.1", "::1"} or parts.username or parts.password or parts.fragment: + raise ValueError("local Agent must use a fixed loopback HTTP endpoint") + self.url = url + self.http = httpx.AsyncClient(timeout=30, follow_redirects=False, transport=transport, trust_env=False) + + async def execute(self, text, owner, turn): + headers = {} + if os.environ.get("HN_LOCAL_AGENT_TOKEN"): + headers["Authorization"] = "Bearer " + os.environ["HN_LOCAL_AGENT_TOKEN"] + try: + async with self.http.stream("POST", self.url, headers=headers, + json={"text": text, "open_id": owner, "turn_id": turn, + "channel": "im", "read_only": True}) as response: + if response.status_code != 200: + return "本机Agent未返回成功结果,请查看本机服务状态。" + body = b"" + async for chunk in response.aiter_bytes(): + body += chunk + if len(body) > 65536: + return "本机Agent结果超过限制。" + result = json.loads(body)["result"] + if not isinstance(result, str): + return "本机Agent响应格式不匹配。" + return result[:16000] + except (httpx.HTTPError, ValueError, KeyError): + return "本机Agent连接失败或响应格式不匹配。" + + async def close(self): + await self.http.aclose() + + +async def bind_device(directory: Path, connector_url: str, code: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + private = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "code": code, "ts": int(time.time())}, private) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as http: + response = await http.post(connector_url.rstrip("/") + "/device/bind", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"binding failed: HTTP {response.status_code}") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt["host_id"] != config["host_id"]: + raise ValueError("wrong binding receipt") + config["owner"] = receipt["owner"] + temp = directory / "device.json.tmp" + temp.write_text(json.dumps(config, ensure_ascii=False, indent=2), encoding="utf-8") + temp.replace(directory / "device.json") + + +async def pair_browser(directory: Path, connector_url: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8-sig")) + if not config.get("owner"): + raise RuntimeError("device must be bound first") + key = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "owner": config["owner"], + "purpose": "h5-pair-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as client: + response = await client.post(connector_url.rstrip("/") + "/device/web-pair", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"browser pairing failed: HTTP {response.status_code}") + result = verified(response.json(), load_public(directory / "connector.pub")) + if result["host_id"] != config["host_id"]: + raise ValueError("wrong pairing receipt") + return result + + +async def run_device(directory: Path, router_url: str, **kwargs): + from .process_lock import DeviceProcessLock + with DeviceProcessLock(directory): + return await _run_device(directory, router_url, **kwargs) + + +async def _run_device(directory: Path, router_url: str, *, root=None, once=False, ssl_context=None, + stop_event=None, connected_event=None, agent_url=None, hainuo_config=None): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + if not config.get("owner"): + raise RuntimeError("bind device before starting") + host, owner = config["host_id"], config["owner"] + key = load_private(directory / "device.key") + connector = load_public(directory / "connector.pub") + cert = (directory / "device.pem").read_text(encoding="utf-8") + verify_device(cert, (directory / "ca.pem").read_bytes(), host) + journal = DeviceJournal(directory / "journal.sqlite") + if agent_url and hainuo_config: + raise ValueError("choose one Agent adapter") + if hainuo_config: + from .hainuo1065 import Hainuo1065Agent + agent = Hainuo1065Agent(hainuo_config) + else: + agent = LocalAgent(agent_url) if agent_url else None + stop_event = stop_event or asyncio.Event() + delay = 1 + try: + while not stop_event.is_set(): + try: + kwargs = {"ssl": ssl_context} if router_url.startswith("wss://") and ssl_context else {} + async with connect(router_url, max_size=262144, open_timeout=10, ping_interval=10, + proxy=None, **kwargs) as ws: + challenge = json.loads(await ws.recv())["challenge"] + ephemeral = x25519.X25519PrivateKey.generate() + session = secrets.token_hex(16) + proof = sign({"host_id": host, "session_id": session, "challenge": challenge, + "kx": raw_public(ephemeral.public_key()), "ts": int(time.time())}, key) + await ws.send(json.dumps({"host_id": host, "cert": cert, "proof": proof})) + accepted = json.loads(await ws.recv()) + if accepted.get("type") != "connected": + raise RuntimeError("device connection rejected") + print("设备连接成功,已绑定本人海能Work身份。", flush=True) + if connected_event: + connected_event.set() + delay = 1 + while not stop_event.is_set(): + try: + raw = await asyncio.wait_for(ws.recv(), timeout=1) + except asyncio.TimeoutError: + continue + data = json.loads(raw) + payload = open_envelope(data["envelope"], ephemeral, connector, + host_id=host, session_id=session) + if payload.get("owner") != owner or payload.get("type") != "instruction": + continue + turn = payload["turn_id"] + if not isinstance(turn, str) or len(turn) > 128: + continue + if not journal.consume(turn): + result = "该请求已处理,拒绝重复执行。" + elif payload.get("read_only") is not True or payload.get("channel") != "im": + result = "拒绝:通道或操作类型不受支持。" + else: + if agent and payload["text"].startswith("/助手 "): + result = await agent.execute(payload["text"][4:], owner, turn) + else: + result = await execute_readonly(payload["text"], root) + reply = seal({"type": "result", "turn_id": turn, "result": result}, + payload["reply_key"], key, host_id=host, session_id=session) + await ws.send(json.dumps({"type": "reply", "turn_id": turn, "envelope": reply})) + if once: + return + except asyncio.CancelledError: + raise + except Exception as error: + if once: + raise + from websockets.exceptions import ConnectionClosed + if isinstance(error, ConnectionClosed) and error.rcvd and error.rcvd.code == 1008: + print("设备被中继拒绝:请检查是否有另一桥接进程、设备准入或证书状态。", flush=True) + # No payloads/URLs/credentials in logs, bounded reconnect backoff + jitter. + print("设备连接中断,等待重连。", flush=True) + with contextlib.suppress(asyncio.TimeoutError): + await asyncio.wait_for(stop_event.wait(), timeout=delay + secrets.randbelow(1000) / 1000) + delay = min(delay * 2, 30) + finally: + journal.close() + if agent: + await agent.close() diff --git a/patch-source/951d53cd16a9de5f/relay/enrollment.py b/patch-source/951d53cd16a9de5f/relay/enrollment.py new file mode 100644 index 0000000..b4ac77f --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/enrollment.py @@ -0,0 +1,178 @@ +"""Device registration with key possession proof; Work binding is separate.""" +import json +import secrets +import time +import httpx + +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey +from fastapi import APIRouter, HTTPException, Request +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .crypto import verified, sign, raw_public, verify_device, fingerprint +from .web_session import digest +from fastapi.responses import JSONResponse + + +def validate_enrollment(body): + try: + key = serialization.load_pem_public_key(body.public_key.encode()) + if not isinstance(key, Ed25519PublicKey): + raise ValueError() + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["purpose"] != "device-enroll-v1" + or abs(time.time() - proof["ts"]) > 90 or len(proof["nonce"]) != 32): + raise ValueError() + return key + except Exception: + raise HTTPException(403, "invalid enrollment proof") from None + + +async def request_certificate(settings, body): + async with httpx.AsyncClient(trust_env=False, timeout=15, follow_redirects=False) as http: + response = await http.post(settings.enrollment_signer_url.rstrip("/") + "/issue", + headers={"X-Internal-Secret": settings.internal_secret}, + json=body.model_dump()) + response.raise_for_status() + return response.json()["cert"] + + +class EnrollmentRequest(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + public_key: str = Field(max_length=1024) + proof: dict + + +class EnrollmentApproval(BaseModel): + cert: str = Field(max_length=10000) + + +def enrollment_router(settings, redis, store, signer, admin): + router = APIRouter() + + @router.post("/device/register") + async def automatic_registration(body: EnrollmentRequest, request: Request): + # No human approval: eligibility follows intranet access plus Work app scope. + # Proof/TLS/limits protect identity and resources, and do not add user approval. + await rate_limit(redis, "auto-register-ip:" + peer(request), 120) + key = validate_enrollment(body) + await rate_limit(redis, "auto-register-host:" + body.host_id, 30) + existing = store.device(body.host_id) + + def check_existing(device): + if device["revoked"]: + raise HTTPException(403, "device revoked") + try: + registered = verify_device(device["cert"], settings.ca_cert.read_bytes(), body.host_id) + if raw_public(registered) != raw_public(key): + raise ValueError() + except Exception: + raise HTTPException(409, "existing device identity mismatch or certificate expired") from None + return device["cert"] + + if existing: + cert = check_existing(existing) + else: + if not settings.enrollment_signer_url: + raise HTTPException(503, "automatic registration is not enabled; update ECS first") + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= settings.max_devices: + raise HTTPException(503, "device capacity reached") + await rate_limit(redis, "auto-register-total", settings.max_pending, 3600) + try: + cert = await request_certificate(settings, body) + issued = verify_device(cert, settings.ca_cert.read_bytes(), body.host_id) + if raw_public(issued) != raw_public(key): + raise ValueError() + except Exception: + raise HTTPException(503, "certificate issuer unavailable or invalid response") from None + from .store import INTEGRITY_ERRORS + try: + store.add_device(body.host_id, cert, fingerprint(cert)) + except INTEGRITY_ERRORS: + # A retry/concurrent request must return the original certificate. + cert = check_existing(store.device(body.host_id)) + store.audit("device", "automatic_registration", body.host_id, "success") + await redis.set("hn:device:" + body.host_id, fingerprint(cert)) + return no_cache(JSONResponse(sign({"status": "registered", "host_id": body.host_id, + "cert": cert}, signer), status_code=201 if not existing else 200)) + + @router.post("/device/enroll") + async def request_enrollment(body: EnrollmentRequest, request: Request): + await rate_limit(redis, "enroll-ip:" + peer(request), 10) + validate_enrollment(body) + await rate_limit(redis, "enroll-host:" + body.host_id, 3, 3600) + if store.device(body.host_id): + raise HTTPException(409, "device already registered; recover its original certificate") + # Global rolling capacity bound; prevents anonymous request-table exhaustion. + await rate_limit(redis, "enroll-global", settings.max_pending, 1800) + identifier, claim_secret = secrets.token_hex(16), secrets.token_urlsafe(32) + record = {"host_id": body.host_id, "public_key": body.public_key, + "claim_hash": digest(claim_secret), "created": time.time(), "status": "pending"} + await redis.set("hn:enroll:" + identifier, json.dumps(record), ex=1800) + return no_cache(JSONResponse(sign({"request_id": identifier, "claim_secret": claim_secret, + "host_id": body.host_id, "expires_in": 1800}, signer), status_code=201)) + + @router.get("/device/enroll/{identifier}") + async def claim(identifier: str, request: Request): + await rate_limit(redis, "enroll-poll:" + peer(request), 60) + data = await redis.get("hn:enroll:" + identifier) + if not data: + raise HTTPException(404, "request expired") + record = json.loads(data) + import hmac + if not hmac.compare_digest(record["claim_hash"], digest(request.headers.get("X-Enrollment-Secret", ""))): + raise HTTPException(404, "request unavailable") + if record["status"] == "pending": + return no_cache(JSONResponse({"status": "pending"}, status_code=202)) + device = store.device(record["host_id"]) + if not device or device["revoked"]: + raise HTTPException(403, "device unavailable") + # Recoverable for its original claimant for the request TTL; public certificate only. + return no_cache(JSONResponse(sign({"status": "approved", "host_id": record["host_id"], + "request_id": identifier, "cert": device["cert"]}, signer))) + + @router.get("/admin/enrollments/{identifier}") + async def inspect(identifier: str, request: Request): + admin(request) + encoded = await redis.get("hn:enroll:" + identifier) + if not encoded: + raise HTTPException(404, "request expired") + record = json.loads(encoded) + return {k: v for k, v in record.items() if k != "claim_hash"} + + @router.post("/admin/enrollments/{identifier}/approve") + async def approve(identifier: str, body: EnrollmentApproval, request: Request): + admin(request) + encoded = await redis.get("hn:enroll:" + identifier) + if not encoded: + raise HTTPException(404, "request expired") + record = json.loads(encoded) + try: + public = verify_device(body.cert, settings.ca_cert.read_bytes(), record["host_id"]) + requested = serialization.load_pem_public_key(record["public_key"].encode()) + if raw_public(public) != raw_public(requested): + raise ValueError() + except Exception: + raise HTTPException(400, "certificate does not match enrollment") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= settings.max_devices: + raise HTTPException(503, "device capacity reached") + fp = fingerprint(body.cert) + existing = store.device(record["host_id"]) + if existing and (existing["revoked"] or existing["fingerprint"] != fp): + raise HTTPException(409, "device conflict") + if not existing: + from .store import INTEGRITY_ERRORS + try: + store.add_device(record["host_id"], body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "concurrent device registration") from None + await redis.set("hn:device:" + record["host_id"], fp) + record["status"] = "approved" + remaining = await redis.ttl("hn:enroll:" + identifier) + if remaining < 1: + raise HTTPException(409, "request expired during approval") + await redis.set("hn:enroll:" + identifier, json.dumps(record), ex=remaining) + store.audit("admin", "enrollment_approved", record["host_id"], "success") + return {"status": "approved", "host_id": record["host_id"]} + + return router diff --git a/patch-source/951d53cd16a9de5f/relay/enrollment_signer.py b/patch-source/951d53cd16a9de5f/relay/enrollment_signer.py new file mode 100644 index 0000000..6089d02 --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/enrollment_signer.py @@ -0,0 +1,43 @@ +"""Internal automatic CA issuer. No published host port, no application credentials.""" +import os +from pathlib import Path + +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from fastapi import FastAPI, Request +from .common import BodyLimitMiddleware, require_secret +from .crypto import load_private, issue_device, raw_public, verify_device +from .enrollment import EnrollmentRequest, validate_enrollment + + +def create_app(*, ca_key_path=None, ca_cert_path=None, secret=None): + key = load_private(Path(ca_key_path or os.environ.get("HN_ISSUER_CA_KEY", "/issuer/ca.key"))) + ca_bytes = Path(ca_cert_path or os.environ.get("HN_ISSUER_CA_CERT", "/trust/ca.pem")).read_bytes() + ca = x509.load_pem_x509_certificate(ca_bytes) + if raw_public(key.public_key()) != raw_public(ca.public_key()): + raise ValueError("issuer CA key mismatch") + token = secret or os.environ.get("HN_INTERNAL_SECRET", "") + if len(token) < 32: + raise ValueError("issuer requires internal credential") + app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=16384) + + @app.get("/ready") + async def ready(): + return {"status": "ready"} + + @app.post("/issue") + async def issue(body: EnrollmentRequest, request: Request): + require_secret(request.headers.get("X-Internal-Secret"), token) + public = validate_enrollment(body) + cert = issue_device(key, ca, body.host_id, public).public_bytes(serialization.Encoding.PEM).decode() + verify_device(cert, ca_bytes, body.host_id) + return {"cert": cert} + + return app + + +if __name__ == "__main__": + import uvicorn + uvicorn.run("relay.enrollment_signer:create_app", factory=True, host="0.0.0.0", port=8000, + workers=1, access_log=False, proxy_headers=False, log_level="warning") diff --git a/patch-source/951d53cd16a9de5f/relay/hainuo1065.py b/patch-source/951d53cd16a9de5f/relay/hainuo1065.py new file mode 100644 index 0000000..6f7735b --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/hainuo1065.py @@ -0,0 +1,107 @@ +"""Adapter for the native API actually found in installer 1.065. + +Loopback only, independent API key, no platform secrets, no redirects or retries. +The included native launcher offers only text inference with no tools. +""" +import hashlib +import json +import re +from pathlib import Path +from urllib.parse import urlsplit + +import httpx + + +class NativeAPIError(RuntimeError): + """Contains a sanitized status only, never response body or credentials.""" + + +def read_native_config(path): + path = Path(path) + data = json.loads(path.read_text(encoding="utf-8-sig")) + url = data.get("url", "") + parts = urlsplit(url) + if (parts.scheme != "http" or parts.hostname != "127.0.0.1" or not parts.port + or parts.username or parts.password or parts.query or parts.fragment + or parts.path not in {"", "/"}): + raise ValueError("native API must be a fixed IPv4 loopback HTTP base URL") + if not isinstance(data.get("api_key"), str) or len(data["api_key"]) < 32: + raise ValueError("native API requires a dedicated key of at least 32 characters") + if data.get("mode") not in {"text-only", "profile-tools"}: + raise ValueError("unsupported native API mode") + data["url"] = url.rstrip("/") + return data + + +class Hainuo1065Agent: + def __init__(self, config_path, transport=None): + self.config = read_native_config(config_path) + self.http = httpx.AsyncClient(base_url=self.config["url"], timeout=120, + follow_redirects=False, trust_env=False, transport=transport) + self.headers = {"Authorization": "Bearer " + self.config["api_key"]} + + async def _json(self, method, path, **kwargs): + try: + async with self.http.stream(method, path, headers={**self.headers, **kwargs.pop("headers", {})}, **kwargs) as response: + if response.status_code != 200: + raise NativeAPIError(f"native API HTTP {response.status_code}") + body = bytearray() + async for chunk in response.aiter_bytes(): + body.extend(chunk) + if len(body) > 262144: + raise NativeAPIError("native API response exceeds limit") + data = json.loads(body) + if not isinstance(data, dict): + raise NativeAPIError("native API response is not an object") + return data + except (httpx.HTTPError, ValueError): + raise NativeAPIError("native API connection/JSON failure") from None + + async def probe(self): + health = await self._json("GET", "/health") + if health.get("platform") != "hermes-agent" or health.get("status") != "ok": + raise NativeAPIError("endpoint is not the native Hermes API") + capabilities = await self._json("GET", "/v1/capabilities") + policy = capabilities.get("hainuo_relay_policy", {}) + # A stock unrestricted API endpoint is deliberately not sufficient. + expected_mode = self.config["mode"] + expected_tools = "profile" if expected_mode == "profile-tools" else "disabled" + if policy != {"mode": expected_mode, "tools": expected_tools, "contract": "hainuo-1065-v1"}: + raise NativeAPIError("native API policy missing; start the supplied restricted launcher") + models = await self._json("GET", "/v1/models") + model = self.config.get("model") + available = models.get("data") + if not isinstance(available, list) or not available or not all(isinstance(x, dict) and isinstance(x.get("id"), str) for x in available): + raise NativeAPIError("native model list invalid") + if model and model not in {x["id"] for x in available}: + raise NativeAPIError("configured model alias unavailable") + self.model = model or available[0]["id"] + return {"status": "ready", "platform": "hermes-agent", "model": self.model, + "mode": expected_mode, "version": health.get("version", "unknown")} + + async def execute(self, text, owner, turn): + try: + # Recheck the actual local service before each inference, including reconnects. + await self.probe() + digest = hashlib.sha256((owner + "\0" + turn).encode()).hexdigest() + data = await self._json("POST", "/v1/chat/completions", + headers={"Idempotency-Key": "hn-" + digest}, + json={"model": self.model, "stream": False, + "messages": [{"role": "user", "content": text}]}) + choices = data.get("choices") + if not isinstance(choices, list) or not choices or not isinstance(choices[0], dict): + raise NativeAPIError("native completion choices missing") + choice = choices[0] + message = choice.get("message", {}) + if (choice.get("finish_reason") != "stop" or message.get("tool_calls") + or not isinstance(message.get("content"), str) + or data.get("hermes", {}).get("failed") + or data.get("hermes", {}).get("partial")): + raise NativeAPIError("native completion incomplete or unexpected tool call") + return message["content"][:16000] + except NativeAPIError as error: + # Safe generated error strings only; never echo upstream model/API errors. + return "海诺原生接口未完成请求:" + str(error) + "。未自动重试。" + + async def close(self): + await self.http.aclose() diff --git a/patch-source/951d53cd16a9de5f/relay/longconn.py b/patch-source/951d53cd16a9de5f/relay/longconn.py new file mode 100644 index 0000000..5c12bbf --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/longconn.py @@ -0,0 +1,74 @@ +"""Optional official SDK event source, private platform domain explicitly configured.""" +import json +import logging +import os +import threading +import time + +import httpx +import redis +from .config import Settings + + +def main(): + import lark_oapi as lark + s = Settings(role="longconn") + if not s.feishu_enabled or s.feishu_event_source != "longconn": + raise RuntimeError("platform is disabled") + # The SDK may log websocket URLs, tokens or event bodies. Disable its logging entirely. + logging.disable(logging.CRITICAL) + r = redis.Redis.from_url(s.redis_url) + lease = r.lock("hn:longconn:leader", timeout=30, thread_local=False) + if not lease.acquire(blocking=False): + raise RuntimeError("another long-connection consumer owns the lease") + url = os.environ.get("HN_CONNECTOR_INTERNAL_URL", "http://connector:8000") + client = httpx.Client(base_url=url, timeout=10, follow_redirects=False) + done = threading.Event() + + def maintain_lease(): + while not done.wait(8): + try: + lease.extend(30, replace_ttl=True) + except Exception: + # Fail closed. Continuing after lease loss could create two consumers. + os._exit(2) + + threading.Thread(target=maintain_lease, daemon=True).start() + + def received(event): + data = json.loads(lark.JSON.marshal(event)) + response = client.post("/internal/events", headers={"X-Internal-Secret": s.internal_secret}, json=data) + if response.status_code != 200: + raise RuntimeError("connector did not accept event") + print(json.dumps({"event": "platform_event_accepted", "type": data.get("header", {}).get("event_type")}), flush=True) + + handler = lark.EventDispatcherHandler.builder("", "").register_p2_im_message_receive_v1(received).build() + class ObservableClient(lark.ws.Client): + async def _connect(self): + try: + await super()._connect() + if self._conn is not None: + print(json.dumps({"event": "platform_connected", "api_host": s.feishu_api_base}), flush=True) + except Exception as error: + print(json.dumps({"event": "platform_connect_failed", "error_type": type(error).__name__}), flush=True) + raise + + try: + # domain is an actual supported SDK argument; no public-cloud-domain monkey patch. + ObservableClient(s.feishu_app_id, s.feishu_app_secret, event_handler=handler, + domain=s.feishu_api_base.rstrip("/"), log_level=lark.LogLevel.ERROR).start() + finally: + done.set() + client.close() + try: + lease.release() + except redis.exceptions.LockError: + pass + + +if __name__ == "__main__": + try: + main() + except Exception as error: + print(json.dumps({"event": "platform_consumer_stopped", "error_type": type(error).__name__}), flush=True) + raise SystemExit(1) from None diff --git a/patch-source/951d53cd16a9de5f/relay/oauth.py b/patch-source/951d53cd16a9de5f/relay/oauth.py new file mode 100644 index 0000000..b04ce6e --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/oauth.py @@ -0,0 +1,127 @@ +import asyncio +import hashlib +import hmac +import secrets +import time +from dataclasses import dataclass +from urllib.parse import urlencode + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .platform import PlatformError + + +@dataclass +class Transaction: + secret_hash: str + expires: float + status: str = "waiting" + result: dict | None = None + + +class Empty(BaseModel): + pass + + +class Refresh(BaseModel): + refresh_token: str = Field(min_length=1, max_length=4096) + + +class OAuth: + """A single connector process owns transactions and tokens in memory only.""" + def __init__(self, settings, redis, platform): + self.settings, self.redis, self.platform = settings, redis, platform + self.pending: dict[str, Transaction] = {} + self.lock = asyncio.Lock() + self.router = APIRouter() + self.router.add_api_route("/oauth/start", self.start, methods=["POST"], status_code=201) + self.router.add_api_route("/oauth/cb", self.callback, methods=["GET"]) + self.router.add_api_route("/oauth/claim", self.claim, methods=["GET"]) + self.router.add_api_route("/oauth/pending", self.status, methods=["GET"]) + self.router.add_api_route("/oauth/refresh", self.refresh, methods=["POST"]) + + def prune(self): + now = time.monotonic() + for state in list(self.pending): + if self.pending[state].expires <= now: + del self.pending[state] + + def authenticated(self, state, secret): + self.prune() + item = self.pending.get(state) + digest = hashlib.sha256((secret or "").encode()).hexdigest() + if not item or not hmac.compare_digest(item.secret_hash, digest): + raise HTTPException(404, "transaction not found") + return item + + async def start(self, body: Empty, request: Request): + if not self.settings.feishu_enabled or not self.settings.oauth_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.redis, "oauth:start:" + peer(request), 20) + async with self.lock: + self.prune() + if len(self.pending) >= self.settings.max_pending: + raise HTTPException(503, "too many pending transactions") + state, secret = secrets.token_urlsafe(32), secrets.token_urlsafe(32) + self.pending[state] = Transaction(hashlib.sha256(secret.encode()).hexdigest(), time.monotonic() + 600) + redirect = self.settings.public_url.rstrip("/") + "/oauth/cb" + url = self.settings.feishu_authorize_url + "?" + urlencode({"app_id": self.settings.feishu_app_id, + "redirect_uri": redirect, "state": state}) + return no_cache(JSONResponse({"state": state, "claim_secret": secret, "authorization_url": url, + "redirect_uri": redirect, "expires_in": 600, + "code_ttl": 60, "poll_interval": 2}, status_code=201)) + + async def callback(self, request: Request, state: str = "", code: str = ""): + await rate_limit(self.redis, "oauth:cb:" + peer(request)) + async with self.lock: + self.prune() + item = self.pending.get(state) + if not code or len(code) > 4096 or not item or item.status != "waiting": + raise HTTPException(400, "invalid callback") + item.status = "exchanging" + try: + result = await self.platform.exchange(code) + except PlatformError: + async with self.lock: + item.status = "failed" + raise HTTPException(502, "authorization exchange failed") from None + async with self.lock: + if state not in self.pending or item.expires <= time.monotonic(): + self.pending.pop(state, None) + raise HTTPException(400, "authorization expired") + item.result, item.status = result, "ready" + return no_cache(HTMLResponse("

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

")) + + async def claim(self, request: Request, state: str): + await rate_limit(self.redis, "oauth:claim:" + peer(request)) + async with self.lock: + item = self.authenticated(state, request.headers.get("X-OAuth-Claim-Secret")) + if item.status == "failed": + del self.pending[state] + raise HTTPException(502, "authorization failed") + if item.status != "ready": + return no_cache(JSONResponse({"status": "waiting", "retry_after": 2}, status_code=202)) + del self.pending[state] + return no_cache(JSONResponse({"status": "claimed", **item.result})) + + async def status(self, request: Request, state: str): + await rate_limit(self.redis, "oauth:pending:" + peer(request)) + async with self.lock: + item = self.authenticated(state, request.headers.get("X-OAuth-Claim-Secret")) + return no_cache(JSONResponse({"status": item.status})) + + async def refresh(self, body: Refresh, request: Request): + if not self.settings.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.redis, "oauth:refresh:" + peer(request), 20) + # Lock and consume before the upstream call; ambiguous failures never retry an old refresh token. + digest = hashlib.sha256(body.refresh_token.encode()).hexdigest() + if not await self.redis.set("hn:refresh:" + digest, "used", nx=True, ex=2592000): + raise HTTPException(409, "refresh already attempted; use returned token or reauthorize") + try: + result = await self.platform.refresh(body.refresh_token) + except PlatformError: + raise HTTPException(502, "refresh failed; reauthorize") from None + return no_cache(JSONResponse(result)) diff --git a/patch-source/951d53cd16a9de5f/relay/platform.py b/patch-source/951d53cd16a9de5f/relay/platform.py new file mode 100644 index 0000000..61c5193 --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/platform.py @@ -0,0 +1,89 @@ +import asyncio +import json +import time +import httpx + + +class PlatformError(Exception): + pass + + +class Feishu: + def __init__(self, settings, transport=None): + self.settings = settings + self.http = httpx.AsyncClient(base_url=settings.feishu_api_base.rstrip("/"), + timeout=15, follow_redirects=False, transport=transport) + self._token = None + self._expires = 0 + self._tenant_token = None + self._tenant_expires = 0 + self._lock = asyncio.Lock() + + async def close(self): + await self.http.aclose() + + async def _json(self, method, path, **kwargs): + try: + response = await self.http.request(method, path, **kwargs) + response.raise_for_status() + data = response.json() + if data.get("code", 0) != 0: + raise PlatformError("platform rejected request") + return data + except (httpx.HTTPError, ValueError, KeyError) as exc: + # Never propagate upstream bodies or credentials to caller/logs. + raise PlatformError("platform request failed") from None + + async def app_token(self): + async with self._lock: + if self._token and self._expires > time.monotonic() + 1800: + return self._token + data = await self._json("POST", "/open-apis/auth/v3/app_access_token/internal", + json={"app_id": self.settings.feishu_app_id, + "app_secret": self.settings.feishu_app_secret}) + self._token = data["app_access_token"] + self._expires = time.monotonic() + int(data.get("expire", 7200)) + return self._token + + async def exchange(self, code): + token = await self.app_token() + data = await self._json("POST", "/open-apis/authen/v1/oidc/access_token", + headers={"Authorization": f"Bearer {token}"}, + json={"grant_type": "authorization_code", "code": code}) + return self.normalize(data["data"]) + + async def tenant_token(self): + async with self._lock: + if self._tenant_token and self._tenant_expires > time.monotonic() + 1800: + return self._tenant_token + data = await self._json("POST", "/open-apis/auth/v3/tenant_access_token/internal", + json={"app_id": self.settings.feishu_app_id, + "app_secret": self.settings.feishu_app_secret}) + self._tenant_token = data["tenant_access_token"] + self._tenant_expires = time.monotonic() + int(data.get("expire", 7200)) + return self._tenant_token + + async def refresh(self, refresh_token): + token = await self.app_token() + data = await self._json("POST", "/open-apis/authen/v1/oidc/refresh_access_token", + headers={"Authorization": f"Bearer {token}"}, + json={"grant_type": "refresh_token", "refresh_token": refresh_token}) + return self.normalize(data["data"]) + + @staticmethod + def normalize(data): + return {"user_access_token": data["access_token"], "refresh_token": data.get("refresh_token", ""), + "expires_in": data.get("expires_in", data.get("expire", 0)), + "refresh_expires_in": data.get("refresh_expires_in", 0), "open_id": data["open_id"]} + + async def user_info(self, token): + data = await self._json("GET", "/open-apis/authen/v1/user_info", + headers={"Authorization": f"Bearer {token}"}) + return data["data"] + + async def reply(self, message_id, text): + # Reply to the original message only: no caller-controlled recipient. + token = await self.tenant_token() + await self._json("POST", f"/open-apis/im/v1/messages/{message_id}/reply", + headers={"Authorization": f"Bearer {token}"}, + json={"msg_type": "text", "content": json.dumps({"text": text}, ensure_ascii=False)}) diff --git a/patch-source/951d53cd16a9de5f/relay/process_lock.py b/patch-source/951d53cd16a9de5f/relay/process_lock.py new file mode 100644 index 0000000..c37da53 --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/process_lock.py @@ -0,0 +1,46 @@ +"""Per-device advisory process lock: same-user GUI and CLI cannot race.""" +import os +from pathlib import Path + + +class DeviceAlreadyRunning(RuntimeError): + pass + + +class DeviceProcessLock: + def __init__(self, directory): + self.path = Path(directory) / "bridge.lock" + self.file = None + + def __enter__(self): + self.path.parent.mkdir(parents=True, exist_ok=True) + self.file = self.path.open("a+b") + self.file.seek(0, 2) + if self.file.tell() == 0: + self.file.write(b"0") + self.file.flush() + self.file.seek(0) + try: + if os.name == "nt": + import msvcrt + msvcrt.locking(self.file.fileno(), msvcrt.LK_NBLCK, 1) + else: + import fcntl + fcntl.flock(self.file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) + except OSError: + self.file.close() + self.file = None + raise DeviceAlreadyRunning("该设备已有桥接进程运行,请勿重复启动。") from None + return self + + def __exit__(self, *args): + if self.file: + self.file.seek(0) + if os.name == "nt": + import msvcrt + msvcrt.locking(self.file.fileno(), msvcrt.LK_UNLCK, 1) + else: + import fcntl + fcntl.flock(self.file.fileno(), fcntl.LOCK_UN) + self.file.close() + self.file = None diff --git a/patch-source/951d53cd16a9de5f/relay/router.py b/patch-source/951d53cd16a9de5f/relay/router.py new file mode 100644 index 0000000..e8d91e1 --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/router.py @@ -0,0 +1,167 @@ +import asyncio +import contextlib +import json +import secrets +import time +from contextlib import asynccontextmanager + +from fastapi import FastAPI, WebSocket, WebSocketDisconnect +from redis.asyncio import Redis +from redis.exceptions import WatchError + +from .config import Settings +from .crypto import fingerprint, raw_public, verified, verify_device + + +async def compare_delete(redis, key, expected): + async with redis.pipeline() as pipe: + try: + await pipe.watch(key) + if await pipe.get(key) != expected: + return False + pipe.multi() + pipe.delete(key) + await pipe.execute() + return True + except WatchError: + return False + + +async def compare_expire(redis, key, expected, ttl): + async with redis.pipeline() as pipe: + try: + await pipe.watch(key) + if await pipe.get(key) != expected: + return False + pipe.multi() + pipe.expire(key, ttl) + await pipe.execute() + return True + except WatchError: + return False + + +def create_app(settings=None, redis=None): + s = settings or Settings(role="router") + r = redis or Redis.from_url(s.redis_url, decode_responses=True) + ca = s.ca_cert.read_bytes() + active = set() + + @asynccontextmanager + async def lifespan(app): + await r.ping() + yield + if redis is None: + await r.aclose() + + app = FastAPI(title="Hainuo B2 router", lifespan=lifespan, docs_url=None, redoc_url=None) + app.state.redis = r + + @app.get("/health") + async def health(): + return {"status": "alive", "role": "router"} + + @app.get("/ready") + async def ready(): + await r.ping() + return {"status": "ready", "connections": len(active)} + + @app.websocket("/device/ws") + async def device(ws: WebSocket): + from .common import rate_limit + from fastapi import HTTPException + await ws.accept() + if len(active) >= s.max_connections: + await ws.close(1013) + return + ip = ws.client.host if ws.client else "unknown" + try: + await rate_limit(r, "ws:" + ip, 30) + except HTTPException: + await ws.close(1008) + return + challenge = secrets.token_urlsafe(32) + host = session = presence = None + tasks = [] + pub = None + try: + await ws.send_json({"type": "challenge", "challenge": challenge}) + hello = await asyncio.wait_for(ws.receive_json(), timeout=10) + if len(json.dumps(hello)) > 20000: + raise ValueError("oversized hello") + host = hello["host_id"] + if not isinstance(host, str) or len(host) != 32 or any(c not in "0123456789abcdef" for c in host): + raise ValueError("invalid host") + public = verify_device(hello["cert"], ca, host) + proof = verified(hello["proof"], public) + if proof["challenge"] != challenge or proof["host_id"] != host or abs(time.time() - proof["ts"]) > 90: + raise ValueError("invalid proof") + if len(proof["kx"]) > 64 or len(proof["session_id"]) > 64: + raise ValueError("invalid session") + fp = fingerprint(hello["cert"]) + if await r.get("hn:device:" + host) != fp or await r.exists("hn:revoked:" + host): + raise ValueError("unregistered/revoked") + session = proof["session_id"] + presence = json.dumps({"cert": hello["cert"], "proof": hello["proof"], "challenge": challenge}) + if not await r.set("hn:online:" + host, presence, nx=True, ex=30): + raise ValueError("already connected") + active.add(session) + pub = r.pubsub() + await pub.subscribe("hn:down:" + host, "hn:kill:" + host) + await ws.send_json({"type": "connected", "session_id": session}) + + async def downstream(): + async for msg in pub.listen(): + if msg["type"] != "message": + continue + if msg["channel"] == "hn:kill:" + host: + await ws.close(1008) + return + await ws.send_text(msg["data"]) + + async def heartbeat(): + while True: + await asyncio.sleep(8) + if await r.exists("hn:revoked:" + host) or not await compare_expire(r, "hn:online:" + host, presence, 30): + await ws.close(1008) + return + + async def upstream(): + while True: + text = await ws.receive_text() + if len(text.encode()) > s.max_body: + raise ValueError("oversized frame") + data = json.loads(text) + if data.get("type") == "ping": + continue + await rate_limit(r, "ws:reply:" + host, 120) + turn = data.get("turn_id", "") + if data.get("type") != "reply" or await r.get(f"hn:inflight:{host}:{turn}") != session: + raise ValueError("unsolicited reply") + envelope = data["envelope"] + verified(envelope, public) + if envelope["host_id"] != host or envelope["session_id"] != session: + raise ValueError("wrong reply destination") + await r.publish("hn:replies", json.dumps(data)) + + tasks = [asyncio.create_task(x()) for x in (downstream, heartbeat, upstream)] + done, _ = await asyncio.wait(tasks, return_when=asyncio.FIRST_COMPLETED) + for task in done: + task.result() + except (WebSocketDisconnect, asyncio.TimeoutError, ValueError, KeyError, TypeError): + with contextlib.suppress(Exception): + await ws.close(1008) + except Exception: + with contextlib.suppress(Exception): + await ws.close(1011) + finally: + for task in tasks: + task.cancel() + await asyncio.gather(*tasks, return_exceptions=True) + if pub: + await pub.aclose() + if presence: + await compare_delete(r, "hn:online:" + host, presence) + active.discard(session) + + return app diff --git a/patch-source/951d53cd16a9de5f/relay/store.py b/patch-source/951d53cd16a9de5f/relay/store.py new file mode 100644 index 0000000..9d6f8ef --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/store.py @@ -0,0 +1,97 @@ +"""Connector-owned persistent metadata. Never stores message/file/token bodies.""" +import sqlite3 +import time +import psycopg +from psycopg.rows import dict_row + +INTEGRITY_ERRORS = (sqlite3.IntegrityError, psycopg.IntegrityError) + + +class Postgres: + def __init__(self, url): + self.url = url + self.conn = psycopg.connect(url, autocommit=True, row_factory=dict_row) + self.transaction = None + + def execute(self, sql, params=()): + if self.conn.closed or self.conn.broken: + self.conn = psycopg.connect(self.url, autocommit=True, row_factory=dict_row) + return self.conn.execute(sql.replace("?", "%s"), params) + + def __enter__(self): + self.transaction = self.conn.transaction() + self.transaction.__enter__() + return self + + def __exit__(self, *args): + return self.transaction.__exit__(*args) + + def close(self): + self.conn.close() + + +class Store: + def __init__(self, path, database_url=""): + path.parent.mkdir(parents=True, exist_ok=True) + schema = """ + CREATE TABLE IF NOT EXISTS devices( + host_id TEXT PRIMARY KEY, cert TEXT NOT NULL, fingerprint TEXT NOT NULL, + owner TEXT UNIQUE, revoked INTEGER NOT NULL DEFAULT 0); + CREATE TABLE IF NOT EXISTS turns( + turn_id TEXT PRIMARY KEY, owner TEXT NOT NULL, host_id TEXT NOT NULL, + status TEXT NOT NULL, created DOUBLE PRECISION NOT NULL); + CREATE TABLE IF NOT EXISTS audit( + id INTEGER PRIMARY KEY, ts DOUBLE PRECISION NOT NULL, actor TEXT NOT NULL, + action TEXT NOT NULL, resource TEXT NOT NULL, result TEXT NOT NULL); + """ + if database_url: + self.db = Postgres(database_url) + self.db.conn.execute(schema.replace("id INTEGER PRIMARY KEY", "id BIGSERIAL PRIMARY KEY")) + else: + self.db = sqlite3.connect(path, check_same_thread=False) + self.db.row_factory = sqlite3.Row + self.db.executescript("PRAGMA journal_mode=WAL;" + schema) + + def device(self, host): + row = self.db.execute("SELECT * FROM devices WHERE host_id=?", (host,)).fetchone() + return dict(row) if row else None + + def by_owner(self, owner): + row = self.db.execute("SELECT * FROM devices WHERE owner=? AND revoked=0", (owner,)).fetchone() + return dict(row) if row else None + + def add_device(self, host, cert, fp): + with self.db: + self.db.execute("INSERT INTO devices(host_id,cert,fingerprint) VALUES(?,?,?)", (host, cert, fp)) + + def bind(self, host, owner): + with self.db: + # Conditional update also protects against two connectors racing for the same device. + count = self.db.execute("UPDATE devices SET owner=? WHERE host_id=? AND owner IS NULL AND revoked=0", (owner, host)).rowcount + if count != 1: + raise ValueError("device unavailable or already bound") + + def unbind(self, owner): + with self.db: + self.db.execute("UPDATE devices SET owner=NULL WHERE owner=?", (owner,)) + + def revoke(self, host): + with self.db: + self.db.execute("UPDATE devices SET revoked=1 WHERE host_id=?", (host,)) + + def begin_turn(self, turn, owner, host): + with self.db: + return self.db.execute("INSERT INTO turns VALUES(?,?,?,?,?) ON CONFLICT(turn_id) DO NOTHING", + (turn, owner, host, "inflight", time.time())).rowcount == 1 + + def finish(self, turn, status): + with self.db: + self.db.execute("UPDATE turns SET status=? WHERE turn_id=?", (status, turn)) + + def audit(self, actor, action, resource, result): + with self.db: + self.db.execute("INSERT INTO audit(ts,actor,action,resource,result) VALUES(?,?,?,?,?)", + (time.time(), actor, action, resource, result)) + + def close(self): + self.db.close() diff --git a/patch-source/951d53cd16a9de5f/relay/web_session.py b/patch-source/951d53cd16a9de5f/relay/web_session.py new file mode 100644 index 0000000..1ac9adf --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/web_session.py @@ -0,0 +1,183 @@ +"""Pilot H5 pairing and conversation. Credentials never appear in URLs.""" +import hashlib +import json +import secrets +import time +from urllib.parse import urlsplit + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import JSONResponse +from pydantic import BaseModel, Field + +from .common import no_cache, peer, rate_limit +from .crypto import sign, verified, verify_device +from .platform import PlatformError + +COOKIE = "hn_web_session" + + +def digest(value): + return hashlib.sha256(value.encode()).hexdigest() + + +def origin_tuple(value): + try: + parts = urlsplit(value) + if parts.scheme not in {"https", "http"} or not parts.hostname or parts.username or parts.password: + return None + return parts.scheme, parts.hostname.lower(), parts.port or (443 if parts.scheme == "https" else 80) + except ValueError: + return None + + +class PairProof(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Claim(BaseModel): + code: str = Field(min_length=12, max_length=128) + + +class Chat(BaseModel): + text: str = Field(min_length=1, max_length=8000) + request_id: str = Field(pattern=r"^[a-f0-9]{32}$") + + +class WebSessions: + def __init__(self, settings, redis, store, broker, platform): + self.s, self.r, self.store, self.broker, self.platform = settings, redis, store, broker, platform + self.router = APIRouter() + for path, method, handler in [ + ("/device/web-pair", "POST", self.device_pair), + ("/web/claim", "POST", self.claim), + ("/web/session", "GET", self.session), + ("/web/platform-session", "POST", self.platform_login), + ("/web/chat", "POST", self.chat), + ("/web/logout", "POST", self.logout), + ]: + self.router.add_api_route(path, handler, methods=[method]) + + def check_origin(self, request): + actual = origin_tuple(request.headers.get("Origin", "")) + if not actual or actual != origin_tuple(self.s.public_url): + raise HTTPException(403, "same-origin browser request required") + + async def valid_device(self, record): + device = self.store.device(record["host_id"]) + if (not device or device["revoked"] or device["owner"] != record["owner"] + or device["fingerprint"] != record["fingerprint"]): + raise HTTPException(401, "device binding changed; pair again") + generation = await self.r.get("hn:web-generation:" + record["host_id"]) or "0" + if record.get("generation") != generation: + raise HTTPException(401, "device binding changed; pair again") + return device + + async def device_pair(self, body: PairProof, request: Request): + await rate_limit(self.r, "web-pair-ip:" + peer(request), 20) + device = self.store.device(body.host_id) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "active bound device required") + try: + key = verify_device(device["cert"], self.s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["owner"] != device["owner"] + or proof["purpose"] != "h5-pair-v1" or abs(time.time() - proof["ts"]) > 90 + or not isinstance(proof["nonce"], str) or len(proof["nonce"]) != 32): + raise ValueError("invalid proof") + except Exception: + raise HTTPException(403, "invalid device proof") from None + replay = "hn:web-pair-proof:" + digest(body.host_id + proof["nonce"]) + if not await self.r.set(replay, "used", nx=True, ex=180): + raise HTTPException(409, "pairing request already used") + await rate_limit(self.r, "web-pair-device:" + body.host_id, 5) + code = secrets.token_urlsafe(18) + record = {"host_id": body.host_id, "owner": device["owner"], "fingerprint": device["fingerprint"], + "generation": await self.r.get("hn:web-generation:" + body.host_id) or "0"} + await self.r.set("hn:web-code:" + digest(code), json.dumps(record), ex=120) + self.store.audit(device["owner"], "web_pair_issued", body.host_id, "success") + return no_cache(JSONResponse(sign({"host_id": body.host_id, "code": code, "expires_in": 120, + "url": self.s.public_url.rstrip("/") + "/h5"}, self.broker.signer))) + + async def new_session(self, record): + token = secrets.token_urlsafe(32) + record = {k: record[k] for k in ("host_id", "owner", "fingerprint", "generation")} + await self.r.set("hn:web-session:" + digest(token), json.dumps(record), ex=1800) + response = no_cache(JSONResponse({"status": "paired", "host_id": record["host_id"], "expires_in": 1800})) + response.set_cookie(COOKIE, token, max_age=1800, secure=True, httponly=True, samesite="strict", path="/") + return response + + async def claim(self, body: Claim, request: Request): + self.check_origin(request) + await rate_limit(self.r, "web-claim:" + peer(request), 20) + key = "hn:web-code:" + digest(body.code.strip()) + encoded = await self.r.get(key) + if not encoded: + raise HTTPException(404, "code expired or consumed") + await self.valid_device(json.loads(encoded)) + encoded = await self.r.getdel(key) + if not encoded: + raise HTTPException(404, "code already consumed") + record = json.loads(encoded) + await self.valid_device(record) + self.store.audit(record["owner"], "web_pair_claimed", record["host_id"], "success") + return await self.new_session(record) + + async def authenticated(self, request): + token = request.cookies.get(COOKIE, "") + if not token or len(token) > 128: + raise HTTPException(401, "pair this browser first") + encoded = await self.r.get("hn:web-session:" + digest(token)) + if not encoded: + raise HTTPException(401, "browser session expired") + record = json.loads(encoded) + await self.valid_device(record) + return record + + async def session(self, request: Request): + record = await self.authenticated(request) + return no_cache(JSONResponse({"authenticated": True, "host_id": record["host_id"], + "online": bool(await self.r.exists("hn:online:" + record["host_id"])), + "mode": "pilot-text-only", "relay_sees_content": True})) + + async def platform_login(self, request: Request): + self.check_origin(request) + if not self.s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.r, "web-platform:" + peer(request), 20) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await self.platform.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = self.store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "bind your device first") + return await self.new_session({**device, "generation": await self.r.get("hn:web-generation:" + device["host_id"]) or "0"}) + + async def chat(self, body: Chat, request: Request): + self.check_origin(request) + record = await self.authenticated(request) + await rate_limit(self.r, "web-chat:" + record["owner"], 20) + text = body.text.strip() + if not text: + raise HTTPException(422, "empty message") + # PC accepts only its bound owner and the existing IM read-only channel. + if not (text == "/主机名" or text.startswith(("/回显 ", "/读取 ", "/助手 "))): + text = "/助手 " + text + turn = "web_" + digest(record["owner"] + "\0" + body.request_id) + result = await self.broker.dispatch(record["owner"], text, turn) + # Revoke/unbind during a long model call invalidates the browser immediately. + await self.authenticated(request) + return no_cache(JSONResponse(result)) + + async def logout(self, request: Request): + self.check_origin(request) + token = request.cookies.get(COOKIE, "") + if token and len(token) <= 128: + await self.r.delete("hn:web-session:" + digest(token)) + response = no_cache(JSONResponse({"status": "logged_out"})) + response.delete_cookie(COOKIE, secure=True, httponly=True, samesite="strict", path="/") + return response diff --git a/patch-source/951d53cd16a9de5f/relay/windows_keys.py b/patch-source/951d53cd16a9de5f/relay/windows_keys.py new file mode 100644 index 0000000..0e84e19 --- /dev/null +++ b/patch-source/951d53cd16a9de5f/relay/windows_keys.py @@ -0,0 +1,40 @@ +"""Current-user Windows DPAPI for device keys; never uses machine-wide scope.""" +import ctypes +import os +from ctypes import wintypes + + +class Blob(ctypes.Structure): + _fields_ = [("cbData", wintypes.DWORD), ("pbData", ctypes.POINTER(ctypes.c_ubyte))] + + +def transform(data, decrypt=False): + if os.name != "nt": + raise RuntimeError("DPAPI is supported only on Windows") + buffer = (ctypes.c_ubyte * len(data)).from_buffer_copy(data) + source = Blob(len(data), buffer) + target = Blob() + crypt = ctypes.WinDLL("crypt32", use_last_error=True) + kernel = ctypes.WinDLL("kernel32", use_last_error=True) + if decrypt: + fn = crypt.CryptUnprotectData + args = [ctypes.byref(source), None, None, None, None, 1, ctypes.byref(target)] + else: + fn = crypt.CryptProtectData + args = [ctypes.byref(source), "Hainuo device identity", None, None, None, 1, ctypes.byref(target)] + fn.restype = wintypes.BOOL + if not fn(*args): + raise ctypes.WinError(ctypes.get_last_error()) + try: + return ctypes.string_at(target.pbData, target.cbData) + finally: + kernel.LocalFree.argtypes = [ctypes.c_void_p] + kernel.LocalFree(target.pbData) + + +def protect(data): + return transform(data) + + +def unprotect(data): + return transform(data, decrypt=True) diff --git a/patch-source/951d53cd16a9de5f/scripts/update-work1067.sh b/patch-source/951d53cd16a9de5f/scripts/update-work1067.sh new file mode 100644 index 0000000..7c279be --- /dev/null +++ b/patch-source/951d53cd16a9de5f/scripts/update-work1067.sh @@ -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.' diff --git a/patch-source/a8137fe7e675b0a9/hainuo-relay/Dockerfile.offline b/patch-source/a8137fe7e675b0a9/hainuo-relay/Dockerfile.offline new file mode 100644 index 0000000..8af3dff --- /dev/null +++ b/patch-source/a8137fe7e675b0a9/hainuo-relay/Dockerfile.offline @@ -0,0 +1,10 @@ +FROM python:3.12-slim-bookworm +ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 +WORKDIR /app +COPY wheelhouse /wheels +COPY requirements.lock pyproject.toml ./ +COPY relay ./relay +RUN pip install --no-index --find-links=/wheels --no-cache-dir -r requirements.lock && rm -rf /wheels +RUN chmod -R a+rX /app +USER 10001:10001 +ENTRYPOINT ["python", "-m", "relay.cli"] diff --git a/patch-source/a8137fe7e675b0a9/hainuo-relay/docs/FEISHU_PRIVATE_LONGCONN.md b/patch-source/a8137fe7e675b0a9/hainuo-relay/docs/FEISHU_PRIVATE_LONGCONN.md new file mode 100644 index 0000000..6fb9c81 --- /dev/null +++ b/patch-source/a8137fe7e675b0a9/hainuo-relay/docs/FEISHU_PRIVATE_LONGCONN.md @@ -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发到聊天或打进镜像。 diff --git a/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/__init__.py b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/__init__.py new file mode 100644 index 0000000..32754a7 --- /dev/null +++ b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/__init__.py @@ -0,0 +1 @@ +"""Hainuo relay 0.1.0.""" diff --git a/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/cli.py b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/cli.py new file mode 100644 index 0000000..fad7979 --- /dev/null +++ b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/cli.py @@ -0,0 +1,216 @@ +import argparse +import asyncio +import json +import os +import secrets +import ssl +import uuid +from datetime import datetime, timedelta, timezone +from pathlib import Path + +import httpx +from cryptography import x509 +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ed25519 +from cryptography.x509.oid import NameOID + +from .crypto import issue_ca, issue_device, load_private, pem_private + + +def write_new(path, data, private=False): + path.parent.mkdir(parents=True, exist_ok=True) + with path.open("xb") as f: + f.write(data if isinstance(data, bytes) else data.encode()) + if private and os.name != "nt": + path.chmod(0o600) + + +def init_project(root: Path, hostname: str): + if (root / ".env").exists(): + raise RuntimeError(".env exists; init never overwrites credentials") + ca_key, ca = issue_ca() + signer = ed25519.Ed25519PrivateKey.generate() + write_new(root / "operator/ca.key", pem_private(ca_key), True) + write_new(root / "runtime/trust/ca.pem", ca.public_bytes(serialization.Encoding.PEM)) + write_new(root / "runtime/connector/connector.key", pem_private(signer), True) + write_new(root / "runtime/trust/connector.pub", signer.public_key().public_bytes( + serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo)) + tls_key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, hostname)]) + now = datetime.now(timezone.utc) + tls = (x509.CertificateBuilder().subject_name(name).issuer_name(name).public_key(tls_key.public_key()) + .serial_number(x509.random_serial_number()).not_valid_before(now - timedelta(minutes=5)) + .not_valid_after(now + timedelta(days=30)) + .add_extension(x509.SubjectAlternativeName([x509.DNSName(hostname)]), critical=False) + .sign(tls_key, algorithm=None)) + write_new(root / "runtime/tls/server.key", pem_private(tls_key), True) + write_new(root / "runtime/tls/server.pem", tls.public_bytes(serialization.Encoding.PEM)) + redis_secret = secrets.token_hex(32) + db_secret = secrets.token_hex(32) + env = (f"HN_ENVIRONMENT=development\nHN_PUBLIC_HOST={hostname}\nHN_PUBLIC_URL=https://{hostname}\n" + f"HN_ROUTER_URL=wss://{hostname}/device/ws\nHN_ADMIN_SECRET={secrets.token_hex(32)}\n" + f"HN_INTERNAL_SECRET={secrets.token_hex(32)}\nHN_REDIS_PASSWORD={redis_secret}\n" + f"HN_REDIS_URL=redis://:{redis_secret}@redis:6379/0\n" + f"HN_POSTGRES_PASSWORD={db_secret}\nHN_DATABASE_URL=postgresql://hainuo:{db_secret}@postgres:5432/hainuo\n" + "HN_FEISHU_ENABLED=false\nHN_FEISHU_API_BASE=https://open.feishu.cn\n" + "HN_FEISHU_AUTHORIZE_URL=https://open.feishu.cn/open-apis/authen/v1/authorize\n" + "HN_FEISHU_APP_ID=\nHN_FEISHU_APP_SECRET=\nHN_FEISHU_ENCRYPT_KEY=\n" + "HN_FEISHU_VERIFICATION_TOKEN=\nHN_PROXY_ENABLED=false\nHN_DEVICE_DOMAIN=\n") + write_new(root / ".env", env, True) + print("已生成配置、连接器密钥、设备CA及临时TLS证书;未输出凭据。") + print("operator/ca.key 仅保留在签发工作站,不要上传到ECS。临时TLS证书上线前须替换。") + + +def device_request(directory): + host = uuid.uuid4().hex + private = ed25519.Ed25519PrivateKey.generate() + write_new(directory / "device.key", pem_private(private), True) + public = private.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo).decode() + write_new(directory / "request.json", json.dumps({"host_id": host, "public_key": public}, indent=2)) + print("已在本机生成私钥和 request.json;仅将request.json交给签发工作站。") + + +def issue(root, request_file, output): + request = json.loads(request_file.read_text(encoding="utf-8")) + host = request["host_id"] + if len(host) != 32 or any(c not in "0123456789abcdef" for c in host): + raise ValueError("invalid host identity") + ca = x509.load_pem_x509_certificate((root / "runtime/trust/ca.pem").read_bytes()) + public = serialization.load_pem_public_key(request["public_key"].encode()) + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("Ed25519 required") + cert = issue_device(load_private(root / "operator/ca.key"), ca, host, public) + pem = cert.public_bytes(serialization.Encoding.PEM) + write_new(output / "device.pem", pem) + write_new(output / "ca.pem", (root / "runtime/trust/ca.pem").read_bytes()) + write_new(output / "connector.pub", (root / "runtime/trust/connector.pub").read_bytes()) + write_new(output / "device.json", json.dumps({"host_id": host, "owner": None}, indent=2)) + write_new(output / "registration.json", json.dumps({"host_id": host, "cert": pem.decode()}, indent=2)) + print("已签发设备证书;将device.pem、ca.pem、connector.pub、device.json交回原设备。") + + +def main(): + parser = argparse.ArgumentParser(description="海诺中继部署和设备工具") + sub = parser.add_subparsers(dest="command", required=True) + p = sub.add_parser("init") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--hostname", required=True) + p = sub.add_parser("serve") + p.add_argument("role", choices=["connector", "router"]) + p.add_argument("--host", default="127.0.0.1") + p.add_argument("--port", type=int, default=8000) + p = sub.add_parser("device-request") + p.add_argument("--directory", type=Path, required=True) + p = sub.add_parser("issue-device") + p.add_argument("--root", type=Path, default=Path(".")) + p.add_argument("--request", type=Path, required=True) + p.add_argument("--output", type=Path, required=True) + p = sub.add_parser("register") + p.add_argument("--registration", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("bind") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p = sub.add_parser("device") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + p.add_argument("--test-root", type=Path) + p.add_argument("--agent-url", help="Explicit loopback read-only Agent adapter endpoint") + p.add_argument("--hainuo-config", type=Path, help="Native installer-1.065 API adapter config; alternative to --agent-url") + p = sub.add_parser("hainuo-probe") + p.add_argument("--config", type=Path, required=True) + p = sub.add_parser("web-pair") + p.add_argument("--directory", type=Path, required=True) + p.add_argument("--url", required=True) + p.add_argument("--tls-ca", type=Path) + for command in ("status", "binding-code", "dispatch", "revoke", "unbind", "reconcile"): + p = sub.add_parser(command) + p.add_argument("--url", default="http://127.0.0.1:18081") + p.add_argument("--tls-ca", type=Path) + if command in {"binding-code", "dispatch"}: + p.add_argument("--owner", required=True) + if command == "dispatch": + p.add_argument("--text", required=True) + p.add_argument("--turn-id", default=None) + if command in {"revoke", "unbind"}: + p.add_argument("--host-id", required=True) + args = parser.parse_args() + if args.command == "init": + init_project(args.root, args.hostname) + elif args.command == "serve": + import uvicorn + uvicorn.run(f"relay.{args.role}:create_app", factory=True, host=args.host, port=args.port, + workers=1, access_log=False, proxy_headers=False, + ws_max_size=262144, ws_max_queue=16, log_level="warning") + elif args.command == "device-request": + device_request(args.directory) + elif args.command == "issue-device": + issue(args.root, args.request, args.output) + elif args.command == "register": + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, follow_redirects=False, timeout=15) as client: + response = client.post(args.url.rstrip("/") + "/admin/devices", + headers={"X-Admin-Secret": secret}, + json=json.loads(args.registration.read_text(encoding="utf-8"))) + if response.status_code != 201: + raise RuntimeError(f"register failed: HTTP {response.status_code}") + print("设备登记成功。") + elif args.command == "bind": + import getpass + from .device import bind_device + code = getpass.getpass("Binding code: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + asyncio.run(bind_device(args.directory, args.url, code, tls_verify=context)) + print("绑定成功。") + elif args.command == "device": + from .device import run_device + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else None + asyncio.run(run_device(args.directory, args.url, root=args.test_root, ssl_context=context, + agent_url=args.agent_url, hainuo_config=args.hainuo_config)) + elif args.command == "hainuo-probe": + from .hainuo1065 import Hainuo1065Agent + async def probe(): + adapter = Hainuo1065Agent(args.config) + try: + print(json.dumps(await adapter.probe(), ensure_ascii=False, indent=2)) + finally: + await adapter.close() + asyncio.run(probe()) + elif args.command == "web-pair": + from .device import pair_browser + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + result = asyncio.run(pair_browser(args.directory, args.url, tls_verify=context)) + print("网页地址:" + result["url"]) + print("一次性网页配对码(120秒有效):" + result["code"]) + else: + import getpass + secret = os.environ.get("HN_ADMIN_SECRET") or getpass.getpass("Admin secret: ") + context = ssl.create_default_context(cafile=str(args.tls_ca)) if args.tls_ca else True + with httpx.Client(verify=context, timeout=200, follow_redirects=False) as client: + paths = {"status": "/admin/devices", "binding-code": "/admin/test/binding-code", + "dispatch": "/admin/test/dispatch", "reconcile": "/admin/reconcile"} + if args.command in {"revoke", "unbind"}: + if len(args.host_id) != 32 or any(c not in "0123456789abcdef" for c in args.host_id): + raise ValueError("invalid host identity") + path = f"/admin/devices/{args.host_id}/{args.command}" + else: + path = paths[args.command] + body = None + if args.command in {"binding-code", "dispatch"}: + body = {"open_id": args.owner, "text": getattr(args, "text", "binding")} + if getattr(args, "turn_id", None): + body["turn_id"] = args.turn_id + response = client.request("GET" if args.command == "status" else "POST", args.url.rstrip("/") + path, + headers={"X-Admin-Secret": secret}, json=body) + if response.status_code != 200: + raise RuntimeError(f"operation failed: HTTP {response.status_code}") + print(json.dumps(response.json(), ensure_ascii=False, indent=2)) + + +if __name__ == "__main__": + main() diff --git a/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/common.py b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/common.py new file mode 100644 index 0000000..c112b99 --- /dev/null +++ b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/common.py @@ -0,0 +1,61 @@ +import hashlib +import hmac +import json +import time +from fastapi import HTTPException, Request +from starlette.responses import JSONResponse + + +def require_secret(actual, expected): + if not actual or not hmac.compare_digest(actual, expected): + raise HTTPException(401, "unauthorized") + + +async def rate_limit(redis, bucket: str, limit=60, seconds=60): + # Atomic counter + expiry: a crash cannot leave a permanent bucket. + key = "hn:rate:" + hashlib.sha256(bucket.encode()).hexdigest() + ":" + str(int(time.time()) // seconds) + pipe = redis.pipeline(transaction=True) + pipe.incr(key) + pipe.expire(key, seconds * 2) + result = (await pipe.execute())[0] + if int(result) > limit: + raise HTTPException(429, "rate limited", headers={"Retry-After": str(seconds)}) + + +class BodyLimitMiddleware: + def __init__(self, app, max_body=262144): + self.app, self.max_body = app, max_body + + async def __call__(self, scope, receive, send): + if scope["type"] != "http": + return await self.app(scope, receive, send) + messages, size = [], 0 + while True: + message = await receive() + if message["type"] == "http.disconnect": + return + size += len(message.get("body", b"")) + if size > self.max_body: + return await JSONResponse({"detail": "body too large"}, status_code=413)(scope, receive, send) + messages.append(message) + if not message.get("more_body", False): + break + async def buffered(): + return messages.pop(0) if messages else await receive() + await self.app(scope, buffered, send) + + +def no_cache(response): + response.headers["Cache-Control"] = "no-store" + response.headers["X-Content-Type-Options"] = "nosniff" + response.headers["Referrer-Policy"] = "no-referrer" + return response + + +async def json_pub(redis, channel, data): + return await redis.publish(channel, json.dumps(data, separators=(",", ":"))) + + +def peer(request: Request): + # Do not trust arbitrary X-Forwarded-For; configure uvicorn trusted proxies explicitly. + return request.client.host if request.client else "unknown" diff --git a/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/config.py b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/config.py new file mode 100644 index 0000000..946b10c --- /dev/null +++ b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/config.py @@ -0,0 +1,64 @@ +from pathlib import Path +from pydantic import model_validator +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + model_config = SettingsConfigDict(env_prefix="HN_", extra="ignore", hide_input_in_errors=True) + environment: str = "production" + role: str = "connector" + redis_url: str = "redis://127.0.0.1:6379/0" + data_dir: Path = Path("runtime/connector") + database_url: str = "" + admin_secret: str = "" + internal_secret: str = "" + ca_cert: Path = Path("runtime/pki/ca.pem") + connector_key: Path = Path("runtime/pki/connector.key") + connector_public_key: Path = Path("runtime/pki/connector.pub") + public_url: str = "https://relay.example.invalid" + router_url: str = "wss://relay.example.invalid/device/ws" + device_domain: str = "" + feishu_api_base: str = "https://open.feishu.cn" + feishu_authorize_url: str = "https://open.feishu.cn/open-apis/authen/v1/authorize" + feishu_app_id: str = "" + feishu_app_secret: str = "" + feishu_encrypt_key: str = "" + feishu_verification_token: str = "" + feishu_enabled: bool = False + oauth_enabled: bool = True + feishu_event_source: str = "longconn" + proxy_enabled: bool = False + # A fixed allowlist, never a user supplied upstream URL. + proxy_paths: str = "/open-apis/drive/v1/files,/open-apis/docx/v1/documents" + request_timeout: int = 45 + max_body: int = 262144 + max_pending: int = 1000 + max_devices: int = 1000 + max_connections: int = 1000 + max_result: int = 16000 + allow_insecure_local: bool = False + + @model_validator(mode="after") + def validate_runtime(self): + if self.environment not in {"production", "test", "development"}: + raise ValueError("invalid environment") + if self.role == "connector" and min(len(self.admin_secret), len(self.internal_secret)) < 32: + raise ValueError("admin/internal secrets must each be at least 32 characters") + if self.role == "longconn" and len(self.internal_secret) < 32: + raise ValueError("internal credential must be at least 32 characters") + if self.role == "connector" and self.admin_secret == self.internal_secret: + raise ValueError("admin and internal credentials must be different") + if self.environment == "production": + if self.allow_insecure_local or not self.public_url.startswith("https://"): + raise ValueError("production requires HTTPS") + if not self.router_url.startswith("wss://"): + raise ValueError("production requires WSS") + if not self.feishu_api_base.startswith("https://"): + raise ValueError("platform API requires HTTPS") + if self.feishu_event_source not in {"longconn", "webhook"}: + raise ValueError("invalid platform event source") + if self.feishu_enabled and not all((self.feishu_app_id, self.feishu_app_secret)): + raise ValueError("platform app ID and secret are required") + if self.feishu_enabled and self.feishu_event_source == "webhook" and not all((self.feishu_encrypt_key, self.feishu_verification_token)): + raise ValueError("webhook requires encryption key and verification token") + return self diff --git a/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/connector.py b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/connector.py new file mode 100644 index 0000000..801716f --- /dev/null +++ b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/connector.py @@ -0,0 +1,499 @@ +import asyncio +import base64 +import contextlib +import hashlib +import hmac +import json +import re +import secrets +import sqlite3 +import time +import uuid +from contextlib import asynccontextmanager +from dataclasses import dataclass +from urllib.parse import unquote, urlsplit + +from cryptography.hazmat.primitives import hashes, padding +from cryptography.hazmat.primitives.asymmetric import x25519 +from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes +from fastapi import FastAPI, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from fastapi.exceptions import RequestValidationError +from pydantic import BaseModel, Field +from redis.asyncio import Redis + +from .common import BodyLimitMiddleware, no_cache, peer, rate_limit, require_secret +from .config import Settings +from .crypto import (fingerprint, load_private, open_envelope, raw_public, seal, + sign, verified, verify_device) +from .oauth import OAuth +from .platform import Feishu, PlatformError +from .store import Store, INTEGRITY_ERRORS + + +class Register(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + cert: str = Field(max_length=10000) + + +class Bind(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Instruction(BaseModel): + open_id: str = Field(min_length=1, max_length=128) + text: str = Field(min_length=1, max_length=8000) + # Stable identity chosen by upstream, not generated again on retries. + turn_id: str = Field(default_factory=lambda: uuid.uuid4().hex, pattern=r"^[A-Za-z0-9_-]{1,128}$") + + +@dataclass +class Pending: + future: asyncio.Future + private: object + public: object + host: str + session: str + + +class Broker: + def __init__(self, settings, redis, store): + self.s, self.r, self.store = settings, redis, store + self.signer = load_private(settings.connector_key) + self.pending: dict[str, Pending] = {} + + async def replies(self, pub): + async for message in pub.listen(): + if message["type"] != "message": + continue + try: + data = json.loads(message["data"]) + item = self.pending.get(data["turn_id"]) + if not item or item.future.done(): + continue + plain = open_envelope(data["envelope"], item.private, item.public, + host_id=item.host, session_id=item.session) + if plain["turn_id"] != data["turn_id"] or plain.get("type") != "result": + continue + result = plain.get("result", "") + if not isinstance(result, str) or len(result) > self.s.max_result: + continue + item.future.set_result({"status": "completed", "turn_id": data["turn_id"], "result": result}) + except Exception: + # Invalid packets never consume a pending result; do not log packet contents. + continue + + async def dispatch(self, owner, text, turn): + device = self.store.by_owner(owner) + if not device: + return {"status": "unavailable", "turn_id": turn, "result": "未绑定设备或设备不可用。"} + host = device["host_id"] + await rate_limit(self.r, "dispatch:" + owner, 30) + if len(self.pending) >= self.s.max_pending: + raise HTTPException(503, "too many in-flight requests") + presence = await self.r.get("hn:online:" + host) + if not presence or await self.r.exists("hn:revoked:" + host): + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线,请开机并连接网络。"} + try: + presence = json.loads(presence) + if fingerprint(presence["cert"]) != device["fingerprint"]: + raise ValueError("certificate changed") + public = verify_device(presence["cert"], self.s.ca_cert.read_bytes(), host) + proof = verified(presence["proof"], public) + if proof["host_id"] != host or proof["challenge"] != presence["challenge"]: + raise ValueError("invalid device session") + session = proof["session_id"] + except Exception: + self.store.audit(owner, "dispatch", host, "identity_rejected") + raise HTTPException(409, "device identity rejected") from None + if not self.store.begin_turn(turn, owner, host): + return {"status": "duplicate", "turn_id": turn, "result": "该请求已接收,不会重复执行。"} + private = x25519.X25519PrivateKey.generate() + future = asyncio.get_running_loop().create_future() + self.pending[turn] = Pending(future, private, public, host, session) + envelope = seal({"type": "instruction", "turn_id": turn, "owner": owner, + "text": text, "read_only": True, "channel": "im", + "reply_key": raw_public(private.public_key())}, proof["kx"], self.signer, + host_id=host, session_id=session) + key = f"hn:inflight:{host}:{turn}" + try: + await self.r.set(key, session, ex=self.s.request_timeout + 5) + count = await self.r.publish("hn:down:" + host, json.dumps({"type": "instruction", "envelope": envelope})) + if not count: + self.store.finish(turn, "offline") + return {"status": "offline", "turn_id": turn, "result": "你的电脑当前离线。"} + result = await asyncio.wait_for(future, timeout=self.s.request_timeout) + self.store.finish(turn, "completed") + self.store.audit(owner, "instruction", host, "completed") + return result + except asyncio.TimeoutError: + self.store.finish(turn, "unknown") + return {"status": "unknown", "turn_id": turn, + "result": "等待结果超时,执行状态未知;不要直接重发有副作用的任务。"} + finally: + self.pending.pop(turn, None) + await self.r.delete(key) + + +def create_app(settings=None, redis=None, platform=None): + s = settings or Settings() + r = redis or Redis.from_url(s.redis_url, decode_responses=True) + store = Store(s.data_dir / "metadata.sqlite", s.database_url) + f = platform or Feishu(s) + broker = Broker(s, r, store) + oauth = OAuth(s, r, f) + background = set() + + def spawn(coro): + task = asyncio.create_task(coro) + background.add(task) + task.add_done_callback(background.discard) + # Retrieve errors without displaying possibly sensitive exception text. + task.add_done_callback(lambda t: t.exception() if not t.cancelled() else None) + + @asynccontextmanager + async def lifespan(app): + await r.ping() + for row in store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall(): + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + pub = r.pubsub() + await pub.subscribe("hn:replies") + reply_task = asyncio.create_task(broker.replies(pub)) + app.state.reply_task = reply_task + async def sweep(): + while True: + await asyncio.sleep(30) + async with oauth.lock: + oauth.prune() + sweep_task = asyncio.create_task(sweep()) + try: + yield + finally: + for task in [reply_task, sweep_task, *background]: + task.cancel() + await asyncio.gather(reply_task, sweep_task, *background, return_exceptions=True) + await pub.aclose() + await f.close() + store.close() + if redis is None: + await r.aclose() + + app = FastAPI(title="Hainuo B1 connector", lifespan=lifespan, docs_url=None, redoc_url=None) + app.add_middleware(BodyLimitMiddleware, max_body=s.max_body) + app.include_router(oauth.router) + from .web_session import WebSessions + web_sessions = WebSessions(s, r, store, broker, f) + app.include_router(web_sessions.router) + app.state.store, app.state.broker, app.state.oauth = store, broker, oauth + app.state.platform, app.state.redis = f, r + + @app.exception_handler(RequestValidationError) + async def validation_error(request, exc): + # FastAPI's default error response includes invalid input values (possibly tokens). + return JSONResponse({"detail": "invalid request parameters"}, status_code=422) + + @app.middleware("http") + async def secure_response(request, call_next): + return no_cache(await call_next(request)) + + def admin(request): + require_secret(request.headers.get("X-Admin-Secret"), s.admin_secret) + + @app.get("/health") + async def health(): + return {"status": "alive", "role": "connector", "version": "0.1.0"} + + @app.get("/ready") + async def ready(): + await r.ping() + store.db.execute("SELECT 1") + if not getattr(app.state, "reply_task", None) or app.state.reply_task.done(): + raise HTTPException(503, "reply subscriber unavailable") + return {"status": "ready"} + + @app.post("/admin/devices", status_code=201) + async def register(body: Register, request: Request): + admin(request) + try: + verify_device(body.cert, s.ca_cert.read_bytes(), body.host_id) + except Exception: + raise HTTPException(400, "invalid device certificate") from None + if store.db.execute("SELECT COUNT(*) AS total FROM devices").fetchone()["total"] >= s.max_devices: + raise HTTPException(503, "device capacity reached") + try: + fp = fingerprint(body.cert) + store.add_device(body.host_id, body.cert, fp) + except INTEGRITY_ERRORS: + raise HTTPException(409, "device already exists") from None + await r.set("hn:device:" + body.host_id, fp) + store.audit("admin", "register", body.host_id, "success") + return {"host_id": body.host_id, "fingerprint": fp} + + @app.get("/admin/devices") + async def devices(request: Request): + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,owner,revoked FROM devices ORDER BY host_id").fetchall() + return {"devices": [{**dict(row), "online": bool(await r.exists("hn:online:" + row["host_id"]))} for row in rows]} + + @app.post("/admin/devices/{host}/revoke") + async def revoke(host: str, request: Request): + admin(request) + if not store.device(host): + raise HTTPException(404, "device not found") + # Persistent source of truth first; router block + disconnect follows. + store.revoke(host) + await r.incr("hn:web-generation:" + host) + await r.set("hn:revoked:" + host, "1") + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "revoke") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "revoked", "result": "设备已停用,结果已丢弃。"}) + store.audit("admin", "revoke", host, "success") + return {"status": "revoked"} + + @app.post("/admin/reconcile") + async def reconcile(request: Request): + """Rebuild router admission metadata after Redis recovery; no dialogue data.""" + admin(request) + rows = store.db.execute("SELECT host_id,fingerprint,revoked FROM devices").fetchall() + for row in rows: + await r.set("hn:device:" + row["host_id"], row["fingerprint"]) + if row["revoked"]: + await r.set("hn:revoked:" + row["host_id"], "1") + return {"devices": len(rows)} + + @app.post("/admin/devices/{host}/unbind") + async def admin_unbind(host: str, request: Request): + admin(request) + device = store.device(host) + if not device: + raise HTTPException(404, "device not found") + if device["owner"]: + store.unbind(device["owner"]) + await r.incr("hn:web-generation:" + host) + await r.delete("hn:online:" + host) + await r.publish("hn:kill:" + host, "unbind") + for item in broker.pending.values(): + if item.host == host and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + store.audit("admin", "unbind", host, "success") + return {"status": "unbound", "host_id": host, "revoked": bool(device["revoked"])} + + async def make_binding_code(owner): + if store.by_owner(owner): + return "你已绑定电脑,请先发送 /解绑。" + await rate_limit(r, "binding:" + owner, 5) + code = secrets.token_urlsafe(12) + await r.set("hn:bind:" + hashlib.sha256(code.encode()).hexdigest(), owner, ex=300) + return "请在本人电脑的桥接客户端输入绑定码(5分钟有效):" + code + + @app.post("/device/bind") + async def bind(body: Bind, request: Request): + await rate_limit(r, "bind:" + peer(request), 20) + device = store.device(body.host_id) + if not device or device["revoked"]: + raise HTTPException(404, "device unavailable") + try: + public = verify_device(device["cert"], s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, public) + if proof["host_id"] != body.host_id or abs(time.time() - proof["ts"]) > 90: + raise ValueError("invalid proof") + code = proof["code"] + if not isinstance(code, str) or len(code) > 128: + raise ValueError("invalid code") + except Exception: + raise HTTPException(403, "invalid device proof") from None + key = "hn:bind:" + hashlib.sha256(code.encode()).hexdigest() + owner = await r.get(key) + if not owner: + raise HTTPException(404, "binding code expired or consumed") + # GETDEL is atomic across connectors. Invalid device proofs never reach this point. + owner = await r.getdel(key) + if not owner: + raise HTTPException(404, "binding code consumed") + try: + store.bind(body.host_id, owner) + except (*INTEGRITY_ERRORS, ValueError): + raise HTTPException(409, "owner/device already bound; request new code") from None + await r.incr("hn:web-generation:" + body.host_id) + store.audit(owner, "bind", body.host_id, "success") + return sign({"host_id": body.host_id, "owner": owner, "ts": int(time.time())}, broker.signer) + + @app.post("/admin/test/binding-code") + async def test_code(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return {"message": await make_binding_code(body.open_id)} + + @app.post("/admin/test/dispatch") + async def test_dispatch(body: Instruction, request: Request): + admin(request) + if s.environment == "production": + raise HTTPException(404, "not found") + return await broker.dispatch(body.open_id, body.text, body.turn_id) + + async def handle_event(data): + header, event = data.get("header", {}), data.get("event", {}) + if header.get("event_type") != "im.message.receive_v1": + return + message, sender = event.get("message", {}), event.get("sender", {}) + owner = sender.get("sender_id", {}).get("open_id") + message_id = message.get("message_id", "") + if (message.get("chat_type") != "p2p" or message.get("message_type") != "text" + or sender.get("sender_type") != "user" or not owner + or not re.fullmatch(r"[A-Za-z0-9_-]{1,128}", message_id)): + return + if header.get("app_id") != s.feishu_app_id: + return + try: + text = json.loads(message["content"])["text"].strip() + if not text or len(text) > 8000: + return + # Persistent receipt BEFORE any side effect. Restarts do not execute a delivery twice. + event_turn = "event_" + hashlib.sha256(message_id.encode()).hexdigest() + if not store.begin_turn(event_turn, owner, "event"): + return + if text == "/绑定": + result = await make_binding_code(owner) + elif text == "/解绑": + old = store.by_owner(owner) + store.unbind(owner) + if old: + await r.incr("hn:web-generation:" + old["host_id"]) + await r.publish("hn:kill:" + old["host_id"], "unbind") + for item in broker.pending.values(): + if item.host == old["host_id"] and not item.future.done(): + item.future.set_result({"status": "unbound", "result": "设备已解绑,结果已丢弃。"}) + result = "已解绑;重新绑定后请更新本机绑定信息。" + elif text == "/主机名" or text.startswith("/回显 "): + result = (await broker.dispatch(owner, text, "msg_" + hashlib.sha256(message_id.encode()).hexdigest()))["result"] + else: + result = "当前为企业身份接入联调:支持 /绑定、/解绑、/主机名、/回显 内容。Agent工具权限验收完成后再开放自然语言任务。" + await f.reply(message_id, result[:s.max_result]) + store.finish(event_turn, "replied") + except Exception: + store.audit(owner, "event", message_id, "failed_or_unknown") + + @app.post("/internal/events") + async def internal_event(request: Request): + require_secret(request.headers.get("X-Internal-Secret"), s.internal_secret) + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(await request.json())) + return {"accepted": True} + + @app.post("/feishu/event") + async def webhook(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + if not s.feishu_encrypt_key or not s.feishu_verification_token: + raise HTTPException(503, "webhook credentials not configured; use selected event source") + await rate_limit(r, "webhook:" + peer(request), 600) + raw = await request.body() + timestamp, nonce, signature = (request.headers.get(x, "") for x in + ("X-Lark-Request-Timestamp", "X-Lark-Request-Nonce", "X-Lark-Signature")) + try: + if abs(time.time() - int(timestamp)) > 300 or not nonce or len(nonce) > 128: + raise ValueError("stale request") + expected = hashlib.sha256((timestamp + nonce + s.feishu_encrypt_key).encode() + raw).hexdigest() + if not hmac.compare_digest(expected, signature): + raise ValueError("bad signature") + data = json.loads(raw) + if "encrypt" in data: + cipher = base64.b64decode(data["encrypt"], validate=True) + decryptor = Cipher(algorithms.AES(hashlib.sha256(s.feishu_encrypt_key.encode()).digest()), + modes.CBC(cipher[:16])).decryptor() + padded = decryptor.update(cipher[16:]) + decryptor.finalize() + unpadder = padding.PKCS7(128).unpadder() + data = json.loads(unpadder.update(padded) + unpadder.finalize()) + token = data.get("token", data.get("header", {}).get("token", "")) + if not hmac.compare_digest(token, s.feishu_verification_token): + raise ValueError("invalid verification token") + except Exception: + raise HTTPException(403, "invalid event authentication") from None + if data.get("type") == "url_verification": + return {"challenge": data.get("challenge", "")} + # Same delivery is ACKed again; durable message_id receipt handles execution deduplication. + if len(background) >= s.max_pending: + raise HTTPException(503, "busy") + spawn(handle_event(data)) + return {"code": 0} + + @app.get("/api/whoami") + async def whoami(request: Request): + if not s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(r, "whoami:" + peer(request)) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await f.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "no device bound") + if not s.device_domain or not re.fullmatch(r"[a-zA-Z0-9.-]+", s.device_domain): + raise HTTPException(503, "device web domain not configured") + return {"host_id": device["host_id"], "url": f"https://h-{device['host_id']}.{s.device_domain}/", + "online": bool(await r.exists("hn:online:" + device["host_id"])), + "device_login_required": True} + + @app.get("/h5", response_class=HTMLResponse) + async def h5(): + from importlib.resources import files + return HTMLResponse(files("relay").joinpath("web/index.html").read_text(encoding="utf-8"), + headers={"Content-Security-Policy": "default-src 'self'; script-src 'self'; style-src 'self'; frame-ancestors 'self'; base-uri 'none'; form-action 'self'"}) + + @app.get("/assets/{name}") + async def asset(name: str): + from importlib.resources import files + from starlette.responses import Response + if name not in {"app.js", "style.css"}: + raise HTTPException(404, "not found") + return Response(files("relay").joinpath("web/" + name).read_bytes(), + media_type="text/javascript" if name.endswith(".js") else "text/css") + + @app.get("/proxy") + async def proxy(request: Request): + if not s.proxy_enabled or not s.feishu_enabled: + raise HTTPException(404, "not found") + auth = request.headers.get("Authorization", "") + target = request.headers.get("X-Relay-Target", "") + decoded = unquote(target) + parts = urlsplit(target) + allowed = s.proxy_paths.split(",") + if (not auth.startswith("Bearer ") or len(auth) > 8192 or len(target) > 4096 + or parts.scheme or parts.netloc or parts.fragment or target.startswith("//") + or any(x in decoded for x in ("..", "\\", "\r", "\n", "#", "%")) + or not any(parts.path == x or parts.path.startswith(x + "/") for x in allowed)): + raise HTTPException(403, "target not allowed") + try: + info = await f.user_info(auth[7:]) + await rate_limit(r, "proxy:" + info["open_id"], 60) + # Buffer bounded response; no disk/cache, redirects never followed. + async with f.http.stream("GET", target, headers={"Authorization": auth}) as response: + if response.status_code in {301, 302, 303, 307, 308}: + raise HTTPException(502, "upstream redirect refused") + chunks, size = [], 0 + async for chunk in response.aiter_bytes(): + size += len(chunk) + if size > 2 * 1024 * 1024: + raise HTTPException(502, "response too large") + chunks.append(chunk) + from starlette.responses import Response + store.audit(info["open_id"], "proxy", parts.path, str(response.status_code)) + return Response(b"".join(chunks), status_code=response.status_code, + media_type=response.headers.get("content-type", "application/json")) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + + return app diff --git a/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/crypto.py b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/crypto.py new file mode 100644 index 0000000..e4d7afd --- /dev/null +++ b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/crypto.py @@ -0,0 +1,134 @@ +"""Signed ephemeral X25519 envelopes. The router never receives plaintext keys.""" +import base64 +import hashlib +import json +import secrets +import time +from datetime import datetime, timedelta, timezone + +from cryptography import x509 +from cryptography.hazmat.primitives import hashes, serialization +from cryptography.hazmat.primitives.asymmetric import ed25519, x25519 +from cryptography.hazmat.primitives.ciphers.aead import AESGCM +from cryptography.hazmat.primitives.kdf.hkdf import HKDF +from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID + + +def b64(data: bytes) -> str: + return base64.urlsafe_b64encode(data).decode() + + +def unb64(data: str) -> bytes: + return base64.b64decode(data.encode(), altchars=b"-_", validate=True) + + +def canonical(data: dict) -> bytes: + return json.dumps(data, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode() + + +def raw_public(key) -> str: + return b64(key.public_bytes(serialization.Encoding.Raw, serialization.PublicFormat.Raw)) + + +def pem_private(key) -> bytes: + return key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, + serialization.NoEncryption()) + + +def load_private(path): + return serialization.load_pem_private_key(path.read_bytes(), password=None) + + +def load_public(path): + return serialization.load_pem_public_key(path.read_bytes()) + + +def fingerprint(pem: str) -> str: + return x509.load_pem_x509_certificate(pem.encode()).fingerprint(hashes.SHA256()).hex() + + +def verify_device(pem: str, ca_pem: bytes, host_id: str): + """v1 deliberately accepts only directly issued Ed25519 device certificates.""" + cert = x509.load_pem_x509_certificate(pem.encode()) + ca = x509.load_pem_x509_certificate(ca_pem) + now = datetime.now(timezone.utc) + if not (ca.not_valid_before_utc <= now <= ca.not_valid_after_utc): + raise ValueError("expired CA") + if not ca.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("not a CA") + cert.verify_directly_issued_by(ca) + if not (cert.not_valid_before_utc <= now <= cert.not_valid_after_utc): + raise ValueError("expired certificate") + if cert.extensions.get_extension_for_class(x509.BasicConstraints).value.ca: + raise ValueError("CA cannot be used as device") + if ExtendedKeyUsageOID.CLIENT_AUTH not in cert.extensions.get_extension_for_class(x509.ExtendedKeyUsage).value: + raise ValueError("clientAuth missing") + names = cert.extensions.get_extension_for_class(x509.SubjectAlternativeName).value + if names.get_values_for_type(x509.UniformResourceIdentifier) != [f"urn:hainuo:host:{host_id}"]: + raise ValueError("host identity mismatch") + public = cert.public_key() + if not isinstance(public, ed25519.Ed25519PublicKey): + raise ValueError("v1 requires Ed25519 device key") + return public + + +def sign(data: dict, key) -> dict: + return {**data, "signature": b64(key.sign(canonical(data)))} + + +def verified(data: dict, key) -> dict: + body = {k: v for k, v in data.items() if k != "signature"} + key.verify(unb64(data["signature"]), canonical(body)) + return body + + +def seal(payload: dict, recipient_public: str, signer, *, host_id: str, session_id: str) -> dict: + ephemeral = x25519.X25519PrivateKey.generate() + header = {"v": 1, "host_id": host_id, "session_id": session_id, + "ts": int(time.time()), "nonce": b64(secrets.token_bytes(12)), + "ephemeral": raw_public(ephemeral.public_key())} + shared = ephemeral.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(recipient_public))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + cipher = AESGCM(key).encrypt(unb64(header["nonce"]), canonical(payload), canonical(header)) + return sign({**header, "ciphertext": b64(cipher)}, signer) + + +def open_envelope(envelope: dict, recipient_private, sender_public, *, host_id: str, session_id: str) -> dict: + body = verified(envelope, sender_public) + if body["v"] != 1 or body["host_id"] != host_id or body["session_id"] != session_id: + raise ValueError("wrong destination/session") + if abs(time.time() - body["ts"]) > 90: + raise ValueError("expired envelope") + header = {k: v for k, v in body.items() if k != "ciphertext"} + shared = recipient_private.exchange(x25519.X25519PublicKey.from_public_bytes(unb64(body["ephemeral"]))) + key = HKDF(algorithm=hashes.SHA256(), length=32, salt=session_id.encode(), + info=b"hainuo-relay-envelope-v1").derive(shared) + plain = AESGCM(key).decrypt(unb64(body["nonce"]), unb64(body["ciphertext"]), canonical(header)) + return json.loads(plain) + + +def issue_ca(): + key = ed25519.Ed25519PrivateKey.generate() + name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "Hainuo device test CA")]) + now = datetime.now(timezone.utc) + cert = (x509.CertificateBuilder().subject_name(name).issuer_name(name) + .public_key(key.public_key()).serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=3650)) + .add_extension(x509.BasicConstraints(ca=True, path_length=0), critical=True) + .add_extension(x509.KeyUsage(False, False, False, False, False, True, True, False, False), critical=True) + .sign(key, algorithm=None)) + return key, cert + + +def issue_device(ca_key, ca_cert, host_id: str, public_key, days=30): + now = datetime.now(timezone.utc) + return (x509.CertificateBuilder() + .subject_name(x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, host_id)])) + .issuer_name(ca_cert.subject).public_key(public_key) + .serial_number(x509.random_serial_number()) + .not_valid_before(now - timedelta(minutes=5)).not_valid_after(now + timedelta(days=days)) + .add_extension(x509.BasicConstraints(ca=False, path_length=None), critical=True) + .add_extension(x509.SubjectAlternativeName([x509.UniformResourceIdentifier(f"urn:hainuo:host:{host_id}")]), critical=False) + .add_extension(x509.ExtendedKeyUsage([ExtendedKeyUsageOID.CLIENT_AUTH]), critical=False) + .sign(ca_key, algorithm=None)) diff --git a/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/device.py b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/device.py new file mode 100644 index 0000000..31f9875 --- /dev/null +++ b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/device.py @@ -0,0 +1,206 @@ +"""Reference PC bridge. No shell/PowerShell execution; local read-only diagnostics.""" +import asyncio +import contextlib +import json +import os +import secrets +import socket +import sqlite3 +import time +from pathlib import Path +from urllib.parse import urlsplit + +import httpx +from cryptography.hazmat.primitives.asymmetric import x25519 +from websockets.asyncio.client import connect + +from .crypto import (load_private, load_public, open_envelope, raw_public, seal, sign, + verified, verify_device) + + +class DeviceJournal: + def __init__(self, path): + path.parent.mkdir(parents=True, exist_ok=True) + self.db = sqlite3.connect(path) + self.db.execute("CREATE TABLE IF NOT EXISTS consumed(turn_id TEXT PRIMARY KEY, ts REAL NOT NULL)") + self.db.commit() + + def consume(self, turn): + with self.db: + return self.db.execute("INSERT OR IGNORE INTO consumed VALUES(?,?)", (turn, time.time())).rowcount == 1 + + def close(self): + self.db.close() + + +async def execute_readonly(text, root: Path | None): + if text in {"/主机名", "hostname"}: + return socket.gethostname() + if text.startswith("/回显 "): + return text[4:][:16000] + if text.startswith("/读取 "): + if root is None: + return "未配置测试文件目录。" + base = root.resolve(strict=True) + try: + path = (base / text[4:].strip()).resolve(strict=True) + if path == base or not path.is_relative_to(base) or not path.is_file(): + return "拒绝:文件不在允许的测试目录。" + if path.stat().st_size > 32768: + return "拒绝:测试文件超过32KB。" + return path.read_text(encoding="utf-8")[:16000] + except (OSError, UnicodeError, ValueError): + return "拒绝:文件不存在、不可读或不是UTF-8文本。" + return "当前桥接客户端仅开放只读诊断:/主机名、/回显 内容、/读取 相对路径;尚未接入海诺本地Agent接口。" + + +class LocalAgent: + """Explicit adapter contract, not a claim of compatibility with installer 1.065.""" + def __init__(self, url, transport=None): + parts = urlsplit(url) + if parts.scheme != "http" or parts.hostname not in {"127.0.0.1", "::1"} or parts.username or parts.password or parts.fragment: + raise ValueError("local Agent must use a fixed loopback HTTP endpoint") + self.url = url + self.http = httpx.AsyncClient(timeout=30, follow_redirects=False, transport=transport, trust_env=False) + + async def execute(self, text, owner, turn): + headers = {} + if os.environ.get("HN_LOCAL_AGENT_TOKEN"): + headers["Authorization"] = "Bearer " + os.environ["HN_LOCAL_AGENT_TOKEN"] + try: + async with self.http.stream("POST", self.url, headers=headers, + json={"text": text, "open_id": owner, "turn_id": turn, + "channel": "im", "read_only": True}) as response: + if response.status_code != 200: + return "本机Agent未返回成功结果,请查看本机服务状态。" + body = b"" + async for chunk in response.aiter_bytes(): + body += chunk + if len(body) > 65536: + return "本机Agent结果超过限制。" + result = json.loads(body)["result"] + if not isinstance(result, str): + return "本机Agent响应格式不匹配。" + return result[:16000] + except (httpx.HTTPError, ValueError, KeyError): + return "本机Agent连接失败或响应格式不匹配。" + + async def close(self): + await self.http.aclose() + + +async def bind_device(directory: Path, connector_url: str, code: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + private = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "code": code, "ts": int(time.time())}, private) + async with httpx.AsyncClient(verify=tls_verify, follow_redirects=False, timeout=15) as http: + response = await http.post(connector_url.rstrip("/") + "/device/bind", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"binding failed: HTTP {response.status_code}") + receipt = verified(response.json(), load_public(directory / "connector.pub")) + if receipt["host_id"] != config["host_id"]: + raise ValueError("wrong binding receipt") + config["owner"] = receipt["owner"] + temp = directory / "device.json.tmp" + temp.write_text(json.dumps(config, ensure_ascii=False, indent=2), encoding="utf-8") + temp.replace(directory / "device.json") + + +async def pair_browser(directory: Path, connector_url: str, *, tls_verify=True): + config = json.loads((directory / "device.json").read_text(encoding="utf-8-sig")) + if not config.get("owner"): + raise RuntimeError("device must be bound first") + key = load_private(directory / "device.key") + proof = sign({"host_id": config["host_id"], "owner": config["owner"], + "purpose": "h5-pair-v1", "ts": int(time.time()), "nonce": secrets.token_hex(16)}, key) + async with httpx.AsyncClient(verify=tls_verify, trust_env=False, follow_redirects=False, timeout=15) as client: + response = await client.post(connector_url.rstrip("/") + "/device/web-pair", + json={"host_id": config["host_id"], "proof": proof}) + if response.status_code != 200: + raise RuntimeError(f"browser pairing failed: HTTP {response.status_code}") + result = verified(response.json(), load_public(directory / "connector.pub")) + if result["host_id"] != config["host_id"]: + raise ValueError("wrong pairing receipt") + return result + + +async def run_device(directory: Path, router_url: str, *, root=None, once=False, ssl_context=None, + stop_event=None, connected_event=None, agent_url=None, hainuo_config=None): + config = json.loads((directory / "device.json").read_text(encoding="utf-8")) + if not config.get("owner"): + raise RuntimeError("bind device before starting") + host, owner = config["host_id"], config["owner"] + key = load_private(directory / "device.key") + connector = load_public(directory / "connector.pub") + cert = (directory / "device.pem").read_text(encoding="utf-8") + verify_device(cert, (directory / "ca.pem").read_bytes(), host) + journal = DeviceJournal(directory / "journal.sqlite") + if agent_url and hainuo_config: + raise ValueError("choose one Agent adapter") + if hainuo_config: + from .hainuo1065 import Hainuo1065Agent + agent = Hainuo1065Agent(hainuo_config) + else: + agent = LocalAgent(agent_url) if agent_url else None + stop_event = stop_event or asyncio.Event() + delay = 1 + try: + while not stop_event.is_set(): + try: + kwargs = {"ssl": ssl_context} if router_url.startswith("wss://") and ssl_context else {} + async with connect(router_url, max_size=262144, open_timeout=10, ping_interval=10, + proxy=None, **kwargs) as ws: + challenge = json.loads(await ws.recv())["challenge"] + ephemeral = x25519.X25519PrivateKey.generate() + session = secrets.token_hex(16) + proof = sign({"host_id": host, "session_id": session, "challenge": challenge, + "kx": raw_public(ephemeral.public_key()), "ts": int(time.time())}, key) + await ws.send(json.dumps({"host_id": host, "cert": cert, "proof": proof})) + accepted = json.loads(await ws.recv()) + if accepted.get("type") != "connected": + raise RuntimeError("device connection rejected") + if connected_event: + connected_event.set() + delay = 1 + while not stop_event.is_set(): + try: + raw = await asyncio.wait_for(ws.recv(), timeout=1) + except asyncio.TimeoutError: + continue + data = json.loads(raw) + payload = open_envelope(data["envelope"], ephemeral, connector, + host_id=host, session_id=session) + if payload.get("owner") != owner or payload.get("type") != "instruction": + continue + turn = payload["turn_id"] + if not isinstance(turn, str) or len(turn) > 128: + continue + if not journal.consume(turn): + result = "该请求已处理,拒绝重复执行。" + elif payload.get("read_only") is not True or payload.get("channel") != "im": + result = "拒绝:通道或操作类型不受支持。" + else: + if agent and payload["text"].startswith("/助手 "): + result = await agent.execute(payload["text"][4:], owner, turn) + else: + result = await execute_readonly(payload["text"], root) + reply = seal({"type": "result", "turn_id": turn, "result": result}, + payload["reply_key"], key, host_id=host, session_id=session) + await ws.send(json.dumps({"type": "reply", "turn_id": turn, "envelope": reply})) + if once: + return + except asyncio.CancelledError: + raise + except Exception: + if once: + raise + # No payloads/URLs/credentials in logs, bounded reconnect backoff + jitter. + print("设备连接中断,等待重连。", flush=True) + with contextlib.suppress(asyncio.TimeoutError): + await asyncio.wait_for(stop_event.wait(), timeout=delay + secrets.randbelow(1000) / 1000) + delay = min(delay * 2, 30) + finally: + journal.close() + if agent: + await agent.close() diff --git a/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/hainuo1065.py b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/hainuo1065.py new file mode 100644 index 0000000..6f7735b --- /dev/null +++ b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/hainuo1065.py @@ -0,0 +1,107 @@ +"""Adapter for the native API actually found in installer 1.065. + +Loopback only, independent API key, no platform secrets, no redirects or retries. +The included native launcher offers only text inference with no tools. +""" +import hashlib +import json +import re +from pathlib import Path +from urllib.parse import urlsplit + +import httpx + + +class NativeAPIError(RuntimeError): + """Contains a sanitized status only, never response body or credentials.""" + + +def read_native_config(path): + path = Path(path) + data = json.loads(path.read_text(encoding="utf-8-sig")) + url = data.get("url", "") + parts = urlsplit(url) + if (parts.scheme != "http" or parts.hostname != "127.0.0.1" or not parts.port + or parts.username or parts.password or parts.query or parts.fragment + or parts.path not in {"", "/"}): + raise ValueError("native API must be a fixed IPv4 loopback HTTP base URL") + if not isinstance(data.get("api_key"), str) or len(data["api_key"]) < 32: + raise ValueError("native API requires a dedicated key of at least 32 characters") + if data.get("mode") not in {"text-only", "profile-tools"}: + raise ValueError("unsupported native API mode") + data["url"] = url.rstrip("/") + return data + + +class Hainuo1065Agent: + def __init__(self, config_path, transport=None): + self.config = read_native_config(config_path) + self.http = httpx.AsyncClient(base_url=self.config["url"], timeout=120, + follow_redirects=False, trust_env=False, transport=transport) + self.headers = {"Authorization": "Bearer " + self.config["api_key"]} + + async def _json(self, method, path, **kwargs): + try: + async with self.http.stream(method, path, headers={**self.headers, **kwargs.pop("headers", {})}, **kwargs) as response: + if response.status_code != 200: + raise NativeAPIError(f"native API HTTP {response.status_code}") + body = bytearray() + async for chunk in response.aiter_bytes(): + body.extend(chunk) + if len(body) > 262144: + raise NativeAPIError("native API response exceeds limit") + data = json.loads(body) + if not isinstance(data, dict): + raise NativeAPIError("native API response is not an object") + return data + except (httpx.HTTPError, ValueError): + raise NativeAPIError("native API connection/JSON failure") from None + + async def probe(self): + health = await self._json("GET", "/health") + if health.get("platform") != "hermes-agent" or health.get("status") != "ok": + raise NativeAPIError("endpoint is not the native Hermes API") + capabilities = await self._json("GET", "/v1/capabilities") + policy = capabilities.get("hainuo_relay_policy", {}) + # A stock unrestricted API endpoint is deliberately not sufficient. + expected_mode = self.config["mode"] + expected_tools = "profile" if expected_mode == "profile-tools" else "disabled" + if policy != {"mode": expected_mode, "tools": expected_tools, "contract": "hainuo-1065-v1"}: + raise NativeAPIError("native API policy missing; start the supplied restricted launcher") + models = await self._json("GET", "/v1/models") + model = self.config.get("model") + available = models.get("data") + if not isinstance(available, list) or not available or not all(isinstance(x, dict) and isinstance(x.get("id"), str) for x in available): + raise NativeAPIError("native model list invalid") + if model and model not in {x["id"] for x in available}: + raise NativeAPIError("configured model alias unavailable") + self.model = model or available[0]["id"] + return {"status": "ready", "platform": "hermes-agent", "model": self.model, + "mode": expected_mode, "version": health.get("version", "unknown")} + + async def execute(self, text, owner, turn): + try: + # Recheck the actual local service before each inference, including reconnects. + await self.probe() + digest = hashlib.sha256((owner + "\0" + turn).encode()).hexdigest() + data = await self._json("POST", "/v1/chat/completions", + headers={"Idempotency-Key": "hn-" + digest}, + json={"model": self.model, "stream": False, + "messages": [{"role": "user", "content": text}]}) + choices = data.get("choices") + if not isinstance(choices, list) or not choices or not isinstance(choices[0], dict): + raise NativeAPIError("native completion choices missing") + choice = choices[0] + message = choice.get("message", {}) + if (choice.get("finish_reason") != "stop" or message.get("tool_calls") + or not isinstance(message.get("content"), str) + or data.get("hermes", {}).get("failed") + or data.get("hermes", {}).get("partial")): + raise NativeAPIError("native completion incomplete or unexpected tool call") + return message["content"][:16000] + except NativeAPIError as error: + # Safe generated error strings only; never echo upstream model/API errors. + return "海诺原生接口未完成请求:" + str(error) + "。未自动重试。" + + async def close(self): + await self.http.aclose() diff --git a/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/longconn.py b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/longconn.py new file mode 100644 index 0000000..5c12bbf --- /dev/null +++ b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/longconn.py @@ -0,0 +1,74 @@ +"""Optional official SDK event source, private platform domain explicitly configured.""" +import json +import logging +import os +import threading +import time + +import httpx +import redis +from .config import Settings + + +def main(): + import lark_oapi as lark + s = Settings(role="longconn") + if not s.feishu_enabled or s.feishu_event_source != "longconn": + raise RuntimeError("platform is disabled") + # The SDK may log websocket URLs, tokens or event bodies. Disable its logging entirely. + logging.disable(logging.CRITICAL) + r = redis.Redis.from_url(s.redis_url) + lease = r.lock("hn:longconn:leader", timeout=30, thread_local=False) + if not lease.acquire(blocking=False): + raise RuntimeError("another long-connection consumer owns the lease") + url = os.environ.get("HN_CONNECTOR_INTERNAL_URL", "http://connector:8000") + client = httpx.Client(base_url=url, timeout=10, follow_redirects=False) + done = threading.Event() + + def maintain_lease(): + while not done.wait(8): + try: + lease.extend(30, replace_ttl=True) + except Exception: + # Fail closed. Continuing after lease loss could create two consumers. + os._exit(2) + + threading.Thread(target=maintain_lease, daemon=True).start() + + def received(event): + data = json.loads(lark.JSON.marshal(event)) + response = client.post("/internal/events", headers={"X-Internal-Secret": s.internal_secret}, json=data) + if response.status_code != 200: + raise RuntimeError("connector did not accept event") + print(json.dumps({"event": "platform_event_accepted", "type": data.get("header", {}).get("event_type")}), flush=True) + + handler = lark.EventDispatcherHandler.builder("", "").register_p2_im_message_receive_v1(received).build() + class ObservableClient(lark.ws.Client): + async def _connect(self): + try: + await super()._connect() + if self._conn is not None: + print(json.dumps({"event": "platform_connected", "api_host": s.feishu_api_base}), flush=True) + except Exception as error: + print(json.dumps({"event": "platform_connect_failed", "error_type": type(error).__name__}), flush=True) + raise + + try: + # domain is an actual supported SDK argument; no public-cloud-domain monkey patch. + ObservableClient(s.feishu_app_id, s.feishu_app_secret, event_handler=handler, + domain=s.feishu_api_base.rstrip("/"), log_level=lark.LogLevel.ERROR).start() + finally: + done.set() + client.close() + try: + lease.release() + except redis.exceptions.LockError: + pass + + +if __name__ == "__main__": + try: + main() + except Exception as error: + print(json.dumps({"event": "platform_consumer_stopped", "error_type": type(error).__name__}), flush=True) + raise SystemExit(1) from None diff --git a/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/oauth.py b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/oauth.py new file mode 100644 index 0000000..b04ce6e --- /dev/null +++ b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/oauth.py @@ -0,0 +1,127 @@ +import asyncio +import hashlib +import hmac +import secrets +import time +from dataclasses import dataclass +from urllib.parse import urlencode + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import HTMLResponse, JSONResponse +from pydantic import BaseModel, Field +from .common import no_cache, peer, rate_limit +from .platform import PlatformError + + +@dataclass +class Transaction: + secret_hash: str + expires: float + status: str = "waiting" + result: dict | None = None + + +class Empty(BaseModel): + pass + + +class Refresh(BaseModel): + refresh_token: str = Field(min_length=1, max_length=4096) + + +class OAuth: + """A single connector process owns transactions and tokens in memory only.""" + def __init__(self, settings, redis, platform): + self.settings, self.redis, self.platform = settings, redis, platform + self.pending: dict[str, Transaction] = {} + self.lock = asyncio.Lock() + self.router = APIRouter() + self.router.add_api_route("/oauth/start", self.start, methods=["POST"], status_code=201) + self.router.add_api_route("/oauth/cb", self.callback, methods=["GET"]) + self.router.add_api_route("/oauth/claim", self.claim, methods=["GET"]) + self.router.add_api_route("/oauth/pending", self.status, methods=["GET"]) + self.router.add_api_route("/oauth/refresh", self.refresh, methods=["POST"]) + + def prune(self): + now = time.monotonic() + for state in list(self.pending): + if self.pending[state].expires <= now: + del self.pending[state] + + def authenticated(self, state, secret): + self.prune() + item = self.pending.get(state) + digest = hashlib.sha256((secret or "").encode()).hexdigest() + if not item or not hmac.compare_digest(item.secret_hash, digest): + raise HTTPException(404, "transaction not found") + return item + + async def start(self, body: Empty, request: Request): + if not self.settings.feishu_enabled or not self.settings.oauth_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.redis, "oauth:start:" + peer(request), 20) + async with self.lock: + self.prune() + if len(self.pending) >= self.settings.max_pending: + raise HTTPException(503, "too many pending transactions") + state, secret = secrets.token_urlsafe(32), secrets.token_urlsafe(32) + self.pending[state] = Transaction(hashlib.sha256(secret.encode()).hexdigest(), time.monotonic() + 600) + redirect = self.settings.public_url.rstrip("/") + "/oauth/cb" + url = self.settings.feishu_authorize_url + "?" + urlencode({"app_id": self.settings.feishu_app_id, + "redirect_uri": redirect, "state": state}) + return no_cache(JSONResponse({"state": state, "claim_secret": secret, "authorization_url": url, + "redirect_uri": redirect, "expires_in": 600, + "code_ttl": 60, "poll_interval": 2}, status_code=201)) + + async def callback(self, request: Request, state: str = "", code: str = ""): + await rate_limit(self.redis, "oauth:cb:" + peer(request)) + async with self.lock: + self.prune() + item = self.pending.get(state) + if not code or len(code) > 4096 or not item or item.status != "waiting": + raise HTTPException(400, "invalid callback") + item.status = "exchanging" + try: + result = await self.platform.exchange(code) + except PlatformError: + async with self.lock: + item.status = "failed" + raise HTTPException(502, "authorization exchange failed") from None + async with self.lock: + if state not in self.pending or item.expires <= time.monotonic(): + self.pending.pop(state, None) + raise HTTPException(400, "authorization expired") + item.result, item.status = result, "ready" + return no_cache(HTMLResponse("

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

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

连接你的
工作电脑。

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

+
01 / 连接本人设备

输入电脑上的配对码

+

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

+
+
使用海能Work授权

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

+ +

正在检查网页会话…

+ +
diff --git a/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/web/style.css b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/web/style.css new file mode 100644 index 0000000..a33d19c --- /dev/null +++ b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/web/style.css @@ -0,0 +1,3 @@ +:root{color-scheme:light;font-family:system-ui,-apple-system,"Segoe UI","Microsoft YaHei",sans-serif;color:#17372c;background:#f2f4ed}*{box-sizing:border-box}body{margin:0}main{max-width:820px;margin:0 auto;padding:60px 26px}.mark{font-size:13px;letter-spacing:.16em;color:#557764;font-weight:700}h1{font-size:clamp(42px,8vw,70px);letter-spacing:-.05em;line-height:1.12;font-weight:650;margin:40px 0 22px}.intro{font-size:17px;line-height:1.8;color:#56675a;max-width:520px}.card{background:#fff;border:1px solid #dbe3d8;border-radius:20px;padding:30px;margin:40px 0 24px;box-shadow:0 12px 44px #18372c06}.step{font-size:12px;letter-spacing:.1em;color:#6d806c}h2{font-size:24px;margin:16px 0}.card p{line-height:1.8;color:#5b6c60}button{background:#214e3a;color:white;border:0;border-radius:10px;font:inherit;font-weight:600;padding:16px 20px;width:100%;text-align:left;cursor:pointer;margin-top:16px}button span{float:right}button:disabled{opacity:.5;cursor:wait}button:focus-visible,a:focus-visible{outline:3px solid #d0a349;outline-offset:4px}#status{font-size:13px;margin-bottom:0}#open{display:block;margin-top:22px;color:#214e3a;font-weight:600}#open[hidden]{display:none}footer{color:#768373;font-size:12px;line-height:1.8}@media(max-width:480px){main{padding-top:36px}.card{padding:22px}h1{margin-top:28px}} + +[hidden]{display:none!important}.badge{float:right;font-size:11px;letter-spacing:0;background:#e4ebdf;border-radius:20px;padding:6px 10px}label{display:block;font-size:13px;color:#526c5b;margin:12px 0 8px}input,textarea{font:inherit;width:100%;border:1px solid #ccd8ce;border-radius:10px;padding:14px;color:#17372c;background:#fafcf8;resize:vertical}input:focus,textarea:focus{outline:2px solid #6c987b;outline-offset:2px}details{margin-top:24px;font-size:13px;color:#587561}summary{cursor:pointer}.chat-head{display:flex;align-items:center;justify-content:space-between;gap:20px}.secondary{width:auto;background:#edf2e9;color:#31523f;padding:9px 12px;font-size:12px;margin:0}.hint{font-size:13px}#messages{display:flex;flex-direction:column;gap:12px;margin:22px 0;max-height:420px;overflow:auto}.message{padding:14px 16px;border-radius:12px;line-height:1.8;white-space:pre-wrap;overflow-wrap:anywhere;font-size:15px}.message.user{background:#e5eee2;margin-left:36px}.message.assistant{background:#f2f5ef;margin-right:24px}.message.notice{background:#fbf4e5;color:#735c2e}#status{font-size:13px;color:#5f745f;line-height:1.8}#chat-panel h2{font-size:20px}footer{max-width:640px} diff --git a/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/web_session.py b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/web_session.py new file mode 100644 index 0000000..1ac9adf --- /dev/null +++ b/patch-source/a8137fe7e675b0a9/hainuo-relay/relay/web_session.py @@ -0,0 +1,183 @@ +"""Pilot H5 pairing and conversation. Credentials never appear in URLs.""" +import hashlib +import json +import secrets +import time +from urllib.parse import urlsplit + +from fastapi import APIRouter, HTTPException, Request +from fastapi.responses import JSONResponse +from pydantic import BaseModel, Field + +from .common import no_cache, peer, rate_limit +from .crypto import sign, verified, verify_device +from .platform import PlatformError + +COOKIE = "hn_web_session" + + +def digest(value): + return hashlib.sha256(value.encode()).hexdigest() + + +def origin_tuple(value): + try: + parts = urlsplit(value) + if parts.scheme not in {"https", "http"} or not parts.hostname or parts.username or parts.password: + return None + return parts.scheme, parts.hostname.lower(), parts.port or (443 if parts.scheme == "https" else 80) + except ValueError: + return None + + +class PairProof(BaseModel): + host_id: str = Field(pattern=r"^[a-f0-9]{32}$") + proof: dict + + +class Claim(BaseModel): + code: str = Field(min_length=12, max_length=128) + + +class Chat(BaseModel): + text: str = Field(min_length=1, max_length=8000) + request_id: str = Field(pattern=r"^[a-f0-9]{32}$") + + +class WebSessions: + def __init__(self, settings, redis, store, broker, platform): + self.s, self.r, self.store, self.broker, self.platform = settings, redis, store, broker, platform + self.router = APIRouter() + for path, method, handler in [ + ("/device/web-pair", "POST", self.device_pair), + ("/web/claim", "POST", self.claim), + ("/web/session", "GET", self.session), + ("/web/platform-session", "POST", self.platform_login), + ("/web/chat", "POST", self.chat), + ("/web/logout", "POST", self.logout), + ]: + self.router.add_api_route(path, handler, methods=[method]) + + def check_origin(self, request): + actual = origin_tuple(request.headers.get("Origin", "")) + if not actual or actual != origin_tuple(self.s.public_url): + raise HTTPException(403, "same-origin browser request required") + + async def valid_device(self, record): + device = self.store.device(record["host_id"]) + if (not device or device["revoked"] or device["owner"] != record["owner"] + or device["fingerprint"] != record["fingerprint"]): + raise HTTPException(401, "device binding changed; pair again") + generation = await self.r.get("hn:web-generation:" + record["host_id"]) or "0" + if record.get("generation") != generation: + raise HTTPException(401, "device binding changed; pair again") + return device + + async def device_pair(self, body: PairProof, request: Request): + await rate_limit(self.r, "web-pair-ip:" + peer(request), 20) + device = self.store.device(body.host_id) + if not device or device["revoked"] or not device["owner"]: + raise HTTPException(403, "active bound device required") + try: + key = verify_device(device["cert"], self.s.ca_cert.read_bytes(), body.host_id) + proof = verified(body.proof, key) + if (proof["host_id"] != body.host_id or proof["owner"] != device["owner"] + or proof["purpose"] != "h5-pair-v1" or abs(time.time() - proof["ts"]) > 90 + or not isinstance(proof["nonce"], str) or len(proof["nonce"]) != 32): + raise ValueError("invalid proof") + except Exception: + raise HTTPException(403, "invalid device proof") from None + replay = "hn:web-pair-proof:" + digest(body.host_id + proof["nonce"]) + if not await self.r.set(replay, "used", nx=True, ex=180): + raise HTTPException(409, "pairing request already used") + await rate_limit(self.r, "web-pair-device:" + body.host_id, 5) + code = secrets.token_urlsafe(18) + record = {"host_id": body.host_id, "owner": device["owner"], "fingerprint": device["fingerprint"], + "generation": await self.r.get("hn:web-generation:" + body.host_id) or "0"} + await self.r.set("hn:web-code:" + digest(code), json.dumps(record), ex=120) + self.store.audit(device["owner"], "web_pair_issued", body.host_id, "success") + return no_cache(JSONResponse(sign({"host_id": body.host_id, "code": code, "expires_in": 120, + "url": self.s.public_url.rstrip("/") + "/h5"}, self.broker.signer))) + + async def new_session(self, record): + token = secrets.token_urlsafe(32) + record = {k: record[k] for k in ("host_id", "owner", "fingerprint", "generation")} + await self.r.set("hn:web-session:" + digest(token), json.dumps(record), ex=1800) + response = no_cache(JSONResponse({"status": "paired", "host_id": record["host_id"], "expires_in": 1800})) + response.set_cookie(COOKIE, token, max_age=1800, secure=True, httponly=True, samesite="strict", path="/") + return response + + async def claim(self, body: Claim, request: Request): + self.check_origin(request) + await rate_limit(self.r, "web-claim:" + peer(request), 20) + key = "hn:web-code:" + digest(body.code.strip()) + encoded = await self.r.get(key) + if not encoded: + raise HTTPException(404, "code expired or consumed") + await self.valid_device(json.loads(encoded)) + encoded = await self.r.getdel(key) + if not encoded: + raise HTTPException(404, "code already consumed") + record = json.loads(encoded) + await self.valid_device(record) + self.store.audit(record["owner"], "web_pair_claimed", record["host_id"], "success") + return await self.new_session(record) + + async def authenticated(self, request): + token = request.cookies.get(COOKIE, "") + if not token or len(token) > 128: + raise HTTPException(401, "pair this browser first") + encoded = await self.r.get("hn:web-session:" + digest(token)) + if not encoded: + raise HTTPException(401, "browser session expired") + record = json.loads(encoded) + await self.valid_device(record) + return record + + async def session(self, request: Request): + record = await self.authenticated(request) + return no_cache(JSONResponse({"authenticated": True, "host_id": record["host_id"], + "online": bool(await self.r.exists("hn:online:" + record["host_id"])), + "mode": "pilot-text-only", "relay_sees_content": True})) + + async def platform_login(self, request: Request): + self.check_origin(request) + if not self.s.feishu_enabled: + raise HTTPException(503, "platform disabled") + await rate_limit(self.r, "web-platform:" + peer(request), 20) + auth = request.headers.get("Authorization", "") + if not auth.startswith("Bearer ") or len(auth) > 8192: + raise HTTPException(401, "user token required") + try: + info = await self.platform.user_info(auth[7:]) + except PlatformError: + raise HTTPException(401, "invalid user token") from None + device = self.store.by_owner(info["open_id"]) + if not device: + raise HTTPException(404, "bind your device first") + return await self.new_session({**device, "generation": await self.r.get("hn:web-generation:" + device["host_id"]) or "0"}) + + async def chat(self, body: Chat, request: Request): + self.check_origin(request) + record = await self.authenticated(request) + await rate_limit(self.r, "web-chat:" + record["owner"], 20) + text = body.text.strip() + if not text: + raise HTTPException(422, "empty message") + # PC accepts only its bound owner and the existing IM read-only channel. + if not (text == "/主机名" or text.startswith(("/回显 ", "/读取 ", "/助手 "))): + text = "/助手 " + text + turn = "web_" + digest(record["owner"] + "\0" + body.request_id) + result = await self.broker.dispatch(record["owner"], text, turn) + # Revoke/unbind during a long model call invalidates the browser immediately. + await self.authenticated(request) + return no_cache(JSONResponse(result)) + + async def logout(self, request: Request): + self.check_origin(request) + token = request.cookies.get(COOKIE, "") + if token and len(token) <= 128: + await self.r.delete("hn:web-session:" + digest(token)) + response = no_cache(JSONResponse({"status": "logged_out"})) + response.delete_cookie(COOKIE, secure=True, httponly=True, samesite="strict", path="/") + return response diff --git a/patch-source/a8137fe7e675b0a9/hainuo-relay/scripts/configure_feishu_longconn.py b/patch-source/a8137fe7e675b0a9/hainuo-relay/scripts/configure_feishu_longconn.py new file mode 100644 index 0000000..026b899 --- /dev/null +++ b/patch-source/a8137fe7e675b0a9/hainuo-relay/scripts/configure_feishu_longconn.py @@ -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 diff --git a/patch-source/a8137fe7e675b0a9/hainuo-relay/scripts/start-feishu-longconn.sh b/patch-source/a8137fe7e675b0a9/hainuo-relay/scripts/start-feishu-longconn.sh new file mode 100644 index 0000000..5dbc2f4 --- /dev/null +++ b/patch-source/a8137fe7e675b0a9/hainuo-relay/scripts/start-feishu-longconn.sh @@ -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 diff --git a/patch-source/a8137fe7e675b0a9/hainuo-relay/tests/test_feishu_longconn_setup.py b/patch-source/a8137fe7e675b0a9/hainuo-relay/tests/test_feishu_longconn_setup.py new file mode 100644 index 0000000..248066f --- /dev/null +++ b/patch-source/a8137fe7e675b0a9/hainuo-relay/tests/test_feishu_longconn_setup.py @@ -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