From fb916754179ec5b9d880238ec86923ad951a3b39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=98=95?= Date: Mon, 17 Jul 2023 19:04:02 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20=20=E9=9A=A7=E9=81=93=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E3=80=81=E5=81=9C=E6=AD=A2=E3=80=81=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E7=BD=91=E7=BB=9C=E7=B1=BB=E5=9E=8B=EF=BC=88=E5=85=AC=E5=85=B1?= =?UTF-8?q?/=E4=B8=93=E7=94=A8=EF=BC=89=E7=9B=B8=E5=85=B3=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NetTunnelSDK/UserManager.cpp | 17 +++-- NetTunnelSDK/network.cpp | 133 +++++++---------------------------- 2 files changed, 32 insertions(+), 118 deletions(-) diff --git a/NetTunnelSDK/UserManager.cpp b/NetTunnelSDK/UserManager.cpp index ba1f791..c03f0f2 100644 --- a/NetTunnelSDK/UserManager.cpp +++ b/NetTunnelSDK/UserManager.cpp @@ -122,16 +122,15 @@ int LocalWireGuardControl(bool isStart, bool setPrivateMode) { SPDLOG_ERROR(TEXT("Call GetNetConnectionNetworkCategory error: {0}"), ret); return ret; } -#if 0 - // 检查网卡模式是否和设定值一致 - if (setPrivateMode != chkStatus) { - if ((ret = SetNetConnectionNetworkCategory(GetGlobalCfgInfo()->userCfg.userName, setPrivateMode)) != - ERR_SUCCESS) { - SPDLOG_ERROR(TEXT("Call SetNetConnectionNetworkCategory error: {0}"), ret); - return ret; - } + + // 检查网卡模式是否和设定值一致 + if (setPrivateMode != chkStatus) { + if ((ret = SetNetConnectionNetworkCategory(GetGlobalCfgInfo()->userCfg.userName, setPrivateMode)) != + ERR_SUCCESS) { + SPDLOG_ERROR(TEXT("Call SetNetConnectionNetworkCategory error: {0}"), ret); + return ret; } -#endif + } } return ERR_SUCCESS; diff --git a/NetTunnelSDK/network.cpp b/NetTunnelSDK/network.cpp index bbaf3c8..09101f9 100644 --- a/NetTunnelSDK/network.cpp +++ b/NetTunnelSDK/network.cpp @@ -312,7 +312,7 @@ int WaitNetAdapterConnected(const TCHAR *pInterfaceName, int timeOutOfMs) { } if (memcmp(&adpterGuid, &guid, sizeof(GUID)) == 0) { - SPDLOG_DEBUG(TEXT("Interface {0} network connected."), pInterfaceName); + SPDLOG_DEBUG(TEXT("Interface {0}({1}) network connected now..."), ifName, pInterfaceName); return ERR_SUCCESS; } } @@ -483,16 +483,24 @@ int IsInternetConnectAdapter(int ifIndex, bool *pRet) { } int SetNetConnectionNetworkCategory(const TCHAR *pInterfaceName, const bool isPrivate) { - INetworkListManager *pNLM; - IEnumNetworks *pEnumNetworks; - INetwork *pINet; - HRESULT hr; + INetworkListManager *pNLM; + IEnumNetworkConnections *pEnumConns; + INetwork *pINet; + INetworkConnection *pIConn; + HRESULT hr; + GUID guid; + int ret; if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) { SPDLOG_ERROR(TEXT("Input pInterfaceName params error: {0}"), pInterfaceName); return -ERR_INPUT_PARAMS; } + if ((ret = GetInterfaceGUIDByName(pInterfaceName, &guid)) != ERR_SUCCESS) { + SPDLOG_ERROR(TEXT("Get NetCard [{0}] GUID error: {1}"), pInterfaceName, ret); + return ret; + } + hr = ::CoCreateInstance(CLSID_NetworkListManager, nullptr, CLSCTX_ALL, @@ -504,29 +512,23 @@ int SetNetConnectionNetworkCategory(const TCHAR *pInterfaceName, const bool isPr return -ERR_CREATE_COMMOBJECT; } - hr = pNLM->GetNetworks(NLM_ENUM_NETWORK_ALL, &pEnumNetworks); + hr = pNLM->GetNetworkConnections(&pEnumConns); - if (hr != S_OK || pEnumNetworks == nullptr) { + if (hr != S_OK || pEnumConns == nullptr) { SPDLOG_ERROR(TEXT("NetworkListManager GetNetworks failed: {0}."), hr); return -ERR_CREATE_COMMOBJECT; } - while (S_OK == pEnumNetworks->Next(1, &pINet, nullptr)) { - BSTR sName = {}; - TCHAR ifName[MAX_PATH]; + while (S_OK == pEnumConns->Next(1, &pIConn, nullptr)) { + GUID adpterGuid; + pIConn->GetNetwork(&pINet); + pIConn->GetAdapterId(&adpterGuid); - pINet->GetName(&sName); - - if (WideCharToTChar(sName, ifName, MAX_PATH) != ERR_SUCCESS) { - SysFreeString(sName); - return -ERR_MEMORY_STR; - } - - SysFreeString(sName); - - if (StrCmp(pInterfaceName, ifName) == 0) { - pINet->SetCategory(isPrivate ? NLM_NETWORK_CATEGORY_PRIVATE : NLM_NETWORK_CATEGORY_PUBLIC); - return ERR_SUCCESS; + if (pINet) { + if (memcmp(&adpterGuid, &guid, sizeof(GUID)) == 0) { + pINet->SetCategory(isPrivate ? NLM_NETWORK_CATEGORY_PRIVATE : NLM_NETWORK_CATEGORY_PUBLIC); + return ERR_SUCCESS; + } } } @@ -535,7 +537,6 @@ int SetNetConnectionNetworkCategory(const TCHAR *pInterfaceName, const bool isPr int GetNetConnectionNetworkCategory(const TCHAR *pInterfaceName, bool *pIsPrivate) { INetworkListManager *pNLM; - //IEnumNetworks *pEnumNetworks; IEnumNetworkConnections *pEnumConns; INetwork *pINet; INetworkConnection *pIConn; @@ -569,91 +570,6 @@ int GetNetConnectionNetworkCategory(const TCHAR *pInterfaceName, bool *pIsPrivat return -ERR_CREATE_COMMOBJECT; } -#if 0 - hr = pNLM->GetNetworks(NLM_ENUM_NETWORK_ALL, &pEnumNetworks); - - if (hr != S_OK || pEnumNetworks == nullptr) { - SPDLOG_ERROR(TEXT("NetworkListManager GetNetworks failed: {0}."), hr); - return -ERR_CREATE_COMMOBJECT; - } - - while (S_OK == pEnumNetworks->Next(1, &pINet, nullptr)) { - BSTR sName = {}; - WCHAR wName[MAX_PATH]; - TCHAR ifName[MAX_PATH]; - TCHAR guidName[MAX_PATH]; - NLM_NETWORK_CATEGORY cat; - GUID netGuid; - - pINet->GetName(&sName); - - if (WideCharToTChar(sName, ifName, MAX_PATH) != ERR_SUCCESS) { - return -ERR_MEMORY_STR; - } - - SysFreeString(sName); - pINet->GetCategory(&cat); - pINet->GetNetworkId(&netGuid); - - StringFromGUID2(netGuid, wName, MAX_PATH); - - if (WideCharToTChar(wName, guidName, MAX_PATH) != ERR_SUCCESS) { - return -ERR_MEMORY_STR; - } - - //SPDLOG_DEBUG(TEXT("[{3}]:{0} --> {1} --> {2}"), pInterfaceName, ifName, guidName, i++); - - if (memcmp(&netGuid, &guid, sizeof(GUID)) == 0) { - *pIsPrivate = (cat == NLM_NETWORK_CATEGORY_PRIVATE) ? true : false; - SPDLOG_DEBUG(TEXT("**** {0} category {1}"), pInterfaceName, *pIsPrivate); - return ERR_SUCCESS; - } - - //SPDLOG_DEBUG(TEXT("++ {0} --> {1}, connect {2}"), ifName, static_cast(cat), static_cast(conn)); - - if (StrNCmp(pInterfaceName, ifName, lstrlen(pInterfaceName)) == 0) { - SPDLOG_DEBUG(TEXT("++ {0} --> {1} --> {2}"), pInterfaceName, ifName, guidName); - /* pINet->GetNetworkConnections(&pEnumConns); - - if (pEnumConns) { - while (S_OK == pEnumConns->Next(1, &pIConn, nullptr)) { - GUID adpterGuid, connGuid; - WCHAR aGuid[MAX_PATH]; - WCHAR cGuid[MAX_PATH]; - TCHAR strAGuid[MAX_PATH]; - TCHAR strCGuid[MAX_PATH]; - - pIConn->GetAdapterId(&adpterGuid); - pIConn->GetConnectionId(&connGuid); - - StringFromGUID2(adpterGuid, aGuid, MAX_PATH); - StringFromGUID2(connGuid, cGuid, MAX_PATH); - - WideCharToTChar(aGuid, strAGuid, MAX_PATH); - WideCharToTChar(cGuid, strCGuid, MAX_PATH); - - SPDLOG_DEBUG(TEXT("--AdapterId: {0}, ConnectionId: {1}"), strAGuid, strCGuid); - } - SPDLOG_ERROR(TEXT("Exit ........")); - } else { - SPDLOG_ERROR(TEXT("----------------------------------------------------- empty")); - }*/ - } - /*if (StrCmp(TEXT("admin 3"), ifName) == 0) { - BSTR dName = {}; - pINet->GetDescription(&dName); - SPDLOG_DEBUG(TEXT("++ {0} --> {1} --> {2}"), pInterfaceName, ifName, guidName); - }*/ - //if (StrCmp(pInterfaceName, ifName) == 0) { - // //NLM_NETWORK_CATEGORY cat; - // //pINet->GetCategory(&cat); - - // *pIsPrivate = (cat == NLM_NETWORK_CATEGORY_PRIVATE) ? true : false; - // return ERR_SUCCESS; - //} - } -#endif - hr = pNLM->GetNetworkConnections(&pEnumConns); if (hr != S_OK || pEnumConns == nullptr) { @@ -671,7 +587,6 @@ int GetNetConnectionNetworkCategory(const TCHAR *pInterfaceName, bool *pIsPrivat NLM_NETWORK_CATEGORY cat; pINet->GetCategory(&cat); *pIsPrivate = (cat == NLM_NETWORK_CATEGORY_PRIVATE) ? true : false; - SPDLOG_DEBUG(TEXT("**** {0} category {1}"), pInterfaceName, *pIsPrivate); return ERR_SUCCESS; } }