From b6aedb43eeaa1139459f3c8838b24a1b827a3757 Mon Sep 17 00:00:00 2001 From: Donald Zou Date: Fri, 6 Sep 2024 16:31:54 +0800 Subject: [PATCH] Fixed updating WG configuration path --- src/dashboard.py | 60 +++++++++++-------- .../accountSettingsInputPassword.vue | 9 ++- ...ettingsInputWireguardConfigurationPath.vue | 40 +++++++++---- 3 files changed, 72 insertions(+), 37 deletions(-) diff --git a/src/dashboard.py b/src/dashboard.py index a93bc13..94b23b1 100644 --- a/src/dashboard.py +++ b/src/dashboard.py @@ -462,11 +462,11 @@ class WireguardConfiguration: if name is not None: self.Name = name - self.__parser.read_file(open(os.path.join(WG_CONF_PATH, f'{self.Name}.conf'))) + 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(WG_CONF_PATH, f'{self.Name}.conf')) + "[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(): @@ -583,7 +583,7 @@ class WireguardConfiguration: self.RestrictedPeers.append(Peer(i, self)) def configurationFileChanged(self) : - mt = os.path.getmtime(os.path.join(WG_CONF_PATH, f'{self.Name}.conf')) + mt = os.path.getmtime(os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], f'{self.Name}.conf')) changed = self.__configFileModifiedTime is None or self.__configFileModifiedTime != mt self.__configFileModifiedTime = mt return changed @@ -592,7 +592,7 @@ class WireguardConfiguration: if self.configurationFileChanged(): self.Peers = [] - with open(os.path.join(WG_CONF_PATH, f'{self.Name}.conf'), 'r') as configFile: + with open(os.path.join(DashboardConfig.GetConfig("Server", "wg_conf_path")[1], f'{self.Name}.conf'), 'r') as configFile: p = [] pCounter = -1 content = configFile.read().split('\n') @@ -1210,6 +1210,10 @@ class DashboardConfig: else: value = self.generatePassword(value).decode("utf-8") + if section == "Server" and key == "wg_conf_path": + if not os.path.exists(value): + return False, "Path does not exist" + if section not in self.__config: self.__config[section] = {} @@ -1232,7 +1236,7 @@ class DashboardConfig: except Exception as e: return False - def GetConfig(self, section, key) -> [any, bool]: + def GetConfig(self, section, key) -> [bool, any]: if section not in self.__config: return False, None @@ -1279,7 +1283,8 @@ def _regexMatch(regex, text): def _getConfigurationList(): # configurations = {} - for i in os.listdir(WG_CONF_PATH): + print(DashboardConfig.GetConfig("Server", "wg_conf_path")[1]) + for i in os.listdir(DashboardConfig.GetConfig("Server", "wg_conf_path")[1]): if _regexMatch("^(.{1,}).(conf)$", i): i = i.replace('.conf', '') try: @@ -1400,8 +1405,11 @@ cursor = sqldb.cursor() def sqlSelect(statement: str, paramters: tuple = ()) -> sqlite3.Cursor: with sqldb: - cursor = sqldb.cursor() - return cursor.execute(statement, paramters) + try: + cursor = sqldb.cursor() + return cursor.execute(statement, paramters) + except sqlite3.OperationalError as error: + print("[WGDashboard] SQLite Error:" + str(error) + " | Statement: " + statement) def sqlUpdate(statement: str, paramters: tuple = ()) -> sqlite3.Cursor: with sqldb: @@ -1602,14 +1610,7 @@ def API_getDashboardConfiguration(): return ResponseObject(data=DashboardConfig.toJson()) -# @app.route(f'{APP_PREFIX}/api/updateDashboardConfiguration', methods=["POST"]) -# def API_updateDashboardConfiguration(): -# data = request.get_json() -# for section in data['DashboardConfiguration'].keys(): -# for key in data['DashboardConfiguration'][section].keys(): -# if not DashboardConfig.SetConfig(section, key, data['DashboardConfiguration'][section][key])[0]: -# return ResponseObject(False, "Section or value is invalid.") -# return ResponseObject() + @app.route(f'{APP_PREFIX}/api/updateDashboardConfigurationItem', methods=["POST"]) @@ -1617,13 +1618,16 @@ def API_updateDashboardConfigurationItem(): data = request.get_json() if "section" not in data.keys() or "key" not in data.keys() or "value" not in data.keys(): return ResponseObject(False, "Invalid request.") - valid, msg = DashboardConfig.SetConfig( data["section"], data["key"], data['value']) - if not valid: return ResponseObject(False, msg) - + + if data['section'] == "Server": + if data['key'] == 'wg_conf_path': + WireguardConfigurations.clear() + _getConfigurationList() + return ResponseObject() @app.route(f'{APP_PREFIX}/api/getDashboardAPIKeys', methods=['GET']) @@ -2141,10 +2145,13 @@ def index(): def backGroundThread(): - with app.app_context(): - print(f"[WGDashboard] Background Thread #1 Started", flush=True) - time.sleep(10) - while True: + global WireguardConfigurations + print(f"[WGDashboard] Background Thread #1 Started", flush=True) + time.sleep(10) + while True: + with app.app_context(): + print(DashboardConfig.GetConfig("Server", "wg_conf_path")[1]) + print(id(WireguardConfigurations)) for c in WireguardConfigurations.values(): if c.getStatus(): try: @@ -2155,7 +2162,7 @@ def backGroundThread(): c.getRestrictedPeersList() except Exception as e: print(f"[WGDashboard] Background Thread #1 Error: {str(e)}", flush=True) - time.sleep(10) + time.sleep(10) def peerJobScheduleBackgroundThread(): @@ -2171,6 +2178,11 @@ def gunicornConfig(): _, app_port = DashboardConfig.GetConfig("Server", "app_port") return app_ip, app_port +def clearWireguardConfigurations(): + WireguardConfigurations = {} + WireguardConfigurations.clear() + print(WireguardConfigurations.keys()) + AllPeerShareLinks: PeerShareLinks = PeerShareLinks() AllPeerJobs: PeerJobs = PeerJobs() diff --git a/src/static/app/src/components/settingsComponent/accountSettingsInputPassword.vue b/src/static/app/src/components/settingsComponent/accountSettingsInputPassword.vue index 53aa4f1..7b43f4b 100644 --- a/src/static/app/src/components/settingsComponent/accountSettingsInputPassword.vue +++ b/src/static/app/src/components/settingsComponent/accountSettingsInputPassword.vue @@ -66,6 +66,11 @@ export default { this.invalidFeedback = "Please fill in all required fields." } } + }, + computed: { + passwordValid(){ + return Object.values(this.value).find(x => x.length === 0) === undefined && this.value.newPassword === this.value.repeatNewPassword + } } } @@ -109,7 +114,9 @@ export default { - diff --git a/src/static/app/src/components/settingsComponent/dashboardSettingsInputWireguardConfigurationPath.vue b/src/static/app/src/components/settingsComponent/dashboardSettingsInputWireguardConfigurationPath.vue index c5ea3e0..423f641 100644 --- a/src/static/app/src/components/settingsComponent/dashboardSettingsInputWireguardConfigurationPath.vue +++ b/src/static/app/src/components/settingsComponent/dashboardSettingsInputWireguardConfigurationPath.vue @@ -2,6 +2,7 @@ import {DashboardConfigurationStore} from "@/stores/DashboardConfigurationStore.js"; import {v4} from "uuid"; import {fetchPost} from "@/utilities/fetch.js"; +import {WireguardConfigurationsStore} from "@/stores/WireguardConfigurationsStore.js"; export default { name: "dashboardSettingsInputWireguardConfigurationPath", @@ -13,8 +14,9 @@ export default { }, setup(){ const store = DashboardConfigurationStore(); + const WireguardConfigurationStore = WireguardConfigurationsStore() const uuid = `input_${v4()}`; - return {store, uuid}; + return {store, uuid, WireguardConfigurationStore}; }, data(){ return{ @@ -33,6 +35,7 @@ export default { methods:{ async useValidation(){ if(this.changed){ + this.updating = true; await fetchPost("/api/updateDashboardConfigurationItem", { section: "Server", key: this.targetData, @@ -44,6 +47,8 @@ export default { this.store.Configuration.Account[this.targetData] = this.value clearTimeout(this.timeout) this.timeout = setTimeout(() => this.isValid = false, 5000); + this.WireguardConfigurationStore.getConfigurations() + this.store.newMessage("Server", "WireGuard configuration path saved", "success") }else{ this.isValid = false; this.showInvalidFeedback = true; @@ -59,24 +64,35 @@ export default {