Files
amnezia-client/client/protocols/openvpnprotocol.cpp
T

352 lines
11 KiB
C++
Raw Normal View History

2020-12-26 15:03:51 +03:00
#include <QCoreApplication>
#include <QFileInfo>
2021-01-26 15:01:15 +03:00
#include <QProcess>
2022-08-29 02:58:23 +03:00
#include <QRandomGenerator>
2023-08-09 18:17:29 +05:00
#include <QTcpServer>
#include <QTcpSocket>
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 "openvpnprotocol.h"
2023-08-09 18:17:29 +05:00
#include "utilities.h"
#include "version.h"
2021-06-12 11:59:36 +03:00
2023-11-10 16:37:03 -05:00
2023-08-09 18:17:29 +05: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
}
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()
{
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 ???
2023-08-09 18:17:29 +05:00
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
killOpenVpnProcess();
2023-11-10 16:37:03 -05:00
QThread::msleep(10);
2020-12-26 15:03:51 +03:00
}
2023-08-09 18:17:29 +05:00
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
{
2021-02-18 15:00:41 +03:00
if (!IpcClient::Interface()) {
return ErrorCode::AmneziaServiceConnectionFailed;
}
QRemoteObjectPendingReply<QStringList> resultCheck = IpcClient::Interface()->getTapList();
resultCheck.waitForFinished();
2023-08-09 18:17:29 +05:00
if (resultCheck.returnValue().isEmpty()) {
2021-02-18 15:00:41 +03:00
QRemoteObjectPendingReply<bool> resultInstall = IpcClient::Interface()->checkAndInstallDriver();
resultInstall.waitForFinished();
2023-08-09 18:17:29 +05: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
{
2023-08-09 18:17:29 +05:00
if (m_openVpnProcess) {
2021-02-18 15:00:41 +03:00
m_openVpnProcess->close();
2020-12-26 15:03:51 +03:00
}
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();
2023-11-10 16:37:03 -05:00
QString plainConfig = jConfig.value(config_key::config).toString().toUtf8();
if (configuration.contains(ProtocolProps::key_proto_config_data(Proto::Cloak))) {
QJsonObject cloakConfig = configuration.value(ProtocolProps::key_proto_config_data(Proto::Cloak)).toObject();
cloakConfig["NumConn"] = 1;
cloakConfig["ProxyMethod"] = "openvpn";
2023-11-12 00:09:37 +02:00
if (cloakConfig.contains("port")) {
2023-11-10 16:37:03 -05:00
int portValue = cloakConfig.value("port").toInt();
cloakConfig.remove("port");
cloakConfig["RemotePort"] = portValue;
}
2023-11-12 00:09:37 +02:00
if (cloakConfig.contains("remote")) {
2023-11-10 16:37:03 -05:00
QString hostValue = cloakConfig.value("remote").toString();
cloakConfig.remove("remote");
cloakConfig["RemoteHost"] = hostValue;
}
plainConfig += "\n<cloak>\n";
QJsonDocument Doc(cloakConfig);
QByteArray ba = Doc.toJson();
QString plainCloak = ba;
plainConfig += QString::fromLatin1(plainCloak.toUtf8().toBase64().data());
plainConfig += "\n</cloak>\n";
}
2021-02-18 15:00:41 +03:00
m_configFile.open();
2023-11-10 16:37:03 -05:00
m_configFile.write(plainConfig.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
{
2023-11-10 16:37:03 -05:00
return Utils::processIsRunning("ovpncli");
2020-12-26 15:03:51 +03:00
}
void OpenVpnProtocol::disconnectFromManagementServer()
{
m_managementServer.stop();
}
QString OpenVpnProtocol::configPath() const
{
return m_configFileName;
}
2023-08-09 18:17:29 +05:00
void OpenVpnProtocol::sendManagementCommand(const QString &command)
2020-12-26 15:03:51 +03:00
{
2023-08-09 18:17:29 +05: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();
2023-08-09 18:17:29 +05:00
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);
2023-08-09 18:17:29 +05:00
if (ok)
return port;
2022-08-29 02:58:23 +03:00
}
return m_managementPort;
}
2021-01-26 15:01:15 +03:00
void OpenVpnProtocol::updateRouteGateway(QString line)
{
2023-11-10 16:37:03 -05:00
const QString substr = "sitnl_route_best_gw result: via ";
int start = line.indexOf(substr) + substr.size();
int end = line.indexOf(" dev ", start);
m_routeGateway = line.mid(start, (end-start));
}
void OpenVpnProtocol::handle_cli_message(QString message)
{
QString line = message;
if (line.isEmpty()) {
2023-08-09 18:17:29 +05:00
return;
2023-11-10 16:37:03 -05:00
}
if (line.contains("EVENT: CONNECTED")) {
setConnectionState(Vpn::ConnectionState::Connected);
} else if (line.contains("EXITING")) {
// openVpnStateSigTermHandler();
setConnectionState(Vpn::ConnectionState::Disconnecting);
} else if (line.contains("RECONNECTING")) {
setConnectionState(Vpn::ConnectionState::Reconnecting);
}
if (line.contains("sitnl_route_best_gw")) {
updateRouteGateway(line);
}
if (line.contains("[ifconfig]")) {
updateVpnGateway(line);
}
// TODO: SET CORRECT STRING
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);
}
return;
}
2021-01-26 15:01:15 +03:00
}
2023-11-10 16:37:03 -05:00
2021-01-06 17:12:24 +03:00
ErrorCode OpenVpnProtocol::start()
2020-12-26 15:03:51 +03:00
{
2023-08-09 18:17:29 +05:00
// qDebug() << "Start OpenVPN connection";
2021-01-15 23:36:35 +03:00
OpenVpnProtocol::stop();
2020-12-26 15:03:51 +03:00
2023-11-10 16:37:03 -05:00
qDebug() << " Utils::openVpnExecPath();" << Utils::openVpnExecPath();
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
}
2023-08-08 16:41:00 -07:00
// Detect default gateway
#ifdef Q_OS_MAC
QProcess p;
p.setProcessChannelMode(QProcess::MergedChannels);
2023-08-09 18:17:29 +05:00
p.start("route",
QStringList() << "-n"
<< "get"
<< "default");
2023-08-08 16:41:00 -07:00
p.waitForFinished();
2023-08-09 18:17:29 +05:00
QString s = p.readAll();
2023-08-08 16:41:00 -07:00
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;
2023-08-09 18:17:29 +05:00
} else {
2023-08-08 16:41:00 -07:00
qWarning() << "Unable to set VPN route gateway, output:\n" << s;
}
#endif
2023-05-14 21:11:19 +08:00
setConnectionState(Vpn::ConnectionState::Connecting);
2021-02-18 15:00:41 +03:00
m_openVpnProcess = IpcClient::CreatePrivilegedProcess();
2021-02-03 15:42:36 +03:00
if (!m_openVpnProcess) {
2023-11-10 16:37:03 -05:00
qWarning() << "IpcProcess replica is not created!";
2021-02-18 15:00:41 +03:00
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);
2023-11-10 16:37:03 -05:00
QStringList arguments({ configPath()/*, "--management", m_managementHost, QString::number(mgmtPort),
"--management-client" *//*, "--log", vpnLogFileNamePath */
2023-08-09 18:17:29 +05:00
});
2021-02-03 15:42:36 +03:00
m_openVpnProcess->setArguments(arguments);
2021-02-02 22:51:31 +03:00
qDebug() << arguments.join(" ");
2023-08-09 18:17:29 +05:00
connect(m_openVpnProcess.data(), &PrivilegedProcess::errorOccurred,
2023-11-10 16:37:03 -05:00
[&](QProcess::ProcessError error) {
qDebug() << "PrivilegedProcess errorOccurred" << error;
setConnectionState(Vpn::ConnectionState::Disconnected);
});
2021-02-02 22:51:31 +03:00
2023-11-10 16:37:03 -05:00
connect(m_openVpnProcess.data(), &PrivilegedProcess::stateChanged, [&](QProcess::ProcessState newState) {
switch ( newState )
{
case QProcess::Starting:
setConnectionState(Vpn::ConnectionState::Connecting);
break;
case QProcess::Running:
setConnectionState(Vpn::ConnectionState::Connecting);
break;
default:
setConnectionState(Vpn::ConnectionState::Disconnected);
}
qDebug() << "PrivilegedProcess stateChanged" << newState;
});
2021-02-02 22:51:31 +03:00
2023-08-09 18:17:29 +05:00
connect(m_openVpnProcess.data(), &PrivilegedProcess::finished, this,
[&]() { setConnectionState(Vpn::ConnectionState::Disconnected); });
2021-02-18 15:00:41 +03:00
2021-02-02 01:47:40 +03:00
2023-11-10 16:37:03 -05:00
connect(m_openVpnProcess.data(), &PrivilegedProcess::readyRead, this, [&] {
2020-12-26 15:03:51 +03:00
2023-11-10 16:37:03 -05:00
QRemoteObjectPendingReply<QByteArray> call = m_openVpnProcess->readAll();
auto *watcher = new QRemoteObjectPendingCallWatcher(call, this);
2020-12-26 15:03:51 +03:00
2023-11-10 16:37:03 -05:00
auto *timeoutTimer = new QTimer(this);
timeoutTimer->setSingleShot(true);
m_watchers.insert(watcher, timeoutTimer);
2020-12-26 15:03:51 +03:00
2023-11-10 16:37:03 -05:00
connect(timeoutTimer, &QTimer::timeout, this, [this, watcher, timeoutTimer]() {
qDebug() << "Foo request timed out.";
2020-12-26 15:03:51 +03:00
2023-11-10 16:37:03 -05:00
m_watchers.remove(watcher);
watcher->deleteLater();
timeoutTimer->deleteLater();
});
2020-12-26 15:03:51 +03:00
2023-11-10 16:37:03 -05:00
connect(watcher, &QRemoteObjectPendingCallWatcher::finished, [this](QRemoteObjectPendingCallWatcher *self) {
QTimer *timer = m_watchers.take(self);
if (timer) {
timer->stop();
timer->deleteLater();
}
2020-12-26 15:03:51 +03:00
2023-11-10 16:37:03 -05:00
QByteArray result = self->returnValue().toByteArray();
handle_cli_message(QString(result));
self->deleteLater();
});
2020-12-26 15:03:51 +03:00
2023-11-10 16:37:03 -05:00
timeoutTimer->start(30000);
2020-12-26 15:03:51 +03:00
2023-11-10 16:37:03 -05:00
});
2021-01-26 15:01:15 +03:00
2023-11-10 16:37:03 -05:00
connect(m_openVpnProcess.data(), QOverload<int, QProcess::ExitStatus>::of(&PrivilegedProcess::finished), this, [this](int exitCode, QProcess::ExitStatus exitStatus) {
qDebug().noquote() << "OpenVPN finished, exitCode, exiStatus" << exitCode << exitStatus;
setConnectionState(Vpn::ConnectionState::Disconnected);
if (exitStatus != QProcess::NormalExit) {
emit protocolError(amnezia::ErrorCode::ShadowSocksExecutableCrashed);
stop();
2021-02-21 09:44:53 -08:00
}
2023-11-10 16:37:03 -05:00
if (exitCode !=0 ) {
emit protocolError(amnezia::ErrorCode::InternalError);
stop();
2021-01-08 16:51:58 +03:00
}
2023-11-10 16:37:03 -05:00
});
2021-01-08 16:51:58 +03:00
2023-11-10 16:37:03 -05:00
m_openVpnProcess->start();
2020-12-26 15:03:51 +03:00
2023-11-10 16:37:03 -05:00
return ErrorCode::NoError;
2020-12-26 15:03:51 +03:00
}
2021-02-21 09:44:53 -08:00
void OpenVpnProtocol::updateVpnGateway(const QString &line)
2021-01-26 15:01:15 +03:00
{
2023-11-10 16:37:03 -05:00
// "[ifconfig] [10.8.0.14] [10.8.0.13]"
QStringList params = line.split("\n");
for (const QString &param : params) {
if (param.contains("ifconfig")) {
QString l = param.right(param.size() - param.indexOf("ifconfig"));
2021-02-21 09:44:53 -08:00
if (l.split(" ").size() == 3) {
2021-06-03 20:23:44 +03:00
m_vpnLocalAddress = l.split(" ").at(1);
2023-11-10 16:37:03 -05:00
m_vpnLocalAddress.remove("[");m_vpnLocalAddress.remove("]");
2021-02-21 09:44:53 -08:00
m_vpnGateway = l.split(" ").at(2);
2023-11-10 16:37:03 -05:00
m_vpnGateway.remove("[");m_vpnGateway.remove("]");
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
}
}
}