Added gateway address fetching message

modified:   src/client.rs
	modified:   src/server.rs
	modified:   src/udp.rs
This commit is contained in:
Michael Wain 2024-11-13 00:27:43 +03:00
parent 3fa76d6127
commit 0684364121
3 changed files with 54 additions and 6 deletions

View File

@ -24,7 +24,7 @@ pub mod general {
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
use std::pin::pin; use std::pin::pin;
use x25519_dalek::{PublicKey, StaticSecret}; use x25519_dalek::{PublicKey, StaticSecret};
use crate::udp::{UDPVpnPacket, UDPVpnHandshake, UDPSerializable}; use crate::udp::{UDPVpnPacket, UDPVpnHandshake, UDPSerializable, UDPVpnRouterIP};
use tun2::{platform::Device, Configuration, DeviceReader, DeviceWriter}; use tun2::{platform::Device, Configuration, DeviceReader, DeviceWriter};
trait ReadWrapper { trait ReadWrapper {
@ -219,6 +219,10 @@ pub mod general {
} }
}, // payload }, // payload
2 => { info!("Got keepalive packet"); }, 2 => { info!("Got keepalive packet"); },
3 => {
let router_packet = UDPVpnRouterIP::deserialize(&(buf1[..l].to_vec()));
// todo: set of the router ip
},
_ => { error!("Unexpected header value."); } _ => { error!("Unexpected header value."); }
} }
}, },

View File

@ -14,7 +14,7 @@ use network_interface::NetworkInterface;
use network_interface::NetworkInterfaceConfig; use network_interface::NetworkInterfaceConfig;
use crate::config::{ ServerConfiguration, ServerPeer}; use crate::config::{ ServerConfiguration, ServerPeer};
use crate::udp::{UDPKeepAlive, UDPSerializable, UDPVpnHandshake, UDPVpnPacket}; use crate::udp::{UDPKeepAlive, UDPSerializable, UDPVpnAskForIP, UDPVpnHandshake, UDPVpnPacket, UDPVpnRouterIP};
fn configure_routes(s_interface: Option<&str>) { fn configure_routes(s_interface: Option<&str>) {
let interfaces = NetworkInterface::show().unwrap(); let interfaces = NetworkInterface::show().unwrap();
@ -189,7 +189,7 @@ pub async fn server_mode(server_config: ServerConfiguration, s_interface: Option
Some(h) => { Some(h) => {
match h { match h {
0 => { 0 => {
let handshake = UDPVpnHandshake::deserialize(&buf); let handshake = UDPVpnHandshake::deserialize(&buf); // todo: replace &buf reference with length dependent reference.
info!("Got handshake from {:?}", handshake.request_ip); info!("Got handshake from {:?}", handshake.request_ip);
let skey = BASE64_STANDARD.encode(&handshake.public_key); let skey = BASE64_STANDARD.encode(&handshake.public_key);
if plp.iter().any(|c| c.ip == handshake.request_ip && c.public_key == skey) { if plp.iter().any(|c| c.ip == handshake.request_ip && c.public_key == skey) {
@ -227,6 +227,14 @@ pub async fn server_mode(server_config: ServerConfiguration, s_interface: Option
} }
}); });
}, // payload }, // payload
2 => { }, // got keepalive packet
3 => {
if let Ok(_packet) = UDPVpnAskForIP::deserialize(&(buf[..len].to_vec())) {
info!("Router address requested");
let response = UDPVpnRouterIP {router_ip: server_config.interface.internal_address.parse::<Ipv4Addr>().unwrap()};
let _ = send2hnd_ssr.send((response.serialize(), addr));
}
}, // fake router address request
_ => error!("Unexpected header value.") _ => error!("Unexpected header value.")
} }
}, },

View File

@ -1,15 +1,14 @@
use std::net::Ipv4Addr; use std::net::Ipv4Addr;
use chrono::{Timelike, Utc}; use chrono::{Timelike, Utc};
use rand::Rng;
pub struct UDPVpnPacket { pub struct UDPVpnPacket {
pub nonce: Vec<u8>, // [u8; 12] pub nonce: Vec<u8>, // [u8; 12]
pub data: Vec<u8> pub data: Vec<u8>
} }
pub struct UDPKeepAlive { pub struct UDPKeepAlive {}
}
impl UDPSerializable for UDPKeepAlive { impl UDPSerializable for UDPKeepAlive {
fn serialize(&self) -> Vec<u8> { fn serialize(&self) -> Vec<u8> {
@ -31,6 +30,43 @@ impl UDPVpnPacket {
} }
} }
pub struct UDPVpnRouterIP {
pub router_ip: Ipv4Addr // [u8; 4]
}
impl UDPSerializable for UDPVpnRouterIP {
fn serialize(&self) -> Vec<u8> {
let h: &[u8] = &[3];
[h, &self.router_ip.octets()].concat() // [u8; 5]
}
}
impl UDPVpnRouterIP {
pub fn deserialize(data: &Vec<u8>) -> Self {
UDPVpnRouterIP { router_ip: Ipv4Addr::new(data[1], data[2], data[3], data[4]) }
}
}
pub struct UDPVpnAskForIP {}
impl UDPVpnAskForIP {
pub fn deserialize(data: &Vec<u8>) -> Result<UDPVpnAskForIP, ()> {
if data.len() == 33 {
return Ok(UDPVpnAskForIP {});
}
Err(())
}
}
impl UDPSerializable for UDPVpnAskForIP {
fn serialize(&self) -> Vec<u8> {
let mut rng = rand::thread_rng();
let h: &[u8] = &[3];
let a: [u8; 32] = rng.gen();
[h, &a].concat()
}
}
pub struct UDPVpnHandshake { pub struct UDPVpnHandshake {
pub public_key: Vec<u8>, pub public_key: Vec<u8>,
pub request_ip: Ipv4Addr // [u8; 4] pub request_ip: Ipv4Addr // [u8; 4]