dodanie podstawowego panelu administracyjnego w wersji testowej

This commit is contained in:
Mateusz779 2023-04-19 09:34:40 +02:00
parent 6bc3ba9ad7
commit 8b444a8bf5
4 changed files with 195 additions and 36 deletions

40
app.py
View File

@ -1,6 +1,6 @@
import datetime
from time import sleep
from flask import Flask, flash, make_response, redirect, send_file, jsonify, request, render_template, url_for
from flask import Flask, make_response, redirect, send_file, jsonify, request, render_template, url_for
import db
import os
from werkzeug.utils import secure_filename
@ -8,20 +8,24 @@ import subprocess
import utils
import shutil
import config
import machines
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = "squash"
app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 512 #512MB
app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 512 # 512MB
utils.init_threads()
@app.route('/')
def main():
auth_token = request.cookies.get('auth_token')
if auth_token != "" or auth_token is not None:
if db.get_user_bytoken(auth_token) is None:
return redirect("/login")
return render_template('index.html')
machines_all = db.get_machines()
return render_template('index.html', ssh_port=config.webssh_port, table=machines_all.to_html_table())
@app.route('/login')
def login():
@ -31,6 +35,7 @@ def login():
return render_template('index.html')
return render_template('login.html')
@app.route('/create/conf')
def create_conf():
auth_token = request.cookies.get('auth_token')
@ -39,6 +44,7 @@ def create_conf():
return redirect("/login")
return render_template("create.html")
@app.route('/api/createconf', methods=['POST'])
def create_conf_post():
config_name = request.form['config_name']
@ -46,24 +52,27 @@ def create_conf_post():
key_length = request.form['key_length']
folder = utils.generate_random_string(5)
try:
os.mkdir(os.path.join(os.getcwd(), 'configs',folder))
os.mkdir(os.path.join(os.getcwd(), 'configs', folder))
authorized_keys_config = request.form['authorized_keys_config']
authorized_keys_file = open(folder+"/authorized_keys","w")
authorized_keys_file = open(folder+"/authorized_keys", "w")
authorized_keys_file.write(authorized_keys_config)
authorized_keys_file.close()
except:
shutil.copy('./configs/authorized_keys', './configs/'+ folder+"/authorized_keys")
shutil.copy('./configs/authorized_keys',
'./configs/' + folder+"/authorized_keys")
script_path = os.path.join(os.getcwd(), 'configs', "create.sh")
ini_path = os.path.join(os.getcwd(), 'configs', "uVPN.ini")
conf_path = os.path.join(os.getcwd(), 'configs', "uVPN.conf")
pub_path = os.path.join(os.getcwd(), 'configs', "server.pub")
scripts_path = os.path.join(os.getcwd(), 'configs', "scripts/")
authorized_keys_path = os.path.join(os.getcwd(), 'configs',folder ,"authorized_keys")
authorized_keys_path = os.path.join(
os.getcwd(), 'configs', folder, "authorized_keys")
sshd_config_path = os.path.join(os.getcwd(), 'configs', "sshd_config")
sendmail_path = os.path.join(os.getcwd(), 'configs', "sendmail.sh")
subprocess.run([script_path,"-i "+ini_path, "-c "+conf_path, "-k "+pub_path, "-l "+key_length, "-n"+config_name, "-s "+scripts_path, "-a "+authorized_keys_path, "-d "+sshd_config_path, "-m "+sendmail_path, " > /dev/null 2>&1 "])
subprocess.run([script_path, "-i "+ini_path, "-c "+conf_path, "-k "+pub_path, "-l "+key_length, "-n"+config_name,
"-s "+scripts_path, "-a "+authorized_keys_path, "-d "+sshd_config_path, "-m "+sendmail_path, " > /dev/null 2>&1 "])
if os.path.exists(folder):
shutil.rmtree(folder)
@ -77,9 +86,9 @@ def create_conf_post():
def login_api():
username = request.form['username']
password = request.form['password']
#register
#db.add_user(username, password)
#register
# register
# db.add_user(username, password)
# register
auth_token = db.login(username, password)
if auth_token is None:
return render_template('login.html', incorrect="Incorrect username or password!")
@ -140,12 +149,13 @@ def get_image():
date = db.get_image_allocation_time(request.headers['token'])
if date is not None:
delta = datetime.datetime.now() - date
if delta.total_seconds() > 30:
if delta.total_seconds() > 30:
db.del_image_allocation_token(request.headers['token'])
else:
filename = None
else:
db.set_image_allocation(request.headers['token'], request.remote_addr)
db.set_image_allocation(
request.headers['token'], request.remote_addr)
except:
pass
@ -154,6 +164,7 @@ def get_image():
return send_file(os.path.join(app.config['UPLOAD_FOLDER'], filename))
@app.route("/api/release_allocation", methods=['POST'])
def release_allocation():
try:
@ -169,6 +180,7 @@ def release_allocation():
return jsonify(message="200")
@app.route("/api/addip", methods=['POST'])
def add_ip():
try:
@ -178,5 +190,7 @@ def add_ip():
pass
print(token, ip)
return ""
if __name__ == '__main__':
app.run(host="0.0.0.0")

31
db.py
View File

@ -1,7 +1,7 @@
import psycopg2
import config
import utils
import machines
def connect():
global cur, conn
@ -139,8 +139,23 @@ def login(username, password):
else:
return None
def get_machines():
connect()
with get_cur() as cur:
cur.execute("""
SELECT image_id, allocation_time, client_ip FROM image_allocation""")
try:
machinesall = machines.MachineManager()
for row in cur.fetchall():
token = get_one("SELECT token FROM image WHERE id = %s", row[0])
image_name = get_one("SELECT image_name FROM image WHERE id = %s", row[0])
machine = machines.Machine(token, image_name, start_time=row[1], ip=row[2], username="root", password="")
machinesall.add_machine(machine)
return machinesall
except:
return None
def get_image_allocation_all():
def get_image_allocation_all_id():
connect()
with get_cur() as cur:
cur.execute("""
@ -152,6 +167,18 @@ def get_image_allocation_all():
return None
def get_image_allocation_all():
connect()
with get_cur() as cur:
cur.execute("""
SELECT * FROM image_allocation""")
try:
results = [list(row) for row in cur.fetchall()]
return results
except:
return None
def get_image_allocation(sql, value):
connect()
with get_cur() as cur:

51
machines.py Normal file
View File

@ -0,0 +1,51 @@
class Machine:
def __init__(self, name, image_name, start_time, ip, username, password):
self.name = name
self.image_name = image_name
self.start_time = start_time
self.ip = ip
self.username = username
self.password = password
def __str__(self):
return f"Name: {self.name}\nImage name: {self.image_name}\nStart time: {self.start_time}\nIP: {self.ip}\nUsername: {self.username}\nPassword: {self.password}"
class MachineManager:
def __init__(self):
self.machines = []
def add_machine(self, machine):
self.machines.append(machine)
def remove_machine(self, machine):
self.machines.remove(machine)
def get_machine_by_name(self, name):
for machine in self.machines:
if machine.name == name:
return machine
return None
def __str__(self):
result = ""
for machine in self.machines:
result += str(machine) + "\n\n"
return result
def to_html_table(self):
html = "<table>"
html += """<th>Nazwa maszyny</th>
<th>Obraz</th>
<th>Czas uruchomienia</th>
<th>IP uVPN</th>
<th></th>"""
for machine in self.machines:
html += f"""<tr>
<td>{ machine.name }</td>
<td>{ machine.image_name }</td>
<td>{ machine.start_time }</td>
<td>{ machine.ip }</td>
<td><button onclick="ssh('{ machine.ip, machine.username, machine.password }')">SSH</button></td>
</tr>"""
html += "</table>"
return html

View File

@ -1,12 +1,79 @@
<!DOCTYPE html>
<html lang="en">
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<title>Panel administracyjny</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
margin: 0;
padding: 0;
}
h1 {
font-size: 32px;
font-weight: bold;
color: #333;
margin: 20px 0;
}
table {
border-collapse: collapse;
margin: 20px 0;
width: 100%;
}
table th, table td {
padding: 10px;
text-align: left;
vertical-align: middle;
border: 1px solid #ccc;
}
table th {
background-color: #eee;
font-weight: bold;
}
table tr:hover {
background-color: #f0f0f0;
}
button {
background-color: #4CAF50;
border: none;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 10px 0;
cursor: pointer;
}
button:hover {
background-color: #3e8e41;
}
</style>
</head>
<body>
<h1>Lista maszyn</h1>
{{table}}
</table>
<script>
function ssh(ipvpn, username, password) {
const currentLocation = window.location;
// budowanie nowego URL
const newUrl = currentLocation.protocol + '//' + currentLocation.hostname + ':' + ssh_port + "/?hostname="+ipvpn+"&username="+username+"&password="+btoa(password);
// przekierowanie na nowy URL
window.location.replace(newUrl);
}
</script>
</body>
</html>