Skip to content

Commit

Permalink
feat: add serialization support (#26)
Browse files Browse the repository at this point in the history
* add serde dependency

* serialize interface configuration

* serialize host struct

---------

Co-authored-by: Maciej Wójcik <[email protected]>
  • Loading branch information
wojcik91 and Maciej Wójcik authored Oct 16, 2023
1 parent 96c7050 commit f05818a
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
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

0 comments on commit f05818a

Please sign in to comment.