From 1262510ab991c2ed06b9a19b65d602e279ea5699 Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Sat, 13 Jul 2024 14:26:55 +0200 Subject: [PATCH] config file and skeleton --- Cargo.lock | 104 ++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 4 ++ src/cloudflare.rs | 0 src/config.rs | 48 +++++++++++++++++++++ src/lib.rs | 6 +++ src/main.rs | 10 +++++ 6 files changed, 172 insertions(+) create mode 100644 src/cloudflare.rs create mode 100644 src/config.rs create mode 100644 src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index ca1d462..4d6ac2f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -178,11 +178,33 @@ version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" +[[package]] +name = "dirs" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.48.0", +] + [[package]] name = "dynip-cloudflare" version = "0.1.0" dependencies = [ "anyhow", + "dirs", "env_logger", "futures", "log", @@ -191,7 +213,10 @@ dependencies = [ "netlink-sys", "reqwest", "rtnetlink", + "serde", + "serde_json", "tokio", + "toml", ] [[package]] @@ -587,6 +612,16 @@ version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +[[package]] +name = "libredox" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +dependencies = [ + "bitflags 2.6.0", + "libc", +] + [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -803,6 +838,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "parking_lot" version = "0.12.3" @@ -903,6 +944,17 @@ dependencies = [ "bitflags 2.6.0", ] +[[package]] +name = "redox_users" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + [[package]] name = "regex" version = "1.10.5" @@ -1142,6 +1194,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79e674e01f999af37c49f70a6ede167a8a60b2503e56c5599532a65baa5969a0" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -1349,6 +1410,40 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.8.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f49eb2ab21d2f26bd6db7bf383edc527a7ebaee412d17af4d40fdccd442f335" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4badfd56924ae69bcc9039335b2e017639ce3f9b001c393c1b2d1ef846ce2cbf" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d59a3a72298453f564e2b111fa896f8d07fabb36f51f06d7e875fc5e0b5a3ef1" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "tower" version = "0.4.13" @@ -1681,6 +1776,15 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "winnow" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59b5e5f6c299a3c7890b876a2a587f3115162487e704907d9b6cd29473052ba1" +dependencies = [ + "memchr", +] + [[package]] name = "winreg" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index 03aaace..b94cede 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] anyhow = "1.0.86" +dirs = "5.0.1" env_logger = "0.11.3" futures = "0.3.30" log = "0.4.22" @@ -13,4 +14,7 @@ netlink-packet-route = "0.19.0" netlink-sys = { version = "0.8.6", features = ["tokio"] } reqwest = "0.12.5" rtnetlink = "0.14.1" # Updated to a version compatible with netlink-packet-route v0.20.1 +serde = { version = "1.0.204", features = ["rc", "derive"] } +serde_json = "1.0.120" tokio = { version = "1.38.0", features = ["full"] } +toml = "0.8.14" diff --git a/src/cloudflare.rs b/src/cloudflare.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..a7cfe87 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,48 @@ +use dirs; +use log::warn; +use serde::{Deserialize, Serialize}; +use std::{env, fs, path::PathBuf}; + +use crate::PROGRAM_NAME; + +#[derive(Deserialize, Serialize, Debug)] +pub struct Config { + pub cloudflare_zone_id: Box, + pub cloudflare_api_key: Box, +} + +pub fn get_config_path() -> Option { + match env::current_dir() { + Ok(current_dir) => { + let cwd_config = current_dir.join(format!("{}.toml", PROGRAM_NAME)); + if let Ok(meta) = fs::metadata(&cwd_config) { + if meta.is_file() { + return Some(cwd_config); + } + } + } + Err(e) => { + warn!("Failed to get current directory {:?}", &e); + } + } + + match dirs::config_dir() { + Some(config_dir) => { + let config_file = config_dir.join(PROGRAM_NAME).join("config.toml"); + if let Ok(meta) = fs::metadata(&config_file) { + if meta.is_file() { + return Some(config_file); + } + } + } + None => {} + } + + None +} + +pub fn read_config(path: PathBuf) -> Option{ + + + None +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..e848e0e --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,6 @@ +mod cloudflare; +mod config; +pub use config::{Config, get_config_path}; + + +pub const PROGRAM_NAME: &'static str = "dynip-cloudflare"; diff --git a/src/main.rs b/src/main.rs index cb4d7bf..a0046c0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,8 @@ use netlink_packet_route::RouteNetlinkMessage as RtnlMessage; use netlink_sys::{AsyncSocket, SocketAddr}; use rtnetlink::new_connection; +use dynip_cloudflare::{get_config_path, Config}; + const RTNLGRP_LINK: u32 = 1; const RTNLGRP_IPV4_IFADDR: u32 = 5; @@ -24,6 +26,14 @@ const fn nl_mgrp(group: u32) -> u32 { #[tokio::main] async fn main() -> Result<()> { env_logger::init(); + let config_path = match get_config_path() { + Some(cp) => cp, + None => { + error!("Failed to find any config file"); + return Ok(()); + } + }; + let (mut conn, mut _handle, mut messages) = new_connection().map_err(|e| anyhow!(e))?; let groups = nl_mgrp(RTNLGRP_LINK) | nl_mgrp(RTNLGRP_IPV4_IFADDR);