moved master to embedded

This commit is contained in:
Aravinth Manivannan 2021-06-03 18:08:43 +05:30
parent d7c9130f5f
commit b0cf17aaa5
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
9 changed files with 86 additions and 18 deletions

View file

@ -8,6 +8,9 @@
internet. Ideally `mCaptcha` should automatically generate random
salt and rotate periodically, maybe in the next version.
- `master::Master` is moved to `master::embedded::Master` in preparation
for Redis based implementation.
## 0.1.3
## Added

2
Cargo.lock generated
View file

@ -618,7 +618,7 @@ checksum = "56d855069fafbb9b344c0f962150cd2c1187975cb1c22c1522c240d8c4986714"
[[package]]
name = "libmcaptcha"
version = "0.1.3"
version = "0.1.4"
dependencies = [
"actix",
"actix-rt",

View file

@ -1,6 +1,6 @@
[package]
name = "libmcaptcha"
version = "0.1.3"
version = "0.1.4"
authors = ["realaravinth <realaravinth@batsense.net>"]
description = "core of mCaptcha captcha system"
keywords = ["DDoS", "mcaptcha", "captcha", "pow"]

View file

@ -21,9 +21,10 @@ If someone wants to hammer your site, they will have to do more work to
send requests than your server will have to do to respond to their
request.
> **NOTE:** `0.1` is out, expect breaking changes as ergonomics and
> **NOTE:** `0.1.x` is out, expect breaking changes as ergonomics and
> performance is improved. Checkout [changelog](./CHANGELOG.md) for
> changes and migration pointers.
> changes and migration pointers. Breaking changes will be frequent
> through `0.1.x`.
## Why use mCaptcha?

View file

@ -1,6 +1,6 @@
use libmcaptcha::{
cache::{messages::VerifyCaptchaResult, HashCache},
master::{AddSiteBuilder, Master},
master::embedded::{AddSiteBuilder, Master},
pow::{ConfigBuilder, Work},
system::SystemBuilder,
DefenseBuilder, LevelBuilder, MCaptchaBuilder,

View file

@ -48,7 +48,7 @@
//! ```rust
//! use libmcaptcha::{
//! cache::{messages::VerifyCaptchaResult, HashCache},
//! master::{AddSiteBuilder, Master},
//! master::embedded::{AddSiteBuilder, Master},
//! pow::{ConfigBuilder, Work},
//! system::SystemBuilder,
//! DefenseBuilder, LevelBuilder, MCaptchaBuilder,

View file

@ -17,6 +17,7 @@
*/
//! [Master] actor module that manages [MCaptcha] actors
use std::collections::BTreeMap;
use std::sync::mpsc::channel;
use std::time::Duration;
//use actix::clock::sleep;
@ -25,18 +26,21 @@ use actix::dev::*;
use derive_builder::Builder;
use log::info;
use super::*;
use crate::mcaptcha::MCaptcha;
/// This Actor manages the [MCaptcha] actors.
/// A service can have several [MCaptcha] actors with
/// varying [Defense][crate::defense::Defense] configurations
/// so a "master" actor is needed to manage them all
#[derive(Clone)]
#[derive(Clone, Default)]
pub struct Master {
sites: BTreeMap<String, (Option<()>, Addr<MCaptcha>)>,
gc: u64,
}
impl Counter for Master {}
impl Master {
/// add [MCaptcha] actor to [Master]
pub fn add_site(&mut self, details: AddSite) {
@ -82,6 +86,31 @@ impl Actor for Master {
}
}
impl Handler<AddVisitor> for Master {
type Result = MessageResult<AddVisitor>;
fn handle(&mut self, m: AddVisitor, ctx: &mut Self::Context) -> Self::Result {
let addr = self.get_site(&m.0);
if addr.is_none() {
return MessageResult(None);
} else {
let (tx, rx) = channel();
let fut = async move {
let config = addr
.unwrap()
.send(crate::mcaptcha::AddVisitor)
.await
.unwrap();
tx.send(config).unwrap();
}
.into_actor(self);
ctx.spawn(fut);
return MessageResult(Some(rx));
}
}
}
/// Message to get an [MCaptcha] actor from master
#[derive(Message)]
#[rtype(result = "Option<Addr<MCaptcha>>")]

31
src/master/mod.rs Normal file
View file

@ -0,0 +1,31 @@
/*
* 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/>.
*/
//! [Master] actor module that manages [MCaptcha] actors
use std::sync::mpsc::Receiver;
use actix::dev::*;
pub mod embedded;
/// Describes actor handler trait impls that are required by a cache implementation
pub trait Counter: actix::Actor + actix::Handler<AddVisitor> {}
/// Message to add visitor to an [MCaptcha] actor
#[derive(Message)]
#[rtype(result = "Option<Receiver<crate::mcaptcha::AddVisitorResult>>")]
pub struct AddVisitor(pub String);

View file

@ -23,35 +23,39 @@ use pow_sha256::Config;
use crate::cache::messages::*;
use crate::cache::Save;
use crate::errors::*;
use crate::master::Master;
use crate::master::Counter;
use crate::pow::*;
/// struct describing various bits of data required for an mCaptcha system
#[derive(Clone, Builder)]
pub struct System<T: Save> {
pub master: Addr<Master>,
pub struct System<T: Save, X: Counter> {
pub master: Addr<X>,
cache: Addr<T>,
pow: Config,
}
impl<T> System<T>
impl<T, X> System<T, X>
where
T: Save,
<T as actix::Actor>::Context: ToEnvelope<T, CachePoW>
+ ToEnvelope<T, RetrivePoW>
+ ToEnvelope<T, CacheResult>
+ ToEnvelope<T, VerifyCaptchaResult>,
X: Counter,
<X as actix::Actor>::Context: ToEnvelope<X, crate::master::AddVisitor>,
{
/// utility function to get difficulty factor of site `id` and cache it
pub async fn get_pow(&self, id: String) -> Option<PoWConfig> {
use crate::master::GetSite;
use crate::mcaptcha::AddVisitor;
use crate::master::AddVisitor;
let site_addr = self.master.send(GetSite(id.clone())).await.unwrap();
if site_addr.is_none() {
let rx = self.master.send(AddVisitor(id.clone())).await.unwrap();
if rx.is_none() {
return None;
}
let mcaptcha = site_addr.unwrap().send(AddVisitor).await.unwrap();
let mcaptcha = rx.unwrap().recv().unwrap();
let pow_config = PoWConfig::new(mcaptcha.difficulty_factor, self.pow.salt.clone());
let cache_msg = CachePoWBuilder::default()
@ -119,12 +123,12 @@ mod tests {
use super::System;
use super::*;
use crate::cache::HashCache;
use crate::master::*;
use crate::master::embedded::*;
use crate::mcaptcha::tests::*;
const MCAPTCHA_NAME: &str = "batsense.net";
async fn boostrap_system(gc: u64) -> System<HashCache> {
async fn boostrap_system(gc: u64) -> System<HashCache, Master> {
let master = Master::new(gc).start();
let mcaptcha = get_counter().start();
let pow = get_config();