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 # (NB) It is important to import these after the app is created
import wg, util, rest_routes import wg, util, rest_routes
rest_routes.register_routes(app)
# TODO: use class and object oriented programming
""" """

View File

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

View File

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