-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcore.py
131 lines (128 loc) · 3.83 KB
/
core.py
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
import socket
import time
import os
from threading import *
import sqlite3
from database.DataBase import DataBase
from Logic.Device import Device
from Logic.Player import Players
from Logic.LogicMessageFactory import packets
from Server.Home.LobbyInfoMessage import LobbyInfoMessage
from Utils.Config import Config
from Utils.Helpers import Helpers
import json
def _(*args):
print('[INFO]', end=' ')
for arg in args:
print(arg, end=' ')
print()
addr = {}
block = []
class Server:
Clients = {"ClientCounts": 0, "Clients": {}}
ThreadCount = 0
def __init__(self, ip: str, port: int):
self.server = socket.socket()
self.port = port
self.ip = ip
def start(self):
self.server.bind((self.ip, self.port))
print(f'Server | Lobby started! {self.ip}:{self.port}\nServer | Battle started! {self.ip}:6969')
plrsinfo = "database/Player/plr.db"
if os.path.exists(plrsinfo):
conn = sqlite3.connect("database/Player/plr.db")
c = conn.cursor()
c.execute("UPDATE plrs SET roomID=0")
c.execute("UPDATE plrs SET online=0")
c.execute("SELECT * FROM plrs")
conn.commit()
conn.close()
while True:
self.server.listen()
client, address = self.server.accept()
if address[0] in addr:
addr[address[0]] += 1
else:
addr[address[0]] = 0
if address[0] in block:
os.system(f"iptables -A INPUT -s {address[0]} -j DROP")
client.close()
elif addr[address[0]] >= 4:
os.system(f"iptables -A INPUT -s {address[0]} -j DROP")
block.append(address[0])
config = open('config.json', 'r')
content = config.read()
settings = json.loads(content)
settings['block'].append(address[0])
print(f"{settings['block']}")
client.close()
else:
ClientThread(client, address).start()
Server.ThreadCount += 1
class ClientThread(Thread):
def __init__(self, client, address):
super().__init__()
self.client = client
self.address = address
self.device = Device(self.client)
self.player = Players(self.device)
def recvall(self, length: int):
data = b''
while len(data) < length:
s = self.client.recv(length)
if not s:
block.append(self.address[0])
break
data += s
return data
def run(self):
last_packet = time.time()
try:
while True:
header = self.client.recv(7)
if len(header) > 0:
last_packet = time.time()
packet_id = int.from_bytes(header[:2], 'big')
length = int.from_bytes(header[2:5], 'big')
data = self.recvall(length)
LobbyInfoMessage(self.client, self.player, Server.ThreadCount).send()
if packet_id in packets:
message = packets[packet_id](self.client, self.player, data)
message.decode()
message.process()
if packet_id == 10101:
Server.Clients["Clients"][str(self.player.low_id)] = {"SocketInfo": self.client}
Server.Clients["ClientCounts"] = Server.ThreadCount
self.player.ClientDict = Server.Clients
if time.time() - last_packet > 9:
addr[self.address[0]] = 0
del addr[self.address[0]]
DataBase.replaceValue(self, 'online', 0)
Server.ThreadCount -= 1
print(f"Player Online {Server.ThreadCount}")
self.client.close()
break
except ConnectionAbortedError:
addr[self.address[0]] = 0
del addr[self.address[0]]
DataBase.replaceValue(self, 'online', 0)
Server.ThreadCount -= 1
print(f"Player Online {Server.ThreadCount}")
self.client.close()
except ConnectionResetError:
addr[self.address[0]] = 0
del addr[self.address[0]]
DataBase.replaceValue(self, 'online', 0)
Server.ThreadCount -= 1
print(f"Player Online {Server.ThreadCount}")
self.client.close()
except TimeoutError:
addr[self.address[0]] = 0
del addr[self.address[0]]
DataBase.replaceValue(self, 'online', 0)
Server.ThreadCount -= 1
print(f"Player Online {Server.ThreadCount}")
self.client.close()
if __name__ == '__main__':
server = Server('0.0.0.0', 2556)
server.start()