register super
This commit is contained in:
parent
1339d30cc4
commit
067c15e5d6
@ -85,6 +85,8 @@ pub enum PacketType {
|
|||||||
PKTRegister,
|
PKTRegister,
|
||||||
// 打洞消息ACK
|
// 打洞消息ACK
|
||||||
PKTRegisterACK,
|
PKTRegisterACK,
|
||||||
|
|
||||||
|
PKTRegisterSuperACK,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::convert::From<u8> for PacketType {
|
impl std::convert::From<u8> for PacketType {
|
||||||
@ -95,6 +97,7 @@ impl std::convert::From<u8> for PacketType {
|
|||||||
2 => Self::PKTPacket,
|
2 => Self::PKTPacket,
|
||||||
3 => Self::PKTRegister,
|
3 => Self::PKTRegister,
|
||||||
4 => Self::PKTRegisterACK,
|
4 => Self::PKTRegisterACK,
|
||||||
|
5 => Self::PKTRegisterSuperACK,
|
||||||
_ => Self::PKTInvalid,
|
_ => Self::PKTInvalid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -108,6 +111,7 @@ impl PacketType {
|
|||||||
Self::PKTPacket => 2,
|
Self::PKTPacket => 2,
|
||||||
Self::PKTRegister => 3,
|
Self::PKTRegister => 3,
|
||||||
Self::PKTRegisterACK => 4,
|
Self::PKTRegisterACK => 4,
|
||||||
|
Self::PKTRegisterSuperACK => 5,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,3 +12,6 @@ pub use register::*;
|
|||||||
|
|
||||||
mod register_ack;
|
mod register_ack;
|
||||||
pub use register_ack::*;
|
pub use register_ack::*;
|
||||||
|
|
||||||
|
mod register_super_ack;
|
||||||
|
pub use register_super_ack::*;
|
||||||
|
|||||||
@ -30,6 +30,7 @@ mod test {
|
|||||||
v6: [1; 16],
|
v6: [1; 16],
|
||||||
}),
|
}),
|
||||||
data: data.into_bytes(),
|
data: data.into_bytes(),
|
||||||
|
// data: &data.into_bytes(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let info = encode_packet(&cmn1, &packet)?;
|
let info = encode_packet(&cmn1, &packet)?;
|
||||||
@ -50,6 +51,16 @@ mod test {
|
|||||||
Err(e) => panic!("failed to unmarshal: {}", e),
|
Err(e) => panic!("failed to unmarshal: {}", e),
|
||||||
};
|
};
|
||||||
assert_eq!(packet.data, packet2.data);
|
assert_eq!(packet.data, packet2.data);
|
||||||
|
/*
|
||||||
|
match packet2.data {
|
||||||
|
Cow::Borrowed(d) => {
|
||||||
|
println!("borrowed data, {:?}", d);
|
||||||
|
}
|
||||||
|
Cow::Owned(d) => {
|
||||||
|
println!("owned data: {:?}", d);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,7 +16,7 @@ pub struct RegisterSuper<'a> {
|
|||||||
// v6 info
|
// v6 info
|
||||||
pub v6_info: Option<V6Info>,
|
pub v6_info: Option<V6Info>,
|
||||||
// 自身的ip信息
|
// 自身的ip信息
|
||||||
pub dev_addr: peer::IpSubnet,
|
pub dev_addr: peer::IpSubnetNonAtomic,
|
||||||
|
|
||||||
// 自身的公钥
|
// 自身的公钥
|
||||||
pub pub_key: &'a str,
|
pub pub_key: &'a str,
|
||||||
@ -69,7 +69,10 @@ mod test {
|
|||||||
cookie: 0,
|
cookie: 0,
|
||||||
sock: None,
|
sock: None,
|
||||||
v6_info: 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",
|
pub_key: "public key",
|
||||||
token: "user's token",
|
token: "user's token",
|
||||||
};
|
};
|
||||||
@ -102,7 +105,11 @@ mod test {
|
|||||||
port: 3306,
|
port: 3306,
|
||||||
v6: [0; 16],
|
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",
|
pub_key: "public key",
|
||||||
token: "user's token",
|
token: "user's token",
|
||||||
};
|
};
|
||||||
|
|||||||
22
src/packet/register_super_ack.rs
Normal file
22
src/packet/register_super_ack.rs
Normal file
@ -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<u8>,
|
||||||
|
// 流量加密aes, rsa加密之后
|
||||||
|
pub encrypted_key: Vec<u8>,
|
||||||
|
|
||||||
|
// the ack's lifetime
|
||||||
|
pub lifetime: u16,
|
||||||
|
}
|
||||||
29
src/peer.rs
29
src/peer.rs
@ -16,7 +16,7 @@ pub struct Peer {
|
|||||||
// 对端对外开放的ip和端口信息
|
// 对端对外开放的ip和端口信息
|
||||||
pub sock: RwLock<SdlanSock>,
|
pub sock: RwLock<SdlanSock>,
|
||||||
// peer's local v6 info
|
// peer's local v6 info
|
||||||
pub pub_key: RwLock<Vec<u8>>,
|
pub pub_key: RwLock<String>,
|
||||||
|
|
||||||
pub timeout: isize,
|
pub timeout: isize,
|
||||||
|
|
||||||
@ -52,7 +52,7 @@ impl Peer {
|
|||||||
port: 0,
|
port: 0,
|
||||||
v6: [0; 16],
|
v6: [0; 16],
|
||||||
}),
|
}),
|
||||||
pub_key: RwLock::new(vec![]),
|
pub_key: RwLock::new("".to_owned()),
|
||||||
timeout: 0,
|
timeout: 0,
|
||||||
last_seen: AtomicU64::new(0),
|
last_seen: AtomicU64::new(0),
|
||||||
last_p2p: 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)]
|
#[derive(Debug, Serialize, Deserialize, FromRow)]
|
||||||
pub struct IpSubnet {
|
pub struct IpSubnet {
|
||||||
#[sqlx(try_from = "u32")]
|
#[sqlx(try_from = "u32")]
|
||||||
|
|||||||
@ -10,7 +10,8 @@ pub use error::*;
|
|||||||
pub use helper::*;
|
pub use helper::*;
|
||||||
pub use myaes::{aes_decrypt, aes_encrypt};
|
pub use myaes::{aes_decrypt, aes_encrypt};
|
||||||
pub use myrsa::{
|
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::*;
|
pub use myuuid::*;
|
||||||
|
|
||||||
|
|||||||
@ -79,7 +79,7 @@ pub fn load_private_key(privkey: &str) -> Result<RsaPrivateKey> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rsa_encrypt(pubkey: Arc<RsaPublicKey>, data: &[u8]) -> Result<Vec<u8>> {
|
pub fn rsa_encrypt(pubkey: &RsaPublicKey, data: &[u8]) -> Result<Vec<u8>> {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
match pubkey.encrypt(&mut rng, Pkcs1v15Encrypt, data) {
|
match pubkey.encrypt(&mut rng, Pkcs1v15Encrypt, data) {
|
||||||
Err(e) => Err(SDLanError::EncryptError(format!("rsa encrypt: {}", e))),
|
Err(e) => Err(SDLanError::EncryptError(format!("rsa encrypt: {}", e))),
|
||||||
@ -87,7 +87,7 @@ pub fn rsa_encrypt(pubkey: Arc<RsaPublicKey>, data: &[u8]) -> Result<Vec<u8>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rsa_decrypt(privkey: Arc<RsaPrivateKey>, cipherd: &[u8]) -> Result<Vec<u8>> {
|
pub fn rsa_decrypt(privkey: &RsaPrivateKey, cipherd: &[u8]) -> Result<Vec<u8>> {
|
||||||
match privkey.decrypt(Pkcs1v15Encrypt, cipherd) {
|
match privkey.decrypt(Pkcs1v15Encrypt, cipherd) {
|
||||||
Err(e) => Err(SDLanError::EncryptError(format!("rsa decrypt: {}", e))),
|
Err(e) => Err(SDLanError::EncryptError(format!("rsa decrypt: {}", e))),
|
||||||
Ok(v) => Ok(v),
|
Ok(v) => Ok(v),
|
||||||
@ -119,13 +119,13 @@ mod tests {
|
|||||||
|
|
||||||
let msgs = vec![zero_msg, normal_msg, big_msg, max_msg];
|
let msgs = vec![zero_msg, normal_msg, big_msg, max_msg];
|
||||||
|
|
||||||
let public = Arc::new(public);
|
// let public = public;
|
||||||
let private = Arc::new(private);
|
// let private = private;
|
||||||
|
|
||||||
for msg in msgs.iter() {
|
for msg in msgs.iter() {
|
||||||
println!("testing {} size length", msg.len());
|
println!("testing {} size length", msg.len());
|
||||||
let encrypted = rsa_encrypt(public.clone(), msg)?;
|
let encrypted = rsa_encrypt(&public.clone(), msg)?;
|
||||||
let decrypted = rsa_decrypt(private.clone(), encrypted.as_slice())?;
|
let decrypted = rsa_decrypt(&private.clone(), encrypted.as_slice())?;
|
||||||
assert_eq!(decrypted.as_slice(), msg);
|
assert_eq!(decrypted.as_slice(), msg);
|
||||||
println!("testing {} ok", msg.len());
|
println!("testing {} ok", msg.len());
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user