Skip to content

Commit 16998d1

Browse files
authored
Merge pull request #306 from donaldzou/v4.0-fix1
Fixed peer status is not refreshing correctly
2 parents 1af708a + 11421d2 commit 16998d1

File tree

2 files changed

+72
-72
lines changed

2 files changed

+72
-72
lines changed

src/dashboard.py

+71-71
Original file line numberDiff line numberDiff line change
@@ -570,78 +570,78 @@ def __getRestrictedPeers(self):
570570
def __getPeers(self):
571571

572572
mt = os.path.getmtime(os.path.join(WG_CONF_PATH, f'{self.Name}.conf'))
573-
if self.__configFileModifiedTime is None or self.__configFileModifiedTime != mt:
574-
self.Peers = []
575-
with open(os.path.join(WG_CONF_PATH, f'{self.Name}.conf'), 'r') as configFile:
576-
p = []
577-
pCounter = -1
578-
content = configFile.read().split('\n')
579-
try:
580-
peerStarts = content.index("[Peer]")
581-
content = content[peerStarts:]
582-
for i in content:
583-
if not regex_match("#(.*)", i) and not regex_match(";(.*)", i):
584-
if i == "[Peer]":
585-
pCounter += 1
586-
p.append({})
587-
p[pCounter]["name"] = ""
588-
else:
589-
if len(i) > 0:
590-
split = re.split(r'\s*=\s*', i, 1)
591-
if len(split) == 2:
592-
p[pCounter][split[0]] = split[1]
593-
594-
if regex_match("#Name# = (.*)", i):
595-
split = re.split(r'\s*=\s*', i, 1)
596-
print(split)
597-
if len(split) == 2:
598-
p[pCounter]["name"] = split[1]
573+
# if self.__configFileModifiedTime is None or self.__configFileModifiedTime != mt:
574+
self.Peers = []
575+
with open(os.path.join(WG_CONF_PATH, f'{self.Name}.conf'), 'r') as configFile:
576+
p = []
577+
pCounter = -1
578+
content = configFile.read().split('\n')
579+
try:
580+
peerStarts = content.index("[Peer]")
581+
content = content[peerStarts:]
582+
for i in content:
583+
if not regex_match("#(.*)", i) and not regex_match(";(.*)", i):
584+
if i == "[Peer]":
585+
pCounter += 1
586+
p.append({})
587+
p[pCounter]["name"] = ""
588+
else:
589+
if len(i) > 0:
590+
split = re.split(r'\s*=\s*', i, 1)
591+
if len(split) == 2:
592+
p[pCounter][split[0]] = split[1]
599593

600-
for i in p:
601-
if "PublicKey" in i.keys():
602-
checkIfExist = sqldb.cursor().execute("SELECT * FROM '%s' WHERE id = ?" % self.Name,
603-
((i['PublicKey']),)).fetchone()
604-
if checkIfExist is None:
605-
newPeer = {
606-
"id": i['PublicKey'],
607-
"private_key": "",
608-
"DNS": DashboardConfig.GetConfig("Peers", "peer_global_DNS")[1],
609-
"endpoint_allowed_ip": DashboardConfig.GetConfig("Peers", "peer_endpoint_allowed_ip")[
610-
1],
611-
"name": i.get("name"),
612-
"total_receive": 0,
613-
"total_sent": 0,
614-
"total_data": 0,
615-
"endpoint": "N/A",
616-
"status": "stopped",
617-
"latest_handshake": "N/A",
618-
"allowed_ip": i.get("AllowedIPs", "N/A"),
619-
"cumu_receive": 0,
620-
"cumu_sent": 0,
621-
"cumu_data": 0,
622-
"traffic": [],
623-
"mtu": DashboardConfig.GetConfig("Peers", "peer_mtu")[1],
624-
"keepalive": DashboardConfig.GetConfig("Peers", "peer_keep_alive")[1],
625-
"remote_endpoint": DashboardConfig.GetConfig("Peers", "remote_endpoint")[1],
626-
"preshared_key": i["PresharedKey"] if "PresharedKey" in i.keys() else ""
627-
}
628-
sqldb.cursor().execute(
629-
"""
630-
INSERT INTO '%s'
631-
VALUES (:id, :private_key, :DNS, :endpoint_allowed_ip, :name, :total_receive, :total_sent,
632-
:total_data, :endpoint, :status, :latest_handshake, :allowed_ip, :cumu_receive, :cumu_sent,
633-
:cumu_data, :mtu, :keepalive, :remote_endpoint, :preshared_key);
634-
""" % self.Name
635-
, newPeer)
636-
sqldb.commit()
637-
self.Peers.append(Peer(newPeer, self))
638-
else:
639-
sqldb.cursor().execute("UPDATE '%s' SET allowed_ip = ? WHERE id = ?" % self.Name,
640-
(i.get("AllowedIPs", "N/A"), i['PublicKey'],))
641-
sqldb.commit()
642-
self.Peers.append(Peer(checkIfExist, self))
643-
except Exception as e:
644-
print(f"[WGDashboard] {self.Name} Error: {str(e)}")
594+
if regex_match("#Name# = (.*)", i):
595+
split = re.split(r'\s*=\s*', i, 1)
596+
print(split)
597+
if len(split) == 2:
598+
p[pCounter]["name"] = split[1]
599+
600+
for i in p:
601+
if "PublicKey" in i.keys():
602+
checkIfExist = sqldb.cursor().execute("SELECT * FROM '%s' WHERE id = ?" % self.Name,
603+
((i['PublicKey']),)).fetchone()
604+
if checkIfExist is None:
605+
newPeer = {
606+
"id": i['PublicKey'],
607+
"private_key": "",
608+
"DNS": DashboardConfig.GetConfig("Peers", "peer_global_DNS")[1],
609+
"endpoint_allowed_ip": DashboardConfig.GetConfig("Peers", "peer_endpoint_allowed_ip")[
610+
1],
611+
"name": i.get("name"),
612+
"total_receive": 0,
613+
"total_sent": 0,
614+
"total_data": 0,
615+
"endpoint": "N/A",
616+
"status": "stopped",
617+
"latest_handshake": "N/A",
618+
"allowed_ip": i.get("AllowedIPs", "N/A"),
619+
"cumu_receive": 0,
620+
"cumu_sent": 0,
621+
"cumu_data": 0,
622+
"traffic": [],
623+
"mtu": DashboardConfig.GetConfig("Peers", "peer_mtu")[1],
624+
"keepalive": DashboardConfig.GetConfig("Peers", "peer_keep_alive")[1],
625+
"remote_endpoint": DashboardConfig.GetConfig("Peers", "remote_endpoint")[1],
626+
"preshared_key": i["PresharedKey"] if "PresharedKey" in i.keys() else ""
627+
}
628+
sqldb.cursor().execute(
629+
"""
630+
INSERT INTO '%s'
631+
VALUES (:id, :private_key, :DNS, :endpoint_allowed_ip, :name, :total_receive, :total_sent,
632+
:total_data, :endpoint, :status, :latest_handshake, :allowed_ip, :cumu_receive, :cumu_sent,
633+
:cumu_data, :mtu, :keepalive, :remote_endpoint, :preshared_key);
634+
""" % self.Name
635+
, newPeer)
636+
sqldb.commit()
637+
self.Peers.append(Peer(newPeer, self))
638+
else:
639+
sqldb.cursor().execute("UPDATE '%s' SET allowed_ip = ? WHERE id = ?" % self.Name,
640+
(i.get("AllowedIPs", "N/A"), i['PublicKey'],))
641+
sqldb.commit()
642+
self.Peers.append(Peer(checkIfExist, self))
643+
except Exception as e:
644+
print(f"[WGDashboard] {self.Name} Error: {str(e)}")
645645
self.__configFileModifiedTime = mt
646646

647647
def addPeers(self, peers: list):

src/wgd.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ _checkPythonVersion(){
220220
else
221221
printf "[WGDashboard] %s Could not find a compatible version of Python. Current Python is %s.\n" "$heavy_crossmark" "$version"
222222
printf "[WGDashboard] WGDashboard required Python 3.10, 3.11 or 3.12. Halting install now.\n"
223-
kill $TOP_PID
223+
kill $TOP_PID
224224
fi
225225
}
226226

0 commit comments

Comments
 (0)