From e2e821881c3bbeb595fffefd9dad611805924fdb Mon Sep 17 00:00:00 2001 From: Donald Zou Date: Wed, 16 Oct 2024 17:44:49 +0800 Subject: [PATCH] Peer restore works Still need to SQL Dump the table aswell --- src/dashboard.py | 124 ++++++++++++--- .../backupRestoreComponents/backup.vue | 144 ++++++++++++++++++ .../configurationBackupRestore.vue | 122 +++++++++++++++ .../editConfiguration.vue | 3 - .../configurationBackupRestore.vue | 109 ------------- .../configurationComponents/peerList.vue | 3 +- 6 files changed, 367 insertions(+), 138 deletions(-) create mode 100644 src/static/app/src/components/configurationComponents/backupRestoreComponents/backup.vue create mode 100644 src/static/app/src/components/configurationComponents/configurationBackupRestore.vue delete mode 100644 src/static/app/src/components/configurationComponents/editConfigurationComponents/configurationBackupRestore.vue diff --git a/src/dashboard.py b/src/dashboard.py index e9ceba5..d5a69dd 100644 --- a/src/dashboard.py +++ b/src/dashboard.py @@ -461,27 +461,10 @@ class WireguardConfiguration: self.PreDown: str = "" self.PostDown: str = "" self.SaveConfig: bool = True + self.Name = name if name is not None: - self.Name = name - self.__parser.read_file(open(os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], f'{self.Name}.conf'))) - sections = self.__parser.sections() - if "Interface" not in sections: - raise self.InvalidConfigurationFileException( - "[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)) - for i in dir(self): - if str(i) in interfaceConfig.keys(): - if isinstance(getattr(self, i), bool): - setattr(self, i, _strToBool(interfaceConfig[i])) - else: - setattr(self, i, interfaceConfig[i]) - - if self.PrivateKey: - self.PublicKey = self.__getPublicKey() - - self.Status = self.getStatus() - + self.__parseConfigurationFile() else: self.Name = data["ConfigurationName"] for i in dir(self): @@ -505,10 +488,33 @@ class WireguardConfiguration: f"{self.Name}.conf"), "w+") as configFile: self.__parser.write(configFile) - self.Peers: list[Peer] = [] + self.__createDatabase() + self.__initPeersList() + + def __initPeersList(self): + self.Peers: list[Peer] = [] self.getPeersList() self.getRestrictedPeersList() + + def __parseConfigurationFile(self): + self.__parser.read_file(open(os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], f'{self.Name}.conf'))) + sections = self.__parser.sections() + if "Interface" not in sections: + raise self.InvalidConfigurationFileException( + "[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)) + for i in dir(self): + if str(i) in interfaceConfig.keys(): + if isinstance(getattr(self, i), bool): + setattr(self, i, _strToBool(interfaceConfig[i])) + else: + setattr(self, i, interfaceConfig[i]) + + if self.PrivateKey: + self.PublicKey = self.__getPublicKey() + + self.Status = self.getStatus() def __createDatabase(self): existingTables = sqlSelect("SELECT name FROM sqlite_master WHERE type='table'").fetchall() @@ -923,10 +929,9 @@ class WireguardConfiguration: os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], f'{self.Name}.conf'), os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], 'WGDashboard_Backup', f'{self.Name}_{datetime.now().strftime("%Y%m%d%H%M%S")}.conf') ) - with open(os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], f'{self.Name}.conf'), 'w') as f: - f.write("\n".join(original)) + - def getBackups(self): + def getBackups(self) -> list[dict[str: str, str: str, str: str]]: backups = [] directory = os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], 'WGDashboard_Backup') @@ -946,8 +951,35 @@ class WireguardConfiguration: return backups - def restoreBackup(self, backupFileName: str): - pass + def restoreBackup(self, backupFileName: str) -> bool: + backups = list(map(lambda x : x['filename'], self.getBackups())) + if backupFileName not in backups: + return False + self.backupConfigurationFile() + if self.Status: + self.toggleConfiguration() + target = os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], 'WGDashboard_Backup', backupFileName) + if not os.path.exists(target): + return False + targetContent = open(target, 'r').read() + try: + with open(os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], f'{self.Name}.conf'), 'w') as f: + f.write(targetContent) + except Exception as e: + return False + self.__parseConfigurationFile() + self.__initPeersList() + return True + + def deleteBackup(self, backupFileName: str) -> bool: + backups = list(map(lambda x : x['filename'], self.getBackups())) + if backupFileName not in backups: + return False + try: + os.remove(os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], 'WGDashboard_Backup', backupFileName)) + except Exception as e: + return False + return True def updateConfigurationSettings(self, newData: dict) -> tuple[bool, str]: if self.Status: @@ -976,6 +1008,8 @@ class WireguardConfiguration: 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() @@ -1691,6 +1725,46 @@ def API_getWireguardConfigurationBackup(): return ResponseObject(False, "Configuration does not exist") return ResponseObject(data=WireguardConfigurations[configurationName].getBackups()) +@app.get(f'{APP_PREFIX}/api/createWireguardConfigurationBackup') +def API_createWireguardConfigurationBackup(): + configurationName = request.args.get('configurationName') + if configurationName is None or configurationName not in WireguardConfigurations.keys(): + return ResponseObject(False, "Configuration does not exist") + return ResponseObject(status=WireguardConfigurations[configurationName].backupConfigurationFile(), + data=WireguardConfigurations[configurationName].getBackups()) + +@app.post(f'{APP_PREFIX}/api/deleteWireguardConfigurationBackup') +def API_deleteWireguardConfigurationBackup(): + data = request.get_json() + if ("configurationName" not in data.keys() or + "backupFileName" not in data.keys() or + len(data['configurationName']) == 0 or + len(data['backupFileName']) == 0): + return ResponseObject(False, + "Please provide configurationName and backupFileName in body") + configurationName = data['configurationName'] + backupFileName = data['backupFileName'] + if configurationName not in WireguardConfigurations.keys(): + return ResponseObject(False, "Configuration does not exist") + + return ResponseObject(WireguardConfigurations[configurationName].deleteBackup(backupFileName)) + +@app.post(f'{APP_PREFIX}/api/restoreWireguardConfigurationBackup') +def API_restoreWireguardConfigurationBackup(): + data = request.get_json() + if ("configurationName" not in data.keys() or + "backupFileName" not in data.keys() or + len(data['configurationName']) == 0 or + len(data['backupFileName']) == 0): + return ResponseObject(False, + "Please provide configurationName and backupFileName in body") + configurationName = data['configurationName'] + backupFileName = data['backupFileName'] + if configurationName not in WireguardConfigurations.keys(): + return ResponseObject(False, "Configuration does not exist") + + return ResponseObject(WireguardConfigurations[configurationName].restoreBackup(backupFileName)) + @app.get(f'{APP_PREFIX}/api/getDashboardConfiguration') def API_getDashboardConfiguration(): return ResponseObject(data=DashboardConfig.toJson()) diff --git a/src/static/app/src/components/configurationComponents/backupRestoreComponents/backup.vue b/src/static/app/src/components/configurationComponents/backupRestoreComponents/backup.vue new file mode 100644 index 0000000..29ca775 --- /dev/null +++ b/src/static/app/src/components/configurationComponents/backupRestoreComponents/backup.vue @@ -0,0 +1,144 @@ + + + + + \ No newline at end of file diff --git a/src/static/app/src/components/configurationComponents/configurationBackupRestore.vue b/src/static/app/src/components/configurationComponents/configurationBackupRestore.vue new file mode 100644 index 0000000..18c6da1 --- /dev/null +++ b/src/static/app/src/components/configurationComponents/configurationBackupRestore.vue @@ -0,0 +1,122 @@ + + + + + \ No newline at end of file diff --git a/src/static/app/src/components/configurationComponents/editConfiguration.vue b/src/static/app/src/components/configurationComponents/editConfiguration.vue index c7270be..9184131 100644 --- a/src/static/app/src/components/configurationComponents/editConfiguration.vue +++ b/src/static/app/src/components/configurationComponents/editConfiguration.vue @@ -4,9 +4,6 @@ import {onMounted, reactive, ref, useTemplateRef, watch} from "vue"; import {WireguardConfigurationsStore} from "@/stores/WireguardConfigurationsStore.js"; import {fetchPost} from "@/utilities/fetch.js"; import {DashboardConfigurationStore} from "@/stores/DashboardConfigurationStore.js"; -import router from "@/router/index.js"; -import ConfigurationBackupRestore - from "@/components/configurationComponents/editConfigurationComponents/configurationBackupRestore.vue"; const props = defineProps({ configurationInfo: Object }) diff --git a/src/static/app/src/components/configurationComponents/editConfigurationComponents/configurationBackupRestore.vue b/src/static/app/src/components/configurationComponents/editConfigurationComponents/configurationBackupRestore.vue deleted file mode 100644 index fe47d98..0000000 --- a/src/static/app/src/components/configurationComponents/editConfigurationComponents/configurationBackupRestore.vue +++ /dev/null @@ -1,109 +0,0 @@ - - - - - \ No newline at end of file diff --git a/src/static/app/src/components/configurationComponents/peerList.vue b/src/static/app/src/components/configurationComponents/peerList.vue index d9f3265..87c6b29 100644 --- a/src/static/app/src/components/configurationComponents/peerList.vue +++ b/src/static/app/src/components/configurationComponents/peerList.vue @@ -45,7 +45,7 @@ import LocaleText from "@/components/text/localeText.vue"; import EditConfiguration from "@/components/configurationComponents/editConfiguration.vue"; import SelectPeers from "@/components/configurationComponents/selectPeers.vue"; import ConfigurationBackupRestore - from "@/components/configurationComponents/editConfigurationComponents/configurationBackupRestore.vue"; + from "@/components/configurationComponents/configurationBackupRestore.vue"; Chart.register( ArcElement, @@ -682,6 +682,7 @@ export default {