Module m_captcha::mcaptcha[][src]

MCaptcha actor module that manages defense levels

Usage:

use m_captcha::{mcaptcha::AddVisitor, MCaptchaBuilder, cache::HashCache, LevelBuilder, DefenseBuilder};
// traits from actix needs to be in scope for starting actor
use actix::prelude::*;

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
    // configure defense
    let defense = DefenseBuilder::default()
        // add as many levels as you see fit
        .add_level(
            LevelBuilder::default()
                // visitor_threshold is the threshold/limit at which
                // mCaptcha will adjust difficulty levels
                // it is advisable to set small values for the first
                // levels visitor_threshold and difficulty_factor
                // as this will be the work that clients will be
                // computing when there's no load
                .visitor_threshold(50)
                .difficulty_factor(500)
                .unwrap()
                .build()
                .unwrap(),
        )
        .unwrap()
        .add_level(
            LevelBuilder::default()
                .visitor_threshold(5000)
                .difficulty_factor(50000)
                .unwrap()
                .build()
                .unwrap(),
        )
        .unwrap()
        .build()
        .unwrap();

    // create and start MCaptcha actor
    //let cache = HashCache::default().start();
    let mcaptcha = MCaptchaBuilder::default()
        .defense(defense)
        // leaky bucket algorithm's emission interval
        .duration(30)
        .build()
        .unwrap()
        .start();

    // increment count when user visits protected routes
    mcaptcha.send(AddVisitor).await.unwrap();

    Ok(())
}

Structs

AddVisitor

Message to increment the visitor count returns difficulty factor and lifetime

AddVisitorResult

Struct representing the return datatime of AddVisitor message. Contains MCaptcha lifetime and difficulty factor

GetCurrentVisitorCount

Message to get the visitor count

MCaptcha

This struct represents the mCaptcha state and is used to configure leaky-bucket lifetime and manage defense

MCaptchaBuilder

Builder for MCaptcha

Stop

Message to stop MCaptcha