This commit is contained in:
Love 2024-07-14 16:58:22 +02:00
parent 4f3e689218
commit 2e2d723b2a
3 changed files with 36 additions and 18 deletions

View File

@ -4,19 +4,12 @@ use reqwest::Client;
use serde::{self, Deserialize, Serialize}; use serde::{self, Deserialize, Serialize};
use std::{ use std::{
collections::HashMap, collections::HashMap,
fmt,
net::{IpAddr, Ipv4Addr}, net::{IpAddr, Ipv4Addr},
}; };
use crate::get_current_public_ipv4; use crate::get_current_public_ipv4;
pub struct CloudflareClient {
client: Client,
domains: Vec<Box<str>>,
current_ip: Ipv4Addr,
api_key: Box<str>,
zone_id: Box<str>,
}
#[derive(Serialize, Deserialize, Clone, Debug)] #[derive(Serialize, Deserialize, Clone, Debug)]
struct DnsRecord { struct DnsRecord {
id: String, id: String,
@ -42,8 +35,24 @@ struct CloudflareResponse {
result: Option<Vec<DnsRecord>>, result: Option<Vec<DnsRecord>>,
} }
impl CloudflareClient { pub struct CloudflareClient<A, Z>
pub async fn new(api_key: Box<str>, zone_id: Box<str>, domains: Vec<Box<str>>) -> Result<Self> { where
A: fmt::Display,
Z: fmt::Display,
{
client: Client,
domains: Vec<Box<str>>,
current_ip: Ipv4Addr,
api_key: A,
zone_id: Z,
}
impl<Api, Zone> CloudflareClient<Api, Zone>
where
Api: fmt::Display,
Zone: fmt::Display,
{
pub async fn new(api_key: Api, zone_id: Zone, domains: Vec<Box<str>>) -> Result<Self> {
let force_ipv4 = IpAddr::from([0, 0, 0, 0]); let force_ipv4 = IpAddr::from([0, 0, 0, 0]);
let client = reqwest::ClientBuilder::new() let client = reqwest::ClientBuilder::new()
.local_address(force_ipv4) .local_address(force_ipv4)
@ -117,7 +126,7 @@ impl CloudflareClient {
Ok(()) Ok(())
} }
async fn get_dns_records(&self, domain: &str) -> Result<Vec<DnsRecord>> { async fn get_dns_records<D: fmt::Display>(&self, domain: &D) -> Result<Vec<DnsRecord>> {
let url = format!( let url = format!(
"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

View File

@ -2,7 +2,8 @@ use anyhow;
use dirs; use dirs;
use log::warn; use log::warn;
use serde::{self, Deserialize, Serialize}; use serde::{self, Deserialize, Serialize};
use std::{env, path::PathBuf}; use std::env;
use std::path::{Path, PathBuf};
use tokio::{fs, io::AsyncReadExt}; use tokio::{fs, io::AsyncReadExt};
use crate::PROGRAM_NAME; use crate::PROGRAM_NAME;
@ -58,8 +59,8 @@ pub async fn get_config_path() -> Result<PathBuf, Vec<PathBuf>> {
Err(tried_paths) Err(tried_paths)
} }
pub async fn read_config(path: &PathBuf) -> anyhow::Result<Config> { pub async fn read_config<P: AsRef<Path>>(path: &P) -> anyhow::Result<Config> {
let mut file = fs::File::open(&path).await?; let mut file = fs::File::open(path).await?;
let mut buf = String::new(); let mut buf = String::new();
file.read_to_string(&mut buf).await?; file.read_to_string(&mut buf).await?;
Ok(toml::from_str(&buf)?) Ok(toml::from_str(&buf)?)

View File

@ -6,14 +6,22 @@ use netlink_packet_route::RouteNetlinkMessage as RtnlMessage;
use crate::CloudflareClient; use crate::CloudflareClient;
pub struct MessageHandler<'a> { pub struct MessageHandler<'a, Api, Zone>
cloudflare: &'a mut CloudflareClient, where
Api: fmt::Display,
Zone: fmt::Display,
{
cloudflare: &'a mut CloudflareClient<Api, Zone>,
errs_counter: usize, errs_counter: usize,
errs_max: usize, errs_max: usize,
} }
impl<'a> MessageHandler<'a> { impl<'a, Api, Zone> MessageHandler<'a, Api, Zone>
pub fn new(cloudflare: &'a mut CloudflareClient, errs_max: usize) -> Self { where
Api: fmt::Display,
Zone: fmt::Display,
{
pub fn new(cloudflare: &'a mut CloudflareClient<Api, Zone>, errs_max: usize) -> Self {
Self { Self {
cloudflare, cloudflare,
errs_counter: 0, errs_counter: 0,