darkwing/server/dtos/entities/browser_profile_dto/
mod.rs

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
//! Browser profile data transfer objects and related types
//!
//! This module contains DTOs and enums for managing browser profile
//! configurations, particularly focusing on browser fingerprint handling. It
//! includes:
//!
//! - The `Mode` enum for different fingerprint override strategies
//! - Implementations for converting between string representations and modes
//! - Re-exports of browser profile and override-specific types
//!
//! The types in this module facilitate the transfer and manipulation of browser
//! profile data between different parts of the application.

use serde::Deserialize;
use tracing::debug;

mod browser_profile;
mod overrides;

pub use browser_profile::*;
pub use overrides::*;

use crate::server::error::Error;

/// Represents different modes for browser fingerprint overrides
///
/// This enum is used to control how browser fingerprint are handled and stored
/// in the MySQL database. Each mode determines a specific strategy for managing
/// fingerprint data.
#[derive(Debug, Deserialize, PartialEq, Eq, Copy, Clone)]
#[serde(rename_all = "lowercase")]
pub enum Mode {
  /// Manually specified fingerprint values sent into API
  Manual,
  /// Generates completely random fingerprint values
  Random,
  /// Adds random noise to real fingerprint values
  Noise,
  /// Disables fingerprint modifications
  Off,
  /// Automatically determines appropriate fingerprint values
  Auto,
  /// Uses real browser fingerprint values without modification
  Real,
  /// Altered appropriate fingerprint values, basically like Auto
  Altered,
  /// Applies privacy-focused modifications to fingerprint (currently used only
  /// for Ports)
  Protect,
  /// Returns empty or null values for fingerprint attributes
  Empty,
  /// Uses software mode instead of hardware mode. Used for videocard and
  /// rendering overrides
  Software,
  /// Represents an unrecognized mode value
  Unknown,
}

impl std::str::FromStr for Mode {
  type Err = Error;

  /// Converts a string to a Mode variant
  ///
  /// # Arguments
  /// * `s` - The string to convert
  ///
  /// # Returns
  /// * `Ok(Mode)` - The corresponding Mode variant
  /// * `Err(Error)` - If the string doesn't match any known mode
  ///
  /// # Examples
  /// ```
  /// use std::str::FromStr;
  ///
  /// let mode = Mode::from_str("real").expect("Valid mode");
  /// assert_eq!(mode, Mode::Real);
  ///
  /// let error = Mode::from_str("invalid").unwrap_err();
  /// assert!(matches!(error, Error::UnknownOverrideMode(_)));
  /// ```
  fn from_str(s: &str) -> Result<Self, Self::Err> {
    match s.to_lowercase().as_str() {
      "manual" => Ok(Mode::Manual),
      "random" => Ok(Mode::Random),
      "noise" => Ok(Mode::Noise),
      "off" => Ok(Mode::Off),
      "auto" => Ok(Mode::Auto),
      "real" => Ok(Mode::Real),
      "on" => Ok(Mode::Real),
      "altered" => Ok(Mode::Altered),
      "protect" => Ok(Mode::Protect),
      "empty" => Ok(Mode::Empty),
      "software" => Ok(Mode::Software),
      _ => {
        debug!("Unknown mode: {}", s);
        sentry::capture_error(&Error::UnknownMode(s.to_string()));
        Err(Error::UnknownOverrideMode(s.to_string()))
      }
    }
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use std::str::FromStr as _;

  #[test]
  fn test_mode_from_str() {
    assert_eq!(Mode::from_str("real").unwrap(), Mode::Real);
    assert_eq!(Mode::from_str("manual").unwrap(), Mode::Manual);
    assert_eq!(Mode::from_str("altered").unwrap(), Mode::Altered);
    assert_eq!(Mode::from_str("off").unwrap(), Mode::Off);
    assert_eq!(Mode::from_str("auto").unwrap(), Mode::Auto);
    assert_eq!(
      Mode::from_str("test").unwrap_err().to_string(),
      "unknown override mode: test"
    );
  }
}