add main loop, improve errorhandling

This commit is contained in:
Tim Wundenberg
2023-06-23 18:56:50 +02:00
parent 3607826c39
commit 3f867d194b
4 changed files with 206 additions and 148 deletions

View File

@@ -4,30 +4,54 @@ mod public_ip;
#[macro_use]
extern crate dotenv_codegen;
use std::{thread::sleep, time::Duration};
use log::{error, info};
use public_ip::get_public_ip_address;
use reqwest::header::{AUTHORIZATION, CONTENT_TYPE};
use serde_json::Value;
use crate::errors::Error;
fn main() {
env_logger::Builder::from_default_env()
.target(env_logger::Target::Stdout)
.filter_level(log::LevelFilter::Info)
.init();
let token = dotenv!("AUTH_BEARER");
let zone_id = dotenv!("ZONE_ID");
let domain = dotenv!("DOMAIN");
let (id, content) = get_id_and_content_of_dns(token, zone_id, domain);
match get_public_ip_address() {
Ok(current_ip_address) => {
if content != current_ip_address {
update_ip_address(token, zone_id, &current_ip_address, domain, &id);
} else {
info!("Not updating IP Address");
}
loop {
let result = update(token, zone_id, domain);
match result {
Ok(_) => (),
Err(err) => error!("{}", err.message),
}
Err(err) => error!("{}", err.message),
sleep(Duration::from_secs(5 * 60));
}
}
fn get_id_and_content_of_dns(token: &str, zone_id: &str, domain: &str) -> (String, String) {
fn update(token: &str, zone_id: &str, domain: &str) -> Result<(), Error> {
let (id, content) = get_id_and_content_of_dns(token, zone_id, domain)?;
let current_ip_address = get_public_ip_address()?;
if content == current_ip_address {
info!("Not updating IP Address");
} else {
update_ip_address(token, zone_id, &current_ip_address, domain, &id)?;
}
Ok(())
}
fn get_id_and_content_of_dns(
token: &str,
zone_id: &str,
domain: &str,
) -> Result<(String, String), Error> {
let client = reqwest::blocking::Client::new();
let resp = client
@@ -36,20 +60,34 @@ fn get_id_and_content_of_dns(token: &str, zone_id: &str, domain: &str) -> (Strin
))
.header(AUTHORIZATION, format!("Bearer {token}",))
.send()
.unwrap();
.map_err(|err| Error::new(err.to_string().as_str()))?;
let content = resp.text().unwrap();
let content = resp
.text()
.map_err(|err| Error::new(err.to_string().as_str()))?;
let json: Value = serde_json::from_str(&content).unwrap();
let json: Value =
serde_json::from_str(&content).map_err(|err| Error::new(err.to_string().as_str()))?;
let array = json["result"].clone();
let array = array.as_array().unwrap();
let array = array
.as_array()
.ok_or(Error::new("Could not parse array"))?;
for value in array {
if value["name"] == domain {
return (
String::from(value["id"].as_str().unwrap()),
String::from(value["content"].as_str().unwrap()),
);
return Ok((
String::from(
value["id"]
.as_str()
.ok_or(Error::new("Could not parse id"))?,
),
String::from(
value["content"]
.as_str()
.ok_or(Error::new("Could not parse content"))?,
),
));
}
}
@@ -62,9 +100,9 @@ fn update_ip_address(
current_ip_address: &str,
domain: &str,
dns_id: &str,
) {
) -> Result<(), Error> {
let client = reqwest::blocking::Client::new();
let resp = client
client
.put(format!(
"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{dns_id}"
))
@@ -78,11 +116,9 @@ fn update_ip_address(
}}"
))
.send()
.unwrap();
.map_err(|err| Error::new(err.to_string().as_str()))?;
println!("{}", resp.text().unwrap());
info!("Successfully updated IP Address");
// if (resp.status()) != 200 {
// panic!()
// }
Ok(())
}