diff --git a/src/args.rs b/src/args.rs index eb60b90..165bd58 100644 --- a/src/args.rs +++ b/src/args.rs @@ -1,7 +1,6 @@ use crate::{Error, Result}; use socks5_impl::protocol::UserKey; use std::net::{IpAddr, SocketAddr, ToSocketAddrs}; -use tproxy_config::TUN_NAME; #[derive(Debug, Clone, clap::Parser)] #[command(author, version, about = "Tunnel interface to proxy.", long_about = None)] @@ -12,9 +11,10 @@ pub struct Args { #[arg(short, long, value_parser = ArgProxy::from_url, value_name = "URL")] pub proxy: ArgProxy, - /// Name of the tun interface - #[arg(short, long, value_name = "name", conflicts_with = "tun_fd", default_value = TUN_NAME)] - pub tun: String, + /// Name of the tun interface, such as tun0, utun4, etc. + /// If this option is not provided, the OS will generate a random one. + #[arg(short, long, value_name = "name", conflicts_with = "tun_fd")] + pub tun: Option, /// File descriptor of the tun interface #[arg(long, value_name = "fd", conflicts_with = "tun")] @@ -25,8 +25,8 @@ pub struct Args { pub ipv6_enabled: bool, #[arg(short, long)] - /// Routing and system setup, which decides whether to setup the routing and system configuration, - /// this option requires root privileges. This option is only available on Linux. + /// Routing and system setup, which decides whether to setup the routing and system configuration. + /// This option is only available on Linux and requires root privileges. pub setup: bool, /// DNS handling strategy @@ -50,7 +50,7 @@ impl Default for Args { fn default() -> Self { Args { proxy: ArgProxy::default(), - tun: TUN_NAME.to_string(), + tun: None, tun_fd: None, ipv6_enabled: false, setup: false, @@ -89,7 +89,7 @@ impl Args { } pub fn tun(&mut self, tun: String) -> &mut Self { - self.tun = tun; + self.tun = Some(tun); self } diff --git a/src/desktop_api.rs b/src/desktop_api.rs index 5c66dca..4763a5f 100644 --- a/src/desktop_api.rs +++ b/src/desktop_api.rs @@ -6,7 +6,7 @@ use crate::{ }; use std::os::raw::{c_char, c_int}; use tproxy_config::{TproxyArgs, TUN_GATEWAY, TUN_IPV4, TUN_NETMASK}; -use tun2::DEFAULT_MTU as MTU; +use tun2::{AbstractDevice, DEFAULT_MTU as MTU}; static TUN_QUIT: std::sync::Mutex> = std::sync::Mutex::new(None); @@ -84,8 +84,8 @@ pub async fn desktop_run_async(args: Args, shutdown_token: tokio_util::sync::Can config.destination(TUN_GATEWAY); if let Some(tun_fd) = args.tun_fd { config.raw_fd(tun_fd); - } else { - config.name(&args.tun); + } else if let Some(ref tun) = args.tun { + config.tun_name(tun); } #[cfg(target_os = "linux")] @@ -105,32 +105,27 @@ pub async fn desktop_run_async(args: Args, shutdown_token: tokio_util::sync::Can .tun_dns(args.dns_addr) .proxy_addr(args.proxy.addr) .bypass_ips(&bypass_ips); - #[allow(unused_assignments)] - if args.tun_fd.is_none() { - tproxy_args = tproxy_args.tun_name(&args.tun); - } #[allow(unused_mut, unused_assignments, unused_variables)] let mut setup = true; let device = tun2::create_as_async(&config)?; + if let Ok(tun_name) = device.as_ref().tun_name() { + tproxy_args = tproxy_args.tun_name(&tun_name); + } + #[cfg(target_os = "linux")] { setup = args.setup; - if setup { - log::trace!("Entering route setup"); - tproxy_config::tproxy_setup(&tproxy_args)?; - } } - #[cfg(any(target_os = "windows", target_os = "macos"))] + #[cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))] if setup { + log::trace!("Entering route setup"); tproxy_config::tproxy_setup(&tproxy_args)?; } - log::info!("Proxy {} server: {}", args.proxy.proxy_type, args.proxy.addr); - let join_handle = tokio::spawn(crate::run(device, MTU, args, shutdown_token)); join_handle.await.map_err(std::io::Error::from)??; diff --git a/src/lib.rs b/src/lib.rs index 283a74c..ce816db 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,6 +47,8 @@ mod virtual_dns; const DNS_PORT: u16 = 53; const MAX_SESSIONS: u64 = 200; +const TCP_TIMEOUT_SEC: u64 = 600; // 10 minutes +const UDP_TIMEOUT_SEC: u64 = 10; // 10 seconds static TASK_COUNT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); use std::sync::atomic::Ordering::Relaxed; @@ -61,6 +63,8 @@ pub async fn run(device: D, mtu: u16, args: Args, shutdown_token: Cancellatio where D: AsyncRead + AsyncWrite + Unpin + Send + 'static, { + log::info!("Proxy {} server: {}", args.proxy.proxy_type, args.proxy.addr); + let server_addr = args.proxy.addr; let key = args.proxy.credentials.clone(); let dns_addr = args.dns_addr; @@ -80,8 +84,8 @@ where let mut ipstack_config = ipstack::IpStackConfig::default(); ipstack_config.mtu(mtu); - ipstack_config.tcp_timeout(std::time::Duration::from_secs(600)); // 10 minutes - ipstack_config.udp_timeout(std::time::Duration::from_secs(10)); // 10 seconds + ipstack_config.tcp_timeout(std::time::Duration::from_secs(TCP_TIMEOUT_SEC)); + ipstack_config.udp_timeout(std::time::Duration::from_secs(UDP_TIMEOUT_SEC)); let mut ip_stack = ipstack::IpStack::new(ipstack_config, device); diff --git a/src/mobile_api.rs b/src/mobile_api.rs index dabfa84..bf2573c 100644 --- a/src/mobile_api.rs +++ b/src/mobile_api.rs @@ -23,8 +23,6 @@ pub fn mobile_run(args: Args, tun_mtu: u16) -> c_int { } let block = async move { - log::info!("Proxy {} server: {}", args.proxy.proxy_type, args.proxy.addr); - let mut config = tun2::Configuration::default(); #[cfg(unix)]