diff options
author | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-11-18 02:53:50 +0100 |
---|---|---|
committer | Heiner Lohaus <hlohaus@users.noreply.github.com> | 2024-11-18 02:53:50 +0100 |
commit | 2a7770ea51237fb2a34da7b853c82139c75ca69b (patch) | |
tree | a515f0051110ecc46d2b46a12ff9a3cb1bfe8e4c /g4f/Provider/openai | |
parent | Update docker tags in workfloe for slim images, (diff) | |
download | gpt4free-2a7770ea51237fb2a34da7b853c82139c75ca69b.tar gpt4free-2a7770ea51237fb2a34da7b853c82139c75ca69b.tar.gz gpt4free-2a7770ea51237fb2a34da7b853c82139c75ca69b.tar.bz2 gpt4free-2a7770ea51237fb2a34da7b853c82139c75ca69b.tar.lz gpt4free-2a7770ea51237fb2a34da7b853c82139c75ca69b.tar.xz gpt4free-2a7770ea51237fb2a34da7b853c82139c75ca69b.tar.zst gpt4free-2a7770ea51237fb2a34da7b853c82139c75ca69b.zip |
Diffstat (limited to 'g4f/Provider/openai')
-rw-r--r-- | g4f/Provider/openai/har_file.py | 72 | ||||
-rw-r--r-- | g4f/Provider/openai/proofofwork.py | 10 |
2 files changed, 41 insertions, 41 deletions
diff --git a/g4f/Provider/openai/har_file.py b/g4f/Provider/openai/har_file.py index b7c36f05..c8d85a65 100644 --- a/g4f/Provider/openai/har_file.py +++ b/g4f/Provider/openai/har_file.py @@ -15,8 +15,27 @@ from ...requests import StreamSession from ...cookies import get_cookies_dir from ... import debug +arkose_url = "https://tcr9i.chat.openai.com/fc/gt2/public_key/35536E1E-65B4-4D96-9D97-6ADB7EFF8147" +backend_url = "https://chatgpt.com/backend-api/conversation" +backend_anon_url = "https://chatgpt.com/backend-anon/conversation" +start_url = "https://chatgpt.com/" +conversation_url = "https://chatgpt.com/c/" + class NoValidHarFileError(Exception): - ... + pass + +class RequestConfig: + user_agent: str = None + cookies: dict = None + headers: dict = None + access_request_id: str = None + access_token: str = None + proof_token: list = None + turnstile_token: str = None + arkose_request: arkReq = None + arkose_token: str = None + headers: dict = {} + cookies: dict = {} class arkReq: def __init__(self, arkURL, arkBx, arkHeader, arkBody, arkCookies, userAgent): @@ -27,23 +46,9 @@ class arkReq: self.arkCookies = arkCookies self.userAgent = userAgent -arkoseURL = "https://tcr9i.chat.openai.com/fc/gt2/public_key/35536E1E-65B4-4D96-9D97-6ADB7EFF8147" -startUrl = "https://chatgpt.com/" -conversationUrl = "https://chatgpt.com/c/" -arkoseRequest: arkReq = None -accessToken: str = None -cookies: dict = None -headers: dict = None -proofToken: list = [] -turnstileToken: str = None - def readHAR(): - global arkoseRequest, accessToken, proofToken, turnstileToken harPath = [] - chatArks = [] - accessToken = None - cookies = {} - for root, dirs, files in os.walk(get_cookies_dir()): + for root, _, files in os.walk(get_cookies_dir()): for file in files: if file.endswith(".har"): harPath.append(os.path.join(root, file)) @@ -60,30 +65,27 @@ def readHAR(): v_headers = get_headers(v) try: if "openai-sentinel-proof-token" in v_headers: - proofToken = json.loads(base64.b64decode( + RequestConfig.proof_token = json.loads(base64.b64decode( v_headers["openai-sentinel-proof-token"].split("gAAAAAB", 1)[-1].encode() ).decode()) if "openai-sentinel-turnstile-token" in v_headers: - turnstileToken = v_headers["openai-sentinel-turnstile-token"] + RequestConfig.turnstile_token = v_headers["openai-sentinel-turnstile-token"] except Exception as e: if debug.logging: print(f"Read proof token: {e}") - if arkoseURL in v['request']['url']: - arkoseRequest = parseHAREntry(v) - elif v['request']['url'] == startUrl or v['request']['url'].startswith(conversationUrl): + if arkose_url == v['request']['url']: + RequestConfig.arkose_request = parseHAREntry(v) + elif v['request']['url'] == start_url or v['request']['url'].startswith(conversation_url): try: match = re.search(r'"accessToken":"(.*?)"', v["response"]["content"]["text"]) if match: - accessToken = match.group(1) + RequestConfig.access_token = match.group(1) except KeyError: continue - cookies = {c['name']: c['value'] for c in v['request']['cookies'] if c['name'] != "oai-did"} - headers = v_headers - if not accessToken: + RequestConfig.cookies = {c['name']: c['value'] for c in v['request']['cookies'] if c['name'] != "oai-did"} + RequestConfig.headers = v_headers + if RequestConfig.access_token is None: raise NoValidHarFileError("No accessToken found in .har files") - if not chatArks: - return cookies, headers - return 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(':')} @@ -147,11 +149,9 @@ def getN() -> str: timestamp = str(int(time.time())) return base64.b64encode(timestamp.encode()).decode() -async def getArkoseAndAccessToken(proxy: str) -> tuple[str, str, dict, dict]: - global arkoseRequest, accessToken, cookies, headers, proofToken, turnstileToken - if arkoseRequest is None or accessToken is None: - cookies, headers = readHAR() - if arkoseRequest is None: - return None, accessToken, cookies, headers, proofToken, turnstileToken - newReq = genArkReq(arkoseRequest) - return await sendRequest(newReq, proxy), accessToken, cookies, headers, proofToken, turnstileToken +async def get_request_config(proxy: str) -> RequestConfig: + if RequestConfig.arkose_request is None or RequestConfig.access_token is None: + readHAR() + if RequestConfig.arkose_request is not None: + RequestConfig.arkose_token = await sendRequest(genArkReq(RequestConfig.arkose_request), proxy) + return RequestConfig
\ No newline at end of file diff --git a/g4f/Provider/openai/proofofwork.py b/g4f/Provider/openai/proofofwork.py index 23e5ab6e..4294c99a 100644 --- a/g4f/Provider/openai/proofofwork.py +++ b/g4f/Provider/openai/proofofwork.py @@ -4,16 +4,16 @@ import json import base64 from datetime import datetime, timezone -def generate_proof_token(required: bool, seed: str = "", difficulty: str = "", user_agent: str = None, proofToken: str = None): +def generate_proof_token(required: bool, seed: str = "", difficulty: str = "", user_agent: str = None, proof_token: str = None): if not required: return - if proofToken is None: + if proof_token is None: 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') - proofToken = [ + proof_token = [ screen, parse_time, None, 0, user_agent, "https://tcr9i.chat.openai.com/v2/35536E1E-65B4-4D96-9D97-6ADB7EFF8147/api.js", @@ -26,8 +26,8 @@ def generate_proof_token(required: bool, seed: str = "", difficulty: str = "", u diff_len = len(difficulty) for i in range(100000): - proofToken[3] = i - json_data = json.dumps(proofToken) + proof_token[3] = i + json_data = json.dumps(proof_token) base = base64.b64encode(json_data.encode()).decode() hash_value = hashlib.sha3_512((seed + base).encode()).digest() |