-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathformat.rs
265 lines (245 loc) · 8.83 KB
/
format.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
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
use mullvad_types::{auth_failed::AuthFailed, location::GeoIpLocation, states::TunnelState};
use talpid_types::{
net::{Endpoint, TunnelEndpoint},
tunnel::ErrorState,
};
#[macro_export]
macro_rules! print_option {
($value:expr $(,)?) => {{
println!("{:<4}{:<24}{}", "", "", $value,)
}};
($option:expr, $value:expr $(,)?) => {{
println!("{:<4}{:<24}{}", "", concat!($option, ":"), $value,)
}};
}
pub fn print_state(state: &TunnelState, verbose: bool) {
use TunnelState::*;
match state {
Error(error) => print_error_state(error),
Connected { endpoint, location } => {
println!(
"Connected to {}",
format_relay_connection(endpoint, location.as_ref(), verbose)
);
if verbose {
if let Some(tunnel_interface) = &endpoint.tunnel_interface {
println!("Tunnel interface: {tunnel_interface}")
}
}
}
Connecting { endpoint, location } => {
let ellipsis = if !verbose { "..." } else { "" };
println!(
"Connecting to {}{ellipsis}",
format_relay_connection(endpoint, location.as_ref(), verbose)
);
}
Disconnected {
location: _,
locked_down,
} => {
if *locked_down {
println!("Disconnected (Internet access is blocked due to lockdown mode)");
} else {
println!("Disconnected");
}
}
Disconnecting(_) => println!("Disconnecting..."),
}
}
pub fn print_location(state: &TunnelState) {
let location = match state {
TunnelState::Disconnected {
location,
locked_down: _,
} => location,
TunnelState::Connected { location, .. } => location,
_ => return,
};
if let Some(location) = location {
print!("Your connection appears to be from: {}", location.country);
if let Some(city) = &location.city {
print!(", {}", city);
}
if let Some(ipv4) = location.ipv4 {
print!(". IPv4: {ipv4}");
}
if let Some(ipv6) = location.ipv6 {
print!(", IPv6: {ipv6}");
}
println!();
}
}
fn format_relay_connection(
endpoint: &TunnelEndpoint,
location: Option<&GeoIpLocation>,
verbose: bool,
) -> String {
let prefix_separator = if verbose { "\n\t" } else { " " };
let mut obfuscator_overlaps = false;
let exit_endpoint = {
let mut exit_endpoint = &endpoint.endpoint;
if let Some(obfuscator) = &endpoint.obfuscation {
if location
.map(|l| l.hostname == l.obfuscator_hostname)
.unwrap_or(false)
{
obfuscator_overlaps = true;
exit_endpoint = &obfuscator.endpoint;
}
};
let exit = format_endpoint(
location.and_then(|l| l.hostname.as_deref()),
exit_endpoint,
verbose,
);
match location {
Some(GeoIpLocation {
country,
city: Some(city),
..
}) => {
format!("{exit} in {city}, {country}")
}
Some(GeoIpLocation {
country,
city: None,
..
}) => {
format!("{exit} in {country}")
}
None => exit,
}
};
let first_hop = endpoint.entry_endpoint.as_ref().map(|entry| {
let mut entry_endpoint = entry;
if let Some(obfuscator) = &endpoint.obfuscation {
if location
.map(|l| l.entry_hostname == l.obfuscator_hostname)
.unwrap_or(false)
{
obfuscator_overlaps = true;
entry_endpoint = &obfuscator.endpoint;
}
};
let endpoint = format_endpoint(
location.and_then(|l| l.entry_hostname.as_deref()),
entry_endpoint,
verbose,
);
format!("{prefix_separator}via {endpoint}")
});
let obfuscator = endpoint.obfuscation.as_ref().map(|obfuscator| {
if !obfuscator_overlaps {
let endpoint_str = format_endpoint(
location.and_then(|l| l.obfuscator_hostname.as_deref()),
&obfuscator.endpoint,
verbose,
);
format!("{prefix_separator}obfuscated via {endpoint_str}")
} else {
String::new()
}
});
let bridge = endpoint.proxy.as_ref().map(|proxy| {
let proxy_endpoint = format_endpoint(
location.and_then(|l| l.bridge_hostname.as_deref()),
&proxy.endpoint,
verbose,
);
format!("{prefix_separator}via {proxy_endpoint}")
});
let tunnel_type = if verbose {
format!("\nTunnel type: {}", endpoint.tunnel_type)
} else {
String::new()
};
let quantum_resistant = if !verbose {
""
} else if endpoint.quantum_resistant {
"\nQuantum resistant tunnel: yes"
} else {
"\nQuantum resistant tunnel: no"
};
#[cfg(target_os = "windows")]
let daita = if !verbose {
""
} else if endpoint.daita {
"\nDAITA: yes"
} else {
"\nDAITA: no"
};
#[cfg(not(target_os = "windows"))]
let daita = "";
let mut bridge_type = String::new();
let mut obfuscator_type = String::new();
if verbose {
if let Some(bridge) = &endpoint.proxy {
bridge_type = format!("\nBridge type: {}", bridge.proxy_type);
}
if let Some(obfuscator) = &endpoint.obfuscation {
obfuscator_type = format!("\nObfuscator: {}", obfuscator.obfuscation_type);
}
}
format!(
"{exit_endpoint}{first_hop}{bridge}{obfuscator}{tunnel_type}{quantum_resistant}{daita}{bridge_type}{obfuscator_type}",
first_hop = first_hop.unwrap_or_default(),
bridge = bridge.unwrap_or_default(),
obfuscator = obfuscator.unwrap_or_default(),
)
}
fn format_endpoint(hostname: Option<&str>, endpoint: &Endpoint, verbose: bool) -> String {
match (hostname, verbose) {
(Some(hostname), true) => format!("{hostname} ({endpoint})"),
(None, true) => endpoint.to_string(),
(Some(hostname), false) => hostname.to_string(),
(None, false) => endpoint.address.to_string(),
}
}
fn print_error_state(error_state: &ErrorState) {
if error_state.block_failure().is_some() {
eprintln!("Mullvad daemon failed to setup firewall rules!");
eprintln!("Daemon cannot block traffic from flowing, non-local traffic will leak");
}
match error_state.cause() {
#[cfg(target_os = "linux")]
cause @ talpid_types::tunnel::ErrorStateCause::SetFirewallPolicyError(_) => {
println!("Blocked: {cause}");
println!("Your kernel might be terribly out of date or missing nftables");
}
#[cfg(target_os = "macos")]
cause @ talpid_types::tunnel::ErrorStateCause::NeedFullDiskPermissions => {
println!("Blocked: {cause}");
println!();
println!(
r#"Enable "Full Disk Access" for "Mullvad VPN" in the macOS system settings:"#
);
println!(
r#"open "x-apple.systempreferences:com.apple.preference.security?Privacy_AllFiles""#
);
println!();
println!("Restart the Mullvad daemon for the change to take effect:");
println!("launchctl unload -w /Library/LaunchDaemons/net.mullvad.daemon.plist");
println!("launchctl load -w /Library/LaunchDaemons/net.mullvad.daemon.plist");
}
talpid_types::tunnel::ErrorStateCause::AuthFailed(Some(auth_failed)) => {
println!(
"Blocked: Authentication with remote server failed: {}",
get_auth_failed_message(AuthFailed::from(auth_failed.as_str()))
);
}
cause => println!("Blocked: {cause}"),
}
}
const fn get_auth_failed_message(auth_failed: AuthFailed) -> &'static str {
const INVALID_ACCOUNT_MSG: &str = "You've logged in with an account number that is not valid. Please log out and try another one.";
const EXPIRED_ACCOUNT_MSG: &str = "You have no more VPN time left on this account. Please log in on our website to buy more credit.";
const TOO_MANY_CONNECTIONS_MSG: &str = "This account has too many simultaneous connections. Disconnect another device or try connecting again shortly.";
const UNKNOWN_MSG: &str = "Unknown error.";
match auth_failed {
AuthFailed::InvalidAccount => INVALID_ACCOUNT_MSG,
AuthFailed::ExpiredAccount => EXPIRED_ACCOUNT_MSG,
AuthFailed::TooManyConnections => TOO_MANY_CONNECTIONS_MSG,
AuthFailed::Unknown => UNKNOWN_MSG,
}
}