Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __intermediate_peer(self, peer, index):
if index > 1:
uci_name = f"{uci_name}_{index}"
peer.update({".type": f"wireguard_{interface}", ".name": uci_name})
peer.pop("shared_ips", None)
if not peer.get("endpoint_host") and "endpoint_port" in peer:
del peer["endpoint_port"]
return self.sorted_dict(peer)
Expand Down
11 changes: 11 additions & 0 deletions netjsonconfig/backends/openwrt/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,17 @@
"minLength": 1,
},
},
"shared_ips": {
"type": "array",
"title": "shared IPs",
"propertyOrder": 3,
"uniqueItems": True,
"items": {
"type": "string",
"title": "IP/prefix",
"minLength": 1,
},
},
"endpoint_host": wireguard_peers["endpoint_host"],
"endpoint_port": wireguard_peers["endpoint_port"],
"preshared_key": wireguard_peers["preshared_key"],
Expand Down
7 changes: 6 additions & 1 deletion netjsonconfig/backends/wireguard/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ def __intermediate_vpn(self, config, remove=None):
def __intermediate_peers(self, peers):
peer_list = []
for peer in peers:
peer["AllowedIPs"] = peer.pop("allowed_ips")
allowed_ips = peer.pop("allowed_ips")
shared_ips = peer.pop("shared_ips", [])
if shared_ips:
extra = ", ".join(ip for ip in shared_ips if ip)
allowed_ips = f"{allowed_ips}, {extra}"
peer["AllowedIPs"] = allowed_ips
peer["PublicKey"] = peer.pop("public_key")
peer["PreSharedKey"] = peer.pop("preshared_key", None)
host = peer.pop("endpoint_host", None)
Expand Down
11 changes: 11 additions & 0 deletions netjsonconfig/backends/wireguard/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@
"pattern": "^[^\\s]*$",
"propertyOrder": 5,
},
"shared_ips": {
"title": "shared IP addresses",
"type": "array",
"uniqueItems": True,
"propertyOrder": 6,
"items": {
"type": "string",
"title": "IP/prefix",
"minLength": 1,
},
},
},
},
},
Expand Down
32 changes: 32 additions & 0 deletions tests/openwrt/test_wireguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,38 @@ def test_render_wireguard_peer(self):
)
expected = self._tabs("""package network

config wireguard_wg0 'wgpeer_wg0'
list allowed_ips '10.0.0.1/32'
option endpoint_host '192.168.1.42'
option endpoint_port '40840'
option persistent_keepalive '30'
option preshared_key 'oPZmGdHBseaV1TF0julyElNuJyeKs2Eo+o62R/09IB4='
option public_key 'rn+isMBpyQ4HX6ZzE709bKnZw5IaLZoIS3hIjmfKCkk='
option route_allowed_ips '1'
""")
self.assertEqual(o.render(), expected)

def test_render_wireguard_peer_shared_ips(self):
o = OpenWrt(
{
"wireguard_peers": [
{
"interface": "wg0",
"public_key": "rn+isMBpyQ4HX6ZzE709bKnZw5IaLZoIS3hIjmfKCkk=",
"allowed_ips": ["10.0.0.1/32"],
"shared_ips": ["192.168.1.0/24"],
"endpoint_host": "192.168.1.42",
"endpoint_port": 40840,
"preshared_key": "oPZmGdHBseaV1TF0julyElNuJyeKs2Eo+o62R/09IB4=",
"persistent_keepalive": 30,
"route_allowed_ips": True,
}
]
}
)
o.validate()
expected = self._tabs("""package network

config wireguard_wg0 'wgpeer_wg0'
list allowed_ips '10.0.0.1/32'
option endpoint_host '192.168.1.42'
Expand Down
34 changes: 34 additions & 0 deletions tests/wireguard/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,40 @@ def test_peers(self):
Endpoint = 192.168.1.35:4908
PreSharedKey = xisFXck9KfEZga4hlkproH6+86S8ki1tmLtMtqVipjg=
PublicKey = 94a+MnZSdzHCzOy5y2K+0+Xe7lQzaa4v7lEiBZ7elVE=
"""
self.assertEqual(c.render(), expected)

def test_peers_shared_ips(self):
c = Wireguard(
{
"wireguard": [
{
"name": "test1",
"private_key": "QFdbnuYr7rrF4eONCAs7FhZwP7BXX/jD/jq2LXCpaXI=",
"port": 40842,
"address": "10.0.0.1/24",
"peers": [
{
"public_key": "jqHs76yCH0wThMSqogDshndAiXelfffUJVcFmz352HI=",
"allowed_ips": "10.0.0.3/32",
"shared_ips": ["192.168.1.0/24"],
}
],
}
]
}
)
c.validate()
expected = """# wireguard config: test1

[Interface]
Address = 10.0.0.1/24
ListenPort = 40842
PrivateKey = QFdbnuYr7rrF4eONCAs7FhZwP7BXX/jD/jq2LXCpaXI=

[Peer]
AllowedIPs = 10.0.0.3/32, 192.168.1.0/24
PublicKey = jqHs76yCH0wThMSqogDshndAiXelfffUJVcFmz352HI=
"""
self.assertEqual(c.render(), expected)

Expand Down
Loading