From 236109c6287cbef29fcd64ef40d64968487a4a31 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Thu, 3 Jun 2021 18:19:57 +0530 Subject: [PATCH] moved master and mcaptcha to embedded --- examples/simple.rs | 2 +- src/cache/hashcache.rs | 2 +- src/lib.rs | 5 ++--- .../{embedded.rs => embedded/master.rs} | 13 ++++++------ src/{ => master/embedded}/mcaptcha.rs | 12 ++--------- src/master/embedded/mod.rs | 20 +++++++++++++++++++ src/master/mod.rs | 14 +++++++++++-- src/system.rs | 11 +++++----- 8 files changed, 51 insertions(+), 28 deletions(-) rename src/master/{embedded.rs => embedded/master.rs} (95%) rename src/{ => master/embedded}/mcaptcha.rs (96%) create mode 100644 src/master/embedded/mod.rs diff --git a/examples/simple.rs b/examples/simple.rs index ac90910..e61668d 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -1,6 +1,6 @@ use libmcaptcha::{ cache::{messages::VerifyCaptchaResult, HashCache}, - master::embedded::{AddSiteBuilder, Master}, + master::embedded::master::{AddSiteBuilder, Master}, pow::{ConfigBuilder, Work}, system::SystemBuilder, DefenseBuilder, LevelBuilder, MCaptchaBuilder, diff --git a/src/cache/hashcache.rs b/src/cache/hashcache.rs index b6a1f47..750d216 100644 --- a/src/cache/hashcache.rs +++ b/src/cache/hashcache.rs @@ -178,7 +178,7 @@ impl Handler for HashCache { #[cfg(test)] mod tests { use super::*; - use crate::mcaptcha::AddVisitorResult; + use crate::master::AddVisitorResult; use crate::pow::PoWConfig; // async fn sleep(time: u64) { diff --git a/src/lib.rs b/src/lib.rs index 7c34576..636feaa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,7 +48,7 @@ //! ```rust //! use libmcaptcha::{ //! cache::{messages::VerifyCaptchaResult, HashCache}, -//! master::embedded::{AddSiteBuilder, Master}, +//! master::embedded::master::{AddSiteBuilder, Master}, //! pow::{ConfigBuilder, Work}, //! system::SystemBuilder, //! DefenseBuilder, LevelBuilder, MCaptchaBuilder, @@ -186,7 +186,6 @@ pub mod defense; pub mod errors; pub mod master; -pub mod mcaptcha; /// message datatypes to interact with [MCaptcha] actor pub mod cache; @@ -197,4 +196,4 @@ mod utils; pub use crate::cache::hashcache::HashCache; pub use defense::{Defense, DefenseBuilder, LevelBuilder}; -pub use mcaptcha::{MCaptcha, MCaptchaBuilder}; +pub use master::embedded::mcaptcha::{MCaptcha, MCaptchaBuilder}; diff --git a/src/master/embedded.rs b/src/master/embedded/master.rs similarity index 95% rename from src/master/embedded.rs rename to src/master/embedded/master.rs index 6aea1a6..682a54a 100644 --- a/src/master/embedded.rs +++ b/src/master/embedded/master.rs @@ -26,8 +26,9 @@ use actix::dev::*; use derive_builder::Builder; use log::info; -use super::*; -use crate::mcaptcha::MCaptcha; +use super::mcaptcha::MCaptcha; +use crate::master::AddVisitor; +use crate::master::Master as MasterTrait; /// This Actor manages the [MCaptcha] actors. /// A service can have several [MCaptcha] actors with @@ -39,7 +40,7 @@ pub struct Master { gc: u64, } -impl Counter for Master {} +impl MasterTrait for Master {} impl Master { /// add [MCaptcha] actor to [Master] @@ -98,7 +99,7 @@ impl Handler for Master { let fut = async move { let config = addr .unwrap() - .send(crate::mcaptcha::AddVisitor) + .send(super::mcaptcha::AddVisitor) .await .unwrap(); @@ -144,7 +145,7 @@ impl Handler for Master { info!("init master actor cleanup up"); let task = async move { for (id, (new, addr)) in sites.iter() { - use crate::mcaptcha::{GetCurrentVisitorCount, Stop}; + use super::mcaptcha::{GetCurrentVisitorCount, Stop}; let visitor_count = addr.send(GetCurrentVisitorCount).await.unwrap(); println!("{}", visitor_count); if visitor_count == 0 && new.is_some() { @@ -196,7 +197,7 @@ impl Handler for Master { #[cfg(test)] mod tests { use super::*; - use crate::mcaptcha::tests::*; + use crate::master::embedded::mcaptcha::tests::*; #[actix_rt::test] async fn master_actor_works() { diff --git a/src/mcaptcha.rs b/src/master/embedded/mcaptcha.rs similarity index 96% rename from src/mcaptcha.rs rename to src/master/embedded/mcaptcha.rs index c272215..c8d3977 100644 --- a/src/mcaptcha.rs +++ b/src/master/embedded/mcaptcha.rs @@ -19,7 +19,7 @@ //! //! ## Usage: //! ```rust -//! use libmcaptcha::{mcaptcha::AddVisitor, MCaptchaBuilder, cache::HashCache, LevelBuilder, DefenseBuilder}; +//! use libmcaptcha::{master::embedded::mcaptcha::AddVisitor, MCaptchaBuilder, cache::HashCache, LevelBuilder, DefenseBuilder}; //! // traits from actix needs to be in scope for starting actor //! use actix::prelude::*; //! @@ -81,6 +81,7 @@ use serde::{Deserialize, Serialize}; use crate::{ defense::Defense, errors::{CaptchaError, CaptchaResult}, + master::AddVisitorResult, }; /// Builder for [MCaptcha] @@ -192,15 +193,6 @@ impl Handler for MCaptcha { #[rtype(result = "AddVisitorResult")] pub struct AddVisitor; -/// Struct representing the return datatime of -/// [AddVisitor] message. Contains MCaptcha lifetime -/// and difficulty factor -#[derive(Debug, Clone, Deserialize, Serialize)] -pub struct AddVisitorResult { - pub duration: u64, - pub difficulty_factor: u32, -} - impl AddVisitorResult { fn new(m: &MCaptcha) -> Self { AddVisitorResult { diff --git a/src/master/embedded/mod.rs b/src/master/embedded/mod.rs new file mode 100644 index 0000000..2de680d --- /dev/null +++ b/src/master/embedded/mod.rs @@ -0,0 +1,20 @@ +/* + * mCaptcha - A proof of work based DoS protection system + * Copyright © 2021 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +pub mod master; +pub mod mcaptcha; diff --git a/src/master/mod.rs b/src/master/mod.rs index dbb4b52..d297b99 100644 --- a/src/master/mod.rs +++ b/src/master/mod.rs @@ -19,13 +19,23 @@ use std::sync::mpsc::Receiver; use actix::dev::*; +use serde::{Deserialize, Serialize}; pub mod embedded; /// Describes actor handler trait impls that are required by a cache implementation -pub trait Counter: actix::Actor + actix::Handler {} +pub trait Master: actix::Actor + actix::Handler {} /// Message to add visitor to an [MCaptcha] actor #[derive(Message)] -#[rtype(result = "Option>")] +#[rtype(result = "Option>")] pub struct AddVisitor(pub String); + +/// Struct representing the return datatime of +/// [AddVisitor] message. Contains MCaptcha lifetime +/// and difficulty factor +#[derive(Debug, Clone, Deserialize, Serialize)] +pub struct AddVisitorResult { + pub duration: u64, + pub difficulty_factor: u32, +} diff --git a/src/system.rs b/src/system.rs index fcfc754..72cc5e3 100644 --- a/src/system.rs +++ b/src/system.rs @@ -23,12 +23,12 @@ use pow_sha256::Config; use crate::cache::messages::*; use crate::cache::Save; use crate::errors::*; -use crate::master::Counter; +use crate::master::Master; use crate::pow::*; /// struct describing various bits of data required for an mCaptcha system #[derive(Clone, Builder)] -pub struct System { +pub struct System { pub master: Addr, cache: Addr, pow: Config, @@ -41,7 +41,7 @@ where + ToEnvelope + ToEnvelope + ToEnvelope, - X: Counter, + X: Master, ::Context: ToEnvelope, { /// utility function to get difficulty factor of site `id` and cache it @@ -123,8 +123,9 @@ mod tests { use super::System; use super::*; use crate::cache::HashCache; - use crate::master::embedded::*; - use crate::mcaptcha::tests::*; + use crate::master::embedded::master::Master; + use crate::master::embedded::master::*; + use crate::master::embedded::mcaptcha::tests::*; const MCAPTCHA_NAME: &str = "batsense.net";