less unwraps

This commit is contained in:
17ms 2023-05-01 02:58:20 +03:00
parent be7ac79ab1
commit 772644fa5d
3 changed files with 46 additions and 23 deletions

View File

@ -185,27 +185,47 @@ impl Connector {
loop {
let rx_msg = rx.recv().await;
match rx_msg.unwrap() {
Message::ClientReq(name) => {
let req = Request::new(name, metadata).unwrap(); // TODO: handle
self.request(conn, req).await?;
}
Message::Shutdown => {
let msg = b"DISCONNECT".to_vec();
comms::send(
&mut conn.writer,
Some(&mut conn.cipher),
Some(&mut conn.rng),
&msg,
)
.await?;
break;
}
_ => continue,
if rx_msg.is_none() {
continue;
}
match self.msg_handler(rx_msg.unwrap(), conn, metadata).await? {
true => continue,
false => break,
};
}
Ok(())
}
async fn msg_handler(
&self,
msg: Message,
conn: &mut Connection<'_>,
metadata: &HashMap<String, (u64, String)>,
) -> Result<bool, Box<dyn Error + Send + Sync>> {
match msg {
Message::ClientReq(name) => {
let req = match Request::new(name, metadata) {
Some(req) => req,
None => return Ok(true),
};
self.request(conn, req).await?;
Ok(true)
}
Message::Shutdown => {
let msg = b"DISCONNECT".to_vec();
comms::send(
&mut conn.writer,
Some(&mut conn.cipher),
Some(&mut conn.rng),
&msg,
)
.await?;
Ok(false)
}
_ => Ok(true),
}
}
}

View File

@ -62,7 +62,10 @@ pub fn aes_encrypt(
rng: &mut OsRng,
) -> Result<Vec<u8>, Box<dyn Error + Send + Sync>> {
let nonce = generate_nonce(rng);
let encrypted = cipher.encrypt(&nonce, data.as_ref()).unwrap(); // TODO: handle errors
let encrypted = match cipher.encrypt(&nonce, data.as_ref()) {
Ok(d) => d,
Err(_) => return Err("AES encryption failed".into()),
};
let mut data = nonce.to_vec();
data.extend_from_slice(&encrypted);
@ -74,9 +77,10 @@ pub fn aes_decrypt(
cipher: &mut AesGcm<Aes256, U12>,
) -> Result<Vec<u8>, Box<dyn Error + Send + Sync>> {
let (nonce_bytes, data) = data.split_at(AES_NONCE_SIZE);
let decrypted = cipher
.decrypt(Nonce::from_slice(nonce_bytes), data.as_ref())
.unwrap(); // TODO: handle errors
let decrypted = match cipher.decrypt(Nonce::from_slice(nonce_bytes), data.as_ref()) {
Ok(d) => d,
Err(_) => return Err("AES decryption failed".into()),
};
Ok(decrypted)
}

View File

@ -1 +0,0 @@
// placeholder