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; } }