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 16:37:08 +03:00
parent c8f1835e1a
commit de77445372
3 changed files with 21 additions and 31 deletions

View File

@ -6,7 +6,7 @@ use std::io::{Read, Write};
use tun2::BoxError; use tun2::BoxError;
use log::{error, info, LevelFilter}; use log::{error, info, LevelFilter};
use std::sync::Arc; use std::sync::Arc;
use std::net::SocketAddr; use std::net::{ SocketAddr, Ipv4Addr };
use std::collections::HashMap; use std::collections::HashMap;
use std::process::Command; use std::process::Command;
use tokio::io::AsyncReadExt; use tokio::io::AsyncReadExt;
@ -111,7 +111,9 @@ pub async fn client_mode(client_config: ClientConfiguration) {
} }
}); });
let handshake = UDPVpnHandshake{ public_key: client_config.client.public_key.into_bytes() }; 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().octets() };
sock_snd.send(&handshake.serialize()).await.unwrap(); sock_snd.send(&handshake.serialize()).await.unwrap();
loop { loop {

View File

@ -10,7 +10,7 @@ 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; use base64::prelude::*;
//mod tcp_client; //mod tcp_client;
//mod tcp_server; //mod tcp_server;
@ -31,7 +31,7 @@ impl VpnPacket {
u64::from_be_bytes(d) u64::from_be_bytes(d)
} }
fn deserialize(d: Vec<u8>) -> Result<VpnPacket, Error> { fn deserialize(d: Vec<u8>) -> Result<Self, Error> {
Ok(VpnPacket{ data: d }) Ok(VpnPacket{ data: d })
} }
} }
@ -48,13 +48,14 @@ impl UDPSerializable for UDPVpnPacket {
} }
struct UDPVpnHandshake { struct UDPVpnHandshake {
public_key: Vec<u8> public_key: Vec<u8>,
request_ip: [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[..]].concat() [h, &self.public_key[..], &self.request_ip[..]].concat()
} }
} }
@ -73,7 +74,7 @@ struct ServerInterface {
} }
#[derive(Serialize, Deserialize, PartialEq, Debug)] #[derive(Serialize, Deserialize, PartialEq, Debug)]
struct ServerPeer { pub struct ServerPeer {
public_key: String, public_key: String,
ip: Ipv4Addr ip: Ipv4Addr
} }
@ -105,8 +106,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::encode(secret.as_bytes()), private_key: BASE64_STANDARD.encode(secret.as_bytes()),
public_key: base64::encode(PublicKey::from(&secret).as_bytes()), public_key: BASE64_STANDARD.encode(PublicKey::from(&secret).as_bytes()),
broadcast_mode, broadcast_mode,
keepalive keepalive
}, },
@ -156,8 +157,8 @@ impl ClientConfiguration {
let secret = StaticSecret::new(&mut csprng); let secret = StaticSecret::new(&mut csprng);
ClientConfiguration { ClientConfiguration {
client: ClientInterface { client: ClientInterface {
private_key: base64::encode(secret.as_bytes()), private_key: BASE64_STANDARD.encode(secret.as_bytes()),
public_key: base64::encode(PublicKey::from(&secret).as_bytes()), public_key: BASE64_STANDARD.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 }; use crate::{ VpnPacket, ServerConfiguration, UDPSerializable, ServerPeer };
pub async fn server_mode(server_config: ServerConfiguration) { pub async fn server_mode(server_config: ServerConfiguration) {
info!("Starting server..."); info!("Starting server...");
@ -34,6 +34,7 @@ pub async fn server_mode(server_config: ServerConfiguration) {
let sock_rec = Arc::new(sock); let sock_rec = Arc::new(sock);
let sock_snd = sock_rec.clone(); let sock_snd = sock_rec.clone();
let addresses = Arc::new(Mutex::new(HashMap::<IpAddr, UDPeer>::new())); let addresses = Arc::new(Mutex::new(HashMap::<IpAddr, UDPeer>::new()));
let peers = Arc::new(Mutex::new(Vec::<ServerPeer>::new()));
let (send2tun, recv2tun) = unbounded::<Vec<u8>>(); let (send2tun, recv2tun) = unbounded::<Vec<u8>>();
@ -65,10 +66,12 @@ pub async fn server_mode(server_config: ServerConfiguration) {
let mut buf = vec![0; 2048]; let mut buf = vec![0; 2048];
let addrs_lp = addresses.clone(); let addrs_lp = addresses.clone();
let peers_lp = peers.clone();
loop { loop {
if let Ok((len, addr)) = sock_rec.recv_from(&mut buf).await { if let Ok((len, addr)) = sock_rec.recv_from(&mut buf).await {
let mut mp = addrs_lp.lock().await; let mut mp = addrs_lp.lock().await;
let mut plp = peers_lp.lock().await;
match buf.first() { match buf.first() {
Some(h) => { Some(h) => {
match h { match h {
@ -88,6 +91,7 @@ pub async fn server_mode(server_config: ServerConfiguration) {
}, },
None => error!("There is no header") None => error!("There is no header")
} }
drop(plp);
drop(mp); drop(mp);
} }
} }
@ -96,20 +100,3 @@ pub async fn server_mode(server_config: ServerConfiguration) {
struct UDPeer { struct UDPeer {
addr: SocketAddr addr: SocketAddr
} }
/*struct WrappedUDP {
sock_rec: Arc<UdpSocket>,
sock_snd: Arc<UdpSocket>,
addresses: Arc<Mutex<HashMap<IpAddr, UDPeer>>>
}
impl WrappedUDP {
pub async fn new(addr: &str) -> Self {
WrappedUDP { sock_rec, sock_snd, addresses }
}
pub async fn init(&self) {
}
}*/