diff options
| author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2025-12-06 00:35:32 -0800 |
|---|---|---|
| committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2025-12-06 00:35:32 -0800 |
| commit | 6c88cfefdb8ac6be292210b3d3b076ad53d74ba2 (patch) | |
| tree | 75ce619e9b09d31d3446adc6f83255c895776cae | |
| parent | dc0b6a8ae98f8d830bc2a3ff385e3ff256a0a7f9 (diff) | |
| download | wg-genconf-6c88cfefdb8ac6be292210b3d3b076ad53d74ba2.tar.gz wg-genconf-6c88cfefdb8ac6be292210b3d3b076ad53d74ba2.tar.xz | |
| -rwxr-xr-x | wg.py | 27 |
1 files changed, 15 insertions, 12 deletions
@@ -2,6 +2,7 @@ # # Copyright 2025 David Vazgenovich Shakaryan +import collections import copy import io import ipaddress @@ -86,6 +87,7 @@ def gc_if_wgquick_add_peer(buf, net_name, peerspec): buf.write(f'Endpoint = {host}:{port}\n') def gc_if_wgquick(local, net_name, if_conf): + cm = collections.ChainMap(if_conf, local) buf = io.StringIO() privkey = config['privkey'].get(net_name, 'FIXME') @@ -95,10 +97,9 @@ def gc_if_wgquick(local, net_name, if_conf): for addr in ipspecs_to_ips( net_name, if_conf.get('ips', ['{peer/-}']), local, interface=True): buf.write(f'Address = {addr}\n') - if (port := if_conf.get('port')) or (port := local.get('port')): - if port != 'auto': - buf.write(f'ListenPort = {port}\n') - if (fwmark := local.get('fwmark')): + if (port := cm.get('port')) != 'auto': + buf.write(f'ListenPort = {port}\n') + if (fwmark := cm.get('fwmark')): buf.write(f'FwMark = {fwmark}\n') for peerspec in if_conf['peers']: @@ -140,6 +141,7 @@ def gc_if_systemd_netdev_add_peer(buf, net_name, peerspec): buf.write(f'Endpoint={host}:{port}\n') def gc_if_systemd_netdev(local, net_name, if_conf, netif_name): + cm = collections.ChainMap(if_conf, local) buf = io.StringIO() privkey = config['privkey'].get(net_name, 'FIXME') @@ -151,10 +153,9 @@ def gc_if_systemd_netdev(local, net_name, if_conf, netif_name): '\n' '[WireGuard]\n' f'PrivateKey={privkey}\n') - if (port := if_conf.get('port')) or (port := local.get('port')): - if port != 'auto': - buf.write(f'ListenPort={port}\n') - if (fwmark := local.get('fwmark')): + if (port := cm.get('port')) != 'auto': + buf.write(f'ListenPort={port}\n') + if (fwmark := cm.get('fwmark')): buf.write(f'FirewallMark={fwmark}\n') for peerspec in if_conf['peers']: @@ -176,22 +177,24 @@ def buf_to_file(buf, path, mode=None): shutil.copyfileobj(buf, f) def create_if_files(local, net_name, if_name, if_conf): - netif_name = f'{local.get('prefix', '')}{net_name}' + cm = collections.ChainMap(if_conf, local) + netif_name = f'{cm.get('prefix', '')}{net_name}' if if_name: netif_name += f'-{if_name}' + file_prefix = f'out/{cm.get('file-prefix', '')}' if if_conf.get('type') == 'systemd': buf_to_file( gc_if_systemd_netdev(local, net_name, if_conf, netif_name), - f'out/{netif_name}.netdev', + f'{file_prefix}{netif_name}.netdev', mode=0o640) buf_to_file( gc_if_systemd_network(local, net_name, netif_name), - f'out/{netif_name}.network') + f'{file_prefix}{netif_name}.network') else: buf_to_file( gc_if_wgquick(local, net_name, if_conf), - f'out/{netif_name}.conf') + f'{file_prefix}{netif_name}.conf') def peer_conf(net_name, peer_name): if not (peer := config['peer'].get(peer_name)): |
