1
0
mirror of https://github.com/donaldzou/WGDashboard.git synced 2024-11-22 23:27:45 +01:00

db path is global variable

This commit is contained in:
Galonza Peter 2022-01-01 02:57:59 +03:00
parent e2c8b3bf02
commit 72151ac6a3

View File

@ -34,9 +34,9 @@ from util import regex_match, check_DNS, check_Allowed_IPs, check_remote_endpoin
DASHBOARD_VERSION = 'v3.0' DASHBOARD_VERSION = 'v3.0'
# Dashboard Config Name # Dashboard Config Name
configuration_path = os.getenv('CONFIGURATION_PATH', '.') configuration_path = os.getenv('CONFIGURATION_PATH', '.')
db_path = os.path.join(configuration_path, 'db') DB_PATH = os.path.join(configuration_path, 'db')
if not os.path.isdir(db_path): if not os.path.isdir(DB_PATH):
os.mkdir(db_path) os.mkdir(DB_PATH)
DASHBOARD_CONF = os.path.join(configuration_path, 'wg-dashboard.ini') DASHBOARD_CONF = os.path.join(configuration_path, 'wg-dashboard.ini')
# Upgrade Required # Upgrade Required
UPDATE = None UPDATE = None
@ -256,7 +256,7 @@ def get_allowed_ip(db, peers, conf_peer_data):
# Look for new peers from WireGuard # Look for new peers from WireGuard
def get_all_peers_data(config_name): def get_all_peers_data(config_name):
sem.acquire(timeout=1) sem.acquire(timeout=1)
db = TinyDB(os.path.join(db_path, config_name + '.json')) db = TinyDB(os.path.join(DB_PATH, config_name + '.json'))
peers = Query() peers = Query()
conf_peer_data = read_conf_file(config_name) conf_peer_data = read_conf_file(config_name)
config = get_dashboard_conf() config = get_dashboard_conf()
@ -336,7 +336,7 @@ def get_peers(config_name, search, sort_t):
get_all_peers_data(config_name) get_all_peers_data(config_name)
sem.acquire(timeout=1) sem.acquire(timeout=1)
db = TinyDB(os.path.join(db_path, config_name + ".json")) db = TinyDB(os.path.join(DB_PATH, config_name + ".json"))
peer = Query() peer = Query()
if len(search) == 0: if len(search) == 0:
result = db.all() result = db.all()
@ -386,7 +386,7 @@ def get_conf_listen_port(config_name):
# Get configuration total data # Get configuration total data
def get_conf_total_data(config_name): def get_conf_total_data(config_name):
sem.acquire(timeout=1) sem.acquire(timeout=1)
db = TinyDB(os.path.join(db_path, config_name + ".json")) db = TinyDB(os.path.join(DB_PATH, config_name + ".json"))
upload_total = 0 upload_total = 0
download_total = 0 download_total = 0
for i in db.all(): for i in db.all():
@ -467,7 +467,7 @@ def f_check_key_match(private_key, public_key, config_name):
return result return result
else: else:
sem.acquire(timeout=1) sem.acquire(timeout=1)
db = TinyDB(os.path.join(db_path, config_name + ".json")) db = TinyDB(os.path.join(DB_PATH, config_name + ".json"))
peers = Query() peers = Query()
match = db.search(peers.id == result['data']) match = db.search(peers.id == result['data'])
if len(match) != 1 or result['data'] != public_key: if len(match) != 1 or result['data'] != public_key:
@ -488,7 +488,7 @@ def f_check_key_match(private_key, public_key, config_name):
# Check if there is repeated allowed IP # Check if there is repeated allowed IP
def check_repeat_allowed_ip(public_key, ip, config_name): def check_repeat_allowed_ip(public_key, ip, config_name):
sem.acquire(timeout=1) sem.acquire(timeout=1)
db = TinyDB(os.path.join(db_path, config_name + ".json")) db = TinyDB(os.path.join(DB_PATH, config_name + ".json"))
peers = Query() peers = Query()
peer = db.search(peers.id == public_key) peer = db.search(peers.id == public_key)
if len(peer) != 1: if len(peer) != 1:
@ -890,7 +890,7 @@ def switch(config_name):
@app.route('/add_peer/<config_name>', methods=['POST']) @app.route('/add_peer/<config_name>', methods=['POST'])
def add_peer(config_name): def add_peer(config_name):
sem.acquire(timeout=1) sem.acquire(timeout=1)
db = TinyDB(os.path.join(db_path, config_name + ".json")) db = TinyDB(os.path.join(DB_PATH, config_name + ".json"))
peers = Query() peers = Query()
data = request.get_json() data = request.get_json()
public_key = data['public_key'] public_key = data['public_key']
@ -991,7 +991,7 @@ def remove_peer(config_name):
if get_conf_status(config_name) == "stopped": if get_conf_status(config_name) == "stopped":
return "Your need to turn on " + config_name + " first." return "Your need to turn on " + config_name + " first."
sem.acquire(timeout=1) sem.acquire(timeout=1)
db = TinyDB(os.path.join(db_path, config_name + ".json")) db = TinyDB(os.path.join(DB_PATH, config_name + ".json"))
peers = Query() peers = Query()
data = request.get_json() data = request.get_json()
delete_key = data['peer_id'] delete_key = data['peer_id']
@ -1034,7 +1034,7 @@ def save_peer_setting(config_name):
endpoint_allowed_ip = data['endpoint_allowed_ip'] endpoint_allowed_ip = data['endpoint_allowed_ip']
preshared_key = data['preshared_key'] preshared_key = data['preshared_key']
sem.acquire(timeout=1) sem.acquire(timeout=1)
db = TinyDB(os.path.join(db_path, config_name + ".json")) db = TinyDB(os.path.join(DB_PATH, config_name + ".json"))
peers = Query() peers = Query()
if len(db.search(peers.id == id)) == 1: if len(db.search(peers.id == id)) == 1:
check_ip = check_repeat_allowed_ip(id, allowed_ip, config_name) check_ip = check_repeat_allowed_ip(id, allowed_ip, config_name)
@ -1145,7 +1145,7 @@ def get_peer_name(config_name):
data = request.get_json() data = request.get_json()
id = data['id'] id = data['id']
sem.acquire(timeout=1) sem.acquire(timeout=1)
db = TinyDB(os.path.join(db_path, config_name + ".json")) db = TinyDB(os.path.join(DB_PATH, config_name + ".json"))
peers = Query() peers = Query()
result = db.search(peers.id == id) result = db.search(peers.id == id)
db.close() db.close()
@ -1187,7 +1187,7 @@ def check_key_match(config_name):
def generate_qrcode(config_name): def generate_qrcode(config_name):
id = request.args.get('id') id = request.args.get('id')
sem.acquire(timeout=1) sem.acquire(timeout=1)
db = TinyDB(os.path.join(db_path, config_name + ".json")) db = TinyDB(os.path.join(DB_PATH, config_name + ".json"))
peers = Query() peers = Query()
get_peer = db.search(peers.id == id) get_peer = db.search(peers.id == id)
config = get_dashboard_conf() config = get_dashboard_conf()
@ -1243,7 +1243,7 @@ def download(config_name):
print(request.headers.get('User-Agent')) print(request.headers.get('User-Agent'))
id = request.args.get('id') id = request.args.get('id')
sem.acquire(timeout=1) sem.acquire(timeout=1)
db = TinyDB(os.path.join(db_path, config_name + ".json")) db = TinyDB(os.path.join(DB_PATH, config_name + ".json"))
peers = Query() peers = Query()
get_peer = db.search(peers.id == id) get_peer = db.search(peers.id == id)
config = get_dashboard_conf() config = get_dashboard_conf()
@ -1314,7 +1314,7 @@ Dashboard Tools Related
def get_ping_ip(): def get_ping_ip():
config = request.form['config'] config = request.form['config']
sem.acquire(timeout=1) sem.acquire(timeout=1)
db = TinyDB(os.path.join(db_path, config + ".json")) db = TinyDB(os.path.join(DB_PATH, config + ".json"))
html = "" html = ""
for i in db.all(): for i in db.all():
html += '<optgroup label="' + i['name'] + ' - ' + i['id'] + '">' html += '<optgroup label="' + i['name'] + ' - ' + i['id'] + '">'