Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Response
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
import httpx
|
| 4 |
+
import base64
|
| 5 |
+
import re
|
| 6 |
+
import uvicorn
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
app.add_middleware(
|
| 11 |
+
CORSMiddleware,
|
| 12 |
+
allow_origins=["*"],
|
| 13 |
+
allow_methods=["*"],
|
| 14 |
+
allow_headers=["*"],
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
@app.get("/proxy")
|
| 18 |
+
async def proxy(url: str = None):
|
| 19 |
+
if not url:
|
| 20 |
+
return Response(content="Error: missing ?url= parameter", status_code=400)
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
async with httpx.AsyncClient(timeout=30.0) as client:
|
| 24 |
+
resp = await client.get(url)
|
| 25 |
+
resp.raise_for_status()
|
| 26 |
+
|
| 27 |
+
b64_content = resp.text.strip()
|
| 28 |
+
|
| 29 |
+
# Попробовать декодировать base64
|
| 30 |
+
try:
|
| 31 |
+
decoded = base64.b64decode(b64_content).decode('utf-8')
|
| 32 |
+
lines = decoded.split('\n')
|
| 33 |
+
except:
|
| 34 |
+
lines = b64_content.split('\n')
|
| 35 |
+
|
| 36 |
+
# Парсинг subscription-userinfo
|
| 37 |
+
info = resp.headers.get("subscription-userinfo", "")
|
| 38 |
+
upload, download, total, expire = 0, 0, 0, 0
|
| 39 |
+
|
| 40 |
+
match = re.search(r'upload=(\d+);\s*download=(\d+);\s*total=(\d+);\s*expire=(\d+)', info)
|
| 41 |
+
if match:
|
| 42 |
+
upload, download, total, expire = map(int, match.groups())
|
| 43 |
+
|
| 44 |
+
# Фильтрация конфигов
|
| 45 |
+
configs = [
|
| 46 |
+
line.strip() for line in lines
|
| 47 |
+
if re.match(r'^(ss|vless|vmess|trojan)://', line.strip())
|
| 48 |
+
]
|
| 49 |
+
|
| 50 |
+
# Формирование вывода
|
| 51 |
+
output_lines = [
|
| 52 |
+
"#profile-title: base64:TE9ORVIgU1VC",
|
| 53 |
+
"#profile-update-interval: 1",
|
| 54 |
+
"#support-url: https://t.me/loner_8",
|
| 55 |
+
"#profile-web-page-url: https://t.me/loner_8",
|
| 56 |
+
"#announce: base64:8J+UjCDQn9Cg0J7QkdCj0JnQotCVINCU0KDQo9CT0JjQlSDQodCV0KDQktCV0KDQkCDQldCh0JvQmCDQktCf0J0g0J3QlSDQoNCQ0JHQntCi0JDQldCiIPCfjJA=",
|
| 57 |
+
f"#subscription-userinfo: upload={upload}; download={download}; total={total}; expire={expire}",
|
| 58 |
+
*configs
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
output = "\n".join(output_lines)
|
| 62 |
+
encoded_output = base64.b64encode(output.encode('utf-8')).decode('utf-8')
|
| 63 |
+
|
| 64 |
+
return Response(
|
| 65 |
+
content=encoded_output,
|
| 66 |
+
media_type="text/plain",
|
| 67 |
+
headers={"Access-Control-Allow-Origin": "*"}
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
except Exception as e:
|
| 71 |
+
return Response(content=f"Error: {str(e)}", status_code=500)
|
| 72 |
+
|
| 73 |
+
@app.get("/")
|
| 74 |
+
async def root():
|
| 75 |
+
return {"status": "ok", "usage": "/proxy?url=YOUR_SUBSCRIPTION_URL"}
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|