1
0
mirror of https://github.com/donaldzou/WGDashboard.git synced 2024-11-22 15:20:09 +01:00

Update dashboard_new.py

Yay adding peers in bulk is finally done ;)
This commit is contained in:
Donald Zou 2024-05-14 00:58:01 +08:00
parent 5a34f16dcf
commit 41e05ddf9c

View File

@ -19,7 +19,7 @@ from dataclasses import dataclass
from datetime import datetime, timedelta from datetime import datetime, timedelta
from json import JSONEncoder from json import JSONEncoder
from operator import itemgetter from operator import itemgetter
from typing import Dict, Any from typing import Dict, Any, Tuple
import bcrypt import bcrypt
# PIP installed library # PIP installed library
@ -772,7 +772,7 @@ def _checkDNS(dns):
return True return True
def _generatePublicKey(privateKey) -> [bool, str] | [bool, None]: def _generatePublicKey(privateKey) -> tuple[bool, str] | tuple[bool, None]:
try: try:
publicKey = subprocess.check_output(f"wg pubkey", input=privateKey.encode(), shell=True, publicKey = subprocess.check_output(f"wg pubkey", input=privateKey.encode(), shell=True,
stderr=subprocess.STDOUT) stderr=subprocess.STDOUT)
@ -1032,11 +1032,28 @@ def API_addPeers(configName):
keyPairs = [] keyPairs = []
for i in range(bulkAddAmount): for i in range(bulkAddAmount):
key = _generatePrivateKey()[1] key = _generatePrivateKey()[1]
keyPairs.append([key, _generatePublicKey(key)[1], _generatePrivateKey()[1]]) keyPairs.append([key, _generatePublicKey(key)[1], _generatePrivateKey()[1], availableIps[1][i],
f"{config.Name}_{datetime.now().strftime('%m%d%Y%H%M%S')}_Peer_#_{(i + 1)}"])
if len(keyPairs) == 0: if len(keyPairs) == 0:
return ResponseObject(False, "Generating key pairs by bulk failed") return ResponseObject(False, "Generating key pairs by bulk failed")
print(keyPairs) for i in range(bulkAddAmount):
subprocess.check_output(
f"wg set {config.Name} peer {keyPairs[i][1]} allowed-ips {keyPairs[i][3]}",
shell=True, stderr=subprocess.STDOUT)
subprocess.check_output(
f"wg-quick save {config.Name}", shell=True, stderr=subprocess.STDOUT)
config.getPeersList()
for i in range(bulkAddAmount):
found, peer = config.searchPeer(keyPairs[i][1])
if found:
if not peer.updatePeer(keyPairs[i][4], keyPairs[i][0], preshared_key, dns_addresses,
keyPairs[i][3],
endpoint_allowed_ip, mtu, keep_alive).status:
return ResponseObject(False, "Failed to add peers in bulk")
return ResponseObject()