Qt ro refact
This commit is contained in:
parent
b2392c1943
commit
b6571d99de
14 changed files with 157 additions and 36 deletions
|
@ -9,29 +9,69 @@ IpcClient &IpcClient::Instance()
|
||||||
|
|
||||||
QSharedPointer<IpcProcessInterfaceReplica> IpcClient::createPrivilegedProcess()
|
QSharedPointer<IpcProcessInterfaceReplica> IpcClient::createPrivilegedProcess()
|
||||||
{
|
{
|
||||||
if (! Instance().m_ipcClient->isReplicaValid()) return nullptr;
|
if (! Instance().m_ipcClient || ! Instance().m_ipcClient->isReplicaValid()) {
|
||||||
|
qWarning() << "IpcClient::createPrivilegedProcess : IpcClient IpcClient replica is not valid";
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
QRemoteObjectPendingReply<int> futureResult = Instance().m_ipcClient->createPrivilegedProcess();
|
QRemoteObjectPendingReply<int> futureResult = Instance().m_ipcClient->createPrivilegedProcess();
|
||||||
futureResult.waitForFinished(1000);
|
futureResult.waitForFinished(1000);
|
||||||
|
|
||||||
int pid = futureResult.returnValue();
|
int pid = futureResult.returnValue();
|
||||||
QSharedPointer<QRemoteObjectNode> replica(new QRemoteObjectNode);
|
QSharedPointer<QRemoteObjectNode> replicaNode(new QRemoteObjectNode);
|
||||||
//Instance().m_processNodes.insert(pid, replica);
|
//Instance().m_processNodes.insert(pid, replica);
|
||||||
|
|
||||||
replica->connectToNode(QUrl(amnezia::getIpcProcessUrl(pid)));
|
|
||||||
auto ptr = QSharedPointer<IpcProcessInterfaceReplica>(replica->acquire<IpcProcessInterfaceReplica>());
|
QSharedPointer<QLocalSocket> socket(new QLocalSocket(replicaNode.data()));
|
||||||
connect(ptr.data(), &IpcProcessInterfaceReplica::destroyed, replica.data(), [replica](){
|
QSharedPointer<IpcProcessInterfaceReplica> ptr;
|
||||||
replica->deleteLater();
|
|
||||||
|
connect(socket.data(), &QLocalSocket::connected, replicaNode.data(), [socket, replicaNode, &ptr]() {
|
||||||
|
replicaNode->addClientSideConnection(socket.data());
|
||||||
|
|
||||||
|
ptr.reset(replicaNode->acquire<IpcProcessInterfaceReplica>());
|
||||||
|
|
||||||
|
ptr->waitForSource(1000);
|
||||||
|
|
||||||
|
if (!ptr->isReplicaValid()) {
|
||||||
|
qWarning() << "IpcProcessInterfaceReplica replica is not connected!";
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
socket->connectToServer(amnezia::getIpcProcessUrl(pid));
|
||||||
|
socket->waitForConnected();
|
||||||
|
|
||||||
|
auto proccessReplica = QSharedPointer<IpcProcessInterfaceReplica>(ptr);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// replica->connectToNode(QUrl(amnezia::getIpcProcessUrl(pid)));
|
||||||
|
// auto ptr = QSharedPointer<IpcProcessInterfaceReplica>(replica->acquire<IpcProcessInterfaceReplica>());
|
||||||
|
connect(proccessReplica.data(), &IpcProcessInterfaceReplica::destroyed, proccessReplica.data(), [replicaNode](){
|
||||||
|
replicaNode->deleteLater();
|
||||||
});
|
});
|
||||||
|
|
||||||
return ptr;
|
return proccessReplica;
|
||||||
}
|
}
|
||||||
|
|
||||||
IpcClient::IpcClient(QObject *parent) : QObject(parent)
|
IpcClient::IpcClient(QObject *parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
m_ClientNode.connectToNode(QUrl(QStringLiteral(IPC_SERVICE_URL)));
|
// m_ClientNode.connectToNode(QUrl(amnezia::getIpcServiceUrl()));
|
||||||
m_ipcClient.reset(m_ClientNode.acquire<IpcInterfaceReplica>());
|
// qDebug() << QUrl(amnezia::getIpcServiceUrl());
|
||||||
m_ipcClient->waitForSource(1000);
|
|
||||||
|
|
||||||
|
m_localSocket.reset(new QLocalSocket(this));
|
||||||
|
connect(m_localSocket.data(), &QLocalSocket::connected, &m_ClientNode, [this]() {
|
||||||
|
m_ClientNode.addClientSideConnection(m_localSocket.data());
|
||||||
|
|
||||||
|
m_ipcClient.reset(m_ClientNode.acquire<IpcInterfaceReplica>());
|
||||||
|
m_ipcClient->waitForSource(1000);
|
||||||
|
|
||||||
|
if (!m_ipcClient->isReplicaValid()) {
|
||||||
|
qWarning() << "IpcClient replica is not connected!";
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
m_localSocket->connectToServer(amnezia::getIpcServiceUrl());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef IPCCLIENT_H
|
#ifndef IPCCLIENT_H
|
||||||
#define IPCCLIENT_H
|
#define IPCCLIENT_H
|
||||||
|
|
||||||
|
#include <QLocalSocket>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
||||||
#include "ipc.h"
|
#include "ipc.h"
|
||||||
|
@ -11,11 +12,11 @@ class IpcClient : public QObject
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
static IpcClient &Instance();
|
static IpcClient &Instance();
|
||||||
|
static bool init() { return Instance().m_ipcClient->isReplicaValid(); }
|
||||||
|
static QSharedPointer<IpcInterfaceReplica> ipcClient() { return Instance().m_ipcClient; }
|
||||||
|
|
||||||
static QSharedPointer<IpcProcessInterfaceReplica> createPrivilegedProcess();
|
static QSharedPointer<IpcProcessInterfaceReplica> createPrivilegedProcess();
|
||||||
|
|
||||||
static QSharedPointer<IpcInterfaceReplica> ipcClient() { return Instance().m_ipcClient; }
|
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -23,6 +24,7 @@ private:
|
||||||
|
|
||||||
QRemoteObjectNode m_ClientNode; // create remote object node
|
QRemoteObjectNode m_ClientNode; // create remote object node
|
||||||
QSharedPointer<IpcInterfaceReplica> m_ipcClient;
|
QSharedPointer<IpcInterfaceReplica> m_ipcClient;
|
||||||
|
QSharedPointer<QLocalSocket> m_localSocket;
|
||||||
|
|
||||||
//QMap<int, QSharedPointer<QRemoteObjectNode>> m_processNodes;
|
//QMap<int, QSharedPointer<QRemoteObjectNode>> m_processNodes;
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
#include <QTcpSocket>
|
#include <QTcpSocket>
|
||||||
|
|
||||||
#include "communicator.h"
|
//#include "communicator.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include "openvpnprotocol.h"
|
#include "openvpnprotocol.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
@ -166,29 +166,36 @@ ErrorCode OpenVpnProtocol::start()
|
||||||
|
|
||||||
setConnectionState(ConnectionState::Connecting);
|
setConnectionState(ConnectionState::Connecting);
|
||||||
|
|
||||||
QSharedPointer<IpcProcessInterfaceReplica> process = IpcClient::createPrivilegedProcess();
|
m_openVpnProcess = IpcClient::createPrivilegedProcess();
|
||||||
process->waitForSource(1000);
|
|
||||||
if (!process->isInitialized()) {
|
if (!m_openVpnProcess) {
|
||||||
|
qWarning() << "IpcProcess replica is not created!";
|
||||||
return ErrorCode::AmneziaServiceConnectionFailed;
|
return ErrorCode::AmneziaServiceConnectionFailed;
|
||||||
}
|
}
|
||||||
process->setProgram(openVpnExecPath());
|
|
||||||
|
m_openVpnProcess->waitForSource(1000);
|
||||||
|
if (!m_openVpnProcess->isInitialized()) {
|
||||||
|
qWarning() << "IpcProcess replica is not connected!";
|
||||||
|
return ErrorCode::AmneziaServiceConnectionFailed;
|
||||||
|
}
|
||||||
|
m_openVpnProcess->setProgram(openVpnExecPath());
|
||||||
QStringList arguments({"--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);
|
m_openVpnProcess->setArguments(arguments);
|
||||||
|
|
||||||
qDebug() << arguments.join(" ");
|
qDebug() << arguments.join(" ");
|
||||||
connect(process.data(), &IpcProcessInterfaceReplica::errorOccurred, [&](QProcess::ProcessError error) {
|
connect(m_openVpnProcess.data(), &IpcProcessInterfaceReplica::errorOccurred, [&](QProcess::ProcessError error) {
|
||||||
qDebug() << "IpcProcessInterfaceReplica errorOccurred" << error;
|
qDebug() << "IpcProcessInterfaceReplica errorOccurred" << error;
|
||||||
});
|
});
|
||||||
|
|
||||||
connect(process.data(), &IpcProcessInterfaceReplica::stateChanged, [&](QProcess::ProcessState newState) {
|
connect(m_openVpnProcess.data(), &IpcProcessInterfaceReplica::stateChanged, [&](QProcess::ProcessState newState) {
|
||||||
qDebug() << "IpcProcessInterfaceReplica stateChanged" << newState;
|
qDebug() << "IpcProcessInterfaceReplica stateChanged" << newState;
|
||||||
});
|
});
|
||||||
|
|
||||||
process->start();
|
m_openVpnProcess->start();
|
||||||
|
|
||||||
//m_communicator->sendMessage(Message(Message::State::StartRequest, args));
|
//m_communicator->sendMessage(Message(Message::State::StartRequest, args));
|
||||||
//startTimeoutTimer();
|
//startTimeoutTimer();
|
||||||
|
|
|
@ -53,6 +53,8 @@ protected:
|
||||||
private:
|
private:
|
||||||
void updateRouteGateway(QString line);
|
void updateRouteGateway(QString line);
|
||||||
void updateVpnGateway();
|
void updateVpnGateway();
|
||||||
|
|
||||||
|
QSharedPointer<IpcProcessInterfaceReplica> m_openVpnProcess;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // OPENVPNPROTOCOL_H
|
#endif // OPENVPNPROTOCOL_H
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#include "shadowsocksvpnprotocol.h"
|
#include "shadowsocksvpnprotocol.h"
|
||||||
#include "core/servercontroller.h"
|
#include "core/servercontroller.h"
|
||||||
|
|
||||||
#include "communicator.h"
|
//#include "communicator.h"
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
||||||
#include "communicator.h"
|
//#include "communicator.h"
|
||||||
#include "vpnprotocol.h"
|
#include "vpnprotocol.h"
|
||||||
#include "core/errorstrings.h"
|
#include "core/errorstrings.h"
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
||||||
#include "communicator.h"
|
//#include "communicator.h"
|
||||||
|
|
||||||
#include "core/errorstrings.h"
|
#include "core/errorstrings.h"
|
||||||
#include "core/openvpnconfigurator.h"
|
#include "core/openvpnconfigurator.h"
|
||||||
|
|
|
@ -10,10 +10,16 @@
|
||||||
#include "protocols/shadowsocksvpnprotocol.h"
|
#include "protocols/shadowsocksvpnprotocol.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "vpnconnection.h"
|
#include "vpnconnection.h"
|
||||||
#include "communicator.h"
|
//#include "communicator.h"
|
||||||
|
|
||||||
VpnConnection::VpnConnection(QObject* parent) : QObject(parent)
|
VpnConnection::VpnConnection(QObject* parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
|
QTimer::singleShot(0, [](){
|
||||||
|
if (!IpcClient::init()) {
|
||||||
|
qWarning() << "Error occured when init IPC client";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
//VpnProtocol::initializeCommunicator(parent);
|
//VpnProtocol::initializeCommunicator(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#include "protocols/vpnprotocol.h"
|
#include "protocols/vpnprotocol.h"
|
||||||
#include "core/defs.h"
|
#include "core/defs.h"
|
||||||
|
#include "core/ipcclient.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
|
||||||
using namespace amnezia;
|
using namespace amnezia;
|
||||||
|
|
20
ipc/ipc.h
20
ipc/ipc.h
|
@ -1,12 +1,30 @@
|
||||||
#ifndef IPC_H
|
#ifndef IPC_H
|
||||||
#define IPC_H
|
#define IPC_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
#define IPC_SERVICE_URL "local:AmneziaVpnIpcInterface"
|
#define IPC_SERVICE_URL "local:AmneziaVpnIpcInterface"
|
||||||
|
|
||||||
namespace amnezia {
|
namespace amnezia {
|
||||||
inline QString getIpcProcessUrl(int pid) { return QString("%1_%2").arg(IPC_SERVICE_URL).arg(pid); }
|
|
||||||
|
inline QString getIpcServiceUrl() {
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
|
return IPC_SERVICE_URL;
|
||||||
|
#else
|
||||||
|
return QString("/tmp/%1").arg(IPC_SERVICE_URL);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline QString getIpcProcessUrl(int pid) {
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
|
return QString("%1_%2").arg(IPC_SERVICE_URL).arg(pid);
|
||||||
|
#else
|
||||||
|
return QString("/tmp/%1_%2").arg(IPC_SERVICE_URL).arg(pid);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace amnezia
|
||||||
|
|
||||||
#endif // IPC_H
|
#endif // IPC_H
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include "ipcserver.h"
|
#include "ipcserver.h"
|
||||||
|
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
#include <QLocalSocket>
|
||||||
|
|
||||||
IpcServer::IpcServer(QObject *parent):
|
IpcServer::IpcServer(QObject *parent):
|
||||||
IpcInterfaceSource(parent)
|
IpcInterfaceSource(parent)
|
||||||
|
@ -11,8 +12,35 @@ int IpcServer::createPrivilegedProcess()
|
||||||
m_localpid++;
|
m_localpid++;
|
||||||
|
|
||||||
ProcessDescriptor pd;
|
ProcessDescriptor pd;
|
||||||
pd.serverNode->setHostUrl(QUrl(amnezia::getIpcProcessUrl(m_localpid)));
|
// pd.serverNode->setHostUrl(QUrl(amnezia::getIpcProcessUrl(m_localpid)));
|
||||||
pd.serverNode->enableRemoting(pd.ipcProcess.data());
|
// pd.serverNode->enableRemoting(pd.ipcProcess.data());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
pd.localServer = QSharedPointer<QLocalServer>(new QLocalServer(this));
|
||||||
|
pd.localServer->setSocketOptions(QLocalServer::WorldAccessOption);
|
||||||
|
|
||||||
|
if (!pd.localServer->listen(amnezia::getIpcProcessUrl(m_localpid))) {
|
||||||
|
qDebug() << QString("Unable to start the server: %1.").arg(pd.localServer->errorString());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// connect(m_server.data(), &QLocalServer::newConnection, this, &LocalServer::onNewConnection);
|
||||||
|
|
||||||
|
// qDebug().noquote() << QString("Local server started on '%1'").arg(m_server->serverName());
|
||||||
|
|
||||||
|
// m_serverNode.setHostUrl(QUrl(QStringLiteral(IPC_SERVICE_URL))); // create host node without Registry
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Make sure any connections are handed to QtRO
|
||||||
|
QObject::connect(pd.localServer.data(), &QLocalServer::newConnection, this, [pd]() {
|
||||||
|
qDebug() << "LocalServer new connection";
|
||||||
|
if (pd.serverNode) {
|
||||||
|
pd.serverNode->addHostSideConnection(pd.localServer->nextPendingConnection());
|
||||||
|
pd.serverNode->enableRemoting(pd.ipcProcess.data());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
m_processes.insert(m_localpid, pd);
|
m_processes.insert(m_localpid, pd);
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef IPCSERVER_H
|
#ifndef IPCSERVER_H
|
||||||
#define IPCSERVER_H
|
#define IPCSERVER_H
|
||||||
|
|
||||||
|
#include <QLocalServer>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
||||||
#include "ipc.h"
|
#include "ipc.h"
|
||||||
|
@ -21,9 +22,11 @@ private:
|
||||||
ProcessDescriptor (QObject *parent = nullptr) {
|
ProcessDescriptor (QObject *parent = nullptr) {
|
||||||
serverNode = QSharedPointer<QRemoteObjectHost>(new QRemoteObjectHost(parent));
|
serverNode = QSharedPointer<QRemoteObjectHost>(new QRemoteObjectHost(parent));
|
||||||
ipcProcess = QSharedPointer<IpcServerProcess>(new IpcServerProcess(parent));
|
ipcProcess = QSharedPointer<IpcServerProcess>(new IpcServerProcess(parent));
|
||||||
|
localServer = QSharedPointer<QLocalServer>(new QLocalServer(parent));
|
||||||
}
|
}
|
||||||
QSharedPointer<IpcServerProcess> ipcProcess;
|
QSharedPointer<IpcServerProcess> ipcProcess;
|
||||||
QSharedPointer<QRemoteObjectHost> serverNode;
|
QSharedPointer<QRemoteObjectHost> serverNode;
|
||||||
|
QSharedPointer<QLocalServer> localServer;
|
||||||
};
|
};
|
||||||
|
|
||||||
QMap<int, ProcessDescriptor> m_processes;
|
QMap<int, ProcessDescriptor> m_processes;
|
||||||
|
|
|
@ -18,20 +18,33 @@ LocalServer::LocalServer(QObject *parent) : QObject(parent),
|
||||||
// m_clientConnected(false),
|
// m_clientConnected(false),
|
||||||
m_ipcServer(this)
|
m_ipcServer(this)
|
||||||
{
|
{
|
||||||
// m_server = QSharedPointer<QLocalServer>(new QLocalServer(this));
|
// Create the server and listen outside of QtRO
|
||||||
// m_server->setSocketOptions(QLocalServer::WorldAccessOption);
|
m_server = QSharedPointer<QLocalServer>(new QLocalServer(this));
|
||||||
|
m_server->setSocketOptions(QLocalServer::WorldAccessOption);
|
||||||
|
|
||||||
// if (!m_server->listen(Utils::serverName())) {
|
if (!m_server->listen(amnezia::getIpcServiceUrl())) {
|
||||||
// qDebug() << QString("Unable to start the server: %1.").arg(m_server->errorString());
|
qDebug() << QString("Unable to start the server: %1.").arg(m_server->errorString());
|
||||||
// return;
|
return;
|
||||||
// }
|
}
|
||||||
|
|
||||||
// connect(m_server.data(), &QLocalServer::newConnection, this, &LocalServer::onNewConnection);
|
// connect(m_server.data(), &QLocalServer::newConnection, this, &LocalServer::onNewConnection);
|
||||||
|
|
||||||
// qDebug().noquote() << QString("Local server started on '%1'").arg(m_server->serverName());
|
// qDebug().noquote() << QString("Local server started on '%1'").arg(m_server->serverName());
|
||||||
|
|
||||||
m_serverNode.setHostUrl(QUrl(QStringLiteral(IPC_SERVICE_URL))); // create host node without Registry
|
// m_serverNode.setHostUrl(QUrl(QStringLiteral(IPC_SERVICE_URL))); // create host node without Registry
|
||||||
m_serverNode.enableRemoting(&m_ipcServer); // enable remoting/sharing
|
|
||||||
|
|
||||||
|
|
||||||
|
// Make sure any connections are handed to QtRO
|
||||||
|
QObject::connect(m_server.data(), &QLocalServer::newConnection, this, [this]() {
|
||||||
|
qDebug() << "LocalServer new connection";
|
||||||
|
m_serverNode.addHostSideConnection(m_server->nextPendingConnection());
|
||||||
|
|
||||||
|
if (!m_isRemotingEnabled) {
|
||||||
|
m_isRemotingEnabled = true;
|
||||||
|
m_serverNode.enableRemoting(&m_ipcServer);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
LocalServer::~LocalServer()
|
LocalServer::~LocalServer()
|
||||||
|
|
|
@ -50,6 +50,7 @@ private:
|
||||||
|
|
||||||
IpcServer m_ipcServer;
|
IpcServer m_ipcServer;
|
||||||
QRemoteObjectHost m_serverNode;
|
QRemoteObjectHost m_serverNode;
|
||||||
|
bool m_isRemotingEnabled = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // LOCALSERVER_H
|
#endif // LOCALSERVER_H
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue