moved master and mcaptcha to embedded

This commit is contained in:
Aravinth Manivannan 2021-06-03 18:19:57 +05:30
parent b0cf17aaa5
commit 236109c628
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
8 changed files with 51 additions and 28 deletions

View File

@ -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,

View File

@ -178,7 +178,7 @@ impl Handler<VerifyCaptchaResult> 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) {

View File

@ -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};

View File

@ -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<AddVisitor> 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<CleanUp> 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<AddSite> 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() {

View File

@ -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<DeleteVisitor> 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 {

View File

@ -0,0 +1,20 @@
/*
* mCaptcha - A proof of work based DoS protection system
* Copyright © 2021 Aravinth Manivannan <realravinth@batsense.net>
*
* 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 <http://www.gnu.org/licenses/>.
*/
pub mod master;
pub mod mcaptcha;

View File

@ -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<AddVisitor> {}
pub trait Master: actix::Actor + actix::Handler<AddVisitor> {}
/// Message to add visitor to an [MCaptcha] actor
#[derive(Message)]
#[rtype(result = "Option<Receiver<crate::mcaptcha::AddVisitorResult>>")]
#[rtype(result = "Option<Receiver<AddVisitorResult>>")]
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,
}

View File

@ -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<T: Save, X: Counter> {
pub struct System<T: Save, X: Master> {
pub master: Addr<X>,
cache: Addr<T>,
pow: Config,
@ -41,7 +41,7 @@ where
+ ToEnvelope<T, RetrivePoW>
+ ToEnvelope<T, CacheResult>
+ ToEnvelope<T, VerifyCaptchaResult>,
X: Counter,
X: Master,
<X as actix::Actor>::Context: ToEnvelope<X, crate::master::AddVisitor>,
{
/// 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";