polaris-mirror/src/ddns.rs
Antoine Gersant 86e618d2a5 Removed the dependency on OpenSSL on Windows
- Replaced session handling by secure_session
- Required updating a bunch of dependencies
- Replaced Hyper Client usage in DDNS by Reqwest (which only uses OpenSSL on Linux)
2017-06-04 13:17:27 -07:00

61 lines
1.4 KiB
Rust

use reqwest;
use reqwest::header::{Authorization, Basic};
use std::io;
use std::thread;
use std::time;
#[derive(Clone, Debug, Deserialize)]
pub struct DDNSConfig {
pub host: String,
pub username: String,
pub password: String,
}
#[derive(Debug)]
enum DDNSError {
IoError(io::Error),
ReqwestError(reqwest::Error),
UpdateError(reqwest::StatusCode),
}
impl From<io::Error> for DDNSError {
fn from(err: io::Error) -> DDNSError {
DDNSError::IoError(err)
}
}
impl From<reqwest::Error> for DDNSError {
fn from(err: reqwest::Error) -> DDNSError {
DDNSError::ReqwestError(err)
}
}
const DDNS_UPDATE_URL: &'static str = "https://ydns.io/api/v1/update/";
fn update_my_ip(config: &DDNSConfig) -> Result<(), DDNSError> {
let full_url = format!("{}?host={}", DDNS_UPDATE_URL, &config.host);
let auth_header = Authorization(Basic {
username: config.username.clone(),
password: Some(config.password.to_owned()),
});
let client = reqwest::Client::new()?;
let res = client
.get(full_url.as_str())
.header(auth_header)
.send()?;
if !res.status().is_success() {
return Err(DDNSError::UpdateError(*res.status()));
}
Ok(())
}
pub fn run(config: DDNSConfig) {
loop {
match update_my_ip(&config) {
Err(e) => println!("Dynamic DNS Error: {:?}", e),
Ok(_) => (),
};
thread::sleep(time::Duration::from_secs(60 * 30));
}
}