From 24e75f7cc968e0ad5a2335d47d60a501473dab04 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Mon, 15 Mar 2021 19:30:46 +0530 Subject: [PATCH] Visitor changed to AddVisitor --- CHANGELOG.md | 2 ++ examples/simple.rs | 2 ++ src/cache/hashcache.rs | 4 ++-- src/cache/mod.rs | 4 ++-- src/lib.rs | 6 +++--- src/mcaptcha.rs | 34 +++++++++++++++++----------------- src/system.rs | 4 ++-- 7 files changed, 30 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3ce2bc..1299ecf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ # 0.1.1 - typo fix: `MCaptcha::decrement_visiotr()` became `MCaptcha::decrement_visitor()` - `serde::{Serialize, Deserialize}` impls (shouldn't break anything) +- `MCaptcha` throws error when duration is 0 +- `Visitor` is changed to `AddVisitor` diff --git a/examples/simple.rs b/examples/simple.rs index a386a73..bb196b0 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -67,6 +67,8 @@ async fn main() -> std::io::Result<()> { .build() .unwrap(); + p + // create and start MCaptcha actor that uses the above defense configuration // This is what manages the difficulty factor of sites that an mCaptcha protects let mcaptcha = MCaptchaBuilder::default() diff --git a/src/cache/hashcache.rs b/src/cache/hashcache.rs index 0272cad..d21ba9d 100644 --- a/src/cache/hashcache.rs +++ b/src/cache/hashcache.rs @@ -98,7 +98,7 @@ impl Handler for HashCache { #[cfg(test)] mod tests { use super::*; - use crate::mcaptcha::VisitorResult; + use crate::mcaptcha::AddVisitorResult; use crate::pow::PoWConfig; async fn sleep(time: u64) { @@ -115,7 +115,7 @@ mod tests { const DURATION: u64 = 5; let addr = HashCache::default().start(); let pow: PoWConfig = PoWConfig::new(DIFFICULTY_FACTOR); - let visitor_result = VisitorResult { + let visitor_result = AddVisitorResult { difficulty_factor: DIFFICULTY_FACTOR, duration: DURATION, }; diff --git a/src/cache/mod.rs b/src/cache/mod.rs index f43370d..0c32b37 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -34,7 +34,7 @@ pub mod messages { use serde::{Deserialize, Serialize}; use crate::errors::*; - use crate::mcaptcha::VisitorResult; + use crate::mcaptcha::AddVisitorResult; use crate::pow::PoWConfig; /// Message to cache PoW difficulty factor and string @@ -47,7 +47,7 @@ pub mod messages { } impl Cache { - pub fn new(p: &PoWConfig, v: &VisitorResult) -> Self { + pub fn new(p: &PoWConfig, v: &AddVisitorResult) -> Self { CacheBuilder::default() .string(p.string.clone()) .difficulty_factor(v.difficulty_factor) diff --git a/src/lib.rs b/src/lib.rs index 0a60aba..f642720 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,8 +31,8 @@ //! - Difficulty(Factor): Minimum ammount of work that a client must do to make a valid //! request. //! - [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 -//! your service is. Determines mCaptcha defense defense +//! - [Visitor][crate::mcaptcha::AddVisitor]: Smallest unit of traffic, usually a single request. +//! The more you have, the busier your service is. Determines mCaptcha 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 //! PoW requirements to defend against replay attacks and dictionary attacks. @@ -160,7 +160,7 @@ //! Ok(()) //! } //! ``` - +#![forbid(unsafe_code)] pub mod defense; pub mod errors; pub mod master; diff --git a/src/mcaptcha.rs b/src/mcaptcha.rs index cf7aefe..625f9fb 100644 --- a/src/mcaptcha.rs +++ b/src/mcaptcha.rs @@ -19,7 +19,7 @@ //! //! ## Usage: //! ```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 //! use actix::prelude::*; //! @@ -66,7 +66,7 @@ //! .start(); //! //! // increment count when user visits protected routes -//! mcaptcha.send(Visitor).await.unwrap(); +//! mcaptcha.send(AddVisitor).await.unwrap(); //! //! Ok(()) //! } @@ -188,30 +188,30 @@ impl Handler for MCaptcha { /// Message to increment the visitor count /// returns difficulty factor and lifetime #[derive(Message)] -#[rtype(result = "VisitorResult")] -pub struct Visitor; +#[rtype(result = "AddVisitorResult")] +pub struct AddVisitor; /// Struct representing the return datatime of -/// [Visitor] message. Contains MCaptcha lifetime +/// [AddVisitor] message. Contains MCaptcha lifetime /// and difficulty factor -pub struct VisitorResult { +pub struct AddVisitorResult { pub duration: u64, pub difficulty_factor: u32, } -impl VisitorResult { +impl AddVisitorResult { fn new(m: &MCaptcha) -> Self { - VisitorResult { + AddVisitorResult { duration: m.get_duration(), difficulty_factor: m.get_difficulty(), } } } -impl Handler for MCaptcha { - type Result = MessageResult; +impl Handler for MCaptcha { + type Result = MessageResult; - 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; let addr = ctx.address(); @@ -225,7 +225,7 @@ impl Handler for MCaptcha { ctx.spawn(wait_for); self.add_visitor(); - MessageResult(VisitorResult::new(&self)) + MessageResult(AddVisitorResult::new(&self)) } } @@ -268,7 +268,7 @@ pub mod tests { async fn race(addr: Addr, count: (u32, u32)) { 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() { 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); 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); } @@ -299,13 +299,13 @@ pub mod tests { 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); let duration = Duration::new(DURATION, 0); delay_for(duration).await; - mcaptcha = addr.send(Visitor).await.unwrap(); + mcaptcha = addr.send(AddVisitor).await.unwrap(); assert_eq!(mcaptcha.difficulty_factor, LEVEL_1.1); } diff --git a/src/system.rs b/src/system.rs index b5b9121..b58da70 100644 --- a/src/system.rs +++ b/src/system.rs @@ -43,13 +43,13 @@ where pub async fn get_pow(&self, id: String) -> Option { use crate::cache::messages::Cache; use crate::master::GetSite; - use crate::mcaptcha::Visitor; + use crate::mcaptcha::AddVisitor; let site_addr = self.master.send(GetSite(id)).await.unwrap(); if site_addr.is_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 cache_msg = Cache::new(&pow_config, &mcaptcha);