Created
June 16, 2022 21:10
-
-
Save Mon-ius/4ca2f7b2ac0b56c986a5592097628f91 to your computer and use it in GitHub Desktop.
DDNS
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
[package] | |
name = "ddns" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] | |
reqwest = { version = "0.11", features = ["blocking", "json"] } | |
tokio = { version = "1.19.2", features = ["full"] } | |
regex = "1" | |
local-ip-address = "0.4.4" |
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 regex::Regex; | |
use local_ip_address::local_ip; | |
async fn update_ddns(ip: String, domain:String, domain_token: String) { | |
let url = format!("https://www.duckdns.org/update?domains={}&token={}&ip={}", domain, domain_token, ip); | |
let _res = reqwest::get(&url).await.unwrap(); | |
println!("Success! {:?}", _res.status()); | |
} | |
async fn get_inet_info(priv_addr: &mut String , pub_addr: &mut String){ | |
*pub_addr = reqwest::get("https://ifconfig.me").await.unwrap().text().await.unwrap(); | |
*priv_addr = local_ip().unwrap().to_string(); | |
} | |
async fn auth_inet_info(url: String, payload: String){ | |
let client = reqwest::Client::new(); | |
let _res = client.post(&url).body(payload) | |
.header(reqwest::header::CONTENT_TYPE, "application/x-www-form-urlencoded") | |
.send() | |
.await | |
.unwrap(); | |
println!("Success! {:?}", _res.status()); | |
} | |
async fn get_token(url: String, token: &mut String){ | |
let client = reqwest::Client::new(); | |
let _res = client.get(&url).send().await.unwrap().text().await; | |
let token_re = Regex::new(r#"var session_token = "(.*?)";"#).unwrap(); | |
// println!("Success! {:?}", _res.status()); | |
*token = token_re.captures(&_res.as_ref().unwrap()).unwrap().get(1).unwrap().as_str().to_string(); | |
} | |
async fn log_out(url:String){ | |
let client = reqwest::Client::new(); | |
let _res = client.post(&url).body("logout=1") | |
.header(reqwest::header::CONTENT_TYPE, "application/x-www-form-urlencoded") | |
.send() | |
.await | |
.unwrap(); | |
println!("Success! {:?}", _res.status()); | |
} | |
async fn auth_token(url: String, payload: String, token: String){ | |
let _payload = payload.replace("{token}", &token); | |
let client = reqwest::Client::new(); | |
let _res = client.post(url) | |
.body(_payload) | |
.header(reqwest::header::CONTENT_TYPE, "application/x-www-form-urlencoded") | |
.send() | |
.await | |
.unwrap(); | |
// println!("Success! {:?}", _res.status()); | |
} | |
async fn run(url_list: Vec<String>, pass_code: String, payload: String, domain_token: String){ | |
let mut priv_addr = String::new(); | |
let mut pub_addr = String::new(); | |
let mut token = String::new(); | |
get_inet_info(&mut priv_addr, &mut pub_addr).await; | |
auth_inet_info(url_list[1].clone(), pass_code).await; | |
get_token(url_list[2].clone(), &mut token).await; | |
auth_token(url_list[2].clone(), payload.replace("{dmz}", &priv_addr), token).await; | |
log_out(url_list[0].clone()).await; | |
update_ddns(pub_addr.clone(), url_list[3].clone(), domain_token).await; | |
} | |
#[tokio::main] | |
async fn main() { | |
let mut url_list = Vec::from(vec![ | |
"http://10.0.0.1:8080".to_string(), | |
"http://10.0.0.1:8080/login.cgi".to_string(), | |
"http://10.0.0.1:8080/getpage.gch?pid=1002&nextpage=app_dmz_conf_t.gch".to_string() | |
]); | |
let domain = std::env::args().nth(1).expect("Not a domain"); | |
let domain_token = std::env::args().nth(2).expect("Not a token"); | |
url_list.push(domain); | |
let payload_fmt = "IF_ACTION=apply&InternalHost={dmz}&_SESSION_TOKEN={token}"; | |
let pass_code = "username=useradmin&psd=nE7jA%255m"; | |
run(url_list, pass_code.to_string(), payload_fmt.to_string(), domain_token).await; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment