Skip to content

Commit 4ea8d18

Browse files
authored
Merge pull request #64 from toddyoe/main
支持自定义WAF的Cookie列表名称
2 parents eb641ed + 77934d3 commit 4ea8d18

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@
199199
"sign_in_path": "/api/checkin",
200200
"user_info_path": "/api/profile",
201201
"api_user_key": "New-Api-User",
202-
"bypass_method": "waf_cookies"
202+
"bypass_method": "waf_cookies",
203+
"waf_cookie_names": ["acw_tc", "cdn_sec_tc", "acw_sc__v2"]
203204
}
204205
}
205206
```
@@ -226,6 +227,7 @@
226227
- `bypass_method` (可选):WAF 绕过方法
227228
- `"waf_cookies"`:使用 Playwright 打开浏览器获取 WAF cookies 后再执行签到
228229
- 不设置或 `null`:直接使用用户 cookies 执行签到(适合无 WAF 保护的网站)
230+
- `waf_cookie_names` (可选):绕过 WAF 所需 cookie 的名称列表,`bypass_method``waf_cookies` 时必须设置
229231

230232
**配置示例**(完整):
231233
```json

checkin.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def parse_cookies(cookies_data):
6565
return {}
6666

6767

68-
async def get_waf_cookies_with_playwright(account_name: str, login_url: str):
68+
async def get_waf_cookies_with_playwright(account_name: str, login_url: str, required_cookies: list[str]):
6969
"""使用 Playwright 获取 WAF cookies(隐私模式)"""
7070
print(f'[PROCESSING] {account_name}: Starting browser to get WAF cookies...')
7171

@@ -105,12 +105,11 @@ async def get_waf_cookies_with_playwright(account_name: str, login_url: str):
105105
for cookie in cookies:
106106
cookie_name = cookie.get('name')
107107
cookie_value = cookie.get('value')
108-
if cookie_name in ['acw_tc', 'cdn_sec_tc', 'acw_sc__v2'] and cookie_value is not None:
108+
if cookie_name in required_cookies and cookie_value is not None:
109109
waf_cookies[cookie_name] = cookie_value
110110

111111
print(f'[INFO] {account_name}: Got {len(waf_cookies)} WAF cookies')
112112

113-
required_cookies = ['acw_tc', 'cdn_sec_tc', 'acw_sc__v2']
114113
missing_cookies = [c for c in required_cookies if c not in waf_cookies]
115114

116115
if missing_cookies:
@@ -158,7 +157,7 @@ async def prepare_cookies(account_name: str, provider_config, user_cookies: dict
158157

159158
if provider_config.needs_waf_cookies():
160159
login_url = f'{provider_config.domain}{provider_config.login_path}'
161-
waf_cookies = await get_waf_cookies_with_playwright(account_name, login_url)
160+
waf_cookies = await get_waf_cookies_with_playwright(account_name, login_url, provider_config.waf_cookie_names)
162161
if not waf_cookies:
163162
print(f'[FAILED] {account_name}: Unable to get WAF cookies')
164163
return None

utils/config.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import json
77
import os
88
from dataclasses import dataclass
9-
from typing import Dict, Literal
9+
from typing import Dict, List, Literal
1010

1111

1212
@dataclass
@@ -20,6 +20,23 @@ class ProviderConfig:
2020
user_info_path: str = '/api/user/self'
2121
api_user_key: str = 'new-api-user'
2222
bypass_method: Literal['waf_cookies'] | None = None
23+
waf_cookie_names: List[str] | None = None
24+
25+
def __post_init__(self):
26+
required_waf_cookies = set()
27+
if self.waf_cookie_names and isinstance(self.waf_cookie_names, List):
28+
for item in self.waf_cookie_names:
29+
name = "" if not item or not isinstance(item, str) else item.strip()
30+
if not name:
31+
print(f'[WARNING] Found invalid WAF cookie name: {item}')
32+
continue
33+
34+
required_waf_cookies.add(name)
35+
36+
if not required_waf_cookies:
37+
self.bypass_method = None
38+
39+
self.waf_cookie_names = list(required_waf_cookies)
2340

2441
@classmethod
2542
def from_dict(cls, name: str, data: dict) -> 'ProviderConfig':
@@ -37,6 +54,7 @@ def from_dict(cls, name: str, data: dict) -> 'ProviderConfig':
3754
user_info_path=data.get('user_info_path', '/api/user/self'),
3855
api_user_key=data.get('api_user_key', 'new-api-user'),
3956
bypass_method=data.get('bypass_method'),
57+
waf_cookie_names = data.get('waf_cookie_names'),
4058
)
4159

4260
def needs_waf_cookies(self) -> bool:
@@ -66,6 +84,7 @@ def load_from_env(cls) -> 'AppConfig':
6684
user_info_path='/api/user/self',
6785
api_user_key='new-api-user',
6886
bypass_method='waf_cookies',
87+
waf_cookie_names=['acw_tc', 'cdn_sec_tc', 'acw_sc__v2'],
6988
),
7089
'agentrouter': ProviderConfig(
7190
name='agentrouter',
@@ -74,7 +93,8 @@ def load_from_env(cls) -> 'AppConfig':
7493
sign_in_path=None, # 无需签到接口,查询用户信息时自动完成签到
7594
user_info_path='/api/user/self',
7695
api_user_key='new-api-user',
77-
bypass_method=None,
96+
bypass_method='waf_cookies',
97+
waf_cookie_names=['acw_tc'],
7898
),
7999
}
80100

0 commit comments

Comments
 (0)