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::*;
|
2021-04-09 23:20:14 +05:30
|
|
|
use pow_sha256::Config;
|
2021-03-08 19:43:26 +05:30
|
|
|
|
2021-04-10 13:14:48 +05:30
|
|
|
use crate::cache::messages::*;
|
2021-03-08 19:43:26 +05:30
|
|
|
use crate::cache::Save;
|
|
|
|
use crate::errors::*;
|
2021-06-09 14:03:19 +05:30
|
|
|
use crate::master::messages::*;
|
2021-06-03 18:19:57 +05:30
|
|
|
use crate::master::Master;
|
2021-03-08 19:43:26 +05:30
|
|
|
use crate::pow::*;
|
|
|
|
|
2021-06-11 19:28:14 +05:30
|
|
|
pub struct SystemBuilder<T: Save, X: Master> {
|
|
|
|
pub master: Option<Addr<X>>,
|
|
|
|
cache: Option<Addr<T>>,
|
|
|
|
pow: Option<Config>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Master, S: Save> Default for SystemBuilder<S, T> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
pow: None,
|
|
|
|
cache: None,
|
|
|
|
master: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Master, S: Save> SystemBuilder<S, T> {
|
|
|
|
pub fn master(mut self, m: Addr<T>) -> Self {
|
|
|
|
self.master = Some(m);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cache(mut self, c: Addr<S>) -> Self {
|
|
|
|
self.cache = Some(c);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pow(mut self, p: Config) -> Self {
|
|
|
|
self.pow = Some(p);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn build(self) -> System<S, T> {
|
|
|
|
System {
|
|
|
|
master: self.master.unwrap(),
|
|
|
|
pow: self.pow.unwrap(),
|
|
|
|
cache: self.cache.unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 19:43:26 +05:30
|
|
|
/// struct describing various bits of data required for an mCaptcha system
|
2021-06-03 18:19:57 +05:30
|
|
|
pub struct System<T: Save, X: Master> {
|
2021-06-03 18:08:43 +05:30
|
|
|
pub master: Addr<X>,
|
2021-03-08 19:43:26 +05:30
|
|
|
cache: Addr<T>,
|
|
|
|
pow: Config,
|
|
|
|
}
|
|
|
|
|
2021-06-03 18:08:43 +05:30
|
|
|
impl<T, X> System<T, X>
|
2021-03-08 19:43:26 +05:30
|
|
|
where
|
|
|
|
T: Save,
|
2021-04-10 13:14:48 +05:30
|
|
|
<T as actix::Actor>::Context: ToEnvelope<T, CachePoW>
|
|
|
|
+ ToEnvelope<T, RetrivePoW>
|
|
|
|
+ ToEnvelope<T, CacheResult>
|
|
|
|
+ ToEnvelope<T, VerifyCaptchaResult>,
|
2021-06-03 18:19:57 +05:30
|
|
|
X: Master,
|
2021-06-09 14:03:19 +05:30
|
|
|
<X as actix::Actor>::Context: ToEnvelope<X, AddVisitor> + ToEnvelope<X, AddSite>,
|
2021-03-08 19:43:26 +05:30
|
|
|
{
|
|
|
|
/// utility function to get difficulty factor of site `id` and cache it
|
|
|
|
pub async fn get_pow(&self, id: String) -> Option<PoWConfig> {
|
2021-06-08 18:13:01 +05:30
|
|
|
match self
|
|
|
|
.master
|
|
|
|
.send(AddVisitor(id.clone()))
|
|
|
|
.await
|
|
|
|
.unwrap()
|
2021-06-11 19:28:14 +05:30
|
|
|
.await
|
2021-06-08 18:13:01 +05:30
|
|
|
.unwrap()
|
|
|
|
{
|
|
|
|
Ok(Some(mcaptcha)) => {
|
|
|
|
let pow_config = PoWConfig::new(mcaptcha.difficulty_factor, self.pow.salt.clone());
|
2021-06-03 18:08:43 +05:30
|
|
|
|
2021-06-08 18:13:01 +05:30
|
|
|
let cache_msg = CachePoWBuilder::default()
|
|
|
|
.string(pow_config.string.clone())
|
|
|
|
.difficulty_factor(mcaptcha.difficulty_factor)
|
|
|
|
.duration(mcaptcha.duration)
|
|
|
|
.key(id)
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
2021-03-09 12:17:19 +05:30
|
|
|
|
2021-06-11 12:08:05 +05:30
|
|
|
self.cache
|
|
|
|
.send(cache_msg)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
2021-06-08 18:13:01 +05:30
|
|
|
Some(pow_config)
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
2021-03-08 19:43:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/// utility function to verify [Work]
|
2021-04-10 11:40:59 +05:30
|
|
|
pub async fn verify_pow(&self, work: Work) -> CaptchaResult<String> {
|
2021-03-08 19:43:26 +05:30
|
|
|
let string = work.string.clone();
|
2021-06-11 12:08:05 +05:30
|
|
|
let msg = VerifyCaptchaResult {
|
|
|
|
token: string.clone(),
|
|
|
|
key: work.key.clone(),
|
|
|
|
};
|
|
|
|
let msg = RetrivePoW(msg);
|
2021-04-09 23:20:14 +05:30
|
|
|
|
2021-06-11 12:08:05 +05:30
|
|
|
let cached_config = self.cache.send(msg).await.unwrap().await.unwrap()?;
|
2021-04-09 23:20:14 +05:30
|
|
|
|
|
|
|
if cached_config.is_none() {
|
|
|
|
return Err(CaptchaError::StringNotFound);
|
|
|
|
}
|
|
|
|
|
|
|
|
let cached_config = cached_config.unwrap();
|
|
|
|
|
|
|
|
if work.key != cached_config.key {
|
|
|
|
return Err(CaptchaError::MCaptchaKeyValidationFail);
|
2021-03-08 19:43:26 +05:30
|
|
|
}
|
2021-04-09 23:20:14 +05:30
|
|
|
|
|
|
|
let pow = work.into();
|
|
|
|
|
|
|
|
if !self
|
|
|
|
.pow
|
|
|
|
.is_sufficient_difficulty(&pow, cached_config.difficulty_factor)
|
|
|
|
{
|
|
|
|
return Err(CaptchaError::InsuffiencientDifficulty);
|
|
|
|
}
|
|
|
|
|
2021-04-10 11:40:59 +05:30
|
|
|
if !self.pow.is_valid_proof(&pow, &string) {
|
|
|
|
return Err(CaptchaError::InvalidPoW);
|
|
|
|
}
|
|
|
|
|
|
|
|
let msg: CacheResult = cached_config.into();
|
2021-04-10 13:14:48 +05:30
|
|
|
let res = msg.token.clone();
|
2021-06-11 12:08:05 +05:30
|
|
|
self.cache.send(msg).await.unwrap().await.unwrap()?;
|
2021-04-10 11:40:59 +05:30
|
|
|
Ok(res)
|
2021-03-08 19:43:26 +05:30
|
|
|
}
|
2021-04-10 13:14:48 +05:30
|
|
|
|
|
|
|
/// utility function to validate verification tokens
|
|
|
|
pub async fn validate_verification_tokens(
|
|
|
|
&self,
|
|
|
|
msg: VerifyCaptchaResult,
|
|
|
|
) -> CaptchaResult<bool> {
|
2021-06-11 12:08:05 +05:30
|
|
|
self.cache.send(msg).await.unwrap().await.unwrap()
|
2021-04-10 13:14:48 +05:30
|
|
|
}
|
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::*;
|
2021-06-09 20:09:54 +05:30
|
|
|
use crate::cache::hashcache::HashCache;
|
2021-06-03 19:40:45 +05:30
|
|
|
use crate::master::embedded::counter::tests::*;
|
2021-06-03 18:19:57 +05:30
|
|
|
use crate::master::embedded::master::Master;
|
2021-03-08 19:43:26 +05:30
|
|
|
|
|
|
|
const MCAPTCHA_NAME: &str = "batsense.net";
|
|
|
|
|
2021-06-03 18:08:43 +05:30
|
|
|
async fn boostrap_system(gc: u64) -> System<HashCache, Master> {
|
2021-03-15 22:14:47 +05:30
|
|
|
let master = Master::new(gc).start();
|
2021-06-03 19:40:45 +05:30
|
|
|
let mcaptcha = get_mcaptcha();
|
2021-03-08 19:43:26 +05:30
|
|
|
let pow = get_config();
|
|
|
|
|
|
|
|
let cache = HashCache::default().start();
|
|
|
|
let msg = AddSiteBuilder::default()
|
|
|
|
.id(MCAPTCHA_NAME.into())
|
2021-06-03 19:40:45 +05:30
|
|
|
.mcaptcha(mcaptcha)
|
2021-03-08 19:43:26 +05:30
|
|
|
.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()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_config() -> Config {
|
|
|
|
ConfigBuilder::default()
|
|
|
|
.salt("myrandomsaltisnotlongenoug".into())
|
|
|
|
.build()
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn get_pow_works() {
|
2021-03-15 22:14:47 +05:30
|
|
|
let actors = boostrap_system(10).await;
|
2021-03-08 19:43:26 +05:30
|
|
|
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() {
|
2021-04-10 13:14:48 +05:30
|
|
|
// start system
|
2021-03-15 22:14:47 +05:30
|
|
|
let actors = boostrap_system(10).await;
|
2021-04-10 13:14:48 +05:30
|
|
|
// get work
|
2021-03-08 19:43:26 +05:30
|
|
|
let work_req = actors.get_pow(MCAPTCHA_NAME.into()).await.unwrap();
|
2021-04-10 13:14:48 +05:30
|
|
|
// get config
|
2021-03-08 19:43:26 +05:30
|
|
|
let config = get_config();
|
|
|
|
|
2021-04-10 13:14:48 +05:30
|
|
|
// generate proof
|
2021-03-08 19:43:26 +05:30
|
|
|
let work = config
|
|
|
|
.prove_work(&work_req.string, work_req.difficulty_factor)
|
|
|
|
.unwrap();
|
2021-04-10 13:14:48 +05:30
|
|
|
// generate proof payload
|
2021-03-08 19:43:26 +05:30
|
|
|
let mut payload = Work {
|
|
|
|
string: work_req.string,
|
|
|
|
result: work.result,
|
|
|
|
nonce: work.nonce,
|
2021-04-09 23:20:14 +05:30
|
|
|
key: MCAPTCHA_NAME.into(),
|
2021-03-08 19:43:26 +05:30
|
|
|
};
|
|
|
|
|
2021-04-10 13:14:48 +05:30
|
|
|
// verifiy proof
|
2021-04-10 11:40:59 +05:30
|
|
|
let res = actors.verify_pow(payload.clone()).await;
|
|
|
|
assert!(res.is_ok());
|
2021-03-08 19:43:26 +05:30
|
|
|
|
2021-04-10 13:14:48 +05:30
|
|
|
// verify validation token
|
|
|
|
let mut verifi_msg = VerifyCaptchaResult {
|
|
|
|
token: res.unwrap(),
|
|
|
|
key: MCAPTCHA_NAME.into(),
|
|
|
|
};
|
|
|
|
assert!(actors
|
|
|
|
.validate_verification_tokens(verifi_msg.clone())
|
|
|
|
.await
|
|
|
|
.unwrap());
|
|
|
|
|
|
|
|
// verify wrong validation token
|
|
|
|
verifi_msg.token = MCAPTCHA_NAME.into();
|
|
|
|
assert!(!actors
|
|
|
|
.validate_verification_tokens(verifi_msg)
|
|
|
|
.await
|
|
|
|
.unwrap());
|
|
|
|
|
2021-03-08 19:43:26 +05:30
|
|
|
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-04-09 23:20:14 +05:30
|
|
|
key: MCAPTCHA_NAME.into(),
|
2021-03-09 12:17:19 +05:30
|
|
|
};
|
2021-03-08 19:43:26 +05:30
|
|
|
let res = actors.verify_pow(insufficient_work_payload.clone()).await;
|
|
|
|
assert_eq!(res, Err(CaptchaError::InsuffiencientDifficulty));
|
2021-04-09 23:20:14 +05:30
|
|
|
|
|
|
|
let sitekeyfail_config = actors.get_pow(MCAPTCHA_NAME.into()).await.unwrap();
|
|
|
|
let sitekeyfail_work = config
|
|
|
|
.prove_work(
|
|
|
|
&sitekeyfail_config.string,
|
|
|
|
sitekeyfail_config.difficulty_factor,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let sitekeyfail = Work {
|
|
|
|
string: sitekeyfail_config.string,
|
|
|
|
result: sitekeyfail_work.result,
|
|
|
|
nonce: sitekeyfail_work.nonce,
|
|
|
|
key: "example.com".into(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = actors.verify_pow(sitekeyfail).await;
|
|
|
|
assert_eq!(res, Err(CaptchaError::MCaptchaKeyValidationFail));
|
2021-03-08 19:43:26 +05:30
|
|
|
}
|
|
|
|
}
|