clash-sub-test / app.py
megatrump's picture
隐秘的转换订阅链接
28832ec
raw
history blame
1.38 kB
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)