Skip to content

Commit

Permalink
Args class
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Apr 7, 2024
1 parent 8f56c3b commit 6da8a98
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
31 changes: 25 additions & 6 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct Args {

/// 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")]
#[arg(short, long, value_name = "name", conflicts_with = "tun_fd", value_parser = validate_tun)]
pub tun: Option<String>,

/// File descriptor of the tun interface
Expand All @@ -31,21 +31,21 @@ pub struct Args {
/// File descriptor for UNIX datagram socket meant to transfer
/// network sockets from global namespace to the new one.
/// See `unshare(1)`, `namespaces(7)`, `sendmsg(2)`, `unix(7)`.
#[arg(long)]
#[arg(long, value_name = "fd")]
pub socket_transfer_fd: Option<i32>,

/// Specify a command to run with root-like capabilities in the new namespace.
/// This could be useful to start additional daemons, e.g. `openvpn` instance.
#[arg(requires = "unshare")]
#[arg(long, value_name = "command", requires = "unshare")]
pub admin_command: Vec<OsString>,

/// IPv6 enabled
#[arg(short = '6', long)]
pub ipv6_enabled: bool,

#[arg(short, long)]
/// Routing and system setup, which decides whether to setup the routing and system configuration.
/// This option is only available on Linux and requires root-like privileges. See `capabilities(7)`.
#[arg(short, long, default_value = if cfg!(target_os = "linux") { "false" } else { "true" })]
pub setup: bool,

/// DNS handling strategy
Expand Down Expand Up @@ -73,8 +73,20 @@ pub struct Args {
pub verbosity: ArgVerbosity,
}

fn validate_tun(p: &str) -> Result<String> {
#[cfg(target_os = "macos")]
if p.len() <= 4 || &p[..4] != "utun" {
return Err(Error::from("Invalid tun interface name, please use utunX"));
}
Ok(p.to_string())
}

impl Default for Args {
fn default() -> Self {
#[cfg(target_os = "linux")]
let setup = false;
#[cfg(not(target_os = "linux"))]
let setup = true;
Args {
proxy: ArgProxy::default(),
tun: None,
Expand All @@ -83,7 +95,7 @@ impl Default for Args {
socket_transfer_fd: None,
admin_command: Vec::new(),
ipv6_enabled: false,
setup: false,
setup,
dns: ArgDns::default(),
dns_addr: "8.8.8.8".parse().unwrap(),
bypass: vec![],
Expand All @@ -95,9 +107,16 @@ impl Default for Args {
}

impl Args {
#[allow(clippy::let_and_return)]
pub fn parse_args() -> Self {
use clap::Parser;
Self::parse()
let args = Self::parse();
#[cfg(target_os = "linux")]
if !args.setup && args.tun.is_none() {
eprintln!("Missing required argument, '--tun' must present when '--setup' is not used.");
std::process::exit(-1);
}
args
}

pub fn proxy(&mut self, proxy: ArgProxy) -> &mut Self {
Expand Down
22 changes: 11 additions & 11 deletions src/desktop_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,25 @@ pub unsafe extern "C" fn tun2proxy_with_name_run(
pub async fn desktop_run_async(args: Args, shutdown_token: tokio_util::sync::CancellationToken) -> std::io::Result<()> {
let bypass_ips = args.bypass.clone();

let mut config = tun2::Configuration::default();
config.address(TUN_IPV4).netmask(TUN_NETMASK).mtu(MTU).up();
config.destination(TUN_GATEWAY);
let mut tun_config = tun2::Configuration::default();
tun_config.address(TUN_IPV4).netmask(TUN_NETMASK).mtu(MTU).up();
tun_config.destination(TUN_GATEWAY);
if let Some(tun_fd) = args.tun_fd {
config.raw_fd(tun_fd);
tun_config.raw_fd(tun_fd);
} else if let Some(ref tun) = args.tun {
config.tun_name(tun);
tun_config.tun_name(tun);
}

#[cfg(target_os = "linux")]
config.platform_config(|config| {
tun_config.platform_config(|cfg| {
#[allow(deprecated)]
config.packet_information(true);
config.ensure_root_privileges(args.setup);
cfg.packet_information(true);
cfg.ensure_root_privileges(args.setup);
});

#[cfg(target_os = "windows")]
config.platform_config(|config| {
config.device_guid(Some(12324323423423434234_u128));
tun_config.platform_config(|cfg| {
cfg.device_guid(Some(12324323423423434234_u128));
});

#[allow(unused_variables)]
Expand All @@ -113,7 +113,7 @@ pub async fn desktop_run_async(args: Args, shutdown_token: tokio_util::sync::Can
#[allow(unused_mut, unused_assignments, unused_variables)]
let mut setup = true;

let device = tun2::create_as_async(&config)?;
let device = tun2::create_as_async(&tun_config)?;

if let Ok(tun_name) = device.as_ref().tun_name() {
tproxy_args = tproxy_args.tun_name(&tun_name);
Expand Down

0 comments on commit 6da8a98

Please sign in to comment.