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) {
Ok(response) => {
return if response.status().is_success() {
if response.status().is_success() {
let entries = match response.json::<CloudflareApiResults>() {
Ok(data) => data,
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<(), ()> {
let endpoint = format!("{}/zones/{}/dns_records/{}", API_BASE, self.id, id);
match r#type {
return match r#type {
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());
match self.put(&endpoint, &json_body) {
@ -125,7 +125,7 @@ impl CloudflareZone {
}
},
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());
match self.put(&endpoint, &json_body) {
@ -156,7 +156,7 @@ impl CloudflareZone {
true => warn!("[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<(), ()> {
let endpoint = format!("{}/zones/{}/dns_records", API_BASE, self.id);
match r#type {
return match r#type {
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());
match self.post(&endpoint, &json_body) {
@ -192,7 +192,7 @@ impl CloudflareZone {
}
},
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());
match self.post(&endpoint, &json_body) {
@ -223,7 +223,7 @@ impl CloudflareZone {
true => warn!("[WARN] {warn_msg}"),
false => println!("[WARN] {warn_msg}"),
}
return Err(())
Err(())
}
}
}
@ -275,16 +275,16 @@ impl CloudflareZone {
true => error!("[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();
body.insert("type".to_string(), r#type.to_string());
body.insert("name".to_string(), name.clone());
body.insert("content".to_string(), ip.to_string());
body.insert("type".to_owned(), r#type.to_owned());
body.insert("name".to_owned(), name.to_owned());
body.insert("content".to_owned(), ip.to_owned());
body
}
@ -303,14 +303,14 @@ impl CloudflareZone {
};
match data.success {
true => return Ok(()),
true => Ok(()),
false => {
let err_msg = format!("Unexpected error while updating DNS record. Info: {:?}", data);
match connected_to_journal() {
true => error!("[ERROR] {err_msg}"),
false => eprintln!("[ERROR] {err_msg}"),
}
return Err(())
Err(())
}
}
} else {
@ -325,6 +325,7 @@ impl CloudflareZone {
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Copy, Display, IntoStaticStr)]
#[allow(clippy::upper_case_acronyms)]
pub(crate) enum DnsRecordType {
A,
AAAA,

View file

@ -1,9 +1,8 @@
use std::collections::HashMap;
use std::error::Error;
use std::fs;
use std::hash::Hash;
use std::net::Ipv6Addr;
use ipnet::{IpAdd, IpBitAnd, IpBitOr, IpSub};
use ipnet::{IpBitOr, IpSub};
use std::str::FromStr;
use log::{error, warn};
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, ()> {
let host_range = Ipv6Addr::from(host_v6.saturating_sub(self.host_address));
let interface_address = match self.interfaces.get(interface_name) {
Some(address) => address.clone(),
Some(address) => *address,
None => {
let err_msg = "Malformed IP in interfaces.toml";
match connected_to_journal() {
@ -55,7 +54,7 @@ impl Default for InterfaceConfig {
fn default() -> Self {
InterfaceConfig {
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 {
fn default() -> Self {
ZoneEntry {
name: " ".to_string(),
name: " ".to_owned(),
r#type: vec![A, AAAA],
interface: " ".to_string(),
interface: " ".to_owned(),
}
}
}
@ -131,9 +130,9 @@ impl ZoneConfig {
impl Default for ZoneConfig {
fn default() -> Self {
ZoneConfig {
email: " ".to_string(),
name: " ".to_string(),
id: " ".to_string(),
email: " ".to_owned(),
name: " ".to_owned(),
id: " ".to_owned(),
entries: vec![ZoneEntry::default()],
}
}

View file

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