-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tun): windows auto-route (#561)
- Loading branch information
Showing
15 changed files
with
387 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// https://stackoverflow.com/a/29963675/1109167 | ||
pub struct ScopeCall<F: FnOnce()> { | ||
pub c: Option<F>, | ||
} | ||
impl<F: FnOnce()> Drop for ScopeCall<F> { | ||
fn drop(&mut self) { | ||
self.c.take().unwrap()() | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! expr { | ||
($e:expr) => { | ||
$e | ||
}; | ||
} // tt hack | ||
|
||
#[macro_export] | ||
macro_rules! defer { | ||
($($data: tt)*) => ( | ||
let _scope_call = $crate::common::defer::ScopeCall { | ||
c: Some(|| -> () { $crate::expr!({ $($data)* }) }) | ||
}; | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod auth; | ||
pub mod crypto; | ||
pub mod defer; | ||
pub mod errors; | ||
pub mod geodata; | ||
pub mod http; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#[cfg(windows)] | ||
mod windows; | ||
#[cfg(windows)] | ||
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
Oops, something went wrong.