Skip to content

Commit

Permalink
added fix for only using the top remote host of the server config
Browse files Browse the repository at this point in the history
  • Loading branch information
KalevGonvick committed Feb 5, 2024
1 parent 74ca06e commit 02fe7f8
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 243 deletions.
1 change: 0 additions & 1 deletion config/server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ udp_proxy:
- https://raw.githubusercontent.com/blocklistproject/Lists/master/ads.txt
- config/local_block_list.txt


logging:
enabled: true
level: "trace"
1 change: 0 additions & 1 deletion src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ impl RecordType {
RecordType::MX => 15,
RecordType::AAAA => 28,
RecordType::HTTPS => 65

}
}

Expand Down
73 changes: 0 additions & 73 deletions src/dns_proxy.rs

This file was deleted.

100 changes: 80 additions & 20 deletions src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,100 @@
use std::borrow::Cow;
use std::collections::HashSet;
use log::info;
use crate::logging;
use crate::logging::HighlightStyle::DefaultHighlight;
use std::fs;
use std::io::{BufReader, Read};
use log::{error, trace};
use reqwest::StatusCode;
use url::Url;

#[derive(Hash, Eq, PartialEq, Debug)]
pub(crate) struct Filter {
pub address: String,
pub domain: String
}

impl Filter {
fn matches(&self, address: String, domain: String) -> bool {
return address.eq(&self.address) && domain.eq(&self.domain);
}
pub domain: String,
}

pub(crate) fn should_filter(domain: String, filter_list: &HashSet<Filter>) -> bool {
let style = logging::get_highlight_style(DefaultHighlight);
for entry in filter_list {
if entry.domain == domain {
info!("Block-List contains the name '{style}{}{style:#}'", domain);
return true;
}
}
return false;
}

#[cfg(test)]
mod tests {
use crate::dns_proxy::DnsProxy;
use super::*;
pub(crate) async fn load_block_list(block_list: &[Cow<'_, str>]) -> HashSet<Filter> {
let mut complete_block_list: HashSet<Filter> = HashSet::new();

for source in block_list {
trace!("Found block-list source: {}", source);

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);
}
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);
}
}
}
Err(err) => {
error!("Error occurred while requesting resource from '{}': {}", source, err);
}
};
}
}

#[test]
fn filter_test() {
let proxy: DnsProxy = DnsProxy {
complete_block_list: HashSet::new(),
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);
}
Err(err) => {
error!("Error occurred while reading file '{}': {}", source, err);
}
}
}
Err(err) => {
error!("Error occurred while reading file '{}': {}", source, err);
}
}
}
};
}
return complete_block_list;
}

fn parse_block_list_content(
complete_block_list:
&mut std::collections::HashSet<Filter>, content: String,
) {
let mut filter: Filter;
for line in content.lines() {
let split_line: Vec<&str> = line.split_whitespace().collect();

// we expect lines to follow the pattern of <addr>/s<domain>/n
if split_line.len() > 1 && split_line.len() < 3 {
filter = Filter {
address: split_line.get(0).unwrap().to_string(),
domain: split_line.get(1).unwrap().to_string(),
};
complete_block_list.insert(filter);
}
}
}
36 changes: 23 additions & 13 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@ use std::io::Write;
use crate::logging::HighlightStyle::{DebugHighlight, ErrorHighlight, InfoHighlight, TraceHighlight, WarnHighLight};

const TIMESTAMP_STYLE: Style = Style::new()
.fg_color(Some(Color::Ansi256(Ansi256Color(8))))
.underline();
.fg_color(Some(Color::Ansi256(Ansi256Color(8))))
.underline();

const THREAD_NAME_STYLE: Style = Style::new()
.fg_color(Some(Color::Ansi256(Ansi256Color(14))));
.fg_color(Some(Color::Ansi256(Ansi256Color(14))));

const MODULE_INFO_STYLE: Style = Style::new()
.fg_color(Some(Color::Ansi256(Ansi256Color(11))))
.italic();
.fg_color(Some(Color::Ansi256(Ansi256Color(11))))
.italic();

pub enum HighlightStyle {
DefaultHighlight,
TraceHighlight,
DebugHighlight,
InfoHighlight,
WarnHighLight,
ErrorHighlight
ErrorHighlight,
}

pub fn setup(level: &str) {
Expand All @@ -31,8 +30,6 @@ pub fn setup(level: &str) {
env_logger::builder()
.parse_env(level_filter)
.format(|buf, record| {


let level_colour: Style = match record.level() {
Level::Error => {
get_highlight_style(ErrorHighlight)
Expand Down Expand Up @@ -69,9 +66,6 @@ pub fn setup(level: &str) {

pub fn get_highlight_style(style: HighlightStyle) -> Style {
return match style {
HighlightStyle::DefaultHighlight => {
Style::new().fg_color(Some(Color::Ansi256(Ansi256Color(166)))).bold()
}
TraceHighlight => {
Style::new().fg_color(Some(Color::Ansi256(Ansi256Color(13)))).bold()
}
Expand All @@ -87,5 +81,21 @@ pub fn get_highlight_style(style: HighlightStyle) -> Style {
ErrorHighlight => {
Style::new().fg_color(Some(Color::Ansi256(Ansi256Color(9)))).bold()
}
}
};
}

pub(crate) fn print_title() {
let title_style = Style::new().fg_color(Some(Color::Ansi256(Ansi256Color(13))));
let art = r#"
████████▄ ███▄▄▄▄ ▄████████ ████████▄ ▄████████ ▄██████▄ ▄███████▄ ▄███████▄ ▄████████ ▄████████
███ ▀███ ███▀▀▀██▄ ███ ███ ███ ▀███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███
███ ███ ███ ███ ███ █▀ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ █▀ ███ ███
███ ███ ███ ███ ███ ███ ███ ▄███▄▄▄▄██▀ ███ ███ ███ ███ ███ ███ ▄███▄▄▄ ▄███▄▄▄▄██▀
███ ███ ███ ███ ▀███████████ ███ ███ ▀▀███▀▀▀▀▀ ███ ███ ▀█████████▀ ▀█████████▀ ▀▀███▀▀▀ ▀▀███▀▀▀▀▀
███ ███ ███ ███ ███ ███ ███ ▀███████████ ███ ███ ███ ███ ███ █▄ ▀███████████
███ ▄███ ███ ███ ▄█ ███ ███ ▄███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███
████████▀ ▀█ █▀ ▄████████▀ ████████▀ ███ ███ ▀██████▀ ▄████▀ ▄████▀ ██████████ ███ ███
███ ███ ███ ███
"#;
println!("{title_style}{}{title_style:#}", art);
}
Loading

0 comments on commit 02fe7f8

Please sign in to comment.