Merge pull request #566 from donaldzou/fix-edit-configuration

Edited configuration not updating to dashboard
This commit is contained in:
Donald Zou 2024-12-30 20:33:10 +08:00 committed by GitHub
commit 85be6d53d0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1098,35 +1098,42 @@ 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} = {str(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, _strToBool(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()
print(f"[WGDashboard] Edited Configuration -- {self.Name}.conf")
print("\n".join(new))
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:
return False, msg return False, msg
for i in allowEdit:
if isinstance(getattr(self, i), bool):
setattr(self, i, _strToBool(newData[i]))
else:
setattr(self, i, str(newData[i]))
return True, "" return True, ""
def deleteConfiguration(self): def deleteConfiguration(self):
@ -1631,13 +1638,13 @@ sqldb.isolation_level = None
cursor = sqldb.cursor() cursor = sqldb.cursor()
def sqlSelect(statement: str, paramters: tuple = ()) -> sqlite3.Cursor: def sqlSelect(statement: str, paramters: tuple = ()) -> sqlite3.Cursor:
with sqldb: try:
try: cursor = sqldb.cursor()
cursor = sqldb.cursor() return cursor.execute(statement, paramters)
return cursor.execute(statement, paramters) except Exception as e:
except Exception as e: print("[WGDashboard] SQLite Error:" + str(error) + " | Statement: " + statement)
print("[WGDashboard] SQLite Error:" + str(error) + " | Statement: " + statement) return []
return []
def sqlUpdate(statement: str, paramters: tuple = ()) -> sqlite3.Cursor: def sqlUpdate(statement: str, paramters: tuple = ()) -> sqlite3.Cursor: