8000 Fix allowedips nix setup by adding correct routing as well by b-m-f · Pull Request #10 · b-m-f/wired · GitHub
[go: up one dir, main page]

Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project will be documented in this file.

## [2.2.3] - 2026-08-23

### Fixed
- Fixed Nix outputs to automatically define systemd-networkd kernel routing rules (`routes`) for all `AllowedIPs` configured across connected peers.

## [2.2.2] - 2026-08-23

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wired"
version = "2.2.2"
version = "2.2.3"
edition = "2024"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use wired::{
#[derive(Parser, Debug)]
#[command(name = "wired")]
#[command(author = "b-m-f <maximilian@sodawa.com>")]
#[command(version = "2.2.2")]
#[command(version = "2.2.3")]
#[command(about = "WireGuard network config generator", long_about = None)]
struct Args {
/// Config file to parse
Expand Down
30 changes: 24 additions & 6 deletions src/wired/outputs/nix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ pub fn generate_server(

// create peer section
let mut peers: Vec<String> = Vec::new();
let mut all_routes = vec![format!(
" {{\n Destination = \"{cidr}\";\n }}"
)];
let mut added_routes = vec![cidr.to_string()];

for client in clients {
let publickey = client.publickey.clone();
Expand All @@ -73,6 +77,12 @@ pub fn generate_server(
if a_ip != &format!("{}/32", ip) && a_ip != &ip.to_string() {
final_ips.push(format!("\"{}\"", a_ip));
}
if !added_routes.contains(a_ip) && a_ip != &format!("{}/32", ip) && a_ip != &ip.to_string() {
all_routes.push(format!(
" {{\n Destination = \"{a_ip}\";\n }}"
));
added_routes.push(a_ip.clone());
}
}
let allowed_ips_str = final_ips.join(" ");
let peer = format!(
Expand All @@ -86,6 +96,7 @@ pub fn generate_server(
}

let peers: String = peers.into_iter().collect();
let routes_str = all_routes.join("\n");

return format!(
"
Expand Down Expand Up @@ -117,9 +128,7 @@ pub fn generate_server(
matchConfig.Name = \"{name}\";
address = [\"{ip}/32\"];
routes = [
{{
Destination = \"{cidr}\";
}}
{routes_str}
];
}};
}}
Expand Down Expand Up @@ -153,6 +162,10 @@ pub fn generate_client(

// generate peer section
let mut peers: Vec<String> = Vec::new();
let mut all_routes = vec![format!(
" {{\n Destination = \"{cidr}\";\n }}"
)];
let mut added_routes = vec![cidr.to_string()];

for server in servers {
let publickey = server.publickey.clone();
Expand All @@ -162,6 +175,12 @@ pub fn generate_client(
if a_ip != &format!("{}/32", ip) && a_ip != &ip.to_string() {
final_ips.push(format!("\"{}\"", a_ip));
}
if !added_routes.contains(a_ip) && a_ip != &format!("{}/32", ip) && a_ip != &ip.to_string() {
all_routes.push(format!(
" {{\n Destination = \"{a_ip}\";\n }}"
));
added_routes.push(a_ip.clone());
}
}
let allowed_ips_str = final_ips.join(" ");
let endpoint = server.endpoint.clone();
Expand All @@ -182,6 +201,7 @@ pub fn generate_client(
peers.push(peer)
}
let peers: String = peers.into_iter().collect();
let routes_str = all_routes.join("\n");

return format!(
"{{
Expand Down Expand Up @@ -217,9 +237,7 @@ pub fn generate_client(
IPv6AcceptRA = false;
}};
routes = [
{{
Destination = \"{cidr}\";
}}
{routes_str}
];
}};
}}",
Expand Down
14 changes: 12 additions & 2 deletions tests/allowedips.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,20 @@ Error when allowedips format is invalid:
Invalid allowed IP 'not_an_ip' for server
[1]

Successful parsing of valid allowedips:
Successful parsing of valid allowedips and correct route generation:

$ wired --config-file valid.toml

$ grep "Destination" wired/testnet/server.nix | awk '{$1=$1;print}'
Destination = "10.0.0.0/24";
Destination = "2.2.2.2/32";
$ grep "Destination" wired/testnet/client.nix | awk '{$1=$1;print}'
Destination = "10.0.0.0/24";
Destination = "1.1.1.1/32";
Destination = "0.0.0.0/0";
$ grep "AllowedIPs" wired/testnet/server.nix | awk '{$1=$1;print}'
AllowedIPs =["10.0.0.2" "2.2.2.2/32"];
$ grep "AllowedIPs" wired/testnet/client.nix | awk '{$1=$1;print}'
AllowedIPs = ["10.0.0.1" "1.1.1.1/32" "0.0.0.0/0"];
No duplicate IPs should appear in the generated nix files:

$ wired --config-file duplicate-ip.toml
Expand Down
2 changes: 2 additions & 0 deletions tests/allowedips.t/valid.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ ip = "10.0.0.1"
listenport = 20202
endpoint = "1.1.1.1"
allowedips = ["1.1.1.1/32", "0.0.0.0/0"]
output = "nix"

[clients]
[clients.client]
ip = "10.0.0.2"
allowedips = ["2.2.2.2"]
output = "nix"
Loading
0