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 95f0b60cac
commit 14af465aa3
3 changed files with 149 additions and 45 deletions

View File

@ -668,14 +668,25 @@ def API_addPeers(configName):
endpoint_allowed_ip: str = data.get('endpoint_allowed_ip', DashboardConfig.GetConfig("Peers", "peer_endpoint_allowed_ip")[1])
dns_addresses: str = data.get('DNS', DashboardConfig.GetConfig("Peers", "peer_global_DNS")[1])
mtu: int = data.get('mtu', int(DashboardConfig.GetConfig("Peers", "peer_MTU")[1]))
keep_alive: int = data.get('keepalive', int(DashboardConfig.GetConfig("Peers", "peer_keep_alive")[1]))
mtu: int = data.get('mtu', None)
keep_alive: int = data.get('keepalive', None)
preshared_key: str = data.get('preshared_key', "")
if type(mtu) is not int or mtu < 0 or mtu > 1460:
mtu = int(DashboardConfig.GetConfig("Peers", "peer_MTU")[1])
default: str = DashboardConfig.GetConfig("Peers", "peer_MTU")[1]
if default.isnumeric():
mtu = default
else:
mtu = 0
if type(keep_alive) is not int or keep_alive < 0:
keep_alive = int(DashboardConfig.GetConfig("Peers", "peer_keep_alive")[1])
default = DashboardConfig.GetConfig("Peers", "peer_keep_alive")[1]
if default.isnumeric():
keep_alive = default
else:
keep_alive = 0
config = WireguardConfigurations.get(configName)
if not config.getStatus():
config.toggleConfiguration()
@ -1245,6 +1256,20 @@ def API_Clients_AssignedClients():
return ResponseObject(False, "Please provide all required fields")
return ResponseObject(
data=DashboardClients.GetAssignedPeerClients(configurationName, peerID))
@app.get(f'{APP_PREFIX}/api/clients/allConfigurationsPeers')
def API_Clients_AllConfigurationsPeers():
c = {}
for (key, val) in WireguardConfigurations.items():
c[key] = list(map(lambda x : {
"id": x.id,
"name": x.name
}, val.Peers))
return ResponseObject(
data=c
)

View File

@ -30,31 +30,64 @@ class AmneziaWGPeer(Peer):
if re.match("^[a-zA-Z0-9_=+.-]$", i):
finalFilename += i
peerConfiguration = f'''[Interface]
PrivateKey = {self.private_key}
Address = {self.allowed_ip}
MTU = {str(self.mtu)}
Jc = {self.configuration.Jc}
Jmin = {self.configuration.Jmin}
Jmax = {self.configuration.Jmax}
S1 = {self.configuration.S1}
S2 = {self.configuration.S2}
H1 = {self.configuration.H1}
H2 = {self.configuration.H2}
H3 = {self.configuration.H3}
H4 = {self.configuration.H4}
'''
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"
interfaceSection = {
"PrivateKey": self.private_key,
"Address": self.allowed_ip,
"MTU": self.mtu,
"DNS": self.DNS,
"Jc": self.configuration.Jc,
"Jmin": self.configuration.Jmin,
"Jmax": self.configuration.Jmax,
"S1": self.configuration.S1,
"S2": self.configuration.S2,
"H1": self.configuration.H1,
"H2": self.configuration.H2,
"H3": self.configuration.H3,
"H4": self.configuration.H4
}
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)}
# Jc = {self.configuration.Jc}
# Jmin = {self.configuration.Jmin}
# Jmax = {self.configuration.Jmax}
# S1 = {self.configuration.S1}
# S2 = {self.configuration.S2}
# H1 = {self.configuration.H1}
# H2 = {self.configuration.H2}
# H3 = {self.configuration.H3}
# H4 = {self.configuration.H4}
# '''
# 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
@ -78,6 +111,13 @@ PersistentKeepalive = {str(self.keepalive)}
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 type(keepalive) is str:
keepalive = 0
if mtu < 0 or mtu > 1460:
return False, "MTU format is not correct"
if keepalive < 0:

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