Better error handling for P2P connections.
This commit is contained in:
@@ -1,10 +1,9 @@
|
|||||||
use chacha20poly1305::{ChaCha20Poly1305, Key, Nonce};
|
use chacha20poly1305::{ChaCha20Poly1305, Key, Nonce};
|
||||||
use chacha20poly1305::aead::{Aead, NewAead};
|
use chacha20poly1305::aead::{Aead, NewAead, Error};
|
||||||
use std::fmt::{Debug, Formatter};
|
use std::fmt::{Debug, Formatter};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
pub const ZERO_NONCE: [u8; 12] = [0u8; 12];
|
pub const ZERO_NONCE: [u8; 12] = [0u8; 12];
|
||||||
const FAILURE: &str = "encryption failure!";
|
|
||||||
|
|
||||||
/// A small wrap-up to use Chacha20 encryption for domain names.
|
/// A small wrap-up to use Chacha20 encryption for domain names.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -22,14 +21,14 @@ impl Chacha {
|
|||||||
Chacha { cipher, nonce: buf }
|
Chacha { cipher, nonce: buf }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn encrypt(&self, data: &[u8]) -> Vec<u8> {
|
pub fn encrypt(&self, data: &[u8]) -> Result<Vec<u8>, Error> {
|
||||||
let nonce = Nonce::from(self.nonce.clone());
|
let nonce = Nonce::from(self.nonce.clone());
|
||||||
self.cipher.encrypt(&nonce, data.as_ref()).expect(FAILURE)
|
self.cipher.encrypt(&nonce, data.as_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn decrypt(&self, data: &[u8]) -> Vec<u8> {
|
pub fn decrypt(&self, data: &[u8]) -> Result<Vec<u8>, Error> {
|
||||||
let nonce = Nonce::from(self.nonce.clone());
|
let nonce = Nonce::from(self.nonce.clone());
|
||||||
self.cipher.decrypt(&nonce, data.as_ref()).expect(FAILURE)
|
self.cipher.decrypt(&nonce, data.as_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_nonce(&self) -> &[u8; 12] {
|
pub fn get_nonce(&self) -> &[u8; 12] {
|
||||||
@@ -52,11 +51,11 @@ mod tests {
|
|||||||
pub fn test_chacha() {
|
pub fn test_chacha() {
|
||||||
let buf = b"178135D209C697625E3EC71DA5C760382E54936F824EE5083908DA66B14ECE18";
|
let buf = b"178135D209C697625E3EC71DA5C760382E54936F824EE5083908DA66B14ECE18";
|
||||||
let chacha1 = Chacha::new(b"178135D209C697625E3EC71DA5C76038", &buf[..12]);
|
let chacha1 = Chacha::new(b"178135D209C697625E3EC71DA5C76038", &buf[..12]);
|
||||||
let bytes1 = chacha1.encrypt(b"TEST");
|
let bytes1 = chacha1.encrypt(b"TEST").unwrap();
|
||||||
println!("{}", to_hex(&bytes1));
|
println!("{}", to_hex(&bytes1));
|
||||||
|
|
||||||
let chacha2 = Chacha::new(b"178135D209C697625E3EC71DA5C76038", &buf[..12]);
|
let chacha2 = Chacha::new(b"178135D209C697625E3EC71DA5C76038", &buf[..12]);
|
||||||
let bytes2 = chacha2.decrypt(&bytes1);
|
let bytes2 = chacha2.decrypt(&bytes1).unwrap();
|
||||||
assert_eq!(String::from_utf8(bytes2).unwrap(), "TEST");
|
assert_eq!(String::from_utf8(bytes2).unwrap(), "TEST");
|
||||||
|
|
||||||
let bytes2 = chacha2.encrypt(b"TEST");
|
let bytes2 = chacha2.encrypt(b"TEST");
|
||||||
|
|||||||
+13
-12
@@ -228,8 +228,8 @@ impl Network {
|
|||||||
trace!("Client hello read successfully");
|
trace!("Client hello read successfully");
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(_) => {
|
||||||
info!("Error reading client handshake. {}", e);
|
debug!("Error reading client handshake from {}.", peer.get_addr());
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -256,8 +256,8 @@ impl Network {
|
|||||||
trace!("Server hello read successfully");
|
trace!("Server hello read successfully");
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(_) => {
|
||||||
warn!("Error reading server handshake. {}", e);
|
debug!("Error reading client handshake from {}", peer.get_addr());
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -376,9 +376,10 @@ impl Network {
|
|||||||
State::Connected => {}
|
State::Connected => {}
|
||||||
State::Message { data } => {
|
State::Message { data } => {
|
||||||
//debug!("Sending data to {}: {}", &peer.get_addr(), &String::from_utf8(data.clone()).unwrap());
|
//debug!("Sending data to {}: {}", &peer.get_addr(), &String::from_utf8(data.clone()).unwrap());
|
||||||
let data = encode_bytes(&data, peer.get_cipher());
|
if let Ok(data) = encode_bytes(&data, peer.get_cipher()) {
|
||||||
send_message(peer.get_stream(), &data).unwrap_or_else(|e| warn!("Error sending message {}", e));
|
send_message(peer.get_stream(), &data).unwrap_or_else(|e| warn!("Error sending message {}", e));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
State::Idle { from } => {
|
State::Idle { from } => {
|
||||||
debug!("Odd version of pings :)");
|
debug!("Odd version of pings :)");
|
||||||
if from.elapsed().as_secs() >= 30 {
|
if from.elapsed().as_secs() >= 30 {
|
||||||
@@ -636,16 +637,16 @@ fn subscribe_to_bus(running: Arc<AtomicBool>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn encode_bytes(data: &Vec<u8>, cipher: &Option<Chacha>) -> Vec<u8> {
|
fn encode_bytes(data: &Vec<u8>, cipher: &Option<Chacha>) -> Result<Vec<u8>, chacha20poly1305::aead::Error> {
|
||||||
match cipher {
|
match cipher {
|
||||||
None => { data.clone() }
|
None => { Ok(data.clone()) }
|
||||||
Some(chacha) => {
|
Some(chacha) => {
|
||||||
chacha.encrypt(data.as_slice())
|
chacha.encrypt(data.as_slice())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encode_message(message: &Message, cipher: &Option<Chacha>) -> Result<Vec<u8>, ()> {
|
fn encode_message(message: &Message, cipher: &Option<Chacha>) -> Result<Vec<u8>, chacha20poly1305::aead::Error> {
|
||||||
match serde_cbor::to_vec(message) {
|
match serde_cbor::to_vec(message) {
|
||||||
Ok(vec) => {
|
Ok(vec) => {
|
||||||
match cipher {
|
match cipher {
|
||||||
@@ -655,22 +656,22 @@ fn encode_message(message: &Message, cipher: &Option<Chacha>) -> Result<Vec<u8>,
|
|||||||
}
|
}
|
||||||
Some(chacha) => {
|
Some(chacha) => {
|
||||||
//info!("Encoding message: {:?}", to_hex(&vec));
|
//info!("Encoding message: {:?}", to_hex(&vec));
|
||||||
Ok(chacha.encrypt(vec.as_slice()))
|
chacha.encrypt(vec.as_slice())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
warn!("Could not encode message! {}", e);
|
warn!("Could not encode message! {}", e);
|
||||||
Err(())
|
Err(chacha20poly1305::aead::Error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decode_message(data: &Vec<u8>, cipher: &Option<Chacha>) -> Result<Vec<u8>, Error> {
|
fn decode_message(data: &Vec<u8>, cipher: &Option<Chacha>) -> Result<Vec<u8>, chacha20poly1305::aead::Error> {
|
||||||
match cipher {
|
match cipher {
|
||||||
None => { Ok(data.clone()) }
|
None => { Ok(data.clone()) }
|
||||||
Some(chacha) => {
|
Some(chacha) => {
|
||||||
Ok(chacha.decrypt(data.as_slice()))
|
chacha.decrypt(data.as_slice())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user