Skip to content

Commit

Permalink
Exclude peers from config if they can't connect directly. This allows…
Browse files Browse the repository at this point in the history
… them to connect indirectly.
  • Loading branch information
rudolfbyker committed Oct 12, 2023
1 parent aac994f commit 5a9f1c1
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions wg_meshconf/database_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def init(self):
database = self.read_database()

# check values that cannot be generated automatically
for key in ["Address", "Endpoint"]:
for key in ["Address"]:
for peer in database["peers"]:
if database["peers"][peer].get(key) is None:
print(f"The value of {key} cannot be automatically generated")
Expand Down Expand Up @@ -341,6 +341,7 @@ def genconfig(self, Name: str, output: pathlib.Path):
# for every peer in the database
for peer in peers:
local_peer = database["peers"][peer]
local_peer_endpoint = local_peer.get("Endpoint")

with (output / f"{peer}.conf").open("w") as config:
config.write("[Interface]\n")
Expand All @@ -355,6 +356,16 @@ def genconfig(self, Name: str, output: pathlib.Path):
# generate [Peer] sections for all other peers
for p in [i for i in database["peers"] if i != peer]:
remote_peer = database["peers"][p]
remote_peer_endpoint = remote_peer.get("Endpoint")

peers_can_connect_directly = (
remote_peer_endpoint is not None
or local_peer_endpoint is not None
)
if not peers_can_connect_directly:
# See https://github.com/pirate/wireguard-docs#how-public-relay-servers-work
# In short: Only direct connections between clients should be configured.
continue

config.write("\n[Peer]\n")
config.write("# Name: {}\n".format(p))
Expand All @@ -364,7 +375,7 @@ def genconfig(self, Name: str, output: pathlib.Path):
)
)

if remote_peer.get("Endpoint") is not None:
if remote_peer_endpoint is not None:
config.write(
"Endpoint = {}:{}\n".format(
remote_peer["Endpoint"],
Expand Down

0 comments on commit 5a9f1c1

Please sign in to comment.