Distinguish between "remote" and "local" peer attributes when generating the config files.

This commit is contained in:
dolf 2023-02-27 13:35:27 +02:00
parent e100577e4c
commit d51e1df7d1

View File

@ -45,18 +45,25 @@ INTERFACE_OPTIONAL_ATTRIBUTES = [
"SaveConfig", "SaveConfig",
] ]
PEER_ATTRIBUTES = [ PEER_ATTRIBUTES_REMOTE = [
"PublicKey", "PublicKey",
"PresharedKey", "PresharedKey",
"AllowedIPs", "AllowedIPs",
"Endpoint", "Endpoint",
]
PEER_OPTIONAL_ATTRIBUTES_REMOTE = []
PEER_ATTRIBUTES_LOCAL = [
"PersistentKeepalive", "PersistentKeepalive",
] ]
PEER_OPTIONAL_ATTRIBUTES = [ PEER_OPTIONAL_ATTRIBUTES_LOCAL = [
"PersistentKeepalive", "PersistentKeepalive",
] ]
ALL_ATTRIBUTES = INTERFACE_ATTRIBUTES + PEER_ATTRIBUTES_REMOTE + PEER_ATTRIBUTES_LOCAL
KEY_TYPE = { KEY_TYPE = {
"Name": str, "Name": str,
"Address": list, "Address": list,
@ -199,7 +206,7 @@ class DatabaseManager:
privatekey = self.wireguard.genkey() privatekey = self.wireguard.genkey()
database["peers"][Name]["PrivateKey"] = privatekey database["peers"][Name]["PrivateKey"] = privatekey
for key in INTERFACE_ATTRIBUTES + PEER_ATTRIBUTES: for key in ALL_ATTRIBUTES:
if locals().get(key) is not None: if locals().get(key) is not None:
database["peers"][Name][key] = locals().get(key) database["peers"][Name][key] = locals().get(key)
@ -230,7 +237,7 @@ class DatabaseManager:
print(f"Peer with name {Name} does not exist") print(f"Peer with name {Name} does not exist")
return return
for key in INTERFACE_ATTRIBUTES + PEER_ATTRIBUTES: for key in ALL_ATTRIBUTES:
if locals().get(key) is not None: if locals().get(key) is not None:
database["peers"][Name][key] = locals().get(key) database["peers"][Name][key] = locals().get(key)
@ -268,7 +275,7 @@ class DatabaseManager:
# exclude all columns that only have None's in simplified mode # exclude all columns that only have None's in simplified mode
if verbose is False: if verbose is False:
for peer in peers: for peer in peers:
for key in INTERFACE_ATTRIBUTES + PEER_ATTRIBUTES: for key in ALL_ATTRIBUTES:
if ( if (
database["peers"][peer].get(key) is not None database["peers"][peer].get(key) is not None
and key not in field_names and key not in field_names
@ -277,7 +284,7 @@ class DatabaseManager:
# include all columns by default # include all columns by default
else: else:
field_names += INTERFACE_ATTRIBUTES + PEER_ATTRIBUTES field_names += ALL_ATTRIBUTES
# create new rich table # create new rich table
table = Table(show_lines=True) table = Table(show_lines=True)
@ -333,54 +340,51 @@ class DatabaseManager:
# for every peer in the database # for every peer in the database
for peer in peers: for peer in peers:
local_peer = database["peers"][peer]
with (output / f"{peer}.conf").open("w") as config: with (output / f"{peer}.conf").open("w") as config:
config.write("[Interface]\n") config.write("[Interface]\n")
config.write("# Name: {}\n".format(peer)) config.write("# Name: {}\n".format(peer))
config.write( config.write("Address = {}\n".format(", ".join(local_peer["Address"])))
"Address = {}\n".format( config.write("PrivateKey = {}\n".format(local_peer["PrivateKey"]))
", ".join(database["peers"][peer]["Address"])
)
)
config.write(
"PrivateKey = {}\n".format(database["peers"][peer]["PrivateKey"])
)
for key in INTERFACE_OPTIONAL_ATTRIBUTES: for key in INTERFACE_OPTIONAL_ATTRIBUTES:
if database["peers"][peer].get(key) is not None: if local_peer.get(key) is not None:
config.write( config.write("{} = {}\n".format(key, local_peer[key]))
"{} = {}\n".format(key, database["peers"][peer][key])
)
# generate [Peer] sections for all other peers # generate [Peer] sections for all other peers
for p in [i for i in database["peers"] if i != peer]: for p in [i for i in database["peers"] if i != peer]:
remote_peer = database["peers"][p]
config.write("\n[Peer]\n") config.write("\n[Peer]\n")
config.write("# Name: {}\n".format(p)) config.write("# Name: {}\n".format(p))
config.write( config.write(
"PublicKey = {}\n".format( "PublicKey = {}\n".format(
self.wireguard.pubkey(database["peers"][p]["PrivateKey"]) self.wireguard.pubkey(remote_peer["PrivateKey"])
) )
) )
if database["peers"][p].get("Endpoint") is not None: if remote_peer.get("Endpoint") is not None:
config.write( config.write(
"Endpoint = {}:{}\n".format( "Endpoint = {}:{}\n".format(
database["peers"][p]["Endpoint"], remote_peer["Endpoint"],
database["peers"][p]["ListenPort"], remote_peer["ListenPort"],
) )
) )
if database["peers"][p].get("Address") is not None: if remote_peer.get("Address") is not None:
if database["peers"][p].get("AllowedIPs") is not None: if remote_peer.get("AllowedIPs") is not None:
allowed_ips = ", ".join( allowed_ips = ", ".join(
database["peers"][p]["Address"] remote_peer["Address"] + remote_peer["AllowedIPs"]
+ database["peers"][p]["AllowedIPs"]
) )
else: else:
allowed_ips = ", ".join(database["peers"][p]["Address"]) allowed_ips = ", ".join(remote_peer["Address"])
config.write("AllowedIPs = {}\n".format(allowed_ips)) config.write("AllowedIPs = {}\n".format(allowed_ips))
for key in PEER_OPTIONAL_ATTRIBUTES: for key in PEER_OPTIONAL_ATTRIBUTES_REMOTE:
if database["peers"][p].get(key) is not None: if remote_peer.get(key) is not None:
config.write( config.write("{} = {}\n".format(key, remote_peer[key]))
"{} = {}\n".format(key, database["peers"][p][key])
) for key in PEER_OPTIONAL_ATTRIBUTES_LOCAL:
if local_peer.get(key) is not None:
config.write("{} = {}\n".format(key, local_peer[key]))