port_mapping/
lib.rs

1use socket2::{Domain, Socket, Type};
2use tokio::{fs::File, io::BufReader};
3
4pub mod mapping_rule;
5pub mod tcp_proxy;
6pub mod udp_proxy;
7
8pub fn get_udp_buffer_sizes() -> std::io::Result<usize> {
9    let socket = Socket::new(Domain::IPV4, Type::DGRAM, None)?;
10    Ok(socket.recv_buffer_size()?)
11}
12
13pub async fn get_mapping_file() -> std::io::Result<BufReader<File>> {
14    let file = File::open("mapping.txt").await;
15    Ok(BufReader::new(match file {
16        Ok(file) => file,
17        Err(_) => {
18            let exe_path = std::env::current_exe()?;
19            let dir = exe_path.parent().unwrap();
20            let mapping_path = dir.join("mapping.txt");
21            File::open(&mapping_path).await?
22        }
23    }))
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29
30    #[test]
31    fn test_get_udp_buffer_sizes() {
32        let result = get_udp_buffer_sizes();
33        assert!(result.is_ok());
34        let buffer_size = result.unwrap();
35        assert!(buffer_size > 0);
36    }
37}