Skip to content

Commit

Permalink
feat: default use chromium and test docker image
Browse files Browse the repository at this point in the history
Signed-off-by: vvanglro <vvanglro@gmail.com>
  • Loading branch information
vvanglro committed Jul 18, 2023
1 parent ddec4de commit 08f6b3f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 22 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ jobs:
with:
images: vvanglro/cf-clearance

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

- name: Build and push Docker image
uses: docker/build-push-action@v3
Expand All @@ -47,5 +47,5 @@ jobs:
file: ./docker/Dockerfile
push: true
platforms: linux/amd64
tags: vvanglro/cf-clearance:latest
tags: vvanglro/cf-clearance:latest-test
labels: ${{ steps.meta.outputs.labels }}
72 changes: 60 additions & 12 deletions docker/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ChallengeRequest(BaseModel):
timeout: int = Field(10)
url: str = Field(...)
pure: bool = Field(False)
browser: BrowserEnum = Field(BrowserEnum.firefox)
browser: BrowserEnum = Field(BrowserEnum.chromium)
cookies: List[SetCookieParam] = Field(None)
headers: Dict[str, str] = Field(None)
exec_js: str = Field(None)
Expand All @@ -59,7 +59,7 @@ class Config:
"timeout": 20,
"url": "https://nowsecure.nl",
"pure": True,
"browser": 2,
"browser": 1,
"cookies": [
{
"url": "https://www.example.com",
Expand All @@ -84,7 +84,7 @@ class ChallengeResponse(BaseModel):
exec_js_resp: str = Field(None)


async def pw_challenge(data: ChallengeRequest):
async def chromium(data: ChallengeRequest):
launch_data = {
"headless": False,
"proxy": {
Expand All @@ -108,21 +108,14 @@ async def pw_challenge(data: ChallengeRequest):
# https://github.com/microsoft/playwright/issues/6319
with Display():
async with async_playwright() as p:
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
browser = await p.chromium.launch(**launch_data)
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()
if data.browser != BrowserEnum.firefox:
await async_stealth(page, pure=data.pure)
await async_stealth(page, pure=data.pure)
resp = await page.goto(data.url)
success, cf = await async_cf_retry(page)
if not success:
Expand Down Expand Up @@ -150,6 +143,61 @@ async def pw_challenge(data: ChallengeRequest):
}


async def firefox(data: ChallengeRequest):
launch_data = {
"headless": True,
"proxy": {
"server": data.proxy.server,
"username": data.proxy.username,
"password": data.proxy.password,
}
if data.proxy
else None,
}
# Create a new context for each request:
# https://github.com/microsoft/playwright/issues/17736#issuecomment-1263667429
# https://github.com/microsoft/playwright/issues/6319
async with async_playwright() as p:
browser = await p.firefox.launch(**launch_data)
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()
resp = await page.goto(data.url)
success, cf = await async_cf_retry(page)
if not success:
await browser.close()
return {"success": success, "msg": "cf challenge fail"}
user_agent = await page.evaluate("() => navigator.userAgent")
cookies = {
cookie["name"]: cookie["value"] 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,
"cf": cf,
"user_agent": user_agent,
"cookies": cookies,
"msg": "cf challenge success",
"content": content,
"headers": resp.headers,
"exec_js_resp": exec_js_resp,
}


async def pw_challenge(data: ChallengeRequest):
if data.browser == BrowserEnum.chromium:
return await chromium(data)
elif data.browser == BrowserEnum.firefox:
return await firefox(data)


@app.post("/challenge", response_model=ChallengeResponse)
async def cf_challenge(data: ChallengeRequest):
try:
Expand Down

0 comments on commit 08f6b3f

Please sign in to comment.