增加新功能
This commit is contained in:
parent
048d4e20cf
commit
84e57bc8db
76
app.py
76
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/<username>', 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)
|
||||
5
config/config.json
Normal file
5
config/config.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"admin_password": "admin123",
|
||||
"proxy_address": "127.0.0.1",
|
||||
"proxy_port": "3128"
|
||||
}
|
||||
53
templates/index.html
Normal file
53
templates/index.html
Normal file
@ -0,0 +1,53 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Squid代理用户管理系统</title>
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>Squid代理用户管理系统</h1>
|
||||
|
||||
<div class="dashboard">
|
||||
<div class="dashboard-card">
|
||||
<h3>用户数量</h3>
|
||||
<p class="stat">{{ user_count }}</p>
|
||||
</div>
|
||||
|
||||
<div class="dashboard-card">
|
||||
<h3>代理地址</h3>
|
||||
<p class="stat">{{ proxy_address }}:{{ proxy_port }}</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="proxy-info">
|
||||
<h2>代理使用说明</h2>
|
||||
<p>1. 在浏览器或系统设置中配置代理服务器</p>
|
||||
<p>2. 地址: <code>{{ proxy_address }}</code> 端口: <code>{{ proxy_port }}</code></p>
|
||||
<p>3. 使用格式: <code>http://用户名:密码@{{ proxy_address }}:{{ proxy_port }}</code></p>
|
||||
<p>4. 或者在PAC文件中配置: <code>PROXY {{ proxy_address }}:{{ proxy_port }}</code></p>
|
||||
</div>
|
||||
|
||||
<div class="navigation">
|
||||
<a href="{{ url_for('clients') }}" class="btn-primary">用户管理</a>
|
||||
<a href="{{ url_for('settings') }}" class="btn-primary">系统设置</a>
|
||||
<button id="logoutBtn" class="btn-danger">退出系统</button>
|
||||
</div>
|
||||
|
||||
<footer>
|
||||
由 Flask 重构 - 基于原项目 <a href="https://github.com/ckazi" target="_blank">ckazi</a>
|
||||
</footer>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
document.getElementById('logoutBtn').addEventListener('click', function() {
|
||||
if(confirm('确定要退出系统吗?')) {
|
||||
fetch('/logout').then(() => {
|
||||
window.location.href = '/';
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
x
Reference in New Issue
Block a user