diff --git a/src/packet/common.rs b/src/packet/common.rs index f7cb879..99b640f 100644 --- a/src/packet/common.rs +++ b/src/packet/common.rs @@ -85,6 +85,8 @@ pub enum PacketType { PKTRegister, // 打洞消息ACK PKTRegisterACK, + + PKTRegisterSuperACK, } impl std::convert::From for PacketType { @@ -95,6 +97,7 @@ impl std::convert::From for PacketType { 2 => Self::PKTPacket, 3 => Self::PKTRegister, 4 => Self::PKTRegisterACK, + 5 => Self::PKTRegisterSuperACK, _ => Self::PKTInvalid, } } @@ -108,6 +111,7 @@ impl PacketType { Self::PKTPacket => 2, Self::PKTRegister => 3, Self::PKTRegisterACK => 4, + Self::PKTRegisterSuperACK => 5, } } } diff --git a/src/packet/mod.rs b/src/packet/mod.rs index f935454..f45a093 100644 --- a/src/packet/mod.rs +++ b/src/packet/mod.rs @@ -12,3 +12,6 @@ pub use register::*; mod register_ack; pub use register_ack::*; + +mod register_super_ack; +pub use register_super_ack::*; diff --git a/src/packet/packet.rs b/src/packet/packet.rs index 0e0166a..b509a41 100644 --- a/src/packet/packet.rs +++ b/src/packet/packet.rs @@ -30,6 +30,7 @@ mod test { v6: [1; 16], }), data: data.into_bytes(), + // data: &data.into_bytes(), }; let info = encode_packet(&cmn1, &packet)?; @@ -50,6 +51,16 @@ mod test { Err(e) => panic!("failed to unmarshal: {}", e), }; assert_eq!(packet.data, packet2.data); + /* + match packet2.data { + Cow::Borrowed(d) => { + println!("borrowed data, {:?}", d); + } + Cow::Owned(d) => { + println!("owned data: {:?}", d); + } + } + */ Ok(()) } } diff --git a/src/packet/register_super.rs b/src/packet/register_super.rs index 97fef3a..cab0bd4 100644 --- a/src/packet/register_super.rs +++ b/src/packet/register_super.rs @@ -16,7 +16,7 @@ pub struct RegisterSuper<'a> { // v6 info pub v6_info: Option, // 自身的ip信息 - pub dev_addr: peer::IpSubnet, + pub dev_addr: peer::IpSubnetNonAtomic, // 自身的公钥 pub pub_key: &'a str, @@ -69,7 +69,10 @@ mod test { cookie: 0, sock: None, v6_info: None, - dev_addr: peer::IpSubnet::new(192, 24), + dev_addr: peer::IpSubnetNonAtomic { + net_addr: 192, + net_bit_len: 24, + }, pub_key: "public key", token: "user's token", }; @@ -102,7 +105,11 @@ mod test { port: 3306, v6: [0; 16], }), - dev_addr: peer::IpSubnet::new(192, 24), + // dev_addr: peer::IpSubnet::new(192, 24), + dev_addr: peer::IpSubnetNonAtomic { + net_addr: 192, + net_bit_len: 24, + }, pub_key: "public key", token: "user's token", }; diff --git a/src/packet/register_super_ack.rs b/src/packet/register_super_ack.rs new file mode 100644 index 0000000..f65fce9 --- /dev/null +++ b/src/packet/register_super_ack.rs @@ -0,0 +1,22 @@ +use serde::{Deserialize, Serialize}; + +use crate::peer::{IpSubnetNonAtomic, SdlanSock}; + +#[derive(Serialize, Deserialize)] +pub struct RegisterSuperACK { + // pair the RegisterSuper + pub cookie: u32, + // assigned ip info + pub dev_addr: IpSubnetNonAtomic, + + // sock info + pub sock: SdlanSock, + + // 头加密aes,rsa加密之后 + pub header_key: Vec, + // 流量加密aes, rsa加密之后 + pub encrypted_key: Vec, + + // the ack's lifetime + pub lifetime: u16, +} diff --git a/src/peer.rs b/src/peer.rs index 5689a2a..851fe4d 100644 --- a/src/peer.rs +++ b/src/peer.rs @@ -16,7 +16,7 @@ pub struct Peer { // 对端对外开放的ip和端口信息 pub sock: RwLock, // peer's local v6 info - pub pub_key: RwLock>, + pub pub_key: RwLock, pub timeout: isize, @@ -52,7 +52,7 @@ impl Peer { port: 0, v6: [0; 16], }), - pub_key: RwLock::new(vec![]), + pub_key: RwLock::new("".to_owned()), timeout: 0, last_seen: AtomicU64::new(0), last_p2p: AtomicU64::new(0), @@ -90,6 +90,31 @@ impl IpSubnet { } */ +#[derive(Debug, Serialize, Deserialize, FromRow)] +pub struct IpSubnetNonAtomic { + #[sqlx(try_from = "u32")] + pub net_addr: u32, + #[sqlx(try_from = "u8")] + pub net_bit_len: u8, +} + +impl IpSubnetNonAtomic { + pub fn new(ip: u32, netbit: u8) -> Self { + Self { + net_addr: ip, + net_bit_len: netbit, + } + } + + pub fn net_addr(&self) -> u32 { + self.net_addr + } + + pub fn net_bit_len(&self) -> u8 { + self.net_bit_len + } +} + #[derive(Debug, Serialize, Deserialize, FromRow)] pub struct IpSubnet { #[sqlx(try_from = "u32")] diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 381bf20..e301714 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -10,7 +10,8 @@ pub use error::*; pub use helper::*; pub use myaes::{aes_decrypt, aes_encrypt}; pub use myrsa::{ - gen_rsa_keys, load_private_key_file, load_public_key_file, rsa_decrypt, rsa_encrypt, + gen_rsa_keys, load_private_key_file, load_public_key, load_public_key_file, rsa_decrypt, + rsa_encrypt, }; pub use myuuid::*; diff --git a/src/utils/myrsa.rs b/src/utils/myrsa.rs index 511153a..3fb0293 100644 --- a/src/utils/myrsa.rs +++ b/src/utils/myrsa.rs @@ -79,7 +79,7 @@ pub fn load_private_key(privkey: &str) -> Result { } } -pub fn rsa_encrypt(pubkey: Arc, data: &[u8]) -> Result> { +pub fn rsa_encrypt(pubkey: &RsaPublicKey, data: &[u8]) -> Result> { let mut rng = rand::thread_rng(); match pubkey.encrypt(&mut rng, Pkcs1v15Encrypt, data) { Err(e) => Err(SDLanError::EncryptError(format!("rsa encrypt: {}", e))), @@ -87,7 +87,7 @@ pub fn rsa_encrypt(pubkey: Arc, data: &[u8]) -> Result> { } } -pub fn rsa_decrypt(privkey: Arc, cipherd: &[u8]) -> Result> { +pub fn rsa_decrypt(privkey: &RsaPrivateKey, cipherd: &[u8]) -> Result> { match privkey.decrypt(Pkcs1v15Encrypt, cipherd) { Err(e) => Err(SDLanError::EncryptError(format!("rsa decrypt: {}", e))), Ok(v) => Ok(v), @@ -119,13 +119,13 @@ mod tests { let msgs = vec![zero_msg, normal_msg, big_msg, max_msg]; - let public = Arc::new(public); - let private = Arc::new(private); + // let public = public; + // let private = private; for msg in msgs.iter() { println!("testing {} size length", msg.len()); - let encrypted = rsa_encrypt(public.clone(), msg)?; - let decrypted = rsa_decrypt(private.clone(), encrypted.as_slice())?; + let encrypted = rsa_encrypt(&public.clone(), msg)?; + let decrypted = rsa_decrypt(&private.clone(), encrypted.as_slice())?; assert_eq!(decrypted.as_slice(), msg); println!("testing {} ok", msg.len()); }