From 2101745c800252b8970aaefa7cd8ccb5f14167cd Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 23 Apr 2026 10:40:47 +0800 Subject: [PATCH] added support for linux --- .vscode/settings.json | 2 +- Cargo.lock | 18 +++++++++--------- src/network/tun_linux.rs | 5 +++++ src/quic/mod.rs | 13 ++++++++----- src/utils/socks.rs | 9 ++++++++- 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 9eb94d8..fb0827b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,4 @@ { - "rust-analyzer.cargo.target": "x86_64-pc-windows-gnu", + // "rust-analyzer.cargo.target": "x86_64-pc-windows-gnu", // "rust-analyzer.cargo.features": ["tun"] } \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index c6948f3..62e970b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -129,7 +129,7 @@ version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -140,7 +140,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" dependencies = [ "anstyle", "once_cell_polyfill", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -884,7 +884,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -1869,7 +1869,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -2614,7 +2614,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -2682,7 +2682,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -2965,7 +2965,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.60.2", ] [[package]] @@ -3329,7 +3329,7 @@ dependencies = [ "getrandom 0.4.2", "once_cell", "rustix", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -4029,7 +4029,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.48.0", ] [[package]] diff --git a/src/network/tun_linux.rs b/src/network/tun_linux.rs index 503b33c..74bdc4a 100755 --- a/src/network/tun_linux.rs +++ b/src/network/tun_linux.rs @@ -76,6 +76,11 @@ pub fn new_iface(tunname: &str, mode: Mode) -> Iface { } impl Iface { + + pub fn get_if_idx(&self) -> u32 { + 0 + } + #[allow(unused)] pub fn with_packet_info(ifname: &str, mode: Mode) -> Result { Iface::open_tun(ifname, mode, true) diff --git a/src/quic/mod.rs b/src/quic/mod.rs index 04560c8..b49bf80 100644 --- a/src/quic/mod.rs +++ b/src/quic/mod.rs @@ -2,8 +2,6 @@ use std::fs::File; use std::io::BufReader; use std::net::SocketAddr; use std::net::UdpSocket; -use std::os::windows::io::FromRawSocket; -use std::os::windows::io::IntoRawSocket; use std::path::Path; use std::sync::Arc; @@ -21,9 +19,15 @@ use rustls_pemfile::{certs, private_key}; use socket2::Domain; use socket2::Protocol; -use crate::get_default_interface; use crate::set_unicast_if_v4; +fn convert_to_std_socket(sock: socket2::Socket) -> std::net::UdpSocket { + let std_socket: std::net::UdpSocket = sock.into(); + std_socket.set_nonblocking(true); + std_socket +} + + pub fn quic_init(iface: &Option) -> Endpoint { let default_provider = ring::default_provider(); CryptoProvider::install_default(default_provider).unwrap(); @@ -67,8 +71,7 @@ pub fn quic_init(iface: &Option) -> Endpoint { set_unicast_if_v4(&socket2, iface); } - let std_socket = unsafe { UdpSocket::from_raw_socket(socket2.into_raw_socket()) }; - std_socket.set_nonblocking(true); + let std_socket = convert_to_std_socket(socket2); rustls_config.alpn_protocols = vec![b"punchnet/1.0".to_vec()]; diff --git a/src/utils/socks.rs b/src/utils/socks.rs index 00a1625..7d075e3 100755 --- a/src/utils/socks.rs +++ b/src/utils/socks.rs @@ -4,7 +4,7 @@ use sdlan_sn_rs::{ utils::{Result, SDLanError}, }; use socket2::{Domain, Protocol, SockAddr}; -use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4}; +use std::{net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4}, num::NonZeroU32}; use tokio::net::ToSocketAddrs; use tracing::{debug, error}; @@ -109,6 +109,13 @@ impl Socket { } } +#[cfg(not(target_os = "windows"))] +pub fn set_unicast_if_v4(sock: &socket2::Socket, interface: &Interface) -> Result<()> { + sock.bind_device_by_index_v4(NonZeroU32::new(interface.index))?; + Ok(()) +} + +#[cfg(target_os = "windows")] pub fn set_unicast_if_v4(sock: &socket2::Socket, interface: &Interface) -> Result<()> { use std::os::windows::io::AsRawSocket; use windows_sys::Win32::Networking::WinSock::{setsockopt, IPPROTO_IP, IP_UNICAST_IF, SOCKET};