fix key size error bug
This commit is contained in:
parent
6c2d0f4608
commit
4c6e226b7a
@ -2,9 +2,54 @@ use crate::peer::SdlanSock;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Packet<'a> {
|
||||
pub struct Packet {
|
||||
pub src_ip: u32,
|
||||
pub dst_ip: u32,
|
||||
pub sock: SdlanSock,
|
||||
pub data: &'a [u8],
|
||||
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::utils::Result;
|
||||
use crate::{config::AF_INET, packet::*, peer::SdlanSock, utils::gen_uuid};
|
||||
|
||||
#[test]
|
||||
fn test_packet_encode_and_decode() -> Result<()> {
|
||||
let id = gen_uuid();
|
||||
let data = gen_uuid();
|
||||
let cmn1 = Common::new(&id);
|
||||
let packet = Packet {
|
||||
src_ip: 1,
|
||||
dst_ip: 2,
|
||||
sock: SdlanSock {
|
||||
family: AF_INET,
|
||||
port: 80,
|
||||
v4: [1; 4],
|
||||
v6: [1; 16],
|
||||
},
|
||||
data: data.into_bytes(),
|
||||
};
|
||||
|
||||
let info = encode_packet(&cmn1, &packet)?;
|
||||
|
||||
let (cmn2, rest) = decode_common(&info)?;
|
||||
if cmn2.id != cmn1.id {
|
||||
panic!("cmn not equal");
|
||||
}
|
||||
let data_str = std::str::from_utf8(rest);
|
||||
if let Err(e) = data_str {
|
||||
panic!("convert data failed: {}", e);
|
||||
} else {
|
||||
let data_str = data_str.unwrap();
|
||||
println!("got packet data: {}", data_str);
|
||||
}
|
||||
let packet2: Packet = match serde_json::from_slice(rest) {
|
||||
Ok(p) => p,
|
||||
Err(e) => panic!("failed to unmarshal: {}", e),
|
||||
};
|
||||
assert_eq!(packet.data, packet2.data);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
13
src/peer.rs
13
src/peer.rs
@ -4,6 +4,7 @@ use dashmap::DashMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::prelude::FromRow;
|
||||
use std::default::Default;
|
||||
use std::future::Future;
|
||||
use std::sync::atomic::{AtomicU32, AtomicU64, AtomicU8, Ordering};
|
||||
use std::sync::RwLock;
|
||||
|
||||
@ -186,13 +187,13 @@ impl PeerMap {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn peer_match<F: Fn(&Arc<Peer>) -> bool>(
|
||||
&self,
|
||||
id: &str,
|
||||
f: F,
|
||||
) -> Option<(String, Arc<Peer>)> {
|
||||
// pub fn peer_match<F: Fn(&Arc<Peer>) -> bool>(
|
||||
pub fn peer_match<F>(&self, id: &str, f: F) -> Option<(String, Arc<Peer>)>
|
||||
where
|
||||
F: Fn(&Arc<Peer>) -> bool,
|
||||
{
|
||||
if let Some(v) = self.peers.get(id) {
|
||||
if f(&v) {
|
||||
if f(&*v) {
|
||||
return Some((v.key().to_owned(), v.clone()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,7 +8,11 @@ pub fn aes_encrypt(key: &[u8], plain: &[u8]) -> Result<Vec<u8>> {
|
||||
let mut buf = Vec::new();
|
||||
buf.resize(plain.len() + 16, 0);
|
||||
|
||||
if key.len() != 32 {
|
||||
return Err(SDLanError::EncryptError("key size not 32".to_string()));
|
||||
}
|
||||
let iv = &key[..16];
|
||||
|
||||
match Aes256CbcEnc::new(key.into(), iv.into()).encrypt_padded_b2b_mut::<Pkcs7>(plain, &mut buf)
|
||||
{
|
||||
Err(e) => Err(SDLanError::EncryptError(format!("aes encrypt: {}", e))),
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
pub fn gen_uuid() -> String {
|
||||
format!("{:x}", Uuid::new_v4().as_u128())
|
||||
format!("{:032x}", Uuid::new_v4().as_u128())
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user