api2/node/dns.rs: implement resolv.conf parser

This commit is contained in:
Dietmar Maurer 2019-01-24 14:59:40 +01:00
parent 784252dbc5
commit 8f973f8181
4 changed files with 92 additions and 10 deletions

View File

@ -1,27 +1,75 @@
use failure::*;
use crate::tools;
use crate::tools::common_regex;
use crate::api::schema::*;
use crate::api::router::*;
use lazy_static::lazy_static;
use std::io::{BufRead, BufReader};
use serde_json::{json, Value};
static RESOLV_CONF_FN: &str = "/etc/resolv.conf";
fn read_etc_resolv_conf() -> Result<Value, Error> {
let mut result = json!({});
let mut nscount = 0;
let file = std::fs::File::open(RESOLV_CONF_FN)?;
let mut reader = BufReader::new(file);
let test = IPRE!();
lazy_static! {
static ref DOMAIN_REGEX: regex::Regex = regex::Regex::new(r"^\s*(?:search|domain)\s+(\S+)\s*").unwrap();
static ref SERVER_REGEX: regex::Regex = regex::Regex::new(
concat!(r"^\s*nameserver\s+(", IPRE!(), r")\s*")).unwrap();
}
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) {
nscount += 1;
if nscount > 3 { continue };
let nameserver = m.as_str();
let id = format!("dns{}", nscount);
result[id] = Value::from(m.as_str());
}
}
Ok(result)
}
fn get_dns(_param: Value, _info: &ApiMethod) -> Result<Value, Error> {
Ok(json!({
"search": "test.com",
"dns1": "1.2.3.4",
"dns2": "1.2.3.4",
"dns3": "1.2.3.4",
}))
read_etc_resolv_conf()
}
pub fn router() -> Router {
let route = Router::new()
.get(ApiMethod::new(
get_dns,
ObjectSchema::new("Read DNS settings.")));
.get(
ApiMethod::new(
get_dns,
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."))
)
);
route
}

View File

@ -1,3 +1,4 @@
#[macro_use]
pub mod tools;
/// API definition helper

View File

@ -1,7 +1,6 @@
//! Tools and utilities
//!
//! This is a collection of small and useful tools.
use failure::*;
use nix::unistd;
use nix::sys::stat;
@ -22,6 +21,8 @@ use serde_json::Value;
pub mod timer;
pub mod wrapped_reader_stream;
#[macro_use]
pub mod common_regex;
/// The `BufferedReader` trait provides a single function
/// `buffered_read`. It returns a reference to an internal buffer. The

32
src/tools/common_regex.rs Normal file
View File

@ -0,0 +1,32 @@
//! Predefined Regular Expressions
//!
//! This is a collection of useful regular expressions
use lazy_static::lazy_static;
#[macro_export]
macro_rules! IPV4OCTET { () => (r"(?:25[0-5]|(?:2[0-4]|1[0-9]|[1-9])?[0-9])") }
#[macro_export]
macro_rules! IPV6H16 { () => (r"(?:[0-9a-fA-F]{1,4})") }
#[macro_export]
macro_rules! IPV6LS32 { () => (concat!(r"(?:(?:", IPV4RE!(), "|", IPV6H16!(), ":", IPV6H16!(), "))" )) }
#[macro_export]
macro_rules! IPV4RE { () => (concat!(r"(?:(?:", IPV4OCTET!(), r"\.){3}", IPV4OCTET!(), ")")) }
#[macro_export]
macro_rules! IPV6RE { () => (concat!(r"(?:",
r"(?:(?:", r"(?:", IPV6H16!(), r":){6})", IPV6LS32!(), r")|",
r"(?:(?:", r"::(?:", IPV6H16!(), r":){5})", IPV6LS32!(), r")|",
r"(?:(?:(?:", IPV6H16!(), r")?::(?:", IPV6H16!(), r":){4})", IPV6LS32!(), r")|",
r"(?:(?:(?:(?:", IPV6H16!(), r":){0,1}", IPV6H16!(), r")?::(?:", IPV6H16!(), r":){3})", IPV6LS32!(), r")|",
r"(?:(?:(?:(?:", IPV6H16!(), r":){0,2}", IPV6H16!(), r")?::(?:", IPV6H16!(), r":){2})", IPV6LS32!(), r")|",
r"(?:(?:(?:(?:", IPV6H16!(), r":){0,3}", IPV6H16!(), r")?::(?:", IPV6H16!(), r":){1})", IPV6LS32!(), r")|",
r"(?:(?:(?:(?:", IPV6H16!(), r":){0,4}", IPV6H16!(), r")?::", ")", IPV6LS32!(), r")|",
r"(?:(?:(?:(?:", IPV6H16!(), r":){0,5}", IPV6H16!(), r")?::", ")", IPV6H16!(), r")|",
r"(?:(?:(?:(?:", IPV6H16!(), r":){0,6}", IPV6H16!(), r")?::", ")))"))
}
#[macro_export]
macro_rules! IPRE { () => (concat!(r"(?:", IPV4RE!(), "|", IPV6RE!(), ")")) }