Implement Networkd Addressing and Wireguard Templates

This commit is contained in:
Samuel Lorch 2024-02-10 19:55:58 +01:00
parent 5a2b270925
commit 6f91113b3f
3 changed files with 93 additions and 26 deletions

View file

@ -17,7 +17,7 @@ pub fn apply_networkd(pending_config: Config, current_config: Config) -> Result<
info!("Got Files");
for file in files {
info!("Conf File {}", file.name);
info!("{}", file.content);
info!("\n{}", file.content);
}
Ok(())
}
@ -114,11 +114,10 @@ pub fn generate_networkd_config_files(
}
// Step 4 Generate wireguard netdev files
/* TODO
for interface in &pending_config.network.interfaces {
if let NetworkInterfaceType::Bridge { .. } = interface.interface_type {
for interface in &pending_config.vpn.wireguard.interfaces {
let mut context = Context::new();
context.insert("name", &interface.name);
context.insert("interface", &interface);
context.insert("peers", &interface.peers(pending_config.clone()));
files.push(generate_config_file(
context,
@ -126,27 +125,49 @@ pub fn generate_networkd_config_files(
format!("40-create-wireguard-{}.netdev", &interface.name),
)?);
}
}
*/
// Step 5 Generate Addressing network files
/*
for interface in &pending_config.network.interfaces {
if let NetworkInterfaceType::Vlan { id, .. } = &interface.interface_type {
let mut context = Context::new();
match &interface.interface_type {
NetworkInterfaceType::Hardware { device } => context.insert("name", &device),
_ => context.insert("name", &member.name),
_ => context.insert("name", &interface.name),
};
context.insert("interface", &interface);
// List of all vlans that have this interface as a parent
let mut vlans = Vec::new();
// TODO Use Backreferenceing instead of loop and if
for vlan in &pending_config.network.interfaces {
match &vlan.interface_type {
NetworkInterfaceType::Vlan { parent, .. } => {
if parent == &interface.name {
vlans.push(vlan.name.clone());
}
}
_ => (),
};
}
context.insert("vlans", &vlans);
// List all Static Routes for this interface
let mut static_routes = Vec::new();
// TODO Use Backreferenceing instead of loop and if
for static_route in &pending_config.network.static_routes {
if static_route.interface == interface.name {
static_routes.push(static_route);
}
}
context.insert("static_routes", &static_routes);
files.push(generate_config_file(
context,
"networkd/config-addressing.network",
format!("70-config-addressing-{}.network", &interface.name),
)?);
}
}
*/
Ok(files)
}

View file

@ -0,0 +1,22 @@
[Match]
Name={{ name }}
[Network]
LLMNR=no
{% if interface.addressing_mode is containing("static") -%}
Address={{ interface.addressing_mode.static.address }}
{% elif interface.addressing_mode is containing("dhcp") -%}
DHCP=yes
{% endif -%}
{% for vlan in vlans -%}
VLAN={{ vlan }}
{% endfor -%}
{% for static_route in static_routes %}
[Route]
Destination={{ static_route.destination }}
Gateway={{ static_route.gateway }}
{% if static_route.metric != 0 -%}
Metric={{ static_route.metric }}
{% endif -%}
{% endfor -%}

View file

@ -0,0 +1,24 @@
[NetDev]
Name={{ interface.name }}
Kind=wireguard
[WireGuard]
ListenPort={{ interface.listen_port }}
PrivateKey={{ interface.private_key }}
{% for peer in peers -%}
[WireGuardPeer]
PublicKey={{ peer.public_key }}
{% if peer.preshared_key -%}
PresharedKey={{ peer.preshared_key }}
{% endif -%}
{% for allowed_ip in peer.allowed_ips -%}
AllowedIPs={{ allowed_ip }}
{% endfor -%}
{% if peer.endpoint -%}
Endpoint={{ peer.endpoint }}
{% endif -%}
{% if peer.persistent_keepalive -%}
PersistentKeepalive={{ peer.persistent_keepalive }}
{% endif %}
{% endfor %}