tun_name issues

This commit is contained in:
ssrlive 2024-02-25 16:59:18 +08:00
parent 1b859a5374
commit 5c228ca07e
4 changed files with 23 additions and 26 deletions

View file

@ -1,7 +1,6 @@
use crate::{Error, Result}; use crate::{Error, Result};
use socks5_impl::protocol::UserKey; use socks5_impl::protocol::UserKey;
use std::net::{IpAddr, SocketAddr, ToSocketAddrs}; use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
use tproxy_config::TUN_NAME;
#[derive(Debug, Clone, clap::Parser)] #[derive(Debug, Clone, clap::Parser)]
#[command(author, version, about = "Tunnel interface to proxy.", long_about = None)] #[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")] #[arg(short, long, value_parser = ArgProxy::from_url, value_name = "URL")]
pub proxy: ArgProxy, pub proxy: ArgProxy,
/// Name of the tun interface /// Name of the tun interface, such as tun0, utun4, etc.
#[arg(short, long, value_name = "name", conflicts_with = "tun_fd", default_value = TUN_NAME)] /// If this option is not provided, the OS will generate a random one.
pub tun: String, #[arg(short, long, value_name = "name", conflicts_with = "tun_fd")]
pub tun: Option<String>,
/// File descriptor of the tun interface /// File descriptor of the tun interface
#[arg(long, value_name = "fd", conflicts_with = "tun")] #[arg(long, value_name = "fd", conflicts_with = "tun")]
@ -25,8 +25,8 @@ pub struct Args {
pub ipv6_enabled: bool, pub ipv6_enabled: bool,
#[arg(short, long)] #[arg(short, long)]
/// Routing and system setup, which decides whether to setup the routing and system configuration, /// 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. /// This option is only available on Linux and requires root privileges.
pub setup: bool, pub setup: bool,
/// DNS handling strategy /// DNS handling strategy
@ -50,7 +50,7 @@ impl Default for Args {
fn default() -> Self { fn default() -> Self {
Args { Args {
proxy: ArgProxy::default(), proxy: ArgProxy::default(),
tun: TUN_NAME.to_string(), tun: None,
tun_fd: None, tun_fd: None,
ipv6_enabled: false, ipv6_enabled: false,
setup: false, setup: false,
@ -89,7 +89,7 @@ impl Args {
} }
pub fn tun(&mut self, tun: String) -> &mut Self { pub fn tun(&mut self, tun: String) -> &mut Self {
self.tun = tun; self.tun = Some(tun);
self self
} }

View file

@ -6,7 +6,7 @@ use crate::{
}; };
use std::os::raw::{c_char, c_int}; use std::os::raw::{c_char, c_int};
use tproxy_config::{TproxyArgs, TUN_GATEWAY, TUN_IPV4, TUN_NETMASK}; 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<Option<tokio_util::sync::CancellationToken>> = std::sync::Mutex::new(None); static TUN_QUIT: std::sync::Mutex<Option<tokio_util::sync::CancellationToken>> = 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); config.destination(TUN_GATEWAY);
if let Some(tun_fd) = args.tun_fd { if let Some(tun_fd) = args.tun_fd {
config.raw_fd(tun_fd); config.raw_fd(tun_fd);
} else { } else if let Some(ref tun) = args.tun {
config.name(&args.tun); config.tun_name(tun);
} }
#[cfg(target_os = "linux")] #[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) .tun_dns(args.dns_addr)
.proxy_addr(args.proxy.addr) .proxy_addr(args.proxy.addr)
.bypass_ips(&bypass_ips); .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)] #[allow(unused_mut, unused_assignments, unused_variables)]
let mut setup = true; let mut setup = true;
let device = tun2::create_as_async(&config)?; 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")] #[cfg(target_os = "linux")]
{ {
setup = args.setup; 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 { if setup {
log::trace!("Entering route setup");
tproxy_config::tproxy_setup(&tproxy_args)?; 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)); let join_handle = tokio::spawn(crate::run(device, MTU, args, shutdown_token));
join_handle.await.map_err(std::io::Error::from)??; join_handle.await.map_err(std::io::Error::from)??;

View file

@ -47,6 +47,8 @@ mod virtual_dns;
const DNS_PORT: u16 = 53; const DNS_PORT: u16 = 53;
const MAX_SESSIONS: u64 = 200; 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); static TASK_COUNT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
use std::sync::atomic::Ordering::Relaxed; use std::sync::atomic::Ordering::Relaxed;
@ -61,6 +63,8 @@ pub async fn run<D>(device: D, mtu: u16, args: Args, shutdown_token: Cancellatio
where where
D: AsyncRead + AsyncWrite + Unpin + Send + 'static, D: AsyncRead + AsyncWrite + Unpin + Send + 'static,
{ {
log::info!("Proxy {} server: {}", args.proxy.proxy_type, args.proxy.addr);
let server_addr = args.proxy.addr; let server_addr = args.proxy.addr;
let key = args.proxy.credentials.clone(); let key = args.proxy.credentials.clone();
let dns_addr = args.dns_addr; let dns_addr = args.dns_addr;
@ -80,8 +84,8 @@ where
let mut ipstack_config = ipstack::IpStackConfig::default(); let mut ipstack_config = ipstack::IpStackConfig::default();
ipstack_config.mtu(mtu); ipstack_config.mtu(mtu);
ipstack_config.tcp_timeout(std::time::Duration::from_secs(600)); // 10 minutes ipstack_config.tcp_timeout(std::time::Duration::from_secs(TCP_TIMEOUT_SEC));
ipstack_config.udp_timeout(std::time::Duration::from_secs(10)); // 10 seconds ipstack_config.udp_timeout(std::time::Duration::from_secs(UDP_TIMEOUT_SEC));
let mut ip_stack = ipstack::IpStack::new(ipstack_config, device); let mut ip_stack = ipstack::IpStack::new(ipstack_config, device);

View file

@ -23,8 +23,6 @@ pub fn mobile_run(args: Args, tun_mtu: u16) -> c_int {
} }
let block = async move { let block = async move {
log::info!("Proxy {} server: {}", args.proxy.proxy_type, args.proxy.addr);
let mut config = tun2::Configuration::default(); let mut config = tun2::Configuration::default();
#[cfg(unix)] #[cfg(unix)]