From 9e38137e769c22f23db84384755e96fe10e6af56 Mon Sep 17 00:00:00 2001 From: Donald Zou Date: Fri, 3 May 2024 10:15:00 +0800 Subject: [PATCH] Removed DNS as a required field from peer settings --- src/dashboard.py | 64 +++++++++++++++++++++++++------- src/static/js/configuration.js | 6 +-- src/templates/configuration.html | 8 ++-- src/templates/footer.html | 2 +- 4 files changed, 58 insertions(+), 22 deletions(-) diff --git a/src/dashboard.py b/src/dashboard.py index 258e02e..3776e58 100644 --- a/src/dashboard.py +++ b/src/dashboard.py @@ -1097,7 +1097,7 @@ def add_peer_bulk(config_name): if not amount.isdigit() or int(amount) < 1: return "Amount must be integer larger than 0" amount = int(amount) - if not check_DNS(dns_addresses): + if len(dns_addresses) > 0 and not check_DNS(dns_addresses): return "DNS formate is incorrect. Example: 1.1.1.1" if not check_Allowed_IPs(endpoint_allowed_ip): return "Endpoint Allowed IPs format is incorrect." @@ -1160,7 +1160,7 @@ def add_peer(config_name): enable_preshared_key = data["enable_preshared_key"] preshared_key = data['preshared_key'] keys = get_conf_peer_key(config_name) - if len(public_key) == 0 or len(dns_addresses) == 0 or len(allowed_ips) == 0 or len(endpoint_allowed_ip) == 0: + if len(public_key) == 0 or len(allowed_ips) == 0 or len(endpoint_allowed_ip) == 0: return "Please fill in all required box." if not isinstance(keys, list): return config_name + " is not running." @@ -1171,7 +1171,7 @@ def add_peer(config_name): .fetchone() if check_dup_ip[0] != 0: return "Allowed IP already taken by another peer." - if not check_DNS(dns_addresses): + if len(dns_addresses) > 0 and not check_DNS(dns_addresses): return "DNS formate is incorrect. Example: 1.1.1.1" if not check_Allowed_IPs(endpoint_allowed_ip): return "Endpoint Allowed IPs format is incorrect." @@ -1263,7 +1263,7 @@ def save_peer_setting(config_name): check_ip = check_repeat_allowed_ip(id, allowed_ip, config_name) if not check_IP_with_range(endpoint_allowed_ip): return jsonify({"status": "failed", "msg": "Endpoint Allowed IPs format is incorrect."}) - if not check_DNS(dns_addresses): + if len(dns_addresses) > 0 and not 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."}) @@ -1422,12 +1422,30 @@ def download_all(config_name): filename = filename + "_" + config_name psk = "" if preshared_key != "": - psk = "\nPresharedKey = " + preshared_key + psk = "PresharedKey = " + preshared_key + + return_data = f'''[Interface] +PrivateKey = {private_key} +Address = {allowed_ip} +MTU = {str(mtu_value)} - return_data = "[Interface]\nPrivateKey = " + private_key + "\nAddress = " + allowed_ip + "\nDNS = " + \ - dns_addresses + "\nMTU = " + str(mtu_value) + "\n\n[Peer]\nPublicKey = " + \ - public_key + "\nAllowedIPs = " + endpoint_allowed_ip + "\nEndpoint = " + \ - endpoint + "\nPersistentKeepalive = " + str(keepalive) + psk +''' + if len(dns_addresses) > 0: + return_data += f'DNS = {dns_addresses}' + + return_data += f''' +[Peer] +PublicKey = {public_key} +AllowedIPs = {endpoint_allowed_ip} +Endpoint = {endpoint} +PersistentKeepalive = {str(keepalive)} +{psk} +''' + + # return_data = "[Interface]\nPrivateKey = " + private_key + "\nAddress = " + allowed_ip + "\nDNS = " + \ + # dns_addresses + "\nMTU = " + str(mtu_value) + "\n\n[Peer]\nPublicKey = " + \ + # public_key + "\nAllowedIPs = " + endpoint_allowed_ip + "\nEndpoint = " + \ + # endpoint + "\nPersistentKeepalive = " + str(keepalive) + psk data.append({"filename": f"{filename}.conf", "content": return_data}) return jsonify({"status": True, "peers": data, "filename": f"{config_name}.zip"}) @@ -1475,12 +1493,30 @@ def download(config_name): filename = filename + "_" + config_name psk = "" if preshared_key != "": - psk = "\nPresharedKey = " + preshared_key + psk = "PresharedKey = " + preshared_key - return_data = "[Interface]\nPrivateKey = " + private_key + "\nAddress = " + allowed_ip + "\nDNS = " + \ - dns_addresses + "\nMTU = " + str(mtu_value) + "\n\n[Peer]\nPublicKey = " + \ - public_key + "\nAllowedIPs = " + endpoint_allowed_ip + "\nEndpoint = " + \ - endpoint + "\nPersistentKeepalive = " + str(keepalive) + psk + return_data = f'''[Interface] +PrivateKey = {private_key} +Address = {allowed_ip} +MTU = {str(mtu_value)} + +''' + if len(dns_addresses) > 0: + return_data += f'DNS = {dns_addresses}' + + return_data += f''' +[Peer] +PublicKey = {public_key} +AllowedIPs = {endpoint_allowed_ip} +Endpoint = {endpoint} +PersistentKeepalive = {str(keepalive)} +{psk} +''' + + # return_data = "[Interface]\nPrivateKey = " + private_key + "\nAddress = " + allowed_ip + "\nDNS = " + \ + # dns_addresses + "\nMTU = " + str(mtu_value) + "\n\n[Peer]\nPublicKey = " + \ + # public_key + "\nAllowedIPs = " + endpoint_allowed_ip + "\nEndpoint = " + \ + # endpoint + "\nPersistentKeepalive = " + str(keepalive) + psk return jsonify({"status": True, "filename": f"{filename}.conf", "content": return_data}) return jsonify({"status": False, "filename": "", "content": ""}) diff --git a/src/static/js/configuration.js b/src/static/js/configuration.js index ff4619d..ab12138 100644 --- a/src/static/js/configuration.js +++ b/src/static/js/configuration.js @@ -155,7 +155,7 @@ let $enable_preshare_key = $("#enable_preshare_key"); let data_list = [$new_add_DNS, $new_add_endpoint_allowed_ip,$new_add_MTU, $new_add_keep_alive]; if ($new_add_amount.val() > 0 && !$new_add_amount.hasClass("is-invalid")){ - if ($new_add_DNS.val() !== "" && $new_add_endpoint_allowed_ip.val() !== ""){ + if ($new_add_endpoint_allowed_ip.val() !== ""){ let conf = $add_peer.getAttribute('conf_id'); let keys = []; for (let i = 0; i < $new_add_amount.val(); i++) { @@ -633,7 +633,7 @@ $add_peer.addEventListener("click",function(){ let $enable_preshare_key = $("#enable_preshare_key"); $add_peer.setAttribute("disabled","disabled"); $add_peer.innerHTML = "Adding..."; - if ($allowed_ips.val() !== "" && $public_key.val() !== "" && $new_add_DNS.val() !== "" && $new_add_endpoint_allowed_ip.val() !== ""){ + if ($allowed_ips.val() !== "" && $public_key.val() !== "" && $new_add_endpoint_allowed_ip.val() !== ""){ let conf = $add_peer.getAttribute('conf_id'); let data_list = [$private_key, $allowed_ips, $new_add_name, $new_add_DNS, $new_add_endpoint_allowed_ip,$new_add_MTU, $new_add_keep_alive]; data_list.forEach((ele) => ele.attr("disabled", "disabled")); @@ -924,7 +924,7 @@ $("#save_peer_setting").on("click",function (){ let $peer_mtu = $("#peer_mtu"); let $peer_keep_alive = $("#peer_keep_alive"); - if ($peer_DNS_textbox.val() !== "" && + if ( $peer_allowed_ip_textbox.val() !== "" && $peer_endpoint_allowed_ips.val() !== ""){ let peer_id = $(this).attr("peer_id"); let conf_id = $(this).attr("conf_id"); diff --git a/src/templates/configuration.html b/src/templates/configuration.html index a7f2ce0..c600728 100644 --- a/src/templates/configuration.html +++ b/src/templates/configuration.html @@ -192,7 +192,7 @@
- +
@@ -297,7 +297,7 @@
- +
@@ -410,8 +410,8 @@ {% include "tools.html" %} {% include "footer.html" %} - - + + - +