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

112 lines
3.3 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
2024-07-24 13:57:40 +00:00
from src.utils import logger
2024-07-25 00:06:56 +00:00
from src.utils.consts import LOG_LEVEL
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
@app.get("/")
def read_root():
"""Redirect to /docs."""
logger.info("Redirecting to /docs")
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."""
logger.info("Health check")
2024-11-25 10:19:09 +00:00
health_check_request = read_item(
LinkRequest.model_construct(url="https://prowlarr.servarr.com/v1/ping")
)
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-11-24 22:00:58 +00:00
def read_item(request: LinkRequest):
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}")
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-11-25 10:19:09 +00:00
try:
with SB(uc=True, locale_code="en", test=False, xvfb=True, ad_block=True) as sb:
sb: BaseCase
sb.uc_open_with_reconnect(request.url)
source = sb.get_page_source()
source_bs = BeautifulSoup(source, "html.parser")
title_tag = source_bs.title
logger.info(f"Got webpage: {request.url}")
if title_tag and title_tag.string in src.utils.consts.CHALLENGE_TITLES:
2024-11-25 10:19:09 +00:00
logger.info("Challenge detected")
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,
)
except Exception as e:
logger.error(f"Error: {e}")
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