summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/openai
diff options
context:
space:
mode:
authorHeiner Lohaus <hlohaus@users.noreply.github.com>2024-05-18 07:37:37 +0200
committerHeiner Lohaus <hlohaus@users.noreply.github.com>2024-05-18 07:37:37 +0200
commitb1dafc0ef79bdd94f69c783877217d8a5524d460 (patch)
tree7851c85bafa452d0fa9cce74aa7b383aa704f101 /g4f/Provider/openai
parentMerge pull request #1969 from hlohaus/leech (diff)
downloadgpt4free-b1dafc0ef79bdd94f69c783877217d8a5524d460.tar
gpt4free-b1dafc0ef79bdd94f69c783877217d8a5524d460.tar.gz
gpt4free-b1dafc0ef79bdd94f69c783877217d8a5524d460.tar.bz2
gpt4free-b1dafc0ef79bdd94f69c783877217d8a5524d460.tar.lz
gpt4free-b1dafc0ef79bdd94f69c783877217d8a5524d460.tar.xz
gpt4free-b1dafc0ef79bdd94f69c783877217d8a5524d460.tar.zst
gpt4free-b1dafc0ef79bdd94f69c783877217d8a5524d460.zip
Diffstat (limited to 'g4f/Provider/openai')
-rw-r--r--g4f/Provider/openai/har_file.py30
-rw-r--r--g4f/Provider/openai/proofofwork.py48
2 files changed, 53 insertions, 25 deletions
diff --git a/g4f/Provider/openai/har_file.py b/g4f/Provider/openai/har_file.py
index 220c20bf..eefe305f 100644
--- a/g4f/Provider/openai/har_file.py
+++ b/g4f/Provider/openai/har_file.py
@@ -12,6 +12,7 @@ from copy import deepcopy
from .crypt import decrypt, encrypt
from ...requests import StreamSession
+from ... import debug
class NoValidHarFileError(Exception):
...
@@ -31,6 +32,7 @@ chatArk: arkReq = None
accessToken: str = None
cookies: dict = None
headers: dict = None
+proofTokens: list = []
def readHAR():
dirPath = "./"
@@ -54,6 +56,15 @@ def readHAR():
# Error: not a HAR file!
continue
for v in harFile['log']['entries']:
+ v_headers = get_headers(v)
+ try:
+ if "openai-sentinel-proof-token" in v_headers:
+ proofTokens.append(json.loads(base64.b64decode(
+ v_headers["openai-sentinel-proof-token"].split("gAAAAAB", 1)[-1].encode()
+ ).decode()))
+ except Exception as e:
+ if debug.logging:
+ print(f"Read proof token: {e}")
if arkPreURL in v['request']['url']:
chatArks.append(parseHAREntry(v))
elif v['request']['url'] == sessionUrl:
@@ -61,13 +72,13 @@ def readHAR():
accessToken = json.loads(v["response"]["content"]["text"]).get("accessToken")
except KeyError:
continue
- cookies = {c['name']: c['value'] for c in v['request']['cookies']}
- headers = get_headers(v)
+ cookies = {c['name']: c['value'] for c in v['request']['cookies'] if c['name'] != "oai-did"}
+ headers = v_headers
if not accessToken:
raise NoValidHarFileError("No accessToken found in .har files")
if not chatArks:
- return None, accessToken, cookies, headers
- return chatArks.pop(), accessToken, cookies, headers
+ return None, accessToken, cookies, headers, proofTokens
+ return chatArks.pop(), accessToken, cookies, headers, proofTokens
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(':')}
@@ -101,7 +112,8 @@ def genArkReq(chatArk: arkReq) -> arkReq:
async def sendRequest(tmpArk: arkReq, proxy: str = None):
async with StreamSession(headers=tmpArk.arkHeader, cookies=tmpArk.arkCookies, proxies={"https": proxy}) as session:
async with session.post(tmpArk.arkURL, data=tmpArk.arkBody) as response:
- arkose = (await response.json()).get("token")
+ data = await response.json()
+ arkose = data.get("token")
if "sup=1|rid=" not in arkose:
return RuntimeError("No valid arkose token generated")
return arkose
@@ -131,10 +143,10 @@ def getN() -> str:
return base64.b64encode(timestamp.encode()).decode()
async def getArkoseAndAccessToken(proxy: str) -> tuple[str, str, dict, dict]:
- global chatArk, accessToken, cookies, headers
+ global chatArk, accessToken, cookies, headers, proofTokens
if chatArk is None or accessToken is None:
- chatArk, accessToken, cookies, headers = readHAR()
+ chatArk, accessToken, cookies, headers, proofTokens = readHAR()
if chatArk is None:
- return None, accessToken, cookies, headers
+ return None, accessToken, cookies, headers, proofTokens
newReq = genArkReq(chatArk)
- return await sendRequest(newReq, proxy), accessToken, cookies, headers
+ return await sendRequest(newReq, proxy), accessToken, cookies, headers, proofTokens
diff --git a/g4f/Provider/openai/proofofwork.py b/g4f/Provider/openai/proofofwork.py
index 51d96bc4..cbce153f 100644
--- a/g4f/Provider/openai/proofofwork.py
+++ b/g4f/Provider/openai/proofofwork.py
@@ -2,35 +2,51 @@ import random
import hashlib
import json
import base64
-from datetime import datetime, timedelta, timezone
+from datetime import datetime, timezone
-def generate_proof_token(required: bool, seed: str, difficulty: str, user_agent: str):
+proof_token_cache: dict = {}
+
+def generate_proof_token(required: bool, seed: str = None, difficulty: str = None, user_agent: str = None, proofTokens: list = None):
if not required:
return
-
- cores = [8, 12, 16, 24]
- screens = [3000, 4000, 6000]
-
- core = random.choice(cores)
- screen = random.choice(screens)
+ 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')
- config = [core + screen, parse_time, None, 0, user_agent, "https://tcr9i.chat.openai.com/v2/35536E1E-65B4-4D96-9D97-6ADB7EFF8147/api.js","dpl=53d243de46ff04dadd88d293f088c2dd728f126f","en","en-US",442,"plugins−[object PluginArray]","","alert"]
-
- diff_len = len(difficulty) // 2
-
+ if proofTokens:
+ config = random.choice(proofTokens)
+ else:
+ screen = random.choice([3008, 4010, 6000]) * random.choice([1, 2, 4])
+ config = [
+ screen, parse_time,
+ None, 0, user_agent,
+ "https://tcr9i.chat.openai.com/v2/35536E1E-65B4-4D96-9D97-6ADB7EFF8147/api.js",
+ "dpl=1440a687921de39ff5ee56b92807faaadce73f13","en","en-US",
+ None,
+ "plugins−[object PluginArray]",
+ random.choice(["_reactListeningcfilawjnerp", "_reactListening9ne2dfo1i47", "_reactListening410nzwhan2a"]),
+ 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)
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 + base).encode()).digest()
+ hash_value = hashlib.sha3_512((seed or "" + base).encode()).digest()
- if hash_value.hex()[:diff_len] <= difficulty:
- result = "gAAAAAB" + base
- return result
+ 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]
fallback_base = base64.b64encode(f'"{seed}"'.encode()).decode()
return "gAAAAABwQ8Lk5FbGpA2NcR9dShT6gYjU7VxZ4D" + fallback_base