Skip to content
API Documentation
BastionGuard™
bsc::ui::UIClient Class Reference

#include <UIClient.hpp>

Classes

class  Impl

Public Member Functions

 UIClient (std::string socket_uri)
 ~UIClient ()
 UIClient (const UIClient &)=delete
UIClient & operator= (const UIClient &)=delete
void start ()
void stop ()
bool is_connected () const noexcept
std::optional< bsc::rule::Rule > ask_rule (const bsc::model::Connection &conn)
bool ping (const bsc::stats::Statistics &stats)
bool post_alert (const std::string &text)
bool post_connection_alert (const bsc::model::Connection &conn, const std::string &action, const std::string &rule_name={})
void set_notification_handler (std::function< void(const NotificationEvent &)> cb)

Private Attributes

std::unique_ptr< Impl > impl_

Detailed Description

Client Unix-socket verso il server della UI. Sostituisce la precedente implementazione gRPC. Gestisce:

  • thread di lettura frame che smista pong / notification / ask_rule_reply
  • scrittore serializzato via mutex
  • reconnector che riprova finche' il socket non e' disponibile

Definition at line 35 of file UIClient.hpp.

Constructor & Destructor Documentation

◆ UIClient() [1/2]

bsc::ui::UIClient::UIClient ( std::string socket_uri)
explicit

Definition at line 372 of file UIClient.cpp.

373 : impl_(std::make_unique<Impl>(std::move(socket_uri))) {}
std::unique_ptr< Impl > impl_
Definition UIClient.hpp:58

References impl_.

Referenced by operator=(), and UIClient().

◆ ~UIClient()

bsc::ui::UIClient::~UIClient ( )
default

◆ UIClient() [2/2]

bsc::ui::UIClient::UIClient ( const UIClient & )
delete

References UIClient().

Member Function Documentation

◆ ask_rule()

std::optional< bsc::rule::Rule > bsc::ui::UIClient::ask_rule ( const bsc::model::Connection & conn)

Definition at line 389 of file UIClient.cpp.

389 {
390 if (!impl_->is_connected()) return std::nullopt;
391
392 const auto id = impl_->next_id_++;
393 auto pending = std::make_shared<Impl::PendingAsk>();
394 {
395 std::lock_guard<std::mutex> lk(impl_->pending_mutex_);
396 impl_->pending_asks_[id] = pending;
397 }
398
399 // Costruisci la richiesta
400 json j = {
402 {"id", id}
403 };
404 json cj;
405 connection_to_json(conn, cj);
406 j["connection"] = cj;
407
408 if (!impl_->write_frame(j.dump())) {
409 std::lock_guard<std::mutex> lk(impl_->pending_mutex_);
410 impl_->pending_asks_.erase(id);
411 return std::nullopt;
412 }
413
414 // Aspetta la risposta con timeout 30s (il popup ha countdown 15s,
415 // 30 da margine per il jitter).
416 std::unique_lock<std::mutex> lk(pending->mu);
417 const bool ok = pending->cv.wait_for(lk, std::chrono::seconds(30),
418 [&]() { return pending->ready; });
419
420 {
421 std::lock_guard<std::mutex> lk2(impl_->pending_mutex_);
422 impl_->pending_asks_.erase(id);
423 }
424
425 if (!ok) return std::nullopt; // timeout
426
427 // Estrai la rule dalla risposta
428 bsc::rule::Rule r;
429 r.action = pending->reply.value("action", std::string{"deny"});
430 r.duration = pending->reply.value("duration", std::string{"once"});
431 r.name = pending->reply.value("rule_name", std::string{});
432 r.enabled = true;
433 return r;
434}
constexpr std::string_view kAskRule
Definition Wire.hpp:51
void connection_to_json(const bsc::model::Connection &c, json &j)
Definition UIClient.cpp:34
nlohmann::json json
Definition UIClient.cpp:29

References impl_, and bsc::proto::msg::kAskRule.

Referenced by operator=().

◆ is_connected()

bool bsc::ui::UIClient::is_connected ( ) const
noexcept

Definition at line 380 of file UIClient.cpp.

380 {
381 return impl_->is_connected();
382}

References impl_.

Referenced by operator=().

◆ operator=()

UIClient & bsc::ui::UIClient::operator= ( const UIClient & )
delete

◆ ping()

bool bsc::ui::UIClient::ping ( const bsc::stats::Statistics & stats)

Definition at line 436 of file UIClient.cpp.

436 {
437 if (!impl_->is_connected()) return false;
438 json j = {
439 {"type", bsc::proto::msg::kPing},
440 {"id", impl_->next_id_++},
441 {"stats", {
442 {"daemon_version", "bsc-daemon-cpp"},
443 {"rules", stats.rules()},
444 {"connections", stats.connections()},
445 {"dns_responses", stats.dns_responses()},
446 {"ignored", stats.ignored()},
447 {"accepted", stats.accepted()},
448 {"dropped", stats.dropped()},
449 {"rule_hits", stats.rule_hits()},
450 {"rule_misses", stats.rule_misses()},
451 }}
452 };
453 return impl_->write_frame(j.dump());
454}
std::uint64_t ignored() const
std::uint64_t rule_misses() const
std::uint64_t dns_responses() const
std::uint64_t dropped() const
std::uint64_t connections() const
std::uint64_t rule_hits() const
std::uint64_t accepted() const
std::uint64_t rules() const
constexpr std::string_view kPing
Definition Wire.hpp:49

References impl_, and bsc::proto::msg::kPing.

Referenced by operator=().

◆ post_alert()

bool bsc::ui::UIClient::post_alert ( const std::string & text)

Definition at line 456 of file UIClient.cpp.

456 {
457 json j = {
458 {"type", bsc::proto::msg::kAlert},
459 {"id", impl_->next_id_++},
460 {"level", "info"},
461 {"what", "generic"},
462 {"text", text}
463 };
464 return impl_->send_or_queue_alert(j);
465}
constexpr std::string_view kAlert
Definition Wire.hpp:53

References impl_, and bsc::proto::msg::kAlert.

Referenced by operator=(), and bsc::service::DaemonService::run().

◆ post_connection_alert()

bool bsc::ui::UIClient::post_connection_alert ( const bsc::model::Connection & conn,
const std::string & action,
const std::string & rule_name = {} )

Definition at line 467 of file UIClient.cpp.

469 {
470 json j = {
471 {"type", bsc::proto::msg::kAlert},
472 {"id", impl_->next_id_++},
473 {"level", action == "deny" ? "warning" : "info"},
474 {"what", "connection"},
475 };
476 json cj;
477 connection_to_json(conn, cj);
478 j["connection"] = cj;
479 if (!rule_name.empty()) {
480 j["rule"] = {{"name", rule_name}, {"action", action}};
481 }
482 return impl_->send_or_queue_alert(j);
483}

References impl_, and bsc::proto::msg::kAlert.

Referenced by operator=().

◆ set_notification_handler()

void bsc::ui::UIClient::set_notification_handler ( std::function< void(const NotificationEvent &)> cb)

Definition at line 384 of file UIClient.cpp.

384 {
385 std::lock_guard<std::mutex> lk(impl_->handler_mutex_);
386 impl_->notif_handler_ = std::move(cb);
387}

References impl_.

Referenced by bsc::service::DaemonService::run().

◆ start()

void bsc::ui::UIClient::start ( )

Definition at line 377 of file UIClient.cpp.

377{ impl_->start(); }

References impl_.

Referenced by operator=(), and bsc::service::DaemonService::run().

◆ stop()

void bsc::ui::UIClient::stop ( )

Definition at line 378 of file UIClient.cpp.

378{ impl_->stop(); }

References impl_.

Referenced by operator=().

Member Data Documentation

◆ impl_

std::unique_ptr<Impl> bsc::ui::UIClient::impl_
private

The documentation for this class was generated from the following files:
  • BastionGuard/thirdparty/bastionguard-secure-connection/daemon/include/bsc/ui/UIClient.hpp
  • BastionGuard/thirdparty/bastionguard-secure-connection/daemon/src/ui/UIClient.cpp