-
Notifications
You must be signed in to change notification settings - Fork 488
Add Wireguard support to vpn block #1981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ppx17
wants to merge
7
commits into
greshake:master
Choose a base branch
from
ppx17:vpn-wireshark
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0a46b52
Add Wireguard support to vpn block
ppx17 954f445
Remove redundant static lifetime.
ppx17 037b773
Use borrowed slice instead of owned Vector for command arguments.
ppx17 f66c113
Separate state for connections to a specific country.
ppx17 6dc6286
Fix clippy issues
ppx17 46c5518
Single connect status with optional country attributes.
ppx17 d155853
Little clean-up.
ppx17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use std::process::Stdio; | ||
|
||
use async_trait::async_trait; | ||
use nix::unistd::getuid; | ||
use tokio::process::Command; | ||
|
||
use crate::blocks::prelude::*; | ||
|
||
use super::{Driver, Status}; | ||
|
||
pub struct WireguardDriver { | ||
interface: String, | ||
} | ||
|
||
impl WireguardDriver { | ||
pub async fn new(interface: String) -> WireguardDriver { | ||
WireguardDriver { interface } | ||
} | ||
} | ||
|
||
const SUDO_CMD: &str = "/usr/bin/sudo"; | ||
const WG_QUICK_CMD: &str = "/usr/bin/wg-quick"; | ||
const WG_CMD: &str = "/usr/bin/wg"; | ||
|
||
#[async_trait] | ||
impl Driver for WireguardDriver { | ||
async fn get_status(&self) -> Result<Status> { | ||
let status = run_wg(vec!["show", self.interface.as_str()]).await; | ||
|
||
match status { | ||
Ok(status) => { | ||
if status.contains(format!("interface: {}", self.interface).as_str()) { | ||
Ok(Status::Connected { | ||
country: "".to_owned(), | ||
country_flag: "".to_owned(), | ||
}) | ||
} else { | ||
Ok(Status::Disconnected) | ||
} | ||
} | ||
Err(_) => Ok(Status::Error), | ||
} | ||
} | ||
|
||
async fn toggle_connection(&self, status: &Status) -> Result<()> { | ||
match status { | ||
Status::Connected { .. } => { | ||
run_wg_quick(vec!["down", self.interface.as_str()]).await?; | ||
} | ||
Status::Disconnected => { | ||
run_wg_quick(vec!["up", self.interface.as_str()]).await?; | ||
} | ||
Status::Error => (), | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
async fn run_wg(args: Vec<&str>) -> Result<String> { | ||
let stdout = make_command(should_use_sudo(), WG_CMD) | ||
.args(&args) | ||
.output() | ||
.await | ||
.error(format!("Problem running wg command: {args:?}"))? | ||
.stdout; | ||
let stdout = | ||
String::from_utf8(stdout).error(format!("wg produced non-UTF8 output: {args:?}"))?; | ||
Ok(stdout) | ||
} | ||
|
||
async fn run_wg_quick(args: Vec<&str>) -> Result<()> { | ||
ppx17 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
make_command(should_use_sudo(), WG_QUICK_CMD) | ||
.args(&args) | ||
.stdin(Stdio::null()) | ||
.stdout(Stdio::null()) | ||
.spawn() | ||
.error(format!("Problem running wg-quick command: {args:?}"))? | ||
.wait() | ||
.await | ||
.error(format!("Problem running wg-quick command: {args:?}"))?; | ||
Ok(()) | ||
} | ||
|
||
fn make_command(use_sudo: bool, cmd: &str) -> Command { | ||
let mut command = Command::new(if use_sudo { SUDO_CMD } else { cmd }); | ||
|
||
if use_sudo { | ||
command.arg("-n").arg(cmd); | ||
} | ||
command | ||
} | ||
|
||
fn should_use_sudo() -> bool { | ||
!(getuid().is_root()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.