Changes to be committed:

modified:   src/main.rs
	modified:   src/tcp_client.rs
	modified:   src/tcp_server.rs
This commit is contained in:
Michael Wain 2024-08-12 18:43:15 +03:00
parent 4dac5c5794
commit 4b42c23b24
3 changed files with 18 additions and 13 deletions

View File

@ -13,16 +13,19 @@ use serde_derive::Deserialize;
mod tcp_client; mod tcp_client;
mod tcp_server; mod tcp_server;
const HEADER: [u8;3] = [0x56, 0x66, 0x76];
const TAIL: [u8;3] = [0x76, 0x66, 0x56];
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct VpnPacket { struct VpnPacket {
start: u8, start: Vec<u8>,
data: Vec<u8>, data: Vec<u8>,
end: u8 end: Vec<u8>
} }
impl VpnPacket { impl VpnPacket {
fn init(d: Vec<u8>) -> Self { fn init(d: Vec<u8>) -> Self {
VpnPacket{start: 0x56, data: d, end: 0x66} VpnPacket{start: (&HEADER).to_vec(), data: d, end: (&TAIL).to_vec()}
} }
} }

View File

@ -11,7 +11,7 @@ use std::collections::HashMap;
use std::process::Command; use std::process::Command;
use tokio::io::AsyncReadExt; use tokio::io::AsyncReadExt;
use crate::VpnPacket; use crate::{VpnPacket, HEADER, TAIL};
fn configure_routes() { fn configure_routes() {
let ip_output = Command::new("ip") let ip_output = Command::new("ip")
@ -92,18 +92,19 @@ pub async fn client_mode(remote_addr: String) {
}); });
tokio::spawn(async move { tokio::spawn(async move {
let mut buf = Vec::<u8>::new(); let mut buf = vec![0; 1024];
while let Ok(n) = dev_reader.read_to_end(&mut buf) { while let Ok(n) = dev_reader.read(&mut buf) {
dx.send(buf[..n].to_vec()).unwrap(); dx.send(buf[..n].to_vec()).unwrap();
} }
}); });
tokio::spawn(async move { tokio::spawn(async move {
let mut buf = Vec::<u8>::new(); let mut buf = vec![0; 1024];
loop { loop {
if let Ok(n) = sock_reader.read_to_end(&mut buf).await { if let Ok(n) = sock_reader.read(&mut buf).await {
//info!("Catch from socket: {:?}", &buf[..n]); //info!("Catch from socket: {:?}", &buf[..n]);
let vpn_packet: VpnPacket = bincode::deserialize(&buf[..n]).unwrap(); let vpn_packet: VpnPacket = bincode::deserialize(&buf[..n]).unwrap();
if vpn_packet.start != &HEADER || vpn_packet.end != &TAIL { error!("Bad packet"); continue; }
tx.send(vpn_packet.data).unwrap(); tx.send(vpn_packet.data).unwrap();
} }
} }

View File

@ -10,7 +10,7 @@ use std::net::SocketAddr;
use std::collections::HashMap; use std::collections::HashMap;
use tokio::io::AsyncReadExt; use tokio::io::AsyncReadExt;
use crate::VpnPacket; use crate::{VpnPacket, HEADER, TAIL};
pub async fn server_mode() { pub async fn server_mode() {
info!("Starting server..."); info!("Starting server...");
@ -38,8 +38,8 @@ pub async fn server_mode() {
}); });
tokio::spawn(async move { tokio::spawn(async move {
let mut buf = Vec::<u8>::new(); let mut buf = vec![0; 1024];
while let Ok(n) = dev_reader.read_to_end(&mut buf) { while let Ok(n) = dev_reader.read(&mut buf) {
dx.send(buf[..n].to_vec()).unwrap(); dx.send(buf[..n].to_vec()).unwrap();
} }
}); });
@ -64,11 +64,12 @@ pub async fn server_mode() {
}); });
tokio::spawn(async move { tokio::spawn(async move {
let mut buf = Vec::<u8>::new(); let mut buf = vec![0; 1024];
loop { loop {
if let Ok(n) = sock_reader.read_to_end(&mut buf).await { if let Ok(n) = sock_reader.read(&mut buf).await {
info!("Catched from sock: {:?}", &buf[..n]); info!("Catched from sock: {:?}", &buf[..n]);
let vpn_packet: VpnPacket = bincode::deserialize(&buf[..n]).unwrap(); let vpn_packet: VpnPacket = bincode::deserialize(&buf[..n]).unwrap();
if vpn_packet.start != &HEADER || vpn_packet.end != &TAIL { error!("Bad packet"); continue; }
thread_tx.send(vpn_packet.data).unwrap(); thread_tx.send(vpn_packet.data).unwrap();
} }
} }