Visitor changed to AddVisitor
This commit is contained in:
parent
5264430936
commit
24e75f7cc9
7 changed files with 30 additions and 26 deletions
|
@ -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`
|
||||
|
|
|
@ -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()
|
||||
|
|
4
src/cache/hashcache.rs
vendored
4
src/cache/hashcache.rs
vendored
|
@ -98,7 +98,7 @@ impl Handler<Retrive> 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,
|
||||
};
|
||||
|
|
4
src/cache/mod.rs
vendored
4
src/cache/mod.rs
vendored
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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<DeleteVisitor> 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<Visitor> for MCaptcha {
|
||||
type Result = MessageResult<Visitor>;
|
||||
impl Handler<AddVisitor> for MCaptcha {
|
||||
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;
|
||||
|
||||
let addr = ctx.address();
|
||||
|
@ -225,7 +225,7 @@ impl Handler<Visitor> 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<MCaptcha>, 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);
|
||||
}
|
||||
|
||||
|
|
|
@ -43,13 +43,13 @@ where
|
|||
pub async fn get_pow(&self, id: String) -> Option<PoWConfig> {
|
||||
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);
|
||||
|
|
Loading…
Reference in a new issue