Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#30 PersistentKeepAlive should be specified in the config file of the host which is behind NAT. #33

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,7 @@ venv.bak/

# mypy
.mypy_cache/

# IntelliJ IDEA
.idea
*.iml
k4yt3x marked this conversation as resolved.
Show resolved Hide resolved
68 changes: 36 additions & 32 deletions wg_meshconf/database_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,25 @@
"SaveConfig",
]

PEER_ATTRIBUTES = [
PEER_ATTRIBUTES_REMOTE = [
"PublicKey",
"PresharedKey",
"AllowedIPs",
"Endpoint",
]

PEER_OPTIONAL_ATTRIBUTES_REMOTE = []

PEER_ATTRIBUTES_LOCAL = [
"PersistentKeepalive",
]

PEER_OPTIONAL_ATTRIBUTES = [
PEER_OPTIONAL_ATTRIBUTES_LOCAL = [
"PersistentKeepalive",
]

ALL_ATTRIBUTES = INTERFACE_ATTRIBUTES + PEER_ATTRIBUTES_REMOTE + PEER_ATTRIBUTES_LOCAL

KEY_TYPE = {
"Name": str,
"Address": list,
Expand Down Expand Up @@ -199,7 +206,7 @@ def addpeer(
privatekey = self.wireguard.genkey()
database["peers"][Name]["PrivateKey"] = privatekey

for key in INTERFACE_ATTRIBUTES + PEER_ATTRIBUTES:
for key in ALL_ATTRIBUTES:
if locals().get(key) is not None:
database["peers"][Name][key] = locals().get(key)

Expand Down Expand Up @@ -230,7 +237,7 @@ def updatepeer(
print(f"Peer with name {Name} does not exist")
return

for key in INTERFACE_ATTRIBUTES + PEER_ATTRIBUTES:
for key in ALL_ATTRIBUTES:
if locals().get(key) is not None:
database["peers"][Name][key] = locals().get(key)

Expand Down Expand Up @@ -268,7 +275,7 @@ def showpeers(self, Name: str, verbose: bool = False):
# exclude all columns that only have None's in simplified mode
if verbose is False:
for peer in peers:
for key in INTERFACE_ATTRIBUTES + PEER_ATTRIBUTES:
for key in ALL_ATTRIBUTES:
if (
database["peers"][peer].get(key) is not None
and key not in field_names
Expand All @@ -277,7 +284,7 @@ def showpeers(self, Name: str, verbose: bool = False):

# include all columns by default
else:
field_names += INTERFACE_ATTRIBUTES + PEER_ATTRIBUTES
field_names += ALL_ATTRIBUTES

# create new rich table
table = Table(show_lines=True)
Expand Down Expand Up @@ -333,54 +340,51 @@ def genconfig(self, Name: str, output: pathlib.Path):

# for every peer in the database
for peer in peers:
local_peer = database["peers"][peer]

with (output / f"{peer}.conf").open("w") as config:
config.write("[Interface]\n")
config.write("# Name: {}\n".format(peer))
config.write(
"Address = {}\n".format(
", ".join(database["peers"][peer]["Address"])
)
)
config.write(
"PrivateKey = {}\n".format(database["peers"][peer]["PrivateKey"])
)
config.write("Address = {}\n".format(", ".join(local_peer["Address"])))
config.write("PrivateKey = {}\n".format(local_peer["PrivateKey"]))

for key in INTERFACE_OPTIONAL_ATTRIBUTES:
if database["peers"][peer].get(key) is not None:
config.write(
"{} = {}\n".format(key, database["peers"][peer][key])
)
if local_peer.get(key) is not None:
config.write("{} = {}\n".format(key, local_peer[key]))

# generate [Peer] sections for all other peers
for p in [i for i in database["peers"] if i != peer]:
remote_peer = database["peers"][p]

config.write("\n[Peer]\n")
config.write("# Name: {}\n".format(p))
config.write(
"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(
"Endpoint = {}:{}\n".format(
database["peers"][p]["Endpoint"],
database["peers"][p]["ListenPort"],
remote_peer["Endpoint"],
remote_peer["ListenPort"],
)
)

if database["peers"][p].get("Address") is not None:
if database["peers"][p].get("AllowedIPs") is not None:
if remote_peer.get("Address") is not None:
if remote_peer.get("AllowedIPs") is not None:
allowed_ips = ", ".join(
database["peers"][p]["Address"]
+ database["peers"][p]["AllowedIPs"]
remote_peer["Address"] + remote_peer["AllowedIPs"]
)
else:
allowed_ips = ", ".join(database["peers"][p]["Address"])
allowed_ips = ", ".join(remote_peer["Address"])
config.write("AllowedIPs = {}\n".format(allowed_ips))

for key in PEER_OPTIONAL_ATTRIBUTES:
if database["peers"][p].get(key) is not None:
config.write(
"{} = {}\n".format(key, database["peers"][p][key])
)
for key in PEER_OPTIONAL_ATTRIBUTES_REMOTE:
if remote_peer.get(key) is not None:
config.write("{} = {}\n".format(key, remote_peer[key]))

for key in PEER_OPTIONAL_ATTRIBUTES_LOCAL:
if local_peer.get(key) is not None:
config.write("{} = {}\n".format(key, local_peer[key]))