2020-12-26 15:03:51 +03:00
|
|
|
#include <QCoreApplication>
|
|
|
|
|
#include <QFileInfo>
|
2021-01-26 15:01:15 +03:00
|
|
|
#include <QProcess>
|
2021-01-07 20:53:42 +03:00
|
|
|
#include <QTcpSocket>
|
2022-08-29 02:58:23 +03:00
|
|
|
#include <QTcpServer>
|
|
|
|
|
#include <QRandomGenerator>
|
2020-12-26 15:03:51 +03:00
|
|
|
|
2022-12-28 13:41:45 +03:00
|
|
|
#include "logger.h"
|
2021-06-12 11:59:36 +03:00
|
|
|
#include "defines.h"
|
2022-08-29 12:21:09 +04:30
|
|
|
#include "utilities.h"
|
2021-06-12 11:59:36 +03:00
|
|
|
#include "openvpnprotocol.h"
|
|
|
|
|
|
2020-12-26 15:03:51 +03:00
|
|
|
|
2021-02-18 15:00:41 +03:00
|
|
|
OpenVpnProtocol::OpenVpnProtocol(const QJsonObject &configuration, QObject* parent) :
|
|
|
|
|
VpnProtocol(configuration, parent)
|
2020-12-26 15:03:51 +03:00
|
|
|
{
|
2021-02-18 15:00:41 +03:00
|
|
|
readOpenVpnConfiguration(configuration);
|
2020-12-26 15:03:51 +03:00
|
|
|
connect(&m_managementServer, &ManagementServer::readyRead, this, &OpenVpnProtocol::onReadyReadDataFromManagementServer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OpenVpnProtocol::~OpenVpnProtocol()
|
|
|
|
|
{
|
2021-03-18 18:45:08 +03:00
|
|
|
qDebug() << "OpenVpnProtocol::~OpenVpnProtocol()";
|
2021-01-15 23:36:35 +03:00
|
|
|
OpenVpnProtocol::stop();
|
2021-03-18 18:45:08 +03:00
|
|
|
QThread::msleep(200);
|
2020-12-26 15:03:51 +03:00
|
|
|
}
|
|
|
|
|
|
2021-06-12 11:59:36 +03:00
|
|
|
QString OpenVpnProtocol::defaultConfigFileName()
|
|
|
|
|
{
|
2021-06-19 16:38:35 +03:00
|
|
|
//qDebug() << "OpenVpnProtocol::defaultConfigFileName" << defaultConfigPath() + QString("/%1.ovpn").arg(APPLICATION_NAME);
|
2021-06-12 11:59:36 +03:00
|
|
|
return defaultConfigPath() + QString("/%1.ovpn").arg(APPLICATION_NAME);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString OpenVpnProtocol::defaultConfigPath()
|
|
|
|
|
{
|
2021-06-19 16:38:35 +03:00
|
|
|
QString p = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/config";
|
|
|
|
|
Utils::initializePath(p);
|
|
|
|
|
|
|
|
|
|
return p;
|
2021-06-12 11:59:36 +03:00
|
|
|
}
|
|
|
|
|
|
2020-12-26 15:03:51 +03:00
|
|
|
void OpenVpnProtocol::stop()
|
|
|
|
|
{
|
2021-03-18 18:45:08 +03:00
|
|
|
qDebug() << "OpenVpnProtocol::stop()";
|
|
|
|
|
|
2021-01-09 19:55:16 +03:00
|
|
|
// TODO: need refactoring
|
2021-02-18 15:00:41 +03:00
|
|
|
// sendTermSignal() will even return true while server connected ???
|
2021-06-01 18:18:09 +03:00
|
|
|
if ((m_connectionState == VpnProtocol::Preparing) ||
|
|
|
|
|
(m_connectionState == VpnProtocol::Connecting) ||
|
|
|
|
|
(m_connectionState == VpnProtocol::Connected) ||
|
|
|
|
|
(m_connectionState == VpnProtocol::Reconnecting)) {
|
2020-12-26 15:03:51 +03:00
|
|
|
if (!sendTermSignal()) {
|
|
|
|
|
killOpenVpnProcess();
|
|
|
|
|
}
|
2021-03-18 18:45:08 +03:00
|
|
|
m_managementServer.stop();
|
|
|
|
|
qApp->processEvents();
|
2021-06-01 18:18:09 +03:00
|
|
|
setConnectionState(VpnProtocol::Disconnecting);
|
2020-12-26 15:03:51 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-04 19:07:49 +03:00
|
|
|
ErrorCode OpenVpnProtocol::prepare()
|
2020-12-26 15:03:51 +03:00
|
|
|
{
|
2021-02-18 15:00:41 +03:00
|
|
|
if (!IpcClient::Interface()) {
|
|
|
|
|
return ErrorCode::AmneziaServiceConnectionFailed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QRemoteObjectPendingReply<QStringList> resultCheck = IpcClient::Interface()->getTapList();
|
|
|
|
|
resultCheck.waitForFinished();
|
|
|
|
|
|
|
|
|
|
if (resultCheck.returnValue().isEmpty()){
|
|
|
|
|
QRemoteObjectPendingReply<bool> resultInstall = IpcClient::Interface()->checkAndInstallDriver();
|
|
|
|
|
resultInstall.waitForFinished();
|
|
|
|
|
|
2021-06-03 20:23:44 +03:00
|
|
|
if (!resultInstall.returnValue()) return ErrorCode::OpenVpnTapAdapterError;
|
2021-02-18 15:00:41 +03:00
|
|
|
}
|
|
|
|
|
return ErrorCode::NoError;
|
2020-12-26 15:03:51 +03:00
|
|
|
}
|
|
|
|
|
|
2021-02-18 15:00:41 +03:00
|
|
|
void OpenVpnProtocol::killOpenVpnProcess()
|
2020-12-26 15:03:51 +03:00
|
|
|
{
|
2021-09-15 08:03:28 -07:00
|
|
|
#ifndef Q_OS_IOS
|
2021-02-18 15:00:41 +03:00
|
|
|
if (m_openVpnProcess){
|
|
|
|
|
m_openVpnProcess->close();
|
2020-12-26 15:03:51 +03:00
|
|
|
}
|
2021-09-15 08:03:28 -07:00
|
|
|
#endif
|
2021-02-18 15:00:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenVpnProtocol::readOpenVpnConfiguration(const QJsonObject &configuration)
|
|
|
|
|
{
|
2021-11-30 21:51:06 +03:00
|
|
|
if (configuration.contains(ProtocolProps::key_proto_config_data(Proto::OpenVpn))) {
|
|
|
|
|
QJsonObject jConfig = configuration.value(ProtocolProps::key_proto_config_data(Proto::OpenVpn)).toObject();
|
2021-10-05 12:22:13 +03:00
|
|
|
|
2021-02-18 15:00:41 +03:00
|
|
|
m_configFile.open();
|
2021-10-05 12:22:13 +03:00
|
|
|
m_configFile.write(jConfig.value(config_key::config).toString().toUtf8());
|
2021-02-18 15:00:41 +03:00
|
|
|
m_configFile.close();
|
|
|
|
|
m_configFileName = m_configFile.fileName();
|
2020-12-26 15:03:51 +03:00
|
|
|
|
2021-02-18 15:00:41 +03:00
|
|
|
qDebug().noquote() << QString("Set config data") << m_configFileName;
|
2020-12-26 15:03:51 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool OpenVpnProtocol::openVpnProcessIsRunning() const
|
|
|
|
|
{
|
|
|
|
|
return Utils::processIsRunning("openvpn");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenVpnProtocol::disconnectFromManagementServer()
|
|
|
|
|
{
|
|
|
|
|
m_managementServer.stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString OpenVpnProtocol::configPath() const
|
|
|
|
|
{
|
|
|
|
|
return m_configFileName;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-18 15:00:41 +03:00
|
|
|
void OpenVpnProtocol::sendManagementCommand(const QString& command)
|
2020-12-26 15:03:51 +03:00
|
|
|
{
|
2021-01-07 20:53:42 +03:00
|
|
|
QIODevice *device = dynamic_cast<QIODevice*>(m_managementServer.socket().data());
|
|
|
|
|
if (device) {
|
|
|
|
|
QTextStream stream(device);
|
2021-02-21 09:44:53 -08:00
|
|
|
stream << command << Qt::endl;
|
2021-01-07 20:53:42 +03:00
|
|
|
}
|
2020-12-26 15:03:51 +03:00
|
|
|
}
|
|
|
|
|
|
2022-08-29 02:58:23 +03:00
|
|
|
uint OpenVpnProtocol::selectMgmtPort()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; ++i) {
|
|
|
|
|
quint32 port = QRandomGenerator::global()->generate();
|
|
|
|
|
port = (double)(65000-15001) * port / UINT32_MAX + 15001;
|
|
|
|
|
|
|
|
|
|
QTcpServer s;
|
|
|
|
|
bool ok = s.listen(QHostAddress::LocalHost, port);
|
|
|
|
|
if (ok) return port;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_managementPort;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-26 15:01:15 +03:00
|
|
|
void OpenVpnProtocol::updateRouteGateway(QString line)
|
|
|
|
|
{
|
2021-02-21 09:44:53 -08:00
|
|
|
// TODO: fix for macos
|
2022-08-29 12:21:09 +04:30
|
|
|
line = line.split("ROUTE_GATEWAY", Qt::SkipEmptyParts).at(1);
|
2021-01-26 15:01:15 +03:00
|
|
|
if (!line.contains("/")) return;
|
2022-08-29 12:21:09 +04:30
|
|
|
m_routeGateway = line.split("/", Qt::SkipEmptyParts).first();
|
2021-01-26 15:01:15 +03:00
|
|
|
m_routeGateway.replace(" ", "");
|
|
|
|
|
qDebug() << "Set VPN route gateway" << m_routeGateway;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-06 17:12:24 +03:00
|
|
|
ErrorCode OpenVpnProtocol::start()
|
2020-12-26 15:03:51 +03:00
|
|
|
{
|
2021-09-15 08:03:28 -07:00
|
|
|
#ifndef Q_OS_IOS
|
2021-02-18 15:00:41 +03:00
|
|
|
//qDebug() << "Start OpenVPN connection";
|
2021-01-15 23:36:35 +03:00
|
|
|
OpenVpnProtocol::stop();
|
2020-12-26 15:03:51 +03:00
|
|
|
|
2022-08-10 22:15:00 +03:00
|
|
|
if (!QFileInfo::exists(Utils::openVpnExecPath())) {
|
2021-01-06 17:12:24 +03:00
|
|
|
setLastError(ErrorCode::OpenVpnExecutableMissing);
|
|
|
|
|
return lastError();
|
2020-12-26 15:03:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!QFileInfo::exists(configPath())) {
|
2021-01-06 17:12:24 +03:00
|
|
|
setLastError(ErrorCode::OpenVpnConfigMissing);
|
|
|
|
|
return lastError();
|
2020-12-26 15:03:51 +03:00
|
|
|
}
|
|
|
|
|
|
2022-01-30 17:35:57 +03:00
|
|
|
// QString vpnLogFileNamePath = Utils::systemLogPath() + "/openvpn.log";
|
|
|
|
|
// Utils::createEmptyFile(vpnLogFileNamePath);
|
2020-12-26 15:03:51 +03:00
|
|
|
|
2022-08-29 02:58:23 +03:00
|
|
|
uint mgmtPort = selectMgmtPort();
|
|
|
|
|
qDebug() << "OpenVpnProtocol::start mgmt port selected:" << mgmtPort;
|
|
|
|
|
|
|
|
|
|
if (!m_managementServer.start(m_managementHost, mgmtPort)) {
|
2021-01-06 17:12:24 +03:00
|
|
|
setLastError(ErrorCode::OpenVpnManagementServerError);
|
|
|
|
|
return lastError();
|
2020-12-26 15:03:51 +03:00
|
|
|
}
|
|
|
|
|
|
2021-11-30 21:51:06 +03:00
|
|
|
setConnectionState(VpnConnectionState::Connecting);
|
2021-02-02 01:47:40 +03:00
|
|
|
|
2021-02-18 15:00:41 +03:00
|
|
|
m_openVpnProcess = IpcClient::CreatePrivilegedProcess();
|
2021-02-03 15:42:36 +03:00
|
|
|
|
|
|
|
|
if (!m_openVpnProcess) {
|
2021-02-18 15:00:41 +03:00
|
|
|
//qWarning() << "IpcProcess replica is not created!";
|
|
|
|
|
setLastError(ErrorCode::AmneziaServiceConnectionFailed);
|
2021-02-03 15:42:36 +03:00
|
|
|
return ErrorCode::AmneziaServiceConnectionFailed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_openVpnProcess->waitForSource(1000);
|
|
|
|
|
if (!m_openVpnProcess->isInitialized()) {
|
|
|
|
|
qWarning() << "IpcProcess replica is not connected!";
|
2021-02-18 15:00:41 +03:00
|
|
|
setLastError(ErrorCode::AmneziaServiceConnectionFailed);
|
2021-02-02 01:47:40 +03:00
|
|
|
return ErrorCode::AmneziaServiceConnectionFailed;
|
|
|
|
|
}
|
2022-08-10 22:15:00 +03:00
|
|
|
m_openVpnProcess->setProgram(PermittedProcess::OpenVPN);
|
2021-02-02 22:51:31 +03:00
|
|
|
QStringList arguments({"--config" , configPath(),
|
2022-08-29 02:58:23 +03:00
|
|
|
"--management", m_managementHost, QString::number(mgmtPort),
|
2022-01-30 17:35:57 +03:00
|
|
|
"--management-client"/*, "--log", vpnLogFileNamePath */
|
2021-02-02 22:51:31 +03:00
|
|
|
});
|
2021-02-03 15:42:36 +03:00
|
|
|
m_openVpnProcess->setArguments(arguments);
|
2021-02-02 22:51:31 +03:00
|
|
|
|
|
|
|
|
qDebug() << arguments.join(" ");
|
2021-11-19 19:02:39 +03:00
|
|
|
connect(m_openVpnProcess.data(), &PrivilegedProcess::errorOccurred, [&](QProcess::ProcessError error) {
|
|
|
|
|
qDebug() << "PrivilegedProcess errorOccurred" << error;
|
2021-02-02 22:51:31 +03:00
|
|
|
});
|
|
|
|
|
|
2021-11-19 19:02:39 +03:00
|
|
|
connect(m_openVpnProcess.data(), &PrivilegedProcess::stateChanged, [&](QProcess::ProcessState newState) {
|
|
|
|
|
qDebug() << "PrivilegedProcess stateChanged" << newState;
|
2021-02-02 22:51:31 +03:00
|
|
|
});
|
|
|
|
|
|
2021-11-19 19:02:39 +03:00
|
|
|
connect(m_openVpnProcess.data(), &PrivilegedProcess::finished, this, [&]() {
|
2021-11-30 21:51:06 +03:00
|
|
|
setConnectionState(VpnConnectionState::Disconnected);
|
2021-02-18 15:00:41 +03:00
|
|
|
});
|
|
|
|
|
|
2021-02-03 15:42:36 +03:00
|
|
|
m_openVpnProcess->start();
|
2021-02-02 01:47:40 +03:00
|
|
|
|
2021-02-02 22:51:31 +03:00
|
|
|
//startTimeoutTimer();
|
2020-12-26 15:03:51 +03:00
|
|
|
|
2021-01-06 17:12:24 +03:00
|
|
|
return ErrorCode::NoError;
|
2021-09-15 08:03:28 -07:00
|
|
|
#else
|
|
|
|
|
return ErrorCode::NotImplementedError;
|
|
|
|
|
#endif
|
2020-12-26 15:03:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool OpenVpnProtocol::sendTermSignal()
|
|
|
|
|
{
|
|
|
|
|
return m_managementServer.writeCommand("signal SIGTERM");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenVpnProtocol::sendByteCount()
|
|
|
|
|
{
|
|
|
|
|
m_managementServer.writeCommand("bytecount 1");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenVpnProtocol::sendInitialData()
|
|
|
|
|
{
|
|
|
|
|
m_managementServer.writeCommand("state on");
|
|
|
|
|
m_managementServer.writeCommand("log on");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenVpnProtocol::onReadyReadDataFromManagementServer()
|
|
|
|
|
{
|
|
|
|
|
for (;;) {
|
|
|
|
|
QString line = m_managementServer.readLine().simplified();
|
|
|
|
|
|
|
|
|
|
if (line.isEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!line.contains(">BYTECOUNT")) {
|
|
|
|
|
qDebug().noquote() << line;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (line.contains(">INFO:OpenVPN Management Interface")) {
|
|
|
|
|
sendInitialData();
|
|
|
|
|
} else if (line.startsWith(">STATE")) {
|
|
|
|
|
if (line.contains("CONNECTED,SUCCESS")) {
|
|
|
|
|
sendByteCount();
|
|
|
|
|
stopTimeoutTimer();
|
2021-06-01 18:18:09 +03:00
|
|
|
setConnectionState(VpnProtocol::Connected);
|
2020-12-26 15:03:51 +03:00
|
|
|
continue;
|
|
|
|
|
} else if (line.contains("EXITING,SIGTER")) {
|
2021-02-18 15:00:41 +03:00
|
|
|
//openVpnStateSigTermHandler();
|
2021-06-01 18:18:09 +03:00
|
|
|
setConnectionState(VpnProtocol::Disconnecting);
|
2020-12-26 15:03:51 +03:00
|
|
|
continue;
|
2021-01-09 19:55:16 +03:00
|
|
|
} else if (line.contains("RECONNECTING")) {
|
2021-06-01 18:18:09 +03:00
|
|
|
setConnectionState(VpnProtocol::Reconnecting);
|
2021-01-09 19:55:16 +03:00
|
|
|
continue;
|
2020-12-26 15:03:51 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-26 15:01:15 +03:00
|
|
|
if (line.contains("ROUTE_GATEWAY")) {
|
|
|
|
|
updateRouteGateway(line);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-21 09:44:53 -08:00
|
|
|
if (line.contains("PUSH: Received control message")) {
|
|
|
|
|
updateVpnGateway(line);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-08 16:51:58 +03:00
|
|
|
if (line.contains("FATAL")) {
|
|
|
|
|
if (line.contains("tap-windows6 adapters on this system are currently in use or disabled")) {
|
|
|
|
|
emit protocolError(ErrorCode::OpenVpnAdaptersInUseError);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
emit protocolError(ErrorCode::OpenVpnUnknownError);
|
|
|
|
|
}
|
2021-01-10 18:47:29 +03:00
|
|
|
return;
|
2021-01-08 16:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
2020-12-26 15:03:51 +03:00
|
|
|
QByteArray data(line.toStdString().c_str());
|
|
|
|
|
if (data.contains(">BYTECOUNT:")) {
|
|
|
|
|
int beg = data.lastIndexOf(">BYTECOUNT:");
|
|
|
|
|
int end = data.indexOf("\n", beg);
|
|
|
|
|
|
|
|
|
|
beg += sizeof(">BYTECOUNT:") - 1;
|
|
|
|
|
QList<QByteArray> count = data.mid(beg, end - beg + 1).split(',');
|
|
|
|
|
|
|
|
|
|
quint64 r = static_cast<quint64>(count.at(0).trimmed().toULongLong());
|
|
|
|
|
quint64 s = static_cast<quint64>(count.at(1).trimmed().toULongLong());
|
|
|
|
|
|
|
|
|
|
setBytesChanged(r, s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-21 09:44:53 -08:00
|
|
|
void OpenVpnProtocol::updateVpnGateway(const QString &line)
|
2021-01-26 15:01:15 +03:00
|
|
|
{
|
2021-02-21 09:44:53 -08:00
|
|
|
// line looks like
|
|
|
|
|
// PUSH: Received control message: 'PUSH_REPLY,route 10.8.0.1,topology net30,ping 10,ping-restart 120,ifconfig 10.8.0.6 10.8.0.5,peer-id 0,cipher AES-256-GCM'
|
2021-01-26 15:01:15 +03:00
|
|
|
|
2021-02-21 09:44:53 -08:00
|
|
|
QStringList params = line.split(",");
|
|
|
|
|
for (const QString &l : params) {
|
|
|
|
|
if (l.contains("ifconfig")) {
|
|
|
|
|
if (l.split(" ").size() == 3) {
|
2021-06-03 20:23:44 +03:00
|
|
|
m_vpnLocalAddress = l.split(" ").at(1);
|
2021-02-21 09:44:53 -08:00
|
|
|
m_vpnGateway = l.split(" ").at(2);
|
2021-01-26 15:01:15 +03:00
|
|
|
|
2021-06-03 20:23:44 +03:00
|
|
|
qDebug() << QString("Set vpn local address %1, gw %2").arg(m_vpnLocalAddress).arg(vpnGateway());
|
2021-02-21 09:44:53 -08:00
|
|
|
}
|
2021-01-26 15:01:15 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|