Skip to content
API Documentation
BastionGuard™
bank_filter.hpp File Reference

Go to the source code of this file.

Classes

class  DomainFilter
struct  DomainFilter::CidrEntry

Functions

static std::unordered_set< std::string > extract_list (const std::string &jsonjson)
static std::string read_file (const std::filesystem::path &p)
DomainFilter & domain_filter ()

Function Documentation

◆ domain_filter()

DomainFilter & domain_filter ( )
inline

Definition at line 641 of file bank_filter.hpp.

641 {
642 static DomainFilter f;
643 return f;
644}

References domain_filter().

Referenced by domain_filter(), handle_client(), and main().

◆ extract_list()

std::unordered_set< std::string > extract_list ( const std::string & json)
static

Definition at line 44 of file bank_filter.hpp.

44 {
45 std::unordered_set<std::string> result;
46
47 // Trova "list" : [
48 auto list_pos = json.find("\"list\"");
49 if (list_pos == std::string::npos) return result;
50
51 auto bracket = json.find('[', list_pos);
52 if (bracket == std::string::npos) return result;
53
54
55 std::size_t depth = 1;
56 std::size_t pos = bracket + 1;
57 bool in_string = false;
58
59 while (pos < json.size() && depth > 0) {
60 char c = json[pos];
61 if (in_string) {
62 if (c == '\\') ++pos;
63 else if (c == '"') in_string = false;
64 } else {
65 if (c == '"') in_string = true;
66 else if (c == '[') ++depth;
67 else if (c == ']') --depth;
68 }
69 ++pos;
70 }
71
72 if (depth != 0) return result;
73
74 std::size_t i = bracket + 1;
75 const std::size_t end = pos - 1;
76
77 while (i < end) {
78 auto q1 = json.find('"', i);
79 if (q1 == std::string::npos || q1 >= end) break;
80
81 auto q2 = q1 + 1;
82
83 while (q2 < end) {
84 if (json[q2] == '\\') {
85 q2 += 2;
86 continue;
87 }
88 if (json[q2] == '"') break;
89 ++q2;
90 }
91
92 if (q2 >= end) break;
93
94 std::string entry = json.substr(q1 + 1, q2 - q1 - 1);
95 if (!entry.empty()) result.insert(std::move(entry));
96
97 i = q2 + 1;
98 }
99
100 return result;
101}
nlohmann::json json
Definition Backend.cpp:2908

References extract_list().

Referenced by extract_list(), and DomainFilter::load_file().

◆ read_file()

std::string read_file ( const std::filesystem::path & p)
static

Definition at line 103 of file bank_filter.hpp.

103 {
104 std::ifstream f(p);
105 if (!f) return {};
106
107 std::ostringstream ss;
108 ss << f.rdbuf();
109 return ss.str();
110}

References read_file().

Referenced by DomainFilter::load_file(), and read_file().