From 27de7ddbf826a11ffce165b88bbee56dbc0c7cf9 Mon Sep 17 00:00:00 2001 From: Donald Zou Date: Tue, 15 Oct 2024 00:30:20 +0800 Subject: [PATCH] Still working on backup & restore --- src/dashboard.py | 76 +++++++++++++------ .../editConfiguration.vue | 8 +- .../configurationBackupRestore.vue | 61 +++++++++++++++ .../configurationComponents/peerList.vue | 4 +- .../configurationComponents/peerSettings.vue | 10 +-- .../configurationComponents/selectPeers.vue | 2 - src/static/app/src/components/navbar.vue | 4 +- 7 files changed, 128 insertions(+), 37 deletions(-) create mode 100644 src/static/app/src/components/configurationComponents/editConfigurationComponents/configurationBackupRestore.vue diff --git a/src/dashboard.py b/src/dashboard.py index b2d5c67..e9ceba5 100644 --- a/src/dashboard.py +++ b/src/dashboard.py @@ -136,17 +136,17 @@ class DashboardLogger: def log(self, URL: str = "", IP: str = "", Status: str = "true", Message: str = "") -> bool: pass - # try: - # with self.loggerdb: - # loggerdbCursor = self.loggerdb.cursor() - # loggerdbCursor.execute( - # "INSERT INTO DashboardLog (LogID, URL, IP, Status, Message) VALUES (?, ?, ?, ?, ?)", (str(uuid.uuid4()), URL, IP, Status, Message,)) - # if self.loggerdb.in_transaction: - # self.loggerdb.commit() - # return True - # except Exception as e: - # print(f"[WGDashboard] Access Log Error: {str(e)}") - # return False + try: + with self.loggerdb: + loggerdbCursor = self.loggerdb.cursor() + loggerdbCursor.execute( + "INSERT INTO DashboardLog (LogID, URL, IP, Status, Message) VALUES (?, ?, ?, ?, ?)", (str(uuid.uuid4()), URL, IP, Status, Message,)) + if self.loggerdb.in_transaction: + self.loggerdb.commit() + return True + except Exception as e: + print(f"[WGDashboard] Access Log Error: {str(e)}") + return False class PeerJobLogger: def __init__(self): @@ -587,7 +587,6 @@ class WireguardConfiguration: return changed def __getPeers(self): - if self.configurationFileChanged(): self.Peers = [] with open(os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], f'{self.Name}.conf'), 'r') as configFile: @@ -663,7 +662,6 @@ class WireguardConfiguration: checkIfExist = sqlSelect("SELECT * FROM '%s'" % self.Name).fetchall() for i in checkIfExist: self.Peers.append(Peer(i, self)) - def addPeers(self, peers: list): for p in peers: @@ -918,6 +916,39 @@ class WireguardConfiguration: "TotalPeers": len(self.Peers) } + def backupConfigurationFile(self): + if not os.path.exists(os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], 'WGDashboard_Backup')): + os.mkdir(os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], 'WGDashboard_Backup')) + shutil.copy( + 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): + backups = [] + + directory = os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], 'WGDashboard_Backup') + files = [(file, os.path.getctime(os.path.join(directory, file))) + for file in os.listdir(directory) if os.path.isfile(os.path.join(directory, file))] + files.sort(key=lambda x: x[1], reverse=True) + + for f, ct in files: + if _regexMatch(f"^({self.Name})_(.*)\.(conf)$", f): + s = re.search(f"^({self.Name})_(.*)\.(conf)$", f) + date = s.group(2) + backups.append({ + "filename": f, + "backupDate": date, + "content": open(os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], 'WGDashboard_Backup', f), 'r').read() + }) + + return backups + + def restoreBackup(self, backupFileName: str): + pass + def updateConfigurationSettings(self, newData: dict) -> tuple[bool, str]: if self.Status: self.toggleConfiguration() @@ -927,8 +958,6 @@ class WireguardConfiguration: original = f.readlines() original = [l.rstrip("\n") for l in original] allowEdit = ["Address", "PreUp", "PostUp", "PreDown", "PostDown", "ListenPost", "PrivateKey"] - - start = original.index("[Interface]") for line in range(start+1, len(original)): if original[line] == "[Peer]": @@ -947,15 +976,7 @@ class WireguardConfiguration: dataChanged = True print(original[line]) if dataChanged: - - if not os.path.exists(os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], 'WGDashboard_Backup')): - os.mkdir(os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], 'WGDashboard_Backup')) - shutil.copy( - 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)) + self.backupConfigurationFile() status, msg = self.toggleConfiguration() @@ -1663,6 +1684,13 @@ def API_updateWireguardConfiguration(): return ResponseObject(status, message=msg, data=WireguardConfigurations[name]) +@app.get(f'{APP_PREFIX}/api/getWireguardConfigurationBackup') +def API_getWireguardConfigurationBackup(): + configurationName = request.args.get('configurationName') + if configurationName is None or configurationName not in WireguardConfigurations.keys(): + return ResponseObject(False, "Configuration does not exist") + return ResponseObject(data=WireguardConfigurations[configurationName].getBackups()) + @app.get(f'{APP_PREFIX}/api/getDashboardConfiguration') def API_getDashboardConfiguration(): return ResponseObject(data=DashboardConfig.toJson()) diff --git a/src/static/app/src/components/configurationComponents/editConfiguration.vue b/src/static/app/src/components/configurationComponents/editConfiguration.vue index bc3f64c..ac8d1b9 100644 --- a/src/static/app/src/components/configurationComponents/editConfiguration.vue +++ b/src/static/app/src/components/configurationComponents/editConfiguration.vue @@ -5,6 +5,8 @@ import {WireguardConfigurationsStore} from "@/stores/WireguardConfigurationsStor 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 }) @@ -177,13 +179,17 @@ watch(data, () => { :disabled="!dataChanged || saving"> - +
+
+ +
+ diff --git a/src/static/app/src/components/configurationComponents/editConfigurationComponents/configurationBackupRestore.vue b/src/static/app/src/components/configurationComponents/editConfigurationComponents/configurationBackupRestore.vue new file mode 100644 index 0000000..f743e70 --- /dev/null +++ b/src/static/app/src/components/configurationComponents/editConfigurationComponents/configurationBackupRestore.vue @@ -0,0 +1,61 @@ + + + + + \ 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 b3523e4..dead251 100644 --- a/src/static/app/src/components/configurationComponents/peerList.vue +++ b/src/static/app/src/components/configurationComponents/peerList.vue @@ -145,10 +145,10 @@ export default { selectedPeer: undefined }, editConfiguration: { - modalOpen: false + modalOpen: true }, selectPeers: { - modalOpen: true + modalOpen: false } } }, diff --git a/src/static/app/src/components/configurationComponents/peerSettings.vue b/src/static/app/src/components/configurationComponents/peerSettings.vue index 951baba..4d022e8 100644 --- a/src/static/app/src/components/configurationComponents/peerSettings.vue +++ b/src/static/app/src/components/configurationComponents/peerSettings.vue @@ -231,19 +231,17 @@ export default {
- - +
diff --git a/src/static/app/src/components/configurationComponents/selectPeers.vue b/src/static/app/src/components/configurationComponents/selectPeers.vue index e8b207c..154179c 100644 --- a/src/static/app/src/components/configurationComponents/selectPeers.vue +++ b/src/static/app/src/components/configurationComponents/selectPeers.vue @@ -66,9 +66,7 @@ const downloaded = reactive({ failed: [] }) const cardBody = useTemplateRef('card-body'); -const sleep = m => new Promise(resolve => setTimeout(resolve, m)) const el = useTemplateRef("sp") -console.log(el.value) const submitDownload = async () => { downloadConfirmation.value = true for (const x of selectedPeers.value) { diff --git a/src/static/app/src/components/navbar.vue b/src/static/app/src/components/navbar.vue index 052184e..225a733 100644 --- a/src/static/app/src/components/navbar.vue +++ b/src/static/app/src/components/navbar.vue @@ -66,10 +66,10 @@ export default {