Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
brizental committed Oct 18, 2023
1 parent ab2e18c commit 24c36ad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
10 changes: 9 additions & 1 deletion src/daemon/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Daemon* Daemon::instance() {
bool Daemon::activate(const InterfaceConfig& config) {
Q_ASSERT(wgutils() != nullptr);

m_state = Activating;

// There are 3 possible scenarios in which this method is called:
//
// 1. the VPN is off: the method tries to enable the VPN.
Expand All @@ -69,7 +71,10 @@ bool Daemon::activate(const InterfaceConfig& config) {
// If the activation abort's for any reason `the `activationFailure` signal is
// emitted.
logger.debug() << "Activating interface";
auto emit_failure_guard = qScopeGuard([this] { emit activationFailure(); });
auto emit_failure_guard = qScopeGuard([this] {
m_state = Inactive;
emit activationFailure();
});

if (m_connections.contains(config.m_hopType)) {
if (supportServerSwitching(config)) {
Expand Down Expand Up @@ -155,6 +160,7 @@ bool Daemon::activate(const InterfaceConfig& config) {
m_connections[config.m_hopType] = ConnectionState(config);
m_handshakeTimer.start(HANDSHAKE_POLL_MSEC);
emit_failure_guard.dismiss();
m_state = Active;
return true;
}
return false;
Expand Down Expand Up @@ -335,6 +341,8 @@ bool Daemon::parseConfig(const QJsonObject& obj, InterfaceConfig& config) {
bool Daemon::deactivate(bool emitSignals) {
Q_ASSERT(wgutils() != nullptr);

m_state = Inactive;

// Deactivate the main interface.
if (!m_connections.isEmpty()) {
const ConnectionState& state = m_connections.first();
Expand Down
6 changes: 6 additions & 0 deletions src/daemon/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ class Daemon : public QObject {
Switch,
};

enum State { Activating, Active, Inactive };

explicit Daemon(QObject* parent);
~Daemon();

static Daemon* instance();

static bool parseConfig(const QJsonObject& obj, InterfaceConfig& config);

State state() { return m_state; };

virtual bool activate(const InterfaceConfig& config);
virtual bool deactivate(bool emitSignals = true);
virtual QJsonObject getStatus();
Expand Down Expand Up @@ -82,6 +86,8 @@ class Daemon : public QObject {
};
QMap<InterfaceConfig::HopType, ConnectionState> m_connections;
QTimer m_handshakeTimer;

State m_state = Inactive;
};

#endif // DAEMON_H
9 changes: 4 additions & 5 deletions src/daemon/daemonlocalserverconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ bool DaemonLocalServerConnection::isCallerAuthorized(const QString& command) {
int result = getpeereid(m_socket->socketDescriptor(), &uid, &gid);
if (result == -1) {
logger.error() << "Unable to determine if caller is authorized."
"Assuming unauthorized.";
"Assuming unauthorized. Error:"
<< errno;
return false;
}

Expand All @@ -189,10 +190,8 @@ bool DaemonLocalServerConnection::isCallerAuthorized(const QString& command) {
return true;
}

QJsonObject status = Daemon::instance()->getStatus();
bool isConnected = status.value("connected").toBool();

if (isConnected) {
Daemon::State state = Daemon::instance()->state();
if (state != Daemon::Inactive) {
if (m_sessionUid == uid) {
// Case: VPN is currently active and caller is the one who activated it.
// Caller can access the APIs.
Expand Down

0 comments on commit 24c36ad

Please sign in to comment.