-
Notifications
You must be signed in to change notification settings - Fork 0
/
p2p.py
210 lines (187 loc) · 6.72 KB
/
p2p.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
from Tkinter import *
from ttk import *
import socket
import thread
from ecdsa import SigningKey,VerifyingKey,BadSignatureError, NIST192p
class ChatClient(Frame):
def __init__(self, root):
Frame.__init__(self, root)
self.root = root
self.initUI()
self.serverSoc = None
self.serverStatus = 0
self.buffsize = 1024
self.allClients = {}
self.counter = 0
def initUI(self):
self.root.title("Simple P2P Chat Client")
ScreenSizeX = self.root.winfo_screenwidth()
ScreenSizeY = self.root.winfo_screenheight()
self.FrameSizeX = 800
self.FrameSizeY = 700
FramePosX = (ScreenSizeX - self.FrameSizeX)/2
FramePosY = (ScreenSizeY - self.FrameSizeY)/2
self.root.geometry("%sx%s+%s+%s" % (self.FrameSizeX,self.FrameSizeY,FramePosX,FramePosY))
self.root.resizable(width=False, height=False)
padX = 10
padY = 10
parentFrame = Frame(self.root)
parentFrame.grid(padx=padX, pady=padY, stick=E+W+N+S)
ipGroup = Frame(parentFrame)
serverLabel = Label(ipGroup, text="Set: ")
self.nameVar = StringVar()
self.nameVar.set("SDH")
nameField = Entry(ipGroup, width=10, textvariable=self.nameVar)
self.serverIPVar = StringVar()
self.serverIPVar.set("127.0.0.1")
serverIPField = Entry(ipGroup, width=15, textvariable=self.serverIPVar)
self.serverPortVar = StringVar()
self.serverPortVar.set("8090")
serverPortField = Entry(ipGroup, width=5, textvariable=self.serverPortVar)
serverSetButton = Button(ipGroup, text="Set", width=10, command=self.handleSetServer)
addClientLabel = Label(ipGroup, text="Add friend: ")
self.clientIPVar = StringVar()
self.clientIPVar.set("127.0.0.1")
clientIPField = Entry(ipGroup, width=15, textvariable=self.clientIPVar)
self.clientPortVar = StringVar()
self.clientPortVar.set("8091")
clientPortField = Entry(ipGroup, width=5, textvariable=self.clientPortVar)
clientSetButton = Button(ipGroup, text="Add", width=10, command=self.handleAddClient)
serverLabel.grid(row=0, column=0)
nameField.grid(row=0, column=1)
serverIPField.grid(row=0, column=2)
serverPortField.grid(row=0, column=3)
serverSetButton.grid(row=0, column=4, padx=5)
addClientLabel.grid(row=0, column=5)
clientIPField.grid(row=0, column=6)
clientPortField.grid(row=0, column=7)
clientSetButton.grid(row=0, column=8, padx=5)
readChatGroup = Frame(parentFrame)
self.receivedChats = Text(readChatGroup, bg="white", width=60, height=30, state=DISABLED)
self.friends = Listbox(readChatGroup, bg="white", width=30, height=30)
self.receivedChats.grid(row=0, column=0, sticky=W+N+S, padx = (0,10))
self.friends.grid(row=0, column=1, sticky=E+N+S)
writeChatGroup = Frame(parentFrame)
self.chatVar = StringVar()
self.chatField = Entry(writeChatGroup, width=80, textvariable=self.chatVar)
sendChatButton = Button(writeChatGroup, text="Send", width=10, command=self.handleSendChat)
self.chatField.grid(row=0, column=0, sticky=W)
sendChatButton.grid(row=0, column=1, padx=5)
self.statusLabel = Label(parentFrame)
bottomLabel = Label(parentFrame, text="P2P Chatting Application which uses message verification using ECDSA")
ipGroup.grid(row=0, column=0)
readChatGroup.grid(row=1, column=0)
writeChatGroup.grid(row=2, column=0, pady=10)
self.statusLabel.grid(row=3, column=0)
bottomLabel.grid(row=4, column=0, pady=10)
def handleSetServer(self):
if self.serverSoc != None:
self.serverSoc.close()
self.serverSoc = None
self.serverStatus = 0
serveraddr = (self.serverIPVar.get().replace(' ',''), int(self.serverPortVar.get().replace(' ','')))
try:
self.serverSoc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#print "hi"
self.serverSoc.bind(serveraddr)
self.serverSoc.listen(5)
self.setStatus("Server listening on %s:%s" % serveraddr)
thread.start_new_thread(self.listenClients,())
self.serverStatus = 1
#print "1"
self.genKeys()
#print "2"
self.name = self.nameVar.get().replace(' ','')
if self.name == '':
self.name = "%s:%s" % serveraddr
except:
self.setStatus("Error setting up server")
def listenClients(self):
while 1:
clientsoc, clientaddr = self.serverSoc.accept()
self.setStatus("Client connected from %s:%s" % clientaddr)
self.addClient(clientsoc, clientaddr)
clientsoc.send(self.pubKey.to_string())
thread.start_new_thread(self.handleClientMessages, (clientsoc, clientaddr))
self.serverSoc.close()
def handleAddClient(self):
if self.serverStatus == 0:
self.setStatus("Set server address first")
return
clientaddr = (self.clientIPVar.get().replace(' ',''), int(self.clientPortVar.get().replace(' ','')))
try:
clientsoc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsoc.connect(clientaddr)
self.setStatus("Connected to client on %s:%s" % clientaddr)
self.addClient(clientsoc, clientaddr)
clientsoc.send(self.pubKey.to_string())
thread.start_new_thread(self.handleClientMessages, (clientsoc, clientaddr))
except:
self.setStatus("Error connecting to client")
def handleClientMessages(self, clientsoc, clientaddr):
flag=0
while (flag==0):
data=clientsoc.recv(48)
if (len(data)==48):
self.veriKey= VerifyingKey.from_string(data,curve=NIST192p)
print "len of verikey:" + str(len(self.veriKey.to_string()))
flag=1
while True:
try:
data = clientsoc.recv(self.buffsize)
if not data:
break
sign= data[0:48]
msg= data[48:]
try:
self.veriKey.verify(sign,msg)
self.addChat("%s:%s" % clientaddr, msg)
except:
self.setStatus("Signatures not verified. Exit the chat")
except:
break
self.removeClient(clientsoc, clientaddr)
clientsoc.close()
self.setStatus("Client disconnected from %s:%s" % clientaddr)
def handleSendChat(self):
if self.serverStatus == 0:
self.setStatus("Set server address first")
return
msg = self.chatVar.get()
data=msg
if msg == '':
return
msg = self.priKey.sign(msg)+ msg
self.addChat("me", data)
for client in self.allClients.keys():
client.send(msg)
def addChat(self, client, msg):
self.receivedChats.config(state=NORMAL)
self.receivedChats.insert("end",client+": "+msg+"\n")
self.receivedChats.config(state=DISABLED)
def addClient(self, clientsoc, clientaddr):
self.allClients[clientsoc]=self.counter
self.counter += 1
self.friends.insert(self.counter,"%s:%s" % clientaddr)
def removeClient(self, clientsoc, clientaddr):
print self.allClients
self.friends.delete(self.allClients[clientsoc])
del self.allClients[clientsoc]
print self.allClients
def setStatus(self, msg):
self.statusLabel.config(text=msg)
print msg
def genKeys(self):
#print "11"
self.priKey = SigningKey.generate(curve=NIST192p)
#print "22"
self.pubKey = self.priKey.get_verifying_key()
#print "33"
#open("priv_key.pem",'w').write(priv_key.to_pem())
#open("pub_key.pem",'w').write(pub_key.to_pem())
def main():
root = Tk()
app = ChatClient(root)
root.mainloop()
if __name__ == '__main__':
main()