diff --git a/app.py b/app.py index 0476020..794a0f6 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,5 @@ import os +import re import uuid import subprocess import threading @@ -469,11 +470,41 @@ def _safe_under(base_dir: str, user_rel_path: str) -> Optional[str]: class ProcessRequest(BaseModel): url: str +# Masks user:password credentials embedded in any URL (e.g. the residential +# proxy URL that yt-dlp echoes in its verbose debug output) before the line is +# ever printed to the server console or stored in the job log. +_CREDENTIAL_URL_RE = re.compile(r'(\w+://)[^:/@\s]+:[^@/\s]+@') + + +def _scrub_secrets(line: str) -> str: + return _CREDENTIAL_URL_RE.sub(r'\1***:***@', line) + + +def _visible_logs(logs): + """Logs to surface to the client. + + Self-host (BILLING off) shows the full pipeline output so people running + their own instance can debug. Cloud shows only the pipeline's own friendly + progress lines (emoji-prefixed, plus the worker's start/finish markers) and + hides the raw yt-dlp / ffmpeg / debug spew from paying users. + """ + if not BILLING_ENABLED: + return logs + visible = [] + for ln in logs: + s = ln.lstrip() + if not s: + continue + if s.startswith(("Job started", "Process finished")) or ord(s[0]) > 0x2000: + visible.append(ln) + return visible + + def enqueue_output(out, job_id): """Reads output from a subprocess and appends it to jobs logs.""" try: for line in iter(out.readline, b''): - decoded_line = line.decode('utf-8').strip() + decoded_line = _scrub_secrets(line.decode('utf-8').strip()) if decoded_line: print(f"📝 [Job Output] {decoded_line}") if job_id in jobs: @@ -713,7 +744,7 @@ async def get_status(job_id: str, request: Request): await _assert_job_owner(request, job) return { "status": job['status'], - "logs": job['logs'], + "logs": _visible_logs(job['logs']), "result": job.get('result') } diff --git a/cloud/alerts.py b/cloud/alerts.py index abb2357..e2bc39c 100644 --- a/cloud/alerts.py +++ b/cloud/alerts.py @@ -15,9 +15,24 @@ _ALERT_COOLDOWN = 3600 # 1 hour between repeats of the same alert _FAIL_WINDOW_MIN = 6 # need at least this many recent jobs to judge a rate _FAIL_THRESHOLD = 5 # ...and this many failures among them -# Substrings that suggest the proxy itself failed (auth / balance / tunnel). -_PROXY_HINTS = ("proxy", "407", "proxyauthentication", "tunnel connection", - "credit", "balance", "insufficient", "payment required") +# Specific signatures of a genuine proxy / download-stage failure. Kept precise +# on purpose: a bare "proxy"/"credit"/"balance" match fired on any job whose logs +# merely echoed the proxy URL (yt-dlp debug) or whose video title contained one of +# those words, producing false "out of credits" alerts for jobs that actually +# failed later in processing. These phrases only appear in real proxy failures. +_PROXY_HINTS = ( + "proxyerror", + "cannot connect to proxy", + "failed to connect to proxy", + "unable to connect to proxy", + "proxy authentication required", + "407 proxy", + "http error 407", + "tunnel connection failed", + "402 payment required", + "out of credits", + "insufficient balance", +) def _looks_like_proxy_error(err: str) -> bool: