added support for linux

This commit is contained in:
alex 2026-04-23 10:40:47 +08:00
parent 12dda5d35c
commit 2101745c80
5 changed files with 31 additions and 16 deletions

View File

@ -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"] // "rust-analyzer.cargo.features": ["tun"]
} }

18
Cargo.lock generated
View File

@ -129,7 +129,7 @@ version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [ dependencies = [
"windows-sys 0.61.2", "windows-sys 0.60.2",
] ]
[[package]] [[package]]
@ -140,7 +140,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [ dependencies = [
"anstyle", "anstyle",
"once_cell_polyfill", "once_cell_polyfill",
"windows-sys 0.61.2", "windows-sys 0.60.2",
] ]
[[package]] [[package]]
@ -884,7 +884,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys 0.61.2", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -1869,7 +1869,7 @@ version = "0.50.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
dependencies = [ dependencies = [
"windows-sys 0.61.2", "windows-sys 0.59.0",
] ]
[[package]] [[package]]
@ -2614,7 +2614,7 @@ dependencies = [
"errno", "errno",
"libc", "libc",
"linux-raw-sys", "linux-raw-sys",
"windows-sys 0.61.2", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -2682,7 +2682,7 @@ dependencies = [
"security-framework", "security-framework",
"security-framework-sys", "security-framework-sys",
"webpki-root-certs", "webpki-root-certs",
"windows-sys 0.61.2", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -2965,7 +2965,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e" checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys 0.61.2", "windows-sys 0.60.2",
] ]
[[package]] [[package]]
@ -3329,7 +3329,7 @@ dependencies = [
"getrandom 0.4.2", "getrandom 0.4.2",
"once_cell", "once_cell",
"rustix", "rustix",
"windows-sys 0.61.2", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -4029,7 +4029,7 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [ dependencies = [
"windows-sys 0.61.2", "windows-sys 0.48.0",
] ]
[[package]] [[package]]

View File

@ -76,6 +76,11 @@ pub fn new_iface(tunname: &str, mode: Mode) -> Iface {
} }
impl Iface { impl Iface {
pub fn get_if_idx(&self) -> u32 {
0
}
#[allow(unused)] #[allow(unused)]
pub fn with_packet_info(ifname: &str, mode: Mode) -> Result<Self> { pub fn with_packet_info(ifname: &str, mode: Mode) -> Result<Self> {
Iface::open_tun(ifname, mode, true) Iface::open_tun(ifname, mode, true)

View File

@ -2,8 +2,6 @@ use std::fs::File;
use std::io::BufReader; use std::io::BufReader;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::net::UdpSocket; use std::net::UdpSocket;
use std::os::windows::io::FromRawSocket;
use std::os::windows::io::IntoRawSocket;
use std::path::Path; use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
@ -21,9 +19,15 @@ use rustls_pemfile::{certs, private_key};
use socket2::Domain; use socket2::Domain;
use socket2::Protocol; use socket2::Protocol;
use crate::get_default_interface;
use crate::set_unicast_if_v4; 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<Interface>) -> Endpoint { pub fn quic_init(iface: &Option<Interface>) -> Endpoint {
let default_provider = ring::default_provider(); let default_provider = ring::default_provider();
CryptoProvider::install_default(default_provider).unwrap(); CryptoProvider::install_default(default_provider).unwrap();
@ -67,8 +71,7 @@ pub fn quic_init(iface: &Option<Interface>) -> Endpoint {
set_unicast_if_v4(&socket2, iface); set_unicast_if_v4(&socket2, iface);
} }
let std_socket = unsafe { UdpSocket::from_raw_socket(socket2.into_raw_socket()) }; let std_socket = convert_to_std_socket(socket2);
std_socket.set_nonblocking(true);
rustls_config.alpn_protocols = vec![b"punchnet/1.0".to_vec()]; rustls_config.alpn_protocols = vec![b"punchnet/1.0".to_vec()];

View File

@ -4,7 +4,7 @@ use sdlan_sn_rs::{
utils::{Result, SDLanError}, utils::{Result, SDLanError},
}; };
use socket2::{Domain, Protocol, SockAddr}; 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 tokio::net::ToSocketAddrs;
use tracing::{debug, error}; 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<()> { pub fn set_unicast_if_v4(sock: &socket2::Socket, interface: &Interface) -> Result<()> {
use std::os::windows::io::AsRawSocket; use std::os::windows::io::AsRawSocket;
use windows_sys::Win32::Networking::WinSock::{setsockopt, IPPROTO_IP, IP_UNICAST_IF, SOCKET}; use windows_sys::Win32::Networking::WinSock::{setsockopt, IPPROTO_IP, IP_UNICAST_IF, SOCKET};