Break out API for testability
Signed-off-by: Harsh Shandilya <msfjarvis@gmail.com>
This commit is contained in:
parent
9797b4e837
commit
f4c00d3d65
|
@ -0,0 +1,61 @@
|
|||
use regex::Regex;
|
||||
|
||||
const REGEX_PATTERN: &str = r"^\s*(\d+\.\d+\.\d+\.\d+)\s+([\w\-\.]+)";
|
||||
// I don't fully understand this and would appreciate help :)
|
||||
const LOCALHOST_ADDRS: &[&str] = &[
|
||||
"localhost",
|
||||
"local",
|
||||
"localhost.localdomain",
|
||||
"broadcasthost",
|
||||
];
|
||||
|
||||
pub fn format_to_unbound(raw_hosts: String, ipv4_addr: &str, ipv6_addr: &str) -> Vec<String> {
|
||||
let mut output: Vec<String> = vec![String::from("server:")];
|
||||
let re = Regex::new(REGEX_PATTERN).unwrap();
|
||||
for line in raw_hosts.lines() {
|
||||
if !re.is_match(line) {
|
||||
continue;
|
||||
}
|
||||
for cap in re.captures_iter(line) {
|
||||
if !LOCALHOST_ADDRS.contains(&&cap[2]) {
|
||||
output.push(format!(" local-zone: {} redirect", &cap[2]));
|
||||
output.push(format!(" local-zone: {} A {}", &cap[2], ipv4_addr));
|
||||
output.push(format!(" local-zone: {} AAAAA {}", &cap[2], ipv6_addr))
|
||||
}
|
||||
}
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
pub fn format_to_dnsmasq_server(raw_hosts: String) -> Vec<String> {
|
||||
let mut output: Vec<String> = vec![];
|
||||
let re = Regex::new(REGEX_PATTERN).unwrap();
|
||||
for line in raw_hosts.lines() {
|
||||
if !re.is_match(line) {
|
||||
continue;
|
||||
}
|
||||
for cap in re.captures_iter(line) {
|
||||
if !LOCALHOST_ADDRS.contains(&&cap[2]) {
|
||||
output.push(format!("server=/{}/", &cap[2]))
|
||||
}
|
||||
}
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
pub fn format_to_dnsmasq(raw_hosts: String, ipv4_addr: &str, ipv6_addr: &str) -> Vec<String> {
|
||||
let mut output: Vec<String> = vec![];
|
||||
let re = Regex::new(REGEX_PATTERN).unwrap();
|
||||
for line in raw_hosts.lines() {
|
||||
if !re.is_match(line) {
|
||||
continue;
|
||||
}
|
||||
for cap in re.captures_iter(line) {
|
||||
if !LOCALHOST_ADDRS.contains(&&cap[2]) {
|
||||
output.push(format!("address=/{}/{}", &cap[2], ipv4_addr));
|
||||
output.push(format!("address=/{}/{}", &cap[2], ipv6_addr))
|
||||
}
|
||||
}
|
||||
}
|
||||
output
|
||||
}
|
|
@ -2,6 +2,7 @@ extern crate clap;
|
|||
extern crate regex;
|
||||
extern crate reqwest;
|
||||
|
||||
mod formatters;
|
||||
mod source;
|
||||
|
||||
use clap::{load_yaml, App};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use regex::Regex;
|
||||
use crate::formatters;
|
||||
use std::error::Error;
|
||||
|
||||
pub struct Source {
|
||||
|
@ -6,71 +6,23 @@ pub struct Source {
|
|||
}
|
||||
|
||||
impl Source {
|
||||
// I don't fully understand this and would appreciate help :)
|
||||
const LOCALHOST_ADDRS: &'static [&'static str] = &[
|
||||
"localhost",
|
||||
"local",
|
||||
"localhost.localdomain",
|
||||
"broadcasthost",
|
||||
];
|
||||
const REGEX_PATTERN: &'static str = r"^\s*(\d+\.\d+\.\d+\.\d+)\s+([\w\-\.]+)";
|
||||
|
||||
pub fn download_to_string(&self) -> Result<String, Box<dyn Error>> {
|
||||
let req = reqwest::get(self.url.as_str())?.text()?;
|
||||
Ok(req)
|
||||
}
|
||||
|
||||
pub fn format_to_dnsmasq(&self, ipv4_addr: &str, ipv6_addr: &str) -> Vec<String> {
|
||||
let mut output: Vec<String> = vec![];
|
||||
let re = Regex::new(Self::REGEX_PATTERN).unwrap();
|
||||
let raw_hosts = self.download_to_string().unwrap();
|
||||
for line in raw_hosts.lines() {
|
||||
if !re.is_match(line) {
|
||||
continue;
|
||||
}
|
||||
for cap in re.captures_iter(line) {
|
||||
if !Self::LOCALHOST_ADDRS.contains(&&cap[2]) {
|
||||
output.push(format!("address=/{}/{}", &cap[2], ipv4_addr));
|
||||
output.push(format!("address=/{}/{}", &cap[2], ipv6_addr))
|
||||
}
|
||||
}
|
||||
}
|
||||
output
|
||||
formatters::format_to_dnsmasq(raw_hosts, ipv4_addr, ipv6_addr)
|
||||
}
|
||||
|
||||
pub fn format_to_dnsmasq_server(&self) -> Vec<String> {
|
||||
let mut output: Vec<String> = vec![];
|
||||
let re = Regex::new(Self::REGEX_PATTERN).unwrap();
|
||||
let raw_hosts = self.download_to_string().unwrap();
|
||||
for line in raw_hosts.lines() {
|
||||
if !re.is_match(line) {
|
||||
continue;
|
||||
}
|
||||
for cap in re.captures_iter(line) {
|
||||
if !Self::LOCALHOST_ADDRS.contains(&&cap[2]) {
|
||||
output.push(format!("server=/{}/", &cap[2]))
|
||||
}
|
||||
}
|
||||
}
|
||||
output
|
||||
formatters::format_to_dnsmasq_server(raw_hosts)
|
||||
}
|
||||
|
||||
pub fn format_to_unbound(&self, ipv4_addr: &str, ipv6_addr: &str) -> Vec<String> {
|
||||
let mut output: Vec<String> = vec![String::from("server:")];
|
||||
let re = Regex::new(Self::REGEX_PATTERN).unwrap();
|
||||
let raw_hosts = self.download_to_string().unwrap();
|
||||
for line in raw_hosts.lines() {
|
||||
if !re.is_match(line) {
|
||||
continue;
|
||||
}
|
||||
for cap in re.captures_iter(line) {
|
||||
if !Self::LOCALHOST_ADDRS.contains(&&cap[2]) {
|
||||
output.push(format!(" local-zone: {} redirect", &cap[2]));
|
||||
output.push(format!(" local-zone: {} A {}", &cap[2], ipv4_addr));
|
||||
output.push(format!(" local-zone: {} AAAAA {}", &cap[2], ipv6_addr))
|
||||
}
|
||||
}
|
||||
}
|
||||
output
|
||||
formatters::format_to_unbound(raw_hosts, ipv4_addr, ipv6_addr)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue