/
bytemail.py
289 lines (277 loc) · 9.17 KB
/
bytemail.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
#!/usr/bin/python
import socket
import db
import checkin
import message
import read
import check
import json
import threading
import os
import uuid
import cmd
import thread
import unsent
import delete
import ssl
import get_messages
import get_nodes
import time
import random
import rsa
import addressbook
import sent
import bytemailgui
import webbrowser
__version__ = "0.4.6"
GUI = True
class ByteMail:
def __init__(self, addr, pubkey):
self.cmds = {
"delete":delete.delete,
"checkin":checkin.checkin,
"message":message.message,
"get_messages":get_messages.get_messages,
"get_nodes":get_nodes.get_nodes,
}
self.broker = ("198.147.20.190", 4321)
self.addr = addr
self.pubkey = pubkey
self.port = 5333
self.host = "0.0.0.0"
self.open_port = False
self.config = {
"relay":False
}
def main(self):
if not db.nodes.find("nodes", "all"):
check = self.config['relay']
if check:
self.open_port = True
print "You are running as a relay node."
db.data.insert("data", {"port":self.port})
print "Downloading Nodes..."
self.get_nodes()
print "Checking in to nodes..."
self.send_checkin()
print "Done!"
check = self.config['relay']
if check:
self.open_port = True
print "You are running as a relay node."
else:
print "You are not running as a relay node."
if self.open_port:
sock = ssl.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((self.host, self.port))
sock.listen(5)
while True:
obj, conn = sock.accept()
threading.Thread(target=self.handle, args=(obj, conn[0])).start()
else:
while True:
check = db.nodes.find("nodes", "all")
random.shuffle(check)
node = None
for x in check:
s = ssl.socket()
try:
s.connect((x['ip'], x['port']))
except:
s.close()
continue
else:
node = (x['ip'], x['port'])
s.close()
break
sock = ssl.socket()
try:
sock.connect(node)
except:
continue
else:
while True:
try:
get_messages.send_get_messages(x['ip'], x['port'])
get_nodes.send_get_nodes(x['ip'], x['port'])
time.sleep(10)
except Exception, error:
sock.close()
break
def handle(self, obj, ip):
try:
data = obj.recv(102400)
except:
obj.close()
return
if data:
try:
data = json.loads(data)
except ValueError:
obj.close()
else:
try:
self.cmds[data['cmd']](obj, ip, data)
except KeyError:
pass
def send_checkin(self):
nodes = db.nodes.find("nodes", "all")
for x in nodes:
s = ssl.socket()
try:
s.settimeout(1)
s.connect((x['ip'], x['port']))
s.send(json.dumps({"cmd":"checkin", "addr":self.addr, "port":self.port, "publickey":self.pubkey}))
s.close()
except Exception, error:
s.close()
db.unsent.insert("unsent", {"to":[x['ip'], x['port']], "message":{"cmd":"checkin", "addr":self.addr, "port":self.port, "publickey":self.pubkey}})
def get_nodes(self):
send = {"addr":self.addr, "port":self.port, "publickey":self.pubkey}
s = ssl.socket()
s.connect(self.broker)
s.send(json.dumps(send))
with open("nodes.db", 'wb') as file:
while True:
data = s.recv(1024)
if data:
file.write(data)
else:
break
print "Downloaded Nodes!"
class Prompt(cmd.Cmd):
prompt = "ByteMail$ "
def do_send(self, line):
self.lastcmd = ""
addr = db.data.find("data", "all")[0]['addr']
to = raw_input("To: ")
if to == "":
return "You must fill out the field."
if len(to) != 32:
if len(str(addressbook.check_entry(to))) != 32:
print "Address Invalid."
return
else:
to = str(addressbook.check_entry(to))
title = raw_input("Title: ")
msg = raw_input("Message: ")
if not to or not title or not msg:
print "You need to fill out all the fields."
else:
print message.send_msg(msg, title, to, addr)
def do_version(self, line):
print __version__
self.lastcmd = ""
def help_send(self):
print "Sends a message."
self.lastcmd = ""
def do_check(self, line):
addr = db.data.find("data", "all")[0]['addr']
check_ = check.check(addr)
if not check_:
print "You have no messages."
else:
for x in check_:
print x
self.lastcmd = ""
def help_check(self):
print "Displays a list of messages"
self.lastcmd = ""
def do_read(self, id):
addr = db.data.find("data", "all")[0]['addr']
print read.read(id, addr).decode("utf-8")
self.lastcmd = ""
def help_read(self):
print "Usage: read <id>"
print "Reads the specified message ID"
self.lastcmd = ""
def do_addr(self, line):
addr = db.data.find("data", "all")[0]['addr']
print "Your address is:", addr
self.lastcmd = ""
def help_addr(self):
print "Displays your ByteMail address"
self.lastcmd = ""
def do_add_address(self, line):
self.lastcmd = ""
name = raw_input("Name: ")
address = raw_input("Address: ")
addressbook.add_entry(name,address)
def help_addresses(self, line):
self.lastcmd = ""
print "Shows all addresses in your address book"
def do_addresses(self, line):
self.lastcmd = ""
print addressbook.addresses()
def help_add_address(self):
print "Adds an address to your address book"
def do_delete(self, line):
self.lastcmd = ""
addr = db.data.find("data", "all")[0]['addr']
print delete.send_delete(line, addr)
def help_delete(self):
self.lastcmd = ""
print "Usage: delete <id>"
print "Deletes the message with the specified ID"
def do_purge(self,line):
print "Purging..."
addr = db.data.find("data", "all")[0]['addr']
print delete.send_delete_all(addr)
def help_purge(self):
print "Deletes all messages sent to this node."
def do_exit(self, line):
print "Bye!"
exit()
def help_exit(self):
self.lastcmd = ""
print "Closes ByteMail."
def help_sent(self):
self.lastcmd = ""
print "Shows all sent messages."
def do_sent(self, line):
self.lastcmd = ""
print sent.sent()
def do_sent_delete(self, line):
self.lastcmd = ""
print sent.sent_delete(line)
def help_remove_address(self):
self.lastcmd = ""
print "Delete an address from your addressbook. Usage: remove_address <entryname>"
def do_remove_address(self, line):
self.lastcmd = ""
print addressbook.remove_address(line)
def do_sent_purge(self, line):
self.lastcmd = ""
sent_ = db.sent.find("sent", "all")
for x in sent_:
sent.sent_delete(x['id'])
print "Sent database purged."
if __name__ == "__main__":
exists = db.data.find("data", "all")
if not exists:
print "First time running ByteMail"
print "Generating new keys... This could take a while."
publickey, privatekey = rsa.newkeys(1024)
db.data.insert("data", {"addr":uuid.uuid4().hex, "publickey":str(publickey), "privatekey":str(privatekey)})
db.messages.insert("messages", {})
data = db.data.find("data", "all")[0]
addr = data['addr']
pubkey = data['publickey']
else:
data = db.data.find("data", "all")[0]
addr = data['addr']
pubkey = data['publickey']
b = ByteMail(addr, pubkey)
c = Prompt()
thread.start_new_thread(b.main, ())
thread.start_new_thread(unsent.unsent, ())
if not GUI:
while True:
try:
c.cmdloop()
except KeyboardInterrupt:
continue
else:
webbrowser.open("http://localhost:5334")
bytemailgui.run()