Skip to content

Commit

Permalink
change: tun & dns configuration on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
XOR-op committed Sep 15, 2024
1 parent dff5a7b commit 9f09319
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 29 deletions.
1 change: 1 addition & 0 deletions boltconn/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl App {
let tun_configure = Arc::new(std::sync::Mutex::new(TunConfigure::new(
fake_dns_server,
tun.get_name(),
&outbound_iface,
)));
if will_enable_tun {
tun_configure
Expand Down
10 changes: 8 additions & 2 deletions boltconn/src/network/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ use std::net::Ipv4Addr;
pub struct TunConfigure {
dns_addr: Ipv4Addr,
device_name: String,
outbound_name: String,
dns_handle: Option<SystemDnsHandle>,
routing_table_flag: bool,
}

impl TunConfigure {
pub fn new(dns_addr: Ipv4Addr, device_name: &str) -> Self {
pub fn new(dns_addr: Ipv4Addr, device_name: &str, outbound_name: &str) -> Self {
Self {
dns_addr,
device_name: device_name.to_string(),
outbound_name: outbound_name.to_string(),
dns_handle: None,
routing_table_flag: false,
}
Expand Down Expand Up @@ -46,7 +48,11 @@ impl TunConfigure {

fn enable_dns(&mut self) -> io::Result<()> {
if self.dns_handle.is_none() {
self.dns_handle = Some(SystemDnsHandle::new(self.dns_addr)?)
self.dns_handle = Some(SystemDnsHandle::new(
self.dns_addr,
&self.device_name,
&self.outbound_name,
)?)
}
Ok(())
}
Expand Down
31 changes: 7 additions & 24 deletions boltconn/src/network/windows_tun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,12 @@ impl TunInstance {
self.adapter.set_address(addr.addr()).map_err(|e| match e {
wintun::Error::Io(e) => e,
_ => io_err("Failed to set address"),
})
})?;
self.adapter
.set_netmask(addr.netmask())
.map_err(|e| match e {
wintun::Error::Io(e) => e,
_ => io_err("Failed to set netmask"),
})
}

// pub async fn send_outbound(pkt: &IPPkt, gw_name: &str, ipv6_enabled: bool) -> io::Result<()> {
// let addr = get_iface_address(gw_name)?;
// match pkt {
// IPPkt::V4(_) => {
// let sock = socket2::Socket::new(
// socket2::Domain::IPV4,
// socket2::Type::RAW,
// Some(socket2::Protocol::from(IPPROTO_IP.0)),
// )?;
// sock.bind(&SocketAddr::new(addr, 0).into())?;
// todo!()
// }
// IPPkt::V6(_) => {
// if ipv6_enabled {
// todo!()
// } else {
// tracing::trace!("Drop IPv6 packets: IPv6 disabled");
// }
// }
// }
// Ok(())
// }
}
2 changes: 1 addition & 1 deletion boltconn/src/platform/sys/linux_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub struct SystemDnsHandle {}
impl SystemDnsHandle {
const PATH: &'static str = "/tmp/fake_resolv.conf";
const RESOLV: &'static str = "/etc/resolv.conf";
pub fn new(ip: Ipv4Addr) -> io::Result<Self> {
pub fn new(ip: Ipv4Addr, _tun_name: &str, _outbound_name: &str) -> io::Result<Self> {
let mut output = File::create(Self::PATH).unwrap_or(
OpenOptions::new()
.read(true)
Expand Down
2 changes: 1 addition & 1 deletion boltconn/src/platform/sys/macos_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub struct SystemDnsHandle {
}

impl SystemDnsHandle {
pub fn new(ip: Ipv4Addr) -> io::Result<Self> {
pub fn new(ip: Ipv4Addr, _tun_name: &str, _outbound_name: &str) -> io::Result<Self> {
let services: Vec<String> =
get_command_output("networksetup", ["-listallnetworkservices"])?
.split('\n')
Expand Down
8 changes: 7 additions & 1 deletion boltconn/src/platform/sys/windows_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub struct SystemDnsHandle {
}

impl SystemDnsHandle {
pub fn new(dns_addr: Ipv4Addr) -> io::Result<Self> {
pub fn new(dns_addr: Ipv4Addr, _tun_name: &str, outbound_name: &str) -> io::Result<Self> {
// From https://github.com/dandyvica/resolver/blob/main/src/lib.rs
let mut list: Vec<DnsRecord> = Vec::new();

Expand Down Expand Up @@ -119,6 +119,12 @@ impl SystemDnsHandle {
let iface_name = (*p).FriendlyName.display().to_string();
let iface_index = (*p).Ipv6IfIndex;

// skip non-outbound interfaces
if !(iface_name == outbound_name) {
p = (*p).Next;
continue;
}

// now get all DNS ips for this interface
let mut ip_list: Vec<IpAddr> = Vec::new();
let mut p_dns = (*p).FirstDnsServerAddress;
Expand Down

0 comments on commit 9f09319

Please sign in to comment.