File size: 1,381 Bytes
b2ab624
 
 
28832ec
b2ab624
 
 
c746a96
 
28832ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c746a96
 
28832ec
 
c746a96
 
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
import os
import sys
import requests
from flask import Flask, Response, request
from urllib.parse import urlencode
from asgiref.wsgi import WsgiToAsgi

app = Flask(__name__)

# 从环境变量中获取密钥
API_KEY = os.environ.get('API_KEY')
SUBSCRIBE_URLS = os.environ.get('SUBSCRIBE_URLS')  # 存储真实URL的变量

@app.get("/getsub")
def read_subscribe():
    # 验证API Key
    key = request.args.get('key')
    if key != API_KEY:
        return {"error": "Unauthorized"}, 401

    # 从环境变量获取URL列表
    if not SUBSCRIBE_URLS:
        return {"error": "SUBSCRIBE_URLS not configured"}, 500
    
    urls = SUBSCRIBE_URLS.split('\n')
    cleaned_urls = [url.strip() for url in urls]
    new_url = '|'.join(cleaned_urls)
    encoded_url = urlencode({
        'target': 'clash',
        'url': new_url
    }) # Correct way to encode the URL
    
    target_url = f"http://127.0.0.1:25500/sub?{encoded_url}"
    
    try:
        resp = requests.get(target_url)
        resp.raise_for_status()  # Raise HTTPError for bad responses (4xx or 5xx)
        data = resp.text
        return Response(data, mimetype='text/yaml')
    except requests.exceptions.RequestException as e:
        return {"error": str(e)}, 500  # Handle request errors and return an error response


@app.get("/")
def read_root():
    return {"hello": 'world'}


asgi_app = WsgiToAsgi(app)