Skip to content

Commit 0eaa15e

Browse files
committed
feat : support cloudflare in vpn block
1 parent 71cb8d7 commit 0eaa15e

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/blocks/vpn.rs

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
//! ## Mullvad
3333
//! Behind the scenes the mullvad driver uses the `mullvad` command line binary. In order for this to work properly the binary should be executable and mullvad daemon should be running.
3434
//!
35+
//! ## Cloudflare WARP
36+
//! Behind the scenes the WARP driver uses the `warp-cli` command line binary. Just ensure the binary is executable without root privileges.
37+
//!
3538
//! # Example
3639
//!
3740
//! Shows the current vpn network state:
@@ -71,6 +74,8 @@ mod nordvpn;
7174
use nordvpn::NordVpnDriver;
7275
mod mullvad;
7376
use mullvad::MullvadDriver;
77+
mod warp;
78+
use warp::WarpDriver;
7479

7580
use super::prelude::*;
7681

@@ -80,6 +85,7 @@ pub enum DriverType {
8085
#[default]
8186
Nordvpn,
8287
Mullvad,
88+
Warp,
8389
}
8490

8591
#[derive(Deserialize, Debug, SmartDefault)]
@@ -123,6 +129,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
123129
let driver: Box<dyn Driver> = match config.driver {
124130
DriverType::Nordvpn => Box::new(NordVpnDriver::new().await),
125131
DriverType::Mullvad => Box::new(MullvadDriver::new().await),
132+
DriverType::Warp => Box::new(WarpDriver::new().await),
126133
};
127134

128135
loop {

src/blocks/vpn/warp.rs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use std::process::Stdio;
2+
use tokio::process::Command;
3+
4+
use super::{Driver, Status};
5+
use crate::blocks::prelude::*;
6+
7+
pub struct WarpDriver;
8+
9+
impl WarpDriver {
10+
pub async fn new() -> WarpDriver {
11+
WarpDriver
12+
}
13+
14+
async fn run_network_command(arg: &str) -> Result<()> {
15+
Command::new("warp-cli")
16+
.args([arg])
17+
.stdin(Stdio::null())
18+
.stdout(Stdio::null())
19+
.spawn()
20+
.error(format!("Problem running warp-cli command: {arg}"))?
21+
.wait()
22+
.await
23+
.error(format!("Problem running warp-cli command: {arg}"))?;
24+
Ok(())
25+
}
26+
}
27+
28+
#[async_trait]
29+
impl Driver for WarpDriver {
30+
async fn get_status(&self) -> Result<Status> {
31+
let stdout = Command::new("warp-cli")
32+
.args(["status"])
33+
.output()
34+
.await
35+
.error("Problem running warp-cli command")?
36+
.stdout;
37+
38+
let status = String::from_utf8(stdout).error("warp-cli produced non-UTF8 output")?;
39+
40+
if status.contains("Status update: Disconnected") {
41+
return Ok(Status::Disconnected);
42+
} else if status.contains("Status update: Connected") {
43+
return Ok(Status::Connected {
44+
country: "".to_string(), // because warp-cli doesn't provide country/server info
45+
country_flag: "".to_string(), // no country means no flag
46+
});
47+
}
48+
Ok(Status::Error)
49+
}
50+
51+
async fn toggle_connection(&self, status: &Status) -> Result<()> {
52+
match status {
53+
Status::Connected { .. } => Self::run_network_command("disconnect").await?,
54+
Status::Disconnected => Self::run_network_command("connect").await?,
55+
Status::Error => (),
56+
}
57+
Ok(())
58+
}
59+
}

0 commit comments

Comments
 (0)