forked from dcialdella/verysecurechat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.v.2.0.py
More file actions
328 lines (281 loc) · 11.4 KB
/
Copy pathclient.v.2.0.py
File metadata and controls
328 lines (281 loc) · 11.4 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
# Client define server ip / port 13031
# v 2.0 - minor changes
# Fix for Windows O.S.
# Thanks ZeroCool22 for Debugging.
# Thanks IGNIZ for Lot of changes
#
import sys
import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
import socket
import threading
import datetime
import json
import gnupg
import struct
def send_data(sock, data):
sock.sendall(struct.pack('!I', len(data)))
sock.sendall(data)
def recvall(sock, n):
data = bytearray()
while len(data) < n:
packet = sock.recv(n - len(data))
if not packet:
return None
data.extend(packet)
return bytes(data)
def recv_data(sock):
raw_msglen = recvall(sock, 4)
if not raw_msglen:
return None
msglen = struct.unpack('!I', raw_msglen)[0]
return recvall(sock, msglen)
try:
with open('client_config.json', "r") as configFile:
config = json.load(configFile)
except FileNotFoundError:
print("Warning: No se encontro client_config.json, se cerrara el cliente.")
sys.exit('No config file. Please create client_config.json.')
client = None
key_map = {}
valid_public_keys = []
valid_private_keys = []
HOST_ADDR = config.get('server', '')
HOST_PORT = config.get('port', 13031)
debug_mode = config.get('debug', False)
if debug_mode:
print("[DEBUG] Modo debug ACTIVADO.")
if not HOST_ADDR or not HOST_PORT:
sys.exit('No server IP or PORT en la configuracion.')
try:
gpg = gnupg.GPG()
public_keys = gpg.list_keys()
private_keys = gpg.list_keys(True)
except Exception as e:
print(f"Error critico: GPG no instalado o en PATH: {e}")
sys.exit("Por favor instala GPG en tu sistema (ej. brew install gnupg)")
GPGuidDestino = config.get('GPGid', "182DA782")
def is_valid_key(key):
return not key.get('expired', False) and not key.get('revoked', False)
valid_public_keys = [key for key in public_keys if is_valid_key(key)]
valid_private_keys = [key for key in private_keys if is_valid_key(key)]
window = tk.Tk()
window.title("Cliente v 2.1 (VerySecureChat)")
username = " "
BG_COLOR = "#1a1b26"
FG_COLOR = "#c0caf5"
ACCENT = "#7aa2f7"
BTN_BG = "#3d59a1"
window.configure(bg=BG_COLOR)
style = ttk.Style()
try:
style.theme_use('clam')
except Exception:
pass
style.configure("TFrame", background=BG_COLOR)
style.configure("TLabel", background=BG_COLOR, foreground=FG_COLOR, font=("Helvetica", 12))
style.configure("TButton", background=BTN_BG, foreground="white", font=("Helvetica", 11, "bold"), padding=3)
style.map("TButton", background=[("active", ACCENT)])
topFrame = ttk.Frame(window)
ttk.Label(topFrame, text="GPG U.ID:").pack(side=tk.LEFT, padx=5)
entNameText = tk.StringVar()
entName = ttk.Combobox(topFrame, width=50, textvariable=entNameText)
entName.pack(side=tk.LEFT, padx=5)
keyNames = [key['uids'][0] for key in valid_private_keys] if valid_private_keys else ["<Ninguna Clave Privada GPG>"]
entName['values'] = keyNames
if keyNames:
entNameText.set(keyNames[0])
btnConnect = ttk.Button(topFrame, text="Conectar", command=lambda: connect())
btnConnect.pack(side=tk.LEFT, padx=5)
btnDisconnect = ttk.Button(topFrame, text="Desconectar", command=lambda: handle_disconnection())
btnDisconnect.state(["disabled"])
btnDisconnect.pack(side=tk.LEFT, padx=5)
btnReload = ttk.Button(topFrame, text="🔄 Recargar Llaves", command=lambda: reload_keys())
btnReload.pack(side=tk.LEFT, padx=5)
topFrame.pack(side=tk.TOP, pady=15)
displayFrame = ttk.Frame(window)
scrollBar = tk.Scrollbar(displayFrame)
scrollBar.pack(side=tk.RIGHT, fill=tk.Y)
tkDisplay = tk.Text(displayFrame, height=25, width=65, bg="#24283b", fg=FG_COLOR, font=("Helvetica", 12), insertbackground=FG_COLOR, highlightthickness=0, borderwidth=1)
tkDisplay.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(15, 0))
tkDisplay.tag_config("tag_your_message", foreground="#9ece6a")
tkDisplay.tag_config("tag_your_message2", foreground="#7aa2f7")
scrollBar.config(command=tkDisplay.yview)
tkDisplay.config(yscrollcommand=scrollBar.set, state="disabled")
displayFrame.pack(side=tk.TOP, fill=tk.BOTH, expand=True, padx=10)
bottomFrame = ttk.Frame(window)
ttk.Label(bottomFrame, text="Mensaje:").pack(side=tk.LEFT, padx=5)
tkMessage = tk.Text(bottomFrame, height=4, width=70, bg="#24283b", fg=FG_COLOR, font=("Helvetica", 12), insertbackground=FG_COLOR, highlightthickness=0, borderwidth=1)
tkMessage.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=(5, 5), pady=(10, 15))
tkMessage.config(state="disabled")
tkMessage.bind("<Return>", (lambda event: (getChatMessage(tkMessage.get("1.0", tk.END)), "break")))
bottomFrame.pack(side=tk.BOTTOM, fill=tk.X)
tkUserList = tk.Listbox(displayFrame, height=25, width=36, exportselection=False, bg="#24283b", fg=FG_COLOR, selectbackground=ACCENT, selectforeground="#000000", highlightthickness=0, font=("Helvetica", 12))
tkUserList.pack(side=tk.LEFT, fill=tk.Y, padx=(10, 0))
key_map = {} # tkUserList index -> key dict
tkUserList.insert(0, '<SELECCIONE DESTINO>')
tkUserList.insert(1, '<Todos>')
for i, key in enumerate(valid_public_keys, start=2):
val = key['uids'][0] if key.get('uids') else 'Unknown ID'
tkUserList.insert(i, val)
key_map[i] = key
tkUserList.select_set(0)
def reload_keys():
global public_keys, private_keys, valid_public_keys, valid_private_keys, key_map
try:
public_keys = gpg.list_keys()
private_keys = gpg.list_keys(True)
valid_public_keys = [key for key in public_keys if is_valid_key(key)]
valid_private_keys = [key for key in private_keys if is_valid_key(key)]
tkUserList.delete(0, tk.END)
key_map = {}
tkUserList.insert(0, '<SELECCIONE DESTINO>')
tkUserList.insert(1, '<Todos>')
for i, key in enumerate(valid_public_keys, start=2):
val = key['uids'][0] if key.get('uids') else 'Unknown ID'
tkUserList.insert(i, val)
key_map[i] = key
tkUserList.select_set(0)
if debug_mode:
print("Claves PGP recargadas en vivo.")
except Exception as e:
messagebox.showerror("ERROR!!!", f"Fallo al recargar GPG: {e}")
def connect():
global username, client
if not valid_private_keys:
messagebox.showerror("ERROR!!!", "No tienes claves privadas GPG validas para usar el chat.")
return
if len(entName.get()) < 1:
messagebox.showerror("ERROR!!!", "Debes indicar tu ID de GPG.")
else:
username = entName.get()
connect_to_server(username)
def connect_to_server(name):
global client
try:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((HOST_ADDR, HOST_PORT))
send_data(client, name.encode('utf-8'))
entName.config(state="disabled")
btnConnect.state(["disabled"])
btnDisconnect.state(["!disabled"])
tkMessage.config(state="normal")
tkMessage.focus_set()
threading.Thread(target=receive_message_from_server, args=(client,), daemon=True).start()
except Exception as e:
messagebox.showerror("ERROR!!!", f"Error al conectar al servidor {HOST_ADDR}:{HOST_PORT}.\n{e}")
def handle_disconnection():
global client
if client:
try:
client.close()
except Exception:
pass
client = None
entName.config(state="normal")
btnConnect.state(["!disabled"])
try:
btnDisconnect.state(["disabled"])
except Exception:
pass
tkMessage.config(state="disabled")
insert_text_safe("\n--- DESCONECTADO DEL SERVIDOR ---\n", "tag_your_message")
def insert_text_safe(text, tag=None):
def _insert():
tkDisplay.config(state="normal")
if tag:
tkDisplay.insert(tk.END, text, tag)
else:
tkDisplay.insert(tk.END, text)
tkDisplay.config(state="disabled")
tkDisplay.see(tk.END)
window.after(0, _insert)
def receive_message_from_server(sck):
while True:
try:
raw = recv_data(sck)
if raw is None:
break
from_server = raw.decode('utf-8', errors='replace')
except Exception:
break
if from_server == "SYS:PING":
if debug_mode:
print(f"[DEBUG] SYS:PING recibido - respondiendo PONG")
try:
send_data(sck, b"SYS:PONG")
except Exception:
pass
continue
cuando = datetime.datetime.now()
if 'PGP MESSAGE' in from_server:
if debug_mode:
print(f"[DEBUG] Mensaje Cifrado Recibido:\n{from_server}")
decrypted_data = gpg.decrypt(from_server)
if decrypted_data.ok:
dec_msg = decrypted_data.data.decode(errors='replace')
if debug_mode:
print(f"[DEBUG] Mensaje Descifrado: {dec_msg}")
insert_text_safe(f"IN: {cuando} - {dec_msg}\n", "tag_your_message2")
else:
if debug_mode:
print('[DEBUG] Error desencriptando.', decrypted_data.status)
else:
if debug_mode:
print(f"[DEBUG] Mensaje Plano Recibido: {from_server}")
insert_text_safe(f"IN: {cuando} - {from_server}", "tag_your_message2")
try:
sck.close()
except Exception:
pass
window.after(0, handle_disconnection)
def getChatMessage(msg):
msg = msg.replace('\n', '')
cuando = datetime.datetime.now()
insert_text_safe(f"OUT-{cuando} - {msg}\n", "tag_your_message")
send_msg_to_server(msg)
tkMessage.delete('1.0', tk.END)
def send_msg_to_server(msg):
global client
client_msg = str(msg)
if client_msg == "fin":
window.destroy()
sys.exit(0)
if len(client_msg) > 0:
selections = tkUserList.curselection()
if not selections or selections[0] == 0:
messagebox.showwarning("Destino Inválido", "Tienes seleccionado <SELECCIONE DESTINO>.\nPor seguridad humana, por favor selecciona a qué contacto o grupo enviarás este mensaje.")
return
if selections[0] == 1:
destKeys = valid_public_keys
else:
destKeys = [key_map.get(selections[0])]
if not destKeys[0]:
messagebox.showerror("ERROR", "Destino inválido.")
return
for key in destKeys:
destino = key['uids'][0] if key.get('uids') else ''
cuando = datetime.datetime.now()
try:
encrypted_data = gpg.encrypt(client_msg, destino, always_trust=True)
if not encrypted_data.ok:
if debug_mode:
print(f"MANDO: {cuando}. OK: False (Clave de {destino} inutilizable)")
continue
encrypted_string = str(encrypted_data)
if debug_mode:
print(f"[DEBUG] Mensaje Plano a enviar: {client_msg}")
print(f"[DEBUG] Bloque Cifrado a enviar:\n{encrypted_string}")
print(f"[DEBUG] MANDO (A {destino}) | OK: True")
send_data(client, encrypted_string.encode('utf-8'))
except Exception as e:
insert_text_safe(f"Error en envio a {destino}: {e}\n", "tag_your_message2")
window.update_idletasks()
w = window.winfo_width()
h = window.winfo_height()
x = (window.winfo_screenwidth() // 2) - (w // 2)
y = (window.winfo_screenheight() // 2) - (h // 2)
window.geometry(f"+{x}+{y}")
window.minsize(w, h)
window.mainloop()