XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/gpt4free

feat(provider/blackbox): update API validation patterns and enhance request headers (#2494)

* feat(blackbox2): update license key pattern - Change regex pattern from 'j= to v=' in license_pattern - Maintain consistent error handling and retry logic - Preserve header configuration for API requests * feat(blackbox): update API key pattern and add accept-language header - Change regex pattern from 'w= to p=' in key_pattern for validation - Add accept-language header to API request headers * refactor(blackbox): enhance UUID validation and regex pattern - Implement robust UUID validation with context checking - Update regex pattern to use dynamic character matching - Add helper function for validating UUID context * refactor(blackbox2): simplify license key validation - Update regex pattern from dynamic format to static 'v=' pattern - Remove context validation helper function - Add docstring for license key validation method --------- Co-authored-by: kqlio67 <>

821e78d9
kqlio67 <166700875+kqlio67@users.noreply.github.com>
提交于

代码差异

2 个文件 +36 -23
Modified g4f/Provider/Blackbox.py +20 -14
@@ -150,7 +150,6 @@ class Blackbox(AsyncGeneratorProvider, ProviderModelMixin):
150 150
151 151 @classmethod
152 152 async def fetch_validated(cls):
153 # Let's try to load the value from the cache first
154 153 cached_value = cls._load_cached_value()
155 154 if cached_value:
156 155 return cached_value
@@ -165,19 +164,25 @@ class Blackbox(AsyncGeneratorProvider, ProviderModelMixin):
165 164 page_content = await response.text()
166 165 js_files = re.findall(r'static/chunks/\d{4}-[a-fA-F0-9]+\.js', page_content)
167 166
168 key_pattern = re.compile(r'w="([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})"')
169
170 for js_file in js_files:
171 js_url = f"{cls.url}/_next/{js_file}"
172 async with session.get(js_url) as js_response:
173 if js_response.status == 200:
174 js_content = await js_response.text()
175 match = key_pattern.search(js_content)
176 if match:
177 validated_value = match.group(1)
178 # Save the new value to the cache file
179 cls._save_cached_value(validated_value)
180 return validated_value
167 uuid_format = r'["\']([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})["\']'
168
169 def is_valid_context(text_around):
170 return any(char + '=' in text_around for char in 'abcdefghijklmnopqrstuvwxyz')
171
172 for js_file in js_files:
173 js_url = f"{cls.url}/_next/{js_file}"
174 async with session.get(js_url) as js_response:
175 if js_response.status == 200:
176 js_content = await js_response.text()
177 for match in re.finditer(uuid_format, js_content):
178 start = max(0, match.start() - 10)
179 end = min(len(js_content), match.end() + 10)
180 context = js_content[start:end]
181
182 if is_valid_context(context):
183 validated_value = match.group(1)
184 cls._save_cached_value(validated_value)
185 return validated_value
181 186 except Exception as e:
182 187 print(f"Error fetching validated value: {e}")
183 188
@@ -240,6 +245,7 @@ class Blackbox(AsyncGeneratorProvider, ProviderModelMixin):
240 245
241 246 headers = {
242 247 'accept': '*/*',
248 'accept-language': 'en-US,en;q=0.9',
243 249 'content-type': 'application/json',
244 250 'origin': cls.url,
245 251 'referer': f'{cls.url}/',
Modified g4f/Provider/Blackbox2.py +16 -9
@@ -64,7 +64,6 @@ class Blackbox2(AsyncGeneratorProvider, ProviderModelMixin):
64 64
65 65 @classmethod
66 66 async def _get_license_key(cls, session: ClientSession) -> str:
67 """Gets the license key from the cache or from JavaScript files."""
68 67 cached_license = cls._load_cached_license()
69 68 if cached_license:
70 69 return cached_license
@@ -73,18 +72,26 @@ class Blackbox2(AsyncGeneratorProvider, ProviderModelMixin):
73 72 async with session.get(cls.url) as response:
74 73 html = await response.text()
75 74 js_files = re.findall(r'static/chunks/\d{4}-[a-fA-F0-9]+\.js', html)
76
77 license_pattern = re.compile(r'j="(\d{6}-\d{6}-\d{6}-\d{6}-\d{6})"')
78
75
76 license_format = r'["\'](\d{6}-\d{6}-\d{6}-\d{6}-\d{6})["\']'
77
78 def is_valid_context(text_around):
79 return any(char + '=' in text_around for char in 'abcdefghijklmnopqrstuvwxyz')
80
79 81 for js_file in js_files:
80 82 js_url = f"{cls.url}/_next/{js_file}"
81 83 async with session.get(js_url) as js_response:
82 84 js_content = await js_response.text()
83 if license_match := license_pattern.search(js_content):
84 license_key = license_match.group(1)
85 cls._save_cached_license(license_key)
86 return license_key
87
85 for match in re.finditer(license_format, js_content):
86 start = max(0, match.start() - 10)
87 end = min(len(js_content), match.end() + 10)
88 context = js_content[start:end]
89
90 if is_valid_context(context):
91 license_key = match.group(1)
92 cls._save_cached_license(license_key)
93 return license_key
94
88 95 raise ValueError("License key not found")
89 96 except Exception as e:
90 97 debug.log(f"Error getting license key: {str(e)}")