mirror of
https://github.com/donaldzou/WGDashboard.git
synced 2025-07-13 16:46:58 +00:00
Added automatically assign keys and allowed ip when none provided
Resolve #515
This commit is contained in:
parent
d4055884b1
commit
47aac7fe33
@ -654,7 +654,11 @@ class WireguardConfiguration:
|
||||
for i in checkIfExist:
|
||||
self.Peers.append(Peer(i, self))
|
||||
|
||||
def addPeers(self, peers: list):
|
||||
def addPeers(self, peers: list) -> tuple[bool, dict]:
|
||||
result = {
|
||||
"message": None,
|
||||
"peers": []
|
||||
}
|
||||
try:
|
||||
for i in peers:
|
||||
newPeer = {
|
||||
@ -687,6 +691,7 @@ class WireguardConfiguration:
|
||||
:cumu_data, :mtu, :keepalive, :remote_endpoint, :preshared_key);
|
||||
""" % self.Name
|
||||
, newPeer)
|
||||
|
||||
for p in peers:
|
||||
presharedKeyExist = len(p['preshared_key']) > 0
|
||||
rd = random.Random()
|
||||
@ -702,10 +707,14 @@ class WireguardConfiguration:
|
||||
subprocess.check_output(
|
||||
f"{self.Protocol}-quick save {self.Name}", shell=True, stderr=subprocess.STDOUT)
|
||||
self.getPeersList()
|
||||
return True
|
||||
for p in peers:
|
||||
p = self.searchPeer(p['id'])
|
||||
if p[0]:
|
||||
result['peers'].append(p[1])
|
||||
return True, result
|
||||
except Exception as e:
|
||||
print(str(e))
|
||||
return False
|
||||
result['message'] = str(e)
|
||||
return False, result
|
||||
|
||||
def searchPeer(self, publicKey):
|
||||
for i in self.Peers:
|
||||
@ -2469,7 +2478,7 @@ def API_addPeers(configName):
|
||||
preshared_key_bulkAdd: bool = data.get('preshared_key_bulkAdd', False)
|
||||
|
||||
public_key: str = data.get('public_key', "")
|
||||
allowed_ips: list[str] = data.get('allowed_ips', "")
|
||||
allowed_ips: list[str] = data.get('allowed_ips', [])
|
||||
override_allowed_ips: bool = data.get('override_allowed_ips', False)
|
||||
|
||||
endpoint_allowed_ip: str = data.get('endpoint_allowed_ip', DashboardConfig.GetConfig("Peers", "peer_endpoint_allowed_ip")[1])
|
||||
@ -2487,8 +2496,8 @@ def API_addPeers(configName):
|
||||
if len(endpoint_allowed_ip) == 0:
|
||||
endpoint_allowed_ip = DashboardConfig.GetConfig("Peers", "peer_endpoint_allowed_ip")[1]
|
||||
config = WireguardConfigurations.get(configName)
|
||||
if not bulkAdd and (len(public_key) == 0 or len(allowed_ips) == 0):
|
||||
return ResponseObject(False, "Please provide at least public_key and allowed_ips")
|
||||
# if not bulkAdd and (len(public_key) == 0 or len(allowed_ips) == 0):
|
||||
# return ResponseObject(False, "Please provide at least public_key and allowed_ips")
|
||||
if not config.getStatus():
|
||||
config.toggleConfiguration()
|
||||
availableIps = config.getAvailableIP()
|
||||
@ -2519,21 +2528,31 @@ def API_addPeers(configName):
|
||||
})
|
||||
if len(keyPairs) == 0:
|
||||
return ResponseObject(False, "Generating key pairs by bulk failed")
|
||||
config.addPeers(keyPairs)
|
||||
return ResponseObject()
|
||||
status, result = config.addPeers(keyPairs)
|
||||
return ResponseObject(status=status, message=result['message'], data=result['peers'])
|
||||
|
||||
else:
|
||||
if config.searchPeer(public_key)[0] is True:
|
||||
return ResponseObject(False, f"This peer already exist")
|
||||
name = data.get("name", "")
|
||||
private_key = data.get("private_key", "")
|
||||
|
||||
if len(public_key) == 0 and len(private_key) == 0:
|
||||
private_key = GenerateWireguardPrivateKey()[1]
|
||||
public_key = GenerateWireguardPublicKey(private_key)[1]
|
||||
|
||||
if len(allowed_ips) == 0:
|
||||
if availableIps[0]:
|
||||
allowed_ips = [availableIps[1][0]]
|
||||
else:
|
||||
return ResponseObject(False, "No more available IP can assign")
|
||||
|
||||
if not override_allowed_ips:
|
||||
for i in allowed_ips:
|
||||
if i not in availableIps[1]:
|
||||
return ResponseObject(False, f"This IP is not available: {i}")
|
||||
|
||||
status = config.addPeers([
|
||||
status, result = config.addPeers([
|
||||
{
|
||||
"name": name,
|
||||
"id": public_key,
|
||||
@ -2547,7 +2566,7 @@ def API_addPeers(configName):
|
||||
"advanced_security": data.get("advanced_security", "off")
|
||||
}]
|
||||
)
|
||||
return ResponseObject(status)
|
||||
return ResponseObject(status=status, message=result['message'], data=result['peers'])
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return ResponseObject(False, "Add peers failed. Please see data for specific issue")
|
||||
|
@ -45,6 +45,7 @@ const openFileUpload = () => {
|
||||
<h2 class="mb-0">
|
||||
<LocaleText t="Restore Configuration"></LocaleText>
|
||||
</h2>
|
||||
|
||||
<!-- <div class="d-flex gap-2 ms-auto">-->
|
||||
<!-- <button class="titleBtn py-2 text-decoration-none btn text-primary-emphasis bg-primary-subtle rounded-3 border-1 border-primary-subtle"-->
|
||||
<!-- @click="openFileUpload()"-->
|
||||
@ -54,6 +55,7 @@ const openFileUpload = () => {
|
||||
<!-- </button>-->
|
||||
<!-- </div>-->
|
||||
</div>
|
||||
|
||||
<Transition name="fade" appear>
|
||||
<div v-if="backups">
|
||||
<div class="d-flex mb-5 align-items-center steps" role="button"
|
||||
|
Loading…
x
Reference in New Issue
Block a user