From 8add819c3e1f093466673f1d946d6ee78cc5baa3 Mon Sep 17 00:00:00 2001 From: abc <98614666+xtekky@users.noreply.github.com> Date: Sun, 14 Apr 2024 02:37:15 +0100 Subject: [PATCH] stytch auth udpdate for provider --- g4f/Provider/You.py | 8 ++-- g4f/Provider/you/har_file.py | 84 +++++++++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/g4f/Provider/You.py b/g4f/Provider/You.py index be4ab52309a..3ebd40f2483 100644 --- a/g4f/Provider/You.py +++ b/g4f/Provider/You.py @@ -65,6 +65,7 @@ async def create_async_generator( timeout=(30, timeout) ) as session: cookies = await cls.get_cookies(session) if chat_mode != "default" else None + upload = json.dumps([await cls.upload_file(session, cookies, to_bytes(image), image_name)]) if image else "" headers = { "Accept": "text/event-stream", @@ -131,6 +132,7 @@ async def upload_file(cls, client: StreamSession, cookies: Cookies, file: bytes, @classmethod async def get_cookies(cls, client: StreamSession) -> Cookies: + if not cls._cookies or cls._cookies_used >= 5: cls._cookies = await cls.create_cookies(client) cls._cookies_used = 0 @@ -151,8 +153,8 @@ def get_sdk(cls) -> str: }}).encode()).decode() def get_auth() -> str: - auth_uuid = "507a52ad-7e69-496b-aee0-1c9863c7c8" - auth_token = f"public-token-live-{auth_uuid}bb:public-token-live-{auth_uuid}19" + auth_uuid = "507a52ad-7e69-496b-aee0-1c9863c7c819" + auth_token = f"public-token-live-{auth_uuid}:public-token-live-{auth_uuid}" auth = base64.standard_b64encode(auth_token.encode()).decode() return f"Basic {auth}" @@ -172,12 +174,12 @@ async def create_cookies(cls, client: StreamSession) -> Cookies: "dfp_telemetry_id": await get_dfp_telemetry_id(), "email": f"{user_uuid}@gmail.com", "password": f"{user_uuid}#{user_uuid}", - "dfp_telemetry_id": f"{uuid.uuid4()}", "session_duration_minutes": 129600 } ) as response: await raise_for_status(response) session = (await response.json())["data"] + return { "stytch_session": session["session_token"], 'stytch_session_jwt': session["session_jwt"], diff --git a/g4f/Provider/you/har_file.py b/g4f/Provider/you/har_file.py index 281f37e2378..2bb59840f35 100644 --- a/g4f/Provider/you/har_file.py +++ b/g4f/Provider/you/har_file.py @@ -4,6 +4,8 @@ import os import random import uuid +import asyncio +import requests from ...requests import StreamSession, raise_for_status @@ -65,8 +67,86 @@ async def sendRequest(tmpArk: arkReq, proxy: str = None): return await response.text() async def get_dfp_telemetry_id(proxy: str = None): - return str(uuid.uuid4()) + return await telemetry_id_with_driver(proxy) global chatArks if chatArks is None: chatArks = readHAR() - return await sendRequest(random.choice(chatArks), proxy) \ No newline at end of file + return await sendRequest(random.choice(chatArks), proxy) + +async def telemetry_id_with_driver(proxy: str = None): + from ...debug import logging + if logging: + print('getting telemetry_id for you.com with nodriver') + try: + import nodriver as uc + from nodriver import start, cdp, loop + except ImportError: + if logging: + print('nodriver not found, random uuid (may fail)') + return str(uuid.uuid4()) + + CAN_EVAL = False + payload_received = False + payload = None + + try: + browser = await start() + tab = browser.main_tab + + async def send_handler(event: cdp.network.RequestWillBeSent): + nonlocal CAN_EVAL, payload_received, payload + if 'telemetry.js' in event.request.url: + CAN_EVAL = True + if "/submit" in event.request.url: + payload = event.request.post_data + payload_received = True + + tab.add_handler(cdp.network.RequestWillBeSent, send_handler) + await browser.get("https://you.com") + + while not CAN_EVAL: + await tab.sleep(1) + + await tab.evaluate('window.GetTelemetryID("public-token-live-507a52ad-7e69-496b-aee0-1c9863c7c819", "https://telemetry.stytch.com/submit");') + + while not payload_received: + await tab.sleep(.1) + + except Exception as e: + print(f"Error occurred: {str(e)}") + + finally: + try: + await tab.close() + except Exception as e: + print(f"Error occurred while closing tab: {str(e)}") + + try: + await browser.stop() + except Exception as e: + pass + + headers = { + 'Accept': '*/*', + 'Accept-Language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3', + 'Connection': 'keep-alive', + 'Content-type': 'application/x-www-form-urlencoded', + 'Origin': 'https://you.com', + 'Referer': 'https://you.com/', + 'Sec-Fetch-Dest': 'empty', + 'Sec-Fetch-Mode': 'cors', + 'Sec-Fetch-Site': 'cross-site', + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36', + 'sec-ch-ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"', + 'sec-ch-ua-mobile': '?0', + 'sec-ch-ua-platform': '"macOS"', + } + + proxies = { + 'http': proxy, + 'https': proxy} if proxy else None + + response = requests.post('https://telemetry.stytch.com/submit', + headers=headers, data=payload, proxies=proxies) + + return (response.text)