check allocation

This commit is contained in:
Mateusz779 2023-04-14 10:41:07 +02:00
parent 737040d09f
commit 06e66b0bbc
5 changed files with 136 additions and 4 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
/__pycache__ /__pycache__
/venv /venv
/configs/squash /configs/squash
/squash

22
app.py
View File

@ -1,3 +1,4 @@
from time import sleep
from flask import Flask, flash, make_response, redirect, send_file, jsonify, request, render_template, url_for from flask import Flask, flash, make_response, redirect, send_file, jsonify, request, render_template, url_for
import db import db
import os import os
@ -17,6 +18,27 @@ def ssh_thread_function():
ssh_thread = threading.Thread(target=ssh_thread_function) ssh_thread = threading.Thread(target=ssh_thread_function)
ssh_thread.start() 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('/') @app.route('/')
def main(): def main():
auth_token = request.cookies.get('auth_token') auth_token = request.cookies.get('auth_token')

102
db.py
View File

@ -17,7 +17,7 @@ def connect():
with conn.cursor() as cur: with conn.cursor() as cur:
cur.execute(""" cur.execute("""
CREATE TABLE IF NOT EXISTS vpn ( CREATE TABLE IF NOT EXISTS image (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
image_name VARCHAR(255) NOT NULL, image_name VARCHAR(255) NOT NULL,
token VARCHAR(255) NOT NULL, token VARCHAR(255) NOT NULL,
@ -38,6 +38,14 @@ def connect():
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_on TIMESTAMP NOT NULL 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() conn.commit()
def get_cur(): def get_cur():
@ -50,7 +58,7 @@ def add_conf_image(name, token):
connect() connect()
with get_cur() as cur: with get_cur() as cur:
cur.execute(""" cur.execute("""
INSERT INTO vpn (image_name, token) INSERT INTO image (image_name, token)
VALUES (%s, %s) VALUES (%s, %s)
""",(name, token,)) """,(name, token,))
conn.commit() conn.commit()
@ -59,7 +67,18 @@ def get_conf_image(token):
connect() connect()
with get_cur() as cur: with get_cur() as cur:
cur.execute(""" 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,)) """,(token,))
try: try:
return cur.fetchone()[0] return cur.fetchone()[0]
@ -126,3 +145,80 @@ def login(username, password):
else: else:
return None 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

View File

@ -46,6 +46,10 @@ form textarea {
font-size: 16px; font-size: 16px;
} }
form textarea {
resize: none;
}
/* styl dla przycisku formularza */ /* styl dla przycisku formularza */
form button[type="submit"] { form button[type="submit"] {
background-color: #4CAF50; background-color: #4CAF50;

View File

@ -2,6 +2,7 @@ import hashlib
import secrets import secrets
import string import string
import random import random
import os
def generate_random_string(length): def generate_random_string(length):
letters = string.ascii_letters letters = string.ascii_letters
@ -13,3 +14,11 @@ def hash_password(password):
def generate_auth_token(): def generate_auth_token():
return secrets.token_urlsafe(32) return secrets.token_urlsafe(32)
def ping_client(ip):
response = os.system("ping -c 1 " + ip)
if response == 0:
return True
else:
return False