diff --git a/Cargo.lock b/Cargo.lock index 4ded603..5bd0c61 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -155,6 +155,7 @@ name = "godaddy_ddns" version = "0.1.0" dependencies = [ "reqwest", + "serde", "tokio", ] @@ -682,6 +683,20 @@ name = "serde" version = "1.0.132" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b9875c23cf305cd1fd7eb77234cbb705f21ea6a72c637a5c6db5fe4b8e7f008" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.132" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc0db5cb2556c0e558887d9bbdcf6ac4471e83ff66cf696e5419024d1606276" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] [[package]] name = "serde_json" diff --git a/Cargo.toml b/Cargo.toml index 772d024..0db927d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2021" [dependencies] reqwest = { version = "0.11", features = ["json"] } -tokio = { version = "1", features = ["full"] } \ No newline at end of file +tokio = { version = "1", features = ["full"] } +serde = { version = "1.0.132", features = ["derive"]} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index eaf03e8..b2eee5c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,35 @@ -use std::collections::HashMap; use std::fs::File; use std::io::prelude::*; +use std::path::Path; +use serde::{Serialize, Deserialize}; + +const FILE_NAME: &'static str = "ddns_ip"; + +#[derive(Deserialize)] +struct IP { + origin: String, +} #[tokio::main] async fn main() -> Result<(), Box> { - let ip = get_current_ip().await?; + let current_ip = get_current_ip().await?; - println!("{:#?}", ip); + let previous_ip = match check_previous_ip() { + Some(x) => x, + None => { + let mut file = File::create(FILE_NAME)?; + file.write_all(current_ip.as_ref())?; + + String::new() + } + }; + + println!("Current IP: {}, Previous IP: {}", current_ip, previous_ip); + + if current_ip.ne(&previous_ip) { + println!("Not equal!!!"); + // TODO: GoDaddy connection. + } Ok(()) } @@ -14,8 +37,20 @@ async fn main() -> Result<(), Box> { async fn get_current_ip() -> Result> { let resp = reqwest::get("https://httpbin.org/ip") .await? - .json::>() + .json::() .await?; - Ok(resp.get("origin").unwrap().as_str().parse().unwrap()) + Ok(resp.origin) +} + +fn check_previous_ip() -> Option { + if !Path::new(FILE_NAME).exists() { + return None; + } + + let mut file = File::open(FILE_NAME).ok()?; + let mut contents = String::new(); + file.read_to_string(&mut contents).expect("Error reading file"); + + return Some(contents); }