diff --git a/.gitignore b/.gitignore index 85eed07..5afb0ba 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ private_key.txt public_key.txt venv/** log/** +release/* diff --git a/src/dashboard.py b/src/dashboard.py index 78e1ca8..a00ad0c 100644 --- a/src/dashboard.py +++ b/src/dashboard.py @@ -30,7 +30,11 @@ from icmplib import ping, multiping, traceroute, resolve, Host, Hop # Dashboard Version dashboard_version = 'v3.0' # Dashboard Config Name -dashboard_conf = 'wg-dashboard.ini' +configuration_path = os.getenv('CONFIGURATION_PATH', '.') +db_path = os.path.join(configuration_path, 'db') +if not os.path.isdir(db_path): + os.mkdir(db_path) +dashboard_conf = os.path.join(configuration_path, 'wg-dashboard.ini') # Upgrade Required update = "" # Flask App Configuration @@ -223,10 +227,8 @@ def get_allowed_ip(config_name, db, peers, conf_peer_data): # Look for new peers from WireGuard def get_all_peers_data(config_name): - sem.acquire() - - db = TinyDB('db/' + config_name + '.json') - + sem.acquire(timeout=1) + db = TinyDB(os.path.join(db_path, config_name + '.json')) peers = Query() conf_peer_data = read_conf_file(config_name) config = get_dashboard_conf() @@ -283,7 +285,10 @@ def get_all_peers_data(config_name): toc = time.perf_counter() print(f"Finish fetching data in {toc - tic:0.4f} seconds") db.close() - sem.release() + try: + sem.release() + except RuntimeError as e: + pass """ Frontend Related Functions @@ -291,9 +296,8 @@ Frontend Related Functions # Search for peers def get_peers(config_name, search, sort_t): get_all_peers_data(config_name) - sem.acquire() - - db = TinyDB('db/' + config_name + '.json') + sem.acquire(timeout=1) + db = TinyDB(os.path.join(db_path, config_name + ".json")) peer = Query() if len(search) == 0: result = db.all() @@ -335,9 +339,8 @@ def get_conf_listen_port(config_name): # Get configuration total data def get_conf_total_data(config_name): - sem.acquire() - - db = TinyDB('db/' + config_name + '.json') + sem.acquire(timeout=1) + db = TinyDB(os.path.join(db_path, config_name + ".json")) upload_total = 0 download_total = 0 for i in db.all(): @@ -414,9 +417,8 @@ def checkKeyMatch(private_key, public_key, config_name): if result['status'] == 'failed': return result else: - sem.acquire() - - db = TinyDB('db/' + config_name + '.json') + sem.acquire(timeout=1) + db = TinyDB(os.path.join(db_path, config_name + ".json")) peers = Query() match = db.search(peers.id == result['data']) if len(match) != 1 or result['data'] != public_key: @@ -430,10 +432,8 @@ def checkKeyMatch(private_key, public_key, config_name): # Check if there is repeated allowed IP def check_repeat_allowed_IP(public_key, ip, config_name): - - sem.acquire() - - db = TinyDB('db/' + config_name + '.json') + sem.acquire(timeout=1) + db = TinyDB(os.path.join(db_path, config_name + ".json")) peers = Query() peer = db.search(peers.id == public_key) if len(peer) != 1: @@ -754,7 +754,8 @@ def conf(config_name): def get_conf(config_name): config_interface = read_conf_file_interface(config_name) search = request.args.get('search') - if len(search) == 0: search = "" + if len(search) == 0: + search = "" search = urllib.parse.unquote(search) config = configparser.ConfigParser(strict=False) config.read(dashboard_conf) @@ -772,15 +773,20 @@ def get_conf(config_name): "public_key": get_conf_pub_key(config_name), "listen_port": get_conf_listen_port(config_name), "running_peer": get_conf_running_peer_number(config_name), - "conf_address": conf_address + "conf_address": conf_address, + "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 } if conf_data['status'] == "stopped": conf_data['checked'] = "nope" else: conf_data['checked'] = "checked" print(config.get("Peers","remote_endpoint")) - 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) + 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) # Turn on / off a configuration @app.route('/switch/', methods=['GET']) @@ -805,9 +811,8 @@ def switch(config_name): # Add peer @app.route('/add_peer/', methods=['POST']) def add_peer(config_name): - sem.acquire() - - db = TinyDB('db/' + config_name + '.json') + sem.acquire(timeout=1) + db = TinyDB(os.path.join(db_path, config_name + ".json")) peers = Query() data = request.get_json() public_key = data['public_key'] @@ -875,9 +880,8 @@ def add_peer(config_name): def remove_peer(config_name): if get_conf_status(config_name) == "stopped": return "Your need to turn on " + config_name + " first." - sem.acquire() - - db = TinyDB('db/' + config_name + '.json') + sem.acquire(timeout=1) + db = TinyDB(os.path.join(db_path, config_name + ".json")) peers = Query() data = request.get_json() delete_key = data['peer_id'] @@ -911,9 +915,8 @@ def save_peer_setting(config_name): DNS = data['DNS'] allowed_ip = data['allowed_ip'] endpoint_allowed_ip = data['endpoint_allowed_ip'] - sem.acquire() - - db = TinyDB('db/' + config_name + '.json') + sem.acquire(timeout=1) + db = TinyDB(os.path.join(db_path, config_name + ".json")) peers = Query() if len(db.search(peers.id == id)) == 1: check_ip = check_repeat_allowed_IP(id, allowed_ip, config_name) @@ -984,9 +987,8 @@ def save_peer_setting(config_name): def get_peer_name(config_name): data = request.get_json() id = data['id'] - sem.acquire() - - db = TinyDB('db/' + config_name + '.json') + sem.acquire(timeout=1) + db = TinyDB(os.path.join(db_path, config_name + ".json")) peers = Query() result = db.search(peers.id == id) db.close() @@ -1020,8 +1022,8 @@ def check_key_match(config_name): @app.route("/qrcode/", methods=['GET']) def generate_qrcode(config_name): id = request.args.get('id') - sem.acquire() - db = TinyDB('db/' + config_name + '.json') + sem.acquire(timeout=1) + db = TinyDB(os.path.join(db_path, config_name + ".json")) peers = Query() get_peer = db.search(peers.id == id) config = get_dashboard_conf() @@ -1060,8 +1062,8 @@ def generate_qrcode(config_name): def download(config_name): print(request.headers.get('User-Agent')) id = request.args.get('id') - sem.acquire() - db = TinyDB('db/' + config_name + '.json') + sem.acquire(timeout=1) + db = TinyDB(os.path.join(db_path, config_name + ".json")) peers = Query() get_peer = db.search(peers.id == id) config = get_dashboard_conf() @@ -1123,8 +1125,9 @@ Dashboard Tools Related @app.route('/get_ping_ip', methods=['POST']) def get_ping_ip(): config = request.form['config'] - sem.acquire() - db = TinyDB('db/' + config + '.json') + sem.acquire(timeout=1) + db = TinyDB(os.path.join(db_path, config_name + ".json")) + html = "" for i in db.all(): html += '' @@ -1184,8 +1187,8 @@ Dashboard Initialization """ def init_dashboard(): # Set Default INI File - if not os.path.isfile("wg-dashboard.ini"): - conf_file = open("wg-dashboard.ini", "w+") + if not os.path.isfile(dashboard_conf): + conf_file = open(dashboard_conf, "w+") config = configparser.ConfigParser(strict=False) config.read(dashboard_conf) # Defualt dashboard account setting @@ -1252,7 +1255,7 @@ if __name__ == "__main__": init_dashboard() update = check_update() config = configparser.ConfigParser(strict=False) - config.read('wg-dashboard.ini') + config.read(dashboard_conf) app_ip = config.get("Server", "app_ip") app_port = config.get("Server", "app_port") wg_conf_path = config.get("Server", "wg_conf_path") @@ -1262,7 +1265,7 @@ else: init_dashboard() update = check_update() config = configparser.ConfigParser(strict=False) - config.read('wg-dashboard.ini') + config.read(dashboard_conf) app_ip = config.get("Server", "app_ip") app_port = config.get("Server", "app_port") wg_conf_path = config.get("Server", "wg_conf_path") diff --git a/src/static/js/configuration.js b/src/static/js/configuration.js index a765487..7d6a4b4 100644 --- a/src/static/js/configuration.js +++ b/src/static/js/configuration.js @@ -303,7 +303,7 @@ $("body").on("change", "#sort_by_dropdown", function (){ headers:{"Content-Type": "application/json"}, url: "/update_dashboard_sort", success: function (res){ - location.reload() + load_data($('#search_peer_textbox').val()) } }) }) @@ -336,26 +336,41 @@ function copyToClipboard(element) { // Update Interval $("body").on("click", ".update_interval", function(){ - $.ajax({ - method:"POST", - data: "interval="+$(this).attr("refresh-interval"), - url: "/update_dashboard_refresh_interval", - success: function (res){ - location.reload() - } - }) - }); + $(".interval-btn-group button").removeClass("active"); + $(this).addClass("active"); + $.ajax({ + method:"POST", + data: "interval="+$(this).data("refresh-interval"), + url: "/update_dashboard_refresh_interval", + success: function (res){ + + load_data($('#search_peer_textbox').val()) + } + }) +}); $("body").on("click", ".refresh", function (){ load_data($('#search_peer_textbox').val()); }); // Switch display mode $("body").on("click", ".display_mode", function(){ + $(".display-btn-group button").removeClass("active"); + $(this).addClass("active"); + let display_mode = $(this).data("display-mode"); $.ajax({ method:"GET", - url: "/switch_display_mode/"+$(this).attr("display-mode"), + url: "/switch_display_mode/"+$(this).data("display-mode"), success: function (res){ - location.reload() + // load_data($('#search_peer_textbox').val()) + if (display_mode === "list"){ + Array($(".peer_list").children()).forEach(function(child){ + $(child).removeClass().addClass("col-12"); + }) + }else{ + Array($(".peer_list").children()).forEach(function(child){ + $(child).removeClass().addClass("col-sm-6 col-lg-4"); + }) + } } }) }) \ No newline at end of file diff --git a/src/templates/configuration.html b/src/templates/configuration.html index b1a881b..3f4612e 100644 --- a/src/templates/configuration.html +++ b/src/templates/configuration.html @@ -1,4 +1,4 @@ - + {% with title=title%} {% include "header.html"%} {% endwith %} @@ -11,7 +11,103 @@ -
+
+
+
+
+
+
+ CONFIGURATION +

{{ title }}

+
+
+ ACTION
+
+ +
+
+
+ STATUS +
+
+
+ CONNECTED PEERS +
+
+
+ TOTAL DATA USAGE +
+
+
+ TOTAL RECEIVED +
+
+
+ TOTAL SENT +
+
+
+
+ + PUBLIC KEY + CLICK TO COPY + +
+
+
+ LISTEN PORT +
+
+
+ ADDRESS +
+
+
+
+
+
+
+
+
+ + +
+
+
+
+
+
+ + + + + +
+
+
+
+
+
+
+ + +
+
+
+ +
+
+
+
+