From 5b59bb99c2ffc84f8f26a532f0092c5b050df75b Mon Sep 17 00:00:00 2001 From: Mateusz779 Date: Fri, 21 Apr 2023 16:39:25 +0200 Subject: [PATCH] code cleanup --- app.py | 3 ++- config.py | 12 ++++++------ images.py | 1 + machines.py | 2 ++ utils.py | 15 +++++++-------- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/app.py b/app.py index 7fd6e27..d3982e0 100644 --- a/app.py +++ b/app.py @@ -112,7 +112,8 @@ def create_conf_post(): if os.path.exists(folder): shutil.rmtree(folder) - output = subprocess.run(['openssl','passwd','-6', password], capture_output=True, text=True) + output = subprocess.run( + ['openssl', 'passwd', '-6', password], capture_output=True, text=True) db.add_conf_image(config_name+".squashfs", token_name, ip, output.stdout) return send_file(os.path.join(app.config['UPLOAD_FOLDER'], config_name+".pub")) diff --git a/config.py b/config.py index 04b0d42..dfc0645 100644 --- a/config.py +++ b/config.py @@ -1,7 +1,7 @@ -database="praktyki" -host="localhost" -user="praktyki" -password="2a7driUITXFy73tO" -port="5432" +database = "praktyki" +host = "localhost" +user = "praktyki" +password = "2a7driUITXFy73tO" +port = "5432" default_file = "default.squashfs" -webssh_port = "8000" \ No newline at end of file +webssh_port = "8000" diff --git a/images.py b/images.py index 491bf2b..9aa74de 100644 --- a/images.py +++ b/images.py @@ -5,6 +5,7 @@ class Image: self.token = token self.vpn_ip = vpn_ip + class ImageManager: def __init__(self): self.images = [] diff --git a/machines.py b/machines.py index 332b39a..e514d79 100644 --- a/machines.py +++ b/machines.py @@ -7,6 +7,8 @@ class Machine: self.iplocal = iplocal self.username = username self.password = password + + class MachineManager: def __init__(self): self.machines = [] diff --git a/utils.py b/utils.py index 797525a..c3f50ea 100644 --- a/utils.py +++ b/utils.py @@ -11,6 +11,7 @@ import db import config import ipaddress + def generate_random_string(length): letters = string.ascii_letters random_string = ''.join(random.choice(letters) for i in range(length)) @@ -41,10 +42,11 @@ def ssh_thread_function(): def check_allocation_thread_function(): while True: ids = db.get_image_allocation_all() - for x in ids: - ip = db.get_image_allocation_clientip_id_vpn(x[0]) - ping_thread = PingThread(ip, x[0]) - ping_thread.start() + if ids is not None: + for x in ids: + ip = db.get_image_allocation_clientip_id_vpn(x[0]) + ping_thread = PingThread(ip, x[0]) + ping_thread.start() sleep(10) @@ -61,15 +63,11 @@ class PingThread(threading.Thread): return if ping_client(self.Ip) == False: date = db.get_image_allocation_time_id(self.Id) - print(date) if date is None: return delta = datetime.datetime.utcnow() - date - print(datetime.datetime.utcnow()) - print(delta.total_seconds()) if delta.total_seconds() > 30: db.del_image_allocation_id(self.Id) - print("deleted") else: db.update_image_allocation_time(self.Id) @@ -82,6 +80,7 @@ def init_threads(): target=check_allocation_thread_function) allocation_thread.start() + def is_valid_ip_address(ip: str) -> bool: try: ipaddress.IPv4Address(ip)