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,24 +124,24 @@ 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:]
peer = -1 peer = -1
for i in conf_peers: for i in conf_peers:
@ -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,27 +200,28 @@ 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])
total_sent = cur_i[0]['total_sent'] if len(cur_i) > 0:
total_receive = cur_i[0]['total_receive'] total_sent = cur_i[0]['total_sent']
traffic = cur_i[0]['traffic'] total_receive = cur_i[0]['total_receive']
cur_total_sent = round(int(data_usage[count + 2]) / (1024 ** 3), 4) traffic = cur_i[0]['traffic']
cur_total_receive = round(int(data_usage[count + 1]) / (1024 ** 3), 4) cur_total_sent = round(int(data_usage[count + 2]) / (1024 ** 3), 4)
if cur_i[0]["status"] == "running": cur_total_receive = round(int(data_usage[count + 1]) / (1024 ** 3), 4)
if total_sent <= cur_total_sent and total_receive <= cur_total_receive: if cur_i[0]["status"] == "running":
total_sent = cur_total_sent if total_sent <= cur_total_sent and total_receive <= cur_total_receive:
total_receive = cur_total_receive total_sent = cur_total_sent
else: total_receive = cur_total_receive
now = datetime.now() else:
ctime = now.strftime("%d/%m/%Y %H:%M:%S") now = datetime.now()
traffic.append( ctime = now.strftime("%d/%m/%Y %H:%M:%S")
{"time": ctime, "total_receive": round(total_receive, 4), "total_sent": round(total_sent, 4), traffic.append(
"total_data": round(total_receive + total_sent, 4)}) {"time": ctime, "total_receive": round(total_receive, 4), "total_sent": round(total_sent, 4),
total_sent = 0 "total_data": round(total_receive + total_sent, 4)})
total_receive = 0 total_sent = 0
db.update({"traffic": traffic}, peers.id == data_usage[count]) total_receive = 0
db.update({"total_receive": round(total_receive, 4), db.update({"traffic": traffic}, peers.id == data_usage[count])
"total_sent": round(total_sent, 4), db.update({"total_receive": round(total_receive, 4),
"total_data": round(total_receive + total_sent, 4)}, peers.id == data_usage[count]) "total_sent": round(total_sent, 4),
"total_data": round(total_receive + total_sent, 4)}, peers.id == data_usage[count])
count += 3 count += 3
@ -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):
conf = configparser.ConfigParser(strict=False) try:
conf.read(wg_conf_path + "/" + config_name + ".conf") conf = configparser.ConfigParser(strict=False)
pri = conf.get("Interface", "PrivateKey") conf.read(wg_conf_path + "/" + config_name + ".conf")
pub = subprocess.run(f"echo '{pri}' | wg pubkey", check=True, shell=True, capture_output=True).stdout pri = conf.get("Interface", "PrivateKey")
conf.clear() pub = subprocess.run(f"echo '{pri}' | wg pubkey", check=True, shell=True, capture_output=True).stdout
return pub.decode().strip("\n") conf.clear()
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'] + '">'