From 96e378e9e21092f486e6f77a083651865062d893 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 18 May 2024 15:37:46 +0200 Subject: Fix OpenaiChat provider, improve proofofwork --- g4f/Provider/openai/har_file.py | 7 ++++--- g4f/Provider/openai/proofofwork.py | 29 +++++++++-------------------- 2 files changed, 13 insertions(+), 23 deletions(-) (limited to 'g4f/Provider/openai') diff --git a/g4f/Provider/openai/har_file.py b/g4f/Provider/openai/har_file.py index eefe305f..8dcbe44e 100644 --- a/g4f/Provider/openai/har_file.py +++ b/g4f/Provider/openai/har_file.py @@ -35,6 +35,7 @@ headers: dict = None proofTokens: list = [] def readHAR(): + global proofTokens dirPath = "./" harPath = [] chatArks = [] @@ -77,8 +78,8 @@ def readHAR(): if not accessToken: raise NoValidHarFileError("No accessToken found in .har files") if not chatArks: - return None, accessToken, cookies, headers, proofTokens - return chatArks.pop(), accessToken, cookies, headers, proofTokens + return None, accessToken, cookies, headers + return chatArks.pop(), accessToken, cookies, headers def get_headers(entry) -> dict: return {h['name'].lower(): h['value'] for h in entry['request']['headers'] if h['name'].lower() not in ['content-length', 'cookie'] and not h['name'].startswith(':')} @@ -145,7 +146,7 @@ def getN() -> str: async def getArkoseAndAccessToken(proxy: str) -> tuple[str, str, dict, dict]: global chatArk, accessToken, cookies, headers, proofTokens if chatArk is None or accessToken is None: - chatArk, accessToken, cookies, headers, proofTokens = readHAR() + chatArk, accessToken, cookies, headers = readHAR() if chatArk is None: return None, accessToken, cookies, headers, proofTokens newReq = genArkReq(chatArk) diff --git a/g4f/Provider/openai/proofofwork.py b/g4f/Provider/openai/proofofwork.py index cbce153f..baf8a0ea 100644 --- a/g4f/Provider/openai/proofofwork.py +++ b/g4f/Provider/openai/proofofwork.py @@ -4,22 +4,18 @@ import json import base64 from datetime import datetime, timezone -proof_token_cache: dict = {} -def generate_proof_token(required: bool, seed: str = None, difficulty: str = None, user_agent: str = None, proofTokens: list = None): +def generate_proof_token(required: bool, seed: str = "", difficulty: str = "", user_agent: str = None, proofTokens: list = None): if not required: return - if seed is not None and seed in proof_token_cache: - return proof_token_cache[seed] - - # Get current UTC time - now_utc = datetime.now(timezone.utc) - parse_time = now_utc.strftime('%a, %d %b %Y %H:%M:%S GMT') if proofTokens: - config = random.choice(proofTokens) + config = proofTokens[-1] else: screen = random.choice([3008, 4010, 6000]) * random.choice([1, 2, 4]) + # Get current UTC time + now_utc = datetime.now(timezone.utc) + parse_time = now_utc.strftime('%a, %d %b %Y %H:%M:%S GMT') config = [ screen, parse_time, None, 0, user_agent, @@ -31,22 +27,15 @@ def generate_proof_token(required: bool, seed: str = None, difficulty: str = Non random.choice(["alert", "ontransitionend", "onprogress"]) ] - config[1] = parse_time - config[4] = user_agent - config[7] = random.randint(101, 2100) - - diff_len = None if difficulty is None else len(difficulty) + diff_len = len(difficulty) for i in range(100000): config[3] = i json_data = json.dumps(config) base = base64.b64encode(json_data.encode()).decode() - hash_value = hashlib.sha3_512((seed or "" + base).encode()).digest() + hash_value = hashlib.sha3_512((seed + base).encode()).digest() - if difficulty is None or hash_value.hex()[:diff_len] <= difficulty: - if seed is None: - return "gAAAAAC" + base - proof_token_cache[seed] = "gAAAAAB" + base - return proof_token_cache[seed] + if hash_value.hex()[:diff_len] <= difficulty: + return "gAAAAAB" + base fallback_base = base64.b64encode(f'"{seed}"'.encode()).decode() return "gAAAAABwQ8Lk5FbGpA2NcR9dShT6gYjU7VxZ4D" + fallback_base -- cgit v1.2.3