Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import urllib.parse
|
3 |
+
from flask import Flask, render_template, request, redirect, url_for, flash, jsonify
|
4 |
+
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
|
5 |
+
from werkzeug.security import check_password_hash, generate_password_hash
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
|
8 |
+
# 加载环境变量
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
app = Flask(__name__)
|
12 |
+
app.secret_key = os.environ.get('SECRET_KEY', 'starsky_secret_key')
|
13 |
+
|
14 |
+
# 登录管理配置
|
15 |
+
login_manager = LoginManager()
|
16 |
+
login_manager.init_app(app)
|
17 |
+
login_manager.login_view = 'login'
|
18 |
+
|
19 |
+
# 用户类
|
20 |
+
class User(UserMixin):
|
21 |
+
def __init__(self, id, username, password_hash):
|
22 |
+
self.id = id
|
23 |
+
self.username = username
|
24 |
+
self.password_hash = password_hash
|
25 |
+
|
26 |
+
# 默认用户
|
27 |
+
default_username = os.environ.get('ADMIN_USERNAME', 'admin')
|
28 |
+
default_password = os.environ.get('ADMIN_PASSWORD', 'admin123')
|
29 |
+
|
30 |
+
# 用户存储
|
31 |
+
users = {
|
32 |
+
default_username: User(
|
33 |
+
default_username,
|
34 |
+
default_username,
|
35 |
+
generate_password_hash(default_password)
|
36 |
+
)
|
37 |
+
}
|
38 |
+
|
39 |
+
@login_manager.user_loader
|
40 |
+
def load_user(user_id):
|
41 |
+
return users.get(user_id)
|
42 |
+
|
43 |
+
@app.route('/login', methods=['GET', 'POST'])
|
44 |
+
def login():
|
45 |
+
if request.method == 'POST':
|
46 |
+
username = request.form.get('username')
|
47 |
+
password = request.form.get('password')
|
48 |
+
|
49 |
+
user = users.get(username)
|
50 |
+
if user and check_password_hash(user.password_hash, password):
|
51 |
+
login_user(user)
|
52 |
+
return redirect(url_for('index'))
|
53 |
+
else:
|
54 |
+
flash('用户名或密码错误')
|
55 |
+
|
56 |
+
return render_template('login.html')
|
57 |
+
|
58 |
+
@app.route('/logout')
|
59 |
+
@login_required
|
60 |
+
def logout():
|
61 |
+
logout_user()
|
62 |
+
return redirect(url_for('login'))
|
63 |
+
|
64 |
+
@app.route('/')
|
65 |
+
@login_required
|
66 |
+
def index():
|
67 |
+
return render_template('index.html')
|
68 |
+
|
69 |
+
@app.route('/convert', methods=['POST'])
|
70 |
+
@login_required
|
71 |
+
def convert():
|
72 |
+
# 获取参数
|
73 |
+
backend_url = request.form.get('backend_url', 'https://raw.githubusercontent.com/yuanwangokk-1/subscribe/refs/heads/main/ACL4SSR/ACL4SSR.ini')
|
74 |
+
target = request.form.get('target', 'clash')
|
75 |
+
original_url = request.form.get('original_url', '')
|
76 |
+
|
77 |
+
if not original_url:
|
78 |
+
return jsonify({"status": "error", "message": "订阅链接不能为空"})
|
79 |
+
|
80 |
+
try:
|
81 |
+
encoded_url = urllib.parse.quote(original_url, safe="")
|
82 |
+
|
83 |
+
# 构建转换链接
|
84 |
+
template = (
|
85 |
+
"https://url.v1.mk/sub?"
|
86 |
+
f"target={target}&"
|
87 |
+
f"url={encoded_url}&"
|
88 |
+
"insert=false&"
|
89 |
+
f"config={urllib.parse.quote(backend_url, safe='')}&"
|
90 |
+
"emoji=true&list=false&xudp=false&"
|
91 |
+
"udp=false&tfo=false&expand=true&"
|
92 |
+
"scv=false&fdn=false&new_name=true"
|
93 |
+
)
|
94 |
+
|
95 |
+
return jsonify({"status": "success", "result": template})
|
96 |
+
|
97 |
+
except Exception as e:
|
98 |
+
return jsonify({"status": "error", "message": f"处理失败: {str(e)}"})
|
99 |
+
|
100 |
+
# 不要在这里调用 app.run()
|
101 |
+
# 让 Hugging Face 的 WSGI 服务器来运行应用
|
102 |
+
# 仅在本地开发时使用以下代码
|
103 |
+
if __name__ == '__main__' and os.environ.get('DEVELOPMENT') == 'true':
|
104 |
+
app.run(host='0.0.0.0', port=7860, debug=True)
|