Skip to content

Commit

Permalink
feat: allow setting the local IP in a record
Browse files Browse the repository at this point in the history
Resolves #1
  • Loading branch information
nrdxp committed Oct 12, 2023
1 parent 24b76a0 commit a03d814
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
50 changes: 50 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 @@ -12,6 +12,7 @@ tokio = { version = "^1", features = ["rt-multi-thread", "macros"] }
cloudflare = "^0.10"
anyhow = "^1"
clap-verbosity-flag = "^2.0"
local-ip-address = "^0.5.0"

[dependencies.clap]
version = "^4.4"
Expand Down
4 changes: 4 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ pub struct Cli {
)]
pub email: Option<String>,

/// Use local network ip
#[clap(short, long)]
pub local: bool,

#[clap(flatten)]
pub verbose: Verbosity<InfoLevel>,
/// set an AAAA record to the host's ipv6 address
Expand Down
15 changes: 10 additions & 5 deletions src/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::dns::{Fqdn, Requests, ZoneId};
use anyhow::Result;
use cloudflare::endpoints::dns::{DnsContent, DnsRecord};
use cloudflare::framework::async_api::{ApiClient, Client};
use local_ip_address as local;
use public_ip::{http, Version};
use std::net::IpAddr;
use std::sync::Arc;
Expand Down Expand Up @@ -58,11 +59,15 @@ impl DynDns for Option<IpAddr> {
}
}

pub async fn get_ips() -> Result<IpPair> {
let (ipv4, ipv6) = tokio::join!(
public_ip::addr_with(http::ALL, Version::V4),
public_ip::addr_with(public_ip::ALL, Version::V6)
);
pub async fn get_ips(loc: bool) -> Result<IpPair> {
let (ipv4, ipv6) = if loc {
(local::local_ip().ok(), local::local_ipv6().ok())
} else {
tokio::join!(
public_ip::addr_with(http::ALL, Version::V4),
public_ip::addr_with(public_ip::ALL, Version::V6)
)
};

if (None, None) == (ipv4, ipv6) {
Err(anyhow::anyhow!(
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn main() -> Result<()> {
.filter_level(cli.verbose.log_level_filter())
.init();

let (public_ipv4, public_ipv6) = ip::get_ips().await?;
let (public_ipv4, public_ipv6) = ip::get_ips(cli.local).await?;
let api_client = Arc::new(api::get_client(&cli)?);
let records = dns::get_records(&cli, api_client.clone()).await?;
let mut handles: Vec<JoinHandle<Result<()>>> =
Expand Down

0 comments on commit a03d814

Please sign in to comment.