Skip to content

Commit

Permalink
feat: docker server support firefox and exec js (#52)
Browse files Browse the repository at this point in the history
* feat: docker server support firefox and exec js

Signed-off-by: vvanglro <[email protected]>

* feat: update pyproject.toml

Signed-off-by: vvanglro <[email protected]>

---------

Signed-off-by: vvanglro <[email protected]>
  • Loading branch information
vvanglro authored Jul 17, 2023
1 parent 174c272 commit 02d8594
Show file tree
Hide file tree
Showing 5 changed files with 522 additions and 198 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ jobs:
with:
images: vvanglro/cf-clearance

- name: Build and push Docker image
- name: Build and push Docker Arm image
uses: docker/build-push-action@v3
with:
context: .
file: ./docker/Dockerfile
file: ./docker/Dockerfile_Arm
push: true
platforms: linux/amd64
tags: vvanglro/cf-clearance:latest
platforms: linux/arm64
tags: vvanglro/cf-clearance:latest-arm64
labels: ${{ steps.meta.outputs.labels }}

- name: Build and push Docker Arm image
- name: Build and push Docker image
uses: docker/build-push-action@v3
with:
context: .
file: ./docker/Dockerfile_Arm
file: ./docker/Dockerfile
push: true
platforms: linux/arm64
tags: vvanglro/cf-clearance:latest-arm64
platforms: linux/amd64
tags: vvanglro/cf-clearance:latest
labels: ${{ steps.meta.outputs.labels }}
6 changes: 3 additions & 3 deletions docker/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fastapi==0.85.0
uvicorn==0.18.3
fastapi==0.99.1
uvicorn==0.23.0
pyvirtualdisplay==3.0
playwright>=1.34.0
playwright==1.36.0
.
29 changes: 25 additions & 4 deletions docker/server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import enum
from typing import Dict, List, Literal, Optional

from fastapi import FastAPI
Expand All @@ -12,6 +13,11 @@
app = FastAPI()


class BrowserEnum(enum.IntEnum):
chromium = 1
firefox = 2


class ProxySetting(BaseModel):
server: str = Field(...)
username: str = Field(
Expand Down Expand Up @@ -41,16 +47,19 @@ class ChallengeRequest(BaseModel):
timeout: int = Field(10)
url: str = Field(...)
pure: bool = Field(False)
browser: BrowserEnum = Field(BrowserEnum.firefox)
cookies: List[SetCookieParam] = Field(None)
headers: Dict[str, str] = Field(None)
exec_js: str = Field(None)

class Config:
schema_extra = {
"example": {
"proxy": {"server": "socks5://localhost:7890"},
"timeout": 20,
"url": "https://nowsecure.nl",
"pure": False,
"pure": True,
"browser": 2,
"cookies": [
{
"url": "https://www.example.com",
Expand All @@ -59,6 +68,7 @@ class Config:
}
],
"headers": {"example-ua": "example-ua-value"},
"exec_js": "() => {return navigator.userAgent}",
},
}

Expand All @@ -71,6 +81,7 @@ class ChallengeResponse(BaseModel):
cookies: dict = Field(None)
headers: dict = Field(None)
content: str = Field(None)
exec_js_resp: str = Field(None)


async def pw_challenge(data: ChallengeRequest):
Expand All @@ -89,7 +100,6 @@ async def pw_challenge(data: ChallengeRequest):
"--disable-dev-shm-usage",
"--no-first-run",
"--no-service-autorun",
"--no-default-browser-check",
"--password-store=basic",
],
}
Expand All @@ -98,14 +108,21 @@ async def pw_challenge(data: ChallengeRequest):
# https://github.com/microsoft/playwright/issues/6319
with Display():
async with async_playwright() as p:
browser = await p.chromium.launch(**launch_data)
browser = None
if data.browser == BrowserEnum.firefox:
launch_data.update({"headless": True})
browser = await p.firefox.launch(**launch_data)
elif data.browser == BrowserEnum.chromium:
browser = await p.chromium.launch(**launch_data)
assert browser
ctx = await browser.new_context()
if data.cookies:
await ctx.add_cookies(data.cookies)
if data.headers:
await ctx.set_extra_http_headers(data.headers)
page = await ctx.new_page()
await async_stealth(page, pure=data.pure)
if data.browser != BrowserEnum.firefox:
await async_stealth(page, pure=data.pure)
resp = await page.goto(data.url)
success, cf = await async_cf_retry(page)
if not success:
Expand All @@ -117,6 +134,9 @@ async def pw_challenge(data: ChallengeRequest):
for cookie in await page.context.cookies()
}
content = await page.content()
exec_js_resp = None
if data.exec_js:
exec_js_resp = await page.evaluate(data.exec_js)
await browser.close()
return {
"success": success,
Expand All @@ -126,6 +146,7 @@ async def pw_challenge(data: ChallengeRequest):
"msg": "cf challenge success",
"content": content,
"headers": resp.headers,
"exec_js_resp": exec_js_resp,
}


Expand Down
Loading

0 comments on commit 02d8594

Please sign in to comment.