-
Notifications
You must be signed in to change notification settings - Fork 1
/
gossip.py
343 lines (283 loc) · 10.9 KB
/
gossip.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
import time
import signal
import json
import threading
import socket
import rsa
from filemanager import FileManager
import encryption
from random import choice
from twisted.internet.protocol import Factory, Protocol
from twisted.internet import reactor
from twisted.protocols.basic import LineReceiver
from subprocess import Popen, PIPE
signal.signal(signal.SIGINT, signal.SIG_DFL)
def dict_convert(dic, item):
newdic = {}
if item != None:
newdic[json.dumps(item)] = 100
for key, val in dic.items():
newdic[json.dumps(key)] = val
return newdic
def dict_unconvert(dic):
newdic = {}
for key, val in dic.items():
item = json.loads(key)
if item[0] == 'chunk':
item[3] = decrypt(item[3])
for i in range(len(item)):
if type(item[i]) == type([]):
item[i] = tuple(item[i])
newdic[tuple(item)] = val
return newdic
def decrypt(msg):
msg = str(msg)
result = []
for i in xrange(0, len(msg), 2):
result.append(chr(int(msg[i:i+2], 16)))
data = ''.join(result)
start = 0
block_sz = 256
result = []
while start < len(data) - 1:
block = data[start: start + block_sz]
p = Popen(['openssl', 'rsautl', '-decrypt', '-inkey',
'key.pem'], stdin=PIPE, stdout=PIPE)
out, err = p.communicate(block)
result.append(out)
start += block_sz
return ''.join(result)
class NodeServer(LineReceiver, threading.Thread):
hosts = set()
gossiper = None
cache = ''
def __init__(self):
threading.Thread.__init__(self)
def run(self):
reactor.listenTCP(7060, NodeFactory())
reactor.run(installSignalHandlers=0)
def connectionMade(self):
#client = self.transport.getPeer().host
self.setLineMode()
def dataReceived(self, line):
if line[-1] != '}':
self.cache += line
return
elif self.cache != '':
self.cache += line
line = self.cache
self.cache = ''
self.gossiper.process_gossip(dict_unconvert(json.loads(line)))
else:
self.gossiper.process_gossip(dict_unconvert(json.loads(line)))
def lineReceived(self, line):
print "LLLLLLLLLLLLLLLLLL", line
def rawDataReceived(self, data):
print "DDDDDDDDDDDDDDDDDDDDD", data
def add_host(self, host):
self.hosts.add(host)
class NodeFactory(Factory):
protocol = NodeServer
class GossipServer:
"""
Possible Gossip Information:
File Request -
('filereq', {destination_ip}, {filename}, {manager-ip})
Send Chunk -
('send_chunk', {destination_ip}, {start}, {end}, filereq)
Chunk -
('chunk', {start}, {end}, {data}, filereq)
Has File -
('has_file', {source_ip}, {filesize}, filereq)
"""
def __init__(self, bootstrapper):
self.server = NodeServer()
NodeServer.gossiper = self
self.server.start()
self.filemanager = FileManager()
self.file_lock = threading.RLock()
self.manager = ManagerNode(self)
self.bootstrapper = bootstrapper
self.hosts = bootstrapper.hosts
self.gossip_queue = []
self.gossip_dict = {}
self.current_gossip = set()
self.timed_gossip()
self.timed_hostcheck()
print "Gossip Server Started..."
@classmethod
def encrypt(self, msg, key):
data = str(msg)
start = 0
block_sz = 224
result = []
fout = open("/tmp/pub-key.pem", 'w')
fout.write(key)
fout.close()
while start < len(data) - 1:
block = data[start:start + block_sz]
p = Popen(['openssl', 'rsautl', '-encrypt', '-inkey',
'/tmp/pub-key.pem', '-pubin'], stdin=PIPE, stdout=PIPE)
out, err = p.communicate(block)
result.append(out)
start += block_sz
expand = []
for c in result:
for char in c:
expand.append(ord(char))
return ''.join([(hex(c)[2:] if len(hex(c)) == 4
else '0' + hex(c)[2:]) for c in expand])
def process_gossip(self, data):
#print "PROCESS:", data
for item, ttl in data.items():
if item not in self.current_gossip:
self.current_gossip.add(item)
print "\tProcessing Gossip:", item, self.gossip_dict
if item[0] == 'filereq':
file_offer = self.gen_file_offer(item)
if file_offer:
print "\tHave File:", item
manager_ip = item[3]
if manager_ip == self.bootstrapper.myip:
self.manager.manage(file_offer)
else:
self.gossip_queue.append((manager_ip, file_offer))
elif item[0] == 'chunk':
print "RECEIVED CHUNK"
tag, start, end, data, filereq = item
tag, destip, filename, mip = filereq
self.file_lock.acquire()
self.filemanager.receive_chunk('files/' + filename, start, end, data)
self.file_lock.release()
elif item[0] == 'send_chunk':
tag, dest_ip, start, end, filereq = item
tag, destip, filename, mip = filereq
file_chunk = self.filemanager.find_chunk('files/' + filename, start, end)
if destip in self.hosts:
encrypted_chunk = self.encrypt(file_chunk, self.hosts[destip])
chunk_descriptor = ('chunk', start, end, encrypted_chunk, filereq)
self.gossip_queue.append((destip, chunk_descriptor))
else:
print "NNNOOOOOTTTTT", self.hosts, destip
elif item[0] == 'has_file':
self.manager.manage(item)
def send_chunk_request(self, req, ip):
if ip == self.bootstrapper.myip:
self.process_gossip({req : 100})
else:
self.gossip_queue.append((ip, req))
def gen_file_offer(self, item):
tag, dest_ip, filename, manager_ip = item
self.gossip_dict[(tag, dest_ip, filename, manager_ip)] = 100
filesize = self.filemanager.find_file('files/' + filename)
if filesize != None:
return ('has_file', self.bootstrapper.myip, filesize, item)
return None
def init_file_request(self, filename):
print "You requested:", filename
manager = self.choose_random_host()
while manager == self.bootstrapper.myip:
manager = self.choose_random_host()
filereq = ('filereq', self.bootstrapper.myip, filename, manager)
self.gossip_dict[filereq] = 100
def timed_gossip(self):
self.gossip()
threading.Timer(3, self.timed_gossip, ()).start()
def timed_lease_check(self):
#threading.Timer(30, self.timed_lease_check, ()).start()
pass
def timed_hostcheck(self):
self.hosts = self.bootstrapper.hosts
#for ip, pkey in self.bootstrapper.hosts.items():
# self.hosts[ip] = pkey
threading.Timer(3, self.timed_hostcheck, ()).start()
def choose_random_host(self):
if len(self.hosts) == 0:
return None
host_list = self.hosts.keys()
return choice(host_list)
def gossip(self):
if len(self.gossip_queue) > 0:
host, item = self.gossip_queue.pop(0)
else:
host = self.choose_random_host()
item = None
if not host:
return
self.send(host, item)
def gossip_data(self, item):
return json.dumps(dict_convert(self.gossip_dict, item))
def send(self, host, item):
try:
data = self.gossip_data(item)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
s.connect((host, 7060))
s.send(data)
except Exception as e:
###print "EXCEPTION IN SEND:", str(e), "\n\tFOR HOST:", host
pass
class ManagerNode(GossipServer):
chunk_size = 1024
files_to_process = {}
def __init__ (self, gossiper):
self.gossiper = gossiper
threading.Timer(5, self.process_chunk_requests, ()).start()
def manage(self, item):
# Register each file that this node should be a manager of
# Register every node that tells the manager that it is the source
print "MANAGEMNT STARTED:", item
tag, source_ip, filesize, filereq = item
if filereq not in self.files_to_process:
print "Initializing managemt of:", filereq
self.files_to_process[filereq] = [0, filesize]
self.files_to_process[filereq].append(source_ip)
def process_chunk_requests(self):
for filereq_to_process, filereq_info in self.files_to_process.items():
if not filereq_info:
continue
amount_processed = filereq_info[0]
filesize = filereq_info[1]
file_holders = filereq_info[2:]
finished = False
for file_containing_node in file_holders:
print "Asking Node to send a chunk:", file_containing_node, file_holders
start_byte_number = amount_processed
end_byte_number = amount_processed + self.chunk_size
if end_byte_number > filesize:
end_byte_number = filesize
if filereq_to_process in self.files_to_process:
del self.files_to_process[filereq_to_process]
finished = True
amount_processed = end_byte_number+1
chunk_request = ('send_chunk', filereq_to_process[1],
start_byte_number, end_byte_number,
filereq_to_process)
self.gossiper.send_chunk_request(chunk_request, file_containing_node)
print "SENDING CHUNK:", start_byte_number, end_byte_number
newval = [amount_processed, filesize] + file_holders
if not finished:
self.files_to_process[filereq_to_process] = newval
threading.Timer(2, self.process_chunk_requests, ()).start()
def rand_string():
import random
import string
s = ""
for i in xrange(1, random.randrange(1, 8)):
s += random.choice(string.lowercase)
s *= random.randint(2, 500)
return s
if __name__ == "__main__":
#encryption.make_key()
#for i in xrange(1000):
# s = rand_string()
# e = GossipServer.encrypt(s, 123)
# r = GossipServer.decrypt(e)
# assert s != e and e != r and s == e
# print i,
# if i % 20 == 0: print
a = {('ed', (1, 2, 3), 'fred') : 100, ('chunk', 1, (2, 3), 'cc') : 100}
b = dict_convert(a, None)
c = json.dumps(b)
w = dict_unconvert(json.loads(c))
print w