tmp/src/pow.rs

198 lines
5.7 KiB
Rust
Raw Normal View History

2021-03-08 17:02:36 +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/>.
*/
use actix::dev::*;
use derive_builder::Builder;
2021-03-08 18:14:34 +05:30
use pow_sha256::{Config, PoW};
2021-03-08 17:02:36 +05:30
use serde::Serialize;
use crate::cache::messages;
use crate::cache::Save;
2021-03-08 18:14:34 +05:30
use crate::errors::*;
2021-03-08 17:02:36 +05:30
use crate::master::Master;
/// PoW Config that will be sent to clients for generating PoW
#[derive(Clone, Serialize, Debug)]
pub struct PoWConfig {
pub string: String,
pub difficulty_factor: u32,
}
impl PoWConfig {
pub fn new(m: u32) -> Self {
2021-03-08 18:14:34 +05:30
use crate::utils::get_random;
2021-03-08 17:02:36 +05:30
PoWConfig {
2021-03-08 18:14:34 +05:30
string: get_random(32),
2021-03-08 17:02:36 +05:30
difficulty_factor: m,
}
}
}
#[derive(Clone, Builder)]
pub struct Actors<T: Save> {
master: Addr<Master<'static>>,
cache: Addr<T>,
2021-03-08 18:14:34 +05:30
pow: Config,
2021-03-08 17:02:36 +05:30
}
impl<T> Actors<T>
where
T: Save,
2021-03-08 18:14:34 +05:30
<T as actix::Actor>::Context: ToEnvelope<T, messages::Cache> + ToEnvelope<T, messages::Retrive>,
2021-03-08 17:02:36 +05:30
{
pub async fn get_pow(&self, id: String) -> Option<PoWConfig> {
use crate::cache::messages::Cache;
use crate::master::GetSite;
use crate::mcaptcha::Visitor;
let site_addr = self.master.send(GetSite(id)).await.unwrap();
if site_addr.is_none() {
return None;
}
let difficulty_factor = site_addr.unwrap().send(Visitor).await.unwrap();
let pow_config = PoWConfig::new(difficulty_factor);
self.cache
.send(Cache(pow_config.clone()))
.await
.unwrap()
.unwrap();
Some(pow_config)
}
2021-03-08 18:14:34 +05:30
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 difficulty = self.cache.send(msg).await.unwrap();
let pow: PoW<String> = work.into();
match difficulty {
Ok(Some(difficulty)) => {
2021-03-08 18:33:35 +05:30
if self.pow.is_sufficient_difficulty(&pow, difficulty) {
2021-03-08 18:14:34 +05:30
Ok(self.pow.is_valid_proof(&pow, &string))
} else {
Err(CaptchaError::InsuffiencientDifficulty)
}
}
Ok(None) => Err(CaptchaError::StringNotFound),
Err(_) => Err(CaptchaError::Default),
}
}
}
#[derive(Clone, Serialize, Debug)]
pub struct Work {
pub string: String,
pub result: String,
pub nonce: u64,
}
impl From<Work> for PoW<String> {
fn from(w: Work) -> Self {
use pow_sha256::PoWBuilder;
PoWBuilder::default()
.result(w.result)
.nonce(w.nonce)
.build()
.unwrap()
}
2021-03-08 17:02:36 +05:30
}
#[cfg(test)]
mod tests {
2021-03-08 18:14:34 +05:30
use pow_sha256::ConfigBuilder;
2021-03-08 17:02:36 +05:30
use super::*;
2021-03-08 18:33:35 +05:30
use crate::cache::HashCache;
2021-03-08 17:02:36 +05:30
use crate::master::*;
use crate::mcaptcha::tests::*;
2021-03-08 18:14:34 +05:30
const MCAPTCHA_NAME: &str = "batsense.net";
async fn boostrap_system() -> Actors<HashCache> {
2021-03-08 17:02:36 +05:30
let master = Master::new().start();
let mcaptcha = get_counter().start();
2021-03-08 18:14:34 +05:30
let pow = get_config();
2021-03-08 17:02:36 +05:30
let cache = HashCache::default().start();
let msg = AddSiteBuilder::default()
2021-03-08 18:14:34 +05:30
.id(MCAPTCHA_NAME.into())
2021-03-08 17:02:36 +05:30
.addr(mcaptcha.clone())
.build()
.unwrap();
master.send(msg).await.unwrap();
2021-03-08 18:14:34 +05:30
ActorsBuilder::default()
2021-03-08 17:02:36 +05:30
.master(master)
.cache(cache)
2021-03-08 18:14:34 +05:30
.pow(pow)
2021-03-08 17:02:36 +05:30
.build()
2021-03-08 18:14:34 +05:30
.unwrap()
}
2021-03-08 17:02:36 +05:30
2021-03-08 18:14:34 +05:30
fn get_config() -> Config {
ConfigBuilder::default()
.salt("myrandomsaltisnotlongenoug".into())
.build()
.unwrap()
}
2021-03-08 17:02:36 +05:30
2021-03-08 18:14:34 +05:30
#[actix_rt::test]
async fn get_pow_works() {
let actors = boostrap_system().await;
let pow = actors.get_pow(MCAPTCHA_NAME.into()).await.unwrap();
2021-03-08 17:02:36 +05:30
assert_eq!(pow.difficulty_factor, LEVEL_1.0);
}
2021-03-08 18:14:34 +05:30
#[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();
2021-03-08 18:33:35 +05:30
let work = config
.prove_work(&work_req.string, work_req.difficulty_factor)
.unwrap();
2021-03-08 18:14:34 +05:30
2021-03-08 18:33:35 +05:30
let insufficient_work = config.prove_work(&work_req.string, 1).unwrap();
2021-03-08 18:14:34 +05:30
let insufficient_work_payload = Work {
string: work_req.string.clone(),
result: insufficient_work.result,
nonce: insufficient_work.nonce,
};
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));
let res = actors.verify_pow(insufficient_work_payload.clone()).await;
assert_eq!(res, Err(CaptchaError::InsuffiencientDifficulty));
}
2021-03-08 17:02:36 +05:30
}