-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tailscale): move dns utils to their own module
- Loading branch information
Showing
6 changed files
with
122 additions
and
122 deletions.
There are no files selected for viewing
This file contains 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,9 @@ | ||
use crate::tailscale::utils::{sanitize_hostname, trim_suffix, Machine, PeerKind}; | ||
|
||
pub fn dns_or_quote_hostname(m: &mut Machine, dns_suffix: &str) { | ||
let base_name = trim_suffix(&m.dns_name, dns_suffix); | ||
m.display_name = match base_name { | ||
n if n.is_empty() => PeerKind::DNSName(sanitize_hostname(m.hostname.as_str())), | ||
base => PeerKind::HostName(base), | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
pub mod dns; | ||
pub mod peer; | ||
pub mod status; | ||
pub mod utils; |
This file contains 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,47 @@ | ||
use crate::clipboard::{copy_to_clipboard, get_from_clipboard}; | ||
use log::{error, info}; | ||
use notify_rust::Notification; | ||
|
||
pub fn check_peer_ip(peer_ip: &str) { | ||
if peer_ip.is_empty() { | ||
error!("No peer IP.") | ||
} else { | ||
info!("Peer IP: {}", peer_ip); | ||
} | ||
} | ||
|
||
pub fn copy_peer_ip(peer_ip: &str, notif_title: &str) { | ||
check_peer_ip(peer_ip); | ||
|
||
match copy_to_clipboard(peer_ip) { | ||
Ok(_) => { | ||
// Get IP from clipboard to verify | ||
match get_from_clipboard() { | ||
Ok(clip_ip) => { | ||
// log success | ||
info!("Copied IP address {} to the Clipboard", clip_ip); | ||
|
||
// send a notification through dbus | ||
let body = format!("Copied IP address {} to the Clipboard", clip_ip); | ||
let _result = Notification::new() | ||
.summary(notif_title) | ||
.body(&body) | ||
.icon("info") | ||
.show(); | ||
} | ||
|
||
Err(e) => { | ||
let message = "Failed to get IP from clipboard"; | ||
error!("{}: {}", message, e); | ||
|
||
let _result = Notification::new() | ||
.summary(notif_title) | ||
.body(&message) | ||
.icon("error") | ||
.show(); | ||
} | ||
} | ||
} | ||
Err(e) => error!("Failed to copy IP to clipboard: {}", e), | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,14 +1,58 @@ | ||
use crate::tailscale::utils; | ||
use crate::tailscale::dns; | ||
use crate::tailscale::utils::{Machine, User}; | ||
use crate::tray::Context; | ||
use serde::{Deserialize, Serialize}; | ||
use std::{collections::HashMap, process::Command}; | ||
use which::which; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct Status { | ||
// TODO: mutex | ||
#[serde(skip)] | ||
pub tailscale_up: bool, | ||
#[serde(rename(deserialize = "BackendState"))] | ||
backend_state: String, | ||
#[serde(rename(deserialize = "Self"))] | ||
pub this_machine: Machine, | ||
#[serde(rename(deserialize = "MagicDNSSuffix"))] | ||
magic_dnssuffix: String, | ||
#[serde(rename(deserialize = "Peer"))] | ||
pub peers: HashMap<String, Machine>, | ||
#[serde(rename(deserialize = "User"))] | ||
user: HashMap<String, User>, | ||
} | ||
|
||
pub fn get_current_status() -> Context { | ||
let status: utils::Status = utils::get_status().unwrap(); | ||
|
||
let status: Status = get_status().unwrap(); | ||
|
||
Context { | ||
ip: status.this_machine.ips[0].clone(), | ||
pkexec: which("pkexec").unwrap(), | ||
status, | ||
} | ||
} | ||
|
||
pub fn get_status_json() -> String { | ||
let output = Command::new("tailscale") | ||
.arg("status") | ||
.arg("--json") | ||
.output() | ||
.expect("Fetch tailscale status fail."); | ||
String::from_utf8(output.stdout).expect("Unable to convert status output string.") | ||
} | ||
|
||
pub fn get_status() -> Result<Status, serde_json::Error> { | ||
let mut st: Status = serde_json::from_str(get_status_json().as_str())?; | ||
let dnssuffix = st.magic_dnssuffix.to_owned(); | ||
st.tailscale_up = match st.backend_state.as_str() { | ||
"Running" => true, | ||
"Stopped" => false, | ||
_ => false, | ||
}; | ||
|
||
dns::dns_or_quote_hostname(&mut st.this_machine, &dnssuffix); | ||
st.peers | ||
.values_mut() | ||
.for_each(|m: &mut Machine| dns::dns_or_quote_hostname(m, &dnssuffix)); | ||
Ok(st) | ||
} |
This file contains 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 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