File size: 4,306 Bytes
0241ae6
384fd6e
 
657d28c
 
 
384fd6e
657d28c
bac5d96
657d28c
 
bac5d96
 
657d28c
 
bac5d96
 
 
657d28c
bac5d96
657d28c
 
 
 
a9e9bc8
657d28c
 
 
 
 
 
 
 
 
 
 
 
 
 
bac5d96
657d28c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bac5d96
657d28c
 
bac5d96
657d28c
bac5d96
657d28c
a193cc0
e447925
657d28c
 
384fd6e
844cefb
e447925
844cefb
a9e9bc8
 
e447925
 
a9e9bc8
 
00e576e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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())