Skip to content

Commit

Permalink
Merge pull request #1 from fu050409/main
Browse files Browse the repository at this point in the history
🚀 修订所有权转移问题
可以合并
  • Loading branch information
fb0sh authored Dec 25, 2023
2 parents 15aa216 + 412ffaf commit 8bdd8e3
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 129 deletions.
8 changes: 2 additions & 6 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@ pub async fn get(olps: &str, tfo: bool) -> Result<Response, OblivionException> {
request("get", olps, None, None, tfo).await
}

pub async fn post(
olps: &str,
data: Option<Value>,
tfo: bool,
) -> Result<Response, OblivionException> {
request("post", olps, data, None, tfo).await
pub async fn post(olps: &str, data: Value, tfo: bool) -> Result<Response, OblivionException> {
request("post", olps, Some(data), None, tfo).await
}

pub async fn put(
Expand Down
49 changes: 23 additions & 26 deletions src/models/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ pub struct Response {
impl Response {
pub fn new(header: String, content: Vec<u8>, olps: String, status_code: i32) -> Self {
Self {
header: header,
content: content,
olps: olps,
status_code: status_code,
header,
content,
olps,
status_code,
}
}

Expand Down Expand Up @@ -90,13 +90,13 @@ impl Request {
let oblivion = Oblivion::new(&method, &olps)?;
let plain_text = oblivion.plain_text();
Ok(Self {
method: method,
olps: olps,
path: path,
data: data,
file: file,
tfo: tfo,
plain_text: plain_text,
method,
olps,
path,
data,
file,
tfo,
plain_text,
prepared: false,
private_key: None,
public_key: None,
Expand Down Expand Up @@ -130,12 +130,8 @@ impl Request {

self.send_header().await?;

let mut oke = OKE::new(
Some(&self.private_key.as_ref().unwrap()),
self.public_key.clone(),
)?;
let mut oke = oke
.from_stream_with_salt(self.tcp.as_mut().unwrap())
let mut oke = OKE::new(Some(&self.private_key.as_ref().unwrap()), self.public_key)?;
oke.from_stream_with_salt(self.tcp.as_mut().unwrap())
.await?;
self.aes_key = Some(oke.get_aes_key());
oke.to_stream(self.tcp.as_mut().unwrap()).await;
Expand All @@ -161,32 +157,33 @@ impl Request {
let mut oed = if self.method == "POST" {
let oed = if self.data.is_none() {
let mut oed = OED::new(self.aes_key.clone());
let oed = oed.from_dict(json!({}));
oed.from_dict(json!({}))?;
oed
} else {
let mut oed = OED::new(self.aes_key.clone());
let oed = oed.from_dict(self.data.clone().unwrap());
oed.from_dict(self.data.clone().unwrap())?;
oed
};
oed
} else if self.method == "PUT" {
let mut oed = if self.data.is_none() {
let mut oed = OED::new(self.aes_key.clone());
let oed = oed.from_dict(json!({}))?;
oed.from_dict(json!({}))?;
oed
} else {
let mut oed = OED::new(self.aes_key.clone());
let oed = oed.from_dict(self.data.clone().unwrap())?;
oed.from_dict(self.data.clone().unwrap())?;
oed
};

oed.to_stream(tcp, 5).await?;

let mut oed = OED::new(self.aes_key.clone());
let oed = oed.from_bytes(self.file.clone().unwrap());
oed.from_bytes(self.file.clone().unwrap())?;
oed
} else {
Err(OblivionException::UnsupportedMethod(None))
}?;
return Err(OblivionException::UnsupportedMethod(None));
};

oed.to_stream(tcp, 5).await?;
Ok(())
Expand All @@ -199,7 +196,7 @@ impl Request {
Err(OblivionException::ErrorNotPrepared(None))
} else {
let mut oed = OED::new(self.aes_key.clone());
let mut oed = oed.from_stream(tcp, 5).await?;
oed.from_stream(tcp, 5).await?;

let mut osc = OSC::from_stream(tcp).await?;

Expand All @@ -214,6 +211,6 @@ impl Request {
}

pub fn is_prepared(&mut self) -> bool {
self.prepared.clone()
self.prepared
}
}
100 changes: 39 additions & 61 deletions src/models/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,11 @@ impl OSC {
}

pub fn get_status_code(&mut self) -> i32 {
self.status_code.clone()
self.status_code
}
}

pub struct OKE<'a> {
length: Option<i32>,
public_key: Option<PublicKey>,
private_key: Option<&'a EphemeralSecret>,
salt: Option<Vec<u8>>,
Expand All @@ -91,61 +90,54 @@ impl<'a> OKE<'a> {
public_key: Option<PublicKey>,
) -> Result<Self, OblivionException> {
Ok(Self {
length: None,
public_key: public_key,
private_key: private_key,
public_key,
private_key,
salt: Some(generate_random_salt()),
remote_public_key: None,
shared_aes_key: None,
})
}

fn clone(&mut self) -> Self {
Self {
length: self.length.clone(),
public_key: self.public_key,
private_key: self.private_key,
salt: self.salt.clone(),
remote_public_key: self.remote_public_key.clone(),
shared_aes_key: self.shared_aes_key.clone(),
}
}

pub fn from_public_key_bytes(
&mut self,
public_key_bytes: &[u8],
) -> Result<Self, OblivionException> {
) -> Result<&mut Self, OblivionException> {
self.public_key = Some(PublicKey::from_sec1_bytes(public_key_bytes).unwrap());
Ok(self.clone())
Ok(self)
}

pub async fn from_stream(&mut self, stream: &mut Socket) -> Result<OKE<'a>, OblivionException> {
pub async fn from_stream(
&mut self,
stream: &mut Socket,
) -> Result<&mut Self, OblivionException> {
let remote_public_key_length = stream.recv_len().await?;
let remote_public_key_bytes = stream.recv(remote_public_key_length).await;
self.remote_public_key = Some(PublicKey::from_sec1_bytes(&remote_public_key_bytes).unwrap());
self.remote_public_key =
Some(PublicKey::from_sec1_bytes(&remote_public_key_bytes).unwrap());
self.shared_aes_key = Some(generate_shared_key(
self.private_key.as_ref().unwrap(),
self.remote_public_key.unwrap(),
&self.salt.as_mut().unwrap(),
));
Ok(self.clone())
Ok(self)
}

pub async fn from_stream_with_salt(
&mut self,
stream: &mut Socket,
) -> Result<OKE<'a>, OblivionException> {
) -> Result<&mut Self, OblivionException> {
let remote_public_key_length = stream.recv_len().await?;
let remote_public_key_bytes = stream.recv(remote_public_key_length).await;
self.remote_public_key = Some(PublicKey::from_sec1_bytes(&remote_public_key_bytes).unwrap());
self.remote_public_key =
Some(PublicKey::from_sec1_bytes(&remote_public_key_bytes).unwrap());
let salt_length = stream.recv_len().await?;
self.salt = Some(stream.recv(salt_length).await);
self.shared_aes_key = Some(generate_shared_key(
self.private_key.as_ref().unwrap(),
self.remote_public_key.unwrap(),
&self.salt.as_mut().unwrap(),
));
Ok(self.clone())
Ok(self)
}

pub async fn to_stream(&mut self, stream: &mut Socket) {
Expand All @@ -165,7 +157,7 @@ impl<'a> OKE<'a> {
}

pub fn plain_salt(&mut self) -> Vec<u8> {
let salt_bytes = self.salt.clone().unwrap();
let salt_bytes = self.salt.as_ref().unwrap();
let mut plain_salt_bytes = length(&salt_bytes).unwrap();
plain_salt_bytes.extend(salt_bytes);
plain_salt_bytes
Expand All @@ -177,7 +169,6 @@ impl<'a> OKE<'a> {
}

pub struct OED {
length: Option<i32>,
aes_key: Option<Vec<u8>>,
data: Option<Vec<u8>>,
encrypted_data: Option<Vec<u8>>,
Expand All @@ -189,7 +180,6 @@ pub struct OED {
impl OED {
pub fn new(aes_key: Option<Vec<u8>>) -> Self {
Self {
length: None,
aes_key: aes_key,
data: None,
encrypted_data: None,
Expand All @@ -199,18 +189,6 @@ impl OED {
}
}

fn clone(&mut self) -> Self {
Self {
length: self.length.clone(),
aes_key: self.aes_key.clone(),
data: self.data.clone(),
encrypted_data: self.encrypted_data.clone(),
tag: self.tag.clone(),
nonce: self.nonce.clone(),
chunk_size: self.chunk_size.clone(),
}
}

fn serialize_bytes(&self, data: &[u8], size: Option<usize>) -> Vec<Vec<u8>> {
let size = if size.is_none() {
let size: usize = 1024;
Expand Down Expand Up @@ -245,39 +223,39 @@ impl OED {
serialized_bytes
}

pub fn from_json_or_string(&mut self, json_or_str: String) -> Result<Self, ()> {
pub fn from_json_or_string(&mut self, json_or_str: String) -> Result<&mut Self, ()> {
let (encrypted_data, tag, nonce) =
encrypt_message(json_or_str, &self.aes_key.clone().unwrap());
encrypt_message(json_or_str, &self.aes_key.as_ref().unwrap());
(self.encrypted_data, self.tag, self.nonce) =
(Some(encrypted_data), Some(tag), Some(nonce));
Ok(self.clone())
Ok(self)
}

pub fn from_dict(&mut self, dict: Value) -> Result<Self, OblivionException> {
pub fn from_dict(&mut self, dict: Value) -> Result<&mut Self, OblivionException> {
let (encrypted_data, tag, nonce) =
encrypt_message(dict.to_string(), &self.aes_key.clone().unwrap());
encrypt_message(dict.to_string(), &self.aes_key.as_ref().unwrap());
(self.encrypted_data, self.tag, self.nonce) =
(Some(encrypted_data), Some(tag), Some(nonce));
Ok(self.clone())
Ok(self)
}

pub fn from_encrypted_data(&mut self, data: Vec<u8>) -> Result<Self, ()> {
pub fn from_encrypted_data(&mut self, data: Vec<u8>) -> Result<&mut Self, ()> {
self.encrypted_data = Some(data);
Ok(self.clone())
Ok(self)
}

pub fn from_bytes(&mut self, data: Vec<u8>) -> Result<Self, OblivionException> {
let (encrypted_data, tag, nonce) = encrypt_bytes(data, &self.aes_key.clone().unwrap());
pub fn from_bytes(&mut self, data: Vec<u8>) -> Result<&mut Self, OblivionException> {
let (encrypted_data, tag, nonce) = encrypt_bytes(data, &self.aes_key.as_ref().unwrap());
(self.encrypted_data, self.tag, self.nonce) =
(Some(encrypted_data), Some(tag), Some(nonce));
Ok(self.clone())
Ok(self)
}

pub async fn from_stream(
&mut self,
stream: &mut Socket,
total_attemps: i32,
) -> Result<Self, OblivionException> {
) -> Result<&mut Self, OblivionException> {
let mut attemp = 0;
let mut ack = false;

Expand All @@ -297,7 +275,7 @@ impl OED {
loop {
let prefix = stream.recv_len().await?;
if prefix == 0 {
self.encrypted_data = Some(encrypted_data.clone());
self.encrypted_data = Some(encrypted_data);
break;
}

Expand All @@ -312,9 +290,9 @@ impl OED {

match decrypt_bytes(
self.encrypted_data.clone().unwrap(),
&self.tag.as_ref().unwrap(),
&self.aes_key.as_ref().unwrap(),
&self.nonce.as_ref().unwrap(),
self.tag.as_ref().unwrap(),
self.aes_key.as_ref().unwrap(),
self.nonce.as_ref().unwrap(),
) {
Ok(data) => {
self.data = Some(data);
Expand All @@ -338,7 +316,7 @@ impl OED {
))));
}

Ok(self.clone())
Ok(self)
}

pub async fn to_stream(
Expand All @@ -357,7 +335,7 @@ impl OED {

self.chunk_size = 0;
for bytes in self
.serialize_bytes(&self.encrypted_data.clone().unwrap(), None)
.serialize_bytes(&self.encrypted_data.as_ref().unwrap(), None)
.iter()
{
stream.send(&bytes).await;
Expand All @@ -382,11 +360,11 @@ impl OED {
}

pub fn plain_data(&mut self) -> Vec<u8> {
let nonce_bytes = self.nonce.clone().unwrap();
let tag_bytes = self.tag.clone().unwrap();
let nonce_bytes = self.nonce.as_ref().unwrap();
let tag_bytes = self.tag.as_ref().unwrap();

let mut plain_bytes = length(&nonce_bytes).unwrap();
plain_bytes.extend(length(&tag_bytes).unwrap());
let mut plain_bytes = length(nonce_bytes).unwrap();
plain_bytes.extend(length(tag_bytes).unwrap());
plain_bytes.extend(nonce_bytes);
plain_bytes.extend(tag_bytes);

Expand Down
4 changes: 2 additions & 2 deletions src/models/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl TextResponse {
}

pub fn get_status_code(&mut self) -> i32 {
self.status_code.clone()
self.status_code
}
}

Expand All @@ -51,7 +51,7 @@ impl JsonResponse {
}

pub fn get_status_code(&mut self) -> i32 {
self.status_code.clone()
self.status_code
}
}

Expand Down
Loading

0 comments on commit 8bdd8e3

Please sign in to comment.