sdfgrtg commited on
Commit
4ef5fae
·
verified ·
1 Parent(s): 8d8ddb7

Update proxy.py

Browse files
Files changed (1) hide show
  1. proxy.py +23 -21
proxy.py CHANGED
@@ -2,8 +2,8 @@ import os
2
  import time
3
  import asyncio
4
  import json
5
- from playwright.async_api import async_playwright
6
  import urllib.parse
 
7
 
8
  # 全局变量
9
  browser = None
@@ -34,7 +34,7 @@ async def start_browser():
34
 
35
  page = await context.new_page()
36
 
37
- # 强力 Stealth
38
  await page.add_init_script("""
39
  Object.defineProperty(navigator, 'webdriver', {get: () => undefined});
40
  Object.defineProperty(navigator, 'plugins', {get: () => [1,2,3,4,5]});
@@ -67,33 +67,37 @@ async def handle_client(reader, writer):
67
  </ul>
68
  """
69
  response = f"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n{html}"
70
- writer.write(response.encode())
71
  await writer.drain()
72
  return
73
 
74
  # 打开登录页
75
  if "/open_google" in path:
76
  await page.goto("https://accounts.google.com", wait_until="domcontentloaded")
77
- html = f"<h1>Google 登录页已打开</h1><p><a href='/screenshot'>查看截图</a></p>"
78
- writer.write(f"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n{html}".encode())
 
79
  await writer.drain()
80
  return
81
 
82
  # 自动输入邮箱
83
  if "/login" in path:
84
  try:
85
- email = urllib.parse.parse_qs(path.split('?')[-1]).get('email', [''])[0]
 
 
86
  if email:
87
  await page.goto("https://accounts.google.com", wait_until="domcontentloaded")
88
  await page.fill('input[type="email"]', email)
89
- await page.click('button[type="button"]') # 点击 Next
90
  await asyncio.sleep(3)
91
- html = f"<h1>邮箱已输入: {email}</h1><p>请继续手动输入密码,或使用 /screenshot 查看</p>"
92
  else:
93
- html = "<h1>请提供邮箱: /login?email=xxx@gmail.com</h1>"
94
  except:
95
  html = "<h1>邮箱输入失败</h1>"
96
- writer.write(f"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n{html}".encode())
 
97
  await writer.drain()
98
  return
99
 
@@ -103,15 +107,11 @@ async def handle_client(reader, writer):
103
  cookies = await page.context.cookies()
104
  with open("/tmp/cookies.json", "w", encoding="utf-8") as f:
105
  json.dump(cookies, f, ensure_ascii=False, indent=2)
106
-
107
- html = f"""
108
- <h1>Cookies 已保存!</h1>
109
- <p>共 {len(cookies)} 个 Cookies</p>
110
- <a href="/download_cookies" download>点击下载 cookies.json</a>
111
- """
112
  except Exception as e:
113
  html = f"<h1>保存失败: {e}</h1>"
114
- writer.write(f"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n{html}".encode())
 
115
  await writer.drain()
116
  return
117
 
@@ -121,7 +121,7 @@ async def handle_client(reader, writer):
121
  with open("/tmp/cookies.json", "rb") as f:
122
  content = f.read()
123
  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"
124
- writer.write(response.encode() + content)
125
  await writer.drain()
126
  except:
127
  writer.write(b"HTTP/1.1 404 Not Found\r\n\r\nCookies not found")
@@ -134,12 +134,14 @@ async def handle_client(reader, writer):
134
  with open("/tmp/screenshot.png", "rb") as f:
135
  img = f.read()
136
  response = f"HTTP/1.1 200 OK\r\nContent-Type: image/png\r\nContent-Length: {len(img)}\r\n\r\n"
137
- writer.write(response.encode() + img)
138
  await writer.drain()
139
  return
140
 
141
- # 默认
142
- writer.write(b"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>Google 代理运行中</h1>")
 
 
143
  await writer.drain()
144
 
145
  except Exception as e:
 
2
  import time
3
  import asyncio
4
  import json
 
5
  import urllib.parse
6
+ from playwright.async_api import async_playwright
7
 
8
  # 全局变量
9
  browser = None
 
34
 
35
  page = await context.new_page()
36
 
37
+ # 强力 Stealth 补丁
38
  await page.add_init_script("""
39
  Object.defineProperty(navigator, 'webdriver', {get: () => undefined});
40
  Object.defineProperty(navigator, 'plugins', {get: () => [1,2,3,4,5]});
 
67
  </ul>
68
  """
69
  response = f"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n{html}"
70
+ writer.write(response.encode('utf-8'))
71
  await writer.drain()
72
  return
73
 
74
  # 打开登录页
75
  if "/open_google" in path:
76
  await page.goto("https://accounts.google.com", wait_until="domcontentloaded")
77
+ html = "<h1>Google 登录页已打开</h1><p><a href='/screenshot'>查看截图</a></p>"
78
+ response = f"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n{html}"
79
+ writer.write(response.encode('utf-8'))
80
  await writer.drain()
81
  return
82
 
83
  # 自动输入邮箱
84
  if "/login" in path:
85
  try:
86
+ query = urllib.parse.urlparse(path).query
87
+ params = urllib.parse.parse_qs(query)
88
+ email = params.get('email', [''])[0]
89
  if email:
90
  await page.goto("https://accounts.google.com", wait_until="domcontentloaded")
91
  await page.fill('input[type="email"]', email)
92
+ await page.click('button[type="button"]') # Next
93
  await asyncio.sleep(3)
94
+ html = f"<h1>✅ 邮箱已输入: {email}</h1><p>请继续输入密码</p>"
95
  else:
96
+ html = "<h1>请提供邮箱参数: /login?email=xxx@gmail.com</h1>"
97
  except:
98
  html = "<h1>邮箱输入失败</h1>"
99
+ response = f"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n{html}"
100
+ writer.write(response.encode('utf-8'))
101
  await writer.drain()
102
  return
103
 
 
107
  cookies = await page.context.cookies()
108
  with open("/tmp/cookies.json", "w", encoding="utf-8") as f:
109
  json.dump(cookies, f, ensure_ascii=False, indent=2)
110
+ html = f"<h1>✅ Cookies 已保存!共 {len(cookies)} 个</h1><p><a href='/download_cookies'>下载 cookies.json</a></p>"
 
 
 
 
 
111
  except Exception as e:
112
  html = f"<h1>保存失败: {e}</h1>"
113
+ response = f"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n{html}"
114
+ writer.write(response.encode('utf-8'))
115
  await writer.drain()
116
  return
117
 
 
121
  with open("/tmp/cookies.json", "rb") as f:
122
  content = f.read()
123
  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"
124
+ writer.write(response.encode('utf-8') + content)
125
  await writer.drain()
126
  except:
127
  writer.write(b"HTTP/1.1 404 Not Found\r\n\r\nCookies not found")
 
134
  with open("/tmp/screenshot.png", "rb") as f:
135
  img = f.read()
136
  response = f"HTTP/1.1 200 OK\r\nContent-Type: image/png\r\nContent-Length: {len(img)}\r\n\r\n"
137
+ writer.write(response.encode('utf-8') + img)
138
  await writer.drain()
139
  return
140
 
141
+ # 默认返回
142
+ html = "<h1>Google 代理运行中</h1>"
143
+ response = f"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n{html}"
144
+ writer.write(response.encode('utf-8'))
145
  await writer.drain()
146
 
147
  except Exception as e: