diff --git a/config/default.toml b/config/default.toml index 72e0f7b9..51d787df 100644 --- a/config/default.toml +++ b/config/default.toml @@ -34,8 +34,11 @@ enable_stats = true [captcha.default_difficulty_strategy] avg_traffic_difficulty = 50000 # almost instant solution +#avg_traffic_time = 1 # almost instant solution peak_sustainable_traffic_difficulty = 3000000 # roughly 1.5s +#peak_sustainable_traffic_time = 3 broke_my_site_traffic_difficulty = 5000000 # greater than 3.5s +#broke_my_site_traffic_time = 5 duration = 30 # cooldown period in seconds [database] diff --git a/docs/CONFIGURATION.md b/docs/CONFIGURATION.md index d964a484..e08356d7 100644 --- a/docs/CONFIGURATION.md +++ b/docs/CONFIGURATION.md @@ -56,23 +56,22 @@ you will be overriding the values set in the configuration files. ### Captcha -| Name | Value | -| ----------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | --- | -| `MCAPTCHA_captcha_SALT` | Salt has to be long and random | -| `MCAPTCHA_captcha_GC` | Garbage collection duration in seconds, requires tuning but 30 is a good starting point | -| `MCAPTCHA_captcha_RUNNERS` | [Performance] Number of runners to use for PoW validation. Defaults to number of CPUs available | -| `MCAPTCHA_captcha_QUEUE_LENGTH` | [Performance] PoW Validation queue length, controls how many pending validation jobs can be held in queue | -| `MCAPTCHA_captcha_ENABLE_STATS` | Record for CAPTCHA events like configuration fetch, solves and authentication of validation token. Useful for commercial deployments. | | -| `MCAPTCHA_captcha_DEFAULT_DIFFICULTY_STRATEGY_avg_traffic_difficulty`% | Default difficulty factor to use in easy mode CAPTCHA configuration estimation for average traffic metric | -| `MCAPTCHA_captcha_DEFAULT_DIFFICULTY_STRATEGY_peak_sustainable_traffic_difficulty`% | Default difficulty factor to use in easy mode CAPTCHA configuration estimation for peak traffic metric | -| `MCAPTCHA_captcha_DEFAULT_DIFFICULTY_STRATEGY_broke_my_site_traffic_difficulty`% | Default difficulty factor to use in easy mode CAPTCHA configuration estimation for traffic that took the website down | -| `MCAPTCHA_captcha_DEFAULT_DIFFICULTY_STRATEGY_duration`% | Default duration to use in CAPTCHA configuration in easy mode | +| Name | Value | +| ---------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- | +| `MCAPTCHA_captcha_SALT` | Salt has to be long and random | +| `MCAPTCHA_captcha_GC` | Garbage collection duration in seconds, requires tuning but 30 is a good starting point | +| `MCAPTCHA_captcha_RUNNERS` | [Performance] Number of runners to use for PoW validation. Defaults to number of CPUs available | +| `MCAPTCHA_captcha_QUEUE_LENGTH` | [Performance] PoW Validation queue length, controls how many pending validation jobs can be held in queue | +| `MCAPTCHA_captcha_ENABLE_STATS` | Record for CAPTCHA events like configuration fetch, solves and authentication of validation token. Useful for commercial deployments. | +| `MCAPTCHA_captcha_DEFAULT_DIFFICULTY_STRATEGY_avg_traffic_difficulty` | Default difficulty factor to use in easy mode CAPTCHA configuration estimation for average traffic metric | +| `MCAPTCHA_captcha_DEFAULT_DIFFICULTY_STRATEGY_avg_traffic_time` | This difficulty factor is used in to use in easy mode CAPTCHA configuration estimation for average traffic metric | +| `MCAPTCHA_captcha_DEFAULT_DIFFICULTY_STRATEGY_peak_sustainable_traffic_difficulty` | Default difficulty factor to use in easy mode CAPTCHA configuration estimation for peak traffic metric | +| `MCAPTCHA_captcha_DEFAULT_DIFFICULTY_STRATEGY_peak_sustainable_traffic_time` | This difficulty factor is used in to use in easy mode CAPTCHA configuration estimation for peak traffic metric | +| `MCAPTCHA_captcha_DEFAULT_DIFFICULTY_STRATEGY_broke_my_site_traffic_difficulty` | Default difficulty factor to use in easy mode CAPTCHA configuration estimation for traffic that took the website down | +| `MCAPTCHA_captcha_DEFAULT_DIFFICULTY_STRATEGY_broke_my_site_traffic_time` | Default time (in seconds) to use to compute difficulty factor using stored PoW performance records. | +| `MCAPTCHA_captcha_DEFAULT_DIFFICULTY_STRATEGY_duration` | Default duration to use in CAPTCHA configuration in easy mode | -\% See commits -[`54b14291ec140e`](https://github.com/mCaptcha/mCaptcha/commit/54b14291ec140ea4cbbf73462d3d6fc2d39f2d2c) -and -[`42544ec421e0`](https://github.com/mCaptcha/mCaptcha/commit/42544ec421e0c3ec4a8d132e6101ab4069bf0065) -for more info. +See commits [`54b14291ec140e`](https://github.com/mCaptcha/mCaptcha/commit/54b14291ec140ea4cbbf73462d3d6fc2d39f2d2c) and [`42544ec421e0`](https://github.com/mCaptcha/mCaptcha/commit/42544ec421e0c3ec4a8d132e6101ab4069bf0065) for more info. ### SMTP diff --git a/src/settings.rs b/src/settings.rs index f5107def..e0ecf056 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -37,8 +37,11 @@ pub struct Captcha { #[derive(Debug, Clone, Deserialize, Eq, PartialEq)] pub struct DefaultDifficultyStrategy { pub avg_traffic_difficulty: u32, - pub broke_my_site_traffic_difficulty: u32, + pub avg_traffic_time: Option, pub peak_sustainable_traffic_difficulty: u32, + pub peak_sustainable_traffic_time: Option, + pub broke_my_site_traffic_time: Option, + pub broke_my_site_traffic_difficulty: u32, pub duration: u32, } @@ -113,7 +116,7 @@ pub struct Settings { pub smtp: Option, } -const ENV_VAR_CONFIG: [(&str, &str); 29] = [ +const ENV_VAR_CONFIG: [(&str, &str); 32] = [ /* top-level */ ("debug", "MCAPTCHA_debug"), ("commercial", "MCAPTCHA_commercial"), @@ -150,6 +153,9 @@ const ENV_VAR_CONFIG: [(&str, &str); 29] = [ ( "captcha.default_difficulty_strategy.duration", "MCAPTCHA_captcha_DEFAULT_DIFFICULTY_STRATEGY_duration" ), + ("captcha.default_difficulty_strategy.avg_traffic_time", "MCAPTCHA_captcha_DEFAULT_DIFFICULTY_STRATEGY_avg_traffic_time"), + ("captcha.default_difficulty_strategy.peak_sustainable_traffic_time", "MCAPTCHA_captcha_DEFAULT_DIFFICULTY_STRATEGY_peak_sustainable_traffic_time"), + ("captcha.default_difficulty_strategy.broke_my_site_traffic_time", "MCAPTCHA_captcha_DEFAULT_DIFFICULTY_STRATEGY_broke_my_site_traffic_time"), /* SMTP */ @@ -251,6 +257,28 @@ impl Settings { Ok(settings) } + fn check_easy_captcha_config(&self) { + let s = &self.captcha.default_difficulty_strategy; + if s.avg_traffic_time.is_some() { + if s.broke_my_site_traffic_time.is_none() + || s.peak_sustainable_traffic_time.is_none() + { + panic!("if captcha.default_difficulty_strategy.avg_traffic_time is set, then captcha.default_difficulty_strategy.broke_my_site_traffic_time and captcha.default_difficulty_strategy.peak_sustainable_traffic_time must also be set"); + } + } + if s.peak_sustainable_traffic_time.is_some() { + if s.avg_traffic_time.is_none() || s.peak_sustainable_traffic_time.is_none() + { + panic!("if captcha.default_difficulty_strategy.peak_sustainable_traffic_time is set, then captcha.default_difficulty_strategy.broke_my_site_traffic_time and captcha.default_difficulty_strategy.avg_traffic_time must also be set"); + } + } + if s.broke_my_site_traffic_time.is_some() { + if s.avg_traffic_time.is_none() || s.peak_sustainable_traffic_time.is_none() + { + panic!("if captcha.default_difficulty_strategy.broke_my_site_traffic_time is set, then captcha.default_difficulty_strategy.peak_sustainable_traffic_time and captcha.default_difficulty_strategy.avg_traffic_time must also be set"); + } + } + } fn env_override(mut s: ConfigBuilder) -> ConfigBuilder { for (parameter, env_var_name) in DEPRECATED_ENV_VARS.iter() { @@ -538,6 +566,30 @@ mod tests { 999, captcha.default_difficulty_strategy.duration ); + helper!( + "MCAPTCHA_captcha_DEFAULT_DIFFICULTY_STRATEGY_avg_traffic_time", + "10", + Some(10), + captcha.default_difficulty_strategy.avg_traffic_time + ); + + helper!( + "MCAPTCHA_captcha_DEFAULT_DIFFICULTY_STRATEGY_peak_sustainable_traffic_time", + "20", + Some(20), + captcha + .default_difficulty_strategy + .peak_sustainable_traffic_time + ); + + helper!( + "MCAPTCHA_captcha_DEFAULT_DIFFICULTY_STRATEGY_broke_my_site_traffic_time", + "30", + Some(30), + captcha + .default_difficulty_strategy + .broke_my_site_traffic_time + ); /* SMTP */