Skip to content

Commit

Permalink
perf: improve exception pass and lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 committed Apr 13, 2024
1 parent 24bc308 commit eb9d236
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/models/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Request {
data: Option<Value>,
file: Option<Vec<u8>>,
tfo: bool,
) -> Result<Self, OblivionException> {
) -> Result<Self> {
let method = method.to_uppercase();
let path = OblivionPath::new(&olps)?;
let olps = path.get_olps();
Expand Down
2 changes: 1 addition & 1 deletion src/models/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ async fn _handle(
stream: &mut Socket,
peer: SocketAddr,
) -> Result<(OblivionRequest, i32)> {
stream.set_ttl(20);
stream.set_ttl(20)?;
let mut connection = ServerConnection::new()?;
let mut request = match connection.handshake(stream, peer).await {
Ok(request) => request,
Expand Down
6 changes: 3 additions & 3 deletions src/utils/decryptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ pub fn decrypt_bytes(
nonce: &[u8],
) -> Result<Vec<u8>, Unspecified> {
// 使用 AES_KEY 加密
let unbound_key = UnboundKey::new(&AES_128_GCM, &aes_key)?;
let nonce_sequence = AbsoluteNonceSequence::new(nonce.to_vec());
let unbound_key = UnboundKey::new(&AES_128_GCM, aes_key)?;
let nonce_sequence = AbsoluteNonceSequence::new(nonce);

let mut opening_key = OpeningKey::new(unbound_key, nonce_sequence);
let mut in_out = [cipherbytes.clone(), tag.to_vec()].concat(); // 复制一份
let mut in_out = [cipherbytes, tag.to_vec()].concat(); // 复制一份
let decrypted_data = opening_key.open_in_place(Aad::empty(), &mut in_out)?;

Ok(decrypted_data.to_vec())
Expand Down
2 changes: 1 addition & 1 deletion src/utils/encryptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn encrypt_bytes(
let rand = SystemRandom::new();
rand.fill(&mut nonce_bytes).unwrap();

let nonce_sequence = AbsoluteNonceSequence::new(nonce_bytes.clone());
let nonce_sequence = AbsoluteNonceSequence::new(&nonce_bytes);
let mut sealing_key = SealingKey::new(unbound_key, nonce_sequence);

let associated_data = Aad::empty();
Expand Down
21 changes: 12 additions & 9 deletions src/utils/gear.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Oblivion Abstract Gear
use crate::exceptions::OblivionException;

use anyhow::Result;
use ring::{
aead::{Nonce, NonceSequence},
error::Unspecified,
Expand All @@ -15,19 +17,19 @@ use tokio::{
///
/// Warning: this is not a generalized generation scheme and should not be used in production environments,
/// you should make sure that the Nonce you pass in is a sufficiently garbled byte string.
pub struct AbsoluteNonceSequence {
nonce: Vec<u8>,
pub struct AbsoluteNonceSequence<'a> {
nonce: &'a [u8],
}

impl NonceSequence for AbsoluteNonceSequence {
impl<'a> NonceSequence for AbsoluteNonceSequence<'a> {
fn advance(&mut self) -> Result<Nonce, Unspecified> {
Nonce::try_assume_unique_for_key(&self.nonce)
Nonce::try_assume_unique_for_key(self.nonce)
}
}

impl AbsoluteNonceSequence {
pub fn new(nonce: Vec<u8>) -> Self {
Self { nonce: nonce }
impl<'a> AbsoluteNonceSequence<'a> {
pub fn new(nonce: &'a [u8]) -> Self {
Self { nonce }
}
}

Expand All @@ -43,8 +45,9 @@ impl Socket {
Self { tcp }
}

pub fn set_ttl(&mut self, ttl: u32) {
self.tcp.set_ttl(ttl).unwrap()
pub fn set_ttl(&mut self, ttl: u32) -> Result<()> {
self.tcp.set_ttl(ttl)?;
Ok(())
}

pub async fn recv_len(&mut self) -> Result<usize, OblivionException> {
Expand Down
14 changes: 7 additions & 7 deletions src/utils/parser.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! # Oblivion Parser
//!
//! Used to parse and reconstruct data and store it.
use anyhow::{Error, Result};
use regex::Regex;
use serde_json::Value;
use std::collections::HashMap;
use std::net::SocketAddr;

use crate::exceptions::OblivionException;
use regex::Regex;
use serde_json::Value;

/// Packet size analysis function
///
Expand Down Expand Up @@ -60,11 +61,10 @@ pub struct OblivionPath {
}

impl OblivionPath {
pub fn new(obl_str: &str) -> Result<Self, OblivionException> {
pub fn new(obl_str: &str) -> Result<Self> {
let re = Regex::new(
r"^(?P<protocol>oblivion)?(?:://)?(?P<host>[^:/]+)(:(?P<port>\d+))?(?P<olps>.+)?$",
)
.unwrap();
)?;

if let Some(captures) = re.captures(obl_str) {
let mut extracted_values: HashMap<&str, Option<&str>> = HashMap::new();
Expand Down Expand Up @@ -99,9 +99,9 @@ impl OblivionPath {
olps,
})
} else {
Err(OblivionException::InvalidOblivion {
Err(Error::from(OblivionException::InvalidOblivion {
olps: obl_str.to_string(),
})
}))
}
}

Expand Down

0 comments on commit eb9d236

Please sign in to comment.