-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.py
248 lines (198 loc) · 9.36 KB
/
Client.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
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
import socket
import json
import threading
# import ssl
try:
with open('config.json') as f:
data = json.load(f)
PORT = data['PORT']
CLIENT_IP = data['SERVER_IP']
UserName = data['USER_NAME']
ClientPrefix = data['PREFIX']
print(f"⚙️---CURRENT SETTINGS---⚙️\n🌐 SERVER IP: {CLIENT_IP}\n🔌 PORT: {PORT}\n👤 USER NAME: {UserName}\n🏷️ PREFIX: {ClientPrefix}\n\n")
check = input("❓ Do you want to change the Settings? [Yes/No]: ")
if check.lower() == 'yes':
check = input("🔧 What do you want to change? [USERNAME/PREFIX]: ")
if check.lower() == 'username':
UserName = input("👤 Enter the User Name: ")
elif check.lower() == 'prefix':
ClientPrefix = input("🏷️ Enter the Prefix: ")
else:
print("⚠️ Invalid Input!\n")
with open('config.json','w') as f:
data['USER_NAME'] = UserName
data['PREFIX'] = ClientPrefix
json.dump(data,f)
else:
with open('config.json','w') as f:
data['USER_NAME'] = data.get('USER_NAME', socket.gethostname())
data['PREFIX'] = data.get('PREFIX', '!')
data['PORT'] = data.get('PORT', 8080)
json.dump(data,f)
ADDR=(CLIENT_IP,PORT)
# context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
# context.load_verify_locations('server.crt')
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# client = context.wrap_socket(client, server_hostname=CLIENT_IP)
client.connect(ADDR)
client.send(f"{len(UserName):04}".encode('utf-8'))
client.send(UserName.encode('utf-8'))
client.send(f"{len(ClientPrefix):04}".encode('utf-8'))
client.send(ClientPrefix.encode('utf-8'))
BanVerify = int(client.recv(4).decode('utf-8'))
BanVerify = client.recv(BanVerify).decode('utf-8')
if BanVerify == 'you are banned':
print(f"🚫You are banned! frome the server {ADDR}\n")
exit()
Server_PASS_Try = 0
while True:
if Server_PASS_Try == 3:
UserVerify = 'Too many attempts!'
client.send(f"{len(UserVerify):04}".encode('utf-8'))
client.send(UserVerify.encode('utf-8'))
print("⚠️ Too many attempts! Exiting...\n")
exit()
while True:
Server_password = input("🔒 Enter the Server Password: ")
if len(Server_password) <= 8:
print("❗ Password must be at least 8 characters long.\n")
else:
break
client.send(f"{len(Server_password):04}".encode('utf-8'))
client.send(Server_password.encode('utf-8'))
UserVerify = int(client.recv(4).decode('utf-8'))
UserVerify = client.recv(UserVerify).decode('utf-8')
if UserVerify == 'access denied':
print("🚫 Access Denied!\n")
Server_PASS_Try += 1
continue
else:
print("✅ Access Granted!\n")
break
AdminVerify = int(client.recv(4).decode('utf-8'))
AdminVerify = client.recv(AdminVerify).decode('utf-8')
if AdminVerify == 'admin?':
while True:
check = input("👑 Are you an Admin! [Yes/No]: ")
client.send(f"{len(check):04}".encode('utf-8'))
client.send(check.encode('utf-8'))
if check.lower() == 'yes':
Admin_PASS_Try = 0
while True:
if Admin_PASS_Try == 3:
AdminVerify = 'Too many attempts!'
client.send(f"{len(AdminVerify):04}".encode('utf-8'))
client.send(AdminVerify.encode('utf-8'))
print("⚠️ Too many attempts! Exiting...\n")
exit()
while True:
AdminPassword = input("Enter the Admin Password: ")
if len(AdminPassword) <= 8:
print("❗ Password must be at least 8 characters long.\n")
else:
break
client.send(f"{len(AdminPassword):04}".encode('utf-8'))
client.send(AdminPassword.encode('utf-8'))
response_length = int(client.recv(4).decode('utf-8'))
AdminVerify = client.recv(response_length).decode('utf-8')
if AdminVerify == 'access denied':
Admin_PASS_Try += 1
print("🚫 Access Denied!\n")
else:
print("✅ Access Granted!\n")
break
if AdminVerify != 'access denied':
break
elif check.lower() == 'no':
break
else:
print("❗ Invalid Input!\n")
except KeyboardInterrupt:
print("Keyboard Interrupt!")
exit()
except Exception as e:
print(f"⚠️ [ERROR] : {e}\n")
exit()
def send(msg):
message=msg.encode('utf-8')
msg_length=len(message)
send_length=str(msg_length).encode('utf-8')
send_length+=b' '*(64-len(send_length))
client.send(send_length)
client.send(message)
def receive():
try:
while True:
message = client.recv(2048).decode('utf-8')
if message == f"[200]Exit":
print("❌ You are disconnected from the server!\n")
break
else:
print(f"{message}\n")
except ConnectionResetError or ConnectionAbortedError:
print(f"🔒 Connection was closed by the Server [{ADDR}]!\n")
except Exception as e:
print(f"⚠️ [ERROR] : {e}\n")
def main():
while True:
try:
try:
message=input()
except EOFError:
print("👋 EXITING...")
break
if all (character in message for character in [ClientPrefix,'help']):
print(f"🟢 {ClientPrefix}online: To check the number of online Members\n👑 {ClientPrefix}adminlist: To Show all Admin Online!\n🚫 {ClientPrefix}ban: To Ban a Member (ADMIN ONLY)\n✅ {ClientPrefix}unban: To UnBan a Member (ADMIN ONLY)\n📋 {ClientPrefix}banlist: To check the list of Banned Members (ADMIN ONLY)\n👢 {ClientPrefix}kick: To Kick a Member (ADMIN ONLY)\n🌐 {ClientPrefix}serverinfo: To now the server info \n🚪 {ClientPrefix}exit: To exit the chat\n")
elif all (character in message for character in [ClientPrefix,'exit']):
send(f"{ClientPrefix}exit")
break
elif all (character in message for character in [ClientPrefix,'banlist']):
send(f"{ClientPrefix}banlist")
elif all (character in message for character in [ClientPrefix,'unban']):
send(f"{ClientPrefix}unban")
try:
index = (input())
client.send(f"{len(index):04}".encode('utf-8'))
client.send(index.encode('utf-8'))
except ValueError:
print("❗Invalid Input!\n")
except KeyboardInterrupt:
pass
elif all (character in message for character in [ClientPrefix,'serverinfo']):
send(f"{ClientPrefix}serverinfo")
elif all (character in message for character in [ClientPrefix,'kick']):
send(f"{ClientPrefix}kick")
try:
index = (input())
client.send(f"{len(index):04}".encode('utf-8'))
client.send(index.encode('utf-8'))
except ValueError:
print("❗Invalid Input!\n")
except KeyboardInterrupt:
pass
elif all (character in message for character in [ClientPrefix,'ban']) and not all (character in message for character in [ClientPrefix,'unban']):
send(f"{ClientPrefix}ban")
try:
index = (input())
client.send(f"{len(index):04}".encode('utf-8'))
client.send(index.encode('utf-8'))
except ValueError:
print("❗Invalid Input!\n")
except KeyboardInterrupt:
pass
elif all (character in message for character in [ClientPrefix,'adminlist']):
send(f"{ClientPrefix}adminlist")
elif all (character in message for character in [ClientPrefix,'shutdown']):
send(f"{ClientPrefix}shutdown")
else:
send(message)
except ConnectionResetError or ConnectionAbortedError:
print(f"🔒 Connection was closed by the Server [{ADDR}]!\n")
break
try:
thread1 = threading.Thread(target=receive)
thread1.start()
thread2 = threading.Thread(target=main)
thread2.start()
except Exception as e:
exit()