lantun_core/
lib.rs

1mod client;
2mod common;
3mod host;
4mod net;
5mod utils;
6
7use client::ClientState;
8use host::HostState;
9use iroh::SecretKey;
10use rand::rngs::OsRng;
11use std::{
12    net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
13    str::FromStr,
14};
15
16pub use client::ClientTunnel;
17pub use common::TunnelCommon;
18pub use host::HostTunnel;
19
20pub const ALPN: &[u8] = b"lan-tun/0.1.0";
21pub const LANTUN_VERSION: &str = env!("CARGO_PKG_VERSION");
22
23pub fn get_unspecified(ip4: bool) -> SocketAddr {
24    if ip4 {
25        SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0))
26    } else {
27        SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 0, 0, 0))
28    }
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
32pub enum TunnelProtocol {
33    Tcp,
34    Udp,
35}
36
37impl std::fmt::Display for TunnelProtocol {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        match self {
40            TunnelProtocol::Tcp => write!(f, "tcp"),
41            TunnelProtocol::Udp => write!(f, "udp"),
42        }
43    }
44}
45
46impl FromStr for TunnelProtocol {
47    type Err = std::io::Error;
48
49    fn from_str(s: &str) -> Result<Self, Self::Err> {
50        match s.to_lowercase().as_str() {
51            "tcp" => Ok(TunnelProtocol::Tcp),
52            "udp" => Ok(TunnelProtocol::Udp),
53            _ => Err(std::io::Error::new(
54                std::io::ErrorKind::InvalidInput,
55                format!("Invalid protocol: {}", s),
56            )),
57        }
58    }
59}
60
61impl serde::Serialize for TunnelProtocol {
62    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
63    where
64        S: serde::Serializer,
65    {
66        let s = match self {
67            TunnelProtocol::Tcp => "tcp",
68            TunnelProtocol::Udp => "udp",
69        };
70        serializer.serialize_str(s)
71    }
72}
73
74impl<'de> serde::Deserialize<'de> for TunnelProtocol {
75    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
76    where
77        D: serde::Deserializer<'de>,
78    {
79        let s = String::deserialize(deserializer)?;
80        TunnelProtocol::from_str(&s).map_err(serde::de::Error::custom)
81    }
82}
83
84/// The state of the LanTun application.
85/// This tracks both the host and client state,
86/// so a user can use other tunnels as well as create their own.
87pub struct LanTun {
88    /// The state of the tunnels hosted by the user.
89    pub host: HostState,
90    /// The state of the tunnels this user is connected to.
91    pub client: ClientState,
92}
93
94impl Default for LanTun {
95    fn default() -> Self {
96        Self::new()
97    }
98}
99
100impl LanTun {
101    pub fn new() -> Self {
102        let host_state = HostState::new();
103        let client_state = ClientState::new();
104
105        LanTun {
106            host: host_state,
107            client: client_state,
108        }
109    }
110}
111
112pub fn gen_secret() -> SecretKey {
113    let mut rng = OsRng;
114    SecretKey::generate(&mut rng)
115}