Skip to content

Commit

Permalink
feat: add proxy-all
Browse files Browse the repository at this point in the history
  • Loading branch information
OhMyMndy committed Feb 13, 2025
1 parent 8075453 commit 6bc8598
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,12 @@ services:
- IMAGE_TAG=${IMAGE_TAG:?}
privileged: true
image: ohmymndy/virter:${IMAGE_TAG:?}

proxy-all:
build:
context: ./proxy-all
args:
- IMAGE_TAG=${IMAGE_TAG:?}
ports:
- 9901:5000
image: ohmymndy/proxy-all:${IMAGE_TAG:?}
9 changes: 9 additions & 0 deletions proxy-all/Dockerfile
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" ]
82 changes: 82 additions & 0 deletions proxy-all/src/app.py
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)
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3
0.4

0 comments on commit 6bc8598

Please sign in to comment.