darkwing/server/services/browser_profile_services/config/
hints.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
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
//! Configuration for browser hints and capabilities.
//! This module handles browser-specific information like architecture, platform
//! details, and user agent strings that help identify the browser profile.

use crate::{
  database::browser_profile::Platform,
  server::{
    dtos::{
      browser_profile_dto::BrowserProfileFullData, start_dto::StartRequest,
    },
    error::Error,
  },
};
use serde::{Deserialize, Serialize};

use super::{
  consts::{
    CHROME_VERSION_MAP, LATEST_CHROME_FULL_VERSION, LATEST_CHROME_VERSION,
  },
  FromStartRequest, Navigator, Screen,
};

/// Represents browser hints and capabilities.
/// Contains information about the system architecture, platform, and browser
/// details that are used to configure the browser profile.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct Hints {
  /// CPU architecture (e.g., "x86" or "arm")
  pub(super) architecture: String,
  /// CPU bitness (e.g., "64")
  pub(super) bitness: String,
  /// Device model identifier
  pub(super) model: String,
  /// Operating system platform (e.g., "Windows", "macOS", "Linux")
  pub(super) platform: String,
  /// Operating system version (e.g., "10.0.0")
  pub(super) platform_version: String,
  /// Full browser version string
  pub(super) ua_full_version: String,
  /// Whether the browser is running in mobile mode
  pub(super) mobile: bool,
  /// User agent header string
  pub(super) ua_header: String,
  /// Browser viewport width in pixels
  pub(super) viewport_width: i16,
}

impl Hints {
  /// Determines the CPU architecture based on the browser profile data.
  ///
  /// # Arguments
  /// * `bp` - Browser profile data containing WebGL information
  ///
  /// # Returns
  /// * `Ok(String)` - "arm" for Apple Silicon devices, "x86" otherwise
  pub fn make_architecture(
    bp: &BrowserProfileFullData,
  ) -> Result<String, Error> {
    let mut architecture = "x86".to_string();

    if let Some(renderer) = &bp.webgl_info.renderer {
      if renderer.contains("Apple M") || renderer.contains("AppleM") {
        architecture = "arm".to_string();
      }
    };

    Ok(architecture)
  }

  /// Determines the platform string based on the browser profile data.
  ///
  /// # Arguments
  /// * `bp` - Browser profile data containing platform information
  ///
  /// # Returns
  /// * `Ok(String)` - Platform string ("Windows", "macOS", or "Linux")
  pub fn make_platform(bp: &BrowserProfileFullData) -> Result<String, Error> {
    let platform = match bp.platform {
      Platform::Windows => "Windows".to_string(),
      Platform::Macos => "macOS".to_string(),
      Platform::Linux => "Linux".to_string(),
    };

    Ok(platform)
  }

  /// Determines the platform version string based on the browser profile data.
  ///
  /// # Arguments
  /// * `bp` - Browser profile data containing platform version information
  ///
  /// # Returns
  /// * `Ok(String)` - Platform version string (e.g., "10.0.0" for Windows)
  pub fn make_platform_version(
    bp: &BrowserProfileFullData,
  ) -> Result<String, Error> {
    let platform_version = if !bp.platform_version.is_empty() {
      bp.platform_version.clone()
    } else {
      match bp.platform {
        Platform::Windows => "10.0.0".to_string(),
        Platform::Macos => "15.0.0".to_string(),
        Platform::Linux => "6.5.0".to_string(),
      }
    };

    Ok(platform_version)
  }

  /// Determines the full browser version string based on the browser profile
  /// and navigator data.
  ///
  /// # Arguments
  /// * `bp` - Browser profile data
  /// * `navigator` - Navigator information containing user agent string
  ///
  /// # Returns
  /// * `Ok(String)` - Full browser version string
  pub fn make_ua_full_version(
    bp: &BrowserProfileFullData,
    navigator: &Navigator,
  ) -> Result<String, Error> {
    let major_version: String = navigator
      .user_agent
      .chars()
      .rev()
      .take(24)
      .skip(21)
      .collect();

    let latest_major_version = LATEST_CHROME_VERSION.to_string();
    let latest_full_version = LATEST_CHROME_FULL_VERSION.as_str();

    let version = CHROME_VERSION_MAP
      .get(&(major_version.as_str(), bp.platform.clone()))
      .unwrap_or(
        CHROME_VERSION_MAP
          .get(&(latest_major_version.as_str(), bp.platform.clone()))
          .unwrap_or(&latest_full_version),
      );

    Ok(version.to_string())
  }
}

/// Implementation of FromStartRequest trait for Hints
/// Allows creating Hints from a start request and related data
impl FromStartRequest<Hints> for Hints {
  /// Creates a new Hints instance from the provided request and profile data
  ///
  /// # Arguments
  /// * `bp` - Browser profile data
  /// * `_request` - Start request data (unused)
  /// * `navigator` - Navigator information
  /// * `screen` - Screen configuration
  /// * `_token` - Authentication token (unused)
  ///
  /// # Returns
  /// * `Ok(Hints)` - New Hints instance
  fn from_start_request(
    bp: &BrowserProfileFullData,
    _request: &StartRequest,
    navigator: &Navigator,
    screen: &Screen,
    _token: &str,
  ) -> Result<Self, Error> {
    let architecture = Self::make_architecture(bp)?;

    let platform_version = Self::make_platform_version(bp)?;

    let platform = Self::make_platform(bp)?;

    let ua_full_version = Self::make_ua_full_version(bp, navigator)?;

    let ua_header =
      r#"" Not A;Brand";v="99", "Chromium";v="91", "Google Chrome";v="91""#
        .to_string();

    let viewport_width = screen.width;

    Ok(Self {
      architecture,
      bitness: "64".to_string(),
      model: "".to_string(),
      platform,
      platform_version,
      ua_full_version,
      mobile: false,
      ua_header,
      viewport_width,
    })
  }
}

/// Tests for the Hints implementation
#[cfg(test)]
mod tests {
  use super::*;
  use crate::server::dtos::browser_profile_dto::WebglInfo;
  use crate::server::services::browser_profile_services::config::consts::LINUX_MOCK_PROFILE;

  fn create_base_profile() -> BrowserProfileFullData {
    LINUX_MOCK_PROFILE.clone()
  }

  #[test]
  fn test_make_architecture_x86() -> Result<(), Error> {
    let mut profile = create_base_profile();
    profile.webgl_info = WebglInfo {
      renderer: Some("NVIDIA GeForce RTX 3080".to_string()),
      ..LINUX_MOCK_PROFILE.clone().webgl_info
    };

    let architecture = Hints::make_architecture(&profile)?;
    assert_eq!(architecture, "x86");
    Ok(())
  }

  #[test]
  fn test_make_architecture_arm() -> Result<(), Error> {
    let mut profile = create_base_profile();
    profile.webgl_info = WebglInfo {
      renderer: Some("Apple M1".to_string()),
      ..LINUX_MOCK_PROFILE.clone().webgl_info
    };

    let architecture = Hints::make_architecture(&profile)?;
    assert_eq!(architecture, "arm");

    // Test AppleM variant
    profile.webgl_info.renderer = Some("AppleM2".to_string());
    let architecture = Hints::make_architecture(&profile)?;
    assert_eq!(architecture, "arm");
    Ok(())
  }

  #[test]
  fn test_make_platform() -> Result<(), Error> {
    let mut profile = create_base_profile();

    profile.platform = Platform::Windows;
    assert_eq!(Hints::make_platform(&profile)?, "Windows");

    profile.platform = Platform::Macos;
    assert_eq!(Hints::make_platform(&profile)?, "macOS");

    profile.platform = Platform::Linux;
    assert_eq!(Hints::make_platform(&profile)?, "Linux");
    Ok(())
  }

  #[test]
  fn test_make_platform_version_custom() -> Result<(), Error> {
    let mut profile = create_base_profile();
    profile.platform_version = "11.5.2".to_string();

    let version = Hints::make_platform_version(&profile)?;
    assert_eq!(version, "11.5.2");
    Ok(())
  }

  #[test]
  fn test_make_platform_version_default() -> Result<(), Error> {
    let mut profile = create_base_profile();
    profile.platform_version = "".to_string();

    profile.platform = Platform::Windows;
    assert_eq!(Hints::make_platform_version(&profile)?, "10.0.0");

    profile.platform = Platform::Macos;
    assert_eq!(Hints::make_platform_version(&profile)?, "15.0.0");

    profile.platform = Platform::Linux;
    assert_eq!(Hints::make_platform_version(&profile)?, "6.5.0");
    Ok(())
  }

  #[test]
  fn test_make_ua_full_version_from_map() -> Result<(), Error> {
    let profile = create_base_profile();
    let mut navigator = Navigator::default();
    navigator.user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36".to_string();

    let version = Hints::make_ua_full_version(&profile, &navigator)?;
    println!("version: {}", version);
    assert!(CHROME_VERSION_MAP.values().any(|&v| v == version));
    Ok(())
  }

  #[test]
  fn test_from_start_request() -> Result<(), Error> {
    let profile = create_base_profile();
    let request = StartRequest::get_mock();
    let navigator = Navigator::default();
    let screen = Screen {
      width: 1920,
      height: 1080,
      is_real: false,
      dpr: 1.,
      depth: 24,
    };
    let token = String::new();

    let hints = Hints::from_start_request(
      &profile, &request, &navigator, &screen, &token,
    )?;

    assert_eq!(hints.bitness, "64");
    assert_eq!(hints.model, "");
    assert!(!hints.mobile);
    assert_eq!(hints.viewport_width, 1920);
    assert_eq!(
      hints.ua_header,
      r#"" Not A;Brand";v="99", "Chromium";v="91", "Google Chrome";v="91""#
    );
    Ok(())
  }
}