-
-
Notifications
You must be signed in to change notification settings - Fork 527
feat: Enhance client robustness with key extraction fallbacks, update… #407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import re | ||
| import logging | ||
| import bs4 | ||
| import math | ||
| import time | ||
|
|
@@ -15,7 +16,15 @@ | |
| ON_DEMAND_FILE_REGEX = re.compile( | ||
| r"""['|\"]{1}ondemand\.s['|\"]{1}:\s*['|\"]{1}([\w]*)['|\"]{1}""", flags=(re.VERBOSE | re.MULTILINE)) | ||
| INDICES_REGEX = re.compile( | ||
| r"""(\(\w{1}\[(\d{1,2})\],\s*16\))+""", flags=(re.VERBOSE | re.MULTILINE)) | ||
| r"""\(?(\w+)\[(\d{1,2})\]\s*,\s*16\)?""", flags=(re.VERBOSE | re.MULTILINE)) | ||
|
|
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # Fallback constants for when key extraction fails | ||
| FALLBACK_KEY = 'mentUisV_1yPzH_3IcNS_nRaF_R_b' | ||
| FALLBACK_ROW_INDEX = 2 | ||
| FALLBACK_KEY_BYTES_INDICES = [13, 14, 7] | ||
|
|
||
|
|
||
| class ClientTransaction: | ||
|
|
@@ -31,12 +40,21 @@ async def init(self, session, headers): | |
| home_page_response = await handle_x_migration(session, headers) | ||
|
|
||
| self.home_page_response = self.validate_response(home_page_response) | ||
| self.DEFAULT_ROW_INDEX, self.DEFAULT_KEY_BYTES_INDICES = await self.get_indices( | ||
| self.home_page_response, session, headers) | ||
| self.key = self.get_key(response=self.home_page_response) | ||
| self.key_bytes = self.get_key_bytes(key=self.key) | ||
| self.animation_key = self.get_animation_key( | ||
| key_bytes=self.key_bytes, response=self.home_page_response) | ||
| try: | ||
| self.DEFAULT_ROW_INDEX, self.DEFAULT_KEY_BYTES_INDICES = await self.get_indices( | ||
| self.home_page_response, session, headers) | ||
| self.key = self.get_key(response=self.home_page_response) | ||
| self.key_bytes = self.get_key_bytes(key=self.key) | ||
| self.animation_key = self.get_animation_key( | ||
| key_bytes=self.key_bytes, response=self.home_page_response) | ||
| except (AttributeError, ValueError, KeyError, Exception) as e: | ||
| # Fallback to defaults to prevent complete initialization failure | ||
| self.key = FALLBACK_KEY | ||
| self.key_bytes = self.get_key_bytes(self.key) | ||
| self.DEFAULT_ROW_INDEX = FALLBACK_ROW_INDEX | ||
| self.DEFAULT_KEY_BYTES_INDICES = FALLBACK_KEY_BYTES_INDICES | ||
| self.animation_key = "0" | ||
| logger.warning(f"Twikit Patch Initialization Warning: {e}, using fallback defaults") | ||
|
|
||
| async def get_indices(self, home_page_response, session, headers): | ||
| key_byte_indices = [] | ||
|
|
@@ -142,10 +160,18 @@ def generate_transaction_id(self, method: str, path: str, response=None, key=Non | |
| time_now = time_now or math.floor( | ||
| (time.time() * 1000 - 1682924400 * 1000) / 1000) | ||
| time_now_bytes = [(time_now >> (i * 8)) & 0xFF for i in range(4)] | ||
| key = key or self.key or self.get_key(response) | ||
| key = key or getattr(self, 'key', None) | ||
| if not key: | ||
| try: | ||
| key = self.get_key(response) | ||
| except Exception: | ||
| key = FALLBACK_KEY | ||
| key_bytes = self.get_key_bytes(key) | ||
| animation_key = animation_key or self.animation_key or self.get_animation_key( | ||
| key_bytes, response) | ||
| try: | ||
|
Comment on lines
+163
to
+170
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guard key-byte decoding to avoid a remaining transaction-generation crash path. Line 169 can still raise if 🔧 Proposed hardening key = key or getattr(self, 'key', None)
if not key:
try:
key = self.get_key(response)
except Exception:
key = FALLBACK_KEY
- key_bytes = self.get_key_bytes(key)
+ try:
+ key_bytes = self.get_key_bytes(key)
+ except Exception as e:
+ logger.warning(
+ "Twikit Patch Key Decode Warning: %s, using fallback key bytes", e
+ )
+ key = FALLBACK_KEY
+ key_bytes = getattr(self, "key_bytes", None) or self.get_key_bytes(FALLBACK_KEY)🧰 Tools🪛 Ruff (0.15.6)[warning] 167-167: Do not catch blind exception: (BLE001) 🤖 Prompt for AI Agents |
||
| animation_key = animation_key or getattr(self, 'animation_key', None) or self.get_animation_key( | ||
| key_bytes, response) | ||
| except Exception: | ||
| animation_key = "0" | ||
| # hash_val = hashlib.sha256(f"{method}!{path}!{time_now}bird{animation_key}".encode()).digest() | ||
| hash_val = hashlib.sha256( | ||
| f"{method}!{path}!{time_now}{self.DEFAULT_KEYWORD}{animation_key}".encode()).digest() | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.