Changes to be committed:

modified:   src/client.rs
	modified:   src/server.rs
This commit is contained in:
Michael Wain 2024-08-24 03:40:29 +03:00
parent faa1086e78
commit 5831be033b
2 changed files with 6 additions and 10 deletions

View File

@ -88,7 +88,7 @@ pub async fn client_mode(client_config: ClientConfiguration) {
tokio::spawn(async move { tokio::spawn(async move {
while let Ok(bytes) = rx.recv() { while let Ok(bytes) = rx.recv() {
info!("Write to tun {:?}", hex::encode(&bytes)); //info!("Write to tun {:?}", hex::encode(&bytes));
dev_writer.write_all(&bytes).unwrap(); dev_writer.write_all(&bytes).unwrap();
} }
}); });
@ -124,7 +124,6 @@ pub async fn client_mode(client_config: ClientConfiguration) {
} }
*s_cipher = Some(StaticSecret::from(k1) *s_cipher = Some(StaticSecret::from(k1)
.diffie_hellman(&PublicKey::from(k))); .diffie_hellman(&PublicKey::from(k)));
// Aes256Gcm::new(shared_secret.as_bytes().into());
}, // handshake }, // handshake
1 => { 1 => {
let wrapped_packet = UDPVpnPacket::deserialize(&(buf[..l].to_vec())); let wrapped_packet = UDPVpnPacket::deserialize(&(buf[..l].to_vec()));
@ -162,13 +161,11 @@ pub async fn client_mode(client_config: ClientConfiguration) {
if s_c.is_some() { if s_c.is_some() {
let aes = Aes256Gcm::new(s_c.as_ref().unwrap().as_bytes().into()); let aes = Aes256Gcm::new(s_c.as_ref().unwrap().as_bytes().into());
let nonce = Aes256Gcm::generate_nonce(&mut OsRng); let nonce = Aes256Gcm::generate_nonce(&mut OsRng);
info!("Key {:?} / nonce {:?}", s_c.as_ref().unwrap().as_bytes(), &nonce.bytes());
let ciphered_data = aes.encrypt(&nonce, &bytes[..]); let ciphered_data = aes.encrypt(&nonce, &bytes[..]);
if let Ok(ciphered_d) = ciphered_data { if let Ok(ciphered_d) = ciphered_data {
let vpn_packet = UDPVpnPacket{ data: ciphered_d, nonce: nonce.to_vec()}; let vpn_packet = UDPVpnPacket{ data: ciphered_d, nonce: nonce.to_vec()};
let serialized_data = vpn_packet.serialize(); let serialized_data = vpn_packet.serialize();
info!("Writing to sock: {:?}", serialized_data);
sock_snd.send(&serialized_data).await.unwrap(); sock_snd.send(&serialized_data).await.unwrap();
} else { } else {
error!("Socket encryption failed."); error!("Socket encryption failed.");

View File

@ -49,9 +49,10 @@ pub async fn server_mode(server_config: ServerConfiguration) {
let addrs_lcl = addresses.clone(); let addrs_lcl = addresses.clone();
if keepalive_sec > 0 { if keepalive_sec > 0 {
tokio::spawn(async move { tokio::spawn(async move {
let kp_sc = keepalive_sec.clone();
loop { loop {
time::sleep(time::Duration::from_secs(3)).await; time::sleep(time::Duration::from_secs(kp_sc.into())).await;
let mut mmp = addrs_lcl.lock().await; let mmp = addrs_lcl.lock().await;
mmp.values().for_each(|p| { mmp.values().for_each(|p| {
let _ = send2hnd_cl.send((UDPKeepAlive{}.serialize(), p.addr)); let _ = send2hnd_cl.send((UDPKeepAlive{}.serialize(), p.addr));
}); });
@ -113,9 +114,8 @@ pub async fn server_mode(server_config: ServerConfiguration) {
Some(h) => { Some(h) => {
match h { match h {
0 => { 0 => {
// (&buf[1..len]).to_vec()
let handshake = UDPVpnHandshake::deserialize(&buf); let handshake = UDPVpnHandshake::deserialize(&buf);
info!("Got handshake! ip: {:?}; key: {:?}", handshake.request_ip, BASE64_STANDARD.encode(&handshake.public_key)); 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) {
let internal_ip = IpAddr::V4(handshake.request_ip); let internal_ip = IpAddr::V4(handshake.request_ip);
@ -138,13 +138,12 @@ pub async fn server_mode(server_config: ServerConfiguration) {
let _ = send2hnd.send((handshake_response.serialize(), addr)); let _ = send2hnd.send((handshake_response.serialize(), addr));
} else { } else {
info!("Bad handshake"); info!("Bad handshake");
plp.iter().for_each(|c| info!("ip: {:?}; pkey: {:?}", c.ip, c.public_key)); //plp.iter().for_each(|c| info!("ip: {:?}; pkey: {:?}", c.ip, c.public_key));
} }
}, // handshake }, // handshake
1 => { 1 => {
let packet = UDPVpnPacket::deserialize(&(buf[..len].to_vec())); let packet = UDPVpnPacket::deserialize(&(buf[..len].to_vec()));
mp.values().filter(| p | p.addr == addr).for_each(|p| { mp.values().filter(| p | p.addr == addr).for_each(|p| {
info!("Key {:?} / nonce {:?}", &p.shared_secret, &packet.nonce);
let aes = Aes256Gcm::new(&p.shared_secret.into()); let aes = Aes256Gcm::new(&p.shared_secret.into());
let nonce = Nonce::clone_from_slice(&packet.nonce[..]); let nonce = Nonce::clone_from_slice(&packet.nonce[..]);
match aes.decrypt(&nonce, &packet.data[..]) { match aes.decrypt(&nonce, &packet.data[..]) {