-
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
101 additions
and
1 deletion.
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
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,9 @@ | ||
FROM python:3.13.2 | ||
|
||
RUN pip install Flask requests beautifulsoup4 Pygments | ||
|
||
RUN mkdir -p /app | ||
COPY ./src /app | ||
|
||
USER nobody | ||
ENTRYPOINT [ "/app/app.py" ] |
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,82 @@ | ||
#!/usr/bin/env python | ||
|
||
from flask import Flask, request, Response | ||
import requests | ||
import urllib.parse | ||
import logging | ||
from bs4 import BeautifulSoup | ||
from pygments.lexers import guess_lexer | ||
|
||
app = Flask(__name__) | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
ALLOWED_DOMAINS = [] | ||
|
||
|
||
def is_allowed(url): | ||
return True | ||
|
||
|
||
def prepend_base_url(base_url, target_base_url, url): | ||
if url.startswith(("http://", "https://")): | ||
return base_url + "?q=" + urllib.parse.quote_plus(url) | ||
else: | ||
return ( | ||
base_url | ||
+ "?q=" | ||
+ urllib.parse.quote_plus(target_base_url + url.lstrip("/")) | ||
) | ||
|
||
|
||
@app.route("/", methods=["GET"]) | ||
def proxy(): | ||
target_url = request.args.get("q") | ||
if not target_url: | ||
return Response("Missing 'q' query parameter.", status=400) | ||
|
||
if not is_allowed(target_url): | ||
return Response("URL not allowed.", status=403) | ||
|
||
try: | ||
headers = {key: value for key, value in request.headers if key != "Host"} | ||
headers = {} | ||
resp = requests.get( | ||
target_url, headers=headers, stream=True, timeout=10, allow_redirects=True | ||
) | ||
|
||
if resp.status_code != 200: | ||
return resp | ||
|
||
excluded_headers = ["content-encoding", "transfer-encoding", "connection"] | ||
headers = [ | ||
(name, value) | ||
for (name, value) in resp.raw.headers.items() | ||
if name.lower() not in excluded_headers | ||
] | ||
|
||
logging.info(f"Proxied URL: {target_url} | Status: {resp.status_code}") | ||
|
||
soup = BeautifulSoup(resp.content, "html.parser") | ||
language = guess_lexer(resp.content).name | ||
|
||
content = resp.content | ||
if bool(soup.find()) and language in ["XML", "HTML"]: | ||
for tag in soup.find_all(href=True): | ||
tag["href"] = prepend_base_url( | ||
request.base_url, target_url, tag["href"] | ||
) | ||
|
||
for tag in soup.find_all(src=True): | ||
tag["src"] = prepend_base_url(request.base_url, target_url, tag["src"]) | ||
|
||
content = soup.encode("utf8") | ||
|
||
return Response(content, resp.status_code, headers) | ||
except requests.exceptions.RequestException as e: | ||
logging.error(f"Error fetching {target_url}: {e}") | ||
return Response(f"Error fetching the URL: {e}", status=500) | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(host="0.0.0.0", port=5000) |
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 +1 @@ | ||
0.3 | ||
0.4 |