|
@ -1,4 +1,6 @@ |
|
|
use std::io::Read;
|
|
|
use std::io::Read;
|
|
|
|
|
|
use std::net::IpAddr;
|
|
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
|
use actix_web::{AsyncResponder, FutureResponse, HttpMessage, HttpRequest, HttpResponse, Scope};
|
|
|
use actix_web::{AsyncResponder, FutureResponse, HttpMessage, HttpRequest, HttpResponse, Scope};
|
|
|
use actix_web::http::Method;
|
|
|
use actix_web::http::Method;
|
|
@ -6,6 +8,8 @@ use actix_web::http::StatusCode; |
|
|
use bytes::{Buf, Bytes, IntoBuf};
|
|
|
use bytes::{Buf, Bytes, IntoBuf};
|
|
|
use futures::future::Future;
|
|
|
use futures::future::Future;
|
|
|
|
|
|
|
|
|
|
|
|
use server::error::APIError;
|
|
|
|
|
|
|
|
|
pub fn route(scope: Scope<()>) -> Scope<()> {
|
|
|
pub fn route(scope: Scope<()>) -> Scope<()> {
|
|
|
scope.resource("address", |r| r.method(Method::GET).f(get_address))
|
|
|
scope.resource("address", |r| r.method(Method::GET).f(get_address))
|
|
|
.nested("{root}/{zone}", |zone_scope| {
|
|
|
.nested("{root}/{zone}", |zone_scope| {
|
|
@ -23,20 +27,53 @@ fn update_address(address: String) -> String { |
|
|
address
|
|
|
address
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn parse_remote_info(remote_info: &str) -> Result<IpAddr, APIError> {
|
|
|
|
|
|
let mut remote_address = String::from(remote_info);
|
|
|
|
|
|
if remote_address.contains(":") {
|
|
|
|
|
|
let last_colon_index = remote_address.rfind(":").unwrap();
|
|
|
|
|
|
let port = remote_address.split_off(last_colon_index);
|
|
|
|
|
|
if remote_address.starts_with("[") && remote_address.ends_with("]") {
|
|
|
|
|
|
remote_address = String::from(remote_address.trim_matches(|c| c == '[' || c == ']'))
|
|
|
|
|
|
}
|
|
|
|
|
|
match IpAddr::from_str(&remote_address) {
|
|
|
|
|
|
Ok(v) => Ok(v),
|
|
|
|
|
|
Err(e) => Err(APIError::new(&format!("Address Parse Error \"{}\"", remote_address), Some(Box::from(e))))
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Err(APIError::new(&format!("{} could not be parsed", remote_address), None))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
fn get_address(req: &HttpRequest) -> HttpResponse {
|
|
|
fn get_address(req: &HttpRequest) -> HttpResponse {
|
|
|
match req.connection_info().remote() {
|
|
|
match req.connection_info().remote() {
|
|
|
Some(addr) => HttpResponse::build(StatusCode::OK)
|
|
|
|
|
|
.content_type("text/plain")
|
|
|
|
|
|
.body(format!("{}", addr)),
|
|
|
|
|
|
|
|
|
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(),
|
|
|
None => HttpResponse::build(StatusCode::BAD_REQUEST).finish(),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn update_address_automatically(req: &HttpRequest) -> HttpResponse {
|
|
|
fn update_address_automatically(req: &HttpRequest) -> HttpResponse {
|
|
|
match req.connection_info().remote() {
|
|
|
match req.connection_info().remote() {
|
|
|
Some(addr) => HttpResponse::build(StatusCode::OK)
|
|
|
|
|
|
.content_type("text/plain")
|
|
|
|
|
|
.body(format!("{}", addr)),
|
|
|
|
|
|
|
|
|
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(),
|
|
|
None => HttpResponse::build(StatusCode::BAD_REQUEST).finish(),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|