Changes to be committed:
modified: Cargo.lock modified: Cargo.toml modified: src/client.rs modified: src/server.rs
This commit is contained in:
parent
149792eee5
commit
89ae8ad0f9
105
Cargo.lock
generated
105
Cargo.lock
generated
@ -312,6 +312,95 @@ dependencies = [
|
||||
"termcolor",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures"
|
||||
version = "0.3.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-executor",
|
||||
"futures-io",
|
||||
"futures-sink",
|
||||
"futures-task",
|
||||
"futures-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-channel"
|
||||
version = "0.3.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-sink",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-core"
|
||||
version = "0.3.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
|
||||
|
||||
[[package]]
|
||||
name = "futures-executor"
|
||||
version = "0.3.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-task",
|
||||
"futures-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-io"
|
||||
version = "0.3.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1"
|
||||
|
||||
[[package]]
|
||||
name = "futures-macro"
|
||||
version = "0.3.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.72",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-sink"
|
||||
version = "0.3.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5"
|
||||
|
||||
[[package]]
|
||||
name = "futures-task"
|
||||
version = "0.3.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004"
|
||||
|
||||
[[package]]
|
||||
name = "futures-util"
|
||||
version = "0.3.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48"
|
||||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-io",
|
||||
"futures-macro",
|
||||
"futures-sink",
|
||||
"futures-task",
|
||||
"memchr",
|
||||
"pin-project-lite",
|
||||
"pin-utils",
|
||||
"slab",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.14.7"
|
||||
@ -500,6 +589,12 @@ version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02"
|
||||
|
||||
[[package]]
|
||||
name = "pin-utils"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
|
||||
|
||||
[[package]]
|
||||
name = "polyval"
|
||||
version = "0.6.2"
|
||||
@ -627,6 +722,7 @@ dependencies = [
|
||||
"clap",
|
||||
"ctrlc",
|
||||
"env_logger",
|
||||
"futures",
|
||||
"generic-array",
|
||||
"log",
|
||||
"rand",
|
||||
@ -672,6 +768,15 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "slab"
|
||||
version = "0.4.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.13.2"
|
||||
|
@ -24,3 +24,4 @@ generic-array = "0.14"
|
||||
socket2 = "0.4"
|
||||
env_logger = "0.9"
|
||||
log = "0.4.20"
|
||||
futures = "0.3.30"
|
||||
|
@ -8,6 +8,51 @@ use tun::platform::Device;
|
||||
use serde_derive::Serialize;
|
||||
use serde_derive::Deserialize;
|
||||
|
||||
fn configure_routes() {
|
||||
let ip_output = Command::new("ip")
|
||||
.arg("addr")
|
||||
.arg("add")
|
||||
.arg("10.8.0.2/24")
|
||||
.arg("dev")
|
||||
.arg("tun0")
|
||||
.output()
|
||||
.expect("Failed to execute IP command");
|
||||
|
||||
if !ip_output.status.success() {
|
||||
eprintln!("Failed to set IP: {}", String::from_utf8_lossy(&ip_output.stderr));
|
||||
return;
|
||||
}
|
||||
|
||||
let link_output = Command::new("ip")
|
||||
.arg("link")
|
||||
.arg("set")
|
||||
.arg("up")
|
||||
.arg("dev")
|
||||
.arg("tun0")
|
||||
.output()
|
||||
.expect("Failed to execute IP LINK command");
|
||||
|
||||
if !link_output.status.success() {
|
||||
eprintln!("Failed to set link up: {}", String::from_utf8_lossy(&link_output.stderr));
|
||||
return;
|
||||
}
|
||||
|
||||
let route_output = Command::new("ip")
|
||||
.arg("route")
|
||||
.arg("add")
|
||||
.arg("0.0.0.0/0")
|
||||
.arg("via")
|
||||
.arg("10.8.0.1")
|
||||
.arg("dev")
|
||||
.arg("tun0")
|
||||
.output()
|
||||
.expect("Failed to execute IP ROUTE command");
|
||||
|
||||
if !route_output.status.success() {
|
||||
eprintln!("Failed to set route: {}", String::from_utf8_lossy(&route_output.stderr));
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn client_mode(remote_addr: &str) -> io::Result<()> {
|
||||
info!("Starting client...");
|
||||
|
||||
@ -23,6 +68,10 @@ pub async fn client_mode(remote_addr: &str) -> io::Result<()> {
|
||||
config.packet_information(true);
|
||||
});
|
||||
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
configure_routes();
|
||||
|
||||
let tun_device = Arc::new(Mutex::new(tun::create(&config).unwrap()));
|
||||
|
||||
let sock = Arc::new(Mutex::new(UdpSocket::bind("0.0.0.0:59611").await?));
|
||||
|
@ -9,8 +9,9 @@ use serde_derive::Serialize;
|
||||
use serde_derive::Deserialize;
|
||||
//use packet::{builder::Builder, icmp, ip, Packet};
|
||||
use std::collections::HashMap;
|
||||
use futures::future;
|
||||
|
||||
pub async fn server_mode() -> io::Result<()> {
|
||||
pub async fn server_mode() {
|
||||
info!("Starting server...");
|
||||
|
||||
let mut config = tun::Configuration::default();
|
||||
@ -25,7 +26,10 @@ pub async fn server_mode() -> io::Result<()> {
|
||||
|
||||
let tun_device = Arc::new(Mutex::new(tun::create(&config).unwrap()));
|
||||
|
||||
let sock = Arc::new(Mutex::new(UdpSocket::bind("192.168.0.5:8879".parse::<SocketAddr>().unwrap()).await?));
|
||||
let sock = Arc::new(match UdpSocket::bind("192.168.0.5:8879".parse::<SocketAddr>().unwrap()).await {
|
||||
Ok(s) => s,
|
||||
Err(_error) => panic!("Cannot bind to address")
|
||||
});
|
||||
|
||||
let clients = Arc::new(Mutex::new(HashMap::new()));
|
||||
|
||||
@ -48,19 +52,54 @@ pub async fn server_mode() -> io::Result<()> {
|
||||
}*/
|
||||
|
||||
let sock_main = sock.clone();
|
||||
let sock_main_instance = sock_main.lock().await;
|
||||
|
||||
let clients_main = clients.clone();
|
||||
let clients_main_instance = clients_main.lock().await;
|
||||
|
||||
|
||||
let tun_device_clone = tun_device.clone();
|
||||
let sock_clone = sock.clone();
|
||||
let clients_clone = clients.clone();
|
||||
let tun_device_clone_second = tun_device.clone();
|
||||
|
||||
let tasks = vec![
|
||||
tokio::spawn(async move {
|
||||
let mut buf = [0; 1024];
|
||||
let mut tun = tun_device_clone.lock().await;
|
||||
let sock = sock_clone.lock().await;
|
||||
let mut clients = clients_clone.lock().await;
|
||||
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; }
|
||||
|
||||
info!("{:?} bytes received from tun", len);
|
||||
|
||||
match clients_main_instance.get(&"10.8.0.2") {
|
||||
Some(&addr) => {
|
||||
let len = match sock_main_instance.send_to(&buf, addr).await {
|
||||
Ok(l) => l,
|
||||
Err(error) => {error!("Problem with writing to tun: {error:?}");
|
||||
0},
|
||||
};
|
||||
info!("{:?} bytes sent to socket", len);
|
||||
},
|
||||
None => error!("There is no client..."),
|
||||
}
|
||||
}
|
||||
}),
|
||||
|
||||
tokio::spawn(async move {
|
||||
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:?}");
|
||||
@ -84,30 +123,8 @@ pub async fn server_mode() -> io::Result<()> {
|
||||
|
||||
info!("{:?} bytes sent to tun", len);
|
||||
}
|
||||
});
|
||||
})
|
||||
];
|
||||
|
||||
let tun_device_clone_second = tun_device.clone();
|
||||
let mut buf = [0; 1024];
|
||||
let mut tun = tun_device_clone_second.lock().await;
|
||||
loop {
|
||||
let len = match tun.read(&mut buf) {
|
||||
Ok(l) => l,
|
||||
Err(error) => {
|
||||
error!("Problem with reading from tun: {error:?}");
|
||||
0
|
||||
},
|
||||
};
|
||||
|
||||
if len <= 0 { continue; }
|
||||
|
||||
info!("{:?} bytes received from tun", len);
|
||||
|
||||
match clients_main_instance.get(&"10.8.0.2") {
|
||||
Some(&addr) => {
|
||||
let len = sock_main_instance.send_to(&buf, addr).await?;
|
||||
info!("{:?} bytes sent to socket", len);
|
||||
},
|
||||
None => error!("There is no client..."),
|
||||
}
|
||||
}
|
||||
futures::future::join_all(tasks).await;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user