wireguard_conf/models/
interface.rs1use either::Either;
2use ipnet::Ipv4Net;
3
4use std::fmt;
5
6use crate::prelude::*;
7
8#[must_use]
9#[derive(Clone, Debug)]
10pub struct Interface {
11 pub address: Ipv4Net,
12 pub listen_port: Option<u16>,
13 pub private_key: PrivateKey,
14 pub dns: Vec<String>,
15 pub endpoint: Option<String>,
17
18 pub peers: Vec<Peer>,
19}
20
21impl Interface {
22 pub fn to_peer(&self) -> Peer {
23 Peer {
24 endpoint: self.endpoint.clone(),
25 allowed_ips: vec![self.address],
26 key: Either::Left(self.private_key.clone()),
27 }
28 }
29}
30
31impl fmt::Display for Interface {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 writeln!(f, "[Interface]")?;
34 if let Some(endpoint) = self.endpoint.clone() {
35 writeln!(f, "# Name = {endpoint}")?;
36 }
37 writeln!(f, "Address = {}", self.address)?;
38 if let Some(listen_port) = self.listen_port {
39 writeln!(f, "ListenPort = {listen_port}")?;
40 }
41 writeln!(f, "PrivateKey = {}", self.private_key)?;
42 if !self.dns.is_empty() {
43 writeln!(f, "DNS = {}", self.dns.join(","))?;
44 }
45
46 for peer in &self.peers {
47 writeln!(f)?;
48 writeln!(f, "{peer}")?;
49 }
50
51 fmt::Result::Ok(())
52 }
53}