Skip to content

Commit

Permalink
use built-in hashset instead of handcrafted boomfilter
Browse files Browse the repository at this point in the history
Signed-off-by: yuguorui <[email protected]>
  • Loading branch information
yuguorui committed Dec 30, 2024
1 parent baea3ca commit fb54dfb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 54 deletions.
8 changes: 4 additions & 4 deletions src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tokio::{
};
use url::Url;

use crate::utils::{to_io_err, BoomHashSet, ToV6Net, ToV6SockAddr};
use crate::utils::{to_io_err, ToV6Net, ToV6SockAddr};
use crate::SETTINGS;

use fast_socks5::client as socks_client;
Expand All @@ -32,7 +32,7 @@ pub struct Outbound {
pub struct Condition {
pub maxmind_regions: Vec<String>,
pub dst_ip_range: IpRange<Ipv6Net>,
pub domains: Option<BoomHashSet<String>>,
pub domains: Option<std::collections::HashSet<String>>,
}

impl Condition {
Expand All @@ -46,15 +46,15 @@ impl Condition {

pub fn match_domain(&self, name: &str) -> bool {
if let Some(ref domains) = self.domains {
if domains.get(&name.to_owned()) {
if domains.get(&name.to_owned()).is_some() {
return true;
}

/* Check the DOMAIN-SUFFIX rules */
let domain = format!(".{}{}", name, RULE_DOMAIN_SUFFIX_TAG);
let indices = domain.match_indices(".");
for index in indices {
if domains.get(&domain[index.0 + 1..].to_string()) {
if domains.get(&domain[index.0 + 1..].to_string()).is_some() {
return true;
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::net::{Ipv4Addr, Ipv6Addr};

use crate::rules::{RouteTable, RULE_DOMAIN_SUFFIX_TAG};

use crate::utils::{vec_to_array, BoomHashSet, ToV6Net};
use crate::utils::{vec_to_array, ToV6Net};

const DIRECT_OUTBOUND_NAME: &str = "DIRECT";
const DROP_OUTBOUND_NAME: &str = "DROP";
Expand Down Expand Up @@ -148,10 +148,11 @@ fn parse_intercept_mode(s: &mut Config) -> Result<InterceptMode, ConfigError> {
.get("mode")
.expect("mode field not found.")
.clone()
.into_str()?.to_lowercase();
.into_str()?
.to_lowercase();
match mode.as_str() {
"manual" => return Ok(InterceptMode::MANUAL),
"auto"|"tproxy"|"redirect" => {
"auto" | "tproxy" | "redirect" => {
let capture_local_traffic = table
.get("local-traffic")
.and_then(|v| {
Expand Down Expand Up @@ -230,7 +231,7 @@ fn parse_intercept_mode(s: &mut Config) -> Result<InterceptMode, ConfigError> {
)
})
.unwrap_or(DEFAULT_IPRULE_TABLE);

if mode.as_str() != "redirect" {
return Ok(InterceptMode::TPROXY {
local_traffic: capture_local_traffic,
Expand All @@ -249,7 +250,6 @@ fn parse_intercept_mode(s: &mut Config) -> Result<InterceptMode, ConfigError> {
proxy_chain,
});
}

}
_ => Err(ConfigError::Message(
"either `auto/tproxy`, `redirect` or `manual` is expected.".to_owned(),
Expand Down Expand Up @@ -415,7 +415,7 @@ fn parse_route_rules(s: &mut Config, route: &mut RouteTable) -> Result<(), Confi

for (i, v) in domain_sets.iter().enumerate() {
let cond = route.rules.get_mut(i).unwrap();
cond.domains = Some(BoomHashSet::new(v.to_owned().into_iter().collect_vec()));
cond.domains = Some(v.clone());
}

Ok(())
Expand Down
44 changes: 0 additions & 44 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,50 +88,6 @@ pub struct BoomHashSet<K: Hash> {
keys: Vec<K>,
}

impl<K> BoomHashSet<K>
where
K: Clone + Hash + Debug + PartialEq,
{
fn create_set(mut keys: Vec<K>, mphf: Mphf<K>) -> BoomHashSet<K> {
// reorder the keys and values according to the Mphf
for i in 0..keys.len() {
loop {
let kmer_slot = mphf.hash(&keys[i]) as usize;
if i == kmer_slot {
break;
}
keys.swap(i, kmer_slot);
}
}
BoomHashSet {
mphf: mphf,
keys: keys,
}
}

/// Create a new hash map from the parallel array `keys` and `values`
pub fn new(keys: Vec<K>) -> BoomHashSet<K> {
let mphf = Mphf::new(1.7, &keys);
Self::create_set(keys, mphf)
}

/// Get the value associated with `key`, if available, otherwise return None
pub fn get(&self, kmer: &K) -> bool {
let maybe_pos = self.mphf.try_hash(kmer);
match maybe_pos {
Some(pos) => {
let hashed_kmer = &self.keys[pos as usize];
if *kmer == hashed_kmer.clone() {
true
} else {
false
}
}
None => false,
}
}
}

pub async fn transfer_tcp(in_sock: &mut TcpStream, rt_context: RouteContext) -> Result<()> {
let mut out_sock = match SETTINGS
.read()
Expand Down

0 comments on commit fb54dfb

Please sign in to comment.