diff --git a/src/dashboard.py b/src/dashboard.py index 41387c9..1407a75 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 @@ -224,7 +228,7 @@ 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(timeout=1) - db = TinyDB('db/' + config_name + '.json') + db = TinyDB(os.path.join(db_path, config_name + '.json')) peers = Query() conf_peer_data = read_conf_file(config_name) config = get_dashboard_conf() @@ -293,8 +297,7 @@ Frontend Related Functions def get_peers(config_name, search, sort_t): get_all_peers_data(config_name) sem.acquire(timeout=1) - - db = TinyDB('db/' + config_name + '.json') + db = TinyDB(os.path.join(db_path, config_name + ".json")) peer = Query() if len(search) == 0: result = db.all() @@ -337,8 +340,7 @@ def get_conf_listen_port(config_name): # Get configuration total data def get_conf_total_data(config_name): sem.acquire(timeout=1) - - db = TinyDB('db/' + config_name + '.json') + db = TinyDB(os.path.join(db_path, config_name + ".json")) upload_total = 0 download_total = 0 for i in db.all(): @@ -416,8 +418,7 @@ def checkKeyMatch(private_key, public_key, config_name): return result else: sem.acquire(timeout=1) - - db = TinyDB('db/' + config_name + '.json') + 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: @@ -431,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(timeout=1) - - db = TinyDB('db/' + config_name + '.json') + db = TinyDB(os.path.join(db_path, config_name + ".json")) peers = Query() peer = db.search(peers.id == public_key) if len(peer) != 1: @@ -813,8 +812,7 @@ def switch(config_name): @app.route('/add_peer/', methods=['POST']) def add_peer(config_name): sem.acquire(timeout=1) - - db = TinyDB('db/' + config_name + '.json') + db = TinyDB(os.path.join(db_path, config_name + ".json")) peers = Query() data = request.get_json() public_key = data['public_key'] @@ -883,8 +881,7 @@ def remove_peer(config_name): if get_conf_status(config_name) == "stopped": return "Your need to turn on " + config_name + " first." sem.acquire(timeout=1) - - db = TinyDB('db/' + config_name + '.json') + db = TinyDB(os.path.join(db_path, config_name + ".json")) peers = Query() data = request.get_json() delete_key = data['peer_id'] @@ -919,8 +916,7 @@ def save_peer_setting(config_name): allowed_ip = data['allowed_ip'] endpoint_allowed_ip = data['endpoint_allowed_ip'] sem.acquire(timeout=1) - - db = TinyDB('db/' + config_name + '.json') + 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) @@ -992,8 +988,7 @@ def get_peer_name(config_name): data = request.get_json() id = data['id'] sem.acquire(timeout=1) - - db = TinyDB('db/' + config_name + '.json') + db = TinyDB(os.path.join(db_path, config_name + ".json")) peers = Query() result = db.search(peers.id == id) db.close() @@ -1028,7 +1023,7 @@ def check_key_match(config_name): def generate_qrcode(config_name): id = request.args.get('id') sem.acquire(timeout=1) - db = TinyDB('db/' + config_name + '.json') + db = TinyDB(os.path.join(db_path, config_name + ".json")) peers = Query() get_peer = db.search(peers.id == id) config = get_dashboard_conf() @@ -1068,7 +1063,7 @@ def download(config_name): print(request.headers.get('User-Agent')) id = request.args.get('id') sem.acquire(timeout=1) - db = TinyDB('db/' + config_name + '.json') + db = TinyDB(os.path.join(db_path, config_name + ".json")) peers = Query() get_peer = db.search(peers.id == id) config = get_dashboard_conf() @@ -1131,7 +1126,8 @@ Dashboard Tools Related def get_ping_ip(): config = request.form['config'] sem.acquire(timeout=1) - db = TinyDB('db/' + config + '.json') + db = TinyDB(os.path.join(db_path, config_name + ".json")) + html = "" for i in db.all(): html += '' @@ -1191,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 @@ -1259,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") @@ -1269,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")