File size: 2,624 Bytes
094ca15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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)