Spaces:
Paused
Paused
Upload 3 files
Browse files- Dockerfile +13 -0
- README.md +8 -0
- app.py +51 -0
Dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
RUN pip install --no-cache-dir fastapi uvicorn httpx
|
| 4 |
+
|
| 5 |
+
RUN useradd -m -u 1000 user
|
| 6 |
+
USER user
|
| 7 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 8 |
+
|
| 9 |
+
WORKDIR /app
|
| 10 |
+
COPY --chown=user . .
|
| 11 |
+
|
| 12 |
+
EXPOSE 7860
|
| 13 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: RC Proxy
|
| 3 |
+
emoji: 🔁
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
---
|
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import httpx
|
| 2 |
+
from fastapi import FastAPI, Request, Response
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from fastapi.responses import PlainTextResponse
|
| 5 |
+
|
| 6 |
+
TARGET = "https://rc.bwa.ad"
|
| 7 |
+
PLUGIN_URL = "https://bwa.ad/rc"
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
app.add_middleware(
|
| 12 |
+
CORSMiddleware,
|
| 13 |
+
allow_origins=["*"],
|
| 14 |
+
allow_methods=["*"],
|
| 15 |
+
allow_headers=["*"],
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
async def fetch(url, method="GET", headers=None, body=None):
|
| 19 |
+
async with httpx.AsyncClient(timeout=30, follow_redirects=True) as client:
|
| 20 |
+
return await client.request(method=method, url=url, headers=headers or {}, content=body)
|
| 21 |
+
|
| 22 |
+
@app.get("/plugin.js")
|
| 23 |
+
async def plugin(request: Request):
|
| 24 |
+
"""Отдаём модифицированный JS плагина с подменённым localhost"""
|
| 25 |
+
resp = await fetch(PLUGIN_URL)
|
| 26 |
+
js = resp.text
|
| 27 |
+
|
| 28 |
+
# Подменяем адрес сервера на наш прокси
|
| 29 |
+
hf_url = str(request.base_url).rstrip("/")
|
| 30 |
+
js = js.replace("https://rc.bwa.ad", hf_url)
|
| 31 |
+
js = js.replace("https://rc.bwa.ad/", hf_url + "/")
|
| 32 |
+
|
| 33 |
+
return PlainTextResponse(js, media_type="application/javascript")
|
| 34 |
+
|
| 35 |
+
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD"])
|
| 36 |
+
async def proxy(path: str, request: Request):
|
| 37 |
+
"""Проксируем всё остальное на rc.bwa.ad"""
|
| 38 |
+
url = f"{TARGET}/{path}"
|
| 39 |
+
if request.url.query:
|
| 40 |
+
url += f"?{request.url.query}"
|
| 41 |
+
|
| 42 |
+
headers = {k: v for k, v in request.headers.items() if k.lower() not in ("host", "content-length")}
|
| 43 |
+
headers["host"] = "rc.bwa.ad"
|
| 44 |
+
|
| 45 |
+
body = await request.body()
|
| 46 |
+
resp = await fetch(url, method=request.method, headers=headers, body=body)
|
| 47 |
+
|
| 48 |
+
excluded = {"transfer-encoding", "content-encoding"}
|
| 49 |
+
resp_headers = {k: v for k, v in resp.headers.items() if k.lower() not in excluded}
|
| 50 |
+
|
| 51 |
+
return Response(content=resp.content, status_code=resp.status_code, headers=resp_headers)
|