def fork(self, timeout=15, sleeptime=1): if self.running: return _response_.success(f"The {self.id} is already running.") if self.log_level <= 0: print(f"Starting the {self.id}.") serialized = self.dict( keys={ "id": "webserver", "host": "127.0.0.1", "port": 52379, "sleeptime": 3, "log_level": 0, }) for i in [ "__cache__", "cache", "system_cache", "_stder", "_traceback_", "_name", "_daemonic", "_ident", "_native_id", "_tstate_lock", "_started", "_stderr", "_initialized", "_invoke_excepthook", "__status__", "__running__", "__response__", "_is_stopped", "_args", "_kwargs", "_target", "_raw_traceback_", ]: try: del serialized[i] except: a = 1 serialized = f"{serialized}" command = [ str(defaults.vars.executable), f"{SOURCE_PATH}classes/database/fork.py", "--serialized", f"'{serialized}'", "--dev0s-webserver-tag", self.tag ] if self.log_level < 0: command += ["2>", "/dev/null"] p = subprocess.Popen(command) success = False for i in range(int(timeout / sleeptime)): if self.running: success = True break time.sleep(sleeptime) if success: return _response_.success(f"Successfully started the {self.id}.") else: return _response_.error(f"Failed to start the {self.id}.")
def status(self, log_level=defaults.options.log_level): # loader. loader = None if log_level >= 0: loader = Loader(f"Retrieving the status of {self.id}") # launchd. if defaults.vars.os in ["macos"]: output = code.execute(f"sudo launchctl list | grep {self.id}") if not output.success: if loader != None: loader.stop(success=False) return output else: if loader != None: loader.stop() return _response_.success( f"Successfully retrieved the status of {self.id}", { "status": output.output, }) # systemd. else: #output = code.execute(f"sudo systemctl status {self.id}") output = code.execute( f"sudo systemctl status {self.id} > /tmp/status && cat /tmp/status && rm -fr /tmp/status" ) if not output.success: if loader != None: loader.stop(success=False) return output else: if loader != None: loader.stop() return _response_.success( f"Successfully retrieved the status of {self.id}", { "status": output.output, })
def decrypt_directory(self, path, recursive=False, layers=1): # checks. if not self.private_key_activated: return _response_.error("Can not decrypt data since the private key is not activated yet.") # defaults. # recursively decrypt all files. if recursive: # recursive. for i in os.listdir(path): i = f'{path}/{i}'.replace("//",'/') if os.path.isdir(i): response = self.decrypt_directory(i, recursive=True) if response["error"] != None: return response else: response = self.decrypt_file(i) if response["error"] != None: return response # success. if layers-1 > 0: return self.decrypt_directory(path, recursive=True, layers=layers-1) else: return _response_.success(f"Successfully decrypted directory [{path}] (recursively).") # decrypt the encrypted.zip file. else: path = path.replace('.encrypted.zip', '/') # checks. file_path = FilePath(path) # set zip path. if path[len(path)-1] == "/": zip_path = path[:-1] else: zip_path = str(path) zip_path = f'{zip_path}.encrypted.zip' if not Files.exists(zip_path): return _response_.error(f"System encrypted zip [{path}] does not exist.") # decrypt zip. response = self.decrypt_file(zip_path, layers=layers) if response["error"] != None: return _response_.error(f"Failed to decrypted directory [{path}].") # extract zip. zip = Zip(path=zip_path) zip.extract(base=None) if Files.exists(path): zip.file_path.delete(forced=True) return _response_.success(f"Successfully decrypted directory [{path}].") else: return _response_.error(f"Failed to decrypted directory [{path}].")
def read( self, # optionally specify a chat id (str). chat_id=None, ): # request. response = self.__request__( f"https://api.telegram.org/bot{self.token}/getUpdates") #response = self.__request__(f"https://api.telegram.org/bot{self.token}/getMessages") if not response.success: return response response = response["response"] # read all messages. messages = {} for response_data in response['result']: # parse. from_ = response_data["message"]["from"] chat = response_data["message"]["chat"] date = Date().from_seconds(response_data["message"]["date"]) message = response_data["message"]["text"] # append. try: messages[chat['id']] except KeyError: messages[chat['id']] = {} messages[chat['id']][str(date)] = { "message": message, "date": str(date), "from": from_, "chat": chat, } # handler. if chat_id == None: return _response_.success("Successfully retrieved the messages.", { "messages": messages, }) else: try: messages = messages[chat_id] except KeyError: messages = {} return _response_.success( f"Successfully retrieved the messages from chat {chat_id}.", { "messages": messages, })
def initialize(self): # init encryption, self.encryption = RSA( directory=self.key, passphrase=self.passphrase,) response = self.encryption.load_keys() if response["error"] != None: return response # checks. created = False if not self._file_.file_path.exists(): self._file_.save(json.dumps(self.default, indent=4, ensure_ascii=False)) response = self.encryption.encrypt_file(self.file_path.path, layers=self.layers) elif self._load_: response = self.load() if response["error"] != None: return response if not created and self.default != None: if self.dictionary == None: response = self.load() if response["error"] != None: return response response = self.check(default=self.default) if response["error"] != None: return response # success. return _response_.success(f"Successfully initialized encrypted dictionary [{self.file_path.path}].")
def load_private_key(self): # load keys. if not self.memory: self.private_key_data = Files.load(self.private_key) else: self.private_key_data = self.private_key # initialize keys. if self.passphrase == None: try: self.private_key_object = _RSA_.importKey(str(self.private_key_data)) except ValueError as e: self.private_key_object = None if "Padding is incorrect" in str(e): return _response_.error("Provided an incorrect passphrase.") else: return _response_.error(f"ValueError: {e}") return response else: try: self.private_key_object = _RSA_.importKey(str(self.private_key_data), passphrase=self.passphrase) except ValueError as e: self.private_key_object = None if "Padding is incorrect" in str(e): return _response_.error("Provided an incorrect passphrase.") else: return _response_.error(f"ValueError: {e}") return response # response. return _response_.success("Successfully loaded the key pair.")
def safe_stop(self, timeout=120, sleeptime=1): # check already crashed. if self.crashed: return _response_.error( f"Thread {self.id} has crashed, error: {self.response.error}") # send stop. self.log(f"Stopping thread {self.id}.") self.send_stop() for i in range(int(timeout / sleeptime)): if self.stopped: break time.sleep(sleeptime) if not self.stopped: return _response_.error(f"Unable to stop thread {self}.") # handle stop functions. found = False try: response = self.stop() found = True except AttributeError: response = None if not found: try: response = self.__stop__() found = True except AttributeError: response = None if isinstance(response, ResponseObject) and not response.success: return response # handler. return _response_.success(f"Successfully stopped thread {self.id}.", required_log_level=0)
def check_os(supported=["linux"], error=False): if OS not in supported: if error: raise ValueError(f"Unsupported operating system: {OS}.") else: return _response_.error(f"Unsupported operating system: {OS}.") else: if error: return None else: return _response_.success(f"Supported operating system: {OS}.")
def partition( self, # the device without partition number (/dev/sdb). device=None, ): coming_soon() # check parameters. response = _response_.parameters.check({ "device:str": device, }) if not response.success: return response # check os. response = check_os() if not response.success: return response # execute. output = utils.__execute_script__(f""" sudo parted {device} mklabel gpt sudo parted -a opt {device} mkpart primary ext4 0% 100% """) # handler. if "SUCCESS!" in output: return _response_.success( f"Successfully partitioned device {device}.") else: return _response_.error( f"Failed to partition device {device}, output: {output}")
def mount( self, # the device with partition number (/dev/sdb1). device=None, # the mountpoint path. path=None, ): coming_soon() # check parameters. response = _response_.parameters.check({ "device:str": device, "path:str": path, }) if not response.success: return response # check os. response = check_os() if not response.success: return response # handler. if "SUCCESS!" in output: return _response_.success( f"Successfully mounted device {device} to {path}.") else: return _response_.error( f"Failed to mount device {device} to {path}, output: {output}")
def format( self, # the device with partition number (/dev/sdb1). device=None, # the assigned label (name). label=None, ): coming_soon() # check parameters. response = _response_.parameters.check({ "device:str": device, "label:str": label, }) if not response.success: return response # check os. response = check_os() if not response.success: return response # handler. if "SUCCESS!" in output: return _response_.success( f"Successfully formatted device {device}.") else: return _response_.error( f"Failed to format device {device}, output: {output}")
def ping(self, ip, timeout=1): # set info. info = { "ip": None, "up": False, } # execute. response = code.execute(f"ping {ip}", async_=True, wait=False) if not response.success: return response process = response.process time.sleep(timeout) response = process.kill() if not response.success: return response response = process.read() if not response.success: return response output = response.output # handle. info["dns"] = ip try: info["ip"] = output.split(f"PING {ip} (")[1].split("):")[0] except: info["ip"] = None if "Request timeout for" in output: info["up"] = False elif " bytes from " in output: info["up"] = True else: info["up"] = None # success. return _response_.success(f"Successfully pinged [{ip}].", info)
def convert_dns(self, dns, timeout=1): response = self.ping(dns, timeout=timeout) if response["error"] != None: return response if response["ip"] == None: return _response_.error(f"Failed to convert dns [{dns}].") return _response_.success(f"Successfully converted dns [{dns}].", {"ip": response["ip"]})
def connect(self, # the network's ssid. ssid=None, # the network's password. password=None, ): # linux. if defaults.vars.os in ["linux"]: path = f"/etc/netplan/{ssid}.yaml" Files.Save(path, f"""network: wifis: wlan0: dhcp4: true optional: true access-points: "{ssid}": password: "******" """, sudo=True) response = code.execute("sudo netplan generate") if not response.success: return response response = code.execute("sudo netplan apply") if not response.success: return response return _response_.success(f"Successfully connected with {ssid}.") # invalid os. else: return _response_.error(f"Invalid operating system {defaults.vars.os}.")
def create(self): # check params. response = _response_.parameters.check( traceback=self.__traceback__(function="create"), parameters={ "id": self.id, "user": self.user, "start": self.start_, }) if not response.success: return response # checks. if self.service.fp.exists(): return _response_.error( f"Service [{self.service.fp.path}] already exists (call service.check() instead)." ) # save. _response_.log( f"&ORANGE&Root permission&END& required to save changes to [{self.service.fp}].", log_level=self.log_level) self.service.save(data=self.__create__(), sudo=True) self.service.fp.ownership.set("root", sudo=True) self.service.fp.permission.set(700, sudo=True) if dev0s.defaults.vars.os in ["macos"]: os.system("sudo systemctl daemon-reload") # handler. if self.service.fp.exists(): return _response_.success( f"Successfully created service {self.id}.") else: return _response_.error(f"Failed to create service {self.id}.")
def decrypt_file(self, input=None, output=None, remove=False, base64_encoding=False): input = Formats.denitialize(input) output = Formats.denitialize(output) # check params. response = _response_.parameters.check({ "input":input, "output":output,}) if not response.success: return response # encrypt. response = self.decrypt(Files.load(input, format="bytes"), decode=False) if not response.success: return response # write out. decrypted = response.decrypted if base64_encoding: decrypted = base64.b64decode(decrypted) try: Files.save(output, decrypted, format="bytes") except: return _response_.error(f"Failed to write out decrypted file {output}.") # remove. if remove and input != output: try: os.remove(input) except PermissionError: os.system(f"sudo rm -fr {input}") # handler. return _response_.success(f"Successfully decrypted file {input} to {output}.")
def active(): token = flask.request.args.get('token') if token != Database(path=flask.request.args.get('cache')).load( Files.join(flask.request.args.get('cache_id'), "token")): return _response_.error( f"Provided an invalid token {token}.").json() return _response_.success(f"Active.").json()
def encrypt(self, string, decode=False): string = Formats.denitialize(string) if isinstance(string, bytes): string = string.decode() # encrypt data with aes. passphrase = String().generate(length=64, digits=True, capitalize=True) aes = AES(passphrase=passphrase) response = aes.encrypt(string) if not response.success: return response aes_encrypted = response["encrypted"] if b" " in aes_encrypted: return _response_.error("AES encrypt data contains invalid ' ' character(s).") # encrypt aes key with rsa. response = self.rsa.encrypt_string(passphrase, decode=False) if not response.success: return response rsa_encrypted = response["encrypted"] # pack encrypted. encrypted = rsa_encrypted+b" "+aes_encrypted # success. if decode: encrypted = encrypted.decode() return _response_.success("Successfully encrypted the specified data.", { "encrypted":encrypted })
def decrypt(self, string, decode=False): # split encrypted aes key. string = Formats.denitialize(string) if isinstance(string, bytes): string = string.decode() try: key,encrypted = unpack(string) #except: except KeyboardInterrupt: return _response_.error("Unable to unpack the encrypted data.") # decypt key with rsa. response = self.rsa.decrypt_string(key, decode=False) if not response.success: return response passphrase = response["decrypted"].decode() # decrypt with aes. aes = AES(passphrase=passphrase) response = aes.decrypt(encrypted) if not response.success: return response decrypted = response["decrypted"] # success. if decode: decrypted = decrypted.decode() return _response_.success("Successfully decrypted the specified data.", { "decrypted":decrypted })
def process_dict_input(dictionary): # vars. expect = list(dictionary.keys()) send = list(dictionary.values()) # expect . if log_level >= 8: print(f"Expecting one of the following inputs: {expect}.") response = spawn.expect(expect=expect, send=send, timeout=timeout) if not response.success: if "None of the specified inputs were expected." in response.error: if optional: return _response_.error( f"Unable to find the expected input but still success since it is optional." ) else: return _response_.error( f"Specified input [{Array(expect).string(joiner=', ')}] was not expected." ) else: return response if log_level >= 8: print("Send response message:", response.message) # success. return _response_.success("Success.")
def generate_salt(self): length=16 chars = ''.join([string.ascii_uppercase, string.ascii_lowercase, string.digits]) salt = ''.join(random.choice(chars) for x in range(length)) return _response_.success("Successfully generated a salt.", { "salt":salt, })
def kill(self): # handle output. """ killed = None try: #if self.async_: # g = yield from self.child.terminate(force=True, async_=True) # killed = next(x) #else: killed = self.child.terminate(force=True) except Exception as e: return _response_.error(f"Failed to kill process [{self.command}], error: {e}.") if killed == None: return _response_.error(f"Failed to kill process [{self.command}] (#452983).") if not killed: """ # handle output. response = kill(pid=self.pid, log_level=self.log_level) if not response.success: return _response_.error( f"Unable to kill process [{self.command}].") else: return _response_.success( f"Successfully killed process [{self.command}].")
def create(self): # check duplicates. l_response = self.check() if l_response["error"] != None: return l_response if l_response["exists"]: return _response_.error(f"User [{self.username}] already exists.") # check home dir. if Files.exists(self.home_directory): return _response_.error(f"Home directory [{self.home_directory}] already exists.") # handle linux. if OS in ["linux"]: # ubuntu. output = code.execute(f"sudo useradd -s /bin/bash -m {self.username}") # success. if output == "": return _response_.success(f"Successfully created user [{self.username}].") else: return _response_.error(String(output.output.replace("useradd: ", "").replace("\n", ". ")).capitalized_word()) # handle macos. elif OS in ["macos"]: return _response_.error(f"Unsupported operating system [{OS}].")
def delete(self): # check existance. l_response = self.check() if l_response["error"] != None: return l_response if not l_response["exists"]: return _response_.error(f"User [{self.username}] does not exist.") # handle linux. if OS in ["linux"]: # delete. os.system(f"sudo userdel -r {self.username}") os.system(f"sudo rm -fr {self.home_directory}") # handle macos. elif OS in ["macos"]: return _response_.error(f"Unsupported operating system [{OS}].") # check. l_response = self.check() if l_response["error"] != None: return l_response # success. if not l_response["exists"]: return _response_.success(f"Successfully deleted user [{self.username}].") else: return _response_.error(f"Failed to delete user [{self.username}].")
def list_users(self): # check existance. l_response = self.check() if l_response["error"] != None: return l_response elif not l_response["exists"]: return _response_.error(f"Group [{self.name}] does not exists.") # handle linux. users = [] if OS in ["linux"]: try: output = subprocess.check_output("members "+self.name, shell=True).decode().replace("\n", "").split(" ") except: output = [] for i in output: if i not in [""]: users.append(i.replace("\n", "")) # handle macos. elif OS in ["macos"]: return _response_.error(f"Unsupported operating system [{OS}].") # success. self.users = users return _response_.success(f"Successfully listed all users {len(users)} of group [{self.name}].", { "users":users, })
def set_password(self, password=None): # check params. response = _response_.parameters.check( parameters={ "password":password, }) if not response.success: return response # handle linux. if OS in ["linux"]: # get output. output = code.execute(f"export HISTIGNORE='*sudo -S*' && echo '{password}\n{password}\n' | sudo -S -k sudo passwd {self.username}") # success. if "password updated successfully" in output: return _response_.success(f"Successfully edited the password of user [{self.username}].") # fail. else: print(output) return _response_.error(f"Failed to edit the password of user [{self.username}].") # handle macos. elif OS in ["macos"]: return _response_.error(f"Unsupported operating system [{OS}].")
def send( self, # the message to send (str). message=None, # the chat id where the message will be sent to. chat_id=None, # the message title. title=None, ): # check params. response = _response_.parameters.check({ "message:str": message, "chat_id:str": chat_id, }) if not response.success: return response # clean msg. message = message.replace('[', '(').replace(']', ')') # make request. if title not in [None, False, "", "null", "NaN"]: message = f"*{title}*\n" + message response = self.__request__( f"https://api.telegram.org/bot{self.token}/sendMessage?chat_id={chat_id}&parse_mode=Markdown&text={message}" ) if not response.success: return response # handler. return _response_.success( f"Successfull send the message to {chat_id}.")
def crashed(self): response = self.read(wait=False) if not response.success: return response if self.exit_status not in [0, None]: return _response_.error( f"{self.response_str} returned exit status: [{self.exit_status}] (output: {self.read(wait=False, __safe__=True).output})." ) return _response_.success(f"{self.response_str} is not crashed.")
def check_user(id, create=False): user = User(username=id) response = user.check() if not response.success: return response if response["success"] and not response["exists"] and create: response = user.create() if not response.success: return response return _response_.success(f"Successfully checked user [{id}].")
def free_port(self, start=6080): for i in range(10000): port = start + i if not self.port_in_use(port): return _response_.success(f"Successfully found a free port.", { "port": port, }) return _response_.error(f"Unable to find a free port.")