Added documentation comments for ip_handler.
This commit is contained in:
parent
6729c06af7
commit
f6118859ac
|
@ -1,7 +1,7 @@
|
||||||
|
use serde::Deserialize;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
const FILE_NAME: &'static str = "ddns_ip";
|
const FILE_NAME: &'static str = "ddns_ip";
|
||||||
const WEBSITE_URL: &'static str = "https://httpbin.org/ip";
|
const WEBSITE_URL: &'static str = "https://httpbin.org/ip";
|
||||||
|
@ -11,35 +11,49 @@ struct IP {
|
||||||
origin: String,
|
origin: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn ip_has_changed() -> bool {
|
/// Returns an Option holding the current IP address if the value has changed, otherwise returns None.
|
||||||
let current_ip = get_current_ip().await.expect("Error getting the current IP.");
|
pub async fn get_ip_to_publish() -> Option<String> {
|
||||||
|
let current_ip = check_current_ip()
|
||||||
|
.await
|
||||||
|
.expect("Error getting the current IP.");
|
||||||
|
|
||||||
let previous_ip = match check_previous_ip() {
|
let previous_ip = match check_previous_ip() {
|
||||||
Some(x) => x,
|
Some(x) => x,
|
||||||
None => String::new()
|
None => String::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("Current IP: {}, Previous IP: {}", current_ip, previous_ip);
|
println!("Current IP: {}, Previous IP: {}", current_ip, previous_ip);
|
||||||
|
|
||||||
current_ip.ne(&previous_ip)
|
if current_ip.eq(&previous_ip) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(current_ip)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_current_ip() -> Result<String, Box<dyn std::error::Error>> {
|
/// Checks the current WAN IP.
|
||||||
let resp = reqwest::get(WEBSITE_URL)
|
///
|
||||||
.await?
|
/// Connects to WEBSITE_URL and retrieves the current WAN IP value.
|
||||||
.json::<IP>()
|
async fn check_current_ip() -> Result<String, Box<dyn std::error::Error>> {
|
||||||
.await?;
|
let resp = reqwest::get(WEBSITE_URL).await?.json::<IP>().await?;
|
||||||
|
|
||||||
record_current_ip(&resp.origin);
|
record_current_ip(&resp.origin);
|
||||||
|
|
||||||
Ok(resp.origin)
|
Ok(resp.origin)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn record_current_ip(current_ip: &str) {
|
/// Stores the current IP value in the FILE_NAME.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `current_ip` - A &str holding the current IP value.
|
||||||
|
fn record_current_ip(current_ip: &str) {
|
||||||
let mut file = File::create(FILE_NAME).expect("Error creating file.");
|
let mut file = File::create(FILE_NAME).expect("Error creating file.");
|
||||||
file.write_all(current_ip.as_ref()).expect("Error writing file.");
|
file.write_all(current_ip.as_ref())
|
||||||
|
.expect("Error writing file.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Reads the current IP value from the FILE_NAME and returns it.
|
||||||
fn check_previous_ip() -> Option<String> {
|
fn check_previous_ip() -> Option<String> {
|
||||||
if !Path::new(FILE_NAME).exists() {
|
if !Path::new(FILE_NAME).exists() {
|
||||||
return None;
|
return None;
|
||||||
|
|
Loading…
Reference in New Issue