Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,26 @@
|
|
| 1 |
-
from fastapi import FastAPI, Request
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
content_type
|
| 19 |
-
|
| 20 |
-
text =
|
| 21 |
-
text = text.replace('
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
return Response(content=f"Error fetching {target_url}: {str(e)}", status_code=500)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, Response
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
app = FastAPI()
|
| 5 |
+
|
| 6 |
+
@app.get("/")
|
| 7 |
+
async def proxy(request: Request):
|
| 8 |
+
target_url = request.query_params.get('url')
|
| 9 |
+
if not target_url:
|
| 10 |
+
return Response(content="Missing 'url' query parameter", status_code=400)
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
resp = requests.get(target_url, headers={'User-Agent': 'Mozilla/5.0'})
|
| 14 |
+
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
| 15 |
+
headers = {k: v for k, v in resp.headers.items() if k.lower() not in excluded_headers}
|
| 16 |
+
|
| 17 |
+
content_type = resp.headers.get('content-type')
|
| 18 |
+
if content_type and 'text/html' in content_type:
|
| 19 |
+
text = resp.text
|
| 20 |
+
text = text.replace('href="/', f'href="?url={target_url}/')
|
| 21 |
+
text = text.replace('src="/', f'src="?url={target_url}/')
|
| 22 |
+
return Response(content=text, headers=headers, status_code=resp.status_code)
|
| 23 |
+
|
| 24 |
+
return Response(content=resp.content, headers=headers, status_code=resp.status_code)
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return Response(content=f"Error fetching {target_url}: {str(e)}", status_code=500)
|
|
|