diff --git a/app.py b/app.py index d050648..d6993af 100644 --- a/app.py +++ b/app.py @@ -1,14 +1,40 @@ -from flask import Flask, render_template, request, jsonify, abort, redirect, url_for +from flask import Flask, render_template, request, jsonify, abort, redirect, url_for, session from functools import wraps import os import subprocess -import base64 +import json +import uuid app = Flask(__name__) +app.secret_key = str(uuid.uuid4()) -# 配置 +# 配置文件路径 +CONFIG_FILE = 'config/config.json' SQUID_PASSWD_FILE = 'config/squid_passwd' -ADMIN_PASSWORD = os.getenv('SQUID_PASSWORD', 'admin123') + + +# 加载配置 +def load_config(): + try: + with open(CONFIG_FILE, 'r') as f: + return json.load(f) + except (FileNotFoundError, json.JSONDecodeError): + # 默认配置 + default_config = { + "admin_password": "admin123", + "proxy_address": "127.0.0.1", + "proxy_port": "3128" + } + os.makedirs('config', exist_ok=True) + with open(CONFIG_FILE, 'w') as f: + json.dump(default_config, f, indent=4) + return default_config + + +# 保存配置 +def save_config(config): + with open(CONFIG_FILE, 'w') as f: + json.dump(config, f, indent=4) class User: @@ -21,8 +47,9 @@ class User: def basic_auth_required(f): @wraps(f) def decorated(*args, **kwargs): + config = load_config() auth = request.authorization - if not auth or not (auth.username == 'admin' and auth.password == ADMIN_PASSWORD): + if not auth or not (auth.username == 'admin' and auth.password == config['admin_password']): return ('Unauthorized', 401, {'WWW-Authenticate': 'Basic realm="Authorization Required"'}) return f(*args, **kwargs) @@ -73,10 +100,47 @@ def create_user(username, password): @app.route('/') @basic_auth_required def index(): + config = load_config() + users = read_squid_file() + return render_template('index.html', + user_count=len(users), + proxy_address=config['proxy_address'], + proxy_port=config['proxy_port']) + + +@app.route('/clients') +@basic_auth_required +def clients(): users = read_squid_file() return render_template('clients.html', users=users) +@app.route('/settings') +@basic_auth_required +def settings(): + config = load_config() + return render_template('settings.html', config=config) + + +@app.route('/update_settings', methods=['POST']) +@basic_auth_required +def update_settings(): + config = load_config() + new_password = request.form.get('admin_password') + proxy_address = request.form.get('proxy_address') + proxy_port = request.form.get('proxy_port') + + if new_password: + config['admin_password'] = new_password + if proxy_address: + config['proxy_address'] = proxy_address + if proxy_port: + config['proxy_port'] = proxy_port + + save_config(config) + return redirect(url_for('settings')) + + @app.route('/toggle/', methods=['POST']) @basic_auth_required def toggle_user(username): @@ -85,7 +149,6 @@ def toggle_user(username): if user.name == username: user.is_active = not user.is_active break - write_squid_file(users) return '', 200 @@ -139,4 +202,5 @@ def logout(): if __name__ == '__main__': if not os.path.exists('config'): os.makedirs('config') + load_config() # 初始化配置文件 app.run(host='0.0.0.0', port=8080, debug=True) \ No newline at end of file diff --git a/config/config.json b/config/config.json new file mode 100644 index 0000000..b23cb5a --- /dev/null +++ b/config/config.json @@ -0,0 +1,5 @@ +{ + "admin_password": "admin123", + "proxy_address": "127.0.0.1", + "proxy_port": "3128" +} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..583dbda --- /dev/null +++ b/templates/index.html @@ -0,0 +1,53 @@ + + + + + Squid代理用户管理系统 + + + +
+

Squid代理用户管理系统

+ +
+
+

用户数量

+

{{ user_count }}

+
+ +
+

代理地址

+

{{ proxy_address }}:{{ proxy_port }}

+
+
+ +
+

代理使用说明

+

1. 在浏览器或系统设置中配置代理服务器

+

2. 地址: {{ proxy_address }} 端口: {{ proxy_port }}

+

3. 使用格式: http://用户名:密码@{{ proxy_address }}:{{ proxy_port }}

+

4. 或者在PAC文件中配置: PROXY {{ proxy_address }}:{{ proxy_port }}

+
+ + + +
+ 由 Flask 重构 - 基于原项目 ckazi +
+
+ + + + \ No newline at end of file