From 1abf4579070a71b8c5846a5a93aa0a45abfa46e4 Mon Sep 17 00:00:00 2001 From: zonyitoo Date: Mon, 19 Feb 2024 22:17:08 +0800 Subject: [PATCH] fix: wintun already add default route automatically --- .github/workflows/build-nightly-release.yml | 6 +- crates/shadowsocks-service/src/config.rs | 3 +- .../shadowsocks-service/src/local/tun/mod.rs | 2 +- .../src/local/tun/sys/unix/android.rs | 2 +- .../src/local/tun/sys/unix/apple/macos.rs | 2 +- .../src/local/tun/sys/unix/apple/others.rs | 2 +- .../src/local/tun/sys/unix/bsd.rs | 2 +- .../src/local/tun/sys/unix/linux.rs | 2 +- .../src/local/tun/sys/windows/mod.rs | 65 +------------------ 9 files changed, 13 insertions(+), 73 deletions(-) diff --git a/.github/workflows/build-nightly-release.yml b/.github/workflows/build-nightly-release.yml index 1730adf44634..26b34b89c928 100644 --- a/.github/workflows/build-nightly-release.yml +++ b/.github/workflows/build-nightly-release.yml @@ -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/* @@ -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/* @@ -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/* diff --git a/crates/shadowsocks-service/src/config.rs b/crates/shadowsocks-service/src/config.rs index 1bddeb4e706c..c02c2f0cb0bb 100644 --- a/crates/shadowsocks-service/src/config.rs +++ b/crates/shadowsocks-service/src/config.rs @@ -1630,7 +1630,8 @@ impl Config { match tun_interface_destination.parse::() { 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); } } diff --git a/crates/shadowsocks-service/src/local/tun/mod.rs b/crates/shadowsocks-service/src/local/tun/mod.rs index d0edd95adf5a..7d4a91740dcb 100644 --- a/crates/shadowsocks-service/src/local/tun/mod.rs +++ b/crates/shadowsocks-service/src/local/tun/mod.rs @@ -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); } diff --git a/crates/shadowsocks-service/src/local/tun/sys/unix/android.rs b/crates/shadowsocks-service/src/local/tun/sys/unix/android.rs index 23f87149b697..a7fb183f7722 100644 --- a/crates/shadowsocks-service/src/local/tun/sys/unix/android.rs +++ b/crates/shadowsocks-service/src/local/tun/sys/unix/android.rs @@ -16,6 +16,6 @@ pub async fn write_packet_with_pi(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(()) } diff --git a/crates/shadowsocks-service/src/local/tun/sys/unix/apple/macos.rs b/crates/shadowsocks-service/src/local/tun/sys/unix/apple/macos.rs index 5c19ec01f0ef..87a079f2d325 100644 --- a/crates/shadowsocks-service/src/local/tun/sys/unix/apple/macos.rs +++ b/crates/shadowsocks-service/src/local/tun/sys/unix/apple/macos.rs @@ -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) => { diff --git a/crates/shadowsocks-service/src/local/tun/sys/unix/apple/others.rs b/crates/shadowsocks-service/src/local/tun/sys/unix/apple/others.rs index 7c28f6b39e65..7ef4be546805 100644 --- a/crates/shadowsocks-service/src/local/tun/sys/unix/apple/others.rs +++ b/crates/shadowsocks-service/src/local/tun/sys/unix/apple/others.rs @@ -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(()) } diff --git a/crates/shadowsocks-service/src/local/tun/sys/unix/bsd.rs b/crates/shadowsocks-service/src/local/tun/sys/unix/bsd.rs index 67789723d804..5ba7f41c018a 100644 --- a/crates/shadowsocks-service/src/local/tun/sys/unix/bsd.rs +++ b/crates/shadowsocks-service/src/local/tun/sys/unix/bsd.rs @@ -54,6 +54,6 @@ pub async fn write_packet_with_pi(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(()) } diff --git a/crates/shadowsocks-service/src/local/tun/sys/unix/linux.rs b/crates/shadowsocks-service/src/local/tun/sys/unix/linux.rs index 15447d3a353a..4d710e3c5ed4 100644 --- a/crates/shadowsocks-service/src/local/tun/sys/unix/linux.rs +++ b/crates/shadowsocks-service/src/local/tun/sys/unix/linux.rs @@ -16,6 +16,6 @@ pub async fn write_packet_with_pi(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(()) } diff --git a/crates/shadowsocks-service/src/local/tun/sys/windows/mod.rs b/crates/shadowsocks-service/src/local/tun/sys/windows/mod.rs index 2f5d136bc72c..332de0bec386 100644 --- a/crates/shadowsocks-service/src/local/tun/sys/windows/mod.rs +++ b/crates/shadowsocks-service/src/local/tun/sys/windows/mod.rs @@ -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 /// @@ -31,52 +16,6 @@ pub async fn write_packet_with_pi(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(()) }