Browse Source

POC work for updating domain record automatically

develop
Drew Short 6 years ago
parent
commit
0c1e6c8507
  1. 86
      src/server/api.rs

86
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<IpAddr> {
fn get_request_address(req: &HttpRequest<AppState>) -> 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<AppState>) -> HttpResponse {
}
fn update_address_automatically(req: &HttpRequest<AppState>) -> 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(),
}
}

Loading…
Cancel
Save