def get_input(self): path_short_form = self.unionpath.create_short_cwd(True) if self.unionpath.connected: message = color.bold( color.green('● ' + self.unionpath.user_name + "@{}".format(self.unionpath.client.IP)) ) + ":" + color.bold(color.blue(path_short_form) + '$ ') else: message = color.bold( color.grey('● ') + color.green(self.unionpath.user_name) ) + ":" + color.bold(color.blue(path_short_form) + '$ ') message = input(message) cmds = message.split() result = self._handle(cmds) if result: if (isinstance(result, tuple)): if len(result) == 2: self.protocol.handle(result[0], additional=result[1]) else: if len([result]) == 1: result = self.protocol.handle(result) if result == "END": return result elif len([result]) == 2: self.protocol.handle(result[0], additional=result[1])
def assign_new_hashes_dir(dir_path, unionpath): objects = [] for (dirpath, dirnames, filenames) in os.walk(dir_path): for file in filenames: objects.append(os.path.join(dirpath, file)) for dir in dirnames: objects.append(os.path.join(dirpath, dir)) for object in objects: hash = object.split(os.sep)[-1] path = object.replace(os.sep + hash, "") info = unionpath.translate_from_hash(hash) name = info[0] type = info[2] extension = info[5] hash = unionpath.create_hash(os.path.join(path, name)) new = os.path.join(path, hash) print(color.green("{} -> {}".format(object, new))) os.rename(object, new) for i in range(len(objects)): objects[i] = objects[i].replace(object, new) fs_loc = unionpath.hashpath_to_fspath(path) unionpath.add_to_dictionary(hash, name, type, path, fs_loc, extension=extension)
def make_directory(self, new_directory): path = self.unionpath.current_folder dirs = None flag = False if os.sep in new_directory: flag = True dirs = new_directory.split(os.sep) for i in range(len(dirs) - 1): print(dirs[i]) dirs[i] = self.unionpath.translate_to_hash( dirs[i], path, False) if not dirs[i]: return path = os.path.join(path, dirs[i]) new_directory = new_directory.split(os.sep)[-1] if flag: print(color.green(new_directory)) for i in range(len(dirs) - 1): os.chdir(dirs[i]) fs_loc = self.unionpath.hashpath_to_fspath(os.getcwd()) hashname = self.unionpath.create_hash( os.path.join(os.getcwd(), new_directory)) dir_path = os.path.join(os.getcwd(), hashname) os.mkdir(dir_path) self.unionpath.add_to_dictionary(hashname, new_directory, "directory", os.getcwd(), fs_loc) os.chdir(self.unionpath.current_folder) return hashname
def get_input(self): path_short_form = self.unionpath.create_short_cwd(True) if self.unionpath.connected: message = color.bold(color.green('● ' + self.unionpath.user_name + "@{}".format(self.unionpath.client.IP))) + ":" + color.bold( color.blue(path_short_form) + '$ ') else: message = color.bold(color.grey('● ') + color.green(self.unionpath.user_name)) + ":" + color.bold( color.blue(path_short_form) + '$ ') message = input(message) cmds = message.split() result = self._handle(cmds) if result: result = self.protocol.handle(result) if result == "END": return result
def get_file(self, msg_spl): flag = False src = msg_spl[1] dst = msg_spl[2] print(color.purple("ADD {} {}".format(src, dst))) dir = self._get_dir(dst) filename = self._get_filename(src) extension = os.path.splitext(filename)[1] path = os.path.join(self.hash, dir) filepath = create.file_path_in_filesystem(os.path.join(path, filename)) print(color.green(filename)) with open(filepath, "wb") as file: while True: bytes = self.conn.recv(BUFFER_SIZE) file.write(bytes) if bytes.strip()[-3:] == b'EOF': break elif bytes.strip()[-3:] == b'INT': flag = True break file.close() if flag: os.remove(filepath) return "NCADD" timestamp = math.trunc(datetime.timestamp(datetime.now())) hashname = hash_.get_hash_from_string(filepath + str(timestamp)) new_filepath = filepath.replace(filename, "{}{}".format(hashname, extension)) timestamp = self.add_to_dict(hashname, filename, "file", extension=extension, timestamp=timestamp) os.rename(filepath, new_filepath) try: os.remove(filepath) except: None return "CADD {} {}".format(hashname, timestamp)
def help(): ''' USE: help.help() Description: returns the '--help' text Notes: if you add extra operators, make sure to add them here ad well as in help.spfc_opr() and the error.get() dictionary and the help.dct() dictionary. ''' css_1 = '\n\n ' css_2 = '\n ' commands = [ 'reg', 'con', 'cd', 'open', 'ls', 'srv', 'mk', 'add', 'rm', 'mt', 'mv', 'cp', 'rn', 'f', '--help', 'quit', 'clear', 'exp' ] help = color.green('this is the --help section:') for cmd in commands: help += css_1 + '» ' + color.bold(str( spfc_opr(cmd, True))) + color.cyan(css_2 + 'aliases: ' + str( spfc_opr(cmd))) + css_2 + '' + color.yellow( dct(cmd)[0]) + css_2 + '' + color.grey(dct(cmd)[1]) return help
def info(word, debug_print=True): msg = "[+] %s\n" % color.green(word) if not debug_print: return log.my_log(msg, word)
def mount(client, user, fs_name, dst): try: msg = "MNT_USER {} {} {}".format(user, client.filesystem_hash, fs_name) client.send(msg) user_matches = client.server_socket.recv(2048) user_matches = ast.literal_eval(user_matches.decode("utf-8")) #print(user_matches) if user_matches == "DMNT": return False if len(user_matches) == 0: user_choice = "None" elif len(user_matches) == 1: user_choice = user_matches[0][0] else: msg = "" #print(color.yellow("Multiple possibilities found:")) for i in range(len(user_matches) - 1): msg += color.yellow("[{}] {}@{} -> {} [{}]\n".format( i + 1, user_matches[i][1], user_matches[i][2], user_matches[i][0], fs_name)) msg += color.yellow("[{}] {}@{} -> {} [{}]".format( len(user_matches) - 1 + 1, user_matches[len(user_matches) - 1][1], user_matches[len(user_matches) - 1][2], user_matches[len(user_matches) - 1][0], fs_name)) while True: print(msg) try: choice = input( color.bold( color.green('● ' + client.user + "@{}".format(client.IP))) + color.purple(":{} -> ".format(user))) choice = int(choice) if choice >= 1 and choice <= len(user_matches): choice -= 1 break else: print( color.red( "Please enter a number between {} and {}.". format(1, len(user_matches)))) except: print( color.red( "Please enter a number between {} and {}.".format( 1, len(user_matches)))) user_choice = user_matches[choice][0] if user_choice in client.get_mounts(): print(color.yellow("This filesystem is already mounted")) return False client.send(user_choice) root_mount = client.server_socket.recv(2048) root_mount = root_mount.decode("utf-8").split() if not root_mount[0] == "DIR": return False root_dir_hash, time = root_mount[1], root_mount[2] client.add_to_dict(root_dir_hash, fs_name, "directory", dst, timestamp=time) path = client.mk(dst, root_dir_hash) client.send("DIR_DONE") dir_batch = [] fs_path_root = fs_name while True: dir_info = client.server_socket.recv(2048) dir_info = dir_info.decode("utf-8").split() if dir_info[0] == "DIR": dir_batch.append([ dir_info[1][1:], dir_info[2], dir_info[3], dir_info[4], dir_info[5] ]) client.send("DONE") elif dir_info[0] == "DIR_DONE": #print(color.green("GOT ALL")) break hashdict = {} for i in range(len(dir_batch)): dir_path, dir_hash, name, time, type = dir_batch[i] #print("DIR {} {} {} {} {} ".format(dir_path, dir_hash, name, time, type)) hashdict.update({dir_hash: name}) client.mk(path, dir_path) if os.sep not in dir_path: dir_path = dir_path.replace(dir_hash, "") else: dir_path = dir_path.replace(dir_hash + os.sep, "") if not dir_path: # hash, name, type, location, extension="", fs_path = "", timestamp=None client.add_to_dict(dir_hash, name, type, path, fs_path=fs_path_root, timestamp=time) else: fs_path = dir_path dirs = dir_path.split(os.sep) #print(color.purple(fs_path)) #print(hashdict) for dir in dirs: fs_path = fs_path.replace(dir, hashdict.get(dir)) fs_path = fs_path.replace(os.sep + name, "") client.add_to_dict(dir_hash, name, type, os.path.join( path, dir_path.replace(os.sep + dir_hash, "")), fs_path=os.path.join(fs_path_root, fs_path), timestamp=time) client.send("DIRS_DONE") #print(color.green("DIRS_DONE SENT")) filehashes = [] while True: file_info = client.server_socket.recv(2048) file_info = file_info.decode("utf-8").split() if file_info[0] == "CMNT": return True, hashdict, filehashes, root_dir_hash elif len(file_info) != 7: continue #print(file_info) file_fs_path, file_hash, name, time, type, extension = file_info[ 1], file_info[2], file_info[3], file_info[4], file_info[ 5], file_info[6] if file_info[0] == "FILE": #print(color.green("SEND CFILE")) client.send("CFILE") filepath = os.path.join(path, file_fs_path) with open(filepath, "wb") as file: while True: bytes = client.server_socket.recv(BUFFER_SIZE) file.write(bytes) if bytes.strip()[-3:] == b'EOF': break elif bytes.strip()[-3:] == b'INT': return False file.close() if extension == "None": extension = "" abs_path = os.path.join(path, file_fs_path.replace( root_dir_hash, "")).replace( os.sep + file_hash + extension, "") file_fs_path = os.path.join(fs_path_root, file_fs_path).replace( os.sep + file_hash + extension, "") client.add_to_dict(file_hash, name, type, abs_path, fs_path=file_fs_path, timestamp=time, extension=extension) #print(color.green("SEND CCFILE")) filehashes.append(file_hash) client.send("CCFILE") except: return False
def send_mount_instruction(self, info): info = info.split() if info[1] == "upload": self.client.send("MNT-U {} {} {}".format(info[2], info[3], info[4])) answer = self.client.get() print( color.green("{} has been successfully upladed.".format( info[2]))) if answer == "DONE": return elif answer == "GIVE": self.filehandler.send_all_files_of_dir( os.path.join(self.unionpath.filesystem_root_dir, info[2])) self.client.send("DONE") elif info[1] == "download": self.client.send("MNT-D {}".format(info[2])) answer = self.client.get() if answer == "NONE": print( color.yellow( "There are no mounts named {} on the server.".format( info[2]))) return elif answer == "ONE": self.client.send("OK") mount = self.client.get().split()[1] mountpath = os.path.join(self.unionpath.filesystem_root_dir, mount) os.mkdir(mountpath) self.unionpath.add_to_dictionary( mount, info[2], "directory", self.unionpath.filesystem_root_dir, "", timestamp=None, extension=None, mount=mount) self.client.send("GIVE") while (True): message = self.client.get() if message == "DONE": break elif message == "\EOF": continue self.filehandler.get_file(message + " {}".format(mountpath), dir=mount) elif answer == "MORE": self.client.send("OK") mounts = self.client.get().split(".") msg = "{} duplicates of {} have been found. Select one by entering the corresponding number:".format( len(mounts), info[2]) str = "" cnt = 0 for mount in mounts: str += "\r\n[{}] {}: Identifier -> {}".format( cnt + 1, info[2], mount) cnt += 1 msg += str print(color.yellow(msg)) while True: try: choice = input( color.bold(color.purple("{} -> ".format(info[2])))) choice = int(choice) if choice >= 1 and choice <= len(mounts): choice -= 1 break else: print( color.red( "Please enter a number between {} and {}.". format(1, len(mounts)))) except: print( color.red( "Please enter a number between {} and {}.". format(1, len(mounts)))) self.client.send("{}".format(choice)) mount = self.client.get().split()[1] mountpath = os.path.join(self.unionpath.filesystem_root_dir, mount) os.mkdir(mountpath) self.unionpath.add_to_dictionary( mount, info[2], "directory", self.unionpath.filesystem_root_dir, "", timestamp=None, extension=None, mount=mount) self.client.send("GIVE") while (True): message = self.client.get() if message == "DONE": break elif message == "\EOF": continue self.filehandler.get_file(message + " {}".format(mountpath), dir=mount, mount=mount) self.unionpath.edit_mount_list(op="add_mount", mounthash=mount, mountname=info[2], IP=self.client.IP) self.unionpath.sort_files_in_dir( os.path.join(self.unionpath.filesystem_root_dir, mount), info[2])