-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
93 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,76 @@ | ||
#[cfg(feature = "aead-cipher-2022")] | ||
use std::time::Duration; | ||
|
||
use cfg_if::cfg_if; | ||
#[cfg(feature = "aead-cipher-2022")] | ||
use lru_time_cache::LruCache; | ||
#[allow(dead_code)] | ||
use spin::Mutex as SpinMutex; | ||
|
||
#[cfg(feature = "aead-cipher-2022")] | ||
use crate::relay::tcprelay::proxy_stream::protocol::v2::SERVER_STREAM_TIMESTAMP_MAX_DIFF; | ||
use crate::{config::ServerType, crypto::CipherKind}; | ||
|
||
#[cfg(feature = "security-replay-attack-detect")] | ||
use self::ppbloom::PingPongBloom; | ||
|
||
#[cfg(feature = "security-replay-attack-detect")] | ||
mod ppbloom; | ||
|
||
/// A Bloom Filter based protector against replay attach | ||
pub struct ReplayProtector { | ||
// Check for duplicated IV/Nonce, for prevent replay attack | ||
// https://github.com/shadowsocks/shadowsocks-org/issues/44 | ||
#[cfg(feature = "security-replay-attack-detect")] | ||
nonce_ppbloom: SpinMutex<PingPongBloom>, | ||
|
||
// AEAD 2022 specific filter. | ||
// AEAD 2022 TCP protocol has a timestamp, which can already reject most of the replay requests, | ||
// so we only need to remember nonce that are in the valid time range | ||
#[cfg(feature = "aead-cipher-2022")] | ||
nonce_set: SpinMutex<LruCache<Vec<u8>, ()>>, | ||
} | ||
|
||
impl ReplayProtector { | ||
/// Create a new ReplayProtector | ||
#[allow(unused_variables)] | ||
pub fn new(config_type: ServerType) -> ReplayProtector { | ||
ReplayProtector { | ||
#[cfg(feature = "security-replay-attack-detect")] | ||
nonce_ppbloom: SpinMutex::new(PingPongBloom::new(config_type)), | ||
#[cfg(feature = "aead-cipher-2022")] | ||
nonce_set: SpinMutex::new(LruCache::with_expiry_duration(Duration::from_secs( | ||
SERVER_STREAM_TIMESTAMP_MAX_DIFF, | ||
))), | ||
} | ||
} | ||
|
||
/// Check if nonce exist or not | ||
#[inline(always)] | ||
pub fn check_nonce_and_set(&self, method: CipherKind, nonce: &[u8]) -> bool { | ||
// Plain cipher doesn't have a nonce | ||
// Always treated as non-duplicated | ||
if nonce.is_empty() { | ||
return false; | ||
} | ||
|
||
#[cfg(feature = "aead-cipher-2022")] | ||
if method.is_aead_2022() { | ||
let mut set = self.nonce_set.lock(); | ||
if set.get(nonce).is_some() { | ||
return true; | ||
} | ||
set.insert(nonce.to_vec(), ()); | ||
return false; | ||
} | ||
|
||
cfg_if! { | ||
if #[cfg(feature = "security-replay-attack-detect")] { | ||
mod ppbloom; | ||
pub use self::ppbloom::*; | ||
} else { | ||
mod dummy; | ||
pub use self::dummy::*; | ||
cfg_if! { | ||
if #[cfg(feature = "security-replay-attack-detect")] { | ||
let mut ppbloom = self.nonce_ppbloom.lock(); | ||
ppbloom.check_and_set(nonce) | ||
} else { | ||
false | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.