Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP. Co-authored-by: Cursor <cursoragent@cursor.com>
145 lines
5.1 KiB
Python
145 lines
5.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Shorts upload SOCKS5 proxy profiles (optional, default off)."""
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
from typing import Any
|
|
from urllib.parse import quote
|
|
|
|
DEFAULT_SOCKS_PROFILE_ID = "residential-1"
|
|
|
|
DEFAULT_SOCKS_PROFILES: list[dict[str, Any]] = []
|
|
# Fresh installs: no baked-in residential credentials.
|
|
|
|
|
|
def _norm_profile(raw: Any) -> dict[str, Any] | None:
|
|
if not isinstance(raw, dict):
|
|
return None
|
|
pid = str(raw.get("id") or "").strip() or str(uuid.uuid4())
|
|
server = str(raw.get("server") or "").strip()
|
|
try:
|
|
port = int(raw.get("port") or 0)
|
|
except Exception:
|
|
port = 0
|
|
ptype = str(raw.get("type") or "socks5").strip().lower() or "socks5"
|
|
if ptype not in {"socks5", "socks5h"}:
|
|
ptype = "socks5"
|
|
return {
|
|
"id": pid,
|
|
"label": str(raw.get("label") or server or pid).strip() or pid,
|
|
"type": ptype,
|
|
"server": server,
|
|
"port": port,
|
|
"username": str(raw.get("username") or ""),
|
|
"password": str(raw.get("password") or ""),
|
|
}
|
|
|
|
|
|
def list_socks_profiles(cfg: dict[str, Any] | None = None, *, public: bool = False) -> list[dict[str, Any]]:
|
|
from . import db
|
|
|
|
settings = dict(cfg if cfg is not None else db.get_settings_map())
|
|
raw = settings.get("shorts_upload_socks_profiles")
|
|
rows: list[dict[str, Any]] = []
|
|
if isinstance(raw, list):
|
|
for item in raw:
|
|
p = _norm_profile(item)
|
|
if p and p["server"] and p["port"] > 0:
|
|
rows.append(p)
|
|
if not rows:
|
|
rows = [dict(x) for x in DEFAULT_SOCKS_PROFILES]
|
|
if not public:
|
|
return rows
|
|
out = []
|
|
for p in rows:
|
|
pwd = str(p.get("password") or "")
|
|
q = dict(p)
|
|
q["has_password"] = bool(pwd.strip())
|
|
q["password"] = ("***" + pwd[-4:]) if len(pwd) > 4 else ("***" if pwd else "")
|
|
out.append(q)
|
|
return out
|
|
|
|
|
|
def get_socks_profile(profile_id: str, cfg: dict[str, Any] | None = None) -> dict[str, Any] | None:
|
|
pid = str(profile_id or "").strip()
|
|
for p in list_socks_profiles(cfg, public=False):
|
|
if str(p.get("id") or "") == pid:
|
|
return p
|
|
return None
|
|
|
|
|
|
def socks_proxy_url(profile: dict[str, Any] | None) -> str:
|
|
"""Build httpx socks URL. Uses socks5h so DNS goes through the proxy."""
|
|
if not profile:
|
|
return ""
|
|
server = str(profile.get("server") or "").strip()
|
|
try:
|
|
port = int(profile.get("port") or 0)
|
|
except Exception:
|
|
port = 0
|
|
if not server or port <= 0:
|
|
return ""
|
|
user = str(profile.get("username") or "")
|
|
pwd = str(profile.get("password") or "")
|
|
# Prefer remote DNS via proxy (socks5h); allow explicit type override
|
|
raw_type = str(profile.get("type") or "socks5").strip().lower()
|
|
scheme = "socks5h" if raw_type in {"socks5", "socks5h"} else "socks5h"
|
|
if user or pwd:
|
|
return f"{scheme}://{quote(user, safe='')}:{quote(pwd, safe='')}@{server}:{port}"
|
|
return f"{scheme}://{server}:{port}"
|
|
|
|
|
|
def resolve_upload_socks_proxy(cfg: dict[str, Any] | None = None) -> str:
|
|
"""Return proxy URL when business egress uploads cover is on; else empty (direct)."""
|
|
try:
|
|
from . import egress_hub
|
|
|
|
url = egress_hub.resolve_upload_proxy(cfg)
|
|
if url:
|
|
return url
|
|
# Fall through if egress off — keep legacy shorts toggle for older DBs.
|
|
except Exception:
|
|
pass
|
|
from . import db
|
|
|
|
settings = dict(cfg if cfg is not None else db.get_settings_map())
|
|
if not settings.get("shorts_upload_socks_enabled") and not settings.get("global_socks_enabled"):
|
|
return ""
|
|
try:
|
|
from . import global_socks
|
|
|
|
return global_socks.resolve_app_proxy(settings) or ""
|
|
except Exception:
|
|
pass
|
|
pid = str(settings.get("shorts_upload_socks_profile_id") or "").strip() or DEFAULT_SOCKS_PROFILE_ID
|
|
prof = get_socks_profile(pid, settings)
|
|
if not prof:
|
|
rows = list_socks_profiles(settings, public=False)
|
|
prof = rows[0] if rows else None
|
|
return socks_proxy_url(prof)
|
|
|
|
|
|
def merge_socks_profiles_patch(incoming: Any, existing_cfg: dict[str, Any] | None = None) -> list[dict[str, Any]]:
|
|
"""Merge UI patch; keep existing password when incoming is blank or ***masked."""
|
|
existing = {str(p["id"]): p for p in list_socks_profiles(existing_cfg, public=False)}
|
|
out: list[dict[str, Any]] = []
|
|
seen: set[str] = set()
|
|
src = incoming if isinstance(incoming, list) else []
|
|
for item in src:
|
|
p = _norm_profile(item)
|
|
if not p:
|
|
continue
|
|
pid = str(p["id"])
|
|
old = existing.get(pid) or {}
|
|
pwd = str(p.get("password") or "")
|
|
if (not pwd.strip()) or pwd.startswith("***"):
|
|
p["password"] = str(old.get("password") or "")
|
|
if not p["server"] or int(p.get("port") or 0) <= 0:
|
|
continue
|
|
out.append(p)
|
|
seen.add(pid)
|
|
# Keep any existing profiles not present in patch (UI may send full list; if empty patch keep old)
|
|
if not src:
|
|
return list(existing.values()) or [dict(x) for x in DEFAULT_SOCKS_PROFILES]
|
|
return out or [dict(x) for x in DEFAULT_SOCKS_PROFILES]
|