Skip to content
API Documentation
BastionGuard™
DaemonService.hpp
Go to the documentation of this file.
1#pragma once
3#include "bsc/fw/Firewall.hpp"
9#include "bsc/rule/Rule.hpp"
12#include "bsc/ui/UIClient.hpp"
13#include <atomic>
14#include <chrono>
15#include <filesystem>
16#include <mutex>
17#include <optional>
18#include <set>
19#include <unordered_map>
20
21namespace bsc::service {
22
24 public:
26 void run();
27 void stop();
28 private:
29 void scan_connections();
30 bool desktop_allows_activity() const;
31 void set_desktop_active(bool active);
33 void heartbeat();
34 void reload_rules();
35 void reload_firewall();
38 bool realtime,
39 bool allow_blocking_ask = true);
40 void persist_learned_rule(const bsc::model::Connection& conn, const bsc::rule::Rule& decision);
44 std::chrono::milliseconds max_age = std::chrono::milliseconds(5000));
45 bool rules_changed() const;
46 static std::chrono::milliseconds parse_interval(const std::string& v, std::chrono::milliseconds def = std::chrono::seconds(5));
47
59 std::atomic_bool running_{true};
60 std::mutex seen_mutex_;
61 std::set<std::string> seen_;
62 mutable std::mutex rules_reload_mutex_;
63 mutable std::mutex flow_mutex_;
64 std::unordered_map<std::string, std::pair<bsc::model::Connection, std::chrono::steady_clock::time_point>> recent_flows_;
65
66 // ── Cache verdetti utente ─────────────────────────────────────────
67 // Chiave: "app_path|dst_host|dst_port" (dst_host se presente, altrimenti
68 // dst_ip). Valore: action ("allow"/"deny") + duration + scadenza.
69 // Evita di fare un popup per ogni connessione della stessa app verso lo
70 // stesso host — necessario o la UI viene sommersa.
72 std::string action;
73 std::string duration; // "once" | "always" | "until.restart" | "30s" | "5m" ...
74 std::chrono::steady_clock::time_point expires_at;
75 };
76 mutable std::mutex verdict_cache_mutex_;
77 std::unordered_map<std::string, CachedVerdict> verdict_cache_;
78 static std::string verdict_key(const bsc::model::Connection& c);
79 std::optional<CachedVerdict> verdict_lookup(const bsc::model::Connection& c) const;
81
82 // ── Flag isAsking ─────────────────────────────────────────────────
83 // Vero mentre un ask_rule e' in volo verso la UI. Le altre connessioni
84 // che arrivano nel frattempo NON aprono altri popup: ricevono il
85 // default_action cosi' il traffico non resta bloccato. Il popup per loro
86 // arrivera' al giro successivo, una alla volta.
87 std::atomic_bool is_asking_{false};
88
89 // Stato attività desktop comunicato dalla UI.
90 // False = desktop non pronto/bloccato/idle oppure UI non connessa:
91 // il daemon non scansiona, non apre popup e accetta velocemente i pacchetti.
92 std::atomic_bool desktop_active_{false};
93 std::atomic_bool inactive_notice_logged_{false};
94
95 std::filesystem::file_time_type rules_mtime_{};
96 std::chrono::steady_clock::time_point start_time_{std::chrono::steady_clock::now()};
97};
98
99} // namespace bsc::service
static std::chrono::milliseconds parse_interval(const std::string &v, std::chrono::milliseconds def=std::chrono::seconds(5))
bsc::stats::Statistics stats_
bsc::netlink::ProcEventMonitor proc_events_
void enrich_from_recent_processes(bsc::model::Connection &conn, std::chrono::milliseconds max_age=std::chrono::milliseconds(5000))
std::chrono::steady_clock::time_point start_time_
bsc::netfilter::Decision decide_connection(bsc::model::Connection conn, bool realtime, bool allow_blocking_ask=true)
static std::string verdict_key(const bsc::model::Connection &c)
void verdict_store(const bsc::model::Connection &c, const bsc::rule::Rule &r)
void set_desktop_active(bool active)
bsc::net::SocketSnapshot net_
bsc::net::DnsResolver dns_
void handle_notification(const bsc::ui::NotificationEvent &ev)
bsc::config::Config config_
std::unordered_map< std::string, CachedVerdict > verdict_cache_
bsc::task::TaskRunner tasks_
DaemonService(bsc::config::Config config)
bsc::netfilter::NFQueue nfqueue_
void remember_connection(const bsc::model::Connection &conn)
void handle_proc_event(const bsc::netlink::ProcEvent &ev)
std::filesystem::file_time_type rules_mtime_
std::set< std::string > seen_
std::atomic_bool desktop_active_
std::optional< CachedVerdict > verdict_lookup(const bsc::model::Connection &c) const
std::atomic_bool inactive_notice_logged_
void persist_learned_rule(const bsc::model::Connection &conn, const bsc::rule::Rule &decision)
std::unordered_map< std::string, std::pair< bsc::model::Connection, std::chrono::steady_clock::time_point > > recent_flows_
void enrich_from_recent_flows(bsc::model::Connection &conn)
std::chrono::steady_clock::time_point expires_at