diff --git a/.gitignore b/.gitignore index 6ec7b5e..85eed07 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,3 @@ private_key.txt public_key.txt venv/** log/** -*~ \ No newline at end of file diff --git a/src/dashboard.py b/src/dashboard.py index 9df79e4..5c85a0a 100644 --- a/src/dashboard.py +++ b/src/dashboard.py @@ -8,7 +8,6 @@ import hashlib import ipaddress import json # Python Built-in Library -import logging import os import secrets import subprocess @@ -59,7 +58,8 @@ def get_dashboard_conf(): def set_dashboard_conf(config): - config.write(open(dashboard_conf, "w")) + with open(dashboard_conf, "w") as conf_object: + config.write(conf_object) # Get all keys from a configuration @@ -101,17 +101,16 @@ def get_conf_running_peer_number(config_name): # Read [Interface] section from configuration file def read_conf_file_interface(config_name): conf_location = wg_conf_path + "/" + config_name + ".conf" - f = open(conf_location, 'r') - file = f.read().split("\n") - data = {} - for i in range(len(file)): - if not regex_match("#(.*)", file[i]): - if len(file[i]) > 0: - if file[i] != "[Interface]": - tmp = re.split(r'\s*=\s*', file[i], 1) - if len(tmp) == 2: - data[tmp[0]] = tmp[1] - f.close() + with open(conf_location, 'r') as file_object: + file = file_object.read().split("\n") + data = {} + for i in range(len(file)): + if not regex_match("#(.*)", file[i]): + if len(file[i]) > 0: + if file[i] != "[Interface]": + tmp = re.split(r'\s*=\s*', file[i], 1) + if len(tmp) == 2: + data[tmp[0]] = tmp[1] return data @@ -119,8 +118,8 @@ def read_conf_file_interface(config_name): def read_conf_file(config_name): # Read Configuration File Start conf_location = wg_conf_path + "/" + config_name + ".conf" - f = open(conf_location, 'r') - file = f.read().split("\n") + with open(conf_location, 'r') as file_object: + file = file_object.read().split("\n") conf_peer_data = { "Interface": {}, "Peers": [] @@ -150,7 +149,6 @@ def read_conf_file(config_name): if len(tmp) == 2: conf_peer_data["Peers"][peer][tmp[0]] = tmp[1] - f.close() # Read Configuration File End return conf_peer_data @@ -404,13 +402,11 @@ def get_conf_list(): # Generate private key def gen_private_key(): - private = open('private_key.txt') - private_key = private.readline().strip() - public = open('public_key.txt') - public_key = public.readline().strip() + with open('private_key.txt') as file_object: + private_key = file_object.readline().strip() + with open('public_key.txt') as file_object: + public_key = file_object.readline().strip() data = {"private_key": private_key, "public_key": public_key} - private.close() - public.close() os.remove('private_key.txt') os.remove('public_key.txt') return data @@ -418,12 +414,11 @@ def gen_private_key(): # Generate public key def gen_public_key(private_key): - pri_key_file = open('private_key.txt', 'w') - pri_key_file.write(private_key) - pri_key_file.close() + with open('private_key.txt', 'w') as file_object: + file_object.write(private_key) try: - public = open('public_key.txt') - public_key = public.readline().strip() + with open('public_key.txt') as file_object: + public_key = file_object.readline().strip() os.remove('private_key.txt') os.remove('public_key.txt') return {"status": 'success', "msg": "", "data": public_key} @@ -593,14 +588,14 @@ def update_acct(): config.read(dashboard_conf) config.set("Account", "username", request.form['username']) try: - config.write(open(dashboard_conf, "w")) + with open(dashboard_conf, "w") as config_object: + config.write(config_object) + config.clear() session['message'] = "Username update successfully!" session['message_status'] = "success" session['username'] = request.form['username'] - config.clear() return redirect(url_for("settings")) - except Exception as exc: - logging.error(exc) + except Exception: session['message'] = "Username update failed." session['message_status'] = "danger" config.clear() @@ -635,25 +630,15 @@ def update_peer_default_config(): session['message_status'] = "danger" return redirect(url_for("settings")) # Check MTU Format - if len(request.form['peer_mtu']) > 0: - try: - # TODO need to using - mtu = int(request.form['peer_mtu']) - except Exception as exc: - logging.info(exc) - session['message'] = "MTU format is incorrect." - session['message_status'] = "danger" - return redirect(url_for("settings")) + 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" + return redirect(url_for("settings")) # Check keepalive Format - if len(request.form['peer_keep_alive']) > 0: - try: - # TODO need to using - mtu = int(request.form['peer_keep_alive']) - except Exception as exc: - logging.error(exc) - session['message'] = "Persistent keepalive format is incorrect." - session['message_status'] = "danger" - return redirect(url_for("settings")) + 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" + 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 " \ @@ -668,13 +653,13 @@ def update_peer_default_config(): config.set("Peers", "peer_global_DNS", dns_addresses) try: - config.write(open(dashboard_conf, "w")) - session['message'] = "Peer Default Settings update successfully!" - session['message_status'] = "success" - config.clear() + with open(dashboard_conf, "w") as conf_object: + config.write(conf_object) + session['message'] = "Peer Default Settings update successfully!" + session['message_status'] = "success" + config.clear() return redirect(url_for("settings")) - except Exception as exc: - logging.error(exc) + except Exception: session['message'] = "Peer Default Settings update failed." session['message_status'] = "danger" config.clear() @@ -691,13 +676,13 @@ def update_pwd(): request.form['repnewpass'].encode()).hexdigest(): config.set("Account", "password", hashlib.sha256(request.form['repnewpass'].encode()).hexdigest()) try: - config.write(open(dashboard_conf, "w")) - session['message'] = "Password update successfully!" - session['message_status'] = "success" - config.clear() - return redirect(url_for("settings")) - except Exception as exc: - logging.error(exc) + with open(dashboard_conf, "w") as conf_object: + config.write(conf_object) + 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" config.clear() @@ -721,8 +706,9 @@ def update_app_ip_port(): config.read(dashboard_conf) config.set("Server", "app_ip", request.form['app_ip']) config.set("Server", "app_port", request.form['app_port']) - config.write(open(dashboard_conf, "w")) - config.clear() + with open(dashboard_conf, "w") as config_object: + config.write(config_object) + config.clear() os.system('bash wgd.sh restart') @@ -732,10 +718,11 @@ def update_wg_conf_path(): config = configparser.ConfigParser(strict=False) config.read(dashboard_conf) config.set("Server", "wg_conf_path", request.form['wg_conf_path']) - config.write(open(dashboard_conf, "w")) + with open(dashboard_conf, "w") as config_object: + config.write(config_object) + config.clear() session['message'] = "WireGuard Configuration Path Update Successfully!" session['message_status'] = "success" - config.clear() os.system('bash wgd.sh restart') @@ -754,8 +741,9 @@ def update_dashbaord_sort(): config.set("Server", "dashboard_sort", data['sort']) else: config.set("Server", "dashboard_sort", 'status') - config.write(open(dashboard_conf, "w")) - config.clear() + with open(dashboard_conf, "w") as config_object: + config.write(config_object) + config.clear() return "true" @@ -765,8 +753,9 @@ def update_dashboard_refresh_interval(): config = configparser.ConfigParser(strict=False) config.read(dashboard_conf) config.set("Server", "dashboard_refresh_interval", str(request.form['interval'])) - config.write(open(dashboard_conf, "w")) - config.clear() + with open(dashboard_conf, "w") as config_object: + config.write(config_object) + config.clear() return "true" @@ -895,24 +884,14 @@ def add_peer(config_name): db.close() sem.release() return "Endpoint Allowed IPs format is incorrect." - if len(data['MTU']) != 0: - try: - # TODO need to using - mtu = int(data['MTU']) - except Exception as exc: - logging.error(exc) - db.close() - sem.release() - return "MTU format is not correct." - if len(data['keep_alive']) != 0: - try: - # TODO need to using - keep_alive = int(data['keep_alive']) - except Exception as exc: - logging.error(exc) - db.close() - sem.release() - return "Persistent Keepalive format is not correct." + if len(data['MTU']) == 0 or not data['MTU'].isdigit(): + db.close() + sem.release() + return "MTU format is not correct." + if len(data['keep_alive']) == 0 or not data['keep_alive'].isdigit(): + db.close() + sem.release() + return "Persistent Keepalive format is not correct." try: subprocess.run(f"wg set {config_name} peer {public_key} allowed-ips {allowed_ips}", check=True, shell=True, capture_output=True, stderr=subprocess.STDOUT) @@ -988,24 +967,14 @@ def save_peer_setting(config_name): db.close() sem.release() return jsonify({"status": "failed", "msg": "DNS format is incorrect."}) - if len(data['MTU']) != 0: - try: - # TODO need to use - mtu = int(data['MTU']) - except Exception as exc: - logging.error(exc) - db.close() - sem.release() - return jsonify({"status": "failed", "msg": "MTU format is not correct."}) - if len(data['keep_alive']) != 0: - try: - # TODO need to using - keep_alive = int(data['keep_alive']) - except Exception as exc: - logging.error(exc) - db.close() - sem.release() - return jsonify({"status": "failed", "msg": "Persistent Keepalive format is not correct."}) + if len(data['MTU']) == 0 or not data['MTU'].isdigit(): + db.close() + sem.release() + return jsonify({"status": "failed", "msg": "MTU format is not correct."}) + if len(data['keep_alive']) == 0 or not data['keep_alive'].isdigit(): + db.close() + sem.release() + return jsonify({"status": "failed", "msg": "Persistent Keepalive format is not correct."}) if private_key != "": check_key = checkKeyMatch(private_key, id, config_name) if check_key['status'] == "failed": @@ -1187,7 +1156,8 @@ def switch_display_mode(mode): if mode in ['list', 'grid']: config.read(dashboard_conf) config.set("Peers", "peer_display_mode", mode) - config.write(open(dashboard_conf, "w")) + with open(dashboard_conf, "w") as config_object: + config.write(config_object) return "true" else: return "false" @@ -1239,8 +1209,7 @@ def ping_ip(): if returnjson['package_loss'] == 1.0: returnjson['package_loss'] = returnjson['package_sent'] return jsonify(returnjson) - except Exception as exc: - logging.error(exc) + except Exception: return "Error" @@ -1258,8 +1227,7 @@ def traceroute_ip(): "max_rtt": hop.max_rtt}) last_distance = hop.distance return jsonify(returnjson) - except Exception as exc: - logging.error(exc) + except Exception: return "Error" @@ -1314,8 +1282,9 @@ def init_dashboard(): config['Peers']['peer_MTU'] = "1420" if 'peer_keep_alive' not in config['Peers']: config['Peers']['peer_keep_alive'] = "21" - config.write(open(dashboard_conf, "w")) - config.clear() + with open(dashboard_conf, "w") as config_object: + config.write(config_object) + config.clear() def check_update(): diff --git a/src/db/hi.txt b/src/db/hi.txt deleted file mode 100644 index 035ff7e..0000000 --- a/src/db/hi.txt +++ /dev/null @@ -1 +0,0 @@ -You can delete this later ;) \ No newline at end of file diff --git a/src/wgd.sh b/src/wgd.sh index 83590c6..97a5e80 100755 --- a/src/wgd.sh +++ b/src/wgd.sh @@ -32,37 +32,25 @@ _check_and_set_venv(){ } install_wgd(){ - # Check Python3 version - version_pass=$(python3 -c 'import sys; print("1") if (sys.version_info.major == 3 and sys.version_info.minor >= 8) else print("0");') + printf "| Starting to install WGDashboard |\n" + version_pass=$(python3 -c 'import sys; print("1") if (sys.version_info.major == 3 and sys.version_info.minor >= 7) else print("0");') if [ $version_pass == "0" ] - then printf "| WGDashboard required Python3.8+ |\n" + then printf "| WGDashboard required Python 3.7 or above |\n" printf "%s\n" "$dashes" exit 1 fi - rm db/hi.txt > /dev/null 2>&1 + if [ ! -d "db" ] + then mkdir "db" + fi if [ ! -d "log" ] then mkdir "log" fi + printf "| Upgrading pip |\n" + python3 -m pip install -U pip printf "| Installing latest Python dependencies |\n" - - # set up the local environment - _check_and_set_venv - ${VIRTUAL_ENV}/bin/python3 -m pip -U pip - ${VIRTUAL_ENV}/bin/python3 -m pip install -U -r requirements.txt - printf "| WGDashboard installed successfully! |\n" - - printf "| Preparing the systemctl unit file |\n" - sed -i "s#{{APP_ROOT}}#${APP_ROOT}#" wg-dashboard.service - sed -i "s#{{VIRTUAL_ENV}}#${VIRTUAL_ENV}#" wg-dashboard.service -# cat wg-dashboard.service | sudo SYSTEMD_EDITOR=tee systemctl edit --force --full wg-dashboard.service - systemctl daemon-reload - printf "| Consider 'systemctl enable wg-dashboard' |\n" - printf " and 'systemctl start wg-dashboard'\n" - printf " use '${0} stop' before starting with systemctl\n" - echo - - printf "| Now starting Dashboard in background |\n" - start_wgd + python3 -m pip install -U -r requirements.txt + printf "| WGDashboard installed successfully! |\n" + printf "| Enter ./wgd start to start the dashboard |\n" } @@ -76,7 +64,6 @@ check_wgd_status(){ } start_wgd () { - _check_and_set_venv printf "%s\n" "$dashes" printf "| Starting WGDashboard in the background. |\n" if [ ! -d "log" ] @@ -94,7 +81,6 @@ stop_wgd() { start_wgd_debug() { printf "%s\n" "$dashes" - _check_and_set_venv printf "| Starting WGDashboard in the foreground. |\n" python3 "$app_name" printf "%s\n" "$dashes" @@ -107,15 +93,21 @@ update_wgd() { read up if [ "$up" = "Y" ]; then printf "| Shutting down WGDashboard... |\n" - kill "$(ps aux | grep "[p]ython3 $app_name" | awk '{print $2}')" + if check_wgd_status; then + stop_wgd + fi + mv wgd.sh wgd.sh.old printf "| Downloading %s from GitHub... |\n" "$new_ver" git stash > /dev/null 2>&1 - git pull https://github.com/donaldzou/wireguard-dashboard.git $new_ver --force > /dev/null 2>&1 + git pull +# git pull https://github.com/donaldzou/wireguard-dashboard.git $new_ver --force > /dev/null 2>&1 + printf "| Upgrading pip |\n" + python3 -m pip install -U pip printf "| Installing latest Python dependencies |\n" - _check_and_set_venv - python3 -m pip install -r requirements.txt > /dev/null 2>&1 + python3 -m pip install -U -r requirements.txt printf "| Update Successfully! |\n" - start_wgd + printf "%s\n" "$dashes" + rm wgd.sh.old else printf "%s\n" "$dashes" printf "| Update Canceled. |\n" @@ -150,7 +142,9 @@ if [ "$#" != 1 ]; elif [ "$1" = "update" ]; then update_wgd elif [ "$1" = "install" ]; then + printf "%s\n" "$dashes" install_wgd + printf "%s\n" "$dashes" elif [ "$1" = "restart" ]; then if check_wgd_status; then printf "%s\n" "$dashes"