Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add serialization support #26

Merged
merged 4 commits into from
Oct 16, 2023
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
19 changes: 10 additions & 9 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ categories = ["network-programming"]
[dependencies]
base64 = "0.21"
log = "0.4"
serde = { version = "1.0", features = ["derive"] }
thiserror = "1.0"

[dev-dependencies]
Expand Down
5 changes: 3 additions & 2 deletions src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ use netlink_packet_wireguard::{
constants::{WGDEVICE_F_REPLACE_PEERS, WGPEER_F_REPLACE_ALLOWEDIPS},
nlas::{WgAllowedIpAttrs, WgDeviceAttrs, WgPeer, WgPeerAttrs},
};
use serde::{Deserialize, Serialize};

use crate::{key::Key, net::IpAddrMask};

/// WireGuard peer representation.
#[derive(Debug, Default, PartialEq, Clone)]
#[derive(Debug, Default, PartialEq, Clone, Serialize, Deserialize)]
pub struct Peer {
pub public_key: Key,
pub preshared_key: Option<Key>,
Expand Down Expand Up @@ -163,7 +164,7 @@ impl Peer {
}

/// WireGuard host representation.
#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Host {
pub listen_port: u16,
pub private_key: Option<Key>,
Expand Down
3 changes: 2 additions & 1 deletion src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use std::{
};

use base64::{prelude::BASE64_STANDARD, DecodeError, Engine};
use serde::{Deserialize, Serialize};

const KEY_LENGTH: usize = 32;

/// WireGuard key representation in binary form.
#[derive(Clone, Default)]
#[derive(Clone, Default, Serialize, Deserialize)]
pub struct Key([u8; KEY_LENGTH]);

#[derive(Debug)]
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ mod wireguard_interface;
#[macro_use]
extern crate log;

use serde::{Deserialize, Serialize};
use std::process::Output;

use self::{
Expand All @@ -90,7 +91,7 @@ pub use wgapi_userspace::WireguardApiUserspace;
pub use wireguard_interface::WireguardInterfaceApi;

/// Host WireGuard interface configuration
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InterfaceConfiguration {
pub name: String,
pub prvkey: String,
Expand Down
3 changes: 2 additions & 1 deletion src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use netlink_packet_wireguard::{
constants::{AF_INET, AF_INET6},
nlas::{WgAllowedIp, WgAllowedIpAttrs},
};
use serde::{Deserialize, Serialize};

/// IP address with CIDR.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct IpAddrMask {
// IP v4 or v6
pub ip: IpAddr,
Expand Down