def handle(self): self.data = self.request.recv(1024).strip() self.jdata = json.loads(self.data) #Ping check if "type" in self.jdata: if self.jdata["type"] == "ping": self.request.send("[]") return ticket = json.loads(secure.decrypt_with_key(self.jdata["ticket"], password)) message = secure.decrypt_with_key(self.jdata["message"], ticket[0]).strip() #Note that there are all the file server which service that file #being returned, this allows the client to keep trying them until it finds #one which is live. It also allows the file server to push changes to each of the fileserver #when a change is detected. self.directory_data = dir_lookup(message) #Some weird list comprehensions, too much haskell on the brain. server_id = [ [host, port, file_name] for host, port, file_name, pwd in self.directory_data ] server_tickets = [ pwd for _,_,_,pwd in self.directory_data ] encrypted_ticket = [ secure.encrypt_with_key(json.dumps([ticket[0]]), pwd) for pwd in server_tickets ] client_msg = json.dumps(server_id) data = { "ticket":encrypted_ticket, "message":client_msg } #Encrypt response with session key self.request.send(secure.encrypt_with_key(json.dumps(data), ticket[0]))
def handle(self): resp = {"status_code" : 0} self.data = self.request.recv(1024).strip() try: self.jdata = json.loads(self.data) #This ping message is for servers trying to #make sure its live. if "type" in self.jdata: if self.jdata["type"] == "ping": self.request.send("[]") session = None return session = secure.decrypt_with_key(self.jdata["ticket"], password).strip() session = json.loads(session)[0] filename = secure.decrypt_with_key(self.jdata["filename"], session).strip() filename = secure.decrypt_with_key(self.jdata["filename"], session).strip() if "type" in self.jdata: if self.jdata["type"] == "ping": self.request.send("[]") return elif self.jdata["type"] == "unlock": if filename in locked_files: if locked_files[filename] == str(self.client_address[0]) + self.jdata["name"]: del locked_files[filename] resp["message"] = "Successfully unlocked {0}".format(filename) resp["status_code"] = 1 else: resp["message"] = "You don't have the correct permission to unlock this file." else: resp["message"] = "File is not locked so an unlock is not possible." elif self.jdata["type"] == "lock": if filename in locked_files: if locked_files[filename] == str(self.client_address[0]) + self.jdata["name"]: resp["message"] = "You already have the lock for {0}".format(filename) resp["status_code"] = 1 else: resp["message"] = "Filename {0} is locked. Unable to access.".format(filename) else: locked_files[filename] = str(self.client_address[0]) + self.jdata["name"] resp["message"] = "Filename {0} successfully locked.".format(filename) resp["status_code"] = 1 else: resp["message"] = "Incorrect message parameters." except ValueError: resp["message"] = "Incorrect incoming json message." except KeyError: resp["message"] = "Incorrect incoming json parameters." finally: if session: jdata= secure.encrypt_with_key(json.dumps(resp), session) else: jdata = json.dumps(resp) self.request.send(jdata)
def lookup_fs(data, local_file, server_id, password): received = None sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # Connect to server and send data sock.connect((server_id[0], int(server_id[1]))) content = json.dumps(data) content_length = json.dumps({"content-length": len(content)}) sock.send(content_length) received = sock.recv(10) sock.send(content) # Receive data from the server and shut down packet = None received = "" while 1: packet = sock.recv(1024) if not packet: break received += packet received = secure.decrypt_with_key(received, password) received = json.loads(received) except: sock.close() aslookups = {} dslookups = {} finally: sock.close() if "type" in received: if received["type"] == "read": contents = "" if received: contents = base64.b64decode(received["payload"]) try: if not os.path.exists(os.path.dirname(local_file)): os.makedirs(os.path.dirname(local_file)) f = open(local_file, "wb") fc = open(hidden_file_path(local_file), "wb") f.write(contents) fc.write(contents) f.close() fc.close() return "{0}".format(local_file) except OSError: return None elif received["type"] == "write": return "Success" elif received["type"] == "ping": if "error" in received: return received["error"] else: return received["mtime"] return None
def handle(self): content_length = self.request.recv(1024).strip() content_length = json.loads(content_length) if "type" in content_length: if content_length["type"] == "ping": self.request.send("[]") return content_length = content_length["content-length"] self.request.send("Got it") self.data = self.request.recv(int(content_length)).strip() self.jdata = json.loads(self.data) ticket = secure.decrypt_with_key(self.jdata["ticket"], password) ticket = json.loads(ticket) message = secure.decrypt_with_key(self.jdata["request"], ticket[0]) message = json.loads(message) if "type" in message: print "message type:{0}".format(message["type"]) if message["type"] == "open": self.handle_open(message, ticket[0]) elif message["type"] == "write": self.handle_write(message, ticket[0]) elif message["type"] == "changed": self.handle_ping(message, ticket[0])
def lookup_ds(message, server_id, ticket, session): data = {"message": secure.encrypt_with_key(message, session), "ticket": ticket} sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) received = None try: sock.connect((server_id[0], int(server_id[1]))) sock.send(json.dumps(data)) received = sock.recv(1024) received = secure.decrypt_with_key(received, session) finally: sock.close() if received: return received else: return None
def lookup_ls(filename, request, server_id, ticket, session, name): data = {"ticket": ticket, "filename": secure.encrypt_with_key(filename, session), "type": request, "name": name} sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) received = None try: sock.connect((server_id[0], int(server_id[1]))) sock.send(json.dumps(data)) received = sock.recv(1024) received = secure.decrypt_with_key(received, session) finally: sock.close() if received: # print received return json.loads(received) else: return None
def lookup_as(data, password, HOST, PORT): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) received = None try: # Connect to server and send data sock.connect((HOST, PORT)) sock.send(json.dumps(data)) # Receive data from the server and shut down received = sock.recv(1024) received = secure.decrypt_with_key(received, password) received = json.loads(received) finally: sock.close() if received: return (received["ticket"], received["session"], received["server_id"]) else: return None