Skip to content

Commit

Permalink
wip: feat impl auto-route
Browse files Browse the repository at this point in the history
  • Loading branch information
Itsusinn committed Mar 28, 2024
1 parent 069cd27 commit cfa1775
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
2 changes: 2 additions & 0 deletions clash_lib/src/config/internal/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ pub struct TunConfig {
pub auto_route: Option<bool>,
/// fwmark for preveting loop
pub mark: Option<u32>,
/// ip rule table name
pub table: Option<String>
}

#[derive(Clone, Default)]
Expand Down
4 changes: 2 additions & 2 deletions clash_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ async fn start_async(opts: Options) -> Result<(), Error> {
let inbound_runner = inbound_manager.lock().await.get_runner()?;
let inbound_listener_handle = tokio::spawn(inbound_runner);

let tun_runner = get_tun_runner(config.tun, dispatcher.clone(), dns_resolver.clone())?;
let tun_runner = get_tun_runner(config.tun, dispatcher.clone(), dns_resolver.clone()).await?;
let tun_runner_handle = tun_runner.map(tokio::spawn);

debug!("initializing dns listener");
Expand Down Expand Up @@ -419,7 +419,7 @@ async fn start_async(opts: Options) -> Result<(), Error> {
.map(tokio::spawn)?;

let tun_runner_handle =
get_tun_runner(config.tun, dispatcher.clone(), dns_resolver.clone())?
get_tun_runner(config.tun, dispatcher.clone(), dns_resolver.clone()).await?
.map(tokio::spawn);

debug!("reloading dns listener");
Expand Down
20 changes: 20 additions & 0 deletions clash_lib/src/proxy/tun/auto_route.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::config::internal::config::TunConfig;

#[cfg(target_os = "linux")]
pub async fn setup(cfg: &mut TunConfig, tun_name: &str) -> anyhow::Result<()>{
if !cfg.auto_route.unwrap_or(false) {
return Ok(());
}
let mark = cfg.mark.unwrap_or(6969);
cfg.mark = Some(mark);
let table = cfg.table.take().unwrap_or("CLASH_RS".into());
cfg.table = Some(table);
// TODO
Ok(())
}

#[cfg(not(target_os = "linux"))]
pub fn setup(cfg: &mut TunConfig, tun_name: &str) -> anyhow::Result<()>{
tracing::error!("Auto route not impl!");
Ok(())
}
20 changes: 9 additions & 11 deletions clash_lib/src/proxy/tun/inbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
app::{dispatcher::Dispatcher, dns::ThreadSafeDNSResolver},
common::errors::map_io_error,
config::internal::config::TunConfig,
proxy::datagram::UdpPacket,
proxy::{datagram::UdpPacket, tun::auto_route},
session::{Network, Session, SocksAddr, Type},
Error, Runner,
};
Expand Down Expand Up @@ -121,7 +121,7 @@ async fn handle_inbound_datagram(
let _ = futures::future::join(fut1, fut2).await;
}

pub fn get_runner(
pub async fn get_runner(
mut cfg: TunConfig,
dispatcher: Arc<Dispatcher>,
resolver: ThreadSafeDNSResolver,
Expand All @@ -130,18 +130,11 @@ pub fn get_runner(
trace!("tun is disabled");
return Ok(None);
}
if cfg.auto_route.unwrap_or(false)
&& cfg.mark.is_none()
&& cfg!(any(target_os = "android", target_os = "linux"))
{
cfg.mark = Some(6969);
tracing::info!("tun.mark not set, using default {}", cfg.mark.unwrap());
}

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

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

let mut tun_cfg = tun::Configuration::default();

Expand Down Expand Up @@ -174,6 +167,11 @@ pub fn get_runner(
let tun_name = tun.get_ref().name().map_err(map_io_error)?;
info!("tun started at {}", tun_name);

// Configuare auto-route when tun is ready
if cfg.auto_route.unwrap_or(false) {
auto_route::setup(&mut cfg, &tun_name).await.map_err(|e| Error::Operation(e.to_string()))?;
}

let (stack, mut tcp_listener, udp_socket) =
netstack::NetStack::with_buffer_size(512, 256).map_err(map_io_error)?;

Expand Down
1 change: 1 addition & 0 deletions clash_lib/src/proxy/tun/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod inbound;
pub mod auto_route;
pub use netstack_lwip as netstack;
mod datagram;
pub use inbound::get_runner as get_tun_runner;

0 comments on commit cfa1775

Please sign in to comment.