Skip to content
API Documentation
BastionGuard™
local_warning_server.cpp File Reference

Go to the source code of this file.

Macros

#define BastionGuard_WARNING_DEFAULT_HTTP   81
#define BastionGuard_WARNING_DEFAULT_HTTPS   444
#define BastionGuard_LOCAL_WARNING_IP   "127.0.0.2"

Functions

static std::string to_lower (std::string s)
static std::string clean_domain (std::string d)
static std::string read_last_lines (const std::string &path, size_t max_lines=50)
static std::string html_escape (const std::string &s)
static std::string extract_host (const std::string &raw)
static void load_list_file (const std::string &path, std::vector< std::string > &out)
static void ensure_trusted_loaded ()
static bool is_trusted_domain (const std::string &host)
static bool listen_one (SoupServer *srv, const char *addr_str, int port, bool https, GError **error)

Variables

static std::unordered_set< std::string > g_trusted
static bool g_trusted_loaded = false

Macro Definition Documentation

◆ BastionGuard_LOCAL_WARNING_IP

#define BastionGuard_LOCAL_WARNING_IP   "127.0.0.2"

◆ BastionGuard_WARNING_DEFAULT_HTTP

#define BastionGuard_WARNING_DEFAULT_HTTP   81

Definition at line 44 of file local_warning_server.cpp.

Referenced by LocalWarningServer::LocalWarningServer().

◆ BastionGuard_WARNING_DEFAULT_HTTPS

#define BastionGuard_WARNING_DEFAULT_HTTPS   444

Definition at line 47 of file local_warning_server.cpp.

Referenced by LocalWarningServer::LocalWarningServer().

Function Documentation

◆ clean_domain()

std::string clean_domain ( std::string d)
static

Definition at line 59 of file local_warning_server.cpp.

59 {
60 d = to_lower(d);
61 if (!d.empty() && d.front() == '.') d.erase(0, 1);
62 if (d.rfind("www.", 0) == 0) d.erase(0, 4);
63 auto p = d.find(':'); if (p != std::string::npos) d.erase(p);
64 if (!d.empty() && d.back() == '.') d.pop_back();
65 return d;
66}
static std::string to_lower(std::string s)

References clean_domain(), and to_lower().

Referenced by LocalWarningServer::on_request().

◆ ensure_trusted_loaded()

void ensure_trusted_loaded ( )
static

Definition at line 180 of file local_warning_server.cpp.

180 {
181 if (g_trusted_loaded) return;
182
183 std::vector<std::string> tmp;
184 load_list_file("/usr/share/BastionGuard/data/bank/banks.json", tmp);
185 if (const char* home = std::getenv("HOME")) {
186 load_list_file(std::string(home) + "/.config/BastionGuard/whitelist.json", tmp);
187 }
188 for (auto& d : tmp) g_trusted.insert(d);
189 g_trusted_loaded = true;
190
191 std::cerr << _("[LocalWarningServer] ✅ Whitelist caricata: ")
192 << g_trusted.size() << _(" domini trusted\n");
193}
static void load_list_file(const std::string &path, std::vector< std::string > &out)
static std::unordered_set< std::string > g_trusted
static bool g_trusted_loaded
#define _(s)

References _, ensure_trusted_loaded(), g_trusted, g_trusted_loaded, and load_list_file().

◆ extract_host()

std::string extract_host ( const std::string & raw)
static

Definition at line 113 of file local_warning_server.cpp.

113 {
114 if (raw.empty()) return {};
115 std::string s = raw;
116
117 auto notspace = [](int ch){ return !std::isspace(ch); };
118 s.erase(s.begin(), std::find_if(s.begin(), s.end(), notspace));
119 s.erase(std::find_if(s.rbegin(), s.rend(), notspace).base(), s.end());
120 if (s.empty()) return {};
121
122 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
123
124 auto strip_scheme = [&](const char* prefix) {
125 size_t n = std::strlen(prefix);
126 if (s.rfind(prefix, 0) == 0) s.erase(0, n);
127 };
128 strip_scheme("http://");
129 strip_scheme("https://");
130 strip_scheme("ftp://");
131
132 auto atpos = s.find('@');
133 if (atpos != std::string::npos) s.erase(0, atpos + 1);
134
135 size_t pos = s.find_first_of("/?#");
136 if (pos != std::string::npos) s = s.substr(0, pos);
137
138 size_t colon = s.rfind(':');
139 if (colon != std::string::npos) {
140 std::string after = s.substr(colon + 1);
141 bool all_digits = !after.empty() && std::all_of(after.begin(), after.end(), ::isdigit);
142 if (all_digits) s = s.substr(0, colon);
143 }
144 if (!s.empty() && s.back() == '.') s.pop_back();
145 return s;
146}

References extract_host().

Referenced by extract_host(), and LocalWarningServer::on_request().

◆ html_escape()

std::string html_escape ( const std::string & s)
static

Definition at line 95 of file local_warning_server.cpp.

95 {
96 std::string o;
97 o.reserve(s.size());
98 for (char c : s) {
99 switch (c) {
100 case '&': o += "&amp;"; break;
101 case '<': o += "&lt;"; break;
102 case '>': o += "&gt;"; break;
103 case '"': o += "&quot;"; break;
104 case '\'': o += "&#39;"; break;
105 case '\n': o += "<br/>"; break;
106 default: o += c;
107 }
108 }
109 return o;
110}

References html_escape().

Referenced by html_escape(), and LocalWarningServer::on_request().

◆ is_trusted_domain()

bool is_trusted_domain ( const std::string & host)
static

Definition at line 195 of file local_warning_server.cpp.

195 {
197 std::string h = clean_domain(host);
198 if (h.empty()) return false;
199
200 if (g_trusted.count(h)) return true;
201 auto pos = h.find('.');
202 while (pos != std::string::npos) {
203 std::string parent = h.substr(pos + 1);
204 if (g_trusted.count(parent)) return true;
205 pos = h.find('.', pos + 1);
206 }
207 return false;
208}
static std::string clean_domain(std::string d)
static void ensure_trusted_loaded()

References clean_domain(), ensure_trusted_loaded(), g_trusted, and is_trusted_domain().

Referenced by LocalWarningServer::on_request().

◆ listen_one()

bool listen_one ( SoupServer * srv,
const char * addr_str,
int port,
bool https,
GError ** error )
static

Definition at line 210 of file local_warning_server.cpp.

210 {
211 if (!srv || !addr_str) return false;
212
213 GInetAddress* ia4 = g_inet_address_new_from_string(addr_str);
214 GSocketAddress* sa4 = ia4 ? g_inet_socket_address_new(ia4, port) : nullptr;
215 if (ia4) g_object_unref(ia4);
216
217 bool ok = false;
218 if (sa4) {
219 ok = soup_server_listen(srv, sa4,
220 https ? SOUP_SERVER_LISTEN_HTTPS : (SoupServerListenOptions)0, error);
221 g_object_unref(sa4);
222 }
223
224 if (!ok && error && *error) {
225 std::cerr << _("[LocalWarningServer] ⚠️ Porta ")
226 << port << _(" non disponibile: ") << (*error)->message << "\n";
227 g_error_free(*error);
228 *error = nullptr;
229 }
230 return ok;
231}

References _, and listen_one().

Referenced by listen_one(), and LocalWarningServer::start().

◆ load_list_file()

void load_list_file ( const std::string & path,
std::vector< std::string > & out )
static

Definition at line 152 of file local_warning_server.cpp.

152 {
153 if (!fs::exists(path)) return;
154 try {
155 std::ifstream f(path);
156 if (!f.good()) return;
157
158 nlohmann::json j;
159 f >> j;
160
161 auto push = [&](const std::string& s){
162 auto d = clean_domain(s);
163 if(!d.empty()) out.push_back(d);
164 };
165
166 if (j.is_array()) {
167 for (auto& v : j) if (v.is_string()) push(v.get<std::string>());
168 } else {
169 if (j.contains("list") && j["list"].is_array())
170 for (auto& v : j["list"]) if (v.is_string()) push(v.get<std::string>());
171 if (j.contains("domains") && j["domains"].is_array())
172 for (auto& v : j["domains"]) if (v.is_string()) push(v.get<std::string>());
173 if (j.contains("whitelist") && j["whitelist"].is_array())
174 for (auto& v : j["whitelist"]) if (v.is_string()) push(v.get<std::string>());
175 }
176 } catch (...) {
177 }
178}

References clean_domain(), and load_list_file().

◆ read_last_lines()

std::string read_last_lines ( const std::string & path,
size_t max_lines = 50 )
static

Definition at line 68 of file local_warning_server.cpp.

68 {
69 std::ifstream file(path, std::ios::binary);
70 if (!file.is_open()) return _("(Log non disponibile)");
71 std::string content;
72 file.seekg(0, std::ios::end);
73 auto size = file.tellg();
74 if (size <= 0) return _("(Log vuoto)");
75 file.seekg(0);
76 content.assign(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
77
78 std::vector<std::string> lines;
79 size_t start = 0;
80 for (size_t i = 0; i < content.size(); ++i) {
81 if (content[i] == '\n') {
82 lines.emplace_back(content.substr(start, i - start));
83 start = i + 1;
84 }
85 }
86 if (start < content.size()) lines.emplace_back(content.substr(start));
87
88 std::ostringstream out;
89 size_t from = lines.size() > max_lines ? lines.size() - max_lines : 0;
90 for (size_t i = from; i < lines.size(); ++i) out << lines[i] << '\n';
91 std::string res = out.str();
92 return res.empty() ? _("(Nessuna attività recente)") : res;
93}

References _, and read_last_lines().

Referenced by read_last_lines().

◆ to_lower()

std::string to_lower ( std::string s)
static

Definition at line 54 of file local_warning_server.cpp.

54 {
55 std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c){ return std::tolower(c); });
56 return s;
57}

References to_lower().

Variable Documentation

◆ g_trusted

std::unordered_set<std::string> g_trusted
static

Definition at line 149 of file local_warning_server.cpp.

◆ g_trusted_loaded

bool g_trusted_loaded = false
static

Definition at line 150 of file local_warning_server.cpp.