Skip to content
API Documentation
BastionGuard™
WebProbe.cpp File Reference

Go to the source code of this file.

Classes

struct  VerdictEntry

Functions

static uint64_t now_ms ()
static std::string expand_user_path (const std::string &rel)
static std::string to_ascii_idna (const std::string &host)
static void load_blocklist (const std::string &hint)
static bool match_blocklist_pattern (const std::string &pattern, const std::string &host)
static bool is_blocked_host (const std::string &host)

Variables

static std::mutex g_verdict_mutex
static std::unordered_map< std::string, VerdictEntry > g_verdicts
static std::unordered_set< std::string > g_blocklist
static std::shared_mutex g_hot_mtx
static std::unordered_map< std::string, std::pair< bool, uint64_t > > g_hot
static constexpr uint64_t TTL_MS = 15000
static const std::string kBlocklistFile = "/.local/share/BastionGuard/blocklist_hosts.txt"

Function Documentation

◆ expand_user_path()

std::string expand_user_path ( const std::string & rel)
static

Definition at line 67 of file WebProbe.cpp.

67 {
68 std::string home = Glib::get_home_dir();
69 if (rel.empty()) return home;
70 return home + rel;
71}

References expand_user_path().

Referenced by expand_user_path(), and load_blocklist().

◆ is_blocked_host()

bool is_blocked_host ( const std::string & host)
static

Definition at line 130 of file WebProbe.cpp.

130 {
131 std::lock_guard<std::mutex> lg(g_verdict_mutex);
132 for (const auto& pat : g_blocklist)
133 if (match_blocklist_pattern(pat, host))
134 return true;
135 return false;
136}
static bool match_blocklist_pattern(const std::string &pattern, const std::string &host)
Definition WebProbe.cpp:119
static std::mutex g_verdict_mutex
Definition WebProbe.cpp:55
static std::unordered_set< std::string > g_blocklist
Definition WebProbe.cpp:57

References g_blocklist, g_verdict_mutex, is_blocked_host(), and match_blocklist_pattern().

Referenced by WebProbe::check_url(), and is_blocked_host().

◆ load_blocklist()

void load_blocklist ( const std::string & hint)
static

Definition at line 89 of file WebProbe.cpp.

89 {
90 std::lock_guard<std::mutex> lg(g_verdict_mutex);
91 g_blocklist.clear();
92
93 std::vector<std::string> tries;
94 if (!hint.empty()) tries.push_back(hint);
95 tries.push_back(expand_user_path(kBlocklistFile));
96 tries.push_back("/etc/BastionGuard/blocklist_hosts.txt");
97
98 for (const auto& p : tries) {
99 if (std::filesystem::exists(p)) {
100 std::ifstream ifs(p);
101 if (!ifs) continue;
102 std::string line;
103 while (std::getline(ifs, line)) {
104 line.erase(std::remove_if(line.begin(), line.end(), ::isspace), line.end());
105 if (!line.empty()) {
106 std::string lower = to_ascii_idna(line);
107 std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
108 g_blocklist.insert(lower);
109 }
110 }
111 std::cout << _("[WebProbe] ✅ Caricata blocklist da: ") << p
112 << " (" << g_blocklist.size() << _(" domini)\n");
113 return;
114 }
115 }
116 std::cout << _("[WebProbe] ⚠️ Nessuna blocklist trovata.\n");
117}
#define _(s)
static const std::string kBlocklistFile
Definition WebProbe.cpp:87
static std::string expand_user_path(const std::string &rel)
Definition WebProbe.cpp:67
static std::string to_ascii_idna(const std::string &host)
Definition WebProbe.cpp:73

References _, expand_user_path(), g_blocklist, g_verdict_mutex, kBlocklistFile, load_blocklist(), and to_ascii_idna().

Referenced by load_blocklist(), WebProbe::reload_blocklist(), and WebProbe::WebProbe().

◆ match_blocklist_pattern()

bool match_blocklist_pattern ( const std::string & pattern,
const std::string & host )
static

Definition at line 119 of file WebProbe.cpp.

119 {
120 if (pattern.size() > 2 && pattern[0]=='*' && pattern[1]=='.') {
121 const std::string tail = pattern.substr(2);
122 if (host == tail) return true;
123 if (host.size() > tail.size()+1 &&
124 host.compare(host.size()-tail.size(), tail.size(), tail) == 0 &&
125 host[host.size()-tail.size()-1] == '.') return true;
126 }
127 return host == pattern;
128}

References match_blocklist_pattern().

Referenced by is_blocked_host(), and match_blocklist_pattern().

◆ now_ms()

uint64_t now_ms ( )
inlinestatic

Definition at line 62 of file WebProbe.cpp.

62 {
63 return std::chrono::duration_cast<std::chrono::milliseconds>(
64 std::chrono::steady_clock::now().time_since_epoch()).count();
65}

References now_ms().

Referenced by WebProbe::check_url(), and now_ms().

◆ to_ascii_idna()

std::string to_ascii_idna ( const std::string & host)
static

Definition at line 73 of file WebProbe.cpp.

73 {
74 if (host.empty()) return host;
75 char *out = nullptr;
76 int rc = idn2_lookup_ul(host.c_str(), &out, 0);
77 if (rc == IDN2_OK && out) {
78 std::string r(out);
79 idn2_free(out);
80 return r;
81 }
82 return host;
83}

References to_ascii_idna().

Referenced by WebProbe::extract_host(), load_blocklist(), and to_ascii_idna().

Variable Documentation

◆ g_blocklist

std::unordered_set<std::string> g_blocklist
static

Definition at line 57 of file WebProbe.cpp.

Referenced by is_blocked_host(), and load_blocklist().

◆ g_hot

std::unordered_map<std::string, std::pair<bool,uint64_t> > g_hot
static

Definition at line 59 of file WebProbe.cpp.

Referenced by WebProbe::check_url().

◆ g_hot_mtx

std::shared_mutex g_hot_mtx
static

Definition at line 58 of file WebProbe.cpp.

Referenced by WebProbe::check_url().

◆ g_verdict_mutex

std::mutex g_verdict_mutex
static

Definition at line 55 of file WebProbe.cpp.

Referenced by is_blocked_host(), and load_blocklist().

◆ g_verdicts

std::unordered_map<std::string, VerdictEntry> g_verdicts
static

Definition at line 56 of file WebProbe.cpp.

◆ kBlocklistFile

const std::string kBlocklistFile = "/.local/share/BastionGuard/blocklist_hosts.txt"
static

Definition at line 87 of file WebProbe.cpp.

Referenced by load_blocklist().

◆ TTL_MS

uint64_t TTL_MS = 15000
staticconstexpr

Definition at line 60 of file WebProbe.cpp.

Referenced by WebProbe::check_url().