Spaces:
Sleeping
Sleeping
Deploy commited on
Commit ·
b958c14
1
Parent(s): 6391701
Deploy proxy
Browse files- Dockerfile +4 -8
- app.py +64 -13
- requirements.txt +3 -1
Dockerfile
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
FROM python:3.
|
| 2 |
|
| 3 |
WORKDIR /code
|
| 4 |
|
|
@@ -6,11 +6,9 @@ COPY ./requirements.txt /code/requirements.txt
|
|
| 6 |
|
| 7 |
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 8 |
|
| 9 |
-
# Set up a new user named "user" with user ID 1000
|
| 10 |
RUN useradd -m -u 1000 user
|
| 11 |
-
# Switch to the "user" user
|
| 12 |
USER user
|
| 13 |
-
|
| 14 |
ENV HOME=/home/user \
|
| 15 |
PATH=/home/user/.local/bin:$PATH \
|
| 16 |
PYTHONPATH=$HOME/app \
|
|
@@ -20,11 +18,9 @@ ENV HOME=/home/user \
|
|
| 20 |
GRADIO_SERVER_NAME=0.0.0.0 \
|
| 21 |
GRADIO_THEME=huggingface \
|
| 22 |
SYSTEM=spaces
|
| 23 |
-
|
| 24 |
-
# Set the working directory to the user's home directory
|
| 25 |
WORKDIR $HOME/app
|
| 26 |
|
| 27 |
-
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
| 28 |
COPY --chown=user . $HOME/app
|
| 29 |
|
| 30 |
-
CMD ["python", "app.py"]
|
|
|
|
| 1 |
+
FROM python:3.12
|
| 2 |
|
| 3 |
WORKDIR /code
|
| 4 |
|
|
|
|
| 6 |
|
| 7 |
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 8 |
|
|
|
|
| 9 |
RUN useradd -m -u 1000 user
|
|
|
|
| 10 |
USER user
|
| 11 |
+
|
| 12 |
ENV HOME=/home/user \
|
| 13 |
PATH=/home/user/.local/bin:$PATH \
|
| 14 |
PYTHONPATH=$HOME/app \
|
|
|
|
| 18 |
GRADIO_SERVER_NAME=0.0.0.0 \
|
| 19 |
GRADIO_THEME=huggingface \
|
| 20 |
SYSTEM=spaces
|
| 21 |
+
|
|
|
|
| 22 |
WORKDIR $HOME/app
|
| 23 |
|
|
|
|
| 24 |
COPY --chown=user . $HOME/app
|
| 25 |
|
| 26 |
+
CMD ["python", "app.py"]
|
app.py
CHANGED
|
@@ -1,13 +1,64 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, Response
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import requests
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
@app.get("/")
|
| 9 |
+
async def root():
|
| 10 |
+
return {"status": "ok", "service": "HTTP Proxy Node"}
|
| 11 |
+
|
| 12 |
+
@app.post("/api/proxy")
|
| 13 |
+
async def proxy(request: Request):
|
| 14 |
+
"""标准 HTTP 代理 API"""
|
| 15 |
+
try:
|
| 16 |
+
# 解析请求
|
| 17 |
+
data = await request.json()
|
| 18 |
+
|
| 19 |
+
target_url = data.get("url")
|
| 20 |
+
method = data.get("method", "GET")
|
| 21 |
+
headers = data.get("headers", {})
|
| 22 |
+
body = data.get("body")
|
| 23 |
+
|
| 24 |
+
if not target_url:
|
| 25 |
+
return JSONResponse(
|
| 26 |
+
{"error": "Missing url parameter"},
|
| 27 |
+
status_code=400
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# 转换 body(如果是 base64)
|
| 31 |
+
if body and isinstance(body, str):
|
| 32 |
+
import base64
|
| 33 |
+
try:
|
| 34 |
+
body = base64.b64decode(body)
|
| 35 |
+
except:
|
| 36 |
+
body = body.encode('utf-8')
|
| 37 |
+
|
| 38 |
+
# 发送请求
|
| 39 |
+
resp = requests.request(
|
| 40 |
+
method=method,
|
| 41 |
+
url=target_url,
|
| 42 |
+
headers=headers,
|
| 43 |
+
data=body,
|
| 44 |
+
timeout=30,
|
| 45 |
+
allow_redirects=True
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# 返回响应
|
| 49 |
+
return Response(
|
| 50 |
+
content=resp.content,
|
| 51 |
+
status_code=resp.status_code,
|
| 52 |
+
headers=dict(resp.headers)
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return JSONResponse(
|
| 57 |
+
{"error": str(e)},
|
| 58 |
+
status_code=500
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
import uvicorn
|
| 63 |
+
port = int(os.environ.get("PORT", 7860))
|
| 64 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|
requirements.txt
CHANGED
|
@@ -1 +1,3 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
requests
|