Improve reading .har file in OpenaiChat

This commit is contained in:
Heiner Lohaus 2024-11-21 08:22:48 +01:00
parent 6f2b6cccbd
commit 1e2c18580c
2 changed files with 22 additions and 20 deletions

View File

@ -412,7 +412,7 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin):
if "proofofwork" in chat_requirements: if "proofofwork" in chat_requirements:
proofofwork = generate_proof_token( proofofwork = generate_proof_token(
**chat_requirements["proofofwork"], **chat_requirements["proofofwork"],
user_agent=cls._headers["user-agent"], user_agent=cls._headers.get("user-agent"),
proof_token=RequestConfig.proof_token proof_token=RequestConfig.proof_token
) )
[debug.log(text) for text in ( [debug.log(text) for text in (
@ -439,15 +439,15 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin):
messages = messages if conversation_id is None else [messages[-1]] messages = messages if conversation_id is None else [messages[-1]]
data["messages"] = cls.create_messages(messages, image_request) data["messages"] = cls.create_messages(messages, image_request)
headers = { headers = {
"Accept": "text/event-stream", **cls._headers,
"Content-Type": "application/json", "accept": "text/event-stream",
"Openai-Sentinel-Chat-Requirements-Token": chat_token, "content-type": "application/json",
**cls._headers "openai-sentinel-chat-requirements-token": chat_token,
} }
if RequestConfig.arkose_token: if RequestConfig.arkose_token:
headers["Openai-Sentinel-Arkose-Token"] = RequestConfig.arkose_token headers["openai-sentinel-arkose-token"] = RequestConfig.arkose_token
if proofofwork is not None: if proofofwork is not None:
headers["Openai-Sentinel-Proof-Token"] = proofofwork headers["openai-sentinel-proof-token"] = proofofwork
if need_turnstile and RequestConfig.turnstile_token is not None: if need_turnstile and RequestConfig.turnstile_token is not None:
headers['openai-sentinel-turnstile-token'] = RequestConfig.turnstile_token headers['openai-sentinel-turnstile-token'] = RequestConfig.turnstile_token
async with session.post( async with session.post(

View File

@ -63,26 +63,28 @@ def readHAR():
continue continue
for v in harFile['log']['entries']: for v in harFile['log']['entries']:
v_headers = get_headers(v) v_headers = get_headers(v)
try:
if "openai-sentinel-proof-token" in v_headers:
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:
RequestConfig.turnstile_token = v_headers["openai-sentinel-turnstile-token"]
except Exception as e:
debug.log(f"Read proof token: {e}")
if arkose_url == v['request']['url']: if arkose_url == v['request']['url']:
RequestConfig.arkose_request = parseHAREntry(v) RequestConfig.arkose_request = parseHAREntry(v)
elif v['request']['url'] == start_url or v['request']['url'].startswith(conversation_url): elif v['request']['url'].startswith(start_url):
try: try:
match = re.search(r'"accessToken":"(.*?)"', v["response"]["content"]["text"]) match = re.search(r'"accessToken":"(.*?)"', v["response"]["content"]["text"])
if match: if match:
RequestConfig.access_token = match.group(1) RequestConfig.access_token = match.group(1)
except KeyError: except KeyError:
continue pass
RequestConfig.cookies = {c['name']: c['value'] for c in v['request']['cookies']} try:
RequestConfig.headers = v_headers if "openai-sentinel-proof-token" in v_headers:
RequestConfig.headers = v_headers
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:
RequestConfig.turnstile_token = v_headers["openai-sentinel-turnstile-token"]
if "authorization" in v_headers:
RequestConfig.access_token = v_headers["authorization"].split(" ")[1]
RequestConfig.cookies = {c['name']: c['value'] for c in v['request']['cookies']}
except Exception as e:
debug.log(f"Error on read headers: {e}")
if RequestConfig.proof_token is None: if RequestConfig.proof_token is None:
raise NoValidHarFileError("No proof_token found in .har files") raise NoValidHarFileError("No proof_token found in .har files")