use dashmap in icons blacklist regex

This commit is contained in:
Daniel García 2022-05-20 23:49:05 +02:00
parent 16ff49d712
commit 699777be9e
No known key found for this signature in database
GPG Key ID: FC8A7D14C3CD543A
1 changed files with 10 additions and 19 deletions

View File

@ -1,5 +1,4 @@
use std::{ use std::{
collections::HashMap,
net::IpAddr, net::IpAddr,
sync::Arc, sync::Arc,
time::{Duration, SystemTime}, time::{Duration, SystemTime},
@ -18,7 +17,6 @@ use tokio::{
fs::{create_dir_all, remove_file, symlink_metadata, File}, fs::{create_dir_all, remove_file, symlink_metadata, File},
io::{AsyncReadExt, AsyncWriteExt}, io::{AsyncReadExt, AsyncWriteExt},
net::lookup_host, net::lookup_host,
sync::RwLock,
}; };
use html5gum::{Emitter, EndTag, InfallibleTokenizer, Readable, StartTag, StringReader, Tokenizer}; use html5gum::{Emitter, EndTag, InfallibleTokenizer, Readable, StartTag, StringReader, Tokenizer};
@ -76,7 +74,7 @@ static CLIENT: Lazy<Client> = Lazy::new(|| {
static ICON_SIZE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?x)(\d+)\D*(\d+)").unwrap()); static ICON_SIZE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?x)(\d+)\D*(\d+)").unwrap());
// Special HashMap which holds the user defined Regex to speedup matching the regex. // Special HashMap which holds the user defined Regex to speedup matching the regex.
static ICON_BLACKLIST_REGEX: Lazy<RwLock<HashMap<String, Regex>>> = Lazy::new(|| RwLock::new(HashMap::new())); static ICON_BLACKLIST_REGEX: Lazy<dashmap::DashMap<String, Regex>> = Lazy::new(dashmap::DashMap::new);
async fn icon_redirect(domain: &str, template: &str) -> Option<Redirect> { async fn icon_redirect(domain: &str, template: &str) -> Option<Redirect> {
if !is_valid_domain(domain).await { if !is_valid_domain(domain).await {
@ -293,32 +291,25 @@ async fn is_domain_blacklisted(domain: &str) -> bool {
} }
if let Some(blacklist) = CONFIG.icon_blacklist_regex() { if let Some(blacklist) = CONFIG.icon_blacklist_regex() {
let mut regex_hashmap = ICON_BLACKLIST_REGEX.read().await;
// Use the pre-generate Regex stored in a Lazy HashMap if there's one, else generate it. // Use the pre-generate Regex stored in a Lazy HashMap if there's one, else generate it.
let regex = if let Some(regex) = regex_hashmap.get(&blacklist) { let is_match = if let Some(regex) = ICON_BLACKLIST_REGEX.get(&blacklist) {
regex regex.is_match(domain)
} else { } else {
drop(regex_hashmap);
let mut regex_hashmap_write = ICON_BLACKLIST_REGEX.write().await;
// Clear the current list if the previous key doesn't exists. // Clear the current list if the previous key doesn't exists.
// To prevent growing of the HashMap after someone has changed it via the admin interface. // To prevent growing of the HashMap after someone has changed it via the admin interface.
if regex_hashmap_write.len() >= 1 { if ICON_BLACKLIST_REGEX.len() >= 1 {
regex_hashmap_write.clear(); ICON_BLACKLIST_REGEX.clear();
} }
// Generate the regex to store in too the Lazy Static HashMap. // Generate the regex to store in too the Lazy Static HashMap.
let blacklist_regex = Regex::new(&blacklist); let blacklist_regex = Regex::new(&blacklist).unwrap();
regex_hashmap_write.insert(blacklist.to_string(), blacklist_regex.unwrap()); let is_match = blacklist_regex.is_match(domain);
drop(regex_hashmap_write); ICON_BLACKLIST_REGEX.insert(blacklist.to_string(), blacklist_regex);
regex_hashmap = ICON_BLACKLIST_REGEX.read().await; is_match
regex_hashmap.get(&blacklist).unwrap()
}; };
// Use the pre-generate Regex stored in a Lazy HashMap. if is_match {
if regex.is_match(domain) {
debug!("Blacklisted domain: {} matched ICON_BLACKLIST_REGEX", domain); debug!("Blacklisted domain: {} matched ICON_BLACKLIST_REGEX", domain);
return true; return true;
} }