Skip to content
API Documentation
BastionGuard™
fastfilter::Engine Class Reference

#include <fast_filter.h>

Public Member Functions

 Engine ()
 Engine (const Config &c)
bool load_patterns_file (const std::string &path)
bool load_regex_file (const std::string &path)
bool load_patterns_sqlite (const std::string &db_path, const std::string &table="phishing_rules")
Verdict check_url (const std::string &url)
size_t get_pattern_count () const

Private Member Functions

void build_bloom (size_t n)

Static Private Member Functions

static void trim (std::string &s)
static void tolower_inplace (std::string &s)
static std::string lowercase (const std::string &s)

Private Attributes

Config cfg
Bloom bloom
Automaton ac
LRUCache cache
size_t total_patterns =0

Detailed Description

Definition at line 150 of file fast_filter.h.

Constructor & Destructor Documentation

◆ Engine() [1/2]

fastfilter::Engine::Engine ( )
inline

Definition at line 152 of file fast_filter.h.

152: cfg(Config{}), cache(cfg.cache_capacity) {}

References cache, and cfg.

◆ Engine() [2/2]

fastfilter::Engine::Engine ( const Config & c)
inlineexplicit

Definition at line 153 of file fast_filter.h.

153: cfg(c), cache(c.cache_capacity) {}

References cache, and cfg.

Member Function Documentation

◆ build_bloom()

void fastfilter::Engine::build_bloom ( size_t n)
inlineprivate

Definition at line 199 of file fast_filter.h.

199{ bloom.init(n>0?n:1, cfg.bloom_fp_rate); }
void init(size_t n_items, double fp_rate)
Definition fast_filter.h:89

References bloom, fastfilter::Config::bloom_fp_rate, cfg, and fastfilter::Bloom::init().

Referenced by load_patterns_file(), and load_patterns_sqlite().

◆ check_url()

Verdict fastfilter::Engine::check_url ( const std::string & url)
inline

Definition at line 176 of file fast_filter.h.

176 {
177 auto norm = lowercase(url);
178 uint64_t key = murmur3_64(norm.data(), norm.size()); bool cv; if(cache.get(key, cv)) return {cv, cv?Verdict::AC:Verdict::NONE, cv?"cache-hit": "cache-hit"};
179 if(!bloom.m_bits || !bloom.might_contain(norm)){ cache.put(key,false); return {false, Verdict::BLOOM, "bloom-negative"}; }
180 bool hit = ac.search_any(norm);
181 if(!hit){ cache.put(key,false); return {false, Verdict::AC, "ac-nohit"}; }
182#ifdef FASTFILTER_USE_RE2
183 // stage 3: regex (if any)
184 for(const auto& r: regexes){ if(RE2::PartialMatch(norm, *r)){ cache.put(key,true); return {true, Verdict::REGEX, "regex-match"}; } }
185 cache.put(key,true); return {true, Verdict::AC, "ac-match"};
186#else
187 cache.put(key,true); return {true, Verdict::AC, "ac-match"};
188#endif
189 }
static std::string lowercase(const std::string &s)
static uint64_t murmur3_64(const void *key, size_t len, uint32_t seed=0xB00B1E)
Definition fast_filter.h:52
bool search_any(const std::string &s) const
bool might_contain(const std::string &s) const
bool get(uint64_t k, bool &out)
void put(uint64_t k, bool v)

References fastfilter::Verdict::AC, ac, fastfilter::Verdict::BLOOM, bloom, cache, fastfilter::LRUCache::get(), lowercase(), fastfilter::Bloom::m_bits, fastfilter::Bloom::might_contain(), fastfilter::murmur3_64(), fastfilter::Verdict::NONE, fastfilter::LRUCache::put(), fastfilter::Verdict::REGEX, and fastfilter::Automaton::search_any().

◆ get_pattern_count()

size_t fastfilter::Engine::get_pattern_count ( ) const
inline

Definition at line 191 of file fast_filter.h.

191{ return total_patterns; }

References total_patterns.

◆ load_patterns_file()

bool fastfilter::Engine::load_patterns_file ( const std::string & path)
inline

Definition at line 156 of file fast_filter.h.

156 {
157 std::ifstream in(path); if(!in) return false; std::vector<std::string> pats; pats.reserve(100000);
158 std::string line; while(std::getline(in,line)){ trim(line); if(line.empty()||line[0]=='#') continue; tolower_inplace(line); pats.push_back(line); }
159 build_bloom(pats.size()); for(const auto& p: pats){ bloom.add(p);} for(const auto& p: pats){ ac.add(p);} ac.build(); total_patterns = pats.size(); return true;
160 }
static void tolower_inplace(std::string &s)
static void trim(std::string &s)
void build_bloom(size_t n)
void add(const std::string &pat)
void add(const std::string &s)
Definition fast_filter.h:99

References ac, fastfilter::Automaton::add(), fastfilter::Bloom::add(), bloom, fastfilter::Automaton::build(), build_bloom(), tolower_inplace(), total_patterns, and trim().

◆ load_patterns_sqlite()

bool fastfilter::Engine::load_patterns_sqlite ( const std::string & db_path,
const std::string & table = "phishing_rules" )
inline

Definition at line 207 of file fast_filter.h.

207 {
208 sqlite3* db = nullptr;
209 if (sqlite3_open(db_path.c_str(), &db) != SQLITE_OK) return false;
210
211 std::string sql = "SELECT pattern, is_regex FROM " + table + ";";
212 sqlite3_stmt* stmt = nullptr;
213 if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
214 sqlite3_close(db);
215 return false;
216 }
217
218 std::vector<std::string> pats; pats.reserve(100000);
219 std::vector<std::string> regex_pats; regex_pats.reserve(10000);
220
221 while (sqlite3_step(stmt) == SQLITE_ROW) {
222 const unsigned char* p = sqlite3_column_text(stmt, 0);
223 int is_regex = sqlite3_column_int(stmt, 1);
224 if (!p) continue;
225 std::string s = reinterpret_cast<const char*>(p);
226 trim(s); tolower_inplace(s);
227 if (s.empty()) continue;
228 if (is_regex) regex_pats.push_back(s); else pats.push_back(s);
229 }
230 sqlite3_finalize(stmt);
231 sqlite3_close(db);
232
233 // build bloom + AC
234 build_bloom(pats.size());
235 for (auto& p : pats) bloom.add(p);
236 for (auto& p : pats) ac.add(p);
237 ac.build();
238 total_patterns = pats.size();
239
240#ifdef FASTFILTER_USE_RE2
241 regexes.clear();
242 for (auto& r : regex_pats) regexes.emplace_back(std::make_unique<RE2>(r));
243#endif
244
245 return true;
246}

References ac, fastfilter::Automaton::add(), fastfilter::Bloom::add(), bloom, fastfilter::Automaton::build(), build_bloom(), tolower_inplace(), total_patterns, and trim().

◆ load_regex_file()

bool fastfilter::Engine::load_regex_file ( const std::string & path)
inline

Definition at line 162 of file fast_filter.h.

162 {
163#ifdef FASTFILTER_USE_RE2
164 std::ifstream in(path); if(!in) return false; std::string line; size_t cnt=0; regexes.clear();
165 while(std::getline(in,line)){
166 trim(line); if(line.rfind("Pattern:",0)==0){ std::string pat = line.substr(8); trim(pat); if(!pat.empty()){ regexes.emplace_back(std::make_unique<RE2>(pat)); cnt++; } }
167 }
168 return cnt>0;
169#else
170 (void)path; return false;
171#endif
172 }

References trim().

◆ lowercase()

std::string fastfilter::Engine::lowercase ( const std::string & s)
inlinestaticprivate

Definition at line 203 of file fast_filter.h.

203{ std::string t=s; tolower_inplace(t); return t; }

References tolower_inplace().

Referenced by check_url().

◆ tolower_inplace()

void fastfilter::Engine::tolower_inplace ( std::string & s)
inlinestaticprivate

Definition at line 202 of file fast_filter.h.

202{ for(char& c: s) c=(char)std::tolower((unsigned char)c); }

Referenced by load_patterns_file(), load_patterns_sqlite(), and lowercase().

◆ trim()

void fastfilter::Engine::trim ( std::string & s)
inlinestaticprivate

Definition at line 201 of file fast_filter.h.

201{ size_t a=0; while(a<s.size() && std::isspace((unsigned char)s[a])) a++; size_t b=s.size(); while(b>a && std::isspace((unsigned char)s[b-1])) b--; s.assign(s.substr(a,b-a)); }

Referenced by load_patterns_file(), load_patterns_sqlite(), and load_regex_file().

Member Data Documentation

◆ ac

Automaton fastfilter::Engine::ac
private

Definition at line 194 of file fast_filter.h.

Referenced by check_url(), load_patterns_file(), and load_patterns_sqlite().

◆ bloom

Bloom fastfilter::Engine::bloom
private

Definition at line 194 of file fast_filter.h.

Referenced by build_bloom(), check_url(), load_patterns_file(), and load_patterns_sqlite().

◆ cache

LRUCache fastfilter::Engine::cache
private

Definition at line 194 of file fast_filter.h.

Referenced by check_url(), Engine(), and Engine().

◆ cfg

Config fastfilter::Engine::cfg
private

Definition at line 194 of file fast_filter.h.

Referenced by build_bloom(), Engine(), and Engine().

◆ total_patterns

size_t fastfilter::Engine::total_patterns =0
private

Definition at line 194 of file fast_filter.h.

Referenced by get_pattern_count(), load_patterns_file(), and load_patterns_sqlite().


The documentation for this class was generated from the following file: