Skip to content

Commit

Permalink
clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
ibigbug committed Aug 31, 2024
1 parent 5155fa1 commit 5717023
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 52 deletions.
58 changes: 7 additions & 51 deletions clash_lib/src/proxy/tun/inbound.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use super::{datagram::TunDatagram, netstack};
use std::{
net::{Ipv4Addr, SocketAddr},
sync::Arc,
};
use std::{net::SocketAddr, sync::Arc};

use futures::{SinkExt, StreamExt};
use ipnet::IpNet;
use network_interface::NetworkInterfaceConfig;

use tracing::{debug, error, info, trace, warn};
use tun::{Device, TunPacket};
use url::Url;
Expand All @@ -16,9 +12,8 @@ use crate::{
common::errors::{map_io_error, new_io_error},
config::internal::config::TunConfig,
proxy::{
datagram::UdpPacket,
tun::routes::add_route,
utils::{get_outbound_interface, OutboundInterface},
datagram::UdpPacket, tun::routes::maybe_add_routes,
utils::get_outbound_interface,
},
session::{Network, Session, SocksAddr, Type},
Error, Runner,
Expand Down Expand Up @@ -157,9 +152,9 @@ pub fn get_runner(
return Ok(None);
}

let device_id = cfg.device_id;
let device_id = &cfg.device_id;

let u = Url::parse(&device_id)
let u = Url::parse(device_id)
.map_err(|x| Error::InvalidConfig(format!("tun device {}", x)))?;

let mut tun_cfg = tun::Configuration::default();
Expand Down Expand Up @@ -195,46 +190,7 @@ pub fn get_runner(
let tun_name = tun.get_ref().name().map_err(map_io_error)?;
info!("tun started at {}", tun_name);

#[cfg(target_os = "windows")]
if cfg.route_all || !cfg.routes.is_empty() {
let tun_iface = network_interface::NetworkInterface::show()
.map_err(map_io_error)?
.into_iter()
.find(|iface| iface.name == tun_name)
.map(|x| OutboundInterface {
name: x.name,
addr_v4: x.addr.iter().find_map(|addr| match addr {
network_interface::Addr::V4(addr) => Some(addr.ip),
_ => None,
}),
addr_v6: x.addr.iter().find_map(|addr| match addr {
network_interface::Addr::V6(addr) => Some(addr.ip),
_ => None,
}),
index: x.index,
})
.expect("tun interface not found");

if cfg.route_all {
warn!(
"route_all is enabled, all traffic will be routed through the tun \
interface"
);
let default_routes = vec![
IpNet::new(std::net::IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 1)
.unwrap(),
IpNet::new(std::net::IpAddr::V4(Ipv4Addr::new(128, 0, 0, 0)), 1)
.unwrap(),
];
for r in default_routes {
add_route(&tun_iface, &r).map_err(map_io_error)?;
}
} else {
for r in cfg.routes {
add_route(&tun_iface, &r).map_err(map_io_error)?;
}
}
}
maybe_add_routes(&cfg, &tun_name)?;

let (stack, mut tcp_listener, udp_socket) =
netstack::NetStack::with_buffer_size(512, 256).map_err(map_io_error)?;
Expand Down
9 changes: 9 additions & 0 deletions clash_lib/src/proxy/tun/routes/linux.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use ipnet::IpNet;
use tracing::warn;

use crate::proxy::utils::OutboundInterface;

pub fn add_route(_: &OutboundInterface, _: &IpNet) -> std::io::Result<()> {
warn!("add_route is not implemented on Linux");
Ok(())
}
9 changes: 9 additions & 0 deletions clash_lib/src/proxy/tun/routes/macos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use ipnet::IpNet;
use tracing::warn;

use crate::proxy::utils::OutboundInterface;

pub fn add_route(_: &OutboundInterface, _: &IpNet) -> std::io::Result<()> {
warn!("add_route is not implemented on macOS");
Ok(())
}
73 changes: 72 additions & 1 deletion clash_lib/src/proxy/tun/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,75 @@
#[cfg(windows)]
mod windows;
#[cfg(windows)]
pub(crate) use windows::add_route;
use windows::add_route;

#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
use macos::add_route;

#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "linux")]
use linux::add_route;

#[cfg(not(any(windows, target_os = "macos", target_os = "linux")))]
mod other;
#[cfg(not(any(windows, target_os = "macos", target_os = "linux")))]
use other::add_route;

use std::net::Ipv4Addr;

use tracing::warn;

use crate::{
common::errors::map_io_error, config::internal::config::TunConfig,
proxy::utils::OutboundInterface,
};

use ipnet::IpNet;
use network_interface::NetworkInterfaceConfig;

pub fn maybe_add_routes(cfg: &TunConfig, tun_name: &str) -> std::io::Result<()> {
if cfg.route_all || !cfg.routes.is_empty() {
let tun_iface = network_interface::NetworkInterface::show()
.map_err(map_io_error)?
.into_iter()
.find(|iface| iface.name == tun_name)
.map(|x| OutboundInterface {
name: x.name,
addr_v4: x.addr.iter().find_map(|addr| match addr {
network_interface::Addr::V4(addr) => Some(addr.ip),
_ => None,
}),
addr_v6: x.addr.iter().find_map(|addr| match addr {
network_interface::Addr::V6(addr) => Some(addr.ip),
_ => None,
}),
index: x.index,
})
.expect("tun interface not found");

if cfg.route_all {
warn!(
"route_all is enabled, all traffic will be routed through the tun \
interface"
);
let default_routes = vec![
IpNet::new(std::net::IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 1)
.unwrap(),
IpNet::new(std::net::IpAddr::V4(Ipv4Addr::new(128, 0, 0, 0)), 1)
.unwrap(),
];
for r in default_routes {
add_route(&tun_iface, &r).map_err(map_io_error)?;
}
} else {
for r in &cfg.routes {
add_route(&tun_iface, r).map_err(map_io_error)?;
}
}
}

Ok(())
}
9 changes: 9 additions & 0 deletions clash_lib/src/proxy/tun/routes/other.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use ipnet::IpNet;
use tracing::warn;

use crate::proxy::utils::OutboundInterface;

pub fn add_route(_: &OutboundInterface, _: &IpNet) -> std::io::Result<()> {
warn!("add_route is not implemented on {}", std::env::consts::OS);
Ok(())
}

0 comments on commit 5717023

Please sign in to comment.