api2/node/dns.rs: improve dns api
This commit is contained in:
parent
332dcc2277
commit
46b79b9ee5
|
@ -2,6 +2,7 @@ use failure::*;
|
|||
use std::collections::HashMap;
|
||||
use serde_json::{json, Value};
|
||||
use url::form_urlencoded;
|
||||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
use std::fmt;
|
||||
use std::sync::Arc;
|
||||
|
@ -358,7 +359,7 @@ impl From<ArraySchema> for Arc<Schema> {
|
|||
|
||||
pub enum ApiStringFormat {
|
||||
Enum(Vec<String>),
|
||||
Pattern(Box<Regex>),
|
||||
Pattern(&'static Regex),
|
||||
Complex(Arc<Schema>),
|
||||
VerifyFn(fn(&str) -> Result<(), Error>),
|
||||
}
|
||||
|
@ -695,11 +696,15 @@ fn test_query_string() {
|
|||
assert!(res.is_ok());
|
||||
|
||||
// TEST regex pattern
|
||||
lazy_static! {
|
||||
static ref TEST_REGEX: Regex = Regex::new("test").unwrap();
|
||||
static ref TEST2_REGEX: Regex = Regex::new("^test$").unwrap();
|
||||
}
|
||||
|
||||
let schema = ObjectSchema::new("Parameters.")
|
||||
.required(
|
||||
"name", StringSchema::new("Name.")
|
||||
.format(Arc::new(ApiStringFormat::Pattern(Box::new(Regex::new("test").unwrap()))))
|
||||
.format(Arc::new(ApiStringFormat::Pattern(&TEST_REGEX)))
|
||||
);
|
||||
|
||||
let res = parse_query_string("name=abcd", &schema, true);
|
||||
|
@ -711,7 +716,7 @@ fn test_query_string() {
|
|||
let schema = ObjectSchema::new("Parameters.")
|
||||
.required(
|
||||
"name", StringSchema::new("Name.")
|
||||
.format(Arc::new(ApiStringFormat::Pattern(Box::new(Regex::new("^test$").unwrap()))))
|
||||
.format(Arc::new(ApiStringFormat::Pattern(&TEST2_REGEX)))
|
||||
);
|
||||
|
||||
let res = parse_query_string("name=ateststring", &schema, true);
|
||||
|
|
14
src/api2.rs
14
src/api2.rs
|
@ -3,6 +3,7 @@ use failure::*;
|
|||
use crate::api::schema::*;
|
||||
use crate::api::router::*;
|
||||
use serde_json::{json, Value};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub mod config;
|
||||
pub mod admin;
|
||||
|
@ -10,6 +11,19 @@ pub mod node;
|
|||
mod version;
|
||||
mod subscription;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use crate::tools::common_regex;
|
||||
|
||||
// common schema definitions
|
||||
|
||||
lazy_static! {
|
||||
pub static ref IP_FORMAT: Arc<ApiStringFormat> = ApiStringFormat::Pattern(&common_regex::IP_REGEX).into();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn test_sync_api_handler(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
|
||||
println!("This is a test {}", param);
|
||||
|
||||
|
|
|
@ -2,14 +2,14 @@ use failure::*;
|
|||
|
||||
|
||||
use crate::tools;
|
||||
use crate::tools::common_regex;
|
||||
|
||||
use crate::api::schema::*;
|
||||
use crate::api::router::*;
|
||||
use crate::api2::*;
|
||||
//use crate::api::schema::*;
|
||||
//use crate::api::router::*;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde_json::{json, Value};
|
||||
|
||||
|
@ -22,7 +22,7 @@ fn read_etc_resolv_conf() -> Result<Value, Error> {
|
|||
let mut nscount = 0;
|
||||
|
||||
let file = std::fs::File::open(RESOLV_CONF_FN)?;
|
||||
let mut reader = BufReader::new(file);
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
let test = IPRE!();
|
||||
|
||||
|
@ -35,26 +35,59 @@ fn read_etc_resolv_conf() -> Result<Value, Error> {
|
|||
for line in reader.lines() {
|
||||
let line = line?;
|
||||
|
||||
if let Some(m) = DOMAIN_REGEX.find(&line) {
|
||||
let domain = m.as_str();
|
||||
result["search"] = Value::from(domain);
|
||||
} else if let Some(m) = SERVER_REGEX.find(&line) {
|
||||
if let Some(caps) = DOMAIN_REGEX.captures(&line) {
|
||||
result["search"] = Value::from(&caps[1]);
|
||||
} else if let Some(caps) = SERVER_REGEX.captures(&line) {
|
||||
nscount += 1;
|
||||
if nscount > 3 { continue };
|
||||
let nameserver = m.as_str();
|
||||
let nameserver = &caps[1];
|
||||
let id = format!("dns{}", nscount);
|
||||
result[id] = Value::from(m.as_str());
|
||||
result[id] = Value::from(nameserver);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn update_dns(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
|
||||
|
||||
let search = tools::required_string_param(¶m, "search")?;
|
||||
|
||||
let mut data = format!("search {}\n", search);
|
||||
|
||||
for opt in &["dns1", "dns2", "dns3"] {
|
||||
if let Some(server) = param[opt].as_str() {
|
||||
data.push_str(&format!("nameserver {}\n", server));
|
||||
}
|
||||
}
|
||||
|
||||
tools::file_set_contents(RESOLV_CONF_FN, data.as_bytes(), None)?;
|
||||
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
fn get_dns(_param: Value, _info: &ApiMethod) -> Result<Value, Error> {
|
||||
|
||||
read_etc_resolv_conf()
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref SEARCH_DOMAIN_SCHEMA: Arc<Schema> =
|
||||
StringSchema::new("Search domain for host-name lookup.").into();
|
||||
|
||||
pub static ref FIRST_DNS_SERVER_SCHEMA: Arc<Schema> =
|
||||
StringSchema::new("First name server IP address.")
|
||||
.format(IP_FORMAT.clone()).into();
|
||||
|
||||
pub static ref SECOND_DNS_SERVER_SCHEMA: Arc<Schema> =
|
||||
StringSchema::new("Second name server IP address.")
|
||||
.format(IP_FORMAT.clone()).into();
|
||||
|
||||
pub static ref THIRD_DNS_SERVER_SCHEMA: Arc<Schema> =
|
||||
StringSchema::new("Third name server IP address.")
|
||||
.format(IP_FORMAT.clone()).into();
|
||||
}
|
||||
|
||||
pub fn router() -> Router {
|
||||
|
||||
let route = Router::new()
|
||||
|
@ -64,11 +97,21 @@ pub fn router() -> Router {
|
|||
ObjectSchema::new("Read DNS settings.")
|
||||
).returns(
|
||||
ObjectSchema::new("Returns DNS server IPs and sreach domain.")
|
||||
.optional("search", StringSchema::new("Search domain for host-name lookup."))
|
||||
.optional("dns1", StringSchema::new("First name server IP address."))
|
||||
.optional("dns2", StringSchema::new("Second name server IP address."))
|
||||
.optional("dns3", StringSchema::new("Third name server IP address."))
|
||||
.optional("search", SEARCH_DOMAIN_SCHEMA.clone())
|
||||
.optional("dns1", FIRST_DNS_SERVER_SCHEMA.clone())
|
||||
.optional("dns2", SECOND_DNS_SERVER_SCHEMA.clone())
|
||||
.optional("dns3", THIRD_DNS_SERVER_SCHEMA.clone())
|
||||
)
|
||||
)
|
||||
.put(
|
||||
ApiMethod::new(
|
||||
update_dns,
|
||||
ObjectSchema::new("Returns DNS server IPs and sreach domain.")
|
||||
.required("search", SEARCH_DOMAIN_SCHEMA.clone())
|
||||
.optional("dns1", FIRST_DNS_SERVER_SCHEMA.clone())
|
||||
.optional("dns2", SECOND_DNS_SERVER_SCHEMA.clone())
|
||||
.optional("dns3", THIRD_DNS_SERVER_SCHEMA.clone())
|
||||
)
|
||||
);
|
||||
|
||||
route
|
||||
|
|
|
@ -42,7 +42,7 @@ fn set_timezone(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
|
|||
bail!("No such timezone.");
|
||||
}
|
||||
|
||||
tools::file_set_contents("/etc/timezone", timezone.as_bytes(), None);
|
||||
tools::file_set_contents("/etc/timezone", timezone.as_bytes(), None)?;
|
||||
|
||||
let _ = std::fs::remove_file("/etc/localtime");
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
//! This is a collection of useful regular expressions
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! IPV4OCTET { () => (r"(?:25[0-5]|(?:2[0-4]|1[0-9]|[1-9])?[0-9])") }
|
||||
|
@ -30,3 +31,7 @@ macro_rules! IPV6RE { () => (concat!(r"(?:",
|
|||
|
||||
#[macro_export]
|
||||
macro_rules! IPRE { () => (concat!(r"(?:", IPV4RE!(), "|", IPV6RE!(), ")")) }
|
||||
|
||||
lazy_static! {
|
||||
pub static ref IP_REGEX: Regex = Regex::new(IPRE!()).unwrap();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue