From 0c1e6c8507a3115f2a18b1caac3c328dd3eedee4 Mon Sep 17 00:00:00 2001 From: Drew Short Date: Fri, 14 Dec 2018 23:16:24 -0600 Subject: [PATCH] POC work for updating domain record automatically --- src/server/api.rs | 86 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 67 insertions(+), 19 deletions(-) diff --git a/src/server/api.rs b/src/server/api.rs index ae59310..7d5d0bf 100644 --- a/src/server/api.rs +++ b/src/server/api.rs @@ -1,5 +1,5 @@ use std::io::Read; -use std::net::IpAddr; +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; use std::str::FromStr; use actix_web::{AsyncResponder, FutureResponse, HttpMessage, HttpRequest, HttpResponse, Scope}; @@ -54,15 +54,13 @@ fn parse_remote_info(remote_info: &str) -> Result { fn get_request_address(req: &HttpRequest) -> HttpResponse { match req.connection_info().remote() { - Some(remote_info) => { - match parse_remote_info(remote_info) { - Ok(addr) => HttpResponse::build(StatusCode::OK) - .content_type("text/plain") - .body(format!("{}", addr)), - Err(e) => { - error!("{:?}", e); - HttpResponse::build(StatusCode::BAD_REQUEST).finish() - } + Some(remote_info) => match parse_remote_info(remote_info) { + Ok(addr) => HttpResponse::build(StatusCode::OK) + .content_type("text/plain") + .body(format!("{}", addr)), + Err(e) => { + error!("{:?}", e); + HttpResponse::build(StatusCode::BAD_REQUEST).finish() } } None => HttpResponse::build(StatusCode::BAD_REQUEST).finish(), @@ -111,19 +109,69 @@ fn get_address(req: &HttpRequest) -> HttpResponse { } fn update_address_automatically(req: &HttpRequest) -> HttpResponse { - match req.connection_info().remote() { - Some(remote_info) => { - match parse_remote_info(remote_info) { - Ok(addr) => HttpResponse::build(StatusCode::OK) - .content_type("text/plain") - .body(format!("{}", addr)), + let root_match = util::get_match_value(req, "root"); + let zone_match = util::get_match_value(req, "zone"); + let config: &Config = &req.state().config; + let cloudflare: &Cloudflare = &req.state().cloudflare; + if root_match.is_none() || zone_match.is_none() { + HttpResponse::BadRequest().into() + } else { + let root = root_match.unwrap(); + let zone = zone_match.unwrap(); + match util::get_domain_from_root(&root) { + Some(domain) => match zones::get_zoneid(cloudflare, &domain) { + Ok(zone_id) => match zones::dns::list_dns_of_type(cloudflare, &zone_id, zones::dns::RecordType::A) { + Ok(dns_entries) => { + let record_name = format!("{}.{}", &zone, &root); + debug!("Looking for dns record: {}", record_name); + for dns_entry in dns_entries { + debug!("record {:?}", dns_entry); + if dns_entry.name == record_name { + return match req.connection_info().remote() { + Some(remote_info) => match parse_remote_info(remote_info) { + Ok(addr) => match addr { + IpAddr::V4(_) => match zones::dns::create_dns_entry(cloudflare, &zone_id, zones::dns::RecordType::A, &record_name, &format!("{}", addr)) { + Ok(result_record) => HttpResponse::build(StatusCode::OK) + .content_type("text/plain") + .body(format!("{}", result_record.content)), + Err(e) => { + error!("{:?}", e); + HttpResponse::InternalServerError().into() + } + }, + IpAddr::V6(_) => match zones::dns::create_dns_entry(cloudflare, &zone_id, zones::dns::RecordType::AAAA, &record_name, &format!("{}", addr)) { + Ok(result_record) => HttpResponse::build(StatusCode::OK) + .content_type("text/plain") + .body(format!("{}", result_record.content)), + Err(e) => { + error!("{:?}", e); + HttpResponse::InternalServerError().into() + } + } + }, + Err(e) => { + error!("{:?}", e); + HttpResponse::InternalServerError().into() + } + }, + None => HttpResponse::BadRequest().into() + }; + } + } + HttpResponse::BadRequest().into() + } + Err(e) => { + error!("{:?}", e); + HttpResponse::InternalServerError().into() + } + }, Err(e) => { error!("{:?}", e); - HttpResponse::build(StatusCode::BAD_REQUEST).finish() + HttpResponse::InternalServerError().into() } - } + }, + None => HttpResponse::BadRequest().into() } - None => HttpResponse::build(StatusCode::BAD_REQUEST).finish(), } }