1
mirror of https://github.com/ThePhaseless/Byparr.git synced 2025-03-16 02:00:21 +08:00

134 lines
3.9 KiB
Python
Raw Normal View History

2024-07-24 13:57:40 +00:00
from __future__ import annotations
2024-09-13 18:03:38 +00:00
import logging
2024-07-24 13:57:40 +00:00
import time
2024-11-26 12:20:03 +00:00
from http import HTTPStatus
2024-07-24 13:57:40 +00:00
2024-11-28 12:33:04 +00:00
import uvicorn
2024-11-24 23:04:19 +00:00
from bs4 import BeautifulSoup
from fastapi import FastAPI, HTTPException
2024-07-24 13:57:40 +00:00
from fastapi.responses import RedirectResponse
2024-11-24 22:00:58 +00:00
from sbase import SB, BaseCase
2024-07-24 13:57:40 +00:00
2024-11-24 23:04:19 +00:00
import src
import src.utils
import src.utils.consts
2024-11-24 22:00:58 +00:00
from src.models.requests import LinkRequest, LinkResponse, Solution
2025-01-02 12:18:21 +00:00
from src.utils import logger
from src.utils.consts import LOG_LEVEL, USE_HEADLESS, USE_XVFB
2024-07-24 13:57:40 +00:00
2024-09-13 18:03:38 +00:00
app = FastAPI(debug=LOG_LEVEL == logging.DEBUG, log_level=LOG_LEVEL)
2024-07-24 13:57:40 +00:00
2024-12-12 23:45:24 +00:00
cookies = []
2024-07-24 13:57:40 +00:00
@app.get("/")
def read_root():
"""Redirect to /docs."""
2024-12-13 22:38:47 +00:00
logger.debug("Redirecting to /docs")
2024-07-24 13:57:40 +00:00
return RedirectResponse(url="/docs", status_code=301)
2024-10-21 13:34:07 +00:00
@app.get("/health")
async def health_check():
"""Health check endpoint."""
2024-11-25 10:19:09 +00:00
health_check_request = read_item(
LinkRequest.model_construct(url="https://prowlarr.servarr.com/v1/ping")
)
2024-12-13 22:38:47 +00:00
2024-11-26 12:20:03 +00:00
if health_check_request.solution.status != HTTPStatus.OK:
2024-11-25 10:19:09 +00:00
raise HTTPException(
status_code=500,
detail="Health check failed",
)
2024-10-21 13:34:07 +00:00
return {"status": "ok"}
2024-07-24 13:57:40 +00:00
@app.post("/v1")
2024-12-10 21:54:39 +00:00
def read_item(request: LinkRequest) -> LinkResponse:
2024-07-24 13:57:40 +00:00
"""Handle POST requests."""
2024-11-24 22:00:58 +00:00
start_time = int(time.time() * 1000)
2024-07-24 15:41:41 +00:00
# request.url = "https://nowsecure.nl"
2024-07-24 13:57:40 +00:00
logger.info(f"Request: {request}")
2025-01-02 12:17:43 +00:00
# Check is string is url
if not (request.url.startswith("http://") or request.url.startswith("https://")):
return LinkResponse.invalid(request.url)
2024-11-24 22:00:58 +00:00
response: LinkResponse
2024-09-13 18:48:31 +00:00
2024-11-24 22:00:58 +00:00
# start_time = int(time.time() * 1000)
2024-12-18 13:29:00 +00:00
with SB(
2025-01-02 12:18:21 +00:00
uc=True,
locale_code="en",
test=False,
ad_block=True,
xvfb=USE_XVFB,
headless=USE_HEADLESS,
2024-12-18 13:29:00 +00:00
) as sb:
2024-12-10 21:29:20 +00:00
try:
2024-11-25 10:19:09 +00:00
sb: BaseCase
2024-12-12 23:45:24 +00:00
global cookies # noqa: PLW0603
if cookies:
sb.uc_open_with_reconnect(request.url)
sb.add_cookies(cookies)
2024-11-25 10:19:09 +00:00
sb.uc_open_with_reconnect(request.url)
source = sb.get_page_source()
source_bs = BeautifulSoup(source, "html.parser")
title_tag = source_bs.title
2024-12-13 22:38:47 +00:00
logger.debug(f"Got webpage: {request.url}")
if title_tag and title_tag.string in src.utils.consts.CHALLENGE_TITLES:
2024-12-13 22:38:47 +00:00
logger.debug("Challenge detected")
2024-11-25 10:19:09 +00:00
sb.uc_gui_click_captcha()
logger.info("Clicked captcha")
source = sb.get_page_source()
source_bs = BeautifulSoup(source, "html.parser")
title_tag = source_bs.title
if title_tag and title_tag.string in src.utils.consts.CHALLENGE_TITLES:
sb.save_screenshot(f"./screenshots/{request.url}.png")
2024-11-28 12:33:04 +00:00
raise_captcha_bypass_error()
2024-11-25 10:19:09 +00:00
response = LinkResponse(
message="Success",
solution=Solution(
userAgent=sb.get_user_agent(),
url=sb.get_current_url(),
status=200,
cookies=sb.get_cookies(),
headers={},
response=source,
),
startTimestamp=start_time,
)
2024-12-12 23:45:24 +00:00
cookies = sb.get_cookies()
2024-12-10 21:29:20 +00:00
except Exception as e:
logger.error(f"Error: {e}")
if sb.driver:
sb.driver.quit()
raise HTTPException(
status_code=500, detail="Unknown error, check logs"
) from e
2024-09-13 18:48:31 +00:00
2024-07-24 13:57:40 +00:00
return response
2024-11-28 12:33:04 +00:00
def raise_captcha_bypass_error():
"""
Raise a 500 error if the challenge could not be bypassed.
This function should be called if the challenge is not bypassed after
clicking the captcha.
Returns:
None
"""
raise HTTPException(status_code=500, detail="Could not bypass challenge")
2024-07-24 13:57:40 +00:00
if __name__ == "__main__":
2024-07-25 00:06:56 +00:00
uvicorn.run(app, host="0.0.0.0", port=8191, log_level=LOG_LEVEL) # noqa: S104