2021-03-08 19:43:26 +05:30
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2021-03-09 15:35:33 +05:30
|
|
|
//! module describing mCaptcha system
|
2021-03-08 19:43:26 +05:30
|
|
|
use actix::dev::*;
|
|
|
|
use derive_builder::Builder;
|
|
|
|
use pow_sha256::{Config, PoW};
|
|
|
|
|
|
|
|
use crate::cache::messages;
|
|
|
|
use crate::cache::Save;
|
|
|
|
use crate::errors::*;
|
|
|
|
use crate::master::Master;
|
|
|
|
use crate::pow::*;
|
|
|
|
|
|
|
|
/// struct describing various bits of data required for an mCaptcha system
|
|
|
|
#[derive(Clone, Builder)]
|
2021-03-09 15:35:33 +05:30
|
|
|
pub struct System<T: Save> {
|
|
|
|
pub master: Addr<Master>,
|
2021-03-08 19:43:26 +05:30
|
|
|
cache: Addr<T>,
|
|
|
|
pow: Config,
|
|
|
|
}
|
|
|
|
|
2021-03-09 15:35:33 +05:30
|
|
|
impl<T> System<T>
|
2021-03-08 19:43:26 +05:30
|
|
|
where
|
|
|
|
T: Save,
|
|
|
|
<T as actix::Actor>::Context: ToEnvelope<T, messages::Cache> + ToEnvelope<T, messages::Retrive>,
|
|
|
|
{
|
|
|
|
/// utility function to get difficulty factor of site `id` and cache it
|
|
|
|
pub async fn get_pow(&self, id: String) -> Option<PoWConfig> {
|
|
|
|
use crate::cache::messages::Cache;
|
|
|
|
use crate::master::GetSite;
|
2021-03-15 19:30:46 +05:30
|
|
|
use crate::mcaptcha::AddVisitor;
|
2021-03-08 19:43:26 +05:30
|
|
|
|
|
|
|
let site_addr = self.master.send(GetSite(id)).await.unwrap();
|
|
|
|
if site_addr.is_none() {
|
|
|
|
return None;
|
|
|
|
}
|
2021-03-15 19:30:46 +05:30
|
|
|
let mcaptcha = site_addr.unwrap().send(AddVisitor).await.unwrap();
|
2021-03-09 12:17:19 +05:30
|
|
|
let pow_config = PoWConfig::new(mcaptcha.difficulty_factor);
|
|
|
|
|
|
|
|
let cache_msg = Cache::new(&pow_config, &mcaptcha);
|
|
|
|
self.cache.send(cache_msg).await.unwrap().unwrap();
|
2021-03-08 19:43:26 +05:30
|
|
|
Some(pow_config)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// utility function to verify [Work]
|
|
|
|
pub async fn verify_pow(&self, work: Work) -> CaptchaResult<bool> {
|
|
|
|
use crate::cache::messages::Retrive;
|
|
|
|
|
|
|
|
let string = work.string.clone();
|
|
|
|
let msg = Retrive(string.clone());
|
|
|
|
let pow: PoW<String> = work.into();
|
2021-03-09 17:37:46 +05:30
|
|
|
|
|
|
|
let difficulty = self.cache.send(msg).await.unwrap()?;
|
2021-03-08 19:43:26 +05:30
|
|
|
match difficulty {
|
2021-03-09 17:37:46 +05:30
|
|
|
Some(difficulty) => {
|
2021-03-08 19:43:26 +05:30
|
|
|
if self.pow.is_sufficient_difficulty(&pow, difficulty) {
|
|
|
|
Ok(self.pow.is_valid_proof(&pow, &string))
|
|
|
|
} else {
|
|
|
|
Err(CaptchaError::InsuffiencientDifficulty)
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 17:37:46 +05:30
|
|
|
None => Err(CaptchaError::StringNotFound),
|
2021-03-08 19:43:26 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use pow_sha256::ConfigBuilder;
|
|
|
|
|
2021-03-09 15:35:33 +05:30
|
|
|
use super::System;
|
2021-03-08 19:43:26 +05:30
|
|
|
use super::*;
|
|
|
|
use crate::cache::HashCache;
|
|
|
|
use crate::master::*;
|
|
|
|
use crate::mcaptcha::tests::*;
|
|
|
|
|
|
|
|
const MCAPTCHA_NAME: &str = "batsense.net";
|
|
|
|
|
2021-03-09 15:35:33 +05:30
|
|
|
async fn boostrap_system() -> System<HashCache> {
|
2021-03-08 19:43:26 +05:30
|
|
|
let master = Master::new().start();
|
|
|
|
let mcaptcha = get_counter().start();
|
|
|
|
let pow = get_config();
|
|
|
|
|
|
|
|
let cache = HashCache::default().start();
|
|
|
|
let msg = AddSiteBuilder::default()
|
|
|
|
.id(MCAPTCHA_NAME.into())
|
|
|
|
.addr(mcaptcha.clone())
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
master.send(msg).await.unwrap();
|
|
|
|
|
2021-03-09 15:35:33 +05:30
|
|
|
SystemBuilder::default()
|
2021-03-08 19:43:26 +05:30
|
|
|
.master(master)
|
|
|
|
.cache(cache)
|
|
|
|
.pow(pow)
|
|
|
|
.build()
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_config() -> Config {
|
|
|
|
ConfigBuilder::default()
|
|
|
|
.salt("myrandomsaltisnotlongenoug".into())
|
|
|
|
.build()
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn get_pow_works() {
|
|
|
|
let actors = boostrap_system().await;
|
|
|
|
let pow = actors.get_pow(MCAPTCHA_NAME.into()).await.unwrap();
|
|
|
|
assert_eq!(pow.difficulty_factor, LEVEL_1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn verify_pow_works() {
|
|
|
|
let actors = boostrap_system().await;
|
|
|
|
let work_req = actors.get_pow(MCAPTCHA_NAME.into()).await.unwrap();
|
|
|
|
let config = get_config();
|
|
|
|
|
|
|
|
let work = config
|
|
|
|
.prove_work(&work_req.string, work_req.difficulty_factor)
|
|
|
|
.unwrap();
|
|
|
|
let mut payload = Work {
|
|
|
|
string: work_req.string,
|
|
|
|
result: work.result,
|
|
|
|
nonce: work.nonce,
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = actors.verify_pow(payload.clone()).await.unwrap();
|
|
|
|
assert!(res);
|
|
|
|
|
|
|
|
payload.string = "wrongstring".into();
|
|
|
|
let res = actors.verify_pow(payload.clone()).await;
|
|
|
|
assert_eq!(res, Err(CaptchaError::StringNotFound));
|
|
|
|
|
2021-03-09 12:17:19 +05:30
|
|
|
let insufficient_work_req = actors.get_pow(MCAPTCHA_NAME.into()).await.unwrap();
|
|
|
|
let insufficient_work = config.prove_work(&insufficient_work_req.string, 1).unwrap();
|
|
|
|
let insufficient_work_payload = Work {
|
|
|
|
string: insufficient_work_req.string,
|
|
|
|
result: insufficient_work.result,
|
|
|
|
nonce: insufficient_work.nonce,
|
|
|
|
};
|
2021-03-08 19:43:26 +05:30
|
|
|
let res = actors.verify_pow(insufficient_work_payload.clone()).await;
|
|
|
|
assert_eq!(res, Err(CaptchaError::InsuffiencientDifficulty));
|
|
|
|
}
|
|
|
|
}
|