Skip to content

Commit

Permalink
Provide a command-line option for the local url
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Hollensbe <git@hollensbe.org>
  • Loading branch information
erikh committed Mar 6, 2023
1 parent 05c2e9d commit aedcdeb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ pub struct StartArgs {
#[clap(long = "tls-key", value_name = "PATH")]
pub tls_key: Option<PathBuf>,

/// Provide a different URL for contacting the local zerotier-one service.
#[clap(long = "local-url", value_name = "LOCAL_URL")]
pub local_url: Option<String>,

/// Log Level to print [off, trace, debug, error, warn, info]
#[clap(short = 'l', long = "log-level", value_name = "LEVEL")]
pub log_level: Option<crate::log::LevelFilter>,
Expand Down Expand Up @@ -103,6 +107,7 @@ impl Into<Launcher> for StartArgs {
tls_key: self.tls_key,
log_level: self.log_level,
network_id: Some(self.network_id),
local_url: self.local_url,
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct Launcher {
pub tls_key: Option<PathBuf>,
pub wildcard: bool,
pub log_level: Option<crate::log::LevelFilter>,
pub local_url: Option<String>,
#[serde(skip_deserializing)]
pub network_id: Option<String>,
}
Expand Down Expand Up @@ -65,6 +66,7 @@ impl Default for Launcher {
wildcard: false,
network_id: None,
log_level: None,
local_url: None,
}
}
}
Expand Down Expand Up @@ -106,7 +108,12 @@ impl Launcher {
let client = central_client(central_token(self.token.as_deref())?)?;

info!("Welcome to ZeroNS!");
let ips = get_listen_ips(&authtoken, &self.network_id.clone().unwrap()).await?;
let ips = get_listen_ips(
&authtoken,
&self.network_id.clone().unwrap(),
self.local_url.clone(),
)
.await?;

// more or less the setup for the "main loop"
if ips.len() > 0 {
Expand Down Expand Up @@ -141,7 +148,8 @@ impl Launcher {
}
}

let member_name = get_member_name(authtoken, domain_name.clone()).await?;
let member_name =
get_member_name(authtoken, domain_name.clone(), self.local_url.clone()).await?;

let network = client
.get_network_by_id(&self.network_id.clone().unwrap())
Expand Down
22 changes: 17 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ pub fn parse_member_name(name: Option<String>, domain_name: Name) -> Option<Name
pub async fn get_member_name(
authtoken_path: &Path,
domain_name: Name,
local_url: Option<String>,
) -> Result<LowerName, anyhow::Error> {
let client = local_client_from_file(authtoken_path)?;
let client = local_client_from_file(authtoken_path, local_url)?;

let status = client.get_status().await?;
if let Some(address) = &status.address {
Expand All @@ -165,16 +166,26 @@ pub async fn get_member_name(

fn local_client_from_file(
authtoken_path: &Path,
local_url: Option<String>,
) -> Result<zerotier_one_api::Client, anyhow::Error> {
let authtoken = std::fs::read_to_string(authtoken_path)?;
local_client(authtoken)
local_client(authtoken, local_url)
}

pub fn local_client(authtoken: String) -> Result<zerotier_one_api::Client, anyhow::Error> {
pub fn local_client(
authtoken: String,
local_url: Option<String>,
) -> Result<zerotier_one_api::Client, anyhow::Error> {
let mut headers = HeaderMap::new();
headers.insert("X-ZT1-Auth", HeaderValue::from_str(&authtoken)?);

let local_url = if let Ok(url) = std::env::var("ZEROTIER_LOCAL_URL") {
let local_url = if let Some(url) = local_url {
if url.len() > 0 {
url
} else {
ZEROTIER_LOCAL_URL.to_string()
}
} else if let Ok(url) = std::env::var("ZEROTIER_LOCAL_URL") {
if url.len() > 0 {
url
} else {
Expand All @@ -198,8 +209,9 @@ pub fn local_client(authtoken: String) -> Result<zerotier_one_api::Client, anyho
pub async fn get_listen_ips(
authtoken_path: &Path,
network_id: &str,
local_url: Option<String>,
) -> Result<Vec<String>, anyhow::Error> {
let client = local_client_from_file(authtoken_path)?;
let client = local_client_from_file(authtoken_path, local_url)?;

match client.get_network(network_id).await {
Err(error) => Err(anyhow!(
Expand Down

0 comments on commit aedcdeb

Please sign in to comment.