Crate libmcaptcha

source ·
Expand description

mCaptcha is a proof of work based Denaial-of-Service attack protection system. This is is a server library that you can embed in your services to protect your servers.

A commercial managed solution is in the works but I’d much rather prefer folks host their own instances as it will make the more decentralized and free.

In mCaptcha, defense is adjusted in discrete levels that depend on the ammount of traffic that a service is experiencing. So users of this library are requested to benchmark their target machines before configuring their mCaptcha component.


  • 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: 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 : A datatype that implements Save. Used to store PoW requirements to defend against replay attacks and dictionary attacks.
  • Master: A datatype that manages MCaptcha actors. Works like a DNS for [AddVisitor][crate::mcaptcha::AddVisitor] messages.
  • System: mCaptcha system that manages cache, master and provides useful abstractions. An mCaptcha system/instance can have only a single System


use libmcaptcha::{
    cache::{messages::VerifyCaptchaResult, hashcache::HashCache},
    master::embedded::master:: Master,
    pow::{ConfigBuilder, Work},
    DefenseBuilder, LevelBuilder, MCaptchaBuilder,
// traits from actix needs to be in scope for starting actor
use actix::prelude::*;

async fn main() -> std::io::Result<()> {
    // start cahce actor
    // cache is used to store PoW requirements that are sent to clients
    // This way, it can be verified that the client computed work over a config
    // that _we_ sent. Offers protection against rainbow tables powered dictionary attacks
    let cache = HashCache::default().start();

    // create PoW config with unique salt. Salt has to be safely guarded.
    // salts protect us from replay attacks
    let pow = ConfigBuilder::default()

    // start master actor. Master actor is responsible for managing MCaptcha actors
    // each mCaptcha system should have only one master
    let master = Master::new(5).start();

    // Create system. System encapsulates master and cache and provides useful abstraction
    // each mCaptcha system should have only one system
    let system = SystemBuilder::default()

    // configure defense. This is a per site configuration. A site can have several levels
    // of defenses configured
    let defense = DefenseBuilder::default()
        // add as many defense as you see fit
                // visitor_threshold is the threshold/limit at which
                // mCaptcha will adjust difficulty defense
                // it is advisable to set small values for the first
                // defense visitor_threshold and difficulty_factor
                // as this will be the work that clients will be
                // computing when there's no load

    // 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()
        // leaky bucket algorithm's emission interval
        //   .cache(cache)

    // unique value identifying an MCaptcha actor
    let mcaptcha_name = "";

    // add MCaptcha to Master
    let msg = AddSiteBuilder::default()

    // Get PoW config. Should be called everytime there's a visitor for a
    // managed site(here mcaptcha_name)
    let work_req = system.get_pow(mcaptcha_name.into()).await.unwrap().unwrap();

    // the following computation should be done on the client but for the purpose
    // of this illustration, we are going to do it on the server it self
    let work = pow
        .prove_work(&work_req.string, work_req.difficulty_factor)

    // the payload that the client sends to the server
    let payload = Work {
        string: work_req.string,
        result: work.result,
        nonce: work.nonce,
        key: mcaptcha_name.into(),

    // Server evaluates client's work. Returns true if everything
    // checksout and Err() if something fishy is happening
    let res = system.verify_pow(payload.clone(), "".into()).await;
    let (res, _res_difficulty_factor) = res.unwrap();

   // The client should submit the token to the mCaptcha protected service
   // The service should validate the token received from the client
   // with the mCaptcha server before processing client's
   // request

   // mcaptcha protected service sends the following paylaod to mCaptcha
   // server:
   let verify_msg = VerifyCaptchaResult {
       token: res,
       key: mcaptcha_name.into(),

   // on mCaptcha server:
   let res = system.validate_verification_tokens(verify_msg).await;
   // mCaptcha will return true if token is valid and false if
   // token is invalid




  • message datatypes to interact with MCaptcha actor Cache is used to save proofof work details and nonces to prevent replay attacks and rainbow/dictionary attacks
  • Defense datatypes
  • Errors and Result module
  • Master actor module that manages MCaptcha actors
  • PoW datatypes used in client-server interaction
  • Redis Client/Connection manager that can handle both single and clustered Redis Instances
  • module describing mCaptcha system