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
|
use crate::{new_rpc_client, Command, Error, Result};
use futures::{Future, Stream};
use mullvad_ipc_client::DaemonRpcClient;
use mullvad_types::{auth_failed::AuthFailed, states::TunnelState, DaemonEvent};
use talpid_types::tunnel::{ErrorState, ErrorStateCause};
pub struct Status;
impl Command for Status {
fn name(&self) -> &'static str {
"status"
}
fn clap_subcommand(&self) -> clap::App<'static, 'static> {
clap::SubCommand::with_name(self.name())
.about("View the state of the VPN tunnel")
.arg(
clap::Arg::with_name("location")
.long("location")
.short("l")
.help("Prints the current location and IP. Based on GeoIP lookups"),
)
.subcommand(
clap::SubCommand::with_name("listen")
.about("Listen for VPN tunnel state changes")
.arg(
clap::Arg::with_name("verbose")
.short("v")
.help("Enables verbose output"),
),
)
}
fn run(&self, matches: &clap::ArgMatches<'_>) -> Result<()> {
let mut rpc = new_rpc_client()?;
let state = rpc.get_state()?;
print_state(&state);
if matches.is_present("location") {
print_location(&mut rpc)?;
}
if let Some(listen_matches) = matches.subcommand_matches("listen") {
let verbose = listen_matches.is_present("verbose");
let subscription = rpc
.daemon_event_subscribe()
.wait()
.map_err(Error::CantSubscribe)?;
for event in subscription.wait() {
match event? {
DaemonEvent::TunnelState(new_state) => {
print_state(&new_state);
use self::TunnelState::*;
match new_state {
Connected { .. } | Disconnected => {
if matches.is_present("location") {
print_location(&mut rpc)?;
}
}
_ => {}
}
}
DaemonEvent::Settings(settings) => {
if verbose {
println!("New settings: {:#?}", settings);
}
}
DaemonEvent::RelayList(relay_list) => {
if verbose {
println!("New relay list: {:#?}", relay_list);
}
}
DaemonEvent::AppVersionInfo(app_version_info) => {
if verbose {
println!("New app version info: {:#?}", app_version_info);
}
}
DaemonEvent::WireguardKey(key_event) => {
if verbose {
println!("{}", key_event);
}
}
}
}
}
Ok(())
}
}
fn print_state(state: &TunnelState) {
use self::TunnelState::*;
print!("Tunnel status: ");
match state {
Error(reason) => print_error_state(reason),
Connected { endpoint, .. } => {
println!("Connected to {}", endpoint);
}
Connecting { endpoint, .. } => println!("Connecting to {}...", endpoint),
Disconnected => println!("Disconnected"),
Disconnecting(_) => println!("Disconnecting..."),
}
}
fn print_error_state(error_state: &ErrorState) {
if !error_state.is_blocking() {
eprintln!("Mullvad daemon failed to setup firewall rules!");
eprintln!("Deamon cannot block traffic from flowing, non-local traffic will leak");
}
print_blocked_reason(error_state.cause());
}
fn print_blocked_reason(reason: &ErrorStateCause) {
match reason {
ErrorStateCause::AuthFailed(ref auth_failure) => {
let auth_failure_str = auth_failure
.as_ref()
.map(|s| s.as_str())
.unwrap_or("Account authentication failed");
println!("Blocked: {}", AuthFailed::from(auth_failure_str));
}
#[cfg(target_os = "linux")]
ErrorStateCause::SetFirewallPolicyError => {
println!("Blocked: {}", ErrorStateCause::SetFirewallPolicyError);
println!("Your kernel might be terribly out of date or missing nftables");
}
other => println!("Blocked: {}", other),
}
}
fn print_location(rpc: &mut DaemonRpcClient) -> Result<()> {
let location = match rpc.get_current_location()? {
Some(loc) => loc,
None => {
println!("Location data unavailable");
return Ok(());
}
};
if let Some(hostname) = location.hostname {
println!("Relay: {}", hostname);
}
if let Some(ipv4) = location.ipv4 {
println!("IPv4: {}", ipv4);
}
if let Some(ipv6) = location.ipv6 {
println!("IPv6: {}", ipv6);
}
print!("Location: ");
if let Some(city) = location.city {
print!("{}, ", city);
}
println!("{}", location.country);
println!(
"Position: {:.5}°N, {:.5}°W",
location.latitude, location.longitude
);
Ok(())
}
|