-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💎 [Feature] Request cookies to bypass challenge
- Loading branch information
Showing
3 changed files
with
104 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,98 @@ | ||
import json | ||
import requests | ||
from datetime import datetime | ||
from pathlib import Path | ||
|
||
from utils.logger import logger | ||
|
||
|
||
class CookiesConstructor: | ||
def __init__(self) -> None: | ||
self.cookies_list = [] # Dumped from Cookie-Editor extension in Browser | ||
bypass_url = "https://zklcdc-go-bingai-pass.hf.space" | ||
|
||
def construct(self): | ||
def __init__(self): | ||
self.cookies = {} | ||
for cookie in self.cookies_list: | ||
self.cookies[cookie["name"]] = cookie["value"] | ||
self.secrets_path = Path(__file__).parents[1] / "secrets.json" | ||
self.created_datetime_format = "%Y-%m-%d %H:%M:%S" | ||
|
||
def create_secrets_json(self): | ||
if not self.secrets_path.exists(): | ||
self.secrets_path.parent.mkdir(parents=True, exist_ok=True) | ||
with open(self.secrets_path, "w") as wf: | ||
json.dump({}, wf) | ||
|
||
def is_local_cookies_valid(self): | ||
self.create_secrets_json() | ||
if self.secrets_path.exists(): | ||
with open(self.secrets_path, "r") as f: | ||
secrets = json.load(f) | ||
if secrets.get("cookies"): | ||
cookies = secrets["cookies"] | ||
cookies_str = cookies.get("cookies_str") | ||
cookies_created_datetime = datetime.strptime( | ||
cookies.get("created_time"), self.created_datetime_format | ||
) | ||
datetime_now = datetime.now() | ||
# if cookies created more than 12 hours, then it's invalid | ||
self.cookies_created_seconds = ( | ||
datetime_now - cookies_created_datetime | ||
).seconds | ||
if self.cookies_created_seconds < 12 * 60 * 60: | ||
self.cookies_str = cookies_str | ||
self.cookies_created_datetime = cookies_created_datetime | ||
return True | ||
else: | ||
return False | ||
return False | ||
|
||
def requests_cookies(self): | ||
if self.is_local_cookies_valid(): | ||
logger.success( | ||
f"Local Cookies Used: {self.cookies_created_datetime} " | ||
f"({round(self.cookies_created_seconds/60/60,2)} hours ago)" | ||
) | ||
return | ||
|
||
requests_body = {"cookies": ""} | ||
try: | ||
res = requests.post( | ||
self.bypass_url, | ||
json=requests_body, | ||
timeout=15, | ||
) | ||
data = res.json() | ||
cookies_str = data["result"]["cookies"] | ||
logger.note(f"Get Cookies: {cookies_str}") | ||
if cookies_str: | ||
with open(self.secrets_path, "r") as rf: | ||
secrets = json.load(rf) | ||
secrets["cookies"] = { | ||
"cookies_str": cookies_str, | ||
"created_time": datetime.now().strftime( | ||
self.created_datetime_format | ||
), | ||
} | ||
with open(self.secrets_path, "w") as wf: | ||
json.dump(secrets, wf) | ||
except Exception as e: | ||
cookies_str = "" | ||
logger.err(e) | ||
|
||
self.cookies_str = cookies_str | ||
|
||
def cookies_str_to_dict(self): | ||
cookie_items = self.cookies_str.split(";") | ||
for cookie_item in cookie_items: | ||
if not cookie_item: | ||
continue | ||
cookie_key, cookie_value = cookie_item.split("=", 1) | ||
self.cookies[cookie_key.strip()] = cookie_value.strip() | ||
logger.success(f"Cookies: {self.cookies}") | ||
|
||
def construct(self): | ||
self.requests_cookies() | ||
self.cookies_str_to_dict() | ||
|
||
|
||
if __name__ == "__main__": | ||
cookies_constructor = CookiesConstructor() | ||
cookies_constructor.construct() |