Added a method to store and get the previous IP.
This commit is contained in:
parent
082daa4f90
commit
873e20195d
|
@ -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"
|
||||
|
|
|
@ -8,3 +8,4 @@ edition = "2021"
|
|||
[dependencies]
|
||||
reqwest = { version = "0.11", features = ["json"] }
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
serde = { version = "1.0.132", features = ["derive"]}
|
45
src/main.rs
45
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<dyn std::error::Error>> {
|
||||
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<dyn std::error::Error>> {
|
|||
async fn get_current_ip() -> Result<String, Box<dyn std::error::Error>> {
|
||||
let resp = reqwest::get("https://httpbin.org/ip")
|
||||
.await?
|
||||
.json::<HashMap<String, String>>()
|
||||
.json::<IP>()
|
||||
.await?;
|
||||
|
||||
Ok(resp.get("origin").unwrap().as_str().parse().unwrap())
|
||||
Ok(resp.origin)
|
||||
}
|
||||
|
||||
fn check_previous_ip() -> Option<String> {
|
||||
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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue