summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xwg.py27
1 files changed, 15 insertions, 12 deletions
diff --git a/wg.py b/wg.py
index 24c2b60..604f0c0 100755
--- a/wg.py
+++ b/wg.py
@@ -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)):