Fix handling default settings

If a setting has ben set to empty string, dict.get will return it and
not default argument. This is wrong when default is something else.
This commit is contained in:
Timotej Lazar 2024-04-30 09:38:14 +02:00
parent f8d71b7b06
commit cac7658566
2 changed files with 5 additions and 5 deletions

View file

@ -90,7 +90,7 @@ def save_config():
# config files, then increment version before unlocking.
with db.locked():
settings = db.read('settings')
version = settings['version'] = int(settings.get('version', 0)) + 1
version = settings['version'] = int(settings.get('version') or '0') + 1
# Populate IP sets.
ipsets = collections.defaultdict(set)
@ -155,7 +155,7 @@ def save_config():
# Print wireguard config.
with open(output / 'etc/wireguard/wg.conf', 'w', encoding='utf-8') as f:
f.write(wg_intf.format(
port=settings.get('wg_port', 51820),
port=settings.get('wg_port') or 51820,
key=settings.get('wg_key')))
for ip, data in wireguard.items():
f.write(wg_peer.format(

View file

@ -50,7 +50,7 @@ def new():
server_pubkey = subprocess.run([f'wg pubkey'], input=settings.get('wg_key'),
text=True, capture_output=True, shell=True).stdout.strip()
host = ipaddress.ip_interface(settings.get('wg_net', '10.0.0.1/24'))
host = ipaddress.ip_interface(settings.get('wg_net') or '10.0.0.1/24')
ip6 = None
with db.locked():
# Find a free address for the new key.
@ -80,7 +80,7 @@ def new():
# Template arguments.
args = {
'server': settings.get('wg_endpoint'),
'port': settings.get('wg_port', '51820'),
'port': settings.get('wg_port') or '51820',
'server_key': server_pubkey,
'pubkey': pubkey,
'ip': str(ip),
@ -88,7 +88,7 @@ def new():
'timestamp': now,
'name': name,
'dns': settings.get('wg_dns') if flask.request.json.get('use_dns', True) else False,
'allowed_nets': settings.get('wg_allowed_nets', []),
'allowed_nets': settings.get('wg_allowed_nets', ''),
'add_default': flask.request.json.get('add_default', False),
}
return flask.render_template('vpn/wg-fri.conf', **args)