-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui_server.py
122 lines (98 loc) · 3.61 KB
/
gui_server.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
import socket
import threading
from cryptography.fernet import Fernet
# Connection Data
host = '127.0.0.1'
port = 9090
# Starting Server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen()
# Lists For Clients and Their Nicknames
clients = []
nicknames = []
banned = []
keys = []
# Sending Messages To All Connected Clients
def broadcast(message):
for client in clients:
client.send(message)
# Handling Messages From Clients
def handle(client):
while True:
try:
# Broadcasting Messages
msg = client.recv(1024)
key = keys[clients.index(client)]
fernet = Fernet(key)
decMessage= fernet.decrypt(msg)
if decMessage.decode('utf-8').startswith('KICK'):
if nicknames[clients.index(client)] == 'admin':
name_to_kick = decMessage.decode('utf-8')[5:len(decMessage)-1]
print(name_to_kick)
kick_user(str(name_to_kick))
else:
client.send('command was refused\n'.encode('utf-8'))
elif decMessage.decode('utf-8').startswith('BAN'):
if nicknames[clients.index(client)] == 'admin':
name_to_ban = decMessage.decode('utf-8')[4:len(decMessage)-1]
kick_user(name_to_ban)
banned.append(name_to_ban)
client.send(f'{name_to_ban} was banned!\n')
else:
client.send('command was refused\n'.encode('utf-8'))
else:
broadcast(decMessage)
except:
# Removing And Closing Clients
index = clients.index(client)
clients.remove(client)
client.close()
nickname = nicknames[index]
broadcast(f'{nickname} left!'.encode('utf-8'))
nicknames.remove(nickname)
break
# Receiving / Listening Function
def receive():
while True:
# Accept Connection
client, address = server.accept()
print(f"Connected with {address}")
# Request And Store Nickname
client.send('Key'.encode('utf-8'))
key = client.recv(1024)
keys.append(key)
client.send('NICK'.encode('utf-8'))
nickname = client.recv(1024).decode('utf-8')
if nickname in banned:
client.send('BAN'.encode('utf-8'))
client.close()
continue
if nickname == 'admin':
client.send('PASS'.encode('utf-8'))
password = client.recv(1024).decode('utf-8')
if password != 'adminpass':
client.send('REFUSE'.encode('utf-8'))
client.close()
continue
nicknames.append(nickname)
clients.append(client)
# Print And Broadcast Nickname
print(f"Nickname is {nickname}")
broadcast(f"{nickname} joined!\n".encode('utf-8'))
client.send('Connected to server!'.encode('utf-8'))
# Start Handling Thread For Client
thread = threading.Thread(target=handle, args=(client,))
thread.start()
def kick_user(name):
if name in nicknames:
name_index = nicknames.index(name)
print(name_index)
client_to_kick = clients[name_index]
clients.remove(client_to_kick)
client_to_kick.send('YOU WERE KICKED BY ADMIN!\n'.encode('utf-8'))
client_to_kick.close()
nicknames.remove(name)
broadcast(f'{name} was kicked by admin!'.encode('utf-8'))
print("server is listening")
receive()