From 4af5e154c6ef39756fb7d1b4adc90ee324f51d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=98=95?= Date: Mon, 25 Sep 2023 16:28:02 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20=E5=90=AF=E5=8A=A8ICS=E5=89=8D?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=E6=89=80=E6=9C=89=E7=8E=B0=E6=9C=89=E7=9A=84?= =?UTF-8?q?ICS=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NetTunnelSDK/UserManager.cpp | 30 +++++------- NetTunnelSDK/include/network.h | 12 +++++ NetTunnelSDK/network/network.cpp | 70 ++++++++++++++++++++++++++- TestNetTunnelSDK/TestNetTunnelSDK.cpp | 6 +++ 4 files changed, 100 insertions(+), 18 deletions(-) diff --git a/NetTunnelSDK/UserManager.cpp b/NetTunnelSDK/UserManager.cpp index 0d173da..c1c1b1f 100644 --- a/NetTunnelSDK/UserManager.cpp +++ b/NetTunnelSDK/UserManager.cpp @@ -117,20 +117,12 @@ int LocalWireGuardControl(bool isStart, bool setPrivateMode) { } } - // 检查 Internet 网络共享状态 - if ((ret = GetNetIntelnetConnectionSharing(ifInetlnetIndex, &chkStatus)) != ERR_SUCCESS) { - SPDLOG_ERROR(TEXT("Call GetNetIntelnetConnectionSharing error: {0}"), ret); + // 移除系统所有先前ICS共享 + if ((ret = RemoveAllIntelnetConnectSharing()) != ERR_SUCCESS) { + SPDLOG_ERROR(TEXT("Call RemoveAllIntelnetConnectSharing error: {0}"), ret); return ret; } - // 关闭 Intelnet 网络连接共享 - if (chkStatus) { - if ((ret = SetNetIntelnetConnectionSharing(ifInetlnetIndex, false, false)) != ERR_SUCCESS) { - SPDLOG_ERROR(TEXT("Call SetNetIntelnetConnectionSharing error: {0}"), ret); - return ret; - } - } - if (isStart) { // 启动服务 ret = WireGuardInstallDefaultServerService(true); @@ -174,7 +166,7 @@ int LocalWireGuardControl(bool isStart, bool setPrivateMode) { return -ERR_NET_WIREGUARD_ICS; } - // 检查 WireGuard 网络共享状态 + // 检查 WireGuard 网络共享状态 if ((ret = GetNetIntelnetConnectionSharing(ifInetlnetIndex, &chkStatus)) != ERR_SUCCESS) { SPDLOG_ERROR(TEXT("Call GetNetIntelnetConnectionSharing error: {0}"), ret); return ret; @@ -188,14 +180,18 @@ int LocalWireGuardControl(bool isStart, bool setPrivateMode) { 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) { + 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) { diff --git a/NetTunnelSDK/include/network.h b/NetTunnelSDK/include/network.h index a42399d..a6c9670 100644 --- a/NetTunnelSDK/include/network.h +++ b/NetTunnelSDK/include/network.h @@ -142,6 +142,18 @@ int SetNetIntelnetConnectionSharing(int ifIndex, bool isEnable, bool isSetPrivat * - ERR_SUCCESS 成功 */ int GetNetIntelnetConnectionSharing(int ifIndex, bool *pIsEnable); + +/** + * brief 删除系统所有ICS共享 + * return 函数执行结果 0: 成功, 小于0 失败 @see USER_ERRNO + * - -ERR_INPUT_PARAMS 输入参数错误 + * - -ERR_MEMORY_STR 字符串处理 + * - -ERR_CALL_SHELL 调用系统命令行失败 + * - -ERR_PROCESS_RETURN 系统调用执行结束返回失败 + * - ERR_SUCCESS 成功 + */ +int RemoveAllIntelnetConnectSharing(); + /** * @brief 设置网卡为 Private/Public 模式 * @param[in] pInterfaceName pInterfaceName 网卡名称 diff --git a/NetTunnelSDK/network/network.cpp b/NetTunnelSDK/network/network.cpp index 6fff3ba..ebb79cb 100644 --- a/NetTunnelSDK/network/network.cpp +++ b/NetTunnelSDK/network/network.cpp @@ -1008,7 +1008,7 @@ int SetNetIntelnetConnectionSharing(int ifIndex, bool isEnable, bool isSetPrivat } // δӵ - if (pNP->Status != NCS_CONNECTED) { + if (pNP->Status != NCS_CONNECTED && isEnable) { return -ERR_NET_UNCONNECT; } @@ -1055,6 +1055,74 @@ int SetNetIntelnetConnectionSharing(int ifIndex, bool isEnable, bool isSetPrivat return ERR_ITEM_UNEXISTS; } +/** + * brief ɾϵͳICS + * return ִн 0: ɹ С0 ʧ @see USER_ERRNO + * - -ERR_INPUT_PARAMS + * - -ERR_MEMORY_STR ַ + * - -ERR_CALL_SHELL ϵͳʧ + * - -ERR_PROCESS_RETURN ϵͳִнʧ + * - ERR_SUCCESS ɹ + */ +int RemoveAllIntelnetConnectSharing() { + bool chkStatus = false; + PIP_ADAPTER_INFO pAdapterInfo; + DWORD dwRetVal; + + ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO); + pAdapterInfo = static_cast(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(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) { + int ret; + PIP_ADAPTER_INFO pAdapter = pAdapterInfo; + while (pAdapter) { + // + int ifIndex = static_cast(pAdapter->Index); + + // Internet 繲״̬ + if ((ret = GetNetIntelnetConnectionSharing(ifIndex, &chkStatus)) == ERR_SUCCESS) { + if (chkStatus) { + // ر Intelnet ӹ + if ((ret = SetNetIntelnetConnectionSharing(ifIndex, false, false)) != ERR_SUCCESS) { + SPDLOG_ERROR(TEXT("Remove {0}-{1} ICS error: {2}"), ifIndex, pAdapter->AdapterName, ret); + } else { + SPDLOG_DEBUG(TEXT("Remove {0}-{1} ICS"), ifIndex, pAdapter->AdapterName); + } + } + + } else { + SPDLOG_ERROR(TEXT("Get {0}-{1} ICS status error: {2}"), ifIndex, pAdapter->AdapterName, ret); + } + + 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); + } + + return ERR_SUCCESS; +} + int GetNetIntelnetConnectionSharing(int ifIndex, bool *pIsEnable) { VARIANT v; INetConnection *pNC = nullptr; diff --git a/TestNetTunnelSDK/TestNetTunnelSDK.cpp b/TestNetTunnelSDK/TestNetTunnelSDK.cpp index 50e89b1..b885a51 100644 --- a/TestNetTunnelSDK/TestNetTunnelSDK.cpp +++ b/TestNetTunnelSDK/TestNetTunnelSDK.cpp @@ -64,6 +64,12 @@ public: } #if 0 + + TEST_METHOD(TestRemoveAllConnectSHaring) { + const int ret = RemoveAllConnectSHaring(); + Assert::AreEqual(RET_OK, ret); + } + TEST_METHOD(TestVerifyInterfaceIpAddr) { const int ret = VerifyInterfaceIpAddr(11, TEXT("10.10.10.251"), TEXT("255.255.255.0"), 2); Assert::AreEqual(RET_OK, ret);