diff --git a/app.py b/app.py index d3982e0..e2478d5 100644 --- a/app.py +++ b/app.py @@ -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/', 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) diff --git a/configs/create.sh b/configs/create.sh index d38086d..6d856c3 100755 --- a/configs/create.sh +++ b/configs/create.sh @@ -83,12 +83,12 @@ fi mkdir /tmp/output/vpn/scripts cat < /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 < /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 diff --git a/configs/scripts/arpinggw.sh b/configs/scripts/arpinggw.sh index 1fef1c9..6efb4a4 100755 --- a/configs/scripts/arpinggw.sh +++ b/configs/scripts/arpinggw.sh @@ -1,3 +1,3 @@ #!/bin/sh -ip -s -s neigh flush all dev uvpnT2 +ip -s -s neigh flush all dev $TAP diff --git a/configs/scripts/starttap.sh b/configs/scripts/starttap.sh index 12803e8..679aef9 100755 --- a/configs/scripts/starttap.sh +++ b/configs/scripts/starttap.sh @@ -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 \ No newline at end of file diff --git a/configs/sendmail.sh b/configs/sendmail.sh index 0711705..b863568 100755 --- a/configs/sendmail.sh +++ b/configs/sendmail.sh @@ -11,7 +11,7 @@ account notification auth on user test from test@mkedziora.pl - password + password V8ufrLgiEwRF72dM tls on syslog on tls_starttls on diff --git a/db.py b/db.py index 952bd86..a381cac 100644 --- a/db.py +++ b/db.py @@ -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: diff --git a/utils.py b/utils.py index 89b4d1a..84527c1 100644 --- a/utils.py +++ b/utils.py @@ -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