Skip to content

Commit

Permalink
fix: wintun already add default route automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Feb 19, 2024
1 parent 1aad70e commit 1abf457
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 73 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build-nightly-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
./build-release -t ${{ matrix.target }} $compile_features $compile_compress
- name: Upload Artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.target }}
path: build/release/*
Expand Down Expand Up @@ -94,7 +94,7 @@ jobs:
./build/build-host-release -t ${{ matrix.target }}
- name: Upload Artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.target }}
path: build/release/*
Expand All @@ -119,7 +119,7 @@ jobs:
pwsh ./build/build-host-release.ps1
- name: Upload Artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: windows-native
path: build/release/*
3 changes: 2 additions & 1 deletion crates/shadowsocks-service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,8 @@ impl Config {
match tun_interface_destination.parse::<IpNet>() {
Ok(addr) => local_config.tun_interface_destination = Some(addr),
Err(..) => {
let err = Error::new(ErrorKind::Malformed, "`tun_interface_destination` invalid", None);
let err =
Error::new(ErrorKind::Malformed, "`tun_interface_destination` invalid", None);
return Err(err);
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks-service/src/local/tun/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl Tun {
);

// Set default route
if let Err(err) = sys::set_route_configuration(&self.device.get_ref()).await {
if let Err(err) = sys::set_route_configuration(self.device.get_mut()).await {
warn!("[TUN] tun device set route failed, error: {}", err);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ pub async fn write_packet_with_pi<W: AsyncWrite + Unpin>(writer: &mut W, packet:
}

/// Set platform specific route configuration
pub async fn set_route_configuration(_device: &TunDevice) -> io::Result<()> {
pub async fn set_route_configuration(_device: &mut TunDevice) -> io::Result<()> {
Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct rt_msg {
}

/// Set platform specific route configuration
pub async fn set_route_configuration(device: &TunDevice) -> io::Result<()> {
pub async fn set_route_configuration(device: &mut TunDevice) -> io::Result<()> {
let tun_address = match device.address() {
Ok(t) => t,
Err(err) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ use std::io;
use tun::platform::Device as TunDevice;

/// Set platform specific route configuration
pub async fn set_route_configuration(_device: &TunDevice) -> io::Result<()> {
pub async fn set_route_configuration(_device: &mut TunDevice) -> io::Result<()> {
Ok(())
}
2 changes: 1 addition & 1 deletion crates/shadowsocks-service/src/local/tun/sys/unix/bsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ pub async fn write_packet_with_pi<W: AsyncWrite + Unpin>(writer: &mut W, packet:
}

/// Set platform specific route configuration
pub async fn set_route_configuration(_device: &TunDevice) -> io::Result<()> {
pub async fn set_route_configuration(_device: &mut TunDevice) -> io::Result<()> {
Ok(())
}
2 changes: 1 addition & 1 deletion crates/shadowsocks-service/src/local/tun/sys/unix/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ pub async fn write_packet_with_pi<W: AsyncWrite + Unpin>(writer: &mut W, packet:
}

/// Set platform specific route configuration
pub async fn set_route_configuration(_device: &TunDevice) -> io::Result<()> {
pub async fn set_route_configuration(_device: &mut TunDevice) -> io::Result<()> {
Ok(())
}
65 changes: 2 additions & 63 deletions crates/shadowsocks-service/src/local/tun/sys/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
use std::{
io::{self, ErrorKind},
marker::Unpin,
mem,
};
use std::{io, marker::Unpin};

use log::{error, trace};
use tokio::io::{AsyncWrite, AsyncWriteExt};
use tun::{platform::Device as TunDevice, Device};
use windows_sys::Win32::{
Foundation::NO_ERROR,
NetworkManagement::IpHelper::{
CreateIpForwardEntry,
GetBestInterface,
MIB_IPFORWARDROW,
MIB_IPROUTE_TYPE_INDIRECT,
},
Networking::WinSock::MIB_IPPROTO_NETMGMT,
};

/// Packet Information length in bytes
///
Expand All @@ -31,52 +16,6 @@ pub async fn write_packet_with_pi<W: AsyncWrite + Unpin>(writer: &mut W, packet:
}

/// Set platform specific route configuration
pub async fn set_route_configuration(device: &TunDevice) -> io::Result<()> {
let tun_address = match device.address() {
Ok(t) => t,
Err(err) => {
error!("tun device doesn't have address, error: {}", err);
return Err(io::Error::new(ErrorKind::Other, err));
}
};

let tun_netmask = match device.netmask() {
Ok(m) => m,
Err(err) => {
error!("tun device doesn't have netmask, error: {}", err);
return Err(io::Error::new(ErrorKind::Other, err));
}
};

unsafe {
// https://learn.microsoft.com/en-us/windows/win32/api/ipmib/ns-ipmib-mib_ipforwardrow
let mut ipfrow: MIB_IPFORWARDROW = mem::zeroed();

ipfrow.dwForwardDest = u32::from(tun_address);
ipfrow.dwForwardMask = u32::from(tun_netmask);

// Get ifindex of this inteface
// https://learn.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getbestinterface
let mut if_index: u32 = 0;
let ret = GetBestInterface(ipfrow.dwForwardDest, &mut if_index);
if ret != NO_ERROR {
error!("GetBestInterface failed, ret: {}, destination: {}", ret, tun_address);
return Err(io::Error::new(ErrorKind::Other, format!("GetBestInterface {}", ret)));
}
ipfrow.dwForwardIfIndex = if_index;

ipfrow.Anonymous1.dwForwardType = MIB_IPROUTE_TYPE_INDIRECT as u32;
ipfrow.Anonymous2.dwForwardProto = MIB_IPPROTO_NETMGMT as u32;

let status = CreateIpForwardEntry(&ipfrow);
if status != NO_ERROR {
error!("CreateIpForwardEntry failed, status: {}", status);
return Err(io::Error::new(
ErrorKind::Other,
format!("CreateIpForwardEntry {}", status),
));
}
}

pub async fn set_route_configuration(_device: &mut TunDevice) -> io::Result<()> {
Ok(())
}

0 comments on commit 1abf457

Please sign in to comment.