1.0.0 Rework #25

Merged
Neshura merged 25 commits from v2 into main 2023-12-27 20:26:16 +00:00
3 changed files with 28 additions and 29 deletions
Showing only changes of commit bd7514ea84 - Show all commits

View file

@ -60,7 +60,7 @@ impl CloudflareZone {
match self.get(&endpoint) { match self.get(&endpoint) {
Ok(response) => { Ok(response) => {
return if response.status().is_success() { if response.status().is_success() {
let entries = match response.json::<CloudflareApiResults>() { let entries = match response.json::<CloudflareApiResults>() {
Ok(data) => data, Ok(data) => data,
Err(e) => { Err(e) => {
@ -97,9 +97,9 @@ impl CloudflareZone {
pub(crate) fn update(&self, entry: &ZoneEntry, r#type: &DnsRecordType, id: &String, ipv6: Option<Ipv6Addr>, ipv4: Option<Ipv4Addr>) -> Result<(), ()> { pub(crate) fn update(&self, entry: &ZoneEntry, r#type: &DnsRecordType, id: &String, ipv6: Option<Ipv6Addr>, ipv4: Option<Ipv4Addr>) -> Result<(), ()> {
let endpoint = format!("{}/zones/{}/dns_records/{}", API_BASE, self.id, id); let endpoint = format!("{}/zones/{}/dns_records/{}", API_BASE, self.id, id);
match r#type { return match r#type {
DnsRecordType::A => { DnsRecordType::A => {
return if let Some(ip) = ipv4 { if let Some(ip) = ipv4 {
let json_body = self.create_body("A", &entry.name, ip.to_string().as_str()); let json_body = self.create_body("A", &entry.name, ip.to_string().as_str());
match self.put(&endpoint, &json_body) { match self.put(&endpoint, &json_body) {
@ -125,7 +125,7 @@ impl CloudflareZone {
} }
}, },
DnsRecordType::AAAA => { DnsRecordType::AAAA => {
return if let Some(ip) = ipv6 { if let Some(ip) = ipv6 {
let json_body = self.create_body("AAAA", &entry.name, ip.to_string().as_str()); let json_body = self.create_body("AAAA", &entry.name, ip.to_string().as_str());
match self.put(&endpoint, &json_body) { match self.put(&endpoint, &json_body) {
@ -156,7 +156,7 @@ impl CloudflareZone {
true => warn!("[WARN] {warn_msg}"), true => warn!("[WARN] {warn_msg}"),
false => println!("[WARN] {warn_msg}"), false => println!("[WARN] {warn_msg}"),
} }
return Err(()) Err(())
} }
} }
} }
@ -164,9 +164,9 @@ impl CloudflareZone {
pub(crate) fn create(&self, entry: &ZoneEntry, r#type: &DnsRecordType, ipv6: Option<Ipv6Addr>, ipv4: Option<Ipv4Addr>) -> Result<(), ()> { pub(crate) fn create(&self, entry: &ZoneEntry, r#type: &DnsRecordType, ipv6: Option<Ipv6Addr>, ipv4: Option<Ipv4Addr>) -> Result<(), ()> {
let endpoint = format!("{}/zones/{}/dns_records", API_BASE, self.id); let endpoint = format!("{}/zones/{}/dns_records", API_BASE, self.id);
match r#type { return match r#type {
DnsRecordType::A => { DnsRecordType::A => {
return if let Some(ip) = ipv4 { if let Some(ip) = ipv4 {
let json_body = self.create_body("A", &entry.name, ip.to_string().as_str()); let json_body = self.create_body("A", &entry.name, ip.to_string().as_str());
match self.post(&endpoint, &json_body) { match self.post(&endpoint, &json_body) {
@ -192,7 +192,7 @@ impl CloudflareZone {
} }
}, },
DnsRecordType::AAAA => { DnsRecordType::AAAA => {
return if let Some(ip) = ipv6 { if let Some(ip) = ipv6 {
let json_body = self.create_body("AAAA", &entry.name, ip.to_string().as_str()); let json_body = self.create_body("AAAA", &entry.name, ip.to_string().as_str());
match self.post(&endpoint, &json_body) { match self.post(&endpoint, &json_body) {
@ -223,7 +223,7 @@ impl CloudflareZone {
true => warn!("[WARN] {warn_msg}"), true => warn!("[WARN] {warn_msg}"),
false => println!("[WARN] {warn_msg}"), false => println!("[WARN] {warn_msg}"),
} }
return Err(()) Err(())
} }
} }
} }
@ -275,16 +275,16 @@ impl CloudflareZone {
true => error!("[ERROR] {err_msg}"), true => error!("[ERROR] {err_msg}"),
false => eprintln!("[ERROR] {err_msg}"), false => eprintln!("[ERROR] {err_msg}"),
} }
return Err(e) Err(e)
} }
} }
} }
fn create_body(&self, r#type: &str, name: &String, ip: &str) -> HashMap<String, String> { fn create_body(&self, r#type: &str, name: &str, ip: &str) -> HashMap<String, String> {
let mut body = HashMap::new(); let mut body = HashMap::new();
body.insert("type".to_string(), r#type.to_string()); body.insert("type".to_owned(), r#type.to_owned());
body.insert("name".to_string(), name.clone()); body.insert("name".to_owned(), name.to_owned());
body.insert("content".to_string(), ip.to_string()); body.insert("content".to_owned(), ip.to_owned());
body body
} }
@ -303,14 +303,14 @@ impl CloudflareZone {
}; };
match data.success { match data.success {
true => return Ok(()), true => Ok(()),
false => { false => {
let err_msg = format!("Unexpected error while updating DNS record. Info: {:?}", data); let err_msg = format!("Unexpected error while updating DNS record. Info: {:?}", data);
match connected_to_journal() { match connected_to_journal() {
true => error!("[ERROR] {err_msg}"), true => error!("[ERROR] {err_msg}"),
false => eprintln!("[ERROR] {err_msg}"), false => eprintln!("[ERROR] {err_msg}"),
} }
return Err(()) Err(())
} }
} }
} else { } else {
@ -325,6 +325,7 @@ impl CloudflareZone {
} }
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Copy, Display, IntoStaticStr)] #[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Copy, Display, IntoStaticStr)]
#[allow(clippy::upper_case_acronyms)]
pub(crate) enum DnsRecordType { pub(crate) enum DnsRecordType {
A, A,
AAAA, AAAA,

View file

@ -1,9 +1,8 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::error::Error; use std::error::Error;
use std::fs; use std::fs;
use std::hash::Hash;
use std::net::Ipv6Addr; use std::net::Ipv6Addr;
use ipnet::{IpAdd, IpBitAnd, IpBitOr, IpSub}; use ipnet::{IpBitOr, IpSub};
use std::str::FromStr; use std::str::FromStr;
use log::{error, warn}; use log::{error, warn};
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
@ -36,7 +35,7 @@ impl InterfaceConfig {
pub(crate) fn full_v6(&self, interface_name: &String, host_v6: Ipv6Addr) -> Result<Ipv6Addr, ()> { pub(crate) fn full_v6(&self, interface_name: &String, host_v6: Ipv6Addr) -> Result<Ipv6Addr, ()> {
let host_range = Ipv6Addr::from(host_v6.saturating_sub(self.host_address)); let host_range = Ipv6Addr::from(host_v6.saturating_sub(self.host_address));
let interface_address = match self.interfaces.get(interface_name) { let interface_address = match self.interfaces.get(interface_name) {
Some(address) => address.clone(), Some(address) => *address,
None => { None => {
let err_msg = "Malformed IP in interfaces.toml"; let err_msg = "Malformed IP in interfaces.toml";
match connected_to_journal() { match connected_to_journal() {
@ -55,7 +54,7 @@ impl Default for InterfaceConfig {
fn default() -> Self { fn default() -> Self {
InterfaceConfig { InterfaceConfig {
host_address: Ipv6Addr::from_str("::").expect("Malformed literal in code"), host_address: Ipv6Addr::from_str("::").expect("Malformed literal in code"),
interfaces: HashMap::from([(" ".to_string(), Ipv6Addr::from(0))]), interfaces: HashMap::from([(" ".to_owned(), Ipv6Addr::from(0))]),
} }
} }
} }
@ -72,9 +71,9 @@ pub(crate) struct ZoneEntry {
impl Default for ZoneEntry { impl Default for ZoneEntry {
fn default() -> Self { fn default() -> Self {
ZoneEntry { ZoneEntry {
name: " ".to_string(), name: " ".to_owned(),
r#type: vec![A, AAAA], r#type: vec![A, AAAA],
interface: " ".to_string(), interface: " ".to_owned(),
} }
} }
} }
@ -131,9 +130,9 @@ impl ZoneConfig {
impl Default for ZoneConfig { impl Default for ZoneConfig {
fn default() -> Self { fn default() -> Self {
ZoneConfig { ZoneConfig {
email: " ".to_string(), email: " ".to_owned(),
name: " ".to_string(), name: " ".to_owned(),
id: " ".to_string(), id: " ".to_owned(),
entries: vec![ZoneEntry::default()], entries: vec![ZoneEntry::default()],
} }
} }

View file

@ -1,6 +1,5 @@
/*use cloudflare_old::{Instance, CloudflareDnsType};*/ /*use cloudflare_old::{Instance, CloudflareDnsType};*/
use reqwest::blocking::get; use reqwest::blocking::get;
use serde_derive::{Deserialize, Serialize};
use std::{thread::{sleep}}; use std::{thread::{sleep}};
use std::error::Error; use std::error::Error;
use std::net::{Ipv4Addr, Ipv6Addr}; use std::net::{Ipv4Addr, Ipv6Addr};
@ -25,8 +24,8 @@ struct Addresses {
impl Addresses { impl Addresses {
fn new() -> Result<Self, Box<dyn Error>> { fn new() -> Result<Self, Box<dyn Error>> {
let mut ret = Self { let mut ret = Self {
ipv4_uri: "https://am.i.mullvad.net/ip".to_string(), ipv4_uri: "https://am.i.mullvad.net/ip".to_owned(),
ipv6_uri: "https://ipv6.am.i.mullvad.net/ip".to_string(), ipv6_uri: "https://ipv6.am.i.mullvad.net/ip".to_owned(),
ipv4: Ipv4Addr::new(0, 0, 0, 0), ipv4: Ipv4Addr::new(0, 0, 0, 0),
ipv6: Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0) ipv6: Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)
}; };
@ -200,7 +199,7 @@ fn main() {
if true { if true {
let mut error = false; let mut error = false;
for zone in &zone_cfgs { for zone in &zone_cfgs {
let cf_zone = match CloudflareZone::new(&zone) { let cf_zone = match CloudflareZone::new(zone) {
Ok(data) => data, Ok(data) => data,
Err(e) => { Err(e) => {
let err_msg = format!("Cloudflare Token likely not set. Error: {}", e); let err_msg = format!("Cloudflare Token likely not set. Error: {}", e);