Moved IP Handling code to its own module.
This commit is contained in:
parent
9e7a59389e
commit
6729c06af7
|
@ -0,0 +1,54 @@
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{Read, Write};
|
||||||
|
use std::path::Path;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
const FILE_NAME: &'static str = "ddns_ip";
|
||||||
|
const WEBSITE_URL: &'static str = "https://httpbin.org/ip";
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct IP {
|
||||||
|
origin: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn ip_has_changed() -> bool {
|
||||||
|
let current_ip = get_current_ip().await.expect("Error getting the current IP.");
|
||||||
|
|
||||||
|
let previous_ip = match check_previous_ip() {
|
||||||
|
Some(x) => x,
|
||||||
|
None => String::new()
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("Current IP: {}, Previous IP: {}", current_ip, previous_ip);
|
||||||
|
|
||||||
|
current_ip.ne(&previous_ip)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_current_ip() -> Result<String, Box<dyn std::error::Error>> {
|
||||||
|
let resp = reqwest::get(WEBSITE_URL)
|
||||||
|
.await?
|
||||||
|
.json::<IP>()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
record_current_ip(&resp.origin);
|
||||||
|
|
||||||
|
Ok(resp.origin)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn record_current_ip(current_ip: &str) {
|
||||||
|
let mut file = File::create(FILE_NAME).expect("Error creating file.");
|
||||||
|
file.write_all(current_ip.as_ref()).expect("Error writing file.");
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
51
src/main.rs
51
src/main.rs
|
@ -1,56 +1,13 @@
|
||||||
use std::fs::File;
|
mod ip_handler;
|
||||||
use std::io::prelude::*;
|
|
||||||
use std::path::Path;
|
|
||||||
use serde::{Serialize, Deserialize};
|
|
||||||
|
|
||||||
const FILE_NAME: &'static str = "ddns_ip";
|
use ip_handler::ip_has_changed;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
struct IP {
|
|
||||||
origin: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let current_ip = get_current_ip().await?;
|
|
||||||
|
|
||||||
let previous_ip = match check_previous_ip() {
|
if ip_has_changed().await {
|
||||||
Some(x) => x,
|
println!("Ip has changed!");
|
||||||
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_current_ip() -> Result<String, Box<dyn std::error::Error>> {
|
|
||||||
let resp = reqwest::get("https://httpbin.org/ip")
|
|
||||||
.await?
|
|
||||||
.json::<IP>()
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
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