Revert "less unwraps"

This reverts commit 772644fa5d.
This commit is contained in:
17ms 2023-05-01 04:05:36 +03:00
parent aa35573b44
commit 573a2a44a1
3 changed files with 23 additions and 46 deletions

View File

@ -185,47 +185,27 @@ impl Connector {
loop { loop {
let rx_msg = rx.recv().await; let rx_msg = rx.recv().await;
if rx_msg.is_none() { match rx_msg.unwrap() {
continue; 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?;
match self.msg_handler(rx_msg.unwrap(), conn, metadata).await? { break;
true => continue, }
false => break, _ => continue,
}; }
} }
Ok(()) 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,10 +62,7 @@ pub fn aes_encrypt(
rng: &mut OsRng, rng: &mut OsRng,
) -> Result<Vec<u8>, Box<dyn Error + Send + Sync>> { ) -> Result<Vec<u8>, Box<dyn Error + Send + Sync>> {
let nonce = generate_nonce(rng); let nonce = generate_nonce(rng);
let encrypted = match cipher.encrypt(&nonce, data.as_ref()) { let encrypted = cipher.encrypt(&nonce, data.as_ref()).unwrap(); // TODO: handle errors
Ok(d) => d,
Err(_) => return Err("AES encryption failed".into()),
};
let mut data = nonce.to_vec(); let mut data = nonce.to_vec();
data.extend_from_slice(&encrypted); data.extend_from_slice(&encrypted);
@ -77,10 +74,9 @@ pub fn aes_decrypt(
cipher: &mut AesGcm<Aes256, U12>, cipher: &mut AesGcm<Aes256, U12>,
) -> Result<Vec<u8>, Box<dyn Error + Send + Sync>> { ) -> Result<Vec<u8>, Box<dyn Error + Send + Sync>> {
let (nonce_bytes, data) = data.split_at(AES_NONCE_SIZE); let (nonce_bytes, data) = data.split_at(AES_NONCE_SIZE);
let decrypted = match cipher.decrypt(Nonce::from_slice(nonce_bytes), data.as_ref()) { let decrypted = cipher
Ok(d) => d, .decrypt(Nonce::from_slice(nonce_bytes), data.as_ref())
Err(_) => return Err("AES decryption failed".into()), .unwrap(); // TODO: handle errors
};
Ok(decrypted) Ok(decrypted)
} }

1
src/error.rs Normal file
View File

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