Skip to content

Commit

Permalink
linux headless
Browse files Browse the repository at this point in the history
  • Loading branch information
frederik-uni authored Aug 12, 2024
1 parent a12a0b6 commit 26f0310
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
24 changes: 20 additions & 4 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from fastapi import FastAPI, HTTPException, Response
from pydantic import BaseModel
from typing import Dict
import argparse

# Chromium options arguments
arguments = [
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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))

Expand All @@ -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)
3 changes: 2 additions & 1 deletion server_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fastapi
pydantic
uvicorn
uvicorn
PyVirtualDisplay

0 comments on commit 26f0310

Please sign in to comment.