summaryrefslogtreecommitdiffhomepage
path: root/mullvad-relay-selector/src/relay_selector/query.rs
blob: 93931cfa7de0f0320bd0efee5474417d5439c9b7 (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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
//! This module provides a flexible way to specify 'queries' for relays.
//!
//! A query is a set of constraints that the [`crate::RelaySelector`] will use when filtering out
//! potential relays that the daemon should connect to. It supports filtering relays by geographic
//! location, provider, ownership, and tunnel protocol, along with protocol-specific settings for
//! WireGuard and OpenVPN.
//!
//! The main components of this module include:
//!
//! - [`RelayQuery`]: The core struct for specifying a query to select relay servers. It aggregates
//!   constraints on location, providers, ownership, tunnel protocol, and protocol-specific
//!   constraints for WireGuard and OpenVPN.
//! - [`WireguardRelayQuery`] and [`OpenVpnRelayQuery`]: Structs that define protocol-specific
//!   constraints for selecting WireGuard and OpenVPN relays, respectively.
//! - [`Intersection`]: A trait implemented by the different query types that support intersection
//!   logic, which allows for combining two queries into a single query that represents the common
//!   constraints of both.
//! - [Builder patterns][builder]: The module also provides builder patterns for creating instances
//!   of `RelayQuery`, `WireguardRelayQuery`, and `OpenVpnRelayQuery` with a fluent API.
//!
//! ## Design
//!
//! This module has been built in such a way that it should be easy to reason about,
//! while providing a flexible and easy-to-use API. The `Intersection` trait provides
//! a robust framework for combining and refining queries based on multiple criteria.
//!
//! The builder patterns included in the module simplify the process of constructing
//! queries and ensure that queries are built in a type-safe manner, reducing the risk
//! of runtime errors and improving code readability.

use crate::Error;
use mullvad_types::{
    Intersection,
    constraints::Constraint,
    relay_constraints::{
        BridgeConstraints, BridgeSettings, BridgeState, BridgeType, LocationConstraint,
        ObfuscationSettings, OpenVpnConstraints, Ownership, Providers, RelayConstraints,
        RelaySettings, SelectedObfuscation, ShadowsocksSettings, TransportPort,
        Udp2TcpObfuscationSettings, WireguardConstraints, allowed_ip::AllowedIps,
    },
    wireguard::QuantumResistantState,
};
use talpid_types::net::{IpVersion, TunnelType, proxy::CustomProxy};

/// Represents a query for a relay based on various constraints.
///
/// This struct contains constraints for the location, providers, ownership,
/// tunnel protocol, and additional protocol-specific constraints for WireGuard
/// and OpenVPN. These constraints are used by the [`crate::RelaySelector`] to
/// filter and select suitable relay servers that match the specified criteria.
///
/// A [`RelayQuery`] is best constructed via the fluent builder API exposed by
/// [`builder::RelayQueryBuilder`].
///
/// # Examples
///
/// Creating a basic `RelayQuery` to filter relays by location, ownership and tunnel protocol:
///
/// ```rust
/// // Create a query for a Wireguard relay that is owned by Mullvad and located in Norway.
/// // The endpoint should specify port 443.
/// use mullvad_relay_selector::query::RelayQuery;
/// use mullvad_relay_selector::query::builder::RelayQueryBuilder;
/// use mullvad_relay_selector::query::builder::{Ownership, GeographicLocationConstraint};
///
/// let query: RelayQuery = RelayQueryBuilder::wireguard()      // Specify the tunnel protocol
///     .location(GeographicLocationConstraint::country("no"))  // Specify the country as Norway
///     .ownership(Ownership::MullvadOwned)                     // Specify that the relay must be owned by Mullvad
///     .port(443)                                              // Specify the port to use when connecting to the relay
///     .build();                                               // Construct the query
/// ```
///
/// This example demonstrates creating a `RelayQuery` which can then be passed
/// to the [`crate::RelaySelector`] to find a relay that matches the criteria.
/// See [`builder`] for more info on how to construct queries.
#[derive(Debug, Clone, Eq, PartialEq, Intersection)]
pub struct RelayQuery {
    location: Constraint<LocationConstraint>,
    providers: Constraint<Providers>,
    ownership: Constraint<Ownership>,
    tunnel_protocol: TunnelType,
    wireguard_constraints: WireguardRelayQuery,
    openvpn_constraints: OpenVpnRelayQuery,
}

impl RelayQuery {
    /// Create a new [`RelayQuery`], and fail if the combination of constraints is invalid.
    pub fn new(
        location: Constraint<LocationConstraint>,
        providers: Constraint<Providers>,
        ownership: Constraint<Ownership>,
        tunnel_protocol: TunnelType,
        wireguard_constraints: WireguardRelayQuery,
        openvpn_constraints: OpenVpnRelayQuery,
    ) -> Result<RelayQuery, Error> {
        let mut query = RelayQuery {
            location,
            providers,
            ownership,
            tunnel_protocol,
            wireguard_constraints,
            openvpn_constraints,
        };
        query.validate()?;
        Ok(query)
    }

    fn validate(&mut self) -> Result<(), Error> {
        if self.core_privacy_feature_enabled() {
            if self.tunnel_protocol == TunnelType::OpenVpn {
                log::error!(
                    "Cannot use OpenVPN with a core privacy feature enabled (DAITA = {}, PQ = {}, or multihop = {})",
                    self.wireguard_constraints.daita,
                    self.wireguard_constraints.quantum_resistant,
                    self.wireguard_constraints.multihop()
                );
                return Err(Error::InvalidConstraints);
            }
            self.tunnel_protocol = TunnelType::Wireguard;
        }
        Ok(())
    }

    fn core_privacy_feature_enabled(&self) -> bool {
        self.wireguard_constraints.daita == Constraint::Only(true)
            || self.wireguard_constraints.multihop()
            || self.wireguard_constraints.quantum_resistant == QuantumResistantState::On
    }

    pub fn location(&self) -> &Constraint<LocationConstraint> {
        &self.location
    }

    pub fn set_location(&mut self, location: Constraint<LocationConstraint>) -> Result<(), Error> {
        self.set_if_valid(|query| query.location = location)
    }

    pub fn providers(&self) -> &Constraint<Providers> {
        &self.providers
    }

    pub fn set_providers(&mut self, providers: Constraint<Providers>) {
        self.providers = providers;
    }

    pub fn ownership(&self) -> Constraint<Ownership> {
        self.ownership
    }

    pub fn set_ownership(&mut self, ownership: Constraint<Ownership>) {
        self.ownership = ownership;
    }

    pub fn tunnel_protocol(&self) -> TunnelType {
        self.tunnel_protocol
    }

    pub fn set_tunnel_protocol(&mut self, tunnel_protocol: TunnelType) -> Result<(), Error> {
        self.set_if_valid(|query| query.tunnel_protocol = tunnel_protocol)
    }

    pub fn openvpn_constraints(&self) -> &OpenVpnRelayQuery {
        &self.openvpn_constraints
    }

    pub fn into_openvpn_constraints(self) -> OpenVpnRelayQuery {
        self.openvpn_constraints
    }

    pub fn set_openvpn_constraints(
        &mut self,
        openvpn_constraints: OpenVpnRelayQuery,
    ) -> Result<(), Error> {
        self.set_if_valid(|query| query.openvpn_constraints = openvpn_constraints)
    }

    pub fn wireguard_constraints(&self) -> &WireguardRelayQuery {
        &self.wireguard_constraints
    }

    pub fn into_wireguard_constraints(self) -> WireguardRelayQuery {
        self.wireguard_constraints
    }

    pub fn set_wireguard_constraints(
        &mut self,
        wireguard_constraints: WireguardRelayQuery,
    ) -> Result<(), Error> {
        self.set_if_valid(|query| query.wireguard_constraints = wireguard_constraints)
    }

    fn set_if_valid(&mut self, set_fn: impl FnOnce(&mut Self)) -> Result<(), Error> {
        let mut new = self.clone();
        (set_fn)(&mut new);
        new.validate()?;
        *self = new;

        Ok(())
    }

    /// The mapping from [`RelayQuery`] to all underlying settings types.
    ///
    /// Useful in contexts where you cannot use the query directly but
    /// still want use of the builder for convenience. For example in
    /// end to end tests where you must use the management interface
    /// to apply settings to the daemon.
    pub fn into_settings(
        self,
    ) -> (
        RelayConstraints,
        BridgeState,
        BridgeSettings,
        ObfuscationSettings,
    ) {
        let (bridge_state, bridge_settings) = self
            .openvpn_constraints
            .bridge_settings
            .clone()
            .into_settings();
        let obfuscation = self
            .wireguard_constraints
            .obfuscation
            .clone()
            .into_settings();
        let constraints = RelayConstraints {
            location: self.location,
            providers: self.providers,
            ownership: self.ownership,
            tunnel_protocol: self.tunnel_protocol,
            wireguard_constraints: self.wireguard_constraints.into_constraints(),
            openvpn_constraints: self.openvpn_constraints.into_constraints(),
        };

        (constraints, bridge_state, bridge_settings, obfuscation)
    }
}

impl Default for RelayQuery {
    /// Create a new [`RelayQuery`] with no opinionated defaults. This query matches every relay
    /// with any configuration by setting each of its fields to [`Constraint::Any`].
    ///
    /// Note that the following identity applies for any `other_query`:
    /// ```rust
    /// # use mullvad_relay_selector::query::RelayQuery;
    /// # use mullvad_types::Intersection;
    ///
    /// # let other_query = RelayQuery::default();
    /// assert_eq!(RelayQuery::default().intersection(other_query.clone()), Some(other_query));
    /// # let other_query = RelayQuery::default();
    /// assert_eq!(other_query.clone().intersection(RelayQuery::default()), Some(other_query));
    /// ```
    fn default() -> Self {
        RelayQuery {
            location: Constraint::Any,
            providers: Constraint::Any,
            ownership: Constraint::Any,
            tunnel_protocol: TunnelType::default(),
            wireguard_constraints: WireguardRelayQuery::new(),
            openvpn_constraints: OpenVpnRelayQuery::new(),
        }
    }
}

impl From<RelayQuery> for RelaySettings {
    fn from(query: RelayQuery) -> Self {
        let (relay_constraints, ..) = query.into_settings();
        RelaySettings::from(relay_constraints)
    }
}

/// A query for a relay with Wireguard-specific properties, such as `multihop` and [wireguard
/// obfuscation][`SelectedObfuscation`].
///
/// This struct may look a lot like [`WireguardConstraints`], and that is the point!
/// This struct is meant to be that type in the "universe of relay queries". The difference
/// between them may seem subtle, but in a [`WireguardRelayQuery`] every field is represented
/// as a [`Constraint`], which allow us to implement [`Intersection`] in a straight forward manner.
/// Notice that [obfuscation][`SelectedObfuscation`] is not a [`Constraint`], but it is trivial
/// to define [`Intersection`] on it, so it is fine.
#[derive(Debug, Clone, Eq, PartialEq, Intersection)]
pub struct WireguardRelayQuery {
    pub port: Constraint<u16>,
    pub ip_version: Constraint<IpVersion>,
    pub allowed_ips: Constraint<AllowedIps>,
    pub use_multihop: Constraint<bool>,
    pub entry_location: Constraint<LocationConstraint>,
    pub entry_providers: Constraint<Providers>,
    pub entry_ownership: Constraint<Ownership>,
    pub obfuscation: ObfuscationQuery,
    pub daita: Constraint<bool>,
    pub daita_use_multihop_if_necessary: Constraint<bool>,
    pub quantum_resistant: QuantumResistantState,
}

#[derive(Default, Debug, Clone, Eq, PartialEq)]
pub enum ObfuscationQuery {
    Off,
    #[default]
    Auto,
    Udp2tcp(Udp2TcpObfuscationSettings),
    Shadowsocks(ShadowsocksSettings),
    Quic,
    Lwo,
}

impl ObfuscationQuery {
    fn into_settings(self) -> ObfuscationSettings {
        match self {
            ObfuscationQuery::Auto => ObfuscationSettings {
                selected_obfuscation: SelectedObfuscation::Auto,
                ..Default::default()
            },
            ObfuscationQuery::Off => ObfuscationSettings {
                selected_obfuscation: SelectedObfuscation::Off,
                ..Default::default()
            },
            ObfuscationQuery::Udp2tcp(settings) => ObfuscationSettings {
                selected_obfuscation: SelectedObfuscation::Udp2Tcp,
                udp2tcp: settings,
                ..Default::default()
            },
            ObfuscationQuery::Shadowsocks(settings) => ObfuscationSettings {
                selected_obfuscation: SelectedObfuscation::Shadowsocks,
                shadowsocks: settings,
                ..Default::default()
            },
            ObfuscationQuery::Quic => ObfuscationSettings {
                selected_obfuscation: SelectedObfuscation::Quic,
                ..Default::default()
            },
            ObfuscationQuery::Lwo => ObfuscationSettings {
                selected_obfuscation: SelectedObfuscation::Lwo,
                ..Default::default()
            },
        }
    }
}

impl From<ObfuscationSettings> for ObfuscationQuery {
    /// A query for obfuscation settings.
    ///
    /// Note that this drops obfuscation protocol specific constraints from [`ObfuscationSettings`]
    /// when the selected obfuscation type is auto.
    fn from(obfuscation: ObfuscationSettings) -> Self {
        match obfuscation.selected_obfuscation {
            SelectedObfuscation::Off => ObfuscationQuery::Off,
            SelectedObfuscation::Auto => ObfuscationQuery::Auto,
            SelectedObfuscation::Udp2Tcp => ObfuscationQuery::Udp2tcp(obfuscation.udp2tcp),
            SelectedObfuscation::Shadowsocks => {
                ObfuscationQuery::Shadowsocks(obfuscation.shadowsocks)
            }
            SelectedObfuscation::Quic => ObfuscationQuery::Quic,
            SelectedObfuscation::Lwo => ObfuscationQuery::Lwo,
        }
    }
}

impl Intersection for ObfuscationQuery {
    fn intersection(self, other: Self) -> Option<Self> {
        match (self, other) {
            (ObfuscationQuery::Off, _) | (_, ObfuscationQuery::Off) => Some(ObfuscationQuery::Off),
            (ObfuscationQuery::Auto, other) | (other, ObfuscationQuery::Auto) => Some(other),
            (ObfuscationQuery::Udp2tcp(a), ObfuscationQuery::Udp2tcp(b)) => {
                Some(ObfuscationQuery::Udp2tcp(a.intersection(b)?))
            }
            (ObfuscationQuery::Shadowsocks(a), ObfuscationQuery::Shadowsocks(b)) => {
                Some(ObfuscationQuery::Shadowsocks(a.intersection(b)?))
            }
            _ => None,
        }
    }
}

impl WireguardRelayQuery {
    pub fn multihop(&self) -> bool {
        matches!(self.use_multihop, Constraint::Only(true))
    }
}

impl WireguardRelayQuery {
    pub const fn new() -> WireguardRelayQuery {
        WireguardRelayQuery {
            port: Constraint::Any,
            ip_version: Constraint::Any,
            allowed_ips: Constraint::Any,
            use_multihop: Constraint::Any,
            entry_location: Constraint::Any,
            entry_providers: Constraint::Any,
            entry_ownership: Constraint::Any,
            obfuscation: ObfuscationQuery::Auto,
            daita: Constraint::Any,
            daita_use_multihop_if_necessary: Constraint::Any,
            quantum_resistant: QuantumResistantState::Auto,
        }
    }

    /// The mapping from [`WireguardRelayQuery`] to [`WireguardConstraints`].
    fn into_constraints(self) -> WireguardConstraints {
        WireguardConstraints {
            port: self.port,
            ip_version: self.ip_version,
            allowed_ips: self.allowed_ips,
            entry_location: self.entry_location,
            entry_providers: self.entry_providers,
            entry_ownership: self.entry_ownership,
            use_multihop: self.use_multihop.unwrap_or(false),
        }
    }
}

impl Default for WireguardRelayQuery {
    fn default() -> Self {
        Self::new()
    }
}

impl From<WireguardRelayQuery> for WireguardConstraints {
    /// The mapping from [`WireguardRelayQuery`] to [`WireguardConstraints`].
    fn from(value: WireguardRelayQuery) -> Self {
        WireguardConstraints {
            port: value.port,
            ip_version: value.ip_version,
            allowed_ips: value.allowed_ips,
            entry_location: value.entry_location,
            entry_providers: value.entry_providers,
            entry_ownership: value.entry_ownership,
            use_multihop: value.use_multihop.unwrap_or(false),
        }
    }
}

/// A query for a relay with OpenVPN-specific properties, such as `bridge_settings`.
///
/// This struct may look a lot like [`OpenVpnConstraints`], and that is the point!
/// This struct is meant to be that type in the "universe of relay queries". The difference
/// between them may seem subtle, but in a [`OpenVpnRelayQuery`] every field is represented
/// as a [`Constraint`], which allow us to implement [`Intersection`] in a straight forward manner.
#[derive(Debug, Clone, Eq, PartialEq, Intersection)]
pub struct OpenVpnRelayQuery {
    pub port: Constraint<TransportPort>,
    pub bridge_settings: BridgeQuery,
}

impl OpenVpnRelayQuery {
    pub const fn new() -> OpenVpnRelayQuery {
        OpenVpnRelayQuery {
            port: Constraint::Any,
            bridge_settings: BridgeQuery::Auto,
        }
    }

    /// The mapping from [`OpenVpnRelayQuery`] to [`OpenVpnConstraints`].
    fn into_constraints(self) -> OpenVpnConstraints {
        OpenVpnConstraints { port: self.port }
    }
}

impl Default for OpenVpnRelayQuery {
    fn default() -> Self {
        Self::new()
    }
}

/// This is the reflection of [`BridgeState`] + [`BridgeSettings`] in the "universe of relay
/// queries".
///
/// [`BridgeState`]: mullvad_types::relay_constraints::BridgeState
/// [`BridgeSettings`]: mullvad_types::relay_constraints::BridgeSettings
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub enum BridgeQuery {
    /// Bridges should not be used.
    Off,
    /// Don't care, let the relay selector choose!
    ///
    /// If this variant is intersected with another [`BridgeQuery`] `bq`,
    /// `bq` is always preferred.
    #[default]
    Auto,
    /// Bridges should be used.
    Normal(BridgeConstraints),
    /// Bridges should be used.
    Custom(Option<CustomProxy>),
}

impl BridgeQuery {
    /// `settings` will be used to decide if bridges should be used. See [`BridgeQuery`]
    /// for more details, but the algorithm beaks down to this:
    ///
    /// * `BridgeQuery::Off`: bridges will not be used
    /// * otherwise: bridges should be used
    pub const fn should_use_bridge(settings: &BridgeQuery) -> bool {
        match settings {
            BridgeQuery::Normal(_) | BridgeQuery::Custom(_) => true,
            BridgeQuery::Off | BridgeQuery::Auto => false,
        }
    }

    fn into_settings(self) -> (BridgeState, BridgeSettings) {
        match self {
            BridgeQuery::Off => (BridgeState::Off, Default::default()),
            BridgeQuery::Auto => (BridgeState::Auto, Default::default()),
            BridgeQuery::Normal(constraints) => (
                BridgeState::On,
                BridgeSettings {
                    bridge_type: BridgeType::Normal,
                    normal: constraints,
                    custom: None,
                },
            ),
            BridgeQuery::Custom(custom) => (
                BridgeState::On,
                BridgeSettings {
                    bridge_type: BridgeType::Normal,
                    normal: Default::default(),
                    custom,
                },
            ),
        }
    }
}

impl Intersection for BridgeQuery {
    fn intersection(self, other: Self) -> Option<Self>
    where
        Self: PartialEq,
        Self: Sized,
    {
        match (self, other) {
            (BridgeQuery::Normal(left), BridgeQuery::Normal(right)) => {
                Some(BridgeQuery::Normal(left.intersection(right)?))
            }
            (BridgeQuery::Auto, right) => Some(right),
            (left, BridgeQuery::Auto) => Some(left),
            (left, right) if left == right => Some(left),
            _ => None,
        }
    }
}

#[allow(unused)]
pub mod builder {
    //! Strongly typed Builder pattern for of relay constraints though the use of the Typestate
    //! pattern.
    use mullvad_types::{
        constraints::Constraint,
        relay_constraints::{
            BridgeConstraints, LocationConstraint, RelayConstraints, SelectedObfuscation,
            ShadowsocksSettings, TransportPort, Udp2TcpObfuscationSettings,
        },
        wireguard::QuantumResistantState,
    };
    use talpid_types::net::TunnelType;

    use super::{BridgeQuery, ObfuscationQuery, RelayQuery};

    // Re-exports
    pub use mullvad_types::relay_constraints::{
        GeographicLocationConstraint, Ownership, Providers,
    };
    pub use talpid_types::net::{IpVersion, TransportProtocol};

    /// Internal builder state for a [`RelayQuery`] parameterized over the
    /// type of VPN tunnel protocol. Some [`RelayQuery`] options are
    /// generic over the VPN protocol, while some options are protocol-specific.
    ///
    /// - The type parameter `VpnProtocol` keeps track of which VPN protocol that is being
    ///   configured. Different instantiations of `VpnProtocol` will expose different functions for
    ///   configuring a [`RelayQueryBuilder`] further.
    pub struct RelayQueryBuilder<VpnProtocol = Any> {
        query: RelayQuery,
        protocol: VpnProtocol,
    }

    ///  The `Any` type is equivalent to the `Constraint::Any` value. If a
    ///  type-parameter is of type `Any`, it means that the corresponding value
    ///  in the final `RelayQuery` is `Constraint::Any`.
    pub struct Any;

    // This impl-block is quantified over all configurations, e.g. [`Any`],
    // [`WireguardRelayQuery`] & [`OpenVpnRelayQuery`]
    impl<VpnProtocol> RelayQueryBuilder<VpnProtocol> {
        /// Configure the [`LocationConstraint`] to use.
        pub fn location(mut self, location: impl Into<LocationConstraint>) -> Self {
            self.query.location = Constraint::Only(location.into());
            self
        }

        /// Configure which [`Ownership`] to use.
        pub const fn ownership(mut self, ownership: Ownership) -> Self {
            self.query.ownership = Constraint::Only(ownership);
            self
        }

        /// Configure which [`Providers`] to use.
        pub fn providers(mut self, providers: Providers) -> Self {
            self.query.providers = Constraint::Only(providers);
            self
        }

        /// Assemble the final [`RelayQuery`] that has been configured
        /// through `self`.
        pub fn build(mut self) -> RelayQuery {
            debug_assert!(self.query.validate().is_ok());
            self.query
        }
    }

    impl RelayQueryBuilder<Any> {
        /// Create a new [`RelayQueryBuilder`] for Wireguard.
        ///
        /// Call [`Self::build`] to convert the builder into a [`RelayQuery`],
        /// which is used to guide the [`RelaySelector`]
        ///
        /// [`RelaySelector`]: crate::RelaySelector
        pub fn wireguard() -> RelayQueryBuilder<Wireguard<Any, Any, Any, Any>> {
            let protocol = Wireguard {
                multihop: Any,
                obfuscation: Any,
                daita: Any,
                quantum_resistant: Any,
            };
            let query = RelayQuery {
                tunnel_protocol: TunnelType::Wireguard,
                ..Default::default()
            };
            // Update the type state
            RelayQueryBuilder { query, protocol }
        }

        /// Create a new [`RelayQueryBuilder`] for OpenVPN.
        ///
        /// Call [`Self::build`] to convert the builder into a [`RelayQuery`],
        /// which is used to guide the [`RelaySelector`]
        ///
        /// [`RelaySelector`]: crate::RelaySelector
        pub fn openvpn() -> RelayQueryBuilder<OpenVPN<Any, Any>> {
            let protocol = OpenVPN {
                transport_port: Any,
                bridge_settings: Any,
            };
            let query = RelayQuery {
                tunnel_protocol: TunnelType::OpenVpn,
                ..Default::default()
            };
            // Update the type state
            RelayQueryBuilder { query, protocol }
        }
    }

    // Type-safe builder for Wireguard relay constraints.

    /// Internal builder state for a [`WireguardRelayQuery`] configuration.
    ///
    /// - The type parameter `Multihop` keeps track of the state of multihop. If multihop has been
    ///   enabled, the builder should expose an option to select entry point.
    ///
    /// [`WireguardRelayQuery`]: super::WireguardRelayQuery
    pub struct Wireguard<Multihop, Obfuscation, Daita, QuantumResistant> {
        multihop: Multihop,
        obfuscation: Obfuscation,
        daita: Daita,
        quantum_resistant: QuantumResistant,
    }

    /// Quic obfuscation.
    ///
    /// Quic does not have any user-configurable parameters, so there is no type defined
    /// in the mullvad-types crate.
    pub struct Quic;

    /// LWO obfuscation.
    ///
    /// LWO does not have any user-configurable parameters, so there is no type defined
    /// in the mullvad-types crate.
    pub struct Lwo;

    // This impl-block is quantified over all configurations
    impl<Multihop, Obfuscation, Daita, QuantumResistant>
        RelayQueryBuilder<Wireguard<Multihop, Obfuscation, Daita, QuantumResistant>>
    {
        /// Specify the port to ues when connecting to the selected
        /// Wireguard relay.
        pub const fn port(mut self, port: u16) -> Self {
            self.query.wireguard_constraints.port = Constraint::Only(port);
            self
        }

        /// Set the [`IpVersion`] to use when connecting to the selected
        /// Wireguard relay.
        pub const fn ip_version(mut self, ip_version: IpVersion) -> Self {
            self.query.wireguard_constraints.ip_version = Constraint::Only(ip_version);
            self
        }
    }

    impl<Multihop, Obfuscation, QuantumResistant>
        RelayQueryBuilder<Wireguard<Multihop, Obfuscation, Any, QuantumResistant>>
    {
        /// Enable DAITA support.
        pub fn daita(
            mut self,
        ) -> RelayQueryBuilder<Wireguard<Multihop, Obfuscation, bool, QuantumResistant>> {
            self.query.wireguard_constraints.daita = Constraint::Only(true);
            // Update the type state
            RelayQueryBuilder {
                query: self.query,
                protocol: Wireguard {
                    multihop: self.protocol.multihop,
                    obfuscation: self.protocol.obfuscation,
                    daita: true,
                    quantum_resistant: self.protocol.quantum_resistant,
                },
            }
        }
    }

    // impl-block for after DAITA is set
    impl<Multihop, Obfuscation, QuantumResistant>
        RelayQueryBuilder<Wireguard<Multihop, Obfuscation, bool, QuantumResistant>>
    {
        /// Enable DAITA 'use_multihop_if_necessary'.
        pub fn daita_use_multihop_if_necessary(
            mut self,
            constraint: impl Into<Constraint<bool>>,
        ) -> Self {
            self.query
                .wireguard_constraints
                .daita_use_multihop_if_necessary = constraint.into();
            self
        }
    }

    impl<Multihop, Obfuscation, Daita> RelayQueryBuilder<Wireguard<Multihop, Obfuscation, Daita, Any>> {
        /// Enable PQ support.
        pub fn quantum_resistant(
            mut self,
        ) -> RelayQueryBuilder<Wireguard<Multihop, Obfuscation, Daita, bool>> {
            self.query.wireguard_constraints.quantum_resistant = QuantumResistantState::On;
            // Update the type state
            RelayQueryBuilder {
                query: self.query,
                protocol: Wireguard {
                    multihop: self.protocol.multihop,
                    obfuscation: self.protocol.obfuscation,
                    daita: self.protocol.daita,
                    quantum_resistant: true,
                },
            }
        }
    }

    impl<Obfuscation, Daita, QuantumResistant>
        RelayQueryBuilder<Wireguard<Any, Obfuscation, Daita, QuantumResistant>>
    {
        /// Enable multihop.
        ///
        /// To configure the entry relay, see [`RelayQueryBuilder::entry`].
        pub fn multihop(
            mut self,
        ) -> RelayQueryBuilder<Wireguard<bool, Obfuscation, Daita, QuantumResistant>> {
            self.query.wireguard_constraints.use_multihop = Constraint::Only(true);
            // Update the type state
            RelayQueryBuilder {
                query: self.query,
                protocol: Wireguard {
                    multihop: true,
                    obfuscation: self.protocol.obfuscation,
                    daita: self.protocol.daita,
                    quantum_resistant: self.protocol.quantum_resistant,
                },
            }
        }
    }

    impl<Obfuscation, Daita, QuantumResistant>
        RelayQueryBuilder<Wireguard<bool, Obfuscation, Daita, QuantumResistant>>
    {
        /// Set the entry location in a multihop configuration. This requires
        /// multihop to be enabled.
        pub fn entry(mut self, location: impl Into<LocationConstraint>) -> Self {
            self.query.wireguard_constraints.entry_location = Constraint::Only(location.into());
            self
        }

        /// Set the entry location in a multihop configuration. This requires
        /// multihop to be enabled.
        pub fn entry_providers(mut self, providers: Providers) -> Self {
            self.query.wireguard_constraints.entry_providers = Constraint::Only(providers);
            self
        }

        /// Set the entry location in a multihop configuration. This requires
        /// multihop to be enabled.
        pub fn entry_ownership(mut self, ownership: Ownership) -> Self {
            self.query.wireguard_constraints.entry_ownership = Constraint::Only(ownership);
            self
        }
    }

    impl<Multihop, Daita, QuantumResistant>
        RelayQueryBuilder<Wireguard<Multihop, Any, Daita, QuantumResistant>>
    {
        /// Enable `UDP2TCP` obufscation. This will in turn enable the option to configure the
        /// `UDP2TCP` port.
        pub fn udp2tcp(
            mut self,
        ) -> RelayQueryBuilder<
            Wireguard<Multihop, Udp2TcpObfuscationSettings, Daita, QuantumResistant>,
        > {
            let obfuscation = Udp2TcpObfuscationSettings {
                port: Constraint::Any,
            };
            let protocol = Wireguard {
                multihop: self.protocol.multihop,
                obfuscation: obfuscation.clone(),
                daita: self.protocol.daita,
                quantum_resistant: self.protocol.quantum_resistant,
            };
            self.query.wireguard_constraints.obfuscation = ObfuscationQuery::Udp2tcp(obfuscation);
            RelayQueryBuilder {
                query: self.query,
                protocol,
            }
        }

        /// Enable Shadowsocks obufscation. This will in turn enable the option to configure the
        /// port.
        pub fn shadowsocks(
            mut self,
        ) -> RelayQueryBuilder<Wireguard<Multihop, ShadowsocksSettings, Daita, QuantumResistant>>
        {
            let obfuscation = ShadowsocksSettings {
                port: Constraint::Any,
            };
            let protocol = Wireguard {
                multihop: self.protocol.multihop,
                obfuscation: obfuscation.clone(),
                daita: self.protocol.daita,
                quantum_resistant: self.protocol.quantum_resistant,
            };
            self.query.wireguard_constraints.obfuscation =
                ObfuscationQuery::Shadowsocks(obfuscation);
            RelayQueryBuilder {
                query: self.query,
                protocol,
            }
        }

        /// Enable QUIC obfuscation.
        pub fn quic(
            mut self,
        ) -> RelayQueryBuilder<Wireguard<Multihop, Quic, Daita, QuantumResistant>> {
            self.query.wireguard_constraints.obfuscation = ObfuscationQuery::Quic;
            RelayQueryBuilder {
                query: self.query,
                protocol: Wireguard {
                    multihop: self.protocol.multihop,
                    obfuscation: Quic,
                    daita: self.protocol.daita,
                    quantum_resistant: self.protocol.quantum_resistant,
                },
            }
        }

        /// Enable LWO obfuscation.
        pub fn lwo(
            mut self,
        ) -> RelayQueryBuilder<Wireguard<Multihop, Lwo, Daita, QuantumResistant>> {
            self.query.wireguard_constraints.obfuscation = ObfuscationQuery::Lwo;
            RelayQueryBuilder {
                query: self.query,
                protocol: Wireguard {
                    multihop: self.protocol.multihop,
                    obfuscation: Lwo,
                    daita: self.protocol.daita,
                    quantum_resistant: self.protocol.quantum_resistant,
                },
            }
        }
    }

    impl<Multihop, Daita, QuantumResistant>
        RelayQueryBuilder<Wireguard<Multihop, Udp2TcpObfuscationSettings, Daita, QuantumResistant>>
    {
        /// Set the `UDP2TCP` port. This is the TCP port which the `UDP2TCP` obfuscation
        /// protocol should use to connect to a relay.
        pub fn udp2tcp_port(mut self, port: u16) -> Self {
            self.protocol.obfuscation.port = Constraint::Only(port);
            self.query.wireguard_constraints.obfuscation =
                ObfuscationQuery::Udp2tcp(self.protocol.obfuscation.clone());
            self
        }
    }

    // Type-safe builder pattern for OpenVPN relay constraints.

    /// Internal builder state for a [`OpenVpnRelayQuery`] configuration.
    ///
    /// - The type parameter `TransportPort` keeps track of which [`TransportProtocol`] & port-combo
    ///   to use. [`TransportProtocol`] has to be set first before the option to select a specific
    ///   port is exposed.
    ///
    /// [`OpenVpnRelayQuery`]: super::OpenVpnRelayQuery
    pub struct OpenVPN<TransportPort, Bridge> {
        transport_port: TransportPort,
        bridge_settings: Bridge,
    }

    // This impl-block is quantified over all configurations
    impl<Transport, Bridge> RelayQueryBuilder<OpenVPN<Transport, Bridge>> {
        /// Configure what [`TransportProtocol`] to use. Calling this
        /// function on a builder will expose the option to select which
        /// port to use in combination with `protocol`.
        pub fn transport_protocol(
            mut self,
            protocol: TransportProtocol,
        ) -> RelayQueryBuilder<OpenVPN<TransportProtocol, Bridge>> {
            let transport_port = TransportPort {
                protocol,
                port: Constraint::Any,
            };
            self.query.openvpn_constraints.port = Constraint::Only(transport_port);
            // Update the type state
            RelayQueryBuilder {
                query: self.query,
                protocol: OpenVPN {
                    transport_port: protocol,
                    bridge_settings: self.protocol.bridge_settings,
                },
            }
        }
    }

    impl<Bridge> RelayQueryBuilder<OpenVPN<TransportProtocol, Bridge>> {
        /// Configure what port to use when connecting to a relay.
        pub fn port(mut self, port: u16) -> RelayQueryBuilder<OpenVPN<TransportPort, Bridge>> {
            let port = Constraint::Only(port);
            let transport_port = TransportPort {
                protocol: self.protocol.transport_port,
                port,
            };
            self.query.openvpn_constraints.port = Constraint::Only(transport_port);
            // Update the type state
            RelayQueryBuilder {
                query: self.query,
                protocol: OpenVPN {
                    transport_port,
                    bridge_settings: self.protocol.bridge_settings,
                },
            }
        }
    }

    impl<Transport> RelayQueryBuilder<OpenVPN<Transport, Any>> {
        /// Enable Bridges. This also sets the transport protocol to TCP and resets any
        /// previous port settings.
        pub fn bridge(
            mut self,
        ) -> RelayQueryBuilder<OpenVPN<TransportProtocol, BridgeConstraints>> {
            let bridge_settings = BridgeConstraints {
                location: Constraint::Any,
                providers: Constraint::Any,
                ownership: Constraint::Any,
            };

            let protocol = OpenVPN {
                transport_port: self.protocol.transport_port,
                bridge_settings: bridge_settings.clone(),
            };

            self.query.openvpn_constraints.bridge_settings = BridgeQuery::Normal(bridge_settings);

            let builder = RelayQueryBuilder {
                query: self.query,
                protocol,
            };

            builder.transport_protocol(TransportProtocol::Tcp)
        }
    }

    impl<Transport> RelayQueryBuilder<OpenVPN<Transport, BridgeConstraints>> {
        /// Constraint the geographical location of the selected bridge.
        pub fn bridge_location(mut self, location: impl Into<LocationConstraint>) -> Self {
            self.protocol.bridge_settings.location = Constraint::Only(location.into());
            self.query.openvpn_constraints.bridge_settings =
                BridgeQuery::Normal(self.protocol.bridge_settings.clone());
            self
        }
        /// Constrain the [`Providers`] of the selected bridge.
        pub fn bridge_providers(mut self, providers: Providers) -> Self {
            self.protocol.bridge_settings.providers = Constraint::Only(providers);
            self.query.openvpn_constraints.bridge_settings =
                BridgeQuery::Normal(self.protocol.bridge_settings.clone());
            self
        }
        /// Constrain the [`Ownership`] of the selected bridge.
        pub fn bridge_ownership(mut self, ownership: Ownership) -> Self {
            self.protocol.bridge_settings.ownership = Constraint::Only(ownership);
            self
        }
    }
}

/// This trait defines a bunch of helper methods on [`RelayQuery`].
pub trait RelayQueryExt {
    /// Are we using daita?
    fn using_daita(&self) -> bool;
    /// is `use_multihop_if_necessary` enabled? In other words, is `Direct only` disabled?
    fn use_multihop_if_necessary(&self) -> bool;
    /// Are we using singlehop? I.e. is multihop *not* explicitly enabled?
    fn singlehop(&self) -> bool;
}

impl RelayQueryExt for RelayQuery {
    fn using_daita(&self) -> bool {
        self.wireguard_constraints()
            .daita
            .is_only_and(|enabled| enabled)
    }
    fn use_multihop_if_necessary(&self) -> bool {
        self.wireguard_constraints()
            .daita_use_multihop_if_necessary
            // The default value is `Any`, which means that we need to check the intersection.
            .intersection(Constraint::Only(true))
            .is_some()
    }
    fn singlehop(&self) -> bool {
        !self.wireguard_constraints().multihop()
    }
}

#[cfg(test)]
mod test {
    use mullvad_types::{
        constraints::Constraint,
        relay_constraints::{
            ObfuscationSettings, SelectedObfuscation, ShadowsocksSettings,
            Udp2TcpObfuscationSettings,
        },
    };
    use proptest::prelude::*;
    use talpid_types::net::TunnelType;

    use super::{Intersection, ObfuscationQuery, RelayQuery, builder::RelayQueryBuilder};

    // Define proptest combinators for the `Constraint` type.

    pub fn constraint<T>(
        base_strategy: impl Strategy<Value = T> + 'static,
    ) -> impl Strategy<Value = Constraint<T>>
    where
        T: core::fmt::Debug + std::clone::Clone + 'static,
    {
        prop_oneof![any(), only(base_strategy),]
    }

    pub fn only<T>(
        base_strategy: impl Strategy<Value = T> + 'static,
    ) -> impl Strategy<Value = Constraint<T>>
    where
        T: core::fmt::Debug + std::clone::Clone + 'static,
    {
        base_strategy.prop_map(Constraint::Only)
    }

    pub fn any<T>() -> impl Strategy<Value = Constraint<T>>
    where
        T: core::fmt::Debug + std::clone::Clone + 'static,
    {
        Just(Constraint::Any)
    }

    proptest! {
        #[test]
        fn identity(x in only(proptest::arbitrary::any::<bool>())) {
            // Identity laws
            //  x ∩ identity = x
            //  identity ∩ x = x

            // The identity element
            let identity = Constraint::Any;
            prop_assert_eq!(x.intersection(identity), x.into());
            prop_assert_eq!(identity.intersection(x), x.into());
        }

        #[test]
        fn idempotency (x in constraint(proptest::arbitrary::any::<bool>())) {
            // Idempotency law
            //  x ∩ x = x
            prop_assert_eq!(x.intersection(x), x.into()) // lift x to the return type of `intersection`
        }

        #[test]
        fn commutativity(x in constraint(proptest::arbitrary::any::<bool>()),
                         y in constraint(proptest::arbitrary::any::<bool>())) {
            // Commutativity law
            //  x ∩ y = y ∩ x
            prop_assert_eq!(x.intersection(y), y.intersection(x))
        }

        #[test]
        fn associativity(x in constraint(proptest::arbitrary::any::<bool>()),
                         y in constraint(proptest::arbitrary::any::<bool>()),
                         z in constraint(proptest::arbitrary::any::<bool>()))
        {
            // Associativity law
            //  (x ∩ y) ∩ z = x ∩ (y ∩ z)
            let left: Option<_> = {
                x.intersection(y).and_then(|xy| xy.intersection(z))
            };
            let right: Option<_> = {
                // It is fine to rewrite the order of the application from
                //  x ∩ (y ∩ z)
                // to
                //  (y ∩ z) ∩ x
                // due to the commutative property of intersection
                (y.intersection(z)).and_then(|yz| yz.intersection(x))
            };
            prop_assert_eq!(left, right);
        }

        /// When obfuscation is set to automatic in [`ObfuscationSettings`], the query should not
        /// contain any specific obfuscation protocol settings.
        #[test]
        fn test_auto_obfuscation_settings(port1 in constraint(proptest::arbitrary::any::<u16>()), port2 in constraint(proptest::arbitrary::any::<u16>())) {
            let query = ObfuscationQuery::from(ObfuscationSettings {
                selected_obfuscation: SelectedObfuscation::Auto,
                udp2tcp: Udp2TcpObfuscationSettings {
                    port: port1,
                },
                shadowsocks: ShadowsocksSettings {
                    port: port2,
                },
            });
            assert_eq!(query, ObfuscationQuery::Auto);
        }
    }

    /// Test whether the default relay query is valid
    #[test]
    fn test_relay_query_default_valid() {
        RelayQuery::default().validate().unwrap();
    }

    /// OpenVPN queries with DAITA enabled are invalid
    /// DAITA is a core privacy feature.
    #[test]
    fn test_relay_query_daita_openvpn() {
        let mut query = RelayQueryBuilder::wireguard().daita().build();
        query
            .set_tunnel_protocol(TunnelType::OpenVpn)
            .expect_err("expected query to be invalid for OpenVPN");
    }

    /// OpenVPN queries with multihop enabled are invalid
    /// Multihop is a core privacy feature.
    #[test]
    fn test_relay_query_multihop_openvpn() {
        let mut query = RelayQueryBuilder::wireguard().multihop().build();
        query
            .set_tunnel_protocol(TunnelType::OpenVpn)
            .expect_err("expected query to be invalid for OpenVPN");
    }

    /// OpenVPN queries with PQ enabled are invalid
    /// PQ is a core privacy feature.
    #[test]
    fn test_relay_query_quantum_resistant_openvpn() {
        let mut query = RelayQueryBuilder::wireguard().quantum_resistant().build();
        query
            .set_tunnel_protocol(TunnelType::OpenVpn)
            .expect_err("expected query to be invalid for OpenVPN");
    }
}