class node1(object): logger = log() def __init__(self): pass def sendPayload(self, payload): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ssl_sock = ssl.wrap_socket(s, ca_certs="server.crt", cert_reqs=ssl.CERT_REQUIRED) ssl_sock.connect(('localhost', 10028)) ssl_sock.write(payload) self.logger.addLog('Node1', 'Node1 sent payload to Node2') def getPayload(self): title = 'Blade Runner' url = 'http://omdbapi.com/?' param = {'t': title, 'y': '', 'plot': 'short', 'r': 'json'} value = urllib.urlencode(param) response = urllib2.urlopen(url + value) payload = response.read() self.logger.addLog('Node1', 'Node1 recieved payload from OMDB') self.sendPayload(payload) #This is where we listen for the return message from node 4 def listenForPayload(self): connection = pika.BlockingConnection( pika.ConnectionParameters(host='localhost')) channel = connection.channel() channel.queue_declare(queue='node4Message') #The message is parsed and decrypted def callback(ch, method, properties, body): aesObj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456') message = aesObj.decrypt(body) self.logger.addLog('Node1', 'Node1 recieved payload from Node4') print("Node1 received %r" % message) self.logger.printAll('default') self.logger.clearAll('default') channel.basic_consume(callback, queue='node4Message', no_ack=True) self.logger.addLog('Node1', 'Node1 listening') channel.start_consuming()
class RemoteNode(object): logger = log() @Pyro4.oneway def recievePayload(self, payload): self.logger.addLog('Node4', 'Node4 recieved message') #self.decompress(payload) self.sendPayload(payload) #send the encrypted message to node1 via rabbitmq def sendPayload(self, payload): connection = pika.BlockingConnection( pika.ConnectionParameters(host='localhost')) channel = connection.channel() channel.queue_declare(queue='node4Message') self.logger.addLog('Node4', "Node4 sent message to Node1") payload = self.encryptAES(payload) channel.basic_publish(exchange='', routing_key='node4Message', body=payload) connection.close() def decompress(self, fileCRC): #fileCRC = open(location+'.json','rb') payload = zlib.decompress(fileCRC) self.logger.addLog('Node4', 'Node4 uncompressed message') self.sendPayload(payload) #encrypt payload with AES def encryptAES(self, payload): pad = b' ' obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456') #plaintext = message.encode('utf-8') #print(plaintext) length = 16 - (len(payload) % 16) #print(length) payload += length * pad ciphertext = obj.encrypt(payload) self.logger.addLog('Node4', 'Node4 encrypted with AES') return ciphertext
class node2(object): logger = log() run = True def __init__(self): pass def listenForPayload(self): bindsocket = socket.socket() bindsocket.bind(('', 10028)) self.logger.addLog('Node2', 'Node2 listening') bindsocket.listen(5) run = True def do_something(connstream, data): self.logger.addLog('Node2', 'Node2 recieved payload from Node1') self.sendPayload(data) self.run = False return False def deal_with_client(connstream): data = connstream.read() while data: if not do_something(connstream, data): break data = connstream.read() while self.run: newsocket, fromaddr = bindsocket.accept() connstream = ssl.wrap_socket(newsocket, server_side=True, certfile="server.crt", keyfile="server.key") try: deal_with_client(connstream) finally: connstream.shutdown(socket.SHUT_RDWR) connstream.close() def sendPayload(self, data): #creates new file with payload file = open('IvanIakimenko.json', 'w') data = self.calculateChecksum(data) file.write(data) file.close() self.logger.addLog('Node2', 'Node2 stored payload in file') self.sftpPut() def calculateChecksum(self, data): checksum = hashlib.md5(data.encode()).hexdigest() self.logger.addLog('Node2', 'Node2 calculated checksum') return data + '\n' + checksum def sftpPut(self): time.sleep(0.2) cnopts = pysftp.CnOpts() cnopts.hostkeys = None cinfo = { 'cnopts': cnopts, 'host': 'oz-ist-linux.abington.psu.edu', 'username': '******', 'password': '******', 'port': 109 } try: with pysftp.Connection(**cinfo) as sftp: try: with sftp.cd('/home/ftpuser'): sftp.put('/home/ftpuser/IvanIakimenko.json') self.logger.addLog( 'Node2', 'Node2 sftp file into Node3 directory') except: self.logger.addLog('Node2', 'file issue') except: self.logger.addLog('Node2', 'connection issue')
class node3(object): logger = log() run = True def __init__(self): pass def listenForPayload(self): self.logger.addLog('Node3', 'Node3 listening') cnopts = pysftp.CnOpts() cnopts.hostkeys = None cinfo = { 'cnopts': cnopts, 'host': 'oz-ist-linux.abington.psu.edu', 'username': '******', 'password': '******', 'port': 109 } while (self.run): try: with pysftp.Connection(**cinfo) as sftp: if sftp.isfile('/home/ftpuser/IvanIakimenko.json'): try: file = sftp.open( '/home/ftpuser/IvanIakimenko.json', 'r', -1) self.logger.addLog('Node3', 'Node3 oppened sftp file') lines = file.readlines() payload = lines[0].rstrip('\n') if (self.checkChecksum(payload, lines[1])): self.sendPayload(payload) sftp.remove('/home/ftpuser/IvanIakimenko.json') self.run = False except: self.logger.addLog('Node3', 'Node3 file access error') except: self.logger.addLog('Node3', 'Node3 sftp connection error') def checkChecksum(self, payload, checksum): checksumGen = hashlib.md5(payload.encode()).hexdigest() if checksum == checksumGen: self.logger.addLog('Node3', 'Node3 passed checksum test') return True else: self.logger.addLog('Node3', 'Node3 failed checksum test') print checksum + '!=' + checksumGen return False def compress(self, payload): compPayload = zlib.compress(payload.encode('utf-8'), 9) self.logger.addLog('Node3', 'Node3 compressed payload') checksum = zlib.crc32(payload) compChecksum = zlib.crc32(compPayload) print 'Node3 checksum:' + str(checksum) + ' compressed:' + str( compChecksum) return compPayload def sendPayload(self, payload): #compPayload = self.compress(payload) #file = open('compressed_payload.json','wb') #file.write(compPayload) #file.close() remoteNode = Pyro4.Proxy( "PYRONAME:node4") # use name server object lookup uri shortcut remoteNode.recievePayload(payload) self.logger.addLog('Node3', 'Node3 sent payload to Node4')
class node4(object): logger = log() def __init__(self): pass #receive message from node3 via pyro def listenForPayload(self): @Pyro4.expose class RemoteNode(object): logger = log() @Pyro4.oneway def recievePayload(self, payload): self.logger.addLog('Node4', 'Node4 recieved message') #self.decompress(payload) self.sendPayload(payload) #send the encrypted message to node1 via rabbitmq def sendPayload(self, payload): connection = pika.BlockingConnection( pika.ConnectionParameters(host='localhost')) channel = connection.channel() channel.queue_declare(queue='node4Message') self.logger.addLog('Node4', "Node4 sent message to Node1") payload = self.encryptAES(payload) channel.basic_publish(exchange='', routing_key='node4Message', body=payload) connection.close() def decompress(self, fileCRC): #fileCRC = open(location+'.json','rb') payload = zlib.decompress(fileCRC) self.logger.addLog('Node4', 'Node4 uncompressed message') self.sendPayload(payload) #encrypt payload with AES def encryptAES(self, payload): pad = b' ' obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456') #plaintext = message.encode('utf-8') #print(plaintext) length = 16 - (len(payload) % 16) #print(length) payload += length * pad ciphertext = obj.encrypt(payload) self.logger.addLog('Node4', 'Node4 encrypted with AES') return ciphertext daemon = Pyro4.Daemon() # make a Pyro daemon ns = Pyro4.locateNS() uri = daemon.register( RemoteNode) # register the greeting maker as a Pyro object ns.register("node4", uri) self.logger.addLog('Node4', 'Node4 listening') daemon.requestLoop( ) # start the event loop of the server to wait for calls