diff --git a/src/client.rs b/src/client.rs index 418c1c5..b43440a 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,4 +1,5 @@ use crossbeam_channel::unbounded; +use socket2::SockAddr; use tokio::{net::UdpSocket, sync::Mutex}; use std::{io::{Read, Write}, net::SocketAddr}; use base64::prelude::*; @@ -6,12 +7,15 @@ use log::{error, info, warn}; use std::sync::Arc; use std::net::Ipv4Addr; use x25519_dalek::{PublicKey, StaticSecret}; +use std::process::Command; use aes_gcm::{ aead::{Aead, AeadCore, KeyInit, OsRng}, Aes256Gcm, Nonce}; use crate::config::ClientConfiguration; use crate::udp::{UDPVpnPacket, UDPVpnHandshake, UDPSerializable}; +use network_interface::NetworkInterface; +use network_interface::NetworkInterfaceConfig; pub async fn client_mode(client_config: ClientConfiguration, fd: i32) { info!("Starting client..."); @@ -20,7 +24,6 @@ pub async fn client_mode(client_config: ClientConfiguration, fd: i32) { sock.connect(&client_config.server.endpoint).await.unwrap(); let mut config = tun2::Configuration::default(); - info!("address: {:?}", &client_config.client.address); config.raw_fd(fd).up(); let dev = tun2::create(&config).unwrap(); @@ -34,17 +37,17 @@ pub async fn client_mode(client_config: ClientConfiguration, fd: i32) { let cipher_shared = Arc::new(Mutex::new(None)); - let tun_writer_task = tokio::spawn(async move { + tokio::spawn(async move { while let Ok(bytes) = rx.recv() { - info!("Write to tun"); + info!("Write to tun."); dev_writer.write_all(&bytes).unwrap(); } }); - let tun_reader_task = tokio::spawn(async move { + tokio::spawn(async move { let mut buf = vec![0; 8192]; while let Ok(n) = dev_reader.read(&mut buf) { - info!("Read from tun"); + info!("Read from tun."); dx.send(buf[..n].to_vec()).unwrap(); } }); @@ -52,11 +55,12 @@ pub async fn client_mode(client_config: ClientConfiguration, fd: i32) { let priv_key = BASE64_STANDARD.decode(client_config.client.private_key).unwrap(); let cipher_shared_clone = cipher_shared.clone(); - let socket_reader_task = tokio::spawn(async move { + tokio::spawn(async move { let mut buf = vec![0; 4096]; loop { if let Ok(l) = sock_rec.recv(&mut buf).await { + info!("Read from socket"); let mut s_cipher = cipher_shared_clone.lock().await; match buf.first() { Some(h) => { @@ -98,41 +102,36 @@ pub async fn client_mode(client_config: ClientConfiguration, fd: i32) { } }); - let s_cipher = cipher_shared.clone(); let pkey = BASE64_STANDARD.decode(client_config.client.public_key).unwrap(); let handshake = UDPVpnHandshake{ public_key: pkey, request_ip: client_config.client.address.parse::().unwrap() }; + let mut nz = 0; + while nz < 25 { + sock_snd.send(&handshake.serialize()).await.unwrap(); + nz += 1 + } + //sock_snd.send(&handshake.serialize()).await.unwrap(); - let socket_writer_task = tokio::spawn(async move { - let mut nz = 0; - while nz < 25 { - sock_snd.send(&handshake.serialize()).await.unwrap(); - nz += 1 - } - //sock_snd.send(&handshake.serialize()).await.unwrap(); - - loop { - if let Ok(bytes) = mx.recv() { - let s_c = s_cipher.lock().await; + let s_cipher = cipher_shared.clone(); + loop { + if let Ok(bytes) = mx.recv() { + let s_c = s_cipher.lock().await; + + if s_c.is_some() { + let aes = Aes256Gcm::new(s_c.as_ref().unwrap().as_bytes().into()); + let nonce = Aes256Gcm::generate_nonce(&mut OsRng); + let ciphered_data = aes.encrypt(&nonce, &bytes[..]); - if s_c.is_some() { - let aes = Aes256Gcm::new(s_c.as_ref().unwrap().as_bytes().into()); - let nonce = Aes256Gcm::generate_nonce(&mut OsRng); - let ciphered_data = aes.encrypt(&nonce, &bytes[..]); - - if let Ok(ciphered_d) = ciphered_data { - let vpn_packet = UDPVpnPacket{ data: ciphered_d, nonce: nonce.to_vec()}; - let serialized_data = vpn_packet.serialize(); - info!("Sent to socket"); - sock_snd.send(&serialized_data).await.unwrap(); - } else { - error!("Socket encryption failed."); - } + if let Ok(ciphered_d) = ciphered_data { + let vpn_packet = UDPVpnPacket{ data: ciphered_d, nonce: nonce.to_vec()}; + let serialized_data = vpn_packet.serialize(); + info!("Write to socket"); + sock_snd.send(&serialized_data).await.unwrap(); } else { - warn!("There is no shared_secret in main loop"); + error!("Socket encryption failed."); } + } else { + warn!("There is no shared_secret in main loop"); } } - }); - - tokio::join!(tun_writer_task, tun_reader_task, socket_writer_task, socket_reader_task); + } } \ No newline at end of file