From 9ca2059b91db6b518a94178f8a9ed59bcd7547f4 Mon Sep 17 00:00:00 2001 From: Thephaseless Date: Mon, 21 Oct 2024 13:12:09 +0000 Subject: [PATCH] fix addon passing all websites --- main.py | 7 ++++++- src/utils/browser.py | 22 ++++++++++++---------- src/utils/consts.py | 2 ++ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/main.py b/main.py index bf56c61..e7d8e1b 100644 --- a/main.py +++ b/main.py @@ -31,6 +31,7 @@ async def read_item(request: LinkRequest): logger.info(f"Request: {request}") start_time = int(time.time() * 1000) browser = await new_browser() + await browser.grant_all_permissions() await asyncio.sleep(1) page = await browser.get(request.url) await page.bring_to_front() @@ -39,8 +40,12 @@ async def read_item(request: LinkRequest): timeout = None try: challenged = await asyncio.wait_for(bypass_cloudflare(page), timeout=timeout) - except asyncio.TimeoutError: + except asyncio.TimeoutError as e: logger.info("Timed out bypassing Cloudflare") + raise HTTPException( + detail="Timed out bypassing Cloudflare", status_code=408 + ) from e + browser.stop() except Exception as e: browser.stop() raise HTTPException(detail="Couldn't bypass", status_code=408) from e diff --git a/src/utils/browser.py b/src/utils/browser.py index 5468ddf..fbdb94b 100644 --- a/src/utils/browser.py +++ b/src/utils/browser.py @@ -4,7 +4,7 @@ import nodriver as webdriver from nodriver.core.element import Element from src.utils import logger -from src.utils.consts import CHALLENGE_TITLES +from src.utils.consts import CHALLENGE_TITLES, UBLOCK_TITLE from src.utils.extentions import download_extentions downloaded_extentions = download_extentions() @@ -58,11 +58,13 @@ async def bypass_cloudflare(page: webdriver.Tab): """ challenged = False + await page while True: - await asyncio.sleep(1) logger.debug(f"Current page: {page.target.title}") if page.target.title not in CHALLENGE_TITLES: + if page.target.title == UBLOCK_TITLE: + continue return challenged if not challenged: @@ -78,12 +80,12 @@ async def bypass_cloudflare(page: webdriver.Tab): loaded = False try: - elem = await page.find("lds-ring") - except asyncio.TimeoutError as e: + elem = await page.find("lds-ring", timeout=3) + except asyncio.TimeoutError: logger.error( "Couldn't find lds-ring, probably not a cloudflare challenge, trying again..." ) - raise InvalidElementError from e + continue if elem is None: logger.error("elem is None") logger.debug(elem) @@ -115,11 +117,11 @@ async def bypass_cloudflare(page: webdriver.Tab): logger.debug("Clicking element") await inner_elem.mouse_click() await asyncio.sleep(3) - else: - logger.warning( - "Couldn't find element containing shadow root, trying again..." - ) - logger.debug(inner_elem) + continue + logger.warning( + "Couldn't find element containing shadow root, trying again..." + ) + logger.debug(inner_elem) else: logger.warning("Coulnd't find checkbox, trying again...") logger.debug(elem) diff --git a/src/utils/consts.py b/src/utils/consts.py index 2d9cf23..21bd813 100644 --- a/src/utils/consts.py +++ b/src/utils/consts.py @@ -5,6 +5,8 @@ from pathlib import Path LOG_LEVEL = os.getenv("LOG_LEVEL") or "INFO" LOG_LEVEL = logging.getLevelNamesMapping()[LOG_LEVEL.upper()] +UBLOCK_TITLE = "uBO Lite — Dashboard" + CHALLENGE_TITLES = [ # Cloudflare "Just a moment...",