mirror of
https://github.com/donaldzou/WGDashboard.git
synced 2025-10-03 07:46:18 +00:00
Fixed MTU and KeepAlive can be empty, rewrote config builder
This commit is contained in:
@@ -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])
|
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])
|
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', "")
|
preshared_key: str = data.get('preshared_key', "")
|
||||||
|
|
||||||
if type(mtu) is not int or mtu < 0 or mtu > 1460:
|
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:
|
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)
|
config = WireguardConfigurations.get(configName)
|
||||||
if not config.getStatus():
|
if not config.getStatus():
|
||||||
config.toggleConfiguration()
|
config.toggleConfiguration()
|
||||||
@@ -1245,6 +1256,20 @@ def API_Clients_AssignedClients():
|
|||||||
return ResponseObject(False, "Please provide all required fields")
|
return ResponseObject(False, "Please provide all required fields")
|
||||||
return ResponseObject(
|
return ResponseObject(
|
||||||
data=DashboardClients.GetAssignedPeerClients(configurationName, peerID))
|
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
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@@ -30,31 +30,64 @@ class AmneziaWGPeer(Peer):
|
|||||||
if re.match("^[a-zA-Z0-9_=+.-]$", i):
|
if re.match("^[a-zA-Z0-9_=+.-]$", i):
|
||||||
finalFilename += i
|
finalFilename += i
|
||||||
|
|
||||||
peerConfiguration = f'''[Interface]
|
interfaceSection = {
|
||||||
PrivateKey = {self.private_key}
|
"PrivateKey": self.private_key,
|
||||||
Address = {self.allowed_ip}
|
"Address": self.allowed_ip,
|
||||||
MTU = {str(self.mtu)}
|
"MTU": self.mtu,
|
||||||
Jc = {self.configuration.Jc}
|
"DNS": self.DNS,
|
||||||
Jmin = {self.configuration.Jmin}
|
"Jc": self.configuration.Jc,
|
||||||
Jmax = {self.configuration.Jmax}
|
"Jmin": self.configuration.Jmin,
|
||||||
S1 = {self.configuration.S1}
|
"Jmax": self.configuration.Jmax,
|
||||||
S2 = {self.configuration.S2}
|
"S1": self.configuration.S1,
|
||||||
H1 = {self.configuration.H1}
|
"S2": self.configuration.S2,
|
||||||
H2 = {self.configuration.H2}
|
"H1": self.configuration.H1,
|
||||||
H3 = {self.configuration.H3}
|
"H2": self.configuration.H2,
|
||||||
H4 = {self.configuration.H4}
|
"H3": self.configuration.H3,
|
||||||
'''
|
"H4": self.configuration.H4
|
||||||
if len(self.DNS) > 0:
|
}
|
||||||
peerConfiguration += f"DNS = {self.DNS}\n"
|
peerSection = {
|
||||||
peerConfiguration += f'''
|
"PublicKey": self.configuration.PublicKey,
|
||||||
[Peer]
|
"AllowedIPs": self.endpoint_allowed_ip,
|
||||||
PublicKey = {self.configuration.PublicKey}
|
"Endpoint": f'{self.configuration.DashboardConfig.GetConfig("Peers", "remote_endpoint")[1]}:{self.configuration.ListenPort}',
|
||||||
AllowedIPs = {self.endpoint_allowed_ip}
|
"PersistentKeepalive": self.keepalive,
|
||||||
Endpoint = {self.configuration.DashboardConfig.GetConfig("Peers", "remote_endpoint")[1]}:{self.configuration.ListenPort}
|
"PresharedKey": self.preshared_key
|
||||||
PersistentKeepalive = {str(self.keepalive)}
|
}
|
||||||
'''
|
combine = [interfaceSection.items(), peerSection.items()]
|
||||||
if len(self.preshared_key) > 0:
|
peerConfiguration = ""
|
||||||
peerConfiguration += f"PresharedKey = {self.preshared_key}\n"
|
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 {
|
return {
|
||||||
"fileName": finalFilename,
|
"fileName": finalFilename,
|
||||||
"file": peerConfiguration
|
"file": peerConfiguration
|
||||||
@@ -78,6 +111,13 @@ PersistentKeepalive = {str(self.keepalive)}
|
|||||||
return False, f"Endpoint Allowed IPs format is incorrect"
|
return False, f"Endpoint Allowed IPs format is incorrect"
|
||||||
if len(dns_addresses) > 0 and not ValidateDNSAddress(dns_addresses):
|
if len(dns_addresses) > 0 and not ValidateDNSAddress(dns_addresses):
|
||||||
return False, f"DNS format is incorrect"
|
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:
|
if mtu < 0 or mtu > 1460:
|
||||||
return False, "MTU format is not correct"
|
return False, "MTU format is not correct"
|
||||||
if keepalive < 0:
|
if keepalive < 0:
|
||||||
|
@@ -60,8 +60,16 @@ class Peer:
|
|||||||
return False, f"Endpoint Allowed IPs format is incorrect"
|
return False, f"Endpoint Allowed IPs format is incorrect"
|
||||||
if len(dns_addresses) > 0 and not ValidateDNSAddress(dns_addresses):
|
if len(dns_addresses) > 0 and not ValidateDNSAddress(dns_addresses):
|
||||||
return False, f"DNS format is incorrect"
|
return False, f"DNS format is incorrect"
|
||||||
|
|
||||||
|
if type(mtu) is str:
|
||||||
|
mtu = 0
|
||||||
|
|
||||||
if mtu < 0 or mtu > 1460:
|
if mtu < 0 or mtu > 1460:
|
||||||
return False, "MTU format is not correct"
|
return False, "MTU format is not correct"
|
||||||
|
|
||||||
|
if type(keepalive) is str:
|
||||||
|
keepalive = 0
|
||||||
|
|
||||||
if keepalive < 0:
|
if keepalive < 0:
|
||||||
return False, "Persistent Keepalive format is not correct"
|
return False, "Persistent Keepalive format is not correct"
|
||||||
if len(private_key) > 0:
|
if len(private_key) > 0:
|
||||||
@@ -122,24 +130,55 @@ class Peer:
|
|||||||
for i in filename:
|
for i in filename:
|
||||||
if re.match("^[a-zA-Z0-9_=+.-]$", i):
|
if re.match("^[a-zA-Z0-9_=+.-]$", i):
|
||||||
finalFilename += 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}
|
# for (key, val) in interfaceSection.items():
|
||||||
Address = {self.allowed_ip}
|
# if val is not None and ((type(val) is str and len(val) > 0) or type(val) is int):
|
||||||
MTU = {str(self.mtu)}
|
# peerConfiguration += f"{key} = {val}\n"
|
||||||
'''
|
# peerConfiguration = "\n[Peer]\n"
|
||||||
if len(self.DNS) > 0:
|
|
||||||
peerConfiguration += f"DNS = {self.DNS}\n"
|
|
||||||
|
|
||||||
peerConfiguration += f'''
|
# peerConfiguration = f'''[Interface]
|
||||||
[Peer]
|
# PrivateKey = {self.private_key}
|
||||||
PublicKey = {self.configuration.PublicKey}
|
# Address = {self.allowed_ip}
|
||||||
AllowedIPs = {self.endpoint_allowed_ip}
|
# MTU = {str(self.mtu)}
|
||||||
Endpoint = {self.configuration.DashboardConfig.GetConfig("Peers", "remote_endpoint")[1]}:{self.configuration.ListenPort}
|
# '''
|
||||||
PersistentKeepalive = {str(self.keepalive)}
|
# if len(self.DNS) > 0:
|
||||||
'''
|
# peerConfiguration += f"DNS = {self.DNS}\n"
|
||||||
if len(self.preshared_key) > 0:
|
#
|
||||||
peerConfiguration += f"PresharedKey = {self.preshared_key}\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 {
|
return {
|
||||||
"fileName": finalFilename,
|
"fileName": finalFilename,
|
||||||
"file": peerConfiguration
|
"file": peerConfiguration
|
||||||
|
Reference in New Issue
Block a user