refactor #2

Open
chapoline wants to merge 2 commits from refactor into main
2 changed files with 9 additions and 10 deletions
Showing only changes of commit c177ea56ea - Show all commits

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)
faercol marked this conversation as resolved
Review

Oui voilà, ça je l'aurais fait dans le template, mais j'aurais gardé la liste pour le stockage dans l'objet

Oui voilà, ça je l'aurais fait dans le template, mais j'aurais gardé la liste pour le stockage dans l'objet
@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 = ""
faercol marked this conversation as resolved
Review

je crois que c'est list plutot que array ici

je crois que c'est `list` plutot que `array` ici
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():
faercol marked this conversation as resolved
Review

Si tu veux faire une docstring pour la fonction (qui du coup est visible sur ton IDE ou avec un help, tu dois utiliser directement une string et non un commentaire.

Du coup ici

    def dns_server(self, qname):
        "Guess which DNS server to call from the requested domain name"
        ...
Si tu veux faire une docstring pour la fonction (qui du coup est visible sur ton IDE ou avec un `help`, tu dois utiliser directement une string et non un commentaire. Du coup ici ```python def dns_server(self, qname): "Guess which DNS server to call from the requested domain name" ... ```
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 %}