Skip to content
API Documentation
BastionGuard™
AppsPage.cpp
Go to the documentation of this file.
1/*
2 * BastionGuard™ Secure Connection
3 * Copyright (C) 2025–2026 Calogero Scarnà
4 * GPL-3.0
5 */
6
7#include "AppsPage.hpp"
9
10#include <ctime>
11#include <filesystem>
12#include <glibmm/i18n.h>
13#include <iomanip>
14#include <sstream>
15
17: Gtk::Box(Gtk::Orientation::VERTICAL, 0)
18{
19 // Classe CSS sulla pagina intera per consentire al tema (BSC.css /
20 // BSC-dark.css) di personalizzare colori, padding e bordi in modo
21 // coerente con le altre pagine.
22 get_style_context()->add_class("bsc-page");
23 get_style_context()->add_class("bsc-apps-page");
24 buildUI();
25}
26
28{
29 set_margin_start(12);
30 set_margin_end(12);
31 set_margin_top(12);
32 set_margin_bottom(12);
33
34 // ── Header ─────────────────────────────────────────────────────────
35 auto header = Gtk::make_managed<Gtk::Box>(Gtk::Orientation::HORIZONTAL, 8);
36 header->get_style_context()->add_class("bsc-page-header");
37
38 m_headerLabel.set_markup("<b>" + Glib::ustring(_("Applicazioni")) + "</b>");
39 m_headerLabel.set_halign(Gtk::Align::START);
40 m_headerLabel.set_hexpand(true);
41 m_headerLabel.get_style_context()->add_class("bsc-page-title");
42 header->append(m_headerLabel);
43
44 m_clearBtn.set_label(_("Pulisci"));
45 m_clearBtn.get_style_context()->add_class("bsc-toolbar-button");
46 m_clearBtn.signal_clicked().connect([this]() { clear(); });
47 header->append(m_clearBtn);
48
49 append(*header);
50
51 // ── Store + TreeView ───────────────────────────────────────────────
52 m_store = Gtk::ListStore::create(m_columns);
53 m_treeView.set_model(m_store);
54 m_treeView.set_headers_visible(true);
55 m_treeView.set_hexpand(true);
56 m_treeView.set_vexpand(true);
57 // Classi CSS coerenti con le altre TreeView (Connessioni, Regole)
58 m_treeView.get_style_context()->add_class("bsc-treeview");
59 m_treeView.get_style_context()->add_class("bsc-apps-treeview");
60
61 // ── Colonne ────────────────────────────────────────────────────────
62 // Nessuna colonna icona: senza .desktop scanning non avremmo modo di
63 // mostrare un'icona significativa, e una colonna vuota e' peggio di
64 // nessuna colonna. La prima colonna ora e' direttamente il NOME.
65 m_treeView.append_column(_("Applicazione"), m_columns.col_name);
66 m_treeView.append_column(_("Path"), m_columns.col_path);
67 m_treeView.append_column(_("Connessioni"), m_columns.col_count);
68 m_treeView.append_column(_("Ultimo host"), m_columns.col_last_host);
69 m_treeView.append_column(_("Ultimo esito"), m_columns.col_last_verdict);
70 m_treeView.append_column(_("Aggiornata"), m_columns.col_last_time);
71
72 // Tutte le colonne sono ridimensionabili dall'utente; la colonna "Path"
73 // si espande per occupare lo spazio rimanente (e' il dato piu' lungo).
74 for (int i = 0; i < m_treeView.get_n_columns(); ++i) {
75 if (auto* c = m_treeView.get_column(i)) {
76 c->set_resizable(true);
77 c->set_reorderable(false);
78 }
79 }
80 if (auto* path_col = m_treeView.get_column(1)) {
81 path_col->set_expand(true);
82 path_col->set_min_width(200);
83 }
84
85 m_scroll.set_child(m_treeView);
86 m_scroll.set_policy(Gtk::PolicyType::AUTOMATIC, Gtk::PolicyType::AUTOMATIC);
87 m_scroll.set_hexpand(true);
88 m_scroll.set_vexpand(true);
89 m_scroll.get_style_context()->add_class("bsc-scrolled");
90 append(m_scroll);
91}
92
94{
95 std::time_t t = std::time(nullptr);
96 std::tm tm{};
97 ::localtime_r(&t, &tm);
98 std::ostringstream oss;
99 oss << std::put_time(&tm, "%H:%M:%S");
100 return oss.str();
101}
102
104{
106 info.app_comm, info.app_path, info.app_args, info.dest_ip, info.src_ip);
107}
108
109void AppsPage::noteConnection(const ConnectionInfo& info, const std::string& verdict)
110{
111 // Aggregazione per path completo: lo stesso binario eseguito due volte
112 // produce un'unica riga aggregata.
113 std::string key = info.app_path.empty() ? info.app_comm : info.app_path;
114 if (key.empty() && bsc::ui_util::is_safe_local_connection(info.dest_ip, info.src_ip)) {
115 key = "__bsc_safe_local_process__";
116 }
117 if (key.empty()) return;
118
119 const std::string host = !info.dest_host.empty() ? info.dest_host : info.dest_ip;
120 const std::string time_str = currentTimeString();
121
122 auto it = m_appRows.find(key);
123 if (it != m_appRows.end()) {
124 // Riga esistente: aggiorna contatore + ultimi valori
125 auto row = *(it->second);
126 const uint32_t count = row[m_columns.col_count];
127 row[m_columns.col_count] = count + 1;
128 row[m_columns.col_last_host] = host + ":" + std::to_string(info.dest_port);
129 row[m_columns.col_last_verdict] = verdict;
130 row[m_columns.col_last_time] = time_str;
131 return;
132 }
133
134 // Nuova riga
135 auto iter = m_store->append();
136 auto row = *iter;
137 row[m_columns.col_name] = displayNameFor(info);
138 row[m_columns.col_path] = info.app_path.empty()
140 ? std::string{"loopback/local"}
141 : std::string{})
142 : info.app_path;
143 row[m_columns.col_count] = 1u;
144 row[m_columns.col_last_host] = host + ":" + std::to_string(info.dest_port);
145 row[m_columns.col_last_verdict] = verdict;
146 row[m_columns.col_last_time] = time_str;
147 m_appRows.emplace(key, iter);
148}
149
151{
152 m_store->clear();
153 m_appRows.clear();
154}
#define _(s)
Gtk::Button m_clearBtn
Definition AppsPage.hpp:59
static std::string currentTimeString()
Definition AppsPage.cpp:93
Gtk::ScrolledWindow m_scroll
Definition AppsPage.hpp:57
void clear()
Definition AppsPage.cpp:150
Glib::RefPtr< Gtk::ListStore > m_store
Definition AppsPage.hpp:55
Gtk::Label m_headerLabel
Definition AppsPage.hpp:58
std::unordered_map< std::string, Gtk::TreeModel::iterator > m_appRows
Definition AppsPage.hpp:62
static std::string displayNameFor(const ConnectionInfo &info)
Definition AppsPage.cpp:103
Gtk::TreeView m_treeView
Definition AppsPage.hpp:56
void noteConnection(const ConnectionInfo &info, const std::string &verdict)
Definition AppsPage.cpp:109
Columns m_columns
Definition AppsPage.hpp:54
void buildUI()
Definition AppsPage.cpp:27
std::string best_connection_display_name(const std::string &app_comm, const std::string &app_path, const std::vector< std::string > &app_args, const std::string &dst_ip, const std::string &src_ip)
bool is_safe_local_connection(const std::string &dst_ip, const std::string &src_ip)
Gtk::TreeModelColumn< Glib::ustring > col_name
Definition AppsPage.hpp:46
Gtk::TreeModelColumn< Glib::ustring > col_last_host
Definition AppsPage.hpp:49
Gtk::TreeModelColumn< Glib::ustring > col_last_verdict
Definition AppsPage.hpp:50
Gtk::TreeModelColumn< Glib::ustring > col_last_time
Definition AppsPage.hpp:51
Gtk::TreeModelColumn< Glib::ustring > col_path
Definition AppsPage.hpp:47
Gtk::TreeModelColumn< uint32_t > col_count
Definition AppsPage.hpp:48
std::string src_ip
Definition UIServer.hpp:35
std::string dest_host
Definition UIServer.hpp:37
uint32_t dest_port
Definition UIServer.hpp:39
std::string app_path
Definition UIServer.hpp:27
std::vector< std::string > app_args
Definition UIServer.hpp:30
std::string dest_ip
Definition UIServer.hpp:38
std::string app_comm
Definition UIServer.hpp:28