Skip to content

Commit

Permalink
Add proxy support for server mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sarperavci committed Jan 24, 2025
1 parent fef6b91 commit bfa7b85
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ python server.py

Two endpoints are available:

- `/cookies?url=<URL>&retries=<>`: This endpoint returns the cookies of the website (including the Cloudflare cookies).
- `/html?url=<URL>&retries=<>`: This endpoint returns the HTML content of the website.
- `/cookies?url=<URL>&retries=<>&proxy=<>`: This endpoint returns the cookies of the website (including the Cloudflare cookies).
- `/html?url=<URL>&retries=<>&proxy=<>`: This endpoint returns the HTML content of the website.

Send a GET request to the desired endpoint with the URL of the website you want to bypass the Cloudflare protection.

Expand Down
23 changes: 12 additions & 11 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def is_safe_url(url: str) -> bool:


# Function to bypass Cloudflare protection
def bypass_cloudflare(url: str, retries: int, log: bool) -> ChromiumPage:
def bypass_cloudflare(url: str, retries: int, log: bool, proxy: str = None) -> ChromiumPage:

if DOCKER_MODE:
options = ChromiumOptions()
Expand All @@ -72,9 +72,11 @@ def bypass_cloudflare(url: str, retries: int, log: bool) -> ChromiumPage:
options.set_paths(browser_path=browser_path).headless(False)
else:
options = ChromiumOptions()
options.set_argument("--auto-open-devtools-for-tabs", "true")
options.set_paths(browser_path=browser_path).headless(False)


if proxy:
options.set_proxy(proxy)

driver = ChromiumPage(addr_or_opts=options)
try:
driver.get(url)
Expand All @@ -88,12 +90,12 @@ def bypass_cloudflare(url: str, retries: int, log: bool) -> ChromiumPage:

# Endpoint to get cookies
@app.get("/cookies", response_model=CookieResponse)
async def get_cookies(url: str, retries: int = 5):
async def get_cookies(url: str, retries: int = 5, proxy: str = None):
if not is_safe_url(url):
raise HTTPException(status_code=400, detail="Invalid URL")
try:
driver = bypass_cloudflare(url, retries, log)
cookies = driver.cookies(as_dict=True)
driver = bypass_cloudflare(url, retries, log, proxy)
cookies = {cookie.get("name", ""): cookie.get("value", " ") for cookie in driver.cookies()}
user_agent = driver.user_agent
driver.quit()
return CookieResponse(cookies=cookies, user_agent=user_agent)
Expand All @@ -103,16 +105,15 @@ async def get_cookies(url: str, retries: int = 5):

# Endpoint to get HTML content and cookies
@app.get("/html")
async def get_html(url: str, retries: int = 5):
async def get_html(url: str, retries: int = 5, proxy: str = None):
if not is_safe_url(url):
raise HTTPException(status_code=400, detail="Invalid URL")
try:
driver = bypass_cloudflare(url, retries, log)
driver = bypass_cloudflare(url, retries, log, proxy)
html = driver.html
cookies_json = json.dumps(driver.cookies(as_dict=True))

cookies_json = {cookie.get("name", ""): cookie.get("value", " ") for cookie in driver.cookies()}
response = Response(content=html, media_type="text/html")
response.headers["cookies"] = cookies_json
response.headers["cookies"] = json.dumps(cookies_json)
response.headers["user_agent"] = driver.user_agent
driver.quit()
return response
Expand Down

0 comments on commit bfa7b85

Please sign in to comment.