This commit is contained in:
chapeau 2024-08-02 16:21:58 +02:00
parent 6dbbed1c7a
commit c177ea56ea
2 changed files with 9 additions and 10 deletions

View file

@ -6,16 +6,15 @@ from dataclasses import dataclass, InitVar
class Peer:
public_key: str
endpoint: str | None
allowed_ips: str | None = None
allowed_ips: list = []
untranslated_networks: InitVar(str | None) = None
local_translated_range: InitVar(str)
wireguard_address: InitVar(str)
def __post_init__(self):
allowed = [self.local_translated_range, wireguard_address]
self.allowed_ips = [self.local_translated_range, wireguard_address]
if untranslated_networks != None:
allowed.append(untranslated_networks)
self.allowed_ips = ", ".join(allowed)
self.allowed_ips.append(untranslated_networks)
@dataclass
class Network:
@ -58,7 +57,7 @@ class Config:
default_dns: DNSServer | None = None
dns_servers: dict = {}
remote_networks: dict = {}
peers: array = []
peers: list = []
local_wireguard_address: str = ""
def __post_init__(self, default_dns_address, default_dns_port, data):
@ -106,20 +105,20 @@ class Config:
)
def dns_server(self, qname):
# Guess which DNS server call from the requested domain name
"""Guess which DNS server call from the requested domain name"""
for dns in self.dns_servers.values():
if dns.is_same_zone(qname):
return dns
return self.default_dns
def translate(self, ip, network):
# Translate if required given ip from given network
"""Translate if required given ip from given network"""
if IPv4Address(ip) in self.networks[network].local_range:
return self.networks[network].translation_dict[ip]
return ip
def untranslate(self, ip, network):
# Give back the original ip from a translated one from given network
"""Give back the original ip from a translated one from given network"""
if IPv4Address(ip) in self.networks[network].local_translated_range:
return self.networks[network].untranslation_dict[ip]
return ip

View file

@ -9,6 +9,6 @@ PublicKey = {{ peer.public_key }}
{%- if peer.endpoint is defined %}
Endpoint = {{ peer.endpoint }}
{%- endif %}
AllowedIPs = {{ peer.allowed_ips}}
AllowedIPs = {{ peer.allowed_ips | join(', ') }}
PersistentKeepalive = 25
{% endfor %}
{% endfor %}