Changes to be committed:

modified:   src/client.rs
	modified:   src/main.rs
	modified:   src/server.rs
This commit is contained in:
Michael Wain 2024-08-19 17:02:07 +03:00
parent de77445372
commit 0e63a71629
3 changed files with 16 additions and 10 deletions

View File

@ -112,8 +112,7 @@ pub async fn client_mode(client_config: ClientConfiguration) {
}); });
let pkey = base64::decode(client_config.client.public_key).unwrap(); let pkey = base64::decode(client_config.client.public_key).unwrap();
info!("Handshake public_key: {:?}", pkey.len()); let handshake = UDPVpnHandshake{ public_key: pkey, request_ip: client_config.client.address.parse::<Ipv4Addr>().unwrap() };
let handshake = UDPVpnHandshake{ public_key: pkey, request_ip: client_config.client.address.parse::<Ipv4Addr>().unwrap().octets() };
sock_snd.send(&handshake.serialize()).await.unwrap(); sock_snd.send(&handshake.serialize()).await.unwrap();
loop { loop {

View File

@ -10,7 +10,6 @@ use serde_derive::Deserialize;
use std::str::FromStr; use std::str::FromStr;
use x25519_dalek::{StaticSecret, PublicKey}; use x25519_dalek::{StaticSecret, PublicKey};
use rand::{rngs::StdRng, SeedableRng}; use rand::{rngs::StdRng, SeedableRng};
use base64::prelude::*;
//mod tcp_client; //mod tcp_client;
//mod tcp_server; //mod tcp_server;
@ -49,13 +48,19 @@ impl UDPSerializable for UDPVpnPacket {
struct UDPVpnHandshake { struct UDPVpnHandshake {
public_key: Vec<u8>, public_key: Vec<u8>,
request_ip: [u8; 4] request_ip: Ipv4Addr // [u8; 4]
} }
impl UDPSerializable for UDPVpnHandshake { impl UDPSerializable for UDPVpnHandshake {
fn serialize(&self) -> Vec<u8> { fn serialize(&self) -> Vec<u8> {
let h: &[u8] = &[0]; let h: &[u8] = &[0];
[h, &self.public_key[..], &self.request_ip[..]].concat() [h, &self.public_key[..], &self.request_ip.octets()].concat()
}
}
impl UDPVpnHandshake {
fn deserialize(data: &Vec<u8>) -> Self {
UDPVpnHandshake { public_key: data[1..=32].to_vec(), request_ip: Ipv4Addr::new(data[33], data[34], data[35], data[36]) }
} }
} }
@ -106,8 +111,8 @@ impl ServerConfiguration {
ServerConfiguration { interface: ServerInterface { ServerConfiguration { interface: ServerInterface {
bind_address: String::from_str(bind_address).unwrap(), bind_address: String::from_str(bind_address).unwrap(),
internal_address: String::from_str(internal_address).unwrap(), internal_address: String::from_str(internal_address).unwrap(),
private_key: BASE64_STANDARD.encode(secret.as_bytes()), private_key: base64::encode(secret.as_bytes()),
public_key: BASE64_STANDARD.encode(PublicKey::from(&secret).as_bytes()), public_key: base64::encode(PublicKey::from(&secret).as_bytes()),
broadcast_mode, broadcast_mode,
keepalive keepalive
}, },
@ -157,8 +162,8 @@ impl ClientConfiguration {
let secret = StaticSecret::new(&mut csprng); let secret = StaticSecret::new(&mut csprng);
ClientConfiguration { ClientConfiguration {
client: ClientInterface { client: ClientInterface {
private_key: BASE64_STANDARD.encode(secret.as_bytes()), private_key: base64::encode(secret.as_bytes()),
public_key: BASE64_STANDARD.encode(PublicKey::from(&secret).as_bytes()), public_key: base64::encode(PublicKey::from(&secret).as_bytes()),
address: String::from_str(internal_address).unwrap() address: String::from_str(internal_address).unwrap()
}, },
server: EndpointInterface { server: EndpointInterface {

View File

@ -11,7 +11,7 @@ use std::collections::HashMap;
use tokio::io::AsyncReadExt; use tokio::io::AsyncReadExt;
use std::process::Command; use std::process::Command;
use crate::{ VpnPacket, ServerConfiguration, UDPSerializable, ServerPeer }; use crate::{ ServerConfiguration, ServerPeer, UDPSerializable, UDPVpnHandshake, VpnPacket };
pub async fn server_mode(server_config: ServerConfiguration) { pub async fn server_mode(server_config: ServerConfiguration) {
info!("Starting server..."); info!("Starting server...");
@ -77,6 +77,8 @@ pub async fn server_mode(server_config: ServerConfiguration) {
match h { match h {
0 => { 0 => {
// (&buf[1..len]).to_vec() // (&buf[1..len]).to_vec()
let handshake = UDPVpnHandshake::deserialize(&buf);
info!("Got handshake! ip: {:?}; key: {:?}", handshake.request_ip, base64::encode(handshake.public_key));
let internal_ip = IpAddr::V4(Ipv4Addr::new(10,8,0,2)); let internal_ip = IpAddr::V4(Ipv4Addr::new(10,8,0,2));
info!("Got handshake"); info!("Got handshake");
mp.insert(internal_ip, UDPeer { addr }); mp.insert(internal_ip, UDPeer { addr });