Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrinux committed Jul 22, 2024
1 parent ed30536 commit 699b0e4
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 17 deletions.
94 changes: 94 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ reqwest = { version = "~0.12", features = ["blocking", "default"] }
notify-rust = { version = "4", features = ["default"] }
which = { version = "~6.0" }
clap = { version = "4", features = ["derive"] }
ipaddress = "0.1.3"
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ pub struct RealCommandRunner;

impl CommandRunner for RealCommandRunner {
fn run_command(&self, command: &str, args: &[&str]) -> Result<Output, std::io::Error> {
Command::new(command).args(args).output()
Command::new(command).args(args).env("LC_ALL", "C").output()
}
}

Expand Down Expand Up @@ -472,6 +472,7 @@ pub fn is_known_network(ssid: &str) -> Result<bool, Box<dyn std::error::Error>>
}

fn convert_network_strength(line: &str) -> String {
println!("{line}");
// Define the mapping for network strength symbols
let strength_symbols = ["_", "▂", "▄", "▆", "█"];

Expand Down
45 changes: 29 additions & 16 deletions src/tailscale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,31 @@ fn get_mullvad_actions_with_command_runner(command_runner: &dyn CommandRunner) -

fn parse_mullvad_line(line: &str, regex: &Regex, active_exit_node: &str) -> String {
let parts: Vec<&str> = regex.split(line).collect();
let country = parts.get(2).unwrap_or(&"");
let node_name = parts.get(1).unwrap_or(&"");
let is_active = active_exit_node == *node_name;
let node_ip = parts.get(0).unwrap_or(&"").trim();
let node_name = parts.get(1).unwrap_or(&"").trim();
let country = parts.get(2).unwrap_or(&"").trim();
let is_active = active_exit_node == node_name;
format_entry(
"mullvad",
if is_active { "✅" } else { get_flag(country) },
&format!("{country} - {node_name}"),
&format!("{country:<15} - {node_ip:<16} {node_name}"),
)
}

fn extract_short_name(node_name: &str) -> &str {
node_name.split('.').next().unwrap_or(node_name)
}

fn parse_exit_node_line(line: &str, regex: &Regex, active_exit_node: &str) -> String {
let parts: Vec<&str> = regex.split(line).collect();
let node_ip = parts.first().unwrap_or(&"").trim();
let node_name = parts.get(1).unwrap_or(&"");
let is_active = active_exit_node == *node_name;
let node_name = parts.get(1).unwrap_or(&"").trim();
let node_short_name = extract_short_name(node_name);
let is_active = active_exit_node == node_name;
format_entry(
"exit-node",
if is_active { "✅" } else { "🌿" },
&format!("{node_name} - {node_ip}"),
&format!("{node_short_name:<15} - {node_ip:<16} {node_name}"),
)
}

Expand Down Expand Up @@ -119,11 +125,14 @@ fn get_active_exit_node() -> String {
}

fn set_exit_node(action: &str) -> bool {
let node_name = match extract_node_name(action) {
Some(name) => name,
let node_ip = match extract_node_ip(action) {
Some(ip) => ip,
None => return false,
};

#[cfg(debug_assertions)]
println!("Exit-node ip address: {node_ip}");

if !execute_command("tailscale", &["up"]) {
return false;
}
Expand All @@ -133,23 +142,24 @@ fn set_exit_node(action: &str) -> bool {
&[
"set",
"--exit-node",
node_name,
node_ip,
"--exit-node-allow-lan-access=true",
],
)
}

fn extract_node_name(action: &str) -> Option<&str> {
let regex = Regex::new(r" ([\w_.-]+)$").ok()?;
regex
fn extract_node_ip(action: &str) -> Option<&str> {
Regex::new(r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b")
.ok()?
.captures(action)
.and_then(|caps| caps.get(1))
.and_then(|caps| caps.get(0))
.map(|m| m.as_str())
}

fn execute_command(command: &str, args: &[&str]) -> bool {
Command::new(command)
.args(args)
.env("LC_ALL", "C")
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
Expand Down Expand Up @@ -212,7 +222,10 @@ fn get_flag(country: &str) -> &'static str {
}

pub fn is_exit_node_active() -> Result<bool, Box<dyn std::error::Error>> {
let output = Command::new("tailscale").arg("status").output()?;
let output = Command::new("tailscale")
.arg("status")
.env("LC_ALL", "C")
.output()?;

if output.status.success() {
let reader = BufReader::new(output.stdout.as_slice());
Expand Down Expand Up @@ -242,7 +255,7 @@ impl CommandRunner for RealCommandRunner {
command: &str,
args: &[&str],
) -> Result<std::process::Output, std::io::Error> {
Command::new(command).args(args).output()
Command::new(command).args(args).env("LC_ALL", "C").output()
}
}

Expand Down

0 comments on commit 699b0e4

Please sign in to comment.