diff --git a/server.py b/server.py index ab3a6d7..a6a6098 100644 --- a/server.py +++ b/server.py @@ -7,6 +7,7 @@ from fastapi import FastAPI, HTTPException, Response from pydantic import BaseModel from typing import Dict +import argparse # Chromium options arguments arguments = [ @@ -41,14 +42,14 @@ def is_safe_url(url: str) -> bool: return True # Function to bypass Cloudflare protection -def bypass_cloudflare(url: str, retries: int) -> ChromiumPage: +def bypass_cloudflare(url: str, retries: int, log: bool) -> ChromiumPage: options = ChromiumOptions() options.set_paths(browser_path=browser_path).headless(False) driver = ChromiumPage(addr_or_opts=options) try: driver.get(url) - cf_bypasser = CloudflareBypasser(driver, retries, True) + cf_bypasser = CloudflareBypasser(driver, retries, log) cf_bypasser.bypass() return driver except Exception as e: @@ -61,7 +62,7 @@ async def get_cookies(url: str, retries: int = 5): if not is_safe_url(url): raise HTTPException(status_code=400, detail="Invalid URL") try: - driver = bypass_cloudflare(url, retries) + driver = bypass_cloudflare(url, retries, log) cookies = driver.cookies(as_dict=True) driver.quit() return CookieResponse(cookies=cookies) @@ -74,7 +75,7 @@ async def get_html(url: str, retries: int = 5): if not is_safe_url(url): raise HTTPException(status_code=400, detail="Invalid URL") try: - driver = bypass_cloudflare(url, retries) + driver = bypass_cloudflare(url, retries, log) html = driver.html cookies_json = json.dumps(driver.cookies(as_dict=True)) @@ -87,5 +88,20 @@ async def get_html(url: str, retries: int = 5): # Main entry point if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Cloudflare bypass api") + + parser.add_argument('--nolog', action='store_true', help='Disable logging') + parser.add_argument('--headless', action='store_true', help='Run in headless mode') + + args = parser.parse_args() + if args.headless: + from pyvirtualdisplay import Display + + display = Display(visible=0, size=(1920, 1080)) + display.start() + if args.nolog: + log = False + else: + log = True import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000) diff --git a/server_requirements.txt b/server_requirements.txt index 6f745c3..5ce1279 100644 --- a/server_requirements.txt +++ b/server_requirements.txt @@ -1,3 +1,4 @@ fastapi pydantic -uvicorn \ No newline at end of file +uvicorn +PyVirtualDisplay