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:
parent
fca81d413c
commit
c58b5c1ea2
@ -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)
|
||||
|
||||
|
||||
"""
|
||||
|
@ -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,8 +10,9 @@ import urllib.error
|
||||
import db, wg, util
|
||||
|
||||
|
||||
@app.route("/update_dashboard_sort", methods=["POST"])
|
||||
def update_dashbaord_sort():
|
||||
def register_routes(app):
|
||||
@app.route("/update_dashboard_sort", methods=["POST"])
|
||||
def update_dashbaord_sort():
|
||||
"""
|
||||
Update configuration sorting
|
||||
@return: Boolean
|
||||
@ -29,10 +29,9 @@ 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():
|
||||
# Update configuration refresh interval
|
||||
@app.route("/update_dashboard_refresh_interval", methods=["POST"])
|
||||
def update_dashboard_refresh_interval():
|
||||
"""
|
||||
Change the refresh time.
|
||||
@return: Return text with result
|
||||
@ -51,9 +50,8 @@ def update_dashboard_refresh_interval():
|
||||
else:
|
||||
return "false"
|
||||
|
||||
|
||||
@app.route("/qrcode/<interface_name>", methods=["GET"])
|
||||
def generate_qrcode(interface_name):
|
||||
@app.route("/qrcode/<interface_name>", methods=["GET"])
|
||||
def generate_qrcode(interface_name):
|
||||
"""
|
||||
Generate QRCode
|
||||
@param interface_name: Configuration 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,9 +106,8 @@ def generate_qrcode(interface_name):
|
||||
else:
|
||||
return redirect("/configuration/" + interface_name)
|
||||
|
||||
|
||||
@app.route("/download_all/<interface_name>", methods=["GET"])
|
||||
def download_all(interface_name):
|
||||
@app.route("/download_all/<interface_name>", methods=["GET"])
|
||||
def download_all(interface_name):
|
||||
"""
|
||||
Download all configuration
|
||||
@param interface_name: Configuration Name
|
||||
@ -202,12 +201,13 @@ 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"])
|
||||
def download(interface_name):
|
||||
# Download configuration file
|
||||
@app.route("/download/<interface_name>", methods=["GET"])
|
||||
def download(interface_name):
|
||||
"""
|
||||
Download one configuration
|
||||
@param interface_name: Configuration name
|
||||
@ -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,13 +307,16 @@ 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):
|
||||
@app.route("/add_peer/<interface_name>", methods=["POST"])
|
||||
def add_peer(interface_name):
|
||||
"""
|
||||
Add Peers
|
||||
@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.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,9 +388,8 @@ 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):
|
||||
@app.route("/save_peer_setting/<interface_name>", methods=["POST"])
|
||||
def save_peer_setting(interface_name):
|
||||
"""
|
||||
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)
|
||||
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,10 +483,9 @@ 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):
|
||||
# Get peer settings
|
||||
@app.route("/get_peer_data/<interface_name>", methods=["POST"])
|
||||
def get_peer_data(interface_name):
|
||||
"""
|
||||
Get peer settings.
|
||||
|
||||
@ -488,17 +502,15 @@ 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 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):
|
||||
# Check if both key match
|
||||
@app.route("/check_key_match/<interface_name>", methods=["POST"])
|
||||
def check_key_match(interface_name):
|
||||
"""
|
||||
Check key matches
|
||||
@param interface_name: Name of WG interface
|
||||
@ -511,9 +523,8 @@ 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):
|
||||
@app.route("/switch_display_mode/<mode>", methods=["GET"])
|
||||
def switch_display_mode(mode):
|
||||
"""
|
||||
Change display view style.
|
||||
|
||||
@ -531,10 +542,9 @@ 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):
|
||||
# Get configuration details
|
||||
@app.route("/get_config/<interface_name>", methods=["GET"])
|
||||
def get_conf(interface_name):
|
||||
"""
|
||||
Get configuration setting of wireguard interface.
|
||||
@param interface_name: Name of WG interface
|
||||
@ -582,9 +592,8 @@ def get_conf(interface_name):
|
||||
config.clear()
|
||||
return jsonify(conf_data)
|
||||
|
||||
|
||||
@app.route("/remove_peer/<interface_name>", methods=["POST"])
|
||||
def remove_peer(interface_name):
|
||||
@app.route("/remove_peer/<interface_name>", methods=["POST"])
|
||||
def remove_peer(interface_name):
|
||||
"""
|
||||
Remove peer.
|
||||
@param interface_name: Name of WG interface
|
||||
@ -613,9 +622,8 @@ 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):
|
||||
@app.route("/add_peer_bulk/<interface_name>", methods=["POST"])
|
||||
def add_peer_bulk(interface_name):
|
||||
"""
|
||||
Add peers by bulk
|
||||
@param interface_name: Configuration 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"])
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user