-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTFserver.py
More file actions
674 lines (589 loc) · 25.5 KB
/
TFserver.py
File metadata and controls
674 lines (589 loc) · 25.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
TFserver - 新版无UI控制台服务端
基于server_ui.py的功能实现,采用chat.py的命令行形式
完全适配client_gui.py和client_lite.py客户端
"""
import socket
import threading
import json
import os
import sys
import datetime
import time
class TFServer:
def __init__(self, ip, port, max_connections):
self.ip = ip
self.port = port
self.max_connections = max_connections
self.original_max_connections = max_connections # 保存原始最大连接数
self.socket = None
self.conn = []
self.address = []
self.usernames = []
self.banned_ips = []
self.banned_ports = {} # 存储被封禁的IP和端口 {ip: [ports]}
self.server_running = False
self.start_time = time.time() # 记录服务器启动时间
# 尝试加载已封禁的IP和端口
self.load_banned_data()
def start(self):
"""启动服务器"""
try:
self.socket = socket.socket()
self.socket.bind((self.ip, self.port))
self.socket.listen(self.max_connections)
self.socket.setblocking(0)
self.server_running = True
print(f"\nTouchFish服务器已启动!")
print(f"监听地址: {self.ip}:{self.port}")
print(f"最大连接数: {self.max_connections}")
print("\n输入 'help' 查看所有可用命令")
print("按 Ctrl+C 或输入 'exit' 停止服务器\n")
# 启动线程
t1 = threading.Thread(target=self.accept_connections)
t2 = threading.Thread(target=self.receive_messages)
t1.daemon = True
t2.daemon = True
t1.start()
t2.start()
# 启动命令处理线程(非daemon,确保能正常处理命令)
t3 = threading.Thread(target=self.handle_commands)
t3.start()
# 保持主线程运行
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\n🛑 正在停止服务器...")
self.stop()
except Exception as e:
print(f"❌ 启动服务器失败: {e}")
def stop(self):
"""停止服务器"""
self.server_running = False
# 关闭所有连接
for conn in self.conn:
try:
conn.close()
except:
pass
self.conn.clear()
self.address.clear()
self.usernames.clear()
try:
self.socket.close()
except:
pass
print("✅ 服务器已停止")
def accept_connections(self):
"""接受客户端连接"""
while self.server_running:
try:
conn, addr = self.socket.accept()
# 检查IP是否被封禁
if addr[0] in self.banned_ips:
conn.send("您已被服务器封禁".encode("utf-8"))
conn.close()
continue
# 检查IP和端口是否被封禁
if addr[0] in self.banned_ports and addr[1] in self.banned_ports[addr[0]]:
conn.send("您已被服务器封禁".encode("utf-8"))
conn.close()
continue
conn.setblocking(0)
self.conn.append(conn)
self.address.append(addr)
self.usernames.append("")
print(f"[{self.get_timestamp()}] 🔗 新连接: {addr}")
except BlockingIOError:
# 正常的非阻塞socket行为,继续等待
continue
except Exception as e:
print(f"❌ [ERROR] accept_connections: {e}")
time.sleep(0.1)
continue
def receive_messages(self):
"""接收客户端消息"""
while self.server_running:
for i in range(len(self.conn)):
try:
data = self.conn[i].recv(1024).decode('UTF-8')
if not data:
continue
except BlockingIOError:
# 正常的非阻塞socket行为,继续等待
continue
except Exception as e:
print(f"❌ [ERROR] receive_messages (recv): {e}")
# 移除断开的连接
try:
self.conn[i].close()
except:
pass
if i < len(self.conn):
self.conn.pop(i)
self.address.pop(i)
self.usernames.pop(i)
continue
# 检查是否是用户名注册(客户端连接时发送用户名)
if i < len(self.usernames) and not self.usernames[i] and data.strip() and ":" not in data:
# 这是用户名注册
username = data.strip()
if username.lower() == "server":
self.conn[i].send("用户名'server'被保留,请使用其他用户名".encode("utf-8"))
else:
self.usernames[i] = username
print(f"[{self.get_timestamp()}] 👤 用户 {username} 已连接")
# 发送确认消息
confirmation = f"USERNAME_OK:{username}"
self.conn[i].send(confirmation.encode("utf-8"))
continue
# 解析用户名和消息
if ":" in data:
parts = data.split(":", 1) # 只分割第一个冒号
username = parts[0]
message_content = parts[1] if len(parts) > 1 else ""
# 检查用户名是否为server
if username.lower() == "server":
self.conn[i].send("用户名'server'被保留,请使用其他用户名".encode("utf-8"))
continue
# 更新用户名
if i < len(self.usernames):
self.usernames[i] = username
# 显示消息
print(f"[{self.get_timestamp()}] 💬 消息: {data.strip()}")
# 转发给其他客户端
for j in range(len(self.conn)):
if i != j: # 不转发给自己
try:
self.conn[j].send(data.encode("utf-8"))
except:
pass
else:
# 显示消息
print(f"[{self.get_timestamp()}] 💬 消息: {data.strip()}")
# 转发给其他客户端
for j in range(len(self.conn)):
if i != j: # 不转发给自己
try:
self.conn[j].send(data.encode("utf-8"))
except:
pass
def handle_commands(self):
"""处理控制台命令"""
while self.server_running:
try:
cmd = input().strip().lower()
if cmd == "help":
self.show_help()
elif cmd == "list":
self.list_connections()
elif cmd.startswith("ban "):
args = cmd[4:].strip()
if " " in args:
# ban ip port 格式
ip, port = args.split(" ", 1)
try:
port = int(port)
self.ban_port(ip, port)
except ValueError:
print("❌ 错误: 端口必须是整数")
else:
# ban ip 格式
self.ban_user(args)
elif cmd.startswith("unban "):
args = cmd[6:].strip()
if " " in args:
# unban ip port 格式
ip, port = args.split(" ", 1)
try:
port = int(port)
self.unban_port(ip, port)
except ValueError:
print("❌ 错误: 端口必须是整数")
else:
# unban ip 格式
self.unban_user(args)
elif cmd == "banned":
self.list_banned_ips()
elif cmd.startswith("msg "):
message = cmd[4:]
self.send_server_message(message)
elif cmd == "clear":
self.clear_banned()
elif cmd == "status":
self.show_status()
elif cmd == "exit" or cmd == "quit":
print("🛑 正在停止服务器...")
self.stop()
break
elif cmd.startswith("maxconn "):
args = cmd[8:].strip()
self.handle_maxconn_command(args)
elif cmd == "":
continue
else:
print(f"❌ 未知命令: {cmd}. 输入 'help' 查看可用命令")
except EOFError:
break
except KeyboardInterrupt:
break
except Exception as e:
print(f"❌ 命令处理错误: {e}")
def show_help(self):
"""显示帮助信息"""
print("\n=== TouchFish服务器命令帮助 ===")
print("\n基本命令:")
print(" help - 显示此帮助信息")
print(" list - 显示所有连接")
print(" status - 显示服务器状态")
print(" exit/quit - 停止服务器")
print("\n消息命令:")
print(" msg <text> - 发送服务器消息给所有客户端")
print("\n封禁管理:")
print(" ban <ip> - 封禁指定IP的所有连接")
print(" ban <ip> <port> - 封禁指定IP的指定端口")
print(" unban <ip> - 解封指定IP")
print(" unban <ip> <port> - 解封指定IP的指定端口")
print(" banned - 显示被封禁的IP和端口列表")
print(" clear - 清除所有封禁记录")
print("\n连接数控制:")
print(" maxconn <number> - 设置最大连接数")
print(" maxconn show - 显示当前最大连接数")
print(" maxconn reset - 重置为初始最大连接数")
print("\n示例:")
print(" ban 192.168.1.100")
print(" ban 192.168.1.100 8080")
print(" msg 欢迎使用TouchFish聊天室!")
print("\n=================================\n")
def list_connections(self):
"""显示所有连接"""
print("\n=== 当前连接列表 ===")
if not self.address:
print("当前没有活跃连接")
else:
print(f"总连接数: {len(self.address)}")
for i, addr in enumerate(self.address):
username = self.usernames[i] if i < len(self.usernames) else "未注册"
print(f" {i+1}. {addr[0]}:{addr[1]} - 用户: {username}")
print("===================\n")
def ban_user(self, ip):
"""封禁用户IP"""
if not ip:
print("❌ 错误: 请指定要封禁的IP地址")
return
if ip not in self.banned_ips:
self.banned_ips.append(ip)
self.save_banned_ips()
print(f"✅ 已成功封禁IP: {ip}")
# 断开该IP的所有连接
disconnected_count = 0
for i, addr in enumerate(self.address):
if addr[0] == ip:
try:
self.conn[i].send("您已被服务器封禁".encode("utf-8"))
self.conn[i].close()
disconnected_count += 1
except:
pass
if disconnected_count > 0:
print(f"🔌 已断开 {disconnected_count} 个来自该IP的连接")
else:
print(f"ℹ️ IP {ip} 已被封禁")
def unban_user(self, ip):
"""解封用户IP"""
if not ip:
print("❌ 错误: 请指定要解封的IP地址")
return
if ip in self.banned_ips:
self.banned_ips.remove(ip)
self.save_banned_ips()
print(f"✅ 已成功解封IP: {ip}")
else:
print(f"ℹ️ IP {ip} 未被封禁")
def list_banned_ips(self):
"""列出所有被封禁的IP和端口"""
print("\n=== 封禁列表 ===")
if self.banned_ips or self.banned_ports:
if self.banned_ips:
print(f"\n完全封禁的IP ({len(self.banned_ips)}个):")
for ip in self.banned_ips:
print(f" 🚫 {ip} (所有端口)")
if self.banned_ports:
total_port_bans = sum(len(ports) for ports in self.banned_ports.values())
print(f"\n端口封禁 ({total_port_bans}个):")
for ip, ports in self.banned_ports.items():
for port in ports:
print(f" 🚫 {ip}:{port}")
else:
print("\n✅ 当前没有被封禁的IP或端口")
print("\n==================\n")
def clear_banned(self):
"""清除所有历史封禁"""
if self.banned_ips or self.banned_ports:
total_bans = len(self.banned_ips) + sum(len(ports) for ports in self.banned_ports.values())
self.banned_ips.clear()
self.banned_ports.clear()
self.save_banned_data()
print(f"✅ 已成功清除 {total_bans} 条封禁记录")
else:
print("ℹ️ 当前没有需要清除的封禁记录")
def send_server_message(self, message):
"""发送服务器消息"""
if not message:
print("❌ 错误: 消息内容不能为空")
return
full_msg = f"server: {message}\n"
# 显示在控制台
print(f"[{self.get_timestamp()}] 📢 服务器广播: {message}")
# 发送给所有客户端
sent_count = 0
for conn in self.conn:
try:
conn.send(full_msg.encode("utf-8"))
sent_count += 1
except:
pass
if sent_count > 0:
print(f"✅ 消息已发送给 {sent_count} 个客户端")
else:
print("ℹ️ 当前没有连接的客户端")
def handle_maxconn_command(self, args):
"""处理最大连接数命令"""
if args == "show":
print(f"📊 当前最大连接数: {self.max_connections}")
print(f"📊 初始最大连接数: {self.original_max_connections}")
elif args == "reset":
old_value = self.max_connections
self.max_connections = self.original_max_connections
print(f"✅ 最大连接数已从 {old_value} 重置为 {self.original_max_connections}")
else:
try:
new_max = int(args)
if new_max < 1:
print("❌ 错误: 最大连接数必须大于0")
elif new_max > 1000:
print("❌ 错误: 最大连接数不能超过1000")
else:
old_value = self.max_connections
self.max_connections = new_max
print(f"✅ 最大连接数已从 {old_value} 更改为 {new_max}")
# 如果当前连接数超过新的最大连接数,需要断开超出的连接
current_connections = len(self.conn)
if current_connections > new_max:
excess = current_connections - new_max
print(f"⚠️ 当前连接数({current_connections})超过新限制({new_max}),将断开{excess}个连接")
self.disconnect_excess_connections(excess)
except ValueError:
print("❌ 错误: 请输入有效的数字或'show'/'reset'")
def disconnect_excess_connections(self, excess_count):
"""断开超出的连接"""
disconnected = 0
# 从最新的连接开始断开(后进先出)
for i in range(len(self.conn) - 1, -1, -1):
if disconnected >= excess_count:
break
try:
addr = self.address[i]
username = self.usernames[i]
self.conn[i].send("服务器已调整最大连接数,您的连接已被断开".encode("utf-8"))
self.conn[i].close()
print(f"🔌 已断开连接: {addr[0]}:{addr[1]} (用户: {username})")
disconnected += 1
# 从列表中移除
self.conn.pop(i)
self.address.pop(i)
self.usernames.pop(i)
except Exception as e:
print(f"❌ 断开连接时出错: {e}")
# 即使出错也移除
if i < len(self.conn):
self.conn.pop(i)
if i < len(self.address):
self.address.pop(i)
if i < len(self.usernames):
self.usernames.pop(i)
disconnected += 1
print(f"✅ 已成功断开 {disconnected} 个连接")
def show_status(self):
"""显示服务器状态"""
print("\n=== TouchFish服务器状态 ===")
print(f"\n服务器状态: {'🟢 运行中' if self.server_running else '🔴 已停止'}")
print(f"监听地址: {self.ip}:{self.port}")
print(f"最大连接数: {self.max_connections}")
print(f"当前连接数: {len(self.conn)}")
print(f"已注册用户: {len([name for name in self.usernames if name])}")
print(f"完全封禁IP: {len(self.banned_ips)}")
print(f"端口封禁数: {sum(len(ports) for ports in self.banned_ports.values())}")
print(f"\n服务器运行时间: {self.get_uptime()}")
print("==========================\n")
def get_uptime(self):
"""获取服务器运行时间"""
if hasattr(self, 'start_time'):
uptime = time.time() - self.start_time
hours = int(uptime // 3600)
minutes = int((uptime % 3600) // 60)
seconds = int(uptime % 60)
return f"{hours:02d}:{minutes:02d}:{seconds:02d}"
else:
return "00:00:00"
def load_banned_data(self):
"""加载封禁的IP和端口数据"""
try:
if os.path.exists("banned_data.json"):
with open("banned_data.json", "r") as f:
data = json.load(f)
self.banned_ips = data.get("banned_ips", [])
self.banned_ports = data.get("banned_ports", {})
except:
self.banned_ips = []
self.banned_ports = {}
def save_banned_data(self):
"""保存封禁的IP和端口数据"""
try:
data = {
"banned_ips": self.banned_ips,
"banned_ports": self.banned_ports
}
with open("banned_data.json", "w") as f:
json.dump(data, f)
except Exception as e:
print(f"❌ 保存封禁数据失败: {e}")
def ban_port(self, ip, port):
"""封禁指定ip的指定端口"""
if not ip:
print("❌ 错误: 请指定要封禁的ip地址")
return
if ip not in self.banned_ports:
self.banned_ports[ip] = []
if port not in self.banned_ports[ip]:
self.banned_ports[ip].append(port)
self.save_banned_data()
print(f"✅ 已成功封禁 {ip}:{port}")
# 断开该ip和端口的所有连接
disconnected_count = 0
for i, addr in enumerate(self.address):
if addr[0] == ip and addr[1] == port:
try:
self.conn[i].send("您已被服务器封禁".encode("utf-8"))
self.conn[i].close()
disconnected_count += 1
except:
pass
if disconnected_count > 0:
print(f"🔌 已断开 {disconnected_count} 个来自该IP:端口的连接")
else:
print(f"ℹ️ {ip}:{port} 已被封禁")
def unban_port(self, ip, port):
"""解封指定ip的指定端口"""
if not ip:
print("❌ 错误: 请指定要解封的ip地址")
return
if ip in self.banned_ports and port in self.banned_ports[ip]:
self.banned_ports[ip].remove(port)
# 如果该ip没有被封禁的端口了,删除该ip条目
if not self.banned_ports[ip]:
del self.banned_ports[ip]
self.save_banned_data()
print(f"✅ 已成功解封 {ip}:{port}")
else:
print(f"ℹ️ {ip}:{port} 未被封禁")
def get_timestamp(self):
"""获取当前时间戳"""
return datetime.datetime.now().strftime("%H:%M:%S")
def print_usage():
"""显示使用说明"""
print("TouchFish服务器 - TFserver")
print("=" * 40)
print("用法:")
print(" TFserver.exe [IP] [端口] [最大连接数]")
print("")
print("参数说明:")
print(" IP - 服务器IP地址 (默认: 127.0.0.1)")
print(" 端口 - 监听端口 (默认: 8080)")
print(" 最大连接数 - 最大客户端连接数 (默认: 10)")
print("")
print("示例:")
print(" TFserver.exe # 使用默认配置")
print(" TFserver.exe 192.168.1.100 8080 20")
print(" TFserver.exe 0.0.0.0 1234 5")
print("")
print("启动后输入 'help' 查看服务器命令")
print("=" * 40)
def main():
"""主函数 - 支持命令行参数和默认值"""
try:
# 默认配置
ip = "127.0.0.1"
port = 8080
max_connections = 10
# 处理命令行参数
if len(sys.argv) == 1:
# 无参数,使用默认配置
print("TouchFish服务器启动中...")
print("使用默认配置: 127.0.0.1:8080 (最大连接: 10)")
elif len(sys.argv) == 2:
# 只有IP参数
ip = sys.argv[1]
print(f"TouchFish服务器启动中...")
print(f"使用配置: {ip}:{port} (最大连接: {max_connections})")
elif len(sys.argv) == 3:
# IP和端口参数
ip = sys.argv[1]
port = int(sys.argv[2])
print(f"TouchFish服务器启动中...")
print(f"使用配置: {ip}:{port} (最大连接: {max_connections})")
elif len(sys.argv) == 4:
# 全部参数
ip = sys.argv[1]
port = int(sys.argv[2])
max_connections = int(sys.argv[3])
print(f"TouchFish服务器启动中...")
print(f"使用配置: {ip}:{port} (最大连接: {max_connections})")
else:
print_usage()
return
# 验证参数
if not (1 <= port <= 65535):
print("错误: 端口必须在1-65535之间")
return
if max_connections < 1 or max_connections > 100:
print("错误: 最大连接数必须在1-100之间")
return
# 启动服务器
server = TFServer(ip, port, max_connections)
server.start()
except ValueError:
print("错误: 端口和最大连接数必须是有效的整数")
print()
print_usage()
except socket.gaierror:
print("错误: IP地址格式无效")
print()
print_usage()
except PermissionError:
print("错误: 权限不足,请尝试使用1024以上的端口")
print()
print_usage()
except OSError as e:
if "address already in use" in str(e).lower():
print(f"错误: 端口 {port} 已被占用")
else:
print(f"启动服务器时发生错误: {e}")
print()
print_usage()
except Exception as e:
print(f"启动服务器时发生错误: {e}")
print()
print_usage()
if __name__ == "__main__":
# 如果是直接运行exe文件,隐藏控制台窗口
if getattr(sys, 'frozen', False):
# 这是编译后的exe
print("TouchFish服务器 (编译版)")
else:
# 这是Python脚本
print("TouchFish服务器 (Python版)")
main()