port to local pow lib and renamed config

This commit is contained in:
Aravinth Manivannan 2021-03-07 11:49:52 +05:30
parent 8ddaf4ec2e
commit 9851a5d798
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
6 changed files with 161 additions and 122 deletions

132
Cargo.lock generated
View file

@ -1,7 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "actix"
version = "0.10.0"
@ -104,9 +102,9 @@ dependencies = [
[[package]]
name = "async-trait"
version = "0.1.44"
version = "0.1.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a4c64e223db1fffa7683a719921434caa880463cfa5820032b063c9ecd5cc49"
checksum = "4968268c4b1be4c2ee4a619dba411ec2db09acde6c26da368cb01a330b60f06c"
dependencies = [
"proc-macro2",
"quote",
@ -175,7 +173,16 @@ dependencies = [
"block-padding",
"byte-tools",
"byteorder",
"generic-array",
"generic-array 0.12.4",
]
[[package]]
name = "block-buffer"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4"
dependencies = [
"generic-array 0.14.4",
]
[[package]]
@ -276,6 +283,12 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2df960f5d869b2dd8532793fde43eb5427cceb126c929747a26823ab0eeb536"
[[package]]
name = "cpuid-bool"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634"
[[package]]
name = "crossbeam-channel"
version = "0.4.4"
@ -374,7 +387,16 @@ version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5"
dependencies = [
"generic-array",
"generic-array 0.12.4",
]
[[package]]
name = "digest"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066"
dependencies = [
"generic-array 0.14.4",
]
[[package]]
@ -487,20 +509,30 @@ dependencies = [
"futures-sink",
"futures-task",
"memchr",
"pin-project-lite 0.2.4",
"pin-project-lite 0.2.6",
"pin-utils",
"slab",
]
[[package]]
name = "generic-array"
version = "0.12.3"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec"
checksum = "ffdf9f34f1447443d37393cc6c2b8313aebddcd96906caf34e54c68d8e57d7bd"
dependencies = [
"typenum",
]
[[package]]
name = "generic-array"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.1.16"
@ -627,9 +659,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.86"
version = "0.2.88"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c"
checksum = "03b07a082330a35e43f63177cc01689da34fbffa0105e1246cf0311472cac73a"
[[package]]
name = "linked-hash-map"
@ -669,7 +701,7 @@ name = "mCaptcha-browser"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"pow_sha256",
"pow_sha256 0.2.1",
"serde",
"serde_derive",
"serde_json",
@ -688,7 +720,7 @@ dependencies = [
"derive_builder",
"derive_more",
"lazy_static",
"pow_sha256",
"pow_sha256 0.1.0",
"rand",
"serde",
"serde_json",
@ -727,7 +759,7 @@ version = "0.1.0"
dependencies = [
"clap",
"clipboard",
"pow_sha256",
"pow_sha256 0.2.1",
"serde",
"serde_derive",
"serde_json",
@ -855,9 +887,9 @@ checksum = "a9a7ab5d64814df0fe4a4b5ead45ed6c5f181ee3ff04ba344313a6c80446c5d4"
[[package]]
name = "once_cell"
version = "1.7.0"
version = "1.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10acf907b94fc1b1a152d08ef97e7759650268cf986bf127f387e602b02c7e5a"
checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3"
[[package]]
name = "opaque-debug"
@ -865,6 +897,12 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c"
[[package]]
name = "opaque-debug"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]]
name = "parking_lot"
version = "0.11.1"
@ -918,15 +956,15 @@ dependencies = [
[[package]]
name = "pin-project-lite"
version = "0.1.11"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c917123afa01924fc84bb20c4c03f004d9c38e5127e3c039bbf7f4b9c76a2f6b"
checksum = "257b64915a082f7811703966789728173279bdebb956b143dbcd23f6f970a777"
[[package]]
name = "pin-project-lite"
version = "0.2.4"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439697af366c49a6d0a010c56a0d97685bc140ce0d377b13a2ea2aa42d64a827"
checksum = "dc0e1f259c92177c30a4c9d177246edd0a3568b25756a977d0632cf8fa37e905"
[[package]]
name = "pin-utils"
@ -934,6 +972,17 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "pow_sha256"
version = "0.1.0"
source = "git+https://github.com/mcaptcha/pow_sha256#0bfbce0185f111b2eb8c14ff74d95dde5af22388"
dependencies = [
"bincode",
"derive_builder",
"serde",
"sha2 0.9.3",
]
[[package]]
name = "pow_sha256"
version = "0.2.1"
@ -942,7 +991,7 @@ checksum = "59a863ac8e10b57fc08a011f8035cedbcc1ddd3c2af7bb546b6462a68059182f"
dependencies = [
"bincode",
"serde",
"sha2",
"sha2 0.8.2",
]
[[package]]
@ -1061,18 +1110,18 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "serde"
version = "1.0.123"
version = "1.0.124"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae"
checksum = "bd761ff957cb2a45fbb9ab3da6512de9de55872866160b23c25f1a841e99d29f"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.123"
version = "1.0.124"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9391c295d64fc0abb2c556bad848f33cb8296276b1ad2677d1ae1ace4f258f31"
checksum = "1800f7693e94e186f5e25a28291ae1570da908aff7d97a095dec1e56ff99069b"
dependencies = [
"proc-macro2",
"quote",
@ -1096,10 +1145,23 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a256f46ea78a0c0d9ff00077504903ac881a1dafdc20da66545699e7776b3e69"
dependencies = [
"block-buffer",
"digest",
"block-buffer 0.7.3",
"digest 0.8.1",
"fake-simd",
"opaque-debug",
"opaque-debug 0.2.3",
]
[[package]]
name = "sha2"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa827a14b29ab7f44778d14a88d3cb76e949c45083f7dbfa507d0cb699dc12de"
dependencies = [
"block-buffer 0.9.0",
"cfg-if 1.0.0",
"cpuid-bool",
"digest 0.9.0",
"opaque-debug 0.3.0",
]
[[package]]
@ -1148,9 +1210,9 @@ checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"
[[package]]
name = "syn"
version = "1.0.61"
version = "1.0.62"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed22b90a0e734a23a7610f4283ac9e5acfb96cbb30dfefa540d66f866f1c09c5"
checksum = "123a78a3596b24fee53a6464ce52d8ecbf62241e6294c7e7fe12086cd161f512"
dependencies = [
"proc-macro2",
"quote",
@ -1225,7 +1287,7 @@ dependencies = [
"memchr",
"mio",
"mio-uds",
"pin-project-lite 0.1.11",
"pin-project-lite 0.1.12",
"signal-hook-registry",
"slab",
"winapi 0.3.9",
@ -1242,7 +1304,7 @@ dependencies = [
"futures-io",
"futures-sink",
"log",
"pin-project-lite 0.1.11",
"pin-project-lite 0.1.12",
"tokio",
]
@ -1346,6 +1408,12 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"

View file

@ -27,6 +27,6 @@ derive_more = "0.99"
lazy_static = "1.4"
rand = "0.7"
pow_sha256 = "0.2.1"
pow_sha256 = { version = "0.1", git = "https://github.com/mcaptcha/pow_sha256" }
async-trait = "0.1.44"

View file

@ -2,10 +2,9 @@ use std::collections::HashMap;
use std::sync::Arc;
use actix::prelude::*;
use async_trait::async_trait;
use crate::config::PoWConfig;
use crate::config::Save;
use super::messages::*;
use super::Save;
use crate::errors::*;
#[derive(Clone, Default)]
@ -33,11 +32,6 @@ impl Actor for HashCache {
type Context = Context<Self>;
}
/// Message to decrement the visitor count
#[derive(Message)]
#[rtype(result = "CaptchaResult<()>")]
pub struct Cache(pub Arc<PoWConfig>);
impl Handler<Cache> for HashCache {
type Result = MessageResult<Cache>;
fn handle(&mut self, msg: Cache, _ctx: &mut Self::Context) -> Self::Result {
@ -50,11 +44,6 @@ impl Handler<Cache> for HashCache {
}
}
/// Message to decrement the visitor count
#[derive(Message)]
#[rtype(result = "CaptchaResult<Option<u32>>")]
pub struct Retrive(pub Arc<String>);
impl Handler<Retrive> for HashCache {
type Result = MessageResult<Retrive>;
fn handle(&mut self, msg: Retrive, _ctx: &mut Self::Context) -> Self::Result {
@ -70,7 +59,7 @@ mod tests {
async fn counter_defense_tightenup_works() {
let addr = HashCache::default().start();
let p = Arc::new("ewerw".to_string());
addr.send(Retrive(p)).await.unwrap();
addr.send(Retrive(p)).await.unwrap().unwrap();
}
//
// #[actix_rt::test]

48
src/cache/mod.rs vendored Normal file
View file

@ -0,0 +1,48 @@
pub use hashcache::HashCache;
use messages::*;
pub mod hashcache;
pub trait Save: actix::Actor + actix::Handler<Retrive> + actix::Handler<Cache> {}
pub mod messages {
use std::sync::Arc;
use actix::dev::*;
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use serde::Serialize;
use super::Save;
use crate::errors::*;
use crate::mcaptcha::MCaptcha;
/// Message to decrement the visitor count
#[derive(Message)]
#[rtype(result = "CaptchaResult<()>")]
pub struct Cache(pub Arc<PoWConfig>);
/// Message to decrement the visitor count
#[derive(Message)]
#[rtype(result = "CaptchaResult<Option<u32>>")]
pub struct Retrive(pub Arc<String>);
/// 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<T>(m: &MCaptcha<T>) -> Self
where
T: Save,
<T as Actor>::Context: ToEnvelope<T, Retrive> + ToEnvelope<T, Cache>,
{
PoWConfig {
string: thread_rng().sample_iter(&Alphanumeric).take(32).collect(),
difficulty_factor: m.get_difficulty(),
}
}
}
}

View file

@ -93,20 +93,19 @@
//! }
//! ```
pub mod config;
pub mod defense;
pub mod errors;
pub mod hashcache;
pub mod mcaptcha;
/// message datatypes to interact with [MCaptcha] actor
pub mod message {
pub use crate::config::Visitor;
pub use crate::mcaptcha::Visitor;
}
/// message datatypes to interact with [MCaptcha] actor
pub mod cache {
pub use crate::hashcache::HashCache;
}
pub mod cache;
pub use crate::cache::hashcache::HashCache;
pub use config::{MCaptcha, MCaptchaBuilder};
pub use defense::{Defense, DefenseBuilder, LevelBuilder};
pub use mcaptcha::{MCaptcha, MCaptchaBuilder};

View file

@ -73,39 +73,25 @@
//! }
//! ```
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use actix::dev::*;
use actix::prelude::*;
use async_trait::async_trait;
use derive_builder::Builder;
use pow_sha256::PoW as ShaPoW;
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use serde::{Deserialize, Serialize};
use crate::cache::{messages::*, HashCache, Save};
use crate::defense::Defense;
use crate::errors::*;
use crate::hashcache::*;
//#[async_trait]
//pub trait PersistPow {
// async fn save(&mut self, config: Arc<PoWConfig>) -> CaptchaResult<()>;
// async fn retrive(&mut self, string: &str) -> CaptchaResult<Option<u32>>;
//}
pub trait Save: actix::Actor + actix::Handler<Retrive> + actix::Handler<Cache> {}
/// This struct represents the mCaptcha state and is used
/// to configure leaky-bucket lifetime and manage defense
#[derive(Clone, Builder)]
pub struct MCaptcha<T>
where
// Actor + Handler<Cache>,
T: Save,
<T as Actor>::Context: ToEnvelope<T, Retrive> + ToEnvelope<T, Cache>,
// <T as Actor>::Context: ToEnvelope<T, Cache>,
{
#[builder(default = "0", setter(skip))]
visitor_threshold: u32,
@ -114,26 +100,9 @@ where
cache: Addr<T>,
}
//#[async_trait]
//impl PersistPow for HashCache {
// async fn save(&mut self, config: Arc<PoWConfig>) -> CaptchaResult<()> {
// self.insert(config.string.clone(), config.difficulty_factor);
// Ok(())
// }
//
// async fn retrive(&mut self, string: &str) -> CaptchaResult<Option<u32>> {
// if let Some(difficulty_factor) = self.get(string) {
// Ok(Some(difficulty_factor.to_owned()))
// } else {
// Ok(None)
// }
// }
//}
impl<T> MCaptcha<T>
where
T: Save,
<T as Actor>::Context: ToEnvelope<T, Retrive> + ToEnvelope<T, Cache>,
{
/// incerment visiotr count by one
pub fn add_visitor(&mut self) {
@ -156,23 +125,10 @@ where
pub fn get_difficulty(&self) -> u32 {
self.defense.get_difficulty()
}
// /// cache PoW configuration: difficulty and string
// pub async fn cache_pow(&mut self, pow: Arc<PoWConfig>) -> CaptchaResult<()> {
// unimplemented!();
// Ok(self.cache.save(pow).await?)
// }
//
// /// retrivee PoW configuration: difficulty and string
// pub async fn retrive_pow(&mut self, pow: &PoWConfig) -> CaptchaResult<Option<u32>> {
// unimplemented!();
// Ok(self.cache.retrive(&pow.string).await?)
// }
}
impl<T> Actor for MCaptcha<T>
where
T: Save,
<T as Actor>::Context: ToEnvelope<T, Retrive> + ToEnvelope<T, Cache>,
{
type Context = Context<Self>;
}
@ -185,7 +141,7 @@ struct DeleteVisitor;
impl<T> Handler<DeleteVisitor> for MCaptcha<T>
where
T: Save,
<T as Actor>::Context: ToEnvelope<T, Retrive> + ToEnvelope<T, Cache>,
// <T as Actor>::Context: ToEnvelope<T, Retrive> + ToEnvelope<T, Cache>,
{
type Result = ();
fn handle(&mut self, _msg: DeleteVisitor, _ctx: &mut Self::Context) -> Self::Result {
@ -193,30 +149,9 @@ where
}
}
/// 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<T>(m: &MCaptcha<T>) -> Self
where
T: Save,
<T as Actor>::Context: ToEnvelope<T, Retrive> + ToEnvelope<T, Cache>,
{
PoWConfig {
string: thread_rng().sample_iter(&Alphanumeric).take(32).collect(),
difficulty_factor: m.get_difficulty(),
}
}
}
/// Message to increment the visitor count
#[derive(Message)]
#[rtype(result = "CaptchaResult<PoWConfig>")]
//#[rtype(result = "()")]
pub struct Visitor;
impl<T> Handler<Visitor> for MCaptcha<T>
@ -225,8 +160,8 @@ where
<T as Actor>::Context: ToEnvelope<T, Retrive> + ToEnvelope<T, Cache>,
{
type Result = ResponseActFuture<Self, CaptchaResult<PoWConfig>>;
fn handle(&mut self, _: Visitor, ctx: &mut Self::Context) -> Self::Result {
use crate::hashcache::Cache;
use actix::clock::delay_for;
use actix::fut::wrap_future;