ZeroBlade's picture
Create app.py
094ca15 verified
from fastapi import FastAPI, Response
from fastapi.middleware.cors import CORSMiddleware
import httpx
import base64
import re
import uvicorn
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/proxy")
async def proxy(url: str = None):
if not url:
return Response(content="Error: missing ?url= parameter", status_code=400)
try:
async with httpx.AsyncClient(timeout=30.0) as client:
resp = await client.get(url)
resp.raise_for_status()
b64_content = resp.text.strip()
# Попробовать декодировать base64
try:
decoded = base64.b64decode(b64_content).decode('utf-8')
lines = decoded.split('\n')
except:
lines = b64_content.split('\n')
# Парсинг subscription-userinfo
info = resp.headers.get("subscription-userinfo", "")
upload, download, total, expire = 0, 0, 0, 0
match = re.search(r'upload=(\d+);\s*download=(\d+);\s*total=(\d+);\s*expire=(\d+)', info)
if match:
upload, download, total, expire = map(int, match.groups())
# Фильтрация конфигов
configs = [
line.strip() for line in lines
if re.match(r'^(ss|vless|vmess|trojan)://', line.strip())
]
# Формирование вывода
output_lines = [
"#profile-title: base64:TE9ORVIgU1VC",
"#profile-update-interval: 1",
"#support-url: https://t.me/loner_8",
"#profile-web-page-url: https://t.me/loner_8",
"#announce: base64:8J+UjCDQn9Cg0J7QkdCj0JnQotCVINCU0KDQo9CT0JjQlSDQodCV0KDQktCV0KDQkCDQldCh0JvQmCDQktCf0J0g0J3QlSDQoNCQ0JHQntCi0JDQldCiIPCfjJA=",
f"#subscription-userinfo: upload={upload}; download={download}; total={total}; expire={expire}",
*configs
]
output = "\n".join(output_lines)
encoded_output = base64.b64encode(output.encode('utf-8')).decode('utf-8')
return Response(
content=encoded_output,
media_type="text/plain",
headers={"Access-Control-Allow-Origin": "*"}
)
except Exception as e:
return Response(content=f"Error: {str(e)}", status_code=500)
@app.get("/")
async def root():
return {"status": "ok", "usage": "/proxy?url=YOUR_SUBSCRIPTION_URL"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860)