-
Notifications
You must be signed in to change notification settings - Fork 389
/
Copy pathcustom_list.rs
165 lines (146 loc) · 5.3 KB
/
custom_list.rs
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
use crate::{new_selector_config, Daemon, Error, EventListener};
use mullvad_types::{
custom_list::{CustomList, Id},
relay_constraints::{
BridgeState, Constraint, LocationConstraint, RelaySettings, ResolvedBridgeSettings,
},
};
use talpid_types::net::TunnelType;
impl<L> Daemon<L>
where
L: EventListener + Clone + Send + 'static,
{
pub async fn create_custom_list(&mut self, name: String) -> Result<Id, crate::Error> {
if self
.settings
.custom_lists
.iter()
.any(|list| list.name == name)
{
return Err(Error::CustomListExists);
}
let new_list = CustomList::new(name);
let id = new_list.id;
self.settings
.update(|settings| {
settings.custom_lists.add(new_list);
})
.await
.map_err(Error::SettingsError)?;
Ok(id)
}
pub async fn delete_custom_list(&mut self, id: Id) -> Result<(), Error> {
let Some(list_index) = self
.settings
.custom_lists
.iter()
.position(|elem| elem.id == id)
else {
return Err(Error::CustomListNotFound);
};
let settings_changed = self
.settings
.update(|settings| {
// NOTE: Not using swap remove because it would make user output slightly
// more confusing and the cost is so small.
settings.custom_lists.remove(list_index);
})
.await
.map_err(Error::SettingsError);
if let Ok(true) = settings_changed {
self.relay_selector
.set_config(new_selector_config(&self.settings));
if self.change_should_cause_reconnect(id) {
log::info!("Initiating tunnel restart because a selected custom list was deleted");
self.reconnect_tunnel();
}
}
settings_changed?;
Ok(())
}
pub async fn update_custom_list(&mut self, new_list: CustomList) -> Result<(), Error> {
let Some((list_index, old_list)) = self
.settings
.custom_lists
.iter()
.enumerate()
.find(|elem| elem.1.id == new_list.id)
else {
return Err(Error::CustomListNotFound);
};
let id = old_list.id;
if old_list.name != new_list.name
&& self
.settings
.custom_lists
.iter()
.any(|list| list.name == new_list.name)
{
return Err(Error::CustomListExists);
}
let settings_changed = self
.settings
.update(|settings| {
settings.custom_lists[list_index] = new_list;
})
.await
.map_err(Error::SettingsError);
if let Ok(true) = settings_changed {
self.relay_selector
.set_config(new_selector_config(&self.settings));
if self.change_should_cause_reconnect(id) {
log::info!("Initiating tunnel restart because a selected custom list changed");
self.reconnect_tunnel();
}
}
settings_changed?;
Ok(())
}
fn change_should_cause_reconnect(&self, custom_list_id: Id) -> bool {
use mullvad_types::states::TunnelState;
let mut need_to_reconnect = false;
if let RelaySettings::Normal(relay_settings) = &self.settings.relay_settings {
if let Constraint::Only(LocationConstraint::CustomList { list_id }) =
&relay_settings.location
{
need_to_reconnect |= list_id == &custom_list_id;
}
if let TunnelState::Connecting {
endpoint,
location: _,
}
| TunnelState::Connected {
endpoint,
location: _,
} = &self.tunnel_state
{
match endpoint.tunnel_type {
TunnelType::Wireguard => {
if relay_settings.wireguard_constraints.use_multihop {
if let Constraint::Only(LocationConstraint::CustomList { list_id }) =
&relay_settings.wireguard_constraints.entry_location
{
need_to_reconnect |= list_id == &custom_list_id;
}
}
}
TunnelType::OpenVpn => {
if !matches!(self.settings.bridge_state, BridgeState::Off) {
if let Ok(ResolvedBridgeSettings::Normal(bridge_settings)) =
self.settings.bridge_settings.resolve()
{
if let Constraint::Only(LocationConstraint::CustomList {
list_id,
}) = &bridge_settings.location
{
need_to_reconnect |= list_id == &custom_list_id;
}
}
}
}
}
}
}
need_to_reconnect
}
}