diff options
| -rw-r--r-- | dist-assets/windows/installer.nsh | 42 | ||||
| -rw-r--r-- | windows/driverlogic/src/driverlogic.cpp | 32 |
2 files changed, 65 insertions, 9 deletions
diff --git a/dist-assets/windows/installer.nsh b/dist-assets/windows/installer.nsh index 34c5c21fa4..3367c2425c 100644 --- a/dist-assets/windows/installer.nsh +++ b/dist-assets/windows/installer.nsh @@ -51,6 +51,7 @@ !define BLOCK_OUTBOUND_IPV4_FILTER_GUID "{a81c5411-0fd0-43a9-a9be-313f299de64f}" !define PERSISTENT_BLOCK_OUTBOUND_IPV4_FILTER_GUID "{79860c64-9a5e-48a3-b5f3-d64b41659aa5}" +!define WINTUN_ADAPTER_GUID "{AFE43773-E1F8-4EBB-8536-576AB86AFE9A}" # # ExtractWintun @@ -194,6 +195,43 @@ !define RemoveWintun '!insertmacro "RemoveWintun"' # +# RemoveAbandonedWintunAdapter +# +# Removes old Wintun interface, even if it belongs to a different pool. +# +!macro RemoveAbandonedWintunAdapter + Push $0 + Push $1 + + log::Log "RemoveAbandonedWintunAdapter()" + + nsExec::ExecToStack '"$TEMP\driverlogic.exe" remove-device-by-guid ${WINTUN_ADAPTER_GUID}' + Pop $0 + Pop $1 + + ${If} $0 != ${DL_GENERAL_SUCCESS} + ${AndIf} $0 != ${DL_ADAPTER_NOT_FOUND} + IntFmt $0 "0x%X" $0 + StrCpy $R0 "Failed to remove network adapter: error $0" + log::LogWithDetails $R0 $1 + Goto RemoveAbandonedWintunAdapter_return_only + ${EndIf} + + log::Log "RemoveAbandonedWintunAdapter() completed successfully" + + Push 0 + Pop $R0 + + RemoveAbandonedWintunAdapter_return_only: + + Pop $1 + Pop $0 + +!macroend + +!define RemoveAbandonedWintunAdapter '!insertmacro "RemoveAbandonedWintunAdapter"' + +# # InstallService # # Register the service with Windows and start it @@ -679,6 +717,10 @@ ${RemoveRelayCache} ${RemoveApiAddressCache} + SetOutPath "$TEMP" + File "${BUILD_RESOURCES_DIR}\..\windows\driverlogic\bin\x64-Release\driverlogic.exe" + ${RemoveAbandonedWintunAdapter} + ${If} ${AtMostWin7} ${InstallWin7Hotfix} ${If} $R0 != 0 diff --git a/windows/driverlogic/src/driverlogic.cpp b/windows/driverlogic/src/driverlogic.cpp index acc601811b..f849c828f2 100644 --- a/windows/driverlogic/src/driverlogic.cpp +++ b/windows/driverlogic/src/driverlogic.cpp @@ -759,7 +759,7 @@ std::optional<NetworkAdapter> FindAdapterByAlias(const std::set<NetworkAdapter> return std::nullopt; } -bool RemoveNetDevice(const std::wstring &tapHardwareId, const std::wstring &guid) +bool RemoveNetDevice(const std::optional<std::wstring> tapHardwareId, const std::wstring &guid) { bool deletedAdapter = false; @@ -806,7 +806,7 @@ void RemoveNetAdapterByAlias(const std::wstring &hardwareId, const std::wstring // and delete any adapter whose GUID matches that of the "Mullvad" adapter. // - if (!RemoveNetDevice(hardwareId, guid)) + if (!RemoveNetDevice(std::make_optional(hardwareId), guid)) { THROW_ERROR("The virtual adapter could not be removed"); } @@ -818,24 +818,24 @@ std::filesystem::path GetCurrentModulePath() SetLastError(ERROR_SUCCESS); - size_t nextCapacity = 256; + size_t nextCapacity = MAX_PATH; + DWORD writtenChars = 0; do { - pathBuffer.reserve(nextCapacity); - - const auto writtenChars = GetModuleFileNameW(nullptr, &pathBuffer[0], static_cast<DWORD>(pathBuffer.capacity())); + pathBuffer.resize(nextCapacity); + writtenChars = GetModuleFileNameW(nullptr, &pathBuffer[0], static_cast<DWORD>(pathBuffer.size())); if (0 == writtenChars) { THROW_WINDOWS_ERROR(GetLastError(), "GetModuleFileNameW"); } - pathBuffer.resize(writtenChars); - - nextCapacity = 2 * pathBuffer.capacity(); + nextCapacity = 2 * pathBuffer.size(); } while (ERROR_INSUFFICIENT_BUFFER == GetLastError()); + pathBuffer.resize(writtenChars); + return std::filesystem::path(pathBuffer.begin(), pathBuffer.end()); } @@ -1038,6 +1038,20 @@ int wmain(int argc, const wchar_t * argv[], const wchar_t * []) RemoveNetAdapterByAlias(hardwareId, baseName); } + else if (0 == _wcsicmp(argv[1], L"remove-device-by-guid")) + { + if (3 != argc) + { + goto INVALID_ARGUMENTS; + } + + const wchar_t *guid = argv[2]; + + if (!RemoveNetDevice(std::nullopt, guid)) + { + return ADAPTER_NOT_FOUND; + } + } else if (0 == _wcsicmp(argv[1], L"device-exists")) { if (4 != argc) |
