1
0
mirror of https://github.com/donaldzou/WGDashboard.git synced 2024-10-02 09:30:11 +02:00

Update dashboard.py

This commit is contained in:
Donald Cheng Hong Zou 2021-12-29 14:57:44 -05:00
parent a136b86d81
commit b5b30c8119

View File

@ -124,22 +124,22 @@ def read_conf_file_interface(config_name):
def read_conf_file(config_name): def read_conf_file(config_name):
# Read Configuration File Start # Read Configuration File Start
conf_location = wg_conf_path + "/" + config_name + ".conf" conf_location = wg_conf_path + "/" + config_name + ".conf"
with open(conf_location, 'r', encoding='utf-8') as file_object: f = open(conf_location, 'r')
file = file_object.read().split("\n") file = f.read().split("\n")
conf_peer_data = { conf_peer_data = {
"Interface": {}, "Interface": {},
"Peers": [] "Peers": []
} }
peers_start = 0 peers_start = 0
for i in file: for i in range(len(file)):
if not regex_match("#(.*)", i): if not regex_match("#(.*)", file[i]):
if i == "[Peer]": if file[i] == "[Peer]":
peers_start = i peers_start = i
break break
else:
if len(i) > 0: if len(file[i]) > 0:
if i != "[Interface]": if file[i] != "[Interface]":
tmp = re.split(r'\s*=\s*', i, 1) tmp = re.split(r'\s*=\s*', file[i], 1)
if len(tmp) == 2: if len(tmp) == 2:
conf_peer_data['Interface'][tmp[0]] = tmp[1] conf_peer_data['Interface'][tmp[0]] = tmp[1]
conf_peers = file[peers_start:] conf_peers = file[peers_start:]
@ -151,10 +151,11 @@ def read_conf_file(config_name):
conf_peer_data["Peers"].append({}) conf_peer_data["Peers"].append({})
elif peer > -1: elif peer > -1:
if len(i) > 0: if len(i) > 0:
tmp = re.split(r'\s*=\s*', i, 1) tmp = re.split('\s*=\s*', i, 1)
if len(tmp) == 2: if len(tmp) == 2:
conf_peer_data["Peers"][peer][tmp[0]] = tmp[1] conf_peer_data["Peers"][peer][tmp[0]] = tmp[1]
f.close()
# Read Configuration File End # Read Configuration File End
return conf_peer_data return conf_peer_data
@ -199,6 +200,7 @@ def get_transfer(config_name, db, peers):
count = 0 count = 0
for _ in range(int(len(data_usage) / 3)): for _ in range(int(len(data_usage) / 3)):
cur_i = db.search(peers.id == data_usage[count]) cur_i = db.search(peers.id == data_usage[count])
if len(cur_i) > 0:
total_sent = cur_i[0]['total_sent'] total_sent = cur_i[0]['total_sent']
total_receive = cur_i[0]['total_receive'] total_receive = cur_i[0]['total_receive']
traffic = cur_i[0]['traffic'] traffic = cur_i[0]['traffic']
@ -353,12 +355,15 @@ def get_peers(config_name, search, sort_t):
# Get configuration public key # Get configuration public key
def get_conf_pub_key(config_name): def get_conf_pub_key(config_name):
try:
conf = configparser.ConfigParser(strict=False) conf = configparser.ConfigParser(strict=False)
conf.read(wg_conf_path + "/" + config_name + ".conf") conf.read(wg_conf_path + "/" + config_name + ".conf")
pri = conf.get("Interface", "PrivateKey") pri = conf.get("Interface", "PrivateKey")
pub = subprocess.run(f"echo '{pri}' | wg pubkey", check=True, shell=True, capture_output=True).stdout pub = subprocess.run(f"echo '{pri}' | wg pubkey", check=True, shell=True, capture_output=True).stdout
conf.clear() conf.clear()
return pub.decode().strip("\n") return pub.decode().strip("\n")
except configparser.NoSectionError as e:
return ""
# Get configuration listen port # Get configuration listen port
@ -1322,8 +1327,7 @@ Dashboard Tools Related
def get_ping_ip(): def get_ping_ip():
config_name = request.form['config'] config_name = 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_name + ".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'] + '">'