Implemented adding of routes for all clients on startup

This commit is contained in:
Erik Thuning 2025-03-26 15:55:13 +01:00
parent 670c88c7f3
commit 184d914107

@ -180,8 +180,8 @@ class WireGuard:
self.user_base = None
# Ensure a wg config exists on startup
self.wg_updated = True
self.update()
self.update(force=True)
self.init_routes()
def log(self, context_id: str, message) -> None:
@ -229,6 +229,14 @@ class WireGuard:
def meta_filepath(self, config_id: str) -> Path:
return self.filepath(f'{config_id}{metasuffix}')
def init_routes(self) -> None:
config = Configparser()
for conffile in self.configs_base.glob('*/*'+serversuffix):
config.read(conffile)
client_ip_str = config['peer']['allowedips']
client_ip = ipaddress(client_ip_str.split('/')[0])
create_route(client_ip)
def generate_server_config(self):
config = ConfigParser(interpolation=None)
config['Interface'] = {
@ -395,8 +403,8 @@ class WireGuard:
# Delete the expired configs in a separate step to minimize lock time
self.delete_many_configs(expired)
def update(self) -> None:
if not self.wg_updated:
def update(self, force: bool = False) -> None:
if not force and not self.wg_updated:
return
with open(self.server_config_file, 'w') as sf:
sf.write(self.generate_server_config())