From 8d7c39bac41b19b4385fb3d7144d862745b8fb6a Mon Sep 17 00:00:00 2001 From: Donald Cheng Hong Zou Date: Wed, 29 Dec 2021 15:29:29 -0500 Subject: [PATCH] Update dashboard.py Wrapped all reading and writing to dashboard configuration into functions --- src/dashboard.py | 140 +++++++++++++++++++++-------------------------- 1 file changed, 61 insertions(+), 79 deletions(-) diff --git a/src/dashboard.py b/src/dashboard.py index b98ae77..7080acd 100644 --- a/src/dashboard.py +++ b/src/dashboard.py @@ -57,7 +57,6 @@ def get_dashboard_conf(): """ Dashboard Configuration Related """ - config = configparser.ConfigParser(strict=False) config.read(DASHBOARD_CONF) return config @@ -519,8 +518,7 @@ Flask Functions # Before request @app.before_request def auth_req(): - conf = configparser.ConfigParser(strict=False) - conf.read(DASHBOARD_CONF) + conf = get_dashboard_conf() req = conf.get("Server", "auth_req") session['update'] = UPDATE session['dashboard_version'] = DASHBOARD_VERSION @@ -535,12 +533,14 @@ def auth_req(): session['message'] = "You need to sign in first!" else: session['message'] = "" + conf.clear() return redirect(url_for("signin")) else: if request.endpoint in ['signin', 'signout', 'auth', 'settings', 'update_acct', 'update_pwd', 'update_app_ip_port', 'update_wg_conf_path']: + conf.clear() return redirect(url_for("index")) - + conf.clear() return None @@ -571,8 +571,7 @@ def signout(): # Authentication @app.route('/auth', methods=['POST']) def auth(): - config = configparser.ConfigParser(strict=False) - config.read(DASHBOARD_CONF) + config = get_dashboard_conf() password = hashlib.sha256(request.form['password'].encode()) if password.hexdigest() == config["Account"]["password"] \ and request.form['username'] == config["Account"]["username"]: @@ -600,11 +599,9 @@ def settings(): """ Setting Page Related """ - message = "" status = "" - config = configparser.ConfigParser(strict=False) - config.read(DASHBOARD_CONF) + config = get_dashboard_conf() if "message" in session and "message_status" in session: message = session['message'] status = session['message_status'] @@ -628,13 +625,11 @@ def update_acct(): session['message'] = "Username cannot be empty." session['message_status'] = "danger" return redirect(url_for("settings")) - config = configparser.ConfigParser(strict=False) - config.read(DASHBOARD_CONF) + config = get_dashboard_conf() config.set("Account", "username", request.form['username']) try: - with open(DASHBOARD_CONF, "w", encoding='utf-8') as config_object: - config.write(config_object) - config.clear() + set_dashboard_conf(config) + config.clear() session['message'] = "Username update successfully!" session['message_status'] = "success" session['username'] = request.form['username'] @@ -649,59 +644,60 @@ def update_acct(): # Update peer default settting @app.route('/update_peer_default_config', methods=['POST']) def update_peer_default_config(): - config = configparser.ConfigParser(strict=False) - config.read(DASHBOARD_CONF) + config = get_dashboard_conf() if len(request.form['peer_endpoint_allowed_ip']) == 0 or \ len(request.form['peer_global_DNS']) == 0 or \ len(request.form['peer_remote_endpoint']) == 0: session['message'] = "Please fill in all required boxes." session['message_status'] = "danger" + config.clear() return redirect(url_for("settings")) # Check DNS Format dns_addresses = request.form['peer_global_DNS'] if not check_DNS(dns_addresses): session['message'] = "Peer DNS Format Incorrect." session['message_status'] = "danger" + config.clear() return redirect(url_for("settings")) dns_addresses = dns_addresses.replace(" ", "").split(',') dns_addresses = ",".join(dns_addresses) - # Check Endpoint Allowed IPs ip = request.form['peer_endpoint_allowed_ip'] if not check_Allowed_IPs(ip): session['message'] = "Peer Endpoint Allowed IPs Format Incorrect. " \ "Example: 192.168.1.1/32 or 192.168.1.1/32,192.168.1.2/32" session['message_status'] = "danger" + config.clear() return redirect(url_for("settings")) # Check MTU Format if not len(request.form['peer_mtu']) > 0 or not request.form['peer_mtu'].isdigit(): session['message'] = "MTU format is incorrect." session['message_status'] = "danger" + config.clear() return redirect(url_for("settings")) # Check keepalive Format if not len(request.form['peer_keep_alive']) > 0 or not request.form['peer_keep_alive'].isdigit(): session['message'] = "Persistent keepalive format is incorrect." session['message_status'] = "danger" + config.clear() return redirect(url_for("settings")) # Check peer remote endpoint if not check_remote_endpoint(request.form['peer_remote_endpoint']): session['message'] = "Peer Remote Endpoint format is incorrect. It can only be a valid " \ "IP address or valid domain (without http:// or https://). " session['message_status'] = "danger" + config.clear() return redirect(url_for("settings")) - config.set("Peers", "remote_endpoint", request.form['peer_remote_endpoint']) config.set("Peers", "peer_keep_alive", request.form['peer_keep_alive']) config.set("Peers", "peer_mtu", request.form['peer_mtu']) config.set("Peers", "peer_endpoint_allowed_ip", ','.join(clean_IP_with_range(ip))) config.set("Peers", "peer_global_DNS", dns_addresses) - try: - with open(DASHBOARD_CONF, "w", encoding='utf-8') as conf_object: - config.write(conf_object) - session['message'] = "Peer Default Settings update successfully!" - session['message_status'] = "success" - config.clear() + set_dashboard_conf(config) + session['message'] = "Peer Default Settings update successfully!" + session['message_status'] = "success" + config.clear() return redirect(url_for("settings")) except Exception: session['message'] = "Peer Default Settings update failed." @@ -713,19 +709,17 @@ def update_peer_default_config(): # Update dashboard password @app.route('/update_pwd', methods=['POST']) def update_pwd(): - config = configparser.ConfigParser(strict=False) - config.read(DASHBOARD_CONF) + config = get_dashboard_conf() if hashlib.sha256(request.form['currentpass'].encode()).hexdigest() == config.get("Account", "password"): if hashlib.sha256(request.form['newpass'].encode()).hexdigest() == hashlib.sha256( request.form['repnewpass'].encode()).hexdigest(): config.set("Account", "password", hashlib.sha256(request.form['repnewpass'].encode()).hexdigest()) try: - with open(DASHBOARD_CONF, "w", encoding='utf-8') as conf_object: - config.write(conf_object) - session['message'] = "Password update successfully!" - session['message_status'] = "success" - config.clear() - return redirect(url_for("settings")) + set_dashboard_conf(config) + session['message'] = "Password update successfully!" + session['message_status'] = "success" + config.clear() + return redirect(url_for("settings")) except Exception: session['message'] = "Password update failed" session['message_status'] = "danger" @@ -746,25 +740,21 @@ def update_pwd(): # Update dashboard IP and port @app.route('/update_app_ip_port', methods=['POST']) def update_app_ip_port(): - config = configparser.ConfigParser(strict=False) - config.read(DASHBOARD_CONF) + config = get_dashboard_conf() config.set("Server", "app_ip", request.form['app_ip']) config.set("Server", "app_port", request.form['app_port']) - with open(DASHBOARD_CONF, "w", encoding='utf-8') as config_object: - config.write(config_object) - config.clear() + set_dashboard_conf(config) + config.clear() os.system('bash wgd.sh restart') # Update WireGuard configuration file path @app.route('/update_wg_conf_path', methods=['POST']) def update_wg_conf_path(): - config = configparser.ConfigParser(strict=False) - config.read(DASHBOARD_CONF) + config = get_dashboard_conf() config.set("Server", "wg_conf_path", request.form['wg_conf_path']) - with open(DASHBOARD_CONF, "w", encoding='utf-8') as config_object: - config.write(config_object) - config.clear() + set_dashboard_conf(config) + config.clear() session['message'] = "WireGuard Configuration Path Update Successfully!" session['message_status'] = "success" os.system('bash wgd.sh restart') @@ -776,18 +766,15 @@ def update_dashbaord_sort(): """ Configuration Page Related """ - - config = configparser.ConfigParser(strict=False) - config.read(DASHBOARD_CONF) + config = get_dashboard_conf() data = request.get_json() sort_tag = ['name', 'status', 'allowed_ip'] if data['sort'] in sort_tag: config.set("Server", "dashboard_sort", data['sort']) else: config.set("Server", "dashboard_sort", 'status') - with open(DASHBOARD_CONF, "w", encoding='utf-8') as config_object: - config.write(config_object) - config.clear() + set_dashboard_conf(config) + config.clear() return "true" @@ -796,12 +783,10 @@ def update_dashbaord_sort(): def update_dashboard_refresh_interval(): preset_interval = ["5000", "10000", "30000", "60000"] if request.form["interval"] in preset_interval: - config = configparser.ConfigParser(strict=False) - config.read(dashboard_conf) + config = get_dashboard_conf() config.set("Server", "dashboard_refresh_interval", str(request.form['interval'])) - with open(DASHBOARD_CONF, "w", encoding='utf-8') as config_object: - config.write(config_object) - config.clear() + set_dashboard_conf(config) + config.clear() return "true" else: return "false" @@ -810,8 +795,7 @@ def update_dashboard_refresh_interval(): # Configuration Page @app.route('/configuration/', methods=['GET']) def configuration(config_name): - config = configparser.ConfigParser(strict=False) - config.read(DASHBOARD_CONF) + config = get_dashboard_conf() conf_data = { "name": config_name, "status": get_conf_status(config_name), @@ -821,18 +805,23 @@ def configuration(config_name): conf_data['checked'] = "nope" else: conf_data['checked'] = "checked" - config = configparser.ConfigParser(strict=False) - config.read(DASHBOARD_CONF) config_list = get_conf_list() if config_name not in [conf['conf'] for conf in config_list]: return render_template('index.html', conf=get_conf_list()) + + refresh_interval = int(config.get("Server", "dashboard_refresh_interval")) + dns_address = config.get("Peers", "peer_global_DNS") + allowed_ip = config.get("Peers", "peer_endpoint_allowed_ip") + peer_mtu = config.get("Peers", "peer_MTU") + peer_keep_alive = config.get("Peers", "peer_keep_alive") + config.clear() return render_template('configuration.html', conf=get_conf_list(), conf_data=conf_data, - dashboard_refresh_interval=int(config.get("Server", "dashboard_refresh_interval")), - DNS=config.get("Peers", "peer_global_DNS"), - endpoint_allowed_ip=config.get("Peers", "peer_endpoint_allowed_ip"), + dashboard_refresh_interval=refresh_interval, + DNS=dns_address, + endpoint_allowed_ip=allowed_ip, title=config_name, - mtu=config.get("Peers", "peer_MTU"), - keep_alive=config.get("Peers", "peer_keep_alive")) + mtu=peer_mtu, + keep_alive=peer_keep_alive) # Get configuration details @@ -843,8 +832,7 @@ def get_conf(config_name): if len(search) == 0: search = "" search = urllib.parse.unquote(search) - config = configparser.ConfigParser(strict=False) - config.read(DASHBOARD_CONF) + config = get_dashboard_conf() sort = config.get("Server", "dashboard_sort") peer_display_mode = config.get("Peers", "peer_display_mode") if "Address" not in config_interface: @@ -870,6 +858,7 @@ def get_conf(config_name): else: conf_data['checked'] = "checked" print(config.get("Peers","remote_endpoint")) + config.clear() return jsonify(conf_data) # return render_template('get_conf.html', conf_data=conf_data, wg_ip=config.get("Peers","remote_endpoint"), sort_tag=sort, # dashboard_refresh_interval=int(config.get("Server", "dashboard_refresh_interval")), peer_display_mode=peer_display_mode) @@ -1307,13 +1296,11 @@ def download(config_name): @app.route('/switch_display_mode/', methods=['GET']) def switch_display_mode(mode): if mode in ['list', 'grid']: - config = configparser.ConfigParser(strict=False) - config.read(DASHBOARD_CONF) + config = get_dashboard_conf() config.set("Peers", "peer_display_mode", mode) - with open(DASHBOARD_CONF, "w", encoding='utf-8') as config_object: - config.write(config_object) + set_dashboard_conf(config) + config.clear() return "true" - return "false" @@ -1397,8 +1384,7 @@ def init_dashboard(): # Set Default INI File if not os.path.isfile(DASHBOARD_CONF): conf_file = open(DASHBOARD_CONF, "w+") - config = configparser.ConfigParser(strict=False) - config.read(DASHBOARD_CONF) + config = get_dashboard_conf() # Defualt dashboard account setting if "Account" not in config: config['Account'] = {} @@ -1439,18 +1425,15 @@ def init_dashboard(): config['Peers']['peer_MTU'] = "1420" if 'peer_keep_alive' not in config['Peers']: config['Peers']['peer_keep_alive'] = "21" - with open(DASHBOARD_CONF, "w", encoding='utf-8') as config_object: - config.write(config_object) - config.clear() + set_dashboard_conf(config) + config.clear() def check_update(): """ Dashboard check update """ - - config = configparser.ConfigParser(strict=False) - config.read(DASHBOARD_CONF) + config = get_dashboard_conf() data = urllib.request.urlopen("https://api.github.com/repos/donaldzou/WGDashboard/releases").read() output = json.loads(data) release = [] @@ -1468,8 +1451,7 @@ def check_update(): if __name__ == "__main__": init_dashboard() UPDATE = check_update() - configuration_settings = configparser.ConfigParser(strict=False) - configuration_settings.read(DASHBOARD_CONF) + configuration_settings = get_dashboard_conf() app_ip = configuration_settings.get("Server", "app_ip") app_port = configuration_settings.get("Server", "app_port") wg_conf_path = configuration_settings.get("Server", "wg_conf_path")