Spaces:
Runtime error
Runtime error
Update proxy.py
Browse files2 unmodified lines34567814 unmodified lines232425262728294 unmodified lines343536373839401 unmodified line42434445464748495051525354555657585960616263646566676869707172737475761 unmodified line787980818283848586878889909192939495969798991002 unmodified lines1031041051061071081091101111121131141152 unmodified lines1181191201211221231241252 unmodified linesimport asyncioimport jsonfrom playwright.async_api import async_playwright# 全局变量browser = None14 unmodified lines"--disable-gpu","--disable-blink-features=AutomationControlled","--disable-features=IsolateOrigins,site-per-process","--remote-allow-origins=*",])4 unmodified linespage = await context.new_page()# 强力 Stealth 补丁await page.add_init_script("""Object.defineProperty(navigator, 'webdriver', {get: () => undefined});Object.defineProperty(navigator, 'plugins', {get: () => [1,2,3,4,5]});1 unmodified lineObject.defineProperty(navigator, 'hardwareConcurrency', {get: () => 8});Object.defineProperty(navigator, 'deviceMemory', {get: () => 8});Object.defineProperty(navigator, 'platform', {get: () => 'Win32'});window.chrome = {runtime: {},loadTimes: () => ({}),csi: () => ({}),app: {isInstalled: false}};// 模拟真实用户行为Object.defineProperty(document, 'hidden', {get: () => false});Object.defineProperty(document, 'visibilityState', {get: () => 'visible'});""")print("[+] Playwright 浏览器启动成功 + Stealth 已注入")return Trueasync def handle_client(reader, writer):try:data = await reader.read(2048)request = data.decode('utf-8', errors='ignore').strip()# 首页if request.startswith("GET / ") or request.startswith("GET /?"):html = """<h1>Google 浏览器代理已就绪</h1><p>可用路径:</p><ul><li><a href="/open_google">/open_google - 打开 Google 登录页</a></li><li><a href="/screenshot">/screenshot - 查看当前页面截图</a></li></ul>"""response = f"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n{html}"1 unmodified lineawait writer.drain()return# 打开 Google 登录页if "GET /open_google" in request:await page.goto("https://accounts.google.com", wait_until="domcontentloaded")title = await page.title()html = f"""<h1>Google 登录页已打开</h1><p>标题: {title}</p><p>URL: {page.url}</p><p><a href="/screenshot">点击查看截图</a></p>"""response = f"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n{html}"writer.write(response.encode())await writer.drain()return# 返回截图if "GET /screenshot" in request:await page.screenshot(path="/tmp/screenshot.png")with open("/tmp/screenshot.png", "rb") as f:img = f.read()2 unmodified linesawait writer.drain()return# 默认返回response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>Google 代理运行正常</h1>"writer.write(response.encode())await writer.drain()except Exception as e:print(f"[-] 请求处理错误: {e}")finally:try:writer.close()2 unmodified linesasync def main():await start_browser()print("[*] HTTP 服务已启动 (端口 7860)...")print("[*] 测试地址: https://你的space.hf.space/open_google")server = await asyncio.start_server(handle_client, '0.0.0.0', 7860)async with server:2 unmodified lines345678914 unmodified lines2425262728294 unmodified lines343536373839401 unmodified line424344454647484950515253545556575859606162636465666768691 unmodified line71727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341352 unmodified lines1381391401411421431441451461471481492 unmodified lines1521531541551561571581592 unmodified linesimport asyncioimport jsonfrom playwright.async_api import async_playwrightimport urllib.parse# 全局变量browser = None14 unmodified lines"--disable-gpu","--disable-blink-features=AutomationControlled","--disable-features=IsolateOrigins,site-per-process",])4 unmodified linespage = await context.new_page()# 强力 Stealthawait page.add_init_script("""Object.defineProperty(navigator, 'webdriver', {get: () => undefined});Object.defineProperty(navigator, 'plugins', {get: () => [1,2,3,4,5]});1 unmodified lineObject.defineProperty(navigator, 'hardwareConcurrency', {get: () => 8});Object.defineProperty(navigator, 'deviceMemory', {get: () => 8});Object.defineProperty(navigator, 'platform', {get: () => 'Win32'});window.chrome = { runtime: {}, loadTimes: () => ({}), csi: () => ({}), app: {} };""")print("[+] 浏览器 + Stealth 初始化完成")return Trueasync def handle_client(reader, writer):try:data = await reader.read(4096)request = data.decode('utf-8', errors='ignore')path = request.split(' ')[1] if ' ' in request else '/'# 首页if path == '/' or path.startswith('/?'):html = """<h1>✅ Google 浏览器代理运行正常</h1><p>可用功能:</p><ul><li><a href="/open_google">打开 Google 登录页</a></li><li><a href="/login?email=你的邮箱@gmail.com">自动输入邮箱</a></li><li><a href="/screenshot">查看截图</a></li><li><a href="/save_cookies">保存 Cookies(登录后使用)</a></li></ul>"""response = f"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n{html}"1 unmodified lineawait writer.drain()return# 打开登录页if "/open_google" in path:await page.goto("https://accounts.google.com", wait_until="domcontentloaded")html = f"<h1>Google 登录页已打开</h1><p><a href='/screenshot'>查看截图</a></p>"writer.write(f"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n{html}".encode())await writer.drain()return# 自动输入邮箱if "/login" in path:try:email = urllib.parse.parse_qs(path.split('?')[-1]).get('email', [''])[0]if email:await page.goto("https://accounts.google.com", wait_until="domcontentloaded")await page.fill('input[type="email"]', email)await page.click('button[type="button"]') # 点击 Nextawait asyncio.sleep(3)html = f"<h1>邮箱已输入: {email}</h1><p>请继续手动输入密码,或使用 /screenshot 查看</p>"else:html = "<h1>请提供邮箱: /login?email=xxx@gmail.com</h1>"except:html = "<h1>邮箱输入失败</h1>"writer.write(f"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n{html}".encode())await writer.drain()return# 保存 Cookiesif "/save_cookies" in path:try:cookies = await page.context.cookies()with open("/tmp/cookies.json", "w", encoding="utf-8") as f:json.dump(cookies, f, ensure_ascii=False, indent=2)html = f"""<h1>Cookies 已保存!</h1><p>共 {len(cookies)} 个 Cookies</p><a href="/download_cookies" download>点击下载 cookies.json</a>"""except Exception as e:html = f"<h1>保存失败: {e}</h1>"writer.write(f"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n{html}".encode())await writer.drain()return# 下载 Cookiesif "/download_cookies" in path:try:with open("/tmp/cookies.json", "rb") as f:content = f.read()response = f"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Disposition: attachment; filename=cookies.json\r\nContent-Length: {len(content)}\r\n\r\n"writer.write(response.encode() + content)await writer.drain()except:writer.write(b"HTTP/1.1 404 Not Found\r\n\r\nCookies not found")await writer.drain()return# 截图if "/screenshot" in path:await page.screenshot(path="/tmp/screenshot.png")with open("/tmp/screenshot.png", "rb") as f:img = f.read()2 unmodified linesawait writer.drain()return# 默认writer.write(b"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>Google 代理运行中</h1>")await writer.drain()except Exception as e:print(f"[-] 处理错误: {e}")finally:try:writer.close()2 unmodified linesasync def main():await start_browser()print("[*] HTTP 服务启动成功 (端口 7860)")print("[*] 测试地址: https://sdfgrtg-ggfpp.hf.space/open_google")server = await asyncio.start_server(handle_client, '0.0.0.0', 7860)async with server:
proxy.py+75-41