Qt Remote objects done
This commit is contained in:
parent
048a673d31
commit
b2392c1943
15 changed files with 240 additions and 423 deletions
|
|
@ -11,7 +11,6 @@ include("3rd/QtSsh/src/botan/botan.pri")
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
../ipc/ipc.h \
|
../ipc/ipc.h \
|
||||||
communicator.h \
|
|
||||||
core/defs.h \
|
core/defs.h \
|
||||||
core/errorstrings.h \
|
core/errorstrings.h \
|
||||||
core/ipcclient.h \
|
core/ipcclient.h \
|
||||||
|
|
@ -19,7 +18,6 @@ HEADERS += \
|
||||||
core/servercontroller.h \
|
core/servercontroller.h \
|
||||||
debug.h \
|
debug.h \
|
||||||
defines.h \
|
defines.h \
|
||||||
localclient.h \
|
|
||||||
managementserver.h \
|
managementserver.h \
|
||||||
message.h \
|
message.h \
|
||||||
protocols/shadowsocksvpnprotocol.h \
|
protocols/shadowsocksvpnprotocol.h \
|
||||||
|
|
@ -33,12 +31,10 @@ HEADERS += \
|
||||||
protocols/openvpnprotocol.h \
|
protocols/openvpnprotocol.h \
|
||||||
|
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
communicator.cpp \
|
|
||||||
core/ipcclient.cpp \
|
core/ipcclient.cpp \
|
||||||
core/openvpnconfigurator.cpp \
|
core/openvpnconfigurator.cpp \
|
||||||
core/servercontroller.cpp \
|
core/servercontroller.cpp \
|
||||||
debug.cpp \
|
debug.cpp \
|
||||||
localclient.cpp \
|
|
||||||
main.cpp \
|
main.cpp \
|
||||||
managementserver.cpp \
|
managementserver.cpp \
|
||||||
message.cpp \
|
message.cpp \
|
||||||
|
|
|
||||||
|
|
@ -1,79 +0,0 @@
|
||||||
#include "communicator.h"
|
|
||||||
#include "defines.h"
|
|
||||||
#include "localclient.h"
|
|
||||||
#include "utils.h"
|
|
||||||
|
|
||||||
Communicator::Communicator(QObject* parent) : QObject(parent),
|
|
||||||
m_localClient(nullptr)
|
|
||||||
{
|
|
||||||
connectToServer();
|
|
||||||
}
|
|
||||||
|
|
||||||
Communicator::~Communicator()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void Communicator::connectToServer()
|
|
||||||
{
|
|
||||||
if (m_localClient) {
|
|
||||||
delete m_localClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_localClient = new LocalClient(this);
|
|
||||||
connect(m_localClient, &LocalClient::connected, this, &Communicator::onConnected);
|
|
||||||
connect(m_localClient, &LocalClient::lineAvailable, this, &Communicator::onLineAvailable);
|
|
||||||
|
|
||||||
m_localClient->connectToServer(Utils::serverName());
|
|
||||||
}
|
|
||||||
|
|
||||||
void Communicator::onConnected()
|
|
||||||
{
|
|
||||||
qDebug().noquote() << QString("Connected to local server '%1'").arg(m_localClient->serverName());
|
|
||||||
Message message(Message::State::Initialize, QStringList({"Client"}));
|
|
||||||
sendMessage(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Communicator::onLineAvailable(const QString& line)
|
|
||||||
{
|
|
||||||
Message message(line);
|
|
||||||
if (!message.isValid()) {
|
|
||||||
qDebug() << "Message is not valid";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
emit messageReceived(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Communicator::isConnected() const
|
|
||||||
{
|
|
||||||
if (!m_localClient) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return m_localClient->connectedState();
|
|
||||||
}
|
|
||||||
|
|
||||||
QString Communicator::readData()
|
|
||||||
{
|
|
||||||
return QString();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Communicator::writeData(const QString& data)
|
|
||||||
{
|
|
||||||
return m_localClient->write(data.toUtf8());
|
|
||||||
}
|
|
||||||
|
|
||||||
void Communicator::sendMessage(const Message& message)
|
|
||||||
{
|
|
||||||
if (!isConnected()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const QString data = message.toString();
|
|
||||||
bool status = writeData(data + "\n");
|
|
||||||
|
|
||||||
qDebug().noquote() << QString("Send message '%1',%2 status '%2'").
|
|
||||||
arg(static_cast<int>(message.state())).
|
|
||||||
arg(data).
|
|
||||||
arg(Utils::toString(status));
|
|
||||||
}
|
|
||||||
|
|
@ -1,41 +0,0 @@
|
||||||
#ifndef COMMUNICATOR_H
|
|
||||||
#define COMMUNICATOR_H
|
|
||||||
|
|
||||||
#include <QObject>
|
|
||||||
#include <QStringList>
|
|
||||||
|
|
||||||
#include "message.h"
|
|
||||||
|
|
||||||
class LocalClient;
|
|
||||||
|
|
||||||
class Communicator : public QObject
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
|
|
||||||
public:
|
|
||||||
explicit Communicator(QObject* parent = nullptr);
|
|
||||||
~Communicator();
|
|
||||||
|
|
||||||
bool isConnected() const;
|
|
||||||
void sendMessage(const Message& message);
|
|
||||||
|
|
||||||
signals:
|
|
||||||
void messageReceived(const Message& message);
|
|
||||||
|
|
||||||
void comminicatorConnected();
|
|
||||||
void comminicatorDisconnected();
|
|
||||||
|
|
||||||
protected slots:
|
|
||||||
void onConnected();
|
|
||||||
void onLineAvailable(const QString& line);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
QString readData();
|
|
||||||
bool writeData(const QString& data);
|
|
||||||
void connectToServer();
|
|
||||||
|
|
||||||
LocalClient* m_localClient;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
#endif // COMMUNICATOR_H
|
|
||||||
|
|
@ -1,62 +0,0 @@
|
||||||
#include <QDebug>
|
|
||||||
#include <QtNetwork>
|
|
||||||
|
|
||||||
#include "localclient.h"
|
|
||||||
|
|
||||||
LocalClient::LocalClient(QObject *parent) : QObject(parent),
|
|
||||||
m_socket(new QLocalSocket(this))
|
|
||||||
{
|
|
||||||
m_in.setDevice(m_socket);
|
|
||||||
m_in.setVersion(QDataStream::Qt_5_10);
|
|
||||||
|
|
||||||
connect(m_socket, &QLocalSocket::readyRead, this, &LocalClient::onReadyRead);
|
|
||||||
connect(m_socket, &QLocalSocket::connected, this, &LocalClient::onConnected);
|
|
||||||
connect(m_socket, QOverload<QLocalSocket::LocalSocketError>::of(&QLocalSocket::error), this, &LocalClient::displayError);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LocalClient::connectToServer(const QString& name)
|
|
||||||
{
|
|
||||||
m_blockSize = 0;
|
|
||||||
m_socket->abort();
|
|
||||||
m_socket->connectToServer(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
QString LocalClient::serverName() const
|
|
||||||
{
|
|
||||||
return m_socket->serverName();
|
|
||||||
}
|
|
||||||
|
|
||||||
void LocalClient::onConnected()
|
|
||||||
{
|
|
||||||
emit connected();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool LocalClient::connectedState() const
|
|
||||||
{
|
|
||||||
return (m_socket->state() == QLocalSocket::ConnectedState);
|
|
||||||
}
|
|
||||||
|
|
||||||
quint64 LocalClient::write(const QByteArray& data)
|
|
||||||
{
|
|
||||||
return m_socket->write(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LocalClient::onReadyRead()
|
|
||||||
{
|
|
||||||
if (m_socket->canReadLine()) {
|
|
||||||
char buf[1024];
|
|
||||||
qint64 lineLength = m_socket->readLine(buf, sizeof(buf));
|
|
||||||
if (lineLength != -1) {
|
|
||||||
QString line = buf;
|
|
||||||
line = line.simplified();
|
|
||||||
qDebug().noquote() << QString("Read line: '%1'").arg(line);
|
|
||||||
emit lineAvailable(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void LocalClient::displayError(QLocalSocket::LocalSocketError socketError)
|
|
||||||
{
|
|
||||||
Q_UNUSED(socketError)
|
|
||||||
qDebug().noquote() << QString("The following error occurred: %1.").arg(m_socket->errorString());
|
|
||||||
}
|
|
||||||
|
|
@ -1,34 +0,0 @@
|
||||||
#ifndef LOCALCLIENT_H
|
|
||||||
#define LOCALCLIENT_H
|
|
||||||
|
|
||||||
#include <QDataStream>
|
|
||||||
#include <QLocalSocket>
|
|
||||||
|
|
||||||
class LocalClient : public QObject
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
|
|
||||||
public:
|
|
||||||
explicit LocalClient(QObject *parent = nullptr);
|
|
||||||
|
|
||||||
QString serverName() const;
|
|
||||||
bool connectedState() const;
|
|
||||||
quint64 write(const QByteArray& data);
|
|
||||||
void connectToServer(const QString& name);
|
|
||||||
|
|
||||||
signals:
|
|
||||||
void connected();
|
|
||||||
void lineAvailable(const QString& line);
|
|
||||||
|
|
||||||
private slots:
|
|
||||||
void displayError(QLocalSocket::LocalSocketError socketError);
|
|
||||||
void onConnected();
|
|
||||||
void onReadyRead();
|
|
||||||
|
|
||||||
private:
|
|
||||||
QLocalSocket* m_socket;
|
|
||||||
QDataStream m_in;
|
|
||||||
quint32 m_blockSize;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // LOCALCLIENT_H
|
|
||||||
|
|
@ -15,7 +15,7 @@ OpenVpnProtocol::OpenVpnProtocol(const QString& args, QObject* parent) :
|
||||||
//m_requestFromUserToStop(false)
|
//m_requestFromUserToStop(false)
|
||||||
{
|
{
|
||||||
setConfigFile(args);
|
setConfigFile(args);
|
||||||
connect(m_communicator, &Communicator::messageReceived, this, &OpenVpnProtocol::onMessageReceived);
|
//connect(m_communicator, &Communicator::messageReceived, this, &OpenVpnProtocol::onMessageReceived);
|
||||||
connect(&m_managementServer, &ManagementServer::readyRead, this, &OpenVpnProtocol::onReadyReadDataFromManagementServer);
|
connect(&m_managementServer, &ManagementServer::readyRead, this, &OpenVpnProtocol::onReadyReadDataFromManagementServer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -172,14 +172,26 @@ ErrorCode OpenVpnProtocol::start()
|
||||||
return ErrorCode::AmneziaServiceConnectionFailed;
|
return ErrorCode::AmneziaServiceConnectionFailed;
|
||||||
}
|
}
|
||||||
process->setProgram(openVpnExecPath());
|
process->setProgram(openVpnExecPath());
|
||||||
process->setArguments(QStringList() << "--config" << configPath()<<
|
QStringList arguments({"--config" , configPath(),
|
||||||
"--management"<< m_managementHost<< QString::number(m_managementPort)<<
|
"--management", m_managementHost, QString::number(m_managementPort),
|
||||||
"--management-client"<<
|
"--management-client",
|
||||||
"--log-append"<< vpnLogFileNamePath);
|
"--log-append", vpnLogFileNamePath
|
||||||
|
});
|
||||||
|
process->setArguments(arguments);
|
||||||
|
|
||||||
|
qDebug() << arguments.join(" ");
|
||||||
|
connect(process.data(), &IpcProcessInterfaceReplica::errorOccurred, [&](QProcess::ProcessError error) {
|
||||||
|
qDebug() << "IpcProcessInterfaceReplica errorOccurred" << error;
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(process.data(), &IpcProcessInterfaceReplica::stateChanged, [&](QProcess::ProcessState newState) {
|
||||||
|
qDebug() << "IpcProcessInterfaceReplica stateChanged" << newState;
|
||||||
|
});
|
||||||
|
|
||||||
process->start();
|
process->start();
|
||||||
|
|
||||||
//m_communicator->sendMessage(Message(Message::State::StartRequest, args));
|
//m_communicator->sendMessage(Message(Message::State::StartRequest, args));
|
||||||
startTimeoutTimer();
|
//startTimeoutTimer();
|
||||||
|
|
||||||
return ErrorCode::NoError;
|
return ErrorCode::NoError;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
#include "vpnprotocol.h"
|
#include "vpnprotocol.h"
|
||||||
#include "core/errorstrings.h"
|
#include "core/errorstrings.h"
|
||||||
|
|
||||||
Communicator* VpnProtocol::m_communicator = nullptr;
|
//Communicator* VpnProtocol::m_communicator = nullptr;
|
||||||
|
|
||||||
VpnProtocol::VpnProtocol(const QString& args, QObject* parent)
|
VpnProtocol::VpnProtocol(const QString& args, QObject* parent)
|
||||||
: QObject(parent),
|
: QObject(parent),
|
||||||
|
|
@ -20,17 +20,17 @@ VpnProtocol::VpnProtocol(const QString& args, QObject* parent)
|
||||||
Q_UNUSED(args)
|
Q_UNUSED(args)
|
||||||
}
|
}
|
||||||
|
|
||||||
void VpnProtocol::initializeCommunicator(QObject* parent)
|
//void VpnProtocol::initializeCommunicator(QObject* parent)
|
||||||
{
|
//{
|
||||||
if (!m_communicator) {
|
// if (!m_communicator) {
|
||||||
m_communicator = new Communicator(parent);
|
// m_communicator = new Communicator(parent);
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
Communicator* VpnProtocol::communicator()
|
//Communicator* VpnProtocol::communicator()
|
||||||
{
|
//{
|
||||||
return m_communicator;
|
// return m_communicator;
|
||||||
}
|
//}
|
||||||
|
|
||||||
void VpnProtocol::setLastError(ErrorCode lastError)
|
void VpnProtocol::setLastError(ErrorCode lastError)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ public:
|
||||||
|
|
||||||
static Communicator* communicator();
|
static Communicator* communicator();
|
||||||
static QString textConnectionState(ConnectionState connectionState);
|
static QString textConnectionState(ConnectionState connectionState);
|
||||||
static void initializeCommunicator(QObject* parent = nullptr);
|
//static void initializeCommunicator(QObject* parent = nullptr);
|
||||||
|
|
||||||
|
|
||||||
virtual bool onConnected() const;
|
virtual bool onConnected() const;
|
||||||
|
|
@ -54,7 +54,7 @@ protected:
|
||||||
virtual void setBytesChanged(quint64 receivedBytes, quint64 sentBytes);
|
virtual void setBytesChanged(quint64 receivedBytes, quint64 sentBytes);
|
||||||
virtual void setConnectionState(VpnProtocol::ConnectionState state);
|
virtual void setConnectionState(VpnProtocol::ConnectionState state);
|
||||||
|
|
||||||
static Communicator* m_communicator;
|
//static Communicator* m_communicator;
|
||||||
|
|
||||||
ConnectionState m_connectionState;
|
ConnectionState m_connectionState;
|
||||||
QString m_routeGateway;
|
QString m_routeGateway;
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@
|
||||||
|
|
||||||
VpnConnection::VpnConnection(QObject* parent) : QObject(parent)
|
VpnConnection::VpnConnection(QObject* parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
VpnProtocol::initializeCommunicator(parent);
|
//VpnProtocol::initializeCommunicator(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
void VpnConnection::onBytesChanged(quint64 receivedBytes, quint64 sentBytes)
|
void VpnConnection::onBytesChanged(quint64 receivedBytes, quint64 sentBytes)
|
||||||
|
|
|
||||||
|
|
@ -3,25 +3,51 @@
|
||||||
|
|
||||||
IpcServerProcess::IpcServerProcess(QObject *parent) :
|
IpcServerProcess::IpcServerProcess(QObject *parent) :
|
||||||
IpcProcessInterfaceSource(parent),
|
IpcProcessInterfaceSource(parent),
|
||||||
m_process(QSharedPointer<QProcess>(new QProcess(this)))
|
m_process(QSharedPointer<QProcess>(new QProcess()))
|
||||||
{
|
{
|
||||||
connect(m_process.data(), &QProcess::errorOccurred, this, &IpcServerProcess::errorOccurred);
|
// connect(m_process.data(), &QProcess::errorOccurred, this, &IpcServerProcess::errorOccurred);
|
||||||
connect(m_process.data(), QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &IpcServerProcess::finished);
|
// connect(m_process.data(), QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &IpcServerProcess::finished);
|
||||||
connect(m_process.data(), &QProcess::readyReadStandardError, this, &IpcServerProcess::readyReadStandardError);
|
// connect(m_process.data(), &QProcess::readyReadStandardError, this, &IpcServerProcess::readyReadStandardError);
|
||||||
connect(m_process.data(), &QProcess::readyReadStandardOutput, this, &IpcServerProcess::readyReadStandardOutput);
|
// connect(m_process.data(), &QProcess::readyReadStandardOutput, this, &IpcServerProcess::readyReadStandardOutput);
|
||||||
connect(m_process.data(), &QProcess::started, this, &IpcServerProcess::started);
|
// connect(m_process.data(), &QProcess::started, this, &IpcServerProcess::started);
|
||||||
connect(m_process.data(), &QProcess::stateChanged, this, &IpcServerProcess::stateChanged);
|
// connect(m_process.data(), &QProcess::stateChanged, this, &IpcServerProcess::stateChanged);
|
||||||
|
|
||||||
|
connect(m_process.data(), &QProcess::errorOccurred, [&](QProcess::ProcessError error){
|
||||||
|
qDebug() << "IpcServerProcess errorOccurred " << error;
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(m_process.data(), &QProcess::readyReadStandardError, [&](){
|
||||||
|
qDebug() << "IpcServerProcess StandardError " << m_process->readAllStandardError();
|
||||||
|
|
||||||
|
});
|
||||||
|
connect(m_process.data(), &QProcess::readyReadStandardOutput, [&](){
|
||||||
|
qDebug() << "IpcServerProcess StandardOutput " << m_process->readAllStandardOutput();
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(m_process.data(), &QProcess::readyRead, [&](){
|
||||||
|
qDebug() << "IpcServerProcess StandardOutput " << m_process->readAll();
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void IpcServerProcess::start(const QString &program, const QStringList &args)
|
void IpcServerProcess::start(const QString &program, const QStringList &arguments)
|
||||||
{
|
{
|
||||||
m_process->start(program, args);
|
m_process->start(program, arguments);
|
||||||
|
qDebug() << "IpcServerProcess started, " << arguments;
|
||||||
|
|
||||||
|
m_process->waitForStarted();
|
||||||
|
qDebug() << "waitForStarted started, " << m_process->errorString();
|
||||||
}
|
}
|
||||||
|
|
||||||
void IpcServerProcess::start()
|
void IpcServerProcess::start()
|
||||||
{
|
{
|
||||||
m_process->start();
|
m_process->start();
|
||||||
qDebug() << "IpcServerProcess started, " << m_process->arguments();
|
qDebug() << "IpcServerProcess started, " << m_process->program() << m_process->arguments();
|
||||||
|
|
||||||
|
m_process->waitForStarted();
|
||||||
|
qDebug() << "waitForStarted , " << m_process->errorString() << m_process->error();
|
||||||
}
|
}
|
||||||
|
|
||||||
void IpcServerProcess::close()
|
void IpcServerProcess::close()
|
||||||
|
|
@ -32,7 +58,6 @@ void IpcServerProcess::close()
|
||||||
void IpcServerProcess::setArguments(const QStringList &arguments)
|
void IpcServerProcess::setArguments(const QStringList &arguments)
|
||||||
{
|
{
|
||||||
m_process->setArguments(arguments);
|
m_process->setArguments(arguments);
|
||||||
qDebug() << "IpcServerProcess started, " << arguments;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void IpcServerProcess::setInputChannelMode(QProcess::InputChannelMode mode)
|
void IpcServerProcess::setInputChannelMode(QProcess::InputChannelMode mode)
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ class IpcServerProcess : public IpcProcessInterfaceSource
|
||||||
public:
|
public:
|
||||||
explicit IpcServerProcess(QObject *parent = nullptr);
|
explicit IpcServerProcess(QObject *parent = nullptr);
|
||||||
|
|
||||||
void start(const QString &program, const QStringList &args) override;
|
void start(const QString &program, const QStringList &arguments) override;
|
||||||
void start() override;
|
void start() override;
|
||||||
void close() override;
|
void close() override;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,12 +14,12 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
LocalServer::LocalServer(QObject *parent) : QObject(parent),
|
LocalServer::LocalServer(QObject *parent) : QObject(parent),
|
||||||
m_clientConnection(nullptr),
|
// m_clientConnection(nullptr),
|
||||||
m_clientConnected(false),
|
// m_clientConnected(false),
|
||||||
m_ipcServer(this)
|
m_ipcServer(this)
|
||||||
{
|
{
|
||||||
m_server = QSharedPointer<QLocalServer>(new QLocalServer(this));
|
// m_server = QSharedPointer<QLocalServer>(new QLocalServer(this));
|
||||||
m_server->setSocketOptions(QLocalServer::WorldAccessOption);
|
// m_server->setSocketOptions(QLocalServer::WorldAccessOption);
|
||||||
|
|
||||||
// if (!m_server->listen(Utils::serverName())) {
|
// if (!m_server->listen(Utils::serverName())) {
|
||||||
// qDebug() << QString("Unable to start the server: %1.").arg(m_server->errorString());
|
// qDebug() << QString("Unable to start the server: %1.").arg(m_server->errorString());
|
||||||
|
|
@ -36,186 +36,186 @@ LocalServer::LocalServer(QObject *parent) : QObject(parent),
|
||||||
|
|
||||||
LocalServer::~LocalServer()
|
LocalServer::~LocalServer()
|
||||||
{
|
{
|
||||||
m_clientConnected = false;
|
// m_clientConnected = false;
|
||||||
m_server->disconnect();
|
// m_server->disconnect();
|
||||||
|
|
||||||
QFile::remove(Utils::serverName());
|
// QFile::remove(Utils::serverName());
|
||||||
|
|
||||||
qDebug() << "Local server stopped";
|
qDebug() << "Local server stopped";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LocalServer::isRunning() const
|
//bool LocalServer::isRunning() const
|
||||||
{
|
//{
|
||||||
return true;
|
// return true;
|
||||||
//return m_server->isListening();
|
// //return m_server->isListening();
|
||||||
}
|
//}
|
||||||
|
|
||||||
void LocalServer::onNewConnection()
|
//void LocalServer::onNewConnection()
|
||||||
{
|
//{
|
||||||
if (m_clientConnection) {
|
// if (m_clientConnection) {
|
||||||
m_clientConnection->deleteLater();
|
// m_clientConnection->deleteLater();
|
||||||
}
|
// }
|
||||||
|
|
||||||
m_clientConnection = m_server->nextPendingConnection();
|
// m_clientConnection = m_server->nextPendingConnection();
|
||||||
connect(m_clientConnection, &QLocalSocket::disconnected, this, &LocalServer::onDisconnected);
|
// connect(m_clientConnection, &QLocalSocket::disconnected, this, &LocalServer::onDisconnected);
|
||||||
m_clientConnected = true;
|
// m_clientConnected = true;
|
||||||
|
|
||||||
qDebug() << "New connection";
|
// qDebug() << "New connection";
|
||||||
|
|
||||||
for(;;) {
|
// for(;;) {
|
||||||
qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
|
// qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
|
||||||
if (!m_clientConnected || !m_clientConnection) {
|
// if (!m_clientConnected || !m_clientConnection) {
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (m_clientConnection->waitForReadyRead(1000) && m_clientConnection->canReadLine()) {
|
// if (m_clientConnection->waitForReadyRead(1000) && m_clientConnection->canReadLine()) {
|
||||||
char buf[1024];
|
// char buf[1024];
|
||||||
qint64 lineLength = m_clientConnection->readLine(buf, sizeof(buf));
|
// qint64 lineLength = m_clientConnection->readLine(buf, sizeof(buf));
|
||||||
if (lineLength != -1) {
|
// if (lineLength != -1) {
|
||||||
QString line = buf;
|
// QString line = buf;
|
||||||
line = line.simplified();
|
// line = line.simplified();
|
||||||
qDebug().noquote() << QString("Read line: '%1'").arg(line);
|
// qDebug().noquote() << QString("Read line: '%1'").arg(line);
|
||||||
Message incomingMessage(line);
|
// Message incomingMessage(line);
|
||||||
if (!incomingMessage.isValid()) {
|
// if (!incomingMessage.isValid()) {
|
||||||
qWarning().noquote() << "Message is not valid!";
|
// qWarning().noquote() << "Message is not valid!";
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
else {
|
// else {
|
||||||
qDebug().noquote() << QString("Got message id: '%1'").arg(static_cast<int>(incomingMessage.state()));
|
// qDebug().noquote() << QString("Got message id: '%1'").arg(static_cast<int>(incomingMessage.state()));
|
||||||
//qDebug().noquote() << incomingMessage.rawData();
|
// //qDebug().noquote() << incomingMessage.rawData();
|
||||||
}
|
// }
|
||||||
|
|
||||||
switch (incomingMessage.state()) {
|
// switch (incomingMessage.state()) {
|
||||||
case Message::State::Initialize:
|
// case Message::State::Initialize:
|
||||||
#ifdef Q_OS_WIN
|
// #ifdef Q_OS_WIN
|
||||||
TapController::Instance().checkAndSetup();
|
// TapController::Instance().checkAndSetup();
|
||||||
#endif
|
// #endif
|
||||||
sendMessage(Message(Message::State::Initialize, QStringList({"Server"})));
|
// sendMessage(Message(Message::State::Initialize, QStringList({"Server"})));
|
||||||
break;
|
// break;
|
||||||
case Message::State::StartRequest:
|
// case Message::State::StartRequest:
|
||||||
startProcess(incomingMessage.args());
|
// startProcess(incomingMessage.args());
|
||||||
break;
|
// break;
|
||||||
case Message::State::FinishRequest:
|
// case Message::State::FinishRequest:
|
||||||
finishProcess(incomingMessage.args());
|
// finishProcess(incomingMessage.args());
|
||||||
break;
|
// break;
|
||||||
|
|
||||||
case Message::State::RoutesAddRequest:
|
// case Message::State::RoutesAddRequest:
|
||||||
routesAddRequest(incomingMessage.args());
|
// routesAddRequest(incomingMessage.args());
|
||||||
break;
|
// break;
|
||||||
case Message::State::RouteDeleteRequest:
|
// case Message::State::RouteDeleteRequest:
|
||||||
routeDeleteRequest(incomingMessage.args());
|
// routeDeleteRequest(incomingMessage.args());
|
||||||
break;
|
// break;
|
||||||
case Message::State::ClearSavedRoutesRequest:
|
// case Message::State::ClearSavedRoutesRequest:
|
||||||
Router::Instance().clearSavedRoutes();
|
// Router::Instance().clearSavedRoutes();
|
||||||
break;
|
// break;
|
||||||
case Message::State::FlushDnsRequest:
|
// case Message::State::FlushDnsRequest:
|
||||||
Router::Instance().flushDns();
|
// Router::Instance().flushDns();
|
||||||
break;
|
// break;
|
||||||
case Message::State::InstallDriverRequest:
|
// case Message::State::InstallDriverRequest:
|
||||||
checkAndInstallDriver(incomingMessage.args());
|
// checkAndInstallDriver(incomingMessage.args());
|
||||||
break;
|
// break;
|
||||||
|
|
||||||
default:
|
// default:
|
||||||
;
|
// ;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
qDebug() << "Released";
|
// qDebug() << "Released";
|
||||||
}
|
//}
|
||||||
|
|
||||||
void LocalServer::finishProcess(const QStringList& args)
|
//void LocalServer::finishProcess(const QStringList& args)
|
||||||
{
|
//{
|
||||||
Q_UNUSED(args)
|
// Q_UNUSED(args)
|
||||||
}
|
//}
|
||||||
|
|
||||||
void LocalServer::startProcess(const QStringList& messageArgs)
|
//void LocalServer::startProcess(const QStringList& messageArgs)
|
||||||
{
|
//{
|
||||||
if (messageArgs.size() < 1) {
|
// if (messageArgs.size() < 1) {
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
QProcess* process = new QProcess();
|
// QProcess* process = new QProcess();
|
||||||
connect(process, SIGNAL(started()), this, SLOT(onStarted()));
|
// connect(process, SIGNAL(started()), this, SLOT(onStarted()));
|
||||||
connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onFinished(int, QProcess::ExitStatus)));
|
// connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onFinished(int, QProcess::ExitStatus)));
|
||||||
|
|
||||||
const QString program = messageArgs.at(0);
|
// const QString program = messageArgs.at(0);
|
||||||
QStringList args;
|
// QStringList args;
|
||||||
for (int i = 1; i < messageArgs.size(); i++) {
|
// for (int i = 1; i < messageArgs.size(); i++) {
|
||||||
args.append(messageArgs.at(i));
|
// args.append(messageArgs.at(i));
|
||||||
}
|
// }
|
||||||
|
|
||||||
QFileInfo fi(program);
|
// QFileInfo fi(program);
|
||||||
const QString baseName = fi.baseName();
|
// const QString baseName = fi.baseName();
|
||||||
if (!fi.exists()) {
|
// if (!fi.exists()) {
|
||||||
qWarning() << "This program does not exist";
|
// qWarning() << "This program does not exist";
|
||||||
sendMessage(Message(Message::State::Started, QStringList({baseName})));
|
// sendMessage(Message(Message::State::Started, QStringList({baseName})));
|
||||||
sendMessage(Message(Message::State::Finished, QStringList({baseName, QString::number(-1)})));
|
// sendMessage(Message(Message::State::Finished, QStringList({baseName, QString::number(-1)})));
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
process->setObjectName(baseName);
|
// process->setObjectName(baseName);
|
||||||
|
|
||||||
qDebug().noquote() << QString("Start process '%1' - '%2' with args '%3'")
|
// qDebug().noquote() << QString("Start process '%1' - '%2' with args '%3'")
|
||||||
.arg(baseName).arg(program).arg(args.join(","));
|
// .arg(baseName).arg(program).arg(args.join(","));
|
||||||
|
|
||||||
process->start(program, args);
|
// process->start(program, args);
|
||||||
m_processList.append(process);
|
// m_processList.append(process);
|
||||||
}
|
//}
|
||||||
|
|
||||||
void LocalServer::routesAddRequest(const QStringList &messageArgs)
|
//void LocalServer::routesAddRequest(const QStringList &messageArgs)
|
||||||
{
|
//{
|
||||||
Router::Instance().routeAddList(messageArgs.first(), messageArgs.mid(1));
|
// Router::Instance().routeAddList(messageArgs.first(), messageArgs.mid(1));
|
||||||
}
|
//}
|
||||||
|
|
||||||
void LocalServer::routeDeleteRequest(const QStringList &messageArgs)
|
//void LocalServer::routeDeleteRequest(const QStringList &messageArgs)
|
||||||
{
|
//{
|
||||||
Router::Instance().routeDelete(messageArgs.first());
|
// Router::Instance().routeDelete(messageArgs.first());
|
||||||
}
|
//}
|
||||||
|
|
||||||
void LocalServer::checkAndInstallDriver(const QStringList &messageArgs)
|
//void LocalServer::checkAndInstallDriver(const QStringList &messageArgs)
|
||||||
{
|
//{
|
||||||
|
|
||||||
}
|
//}
|
||||||
|
|
||||||
void LocalServer::onFinished(int exitCode, QProcess::ExitStatus exitStatus)
|
//void LocalServer::onFinished(int exitCode, QProcess::ExitStatus exitStatus)
|
||||||
{
|
//{
|
||||||
Q_UNUSED(exitStatus)
|
// Q_UNUSED(exitStatus)
|
||||||
|
|
||||||
QProcess* process = (QProcess*)sender();
|
// QProcess* process = (QProcess*)sender();
|
||||||
sendMessage(Message(Message::State::Finished, QStringList({process->objectName(), QString::number(exitCode)})));
|
// sendMessage(Message(Message::State::Finished, QStringList({process->objectName(), QString::number(exitCode)})));
|
||||||
}
|
//}
|
||||||
|
|
||||||
void LocalServer::onStarted()
|
//void LocalServer::onStarted()
|
||||||
{
|
//{
|
||||||
QProcess* process = (QProcess*)sender();
|
// QProcess* process = (QProcess*)sender();
|
||||||
sendMessage(Message(Message::State::Started, QStringList({process->objectName()})));
|
// sendMessage(Message(Message::State::Started, QStringList({process->objectName()})));
|
||||||
}
|
//}
|
||||||
|
|
||||||
void LocalServer::onDisconnected()
|
//void LocalServer::onDisconnected()
|
||||||
{
|
//{
|
||||||
if (!m_clientConnected) {
|
// if (!m_clientConnected) {
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
m_clientConnected = false;
|
// m_clientConnected = false;
|
||||||
QLocalSocket* clientConnection = (QLocalSocket*)sender();
|
// QLocalSocket* clientConnection = (QLocalSocket*)sender();
|
||||||
clientConnection->deleteLater();
|
// clientConnection->deleteLater();
|
||||||
|
|
||||||
qDebug() << "Diconnected";
|
// qDebug() << "Diconnected";
|
||||||
}
|
//}
|
||||||
|
|
||||||
void LocalServer::sendMessage(const Message& message)
|
//void LocalServer::sendMessage(const Message& message)
|
||||||
{
|
//{
|
||||||
if (!m_clientConnection || !m_clientConnected) {
|
// if (!m_clientConnection || !m_clientConnected) {
|
||||||
qDebug()<< "Cannot send data, remote peer is not connected";
|
// qDebug()<< "Cannot send data, remote peer is not connected";
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
const QString data = message.toString();
|
// const QString data = message.toString();
|
||||||
bool status = m_clientConnection->write(QString(data + "\n").toUtf8());
|
// bool status = m_clientConnection->write(QString(data + "\n").toUtf8());
|
||||||
|
|
||||||
qDebug().noquote() << QString("Send message '%1', status '%2'").arg(data).arg(Utils::toString(status));
|
// qDebug().noquote() << QString("Send message '%1', status '%2'").arg(data).arg(Utils::toString(status));
|
||||||
}
|
//}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,30 +23,30 @@ public:
|
||||||
explicit LocalServer(QObject* parent = nullptr);
|
explicit LocalServer(QObject* parent = nullptr);
|
||||||
~LocalServer();
|
~LocalServer();
|
||||||
|
|
||||||
bool isRunning() const;
|
// bool isRunning() const;
|
||||||
|
|
||||||
protected slots:
|
protected slots:
|
||||||
void onDisconnected();
|
// void onDisconnected();
|
||||||
void onNewConnection();
|
// void onNewConnection();
|
||||||
|
|
||||||
void onFinished(int exitCode, QProcess::ExitStatus exitStatus);
|
// void onFinished(int exitCode, QProcess::ExitStatus exitStatus);
|
||||||
void onStarted();
|
// void onStarted();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void finishProcess(const QStringList& messageArgs);
|
// void finishProcess(const QStringList& messageArgs);
|
||||||
void sendMessage(const Message& message);
|
// void sendMessage(const Message& message);
|
||||||
void startProcess(const QStringList& messageArgs);
|
// void startProcess(const QStringList& messageArgs);
|
||||||
|
|
||||||
void routesAddRequest(const QStringList& messageArgs);
|
// void routesAddRequest(const QStringList& messageArgs);
|
||||||
void routeDeleteRequest(const QStringList& messageArgs);
|
// void routeDeleteRequest(const QStringList& messageArgs);
|
||||||
|
|
||||||
void checkAndInstallDriver(const QStringList& messageArgs);
|
// void checkAndInstallDriver(const QStringList& messageArgs);
|
||||||
|
|
||||||
QSharedPointer<QLocalServer> m_server;
|
QSharedPointer<QLocalServer> m_server;
|
||||||
QPointer <QLocalSocket> m_clientConnection;
|
// QPointer <QLocalSocket> m_clientConnection;
|
||||||
|
|
||||||
QVector<QProcess*> m_processList;
|
// QVector<QProcess*> m_processList;
|
||||||
bool m_clientConnected;
|
// bool m_clientConnected;
|
||||||
|
|
||||||
IpcServer m_ipcServer;
|
IpcServer m_ipcServer;
|
||||||
QRemoteObjectHost m_serverNode;
|
QRemoteObjectHost m_serverNode;
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,9 @@ int runApplication(int argc, char** argv)
|
||||||
{
|
{
|
||||||
QCoreApplication app(argc,argv);
|
QCoreApplication app(argc,argv);
|
||||||
LocalServer localServer;
|
LocalServer localServer;
|
||||||
if (!localServer.isRunning()) {
|
// if (!localServer.isRunning()) {
|
||||||
return -1;
|
// return -1;
|
||||||
}
|
// }
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
|
|
|
||||||
|
|
@ -13,9 +13,9 @@ void SystemService::start()
|
||||||
QCoreApplication* app = application();
|
QCoreApplication* app = application();
|
||||||
m_localServer = new LocalServer();
|
m_localServer = new LocalServer();
|
||||||
|
|
||||||
if (!m_localServer->isRunning()) {
|
// if (!m_localServer->isRunning()) {
|
||||||
app->quit();
|
// app->quit();
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
void SystemService::stop()
|
void SystemService::stop()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue