2025-06-02 11:43:53 +08:00
|
|
|
import threading
|
|
|
|
import json
|
|
|
|
from flask import Flask, jsonify, request
|
|
|
|
import sqlite3
|
|
|
|
import socket
|
2025-06-06 19:10:31 +08:00
|
|
|
import secrets
|
|
|
|
import time
|
2025-06-13 20:19:55 +08:00
|
|
|
import os
|
|
|
|
|
2025-06-02 11:43:53 +08:00
|
|
|
app = Flask(__name__)
|
|
|
|
socket_server = socket.socket()
|
2025-06-02 14:27:37 +08:00
|
|
|
socket_server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
2025-06-02 14:48:33 +08:00
|
|
|
|
2025-06-13 20:19:55 +08:00
|
|
|
# 修改数据结构:使用用户名作为主键
|
|
|
|
active_connections = {} # {username: {'conn': conn, 'ip': ip}}
|
|
|
|
chat_connections = [] # 所有活跃连接列表
|
|
|
|
tokens = {} # 令牌管理
|
2025-06-02 11:52:02 +08:00
|
|
|
|
2025-06-02 11:43:53 +08:00
|
|
|
def get_db_connection():
|
|
|
|
conn = sqlite3.connect("usr.db")
|
|
|
|
conn.row_factory = sqlite3.Row
|
|
|
|
return conn
|
2025-06-02 11:52:02 +08:00
|
|
|
|
2025-06-02 11:43:53 +08:00
|
|
|
def isuserxist(name):
|
|
|
|
cn = get_db_connection()
|
|
|
|
csr = cn.cursor()
|
|
|
|
csr.execute('SELECT * FROM users WHERE name = ?', (name,))
|
|
|
|
rst = csr.fetchone()
|
|
|
|
cn.close()
|
2025-06-13 20:19:55 +08:00
|
|
|
return rst is not None
|
2025-06-02 11:43:53 +08:00
|
|
|
|
2025-06-13 20:19:55 +08:00
|
|
|
def ispsswdright(name, passwd):
|
2025-06-02 11:43:53 +08:00
|
|
|
cn = get_db_connection()
|
|
|
|
csr = cn.cursor()
|
2025-06-13 20:19:55 +08:00
|
|
|
csr.execute("SELECT passwd FROM users WHERE name = ?", (name,))
|
|
|
|
result = csr.fetchone()
|
|
|
|
cn.close()
|
|
|
|
return result and result[0] == passwd
|
|
|
|
|
|
|
|
def get_avatar(username):
|
|
|
|
cn = get_db_connection()
|
|
|
|
csr = cn.cursor()
|
|
|
|
csr.execute("SELECT avatar FROM users WHERE name = ?", (username,))
|
|
|
|
result = csr.fetchone()
|
|
|
|
cn.close()
|
|
|
|
return result[0] if result else "default_avatar.png"
|
2025-06-02 11:52:02 +08:00
|
|
|
|
2025-06-13 20:19:55 +08:00
|
|
|
def register_user(usr, pwd, avatar="default_avatar.png"):
|
2025-06-02 11:43:53 +08:00
|
|
|
conn = get_db_connection()
|
2025-06-13 20:19:55 +08:00
|
|
|
try:
|
|
|
|
cursor = conn.cursor()
|
|
|
|
# 添加avatar字段
|
|
|
|
cursor.execute("INSERT INTO users (name, passwd, avatar) VALUES (?, ?, ?)",
|
|
|
|
(usr, pwd, avatar))
|
|
|
|
conn.commit()
|
|
|
|
return {"type": "register_1", "success": True, "message": "User registered successfully"}
|
|
|
|
except sqlite3.IntegrityError:
|
2025-06-02 15:49:44 +08:00
|
|
|
return {"type": "register_0", "success": False, "message": "Username already exists"}
|
2025-06-13 20:19:55 +08:00
|
|
|
except sqlite3.Error as e:
|
|
|
|
return {"type": "register_0", "success": False, "message": str(e)}
|
|
|
|
finally:
|
|
|
|
conn.close()
|
2025-06-02 11:52:02 +08:00
|
|
|
|
2025-06-06 19:10:31 +08:00
|
|
|
def generate_token(username):
|
|
|
|
token = secrets.token_hex(16)
|
|
|
|
tokens[token] = {'username': username, 'timestamp': time.time()}
|
|
|
|
return token
|
|
|
|
|
|
|
|
def validate_token(token):
|
|
|
|
if token in tokens:
|
|
|
|
if time.time() - tokens[token]['timestamp'] < 3600:
|
|
|
|
tokens[token]['timestamp'] = time.time()
|
|
|
|
return tokens[token]['username']
|
|
|
|
return None
|
|
|
|
|
2025-06-02 14:48:33 +08:00
|
|
|
@app.route("/api/register", methods=['POST'])
|
|
|
|
def register1():
|
|
|
|
vl = request.get_json()
|
|
|
|
usr = vl.get('username')
|
|
|
|
pwd = vl.get('password')
|
2025-06-13 20:19:55 +08:00
|
|
|
avatar = vl.get('avatar', 'default_avatar.png') # 获取头像
|
|
|
|
|
|
|
|
# 头像验证
|
|
|
|
if avatar and not (avatar.endswith('.png') or avatar.endswith('.jpg')):
|
|
|
|
return jsonify({
|
|
|
|
"type": "register_0",
|
|
|
|
"success": False,
|
|
|
|
"message": "Invalid avatar format. Only .png or .jpg allowed"
|
|
|
|
}), 400
|
|
|
|
|
|
|
|
result = register_user(usr, pwd, avatar)
|
2025-06-02 14:48:33 +08:00
|
|
|
if result['success']:
|
|
|
|
return jsonify(result)
|
|
|
|
else:
|
|
|
|
return jsonify(result), 403 if result['message'] == "Username already exists" else 500
|
|
|
|
|
2025-06-06 19:10:31 +08:00
|
|
|
@app.route("/api/login", methods=['POST'])
|
|
|
|
def login():
|
|
|
|
data = request.get_json()
|
2025-06-13 20:19:55 +08:00
|
|
|
username = data['username']
|
|
|
|
|
|
|
|
# 检查账号是否已登录
|
|
|
|
if username in active_connections:
|
|
|
|
return jsonify({
|
|
|
|
"type": "login_0",
|
|
|
|
"status": "error",
|
|
|
|
"message": "Account already logged in"
|
|
|
|
}), 409 # 冲突状态码
|
|
|
|
|
|
|
|
if isuserxist(username) and ispsswdright(username, data['password']):
|
|
|
|
token = generate_token(username)
|
|
|
|
avatar = get_avatar(username)
|
|
|
|
return jsonify({
|
|
|
|
"type": "login_1",
|
|
|
|
"status": "success",
|
|
|
|
"token": token,
|
|
|
|
"avatar": avatar # 返回头像
|
|
|
|
})
|
|
|
|
return jsonify({
|
|
|
|
"type": "login_0",
|
|
|
|
"status": "error",
|
|
|
|
"message": "Invalid credentials"
|
|
|
|
}), 401
|
|
|
|
|
|
|
|
@app.route("/api/avatar/<username>", methods=['GET'])
|
|
|
|
def get_user_avatar(username):
|
|
|
|
avatar = get_avatar(username)
|
|
|
|
return jsonify({"username": username, "avatar": avatar})
|
2025-06-06 19:10:31 +08:00
|
|
|
|
|
|
|
@app.route("/api/chat", methods=['POST'])
|
|
|
|
def chat():
|
|
|
|
token = request.headers.get('Authorization')
|
|
|
|
username = validate_token(token)
|
|
|
|
if not username:
|
|
|
|
return jsonify({"type": "chat", "status": "error"}), 401
|
2025-06-13 20:19:55 +08:00
|
|
|
|
2025-06-06 19:10:31 +08:00
|
|
|
data = request.get_json()
|
|
|
|
message = {
|
|
|
|
"type": "chat",
|
|
|
|
"user": username,
|
2025-06-13 20:19:55 +08:00
|
|
|
"message": data['message'],
|
|
|
|
"avatar": get_avatar(username) # 添加头像信息
|
2025-06-06 19:10:31 +08:00
|
|
|
}
|
|
|
|
broadcast_message(message)
|
|
|
|
return jsonify({"type": "chat", "status": "success"})
|
|
|
|
|
2025-06-02 11:52:02 +08:00
|
|
|
def broadcast_message(message, sender=None):
|
|
|
|
for conn in chat_connections:
|
|
|
|
try:
|
|
|
|
conn.sendall(json.dumps(message).encode())
|
|
|
|
except:
|
2025-06-13 20:19:55 +08:00
|
|
|
# 连接异常时移除
|
|
|
|
for uname, info in list(active_connections.items()):
|
|
|
|
if info['conn'] == conn:
|
|
|
|
del active_connections[uname]
|
|
|
|
break
|
|
|
|
if conn in chat_connections:
|
|
|
|
chat_connections.remove(conn)
|
2025-06-02 11:52:02 +08:00
|
|
|
|
|
|
|
def handle_socket_message(data, addr, conn):
|
2025-06-02 11:43:53 +08:00
|
|
|
try:
|
|
|
|
action = data.get('type')
|
|
|
|
if action == 'register':
|
2025-06-13 20:19:55 +08:00
|
|
|
avatar = data.get('avatar', 'default_avatar.png')
|
|
|
|
result = register_user(data.get('username'), data.get('password'), avatar)
|
|
|
|
conn.sendall(json.dumps(result).encode())
|
|
|
|
return result
|
2025-06-02 15:49:44 +08:00
|
|
|
|
2025-06-02 11:43:53 +08:00
|
|
|
elif action == 'login':
|
2025-06-13 20:19:55 +08:00
|
|
|
username = data['username']
|
|
|
|
password = data['password']
|
|
|
|
|
|
|
|
# 检查账号是否已登录
|
|
|
|
if username in active_connections:
|
|
|
|
response = {
|
|
|
|
"type": "login_0",
|
|
|
|
"status": "error",
|
|
|
|
"message": "Account already logged in"
|
|
|
|
}
|
|
|
|
conn.sendall(json.dumps(response).encode())
|
|
|
|
return response
|
|
|
|
|
|
|
|
if isuserxist(username) and ispsswdright(username, password):
|
|
|
|
# 移除旧连接(如果存在)
|
|
|
|
if username in active_connections:
|
|
|
|
old_conn = active_connections[username]['conn']
|
|
|
|
if old_conn in chat_connections:
|
|
|
|
chat_connections.remove(old_conn)
|
|
|
|
del active_connections[username]
|
|
|
|
|
|
|
|
# 添加新连接
|
|
|
|
active_connections[username] = {'conn': conn, 'ip': addr[0]}
|
|
|
|
if conn not in chat_connections:
|
2025-06-02 11:52:02 +08:00
|
|
|
chat_connections.append(conn)
|
2025-06-13 20:19:55 +08:00
|
|
|
|
|
|
|
token = generate_token(username)
|
|
|
|
avatar = get_avatar(username)
|
|
|
|
response = {
|
|
|
|
"type": "login_1",
|
|
|
|
"status": "success",
|
|
|
|
"message": "Login successful",
|
|
|
|
"token": token,
|
|
|
|
"username": username,
|
|
|
|
"avatar": avatar # 返回头像
|
|
|
|
}
|
|
|
|
conn.sendall(json.dumps(response).encode())
|
|
|
|
return response
|
|
|
|
else:
|
|
|
|
response = {
|
|
|
|
"type": "login_0",
|
|
|
|
"status": "error",
|
|
|
|
"message": "Invalid credentials"
|
|
|
|
}
|
|
|
|
conn.sendall(json.dumps(response).encode())
|
|
|
|
return response
|
|
|
|
|
2025-06-02 11:52:02 +08:00
|
|
|
elif action == 'chat':
|
2025-06-13 20:19:55 +08:00
|
|
|
username = validate_token(data.get('token', ''))
|
|
|
|
if not username:
|
|
|
|
response = {
|
2025-06-02 11:52:02 +08:00
|
|
|
"type": "chat",
|
2025-06-13 20:19:55 +08:00
|
|
|
"status": "error",
|
|
|
|
"message": "Not authenticated"
|
2025-06-02 11:52:02 +08:00
|
|
|
}
|
2025-06-13 20:19:55 +08:00
|
|
|
conn.sendall(json.dumps(response).encode())
|
|
|
|
return response
|
|
|
|
|
|
|
|
if username not in active_connections:
|
|
|
|
response = {
|
|
|
|
"type": "chat",
|
|
|
|
"status": "error",
|
|
|
|
"message": "Not logged in"
|
|
|
|
}
|
|
|
|
conn.sendall(json.dumps(response).encode())
|
|
|
|
return response
|
|
|
|
|
|
|
|
message = {
|
|
|
|
"type": "chat",
|
|
|
|
"user": username,
|
|
|
|
"message": data['message'],
|
|
|
|
"avatar": get_avatar(username) # 添加头像
|
|
|
|
}
|
|
|
|
broadcast_message(message)
|
|
|
|
return {"type": "chat", "status": "success"}
|
|
|
|
|
2025-06-02 11:43:53 +08:00
|
|
|
except Exception as e:
|
|
|
|
return {"status": "error", "message": str(e)}
|
2025-06-02 11:52:02 +08:00
|
|
|
|
2025-06-02 11:43:53 +08:00
|
|
|
def run_socket_server():
|
2025-06-02 14:48:33 +08:00
|
|
|
socket_server.bind(("localhost", 8889))
|
2025-06-02 11:43:53 +08:00
|
|
|
socket_server.listen()
|
2025-06-02 14:48:33 +08:00
|
|
|
print("Socket server running on port 8889")
|
2025-06-02 11:43:53 +08:00
|
|
|
while True:
|
|
|
|
conn, addr = socket_server.accept()
|
|
|
|
print(f"Socket client connected: {addr}")
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
data = conn.recv(1024)
|
2025-06-02 11:52:02 +08:00
|
|
|
if not data:
|
|
|
|
break
|
2025-06-02 11:43:53 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
json_data = json.loads(data.decode())
|
2025-06-02 11:52:02 +08:00
|
|
|
response = handle_socket_message(json_data, addr, conn)
|
2025-06-02 11:43:53 +08:00
|
|
|
except json.JSONDecodeError:
|
2025-06-13 20:19:55 +08:00
|
|
|
response = {
|
|
|
|
"type": "error",
|
|
|
|
"status": "error",
|
|
|
|
"message": "Invalid JSON"
|
|
|
|
}
|
|
|
|
conn.sendall(json.dumps(response).encode())
|
|
|
|
except (ConnectionResetError, BrokenPipeError):
|
|
|
|
print(f"Client {addr} disconnected abruptly")
|
2025-06-02 11:43:53 +08:00
|
|
|
finally:
|
2025-06-13 20:19:55 +08:00
|
|
|
# 清理断开的连接
|
|
|
|
for username, info in list(active_connections.items()):
|
|
|
|
if info['conn'] == conn:
|
|
|
|
del active_connections[username]
|
|
|
|
print(f"User {username} disconnected")
|
|
|
|
break
|
|
|
|
|
2025-06-02 11:52:02 +08:00
|
|
|
if conn in chat_connections:
|
|
|
|
chat_connections.remove(conn)
|
2025-06-13 20:19:55 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
conn.close()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
print(f"Connection closed for {addr}")
|
2025-06-02 11:52:02 +08:00
|
|
|
|
2025-06-02 11:43:53 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
with get_db_connection() as conn:
|
2025-06-13 20:19:55 +08:00
|
|
|
# 添加avatar字段
|
2025-06-02 11:43:53 +08:00
|
|
|
conn.execute('''CREATE TABLE IF NOT EXISTS users
|
2025-06-13 20:19:55 +08:00
|
|
|
(name TEXT PRIMARY KEY,
|
|
|
|
passwd TEXT,
|
|
|
|
avatar TEXT)''')
|
|
|
|
threading.Thread(target=run_socket_server, daemon=True).start()
|
2025-06-02 14:27:37 +08:00
|
|
|
app.run(port=5001)
|