mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-12-04 22:21:41 +09:00
188 lines
5.3 KiB
C++
188 lines
5.3 KiB
C++
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#include "windowsutils.h"
|
|
|
|
#include <Windows.h>
|
|
#include <cfgmgr32.h>
|
|
#include <errhandlingapi.h>
|
|
|
|
#include <QScopeGuard>
|
|
#include <QSettings>
|
|
#include <QSysInfo>
|
|
#include <QVector>
|
|
|
|
#include "logger.h"
|
|
|
|
namespace {
|
|
Logger logger("WindowsUtils");
|
|
} // namespace
|
|
|
|
constexpr const int WINDOWS_11_BUILD =
|
|
22000; // Build Number of the first release win 11 iso
|
|
|
|
QString WindowsUtils::getErrorMessage(quint32 code) {
|
|
LPSTR messageBuffer = nullptr;
|
|
size_t size = FormatMessageA(
|
|
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
|
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
nullptr, code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
(LPSTR)&messageBuffer, 0, nullptr);
|
|
|
|
std::string message(messageBuffer, size);
|
|
QString result(message.c_str());
|
|
LocalFree(messageBuffer);
|
|
return result;
|
|
}
|
|
|
|
QString WindowsUtils::getErrorMessage() {
|
|
return getErrorMessage(GetLastError());
|
|
}
|
|
|
|
// A simple function to log windows error messages.
|
|
void WindowsUtils::windowsLog(const QString& msg) {
|
|
QString errmsg = getErrorMessage();
|
|
logger.error() << msg << "-" << errmsg;
|
|
}
|
|
|
|
// Static
|
|
QString WindowsUtils::windowsVersion() {
|
|
QSettings regCurrentVersion(
|
|
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion",
|
|
QSettings::NativeFormat);
|
|
|
|
int buildNr = regCurrentVersion.value("CurrentBuild").toInt();
|
|
if (buildNr >= WINDOWS_11_BUILD) {
|
|
return "11";
|
|
}
|
|
return QSysInfo::productVersion();
|
|
}
|
|
|
|
// static
|
|
void WindowsUtils::forceCrash() {
|
|
RaiseException(0x0000DEAD, EXCEPTION_NONCONTINUABLE, 0, NULL);
|
|
}
|
|
|
|
namespace {
|
|
|
|
QString serviceStateName(DWORD state) {
|
|
switch (state) {
|
|
case SERVICE_STOPPED:
|
|
return "stopped";
|
|
case SERVICE_START_PENDING:
|
|
return "start pending";
|
|
case SERVICE_STOP_PENDING:
|
|
return "stop pending";
|
|
case SERVICE_RUNNING:
|
|
return "running";
|
|
default:
|
|
return QString("state %1").arg(state);
|
|
}
|
|
}
|
|
|
|
QString serviceStartTypeName(DWORD startType) {
|
|
switch (startType) {
|
|
case SERVICE_AUTO_START:
|
|
return "auto";
|
|
case SERVICE_DEMAND_START:
|
|
return "demand";
|
|
case SERVICE_DISABLED:
|
|
return "disabled";
|
|
default:
|
|
return QString("start type %1").arg(startType);
|
|
}
|
|
}
|
|
|
|
void logServiceState(const QString& name) {
|
|
SC_HANDLE scm = OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT);
|
|
if (scm == nullptr) {
|
|
WindowsUtils::windowsLog("Failed to open SCManager");
|
|
return;
|
|
}
|
|
auto scmGuard = qScopeGuard([&] { CloseServiceHandle(scm); });
|
|
|
|
SC_HANDLE service = OpenService(scm, (const wchar_t*)name.utf16(),
|
|
SERVICE_QUERY_STATUS | SERVICE_QUERY_CONFIG);
|
|
if (service == nullptr) {
|
|
WindowsUtils::windowsLog("Failed to open the service " + name);
|
|
return;
|
|
}
|
|
auto serviceGuard = qScopeGuard([&] { CloseServiceHandle(service); });
|
|
|
|
SERVICE_STATUS status;
|
|
QString state = QueryServiceStatus(service, &status)
|
|
? serviceStateName(status.dwCurrentState)
|
|
: QString("unknown state");
|
|
|
|
DWORD required = 0;
|
|
QueryServiceConfig(service, nullptr, 0, &required);
|
|
QByteArray buffer(required, 0);
|
|
auto* config = reinterpret_cast<LPQUERY_SERVICE_CONFIG>(buffer.data());
|
|
QString startType =
|
|
config != nullptr && QueryServiceConfig(service, config, required, &required)
|
|
? serviceStartTypeName(config->dwStartType)
|
|
: QString("unknown start type");
|
|
|
|
logger.debug() << name << "is" << state << "with start type" << startType;
|
|
}
|
|
|
|
void logWintunDevices() {
|
|
const wchar_t* enumerator = L"SWD\\WINTUN";
|
|
|
|
ULONG length = 0;
|
|
if (CM_Get_Device_ID_List_SizeW(&length, enumerator,
|
|
CM_GETIDLIST_FILTER_ENUMERATOR) != CR_SUCCESS) {
|
|
logger.warning() << "Failed to size the Wintun device list";
|
|
return;
|
|
}
|
|
|
|
if (length == 0) {
|
|
logger.debug() << "No Wintun devices are registered";
|
|
return;
|
|
}
|
|
|
|
QVector<wchar_t> buffer(length, 0);
|
|
if (CM_Get_Device_ID_ListW(enumerator, buffer.data(), length,
|
|
CM_GETIDLIST_FILTER_ENUMERATOR) != CR_SUCCESS) {
|
|
logger.warning() << "Failed to enumerate the Wintun devices";
|
|
return;
|
|
}
|
|
|
|
bool found = false;
|
|
for (const wchar_t* id = buffer.constData(); *id != 0; id += wcslen(id) + 1) {
|
|
found = true;
|
|
const QString deviceId = QString::fromWCharArray(id);
|
|
|
|
DEVINST device = 0;
|
|
if (CM_Locate_DevNodeW(&device, const_cast<wchar_t*>(id),
|
|
CM_LOCATE_DEVNODE_PHANTOM) != CR_SUCCESS) {
|
|
logger.debug() << "Wintun device" << deviceId << "cannot be located";
|
|
continue;
|
|
}
|
|
|
|
ULONG status = 0;
|
|
ULONG problem = 0;
|
|
if (CM_Get_DevNode_Status(&status, &problem, device, 0) != CR_SUCCESS) {
|
|
logger.debug() << "Wintun device" << deviceId << "has no status";
|
|
continue;
|
|
}
|
|
|
|
logger.debug() << "Wintun device" << deviceId << "status"
|
|
<< QString::number(status, 16) << "problem code"
|
|
<< QString::number(problem, 16);
|
|
}
|
|
|
|
if (!found) {
|
|
logger.debug() << "No Wintun devices are registered";
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
// static
|
|
void WindowsUtils::logNetworkStackDiagnostics() {
|
|
logServiceState("NetSetupSvc");
|
|
logWintunDevices();
|
|
}
|