diff --git a/README.md b/README.md index 9e78fc4..cee70c8 100644 --- a/README.md +++ b/README.md @@ -1,48 +1,68 @@ # 抖音直播间监控(douyinWEB) -独立监控项目:批量跟踪抖音直播间 **开播 / 停播 / 播放时长 / 截图**,控制台对齐 `sh2` 的 `youtube-relay-log` 可视化分析与场次列表。 +批量监控抖音直播间 **开播 / 停播 / 播放时长 / 截图**,控制台对齐 `sh2` 的 `youtube-relay-log` 可视化与场次列表。 -探测逻辑参考 `C:\project\sh2\d2ypp2`(`web/enter + a_bogus`、Cookie、循环间隔),不靠刷直播间 HTML 页面做批量探测,降低风控概率。 +探测逻辑参考 `sh2/d2ypp2`(`web/enter + a_bogus`、Cookie、循环间隔),默认不爬直播间 HTML,降低风控。 + +## 线上地址 + +| 用途 | URL | +|------|-----| +| 控制台 | https://dylive.nomadro.com/ | +| API | https://dylive.nomadro.com/api/health · `/docs` 见下方直连 | +| PocketBase | https://dylive.nomadro.com/_/ | +| SeaweedFS S3 | https://feiniu.nomadro.com/ | +| S3 别名 | https://s3.nomadro.com/ | + +HTTPS 由服务器 **Caddy** 自动签发;进程端口: + +| 服务 | 端口 | +|------|------| +| Next.js | `4015` | +| FastAPI | `8014` | +| PocketBase | `8092` | +| SeaweedFS S3 | `8333` | + +systemd:`douyinweb-web` / `douyinweb-api` / `douyinweb-pb`,以及 `seaweed-master|volume|filer|s3`。 ## 技术栈 -| 组件 | 端口 | 说明 | -|------|------|------| -| Next.js 15 | `:3000` | 雷达台 / 场次 / 直播间 / 主播 / 设置 | -| FastAPI | `:8000` | 轮询、场次、SSE、REST | -| PocketBase | `:8090` | 可选镜像同步 | -| SQLite | 本地 | 监控热路径(低延迟) | +- **Next.js 15** — 雷达台 / 场次 / 直播间 / 主播 / 截图 / 设置 +- **FastAPI** — 轮询、场次、SSE、REST +- **PocketBase** — 可选镜像 +- **SQLite** — 监控热路径 +- **SeaweedFS** — 开源对象存储(S3 API),封面上传 ## 功能 -- 直播间管理:单条 / 批量添加、标签、启用停用、黑名单、CSV 导出 -- 开播建场、停播结场、实时进行中计时(每房仅一场,幂等去重) -- 可视化分析 + 场次列表(时长、主播画像、封面墙、事件时间线) -- SSE 实时推送 + Toast -- Cookie:启动时自动从 sh2 `d2ypp2/config/config.ini` 的 `抖音cookie` 导入;也可在设置页填写,或调用 `POST /api/settings/sync-cookie` +- 直播间:单条 / 批量添加、标签、启用停用、黑名单、CSV 导出 +- 开播建场、停播结场、进行中实时计时(每房仅一场,幂等去重) +- 开播截图 → 本地 + 上传 SeaweedFS;进行中场次缩略图,**点击放大** +- 可视化分析、主播画像、封面墙、事件时间线、SSE + Toast +- Cookie:可从 sh2 `config.ini` 的 `抖音cookie` 导入(`POST /api/settings/sync-cookie`) - 防风控(对齐 sh2): - - 仅 `webcast/room/web/enter` + `a_bogus`(默认不做 HTML 回退) - - 并发 1、请求间隔抖动、离线约 300s / 在播约 120s - - 错误退避;风控短冷却(约 90s),非长时间熔断 - - 大批量房间错开 `next_check_at` + - 仅 `webcast/room/web/enter` + `a_bogus` + - 并发 1、间隔抖动、离线约 300s / 在播约 120s + - 错误退避;风控短冷却约 90s ## 目录 ``` douyinWEB/ - backend/app/ FastAPI(douyin.py / monitor.py / ab_sign.py …) + backend/app/ FastAPI(douyin / monitor / ab_sign / s3_storage …) + backend/data/ SQLite、封面、导入快照(入库便于部署同步) frontend/src/ Next.js App Router - pb/ PocketBase(需自行放置 pocketbase.exe) - scripts/ start.bat、导入与防风控脚本 + pb/ PocketBase 数据;Linux 二进制部署时下载 + scripts/ 本地启动与 VPS 部署脚本 ``` -## 启动 +## 本地启动 ```bat scripts\start.bat ``` -或分别启动: +或: ```bat pb\pocketbase.exe serve --http=127.0.0.1:8090 @@ -51,76 +71,66 @@ cd frontend && npm run dev -- --port 3000 --hostname 127.0.0.1 ``` - 控制台:http://127.0.0.1:3000 -- API 文档:http://127.0.0.1:8000/docs -- PocketBase:http://127.0.0.1:8090/_/ +- API:http://127.0.0.1:8000/docs +- PB:http://127.0.0.1:8090/_/ -默认 PB 管理员:`admin@douyin.local` / `DouyinMonitor_Admin_2026` - -### 后端依赖 +默认 PB:`admin@douyin.local` / `DouyinMonitor_Admin_2026` ```bat -cd backend -pip install -r requirements.txt +cd backend && pip install -r requirements.txt +cd frontend && npm install ``` -### 前端依赖 +## 导入与运维脚本 ```bat -cd frontend -npm install +python scripts\import_from_11.py # 从 Desktop\11.txt 导入房间 +python scripts\apply_sh2_antirisk.py # 对齐 sh2 防风控并错开检测 +python scripts\sync_cookie_and_probe.py # 同步 Cookie 并抽样探测 ``` -## 导入真实直播间 - -将 `主播URL` 列表(格式同 sh2 / `11.txt`:`https://live.douyin.com/{id},主播: 名称`,可按主机分组)导入: +VPS 相关(需 SSH 可达): ```bat -python scripts\import_from_11.py -``` - -默认读取 `C:\Users\admin\Desktop\11.txt`,写入 SQLite,并按主机打标签(如 `10.88.0.4`、`真实`)。 - -对齐 sh2 防风控参数并重新错开检测: - -```bat -python scripts\apply_sh2_antirisk.py -``` - -同步 Cookie 并抽样探测: - -```bat -python scripts\sync_cookie_and_probe.py +python scripts\deploy_seaweedfs.py # 安装 SeaweedFS + 部署封面能力 +python scripts\setup_caddy_dylive.py # 配置 dylive / feiniu / s3 反代 +python scripts\recapture_covers_now.py # 为当前进行中场次补抓封面 +python scripts\restart_vps.py # 重启 douyinWEB 进程 ``` ## 关键设置(防风控) | 项 | 建议 | |----|------| -| `max_concurrency` | 1(上百房间时不要拉高) | -| `offline_interval_sec` | ≥ 300(与 sh2「循环时间」一致) | +| `max_concurrency` | 1 | +| `offline_interval_sec` | ≥ 300 | | `live_interval_sec` | ≥ 90–120 | | `min_request_gap_sec` | ≥ 3 + jitter | -| `dy_cookie` | 使用 sh2 有效 Cookie;过期后从浏览器更新 ini 再 sync | -| `capture_cover_on_start` | 大批量时可关,减少拉流 | +| `dy_cookie` | 有效抖音 Cookie | +| `capture_cover_on_start` | 开播截图(上传 SeaweedFS) | -## 环境变量(可选) +## 环境变量(`DY_` 前缀) -前缀 `DY_`,见 `backend/app/config.py`(如 `DY_PB_URL`、`DY_OFFLINE_INTERVAL_SEC`)。 +见 `backend/app/config.py`,线上 API 示例: -## 线上地址 +``` +DY_PB_URL=http://127.0.0.1:8092 +DY_S3_ENABLED=true +DY_S3_ENDPOINT=http://127.0.0.1:8333 +DY_S3_PUBLIC_BASE=https://feiniu.nomadro.com +DY_S3_ACCESS_KEY=douyin +DY_S3_SECRET_KEY=… +DY_S3_BUCKET=douyin-covers +``` -| 用途 | URL | -|------|-----| -| 控制台 | https://dylive.nomadro.com/ | -| API | https://dylive.nomadro.com/api/health | -| PocketBase | https://dylive.nomadro.com/_/ | -| SeaweedFS S3 | https://feiniu.nomadro.com/ (别名 https://s3.nomadro.com/) | +## Git -反代由本机 **Caddy** 自动 HTTPS;后端进程:`4015` Next / `8014` API / `8092` PB / `8333` SeaweedFS S3。 +``` +https://gitea.dsx2020.com/eric/douyinWEB.git +``` ## 说明 -- 为方便部署同步,仓库包含 `backend/data/`(SQLite、封面、Cookie 设置)与 `pb/pb_data/`。 -- Windows 的 `pocketbase.exe` 不入库;Ubuntu 部署时下载 Linux 版 PocketBase。 -- 本项目仅做状态监控与场次统计,不含推流录制;推流逻辑仍在 sh2。 - +- 仓库含 `backend/data/` 与 `pb/pb_data/`,便于部署后带真实房间与 Cookie 开箱可用。 +- Windows `pocketbase.exe` 不入库;Ubuntu 使用 Linux 版 `pocketbase`。 +- 仅状态监控与场次统计,不含推流录制(推流仍在 sh2)。 diff --git a/backend/data/monitor.db b/backend/data/monitor.db index ef26d37..c9a0a97 100644 Binary files a/backend/data/monitor.db and b/backend/data/monitor.db differ diff --git a/pb/pb_data/auxiliary.db b/pb/pb_data/auxiliary.db index f7b2242..d0eee14 100644 Binary files a/pb/pb_data/auxiliary.db and b/pb/pb_data/auxiliary.db differ diff --git a/pb/pb_data/data.db b/pb/pb_data/data.db index 22065db..0b53801 100644 Binary files a/pb/pb_data/data.db and b/pb/pb_data/data.db differ diff --git a/scripts/deploy_ports.py b/scripts/deploy_ports.py new file mode 100644 index 0000000..1582759 --- /dev/null +++ b/scripts/deploy_ports.py @@ -0,0 +1,162 @@ +# -*- coding: utf-8 -*- +"""Deploy douyinWEB on free ports: web=4014 api=8014 pb=8092.""" +import paramiko + +HOST = "107.173.30.245" +USER = "root" +PASSWORD = "Xiao4669805" +APP = "/opt/douyinWEB" +WEB_PORT = 4014 +API_PORT = 8014 +PB_PORT = 8092 + + +def run(c, cmd, timeout=300): + print(f"\n$ {cmd[:220]}", flush=True) + _, out, _ = c.exec_command(cmd, timeout=timeout, get_pty=True) + text = out.read().decode("utf-8", errors="replace") + code = out.channel.recv_exit_status() + print(text[-4500:], flush=True) + print(f"[exit {code}]", flush=True) + return code, text + + +def main(): + c = paramiko.SSHClient() + c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + c.connect(HOST, 22, USER, PASSWORD, timeout=30, allow_agent=False, look_for_keys=False) + print("connected", flush=True) + + # stop only our app processes + run( + c, + f""" +set +e +fuser -k {WEB_PORT}/tcp 2>/dev/null || true +fuser -k {API_PORT}/tcp 2>/dev/null || true +fuser -k {PB_PORT}/tcp 2>/dev/null || true +pkill -9 -f '{APP}/pb/pocketbase' || true +pkill -9 -f '{APP}/backend/.venv/bin/python -m uvicorn' || true +pkill -9 -f 'next start --hostname 0.0.0.0 --port {WEB_PORT}' || true +sleep 2 +ss -lntp | grep -E ':{WEB_PORT}|:{API_PORT}|:{PB_PORT}' || echo free +""".strip(), + ) + + # patch next rewrite to API_PORT and rebuild if needed + run( + c, + f""" +set -e +cd {APP} +git remote set-url origin 'https://eric:Xiao4669805@gitea.dsx2020.com/eric/douyinWEB.git' +git fetch origin +git reset --hard origin/main + +# rewrite /api -> local API +cat > frontend/next.config.mjs <<'EOF' +/** @type {{import('next').NextConfig}} */ +const nextConfig = {{ + reactStrictMode: true, + async rewrites() {{ + return [ + {{ + source: "/api/:path*", + destination: "http://127.0.0.1:{API_PORT}/api/:path*", + }}, + {{ + source: "/covers/:path*", + destination: "http://127.0.0.1:{API_PORT}/covers/:path*", + }}, + ]; + }}, +}}; + +export default nextConfig; +EOF + +cd frontend +npm run build +""".strip(), + timeout=900, + ) + + # systemd units + run( + c, + f""" +set -e +cat >/etc/systemd/system/douyinweb-pb.service <<'EOF' +[Unit] +Description=DouyinWEB PocketBase +After=network.target + +[Service] +Type=simple +WorkingDirectory={APP}/pb +ExecStart={APP}/pb/pocketbase serve --http=0.0.0.0:{PB_PORT} +Restart=always +RestartSec=3 + +[Install] +WantedBy=multi-user.target +EOF + +cat >/etc/systemd/system/douyinweb-api.service <<'EOF' +[Unit] +Description=DouyinWEB FastAPI +After=network.target douyinweb-pb.service + +[Service] +Type=simple +WorkingDirectory={APP}/backend +ExecStart={APP}/backend/.venv/bin/python -m uvicorn app.main:app --host 0.0.0.0 --port {API_PORT} +Restart=always +RestartSec=3 +Environment=DY_PB_URL=http://127.0.0.1:{PB_PORT} + +[Install] +WantedBy=multi-user.target +EOF + +cat >/etc/systemd/system/douyinweb-web.service <<'EOF' +[Unit] +Description=DouyinWEB Next.js +After=network.target douyinweb-api.service + +[Service] +Type=simple +WorkingDirectory={APP}/frontend +ExecStart=/usr/bin/npm run start -- --hostname 0.0.0.0 --port {WEB_PORT} +Restart=always +RestartSec=3 + +[Install] +WantedBy=multi-user.target +EOF + +systemctl daemon-reload +systemctl enable douyinweb-pb douyinweb-api douyinweb-web +systemctl restart douyinweb-pb douyinweb-api douyinweb-web +sleep 5 +systemctl --no-pager --full status douyinweb-pb douyinweb-api douyinweb-web | sed -n '1,80p' +ss -lntp | grep -E ':{WEB_PORT}|:{API_PORT}|:{PB_PORT}' +echo HEALTH +curl -s --max-time 10 http://127.0.0.1:{API_PORT}/api/health +echo +echo WEB +curl -s -o /dev/null -w 'web:%{{http_code}}\\n' --max-time 20 http://127.0.0.1:{WEB_PORT}/ +echo ROOMS +curl -s --max-time 10 'http://127.0.0.1:{API_PORT}/api/rooms?limit=1' | head -c 400 +echo +""".strip(), + timeout=180, + ) + c.close() + print(f"\nOpen http://{HOST}:{WEB_PORT}/", flush=True) + print(f"API http://{HOST}:{API_PORT}/docs", flush=True) + print(f"PB http://{HOST}:{PB_PORT}/_/", flush=True) + + +if __name__ == "__main__": + main() diff --git a/scripts/deploy_ubuntu.py b/scripts/deploy_ubuntu.py index 95c3c74..99cf254 100644 --- a/scripts/deploy_ubuntu.py +++ b/scripts/deploy_ubuntu.py @@ -1,50 +1,72 @@ # -*- coding: utf-8 -*- -"""Deploy douyinWEB to Ubuntu via SSH (paramiko).""" +"""Deploy douyinWEB to Ubuntu VPS via SSH (paramiko).""" from __future__ import annotations -import sys -import time - import paramiko HOST = "107.173.30.245" -PORT = 4014 -USER = "eric" -PASSWORD = "4669805" +PORT = 22 +USER = "root" +PASSWORD = "Xiao4669805" GIT_URL = "https://eric:Xiao4669805@gitea.dsx2020.com/eric/douyinWEB.git" -APP_DIR = "/home/eric/douyinWEB" +APP_DIR = "/opt/douyinWEB" -def run(client: paramiko.SSHClient, cmd: str, timeout: int = 600) -> tuple[int, str, str]: - print(f"\n$ {cmd}", flush=True) - stdin, stdout, stderr = client.exec_command(cmd, timeout=timeout, get_pty=True) +def run(client: paramiko.SSHClient, cmd: str, timeout: int = 900) -> tuple[int, str]: + print(f"\n$ {cmd[:200]}{'...' if len(cmd) > 200 else ''}", flush=True) + _, stdout, stderr = client.exec_command(cmd, timeout=timeout, get_pty=True) out = stdout.read().decode("utf-8", errors="replace") err = stderr.read().decode("utf-8", errors="replace") code = stdout.channel.recv_exit_status() - if out.strip(): - print(out[-4000:], flush=True) - if err.strip(): - print("[stderr]", err[-2000:], flush=True) + text = (out or "") + (("\n" + err) if err.strip() else "") + if text.strip(): + print(text[-5000:], flush=True) print(f"[exit {code}]", flush=True) - return code, out, err + return code, text def main() -> int: client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) print(f"connecting {USER}@{HOST}:{PORT} ...", flush=True) - client.connect(HOST, port=PORT, username=USER, password=PASSWORD, timeout=30, allow_agent=False, look_for_keys=False) + client.connect( + HOST, + port=PORT, + username=USER, + password=PASSWORD, + timeout=45, + allow_agent=False, + look_for_keys=False, + banner_timeout=45, + ) print("connected", flush=True) - # basic env - run(client, "uname -a; whoami; pwd; df -h / | tail -1") - run( - client, - "command -v git; command -v python3; command -v node; command -v npm; command -v ffmpeg; command -v curl", - ) + run(client, "uname -a; whoami; df -h / | tail -1") + run(client, "command -v git; command -v python3; command -v node; command -v npm; command -v ffmpeg; command -v curl; command -v unzip") + + code, _ = run( + client, + f""" +set -e +export DEBIAN_FRONTEND=noninteractive +apt-get update -y +apt-get install -y git python3 python3-pip python3-venv curl unzip ca-certificates ffmpeg +# Node 20 if missing/old +if ! command -v node >/dev/null 2>&1 || [ "$(node -v | tr -d v | cut -d. -f1)" -lt 18 ]; then + curl -fsSL https://deb.nodesource.com/setup_20.x | bash - + apt-get install -y nodejs +fi +python3 --version +node --version +npm --version +ffmpeg -version | head -1 +""".strip(), + timeout=1200, + ) + if code != 0: + print("WARN: apt/node install returned non-zero, continuing", flush=True) - # clone or pull (force sync to remote main with data) run( client, f""" @@ -52,7 +74,7 @@ set -e if [ -d {APP_DIR}/.git ]; then cd {APP_DIR} git remote set-url origin '{GIT_URL}' - git fetch origin + git fetch --all git reset --hard origin/main else rm -rf {APP_DIR} @@ -60,47 +82,26 @@ else fi cd {APP_DIR} git log -1 --oneline -ls -la -ls -la backend/data || true +ls -la backend/data | head """.strip(), ) - # install system deps if missing - run( - client, - """ -set -e -export DEBIAN_FRONTEND=noninteractive -sudo -n true 2>/dev/null && SUDO=sudo || SUDO= -$SUDO apt-get update -y -$SUDO apt-get install -y python3-pip python3-venv nodejs npm ffmpeg curl unzip ca-certificates -python3 --version -node --version || true -npm --version || true -ffmpeg -version | head -1 || true -""".strip(), - timeout=900, - ) - - # pocketbase linux binary run( client, f""" set -e cd {APP_DIR}/pb if [ ! -x ./pocketbase ]; then - curl -L -o pb.zip https://github.com/pocketbase/pocketbase/releases/download/v0.25.8/pocketbase_0.25.8_linux_amd64.zip + curl -L --fail -o pb.zip https://github.com/pocketbase/pocketbase/releases/download/v0.25.8/pocketbase_0.25.8_linux_amd64.zip unzip -o pb.zip pocketbase chmod +x pocketbase rm -f pb.zip fi -./pocketbase --version || true -ls -la +./pocketbase --version """.strip(), timeout=300, ) - # python venv + deps run( client, f""" @@ -114,7 +115,6 @@ pip install -r requirements.txt timeout=600, ) - # frontend deps + build (production) run( client, f""" @@ -123,13 +123,12 @@ cd {APP_DIR}/frontend npm install --no-fund --no-audit npm run build """.strip(), - timeout=900, + timeout=1200, ) - # stop old processes run( client, - f""" + """ set +e pkill -f 'pocketbase serve' || true pkill -f 'uvicorn app.main:app' || true @@ -140,41 +139,45 @@ echo stopped """.strip(), ) - # start services with nohup - run( + code, text = run( client, f""" set -e cd {APP_DIR} mkdir -p logs -# PocketBase nohup ./pb/pocketbase serve --http=127.0.0.1:8090 > logs/pb.log 2>&1 & echo $! > logs/pb.pid -# FastAPI cd {APP_DIR}/backend nohup .venv/bin/python -m uvicorn app.main:app --host 0.0.0.0 --port 8000 > ../logs/api.log 2>&1 & echo $! > ../logs/api.pid -# Next.js production cd {APP_DIR}/frontend -nohup npm run start -- --port 3000 --hostname 0.0.0.0 > ../logs/web.log 2>&1 & +nohup npm run start -- --hostname 0.0.0.0 --port 3000 > ../logs/web.log 2>&1 & echo $! > ../logs/web.pid -sleep 5 -ss -lntp | grep -E ':3000|:8000|:8090' || netstat -lntp 2>/dev/null | grep -E ':3000|:8000|:8090' || true -curl -s --max-time 8 http://127.0.0.1:8000/api/health || true +sleep 8 +ss -lntp | grep -E ':3000|:8000|:8090' || true +echo '--- health ---' +curl -s --max-time 10 http://127.0.0.1:8000/api/health || true echo -curl -s -o /dev/null -w 'web:%{{http_code}}\\n' --max-time 15 http://127.0.0.1:3000/ || true -echo DONE +echo '--- web ---' +curl -s -o /dev/null -w 'web:%{{http_code}}\\n' --max-time 20 http://127.0.0.1:3000/ || true +echo '--- api log tail ---' +tail -n 30 {APP_DIR}/logs/api.log || true +echo '--- web log tail ---' +tail -n 30 {APP_DIR}/logs/web.log || true """.strip(), - timeout=120, + timeout=180, ) client.close() - print("\nDeploy finished. Open http://107.173.30.245:3000 (if firewall allows)", flush=True) - return 0 + print("\n=== DEPLOY DONE ===", flush=True) + print("Frontend: http://107.173.30.245:3000/", flush=True) + print("API: http://107.173.30.245:8000/docs", flush=True) + print("PB: http://107.173.30.245:8090/_/ (bound 127.0.0.1 by default)", flush=True) + return 0 if code == 0 else 1 if __name__ == "__main__": diff --git a/scripts/recapture_covers_now.py b/scripts/recapture_covers_now.py new file mode 100644 index 0000000..6e1cde7 --- /dev/null +++ b/scripts/recapture_covers_now.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +import paramiko + +c = paramiko.SSHClient() +c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) +c.connect("107.173.30.245", 22, "root", "Xiao4669805", timeout=30, allow_agent=False, look_for_keys=False) + +cmd = r""" +set +e +systemctl is-active douyinweb-api douyinweb-web seaweed-s3 seaweed-master +ss -lntp | grep -E ':4015|:8014|:8333' + +# ensure capture enabled + s3 env present +grep DY_S3 /etc/systemd/system/douyinweb-api.service || true +cd /opt/douyinWEB/backend +. .venv/bin/activate +python - <<'PY' +from app import db +db.init_db() +db.update_settings_map({"capture_cover_on_start": True}) +print("capture", db.get_settings_map().get("capture_cover_on_start")) +from app.config import settings +print("s3_enabled_cfg", settings.s3_enabled, settings.s3_endpoint, settings.s3_bucket) +PY + +# recapture +echo '--- recapture ---' +curl -s --max-time 400 -X POST http://127.0.0.1:8014/api/sessions/active/recapture-covers +echo +echo '--- active ---' +curl -s --max-time 15 http://127.0.0.1:8014/api/sessions/active | python3 -c " +import sys,json +d=json.load(sys.stdin) +print('total', d.get('total')) +for s in (d.get('sessions') or [])[:8]: + print(s.get('anchor_name'), '|', s.get('cover_url'), '|', (s.get('cover_path') or '')[:70]) +" +SID=$(curl -s http://127.0.0.1:8014/api/sessions/active | python3 -c "import sys,json; d=json.load(sys.stdin); ss=d.get('sessions') or []; print(next((x['id'] for x in ss if x.get('cover_path')), ss[0]['id'] if ss else ''))") +echo SID=$SID +curl -s -o /dev/null -w 'cover:%{http_code} size:%{size_download}\n' --max-time 20 http://127.0.0.1:8014/api/sessions/$SID/cover +curl -s -o /dev/null -w 'web:%{http_code}\n' --max-time 15 http://127.0.0.1:4015/ +curl -s -o /dev/null -w 'cover_via_web:%{http_code}\n' --max-time 20 http://127.0.0.1:4015/api/sessions/$SID/cover +""" +_, o, _ = c.exec_command(cmd, timeout=500, get_pty=True) +print(o.read().decode("utf-8", errors="replace")[-8000:]) +c.close() diff --git a/scripts/restart_vps.py b/scripts/restart_vps.py new file mode 100644 index 0000000..f049bc3 --- /dev/null +++ b/scripts/restart_vps.py @@ -0,0 +1,83 @@ +# -*- coding: utf-8 -*- +"""Force restart douyinWEB services on VPS.""" +import paramiko + +HOST = "107.173.30.245" +PORT = 22 +USER = "root" +PASSWORD = "Xiao4669805" +APP = "/opt/douyinWEB" + + +def run(c, cmd, timeout=180): + print(f"\n$ {cmd[:180]}", flush=True) + _, out, err = c.exec_command(cmd, timeout=timeout, get_pty=True) + text = out.read().decode("utf-8", errors="replace") + code = out.channel.recv_exit_status() + print(text[-4000:], flush=True) + print(f"[exit {code}]", flush=True) + return code, text + + +def main(): + c = paramiko.SSHClient() + c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + c.connect(HOST, port=PORT, username=USER, password=PASSWORD, timeout=30, allow_agent=False, look_for_keys=False) + print("connected", flush=True) + + run( + c, + """ +set +e +# kill by port +for p in 3000 8000 8090; do + fuser -k ${p}/tcp 2>/dev/null || true +done +pkill -9 -f 'pocketbase serve' || true +pkill -9 -f 'uvicorn app.main:app' || true +pkill -9 -f 'uvicorn' || true +pkill -9 -f 'next start' || true +pkill -9 -f 'next-server' || true +sleep 3 +ss -lntp | grep -E ':3000|:8000|:8090' || echo 'ports free' +""".strip(), + ) + + run( + c, + f""" +set -e +cd {APP} +git remote set-url origin 'https://eric:Xiao4669805@gitea.dsx2020.com/eric/douyinWEB.git' +git fetch origin +git reset --hard origin/main +test -f backend/data/monitor.db +test -x pb/pocketbase || (cd pb && curl -L -o pb.zip https://github.com/pocketbase/pocketbase/releases/download/v0.25.8/pocketbase_0.25.8_linux_amd64.zip && unzip -o pb.zip pocketbase && chmod +x pocketbase && rm -f pb.zip) +test -x backend/.venv/bin/python +test -d frontend/.next +mkdir -p logs + +nohup ./pb/pocketbase serve --http=0.0.0.0:8090 > logs/pb.log 2>&1 & echo $! > logs/pb.pid +cd backend +nohup .venv/bin/python -m uvicorn app.main:app --host 0.0.0.0 --port 8000 > ../logs/api.log 2>&1 & echo $! > ../logs/api.pid +cd ../frontend +nohup npm run start -- --hostname 0.0.0.0 --port 3000 > ../logs/web.log 2>&1 & echo $! > ../logs/web.pid +sleep 6 +ss -lntp | grep -E ':3000|:8000|:8090' +echo HEALTH +curl -s --max-time 10 http://127.0.0.1:8000/api/health +echo +echo WEB +curl -s -o /dev/null -w 'web:%{{http_code}}\\n' --max-time 15 http://127.0.0.1:3000/ +echo ROOMS +curl -s --max-time 10 'http://127.0.0.1:8000/api/rooms?limit=1' | head -c 300 +echo +tail -n 15 ../logs/api.log +""".strip(), + ) + c.close() + print("\nOK http://107.173.30.245:3000/", flush=True) + + +if __name__ == "__main__": + main()