@@ -24,6 +24,8 @@ const SHADOWSOCKS_EXTRA_PORT_RANGES: &[(u16, u16)] = &[(1, u16::MAX)];
24
24
pub enum Error {
25
25
#[ error( "Port selection algorithm is broken" ) ]
26
26
PortSelectionAlgorithm ,
27
+ #[ error( "Found no valid port matching the selected settings" ) ]
28
+ NoMatchingPort ,
27
29
}
28
30
29
31
/// Picks a relay using [pick_random_relay_weighted], using the `weight` member of each relay
@@ -77,37 +79,38 @@ pub fn get_udp2tcp_obfuscator(
77
79
udp2tcp_ports : & [ u16 ] ,
78
80
relay : Relay ,
79
81
endpoint : & MullvadWireguardEndpoint ,
80
- ) -> Option < SelectedObfuscator > {
82
+ ) -> Result < SelectedObfuscator , Error > {
81
83
let udp2tcp_endpoint_port =
82
84
get_udp2tcp_obfuscator_port ( obfuscation_settings_constraint, udp2tcp_ports) ?;
83
85
let config = ObfuscatorConfig :: Udp2Tcp {
84
86
endpoint : SocketAddr :: new ( endpoint. peer . endpoint . ip ( ) , udp2tcp_endpoint_port) ,
85
87
} ;
86
88
87
- Some ( SelectedObfuscator { config, relay } )
89
+ Ok ( SelectedObfuscator { config, relay } )
88
90
}
89
91
90
- pub fn get_udp2tcp_obfuscator_port (
92
+ fn get_udp2tcp_obfuscator_port (
91
93
obfuscation_settings : & Udp2TcpObfuscationSettings ,
92
94
udp2tcp_ports : & [ u16 ] ,
93
- ) -> Option < u16 > {
94
- if let Constraint :: Only ( desired_port) = obfuscation_settings. port {
95
+ ) -> Result < u16 , Error > {
96
+ let port = if let Constraint :: Only ( desired_port) = obfuscation_settings. port {
95
97
udp2tcp_ports
96
98
. iter ( )
97
99
. find ( |& candidate| desired_port == * candidate)
98
100
. copied ( )
99
101
} else {
100
102
// There are no specific obfuscation settings to take into consideration in this case.
101
103
udp2tcp_ports. choose ( & mut thread_rng ( ) ) . copied ( )
102
- }
104
+ } ;
105
+ port. ok_or ( Error :: NoMatchingPort )
103
106
}
104
107
105
108
pub fn get_shadowsocks_obfuscator (
106
109
settings : & ShadowsocksSettings ,
107
110
non_extra_port_ranges : & [ ( u16 , u16 ) ] ,
108
111
relay : Relay ,
109
112
endpoint : & MullvadWireguardEndpoint ,
110
- ) -> Option < SelectedObfuscator > {
113
+ ) -> Result < SelectedObfuscator , Error > {
111
114
let port = settings. port ;
112
115
let extra_addrs = match & relay. endpoint_data {
113
116
mullvad_types:: relay_list:: RelayEndpointData :: Wireguard ( wg) => {
@@ -123,7 +126,7 @@ pub fn get_shadowsocks_obfuscator(
123
126
port,
124
127
) ?;
125
128
126
- Some ( SelectedObfuscator {
129
+ Ok ( SelectedObfuscator {
127
130
config : ObfuscatorConfig :: Shadowsocks { endpoint } ,
128
131
relay,
129
132
} )
@@ -137,7 +140,7 @@ fn get_shadowsocks_obfuscator_inner(
137
140
wg_in_addr_port_ranges : & [ ( u16 , u16 ) ] ,
138
141
extra_in_addrs : & [ IpAddr ] ,
139
142
desired_port : Constraint < u16 > ,
140
- ) -> Option < SocketAddr > {
143
+ ) -> Result < SocketAddr , Error > {
141
144
// Filter out addresses for the wrong address family
142
145
let extra_in_addrs: Vec < _ > = extra_in_addrs
143
146
. iter ( )
@@ -164,9 +167,10 @@ fn get_shadowsocks_obfuscator_inner(
164
167
Constraint :: Only ( _port) => None ,
165
168
// Selected no specific port
166
169
Constraint :: Any => super :: helpers:: select_random_port ( port_ranges) . ok ( ) ,
167
- } ?;
170
+ }
171
+ . ok_or ( Error :: NoMatchingPort ) ?;
168
172
169
- Some ( SocketAddr :: from ( ( in_ip, selected_port) ) )
173
+ Ok ( SocketAddr :: from ( ( in_ip, selected_port) ) )
170
174
}
171
175
172
176
/// Selects a random port number from a list of provided port ranges.
@@ -255,7 +259,7 @@ mod tests {
255
259
Constraint :: Only ( OUT_OF_RANGE_PORT ) ,
256
260
) ;
257
261
assert ! (
258
- selected_addr. is_none ( ) ,
262
+ selected_addr. is_err ( ) ,
259
263
"expected no relay for port outside range, found {selected_addr:?}"
260
264
) ;
261
265
}
@@ -336,7 +340,7 @@ mod tests {
336
340
Constraint :: Only ( OUT_OF_RANGE_PORT ) ,
337
341
) ;
338
342
assert ! (
339
- selected_addr. is_none ( ) ,
343
+ selected_addr. is_err ( ) ,
340
344
"expected no match for out-of-range port"
341
345
) ;
342
346
@@ -347,7 +351,7 @@ mod tests {
347
351
Constraint :: Only ( IN_RANGE_PORT ) ,
348
352
) ;
349
353
assert ! (
350
- selected_addr. is_some ( ) ,
354
+ selected_addr. is_ok ( ) ,
351
355
"expected match for within-range port"
352
356
) ;
353
357
}
0 commit comments