Skip to content

Commit cbb3fdb

Browse files
committed
use built-in hashset instead of handcrafted boomfilter
Signed-off-by: yuguorui <yuguorui@pku.edu.cn>
1 parent baea3ca commit cbb3fdb

File tree

3 files changed

+11
-54
lines changed

3 files changed

+11
-54
lines changed

src/rules.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use tokio::{
1313
};
1414
use url::Url;
1515

16-
use crate::utils::{to_io_err, BoomHashSet, ToV6Net, ToV6SockAddr};
16+
use crate::utils::{to_io_err, ToV6Net, ToV6SockAddr};
1717
use crate::SETTINGS;
1818

1919
use fast_socks5::client as socks_client;
@@ -32,7 +32,7 @@ pub struct Outbound {
3232
pub struct Condition {
3333
pub maxmind_regions: Vec<String>,
3434
pub dst_ip_range: IpRange<Ipv6Net>,
35-
pub domains: Option<BoomHashSet<String>>,
35+
pub domains: Option<std::collections::HashSet<String>>,
3636
}
3737

3838
impl Condition {
@@ -46,15 +46,16 @@ impl Condition {
4646

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

5353
/* Check the DOMAIN-SUFFIX rules */
5454
let domain = format!(".{}{}", name, RULE_DOMAIN_SUFFIX_TAG);
5555
let indices = domain.match_indices(".");
5656
for index in indices {
57-
if domains.get(&domain[index.0 + 1..].to_string()) {
57+
println!("{}", domain[index.0 + 1..].to_string());
58+
if domains.get(&domain[index.0 + 1..].to_string()).is_some() {
5859
return true;
5960
}
6061
}

src/settings.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::net::{Ipv4Addr, Ipv6Addr};
99

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

12-
use crate::utils::{vec_to_array, BoomHashSet, ToV6Net};
12+
use crate::utils::{vec_to_array, ToV6Net};
1313

1414
const DIRECT_OUTBOUND_NAME: &str = "DIRECT";
1515
const DROP_OUTBOUND_NAME: &str = "DROP";
@@ -148,10 +148,11 @@ fn parse_intercept_mode(s: &mut Config) -> Result<InterceptMode, ConfigError> {
148148
.get("mode")
149149
.expect("mode field not found.")
150150
.clone()
151-
.into_str()?.to_lowercase();
151+
.into_str()?
152+
.to_lowercase();
152153
match mode.as_str() {
153154
"manual" => return Ok(InterceptMode::MANUAL),
154-
"auto"|"tproxy"|"redirect" => {
155+
"auto" | "tproxy" | "redirect" => {
155156
let capture_local_traffic = table
156157
.get("local-traffic")
157158
.and_then(|v| {
@@ -230,7 +231,7 @@ fn parse_intercept_mode(s: &mut Config) -> Result<InterceptMode, ConfigError> {
230231
)
231232
})
232233
.unwrap_or(DEFAULT_IPRULE_TABLE);
233-
234+
234235
if mode.as_str() != "redirect" {
235236
return Ok(InterceptMode::TPROXY {
236237
local_traffic: capture_local_traffic,
@@ -249,7 +250,6 @@ fn parse_intercept_mode(s: &mut Config) -> Result<InterceptMode, ConfigError> {
249250
proxy_chain,
250251
});
251252
}
252-
253253
}
254254
_ => Err(ConfigError::Message(
255255
"either `auto/tproxy`, `redirect` or `manual` is expected.".to_owned(),
@@ -415,7 +415,7 @@ fn parse_route_rules(s: &mut Config, route: &mut RouteTable) -> Result<(), Confi
415415

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

421421
Ok(())

src/utils.rs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -88,50 +88,6 @@ pub struct BoomHashSet<K: Hash> {
8888
keys: Vec<K>,
8989
}
9090

91-
impl<K> BoomHashSet<K>
92-
where
93-
K: Clone + Hash + Debug + PartialEq,
94-
{
95-
fn create_set(mut keys: Vec<K>, mphf: Mphf<K>) -> BoomHashSet<K> {
96-
// reorder the keys and values according to the Mphf
97-
for i in 0..keys.len() {
98-
loop {
99-
let kmer_slot = mphf.hash(&keys[i]) as usize;
100-
if i == kmer_slot {
101-
break;
102-
}
103-
keys.swap(i, kmer_slot);
104-
}
105-
}
106-
BoomHashSet {
107-
mphf: mphf,
108-
keys: keys,
109-
}
110-
}
111-
112-
/// Create a new hash map from the parallel array `keys` and `values`
113-
pub fn new(keys: Vec<K>) -> BoomHashSet<K> {
114-
let mphf = Mphf::new(1.7, &keys);
115-
Self::create_set(keys, mphf)
116-
}
117-
118-
/// Get the value associated with `key`, if available, otherwise return None
119-
pub fn get(&self, kmer: &K) -> bool {
120-
let maybe_pos = self.mphf.try_hash(kmer);
121-
match maybe_pos {
122-
Some(pos) => {
123-
let hashed_kmer = &self.keys[pos as usize];
124-
if *kmer == hashed_kmer.clone() {
125-
true
126-
} else {
127-
false
128-
}
129-
}
130-
None => false,
131-
}
132-
}
133-
}
134-
13591
pub async fn transfer_tcp(in_sock: &mut TcpStream, rt_context: RouteContext) -> Result<()> {
13692
let mut out_sock = match SETTINGS
13793
.read()

0 commit comments

Comments
 (0)