darkwing/server/services/browser_profile_services/config/
ports.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
//! Port protection configuration module
//!
//! This module handles the configuration of port protection settings for
//! browser profiles, including port blacklisting functionality.

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

use super::{FromStartRequest, Navigator, Screen};

/// Configuration for port protection features
///
/// Controls which ports should be blocked when launching a browser profile
/// to prevent unauthorized remote access attempts.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct PortsProtection {
  /// Whether port protection is enabled
  pub(super) enabled: bool,
  /// List of ports that should be blocked
  pub(super) blacklist: Vec<u16>,
}

/// Implementation of conversion from start request for port protection settings
impl FromStartRequest<PortsProtection> for PortsProtection {
  /// Creates a PortsProtection configuration from a browser profile and start
  /// request
  fn from_start_request(
    bp: &BrowserProfileFullData,
    _request: &StartRequest,
    _navigator: &Navigator,
    _screen: &Screen,
    _token: &str,
  ) -> Result<Self, Error> {
    let blacklist = if bp.ports.mode == Mode::Protect {
      bp.ports.blacklist.clone()
    } else {
      Vec::new()
    };

    Ok(Self {
      enabled: true, // from original code - always enabled
      blacklist,
    })
  }
}

/// Unit tests for port protection functionality
#[cfg(test)]
mod tests {
  use crate::server::services::browser_profile_services::config::consts::LINUX_MOCK_PROFILE;

  use super::*;

  /// Tests the conversion from start request to port protection settings
  #[test]
  fn test_from_start_request() {
    let profile = LINUX_MOCK_PROFILE.clone();
    let request = StartRequest::get_mock();

    let result = PortsProtection::from_start_request(
      &profile,
      &request,
      &Navigator::default(),
      &Screen::default(),
      &"".to_string(),
    );
    assert_eq!(result.is_ok(), true);
    let ports_protection = result.expect("Failed to create PortsProtection");
    assert_eq!(ports_protection.enabled, true);
    assert_eq!(
      ports_protection.blacklist,
      vec![
        3389, 5900, 5800, 7070, 6568, 5938, 63333, 5901, 5902, 5903, 5950,
        5931, 5939, 6039, 5944, 6040, 5279, 2112,
      ]
    );
  }
}