OCT 1. 检查ICS创建是,windows操作系统是否自动对隧道网卡设置了不合理的IP地址。

This commit is contained in:
黄昕 2023-09-22 10:47:52 +08:00
parent f0ee6909e0
commit 61ba441f3e
4 changed files with 97 additions and 8 deletions

View File

@ -185,12 +185,17 @@ int LocalWireGuardControl(bool isStart, bool setPrivateMode) {
return -ERR_NET_WIREGUARD_ICS;
}
// 重设隧道 IP 地址
GetIpV4InfoFromCIDR(GetGlobalCfgInfo()->userCfg.cliConfig.cliAddress, &ipInfo);
// 检查 ICS 是否自动添加了其它非法 IP 地址
if((ret = VerifyInterfaceIpAddr(ifWireGuardIndex, ipInfo.ip, ipInfo.netmask, 1)) != ERR_SUCCESS) {
SPDLOG_WARN(TEXT("Check Windows automatic set another ip to wireguard network interface, force set to {0}/{1}"), ipInfo.ip, ipInfo.netmask);
// 重设隧道 IP 地址
if ((ret = SetInterfaceIpAddressWMI(GetGlobalCfgInfo()->userCfg.userName, ipInfo.ip, ipInfo.netmask)) != ERR_SUCCESS) {
SPDLOG_ERROR(TEXT("Call SetInterfaceIpAddressWMI error: {0}"), ret);
return ret;
}
}
SPDLOG_INFO(TEXT("Net Share Service Work now on ICS mode: {0}"), GetGlobalCfgInfo()->userCfg.userName);
} else if (GetCurrentNetShareMode() == NAT_SHARE_MODE) {

View File

@ -7,6 +7,21 @@ extern "C" {
// we need to export the C interface
#endif
/**
* @brief IP
* @param ifIndex
* @param pIpAddr IP
* @param pNetMask
* @param maxIpNumber IP
* @return 0: 0 @see USER_ERRNO
* - -ERR_INPUT_PARAMS
* - -ERR_MALLOC_MEMORY
* - -ERR_ITEM_UNEXISTS
* - -ERR_UN_SUPPORT
* - ERR_SUCCESS
*/
int VerifyInterfaceIpAddr(int ifIndex, const TCHAR *pIpAddr, const TCHAR *pNetMask, int maxIpNumber);
/**
* @brief IP地址
* @param[in] pIpAddr IP地址

View File

@ -26,6 +26,70 @@
static NIC_CONTENT g_NetAdapterInfo[NET_CARD_MAX];
int VerifyInterfaceIpAddr(int ifIndex, const TCHAR *pIpAddr, const TCHAR *pNetMask, int maxIpNumber) {
PIP_ADAPTER_INFO pAdapterInfo;
DWORD dwRetVal;
ULONG ulOutBufLen;
int nIp = 0;
bool isMatched = false;
if (pIpAddr == nullptr || lstrlen(pIpAddr) == 0) {
SPDLOG_ERROR(TEXT("Input pIpAddr error: {0}"), pIpAddr);
return -ERR_INPUT_PARAMS;
}
ulOutBufLen = sizeof(IP_ADAPTER_INFO);
pAdapterInfo = static_cast<IP_ADAPTER_INFO *>(HeapAlloc(GetProcessHeap(), 0, sizeof(IP_ADAPTER_INFO)));
if (pAdapterInfo == nullptr) {
SPDLOG_ERROR(TEXT("Error allocating memory needed to call GetAdaptersinfo"));
return -ERR_MALLOC_MEMORY;
}
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
pAdapterInfo = static_cast<IP_ADAPTER_INFO *>(HeapAlloc(GetProcessHeap(), 0, ulOutBufLen));
if (pAdapterInfo == nullptr) {
SPDLOG_ERROR(TEXT("Error allocating memory needed to call GetAdaptersinfo\n"));
return -ERR_MALLOC_MEMORY;
}
}
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
while (pAdapter) {
PIP_ADDR_STRING ipAddressListPointer = &(pAdapter->IpAddressList);
while (ipAddressListPointer != nullptr) {
if (pAdapter->Index == static_cast<ULONG>(ifIndex)) {
nIp++;
if (StrCmp((ipAddressListPointer->IpAddress).String, pIpAddr) == 0 &&
StrCmp((ipAddressListPointer->IpMask).String, pNetMask) == 0) {
isMatched = true;
}
}
ipAddressListPointer = ipAddressListPointer->Next;
}
pAdapter = pAdapter->Next;
}
} else {
SPDLOG_ERROR(TEXT("GetAdaptersInfo failed with error: {0}\n"), dwRetVal);
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
return -ERR_SYS_CALL;
}
if (pAdapterInfo) {
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
}
if (isMatched == true && nIp == maxIpNumber) {
return ERR_SUCCESS;
}
return -ERR_ITEM_UNEXISTS;
}
int GetInterfaceIfIndexByIpAddr(const TCHAR *pIpAddr, ULONG *pIfIndex) {
PIP_ADAPTER_INFO pAdapterInfo;
DWORD dwRetVal;

View File

@ -55,7 +55,7 @@ public:
Assert::IsNotNull(pInfo);
Assert::AreNotEqual(0, size);
free(pInfo);
//free(pInfo);
}
TEST_METHOD(TestSetInterfaceIpAddressWMI) {
@ -64,6 +64,11 @@ public:
}
#if 0
TEST_METHOD(TestVerifyInterfaceIpAddr) {
const int ret = VerifyInterfaceIpAddr(11, TEXT("10.10.10.251"), TEXT("255.255.255.0"), 2);
Assert::AreEqual(RET_OK, ret);
}
TEST_METHOD(TestSetInterfaceIpAddressFromCIDR) {
Assert::AreEqual(RET_OK, SetInterfaceIpAddressFromCIDR(TEXT("wg_server"), TEXT("192.168.100.250/24")));
}