summaryrefslogtreecommitdiffhomepage
path: root/mullvad-types/src/features.rs
blob: 15f64e999256f83df139b957f6037ca9b30fcff2 (plain)
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use std::{
    collections::HashSet,
    fmt::{Debug, Display},
};

use crate::settings::{DnsState, Settings};
use serde::{Deserialize, Serialize};
use talpid_types::net::{ObfuscationType, TunnelEndpoint, TunnelType};

/// Feature indicators are active settings that should be shown to the user to make them aware of
/// what is affecting their connection at any given time.
///
/// Note that the feature indicators are not ordered.
#[derive(Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FeatureIndicators(HashSet<FeatureIndicator>);

impl Debug for FeatureIndicators {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut indicators: Vec<&str> = self.0.iter().map(|feature| feature.to_str()).collect();
        // Sort the features alphabetically (Just to have some order, arbitrarily chosen)
        indicators.sort();
        f.debug_tuple("FeatureIndicators")
            .field(&indicators)
            .finish()
    }
}

impl FeatureIndicators {
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

impl Display for FeatureIndicators {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut indicators: Vec<&str> = self.0.iter().map(|feature| feature.to_str()).collect();
        // Sort the features alphabetically (Just to have some order, arbitrarily chosen)
        indicators.sort();

        write!(f, "{}", indicators.join(", "))
    }
}

impl IntoIterator for FeatureIndicators {
    type Item = FeatureIndicator;
    type IntoIter = std::collections::hash_set::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl FeatureIndicators {
    pub fn active_features(&self) -> impl Iterator<Item = FeatureIndicator> {
        self.0.clone().into_iter()
    }
}

impl FromIterator<FeatureIndicator> for FeatureIndicators {
    fn from_iter<T: IntoIterator<Item = FeatureIndicator>>(iter: T) -> Self {
        Self(iter.into_iter().collect())
    }
}

/// All possible feature indicators. These represent a subset of all VPN settings in a
/// non-technical fashion.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FeatureIndicator {
    QuantumResistance,
    Multihop,
    BridgeMode,
    SplitTunneling,
    LockdownMode,
    Udp2Tcp,
    Shadowsocks,
    LanSharing,
    DnsContentBlockers,
    CustomDns,
    ServerIpOverride,
    CustomMtu,
    CustomMssFix,
    Daita,
}

impl FeatureIndicator {
    const fn to_str(&self) -> &'static str {
        match self {
            FeatureIndicator::QuantumResistance => "Quantum Resistance",
            FeatureIndicator::Multihop => "Multihop",
            FeatureIndicator::BridgeMode => "Bridge Mode",
            FeatureIndicator::SplitTunneling => "Split Tunneling",
            FeatureIndicator::LockdownMode => "Lockdown Mode",
            FeatureIndicator::Udp2Tcp => "Udp2Tcp",
            FeatureIndicator::Shadowsocks => "Shadowsocks",
            FeatureIndicator::LanSharing => "LAN Sharing",
            FeatureIndicator::DnsContentBlockers => "Dns Content Blocker",
            FeatureIndicator::CustomDns => "Custom Dns",
            FeatureIndicator::ServerIpOverride => "Server Ip Override",
            FeatureIndicator::CustomMtu => "Custom MTU",
            FeatureIndicator::CustomMssFix => "Custom MSS",
            FeatureIndicator::Daita => "DAITA",
        }
    }
}

impl std::fmt::Display for FeatureIndicator {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let feature = self.to_str();
        write!(f, "{feature}")
    }
}

/// Calculate active [`FeatureIndicators`] from setting and endpoint information.
///
/// Note that [`FeatureIndicators`] are only applicable for the connected and connecting states, and
/// this function should not be called with arguments from different tunnel states.
///
/// Server ip override cannot be determined from the settings and endpoint, and has to be fetched
/// from the relay selector parameter generator.
pub fn compute_feature_indicators(
    settings: &Settings,
    endpoint: &TunnelEndpoint,
    server_ip_override: bool,
) -> FeatureIndicators {
    #[cfg(any(windows, target_os = "android", target_os = "macos"))]
    let split_tunneling = settings.split_tunnel.enable_exclusions;
    #[cfg(not(any(windows, target_os = "android", target_os = "macos")))]
    let split_tunneling = false;

    #[cfg(not(target_os = "android"))]
    let lockdown_mode = settings.block_when_disconnected;
    let lan_sharing = settings.allow_lan;
    let dns_content_blockers = settings
        .tunnel_options
        .dns_options
        .default_options
        .any_blockers_enabled();
    let custom_dns = settings.tunnel_options.dns_options.state == DnsState::Custom;

    let generic_features = [
        (split_tunneling, FeatureIndicator::SplitTunneling),
        (lan_sharing, FeatureIndicator::LanSharing),
        (dns_content_blockers, FeatureIndicator::DnsContentBlockers),
        (custom_dns, FeatureIndicator::CustomDns),
        (server_ip_override, FeatureIndicator::ServerIpOverride),
        #[cfg(not(target_os = "android"))]
        (lockdown_mode, FeatureIndicator::LockdownMode),
    ];

    // Pick protocol-specific features and whether they are currently enabled.
    let protocol_features = match endpoint.tunnel_type {
        TunnelType::OpenVpn => {
            let bridge_mode = endpoint.proxy.is_some();
            let mss_fix = settings.tunnel_options.openvpn.mssfix.is_some();

            vec![
                (bridge_mode, FeatureIndicator::BridgeMode),
                (mss_fix, FeatureIndicator::CustomMssFix),
            ]
        }
        TunnelType::Wireguard => {
            let quantum_resistant = endpoint.quantum_resistant;
            let udp_tcp = endpoint
                .obfuscation
                .as_ref()
                .filter(|obfuscation| obfuscation.obfuscation_type == ObfuscationType::Udp2Tcp)
                .is_some();
            let shadowsocks = endpoint
                .obfuscation
                .as_ref()
                .filter(|obfuscation| obfuscation.obfuscation_type == ObfuscationType::Shadowsocks)
                .is_some();

            let mtu = settings.tunnel_options.wireguard.mtu.is_some();

            let mut daita = false;
            let multihop = endpoint.entry_endpoint.is_some();

            #[cfg(daita)]
            if endpoint.daita {
                daita = true;
            }

            vec![
                (quantum_resistant, FeatureIndicator::QuantumResistance),
                (multihop, FeatureIndicator::Multihop),
                (udp_tcp, FeatureIndicator::Udp2Tcp),
                (shadowsocks, FeatureIndicator::Shadowsocks),
                (mtu, FeatureIndicator::CustomMtu),
                (daita, FeatureIndicator::Daita),
            ]
        }
    };

    // use the booleans to filter into a list of only the active features
    generic_features
        .into_iter()
        .chain(protocol_features)
        .filter_map(|(active, feature)| active.then_some(feature))
        .collect()
}

#[cfg(test)]
mod tests {
    use std::net::{IpAddr, Ipv4Addr, SocketAddr};

    use talpid_types::net::{
        proxy::{ProxyEndpoint, ProxyType},
        Endpoint, ObfuscationEndpoint, TransportProtocol,
    };

    use crate::relay_constraints::RelaySettings;

    use super::*;

    #[test]
    fn test_one_indicator_at_a_time() {
        let mut settings = Settings::default();
        let mut endpoint = TunnelEndpoint {
            endpoint: Endpoint {
                address: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080),
                protocol: TransportProtocol::Udp,
            },
            tunnel_type: TunnelType::Wireguard,
            quantum_resistant: Default::default(),
            proxy: Default::default(),
            obfuscation: Default::default(),
            entry_endpoint: Default::default(),
            tunnel_interface: Default::default(),
            daita: Default::default(),
        };

        let mut expected_indicators: FeatureIndicators = [].into_iter().collect();

        assert_eq!(
            compute_feature_indicators(&settings, &endpoint, false),
            expected_indicators,
            "The default settings and TunnelEndpoint should not have any feature indicators. \
            If this is not true anymore, please update this test."
        );

        settings.block_when_disconnected = true;
        expected_indicators.0.insert(FeatureIndicator::LockdownMode);

        assert_eq!(
            compute_feature_indicators(&settings, &endpoint, false),
            expected_indicators
        );

        settings
            .tunnel_options
            .dns_options
            .default_options
            .block_ads = true;

        expected_indicators
            .0
            .insert(FeatureIndicator::DnsContentBlockers);

        assert_eq!(
            compute_feature_indicators(&settings, &endpoint, false),
            expected_indicators
        );

        settings.allow_lan = true;

        expected_indicators.0.insert(FeatureIndicator::LanSharing);

        assert_eq!(
            compute_feature_indicators(&settings, &endpoint, false),
            expected_indicators
        );

        settings.tunnel_options.openvpn.mssfix = Some(1300);
        assert_eq!(
            compute_feature_indicators(&settings, &endpoint, false),
            expected_indicators,
            "Setting mssfix without having an openVPN endpoint should not result in an indicator"
        );

        endpoint.tunnel_type = TunnelType::OpenVpn;
        expected_indicators.0.insert(FeatureIndicator::CustomMssFix);

        assert_eq!(
            compute_feature_indicators(&settings, &endpoint, false),
            expected_indicators
        );

        endpoint.proxy = Some(ProxyEndpoint {
            endpoint: Endpoint {
                address: SocketAddr::from(([1, 2, 3, 4], 443)),
                protocol: TransportProtocol::Tcp,
            },
            proxy_type: ProxyType::Shadowsocks,
        });

        expected_indicators.0.insert(FeatureIndicator::BridgeMode);
        assert_eq!(
            compute_feature_indicators(&settings, &endpoint, false),
            expected_indicators
        );

        endpoint.tunnel_type = TunnelType::Wireguard;
        expected_indicators
            .0
            .remove(&FeatureIndicator::CustomMssFix);
        expected_indicators.0.remove(&FeatureIndicator::BridgeMode);
        assert_eq!(
            compute_feature_indicators(&settings, &endpoint, false),
            expected_indicators
        );

        endpoint.quantum_resistant = true;
        expected_indicators
            .0
            .insert(FeatureIndicator::QuantumResistance);
        assert_eq!(
            compute_feature_indicators(&settings, &endpoint, false),
            expected_indicators
        );

        if let RelaySettings::Normal(constraints) = &mut settings.relay_settings {
            constraints.wireguard_constraints.use_multihop = true;
        };
        assert_eq!(
            compute_feature_indicators(&settings, &endpoint, false),
            expected_indicators,
            "The multihop feature indicator should be enabled by the endpoint, not the settings"
        );
        endpoint.entry_endpoint = Some(Endpoint {
            address: SocketAddr::from(([1, 2, 3, 4], 443)),
            protocol: TransportProtocol::Tcp,
        });
        expected_indicators.0.insert(FeatureIndicator::Multihop);
        assert_eq!(
            compute_feature_indicators(&settings, &endpoint, false),
            expected_indicators
        );

        endpoint.obfuscation = Some(ObfuscationEndpoint {
            endpoint: Endpoint {
                address: SocketAddr::from(([1, 2, 3, 4], 443)),
                protocol: TransportProtocol::Tcp,
            },
            obfuscation_type: ObfuscationType::Udp2Tcp,
        });
        expected_indicators.0.insert(FeatureIndicator::Udp2Tcp);
        assert_eq!(
            compute_feature_indicators(&settings, &endpoint, false),
            expected_indicators
        );
        endpoint.obfuscation.as_mut().unwrap().obfuscation_type = ObfuscationType::Shadowsocks;
        expected_indicators.0.remove(&FeatureIndicator::Udp2Tcp);
        expected_indicators.0.insert(FeatureIndicator::Shadowsocks);
        assert_eq!(
            compute_feature_indicators(&settings, &endpoint, false),
            expected_indicators
        );

        settings.tunnel_options.wireguard.mtu = Some(1300);
        expected_indicators.0.insert(FeatureIndicator::CustomMtu);
        assert_eq!(
            compute_feature_indicators(&settings, &endpoint, false),
            expected_indicators
        );

        #[cfg(daita)]
        {
            // Multihop and DAITA on
            endpoint.daita = true;
            settings
                .tunnel_options
                .wireguard
                .daita
                .use_multihop_if_necessary = true;

            expected_indicators.0.insert(FeatureIndicator::Daita);
            assert_eq!(
                compute_feature_indicators(&settings, &endpoint, false),
                expected_indicators
            );

            // Should not change regardless of whether `use_multihop_if_necessary` is true, since
            // multihop is enabled explicitly
            settings
                .tunnel_options
                .wireguard
                .daita
                .use_multihop_if_necessary = false;
            assert_eq!(
                compute_feature_indicators(&settings, &endpoint, false),
                expected_indicators,
            );

            // Here we mock that multihop was automatically enabled by DAITA.
            // We enable `use_multihop_if_necessary` again and disable the multihop setting, while
            // keeping the entry relay. In this scenario, we should still get a Multihop
            // indicator.
            settings
                .tunnel_options
                .wireguard
                .daita
                .use_multihop_if_necessary = true;
            if let RelaySettings::Normal(constraints) = &mut settings.relay_settings {
                constraints.wireguard_constraints.use_multihop = false;
            };
            assert_eq!(
                compute_feature_indicators(&settings, &endpoint, false),
                expected_indicators,
                "DaitaDirectOnly should be enabled"
            );

            // If we also remove the entry relay, we should not get a multihop indicator
            endpoint.entry_endpoint = None;
            expected_indicators.0.remove(&FeatureIndicator::Multihop);
            assert_eq!(
                compute_feature_indicators(&settings, &endpoint, false),
                expected_indicators,
                "DaitaDirectOnly should be enabled"
            );
        }

        // NOTE: If this match statement fails to compile, it means that a new feature indicator has
        // been added. Please update this test to include the new feature indicator.
        match FeatureIndicator::QuantumResistance {
            FeatureIndicator::QuantumResistance => {}
            FeatureIndicator::Multihop => {}
            FeatureIndicator::BridgeMode => {}
            FeatureIndicator::SplitTunneling => {}
            FeatureIndicator::LockdownMode => {}
            FeatureIndicator::Udp2Tcp => {}
            FeatureIndicator::Shadowsocks => {}
            FeatureIndicator::LanSharing => {}
            FeatureIndicator::DnsContentBlockers => {}
            FeatureIndicator::CustomDns => {}
            FeatureIndicator::ServerIpOverride => {}
            FeatureIndicator::CustomMtu => {}
            FeatureIndicator::CustomMssFix => {}
            FeatureIndicator::Daita => {}
        }
    }
}