Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- Dockerfile +13 -0
- app.py +54 -0
- favicon.ico +0 -0
- requirements.txt +3 -0
Dockerfile
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
RUN useradd -m -u 1000 user
|
4 |
+
USER user
|
5 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
6 |
+
|
7 |
+
WORKDIR /app
|
8 |
+
|
9 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
10 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
11 |
+
|
12 |
+
COPY --chown=user . /app
|
13 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import asyncio
|
2 |
+
import json
|
3 |
+
import urllib.parse
|
4 |
+
from fastapi.params import Header
|
5 |
+
from fastapi.responses import JSONResponse, PlainTextResponse
|
6 |
+
import requests
|
7 |
+
import fastapi
|
8 |
+
|
9 |
+
app = fastapi.FastAPI()
|
10 |
+
|
11 |
+
# Define the root endpoint
|
12 |
+
@app.get("/")
|
13 |
+
def read_root():
|
14 |
+
return {"status": "ok"}
|
15 |
+
|
16 |
+
# Define the proxy endpoint -> https://hostname/proxy?url=stream_url&headers=headers&output=json/text
|
17 |
+
@app.get("/proxy")
|
18 |
+
async def proxy(
|
19 |
+
url: str = fastapi.Query(..., description="The target URL to fetch"),
|
20 |
+
headers: str = fastapi.Query(..., description="URL-encoded JSON headers"),
|
21 |
+
output: str = fastapi.Query("text", description="Output format: json or text"),
|
22 |
+
x_app_id: str = Header(None, convert_underscores=False) # read "X-App-ID"
|
23 |
+
):
|
24 |
+
# Validate App ID
|
25 |
+
if x_app_id != "eu.org.nayankasturi.streamora":
|
26 |
+
return JSONResponse({"error": "Unauthorized app"}, status_code=403)
|
27 |
+
|
28 |
+
try:
|
29 |
+
url = urllib.parse.unquote(url)
|
30 |
+
decoded_headers = urllib.parse.unquote(headers)
|
31 |
+
final_headers = json.loads(decoded_headers)
|
32 |
+
response = await asyncio.to_thread(requests.get, url, headers=final_headers)
|
33 |
+
if response.status_code == 200:
|
34 |
+
if output.lower() == "json":
|
35 |
+
try:
|
36 |
+
return JSONResponse(response.json())
|
37 |
+
except json.JSONDecodeError:
|
38 |
+
return JSONResponse({"error": "Response is not valid JSON"}, status_code=502)
|
39 |
+
return PlainTextResponse(response.text)
|
40 |
+
else:
|
41 |
+
return JSONResponse(
|
42 |
+
{"error": f"{response.status_code} - {response.reason}"},
|
43 |
+
status_code=response.status_code
|
44 |
+
)
|
45 |
+
except json.JSONDecodeError:
|
46 |
+
return JSONResponse({"error": "Invalid JSON format for headers."}, status_code=400)
|
47 |
+
except requests.RequestException as e:
|
48 |
+
return JSONResponse({"error": str(e)}, status_code=502)
|
49 |
+
except Exception as e:
|
50 |
+
return JSONResponse({"error": str(e)}, status_code=500)
|
51 |
+
|
52 |
+
# if __name__ == "__main__":
|
53 |
+
# import uvicorn
|
54 |
+
# uvicorn.run(app, host="localhost", port=8000)
|
favicon.ico
ADDED
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn[standard]
|
3 |
+
requests
|