Download proxy.py from sdfgrtg/pfg: direct link, hf CLI and curl.
- Browser
- Download file 4.31 kB
-
https://huggingface.co/spaces/sdfgrtg/pfg/resolve/main/proxy.py
- Command line
-
hf download hf://spaces/sdfgrtg/pfg/proxy.py
-
curl -L -o proxy.py https://huggingface.co/spaces/sdfgrtg/pfg/resolve/main/proxy.py
4.31 kB
| import os | |
| import time | |
| import subprocess | |
| import asyncio | |
| import urllib.request | |
| import json | |
| async def pipe(reader, writer): | |
| try: | |
| while not reader.at_eof(): | |
| data = await reader.read(4096) | |
| if not data: | |
| break | |
| writer.write(data) | |
| await writer.drain() | |
| except Exception: | |
| pass | |
| finally: | |
| writer.close() | |
| REAL_WS_PATH = "/devtools/browser" | |
| def fetch_real_ws_path(): | |
| global REAL_WS_PATH | |
| print("[*] 正在向无头内核索要动态远控安全密钥 (UUID)...") | |
| for _ in range(15): | |
| try: | |
| with urllib.request.urlopen("http://127.0.0.1:9222/json/version", timeout=2) as response: | |
| data = json.loads(response.read().decode()) | |
| ws_url = data.get("webSocketDebuggerUrl", "") | |
| if ws_url: | |
| REAL_WS_PATH = ws_url.split("9222")[-1] | |
| print(f"[+] 拦截成功!当前有效的云端绝对通道为: {REAL_WS_PATH}") | |
| return | |
| except Exception: | |
| time.sleep(1) | |
| async def handle_client(local_reader, local_writer): | |
| global REAL_WS_PATH | |
| try: | |
| remote_reader, remote_writer = await asyncio.open_connection('127.0.0.1', 9222) | |
| handshake_data = await local_reader.read(8192) | |
| if handshake_data: | |
| lines = handshake_data.split(b'\r\n') | |
| new_lines = [] | |
| for line in lines: | |
| if line.startswith(b'GET '): | |
| parts = line.split(b' ') | |
| if len(parts) >= 2: | |
| parts[1] = REAL_WS_PATH.encode() | |
| line = b' '.join(parts) | |
| if line.lower().startswith(b'host:'): | |
| line = b'Host: 127.0.0.1:9222' | |
| new_lines.append(line) | |
| handshake_data = b'\r\n'.join(new_lines) | |
| remote_writer.write(handshake_data) | |
| await remote_writer.drain() | |
| asyncio.create_task(pipe(local_reader, remote_writer)) | |
| asyncio.create_task(pipe(remote_reader, local_writer)) | |
| except Exception: | |
| local_writer.close() | |
| async def start_proxy(): | |
| profile_path = "/tmp/playwright_chrome_profile" | |
| os.makedirs(profile_path, exist_ok=True) | |
| MY_LOCAL_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" | |
| chrome_executable = None | |
| base_dir = "/app/custom_chrome" | |
| # 💡 优先搜寻标准的无头专用外壳 shell | |
| print("[*] 正在检索云端免显卡服务器专用 chrome-headless-shell...") | |
| if os.path.exists(base_dir): | |
| for root, dirs, files in os.walk(base_dir): | |
| if "chrome-headless-shell" in files: | |
| chrome_executable = os.path.join(root, "chrome-headless-shell") | |
| break | |
| # 兜底:如果没找到 headless-shell,再退回普通的 chrome | |
| if not chrome_executable: | |
| for root, dirs, files in os.walk(base_dir): | |
| if "chrome" in files and "crashpad" not in root: | |
| chrome_executable = os.path.join(root, "chrome") | |
| break | |
| print(f"[*] 唤醒全合规安全无头内核: {chrome_executable}") | |
| # 🛡️ 针对无头 Shell 的深度参数清洗,砍掉所有会引发 Crash 的复杂图形渲染 | |
| cmd = [ | |
| chrome_executable, | |
| f"--user-data-dir={profile_path}", | |
| "--remote-debugging-port=9222", | |
| "--remote-debugging-address=127.0.0.1", | |
| "--no-sandbox", | |
| "--disable-setuid-sandbox", | |
| "--disable-dev-shm-usage", | |
| "--disable-gpu", | |
| "--disable-software-rasterizer", # 彻底禁止任何软件级像素渲染,免疫崩溃 | |
| "--mute-audio", | |
| f"--user-agent={MY_LOCAL_UA}", | |
| "--disable-blink-features=AutomationControlled", | |
| "--remote-allow-origins=*" | |
| ] | |
| subprocess.Popen(cmd, env=os.environ) | |
| await asyncio.sleep(6) | |
| fetch_real_ws_path() | |
| print("[*] 启动智能反劫持重写网桥,死守 7860 端口...") | |
| server = await asyncio.start_server(handle_client, '0.0.0.0', 7860) | |
| async with server: | |
| await server.serve_forever() | |
| if __name__ == "__main__": | |
| asyncio.run(start_proxy()) |