parent
aa35573b44
commit
573a2a44a1
@ -185,47 +185,27 @@ impl Connector {
|
||||
loop {
|
||||
let rx_msg = rx.recv().await;
|
||||
|
||||
if rx_msg.is_none() {
|
||||
continue;
|
||||
}
|
||||
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?;
|
||||
|
||||
match self.msg_handler(rx_msg.unwrap(), conn, metadata).await? {
|
||||
true => continue,
|
||||
false => break,
|
||||
};
|
||||
break;
|
||||
}
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -62,10 +62,7 @@ pub fn aes_encrypt(
|
||||
rng: &mut OsRng,
|
||||
) -> Result<Vec<u8>, Box<dyn Error + Send + Sync>> {
|
||||
let nonce = generate_nonce(rng);
|
||||
let encrypted = match cipher.encrypt(&nonce, data.as_ref()) {
|
||||
Ok(d) => d,
|
||||
Err(_) => return Err("AES encryption failed".into()),
|
||||
};
|
||||
let encrypted = cipher.encrypt(&nonce, data.as_ref()).unwrap(); // TODO: handle errors
|
||||
let mut data = nonce.to_vec();
|
||||
data.extend_from_slice(&encrypted);
|
||||
|
||||
@ -77,10 +74,9 @@ 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 = match cipher.decrypt(Nonce::from_slice(nonce_bytes), data.as_ref()) {
|
||||
Ok(d) => d,
|
||||
Err(_) => return Err("AES decryption failed".into()),
|
||||
};
|
||||
let decrypted = cipher
|
||||
.decrypt(Nonce::from_slice(nonce_bytes), data.as_ref())
|
||||
.unwrap(); // TODO: handle errors
|
||||
|
||||
Ok(decrypted)
|
||||
}
|
||||
|
1
src/error.rs
Normal file
1
src/error.rs
Normal file
@ -0,0 +1 @@
|
||||
// placeholder
|
Loading…
Reference in New Issue
Block a user