@@ -32,8 +32,8 @@ use crate::AdditionalWireguardConstraints;
32
32
use mullvad_types:: {
33
33
constraints:: Constraint ,
34
34
relay_constraints:: {
35
- BridgeConstraints , LocationConstraint , OpenVpnConstraints , Ownership , Providers ,
36
- RelayConstraints , RelaySettings , SelectedObfuscation , TransportPort ,
35
+ BridgeConstraints , LocationConstraint , ObfuscationSettings , OpenVpnConstraints , Ownership ,
36
+ Providers , RelayConstraints , RelaySettings , SelectedObfuscation , TransportPort ,
37
37
Udp2TcpObfuscationSettings , WireguardConstraints ,
38
38
} ,
39
39
Intersection ,
@@ -150,11 +150,50 @@ pub struct WireguardRelayQuery {
150
150
pub ip_version : Constraint < IpVersion > ,
151
151
pub use_multihop : Constraint < bool > ,
152
152
pub entry_location : Constraint < LocationConstraint > ,
153
- pub obfuscation : SelectedObfuscation ,
154
- pub udp2tcp_port : Constraint < Udp2TcpObfuscationSettings > ,
153
+ pub obfuscation : ObfuscationQuery ,
155
154
pub daita : Constraint < bool > ,
156
155
}
157
156
157
+ #[ derive( Default , Debug , Clone , Eq , PartialEq ) ]
158
+ pub enum ObfuscationQuery {
159
+ Off ,
160
+ #[ default]
161
+ Auto ,
162
+ Udp2tcp {
163
+ port : Udp2TcpObfuscationSettings ,
164
+ } ,
165
+ }
166
+
167
+ impl From < ObfuscationSettings > for ObfuscationQuery {
168
+ /// A query for obfuscation settings.
169
+ ///
170
+ /// Note that this drops obfuscation protocol specific constraints from [`ObfuscationSettings`]
171
+ /// when the selected obfuscation type is auto.
172
+ fn from ( obfuscation : ObfuscationSettings ) -> Self {
173
+ match obfuscation. selected_obfuscation {
174
+ SelectedObfuscation :: Off => ObfuscationQuery :: Off ,
175
+ SelectedObfuscation :: Auto => ObfuscationQuery :: Auto ,
176
+ SelectedObfuscation :: Udp2Tcp => ObfuscationQuery :: Udp2tcp {
177
+ port : obfuscation. udp2tcp ,
178
+ } ,
179
+ }
180
+ }
181
+ }
182
+
183
+ impl Intersection for ObfuscationQuery {
184
+ fn intersection ( self , other : Self ) -> Option < Self > {
185
+ match ( self , other) {
186
+ ( ObfuscationQuery :: Off , _) | ( _, ObfuscationQuery :: Off ) => Some ( ObfuscationQuery :: Off ) ,
187
+ ( ObfuscationQuery :: Auto , other) | ( other, ObfuscationQuery :: Auto ) => Some ( other) ,
188
+ ( ObfuscationQuery :: Udp2tcp { port : a } , ObfuscationQuery :: Udp2tcp { port : b } ) => {
189
+ Some ( ObfuscationQuery :: Udp2tcp {
190
+ port : a. intersection ( b) ?,
191
+ } )
192
+ }
193
+ }
194
+ }
195
+ }
196
+
158
197
impl WireguardRelayQuery {
159
198
pub fn multihop ( & self ) -> bool {
160
199
matches ! ( self . use_multihop, Constraint :: Only ( true ) )
@@ -168,8 +207,7 @@ impl WireguardRelayQuery {
168
207
ip_version : Constraint :: Any ,
169
208
use_multihop : Constraint :: Any ,
170
209
entry_location : Constraint :: Any ,
171
- obfuscation : SelectedObfuscation :: Auto ,
172
- udp2tcp_port : Constraint :: Any ,
210
+ obfuscation : ObfuscationQuery :: Auto ,
173
211
daita : Constraint :: Any ,
174
212
}
175
213
}
@@ -310,7 +348,7 @@ pub mod builder {
310
348
} ;
311
349
use talpid_types:: net:: TunnelType ;
312
350
313
- use super :: { BridgeQuery , RelayQuery } ;
351
+ use super :: { BridgeQuery , ObfuscationQuery , RelayQuery } ;
314
352
315
353
// Re-exports
316
354
pub use mullvad_types:: relay_constraints:: {
@@ -505,8 +543,8 @@ pub mod builder {
505
543
obfuscation : obfuscation. clone ( ) ,
506
544
daita : self . protocol . daita ,
507
545
} ;
508
- self . query . wireguard_constraints . udp2tcp_port = Constraint :: Only ( obfuscation ) ;
509
- self . query . wireguard_constraints . obfuscation = SelectedObfuscation :: Udp2Tcp ;
546
+ self . query . wireguard_constraints . obfuscation =
547
+ ObfuscationQuery :: Udp2tcp { port : obfuscation } ;
510
548
RelayQueryBuilder {
511
549
query : self . query ,
512
550
protocol,
@@ -519,8 +557,9 @@ pub mod builder {
519
557
/// protocol should use to connect to a relay.
520
558
pub fn udp2tcp_port ( mut self , port : u16 ) -> Self {
521
559
self . protocol . obfuscation . port = Constraint :: Only ( port) ;
522
- self . query . wireguard_constraints . udp2tcp_port =
523
- Constraint :: Only ( self . protocol . obfuscation . clone ( ) ) ;
560
+ self . query . wireguard_constraints . obfuscation = ObfuscationQuery :: Udp2tcp {
561
+ port : self . protocol . obfuscation . clone ( ) ,
562
+ } ;
524
563
self
525
564
}
526
565
}
@@ -639,10 +678,13 @@ pub mod builder {
639
678
640
679
#[ cfg( test) ]
641
680
mod test {
642
- use mullvad_types:: constraints:: Constraint ;
681
+ use mullvad_types:: {
682
+ constraints:: Constraint ,
683
+ relay_constraints:: { ObfuscationSettings , SelectedObfuscation , Udp2TcpObfuscationSettings } ,
684
+ } ;
643
685
use proptest:: prelude:: * ;
644
686
645
- use super :: Intersection ;
687
+ use super :: { Intersection , ObfuscationQuery } ;
646
688
647
689
// Define proptest combinators for the `Constraint` type.
648
690
@@ -719,5 +761,18 @@ mod test {
719
761
} ;
720
762
prop_assert_eq!( left, right) ;
721
763
}
764
+
765
+ /// When obfuscation is set to automatic in [`ObfuscationSettings`], the query should not
766
+ /// contain any specific obfuscation protocol settings.
767
+ #[ test]
768
+ fn test_auto_obfuscation_settings( port in constraint( proptest:: arbitrary:: any:: <u16 >( ) ) ) {
769
+ let query = ObfuscationQuery :: from( ObfuscationSettings {
770
+ selected_obfuscation: SelectedObfuscation :: Auto ,
771
+ udp2tcp: Udp2TcpObfuscationSettings {
772
+ port,
773
+ } ,
774
+ } ) ;
775
+ assert_eq!( query, ObfuscationQuery :: Auto ) ;
776
+ }
722
777
}
723
778
}
0 commit comments