feat: record and fetch max recorded nonces
This commit is contained in:
parent
77e4a9c473
commit
ad4582cc16
2 changed files with 39 additions and 46 deletions
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
//use actix::prelude::*;
|
//use actix::prelude::*;
|
||||||
use actix_web::{web, HttpResponse, Responder};
|
use actix_web::{web, HttpResponse, Responder};
|
||||||
|
use libmcaptcha::pow::PoWConfig;
|
||||||
use libmcaptcha::{
|
use libmcaptcha::{
|
||||||
defense::LevelBuilder, master::messages::AddSiteBuilder, DefenseBuilder,
|
defense::LevelBuilder, master::messages::AddSiteBuilder, DefenseBuilder,
|
||||||
MCaptchaBuilder,
|
MCaptchaBuilder,
|
||||||
|
@ -21,7 +22,13 @@ pub struct GetConfigPayload {
|
||||||
pub key: String,
|
pub key: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// API keys are mcaptcha actor names
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||||
|
pub struct ApiPoWConfig {
|
||||||
|
pub string: String,
|
||||||
|
pub difficulty_factor: u32,
|
||||||
|
pub salt: String,
|
||||||
|
pub max_recorded_nonce: u32,
|
||||||
|
}
|
||||||
|
|
||||||
/// get PoW configuration for an mcaptcha key
|
/// get PoW configuration for an mcaptcha key
|
||||||
#[my_codegen::post(path = "V1_API_ROUTES.pow.get_config()")]
|
#[my_codegen::post(path = "V1_API_ROUTES.pow.get_config()")]
|
||||||
|
@ -35,52 +42,34 @@ pub async fn get_config(
|
||||||
}
|
}
|
||||||
let payload = payload.into_inner();
|
let payload = payload.into_inner();
|
||||||
|
|
||||||
match data.captcha.get_pow(payload.key.clone()).await {
|
let config: ServiceResult<PoWConfig> =
|
||||||
Ok(Some(config)) => {
|
match data.captcha.get_pow(payload.key.clone()).await {
|
||||||
data.stats.record_fetch(&data, &payload.key).await?;
|
Ok(Some(config)) => Ok(config),
|
||||||
Ok(HttpResponse::Ok().json(config))
|
Ok(None) => {
|
||||||
}
|
init_mcaptcha(&data, &payload.key).await?;
|
||||||
Ok(None) => {
|
let config = data
|
||||||
init_mcaptcha(&data, &payload.key).await?;
|
.captcha
|
||||||
let config = data
|
.get_pow(payload.key.clone())
|
||||||
.captcha
|
.await
|
||||||
.get_pow(payload.key.clone())
|
.expect("mcaptcha should be initialized and ready to go");
|
||||||
.await
|
Ok(config.unwrap())
|
||||||
.expect("mcaptcha should be initialized and ready to go");
|
}
|
||||||
// background it. would require data::Data to be static
|
Err(e) => Err(e.into()),
|
||||||
// to satidfy lifetime
|
};
|
||||||
data.stats.record_fetch(&data, &payload.key).await?;
|
let config = config?;
|
||||||
Ok(HttpResponse::Ok().json(config))
|
let max_nonce = data
|
||||||
}
|
.db
|
||||||
Err(e) => Err(e.into()),
|
.get_max_nonce_for_level(&payload.key, config.difficulty_factor)
|
||||||
}
|
.await?;
|
||||||
|
data.stats.record_fetch(&data, &payload.key).await?;
|
||||||
|
|
||||||
// match res.exists {
|
let config = ApiPoWConfig {
|
||||||
// Some(true) => {
|
string: config.string,
|
||||||
// match data.captcha.get_pow(payload.key.clone()).await {
|
difficulty_factor: config.difficulty_factor,
|
||||||
// Ok(Some(config)) => {
|
salt: config.salt,
|
||||||
// record_fetch(&payload.key, &data.db).await;
|
max_recorded_nonce: max_nonce,
|
||||||
// Ok(HttpResponse::Ok().json(config))
|
};
|
||||||
// }
|
Ok(HttpResponse::Ok().json(config))
|
||||||
// Ok(None) => {
|
|
||||||
// init_mcaptcha(&data, &payload.key).await?;
|
|
||||||
// let config = data
|
|
||||||
// .captcha
|
|
||||||
// .get_pow(payload.key.clone())
|
|
||||||
// .await
|
|
||||||
// .expect("mcaptcha should be initialized and ready to go");
|
|
||||||
// // background it. would require data::Data to be static
|
|
||||||
// // to satidfy lifetime
|
|
||||||
// record_fetch(&payload.key, &data.db).await;
|
|
||||||
// Ok(HttpResponse::Ok().json(config))
|
|
||||||
// }
|
|
||||||
// Err(e) => Err(e.into()),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// Some(false) => Err(ServiceError::TokenNotFound),
|
|
||||||
// None => Err(ServiceError::TokenNotFound),
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
/// Call this when [MCaptcha][libmcaptcha::MCaptcha] is not in master.
|
/// Call this when [MCaptcha][libmcaptcha::MCaptcha] is not in master.
|
||||||
///
|
///
|
||||||
|
|
|
@ -65,6 +65,7 @@ pub async fn verify_pow(
|
||||||
let payload = payload.into_inner();
|
let payload = payload.into_inner();
|
||||||
let worker_type = payload.worker_type.clone();
|
let worker_type = payload.worker_type.clone();
|
||||||
let time = payload.time;
|
let time = payload.time;
|
||||||
|
let nonce = payload.nonce;
|
||||||
let (res, difficulty_factor) = data.captcha.verify_pow(payload.into(), ip).await?;
|
let (res, difficulty_factor) = data.captcha.verify_pow(payload.into(), ip).await?;
|
||||||
data.stats.record_solve(&data, &key).await?;
|
data.stats.record_solve(&data, &key).await?;
|
||||||
if let (Some(time), Some(worker_type)) = (time, worker_type) {
|
if let (Some(time), Some(worker_type)) = (time, worker_type) {
|
||||||
|
@ -75,6 +76,9 @@ pub async fn verify_pow(
|
||||||
};
|
};
|
||||||
data.db.analysis_save(&key, &analytics).await?;
|
data.db.analysis_save(&key, &analytics).await?;
|
||||||
}
|
}
|
||||||
|
data.db
|
||||||
|
.update_max_nonce_for_level(&key, difficulty_factor, nonce as u32)
|
||||||
|
.await?;
|
||||||
let payload = ValidationToken { token: res };
|
let payload = ValidationToken { token: res };
|
||||||
Ok(HttpResponse::Ok().json(payload))
|
Ok(HttpResponse::Ok().json(payload))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue