summaryrefslogtreecommitdiffhomepage
path: root/tunnel-obfuscation/src/lib.rs
blob: 07ab422dd763fff752f0c12820aeb0b276613bd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use async_trait::async_trait;
use std::net::SocketAddr;

mod udp2tcp;
pub use udp2tcp::Udp2TcpSettings;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(err_derive::Error, Debug)]
#[error(no_from)]
pub enum Error {
    #[error(display = "Failed to create Udp2Tcp obfuscator")]
    CreateUdp2TcpObfuscator(#[error(source)] udp2tcp::Error),

    #[error(display = "Failed to run Udp2Tcp obfuscator")]
    RunUdp2TcpObfuscator(#[error(source)] udp2tcp::Error),
}

#[async_trait]
pub trait Obfuscator: Send {
    async fn run(self: Box<Self>) -> Result<()>;

    /// Returns the address of the local socket.
    fn endpoint(&self) -> SocketAddr;

    /// Returns the file descriptor of the outbound socket.
    #[cfg(target_os = "android")]
    fn remote_socket_fd(&self) -> std::os::unix::io::RawFd;
}

pub enum Settings {
    Udp2Tcp(Udp2TcpSettings),
}

pub async fn create_obfuscator(settings: &Settings) -> Result<Box<dyn Obfuscator>> {
    match settings {
        Settings::Udp2Tcp(s) => udp2tcp::create_obfuscator(s)
            .await
            .map_err(Error::CreateUdp2TcpObfuscator),
    }
}