Compare commits

...
Author SHA1 Message Date
yp afb447dde1 fix(windows): reconnect after the GUI dies without stopping the tunnel 2026-11-27 12:35:00 +03:00
3 changed files with 105 additions and 19 deletions

No files matched your search

+26 -16
View File
@@ -1,6 +1,7 @@
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QProcess>
#include <QRandomGenerator>
#include <QRegularExpression>
@@ -225,26 +226,35 @@ bool Utils::killProcessByName(const QString &name)
pe32.dwSize = sizeof(PROCESSENTRY32W);
bool success = false;
const QString baseName = QFileInfo(name).fileName();
if (Process32FirstW(hSnapshot, &pe32)) {
do {
QString exeFile = QString::fromWCharArray(pe32.szExeFile);
if (exeFile.compare(name, Qt::CaseInsensitive) == 0) {
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pe32.th32ProcessID);
if (hProcess != NULL) {
if (TerminateProcess(hProcess, 0)) {
success = true;
} else {
DWORD error = GetLastError();
qCritical() << "Can't terminate process" << exeFile << "(PID:" << pe32.th32ProcessID << "). Error:" << printErrorMessage(error);
}
CloseHandle(hProcess);
} else {
DWORD error = GetLastError();
qCritical() << "Can't open process for termination" << exeFile << "(PID:" << pe32.th32ProcessID << "). Error:" << printErrorMessage(error);
}
const QString exeFile = QString::fromWCharArray(pe32.szExeFile);
if (exeFile.compare(baseName, Qt::CaseInsensitive) != 0) {
continue;
}
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, FALSE, pe32.th32ProcessID);
if (hProcess == NULL) {
DWORD error = GetLastError();
qCritical() << "Can't open process for termination" << exeFile << "(PID:" << pe32.th32ProcessID << "). Error:" << printErrorMessage(error);
continue;
}
if (TerminateProcess(hProcess, 0)) {
if (WaitForSingleObject(hProcess, 5000) == WAIT_TIMEOUT) {
qCritical() << "Terminated process" << exeFile << "(PID:" << pe32.th32ProcessID << ") is still alive";
} else {
success = true;
qDebug().noquote() << "Killed process" << exeFile << "(PID:" << pe32.th32ProcessID << ")";
}
} else {
DWORD error = GetLastError();
qCritical() << "Can't terminate process" << exeFile << "(PID:" << pe32.th32ProcessID << "). Error:" << printErrorMessage(error);
}
CloseHandle(hProcess);
} while (Process32NextW(hSnapshot, &pe32));
}
+77 -2
View File
@@ -6,6 +6,7 @@
#include <QProcess>
#include <QtConcurrent>
#include <wincred.h>
#include <core/utils/networkUtilities.h>
@@ -309,8 +310,61 @@ void RouterWin::resetIpStack()
}
}
void RouterWin::logInterfaces(const QString &namePrefix)
{
PMIB_IF_TABLE2 ifTable = nullptr;
if (GetIfTable2(&ifTable) != NO_ERROR || ifTable == nullptr) {
qWarning() << "RouterWin::logInterfaces failed to get interface table";
return;
}
auto _guardIfTable = qScopeGuard([ifTable]() { FreeMibTable(ifTable); });
PMIB_UNICASTIPADDRESS_TABLE addrTable = nullptr;
const bool hasAddresses = GetUnicastIpAddressTable(AF_INET, &addrTable) == NO_ERROR && addrTable != nullptr;
auto _guardAddrTable = qScopeGuard([addrTable]() {
if (addrTable) FreeMibTable(addrTable);
});
int matched = 0;
for (ULONG i = 0; i < ifTable->NumEntries; i++) {
const MIB_IF_ROW2 &ifRow = ifTable->Table[i];
const QString alias = QString::fromWCharArray(ifRow.Alias);
if (!alias.startsWith(namePrefix, Qt::CaseInsensitive)) {
continue;
}
matched++;
QStringList addresses;
if (hasAddresses) {
for (ULONG j = 0; j < addrTable->NumEntries; j++) {
const MIB_UNICASTIPADDRESS_ROW &addrRow = addrTable->Table[j];
if (addrRow.InterfaceLuid.Value != ifRow.InterfaceLuid.Value) {
continue;
}
char ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &addrRow.Address.Ipv4.sin_addr, ip, INET_ADDRSTRLEN);
addresses << QString("%1/dad%2").arg(ip).arg(static_cast<int>(addrRow.DadState));
}
}
qDebug().noquote() << QString("[iface] \"%1\" index %2 luid %3 oper %4 media %5 addresses [%6]")
.arg(alias)
.arg(ifRow.InterfaceIndex)
.arg(ifRow.InterfaceLuid.Value)
.arg(static_cast<int>(ifRow.OperStatus))
.arg(static_cast<int>(ifRow.MediaConnectState))
.arg(addresses.join(", "));
}
if (matched == 0) {
qDebug().noquote() << QString("[iface] no interfaces with alias prefix \"%1\"").arg(namePrefix);
}
}
bool RouterWin::createTun(const QString &dev, const QString &subnet)
{
qDebug() << "RouterWin::createTun" << dev << subnet;
NET_LUID luid;
DWORD res = ConvertInterfaceAliasToLuid(reinterpret_cast<const wchar_t*>(dev.utf16()), &luid);
if (res != NO_ERROR) {
@@ -318,6 +372,10 @@ bool RouterWin::createTun(const QString &dev, const QString &subnet)
return false;
}
NET_IFINDEX resolvedIndex = 0;
ConvertInterfaceLuidToIndex(&luid, &resolvedIndex);
qDebug() << "RouterWin::createTun alias" << dev << "resolved to luid" << luid.Value << "index" << resolvedIndex;
HANDLE hEvent = CreateEvent(nullptr, true, false, nullptr);
if (!hEvent) {
qCritical() << "Failed to allocate event object";
@@ -366,14 +424,30 @@ bool RouterWin::createTun(const QString &dev, const QString &subnet)
row.DadState = IpDadStatePreferred;
res = CreateUnicastIpAddressEntry(&row);
if (res != NO_ERROR && res != ERROR_OBJECT_ALREADY_EXISTS) {
qDebug() << "Failed to create IP address:" << res;
if (res == ERROR_OBJECT_ALREADY_EXISTS) {
MIB_UNICASTIPADDRESS_ROW assignedRow;
InitializeUnicastIpAddressEntry(&assignedRow);
assignedRow.InterfaceLuid = luid;
assignedRow.Address = row.Address;
if (GetUnicastIpAddressEntry(&assignedRow) == NO_ERROR) {
qDebug() << "RouterWin::createTun" << subnet << "is already assigned to" << dev;
return true;
}
qCritical() << "Address" << subnet << "is held by another interface, not by" << dev;
logInterfaces(dev);
return false;
}
if (res != NO_ERROR) {
qCritical() << "Failed to create IP address:" << res;
return false;
}
res = WaitForSingleObject(hEvent, 10000);
if (res == WAIT_TIMEOUT) {
qCritical() << "Timeout of waiting for IP assignment for " << dev << " device";
logInterfaces(dev);
return false;
}
@@ -538,6 +612,7 @@ bool RouterWin::StopRoutingIpv6()
if (auto loopback = findLoopbackIface(); loopback.isValid()) {
QFuture<bool> res = QtConcurrent::mappedReduced(kIpv6Subnets, [loopback](const QString &subnet) -> bool {
QProcess::execute("netsh", { "interface", "ipv6", "delete", "route", subnet, QString("interface=%1").arg(loopback.index()) });
int res = QProcess::execute("netsh", { "interface", "ipv6", "add", "route", subnet, QString("interface=%1").arg(loopback.index()), "metric=0", "store=active" });
return res == 0;
},
+2 -1
View File
@@ -15,7 +15,7 @@
#include <WS2tcpip.h>
#include <iphlpapi.h>
#include <IcmpAPI.h>
// #include <IcmpAPI.h>
#include <stdio.h>
#include <stdlib.h>
@@ -63,6 +63,7 @@ private:
BOOL SuspendProcess(BOOL fSuspend, DWORD dwProcessId);
QNetworkInterface findLoopbackIface();
void logInterfaces(const QString &namePrefix);
private:
RouterWin() {m_dnsUtil = new DnsUtilsWindows(this);}