/
EECE412_Ass3_VPN.py
363 lines (294 loc) · 10.8 KB
/
EECE412_Ass3_VPN.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
#!/bin/env python
#----------------------------------------------------------------------------
# Name: EECE412_Ass3_VPN.py
#
# Program: Simple implementation of VPN using
# Diffie-Hellman key enchange protocol
#
# Authors: Jobin Ansari-Gilani jobinansari@hotmail.com
# Ivan Cherapau cherapau@gmail.com
# Scott Hazlett scotthazlett@gmail.com
# Mina Savovic minasavovic@gmail.com
#
# Created: 13 Oct 2013
#----------------------------------------------------------------------------
import wx, sys
from socket import *
from EECE412_Ass3_VPN_wdr import *
from EECE412_Ass3_Server import ServerThread
from EECE412_Ass3_Client import ClientThread
# CONSTANTS -- we use functions because Python does not understand constants
def STR_NOT_CONNECTED(): return "Not Connected."
def STR_SERVER_USAGE():
return "Usage: provide valid IPv4 destination address and a destination port between 1024 and 49151 inclusive\n"
def STR_CLIENT_USAGE():
return """Usage: provide valid localhost port between 1024 and 49151 inclusive,
and provide valid IPv4 destination address and a destination port between 1024 and 49151 inclusive\n"""
def BUFFER_SIZE(): return 65535
# WDR: classes
class MyFrame(wx.Frame):
# Constructor
def __init__(self, parent, id, title,
pos = wx.DefaultPosition, size = wx.DefaultSize,
style = wx.DEFAULT_FRAME_STYLE ):
wx.Frame.__init__(self, parent, id, title, pos, size, style)
# Instantiate MenuBar and ToolBar
self.CreateMyMenuBar()
self.CreateMyToolBar()
self.CreateStatusBar(1)
self.SetStatusText(STR_NOT_CONNECTED())
# This took a while to figure out ...
self.sizer = MainSizer(self, -1, "Blah")
self.SetSizer(self.sizer)
self.GetSizer().Fit( self )
# Obtain references to the elements inside of MainSizer
# that we just attached to the wx.Frame
# These are the GUI objects that we click on
self.mode = self.FindWindowById(ID_CHOICE_MODE)
self.remote_ip = self.FindWindowById(ID_TEXTCTRL_REMOTE_SERVER_IP)
self.remote_port = self.FindWindowById(ID_TEXTCTRL_REMOTE_SERVER_PORT)
self.local_port = self.FindWindowById(ID_TEXTCTRL_LOCAL_PORT)
self.shared_key = self.FindWindowById(ID_TEXTCTRL_SHARED_KEY)
self.data = self.FindWindowById(ID_TEXTCTRL_DATA)
self.console = self.FindWindowById(ID_TEXTCTRL_CONSOLE)
self.step = self.FindWindowById(ID_BUTTON_STEP)
self.connect = self.FindWindowById(ID_BUTTON_CONNECT)
self.send = self.FindWindowById(ID_BUTTON_SEND)
# The client or server thread
self.mythread = None
# Functions (or "CallBack"s) that we call when
# something important has been clicked on
# I think we can also capture asynchornous events
# WDR: handler declarations for MyFrame
wx.EVT_BUTTON(self, ID_BUTTON_STEP, self.ButtonStepCB)
wx.EVT_BUTTON(self, ID_BUTTON_SEND, self.ButtonSendCB)
wx.EVT_BUTTON(self, ID_BUTTON_CONNECT, self.ButtonConnectCB)
wx.EVT_CHOICE(self, ID_CHOICE_MODE, self.ChoiceModeCB)
wx.EVT_MENU(self, wx.ID_ABOUT, self.OnAbout)
wx.EVT_MENU(self, wx.ID_EXIT, self.OnQuit)
wx.EVT_CLOSE(self, self.OnCloseWindow)
# Set selection of "mode" to "Server"
self.mode.SetSelection(0)
self.SetServerModeDetails()
def SetClientModeDetails(self):
self.remote_port.SetValue("12345")
self.remote_ip.SetValue(gethostbyname(getfqdn())) # xx.xx.xx.xx format
self.local_port.SetValue("12399")
def SetServerModeDetails(self):
self.local_port.SetValue("12345")
# WDR: methods for MyFrame
def CreateMyMenuBar(self):
self.SetMenuBar( MyMenuBarFunc() )
def CreateMyToolBar(self):
tb = self.CreateToolBar(wx.TB_HORIZONTAL|wx.NO_BORDER)
MyToolBarFunc( tb )
# WDR: handler implementations for MyFrame
def ButtonSendCB(self, event):
if(self.mythread == None):
print "Send called on null thread"
elif(self.mythread.isAlive() == False):
print "Send called on dead thread"
else:
self.mythread.nextstep()
def ButtonStepCB(self, event):
if(self.mythread == None):
print "Step called on null thread"
elif(self.mythread.isAlive() == False):
print "Step called on dead thread"
else:
self.mythread.nextstep()
def ButtonConnectCB(self, event):
selection = self.mode.GetSelection()
print "selection is " + str(selection)
# Is it client or server mode?
if selection == 0 : # Server
# Setup server for listening mode
port = self.local_port.GetValue()
print "Listening for client on port:" + port
if self.IsValidPort( port ):
# we can now attempt to bind to port and
# start listening for incoming clients
self.console.AppendText("Binding to port " + port + " ...\n")
self.SetStatusText("Binding to port " + port + "...")
###################################################
# BEGIN SOCKET CODE TO BIND AND LISTEN
###################################################
#from EECE412_Ass3_Server import ServerThread
self.KillThread()
self.mythread = ServerThread(frame=self, port=port, buffersize=BUFFER_SIZE())
self.mythread.start()
###################################################
# END SOCKET CODE TO BIND AND LISTEN
###################################################
else:
# local port is invalid range; show usage
self.console.AppendText(STR_SERVER_USAGE())
return
# Here is my rudimentary server code from my socket programming assignment
# serverSocket = socket(AF_INET, SOCK_STREAM)
# port = 12373;
# host = getfqdn()
#
# print "Server listening on ", (host, port)
#
# #Prepare a sever socket on localhost on port 'port'
# serverSocket.bind( (host, port) )
#
# # start listening with a maximum number of queued connections of 1
# serverSocket.listen(1)
#
# while True:
# #Establish the connection
# print 'Ready to serve...'
# connectionSocket, addr = serverSocket.accept()
# try:
# # Receive data from the socket and place into 'message'
# message = connectionSocket.recv(65536)
#
# # parse the second token for the filename using default delimiter
# # assumes the first token is GET
# filename = message.split()[1]
#
# print "Opening file", filename
#
# # remove the leading slash
# f = open(filename[1:])
#
# # regurgitate file back to client
# outputdata = f.read()
#
# #Send one HTTP header line into socket
# connectionSocket.send( REQ_OK )
#
# #Send the content of the requested file to the client
# for i in range(0, len(outputdata)):
# connectionSocket.send(outputdata[i])
#
# except IOError:
# #Send response message for file not found
# print "Not found"
# # First line is HTTP header
# connectionSocket.send( REQ_NOT_FOUND )
# connectionSocket.send( REQ_NOT_FOUND_TEXT )
#
# finally:
# #Close client socket
# connectionSocket.close()
#
# serverSocket.close();
else: # client connect to server
ip = self.remote_ip.GetValue()
port = self.remote_port.GetValue()
local_port = self.local_port.GetValue()
print "Connecting to server at ip: " + ip + " and port:" + port
# Then connect to remote host address + port
if self.IsValidIP( ip ) and self.IsValidPort( port ) and self.IsValidPort( local_port ):
# we can attempt to connect
self.console.AppendText("Connecting to server at " + ip + ":" + port + " ...\n")
self.SetStatusText("Connecting to server at " + ip + ":" + port + "...")
###################################################
# BEGIN SOCKET CODE FOR CLIENT
###################################################
self.KillThread()
self.mythread = ClientThread(frame=self, port=port, ip=ip, buffersize=BUFFER_SIZE())
self.mythread.start()
###################################################
# END SOCKET CODE FOR CLIENT
###################################################
else:
self.console.AppendText(STR_CLIENT_USAGE())
# Here is my rudimentary client socket code from previous socket assignment
#s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#HOST = socket.getfqdn() # The remote host
#PORT = 12373 # The same port as used by the server
#
#print "client connecting to ", (HOST, PORT)
#s.connect((HOST, PORT))
#
#s.sendall('GET /HelloWorld.html HTML/1.1')
#data = s.recv(1024)
#s.close()
#print 'Received', repr(data)
def ChoiceModeCB(self, event):
# On which item is the "Mode" Combo box currently selected?
selection = self.mode.GetCurrentSelection()
#print "choiecmodecb "+ str(selection)
# kill thread if mode has changed
if(self.mythread != None and self.mythread.isAlive() and self.mythread.ThreadType() == selection):
self.KillThread()
# We have 2 cases:
# If server, then disable all "remote" fields
# If Client, then enable all "remote" fields
if selection == 0:
#print "server"
children = self.sizerRemote.GetChildren()
for child in children:
child.GetWindow().Disable()
self.SetServerModeDetails()
elif selection == 1:
#print "client"
children = self.sizerRemote.GetChildren()
for child in children:
child.GetWindow().Enable()
self.SetClientModeDetails()
else:
print "Syntax error -- Unknown selection!"
def IsValidPort( self, port ):
print "port is " + str(port)
try:
# First attempt to cast to integer
port_int = int(port)
# Cast success. Now validate range ...
return port_int > 1023 and port_int < 49152
except ValueError as e:
return False;
def IsValidIP( self, ip ):
# This validates that the string can be parsed as an IPv4 address
# This does not support IPv6
# obtained from
# http://stackoverflow.com/questions/319279/how-to-validate-ip-address-in-python
try:
inet_aton( ip )
# legal
return True
except error as e:
# Not legal
return False
# Create a pop-up dialog about this application
# Authors in alphabetical order by last name
def OnAbout(self, event):
credits = """
Simple implementation of VPN using
Diffie-Hellman key enchange protocol
\n
Authors:
Jobin Ansari-Gilani jobinansari@hotmail.com
Ivan Cherapau cherapau@gmail.com
Scott Hazlett scotthazlett@gmail.com
Mina Savovic minasavovic@gmail.com
"""
dialog = wx.MessageDialog(self, credits,
"EECE412 Assignment #3 VPN", wx.OK|wx.ICON_INFORMATION )
dialog.CentreOnParent()
dialog.ShowModal()
dialog.Destroy()
def OnQuit(self, event):
self.KillThread()
self.Close(True)
def OnCloseWindow(self, event):
self.KillThread()
self.Destroy()
def KillThread(self):
if(self.mythread != None and self.mythread.isAlive()):
self.mythread.stop()
self.mythread.join()
#----------------------------------------------------------------------------
class MyApp(wx.App):
def OnInit(self):
wx.InitAllImageHandlers()
frame = MyFrame( None, -1, "VPN Application", [20,20], [500,340] )
frame.Show(True)
return True
#----------------------------------------------------------------------------
app = MyApp(True)
app.MainLoop()