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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#![deny(missing_docs)]
#![deny(warnings)]
#![deny(unsafe_code)]
#![doc = tx5_core::__doc_header!()]
//! # tx5-go-pion-turn
//!
//! Rust process wrapper around tx5-go-pion-turn executable.

/// Re-exported dependencies.
pub mod deps {
    pub use tx5_core;
    pub use tx5_core::deps::*;
}

const EXE_BYTES: &[u8] =
    include_bytes!(concat!(env!("OUT_DIR"), "/tx5-go-pion-turn"));

include!(concat!(env!("OUT_DIR"), "/exe_hash.rs"));

pub use tx5_core::{Error, ErrorExt, Id, Result};

use once_cell::sync::Lazy;

// keep the file handle open to mitigate replacements on some oses
static EXE: Lazy<(std::path::PathBuf, std::fs::File)> = Lazy::new(|| {
    let mut path_1 =
        dirs::data_local_dir().expect("failed to determine data dir");
    let mut path_2 =
        dunce::canonicalize(".").expect("failed to canonicalize current dir");

    #[cfg(windows)]
    let ext = ".exe";
    #[cfg(not(windows))]
    let ext = "";

    path_1.push(format!("tx5-go-pion-turn-{EXE_HASH}{ext}"));
    path_2.push(format!("tx5-go-pion-turn-{EXE_HASH}{ext}"));

    for path in [&path_1, &path_2] {
        tracing::trace!("check exec file: {path:?}");

        let mut opts = std::fs::OpenOptions::new();

        opts.write(true);
        opts.create_new(true);

        #[cfg(unix)]
        std::os::unix::fs::OpenOptionsExt::mode(&mut opts, 0o700);

        if let Ok(mut file) = opts.open(path) {
            use std::io::Write;

            file.write_all(EXE_BYTES)
                .expect("failed to write executable bytes");
            file.flush().expect("failed to flush executable bytes");

            let mut perms = file
                .metadata()
                .expect("failed to get executable metadata")
                .permissions();

            perms.set_readonly(true);
            #[cfg(unix)]
            std::os::unix::fs::PermissionsExt::set_mode(&mut perms, 0o500);

            file.set_permissions(perms)
                .expect("failed to set exe permissions");

            tracing::trace!("wrote exec file: {path:?}");
        }

        if let Ok(mut file) = std::fs::OpenOptions::new().read(true).open(path)
        {
            use std::io::Read;

            let mut data = Vec::new();
            file.read_to_end(&mut data)
                .expect("failed to read executable");

            use sha2::Digest;
            let mut hasher = sha2::Sha256::new();
            hasher.update(data);
            let hash = base64::encode_config(
                hasher.finalize(),
                base64::URL_SAFE_NO_PAD,
            );

            assert_eq!(EXE_HASH, hash);

            let perms = file
                .metadata()
                .expect("failed to get executable metadata")
                .permissions();

            assert!(perms.readonly());

            tracing::trace!("success correct exec file: {path:?}");

            return (path.clone(), file);
        }
    }
    panic!("invalid executable paths: {path_1:?} {path_2:?}");
});

/// Rust process wrapper around tx5-go-pion-turn executable.
pub struct Tx5TurnServer {
    proc: tokio::process::Child,
}

impl Tx5TurnServer {
    /// Start up a new TURN server.
    pub async fn new(
        public_ip: std::net::IpAddr,
        bind_port: u16,
        users: Vec<(String, String)>,
        realm: String,
    ) -> Result<(String, Self)> {
        let mut cmd = tokio::process::Command::new(EXE.0.as_os_str());
        cmd.stdin(std::process::Stdio::piped());
        cmd.stdout(std::process::Stdio::piped());
        cmd.stderr(std::process::Stdio::piped());
        cmd.kill_on_drop(true);
        cmd.arg("-public-ip");
        cmd.arg(public_ip.to_string());
        cmd.arg("-port");
        cmd.arg(format!("{bind_port}"));
        cmd.arg("-realm");
        cmd.arg(realm);
        cmd.arg("-users");
        cmd.arg(
            users
                .into_iter()
                .map(|(u, p)| format!("{u}={p}"))
                .collect::<Vec<_>>()
                .join(","),
        );

        tracing::info!("ABOUT TO SPAWN: {cmd:?}");

        let mut proc = cmd.spawn()?;
        drop(proc.stdin.take());
        let mut stderr = proc.stderr.take().unwrap();

        let mut output = Vec::new();
        let mut buf = [0; 4096];
        loop {
            use tokio::io::AsyncReadExt;

            let len = stderr.read(&mut buf).await?;
            if len == 0 {
                return Err(Error::id("BrokenPipe"));
            }
            output.extend_from_slice(&buf[0..len]);

            let s = String::from_utf8_lossy(&output);
            let mut i = s.split("#ICE#(");
            if i.next().is_some() {
                if let Some(s) = i.next() {
                    if s.contains(")#") {
                        let mut i = s.split(")#");
                        if let Some(s) = i.next() {
                            return Ok((s.to_string(), Self { proc }));
                        }
                    }
                }
            }
        }
    }

    /// Stop and clean up the TURN server sub-process.
    /// Note, a drop will attempt to clean up the process, but to be sure,
    /// use this function.
    pub async fn stop(mut self) -> Result<()> {
        // note, if the process already ended, kill may return an error.
        let _ = self.proc.kill().await;
        self.proc.wait().await?;
        Ok(())
    }
}

/// Construct an ephemeral turn server on an available port designed for
/// unit testing.
pub async fn test_turn_server() -> Result<(String, Tx5TurnServer)> {
    let mut addr = None;

    for iface in if_addrs::get_if_addrs()? {
        if iface.is_loopback() {
            continue;
        }
        if iface.ip().is_ipv6() {
            continue;
        }
        addr = Some(iface.ip());
        break;
    }

    if addr.is_none() {
        return Err(Error::id("NoLocalIp"));
    }

    let (turn, srv) = Tx5TurnServer::new(
        addr.unwrap(),
        0,
        vec![("test".into(), "test".into())],
        "holo.host".into(),
    )
    .await?;

    let turn = format!("{{\"urls\":[\"{turn}\"],\"username\":\"test\",\"credential\":\"test\"}}");

    Ok((turn, srv))
}

#[cfg(test)]
mod tests {
    use super::*;

    fn init_tracing() {
        let subscriber = tracing_subscriber::FmtSubscriber::builder()
            .with_env_filter(
                tracing_subscriber::filter::EnvFilter::from_default_env(),
            )
            .with_file(true)
            .with_line_number(true)
            .finish();
        let _ = tracing::subscriber::set_global_default(subscriber);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn sanity() {
        init_tracing();

        let (ice, srv) = test_turn_server().await.unwrap();

        println!("{}", ice);

        tokio::time::sleep(std::time::Duration::from_millis(300)).await;

        srv.stop().await.unwrap();
    }
}