Update dashboard.py

- Fixed #497
This commit is contained in:
Donald Zou 2024-11-26 21:27:32 +08:00
parent 6c5e0543b4
commit bf7fb898f9

View File

@ -521,21 +521,46 @@ class WireguardConfiguration:
self.getRestrictedPeersList() self.getRestrictedPeersList()
def __parseConfigurationFile(self): def __parseConfigurationFile(self):
self.__parser.read_file(open(self.__configPath)) with open(self.__configPath, 'r') as f:
sections = self.__parser.sections() original = [l.rstrip("\n") for l in f.readlines()]
if "Interface" not in sections: try:
raise self.InvalidConfigurationFileException( start = original.index("[Interface]")
"[Interface] section not found in " + os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], f'{self.Name}.conf'))
interfaceConfig = dict(self.__parser.items("Interface", True)) # Clean
for i in dir(self): for i in range(start, len(original)):
if str(i) in interfaceConfig.keys(): if original[i] == "[Peer]":
if isinstance(getattr(self, i), bool): break
setattr(self, i, StringToBoolean(interfaceConfig[i])) split = re.split(r'\s*=\s*', original[i], 1)
else: if len(split) == 2:
setattr(self, i, interfaceConfig[i]) key = split[0]
if self.PrivateKey: if key in dir(self):
self.PublicKey = self.__getPublicKey() if isinstance(getattr(self, key), bool):
self.Status = self.getStatus() setattr(self, key, False)
else:
setattr(self, key, "")
# Set
for i in range(start, len(original)):
if original[i] == "[Peer]":
break
split = re.split(r'\s*=\s*', original[i], 1)
if len(split) == 2:
key = split[0]
value = split[1]
if key in dir(self):
if isinstance(getattr(self, key), bool):
setattr(self, key, StringToBoolean(value))
else:
if len(getattr(self, key)) > 0:
setattr(self, key, f"{getattr(self, key)}, {value}")
else:
setattr(self, key, value)
except ValueError as e:
raise self.InvalidConfigurationFileException(
"[Interface] section not found in " + self.__configPath)
if self.PrivateKey:
self.PublicKey = self.__getPublicKey()
self.Status = self.getStatus()
def __dropDatabase(self): def __dropDatabase(self):
existingTables = sqlSelect(f"SELECT name FROM sqlite_master WHERE type='table' AND name LIKE '{self.Name}%'").fetchall() existingTables = sqlSelect(f"SELECT name FROM sqlite_master WHERE type='table' AND name LIKE '{self.Name}%'").fetchall()
@ -871,21 +896,6 @@ class WireguardConfiguration:
return ResponseObject(False, return ResponseObject(False,
f"Deleted {numOfDeletedPeers} peer(s) successfully. Failed to delete {numOfFailedToDeletePeers} peer(s)") f"Deleted {numOfDeletedPeers} peer(s) successfully. Failed to delete {numOfFailedToDeletePeers} peer(s)")
def __savePeers(self):
for i in self.Peers:
d = i.toJson()
sqlUpdate(
'''
UPDATE '%s' SET private_key = :private_key,
DNS = :DNS, endpoint_allowed_ip = :endpoint_allowed_ip, name = :name,
total_receive = :total_receive, total_sent = :total_sent, total_data = :total_data,
endpoint = :endpoint, status = :status, latest_handshake = :latest_handshake,
allowed_ip = :allowed_ip, cumu_receive = :cumu_receive, cumu_sent = :cumu_sent,
cumu_data = :cumu_data, mtu = :mtu, keepalive = :keepalive,
remote_endpoint = :remote_endpoint, preshared_key = :preshared_key WHERE id = :id
''' % self.Name, d
)
def __wgSave(self) -> tuple[bool, str] | tuple[bool, None]: def __wgSave(self) -> tuple[bool, str] | tuple[bool, None]:
try: try:
subprocess.check_output(f"wg-quick save {self.Name}", shell=True, stderr=subprocess.STDOUT) subprocess.check_output(f"wg-quick save {self.Name}", shell=True, stderr=subprocess.STDOUT)
@ -988,6 +998,7 @@ class WireguardConfiguration:
check = subprocess.check_output(f"wg-quick up {self.Name}", shell=True, stderr=subprocess.STDOUT) check = subprocess.check_output(f"wg-quick up {self.Name}", shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc: except subprocess.CalledProcessError as exc:
return False, str(exc.output.strip().decode("utf-8")) return False, str(exc.output.strip().decode("utf-8"))
self.__parseConfigurationFile()
self.getStatus() self.getStatus()
return True, None return True, None
@ -1098,31 +1109,28 @@ class WireguardConfiguration:
original = [] original = []
dataChanged = False dataChanged = False
with open(os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], f'{self.Name}.conf'), 'r') as f: with open(os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], f'{self.Name}.conf'), 'r') as f:
original = f.readlines() original = [l.rstrip("\n") for l in f.readlines()]
original = [l.rstrip("\n") for l in original] allowEdit = ["Address", "PreUp", "PostUp", "PreDown", "PostDown", "ListenPort"]
allowEdit = ["Address", "PreUp", "PostUp", "PreDown", "PostDown", "ListenPost"]
start = original.index("[Interface]") start = original.index("[Interface]")
for line in range(start+1, len(original)): try:
if original[line] == "[Peer]": end = original.index("[Peer]")
break except ValueError as e:
end = len(original)
new = ["[Interface]"]
peerFound = False
for line in range(start, end):
split = re.split(r'\s*=\s*', original[line], 1) split = re.split(r'\s*=\s*', original[line], 1)
if len(split) == 2: if len(split) == 2:
key = split[0] if split[0] not in allowEdit:
value = split[1] new.append(original[line])
if key in allowEdit and key in newData.keys() and value != newData[key]: for key in allowEdit:
split[1] = newData[key] new.insert(1, f"{key} = {newData[key].strip()}")
original[line] = " = ".join(split) new.append("")
if isinstance(getattr(self, key), bool): for line in range(end, len(original)):
setattr(self, key, StringToBoolean(newData[key])) new.append(original[line])
else:
setattr(self, key, str(newData[key]))
dataChanged = True
print(original[line])
if dataChanged:
with open(os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], f'{self.Name}.conf'), 'w') as f:
f.write("\n".join(original))
self.backupConfigurationFile() self.backupConfigurationFile()
with open(os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], f'{self.Name}.conf'), 'w') as f:
f.write("\n".join(new))
status, msg = self.toggleConfiguration() status, msg = self.toggleConfiguration()
if not status: if not status: