Merge branch 'host-agnostic-rework' into 'main'

Merge Rust rework into Main

See merge request Neshura/cloudflare-dns-updater!4
This commit is contained in:
Neshura 2023-03-01 02:21:26 +00:00
commit 78524042cc
13 changed files with 1905 additions and 150 deletions

2
.gitignore vendored
View file

@ -1,4 +1,6 @@
# Default ignores
target/
venv/
.idea/
.vscode/
api-key.json

60
.gitlab-ci.yml Normal file
View file

@ -0,0 +1,60 @@
image: 3.10.8-slim-buster
.deploy:
rules:
# Regex magic copied from Neshura/page-test, only deploys on x.y.z or higher (x.y) Tags
- if: $CI_COMMIT_TAG =~ /^((([\d])+\.){1,2}[\d]+)\s*$/ && $CI_COMMIT_TAG
stages:
- build
- release
## Docker steps
build:
image: rust:bullseye
stage: build
variables:
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_BRANCH
CACHING:
script:
- echo "Compiling the code..."
- cargo build -r
- echo "Compile complete."
after_script:
- echo JOB_ID=$CI_JOB_ID >> job.env
- mkdir ./artifacts
- cp /builds/Neshura/cloudflare-dns-updater/target/release/cloudflare-dns-updater ./artifacts/
- cp /builds/Neshura/cloudflare-dns-updater/config.json ./artifacts/
- cp /builds/Neshura/cloudflare-dns-updater/api-key-empty.json ./artifacts/api-key.json
artifacts:
paths:
- ./artifacts/
reports:
dotenv: job.env
rules:
- !reference [.deploy, rules]
Tag Release:
needs:
- job: build
artifacts: true
stage: release
image: registry.gitlab.com/gitlab-org/release-cli:latest
rules:
- !reference [.deploy, rules]
script:
- echo "running Release Job, attaching Artifact from Job $JOB_ID"
release:
tag_name: '$CI_COMMIT_TAG'
description: '$CI_COMMIT_TAG'
assets:
links:
- name: "cloudflare-dns-updater.zip"
url: "https://gitlab.neshweb.net/Neshura/cloudflare-dns-updater/-/jobs/$JOB_ID/artifacts/download"

1199
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

14
Cargo.toml Normal file
View file

@ -0,0 +1,14 @@
[package]
name = "cloudflare-dns-updater"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = "0.4.23"
reqwest = { version = "0.11.14", features = ["blocking", "json"] }
serde = "1.0.152"
serde_derive = "1.0.152"
serde_json = "1.0.93"
strum_macros = "0.24.3"

View file

@ -1,21 +0,0 @@
# Generate iamge using Python base
FROM python:3.10.8-slim-buster
WORKDIR /usr/bin/app
# install required tools
RUN apt update && apt install -y curl
# Copy only the required files over
COPY set_ip.sh cloudflare_script.py config.ini cloudflare.json requirements.txt ./
RUN addgroup --system --gid 1001 pygroup
RUN adduser --system --uid 1001 pyapp
RUN chown -R pyapp:pygroup /usr/bin/app
USER pyapp
RUN pip install -r requirements.txt
CMD ["bash", "set_ip.sh", "docker"]

3
api-key-empty.json Normal file
View file

@ -0,0 +1,3 @@
{
"api_key": ""
}

View file

@ -1,100 +0,0 @@
import sys
import ipaddress
from configparser import ConfigParser
import json
import copy
import CloudFlare
class DNSUpdater:
def __init__(self, args):
# Parse config
config = ConfigParser()
config.read('config.ini')
self.HOSTNAME = config['server']['HOSTNAME']
__TOKEN = config['cloudflare']['TOKEN']
# Parse args
try:
ipversion = args[1].lstrip('-')
ipaddress_new = args[2]
except IndexError:
ipaddress_new = ''
ipversion = 'skip'
match ipversion:
case '4': self.IPVER = 'ipv4'
case '6': self.IPVER = 'ipv6'
case 'skip': self.IPVER = ipversion
case other: raise ValueError(f"Wrong IP Address version given: {ipversion}")
self.IPNEW = ipaddress_new
# Cloudflare class
self._cloudflare = CloudFlare.CloudFlare(token=__TOKEN)
with open('cloudflare.json', 'r') as file:
self.dns_replacements = json.load(file)
def get_zones(self):
# Cloudflare get zones from API token
zones = self._cloudflare.zones.get()
for zone in zones:
self.zone_id = zone['id']
self.zone_name = zone['name']
print(f"Zone ID: {zone['id']} | Zone Name: {zone['name']}")
def get_dns_records(self, log=False):
dns_records_all = self._cloudflare.zones.dns_records.get(self.zone_id)
dns_records_to_update = {'A' : [], 'AAAA' : []}
for dnsrecord in dns_records_all:
if dnsrecord['type'] in ['A', 'AAAA']:
if log:
print(f"DNS Name: {dnsrecord['name']} | Type : {dnsrecord['type']} | IP: {dnsrecord['content']}")
dns_records_to_update[dnsrecord['type']].append(dnsrecord)
else:
if log:
print(f"| Unknow DNS | Type: {dnsrecord['type']} | Zone: {dnsrecord['name']}")
self.DNSRECORDS = copy.deepcopy(dns_records_to_update)
def update_records(self):
if self.IPVER == 'skip':
self.get_dns_records(log=True)
return
self.get_dns_records()
ipinformation = {
'ipv4': [ipaddress.IPv4Address, 'A'],
'ipv6': [ipaddress.IPv6Address, 'AAAA']
}
print(f"Updating IP of version {self.IPVER}")
assert isinstance(ipaddress.ip_address(self.IPNEW), ipinformation[self.IPVER][0]), f'IP {self.IPNEW} is not a valid IPv6 address'
dnstype = ipinformation[self.IPVER][1]
names_replace = self.dns_replacements[dnstype]
print(names_replace)
for name in names_replace:
if (name == "@"):
fullname = self.HOSTNAME
else:
fullname = f"{name}.{self.HOSTNAME}"
for record in self.DNSRECORDS[dnstype]:
if record['name'] == fullname:
new_dnsrecord = {"name": fullname, "type": dnstype, "content": self.IPNEW}
print(f"Sending request {new_dnsrecord}")
res = self._cloudflare.zones.dns_records.patch(self.zone_id, record['id'], data=new_dnsrecord)
if res['content'] != new_dnsrecord['content']:
print(res)
if __name__ == '__main__':
dnsupdate = DNSUpdater(sys.argv)
dnsupdate.get_zones()
dnsupdate.update_records()

View file

@ -1,5 +0,0 @@
[cloudflare]
TOKEN=w5qP_P47q_G8i55e3OvwuDiuyZXPFU5aXcrO3hnN
[server]
HOSTNAME=neshura-server.net

38
config.json Normal file
View file

@ -0,0 +1,38 @@
{
"ipv6_interface": ":da5e:d3ff:feeb:4346",
"zones": [
{
"email": "neshura@proton.me",
"name": "neshura.net",
"id": "0183f167a051f1e432c0d931478638b5",
"dns_entries": [
{
"name": "*.neshura.net",
"type4": true,
"type6": true,
"interface": ":da5e:d3ff:feeb:4346"
},
{
"name": "neshura.net",
"type4": true,
"type6": true,
"interface": ":da5e:d3ff:feeb:4346"
}
]
},
{
"email": "neshura@proton.me",
"name": "neshura-server.net",
"id": "146d4cd6a1777376b423aaedc6824818",
"dns_entries": [
]
},
{
"email": "neshura@proton.me",
"name": "neshweb.net",
"id": "75b0d52229357478b734ae0f6d075c15",
"dns_entries": [
]
}
]
}

View file

@ -1 +0,0 @@
cloudflare >= 2.0.0

View file

@ -1,23 +0,0 @@
#!/bin/bash
if [ "$1" = "docker" ]; then
cd "/usr/bin/app" || exit
while :
do
clear
ipv4=$(curl -k -s https://am.i.mullvad.net/ip)
ipv6=$(curl -k -s https://ipv6.am.i.mullvad.net/)
python3 ./cloudflare_script.py 4 "$ipv4"
python3 ./cloudflare_script.py 6 "$ipv6"
sleep 120
done
else
if [ ! -e cloudflare.json ] || [ ! -e config.ini ]; then
echo "Cloudflare config not found, is the script run in the correct directory?"
else
ipv4=$(curl -k -s https://am.i.mullvad.net/ip -4)
ipv6=$(curl -k -s https://ipv6.am.i.mullvad.net/)
source venv/bin/activate
python3 ./cloudflare_script.py 4 "$ipv4"
python3 ./cloudflare_script.py 6 "$ipv6"
fi
fi

224
src/cloudflare/mod.rs Normal file
View file

@ -0,0 +1,224 @@
use reqwest::{blocking::{Response, Client}, Url};
use serde_derive::{Deserialize, Serialize};
use reqwest::{Error, header::{HeaderMap, HeaderValue}};
use std::{string::String, collections::HashMap};
use strum_macros::{Display, IntoStaticStr};
const API_BASE: &str = "https://api.cloudflare.com/client/v4/";
#[derive(Serialize, Deserialize)]
struct CloudflareDnsZone {
result: Vec<CloudflareDnsEntry>,
success: bool
}
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Copy)]
#[derive(Display, IntoStaticStr)]
pub enum CloudflareDnsType {
#[strum(serialize = "A")]
A = 4,
#[strum(serialize = "AAAA")]
AAAA = 6,
TXT,
MX
}
#[derive(Serialize, Deserialize)]
pub struct CloudflareDnsEntry {
id: String,
pub zone_id: String,
pub name: String,
pub r#type: CloudflareDnsType,
content: String,
proxied: bool
}
impl CloudflareDnsEntry {
pub fn is_equal(&self, other_entry: &String) -> bool {
return if &self.name == other_entry { true } else { false };
}
pub fn is_ip_new(&self, other_entry: &String) -> bool {
return if &self.content == other_entry { false } else { true };
}
}
pub(crate) struct Instance {
api_key: String,
pub dns_entries: Vec<CloudflareDnsEntry>,
dns_email: String
}
impl Instance {
pub fn new(key: &String) -> Instance {
Instance {
api_key: key.clone(),
dns_entries: Vec::new(),
dns_email: "".to_string()
}
}
/// Loads all DNS entires in a Zone as a [`CloudflareDnsEntry`] Vector
///
/// * `email` : E-Mail Address associated with the Zone, required for [`generate_headers()`]
/// * `zone_id` : Zone to be used for the Request
/// * `return` : Returns true if a recoverable Error was encountered, true otherwise
pub fn load_entries(&mut self, email: &String, zone_id: &String) -> Result<(), Error> {
self.dns_email = email.clone();
let endpoint = format!("{}zones/{}/dns_records", API_BASE, zone_id);
match self.get(&endpoint) {
Ok(response) => {
let entries = match response
.json::<CloudflareDnsZone>() {
Ok(data) => {data},
Err(e) => panic!("{:#?}", e)
};
for entry in entries.result {
self.dns_entries.append(&mut vec!(entry));
}
return Ok(())
},
Err(e) => { return Err(e) }
}
}
/// Shorthand for Cloudflare API Requests
///
/// Automatically appends Auth Headers and parses the URL
///
/// * `url` : URL for the request, will be parsed to [`Url`]
fn get(&self, url: &str) -> Result<Response, Error> {
let url_parsed = match Url::parse(url) {
Ok(url) => {url},
Err(e) => panic!("{:#?}", e) // If this call fails the API changed
}; // Continuing without a code update is not possible
let result = match Client::new()
.get(url_parsed)
.headers(self.generate_headers())
.send() {
Ok(res) => { Ok(res) },
Err(e) => { Err(e) }
};
return result;
}
/// Shorthand for Cloudflare API Requests
///
/// Automatically appends Auth Headers and parses the URL
///
/// * `url` : URL for the request, will be parsed to [`Url`]
/// * `data` : PUT request body
fn put(&self, url: &str, data: &HashMap<&str, &str>) -> Result<Response, Error> {
let url_parsed = match Url::parse(url) {
Ok(url) => {url},
Err(e) => panic!("{:#?}", e)// If this call fails the API changed
}; // Continuing without a code update is not possible
let result = match Client::new()
.put(url_parsed)
.headers(self.generate_headers())
.json(data)
.send() {
Ok(res) => { Ok(res) },
Err(e) => { Err(e) }
};
return result;
}
/// Shorthand for Cloudflare API Requests
///
/// Automatically appends Auth Headers and parses the URL
///
/// * `url` : URL for the request, will be parsed to [`Url`]
/// * `data` : POST request body
fn post(&self, url: &str, data: &HashMap<&str, &str>) -> Result<Response, Error> {
let url_parsed = match Url::parse(url) {
Ok(url) => {url},
Err(e) => panic!("{:#?}", e) // If this call fails the API changed
}; // Continuing without a code update is not possible
let result = match Client::new()
.post(url_parsed)
.headers(self.generate_headers())
.json(data)
.send() {
Ok(res) => { Ok(res) },
Err(e) => { Err(e) }
};
return result;
}
/// Updates the given DNS entry with the given IP
///
/// * `entry` : DNS entry to update
/// * `new_ip` : IP used to Update the DNS entry, can be IPv4 or IPv6
/// * `return1` : Returns success of the API call, extracted from the HTTP Response body
pub fn update_entry(&self, entry: &CloudflareDnsEntry, new_ip: &String) -> Result<bool, Error> {
let endpoint = format!("{}zones/{}/dns_records/{}", API_BASE, entry.zone_id, entry.id);
let mut json_body = HashMap::new();
json_body.insert("type", entry.r#type.clone().into());
json_body.insert("name", entry.name.as_ref());
json_body.insert("content", new_ip);
json_body.insert("ttl", "1");
match self.put(&endpoint, &json_body) {
Ok(response) => {
let data = response
.json::<serde_json::Value>()
.expect("Response should always be valid JSON");
let success = data["success"]
.as_bool()
.expect("JSON should always include success bool");
return Ok(success);
},
Err(e) => { return Err(e) },
}
}
pub fn create_entry(&self, zone_id: &String, r#type: &str, name: &str, ip: &String) -> Result<bool, Error> {
let endpoint = format!("{}zones/{}/dns_records", API_BASE, zone_id);
let mut json_body = HashMap::new();
json_body.insert("type", r#type);
json_body.insert("name", name);
json_body.insert("content", ip);
json_body.insert("ttl", "1");
match self.post(&endpoint, &json_body) {
Ok(response) => {
let data = response
.json::<serde_json::Value>()
.expect("Response should always be valid JSON");
let success = data["success"]
.as_bool()
.expect("JSON should always include success bool");
return Ok(success)
},
Err(e) => { return Err(e) },
}
}
/// Generate Cloudflare API v4 Auth Headers
fn generate_headers(&self) -> HeaderMap {
let mut headers = HeaderMap::new();
headers.insert("X-Auth-Key", HeaderValue::from_str(self.api_key.as_ref()).unwrap());
headers.insert("X-Auth-Email", HeaderValue::from_str(self.dns_email.as_ref()).unwrap());
headers
}
}

365
src/main.rs Normal file
View file

@ -0,0 +1,365 @@
use cloudflare::{Instance, CloudflareDnsType};
use reqwest::blocking::get;
use serde_derive::{Deserialize, Serialize};
use std::{fs, thread::{sleep}};
use chrono::{Utc, Duration};
mod cloudflare;
fn default_key() -> String {
return "".to_string();
}
#[derive(Clone)]
struct ChangeTracker {
created: u16,
updated: u16,
unchanged: u16,
error: u16
}
impl ChangeTracker {
fn new() -> ChangeTracker {
ChangeTracker {
created: 0,
updated: 0,
unchanged: 0,
error: 0
}
}
fn print(&self, f: &str) {
match f {
"" => {
self.print("simple");
}
"simple" => {
println!("{} created", self.created);
println!("{} updated", self.updated);
println!("{} unchanged", self.unchanged);
println!("{} errors", self.error);
}
&_ => {
println!("unknown format pattern '{}', defaulting to 'simple'", f);
self.print("simple");
}
}
}
}
#[derive(Serialize, Deserialize)]
struct DnsEntry {
name: String,
type4: bool,
type6: bool,
interface: Option<String>,
}
impl DnsEntry {
/// Updates a single DNS Entry
///
/// * `change4` : IPv4 DNS change tracker, used to print details about the results of the update run after it concludes
/// * `change6` : Same as `change4` but for IPv6
/// * `ips` : Enum of current IPv4 and IPv6 base, used to check against and update the DNS record
/// * `agent` : Cloudflare Auth Agent, provides the needed Authentication headers for the API Calls
/// * `zone_id` : Cloudfalre ID of the Zone this DNS entry resides in, needed for the API Calls
///
/// * `return` : Returns true if the DNS Update failed due to an Error, otherwise returns false
fn update(
&self,
change4: &mut ChangeTracker,
change6: &mut ChangeTracker,
ips: &Ips,
agent: &Instance,
zone_id: &String
) -> bool {
let mut found4 = false;
let mut found6 = false;
// Loops over every Entry in the List fetched from the Cloudflare API
// If the entry is in this List we need to update it, if not it needs to be created
for cloudflare_entry in &agent.dns_entries {
if cloudflare_entry.is_equal(&self.name) {
// Update IPv4 Entry
if cloudflare_entry.r#type == CloudflareDnsType::A && self.type4 {
found4 = true;
if cloudflare_entry.is_ip_new(&ips.ipv4) {
match agent.update_entry(cloudflare_entry, &ips.ipv4) {
Ok(success) => {
if success {
change4.updated += 1;
}
else {
change4.error += 1;
}
},
Err(_e) => { return true } // if an error is detected any subsequent calls are likely to fail as well
};
}
else {
change4.unchanged += 1;
}
}
// Update IPv6 Entry
else if cloudflare_entry.r#type == CloudflareDnsType::AAAA && self.type6 {
found6 = true;
let ipv6 = ips.ipv6base.clone() + self.interface.as_ref().unwrap();
if cloudflare_entry.is_ip_new(&ipv6) {
match agent.update_entry(cloudflare_entry, &ipv6) {
Ok(success) => {
if success {
change6.updated += 1
}
else {
change6.error += 1
}
},
Err(_e) => { return true } // if an error is detected any subsequent calls are likely to fail as well
};
}
else {
change6.unchanged += 1;
}
}
// ignore otherwise
else {}
}
}
if !found4 && self.type4 {
match agent.create_entry(zone_id, "A", &self.name, &ips.ipv4) {
Ok(success) => {
if success {
change4.created += 1;
}
else {
change4.error += 1;
};
},
Err(_e) => { return true } // early return since if an error is detected any subsequent calls are likely to fail as well
}
}
if !found6 && self.type6 {
let ipv6 = ips.ipv6base.clone() + self.interface.as_ref().unwrap();
match agent.create_entry(zone_id, "AAAA", &self.name, &ipv6) {
Ok(success) => {
if success {
change6.created += 1;
}
else {
change6.error += 1;
};
},
Err(_e) => { return true } // if an error is detected any subsequent calls are likely to fail as well
};
}
return false; // If this point is reached no error has been caught therefore returning false
}
}
#[derive(Serialize, Deserialize)]
struct DnsZone {
email: String,
name: String,
id: String,
dns_entries: Vec<DnsEntry>,
}
impl DnsZone {
/// Attempts to Update the DNS Entries of the Zone with a new IP
///
/// * `change4` : IPv4 DNS change tracker, used to print details about the results of the update run after it concludes
/// * `change6` : Same as `change4` but for IPv6
/// * `ips` : Enum of current IPv4 and IPv6 base, used to check against and update the DNS record
/// * `key` : API Key needed to create the Auth Agent for the API
///
/// * `return` : Returns true if the DNS Updates failed due to an Error, false otherwise
fn update(
&self,
change4: &mut ChangeTracker,
change6: &mut ChangeTracker,
ips: &Ips,
key: &String
) -> bool {
let mut agent = Instance::new(key);
match agent.load_entries(&self.email, &self.id) {
Ok(_) => {
let mut results = Vec::<bool>::new();
for entry in &self.dns_entries {
let result = entry.update(change4, change6, &ips, &agent, &self.id);
results.append(&mut vec!(result));
if result {
break; // if one update fails all others are likely to fail as well
}
}
let result = results.contains(&true);
return result;
},
Err(_e) => { return true }, // Early return since an Error likely means subsequent calls will also fail
}
}
}
#[derive(Serialize, Deserialize)]
struct DnsConfig {
ipv6_interface: String,
#[serde(default = "default_key")]
api_key: String,
zones: Vec<DnsZone>,
}
impl DnsConfig {
fn new() -> DnsConfig {
DnsConfig {
ipv6_interface: "".to_string(),
api_key: "".to_string(),
zones: Vec::new(),
}
}
fn load(&mut self) {
let file_contents = match fs::read_to_string("config.json") {
Ok(data) => data,
Err(e) => panic!("{:#?}", e),
};
let config_parse: DnsConfig = match serde_json::from_str(&file_contents) {
Ok(data) => data,
Err(e) => panic!("{:#?}", e),
};
let key_file_contents = match fs::read_to_string("api-key.json") {
Ok(data) => data,
Err(e) => panic!("{:#?}", e),
};
let api_key_parse: serde_json::Value = match serde_json::from_str(&key_file_contents) {
Ok(data) => data,
Err(e) => panic!("{:#?}", e),
};
*self = config_parse;
self.api_key = api_key_parse["api_key"].as_str().unwrap().to_owned();
}
}
struct Ips {
ipv4: String,
ipv6base: String,
}
impl Ips {
fn new() -> Ips {
Ips {
ipv4: "0.0.0.0".to_string(),
ipv6base: ":".to_string(),
}
}
/// Get the current IPv4 address and IPv6 prefix from Mullvad API
///
/// Returns true if the get succeeded and false if it failed.
/// The IPv6 Prefix is generated by removing the IPv6 Interface from the fetched IPv6 address
fn get(&mut self, ipv6_interface: &str) -> bool {
let ipv4uri = "https://am.i.mullvad.net/ip";
let ipv6uri = "https://ipv6.am.i.mullvad.net/ip";
let mut ret;
match get(ipv4uri) {
Ok(data) => {
self.ipv4 = data.text().expect("0.0.0.0").trim_end().to_owned();
ret = true;
},
Err(_e) => {
println!("Could not fetch IPv4");
ret = false;
return ret; // Early return since a fail on IPv4 means a likely fail on IPv6
}
}
match get(ipv6uri) {
Ok(data) => {
let ipv6 = match data.text() {
Ok(ip) => {ip},
Err(_) => {panic!("Expected IP, found none")},
};
let stripped = match ipv6.trim_end().strip_suffix(ipv6_interface) {
Some(string) => string.to_string(),
None => ":".to_string(),
};
self.ipv6base = stripped;
ret = true && ret; // Only set ret to true if previous ret is also true
}
Err(_e) => {
println!("Could not fetch IPv6");
ret = false;
}
}
return ret;
}
}
/// Loops over all DNS Entries in the config and calls an update on them
fn update_dns() {
let mut config = DnsConfig::new();
config.load();
let mut ips = Ips::new();
if ips.get(&config.ipv6_interface) {
println!("Current IPv4: {}", ips.ipv4);
println!("Current IPv6: {}{}", ips.ipv6base, &config.ipv6_interface);
let mut change4 = ChangeTracker::new();
let mut change6 = ChangeTracker::new();
let mut update_results = Vec::<bool>::new();
// Iterator does not work here
for zone in &config.zones {
let error = zone.update(&mut change4, &mut change6, &ips, &config.api_key);
update_results.append(&mut vec!(error));
if error {
break; // if one update fails all others are likely to fail as well
}
}
if update_results.contains(&true) {
println!("DNS Update failed due to Network Error")
}
else {
println!("++++A Records+++");
change4.print("simple");
println!("++AAAA Records++");
change6.print("simple")
}
}
}
fn main() {
let mut now = Utc::now() - Duration::seconds(59);
let mut current = now;
loop {
now = Utc::now();
if now >= current + Duration::seconds(60) {
current = now;
print!("\x1B[2J\x1B[1;1H");
println!("Starting DNS Update at {} {}", now.format("%H:%M:%S"), now.timezone());
update_dns();
}
else {
sleep(std::time::Duration::from_millis(500));
}
}
}