Changes to be committed:

modified:   src/server.rs
This commit is contained in:
Michael Wain 2024-08-11 02:51:06 +03:00
parent 8276be01f2
commit f99f6b4629

View File

@ -1,5 +1,6 @@
use tokio::{net::UdpSocket, sync::{mpsc, Mutex}}; use tokio::{net::UdpSocket, sync::{mpsc, Mutex}};
use std::{borrow::{Borrow, BorrowMut}, future::IntoFuture, io::{self, Read, Write}, net::{SocketAddr, Ipv4Addr, IpAddr}, sync::Arc, thread, time}; use tokio::task::JoinSet;
use std::{borrow::{Borrow, BorrowMut}, future::IntoFuture, io::{self, Read, Write}, net::{SocketAddr, Ipv4Addr, IpAddr}, sync::{Arc}, thread, time};
use std::process::Command; use std::process::Command;
use clap::{App, Arg}; use clap::{App, Arg};
use env_logger::Builder; use env_logger::Builder;
@ -9,7 +10,6 @@ use serde_derive::Serialize;
use serde_derive::Deserialize; use serde_derive::Deserialize;
//use packet::{builder::Builder, icmp, ip, Packet}; //use packet::{builder::Builder, icmp, ip, Packet};
use std::collections::HashMap; use std::collections::HashMap;
use futures::future;
pub async fn server_mode() { pub async fn server_mode() {
info!("Starting server..."); info!("Starting server...");
@ -26,10 +26,10 @@ pub async fn server_mode() {
let tun_device = Arc::new(Mutex::new(tun::create(&config).unwrap())); let tun_device = Arc::new(Mutex::new(tun::create(&config).unwrap()));
let sock = Arc::new(match UdpSocket::bind("192.168.0.5:8879".parse::<SocketAddr>().unwrap()).await { let sock = Arc::new(Mutex::new(match UdpSocket::bind("192.168.0.5:8879".parse::<SocketAddr>().unwrap()).await {
Ok(s) => s, Ok(s) => s,
Err(_error) => panic!("Cannot bind to address") Err(_error) => panic!("Cannot bind to address")
}); }));
let clients = Arc::new(Mutex::new(HashMap::new())); let clients = Arc::new(Mutex::new(HashMap::new()));
@ -61,70 +61,74 @@ pub async fn server_mode() {
let clients_clone = clients.clone(); let clients_clone = clients.clone();
let tun_device_clone_second = tun_device.clone(); let tun_device_clone_second = tun_device.clone();
let tasks = vec![ let mut set = JoinSet::new();
tokio::spawn(async move {
let mut buf = [0; 1024];
let sock_main_instance = sock_main;
loop {
let mut tun = tun_device_clone_second.lock().await;
let clients_main_instance = clients_main.lock().await;
let len = match tun.read(&mut buf) {
Ok(l) => l,
Err(error) => {
error!("Problem with reading from tun: {error:?}");
0
},
};
if len <= 0 { continue; } set.spawn(async move {
let mut buf = [0; 1024];
loop {
let mut tun = tun_device_clone_second.lock().await;
let len = match tun.read(&mut buf) {
Ok(l) => l,
Err(error) => {
error!("Problem with reading from tun: {error:?}");
0
},
};
info!("{:?} bytes received from tun", len); if len <= 0 { continue; }
match clients_main_instance.get(&"10.8.0.2") { info!("{:?} bytes received from tun", len);
Some(&addr) => { let sock_main_instance = sock_main.lock().await;
let len = match sock_main_instance.send_to(&buf, addr).await { let clients_main_instance = clients_main.lock().await;
Ok(l) => l, match clients_main_instance.get(&"10.8.0.2") {
Err(error) => {error!("Problem with writing to tun: {error:?}"); Some(&addr) => {
0}, sock_main_instance.send_to(&buf, addr);
}; info!("bytes sent to socket");
info!("{:?} bytes sent to socket", len); },
}, None => error!("There is no client..."),
None => error!("There is no client..."), }
}
});
set.spawn(async move {
let mut buf = [0; 1024];
loop {
let sock = sock_clone.lock().await;
let (len, addr) = match sock.recv_from(&mut buf).await {
Err(error) => {
error!("Problem with reading from socket: {error:?}");
(0, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0))
},
Ok(l) => l,
};
if len <= 0 { continue; }
let mut tun = tun_device_clone.lock().await;
let mut clients = clients_clone.lock().await;
clients.insert("10.8.0.2", addr);
info!("{:?} bytes received from {:?}", len, addr);
let len = match tun.write(&buf) {
Ok(l) => l,
Err(error) => {
error!("Problem with writing to tun: {error:?}");
0
} }
} };
}),
tokio::spawn(async move { info!("{:?} bytes sent to tun", len);
let mut buf = [0; 1024]; }
let sock = sock_clone; });
loop {
let mut tun = tun_device_clone.lock().await;
let mut clients = clients_clone.lock().await;
let (len, addr) = match sock.recv_from(&mut buf).await {
Err(error) => {
error!("Problem with reading from socket: {error:?}");
(0, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0))
},
Ok(l) => l,
};
if len <= 0 { continue; } /* let tasks = vec![
tokio::spawn(),
clients.insert("10.8.0.2", addr); tokio::spawn()
info!("{:?} bytes received from {:?}", len, addr);
let len = match tun.write(&buf) {
Ok(l) => l,
Err(error) => {
error!("Problem with writing to tun: {error:?}");
0
}
};
info!("{:?} bytes sent to tun", len);
}
})
]; ];
futures::future::join_all(tasks).await; futures::future::join_all(tasks).await;*/
while let Some(res) = set.join_next().await {}
} }