Import ECS deployed relay source and historical patches (2026-10-27)

This commit is contained in:
ex_tianzy
2026-10-27 17:26:58 +08:00
commit 011a7797ee
319 changed files with 34265 additions and 0 deletions
+21
View File
@@ -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.")
+10
View File
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
test "$#" -eq 1 || { echo 'Usage: bash scripts/bootstrap.sh relay.your-domain'; exit 1; }
test ! -f .env || { echo '.env exists; refusing to overwrite credentials'; exit 1; }
docker build -t hainuo-relay:0.1.0 .
docker run --rm --user "$(id -u):$(id -g)" \
-v "$PWD:/work" -w /work hainuo-relay:0.1.0 init --hostname "$1"
echo 'Initialized. Configure .env and replace runtime/tls/server.pem + server.key before acceptance.'
echo 'Keep operator/ca.key offline on the certificate-signing workstation.'
@@ -0,0 +1,24 @@
"""Read-only Docker Hub manifest check; never prints anonymous registry tokens."""
import json
import httpx
images = {
"python": "3.12-slim-bookworm", "redis": "8.2-alpine",
"postgres": "17-alpine", "caddy": "2-alpine",
}
with httpx.Client(timeout=30, follow_redirects=True) as client:
output = []
for name, tag in images.items():
token_response = client.get("https://auth.docker.io/token", params={"service": "registry.docker.io", "scope": f"repository:library/{name}:pull"})
token_response.raise_for_status()
token = token_response.json()["token"]
response = client.get(f"https://registry-1.docker.io/v2/library/{name}/manifests/{tag}", headers={
"Authorization": "Bearer " + token,
"Accept": "application/vnd.oci.image.index.v1+json, application/vnd.docker.distribution.manifest.list.v2+json"})
response.raise_for_status()
manifests = response.json().get("manifests", [])
arm = [x for x in manifests if x.get("platform", {}).get("architecture") == "arm64" and x.get("platform", {}).get("os") == "linux"]
if not arm:
raise RuntimeError(f"No linux/arm64 manifest for {name}:{tag}")
output.append({"image": f"{name}:{tag}", "arm64_digest": arm[0]["digest"]})
print(json.dumps(output, indent=2))
@@ -0,0 +1,117 @@
"""Runs on the ECS; asks for App Secret on the TTY and never echoes it.
Does not send messages, create platform subscriptions, or reset device binding.
Only commits local configuration after platform credential + WS provisioning checks.
"""
import argparse
import getpass
import json
import os
import re
import shutil
import sys
import uuid
from datetime import datetime
from pathlib import Path
from urllib.parse import urlsplit
import httpx
def check_platform(base, app_id, secret, client):
results = []
for path, field in [("/open-apis/auth/v3/app_access_token/internal", "app_access_token"),
("/open-apis/auth/v3/tenant_access_token/internal", "tenant_access_token")]:
response = client.post(base + path, json={"app_id": app_id, "app_secret": secret})
if response.status_code != 200:
raise RuntimeError("platform HTTP " + str(response.status_code))
data = response.json()
if data.get("code") != 0 or not data.get(field):
code = data.get("code")
raise RuntimeError("platform code " + str(code)[:30] if isinstance(code, int) else "unexpected platform response")
results.append(field + ": valid (not displayed)")
response = client.post(base + "/callback/ws/endpoint", json={"AppID": app_id, "AppSecret": secret}, headers={"locale": "zh"})
if response.status_code != 200:
raise RuntimeError("long-connection provisioning HTTP " + str(response.status_code))
data = response.json()
if data.get("code") != 0:
code = data.get("code")
raise RuntimeError("long-connection code " + str(code)[:30] if isinstance(code, int) else "unexpected long-connection response")
url = (data.get("data") or {}).get("URL", "")
parsed = urlsplit(url)
if parsed.scheme != "wss" or not parsed.hostname:
raise RuntimeError("platform did not return a WSS endpoint")
results.append("long-connection endpoint: " + parsed.hostname + " (credential query omitted)")
return results
def update_env(path, settings):
if not path.is_file():
raise RuntimeError("existing .env missing; refusing to initialize deployment")
original = path.read_text(encoding="utf-8-sig")
backup = path.with_name(".env.before-feishu-" + datetime.now().strftime("%Y%m%d-%H%M%S-%f"))
shutil.copy2(path, backup)
backup.chmod(0o600)
lines, written = [], set()
for line in original.splitlines():
name = line.split("=", 1)[0].strip() if "=" in line and not line.lstrip().startswith("#") else ""
if name in settings:
if name not in written:
lines.append(name + "=" + settings[name])
written.add(name)
else:
lines.append(line)
lines += [name + "=" + value for name, value in settings.items() if name not in written]
temporary = path.with_name(".env.feishu-tmp-" + uuid.uuid4().hex)
fd = os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
try:
with os.fdopen(fd, "w", encoding="utf-8", newline="\n") as out:
out.write("\n".join(lines) + "\n")
os.replace(temporary, path)
path.chmod(0o600)
finally:
if temporary.exists():
temporary.unlink()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--root", type=Path, required=True)
parser.add_argument("--app-id", default="cli_a8733bb23078d3c7")
parser.add_argument("--api-base", default="https://open.w.cnooc.com.cn")
args = parser.parse_args()
base = args.api_base.rstrip("/")
parts = urlsplit(base)
if parts.scheme != "https" or not parts.hostname or parts.path or parts.query or parts.fragment or parts.username or parts.password:
raise RuntimeError("API base must be HTTPS origin only; do not append /app/")
if not re.fullmatch(r"cli_[a-zA-Z0-9]{16}", args.app_id):
raise RuntimeError("invalid App ID format")
if not sys.stdin.isatty():
raise RuntimeError("TTY required; use docker run -it so the secret stays hidden")
secret = getpass.getpass("App Secret (hidden input): ")
if not re.fullmatch(r"[A-Za-z0-9_-]{16,256}", secret):
raise RuntimeError("invalid App Secret shape; nothing saved")
# Never follow HTTP redirects carrying credentials, trust proxy environment,
# print upstream bodies, or disable TLS verification.
with httpx.Client(timeout=20, follow_redirects=False, trust_env=False) as client:
results = check_platform(base, args.app_id, secret, client)
update_env(args.root / ".env", {
"HN_FEISHU_APP_ID": args.app_id, "HN_FEISHU_APP_SECRET": secret,
"HN_FEISHU_API_BASE": base,
"HN_FEISHU_AUTHORIZE_URL": base + "/open-apis/authen/v1/authorize",
"HN_FEISHU_ENABLED": "true", "HN_FEISHU_EVENT_SOURCE": "longconn", "HN_OAUTH_ENABLED": "false",
})
for result in results:
print(result)
print("Configuration saved on this ECS; secret/token not displayed. OAuth remains disabled pending callback verification.")
if __name__ == "__main__":
try:
main()
except Exception as error:
if isinstance(error, RuntimeError):
print("Configuration not completed: " + str(error), file=sys.stderr)
else:
print("Configuration not completed: " + type(error).__name__ + "; no credentials displayed", file=sys.stderr)
raise SystemExit(1) from None
@@ -0,0 +1,167 @@
"""Fetch official linux/arm64 images via Docker's ECR Public official-image mirror.
No daemon or third-party image rebuild is required. Verify registry manifest/blob
digests and uncompressed rootfs diff_ids, then write Docker-load-compatible tar.
"""
import argparse
import concurrent.futures
import gzip
import hashlib
import io
import json
import os
import tarfile
import time
from pathlib import Path
import httpx
REGISTRY = "https://public.ecr.aws"
IMAGES = {"python": "3.12-slim-bookworm", "postgres": "17-alpine",
"redis": "8.2-alpine", "caddy": "2-alpine"}
ACCEPT = ",".join(["application/vnd.oci.image.index.v1+json",
"application/vnd.docker.distribution.manifest.list.v2+json",
"application/vnd.oci.image.manifest.v1+json",
"application/vnd.docker.distribution.manifest.v2+json"])
def digest_file(path):
value = hashlib.sha256()
with path.open("rb") as source:
for chunk in iter(lambda: source.read(4 * 1024 * 1024), b""):
value.update(chunk)
return value.hexdigest()
def checked_response(client, url, headers, expected=None):
for attempt in range(6):
response = client.get(url, headers=headers)
if response.status_code not in {429, 500, 502, 503, 504}:
break
time.sleep(min(2 ** attempt, 12))
response.raise_for_status()
data = response.content
actual = "sha256:" + hashlib.sha256(data).hexdigest()
if expected and actual != expected:
raise RuntimeError("manifest digest mismatch")
return json.loads(data), actual
def blob(name, headers, descriptor, cache):
digest = descriptor["digest"].split(":", 1)[1]
path = cache / (digest + ".blob")
if path.exists() and path.stat().st_size == descriptor["size"] and digest_file(path) == digest:
return path
for attempt in range(5):
try:
temp = path.with_suffix(".part")
# Do not forward the registry bearer token across a blob-storage redirect.
with httpx.Client(timeout=httpx.Timeout(120, connect=20), follow_redirects=False) as client:
url = f"{REGISTRY}/v2/docker/library/{name}/blobs/{descriptor['digest']}"
request_headers = {"Authorization": headers["Authorization"]}
for _ in range(6):
with client.stream("GET", url, headers=request_headers) as response:
if response.is_redirect:
url = str(response.url.join(response.headers["location"]))
request_headers = {} # AWS signed blob URL needs no bearer token.
continue
response.raise_for_status()
value, count = hashlib.sha256(), 0
with temp.open("wb") as output:
for data in response.iter_bytes(1024 * 1024):
output.write(data)
value.update(data)
count += len(data)
if count != descriptor["size"] or value.hexdigest() != digest:
raise RuntimeError("blob integrity mismatch")
temp.replace(path)
print(json.dumps({"downloaded_mb": round(count / 1048576, 1), "blob": digest[:16]}), flush=True)
return path
raise RuntimeError("too many blob redirects")
except (httpx.HTTPError, OSError, RuntimeError) as exc:
if attempt == 4:
raise RuntimeError(f"blob download failed ({type(exc).__name__}); credentials omitted") from None
time.sleep(min(2 ** attempt, 8))
def add_bytes(archive, name, data):
entry = tarfile.TarInfo(name)
entry.size, entry.mode, entry.mtime = len(data), 0o644, 0
archive.addfile(entry, io.BytesIO(data))
def export_image(record, cache, out):
config = record["config"]
cfg_name = config["digest"].split(":")[1] + ".json"
cfg_data = (cache / (config["digest"].split(":")[1] + ".blob")).read_bytes()
cfg = json.loads(cfg_data)
if cfg.get("architecture") != "arm64" or cfg.get("os") != "linux":
raise RuntimeError("image is not linux/arm64")
layers, diffids = [], cfg["rootfs"]["diff_ids"]
if len(diffids) != len(record["layers"]):
raise RuntimeError("rootfs/layer count mismatch")
for desc, expected in zip(record["layers"], diffids):
blobpath = cache / (desc["digest"].split(":")[1] + ".blob")
plain = cache / (expected.split(":")[1] + ".layer.tar")
if not plain.exists() or digest_file(plain) != expected.split(":")[1]:
temp = plain.with_suffix(".part")
with blobpath.open("rb") as source:
magic = source.read(2)
opener = gzip.open if magic == b"\x1f\x8b" else open
value = hashlib.sha256()
with opener(blobpath, "rb") as source, temp.open("wb") as destination:
for chunk in iter(lambda: source.read(4 * 1024 * 1024), b""):
value.update(chunk)
destination.write(chunk)
if "sha256:" + value.hexdigest() != expected:
raise RuntimeError("uncompressed rootfs digest mismatch")
temp.replace(plain)
layers.append((expected.split(":")[1] + "/layer.tar", plain))
output = out / (record["name"] + "-" + record["tag"].replace("/", "-") + "-linux-arm64.tar")
with tarfile.open(output.with_suffix(".part"), "w") as archive:
add_bytes(archive, cfg_name, cfg_data)
for name, path in layers:
archive.add(path, arcname=name, recursive=False)
add_bytes(archive, "manifest.json", json.dumps([{"Config": cfg_name,
"RepoTags": [record["name"] + ":" + record["tag"]],
"Layers": [name for name, _ in layers]}]).encode())
output.with_suffix(".part").replace(output)
record["archive"] = output.name
record["archive_sha256"] = digest_file(output)
record["config_json"] = cfg
print(json.dumps({"exported": output.name, "mb": round(output.stat().st_size / 1048576, 1)}), flush=True)
def main():
p = argparse.ArgumentParser()
p.add_argument("--output", type=Path, required=True)
p.add_argument("--cache", type=Path, required=True)
args = p.parse_args()
args.output.mkdir(parents=True, exist_ok=True)
args.cache.mkdir(parents=True, exist_ok=True)
records = []
with httpx.Client(timeout=30, follow_redirects=False) as client:
for name, tag in IMAGES.items():
token_response = client.get(REGISTRY + "/token/", params={"service": "public.ecr.aws", "scope": f"repository:docker/library/{name}:pull"})
token_response.raise_for_status()
headers = {"Authorization": "Bearer " + token_response.json()["token"], "Accept": ACCEPT}
index, index_digest = checked_response(client, f"{REGISTRY}/v2/docker/library/{name}/manifests/{tag}", headers)
descriptor = next(x for x in index["manifests"] if x.get("platform", {}).get("architecture") == "arm64"
and x.get("platform", {}).get("os") == "linux")
manifest, manifest_digest = checked_response(client, f"{REGISTRY}/v2/docker/library/{name}/manifests/{descriptor['digest']}", headers, descriptor["digest"])
record = {"name": name, "tag": tag, "upstream": f"docker.io/library/{name}:{tag}",
"download_source": f"public.ecr.aws/docker/library/{name}:{tag}",
"platform": "linux/arm64", "index_digest": index_digest,
"manifest_digest": manifest_digest, "config": manifest["config"], "layers": manifest["layers"]}
print(json.dumps({"image": name + ":" + tag, "compressed_mb": round(sum(x['size'] for x in manifest['layers']) / 1048576, 1)}), flush=True)
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as pool:
futures = [pool.submit(blob, name, headers, desc, args.cache) for desc in [manifest["config"], *manifest["layers"]]]
for future in futures:
future.result()
export_image(record, args.cache, args.output)
records.append(record)
(args.output / "IMAGE_MANIFEST.json").write_text(json.dumps(records, indent=2) + "\n", encoding="utf-8")
if __name__ == "__main__":
main()
@@ -0,0 +1,132 @@
"""Run the actual 1.065 API adapter, isolated from bot/scheduler startup.
Run with the installed Hermes Python, not the relay Python. No installer code
or existing profiles are edited. Only a separate loopback listener is started.
"""
import argparse
import asyncio
import json
import logging
import os
import secrets
import sys
from pathlib import Path
def prepare_environment(install_root, profile, port, config_path):
if not profile or any(c not in "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-" for c in profile):
raise ValueError("invalid profile")
source = install_root / "hermes-agent"
home = install_root if profile == "default" else install_root / "profiles" / profile
if not (source / "gateway/platforms/api_server.py").is_file() or not (home / "config.yaml").is_file():
raise RuntimeError("installation/profile does not match installer 1.065 layout")
# Environment must be selected before importing any Hermes module (global caches).
os.environ["HERMES_HOME"] = str(home.resolve())
os.environ["API_SERVER_HOST"] = "127.0.0.1"
os.environ["API_SERVER_PORT"] = str(port)
os.environ["API_SERVER_CORS_ORIGINS"] = ""
os.environ["HERMES_SKIP_CWD_MIGRATION"] = "1"
sys.path.insert(0, str(source.resolve()))
# Use the package's own dotenv support to reuse the real profile model credentials.
from dotenv import load_dotenv
load_dotenv(install_root / ".env", override=False)
if home != install_root:
load_dotenv(home / ".env", override=True)
if config_path.exists():
data = json.loads(config_path.read_text(encoding="utf-8-sig"))
if data.get("url") != f"http://127.0.0.1:{port}" or data.get("mode") != "text-only" or len(data.get("api_key", "")) < 32:
raise ValueError("existing local adapter config does not match listener")
key = data["api_key"]
else:
key = secrets.token_hex(32)
data = {"url": f"http://127.0.0.1:{port}", "api_key": key, "mode": "text-only"}
config_path.parent.mkdir(parents=True, exist_ok=True)
with config_path.open("x", encoding="utf-8") as out:
json.dump(data, out, indent=2)
if os.name != "nt":
config_path.chmod(0o600)
os.environ["API_SERVER_KEY"] = key
return key
def restricted_adapter_class(native_class, tools_config):
original_tools = tools_config._get_platform_tools
def without_remote_tools(config, platform, **kwargs):
if platform == "api_server":
return set()
return original_tools(config, platform, **kwargs)
tools_config._get_platform_tools = without_remote_tools
class RestrictedNativeAPI(native_class):
def _wire_plugin_handlers(self, app):
# Native connect() normally appends plugin HTTP handlers after the
# route table. Do not let that reopen the restricted companion API.
return None
def _create_agent(self, *args, **kwargs):
agent = super()._create_agent(*args, **kwargs)
# Model/tool construction is native. Fail BEFORE run_conversation if
# any plugin/recovery path silently reintroduced tools.
if getattr(agent, "tools", None) or getattr(agent, "valid_tool_names", None):
raise RuntimeError("text-only relay listener refuses an Agent with tools")
return agent
def _http_route_table(self):
allowed = {("GET", "/health"), ("GET", "/v1/models"),
("GET", "/v1/capabilities"), ("POST", "/v1/chat/completions")}
return [(method, path, handler) for method, path, handler in super()._http_route_table()
if (method, path) in allowed]
async def _handle_capabilities(self, request):
response = await super()._handle_capabilities(request)
data = json.loads(response.body)
data["hainuo_relay_policy"] = {"mode": "text-only", "tools": "disabled", "contract": "hainuo-1065-v1"}
# Do not advertise native admin/session/tool operations removed above.
endpoints = data.get("endpoints", {})
data["endpoints"] = {k: v for k, v in endpoints.items() if k in {"health", "models", "capabilities", "chat_completions"}}
data["features"] = {"text_only": True, "tools": False, "streaming": False}
from aiohttp import web
return web.json_response(data)
return RestrictedNativeAPI
async def serve_native(port, key):
from gateway.config import PlatformConfig
from gateway.platforms.api_server import APIServerAdapter
from hermes_cli import tools_config
adapter_class = restricted_adapter_class(APIServerAdapter, tools_config)
adapter = adapter_class(PlatformConfig(enabled=True, extra={"host": "127.0.0.1", "port": port,
"key": key, "model_name": "hainuo-relay-text"}))
try:
if not await adapter.connect():
raise RuntimeError("native API did not start; verify port and native installation")
print(f"海诺原生API已启动:http://127.0.0.1:{port}(纯文本,无工具;凭据未回显)", flush=True)
await asyncio.Event().wait()
finally:
await adapter.disconnect()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--install-root", type=Path, required=True)
parser.add_argument("--profile", default="hainuo")
parser.add_argument("--port", type=int, default=8643)
parser.add_argument("--config", type=Path, required=True)
args = parser.parse_args()
if not 1024 <= args.port <= 65535:
raise ValueError("invalid port")
# Native logs can contain provider/user content; this companion emits status only.
logging.disable(logging.CRITICAL)
key = prepare_environment(args.install_root.resolve(), args.profile, args.port, args.config.resolve())
try:
asyncio.run(serve_native(args.port, key))
except KeyboardInterrupt:
print("海诺原生API已停止。")
except Exception as error:
print(f"原生API启动失败({type(error).__name__});未输出凭据。请使用安装包自带Python,并核对安装根目录与Profile。", file=sys.stderr)
raise SystemExit(1) from None
if __name__ == "__main__":
main()
@@ -0,0 +1,42 @@
#!/usr/bin/env bash
set -euo pipefail
project_dir="$(cd "$(dirname "$0")/.." && pwd)"
bundle_dir="$(dirname "$project_dir")"
cd "$project_dir"
case "$(uname -m)" in
aarch64|arm64) ;;
*) echo 'This bundle is only for Linux ARM64.'; exit 1 ;;
esac
docker info >/dev/null
docker compose version
test -d "$bundle_dir/images" || { echo 'Use the complete offline bundle, including images/.'; exit 1; }
test -d wheelhouse || { echo 'Missing ARM64 wheelhouse.'; exit 1; }
if [ ! -f .env ] && [ "$#" -ne 1 ]; then
echo 'Usage: bash install.sh relay.your-company-domain'
exit 1
fi
if [ -f "$bundle_dir/SHA256SUMS" ]; then
(cd "$bundle_dir" && sha256sum -c SHA256SUMS)
fi
for image_file in "$bundle_dir"/images/*.tar; do
docker load -i "$image_file"
done
# Loaded Python base + local ARM64 wheels only. No apt/pip download or remote build frontend.
docker build --network none --pull=false -f Dockerfile.offline -t hainuo-relay:0.1.0 .
if [ ! -f .env ]; then
docker run --rm --user "$(id -u):$(id -g)" -v "$project_dir:/work" -w /work \
hainuo-relay:0.1.0 init --hostname "$1"
fi
if [ "$(id -u)" -eq 0 ]; then
chown -R 10001:10001 runtime/connector
chmod 700 runtime/connector
chmod 600 runtime/connector/connector.key
else
sudo chown -R 10001:10001 runtime/connector
sudo chmod 700 runtime/connector
sudo chmod 600 runtime/connector/connector.key
fi
docker compose -f compose.yaml -f compose.offline.yaml up -d --no-build --pull never
bash scripts/verify_deployment.sh
echo 'Offline service installation finished. Configure real TLS/platform settings for acceptance.'
echo 'Keep operator/ca.key on the offline signing workstation; do not share .env or private keys.'
+30
View File
@@ -0,0 +1,30 @@
"""Package only source/deployment/tests; never include keys, .env or runtime data."""
import hashlib
import json
import zipfile
from pathlib import Path
root = Path(__file__).resolve().parents[1]
output = root.parent / "海诺中继服务_0.1.0_源码部署包.zip"
allowed_dirs = {"relay", "scripts", "tests", "docs", "deploy"}
allowed_files = {"README.md", "pyproject.toml", "requirements.lock", "Dockerfile", "Dockerfile.offline", ".dockerignore",
".gitignore", "compose.yaml", "compose.acceptance.yaml", "compose.offline.yaml"}
items = []
for path in root.rglob("*"):
if not path.is_file():
continue
relative = path.relative_to(root)
if "__pycache__" in relative.parts or ".pytest_cache" in relative.parts:
continue
if relative.parts[0] not in allowed_dirs and relative.as_posix() not in allowed_files:
continue
items.append((relative, path))
manifest = {relative.as_posix(): hashlib.sha256(path.read_bytes()).hexdigest() for relative, path in sorted(items)}
(root / "SOURCE_MANIFEST.json").write_text(json.dumps(manifest, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
with zipfile.ZipFile(output, "w", zipfile.ZIP_DEFLATED) as archive:
for relative, path in sorted(items):
archive.write(path, "hainuo-relay/" + relative.as_posix())
archive.write(root / "SOURCE_MANIFEST.json", "hainuo-relay/SOURCE_MANIFEST.json")
digest = hashlib.sha256(output.read_bytes()).hexdigest()
output.with_suffix(".zip.sha256").write_text(digest + "\n", encoding="ascii")
print(json.dumps({"archive": str(output), "files": len(items) + 1, "sha256": digest}, ensure_ascii=False))
@@ -0,0 +1,86 @@
"""Assemble an allowlisted offline delivery, excluding credentials and runtime data."""
import base64
import hashlib
import json
import shutil
import subprocess
import sys
import tarfile
import zipfile
from pathlib import Path
root = Path(__file__).resolve().parents[1]
workspace = root.parent
bundle = workspace / "hainuo-offline-arm64"
images = bundle / "images"
licenses = bundle / "licenses"
records = json.loads((images / "IMAGE_MANIFEST.json").read_text(encoding="utf-8"))
assert {x["name"] for x in records} == {"python", "postgres", "redis", "caddy"}
assert json.loads((images / "VERIFICATION.json").read_text())["status"] == "passed"
subprocess.run([sys.executable, str(root / "scripts/package.py")], check=True)
included = []
with zipfile.ZipFile(workspace / "海诺中继服务_0.1.0_源码部署包.zip") as source:
for member in source.infolist():
relative = Path(member.filename)
assert relative.parts[0] == "hainuo-relay" and ".." not in relative.parts
destination = bundle / relative
destination.parent.mkdir(parents=True, exist_ok=True)
destination.write_bytes(source.read(member))
included.append(destination)
for path in sorted((workspace / ".arm64-wheelcheck").glob("*.whl")):
destination = bundle / "hainuo-relay/wheelhouse" / path.name
destination.parent.mkdir(parents=True, exist_ok=True)
shutil.copyfile(path, destination)
included.append(destination)
for record in records:
assert Path(record["archive"]).name == record["archive"]
included.append(images / record["archive"])
included += [images / "IMAGE_MANIFEST.json", images / "VERIFICATION.json"]
caddy = json.loads((licenses / "caddy-license.json").read_text())
assert caddy["license"]["spdx_id"] == "Apache-2.0"
(licenses / "CADDY-LICENSE.txt").write_bytes(base64.b64decode(caddy["content"]))
allowed_licenses = {"redis-licensing.html", "postgresql-license.html", "python-license.html", "COPYING",
"LICENSE.txt", "AGPL-3.0.txt", "caddy-license.json", "CADDY-LICENSE.txt"}
for path in licenses.iterdir():
if path.name in allowed_licenses or (path.name.startswith("redis-8.2.") and path.name.endswith("-source.tar.gz")):
included.append(path)
wrapper = bundle / "install.sh"
wrapper.write_text('#!/usr/bin/env bash\nset -euo pipefail\ncd "$(dirname "$0")"\nexec bash hainuo-relay/scripts/install_offline.sh "$@"\n', encoding="utf-8", newline="\n")
included.append(wrapper)
readme = bundle / "README.txt"
readme.write_text("海诺中继 Linux ARM64 官方镜像离线部署包\n\n"
"宿主机需要预装Docker Engine和Compose v2。\n"
"在本目录执行:bash install.sh relay.实际公司域名\n"
"脚本先校验SHA256,再docker load四个官方镜像,离线构建本项目服务并启动。\n"
"安装阶段不访问镜像仓库/PyPI;业务仍需连通海能Work和员工PC。\n"
"Python 3.12.15 / PostgreSQL 17.11 / Redis 8.2.10 / Caddy 2.11.7,全部linux/arm64。\n"
"Redis选择AGPLv3;其他项目许可和Redis对应版本源码见licenses/。\n"
"精确上游摘要与下载来源见images/IMAGE_MANIFEST.json,校验记录见VERIFICATION.json。\n"
"本项目服务镜像由源码在目标机本地构建,不是声称来自某官方仓库的海诺镜像。\n"
"本地无Docker daemon,已验证镜像内容及摘要,未声称已在目标机运行通过。\n"
"完整部署、真实1.065适配边界和TLS配置见hainuo-relay/docs/。\n", encoding="utf-8")
included.append(readme)
hashes = []
for path in sorted(set(included)):
value = hashlib.sha256()
with path.open("rb") as source:
for data in iter(lambda: source.read(4 * 1024 * 1024), b""):
value.update(data)
hashes.append(value.hexdigest() + " " + path.relative_to(bundle).as_posix())
checksum = bundle / "SHA256SUMS"
checksum.write_text("\n".join(hashes) + "\n", encoding="utf-8", newline="\n")
included.append(checksum)
output = workspace / "hainuo-relay-offline-arm64-0.1.0.tar.gz"
print("Packaging verified images and offline dependencies...", flush=True)
with tarfile.open(output, "w:gz", compresslevel=3) as archive:
for path in sorted(set(included)):
arcname = "hainuo-offline-arm64/" + path.relative_to(bundle).as_posix()
assert "/runtime/" not in arcname and "/operator/" not in arcname and not arcname.endswith("/.env")
archive.add(path, arcname=arcname, recursive=False)
value = hashlib.sha256()
with output.open("rb") as source:
for data in iter(lambda: source.read(4 * 1024 * 1024), b""):
value.update(data)
output.with_suffix(output.suffix + ".sha256").write_text(value.hexdigest() + "\n", encoding="ascii")
print(json.dumps({"archive": str(output), "bytes": output.stat().st_size, "sha256": value.hexdigest(),
"files": len(set(included))}, ensure_ascii=False))
@@ -0,0 +1,25 @@
"""Create an optional ARM64 Python dependency bundle, not container base images."""
import argparse
import hashlib
import json
import zipfile
from pathlib import Path
parser = argparse.ArgumentParser()
parser.add_argument("--wheel-directory", type=Path, required=True)
parser.add_argument("--output", type=Path, required=True)
args = parser.parse_args()
wheels = sorted(args.wheel_directory.glob("*.whl"))
if not wheels or any("win_" in x.name or "x86_64" in x.name for x in wheels):
raise SystemExit("expected only ARM64 or platform-independent wheels")
manifest = {p.name: hashlib.sha256(p.read_bytes()).hexdigest() for p in wheels}
with zipfile.ZipFile(args.output, "w", zipfile.ZIP_DEFLATED) as archive:
for path in wheels:
archive.write(path, "wheelhouse/" + path.name)
archive.writestr("wheelhouse/SHA256SUMS.json", json.dumps(manifest, indent=2) + "\n")
archive.writestr("README.txt", "Linux ARM64, CPython 3.12 dependency wheels.\n"
"python -m pip install --no-index --find-links wheelhouse -r hainuo-relay/requirements.lock\n"
"Docker base images are NOT included. ARM64 runtime and kernel compatibility still require target verification.\n")
digest = hashlib.sha256(args.output.read_bytes()).hexdigest()
args.output.with_suffix(".zip.sha256").write_text(digest + "\n", encoding="ascii")
print(json.dumps({"archive": str(args.output), "wheels": len(wheels), "sha256": digest}, ensure_ascii=False))
+12
View File
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -euo pipefail
printf 'Architecture: '; uname -m
printf 'Page size: '; getconf PAGESIZE
docker version --format '{{.Server.Version}}'
docker compose version
test -f .env || { echo 'Run init first'; exit 1; }
test -f runtime/tls/server.pem
test -f runtime/connector/connector.key
test -f runtime/trust/ca.pem
docker compose config --quiet
echo 'Preflight passed. This does not confirm platform/PC connectivity.'
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
test -f .env || { echo 'Missing existing .env'; exit 1; }
test -d wheelhouse || { echo 'Missing existing wheelhouse'; exit 1; }
backup_tag="hainuo-relay:before-feishu-$(date +%Y%m%d-%H%M%S)"
docker image tag hainuo-relay:0.1.0 "$backup_tag"
echo "Rollback image: $backup_tag"
docker build --network none --pull=false -f Dockerfile.offline -t hainuo-relay:0.1.0 .
# No App Secret is embedded here, in the image, or in process arguments.
docker run --rm -it --user 0:0 --entrypoint python \
--mount "type=bind,source=$PWD,target=/work" \
hainuo-relay:0.1.0 /work/scripts/configure_feishu_longconn.py --root /work
cat > compose.work.yaml <<'YAML'
services:
connector:
environment:
HN_FEISHU_EVENT_SOURCE: longconn
HN_OAUTH_ENABLED: "false"
HN_REQUEST_TIMEOUT: "180"
longconn:
environment:
HN_FEISHU_EVENT_SOURCE: longconn
YAML
args=(-f compose.yaml -f compose.offline.yaml)
for extra in compose.edge-fix.yaml compose.native-api.yaml compose.h5.yaml; do
if [ -f "$extra" ]; then args+=(-f "$extra"); fi
done
args+=(-f compose.work.yaml)
docker compose "${args[@]}" --profile longconn config --quiet
docker compose "${args[@]}" --profile longconn up -d --no-build --pull never --no-deps --force-recreate connector
for attempt in {1..30}; do
if docker compose "${args[@]}" exec -T connector python -c \
"import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/ready',timeout=2)" 2>/dev/null; then
docker compose "${args[@]}" --profile longconn up -d --no-build --pull never --no-deps --force-recreate longconn
docker compose "${args[@]}" --profile longconn ps connector longconn
echo 'Next: docker compose logs --tail 30 longconn; look for platform_connected.'
exit 0
fi
sleep 2
done
echo 'Connector not ready; inspect docker compose logs connector. Do not post .env contents.'
exit 1
+17
View File
@@ -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
+32
View File
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
test -f .env || { echo 'Run inside the existing deployment; .env missing.'; exit 1; }
test -d wheelhouse || { echo 'Existing offline wheelhouse missing.'; exit 1; }
backup_tag="hainuo-relay:before-h5-$(date +%Y%m%d-%H%M%S)"
docker image tag hainuo-relay:0.1.0 "$backup_tag"
echo "Rollback image: $backup_tag"
# Offline dependencies are unchanged; do not pull images or initialize a new database.
docker build --network none --pull=false -f Dockerfile.offline -t hainuo-relay:0.1.0 .
cat > compose.h5.yaml <<'YAML'
services:
connector:
environment:
HN_REQUEST_TIMEOUT: "180"
YAML
args=(-f compose.yaml -f compose.offline.yaml)
if [ -f compose.edge-fix.yaml ]; then args+=(-f compose.edge-fix.yaml); fi
args+=(-f compose.h5.yaml)
docker compose "${args[@]}" config --quiet
docker compose "${args[@]}" up -d --no-build --pull never --no-deps --force-recreate connector
for attempt in {1..30}; do
if docker compose "${args[@]}" exec -T connector python -c \
"import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/ready',timeout=2)" 2>/dev/null; then
docker compose "${args[@]}" exec -T connector python -c \
"import urllib.request; t=urllib.request.urlopen('http://127.0.0.1:8000/h5').read(); assert b'pair-form' in t; print('H5 pairing/chat revision is ready')"
exit 0
fi
sleep 2
done
echo 'Connector not ready. Read: sudo docker compose logs --tail 80 connector'
exit 1
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
set -euo pipefail
root=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)
cd "$root"
stamp=$(date +%Y%m%d-%H%M%S)
docker image tag hainuo-relay:0.1.0 "hainuo-relay:before-userapi-$stamp"
docker build --network none --pull=false -f Dockerfile.offline -t hainuo-relay:0.1.0 .
args=(-f compose.yaml)
for override in compose.offline.yaml compose.edge-fix.yaml compose.h5.yaml compose.work.yaml compose.feishu.yaml compose.autoreg.yaml; do
if [[ -f "$override" ]]; then args+=(-f "$override"); fi
done
args+=(-f compose.userapi.yaml)
docker compose "${args[@]}" up -d --no-build --pull never --no-deps --force-recreate connector router
if [[ -n "$(docker compose -f compose.yaml ps -q longconn)" ]]; then
docker compose "${args[@]}" --profile longconn up -d --no-build --pull never --no-deps --force-recreate longconn
fi
docker compose "${args[@]}" ps
echo "Previous image: hainuo-relay:before-userapi-$stamp"
echo 'Keep compose.autoreg.yaml and compose.userapi.yaml in future application recreations.'
echo 'Configure platform callback/scopes, then each user completes personal authorization.'
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
set -euo pipefail
root=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)
cd "$root"
test -f .env && test -d wheelhouse && test -f Dockerfile.offline
stamp=$(date +%Y%m%d-%H%M%S)
# Enable automatic issuance using the current test CA, never generate a new CA.
# Only the internal issuer container receives this key; no host port is published.
if [[ ! -f runtime/issuer/ca.key ]]; then
test -f operator/ca.key || { echo 'Existing CA key missing: provide the original CA to the internal issuer first.' >&2; exit 1; }
install -d -m 700 -o 10001 -g 10001 runtime/issuer
install -m 400 -o 10001 -g 10001 operator/ca.key runtime/issuer/ca.key
fi
docker image tag hainuo-relay:0.1.0 "hainuo-relay:before-work1067-$stamp"
docker build --network none --pull=false -f Dockerfile.offline -t hainuo-relay:0.1.0 .
args=(-f compose.yaml)
for override in compose.offline.yaml compose.edge-fix.yaml compose.h5.yaml compose.work.yaml compose.feishu.yaml; do
if [[ -f "$override" ]]; then args+=(-f "$override"); fi
done
args+=(-f compose.autoreg.yaml)
# Bring up signer and verify it before changing the serving connector.
docker compose "${args[@]}" up -d --no-build --pull never --no-deps --force-recreate enrollment-signer
docker compose "${args[@]}" exec -T enrollment-signer python -c 'import time,urllib.request
for attempt in range(30):
try:
urllib.request.urlopen("http://127.0.0.1:8000/ready", timeout=2)
print("Automatic certificate issuer ready")
break
except Exception:
time.sleep(1)
else:
raise SystemExit("Issuer not ready; connector unchanged")'
docker compose "${args[@]}" up -d --no-build --pull never --no-deps --force-recreate connector router
if [[ -n "$(docker compose -f compose.yaml ps -q longconn)" ]]; then
docker compose "${args[@]}" --profile longconn up -d --no-build --pull never --no-deps --force-recreate longconn
fi
docker compose "${args[@]}" ps
echo "Previous image: hainuo-relay:before-work1067-$stamp"
echo 'Automatic registration enabled. New PCs only need a Work binding code; no per-PC approval command.'
echo 'Keep -f compose.autoreg.yaml in future compose up commands.'
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
docker compose exec -T connector python -c \
"import urllib.request; import json; print(json.load(urllib.request.urlopen('http://127.0.0.1:8000/ready')))"
docker compose exec -T router python -c \
"import urllib.request; import json; print(json.load(urllib.request.urlopen('http://127.0.0.1:8000/ready')))"
docker compose exec -T connector python -m relay.cli status --url http://127.0.0.1:8000
echo 'Services and metadata query passed. Still run the two-PC acceptance procedure.'
@@ -0,0 +1,47 @@
"""Verify the Docker archive identity and each layer's uncompressed rootfs digest."""
import argparse
import hashlib
import json
import tarfile
from pathlib import Path
def stream_sha(source):
value = hashlib.sha256()
for chunk in iter(lambda: source.read(4 * 1024 * 1024), b""):
value.update(chunk)
return value.hexdigest()
def main():
p = argparse.ArgumentParser()
p.add_argument("directory", type=Path)
args = p.parse_args()
records = json.loads((args.directory / "IMAGE_MANIFEST.json").read_text(encoding="utf-8"))
output = []
for record in records:
path = args.directory / record["archive"]
with path.open("rb") as source:
assert stream_sha(source) == record["archive_sha256"], "archive checksum mismatch"
with tarfile.open(path) as archive:
manifest = json.load(archive.extractfile("manifest.json"))[0]
config_bytes = archive.extractfile(manifest["Config"]).read()
assert "sha256:" + hashlib.sha256(config_bytes).hexdigest() == record["config"]["digest"]
cfg = json.loads(config_bytes)
assert cfg["architecture"] == "arm64" and cfg["os"] == "linux"
assert manifest["RepoTags"] == [record["name"] + ":" + record["tag"]]
assert len(manifest["Layers"]) == len(cfg["rootfs"]["diff_ids"])
for name, expected in zip(manifest["Layers"], cfg["rootfs"]["diff_ids"]):
assert "sha256:" + stream_sha(archive.extractfile(name)) == expected, "rootfs checksum mismatch"
versions = {x.split("=", 1)[0]: x.split("=", 1)[1] for x in cfg.get("config", {}).get("Env", [])
if x.startswith(("PYTHON_VERSION=", "REDIS_VERSION=", "PG_VERSION=", "CADDY_VERSION="))}
output.append({"image": manifest["RepoTags"][0], "platform": "linux/arm64", "versions": versions,
"layers_verified": len(manifest["Layers"]), "archive_bytes": path.stat().st_size})
report = {"status": "passed", "checks": "archive/config/rootfs SHA256 + linux/arm64 + tags", "images": output,
"runtime_test": "not run; local Docker daemon unavailable"}
(args.directory / "VERIFICATION.json").write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8")
print(json.dumps(report, indent=2))
if __name__ == "__main__":
main()