Files
DefaultVPN/client/protocols/openvpnprotocol.cpp

388 lines
13 KiB
C++
Raw Normal View History

2020-12-26 15:03:51 +03:00
#include <QCoreApplication>
#include <QFileInfo>
#include <QProcess>
2022-08-29 02:58:23 +03:00
#include <QRandomGenerator>
#include <QTcpServer>
#include <QTcpSocket>
#include <QNetworkInterface>
2020-12-26 15:03:51 +03:00
2024-09-20 04:12:22 -07:00
#include "core/networkUtilities.h"
#include "ipc.h"
2021-06-12 11:59:36 +03:00
#include "openvpnprotocol.h"
#include "utilities.h"
#include "version.h"
2021-06-12 11:59:36 +03:00
OpenVpnProtocol::OpenVpnProtocol(const QJsonObject &configuration, QObject *parent) : VpnProtocol(configuration, parent)
2020-12-26 15:03:51 +03:00
{
readOpenVpnConfiguration(configuration);
connect(&m_managementServer, &ManagementServer::readyRead, this,
&OpenVpnProtocol::onReadyReadDataFromManagementServer);
2020-12-26 15:03:51 +03:00
}
OpenVpnProtocol::~OpenVpnProtocol()
{
2021-01-15 23:36:35 +03:00
OpenVpnProtocol::stop();
QThread::msleep(200);
2020-12-26 15:03:51 +03:00
}
2021-06-12 11:59:36 +03:00
QString OpenVpnProtocol::defaultConfigFileName()
{
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()
{
qDebug() << "OpenVpnProtocol::stop()";
setConnectionState(Vpn::ConnectionState::Disconnecting);
2021-01-09 19:55:16 +03:00
// TODO: need refactoring
// sendTermSignal() will even return true while server connected ???
if ((m_connectionState == Vpn::ConnectionState::Preparing) || (m_connectionState == Vpn::ConnectionState::Connecting)
|| (m_connectionState == Vpn::ConnectionState::Connected)
|| (m_connectionState == Vpn::ConnectionState::Reconnecting)) {
2020-12-26 15:03:51 +03:00
if (!sendTermSignal()) {
killOpenVpnProcess();
}
QThread::msleep(10);
m_managementServer.stop();
2020-12-26 15:03:51 +03:00
}
2023-12-23 12:51:55 +02:00
#if defined(Q_OS_WIN) || defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
IpcClient::withInterface([](QSharedPointer<IpcInterfaceReplica> iface) {
QRemoteObjectPendingReply<bool> reply = iface->disableKillSwitch();
if (!reply.waitForFinished(1000) && !reply.returnValue()) {
qWarning() << "OpenVpnProtocol::stop(): Failed to disable killswitch";
}
});
#endif
setConnectionState(Vpn::ConnectionState::Disconnected);
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
{
return IpcClient::withInterface([](QSharedPointer<IpcInterfaceReplica> iface) {
QRemoteObjectPendingReply<QStringList> listReply = iface->getTapList();
if (!listReply.waitForFinished(1000)) {
return ErrorCode::InternalError;
}
QStringList list = listReply.returnValue();
if (list.empty()) {
QRemoteObjectPendingReply<bool> installReply = iface->checkAndInstallDriver();
if (!installReply.waitForFinished() || !installReply.returnValue()) {
return ErrorCode::OpenVpnTapAdapterError;
}
}
return ErrorCode::NoError;
}, [] () {
return ErrorCode::AmneziaServiceConnectionFailed;
});
2020-12-26 15:03:51 +03:00
}
void OpenVpnProtocol::killOpenVpnProcess()
2020-12-26 15:03:51 +03:00
{
if (m_openVpnProcess) {
m_openVpnProcess->close();
2020-12-26 15:03:51 +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))) {
m_configData = configuration;
2021-11-30 21:51:06 +03:00
QJsonObject jConfig = configuration.value(ProtocolProps::key_proto_config_data(Proto::OpenVpn)).toObject();
2021-10-05 12:22:13 +03:00
m_configFile.open();
2021-10-05 12:22:13 +03:00
m_configFile.write(jConfig.value(config_key::config).toString().toUtf8());
m_configFile.close();
m_configFileName = m_configFile.fileName();
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;
}
void OpenVpnProtocol::sendManagementCommand(const QString &command)
2020-12-26 15:03:51 +03:00
{
QIODevice *device = dynamic_cast<QIODevice *>(m_managementServer.socket().data());
2021-01-07 20:53:42 +03:00
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;
2022-08-29 02:58:23 +03:00
QTcpServer s;
bool ok = s.listen(QHostAddress::LocalHost, port);
if (ok)
return port;
2022-08-29 02:58:23 +03:00
}
return m_managementPort;
}
void OpenVpnProtocol::updateRouteGateway(QString line)
{
2024-01-24 17:20:50 -05:00
if (line.contains("net_route_v4_best_gw")) {
QStringList params = line.split(" ");
if (params.size() == 6) {
m_routeGateway = params.at(3);
}
} else {
line = line.split("ROUTE_GATEWAY", Qt::SkipEmptyParts).at(1);
if (!line.contains("/"))
return;
m_routeGateway = line.split("/", Qt::SkipEmptyParts).first();
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-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
}
#ifdef AMNEZIA_DESKTOP
const ErrorCode res = IpcClient::withInterface([&](QSharedPointer<IpcInterfaceReplica> iface) {
QString ip = NetworkUtilities::getIPAddress(m_configData.value(amnezia::config_key::hostName).toString());
QRemoteObjectPendingReply<bool> reply = iface->addKillSwitchAllowedRange(QStringList(ip));
if (!reply.waitForFinished(1000) || !reply.returnValue()) {
return ErrorCode::AmneziaServiceConnectionFailed;
}
return ErrorCode::NoError;
});
if (res != ErrorCode::NoError) {
return res;
}
feature: fillswitch strict mode (#1333) * Add allowed DNS list for killswitch * Windows killswitch strict mode backend part * Killswitch strict mode for Linux and MacOS * Windows fixes * feature: Add Kill Switch settings page with strict mode option * fix windows build after merge * Refresh killswitch mode when it toggled * Use HLM to store strictMode flag * Some Linux updates * feat: Enhance VerticalRadioButton with improved styling and disabled states * Refresh killSwitch state update * Fix build * refactor: Modularize header components * Change kill switch radio button styling * Fix strict kill switch mode handling * Refactor: Replace HeaderType with new Types for headers in QML pages * Remove deprecated HeaderType QML component * Refresh strict mode killswitch after global toggle change * Implement model, controller and UI for killswitch dns exceptions * Connect backend part and UI * Change label text to DNS exceptions * Remove HeaderType from PageSettingsApiDevices * Some pretty fixes * Fix problem with definition sequence of PageSettingsKillSwitchExceptions.pml elements * Add exclusion method for Windows firewall * Change ubuntu version in deploy script * Update ubuntu version in GH actions * Add confirmation popup for strict killswitch mode * Add qt standard path for build script * Add method to killswitch for expanding strickt mode exceptions list and fix allowTrafficTo() for Windows. Also Added cache in KillSwitch class for exceptions * Add insertion of gateway address to strict killswitch exceptions * Review fixes * buildfix and naming --------- Co-authored-by: aiamnezia <ai@amnezia.org>
2025-05-02 23:54:36 -07:00
#endif
// Detect default gateway
#ifdef Q_OS_MAC
QProcess p;
p.setProcessChannelMode(QProcess::MergedChannels);
p.start("route",
QStringList() << "-n"
<< "get"
<< "default");
p.waitForFinished();
QString s = p.readAll();
QRegularExpression rx(R"(gateway:\s*(\d+\.\d+\.\d+\.\d+))");
QRegularExpressionMatch match = rx.match(s);
if (match.hasMatch()) {
m_routeGateway = match.captured(1);
qDebug() << "Set VPN route gateway" << m_routeGateway;
} else {
qWarning() << "Unable to set VPN route gateway, output:\n" << s;
}
#endif
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
}
setConnectionState(Vpn::ConnectionState::Connecting);
2021-02-02 01:47:40 +03:00
m_openVpnProcess = IpcClient::CreatePrivilegedProcess();
2021-02-03 15:42:36 +03:00
if (!m_openVpnProcess) {
setLastError(ErrorCode::AmneziaServiceConnectionFailed);
2021-02-03 15:42:36 +03:00
return ErrorCode::AmneziaServiceConnectionFailed;
}
2022-08-10 22:15:00 +03:00
m_openVpnProcess->setProgram(PermittedProcess::OpenVPN);
QStringList arguments({
"--config", configPath(), "--management", m_managementHost, QString::number(mgmtPort),
"--management-client" /*, "--log", vpnLogFileNamePath */
});
2021-02-03 15:42:36 +03:00
m_openVpnProcess->setArguments(arguments);
2021-02-02 22:51:31 +03:00
qDebug() << arguments.join(" ");
connect(m_openVpnProcess.data(), &IpcProcessInterfaceReplica::errorOccurred,
[&](QProcess::ProcessError error) { qDebug() << "PrivilegedProcess errorOccurred" << error; });
2021-02-02 22:51:31 +03:00
connect(m_openVpnProcess.data(), &IpcProcessInterfaceReplica::stateChanged,
[&](QProcess::ProcessState newState) { qDebug() << "PrivilegedProcess stateChanged" << newState; });
2021-02-02 22:51:31 +03:00
connect(m_openVpnProcess.data(), &IpcProcessInterfaceReplica::finished, this,
[&]() { setConnectionState(Vpn::ConnectionState::Disconnected); });
2021-02-03 15:42:36 +03:00
m_openVpnProcess->start();
2021-02-02 01:47:40 +03:00
2021-01-06 17:12:24 +03:00
return ErrorCode::NoError;
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 (;;) {
2020-12-26 15:03:51 +03:00
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")) {
2020-12-26 15:03:51 +03:00
if (line.contains("CONNECTED,SUCCESS")) {
sendByteCount();
stopTimeoutTimer();
setConnectionState(Vpn::ConnectionState::Connected);
2020-12-26 15:03:51 +03:00
continue;
} else if (line.contains("EXITING,SIGTER")) {
// openVpnStateSigTermHandler();
setConnectionState(Vpn::ConnectionState::Disconnecting);
2020-12-26 15:03:51 +03:00
continue;
2021-01-09 19:55:16 +03:00
} else if (line.contains("RECONNECTING")) {
setConnectionState(Vpn::ConnectionState::Reconnecting);
2021-01-09 19:55:16 +03:00
continue;
2020-12-26 15:03:51 +03:00
}
}
2024-01-24 17:20:50 -05:00
if (line.contains("ROUTE_GATEWAY") || line.contains("net_route_v4_best_gw")) {
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 {
2021-01-08 16:51:58 +03:00
emit protocolError(ErrorCode::OpenVpnUnknownError);
}
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-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-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);
#ifdef Q_OS_WIN
QThread::msleep(300);
IpcClient::withInterface([&](QSharedPointer<IpcInterfaceReplica> iface) {
QList<QNetworkInterface> netInterfaces = QNetworkInterface::allInterfaces();
for (int i = 0; i < netInterfaces.size(); i++) {
for (int j=0; j < netInterfaces.at(i).addressEntries().size(); j++)
{
// killSwitch toggle
if (m_vpnLocalAddress == netInterfaces.at(i).addressEntries().at(j).ip().toString()) {
if (QVariant(m_configData.value(config_key::killSwitchOption).toString()).toBool()) {
iface->enableKillSwitch(m_configData, netInterfaces.at(i).index());
}
m_configData.insert("vpnAdapterIndex", netInterfaces.at(i).index());
m_configData.insert("vpnGateway", m_vpnGateway);
m_configData.insert("vpnServer",
NetworkUtilities::getIPAddress(m_configData.value(amnezia::config_key::hostName).toString()));
iface->enablePeerTraffic(m_configData);
}
}
}
});
2023-12-16 09:19:04 -05:00
#endif
2023-12-23 12:51:55 +02:00
#if defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
// killSwitch toggle
if (QVariant(m_configData.value(config_key::killSwitchOption).toString()).toBool()) {
2024-09-20 04:12:22 -07:00
m_configData.insert("vpnServer",
NetworkUtilities::getIPAddress(m_configData.value(amnezia::config_key::hostName).toString()));
IpcClient::withInterface([&](QSharedPointer<IpcInterfaceReplica> iface) {
QRemoteObjectPendingReply<bool> reply = iface->enableKillSwitch(m_configData, 0);
if (!reply.waitForFinished(1000) || !reply.returnValue()) {
qWarning() << "OpenVpnProtocol::updateVpnGateway(): Failed to enable killswitch";
}
});
}
#endif
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
}
}
}
}