Visitor changed to AddVisitor

This commit is contained in:
Aravinth Manivannan 2021-03-15 19:30:46 +05:30
parent 5264430936
commit 24e75f7cc9
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
7 changed files with 30 additions and 26 deletions

View file

@ -1,3 +1,5 @@
# 0.1.1 # 0.1.1
- typo fix: `MCaptcha::decrement_visiotr()` became `MCaptcha::decrement_visitor()` - typo fix: `MCaptcha::decrement_visiotr()` became `MCaptcha::decrement_visitor()`
- `serde::{Serialize, Deserialize}` impls (shouldn't break anything) - `serde::{Serialize, Deserialize}` impls (shouldn't break anything)
- `MCaptcha` throws error when duration is 0
- `Visitor` is changed to `AddVisitor`

View file

@ -67,6 +67,8 @@ async fn main() -> std::io::Result<()> {
.build() .build()
.unwrap(); .unwrap();
p
// create and start MCaptcha actor that uses the above defense configuration // create and start MCaptcha actor that uses the above defense configuration
// This is what manages the difficulty factor of sites that an mCaptcha protects // This is what manages the difficulty factor of sites that an mCaptcha protects
let mcaptcha = MCaptchaBuilder::default() let mcaptcha = MCaptchaBuilder::default()

View file

@ -98,7 +98,7 @@ impl Handler<Retrive> for HashCache {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::mcaptcha::VisitorResult; use crate::mcaptcha::AddVisitorResult;
use crate::pow::PoWConfig; use crate::pow::PoWConfig;
async fn sleep(time: u64) { async fn sleep(time: u64) {
@ -115,7 +115,7 @@ mod tests {
const DURATION: u64 = 5; const DURATION: u64 = 5;
let addr = HashCache::default().start(); let addr = HashCache::default().start();
let pow: PoWConfig = PoWConfig::new(DIFFICULTY_FACTOR); let pow: PoWConfig = PoWConfig::new(DIFFICULTY_FACTOR);
let visitor_result = VisitorResult { let visitor_result = AddVisitorResult {
difficulty_factor: DIFFICULTY_FACTOR, difficulty_factor: DIFFICULTY_FACTOR,
duration: DURATION, duration: DURATION,
}; };

4
src/cache/mod.rs vendored
View file

@ -34,7 +34,7 @@ pub mod messages {
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::errors::*; use crate::errors::*;
use crate::mcaptcha::VisitorResult; use crate::mcaptcha::AddVisitorResult;
use crate::pow::PoWConfig; use crate::pow::PoWConfig;
/// Message to cache PoW difficulty factor and string /// Message to cache PoW difficulty factor and string
@ -47,7 +47,7 @@ pub mod messages {
} }
impl Cache { impl Cache {
pub fn new(p: &PoWConfig, v: &VisitorResult) -> Self { pub fn new(p: &PoWConfig, v: &AddVisitorResult) -> Self {
CacheBuilder::default() CacheBuilder::default()
.string(p.string.clone()) .string(p.string.clone())
.difficulty_factor(v.difficulty_factor) .difficulty_factor(v.difficulty_factor)

View file

@ -31,8 +31,8 @@
//! - Difficulty(Factor): Minimum ammount of work that a client must do to make a valid //! - Difficulty(Factor): Minimum ammount of work that a client must do to make a valid
//! request. //! request.
//! - [Defense]: A datatype that various visitor-difficulty mappigns //! - [Defense]: A datatype that various visitor-difficulty mappigns
//! - [Visitor][crate::mcaptcha::Visitor]: Smallest unit of traffic, usually a single request. The more you have, the busier //! - [Visitor][crate::mcaptcha::AddVisitor]: Smallest unit of traffic, usually a single request.
//! your service is. Determines mCaptcha defense defense //! The more you have, the busier your service is. Determines mCaptcha defense defense
//! - Visitor threshold: The threshold at which [MCaptcha] will adjust defense defense //! - Visitor threshold: The threshold at which [MCaptcha] will adjust defense defense
//! - [Cache][crate::cache] : A datatype that implements [Save][crate::cache::Save]. Used to store //! - [Cache][crate::cache] : A datatype that implements [Save][crate::cache::Save]. Used to store
//! PoW requirements to defend against replay attacks and dictionary attacks. //! PoW requirements to defend against replay attacks and dictionary attacks.
@ -160,7 +160,7 @@
//! Ok(()) //! Ok(())
//! } //! }
//! ``` //! ```
#![forbid(unsafe_code)]
pub mod defense; pub mod defense;
pub mod errors; pub mod errors;
pub mod master; pub mod master;

View file

@ -19,7 +19,7 @@
//! //!
//! ## Usage: //! ## Usage:
//! ```rust //! ```rust
//! use m_captcha::{mcaptcha::Visitor, MCaptchaBuilder, cache::HashCache, LevelBuilder, DefenseBuilder}; //! use m_captcha::{mcaptcha::AddVisitor, MCaptchaBuilder, cache::HashCache, LevelBuilder, DefenseBuilder};
//! // traits from actix needs to be in scope for starting actor //! // traits from actix needs to be in scope for starting actor
//! use actix::prelude::*; //! use actix::prelude::*;
//! //!
@ -66,7 +66,7 @@
//! .start(); //! .start();
//! //!
//! // increment count when user visits protected routes //! // increment count when user visits protected routes
//! mcaptcha.send(Visitor).await.unwrap(); //! mcaptcha.send(AddVisitor).await.unwrap();
//! //!
//! Ok(()) //! Ok(())
//! } //! }
@ -188,30 +188,30 @@ impl Handler<DeleteVisitor> for MCaptcha {
/// Message to increment the visitor count /// Message to increment the visitor count
/// returns difficulty factor and lifetime /// returns difficulty factor and lifetime
#[derive(Message)] #[derive(Message)]
#[rtype(result = "VisitorResult")] #[rtype(result = "AddVisitorResult")]
pub struct Visitor; pub struct AddVisitor;
/// Struct representing the return datatime of /// Struct representing the return datatime of
/// [Visitor] message. Contains MCaptcha lifetime /// [AddVisitor] message. Contains MCaptcha lifetime
/// and difficulty factor /// and difficulty factor
pub struct VisitorResult { pub struct AddVisitorResult {
pub duration: u64, pub duration: u64,
pub difficulty_factor: u32, pub difficulty_factor: u32,
} }
impl VisitorResult { impl AddVisitorResult {
fn new(m: &MCaptcha) -> Self { fn new(m: &MCaptcha) -> Self {
VisitorResult { AddVisitorResult {
duration: m.get_duration(), duration: m.get_duration(),
difficulty_factor: m.get_difficulty(), difficulty_factor: m.get_difficulty(),
} }
} }
} }
impl Handler<Visitor> for MCaptcha { impl Handler<AddVisitor> for MCaptcha {
type Result = MessageResult<Visitor>; type Result = MessageResult<AddVisitor>;
fn handle(&mut self, _: Visitor, ctx: &mut Self::Context) -> Self::Result { fn handle(&mut self, _: AddVisitor, ctx: &mut Self::Context) -> Self::Result {
use actix::clock::delay_for; use actix::clock::delay_for;
let addr = ctx.address(); let addr = ctx.address();
@ -225,7 +225,7 @@ impl Handler<Visitor> for MCaptcha {
ctx.spawn(wait_for); ctx.spawn(wait_for);
self.add_visitor(); self.add_visitor();
MessageResult(VisitorResult::new(&self)) MessageResult(AddVisitorResult::new(&self))
} }
} }
@ -268,7 +268,7 @@ pub mod tests {
async fn race(addr: Addr<MCaptcha>, count: (u32, u32)) { async fn race(addr: Addr<MCaptcha>, count: (u32, u32)) {
for _ in 0..count.0 as usize - 1 { for _ in 0..count.0 as usize - 1 {
let _ = addr.send(Visitor).await.unwrap(); let _ = addr.send(AddVisitor).await.unwrap();
} }
} }
@ -284,11 +284,11 @@ pub mod tests {
async fn counter_defense_tightenup_works() { async fn counter_defense_tightenup_works() {
let addr: MyActor = get_counter().start(); let addr: MyActor = get_counter().start();
let mut mcaptcha = addr.send(Visitor).await.unwrap(); let mut mcaptcha = addr.send(AddVisitor).await.unwrap();
assert_eq!(mcaptcha.difficulty_factor, LEVEL_1.0); assert_eq!(mcaptcha.difficulty_factor, LEVEL_1.0);
race(addr.clone(), LEVEL_2).await; race(addr.clone(), LEVEL_2).await;
mcaptcha = addr.send(Visitor).await.unwrap(); mcaptcha = addr.send(AddVisitor).await.unwrap();
assert_eq!(mcaptcha.difficulty_factor, LEVEL_2.1); assert_eq!(mcaptcha.difficulty_factor, LEVEL_2.1);
} }
@ -299,13 +299,13 @@ pub mod tests {
race(addr.clone(), LEVEL_2).await; race(addr.clone(), LEVEL_2).await;
race(addr.clone(), LEVEL_2).await; race(addr.clone(), LEVEL_2).await;
let mut mcaptcha = addr.send(Visitor).await.unwrap(); let mut mcaptcha = addr.send(AddVisitor).await.unwrap();
assert_eq!(mcaptcha.difficulty_factor, LEVEL_2.1); assert_eq!(mcaptcha.difficulty_factor, LEVEL_2.1);
let duration = Duration::new(DURATION, 0); let duration = Duration::new(DURATION, 0);
delay_for(duration).await; delay_for(duration).await;
mcaptcha = addr.send(Visitor).await.unwrap(); mcaptcha = addr.send(AddVisitor).await.unwrap();
assert_eq!(mcaptcha.difficulty_factor, LEVEL_1.1); assert_eq!(mcaptcha.difficulty_factor, LEVEL_1.1);
} }

View file

@ -43,13 +43,13 @@ where
pub async fn get_pow(&self, id: String) -> Option<PoWConfig> { pub async fn get_pow(&self, id: String) -> Option<PoWConfig> {
use crate::cache::messages::Cache; use crate::cache::messages::Cache;
use crate::master::GetSite; use crate::master::GetSite;
use crate::mcaptcha::Visitor; use crate::mcaptcha::AddVisitor;
let site_addr = self.master.send(GetSite(id)).await.unwrap(); let site_addr = self.master.send(GetSite(id)).await.unwrap();
if site_addr.is_none() { if site_addr.is_none() {
return None; return None;
} }
let mcaptcha = site_addr.unwrap().send(Visitor).await.unwrap(); let mcaptcha = site_addr.unwrap().send(AddVisitor).await.unwrap();
let pow_config = PoWConfig::new(mcaptcha.difficulty_factor); let pow_config = PoWConfig::new(mcaptcha.difficulty_factor);
let cache_msg = Cache::new(&pow_config, &mcaptcha); let cache_msg = Cache::new(&pow_config, &mcaptcha);