diff --git a/backend/app/config.py b/backend/app/config.py index 739eaff..ce468f9 100644 --- a/backend/app/config.py +++ b/backend/app/config.py @@ -18,8 +18,8 @@ class Settings(BaseSettings): ) pb_url: str = "http://127.0.0.1:8090" - pb_admin_email: str = "admin@douyin.local" - pb_admin_password: str = "DouyinMonitor_Admin_2026" + pb_admin_email: str = "hackrobot@douyin.local" + pb_admin_password: str = "Xiao4669805@" data_dir: Path = BACKEND / "data" covers_dir: Path = BACKEND / "data" / "covers" @@ -54,10 +54,13 @@ class Settings(BaseSettings): # Optional: require X-API-Key on POST/PATCH/DELETE (leave empty to disable) api_key: str = "" + # Public HTTPS origin, e.g. https://ypp.smallzhiyuns.com + public_base_url: str = "" + # Site login (whole-site protection) auth_enabled: bool = True admin_username: str = "hackrobot" - admin_password: str = "Xiao4669805@!" + admin_password: str = "Xiao4669805@" session_secret: str = "douyinweb-session-2026" class Config: diff --git a/backend/app/db.py b/backend/app/db.py index fdf1873..c189d0b 100644 --- a/backend/app/db.py +++ b/backend/app/db.py @@ -439,7 +439,11 @@ def _seed_defaults(conn: sqlite3.Connection) -> None: "shorts_oauth_check_interval_sec": 3600, "shorts_oauth_client_id": "", "shorts_oauth_client_secret": "", - "shorts_oauth_redirect_uri": "https://dylive.nomadro.com/api/shorts/oauth/callback", + "shorts_oauth_redirect_uri": ( + f"{str(settings.public_base_url or '').strip().rstrip('/')}/api/shorts/oauth/callback" + if str(settings.public_base_url or "").strip() + else "https://dylive.nomadro.com/api/shorts/oauth/callback" + ), # Named OAuth apps A/B/C/D — configure once in settings; channels pick a profile "shorts_oauth_profiles": [ {"id": "A", "label": "谷歌账号 A", "client_id": "", "client_secret": ""}, diff --git a/backend/app/shorts_oauth.py b/backend/app/shorts_oauth.py index 2a3051e..c9dbc77 100644 --- a/backend/app/shorts_oauth.py +++ b/backend/app/shorts_oauth.py @@ -35,8 +35,8 @@ DEFAULT_OAUTH_PROFILES: list[dict[str, str]] = [ {"id": "D", "label": "谷歌账号 D", "client_id": "", "client_secret": ""}, ] -# state -> (channel_pk, expires_at, client_id, client_secret) -_oauth_states: dict[str, tuple[str, float, str, str]] = {} +# state -> (channel_pk, expires_at, client_id, client_secret, return_to?) +_oauth_states: dict[str, tuple[str, float, str, str] | tuple[str, float, str, str, str]] = {} def _cfg() -> dict[str, Any]: @@ -171,6 +171,11 @@ def resolve_redirect_uri(request_base: str = "") -> str: base = (request_base or "").rstrip("/") if base: return f"{base}/api/shorts/oauth/callback" + from .config import settings as app_settings + + pub = str(getattr(app_settings, "public_base_url", "") or "").strip().rstrip("/") + if pub: + return f"{pub}/api/shorts/oauth/callback" return "https://dylive.nomadro.com/api/shorts/oauth/callback" @@ -215,7 +220,7 @@ def channel_can_authorize(channel: dict[str, Any] | None, cfg: dict[str, Any] | return resolve_oauth_client(channel, cfg).get("source") != "none" -def build_authorize_url(*, channel_pk: str, request_base: str = "") -> str: +def build_authorize_url(*, channel_pk: str, request_base: str = "", return_to: str = "") -> str: cfg = _cfg() channel = db.get_shorts_channel(channel_pk) if not channel: @@ -227,12 +232,18 @@ def build_authorize_url(*, channel_pk: str, request_base: str = "") -> str: raise ValueError( "请先在「设置 → OAuth 配置档」填写对应 A/B/C/D 的 Client,并在频道选择该配置档" ) + ret = str(return_to or "").strip() + if ret and (not ret.startswith("/") or ret.startswith("//")): + ret = "" now = time.time() for k, packed in list(_oauth_states.items()): if len(packed) < 2 or packed[1] < now: _oauth_states.pop(k, None) state = secrets.token_urlsafe(24) - _oauth_states[state] = (channel_pk, now + 600, client_id, client_secret) + if ret: + _oauth_states[state] = (channel_pk, now + 600, client_id, client_secret, ret) + else: + _oauth_states[state] = (channel_pk, now + 600, client_id, client_secret) params = { "client_id": client_id, "redirect_uri": resolve_redirect_uri(request_base), @@ -318,6 +329,9 @@ async def exchange_code(*, code: str, state: str, request_base: str = "") -> dic raise ValueError("授权 state 无效或已过期,请重新点击授权") channel_pk = packed[0] exp = packed[1] + return_to = str(packed[4]).strip() if len(packed) >= 5 else "" + if return_to and (not return_to.startswith("/") or return_to.startswith("//")): + return_to = "" if exp < time.time(): raise ValueError("授权已过期,请重新点击授权") cfg = _cfg() @@ -443,6 +457,7 @@ async def exchange_code(*, code: str, state: str, request_base: str = "") -> dic "oauth_scopes_ok": True, "oauth_client_source": source, "oauth_profile_id": source_meta.get("profile_id") or "", + "return_to": return_to, } diff --git a/scripts/bootstrap_vps.sh b/scripts/bootstrap_vps.sh new file mode 100644 index 0000000..eceecdd --- /dev/null +++ b/scripts/bootstrap_vps.sh @@ -0,0 +1,529 @@ +#!/usr/bin/env bash +# douyinWEB one-click bootstrap for a shared Ubuntu VPS. +# +# Safety: +# - Does NOT modify existing Nginx/BaoTa vhosts, Docker, Gitea, or other systemd units +# - Does NOT apt-upgrade, change timezone, install Caddy, or open extra public ports +# - Binds app ports to 127.0.0.1 only; public entry is one new server_name +# - Aborts if chosen ports are already in use +# - Nginx: test config first; reload only if test passes; rolls back new vhost on failure +# +# Usage (on the target VPS as root): +# DOMAIN=ypp.example.com \ +# ADMIN_USER=dsx2016 ADMIN_PASS='***' \ +# GIT_URL='https://user:pass@git.example/eric/douyinWEB.git' \ +# GIT_BRANCH=test1 FRESH=1 \ +# bash scripts/bootstrap_vps.sh +# +# Optional: +# APP_DIR=/opt/douyinWEB +# PUBLIC_SCHEME=https +# WEB_PORT=4015 API_PORT=8014 PB_PORT=8092 YT_PORT=8020 S3_PORT=8333 +# SW_MASTER_PORT=19333 SW_VOLUME_PORT=18080 SW_FILER_PORT=18888 +set -euo pipefail + +DOMAIN="${DOMAIN:?set DOMAIN, e.g. ypp.smallzhiyuns.com}" +ADMIN_USER="${ADMIN_USER:?set ADMIN_USER}" +ADMIN_PASS="${ADMIN_PASS:?set ADMIN_PASS}" +GIT_URL="${GIT_URL:?set GIT_URL}" +GIT_BRANCH="${GIT_BRANCH:-test1}" +APP_DIR="${APP_DIR:-/opt/douyinWEB}" +FRESH="${FRESH:-0}" +PUBLIC_SCHEME="${PUBLIC_SCHEME:-https}" +PUBLIC_BASE="${PUBLIC_BASE:-${PUBLIC_SCHEME}://${DOMAIN}}" + +WEB_PORT="${WEB_PORT:-4015}" +API_PORT="${API_PORT:-8014}" +PB_PORT="${PB_PORT:-8092}" +YT_PORT="${YT_PORT:-8020}" +S3_PORT="${S3_PORT:-8333}" +SW_MASTER_PORT="${SW_MASTER_PORT:-19333}" +SW_VOLUME_PORT="${SW_VOLUME_PORT:-18080}" +SW_FILER_PORT="${SW_FILER_PORT:-18888}" + +PB_ADMIN_EMAIL="${PB_ADMIN_EMAIL:-${ADMIN_USER}@${DOMAIN}}" +S3_ACCESS="${S3_ACCESS:-douyin}" +S3_BUCKET="${S3_BUCKET:-douyin-covers}" +WEED_VER="${WEED_VER:-3.80}" +PB_VER="${PB_VER:-0.25.8}" +ENV_FILE="/etc/douyinweb.env" +SW_DATA="/data/douyinweb-seaweed" +BAOTA_NGINX="/www/server/nginx/sbin/nginx" +BAOTA_VHOST_DIR="/www/server/panel/vhost/nginx" + +log() { printf '\n[%s] %s\n' "$(date '+%F %T')" "$*"; } +die() { printf 'ERROR: %s\n' "$*" >&2; exit 1; } + +[[ "$(id -u)" -eq 0 ]] || die "run as root" + +port_in_use() { + local p="$1" + ss -lnt | awk '{print $4}' | grep -E "[:.]${p}$" >/dev/null 2>&1 +} + +assert_ports_free() { + local p + if systemctl is-active --quiet douyinweb-api 2>/dev/null; then + log "douyinweb already running — skip port occupancy check" + return + fi + for p in "$WEB_PORT" "$API_PORT" "$PB_PORT" "$YT_PORT" "$S3_PORT" "$SW_MASTER_PORT" "$SW_VOLUME_PORT" "$SW_FILER_PORT"; do + if port_in_use "$p"; then + die "port ${p} is already in use — pick another via env, do not kill existing services" + fi + done +} + +detect_nginx() { + if [[ -x "$BAOTA_NGINX" ]]; then + NGINX_BIN="$BAOTA_NGINX" + NGINX_MODE="baota" + return + fi + if command -v nginx >/dev/null 2>&1; then + NGINX_BIN="$(command -v nginx)" + NGINX_MODE="system" + return + fi + die "nginx not found; this script adds a vhost to existing nginx and will not install Caddy" +} + +persist_secrets() { + if [[ -f "$ENV_FILE" ]]; then + # shellcheck disable=SC1090 + source "$ENV_FILE" + log "loaded existing $ENV_FILE (secrets reused)" + return + fi + SESSION_SECRET="$(openssl rand -hex 24)" + S3_SECRET="$(openssl rand -hex 18)" + cat >"$ENV_FILE" </etc/seaweedfs/douyinweb-s3.json </etc/systemd/system/douyinweb-pb.service </etc/systemd/system/douyinweb-yt.service </etc/systemd/system/douyinweb-api.service </etc/systemd/system/douyinweb-web.service </etc/systemd/system/douyinweb-sw-master.service </etc/systemd/system/douyinweb-sw-volume.service </etc/systemd/system/douyinweb-sw-filer.service </etc/systemd/system/douyinweb-s3.service < "/www/server/panel/vhost/nginx/well-known/${DOMAIN}.conf" + : > "/www/server/panel/vhost/rewrite/${DOMAIN}.conf" + echo "# douyinweb extension placeholder" > "/www/server/panel/vhost/nginx/extension/${DOMAIN}/placeholder.conf" + + cat >"/www/server/panel/vhost/nginx/proxy/${DOMAIN}/douyinweb.conf" <"$vhost" <