Skip to content
API Documentation
BastionGuard™
LeakMonitor.cpp
Go to the documentation of this file.
1/*
2 * BastionGuard™
3 * Copyright (C) 2025–2026 Calogero Scarnà
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * BastionGuard™ is a trademark of Calogero Scarnà.
18 * The BastionGuard™ name and branding are not licensed under the GPL.
19 */
20
21#include "LeakMonitor.hpp"
22
26#include "../utils/HashUtil.hpp"
28#include <glib/gi18n.h>
29#include <chrono>
30#include <memory>
31
33 const std::string& e,
34 NotifyFn n)
35 : email(e),
36 notify(std::move(n)),
37 running(false)
38{
39}
40
45
47{
48 if (running)
49 return;
50
51 running = true;
52 worker = std::thread(&LeakMonitor::loop, this);
53}
54
56{
57 running = false;
58
59 if (worker.joinable())
60 worker.join();
61}
62
64{
65 LeakAggregator aggregator;
66
67
68 const std::string hibpApiKey =
70
71 const bool hibpEnabled = !hibpApiKey.empty();
72
73 if (hibpEnabled) {
74 aggregator.addProvider(
75 std::make_unique<HibpProvider>(hibpApiKey));
76 }
77
78 // LeakCheck SEMPRE disponibile (fallback)
79 aggregator.addProvider(
80 std::make_unique<LeakCheckProvider>());
81
82 const std::string emailHash =
84
85 if (hibpEnabled) {
86 notify(_("Monitor Identity Leak attivo (HIBP + LeakCheck)"));
87 } else {
88 notify(_(
89 "⚠ Monitor Identity Leak attivo in modalità LIMITATA "
90 "(solo LeakCheck, API HIBP mancante)"));
91 }
92
93
94 while (running) {
95
96 try {
97 auto leaks = aggregator.queryAll(email);
98
99 if (!leaks.empty()) {
100
101 if (hibpEnabled) {
102 notify(_(
103 "⚠ Nuova violazione rilevata per identità: ") +
104 emailHash);
105 } else {
106 notify(_(
107 "⚠ Possibile esposizione rilevata "
108 "(fonte LeakCheck, verifica manuale consigliata)"));
109 }
110 }
111 }
112 catch (const std::exception& ex) {
113 notify(
114 std::string(_("❌ Errore monitor Identity Leak: ")) +
115 ex.what());
116 }
117
118 for (int i = 0; i < 24 * 60 && running; ++i) {
119 std::this_thread::sleep_for(
120 std::chrono::minutes(1));
121 }
122 }
123
124 notify(_("Monitor Identity Leak fermato"));
125}
#define _(s)
static std::string sha256(const std::string &input)
Definition HashUtil.cpp:27
static std::string hibpApiKey()
void addProvider(std::unique_ptr< ILeakProvider > provider)
std::vector< LeakRecord > queryAll(const std::string &email)
NotifyFn notify
LeakMonitor(const std::string &email, NotifyFn notify)
std::thread worker
std::atomic< bool > running
std::string email
std::function< void(const std::string &)> NotifyFn