Skip to content

Commit

Permalink
Add toml serde for wireguard config
Browse files Browse the repository at this point in the history
  • Loading branch information
JettChenT committed May 11, 2024
1 parent abf1101 commit ae8ea8a
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 89 deletions.

This file was deleted.

53 changes: 53 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions burrow/Cargo.toml
Expand Up @@ -50,6 +50,7 @@ futures = "0.3.28"
once_cell = "1.19"
console-subscriber = { version = "0.2.0", optional = true }
console = "0.15.8"
toml = "0.8.12"

[dependencies.rusqlite]
version = "0.31.0"
Expand Down
3 changes: 3 additions & 0 deletions burrow/src/daemon/instance.rs
Expand Up @@ -116,6 +116,9 @@ impl DaemonInstance {
.await?;
Ok(DaemonResponseData::None)
}
DaemonCommand::AddConfigToml(interface_id, config_toml) => {
Ok(DaemonResponseData::None)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions burrow/src/daemon/rpc/request.rs
Expand Up @@ -10,6 +10,7 @@ pub enum DaemonCommand {
ServerConfig,
Stop,
ReloadConfig(String),
AddConfigToml(String, String),
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
Expand Down
21 changes: 18 additions & 3 deletions burrow/src/wireguard/config.rs
Expand Up @@ -4,6 +4,7 @@ use anyhow::{anyhow, Error, Result};
use base64::{engine::general_purpose, Engine};
use fehler::throws;
use ip_network::IpNetwork;
use serde::{Deserialize, Serialize};
use x25519_dalek::{PublicKey, StaticSecret};

use crate::wireguard::{Interface as WgInterface, Peer as WgPeer};
Expand Down Expand Up @@ -31,7 +32,7 @@ fn parse_public_key(string: &str) -> PublicKey {
/// A raw version of Peer Config that can be used later to reflect configuration files.
/// This should be later converted to a `WgPeer`.
/// Refers to https://github.com/pirate/wireguard-docs?tab=readme-ov-file#overview
#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Peer {
pub public_key: String,
pub preshared_key: Option<String>,
Expand All @@ -41,7 +42,7 @@ pub struct Peer {
pub name: Option<String>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Interface {
pub private_key: String,
pub address: Vec<String>,
Expand All @@ -50,7 +51,7 @@ pub struct Interface {
pub mtu: Option<u32>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Config {
pub peers: Vec<Peer>,
pub interface: Interface, // Support for multiple interfaces?
Expand Down Expand Up @@ -113,3 +114,17 @@ impl Default for Config {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn tst_config_toml() {
let cfig = Config::default();
let toml = toml::to_string(&cfig).unwrap();
insta::assert_snapshot!(toml);
let cfig2: Config = toml::from_str(&toml).unwrap();
assert_eq!(cfig, cfig2);
}
}
@@ -0,0 +1,16 @@
---
source: burrow/src/wireguard/config.rs
expression: toml
---
[[peers]]
public_key = "8GaFjVO6c4luCHG4ONO+1bFG8tO+Zz5/Gy+Geht1USM="
preshared_key = "ha7j4BjD49sIzyF9SNlbueK0AMHghlj6+u0G3bzC698="
allowed_ips = ["8.8.8.8/32", "0.0.0.0/0"]
endpoint = "wg.burrow.rs:51820"

[interface]
private_key = "OEPVdomeLTxTIBvv3TYsJRge0Hp9NMiY0sIrhT8OWG8="
address = ["10.13.13.2/24"]
listen_port = 51820
dns = []

0 comments on commit ae8ea8a

Please sign in to comment.