diff --git a/client/protocols/openvpnprotocol.cpp b/client/protocols/openvpnprotocol.cpp index a62b599c..cfd13a7e 100644 --- a/client/protocols/openvpnprotocol.cpp +++ b/client/protocols/openvpnprotocol.cpp @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include "debug.h" #include "defines.h" @@ -121,6 +123,21 @@ void OpenVpnProtocol::sendManagementCommand(const QString& command) } } +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; +} + void OpenVpnProtocol::updateRouteGateway(QString line) { // TODO: fix for macos @@ -150,7 +167,10 @@ ErrorCode OpenVpnProtocol::start() // QString vpnLogFileNamePath = Utils::systemLogPath() + "/openvpn.log"; // Utils::createEmptyFile(vpnLogFileNamePath); - if (!m_managementServer.start(m_managementHost, m_managementPort)) { + uint mgmtPort = selectMgmtPort(); + qDebug() << "OpenVpnProtocol::start mgmt port selected:" << mgmtPort; + + if (!m_managementServer.start(m_managementHost, mgmtPort)) { setLastError(ErrorCode::OpenVpnManagementServerError); return lastError(); } @@ -173,7 +193,7 @@ ErrorCode OpenVpnProtocol::start() } m_openVpnProcess->setProgram(PermittedProcess::OpenVPN); QStringList arguments({"--config" , configPath(), - "--management", m_managementHost, QString::number(m_managementPort), + "--management", m_managementHost, QString::number(mgmtPort), "--management-client"/*, "--log", vpnLogFileNamePath */ }); m_openVpnProcess->setArguments(arguments); diff --git a/client/protocols/openvpnprotocol.h b/client/protocols/openvpnprotocol.h index 1f3bbd40..ad80fe50 100644 --- a/client/protocols/openvpnprotocol.h +++ b/client/protocols/openvpnprotocol.h @@ -46,6 +46,8 @@ private: QString m_configFileName; QTemporaryFile m_configFile; + uint selectMgmtPort(); + private: void updateRouteGateway(QString line); void updateVpnGateway(const QString &line);