-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
218 lines (169 loc) · 6.1 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
import os, socket, sys, tqdm
from colorama import Fore, init
from cryptography.fernet import Fernet
from datetime import datetime
from threading import Thread
# colours
colour_list = [
f"{Fore.BLUE} BLUE: b{Fore.RESET}",
f"{Fore.LIGHTCYAN_EX} CYAN: c{Fore.RESET}",
f"{Fore.LIGHTBLACK_EX} GRAY: g{Fore.RESET}",
f"{Fore.LIGHTYELLOW_EX} LEMON: le{Fore.RESET}",
f"{Fore.LIGHTGREEN_EX} LIME: li{Fore.RESET}",
f"{Fore.LIGHTMAGENTA_EX} MAGENTA: m{Fore.RESET}",
f"{Fore.MAGENTA} PURPLE: p{Fore.RESET}",
f"{Fore.RED} RED: r{Fore.RESET}",
f"{Fore.LIGHTRED_EX} SALMON: s{Fore.RESET}",
f"{Fore.LIGHTBLUE_EX} SKY BLUE: sb{Fore.RESET}",
f"{Fore.YELLOW} YELLOW: y{Fore.RESET}",
]
def colour_function(colour_chooser):
if colour_chooser.lower() == "b" or colour_chooser.lower() == "blue":
return Fore.BLUE
elif colour_chooser.lower() == "c" or colour_chooser.lower() == "cyan":
return Fore.LIGHTCYAN_EX
elif colour_chooser.lower() == "g" or colour_chooser.lower() == "gray" or colour_chooser.lower() == "grey":
return Fore.LIGHTBLACK_EX
elif colour_chooser.lower() == "le" or colour_chooser.lower() == "lemon":
return Fore.LIGHTYELLOW_EX
elif colour_chooser.lower() == "li" or colour_chooser.lower() == "lime":
return Fore.LIGHTGREEN_EX
elif colour_chooser.lower() == "m" or colour_chooser.lower() == "magenta":
return Fore.LIGHTMAGENTA_EX
elif colour_chooser.lower() == "p" or colour_chooser.lower() == "purple":
return Fore.MAGENTA
elif colour_chooser.lower() == "r" or colour_chooser.lower() == "red":
return Fore.RED
elif colour_chooser.lower() == "s" or colour_chooser.lower() == "salmon":
return Fore.LIGHTRED_EX
elif colour_chooser.lower() == "sb" or colour_chooser.lower() == "skyblue" or colour_chooser.lower() == "sky blue":
return Fore.LIGHTBLUE_EX
elif colour_chooser.lower() == "y" or colour_chooser.lower() == "yellow":
return Fore.YELLOW
else:
return Fore.LIGHTWHITE_EX
def cls():
_ = os.system('cls')
# encryption
def get_key():
return Fernet(b'81a_yR9CuZk3HzPHocYMWNGUKMYylAO7UQMymGFv9mg=')
def decrypt(input):
f = get_key()
return f.decrypt(str(input).encode())
def encrypt(input):
f = get_key()
return f.encrypt(bytes(input, encoding='utf8')).decode()
# for timestamps
def now():
return datetime.now().strftime('%Y-%m-%d %H:%M')
# send file
def send_file(filename, host, port):
SEPARATOR = "<SEPARATOR>"
BUFFER_SIZE = 1024 * 4 #4KB
filesize = os.path.getsize(filename)
# to send:
s.send(f"{filename}{SEPARATOR}{filesize}".encode())
# send file
progress = tqdm.tqdm(range(filesize), f"Sending {filename}", unit="B", unit_scale=True, unit_divisor=1024)
with open(filename, "rb") as f:
while True:
bytes_read = f.read(BUFFER_SIZE)
if not bytes_read:
break
s.sendall(bytes_read)
# update the progress bar
progress.update(len(bytes_read))
# get msg
def listen_for_messages():
while True:
try:
rcvd = s.recv(1024)
msg = rcvd.decode()
decrypted = decrypt(msg)
print("\n" + str(decrypted.decode()))
except: pass
def help():
cmds = '''
!quit: quit
!changecolour: change colour
!changename: change username
!sendfile: send a file (from same directory as application)
!help: see this menu
'''
return cmds
cls()
init()
# todo:
# preference file
# send images
SERVER_HOST = "127.0.0.1" # 192.168.0.## if server is not on local machine but on local network
SERVER_PORT = 5002 # server port
separator_token = ": "
s = socket.socket()
print(f"[*] Connecting to {SERVER_HOST}:{SERVER_PORT}...")
try:
s.connect((SERVER_HOST, SERVER_PORT))
except Exception as e:
print("[!] Unable to connect.")
print(f"[!] Error2: {e}")
else:
print("[+] Connected.")
startup = input("\n\nView help commands?\ny/N: ")
if startup.lower().startswith("n"): pass
else: print(help())
name = input("\n\nEnter your username: ")
if name == "":
name = "anon"
print()
for item in colour_list:
print(item)
colour_chooser = input(f"\nPick your colour: ")
client_color = colour_function(colour_chooser)
print("\nMessages will look like this:")
print(f"{client_color}[{now()}] {name}: \x1B[3m\x1B[1myour message\x1B[0m{Fore.RESET}")
input("\n\nPress RETURN to connect to the main thread")
cls()
join = f"{name}<SEP>[+] Client connected: {client_color}{name} has joined the chat{Fore.RESET}\n"
encrypted = encrypt(join)
try:
s.send(str(encrypted).encode())
except Exception as e:
print(f"[!] Error: {e}")
sys.exit()
t = Thread(target=listen_for_messages)
t.daemon = True
t.start()
while True:
to_send = input()
msg = to_send.lower()
if msg == '!help':
print(help())
elif msg == '!quit':
exitmsg = f'{client_color}[-] Client disconnect: {name} has left the chat{Fore.RESET}'
encrypted = encrypt(exitmsg)
try:
s.send(str(encrypted).encode())
except Exception as e:
print(f"[!] Error: {e}")
s.close()
sys.exit()
elif msg == '!changecolour':
for item in colour_list:
print(item)
client_color = colour_function()
elif msg == '!changename':
name = input("\nEnter your new username: ")
print()
elif msg == '!sendfile':
name = input("\nEnter your new username: ")
print()
elif msg == '':
pass
else:
to_send = f"{client_color}[{now()}] {name}{separator_token}{to_send}{Fore.RESET}"
encrypted = encrypt(to_send)
try:
s.send(str(encrypted).encode())
except Exception as e:
print(f"[!] Error: {e}")
sys.exit()