From 06e66b0bbc4b0400a9899e445b0231aa559b3eb0 Mon Sep 17 00:00:00 2001 From: Mateusz779 Date: Fri, 14 Apr 2023 10:41:07 +0200 Subject: [PATCH] check allocation --- .gitignore | 1 + app.py | 22 +++++++++ db.py | 102 ++++++++++++++++++++++++++++++++++++++++-- templates/create.html | 4 ++ utils.py | 11 ++++- 5 files changed, 136 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index ea4e702..eef5726 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /__pycache__ /venv /configs/squash +/squash diff --git a/app.py b/app.py index 5583236..b4d97d2 100644 --- a/app.py +++ b/app.py @@ -1,3 +1,4 @@ +from time import sleep from flask import Flask, flash, make_response, redirect, send_file, jsonify, request, render_template, url_for import db import os @@ -17,6 +18,27 @@ def ssh_thread_function(): ssh_thread = threading.Thread(target=ssh_thread_function) ssh_thread.start() +class PingThread(threading.Thread): + def __init__(self, ip): + super(PingThread, self).__init__() + self.Ip = ip + def run(self): + utils.ping_client(self.ip) + +def check_allocation_thread_function(): + while True: + ids = db.get_image_allocation_all() + for x in ids: + ip = db.get_image_allocation_clientip_id(x) + ping_thread = PingThread(ip) + ping_thread.start() + + sleep(15) + +allocation_thread = threading.Thread(target=check_allocation_thread_function) +allocation_thread.start() + + @app.route('/') def main(): auth_token = request.cookies.get('auth_token') diff --git a/db.py b/db.py index 5a72dfe..caf257f 100644 --- a/db.py +++ b/db.py @@ -17,7 +17,7 @@ def connect(): with conn.cursor() as cur: cur.execute(""" - CREATE TABLE IF NOT EXISTS vpn ( + CREATE TABLE IF NOT EXISTS image ( id SERIAL PRIMARY KEY, image_name VARCHAR(255) NOT NULL, token VARCHAR(255) NOT NULL, @@ -38,6 +38,14 @@ def connect(): created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, expires_on TIMESTAMP NOT NULL );""") + cur.execute(""" + CREATE TABLE IF NOT EXISTS image_allocation ( + id SERIAL PRIMARY KEY, + image_id INTEGER NOT NULL REFERENCES image(id), + allocation_time TIMESTAMP NOT NULL DEFAULT NOW(), + last_access_time TIMESTAMP, + client_ip INET + );""") conn.commit() def get_cur(): @@ -50,7 +58,7 @@ def add_conf_image(name, token): connect() with get_cur() as cur: cur.execute(""" - INSERT INTO vpn (image_name, token) + INSERT INTO image (image_name, token) VALUES (%s, %s) """,(name, token,)) conn.commit() @@ -59,7 +67,18 @@ def get_conf_image(token): connect() with get_cur() as cur: cur.execute(""" - SELECT image_name FROM vpn WHERE token = %s + SELECT image_name FROM image WHERE token = %s + """,(token,)) + try: + return cur.fetchone()[0] + except: + return None + +def get_conf_id(token): + connect() + with get_cur() as cur: + cur.execute(""" + SELECT id FROM image WHERE token = %s """,(token,)) try: return cur.fetchone()[0] @@ -126,3 +145,80 @@ def login(username, password): else: return None + +def get_image_allocation_all(): + connect() + with get_cur() as cur: + cur.execute(""" + SELECT image_id FROM image_allocation""") + try: + results = [list(row) for row in cur.fetchall()] + return results + except: + return None + +def get_image_allocation_time(token): + id_image = get_conf_id(token) + if id_image is None: + return None + + connect() + with get_cur() as cur: + cur.execute(""" + SELECT last_access_time FROM image_allocation WHERE image_id = %s + """,(id_image,)) + try: + return cur.fetchone()[0] + except: + return None + +def get_image_allocation_time_imageid(image_id): + connect() + with get_cur() as cur: + cur.execute(""" + SELECT last_access_time FROM image_allocation WHERE image_id = %s + """,(image_id,)) + try: + return cur.fetchone()[0] + except: + return None + +def get_image_allocation_clientip(token): + id_image = get_conf_id(token) + if id_image is None: + return None + + connect() + with get_cur() as cur: + cur.execute(""" + SELECT client_ip FROM image_allocation WHERE image_id = %s + """,(id_image,)) + try: + return cur.fetchone()[0] + except: + return None + +def get_image_allocation_clientip_id(id): + connect() + with get_cur() as cur: + cur.execute(""" + SELECT client_ip FROM image_allocation WHERE id = %s + """,(id,)) + try: + return cur.fetchone()[0] + except: + return None + +def set_image_allocation(token, client_ip): + id_image = get_conf_id(token) + if id_image is None: + return None + + connect() + with get_cur() as cur: + cur.execute(""" + INSERT INTO image_allocation (image_id, client_ip, last_access_time) + VALUES (%s, %s, NOW()) + """,(id_image,client_ip,)) + conn.commit() + return token \ No newline at end of file diff --git a/templates/create.html b/templates/create.html index e94f5c1..31ebd3d 100644 --- a/templates/create.html +++ b/templates/create.html @@ -46,6 +46,10 @@ form textarea { font-size: 16px; } +form textarea { + resize: none; +} + /* styl dla przycisku formularza */ form button[type="submit"] { background-color: #4CAF50; diff --git a/utils.py b/utils.py index a77f51b..2934675 100644 --- a/utils.py +++ b/utils.py @@ -2,6 +2,7 @@ import hashlib import secrets import string import random +import os def generate_random_string(length): letters = string.ascii_letters @@ -12,4 +13,12 @@ def hash_password(password): return hashlib.sha512(password.encode('utf-8')).hexdigest() def generate_auth_token(): - return secrets.token_urlsafe(32) \ No newline at end of file + return secrets.token_urlsafe(32) + +def ping_client(ip): + response = os.system("ping -c 1 " + ip) + + if response == 0: + return True + else: + return False \ No newline at end of file