-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
69 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
__version__ = "0.10.7" | ||
__version__ = "0.11.0" | ||
|
||
import re | ||
HEX_COLOR_REGEX = re.compile(r"^[A-Fa-f0-9]{6}$") |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright (c) 2024 iiPython | ||
|
||
# Modules | ||
import os | ||
import base64 | ||
import binascii | ||
|
||
from fastapi import Response | ||
from fastapi.responses import JSONResponse | ||
from requests import Session, RequestException | ||
|
||
from nightwatch.rics import app | ||
from nightwatch.logging import log | ||
|
||
# Exceptions | ||
class IllegalURL(Exception): | ||
pass | ||
|
||
# Handle image forwarding | ||
SESSION = Session() | ||
PROXY_SIZE_LIMIT = 10 * (1024 ** 2) | ||
|
||
FORWARD_DOMAIN = os.getenv("DOMAIN") | ||
if FORWARD_DOMAIN is None: | ||
log.warn("images", "DOMAIN environment variable not set! Image forwarding unprotected!") | ||
|
||
# Routing | ||
@app.get("/api/fwd/{public_url:str}", response_model = None) | ||
async def forward_image(public_url: str) -> Response | JSONResponse: | ||
try: | ||
new_url = f"https://{base64.b64decode(public_url, validate = True).decode('ascii').rstrip('/')}" | ||
if FORWARD_DOMAIN and FORWARD_DOMAIN in new_url: | ||
raise IllegalURL | ||
|
||
except (binascii.Error, UnicodeDecodeError): | ||
return JSONResponse({"code": 400, "message": "Failed to contact the specified URI."}, status_code = 400) | ||
|
||
except IllegalURL: | ||
return JSONResponse({"code": 400, "message": "Requested URL contains an illegal string!"}, status_code = 400) | ||
|
||
try: | ||
data = b"" | ||
with SESSION.get(new_url, stream = True) as response: | ||
response.raise_for_status() | ||
for chunk in response.iter_content(PROXY_SIZE_LIMIT): | ||
data += chunk | ||
if len(data) >= PROXY_SIZE_LIMIT: | ||
return JSONResponse({"code": 400, "message": "Specified URI contains data above size limit."}, status_code = 400) | ||
|
||
return Response( | ||
data, | ||
response.status_code, | ||
{ | ||
k: v | ||
for k, v in response.headers.items() if k in ["Content-Type", "Cache-Control"] | ||
} | ||
) | ||
|
||
except RequestException: | ||
return JSONResponse({"code": 400, "message": "Failed to contact the specified URI."}, status_code = 400) |
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