From 2533cb53cd5c590a1f6558f0356a2c42c113d155 Mon Sep 17 00:00:00 2001 From: INODE64 Date: Wed, 3 Jan 2024 17:12:20 +0100 Subject: [PATCH 1/2] Implement more checks to avoid repeating addresses or endpoints --- wg_meshconf/database_manager.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/wg_meshconf/database_manager.py b/wg_meshconf/database_manager.py index bc36de4..df4c754 100755 --- a/wg_meshconf/database_manager.py +++ b/wg_meshconf/database_manager.py @@ -210,6 +210,23 @@ def addpeer( if locals().get(key) is not None: database["peers"][Name][key] = locals().get(key) + for peer in database["peers"]: + for peer2 in database["peers"]: + if peer != peer2: + # Check if Address is unique + if database["peers"][peer]["Address"] == databasse["peers"][peer2]["Address"]: + print(f"Address {database['peers'][peer]['Address']} already exists") + return + # Check if PrivateKey is unique + if database["peers"][peer]["PrivateKey"] == database["peers"][peer2]["PrivateKey"]: + print(f"PrivateKey {database['peers'][peer]['PrivateKey']} already exists") + return + # Check if Endpoint and ListenPort are unique + if database["peers"][peer]["Endpoint"] == database["peers"][peer2]["Endpoint"] \ + and database["peers"][peer]["ListenPort"] == database["peers"][peer2]["ListenPort"]: + print(f"Endpoint {database['peers'][peer]['Endpoint']} and ListenPort {database['peers'][peer]['ListenPort']} already exists") + return + self.write_database(database) def updatepeer( From ae450dc53262d128763f7cfd47316c0a4b1efd83 Mon Sep 17 00:00:00 2001 From: INODE64 Date: Mon, 8 Jan 2024 13:18:35 +0100 Subject: [PATCH 2/2] solve problem when endpoint is not defined --- wg_meshconf/database_manager.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/wg_meshconf/database_manager.py b/wg_meshconf/database_manager.py index df4c754..b55b7c9 100755 --- a/wg_meshconf/database_manager.py +++ b/wg_meshconf/database_manager.py @@ -214,15 +214,16 @@ def addpeer( for peer2 in database["peers"]: if peer != peer2: # Check if Address is unique - if database["peers"][peer]["Address"] == databasse["peers"][peer2]["Address"]: + if database["peers"][peer]["Address"] == database["peers"][peer2]["Address"]: print(f"Address {database['peers'][peer]['Address']} already exists") return # Check if PrivateKey is unique if database["peers"][peer]["PrivateKey"] == database["peers"][peer2]["PrivateKey"]: print(f"PrivateKey {database['peers'][peer]['PrivateKey']} already exists") return - # Check if Endpoint and ListenPort are unique - if database["peers"][peer]["Endpoint"] == database["peers"][peer2]["Endpoint"] \ + # Check if Endpoint and ListenPort are unique and exist + if "Endpoint" in database["peers"][peer] and "Endpoint" in database["peers"][peer2] \ + and database["peers"][peer]["Endpoint"] == database["peers"][peer2]["Endpoint"] \ and database["peers"][peer]["ListenPort"] == database["peers"][peer2]["ListenPort"]: print(f"Endpoint {database['peers'][peer]['Endpoint']} and ListenPort {database['peers'][peer]['ListenPort']} already exists") return