Created
July 21, 2022 19:39
-
-
Save Tomy2e/7a7323d3345581211458cb2212d1eae5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use dns_lookup::lookup_host; | |
use std::env::set_var; | |
use std::env::var; | |
use std::net::IpAddr; | |
use std::{thread, time}; | |
#[tokio::main] | |
async fn main() { | |
// Debug. | |
set_var("DOMAIN", "google.fr"); | |
set_var("USER", "tomy"); | |
set_var("PASSWORD", "tomy"); | |
println!("Hello, world!"); | |
let hostname = var("DOMAIN").unwrap(); | |
let user = var("USER").unwrap(); | |
let password = var("PASSWORD").unwrap(); | |
let update_url: String = String::from("https://www.ovh.com/nic/update?"); | |
let mut ip = *lookup_host(hostname.as_str()).unwrap().first().unwrap(); | |
println!("Current IP for {} is {}", hostname, ip); | |
loop { | |
let body = reqwest::get("https://api.ipify.org/") | |
.await | |
.unwrap() | |
.text() | |
.await | |
.unwrap(); | |
let current_ip: IpAddr = body.parse().unwrap(); | |
println!("current_ip = {:?}", current_ip); | |
if !ip.eq(¤t_ip) { | |
println!("running update!!"); | |
ip = current_ip; | |
let client = reqwest::Client::new(); | |
let update_url = update_url.clone() | |
+ &querystring::stringify(vec![ | |
("system", "dyndns"), | |
("hostname", "hostname"), | |
("ip", &ip.to_string()), | |
]); | |
let body = client | |
.get(update_url) | |
.basic_auth(&user, Some(&password)) | |
.send() | |
.await | |
.unwrap() | |
.text() | |
.await | |
.unwrap(); | |
if !body.starts_with("nochg") && !body.starts_with("good") { | |
panic!("failed to update DynHost record: {}", body) | |
} | |
} | |
thread::sleep(time::Duration::from_secs(5)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment