Skip to content

Commit

Permalink
refactored a lot of code + cleaned up most logging statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
KalevGonvick committed Mar 3, 2024
1 parent d8c0e7f commit d2c35c7
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 163 deletions.
13 changes: 1 addition & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
<pre>
████████▄ ███▄▄▄▄ ▄████████ ████████▄ ▄████████ ▄██████▄ ▄███████▄ ▄███████▄ ▄████████ ▄████████
███ ▀███ ███▀▀▀██▄ ███ ███ ███ ▀███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███
███ ███ ███ ███ ███ █▀ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ █▀ ███ ███
███ ███ ███ ███ ███ ███ ███ ▄███▄▄▄▄██▀ ███ ███ ███ ███ ███ ███ ▄███▄▄▄ ▄███▄▄▄▄██▀
███ ███ ███ ███ ▀███████████ ███ ███ ▀▀███▀▀▀▀▀ ███ ███ ▀█████████▀ ▀█████████▀ ▀▀███▀▀▀ ▀▀███▀▀▀▀▀
███ ███ ███ ███ ███ ███ ███ ▀███████████ ███ ███ ███ ███ ███ █▄ ▀███████████
███ ▄███ ███ ███ ▄█ ███ ███ ▄███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███
████████▀ ▀█ █▀ ▄████████▀ ████████▀ ███ ███ ▀██████▀ ▄████▀ ▄████▀ ██████████ ███ ███
███ ███
</pre>
<img src="./docs/dns_dropper_banner01.png" alt="DNSDropper">

## What is it?
DNSDropper is a tool for anyone looking for a light-weight dns proxy with filtering capabilities. Like blocking ads! It works by being a proxy in-between you and your normal DNS service, filtering any DNS requests for domains in your black list.
Expand All @@ -24,7 +14,6 @@ DNSDropper uses in a single configuration file that is divided up by major featu

You can also find examples of different configurations under the ```test/``` folder.


## How to use (standard configuration)
1. Configure your ```server.yaml``` to fit your needs, and run the service.
1. To specify the config directory, use the ```--config``` or ```-c``` argument. e.g. ```dns_dropper --config config/myconfig.yaml```
Expand Down
2 changes: 1 addition & 1 deletion config/internal.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Config constants internal systems
default_server_config_dir: "config/server.yaml"
max_udp_packet_size: 4096
worker_thread_name: "WORKER"
worker_thread_name: "WT"
Binary file added docs/dns_dropper_banner01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
111 changes: 61 additions & 50 deletions src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,76 +12,87 @@ pub(crate) struct Filter {
pub domain: String,
}

pub(crate) fn should_filter(domain: &String, filter_list: &HashSet<Filter>) -> bool {
for entry in filter_list {
if &entry.domain == domain {
impl Filter {
fn is_domain_matching(&self, in_domain: &String) -> bool {
return self.domain.eq(in_domain);
}
}

pub(crate) fn should_filter(
domain: &String,
filter_list: &HashSet<Filter>
) -> bool {
for filter in filter_list {
if filter.is_domain_matching(domain) {
return true;
}
}
return false;
}

pub(crate) async fn load_block_list(block_list: &[Cow<'_, str>]) -> HashSet<Filter> {
pub(crate) async fn load_filtered_domains(
block_list: &[Cow<'_, str>]
) -> HashSet<Filter> {
let mut complete_block_list: HashSet<Filter> = HashSet::new();

for source in block_list {
match Url::parse(source) {
Ok(url) => {
if url.scheme().eq("file") {
match fs::read_to_string(source.clone().into_owned()) {
Ok(content) => {
parse_block_list_content(&mut complete_block_list, content)
.unwrap_or_else(|error| error!("Error occurred while trying to parse content from provided url resource: {} {}", source, error));
}
Err(err) => {
error!("Error occurred while reading file '{}': {}", source, err);
}
};
} else if url.scheme().eq("http") || url.scheme().eq("https") {
match reqwest::get(source.clone().into_owned()).await {
Ok(res) => {
trace!("Got response from block-list source: {}", source);
if res.status() == StatusCode::OK {
if let Ok(body) = res.text().await {
parse_block_list_content(&mut complete_block_list, body)
.unwrap_or_else(|error| error!("Error occurred while trying to parse content from provided url resource: {} {}", source, error));
}
}
}
Err(err) => {
error!("Error occurred while requesting resource from '{}': {}", source, err);
}
};

if let Ok(url) = Url::parse(source) {

if url.scheme().eq("file") {

if let Ok(content) = fs::read_to_string(source.clone().into_owned()) {
parse_block_list_content(&mut complete_block_list, content).unwrap();
}
}

Err(_) => {
trace!("Provided string '{}' is not a URL, trying as an external file.", source);
match fs::File::open(source.clone().into_owned()) {
Ok(file) => {
let mut buf_reader = BufReader::new(file);
let mut body = String::new();
match buf_reader.read_to_string(&mut body) {
Ok(_) => {
parse_block_list_content(&mut complete_block_list, body)
.unwrap_or_else(|error| error!("Error occurred while trying to parse content from provided local resource: {} {}", source, error));
}
Err(err) => {
error!("Error occurred while reading file '{}': {}", source, err);
}
} else if url.scheme().eq("http") || url.scheme().eq("https") {

if let Ok(res) = reqwest::get(source.clone().into_owned()).await {
trace!("Got response from block-list source: {}", source);

if res.status() == StatusCode::OK {

if let Ok(body) = res.text().await {
parse_block_list_content(&mut complete_block_list, body).unwrap();
}

} else {
error!("Error! Response was: {}", res.status());
}
Err(err) => {
error!("Error occurred while reading file '{}': {}", source, err);
}

} else {
error!("Error occurred while requesting resource from '{}'", source);
};
}

} else {
trace!("Provided string '{}' is not a URL, trying as an external file.", source);

if let Ok(file) = fs::File::open(source.clone().into_owned()) {
let mut buf_reader = BufReader::new(file);
let mut body = String::new();

if let Ok(_) = buf_reader.read_to_string(&mut body) {
parse_block_list_content(&mut complete_block_list, body).unwrap();

} else if let Err(err) = buf_reader.read_to_string(&mut body) {
error!("Error occurred while reading file '{}': {}", source, err);
}

} else if let Err(err) = fs::File::open(source.clone().into_owned()) {
error!("Error occurred while reading file '{}': {}", source, err);
}
};
}

return complete_block_list;
}

fn parse_block_list_content(complete_block_list: &mut HashSet<Filter>, content: String) -> Result<()> {
fn parse_block_list_content(
complete_block_list: &mut HashSet<Filter>,
content: String
) -> Result<()> {

let mut filter: Filter;

for line in content.lines() {
Expand Down
2 changes: 1 addition & 1 deletion src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ pub struct InternalConfig {
pub const INTERNAL_CONFIG: InternalConfig = InternalConfig {
default_server_config_dir: Cow::Borrowed("config/server.yaml"),
max_udp_packet_size: 4096,
worker_thread_name: Cow::Borrowed("WORKER"),
worker_thread_name: Cow::Borrowed("WT"),
};
51 changes: 23 additions & 28 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,28 @@ use log::Level;
use std::io::Write;
use crate::logging::HighlightStyle::{DebugHighlight, ErrorHighlight, InfoHighlight, TraceHighlight, WarnHighLight};

const DARK_GREY_HIGHLIGHT: Style = Style::new()
pub const DARK_GREY_HIGHLIGHT: Style = Style::new()
.fg_color(Some(Color::Ansi256(Ansi256Color(8))));
const RED_HIGHLIGHT: Style = Style::new()
pub const RED_HIGHLIGHT: Style = Style::new()
.fg_color(Some(Color::Ansi256(Ansi256Color(9))));
const GREEN_HIGHLIGHT: Style = Style::new()
pub const GREEN_HIGHLIGHT: Style = Style::new()
.fg_color(Some(Color::Ansi256(Ansi256Color(10))));
const YELLOW_HIGHLIGHT: Style = Style::new()
pub const YELLOW_HIGHLIGHT: Style = Style::new()
.fg_color(Some(Color::Ansi256(Ansi256Color(11))));
const BLUE_HIGHLIGHT: Style = Style::new()
pub const BLUE_HIGHLIGHT: Style = Style::new()
.fg_color(Some(Color::Ansi256(Ansi256Color(12))));
const PURPLE_HIGHLIGHT: Style = Style::new()
pub const PURPLE_HIGHLIGHT: Style = Style::new()
.fg_color(Some(Color::Ansi256(Ansi256Color(13))));
const AQUA_HIGHLIGHT: Style = Style::new()
pub const AQUA_HIGHLIGHT: Style = Style::new()
.fg_color(Some(Color::Ansi256(Ansi256Color(14))));

const DEFAULT_STYLE: Style = BLUE_HIGHLIGHT;
const TRACE_STYLE: Style = PURPLE_HIGHLIGHT.bold();
const INFO_STYLE: Style = BLUE_HIGHLIGHT.bold();
const ERROR_STYLE: Style = RED_HIGHLIGHT.bold();
const DEBUG_STYLE: Style = GREEN_HIGHLIGHT.bold();
const WARN_STYLE: Style = YELLOW_HIGHLIGHT.bold();
const TIMESTAMP_STYLE: Style = DARK_GREY_HIGHLIGHT.underline();
const THREAD_NAME_STYLE: Style = AQUA_HIGHLIGHT.bold();
const MODULE_INFO_STYLE: Style = YELLOW_HIGHLIGHT.italic();
pub const DEFAULT_STYLE: Style = BLUE_HIGHLIGHT;
pub const TRACE_STYLE: Style = PURPLE_HIGHLIGHT.bold();
pub const INFO_STYLE: Style = BLUE_HIGHLIGHT.bold();
pub const ERROR_STYLE: Style = RED_HIGHLIGHT.bold();
pub const DEBUG_STYLE: Style = GREEN_HIGHLIGHT.bold();
pub const WARN_STYLE: Style = YELLOW_HIGHLIGHT.bold();
pub const TIMESTAMP_STYLE: Style = DARK_GREY_HIGHLIGHT.underline();
pub const THREAD_NAME_STYLE: Style = AQUA_HIGHLIGHT.bold();

pub enum HighlightStyle {
TraceHighlight,
Expand Down Expand Up @@ -82,21 +80,18 @@ pub fn setup(level: &str) {
}
};



let ts = buf.timestamp_millis();
let mod_path = record.module_path();
let mod_line = record.line();
let lvl = record.level();
let args = record.args();

writeln!(buf, "[{TIMESTAMP_STYLE}{}{TIMESTAMP_STYLE:#}][{THREAD_NAME_STYLE}{}{THREAD_NAME_STYLE:#}][{level_colour}{}{level_colour:#}][{MODULE_INFO_STYLE}{}.rs::{}{MODULE_INFO_STYLE:#}] {DEFAULT_STYLE}{}{DEFAULT_STYLE:#}",
ts,
thread::current().name().unwrap_or_default().to_ascii_uppercase(),
lvl,
mod_path.unwrap_or_default(),
mod_line.unwrap_or_default(),
args)
writeln!(
buf,
"[{TIMESTAMP_STYLE}{}{TIMESTAMP_STYLE:#}][{level_colour}{}{level_colour:#}][{THREAD_NAME_STYLE}{}{THREAD_NAME_STYLE:#}] {DEFAULT_STYLE}{}{DEFAULT_STYLE:#}",
ts,
lvl,
thread::current().name().unwrap_or_default().to_ascii_uppercase(),
args
)
}).init();
}

Expand Down
Loading

0 comments on commit d2c35c7

Please sign in to comment.