56
56
self . relay_selector
57
57
. set_config ( new_selector_config ( & self . settings ) ) ;
58
58
59
- if self . change_should_cause_reconnect ( id ) {
59
+ if self . change_should_cause_reconnect ( Some ( id ) ) {
60
60
log:: info!( "Initiating tunnel restart because a selected custom list was deleted" ) ;
61
61
self . reconnect_tunnel ( ) ;
62
62
}
@@ -100,7 +100,7 @@ where
100
100
self . relay_selector
101
101
. set_config ( new_selector_config ( & self . settings ) ) ;
102
102
103
- if self . change_should_cause_reconnect ( id ) {
103
+ if self . change_should_cause_reconnect ( Some ( id ) ) {
104
104
log:: info!( "Initiating tunnel restart because a selected custom list changed" ) ;
105
105
self . reconnect_tunnel ( ) ;
106
106
}
@@ -110,48 +110,77 @@ where
110
110
Ok ( ( ) )
111
111
}
112
112
113
- fn change_should_cause_reconnect ( & self , custom_list_id : Id ) -> bool {
113
+ pub async fn clear_custom_lists ( & mut self ) -> Result < ( ) , Error > {
114
+ let settings_changed = self
115
+ . settings
116
+ . update ( |settings| {
117
+ settings. custom_lists . clear ( ) ;
118
+ } )
119
+ . await
120
+ . map_err ( Error :: SettingsError ) ;
121
+
122
+ if let Ok ( true ) = settings_changed {
123
+ self . relay_selector
124
+ . set_config ( new_selector_config ( & self . settings ) ) ;
125
+
126
+ if self . change_should_cause_reconnect ( None ) {
127
+ log:: info!( "Initiating tunnel restart because a selected custom list was deleted" ) ;
128
+ self . reconnect_tunnel ( ) ;
129
+ }
130
+ }
131
+
132
+ settings_changed?;
133
+ Ok ( ( ) )
134
+ }
135
+
136
+ /// Check whether we need to reconnect after changing custom lists.
137
+ ///
138
+ /// If `custom_list_id` is `Some`, only changes to that custom list will trigger a reconnect.
139
+ fn change_should_cause_reconnect ( & self , custom_list_id : Option < Id > ) -> bool {
114
140
use mullvad_types:: states:: TunnelState ;
115
141
let mut need_to_reconnect = false ;
116
142
117
- if let RelaySettings :: Normal ( relay_settings) = & self . settings . relay_settings {
118
- if let Constraint :: Only ( LocationConstraint :: CustomList { list_id } ) =
119
- & relay_settings. location
120
- {
121
- need_to_reconnect |= list_id == & custom_list_id;
122
- }
143
+ let RelaySettings :: Normal ( relay_settings) = & self . settings . relay_settings else {
144
+ return false ;
145
+ } ;
123
146
124
- if let TunnelState :: Connecting {
125
- endpoint,
126
- location : _,
127
- }
128
- | TunnelState :: Connected {
129
- endpoint,
130
- location : _,
131
- } = & self . tunnel_state
132
- {
133
- match endpoint. tunnel_type {
134
- TunnelType :: Wireguard => {
135
- if relay_settings. wireguard_constraints . multihop ( ) {
136
- if let Constraint :: Only ( LocationConstraint :: CustomList { list_id } ) =
137
- & relay_settings. wireguard_constraints . entry_location
138
- {
139
- need_to_reconnect |= list_id == & custom_list_id;
140
- }
147
+ if let Constraint :: Only ( LocationConstraint :: CustomList { list_id } ) =
148
+ & relay_settings. location
149
+ {
150
+ need_to_reconnect |= custom_list_id. map ( |id| & id == list_id) . unwrap_or ( true ) ;
151
+ }
152
+
153
+ if let TunnelState :: Connecting {
154
+ endpoint,
155
+ location : _,
156
+ }
157
+ | TunnelState :: Connected {
158
+ endpoint,
159
+ location : _,
160
+ } = & self . tunnel_state
161
+ {
162
+ match endpoint. tunnel_type {
163
+ TunnelType :: Wireguard => {
164
+ if relay_settings. wireguard_constraints . multihop ( ) {
165
+ if let Constraint :: Only ( LocationConstraint :: CustomList { list_id } ) =
166
+ & relay_settings. wireguard_constraints . entry_location
167
+ {
168
+ need_to_reconnect |=
169
+ custom_list_id. map ( |id| & id == list_id) . unwrap_or ( true ) ;
141
170
}
142
171
}
172
+ }
143
173
144
- TunnelType :: OpenVpn => {
145
- if !matches ! ( self . settings. bridge_state, BridgeState :: Off ) {
146
- if let Ok ( ResolvedBridgeSettings :: Normal ( bridge_settings) ) =
147
- self . settings . bridge_settings . resolve ( )
174
+ TunnelType :: OpenVpn => {
175
+ if !matches ! ( self . settings. bridge_state, BridgeState :: Off ) {
176
+ if let Ok ( ResolvedBridgeSettings :: Normal ( bridge_settings) ) =
177
+ self . settings . bridge_settings . resolve ( )
178
+ {
179
+ if let Constraint :: Only ( LocationConstraint :: CustomList { list_id } ) =
180
+ & bridge_settings. location
148
181
{
149
- if let Constraint :: Only ( LocationConstraint :: CustomList {
150
- list_id,
151
- } ) = & bridge_settings. location
152
- {
153
- need_to_reconnect |= list_id == & custom_list_id;
154
- }
182
+ need_to_reconnect |=
183
+ custom_list_id. map ( |id| & id == list_id) . unwrap_or ( true ) ;
155
184
}
156
185
}
157
186
}
0 commit comments