1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
 * 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/>.
 */

//! Defense datatypes
//! ```rust
//! use m_captcha::{LevelBuilder, DefenseBuilder};
//! DefenseBuilder::default()
//!        .add_level(
//!            LevelBuilder::default()
//!                .visitor_threshold(50)
//!                .difficulty_factor(50)
//!                .unwrap()
//!                .build()
//!                .unwrap(),
//!        )
//!        .unwrap()
//!        .add_level(
//!            LevelBuilder::default()
//!                .visitor_threshold(500)
//!                .difficulty_factor(500)
//!                .unwrap()
//!                .build()
//!                .unwrap(),
//!        )
//!        .unwrap()
//!        .build()
//!        .unwrap();
//! ```

use crate::errors::*;
use serde::{Deserialize, Serialize};

/// Level struct that describes threshold-difficulty factor mapping
#[derive(Debug, Deserialize, Serialize, Copy, Clone, PartialEq)]
pub struct Level {
    pub visitor_threshold: u32,
    pub difficulty_factor: u32,
}

/// Bulder struct for [Level] to describe threshold-difficulty factor mapping
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct LevelBuilder {
    visitor_threshold: Option<u32>,
    difficulty_factor: Option<u32>,
}

impl Default for LevelBuilder {
    fn default() -> Self {
        LevelBuilder {
            visitor_threshold: None,
            difficulty_factor: None,
        }
    }
}

impl LevelBuilder {
    /// set visitor count for level
    pub fn visitor_threshold(&mut self, visitor_threshold: u32) -> &mut Self {
        self.visitor_threshold = Some(visitor_threshold);
        self
    }

    /// set difficulty factor for level. difficulty_factor can't be zero because
    /// Difficulty is calculated as:
    /// ```no_run
    /// let difficulty_factor = 500;
    /// let difficulty = u128::max_value() - u128::max_value() / difficulty_factor;
    /// ```
    /// the higher the `difficulty_factor`, the higher the difficulty.
    pub fn difficulty_factor(&mut self, difficulty_factor: u32) -> CaptchaResult<&mut Self> {
        if difficulty_factor > 0 {
            self.difficulty_factor = Some(difficulty_factor);
            Ok(self)
        } else {
            Err(CaptchaError::DifficultyFactorZero)
        }
    }

    /// build Level struct
    pub fn build(&mut self) -> CaptchaResult<Level> {
        if self.visitor_threshold.is_none() {
            Err(CaptchaError::SetVisitorThreshold)
        } else if self.difficulty_factor.is_none() {
            Err(CaptchaError::SetDifficultyFactor)
        } else {
            Ok(Level {
                difficulty_factor: self.difficulty_factor.unwrap(),
                visitor_threshold: self.visitor_threshold.unwrap(),
            })
        }
    }
}

/// struct describes all the different [Level]s at which an mCaptcha system operates
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct Defense {
    levels: Vec<Level>,
    // index of current visitor threshold
    current_visitor_threshold: usize,
}

/// Builder struct for [Defense]
#[derive(Debug, Clone, PartialEq)]
pub struct DefenseBuilder {
    levels: Vec<Level>,
}

impl Default for DefenseBuilder {
    fn default() -> Self {
        DefenseBuilder { levels: vec![] }
    }
}

impl DefenseBuilder {
    /// add a level to [Defense]
    pub fn add_level(&mut self, level: Level) -> CaptchaResult<&mut Self> {
        for i in self.levels.iter() {
            if i.visitor_threshold == level.visitor_threshold {
                return Err(CaptchaError::DuplicateVisitorCount);
            }
        }
        self.levels.push(level);
        Ok(self)
    }

    /// Build [Defense]
    pub fn build(&mut self) -> CaptchaResult<Defense> {
        if !self.levels.is_empty() {
            // sort levels to arrange in ascending order
            self.levels.sort_by_key(|a| a.visitor_threshold);

            for level in self.levels.iter() {
                if level.difficulty_factor == 0 {
                    return Err(CaptchaError::DifficultyFactorZero);
                }
            }

            // as visitor count increases, difficulty_factor too should increse
            // if it decreses, an error must be thrown
            for i in 0..self.levels.len() - 1 {
                if self.levels[i].difficulty_factor > self.levels[i + 1].difficulty_factor {
                    return Err(CaptchaError::DecreaseingDifficultyFactor);
                }
            }

            Ok(Defense {
                levels: self.levels.to_owned(),
                current_visitor_threshold: 0,
            })
        } else {
            Err(CaptchaError::LevelEmpty)
        }
    }
}

impl Defense {
    ///! Difficulty is calculated as:
    ///! ```rust
    ///! let difficulty = u128::max_value() - u128::max_value() / difficulty_factor;
    ///! ```
    ///! The higher the `difficulty_factor`, the higher the difficulty.

    /// Get difficulty factor of current level of defense
    pub fn get_difficulty(&self) -> u32 {
        self.levels[self.current_visitor_threshold].difficulty_factor
    }

    /// tighten up defense. Increases defense level by a factor of one.
    /// When defense is at max level, calling this method will have no effect
    pub fn tighten_up(&mut self) {
        if self.current_visitor_threshold != self.levels.len() - 1 {
            self.current_visitor_threshold += 1;
        }
    }
    /// Loosen up defense. Decreases defense level by a factor of one.
    /// When defense is at the lowest level, calling this method will have no effect.
    pub fn loosen_up(&mut self) {
        if self.current_visitor_threshold != 0 {
            self.current_visitor_threshold -= 1;
        }
    }

    /// Set defense to maximum level
    pub fn max_defense(&mut self) {
        self.current_visitor_threshold = self.levels.len() - 1;
    }

    /// Set defense to minimum level
    pub fn min_defense(&mut self) {
        self.current_visitor_threshold = 0;
    }

    /// Get current level's  visitor threshold
    pub fn visitor_threshold(&self) -> u32 {
        self.levels[self.current_visitor_threshold].visitor_threshold
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn level_builder_works() {
        let level = LevelBuilder::default()
            .difficulty_factor(1)
            .unwrap()
            .visitor_threshold(0)
            .build()
            .unwrap();

        assert_eq!(level.visitor_threshold, 0);
        assert_eq!(level.difficulty_factor, 1);

        assert_eq!(
            LevelBuilder::default().difficulty_factor(0),
            Err(CaptchaError::DifficultyFactorZero)
        );

        assert_eq!(
            LevelBuilder::default()
                .difficulty_factor(1)
                .unwrap()
                .build(),
            Err(CaptchaError::SetVisitorThreshold)
        );
        assert_eq!(
            LevelBuilder::default().visitor_threshold(10).build(),
            Err(CaptchaError::SetDifficultyFactor)
        );
    }

    #[test]
    fn defense_builder_duplicate_visitor_threshold() {
        let mut defense_builder = DefenseBuilder::default();
        let err = defense_builder
            .add_level(
                LevelBuilder::default()
                    .visitor_threshold(50)
                    .difficulty_factor(50)
                    .unwrap()
                    .build()
                    .unwrap(),
            )
            .unwrap()
            .add_level(
                LevelBuilder::default()
                    .visitor_threshold(50)
                    .difficulty_factor(50)
                    .unwrap()
                    .build()
                    .unwrap(),
            );
        assert_eq!(err, Err(CaptchaError::DuplicateVisitorCount));
    }

    #[test]
    fn defense_builder_decreasing_difficulty_factor() {
        let mut defense_builder = DefenseBuilder::default();
        let err = defense_builder
            .add_level(
                LevelBuilder::default()
                    .visitor_threshold(50)
                    .difficulty_factor(50)
                    .unwrap()
                    .build()
                    .unwrap(),
            )
            .unwrap()
            .add_level(
                LevelBuilder::default()
                    .visitor_threshold(500)
                    .difficulty_factor(10)
                    .unwrap()
                    .build()
                    .unwrap(),
            )
            .unwrap()
            .build();
        assert_eq!(err, Err(CaptchaError::DecreaseingDifficultyFactor));
    }

    fn get_defense() -> Defense {
        DefenseBuilder::default()
            .add_level(
                LevelBuilder::default()
                    .visitor_threshold(50)
                    .difficulty_factor(50)
                    .unwrap()
                    .build()
                    .unwrap(),
            )
            .unwrap()
            .add_level(
                LevelBuilder::default()
                    .visitor_threshold(500)
                    .difficulty_factor(5000)
                    .unwrap()
                    .build()
                    .unwrap(),
            )
            .unwrap()
            .add_level(
                LevelBuilder::default()
                    .visitor_threshold(5000)
                    .difficulty_factor(50000)
                    .unwrap()
                    .build()
                    .unwrap(),
            )
            .unwrap()
            .add_level(
                LevelBuilder::default()
                    .visitor_threshold(50000)
                    .difficulty_factor(500000)
                    .unwrap()
                    .build()
                    .unwrap(),
            )
            .unwrap()
            .add_level(
                LevelBuilder::default()
                    .visitor_threshold(500000)
                    .difficulty_factor(5000000)
                    .unwrap()
                    .build()
                    .unwrap(),
            )
            .unwrap()
            .build()
            .unwrap()
    }
    #[test]
    fn defense_builder_works() {
        let defense = get_defense();

        assert_eq!(defense.levels[0].difficulty_factor, 50);
        assert_eq!(defense.levels[1].difficulty_factor, 5000);
        assert_eq!(defense.levels[2].difficulty_factor, 50_000);
        assert_eq!(defense.levels[3].difficulty_factor, 500_000);
        assert_eq!(defense.levels[4].difficulty_factor, 5_000_000);
    }

    #[test]
    fn tighten_up_works() {
        let mut defense = get_defense();

        assert_eq!(defense.get_difficulty(), 50);

        defense.tighten_up();
        assert_eq!(defense.get_difficulty(), 5_000);

        defense.tighten_up();
        assert_eq!(defense.get_difficulty(), 50_000);

        defense.tighten_up();
        assert_eq!(defense.get_difficulty(), 500_000);

        defense.tighten_up();
        assert_eq!(defense.get_difficulty(), 5_000_000);

        defense.tighten_up();
        assert_eq!(defense.get_difficulty(), 5_000_000);
    }

    #[test]
    fn max_defense_works() {
        let mut defense = get_defense();
        defense.max_defense();
        assert_eq!(defense.get_difficulty(), 5_000_000);
    }

    #[test]
    fn minimum_defense_works() {
        let mut defense = get_defense();
        defense.min_defense();
        assert_eq!(defense.get_difficulty(), 50);
    }

    #[test]
    fn loosen_up_works() {
        let mut defense = get_defense();
        defense.max_defense();

        assert_eq!(defense.get_difficulty(), 5_000_000);

        defense.loosen_up();
        assert_eq!(defense.get_difficulty(), 500_000);

        defense.loosen_up();
        assert_eq!(defense.get_difficulty(), 50_000);

        defense.loosen_up();
        assert_eq!(defense.get_difficulty(), 5_000);

        defense.loosen_up();
        assert_eq!(defense.get_difficulty(), 50);

        defense.loosen_up();
        assert_eq!(defense.get_difficulty(), 50);
    }
}