From a819b2039102eecc862d68add914b8ef9d5d6476 Mon Sep 17 00:00:00 2001 From: alterdekim Date: Sun, 17 Nov 2024 23:24:16 +0300 Subject: [PATCH] Wrapping, wrapping, wrapping. modified: Cargo.lock modified: Cargo.toml modified: src/client.rs modified: src/server.rs --- Cargo.lock | 62 +++------------------------------------------------ Cargo.toml | 5 ----- src/client.rs | 35 ++++++++++------------------- src/server.rs | 5 ----- 4 files changed, 15 insertions(+), 92 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5c84549..76cebe7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -602,33 +602,13 @@ version = "1.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "428d9aa8fbc0670b7b8d6030a7fadd0f86151cae55e4dbbece15f3780a3dfaf3" -[[package]] -name = "c2rust-bitfields" -version = "0.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b43c3f07ab0ef604fa6f595aa46ec2f8a22172c975e186f6f5bf9829a3b72c41" -dependencies = [ - "c2rust-bitfields-derive 0.18.0", -] - [[package]] name = "c2rust-bitfields" version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "367e5d1b30f28be590b6b3868da1578361d29d9bfac516d22f497d28ed7c9055" dependencies = [ - "c2rust-bitfields-derive 0.19.0", -] - -[[package]] -name = "c2rust-bitfields-derive" -version = "0.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3cbc102e2597c9744c8bd8c15915d554300601c91a079430d309816b0912545" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", + "c2rust-bitfields-derive", ] [[package]] @@ -1604,7 +1584,6 @@ dependencies = [ "tokio-util", "tray-item", "tun", - "tun2", "x25519-dalek", ] @@ -4574,29 +4553,7 @@ dependencies = [ "tokio", "tokio-util", "windows-sys 0.59.0", - "wintun-bindings 0.7.17", -] - -[[package]] -name = "tun2" -version = "2.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b139b40733b3e81560335ccad94948916c4d03ed1ded505a8675428879075b4" -dependencies = [ - "bytes", - "cfg-if", - "futures-core", - "ipnet", - "libc", - "libloading 0.8.5", - "log", - "nix", - "rustversion", - "thiserror 1.0.64", - "tokio", - "tokio-util", - "windows-sys 0.59.0", - "wintun-bindings 0.6.4", + "wintun-bindings", ] [[package]] @@ -5455,19 +5412,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "wintun-bindings" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3af47a132f449a64ff858f9ad876a3d1812df30e9500cddfdcabb2266ab68c0d" -dependencies = [ - "c2rust-bitfields 0.18.0", - "libloading 0.8.5", - "log", - "thiserror 1.0.64", - "windows-sys 0.59.0", -] - [[package]] name = "wintun-bindings" version = "0.7.17" @@ -5475,7 +5419,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8aed5bc5516ca7a52018b0a198911566edfa07584bdbfd71ea39161aa13806e" dependencies = [ "blocking", - "c2rust-bitfields 0.19.0", + "c2rust-bitfields", "futures", "libloading 0.8.5", "log", diff --git a/Cargo.toml b/Cargo.toml index b7a03ac..fdad83f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,11 +45,6 @@ base64 = "0.22.1" chrono = "0.4.38" console-subscriber = "0.4.0" network-interface = "2.0.0" - -[target.'cfg(target_os="linux")'.dependencies] -tun2 = { version = "2", features = ["async"] } - -[target.'cfg(not(target_os="linux"))'.dependencies] tun = { version = "0.7", features = ["async"] } [target.'cfg(target_os="windows")'.dependencies] diff --git a/src/client.rs b/src/client.rs index 9b48c51..c4ab07c 100644 --- a/src/client.rs +++ b/src/client.rs @@ -8,31 +8,24 @@ pub mod general { aead::{Aead, AeadCore, KeyInit, OsRng}, Aes256Gcm, Nonce}; use base64::prelude::*; - use std::sync::Arc; + use std::{io::{Read, Write}, sync::Arc}; use std::net::Ipv4Addr; use x25519_dalek::{PublicKey, StaticSecret}; use crate::udp::{UDPVpnPacket, UDPVpnHandshake, UDPSerializable}; - - #[cfg(not(target_os = "linux"))] - use tun::{DeviceReader, DeviceWriter}; - - #[cfg(target_os = "linux")] - use tun2::{DeviceReader, DeviceWriter}; pub trait ReadWrapper { async fn read(&mut self, buf: &mut [u8]) -> Result; } - pub struct DevReader { - pub dr: DeviceReader + pub struct DevReader where R: Read { + pub dr: R } // TODO: implement custom Error - impl ReadWrapper for DevReader { + impl ReadWrapper for DevReader { async fn read(&mut self, buf: &mut [u8]) -> Result { - let r = self.dr.read(buf).await; - if let Ok(a) = r { + if let Ok(a) = self.dr.read(buf) { return Ok(a); } Err(()) @@ -62,17 +55,17 @@ pub mod general { Gateway(Ipv4Addr) } - pub struct DevWriter { - pub dr: DeviceWriter, + pub struct DevWriter where W: Write { + pub dr: W, //pub dev: AsyncDevice } // TODO: implement custom Error - impl WriteWrapper for DevWriter { + impl WriteWrapper for DevWriter { async fn write(&mut self, msg: WriterMessage) -> Result { match msg { WriterMessage::Plain(buf) => { - if let Ok(a) = self.dr.write(&buf).await { + if let Ok(a) = self.dr.write(&buf) { return Ok(a); } Err(()) @@ -282,11 +275,7 @@ pub mod desktop { #[cfg(target_os = "linux")] use network_interface::{NetworkInterface, NetworkInterfaceConfig}; - #[cfg(target_os = "linux")] - use tun2::{ Configuration, create_as_async }; - - #[cfg(not(target_os = "linux"))] - use tun::{ Configuration, create_as_async }; + use tun::{ Configuration, create }; #[cfg(target_os = "linux")] @@ -372,9 +361,9 @@ pub mod desktop { sock.connect(&self.client_config.server.endpoint).await.unwrap(); info!("AsyncDevice"); - let dev = create_as_async(&config).unwrap(); + let dev = create(&config).unwrap(); info!("Split device"); - let (dev_writer, dev_reader) = dev.split().unwrap(); + let (dev_reader, dev_writer) = dev.split(); info!("CoreVpnClient"); let mut client = CoreVpnClient{ client_config: self.client_config.clone(), dev_reader: DevReader{ dr: dev_reader }, dev_writer: DevWriter{dr: dev_writer}, close_token: tokio_util::sync::CancellationToken::new()}; diff --git a/src/server.rs b/src/server.rs index 1ff3877..2237a12 100644 --- a/src/server.rs +++ b/src/server.rs @@ -9,11 +9,6 @@ use std::net::{ SocketAddr, Ipv4Addr, IpAddr }; use std::collections::HashMap; use aes_gcm::{ aead::{Aead, AeadCore, KeyInit, OsRng}, Aes256Gcm, Nonce }; - -#[cfg(target_os = "linux")] -use tun2::{Configuration, create_as_async}; - -#[cfg(not(target_os = "linux"))] use tun::{Configuration, create_as_async}; #[cfg(target_os = "linux")]