1
0
mirror of https://github.com/donaldzou/WGDashboard.git synced 2024-11-23 07:30:11 +01:00

Got rid of from __main__ import, fixed one call missing a parameter

This commit is contained in:
theonlynexus 2022-07-21 02:22:50 +00:00
parent fca81d413c
commit c58b5c1ea2
3 changed files with 598 additions and 589 deletions

View File

@ -56,8 +56,7 @@ QRcode(app)
# (NB) It is important to import these after the app is created
import wg, util, rest_routes
# TODO: use class and object oriented programming
rest_routes.register_routes(app)
"""

View File

@ -1,7 +1,6 @@
import subprocess
import os
from flask import request, redirect, session, jsonify, g, render_template
from __main__ import app
from flask import request, redirect, jsonify, g, render_template
from datetime import datetime
import urllib.parse
import urllib.request
@ -11,6 +10,7 @@ import urllib.error
import db, wg, util
def register_routes(app):
@app.route("/update_dashboard_sort", methods=["POST"])
def update_dashbaord_sort():
"""
@ -29,7 +29,6 @@ def update_dashbaord_sort():
config.clear()
return "true"
# Update configuration refresh interval
@app.route("/update_dashboard_refresh_interval", methods=["POST"])
def update_dashboard_refresh_interval():
@ -51,7 +50,6 @@ def update_dashboard_refresh_interval():
else:
return "false"
@app.route("/qrcode/<interface_name>", methods=["GET"])
def generate_qrcode(interface_name):
"""
@ -72,7 +70,9 @@ def generate_qrcode(interface_name):
peer = get_peer[0]
if peer[0] != "":
public_key = wg.get_interface_public_key(interface_name, g.WG_CONF_PATH)
listen_port = wg.get_interface_listen_port(interface_name, g.WG_CONF_PATH)
listen_port = wg.get_interface_listen_port(
interface_name, g.WG_CONF_PATH
)
endpoint = config.get("Peers", "remote_endpoint") + ":" + listen_port
private_key = peer[0]
allowed_ips = peer[1]
@ -106,7 +106,6 @@ def generate_qrcode(interface_name):
else:
return redirect("/configuration/" + interface_name)
@app.route("/download_all/<interface_name>", methods=["GET"])
def download_all(interface_name):
"""
@ -202,8 +201,9 @@ def download_all(interface_name):
+ psk
)
data.append({"filename": f"{filename}.conf", "content": return_data})
return jsonify({"status": True, "peers": data, "filename": f"{interface_name}.zip"})
return jsonify(
{"status": True, "peers": data, "filename": f"{interface_name}.zip"}
)
# Download configuration file
@app.route("/download/<interface_name>", methods=["GET"])
@ -226,7 +226,9 @@ def download(interface_name):
peer = get_peer[0]
if peer[0] != "":
public_key = wg.get_interface_public_key(interface_name, g.WG_CONF_PATH)
listen_port = wg.get_interface_listen_port(interface_name, g.WG_CONF_PATH)
listen_port = wg.get_interface_listen_port(
interface_name, g.WG_CONF_PATH
)
endpoint = config.get("Peers", "remote_endpoint") + ":" + listen_port
private_key = peer[0]
allowed_ips = peer[1]
@ -305,11 +307,14 @@ def download(interface_name):
)
return jsonify(
{"status": True, "filename": f"{filename}.conf", "content": return_data}
{
"status": True,
"filename": f"{filename}.conf",
"content": return_data,
}
)
return jsonify({"status": False, "filename": "", "content": ""})
@app.route("/add_peer/<interface_name>", methods=["POST"])
def add_peer(interface_name):
"""
@ -368,7 +373,9 @@ def add_peer(interface_name):
wg.set_peer_options(interface_name, public_key, allowed_ips)
wg.quick_save_interface_config(interface_name, g.WG_CONF_PATH)
util.wg_peer_data_to_db(interface_name, g.WG_CONF_PATH, g.DASHBOARD_CONF_FILE)
util.wg_peer_data_to_db(
interface_name, g.WG_CONF_PATH, g.DASHBOARD_CONF_FILE
)
data = {
"id": public_key,
"name": data["name"],
@ -381,7 +388,6 @@ def add_peer(interface_name):
except subprocess.CalledProcessError as exc:
return exc.output.strip()
@app.route("/save_peer_setting/<interface_name>", methods=["POST"])
def save_peer_setting(interface_name):
"""
@ -405,12 +411,17 @@ def save_peer_setting(interface_name):
check_ip = util.check_repeat_allowed_ips(id, allowed_ips, interface_name)
if not util.check_IP_with_range(endpoint_allowed_ips):
return jsonify(
{"status": "failed", "msg": "Endpoint Allowed IPs format is incorrect."}
{
"status": "failed",
"msg": "Endpoint Allowed IPs format is incorrect.",
}
)
if not util.check_DNS(dns_addresses):
return jsonify({"status": "failed", "msg": "DNS format is incorrect."})
if len(data["MTU"]) == 0 or not data["MTU"].isdigit():
return jsonify({"status": "failed", "msg": "MTU format is not correct."})
return jsonify(
{"status": "failed", "msg": "MTU format is not correct."}
)
if len(data["keep_alive"]) == 0 or not data["keep_alive"].isdigit():
return jsonify(
{
@ -434,7 +445,9 @@ def save_peer_setting(interface_name):
stderr=subprocess.STDOUT,
)
if change_psk.decode("UTF-8") != "":
return jsonify({"status": "failed", "msg": change_psk.decode("UTF-8")})
return jsonify(
{"status": "failed", "msg": change_psk.decode("UTF-8")}
)
if allowed_ips == "":
allowed_ips = '""'
allowed_ips = allowed_ips.replace(" ", "")
@ -445,7 +458,9 @@ def save_peer_setting(interface_name):
)
wg.quick_save_interface_config(interface_name, g.WG_CONF_PATH)
if change_ip.decode("UTF-8") != "":
return jsonify({"status": "failed", "msg": change_ip.decode("UTF-8")})
return jsonify(
{"status": "failed", "msg": change_ip.decode("UTF-8")}
)
db.update_peer(
interface_name,
@ -468,7 +483,6 @@ def save_peer_setting(interface_name):
else:
return jsonify({"status": "failed", "msg": "This peer does not exist."})
# Get peer settings
@app.route("/get_peer_data/<interface_name>", methods=["POST"])
def get_peer_data(interface_name):
@ -488,14 +502,12 @@ def get_peer_data(interface_name):
db_peer = util.adapt_for_rest(db_peer)
return jsonify(db_peer)
# Return available IPs
@app.route("/available_ips/<interface_name>", methods=["GET"])
def available_ips(interface_name):
return jsonify(util.f_available_ips(interface_name, g.WG_CONF_PATH))
# Check if both key match
@app.route("/check_key_match/<interface_name>", methods=["POST"])
def check_key_match(interface_name):
@ -511,7 +523,6 @@ def check_key_match(interface_name):
public_key = data["public_key"]
return jsonify(util.f_check_key_match(private_key, public_key, interface_name))
@app.route("/switch_display_mode/<mode>", methods=["GET"])
def switch_display_mode(mode):
"""
@ -531,7 +542,6 @@ def switch_display_mode(mode):
return "true"
return "false"
# Get configuration details
@app.route("/get_config/<interface_name>", methods=["GET"])
def get_conf(interface_name):
@ -582,7 +592,6 @@ def get_conf(interface_name):
config.clear()
return jsonify(conf_data)
@app.route("/remove_peer/<interface_name>", methods=["POST"])
def remove_peer(interface_name):
"""
@ -613,7 +622,6 @@ def remove_peer(interface_name):
return exc.output.strip()
return "true"
@app.route("/add_peer_bulk/<interface_name>", methods=["POST"])
def add_peer_bulk(interface_name):
"""
@ -644,7 +652,7 @@ def add_peer_bulk(interface_name):
return "MTU format is not correct."
if len(data["keep_alive"]) == 0 or not data["keep_alive"].isdigit():
return "Persistent Keepalive format is not correct."
ips = util.f_available_ips(interface_name)
ips = util.f_available_ips(interface_name, g.WG_CONF_PATH)
if amount > len(ips):
return f"Cannot create more than {len(ips)} peers."
wg_command = ["wg", "set", interface_name]
@ -688,7 +696,9 @@ def add_peer_bulk(interface_name):
" ".join(wg_command), shell=True, stderr=subprocess.STDOUT
)
wg.quick_save_interface_config(interface_name, g.WG_CONF_PATH)
util.wg_peer_data_to_db(interface_name, g.WG_CONF_PATH, g.DASHBOARD_CONF_FILE)
util.wg_peer_data_to_db(
interface_name, g.WG_CONF_PATH, g.DASHBOARD_CONF_FILE
)
if enable_preshared_key:
for i in keys:
os.remove(i["psk_file"])

View File

@ -5,7 +5,7 @@ import sqlite3
import configparser
import time
import ipaddress
from __main__ import app
from dashboard import app
from operator import itemgetter
from flask import g