Skip to content

Commit

Permalink
💎 [Feature] Request cookies to bypass challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
Hansimov committed Jan 21, 2024
1 parent ab87882 commit 027cc10
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 8 deletions.
10 changes: 8 additions & 2 deletions conversations/conversation_creator.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import requests
from pprint import pprint
from utils.logger import logger
from utils.enver import enver
from networks import ConversationCreateHeadersConstructor
from networks import CookiesConstructor


class ConversationCreator:
conversation_create_url = "https://www.bing.com/turing/conversation/create"

def __init__(self, cookies: dict = {}):
self.cookies = cookies
self.request_cookies = cookies

def construct_cookies(self):
pass
cookies_constructor = CookiesConstructor()
if not self.request_cookies:
cookies_constructor.construct()
self.request_cookies = cookies_constructor.cookies

def construct_headers(self):
# New Bing 封锁原理探讨 #78
Expand All @@ -26,6 +31,7 @@ def create(self):
self.conversation_create_url,
headers=self.request_headers,
proxies=enver.requests_proxies,
cookies=self.request_cookies,
)
try:
self.response_data = self.response.json()
Expand Down
2 changes: 1 addition & 1 deletion networks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .chathub_request_payload_constructor import ChathubRequestPayloadConstructor
from .cookies_constructor import CookiesConstructor
from .chathub_request_payload_constructor import ChathubRequestPayloadConstructor
from .conversation_request_headers_constructor import (
ConversationRequestHeadersConstructor,
)
Expand Down
100 changes: 95 additions & 5 deletions networks/cookies_constructor.py
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()

0 comments on commit 027cc10

Please sign in to comment.