Last active
September 19, 2024 06:13
-
-
Save xshapira/3c6841f8d09d21f258fff2b86506cf65 to your computer and use it in GitHub Desktop.
cloaker api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import requests | |
from fastapi import FastAPI, Request | |
from fastapi.middleware.cors import CORSMiddleware | |
from fastapi.middleware.gzip import GZipMiddleware | |
from fastapi.responses import HTMLResponse, RedirectResponse | |
from fastapi.staticfiles import StaticFiles | |
from fastapi.templating import Jinja2Templates | |
logging.basicConfig(level=logging.INFO) | |
log = logging.getLogger(__name__) | |
app = FastAPI() | |
origins = [ | |
"http://0.0.0.0:8000", | |
"http://localhost:8000", | |
"https://wired-games.com", | |
] | |
app.mount("/static", StaticFiles(directory="static"), name="static") | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=origins, | |
allow_credentials=True, | |
allow_methods=["*"], | |
allow_headers=["*"], | |
) | |
app.add_middleware(GZipMiddleware) | |
templates = Jinja2Templates(directory="templates") | |
@app.get("/") | |
async def index(request: Request): | |
log.info("Handling root URL request") | |
return templates.TemplateResponse("base.html", {"request": request}) | |
@app.get("/analyze") | |
async def check(request: Request): | |
log.info(f"Request Method: {request.method}") | |
log.info(f"Request Headers: {request.headers}") | |
data = { | |
"companyId": "66e02e96e8aee41bd432ba46", | |
"referrerCF": request.query_params.get("referrerCF", ""), | |
"urlCF": request.query_params.get("urlCF", ""), | |
"QUERY_STRING": str(request.query_params), | |
"HTTP_REFERER": request.headers.get("referer", ""), | |
"HTTP_USER_AGENT": request.headers.get("user-agent", ""), | |
"REMOTE_ADDR": request.client.host, | |
"HTTP_CF_CONNECTING_IP": request.headers.get("cf-connecting-ip", ""), | |
"CF_CONNECTING_IP": request.headers.get("cf-connecting-ip", ""), | |
"X_FORWARDED_FOR": request.headers.get("x-forwarded-for", ""), | |
"TRUE_CLIENT_IP": request.headers.get("true-client-ip", ""), | |
} | |
response = requests.post("http://api.clofilter.com/v1/check", json=data) | |
api_response = response.json() | |
log.info(f"API Response: {api_response}") | |
if api_response["standartIntegration"]: | |
log.info("Standard Integration") | |
if api_response["type"] == "load": | |
log.info("Type: Load") | |
if api_response["redirectQuery"]: | |
log.info("Redirect Query: Yes") | |
return RedirectResponse(url=f"?{api_response['redirectQuery']}") | |
else: | |
log.info("Redirect Query: No") | |
if api_response["simplePage"].startswith("http"): | |
log.info("simplePage is a URL, redirecting") | |
return RedirectResponse( | |
url=api_response["simplePage"], status_code=302 | |
) | |
else: | |
log.info("simplePage is not a URL, returning HTML") | |
return HTMLResponse(content=api_response["simplePage"]) | |
elif api_response["type"] == "redirect": | |
log.info("Type: Redirect") | |
return RedirectResponse(url=api_response["pageWithParams"], status_code=302) | |
elif api_response["type"] == "iframe": | |
log.info("Type: Iframe") | |
iframe_html = f'<iframe src="{api_response["pageWithParams"]}" width="100%" height="100%" align="left"></iframe>' | |
iframe_style = "<style>body { padding: 0; margin: 0; } iframe { margin: 0; padding: 0; border: 0; }</style>" | |
return HTMLResponse(content=iframe_html + iframe_style) | |
elif api_response["pageType"] == "white": | |
log.info("Page Type: White") | |
return HTMLResponse(content="") | |
else: | |
log.info("Non-Standard Integration") | |
if api_response["type"] == "load": | |
log.info("Type: Load") | |
if api_response["redirectQuery"]: | |
log.info("Redirect Query: Yes") | |
refresh_html = f'<head><meta http-equiv="refresh" content="0; URL={api_response["originPage"]}?{api_response["redirectQuery"]}" /></head>' | |
return HTMLResponse(content=refresh_html) | |
else: | |
log.info("Redirect Query: No") | |
if api_response["simplePage"].startswith("http"): | |
log.info("simplePage is a URL, redirecting") | |
return RedirectResponse( | |
url=api_response["simplePage"], status_code=302 | |
) | |
else: | |
log.info("simplePage is not a URL, returning HTML") | |
return HTMLResponse(content=api_response["simplePage"]) | |
elif api_response["type"] == "redirect": | |
log.info("Type: Redirect") | |
return RedirectResponse(url=api_response["pageWithParams"], status_code=302) | |
elif api_response["type"] == "iframe": | |
log.info("Type: Iframe") | |
iframe_html = f'<iframe src="{api_response["pageWithParams"]}" width="100%" height="100%" align="left"></iframe>' | |
iframe_style = "<style>body { padding: 0; margin: 0; } iframe { margin: 0; padding: 0; border: 0; }</style>" | |
return HTMLResponse(content=iframe_html + iframe_style) | |
log.info("Default Case") | |
return HTMLResponse( | |
content=f'document.open();document.write(`{api_response["pageHTML"]}`);document.close();' | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment