Skip to content

Commit

Permalink
IPs are options now so failing to fetch one doesn't fuck you over
Browse files Browse the repository at this point in the history
  • Loading branch information
N1kO23 committed Apr 4, 2022
1 parent b4af335 commit beeb817
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 57 deletions.
30 changes: 16 additions & 14 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ pub struct CurrentIP {

pub struct IPAddresses {
/// The fetched IPv4 address
pub ipv4: String,
pub ipv4: Option<String>,
/// The fetched IPv6 address
pub ipv6: String,
pub ipv6: Option<String>,
}

/// Struct for the Cloudflares response
Expand Down Expand Up @@ -67,7 +67,7 @@ const CF_BASE_URL: &str = "https://api.cloudflare.com/client/v4/zones/";
/// use cloudflare_dns_updater::api;
///
/// let ip = api::get_current_ip().await?;
/// println!("{}", ip);
/// println!("{}", Some(ip.ip));
/// ```
///
/// # Errors
Expand All @@ -82,17 +82,19 @@ const CF_BASE_URL: &str = "https://api.cloudflare.com/client/v4/zones/";
/// # See Also
/// * [https://api.ipify.org](https://api.ipify.org)
pub async fn get_current_ip() -> Result<IPAddresses, reqwest::Error> {
let ipv4 = reqwest::get(IPV4_ADDRESS_URL)
.await?
.json::<CurrentIP>()
.await?
.ip;
let ipv6 = reqwest::get(IPV6_ADDRESS_URL)
.await?
.json::<CurrentIP>()
.await?
.ip;
let cur_ips = IPAddresses { ipv4, ipv6 };
let mut cur_ips = IPAddresses {
ipv4: None,
ipv6: None,
};
match reqwest::get(IPV4_ADDRESS_URL).await {
Ok(ipv4) => cur_ips.ipv4 = Some(ipv4.json::<CurrentIP>().await?.ip),
Err(_) => (),
};
match reqwest::get(IPV6_ADDRESS_URL).await {
Ok(ipv6) => cur_ips.ipv6 = Some(ipv6.json::<CurrentIP>().await?.ip),
Err(_) => (),
};

Ok(cur_ips)
}

Expand Down
98 changes: 55 additions & 43 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ async fn main() {
/// * `Ok(())` - If the IP addresses were updated successfully
/// * `Err(e)` - If the IP addresses could not be updated
async fn check_and_update_ip(config: &Config) -> Result<(), Box<dyn std::error::Error>> {
print!("\nGetting current IP addresses");
println!("Getting current IP addresses...");
let cur_ip = api::get_current_ip().await?;
println!(" - {}, {}\n", cur_ip.ipv4, cur_ip.ipv6);
for k in 0..config.keys.len() {
println!("Updating zones for key {}", config.keys[k].auth_key);
for z in 0..config.keys[k].zones.len() {
Expand All @@ -56,55 +55,68 @@ async fn check_and_update_ip(config: &Config) -> Result<(), Box<dyn std::error::
"AAAA",
)
.await?;
for i in 0..config.keys[k].zones[z].a_records.len() {
if !a_record_ips.result.get(i).is_none() && !a_record_ips.result[i].locked {
if cur_ip.ipv4 != a_record_ips.result[i].content {
print!(
"Updating record {} from {} to {}",
a_record_ips.result[i].name,
a_record_ips.result[i].content,
cur_ip.ipv4
);
match api::update_record(
&a_record_ips.result[i],
&cur_ip.ipv4,
&config.keys[k].auth_key,
"A",
)
.await
{
Ok(()) => println!(" - Record updated"),
Err(e) => println!(" - Error: {}", e),
match cur_ip.ipv4.clone() {
Some(ipv4) => {
println!("\nCurrent IPv4 address: {}", ipv4);
for i in 0..config.keys[k].zones[z].a_records.len() {
if !a_record_ips.result.get(i).is_none() && !a_record_ips.result[i].locked {
if ipv4 != a_record_ips.result[i].content {
print!(
"Updating record {} from {} to {}",
a_record_ips.result[i].name,
a_record_ips.result[i].content,
ipv4
);
match api::update_record(
&a_record_ips.result[i],
&ipv4,
&config.keys[k].auth_key,
"A",
)
.await
{
Ok(()) => println!(" - Record updated"),
Err(e) => println!(" - Error: {}", e),
}
} else {
println!("Record {} is up to date", a_record_ips.result[i].name);
}
}
} else {
println!("Record {} is up to date", a_record_ips.result[i].name);
}
}
None => println!("No IPv4 address found, skipping A records"),
}
for i in 0..config.keys[k].zones[z].aaaa_records.len() {
if !aaaa_record_ips.result.get(i).is_none() && !aaaa_record_ips.result[i].locked {
if cur_ip.ipv6 != aaaa_record_ips.result[i].content {
print!(
"Updating record {} from {} to {}",
aaaa_record_ips.result[i].name,
aaaa_record_ips.result[i].content,
cur_ip.ipv6
);
match api::update_record(
&aaaa_record_ips.result[i],
&cur_ip.ipv6,
&config.keys[k].auth_key,
"AAAA",
)
.await
match cur_ip.ipv6.clone() {
Some(ipv6) => {
for i in 0..config.keys[k].zones[z].aaaa_records.len() {
if !aaaa_record_ips.result.get(i).is_none()
&& !aaaa_record_ips.result[i].locked
{
Ok(()) => println!(" - Record updated"),
Err(e) => println!(" - Error: {}", e),
if ipv6 != aaaa_record_ips.result[i].content {
print!(
"Updating record {} from {} to {}",
aaaa_record_ips.result[i].name,
aaaa_record_ips.result[i].content,
ipv6
);
match api::update_record(
&aaaa_record_ips.result[i],
&ipv6,
&config.keys[k].auth_key,
"AAAA",
)
.await
{
Ok(()) => println!(" - Record updated"),
Err(e) => println!(" - Error: {}", e),
}
} else {
println!("Record {} is up to date", aaaa_record_ips.result[i].name);
}
}
} else {
println!("Record {} is up to date", aaaa_record_ips.result[i].name);
}
}
None => println!("No IPv6 address found, skipping AAAA records"),
}
println!("Done updating zone")
}
Expand Down

0 comments on commit beeb817

Please sign in to comment.