dostosowanie do code review

This commit is contained in:
Mateusz779 2023-04-24 08:27:35 +02:00
parent 951d0e2631
commit ac3e7f7edf
7 changed files with 32 additions and 35 deletions

45
app.py
View File

@ -1,4 +1,5 @@
import datetime
from functools import wraps
from time import sleep
from flask import Flask, make_response, redirect, send_file, jsonify, request, render_template, url_for
import db
@ -16,25 +17,27 @@ app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 512 # 512MB
utils.init_threads()
def login_required(f):
@wraps(f)
def login_function(*args, **kwargs):
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 f(*args, **kwargs)
return login_function
@app.route('/')
@login_required
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")
machines_all = db.get_machines()
return render_template('index.html', ssh_port=config.webssh_port, machines=machines_all.machines)
@app.route('/login')
@login_required
def login():
auth_token = request.cookies.get('auth_token')
if auth_token != "" or auth_token is not None:
if db.get_user_bytoken(auth_token) is not None:
machines_all = db.get_machines()
return render_template('index.html', ssh_port=config.webssh_port, machines=machines_all.machines)
return render_template('login.html')
return render_template('index.html', ssh_port=config.webssh_port, machines=machines_all.machines)
@app.route('/logout')
@ -50,31 +53,21 @@ def logout():
@app.route('/images')
@login_required
def list_images():
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")
images_all = db.get_images()
return render_template("images.html", images=images_all.images)
@app.route('/create')
@login_required
def create_conf():
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("create.html")
@app.route('/api/createconf', methods=['POST'])
@login_required
def create_conf_post():
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")
try:
config_name = request.form['config_name']
token_name = request.form['token_name']
@ -136,12 +129,8 @@ def login_api():
@app.route('/delete/<int:image_id>', methods=['POST'])
@login_required
def delete(image_id):
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")
if db.get_image_allocation(image_id) is not None:
return jsonify(message="409")
filename = db.get_conf_image_id(image_id)

View File

@ -83,12 +83,12 @@ fi
mkdir /tmp/output/vpn/scripts
cat <<EOF> /tmp/output/vpn/scripts/starttap.sh
#!/bin/sh
ifconfig uvpnT2 $ip netmask 255.255.255.0 up
ifconfig '$TAP' $ip netmask 255.255.255.0 up
EOF
cat <<EOF> /tmp/output/vpn/scripts/arpinggw.sh
#!/bin/sh
ip -s -s neigh flush all dev uvpnT2
ip -s -s neigh flush all dev '$TAP'
EOF
chmod +x /tmp/output/vpn/scripts/starttap.sh

View File

@ -1,3 +1,3 @@
#!/bin/sh
ip -s -s neigh flush all dev uvpnT2
ip -s -s neigh flush all dev $TAP

View File

@ -1,2 +1,3 @@
#!/bin/sh
ifconfig uvpnT2 10.20.0.10 netmask 255.255.255.0 up
ifconfig $TAP 10.20.0.10 netmask 255.255.255.0 up

View File

@ -11,7 +11,7 @@ account notification
auth on
user test
from test@mkedziora.pl
password
password V8ufrLgiEwRF72dM
tls on
syslog on
tls_starttls on

6
db.py
View File

@ -173,7 +173,8 @@ def get_machines():
connect()
with get_cur() as cur:
cur.execute("""
SELECT image_id, allocation_time, client_ip_vpn, client_ip_local FROM image_allocation""")
SELECT image_id, allocation_time, client_ip_vpn,
client_ip_local FROM image_allocation""")
try:
machinesall = machines.MachineManager()
for row in cur.fetchall():
@ -182,7 +183,8 @@ def get_machines():
image_name = get_one(
"SELECT image_name FROM image WHERE id = %s", row[0])
machine = machines.Machine(
token, image_name, start_time=row[1], ipvpn=row[2], iplocal=row[3], username="root", password="")
token, image_name, start_time=row[1], ipvpn=row[2],
iplocal=row[3], username="root", password="")
machinesall.add_machine(machine)
return machinesall
except:

View File

@ -85,4 +85,9 @@ def is_valid_ip_address(ip: str) -> bool:
ipaddress.IPv4Address(ip)
return True
except ipaddress.AddressValueError:
try:
ipaddress.IPv6Address(ip)
return True
except:
pass
return False