Compare commits
No commits in common. "8bee3e48d3786216c78af2943455810f1b32ab48" and "f5d7e05e9ff54965336bcbf26a75bf1b1326eaec" have entirely different histories.
8bee3e48d3
...
f5d7e05e9f
@ -34,14 +34,6 @@ struct DnsRecord {
|
|||||||
meta: HashMap<Box<str>, serde_json::Value>,
|
meta: HashMap<Box<str>, serde_json::Value>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
struct CloudflareResponse {
|
|
||||||
success: bool,
|
|
||||||
errors: Vec<HashMap<String, serde_json::Value>>,
|
|
||||||
messages: Vec<HashMap<String, serde_json::Value>>,
|
|
||||||
result: Option<Vec<DnsRecord>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CloudflareClient {
|
impl CloudflareClient {
|
||||||
pub async fn new(api_key: Box<str>, zone_id: Box<str>, domains: Vec<Box<str>>) -> Result<Self> {
|
pub async fn new(api_key: Box<str>, zone_id: Box<str>, domains: Vec<Box<str>>) -> Result<Self> {
|
||||||
let force_ipv4 = IpAddr::from([0, 0, 0, 0]);
|
let force_ipv4 = IpAddr::from([0, 0, 0, 0]);
|
||||||
@ -65,11 +57,10 @@ impl CloudflareClient {
|
|||||||
pub async fn check(&mut self) -> Result<()> {
|
pub async fn check(&mut self) -> Result<()> {
|
||||||
let new_ip = get_current_public_ipv4(&self.client).await?;
|
let new_ip = get_current_public_ipv4(&self.client).await?;
|
||||||
if new_ip == self.current_ip {
|
if new_ip == self.current_ip {
|
||||||
info!("IP '{}' is already set", new_ip);
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
info!(
|
info!(
|
||||||
"IP has changed from '{}' -> '{}'",
|
"Ip has changed from '{}' -> '{}'",
|
||||||
&self.current_ip, &new_ip
|
&self.current_ip, &new_ip
|
||||||
);
|
);
|
||||||
self.update_dns_records(new_ip).await?;
|
self.update_dns_records(new_ip).await?;
|
||||||
@ -84,7 +75,7 @@ impl CloudflareClient {
|
|||||||
Ok(r) => r,
|
Ok(r) => r,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!(
|
error!(
|
||||||
"Could not fetch DNS records for domain '{}': {:?}",
|
"Could not getch dns records for domain '{}': {:?}",
|
||||||
&domain, &e
|
&domain, &e
|
||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
@ -94,10 +85,6 @@ impl CloudflareClient {
|
|||||||
let new_ip_s = new_ip.to_string().into_boxed_str();
|
let new_ip_s = new_ip.to_string().into_boxed_str();
|
||||||
for mut record in records.into_iter() {
|
for mut record in records.into_iter() {
|
||||||
if record.content == new_ip_s {
|
if record.content == new_ip_s {
|
||||||
info!(
|
|
||||||
"On {}, ip already updated to '{}'",
|
|
||||||
&record.name, &record.content
|
|
||||||
);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
info!(
|
info!(
|
||||||
@ -122,8 +109,7 @@ impl CloudflareClient {
|
|||||||
"https://api.cloudflare.com/client/v4/zones/{}/dns_records?type=A&name={}",
|
"https://api.cloudflare.com/client/v4/zones/{}/dns_records?type=A&name={}",
|
||||||
self.zone_id, domain
|
self.zone_id, domain
|
||||||
);
|
);
|
||||||
|
let mut response = self
|
||||||
let response = self
|
|
||||||
.client
|
.client
|
||||||
.get(&url)
|
.get(&url)
|
||||||
.header("Authorization", format!("Bearer {}", self.api_key))
|
.header("Authorization", format!("Bearer {}", self.api_key))
|
||||||
@ -131,23 +117,12 @@ impl CloudflareClient {
|
|||||||
.send()
|
.send()
|
||||||
.await?
|
.await?
|
||||||
.error_for_status()?
|
.error_for_status()?
|
||||||
.text()
|
.json::<HashMap<String, Vec<DnsRecord>>>()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let cloudflare_response: CloudflareResponse =
|
response
|
||||||
serde_json::from_str(&response).context("Failed to deserialize Cloudflare response")?;
|
.remove("result")
|
||||||
|
.context("Key result not in return")
|
||||||
if !cloudflare_response.success {
|
|
||||||
error!(
|
|
||||||
"Cloudflare API returned errors: {:?}",
|
|
||||||
cloudflare_response.errors
|
|
||||||
);
|
|
||||||
return Err(anyhow::anyhow!("Cloudflare API returned errors"));
|
|
||||||
}
|
|
||||||
|
|
||||||
cloudflare_response
|
|
||||||
.result
|
|
||||||
.context("Key 'result' not found in response")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_dns_record(&self, record: &mut DnsRecord, ip_content: Box<str>) -> Result<()> {
|
async fn update_dns_record(&self, record: &mut DnsRecord, ip_content: Box<str>) -> Result<()> {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use log::{debug, error, info};
|
use log::{debug, error, info, log_enabled, Level};
|
||||||
use netlink_packet_core::{NetlinkMessage, NetlinkPayload};
|
use netlink_packet_core::{NetlinkMessage, NetlinkPayload};
|
||||||
use netlink_packet_route::RouteNetlinkMessage as RtnlMessage;
|
use netlink_packet_route::RouteNetlinkMessage as RtnlMessage;
|
||||||
|
|
||||||
@ -38,7 +38,9 @@ impl<'a> MessageHandler<'a> {
|
|||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
if log_enabled!(Level::Debug) {
|
||||||
debug!("Unhandled message payload: {:?}", message.payload);
|
debug!("Unhandled message payload: {:?}", message.payload);
|
||||||
|
}
|
||||||
Some(())
|
Some(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,8 +51,10 @@ impl<'a> MessageHandler<'a> {
|
|||||||
D: fmt::Display + ?Sized,
|
D: fmt::Display + ?Sized,
|
||||||
M: fmt::Debug,
|
M: fmt::Debug,
|
||||||
{
|
{
|
||||||
info!("{}", log_msg);
|
if log_enabled!(Level::Debug) {
|
||||||
debug!("{}: {:?}", log_msg, msg);
|
debug!("{}: {:?}", log_msg, msg);
|
||||||
|
} else {
|
||||||
|
info!("{}", log_msg);
|
||||||
if let Err(e) = self.cloudflare.check().await {
|
if let Err(e) = self.cloudflare.check().await {
|
||||||
self.errs_counter += 1;
|
self.errs_counter += 1;
|
||||||
error!(
|
error!(
|
||||||
@ -61,12 +65,16 @@ impl<'a> MessageHandler<'a> {
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Some(())
|
Some(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn log_info<M: fmt::Debug>(&self, log_msg: &str, msg: &M) -> Option<()> {
|
async fn log_info<M: fmt::Debug>(&self, log_msg: &str, msg: &M) -> Option<()> {
|
||||||
info!("{}", log_msg);
|
if log_enabled!(Level::Debug) {
|
||||||
debug!("{:?} message: {:?}", log_msg, msg);
|
debug!("{:?} message: {:?}", log_msg, msg);
|
||||||
|
} else {
|
||||||
|
info!("{}", log_msg);
|
||||||
|
}
|
||||||
Some(())
|
Some(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user