diff --git a/app.py b/app.py index f9a219a..ea2d963 100644 --- a/app.py +++ b/app.py @@ -1,8 +1,6 @@ from flask import Flask, render_template, request, jsonify, abort, redirect, url_for, session, flash from functools import wraps import os -import subprocess -import base64 import sqlite3 from werkzeug.security import generate_password_hash, check_password_hash @@ -18,7 +16,6 @@ SQUID_PASSWD_FILE = 'config/squid_passwd' def init_db(): with sqlite3.connect(DATABASE) as conn: cursor = conn.cursor() - # 创建admin_users表 cursor.execute(''' CREATE TABLE IF NOT EXISTS admin_users ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -26,8 +23,6 @@ def init_db(): password TEXT NOT NULL ) ''') - - # 创建squid_users表 cursor.execute(''' CREATE TABLE IF NOT EXISTS squid_users ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -36,8 +31,6 @@ def init_db(): is_active INTEGER DEFAULT 1 ) ''') - - # 创建settings表 cursor.execute(''' CREATE TABLE IF NOT EXISTS settings ( id INTEGER PRIMARY KEY DEFAULT 1, @@ -47,23 +40,21 @@ def init_db(): ) ''') - # 检查并初始化管理员用户 + # 检查是否有管理员用户 cursor.execute("SELECT COUNT(*) FROM admin_users") if cursor.fetchone()[0] == 0: cursor.execute( "INSERT INTO admin_users (username, password) VALUES (?, ?)", ('admin', generate_password_hash('admin123')) - ) - # 检查并初始化设置 - cursor.execute("SELECT COUNT(*) FROM settings") - if cursor.fetchone()[0] == 0: - cursor.execute( - "INSERT INTO settings (proxy_address, proxy_port) VALUES (?, ?)", - ('proxy.example.com', '3128') - ) + # 检查是否有设置 + cursor.execute("SELECT COUNT(*) FROM settings") + if cursor.fetchone()[0] == 0: + cursor.execute( + "INSERT INTO settings (proxy_address, proxy_port) VALUES (?, ?)", + ('proxy.example.com', '3128')) - conn.commit() + conn.commit() # 数据库连接 @@ -74,6 +65,18 @@ def get_db(): return db +# 更新squid_passwd文件 +def update_squid_passwd(): + db = get_db() + users = db.execute("SELECT * FROM squid_users").fetchall() + db.close() + + with open(SQUID_PASSWD_FILE, 'w') as f: + for user in users: + line = f"{'#' if not user['is_active'] else ''}{user['username']}:{user['password']}\n" + f.write(line) + + # 登录装饰器 def login_required(f): @wraps(f) @@ -167,6 +170,7 @@ def settings(): ) db.commit() flash('密码修改成功', 'success') + return redirect(url_for('settings')) elif action == 'update_proxy': proxy_address = request.form['proxy_address'] @@ -178,6 +182,7 @@ def settings(): ) db.commit() flash('代理设置更新成功', 'success') + return redirect(url_for('settings')) settings = db.execute("SELECT * FROM settings WHERE id = 1").fetchone() db.close() @@ -185,7 +190,7 @@ def settings(): return render_template('settings.html', settings=settings) -# Squid用户管理API +# API路由 @app.route('/api/toggle_user/', methods=['POST']) @login_required def toggle_user(user_id): @@ -199,8 +204,6 @@ def toggle_user(user_id): (new_status, user_id) ) db.commit() - - # 更新squid_passwd文件 update_squid_passwd() db.close() @@ -213,10 +216,7 @@ def delete_user(user_id): db = get_db() db.execute("DELETE FROM squid_users WHERE id = ?", (user_id,)) db.commit() - - # 更新squid_passwd文件 update_squid_passwd() - db.close() return jsonify({'success': True}) @@ -237,10 +237,7 @@ def create_user(): (username, password) ) db.commit() - - # 更新squid_passwd文件 update_squid_passwd() - return jsonify({'success': True}) except sqlite3.IntegrityError: return jsonify({'success': False, 'error': '用户名已存在'}), 400 @@ -263,25 +260,11 @@ def update_user_password(): (password, user_id) ) db.commit() - - # 更新squid_passwd文件 update_squid_passwd() - db.close() return jsonify({'success': True}) -def update_squid_passwd(): - db = get_db() - users = db.execute("SELECT * FROM squid_users").fetchall() - db.close() - - with open(SQUID_PASSWD_FILE, 'w') as f: - for user in users: - line = f"{'#' if not user['is_active'] else ''}{user['username']}:{user['password']}\n" - f.write(line) - - if __name__ == '__main__': if not os.path.exists('config'): os.makedirs('config') diff --git a/static/styles.css b/static/styles.css index fcbb51a..967f650 100644 --- a/static/styles.css +++ b/static/styles.css @@ -1,4 +1,52 @@ -/* 基础样式 */ +/* 新增导航栏样式 */ +.navbar { + background-color: #2c3e50; + overflow: hidden; + display: flex; + padding: 0 20px; +} + +.nav-item { + color: white; + text-align: center; + padding: 14px 16px; + text-decoration: none; + font-size: 16px; + transition: background-color 0.3s; +} + +.nav-item:hover { + background-color: #1a252f; +} + +.nav-item.active { + background-color: #3498db; +} + +.nav-item.right { + margin-left: auto; +} + +/* 修改密码提示样式 */ +.alert { + padding: 10px 15px; + margin: 10px 0; + border-radius: 4px; +} + +.alert-error { + background-color: #f8d7da; + color: #721c24; + border: 1px solid #f5c6cb; +} + +.alert-success { + background-color: #d4edda; + color: #155724; + border: 1px solid #c3e6cb; +} + +/* 其他样式保持不变 */ body { font-family: 'Microsoft YaHei', 'PingFang SC', sans-serif; margin: 0; @@ -9,7 +57,7 @@ body { .container { max-width: 1200px; - margin: 0 auto; + margin: 20px auto; padding: 20px; background-color: white; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); diff --git a/templates/clients.html b/templates/clients.html index 3763e52..a158021 100644 --- a/templates/clients.html +++ b/templates/clients.html @@ -6,15 +6,17 @@ + +

用户管理

-
- 返回首页 - 系统设置 - - 退出登录 -
+ @@ -49,10 +51,6 @@ {% endfor %}
- -
- Squid代理管理系统 © 2023 -