Skip to content
API Documentation
BastionGuard™
WebProbe Class Reference

#include <WebProbe.hpp>

Public Types

using NotifyCb = std::function<void(const std::string&, const std::string&, bool)>

Public Member Functions

 WebProbe (const std::string &blocklist_path_hint="", int warning_port=81)
 ~WebProbe ()
std::string check_url (const std::string &url)
void reload_blocklist (const std::string &path_hint)
void register_callback (NotifyCb cb)

Private Member Functions

std::string extract_host (const std::string &raw)
std::string build_local_warning_redirect (const std::string &url) const

Private Attributes

int warning_port_
std::shared_mutex mutex_
NotifyCb notify_cb_

Detailed Description

Definition at line 26 of file WebProbe.hpp.

Member Typedef Documentation

◆ NotifyCb

using WebProbe::NotifyCb = std::function<void(const std::string&, const std::string&, bool)>

Definition at line 28 of file WebProbe.hpp.

Constructor & Destructor Documentation

◆ WebProbe()

WebProbe::WebProbe ( const std::string & blocklist_path_hint = "",
int warning_port = 81 )
explicit

Definition at line 139 of file WebProbe.cpp.

140 : warning_port_(warning_port)
141{
142 load_blocklist(blocklist_path_hint);
143}
static void load_blocklist(const std::string &hint)
Definition WebProbe.cpp:89
int warning_port_
Definition WebProbe.hpp:41

References load_blocklist(), and warning_port_.

◆ ~WebProbe()

Member Function Documentation

◆ build_local_warning_redirect()

std::string WebProbe::build_local_warning_redirect ( const std::string & url) const
private

Definition at line 178 of file WebProbe.cpp.

178 {
179 std::string enc;
180 for (unsigned char c : url)
181 enc += (c == ' ') ? "%20" : std::string(1, c);
182 return "http://127.0.0.2:" + std::to_string(warning_port_) + "/?target=" + enc;
183}

References warning_port_.

Referenced by check_url(), and ~WebProbe().

◆ check_url()

std::string WebProbe::check_url ( const std::string & url)

Definition at line 185 of file WebProbe.cpp.

185 {
186 if (url.empty()) return url;
187 std::string host = extract_host(url);
188 if (host.empty()) return url;
189
190 auto now = now_ms();
191 {
192 std::shared_lock<std::shared_mutex> lk(g_hot_mtx);
193 auto it = g_hot.find(host);
194 if (it != g_hot.end() && now < it->second.second)
195 return it->second.first ? build_local_warning_redirect(url) : url;
196 }
197
198
199 if (Backend::instance().isBankDomain(host)) {
200 std::unique_lock<std::shared_mutex> lk(g_hot_mtx);
201 g_hot[host] = {false, now + TTL_MS};
202 return url;
203 }
204
205 if (is_blocked_host(host)) {
206 std::cout << "[WebProbe] 🔴 Bloccato: " << host << "\n";
207 std::unique_lock<std::shared_mutex> lk(g_hot_mtx);
208 g_hot[host] = {true, now + TTL_MS};
210 }
211
212 std::string cached_ip = AsyncDNSResolver::instance().resolve_cached(host);
213 if (cached_ip == "127.0.0.2") {
214 std::cout << _("[WebProbe] ⚠️ DNS ha restituito 127.0.0.2 per ") << host << "\n";
215 std::unique_lock<std::shared_mutex> lk(g_hot_mtx);
216 g_hot[host] = {true, now + TTL_MS};
218 }
219
220
221 std::unique_lock<std::shared_mutex> lk(g_hot_mtx);
222 g_hot[host] = {false, now + TTL_MS};
223 return url;
224}
#define _(s)
static bool is_blocked_host(const std::string &host)
Definition WebProbe.cpp:130
static uint64_t now_ms()
Definition WebProbe.cpp:62
static std::shared_mutex g_hot_mtx
Definition WebProbe.cpp:58
static std::unordered_map< std::string, std::pair< bool, uint64_t > > g_hot
Definition WebProbe.cpp:59
static constexpr uint64_t TTL_MS
Definition WebProbe.cpp:60
std::string resolve_cached(const std::string &host)
static AsyncDNSResolver & instance()
static Backend & instance()
Definition Backend.cpp:830
std::string build_local_warning_redirect(const std::string &url) const
Definition WebProbe.cpp:178
std::string extract_host(const std::string &raw)
Definition WebProbe.cpp:147

References _, build_local_warning_redirect(), extract_host(), g_hot, g_hot_mtx, AsyncDNSResolver::instance(), Backend::instance(), is_blocked_host(), now_ms(), AsyncDNSResolver::resolve_cached(), and TTL_MS.

Referenced by ~WebProbe().

◆ extract_host()

std::string WebProbe::extract_host ( const std::string & raw)
private

Definition at line 147 of file WebProbe.cpp.

147 {
148 if (raw.empty()) return {};
149 std::string s = raw;
150 auto notspace = [](int ch){ return !std::isspace(ch); };
151 s.erase(s.begin(), std::find_if(s.begin(), s.end(), notspace));
152 s.erase(std::find_if(s.rbegin(), s.rend(), notspace).base(), s.end());
153 if (s.empty()) return {};
154
155 const char* schemes[] = {"http://","https://","ftp://"};
156 for (auto sch : schemes) {
157 size_t n = std::strlen(sch);
158 if (s.rfind(sch, 0) == 0) { s.erase(0, n); break; }
159 }
160 auto at = s.find('@');
161 if (at != std::string::npos) s.erase(0, at + 1);
162 size_t slash = s.find_first_of("/?#");
163 if (slash != std::string::npos) s = s.substr(0, slash);
164 size_t colon = s.rfind(':');
165 if (colon != std::string::npos) {
166 std::string after = s.substr(colon + 1);
167 if (std::all_of(after.begin(), after.end(), ::isdigit))
168 s = s.substr(0, colon);
169 }
170 if (!s.empty() && s.back() == '.') s.pop_back();
171
172 std::string ascii = to_ascii_idna(s);
173 std::transform(ascii.begin(), ascii.end(), ascii.begin(), ::tolower);
174 return ascii;
175}
static std::string to_ascii_idna(const std::string &host)
Definition WebProbe.cpp:73

References to_ascii_idna().

Referenced by check_url(), and ~WebProbe().

◆ register_callback()

void WebProbe::register_callback ( NotifyCb cb)

Definition at line 227 of file WebProbe.cpp.

227 {
228 std::unique_lock<std::shared_mutex> lock(mutex_);
229 notify_cb_ = std::move(cb);
230}
NotifyCb notify_cb_
Definition WebProbe.hpp:43
std::shared_mutex mutex_
Definition WebProbe.hpp:42

References mutex_, and notify_cb_.

Referenced by ~WebProbe().

◆ reload_blocklist()

void WebProbe::reload_blocklist ( const std::string & path_hint)

Definition at line 232 of file WebProbe.cpp.

232 {
233 load_blocklist(path);
234}

References load_blocklist().

Referenced by ~WebProbe().

Member Data Documentation

◆ mutex_

std::shared_mutex WebProbe::mutex_
mutableprivate

Definition at line 42 of file WebProbe.hpp.

Referenced by register_callback().

◆ notify_cb_

NotifyCb WebProbe::notify_cb_
private

Definition at line 43 of file WebProbe.hpp.

Referenced by register_callback().

◆ warning_port_

int WebProbe::warning_port_
private

Definition at line 41 of file WebProbe.hpp.

Referenced by build_local_warning_redirect(), and WebProbe().


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