Fixed MTU and KeepAlive can be empty, rewrote config builder

This commit is contained in:
Donald Zou
2025-07-20 20:52:37 +08:00
parent 327ecbe34c
commit 12f6244930
3 changed files with 149 additions and 45 deletions

View File

@@ -60,8 +60,16 @@ class Peer:
return False, f"Endpoint Allowed IPs format is incorrect"
if len(dns_addresses) > 0 and not ValidateDNSAddress(dns_addresses):
return False, f"DNS format is incorrect"
if type(mtu) is str:
mtu = 0
if mtu < 0 or mtu > 1460:
return False, "MTU format is not correct"
if type(keepalive) is str:
keepalive = 0
if keepalive < 0:
return False, "Persistent Keepalive format is not correct"
if len(private_key) > 0:
@@ -122,24 +130,55 @@ class Peer:
for i in filename:
if re.match("^[a-zA-Z0-9_=+.-]$", i):
finalFilename += i
interfaceSection = {
"PrivateKey": self.private_key,
"Address": self.allowed_ip,
"MTU": self.mtu,
"DNS": self.DNS
}
peerSection = {
"PublicKey": self.configuration.PublicKey,
"AllowedIPs": self.endpoint_allowed_ip,
"Endpoint": f'{self.configuration.DashboardConfig.GetConfig("Peers", "remote_endpoint")[1]}:{self.configuration.ListenPort}',
"PersistentKeepalive": self.keepalive,
"PresharedKey": self.preshared_key
}
combine = [interfaceSection.items(), peerSection.items()]
peerConfiguration = ""
for s in range(len(combine)):
if s == 0:
peerConfiguration += "[Interface]\n"
else:
peerConfiguration += "\n[Peer]\n"
for (key, val) in combine[s]:
if val is not None and ((type(val) is str and len(val) > 0) or (type(val) is int and val > 0)):
peerConfiguration += f"{key} = {val}\n"
peerConfiguration = f'''[Interface]
PrivateKey = {self.private_key}
Address = {self.allowed_ip}
MTU = {str(self.mtu)}
'''
if len(self.DNS) > 0:
peerConfiguration += f"DNS = {self.DNS}\n"
# for (key, val) in interfaceSection.items():
# if val is not None and ((type(val) is str and len(val) > 0) or type(val) is int):
# peerConfiguration += f"{key} = {val}\n"
# peerConfiguration = "\n[Peer]\n"
peerConfiguration += f'''
[Peer]
PublicKey = {self.configuration.PublicKey}
AllowedIPs = {self.endpoint_allowed_ip}
Endpoint = {self.configuration.DashboardConfig.GetConfig("Peers", "remote_endpoint")[1]}:{self.configuration.ListenPort}
PersistentKeepalive = {str(self.keepalive)}
'''
if len(self.preshared_key) > 0:
peerConfiguration += f"PresharedKey = {self.preshared_key}\n"
# peerConfiguration = f'''[Interface]
# PrivateKey = {self.private_key}
# Address = {self.allowed_ip}
# MTU = {str(self.mtu)}
# '''
# if len(self.DNS) > 0:
# peerConfiguration += f"DNS = {self.DNS}\n"
#
# peerConfiguration += f'''
# [Peer]
# PublicKey = {self.configuration.PublicKey}
# AllowedIPs = {self.endpoint_allowed_ip}
# Endpoint = {self.configuration.DashboardConfig.GetConfig("Peers", "remote_endpoint")[1]}:{self.configuration.ListenPort}
# PersistentKeepalive = {str(self.keepalive)}
# '''
# if len(self.preshared_key) > 0:
# peerConfiguration += f"PresharedKey = {self.preshared_key}\n"
return {
"fileName": finalFilename,
"file": peerConfiguration