This repository has been archived by the owner on Apr 3, 2020. It is now read-only.
/
gui.py
403 lines (351 loc) · 14.6 KB
/
gui.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import logging
import socket
from time import sleep
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.config import Config
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.tabbedpanel import TabbedPanelHeader
from kivy.graphics import Rectangle
from ect.chatclient import ChatClientClient
from ect.chatclient import ChatClientServer
from ect.exceptions import BeingAttacked
from ect.exceptions import DataIntegrityException
from ect.exceptions import NoAuthentication
from ect.log import log
from ect.exceptions import NoSharedKey
def is_valid_ip(ip):
try:
parts = ip.split('.')
return len(parts) == 4 and all(0 <= int(part) < 256 for part in parts)
except:
return False
class GuiApp(App):
def build(self):
tab_panel = TabbedPanel()
tab_panel.do_default_tab = False
tab_panel.add_widget(self.make_server_tab())
tab_panel.add_widget(self.make_client_tab())
tab_panel.set_def_tab(tab_panel.tab_list[1])
return tab_panel
def make_server_tab(self):
server_tab = TabbedPanelHeader(text='Server')
server_widget = Widget()
self.bob = None
self.send = False
self.msg = ""
self.done_auth = False
# Console
txt_console = TextInput(multiline=True)
txt_console.pos = (75, 75)
txt_console.size = (650, 200)
txt_console.readonly = True
server_widget.add_widget(txt_console)
def print_console(msg):
txt_console.text = txt_console.text + '\n' + msg
txt_console.cursor = (999,999)
# Config label
lbl_config = Label(text='Server Config')
lbl_config.pos = (75,525)
lbl_config.size = (100, 40)
lbl_config.halign = "left"
lbl_config.text_size = lbl_config.size
lbl_config.bold = True
server_widget.add_widget(lbl_config)
# Port label
lbl_port = Label(text='Port')
lbl_port.pos = (125,450)
lbl_port.size = (100, 30)
lbl_port.halign = "right"
lbl_port.valign = "middle"
lbl_port.text_size = lbl_port.size
server_widget.add_widget(lbl_port)
# Port input
txt_port = TextInput(multiline=False, text="9001")
txt_port.pos = (250,450)
txt_port.size = (150, 30)
server_widget.add_widget(txt_port)
# Secret label
lbl_secret = Label(text='Secret')
lbl_secret.pos = (125,400)
lbl_secret.size = (100, 30)
lbl_secret.halign = "right"
lbl_secret.valign = "middle"
lbl_secret.text_size = lbl_secret.size
server_widget.add_widget(lbl_secret)
# Secret input
txt_secret = TextInput(multiline=False, text="foobar")
txt_secret.pos = (250,400)
txt_secret.size = (150, 30)
server_widget.add_widget(txt_secret)
# Start button
def on_server_btn_start(instance):
btn_start.disabled = True
txt_secret.readonly = True
txt_port.readonly = True
print_console("Starting Server on port " + txt_port.text)
self.bob = None
while self.bob is None:
try:
self.bob = ChatClientServer("0.0.0.0", int(txt_port.text))
except socket.error as e:
log(
logging.warning,
self,
self.make_server_tab,
"Error occurred while trying to connect: "
"socket.error: {}; retrying...".format(e))
sleep(1)
print_console("Setting shared key to " + txt_secret.text)
self.bob.set_shared_key(txt_secret.text.encode())
btn_start = Button(text='Start Server')
btn_start.pos = (500, 395)
btn_start.size = (200, 100)
btn_start.bind(on_release=on_server_btn_start)
server_widget.add_widget(btn_start)
# Separator line
with server_widget.canvas:
Rectangle(pos=(75, 350), size=(650, 2))
# Communication label
lbl_comm = Label(text='Server Communication')
lbl_comm.pos = (75,300)
lbl_comm.size = (400, 40)
lbl_comm.halign = "left"
lbl_comm.text_size = lbl_comm.size
lbl_comm.bold = True
server_widget.add_widget(lbl_comm)
# Message box
txt_message = TextInput(multiline=False)
txt_message.pos = (75,25)
txt_message.size = (450, 30)
server_widget.add_widget(txt_message)
# Send button
def on_server_btn_send(instance):
if self.done_auth:
self.send = True
self.msg = txt_message.text
btn_send.disabled = True
txt_message.text = ""
print_console("Will send [" + self.msg + "] on next 'Continue'")
else:
print_console("Finish authentication first!")
btn_send = Button(text='Send')
btn_send.pos = (535, 25)
btn_send.size = (90, 30)
btn_send.bind(on_release=on_server_btn_send)
server_widget.add_widget(btn_send)
# Continue button
def on_server_btn_continue(instance):
if self.bob is not None:
if self.done_auth:
try:
if self.send:
print_console("Sending [" + self.msg + "]")
self.bob.send(self.msg.encode())
self.send = False
btn_send.disabled = False
self.msg = ""
else:
data = self.bob.recv(nb=True)
if data is not None:
print_console("Received [" + data + "]")
else:
print_console("No data to receive")
except NoAuthentication:
print_console("We are not authenticated. Reset authentication steps.")
self.bob.mutauth_step(reset=True)
except DataIntegrityException:
print_console("Either Trudy is attacking or more than one message was received. Not going to display the message. Tell your friend to send only one message at a time.")
else:
try:
print_console("Performing a mutual authentication step")
self.bob.mutauth_step()
except BeingAttacked:
print_console("We are being attacked! Reset authentication steps.")
self.bob.mutauth_step(reset=True)
except NoSharedKey:
print_console("Shared key is not yet shared. Please share first")
self.bob.mutauth_step(reset=True)
except StopIteration:
print_console("Successfully Authenticated")
self.done_auth = True
btn_continue = Button(text='Continue')
btn_continue.pos = (635, 25)
btn_continue.size = (90, 30)
btn_continue.bind(on_release=on_server_btn_continue)
server_widget.add_widget(btn_continue)
server_tab.content = server_widget
return server_tab
def make_client_tab(self):
client_tab = TabbedPanelHeader(text='Client')
client_widget = Widget()
self.alice = None
self.send = False
self.msg = ""
self.done_auth = False
# Console
txt_console = TextInput(multiline=True)
txt_console.pos = (75, 75)
txt_console.size = (650, 200)
txt_console.readonly = True
client_widget.add_widget(txt_console)
def print_console(msg):
txt_console.text = txt_console.text + '\n' + msg
txt_console.cursor = (999,999)
# Config label
lbl_config = Label(text='Client Config')
lbl_config.pos = (75,525)
lbl_config.size = (100, 40)
lbl_config.halign = "left"
lbl_config.text_size = lbl_config.size
lbl_config.bold = True
client_widget.add_widget(lbl_config)
# IP label
lbl_ip = Label(text='Server IP')
lbl_ip.pos = (125,475)
lbl_ip.size = (100, 30)
lbl_ip.halign = "right"
lbl_ip.valign = "middle"
lbl_ip.text_size = lbl_ip.size
client_widget.add_widget(lbl_ip)
# IP input
txt_ip = TextInput(multiline=False, text="127.0.0.1")
txt_ip.pos = (250,475)
txt_ip.size = (150, 30)
client_widget.add_widget(txt_ip)
# Port label
lbl_port = Label(text='Port')
lbl_port.pos = (125,425)
lbl_port.size = (100, 30)
lbl_port.halign = "right"
lbl_port.valign = "middle"
lbl_port.text_size = lbl_port.size
client_widget.add_widget(lbl_port)
# Port input
txt_port = TextInput(multiline=False, text="9001")
txt_port.pos = (250,425)
txt_port.size = (150, 30)
client_widget.add_widget(txt_port)
# Secret label
lbl_secret = Label(text='Secret')
lbl_secret.pos = (125,375)
lbl_secret.size = (100, 30)
lbl_secret.halign = "right"
lbl_secret.valign = "middle"
lbl_secret.text_size = lbl_secret.size
client_widget.add_widget(lbl_secret)
# Secret input
txt_secret = TextInput(multiline=False, text="foobar")
txt_secret.pos = (250,375)
txt_secret.size = (150, 30)
client_widget.add_widget(txt_secret)
# Start button
def on_client_btn_start(instance):
if is_valid_ip(txt_ip.text):
btn_start.disabled = True
txt_secret.readonly = True
txt_port.readonly = True
txt_ip.readonly = True
print_console("Starting Client on port " + txt_port.text)
self.alice = None
while self.alice is None:
try:
self.alice = ChatClientClient(txt_ip.text,
int(txt_port.text))
except socket.error as e:
log(
logging.warning,
self,
self.make_client_tab,
"Error occurred while trying to connect: "
"socket.error: {}; retrying...".format(e))
sleep(1)
print_console("Setting shared key to " + txt_secret.text)
self.alice.set_shared_key(txt_secret.text.encode())
else:
print_console("Please enter a valid IP address")
btn_start = Button(text='Start Client')
btn_start.pos = (500, 395)
btn_start.size = (200, 100)
btn_start.bind(on_release=on_client_btn_start)
client_widget.add_widget(btn_start)
# Separator line
with client_widget.canvas:
Rectangle(pos=(75, 350), size=(650, 2))
# Communication label
lbl_comm = Label(text='Client Communication')
lbl_comm.pos = (75,300)
lbl_comm.size = (400, 40)
lbl_comm.halign = "left"
lbl_comm.text_size = lbl_comm.size
lbl_comm.bold = True
client_widget.add_widget(lbl_comm)
# Message box
txt_message = TextInput(multiline=False)
txt_message.pos = (75,25)
txt_message.size = (450, 30)
client_widget.add_widget(txt_message)
# Send button
def on_client_btn_send(instance):
if self.done_auth:
self.send = True
self.msg = txt_message.text
btn_send.disabled = True
txt_message.text = ""
print_console("Will send [" + self.msg + "] on next 'Continue'")
else:
print_console("Finish authentication first!")
btn_send = Button(text='Send')
btn_send.pos = (535, 25)
btn_send.size = (90, 30)
btn_send.bind(on_release=on_client_btn_send)
client_widget.add_widget(btn_send)
# Continue button
def on_client_btn_continue(instance):
if self.alice is not None:
if self.done_auth:
try:
if self.send:
print_console("Sending [" + self.msg + "]")
self.alice.send(self.msg.encode())
self.send = False
btn_send.disabled = False
self.msg = ""
else:
data = self.alice.recv(nb=True)
if data is not None:
print_console("Received [" + data + "]")
else:
print_console("No data to receive")
except NoAuthentication:
print_console("We are not authenticated. Reset authentication steps.")
self.alice.mutauth_step(reset=True)
except DataIntegrityException:
print_console("Either Trudy is attacking or more than one message was received. Not going to display the message. Tell your friend to send only one message at a time.")
else:
try:
print_console("Performing a mutual authentication step")
self.alice.mutauth_step()
except BeingAttacked:
print_console("We are being attacked! Reset authentication steps.")
self.alice.mutauth_step(reset=True)
except NoSharedKey:
print_console("Shared key is not yet shared. Please share first")
self.alice.mutauth_step(reset=True)
except StopIteration:
print_console("Successfully Authenticated")
self.done_auth = True
btn_continue = Button(text='Continue')
btn_continue.pos = (635, 25)
btn_continue.size = (90, 30)
btn_continue.bind(on_release=on_client_btn_continue)
client_widget.add_widget(btn_continue)
client_tab.content = client_widget
return client_tab
if __name__ == '__main__':
Config.set('graphics', 'width', '400')
Config.set('graphics', 'height', '600')
GuiApp().run()