diff --git a/phantun/src/bin/client.rs b/phantun/src/bin/client.rs index 8fb5278..65d81f9 100644 --- a/phantun/src/bin/client.rs +++ b/phantun/src/bin/client.rs @@ -2,38 +2,16 @@ use clap::{crate_version, Arg, Command}; use fake_tcp::packet::MAX_PACKET_LEN; use fake_tcp::{Socket, Stack}; use log::{debug, error, info}; +use phantun::utils::new_udp_reuseport; use std::collections::HashMap; -use std::convert::TryInto; use std::net::{Ipv4Addr, SocketAddr}; use std::sync::Arc; -use std::time::Duration; -use tokio::net::UdpSocket; use tokio::sync::{Notify, RwLock}; use tokio::time; use tokio_tun::TunBuilder; use tokio_util::sync::CancellationToken; -const UDP_TTL: Duration = Duration::from_secs(180); - -fn new_udp_reuseport(addr: SocketAddr) -> UdpSocket { - let udp_sock = socket2::Socket::new( - if addr.is_ipv4() { - socket2::Domain::IPV4 - } else { - socket2::Domain::IPV6 - }, - socket2::Type::DGRAM, - None, - ) - .unwrap(); - udp_sock.set_reuse_port(true).unwrap(); - // from tokio-rs/mio/blob/master/src/sys/unix/net.rs - udp_sock.set_cloexec(true).unwrap(); - udp_sock.set_nonblocking(true).unwrap(); - udp_sock.bind(&socket2::SockAddr::from(addr)).unwrap(); - let udp_sock: std::net::UdpSocket = udp_sock.into(); - udp_sock.try_into().unwrap() -} +use phantun::UDP_TTL; #[tokio::main] async fn main() { diff --git a/phantun/src/bin/server.rs b/phantun/src/bin/server.rs index 1ab2864..e7f8a9c 100644 --- a/phantun/src/bin/server.rs +++ b/phantun/src/bin/server.rs @@ -2,34 +2,16 @@ use clap::{crate_version, Arg, Command}; use fake_tcp::packet::MAX_PACKET_LEN; use fake_tcp::Stack; use log::{debug, error, info}; -use std::net::{Ipv4Addr, SocketAddr}; +use phantun::utils::new_udp_reuseport; +use std::net::Ipv4Addr; use std::sync::Arc; use tokio::net::UdpSocket; use tokio::sync::Notify; -use tokio::time::{self, Duration}; +use tokio::time; use tokio_tun::TunBuilder; use tokio_util::sync::CancellationToken; -const UDP_TTL: Duration = Duration::from_secs(180); -fn new_udp_reuseport(addr: SocketAddr) -> UdpSocket { - let udp_sock = socket2::Socket::new( - if addr.is_ipv4() { - socket2::Domain::IPV4 - } else { - socket2::Domain::IPV6 - }, - socket2::Type::DGRAM, - None, - ) - .unwrap(); - udp_sock.set_reuse_port(true).unwrap(); - // from tokio-rs/mio/blob/master/src/sys/unix/net.rs - udp_sock.set_cloexec(true).unwrap(); - udp_sock.set_nonblocking(true).unwrap(); - udp_sock.bind(&socket2::SockAddr::from(addr)).unwrap(); - let udp_sock: std::net::UdpSocket = udp_sock.into(); - udp_sock.try_into().unwrap() -} +use phantun::UDP_TTL; #[tokio::main] async fn main() { diff --git a/phantun/src/lib.rs b/phantun/src/lib.rs new file mode 100644 index 0000000..2e24c3f --- /dev/null +++ b/phantun/src/lib.rs @@ -0,0 +1,5 @@ +use std::time::Duration; + +pub mod utils; + +pub const UDP_TTL: Duration = Duration::from_secs(180); diff --git a/phantun/src/utils.rs b/phantun/src/utils.rs new file mode 100644 index 0000000..ff35ddf --- /dev/null +++ b/phantun/src/utils.rs @@ -0,0 +1,22 @@ +use std::net::SocketAddr; +use tokio::net::UdpSocket; + +pub fn new_udp_reuseport(local_addr: SocketAddr) -> UdpSocket { + let udp_sock = socket2::Socket::new( + if local_addr.is_ipv4() { + socket2::Domain::IPV4 + } else { + socket2::Domain::IPV6 + }, + socket2::Type::DGRAM, + None, + ) + .unwrap(); + udp_sock.set_reuse_port(true).unwrap(); + // from tokio-rs/mio/blob/master/src/sys/unix/net.rs + udp_sock.set_cloexec(true).unwrap(); + udp_sock.set_nonblocking(true).unwrap(); + udp_sock.bind(&socket2::SockAddr::from(local_addr)).unwrap(); + let udp_sock: std::net::UdpSocket = udp_sock.into(); + udp_sock.try_into().unwrap() +}