2024-04-07 12:22:35 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-04-07 11:36:13 +03:00
|
|
|
import json
|
|
|
|
import os
|
2024-04-21 08:38:36 +03:00
|
|
|
import os.path
|
2024-04-07 11:36:13 +03:00
|
|
|
import random
|
2024-05-15 01:45:13 +03:00
|
|
|
import logging
|
2024-04-07 11:36:13 +03:00
|
|
|
|
|
|
|
from ...requests import StreamSession, raise_for_status
|
2024-05-19 00:13:57 +03:00
|
|
|
from ...cookies import get_cookies_dir
|
2024-04-21 08:38:36 +03:00
|
|
|
from ...errors import MissingRequirementsError
|
2024-04-21 08:22:59 +03:00
|
|
|
from ... import debug
|
2024-04-07 11:36:13 +03:00
|
|
|
|
2024-05-15 01:45:13 +03:00
|
|
|
logging.basicConfig(level=logging.ERROR)
|
|
|
|
|
2024-04-07 11:36:13 +03:00
|
|
|
class NoValidHarFileError(Exception):
|
|
|
|
...
|
|
|
|
|
|
|
|
class arkReq:
|
|
|
|
def __init__(self, arkURL, arkHeaders, arkBody, arkCookies, userAgent):
|
|
|
|
self.arkURL = arkURL
|
|
|
|
self.arkHeaders = arkHeaders
|
|
|
|
self.arkBody = arkBody
|
|
|
|
self.arkCookies = arkCookies
|
|
|
|
self.userAgent = userAgent
|
|
|
|
|
2024-04-21 16:15:55 +03:00
|
|
|
telemetry_url = "https://telemetry.stytch.com/submit"
|
|
|
|
public_token = "public-token-live-507a52ad-7e69-496b-aee0-1c9863c7c819"
|
2024-04-07 11:36:13 +03:00
|
|
|
chatArks: list = None
|
|
|
|
|
|
|
|
def readHAR():
|
|
|
|
harPath = []
|
|
|
|
chatArks = []
|
2024-05-19 00:13:57 +03:00
|
|
|
for root, dirs, files in os.walk(get_cookies_dir()):
|
2024-04-07 11:36:13 +03:00
|
|
|
for file in files:
|
|
|
|
if file.endswith(".har"):
|
|
|
|
harPath.append(os.path.join(root, file))
|
|
|
|
if harPath:
|
|
|
|
break
|
|
|
|
if not harPath:
|
|
|
|
raise NoValidHarFileError("No .har file found")
|
|
|
|
for path in harPath:
|
|
|
|
with open(path, 'rb') as file:
|
|
|
|
try:
|
|
|
|
harFile = json.load(file)
|
|
|
|
except json.JSONDecodeError:
|
|
|
|
# Error: not a HAR file!
|
|
|
|
continue
|
|
|
|
for v in harFile['log']['entries']:
|
2024-04-21 16:15:55 +03:00
|
|
|
if v['request']['url'] == telemetry_url:
|
2024-04-07 11:36:13 +03:00
|
|
|
chatArks.append(parseHAREntry(v))
|
|
|
|
if not chatArks:
|
|
|
|
raise NoValidHarFileError("No telemetry in .har files found")
|
|
|
|
return chatArks
|
|
|
|
|
|
|
|
def parseHAREntry(entry) -> arkReq:
|
|
|
|
tmpArk = arkReq(
|
|
|
|
arkURL=entry['request']['url'],
|
|
|
|
arkHeaders={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(':')},
|
|
|
|
arkBody=entry['request']['postData']['text'],
|
|
|
|
arkCookies={c['name']: c['value'] for c in entry['request']['cookies']},
|
|
|
|
userAgent=""
|
|
|
|
)
|
|
|
|
tmpArk.userAgent = tmpArk.arkHeaders.get('user-agent', '')
|
|
|
|
return tmpArk
|
|
|
|
|
|
|
|
async def sendRequest(tmpArk: arkReq, proxy: str = None):
|
2024-05-19 00:13:57 +03:00
|
|
|
async with StreamSession(headers=tmpArk.arkHeaders, cookies=tmpArk.arkCookies, proxy=proxy) as session:
|
|
|
|
async with session.post(tmpArk.arkURL, data=tmpArk.arkBody) as response:
|
|
|
|
await raise_for_status(response)
|
|
|
|
return await response.text()
|
2024-04-07 11:36:13 +03:00
|
|
|
|
2024-04-21 16:15:55 +03:00
|
|
|
async def create_telemetry_id(proxy: str = None):
|
2024-04-07 11:36:13 +03:00
|
|
|
global chatArks
|
|
|
|
if chatArks is None:
|
|
|
|
chatArks = readHAR()
|
2024-04-14 04:37:15 +03:00
|
|
|
return await sendRequest(random.choice(chatArks), proxy)
|
|
|
|
|
2024-04-21 08:22:59 +03:00
|
|
|
async def get_telemetry_ids(proxy: str = None) -> list:
|
2024-04-21 16:15:55 +03:00
|
|
|
try:
|
|
|
|
return [await create_telemetry_id(proxy)]
|
|
|
|
except NoValidHarFileError as e:
|
|
|
|
if debug.logging:
|
2024-05-15 01:45:13 +03:00
|
|
|
logging.error(e)
|
2024-05-15 22:07:49 +03:00
|
|
|
|
2024-04-14 04:37:15 +03:00
|
|
|
try:
|
2024-04-21 08:22:59 +03:00
|
|
|
from nodriver import start
|
2024-04-14 04:37:15 +03:00
|
|
|
except ImportError:
|
2024-04-21 16:15:55 +03:00
|
|
|
raise MissingRequirementsError('Add .har file from you.com or install "nodriver" package | pip install -U nodriver')
|
2024-05-15 22:07:49 +03:00
|
|
|
if debug.logging:
|
|
|
|
logging.error('Getting telemetry_id for you.com with nodriver')
|
|
|
|
|
|
|
|
browser = page = None
|
2024-04-14 04:37:15 +03:00
|
|
|
try:
|
2024-05-15 22:07:49 +03:00
|
|
|
browser = await start(
|
|
|
|
browser_args=None if proxy is None else [f"--proxy-server={proxy}"],
|
|
|
|
)
|
2024-04-28 23:19:51 +03:00
|
|
|
page = await browser.get("https://you.com")
|
|
|
|
while not await page.evaluate('"GetTelemetryID" in this'):
|
|
|
|
await page.sleep(1)
|
2024-04-21 08:22:59 +03:00
|
|
|
async def get_telemetry_id():
|
2024-04-28 23:19:51 +03:00
|
|
|
return await page.evaluate(
|
2024-04-21 16:15:55 +03:00
|
|
|
f'this.GetTelemetryID("{public_token}", "{telemetry_url}");',
|
|
|
|
await_promise=True
|
|
|
|
)
|
2024-04-28 23:19:51 +03:00
|
|
|
return [await get_telemetry_id()]
|
2024-04-14 04:37:15 +03:00
|
|
|
finally:
|
2024-05-15 01:45:13 +03:00
|
|
|
try:
|
|
|
|
if page is not None:
|
|
|
|
await page.close()
|
|
|
|
if browser is not None:
|
2024-05-18 16:37:46 +03:00
|
|
|
await browser.stop()
|
2024-05-15 01:45:13 +03:00
|
|
|
except Exception as e:
|
|
|
|
if debug.logging:
|
|
|
|
logging.error(e)
|