Skip to content
API Documentation
BastionGuard™
LocalWarningServer Class Reference

#include <local_warning_server.hpp>

Public Member Functions

 LocalWarningServer (int http_port=81, int https_port=444, const std::string &bind_address="127.0.0.2", const std::string &cert_file="", const std::string &key_file="", const std::string &page_warning_path="")
 ~LocalWarningServer ()
void start ()
void stop ()

Static Private Member Functions

static void on_request (SoupServer *, SoupServerMessage *, const char *, GHashTable *, gpointer)

Private Attributes

SoupServer * server_
int http_port_
int https_port_
std::string bind_address_
std::string cert_file_
std::string key_file_
std::string page_warning_path_
bool running_
GMainLoop * loop_
std::thread loop_thread_

Detailed Description

Definition at line 27 of file local_warning_server.hpp.

Constructor & Destructor Documentation

◆ LocalWarningServer()

LocalWarningServer::LocalWarningServer ( int http_port = 81,
int https_port = 444,
const std::string & bind_address = "127.0.0.2",
const std::string & cert_file = "",
const std::string & key_file = "",
const std::string & page_warning_path = "" )

Definition at line 234 of file local_warning_server.cpp.

240 : server_(nullptr),
241 http_port_(http_port > 0 ? http_port : BastionGuard_WARNING_DEFAULT_HTTP),
242 https_port_(https_port > 0 ? https_port : BastionGuard_WARNING_DEFAULT_HTTPS),
243 bind_address_(bind_address.empty() ? BastionGuard_LOCAL_WARNING_IP : bind_address),
244 cert_file_(cert_file),
245 key_file_(key_file),
246 page_warning_path_(page_warning_path),
247 running_(false),
248 loop_(nullptr)
249{}
#define BastionGuard_LOCAL_WARNING_IP
#define BastionGuard_WARNING_DEFAULT_HTTPS
#define BastionGuard_WARNING_DEFAULT_HTTP

References BastionGuard_LOCAL_WARNING_IP, BastionGuard_WARNING_DEFAULT_HTTP, BastionGuard_WARNING_DEFAULT_HTTPS, bind_address_, cert_file_, http_port_, https_port_, key_file_, loop_, page_warning_path_, running_, and server_.

Referenced by on_request().

◆ ~LocalWarningServer()

LocalWarningServer::~LocalWarningServer ( )

Definition at line 251 of file local_warning_server.cpp.

251 {
252 stop();
253}

References stop().

Member Function Documentation

◆ on_request()

void LocalWarningServer::on_request ( SoupServer * ,
SoupServerMessage * msg,
const char * path,
GHashTable * ,
gpointer user_data )
staticprivate

Definition at line 319 of file local_warning_server.cpp.

321{
322 auto* self = static_cast<LocalWarningServer*>(user_data);
323 if (!self || !msg) return;
324
325 SoupMessageHeaders* req_headers =
326 const_cast<SoupMessageHeaders*>(soup_server_message_get_request_headers(msg));
327 const char* host_header = req_headers ? soup_message_headers_get_one(req_headers, "Host") : nullptr;
328
329 std::string requested_host = host_header ? extract_host(host_header) : _("dominio-sconosciuto");
330
331 if (requested_host.empty() || requested_host == BastionGuard_LOCAL_WARNING_IP) {
332 if (path && *path) {
333 std::string p = path;
334 if (!p.empty() && p.front() == '/') p.erase(0,1);
335 requested_host = p;
336 } else {
337 requested_host = _("dominio-sconosciuto");
338 }
339 }
340
341 if (is_trusted_domain(requested_host)) {
342 std::string location = "https://" + clean_domain(requested_host) + "/";
343 SoupMessageHeaders* res_headers =
344 const_cast<SoupMessageHeaders*>(soup_server_message_get_response_headers(msg));
345 soup_message_headers_append(res_headers, "Location", location.c_str());
346 soup_server_message_set_status(msg, SOUP_STATUS_TEMPORARY_REDIRECT, nullptr); // 307
347 std::cout << "[BastionGuard-DNS] 🟢 Trusted: redirect a " << location << "\n";
348 return;
349 }
350
351 std::cout << "[BastionGuard-DNS] ⚠️ Dominio bloccato: " << requested_host << std::endl;
352
353 std::ostringstream html;
354
355html << R"HTML(<!DOCTYPE html>
356<html lang="it">
357<head>
358<meta charset="UTF-8">
359<title>⚠️ )HTML" << _("Accesso bloccato - BastionGuard DNS") << R"HTML(</title>
360<meta name="viewport" content="width=device-width, initial-scale=1">
361<style>
362body {
363 background: linear-gradient(135deg, #ff6f61, #d6a4a4);
364 color: #fff;
365 display: flex;
#define _(s)
LocalWarningServer(int http_port=81, int https_port=444, const std::string &bind_address="127.0.0.2", const std::string &cert_file="", const std::string &key_file="", const std::string &page_warning_path="")
static bool is_trusted_domain(const std::string &host)
static std::string clean_domain(std::string d)
static std::string extract_host(const std::string &raw)

References _, BastionGuard_LOCAL_WARNING_IP, clean_domain(), extract_host(), html_escape(), is_trusted_domain(), and LocalWarningServer().

Referenced by start().

◆ start()

void LocalWarningServer::start ( )

Definition at line 255 of file local_warning_server.cpp.

255 {
256 if (running_) return;
257
258 server_ = soup_server_new(nullptr, nullptr);
259 if (!server_) {
260 std::cerr << _("[LocalWarningServer] ❌ Errore creazione SoupServer\n");
261 return;
262 }
263
264 g_object_set(server_, "server-header", "BastionGuard-warning-server", nullptr);
265 soup_server_add_handler(server_, "/", LocalWarningServer::on_request, this, nullptr);
266
267 // HTTPS opzionale
268 bool have_cert = false;
269 if (!cert_file_.empty() && !key_file_.empty()) {
270 GError* cert_err = nullptr;
271 GTlsCertificate* cert = g_tls_certificate_new_from_files(cert_file_.c_str(), key_file_.c_str(), &cert_err);
272 if (cert) {
273 soup_server_set_tls_certificate(server_, cert);
274 have_cert = true;
275 g_object_unref(cert);
276 } else if (cert_err) {
277 std::cerr << _("[LocalWarningServer] TLS error: ") << cert_err->message << "\n";
278 g_error_free(cert_err);
279 }
280 }
281
282 GError* err = nullptr;
283 bool ok_http = listen_one(server_, bind_address_.c_str(), http_port_, false, &err);
284 if (!ok_http && err) { g_error_free(err); err = nullptr; }
285 bool ok_https = have_cert && listen_one(server_, bind_address_.c_str(), https_port_, true, &err);
286 if (!ok_https && err) { g_error_free(err); err = nullptr; }
287
288 if (!(ok_http || ok_https)) {
289 g_object_unref(server_);
290 server_ = nullptr;
291 std::cerr << _("[LocalWarningServer] ⚠️ Nessuna porta disponibile su ") << bind_address_ << "\n";
292 return;
293 }
294
295 loop_ = g_main_loop_new(nullptr, FALSE);
296 loop_thread_ = std::thread([this]() { g_main_loop_run(loop_); });
297 running_ = true;
298 std::cout << _("[LocalWarningServer] ✅ Avviato su ") << bind_address_
299 << " (" << http_port_ << (have_cert ? ", HTTPS " + std::to_string(https_port_) : "") << ")\n";
300}
static void on_request(SoupServer *, SoupServerMessage *, const char *, GHashTable *, gpointer)
static bool listen_one(SoupServer *srv, const char *addr_str, int port, bool https, GError **error)

References _, bind_address_, cert_file_, http_port_, https_port_, key_file_, listen_one(), loop_, loop_thread_, on_request(), running_, and server_.

Referenced by main().

◆ stop()

void LocalWarningServer::stop ( )

Definition at line 302 of file local_warning_server.cpp.

302 {
303 if (!running_) return;
304 running_ = false;
305 if (loop_) {
306 g_main_loop_quit(loop_);
307 if (loop_thread_.joinable()) loop_thread_.join();
308 g_main_loop_unref(loop_);
309 loop_ = nullptr;
310 }
311 if (server_) {
312 soup_server_disconnect(server_);
313 g_object_unref(server_);
314 server_ = nullptr;
315 }
316 std::cout << _("[LocalWarningServer] 🛑 Arrestato\n");
317}

References _, loop_, loop_thread_, running_, and server_.

Referenced by main(), and ~LocalWarningServer().

Member Data Documentation

◆ bind_address_

std::string LocalWarningServer::bind_address_
private

Definition at line 46 of file local_warning_server.hpp.

Referenced by LocalWarningServer(), and start().

◆ cert_file_

std::string LocalWarningServer::cert_file_
private

Definition at line 47 of file local_warning_server.hpp.

Referenced by LocalWarningServer(), and start().

◆ http_port_

int LocalWarningServer::http_port_
private

Definition at line 44 of file local_warning_server.hpp.

Referenced by LocalWarningServer(), and start().

◆ https_port_

int LocalWarningServer::https_port_
private

Definition at line 45 of file local_warning_server.hpp.

Referenced by LocalWarningServer(), and start().

◆ key_file_

std::string LocalWarningServer::key_file_
private

Definition at line 48 of file local_warning_server.hpp.

Referenced by LocalWarningServer(), and start().

◆ loop_

GMainLoop* LocalWarningServer::loop_
private

Definition at line 51 of file local_warning_server.hpp.

Referenced by LocalWarningServer(), start(), and stop().

◆ loop_thread_

std::thread LocalWarningServer::loop_thread_
private

Definition at line 52 of file local_warning_server.hpp.

Referenced by start(), and stop().

◆ page_warning_path_

std::string LocalWarningServer::page_warning_path_
private

Definition at line 49 of file local_warning_server.hpp.

Referenced by LocalWarningServer().

◆ running_

bool LocalWarningServer::running_
private

Definition at line 50 of file local_warning_server.hpp.

Referenced by LocalWarningServer(), start(), and stop().

◆ server_

SoupServer* LocalWarningServer::server_
private

Definition at line 43 of file local_warning_server.hpp.

Referenced by LocalWarningServer(), start(), and stop().


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