-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbypass.py
More file actions
110 lines (82 loc) · 3.34 KB
/
Copy pathbypass.py
File metadata and controls
110 lines (82 loc) · 3.34 KB
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
# run_and_get_cookies.py (async version)
import asyncio
import os
import sys
import json
import aiohttp
def validate_cookies(cookies_data):
"""Validate that cf_clearance cookie is present and not empty"""
cookies = cookies_data.get('cookies', {})
return 'cf_clearance' in cookies and cookies['cf_clearance'].strip() != ''
async def get_and_save_cookies(server_url, cookie_file_path, max_retries=3):
"""
Fetch cookies from local server and persist them to disk (async version)
"""
async with aiohttp.ClientSession() as session:
for attempt in range(max_retries):
try:
async with session.get(server_url) as response:
response.raise_for_status()
cookies_data = await response.json()
if not validate_cookies(cookies_data):
print(f"Attempt {attempt + 1}: cf_clearance missing, retrying...")
await asyncio.sleep(5)
continue
cookies_to_save = {
"cookies": cookies_data.get("cookies", {}),
"user_agent": cookies_data.get("user_agent", "")
}
os.makedirs(os.path.dirname(cookie_file_path), exist_ok=True)
with open(cookie_file_path, "w", encoding="utf-8") as f:
json.dump(cookies_to_save, f, indent=4, ensure_ascii=False)
print("Successfully obtained and saved cookies with cf_clearance!")
return True
except aiohttp.ClientConnectionError as e:
print(f"Connection error on attempt {attempt + 1}: {e}")
if attempt < max_retries - 1:
await asyncio.sleep(5)
else:
print("Max retries reached. Failed to get valid cookies.")
return False
except Exception as e:
print(f"Unexpected error: {e}")
return False
print("Failed to obtain valid cf_clearance cookie after all attempts")
return False
async def run_server_background():
"""
Launch server.py in background async-safe way
"""
script_dir = os.path.dirname(os.path.abspath(__file__))
server_script = os.path.abspath(os.path.join(script_dir, "server.py"))
server_dir = os.path.dirname(server_script)
os.makedirs(server_dir, exist_ok=True)
try:
process = await asyncio.create_subprocess_exec(
sys.executable,
server_script,
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.DEVNULL,
cwd=server_dir
)
return process
except Exception:
return None
async def main():
print("Getting the cookies...")
server_process = await run_server_background()
if server_process:
await asyncio.sleep(10)
server_url = "http://localhost:8021/cookies?url=https://chat.deepseek.com"
cookie_file = "dsk/dsk/cookies.json"
success = await get_and_save_cookies(server_url, cookie_file, max_retries=5)
if not success:
print("Failed to obtain valid cookies.")
server_process.terminate()
sys.exit(1)
server_process.terminate()
else:
print("Failed to start server.")
sys.exit(1)
if __name__ == "__main__":
asyncio.run(main())