
* cherry-pick 4dfcad96506fb5b88c5bb27342b6d9413fc361c9 from mozilla upstream * cherry-pick a95fa8c088b9edaff2de18751336942c2d145a9a from mozilla * cherry-pick commit 4fc1ebbad86a9abcafdc761725a7afd811c8d2d3 from mozilla * cherry-pick 4dfcad96506fb5b88c5bb27342b6d9413fc361c9 from mozilla upstream * cherry-pick 22de4fcbd454c64ff496c3380eeaeeb6afff4d64 from mozilla upstream * cherry-pick 649673be561b66c96367adf379da1545f8838763 from mozilla upstream * cherry-pick 41bdad34517d0ddaef32139482e5505d92e4b533 from mozilla upstream * cherry-pick f6e49a85538eaa230d3a8634fa7600966132ccab from mozilla upstream * cherry-pick 86c585387efa0a09c7937dfe799a90a666404fcd from mozilla upstream * cherry-pick a18c1fac740469ca3566751b74a16227518630c4 from mozilla upstream * fixed missing ; * added excludeLocalNetworks() for linux * build fixes on windows after cherry-picks * Add rules for excluded sites splittunell mode * Fix app splittunell when ipv6 is not setup * Fix Linux build --------- Co-authored-by: Mykola Baibuz <mykola.baibuz@gmail.com>
172 lines
4.2 KiB
C++
172 lines
4.2 KiB
C++
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#include "daemonlocalserverconnection.h"
|
|
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QJsonValue>
|
|
#include <QLocalSocket>
|
|
|
|
#include "daemon.h"
|
|
#include "leakdetector.h"
|
|
#include "logger.h"
|
|
|
|
namespace {
|
|
Logger logger("DaemonLocalServerConnection");
|
|
}
|
|
|
|
DaemonLocalServerConnection::DaemonLocalServerConnection(QObject* parent,
|
|
QLocalSocket* socket)
|
|
: QObject(parent) {
|
|
MZ_COUNT_CTOR(DaemonLocalServerConnection);
|
|
|
|
logger.debug() << "Connection created";
|
|
|
|
Q_ASSERT(socket);
|
|
m_socket = socket;
|
|
|
|
connect(m_socket, &QLocalSocket::readyRead, this,
|
|
&DaemonLocalServerConnection::readData);
|
|
|
|
Daemon* daemon = Daemon::instance();
|
|
connect(daemon, &Daemon::connected, this,
|
|
&DaemonLocalServerConnection::connected);
|
|
connect(daemon, &Daemon::disconnected, this,
|
|
&DaemonLocalServerConnection::disconnected);
|
|
connect(daemon, &Daemon::backendFailure, this,
|
|
&DaemonLocalServerConnection::backendFailure);
|
|
}
|
|
|
|
DaemonLocalServerConnection::~DaemonLocalServerConnection() {
|
|
MZ_COUNT_DTOR(DaemonLocalServerConnection);
|
|
|
|
logger.debug() << "Connection released";
|
|
}
|
|
|
|
void DaemonLocalServerConnection::readData() {
|
|
logger.debug() << "Read Data";
|
|
|
|
Q_ASSERT(m_socket);
|
|
|
|
while (true) {
|
|
int pos = m_buffer.indexOf("\n");
|
|
if (pos == -1) {
|
|
QByteArray input = m_socket->readAll();
|
|
if (input.isEmpty()) {
|
|
break;
|
|
}
|
|
m_buffer.append(input);
|
|
continue;
|
|
}
|
|
|
|
QByteArray line = m_buffer.left(pos);
|
|
m_buffer.remove(0, pos + 1);
|
|
|
|
QByteArray command(line);
|
|
command = command.trimmed();
|
|
|
|
if (command.isEmpty()) {
|
|
continue;
|
|
}
|
|
|
|
parseCommand(command);
|
|
}
|
|
}
|
|
|
|
void DaemonLocalServerConnection::parseCommand(const QByteArray& data) {
|
|
QJsonDocument json = QJsonDocument::fromJson(data);
|
|
if (!json.isObject()) {
|
|
logger.error() << "Invalid input";
|
|
return;
|
|
}
|
|
|
|
QJsonObject obj = json.object();
|
|
QJsonValue typeValue = obj.value("type");
|
|
if (!typeValue.isString()) {
|
|
logger.warning() << "No type command. Ignoring request.";
|
|
return;
|
|
}
|
|
QString type = typeValue.toString();
|
|
|
|
logger.debug() << "Command received:" << type;
|
|
|
|
// It is expected that sometimes the client will request backend logs
|
|
// before the first authentication. In these cases we just return empty
|
|
// logs.
|
|
if (type == "logs") {
|
|
QJsonObject obj;
|
|
obj.insert("type", "logs");
|
|
obj.insert("logs", "");
|
|
write(obj);
|
|
return;
|
|
}
|
|
|
|
if (type == "activate") {
|
|
InterfaceConfig config;
|
|
if (!Daemon::parseConfig(obj, config)) {
|
|
logger.error() << "Invalid configuration";
|
|
emit disconnected();
|
|
return;
|
|
}
|
|
|
|
if (!Daemon::instance()->activate(config)) {
|
|
logger.error() << "Failed to activate the interface";
|
|
emit disconnected();
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (type == "deactivate") {
|
|
Daemon::instance()->deactivate(true);
|
|
return;
|
|
}
|
|
|
|
if (type == "status") {
|
|
QJsonObject obj = Daemon::instance()->getStatus();
|
|
obj.insert("type", "status");
|
|
write(obj);
|
|
return;
|
|
}
|
|
|
|
if (type == "logs") {
|
|
QJsonObject obj;
|
|
obj.insert("type", "logs");
|
|
obj.insert("logs", Daemon::instance()->logs().replace("\n", "|"));
|
|
write(obj);
|
|
return;
|
|
}
|
|
|
|
if (type == "cleanlogs") {
|
|
Daemon::instance()->cleanLogs();
|
|
return;
|
|
}
|
|
|
|
logger.warning() << "Invalid command:" << type;
|
|
}
|
|
|
|
void DaemonLocalServerConnection::connected(const QString& pubkey) {
|
|
QJsonObject obj;
|
|
obj.insert("type", "connected");
|
|
obj.insert("pubkey", QJsonValue(pubkey));
|
|
write(obj);
|
|
}
|
|
|
|
void DaemonLocalServerConnection::disconnected() {
|
|
QJsonObject obj;
|
|
obj.insert("type", "disconnected");
|
|
write(obj);
|
|
}
|
|
|
|
void DaemonLocalServerConnection::backendFailure(DaemonError err) {
|
|
QJsonObject obj;
|
|
obj.insert("type", "backendFailure");
|
|
obj.insert("errorCode", static_cast<int>(err));
|
|
write(obj);
|
|
}
|
|
|
|
void DaemonLocalServerConnection::write(const QJsonObject& obj) {
|
|
m_socket->write(QJsonDocument(obj).toJson(QJsonDocument::Compact));
|
|
m_socket->write("\n");
|
|
}
|