#include "pch.h" #include "tunnel.h" #include "usrerr.h" #include #include #include #include #include #include #include #include "globalcfg.h" #include "misc.h" #include "network.h" #include #include #pragma comment(lib, "Iphlpapi.lib") #pragma comment(lib, "Ws2_32.lib") static NIC_CONTENT g_NetAdapterInfo[NET_CARD_MAX]; static const TCHAR *g_TabCIDR[] = { TEXT("128.0.0.0"), TEXT("192.0.0.0"), TEXT("224.0.0.0"), TEXT("240.0.0.0"), TEXT("248.0.0.0"), TEXT("252.0.0.0"), TEXT("254.0.0.0"), TEXT("255.0.0.0"), TEXT("255.128.0.0"), TEXT("255.192.0.0"), TEXT("255.224.0.0"), TEXT("255.240.0.0"), TEXT("255.248.0.0"), TEXT("255.252.0.0"), TEXT("255.254.0.0"), TEXT("255.255.0.0"), TEXT("255.255.128.0"), TEXT("255.255.192.0"), TEXT("255.255.224.0"), TEXT("255.255.240.0"), TEXT("255.255.248.0"), TEXT("255.255.252.0"), TEXT("255.255.254.0"), TEXT("255.255.255.0"), TEXT("255.255.255.128"), TEXT("255.255.255.192"), TEXT("255.255.255.224"), TEXT("255.255.255.240"), TEXT("255.255.255.248"), TEXT("255.255.255.252"), TEXT("255.255.255.254"), TEXT("255.255.255.255")}; int NetmaskToCIDR(const TCHAR *pNetMask) { for (int i = 0; i < static_cast(std::size(g_TabCIDR)); i++) { if (lstrcmp(g_TabCIDR[i], pNetMask) == 0) { return i + 1; } } return 0xFF; } const TCHAR *CIDRToNetmask(const UINT8 cidr) { if (cidr >= 1 && cidr <= std::size(g_TabCIDR)) { return g_TabCIDR[cidr - 1]; } return TEXT(""); } int GetInterfaceIfIndexByGUID(const TCHAR *pGUID, int *pIfIndex) { PIP_ADAPTER_INFO pAdapterInfo; DWORD dwRetVal; if (pGUID == nullptr || lstrlen(pGUID) == 0) { SPDLOG_ERROR(TEXT("Input pGUID error: {0}"), pGUID); return -ERR_INPUT_PARAMS; } if (pIfIndex == nullptr) { SPDLOG_ERROR(TEXT("Input pIfIndex params error")); return -ERR_INPUT_PARAMS; } 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; } } *pIfIndex = -1; if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) { PIP_ADAPTER_INFO pAdapter = pAdapterInfo; while (pAdapter) { if (StrCmp(pAdapter->AdapterName, pGUID) == 0) { *pIfIndex = static_cast(pAdapter->Index); HeapFree(GetProcessHeap(), 0, pAdapterInfo); return ERR_SUCCESS; } 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_ITEM_UNEXISTS; } int GetInterfaceNameByGUID(const TCHAR *pGUID, TCHAR ifName[MAX_NETCARD_NAME]) { VARIANT v; INetConnection *pNC = nullptr; IEnumVARIANT *pEV = nullptr; IUnknown *pUnk = nullptr; INetSharingEveryConnectionCollection *pNSECC = nullptr; INetSharingManager *pNSM; HRESULT hr; if (pGUID == nullptr || lstrlen(pGUID) == 0) { SPDLOG_ERROR(TEXT("Input pGUID params error: {0}"), pGUID); return -ERR_INPUT_PARAMS; } hr = ::CoCreateInstance(CLSID_NetSharingManager, nullptr, CLSCTX_ALL, IID_INetSharingManager, reinterpret_cast(&pNSM)); if (hr != S_OK || pNSM == nullptr) { SPDLOG_ERROR(TEXT("CoCreateInstance NetSharingManager failed: {0}."), hr); return -ERR_CREATE_COMMOBJECT; } VariantInit(&v); hr = pNSM->get_EnumEveryConnection(&pNSECC); if (hr != S_OK || !pNSECC) { SPDLOG_ERROR(TEXT("INetSharingManager get_EnumEveryConnection failed: {0}."), hr); return -ERR_SYS_CALL; } hr = pNSECC->get__NewEnum(&pUnk); pNSECC->Release(); if (hr != S_OK || !pUnk) { SPDLOG_ERROR(TEXT("INetSharingManager get_EnumEveryConnection failed: {0}."), hr); return -ERR_SYS_CALL; } hr = pUnk->QueryInterface(IID_IEnumVARIANT, reinterpret_cast(&pEV)); pUnk->Release(); if (hr != S_OK || !pUnk) { SPDLOG_ERROR(TEXT("INetSharingManager get_EnumEveryConnection failed: {0}."), hr); return -ERR_SYS_CALL; } while (S_OK == pEV->Next(1, &v, nullptr)) { if (V_VT(&v) == VT_UNKNOWN) { V_UNKNOWN(&v)->QueryInterface(IID_INetConnection, reinterpret_cast(&pNC)); if (pNC) { int ret; TCHAR strGuid[MAX_PATH] = {}; NETCON_PROPERTIES *pNP; pNC->GetProperties(&pNP); StringCbPrintf(strGuid, MAX_PATH, TEXT("{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"), pNP->guidId.Data1, pNP->guidId.Data2, pNP->guidId.Data3, pNP->guidId.Data4[0], pNP->guidId.Data4[1], pNP->guidId.Data4[2], pNP->guidId.Data4[3], pNP->guidId.Data4[4], pNP->guidId.Data4[5], pNP->guidId.Data4[6], pNP->guidId.Data4[7]); // 找到对应的网卡进行处理 if (StrCmp(pGUID, strGuid) != 0) { continue; } memset(ifName, 0, MAX_NETCARD_NAME); ret = WideCharToTChar(pNP->pszwName, ifName, MAX_NETCARD_NAME); // 执行字符串转换 if (ret != ERR_SUCCESS) { SPDLOG_ERROR(TEXT("Convert Unicode wide char to TCHAR failed: {0}."), ret); return ret; } return ERR_SUCCESS; } } } return ERR_ITEM_UNEXISTS; } int GetInternetIfName(TCHAR ifName[MAX_NETCARD_NAME]) { for (int i = 0; i < NET_CARD_MAX; i++) { const PNIC_CONTENT pNic = &g_NetAdapterInfo[i]; bool bIsInternel; if (ERR_SUCCESS == IsInternetConnectAdapter(pNic->InterfaceIndex, &bIsInternel) && bIsInternel) { StringCbCopy(ifName, MAX_NETCARD_NAME, pNic->NetCardName); return ERR_SUCCESS; } } return -ERR_ITEM_UNEXISTS; } int GetInterfaceGUIDByName(const TCHAR *pInterfaceName, GUID *pGuid) { int ret; if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) { SPDLOG_ERROR(TEXT("Input pInterfaceName params error: {0}"), pInterfaceName); return -ERR_INPUT_PARAMS; } if (lstrlen(g_NetAdapterInfo[0].NetCardUUID) == 0) { PNIC_CONTENT nic; int nItem; if ((ret = GetAllNICInfo(&nic, &nItem)) != ERR_SUCCESS) { return ret; } } for (const auto &v : g_NetAdapterInfo) { if (StrCmp(pInterfaceName, v.NetCardName) == 0) { WCHAR strGuid[MAX_PATH]; if ((ret = TCharToWideChar(v.NetCardUUID, strGuid, MAX_PATH)) != ERR_SUCCESS) { return ret; } if (CLSIDFromString(strGuid, pGuid) != NOERROR) { return -ERR_MEMORY_STR; } return ERR_SUCCESS; } } return -ERR_ITEM_UNEXISTS; } int WaitNetAdapterConnected(const TCHAR *pInterfaceName, int timeOutOfMs) { INetworkListManager *pNLM; IEnumNetworkConnections *pEnumConns; INetwork *pINet; INetworkConnection *pIConn; HRESULT hr; GUID guid; const DWORD startTime = timeGetTime(); if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) { SPDLOG_ERROR(TEXT("Input pInterfaceName params error: {0}"), pInterfaceName); return -ERR_INPUT_PARAMS; } hr = ::CoCreateInstance(CLSID_NetworkListManager, nullptr, CLSCTX_ALL, IID_INetworkListManager, reinterpret_cast(&pNLM)); if (hr != S_OK || pNLM == nullptr) { SPDLOG_ERROR(TEXT("CoCreateInstance NetworkListManager failed: {0}."), hr); return -ERR_CREATE_COMMOBJECT; } do { hr = pNLM->GetNetworkConnections(&pEnumConns); if (hr != S_OK || pEnumConns == nullptr) { SPDLOG_ERROR(TEXT("NetworkListManager GetNetworks failed: {0}."), hr); continue; } while (S_OK == pEnumConns->Next(1, &pIConn, nullptr)) { GUID adpterGuid; pIConn->GetAdapterId(&adpterGuid); pIConn->GetNetwork(&pINet); if (pINet) { BSTR sName = {}; TCHAR ifName[MAX_PATH]; pINet->GetName(&sName); if (WideCharToTChar(sName, ifName, MAX_PATH) != ERR_SUCCESS) { SysFreeString(sName); return -ERR_MEMORY_STR; } SysFreeString(sName); if (StrNCmp(pInterfaceName, ifName, lstrlen(pInterfaceName)) == 0) { if (GetInterfaceGUIDByName(pInterfaceName, &guid) != ERR_SUCCESS) { continue; } if (memcmp(&adpterGuid, &guid, sizeof(GUID)) == 0) { SPDLOG_DEBUG(TEXT("Interface {0} network connected."), pInterfaceName); return ERR_SUCCESS; } } } } Sleep(1000); } while (timeGetTime() - startTime <= static_cast(timeOutOfMs)); return -ERR_SYS_TIMEOUT; } int GetAllNICInfo(PNIC_CONTENT *pInfo, int *pItemCounts) { PNIC_CONTENT pNic; PIP_ADAPTER_INFO pAdapterInfo; DWORD dwRetVal; if (pItemCounts == nullptr) { SPDLOG_ERROR(TEXT("Input pItemCounts params error")); return -ERR_INPUT_PARAMS; } if (pInfo == nullptr) { SPDLOG_ERROR(TEXT("Input pInfo params error")); return -ERR_INPUT_PARAMS; } 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 id = 0; PIP_ADAPTER_INFO pAdapter = pAdapterInfo; while (pAdapter && id < NET_CARD_MAX) { // 网卡索引 g_NetAdapterInfo[id].InterfaceIndex = static_cast(pAdapter->Index); // 网卡名称 StringCbCopy(g_NetAdapterInfo[id].NetCardUUID, MAX_ADAPTER_NAME_LENGTH, pAdapter->AdapterName); // 网卡详细描述 StringCbCopy(g_NetAdapterInfo[id].NetCardDescription, MAX_ADAPTER_DESCRIPTION_LENGTH, pAdapter->Description); // 网卡 IP 地址 StringCbCopy(g_NetAdapterInfo[id].NetCardIpaddr, MAX_IP_LEN - 1, pAdapter->IpAddressList.IpAddress.String); // 网卡子网掩码 StringCbCopy(g_NetAdapterInfo[id].NetCardNetmask, MAX_IP_LEN - 1, pAdapter->IpAddressList.IpMask.String); // 网卡网关地址 StringCbCopy(g_NetAdapterInfo[id].NetCardGateway, MAX_IP_LEN - 1, pAdapter->GatewayList.IpAddress.String); // 网卡 MAC 地址 StringCbPrintf(g_NetAdapterInfo[id].NetCardMacAddr, 20 - 1, TEXT("%02X:%02X:%02X:%02X:%02X:%02X"), pAdapter->Address[0], pAdapter->Address[1], pAdapter->Address[2], pAdapter->Address[3], pAdapter->Address[4], pAdapter->Address[5]); GetInterfaceNameByGUID(pAdapter->AdapterName, g_NetAdapterInfo[id].NetCardName); id++; pAdapter = pAdapter->Next; } *pItemCounts = id; } 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); } pNic = static_cast(CoTaskMemAlloc(sizeof(NIC_CONTENT) * (*pItemCounts))); if (pNic == nullptr) { *pItemCounts = 0; SPDLOG_ERROR(TEXT("Error allocating memory {0} bytes"), sizeof(NIC_CONTENT) * (*pItemCounts)); return -ERR_MALLOC_MEMORY; } memset(pNic, 0, sizeof(NIC_CONTENT) * (*pItemCounts)); memcpy(pNic, g_NetAdapterInfo, sizeof(NIC_CONTENT) * *pItemCounts); *pInfo = pNic; return ERR_SUCCESS; } int IsInternetConnectAdapter(int ifIndex, bool *pRet) { DWORD dwSize = 0; DWORD dwRetVal; PMIB_IPFORWARDTABLE pIpForwardTable; if (ifIndex < 0 || ifIndex > 255) { SPDLOG_ERROR(TEXT("Input ifIndex params error: {0}"), ifIndex); return -ERR_INPUT_PARAMS; } if (pRet == nullptr) { SPDLOG_ERROR(TEXT("Input pRet params error")); return -ERR_INPUT_PARAMS; } pIpForwardTable = static_cast(HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IPFORWARDTABLE))); if (pIpForwardTable == nullptr) { SPDLOG_ERROR(TEXT("Malloc {0} bytes memory error"), sizeof(MIB_IPFORWARDTABLE)); return -ERR_MALLOC_MEMORY; } if (GetIpForwardTable(pIpForwardTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) { HeapFree(GetProcessHeap(), 0, pIpForwardTable); pIpForwardTable = static_cast(HeapAlloc(GetProcessHeap(), 0, dwSize)); if (pIpForwardTable == nullptr) { SPDLOG_ERROR(TEXT("Malloc {0} bytes memory error"), dwSize); return -ERR_MALLOC_MEMORY; } } *pRet = false; if ((dwRetVal = GetIpForwardTable(pIpForwardTable, &dwSize, 0)) == NO_ERROR) { for (DWORD i = 0; i < pIpForwardTable->dwNumEntries; i++) { TCHAR ipStr[24] = {}; TCHAR maskStr[24] = {}; if (static_cast(pIpForwardTable->table[i].dwForwardIfIndex) != ifIndex) { continue; } if (InetNtop(AF_INET, &pIpForwardTable->table[i].dwForwardDest, ipStr, 24) == nullptr) { continue; } if (InetNtop(AF_INET, &pIpForwardTable->table[i].dwForwardMask, maskStr, 24) == nullptr) { continue; } if (StrCmp(ipStr, TEXT("0.0.0.0")) == 0 && StrCmp(maskStr, TEXT("0.0.0.0"))) { *pRet = true; break; } } HeapFree(GetProcessHeap(), 0, pIpForwardTable); return ERR_SUCCESS; } else { SPDLOG_ERROR(TEXT("GetIpForwardTable failed: {0}."), dwRetVal); HeapFree(GetProcessHeap(), 0, pIpForwardTable); return ERR_GET_IPFOWARDTBL; } } int SetNetConnectionNetworkCategory(const TCHAR *pInterfaceName, const bool isPrivate) { INetworkListManager *pNLM; IEnumNetworks *pEnumNetworks; INetwork *pINet; HRESULT hr; if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) { SPDLOG_ERROR(TEXT("Input pInterfaceName params error: {0}"), pInterfaceName); return -ERR_INPUT_PARAMS; } hr = ::CoCreateInstance(CLSID_NetworkListManager, nullptr, CLSCTX_ALL, IID_INetworkListManager, reinterpret_cast(&pNLM)); if (hr != S_OK || pNLM == nullptr) { SPDLOG_ERROR(TEXT("CoCreateInstance NetworkListManager failed: {0}."), hr); return -ERR_CREATE_COMMOBJECT; } 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 = {}; TCHAR ifName[MAX_PATH]; 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; } } return -ERR_ITEM_UNEXISTS; } int GetNetConnectionNetworkCategory(const TCHAR *pInterfaceName, bool *pIsPrivate) { INetworkListManager *pNLM; //IEnumNetworks *pEnumNetworks; 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 (pIsPrivate == nullptr) { SPDLOG_ERROR(TEXT("Input pIsPrivate params error")); 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, IID_INetworkListManager, reinterpret_cast(&pNLM)); if (hr != S_OK || pNLM == nullptr) { SPDLOG_ERROR(TEXT("CoCreateInstance NetworkListManager failed: {0}."), hr); 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) { SPDLOG_ERROR(TEXT("NetworkListManager GetNetworks failed: {0}."), hr); return -ERR_CREATE_COMMOBJECT; } while (S_OK == pEnumConns->Next(1, &pIConn, nullptr)) { GUID adpterGuid; pIConn->GetNetwork(&pINet); pIConn->GetAdapterId(&adpterGuid); if (pINet) { if (memcmp(&adpterGuid, &guid, sizeof(GUID)) == 0) { 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; } } } return -ERR_ITEM_UNEXISTS; } int SetNetConnectionSharing(const TCHAR *pInterfaceName, bool isEnable, bool isSetPrivate) { VARIANT v; INetConnection *pNC = nullptr; INetSharingConfiguration *pNSC = nullptr; IEnumVARIANT *pEV = nullptr; IUnknown *pUnk = nullptr; INetSharingEveryConnectionCollection *pNSECC = nullptr; INetSharingManager *pNSM; HRESULT hr; if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) { SPDLOG_ERROR(TEXT("Input pInterfaceName params error: {0}"), pInterfaceName); return -ERR_INPUT_PARAMS; } hr = ::CoCreateInstance(CLSID_NetSharingManager, nullptr, CLSCTX_ALL, IID_INetSharingManager, reinterpret_cast(&pNSM)); if (hr != S_OK || pNSM == nullptr) { SPDLOG_ERROR(TEXT("CoCreateInstance NetSharingManager failed: {0}."), hr); return -ERR_CREATE_COMMOBJECT; } VariantInit(&v); hr = pNSM->get_EnumEveryConnection(&pNSECC); if (hr != S_OK || !pNSECC) { SPDLOG_ERROR(TEXT("INetSharingManager get_EnumEveryConnection failed: {0}."), hr); return -ERR_SYS_CALL; } hr = pNSECC->get__NewEnum(&pUnk); pNSECC->Release(); if (hr != S_OK || !pUnk) { SPDLOG_ERROR(TEXT("INetSharingManager get_EnumEveryConnection failed: {0}."), hr); return -ERR_SYS_CALL; } hr = pUnk->QueryInterface(IID_IEnumVARIANT, reinterpret_cast(&pEV)); pUnk->Release(); if (hr != S_OK || !pUnk) { SPDLOG_ERROR(TEXT("INetSharingManager get_EnumEveryConnection failed: {0}."), hr); return -ERR_SYS_CALL; } while (S_OK == pEV->Next(1, &v, nullptr)) { if (V_VT(&v) == VT_UNKNOWN) { V_UNKNOWN(&v)->QueryInterface(IID_INetConnection, reinterpret_cast(&pNC)); if (pNC) { TCHAR ifName[MAX_PATH]; NETCON_PROPERTIES *pNP; pNC->GetProperties(&pNP); // 执行字符串转换 if (WideCharToTChar(pNP->pszwName, ifName, MAX_PATH) != ERR_SUCCESS) { continue; } // 找到对应的网卡进行处理 if (StrCmp(pInterfaceName, ifName) != 0) { continue; } // 未连接的网卡不处理 if (pNP->Status != NCS_CONNECTED) { return -ERR_NET_UNCONNECT; } hr = pNSM->get_INetSharingConfigurationForINetConnection(pNC, &pNSC); if (hr != S_OK || !pNSC) { continue; } if (pNSC) { if (isEnable) { pNSC->DisableSharing(); Sleep(500); pNSC->EnableSharing(isSetPrivate ? ICSSHARINGTYPE_PRIVATE : ICSSHARINGTYPE_PUBLIC); } else { pNSC->DisableSharing(); } pNSC->Release(); return ERR_SUCCESS; } } } } return ERR_ITEM_UNEXISTS; } int GetNetConnectionSharing(const TCHAR *pInterfaceName, bool *pIsEnable) { VARIANT v; INetConnection *pNC = nullptr; INetSharingConfiguration *pNSC = nullptr; IEnumVARIANT *pEV = nullptr; IUnknown *pUnk = nullptr; INetSharingEveryConnectionCollection *pNSECC = nullptr; INetSharingManager *pNSM; HRESULT hr; if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) { SPDLOG_ERROR(TEXT("Input pInterfaceName params error: {0}"), pInterfaceName); return -ERR_INPUT_PARAMS; } if (pIsEnable == nullptr) { SPDLOG_ERROR(TEXT("Input pIsEnable params error")); return -ERR_INPUT_PARAMS; } hr = ::CoCreateInstance(CLSID_NetSharingManager, nullptr, CLSCTX_ALL, IID_INetSharingManager, reinterpret_cast(&pNSM)); if (hr != S_OK || pNSM == nullptr) { SPDLOG_ERROR(TEXT("CoCreateInstance NetSharingManager failed: {0}."), hr); return -ERR_CREATE_COMMOBJECT; } VariantInit(&v); hr = pNSM->get_EnumEveryConnection(&pNSECC); if (hr != S_OK || !pNSECC) { SPDLOG_ERROR(TEXT("INetSharingManager get_EnumEveryConnection failed: {0}."), hr); return -ERR_SYS_CALL; } hr = pNSECC->get__NewEnum(&pUnk); pNSECC->Release(); if (hr != S_OK || !pUnk) { SPDLOG_ERROR(TEXT("INetSharingManager get_EnumEveryConnection failed: {0}."), hr); return -ERR_SYS_CALL; } hr = pUnk->QueryInterface(IID_IEnumVARIANT, reinterpret_cast(&pEV)); pUnk->Release(); if (hr != S_OK || !pUnk) { SPDLOG_ERROR(TEXT("INetSharingManager get_EnumEveryConnection failed: {0}."), hr); return -ERR_SYS_CALL; } while (S_OK == pEV->Next(1, &v, nullptr)) { if (V_VT(&v) == VT_UNKNOWN) { V_UNKNOWN(&v)->QueryInterface(IID_INetConnection, reinterpret_cast(&pNC)); if (pNC) { TCHAR ifName[MAX_PATH]; NETCON_PROPERTIES *pNP; pNC->GetProperties(&pNP); // 执行字符串转换 if (WideCharToTChar(pNP->pszwName, ifName, MAX_PATH) != ERR_SUCCESS) { continue; } // 找到对应的网卡进行处理 if (StrCmp(pInterfaceName, ifName) != 0) { continue; } hr = pNSM->get_INetSharingConfigurationForINetConnection(pNC, &pNSC); if (hr != S_OK || !pNSC) { continue; } if (pNSC) { VARIANT_BOOL bRet = false; pNSC->get_SharingEnabled(&bRet); *pIsEnable = bRet; pNSC->Release(); return ERR_SUCCESS; } } } } return ERR_ITEM_UNEXISTS; } /** * @brief 添加系统路由表项 * @param[in] ifIndex 网卡 Index * @param[in] pIP 目的 IP 地址 * @param[in] pMask 目的子网掩码 * @param[in] pGateway 路由网关 * @return 函数执行结果 0: 成功, 小于0 失败 @see USER_ERRNO * - -ERR_INPUT_PARAMS 输入参数错误 * - -ERR_UN_SUPPORT IP地址转网络字节序网络地址失败 * - -ERR_NET_ADD_ROUTE 添加路由表项失败 * - ERR_SUCCESS 成功 */ int AddRouteTable(int ifIndex, const char *pIP, const char *pMask, const char *pGateway) { MIB_IPFORWARDROW IpForwardTable; if (pIP == nullptr || lstrlen(pIP) < 8) { SPDLOG_ERROR(TEXT("Input pIP params error: {0}"), pIP); return -ERR_INPUT_PARAMS; } if (pMask == nullptr || lstrlen(pMask) < 8) { SPDLOG_ERROR(TEXT("Input pMask params error: {0}"), pMask); return -ERR_INPUT_PARAMS; } if (pGateway == nullptr || lstrlen(pGateway) < 8) { SPDLOG_ERROR(TEXT("Input pGateway params error: {0}"), pGateway); return -ERR_INPUT_PARAMS; } ZeroMemory(&IpForwardTable, sizeof(MIB_IPFORWARDROW)); if (inet_pton(AF_INET, pIP, &IpForwardTable.dwForwardDest) <= 0) { SPDLOG_ERROR(TEXT("Convert {0} to network ipaddress error."), pIP); return -ERR_UN_SUPPORT; } if (inet_pton(AF_INET, pMask, &IpForwardTable.dwForwardMask) <= 0) { SPDLOG_ERROR(TEXT("Convert {0} to network ipaddress error."), pMask); return -ERR_UN_SUPPORT; } if (inet_pton(AF_INET, pGateway, &IpForwardTable.dwForwardNextHop) <= 0) { SPDLOG_ERROR(TEXT("Convert {0} to network ipaddress error."), pGateway); return -ERR_UN_SUPPORT; } IpForwardTable.dwForwardIfIndex = ifIndex; IpForwardTable.dwForwardType = MIB_IPROUTE_TYPE_DIRECT; IpForwardTable.dwForwardProto = MIB_IPPROTO_LOCAL; IpForwardTable.dwForwardPolicy = 0; IpForwardTable.dwForwardAge = 0; IpForwardTable.dwForwardNextHopAS = 0; IpForwardTable.dwForwardMetric1 = 0xFFFFFFFF; IpForwardTable.dwForwardMetric2 = 0xFFFFFFFF; IpForwardTable.dwForwardMetric3 = 0xFFFFFFFF; IpForwardTable.dwForwardMetric4 = 0xFFFFFFFF; IpForwardTable.dwForwardMetric5 = 0xFFFFFFFF; if (CreateIpForwardEntry(&IpForwardTable) != NO_ERROR) { return -ERR_NET_ADD_ROUTE; } return ERR_SUCCESS; } #if 0 int GetWindowsHyperVStatus(int *pEnabled) { int ret; TCHAR cmdBuf[MAX_PATH]; TCHAR cmdResult[2048]; DWORD retCode; if (pEnabled == nullptr) { SPDLOG_ERROR(TEXT("Input pEnabled params error")); return -ERR_INPUT_PARAMS; } if (FAILED(StringCbPrintf(cmdBuf, MAX_PATH, TEXT("PowerShell -Command \"& {Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All " "-Online | Format-List -Property State}\"")))) { SPDLOG_ERROR("Format String Error"); return -ERR_MEMORY_STR; } if ((ret = RunCommand(cmdBuf, cmdResult, 2048, &retCode)) != ERR_SUCCESS) { SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret); return -ERR_CALL_SHELL; } if (StrStr(cmdResult, TEXT("Enabled")) != nullptr) { *pEnabled = TRUE; } else { *pEnabled = FALSE; } SPDLOG_DEBUG(TEXT("Run Get Windows Hyper-V status Command({1}): {0} result: {2}\n"), cmdBuf, retCode, cmdResult); return ERR_SUCCESS; } int EnableWindowsHyperV(bool enabled) { int ret; TCHAR cmdBuf[MAX_PATH]; DWORD retCode; if (enabled) { if (FAILED(StringCbPrintf(cmdBuf, MAX_PATH, TEXT("PowerShell -Command \"& {Enable-WindowsOptionalFeature -Online -FeatureName " "Microsoft-Hyper-V -All}\"")))) { SPDLOG_ERROR("Format String Error"); return -ERR_MEMORY_STR; } } else { if (FAILED(StringCbPrintf(cmdBuf, MAX_PATH, TEXT("PowerShell -Command \"& {Disable-WindowsOptionalFeature -Online -FeatureName " "Microsoft-Hyper-V-Hypervisor}\"")))) { SPDLOG_ERROR("Format String Error"); return -ERR_MEMORY_STR; } } if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) { SPDLOG_ERROR(TEXT("Run command [{0}] error: {1}"), cmdBuf, ret); return -ERR_CALL_SHELL; } if (retCode != 0) { SPDLOG_ERROR(TEXT("PowerShell return error({1}): {0}"), cmdBuf, retCode); return -ERR_PROCESS_RETURN; } return ERR_SUCCESS; } int GetInterfaceIndexByName(const TCHAR *pInterfaceName, int *pIndex) { int ret; DWORD retCode; TCHAR cmdBuf[MAX_PATH]; TCHAR cmdResult[MAX_PATH] = {}; if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) { SPDLOG_ERROR(TEXT("Input pInterfaceName params error: {0}"), pInterfaceName); return -ERR_INPUT_PARAMS; } if (pIndex == nullptr) { SPDLOG_ERROR(TEXT("Input pIndex params error")); return -ERR_INPUT_PARAMS; } if (FAILED( StringCbPrintf(cmdBuf, MAX_PATH, TEXT("PowerShell -Command \"& {Get-NetAdapter -Name %s | Format-List -Property InterfaceIndex}\""), pInterfaceName))) { SPDLOG_ERROR("Format String Error"); return -ERR_MEMORY_STR; } if ((ret = RunCommand(cmdBuf, cmdResult, MAX_PATH, &retCode)) != ERR_SUCCESS) { SPDLOG_ERROR(TEXT("Run command [{0}] error: {1}"), cmdBuf, ret); return -ERR_CALL_SHELL; } SPDLOG_DEBUG(TEXT("Run command [{0}] resutl \'{1}\' return {2}"), cmdBuf, cmdResult, retCode); // 移除 StringRemoveAll(cmdResult, TEXT("\r\n")); StringRemoveAll(cmdResult, TEXT(" ")); StringRemoveAll(cmdResult, TEXT("InterfaceIndex:")); *pIndex = strtol(cmdResult, nullptr, 10); return ERR_SUCCESS; } int SetNATRule(const TCHAR *pInterfaceName, const TCHAR *pCidrIpaddr) { int ret; TCHAR cmdBuf[MAX_PATH]; DWORD retCode; if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) { SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName); return -ERR_INPUT_PARAMS; } if (pCidrIpaddr == nullptr || lstrlen(pCidrIpaddr) == 0) { SPDLOG_ERROR("Input pCidrIpaddr params error: {0}", pCidrIpaddr); return -ERR_INPUT_PARAMS; } if (FAILED( StringCbPrintf(cmdBuf, MAX_PATH, "PowerShell -Command \"& {New-NetNat -Name %s_nat -InternalIPInterfaceAddressPrefix %s}\"", pInterfaceName, pCidrIpaddr))) { SPDLOG_ERROR("Format String Error"); return -ERR_MEMORY_STR; } if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) { SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret); return -ERR_CALL_SHELL; } SPDLOG_DEBUG("Run Set IP Command({1}): {0}", cmdBuf, retCode); if (retCode != 0) { SPDLOG_ERROR("PowerShell return error({1}): {0}", cmdBuf, retCode); return -ERR_PROCESS_RETURN; } return ERR_SUCCESS; } int RemoveNATRule(const TCHAR *pInterfaceName) { int ret; TCHAR cmdBuf[MAX_PATH]; DWORD retCode; if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) { SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName); return -ERR_INPUT_PARAMS; } if (FAILED(StringCbPrintf(cmdBuf, MAX_PATH, "PowerShell -Command \"& {Remove-NetNat -Name %s_nat -Confirm:$false}\"", pInterfaceName))) { SPDLOG_ERROR("Format String Error"); return -ERR_MEMORY_STR; } if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) { SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret); return -ERR_CALL_SHELL; } SPDLOG_DEBUG("Run Set IP Command({1}): {0}", cmdBuf, retCode); return ERR_SUCCESS; } int RemoveInterfaceIpAddress(const TCHAR *pInterfaceName) { int ret; TCHAR cmdBuf[MAX_PATH]; DWORD retCode; if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) { SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName); return -ERR_INPUT_PARAMS; } if (FAILED(StringCbPrintf(cmdBuf, MAX_PATH, "PowerShell -Command \"& {Remove-NetIPAddress -InterfaceAlias %s -Confirm:$false}\"", pInterfaceName))) { SPDLOG_ERROR("Format String Error"); return -ERR_MEMORY_STR; } if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) { SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret); return -ERR_CALL_SHELL; } SPDLOG_DEBUG("Run Set IP Command({1}): {0}", cmdBuf, retCode); return ERR_SUCCESS; } int SetInterfaceIpAddress(const TCHAR *pInterfaceName, const TCHAR *pIpaddr, const TCHAR *pNetmask) { int ret; TCHAR cmdBuf[MAX_PATH]; int cidr; DWORD retCode; if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) { SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName); return -ERR_INPUT_PARAMS; } if (pIpaddr == nullptr || lstrlen(pIpaddr) == 0) { SPDLOG_ERROR("Input pIpaddr params error: {0}", pIpaddr); return -ERR_INPUT_PARAMS; } if (pNetmask == nullptr || lstrlen(pNetmask) == 0) { SPDLOG_WARN("Input pNetmask params error: {0}", pNetmask); return -ERR_INPUT_PARAMS; } cidr = NetmaskToCIDR(pNetmask); if (FAILED(StringCbPrintf( cmdBuf, MAX_PATH, "PowerShell -Command \"& {New-NetIPAddress -InterfaceAlias %s -IPAddress %s -PrefixLength %d}\"", pInterfaceName, pIpaddr, cidr))) { SPDLOG_ERROR("Format String Error"); return -ERR_MEMORY_STR; } if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) { SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret); return -ERR_CALL_SHELL; } SPDLOG_DEBUG("Run Set IP Command({1}): {0}", cmdBuf, retCode); return ERR_SUCCESS; } int SetInterfaceIpAddressFromCIDR(const TCHAR *pInterfaceName, const TCHAR *pCidrIpaddr) { TCHAR ipAddr[MAX_IP_LEN]; TCHAR ip[MAX_IP_LEN]; int ret; TCHAR cmdBuf[MAX_PATH]; DWORD retCode; TCHAR *token, *p = nullptr; if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) { SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName); return -ERR_INPUT_PARAMS; } if (pCidrIpaddr == nullptr || lstrlen(pCidrIpaddr) == 0) { SPDLOG_ERROR("Input pCidrIpaddr params error: {0}", pCidrIpaddr); return -ERR_INPUT_PARAMS; } StringCbCopy(ipAddr, MAX_IP_LEN, pCidrIpaddr); // 获取前面IP地址 token = strtok_s(ipAddr, TEXT("/"), &p); if (token == nullptr) { SPDLOG_ERROR("CIDR IpAddress string format error: {0}", pCidrIpaddr); return -ERR_INPUT_PARAMS; } StringCbCopy(ip, MAX_IP_LEN, token); // 获取后面子网掩码 token = strtok_s(nullptr, TEXT("/"), &p); if (token == nullptr) { SPDLOG_ERROR("CIDR IpAddress string format error: {0}", pCidrIpaddr); return -ERR_INPUT_PARAMS; } if (FAILED(StringCbPrintf( cmdBuf, MAX_PATH, "PowerShell -Command \"& {New-NetIPAddress -InterfaceAlias %s -IPAddress %s -PrefixLength %s}\"", pInterfaceName, ip, token))) { SPDLOG_ERROR("Format String Error"); return -ERR_MEMORY_STR; } if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) { SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret); return -ERR_CALL_SHELL; } SPDLOG_DEBUG("Run Set IP Command({1}): {0}", cmdBuf, retCode); return ERR_SUCCESS; } int IsInterfacePrivate(const TCHAR *pInterfaceName, bool *pIsPrivateMode) { int ret; DWORD retCode; TCHAR cmdBuf[MAX_PATH]; TCHAR cmdResult[MAX_PATH] = {}; if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) { SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName); return -ERR_INPUT_PARAMS; } if (pIsPrivateMode == nullptr) { SPDLOG_ERROR("Input pIsPrivateMode params error"); return -ERR_INPUT_PARAMS; } if (FAILED(StringCbPrintf( cmdBuf, MAX_PATH, "PowerShell -Command \"& {Get-NetConnectionProfile -Name %s | Format-List -Property NetworkCategory}\"", pInterfaceName))) { SPDLOG_ERROR("Format String Error"); return -ERR_MEMORY_STR; } if ((ret = RunCommand(cmdBuf, cmdResult, MAX_PATH, &retCode)) != ERR_SUCCESS) { SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret); return -ERR_CALL_SHELL; } SPDLOG_DEBUG("Run command [{0}] resutl \'{1}\' return {2}", cmdBuf, cmdResult, retCode); if (StrStr(cmdResult, TEXT("Private")) != nullptr) { *pIsPrivateMode = true; return ERR_SUCCESS; } else { if (StrStr(cmdResult, TEXT("Public")) != nullptr) { *pIsPrivateMode = false; return ERR_SUCCESS; } } return -ERR_ITEM_UNEXISTS; } int SetInterfacePrivate(const TCHAR *pInterfaceName, bool isPrivate) { int ret; TCHAR cmdBuf[MAX_PATH]; DWORD retCode; if (isPrivate) { if (FAILED(StringCbPrintf(cmdBuf, MAX_PATH, "PowerShell -Command \"& {Get-NetConnectionProfile -InterfaceAlias %s | " "Set-NetConnectionProfile -NetworkCategory Private}\"", pInterfaceName))) { SPDLOG_ERROR("Format String Error"); return -ERR_MEMORY_STR; } } else { if (FAILED(StringCbPrintf(cmdBuf, MAX_PATH, "PowerShell -Command \"& {Get-NetConnectionProfile -InterfaceAlias %s | " "Set-NetConnectionProfile -NetworkCategory Public}\"", pInterfaceName))) { SPDLOG_ERROR("Format String Error"); return -ERR_MEMORY_STR; } } if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) { SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret); return -ERR_CALL_SHELL; } if (retCode != 0) { SPDLOG_ERROR("PowerShell return error({1}): {0}", cmdBuf, retCode); return -ERR_PROCESS_RETURN; } return ERR_SUCCESS; } bool IsCustomNatPSCmdInstalled() { TCHAR psCmdPath[MAX_PATH]; StringCbPrintf(psCmdPath, MAX_PATH, TEXT("%s\\system32\\WindowsPowerShell\\v1.0\\Modules\\wireguard"), GetGlobalCfgInfo()->systemDirectory); // 判断 WireGuard NAT 命令是否安装 if (!PathFileExists(psCmdPath)) { if (!CreateDirectory(psCmdPath, nullptr)) { return false; } } StringCbCat(psCmdPath, MAX_PATH, "\\wireguard.psm1"); if (PathFileExists(psCmdPath)) { // 文件存在说明已经安装 return true; } return false; } int IsNetConnectionSharingEnabled(const TCHAR *pInterfaceName, bool *pIsEnabled) { int ret; DWORD retCode; TCHAR cmdResult[MAX_PATH] = {}; TCHAR cmdBuf[512]; if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) { SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName); return -ERR_INPUT_PARAMS; } if (pIsEnabled == nullptr) { SPDLOG_ERROR("Input pIsEnabled params error"); return -ERR_INPUT_PARAMS; } if (FAILED(StringCbPrintf( cmdBuf, 512, TEXT("PowerShell -NoProfile -NonInteractive -WindowStyle Hidden -ExecutionPolicy Bypass Invoke-Command " "-ArgumentList '%s' -ScriptBlock {param($IFNAME);$netShare = New-Object -ComObject HNetCfg.HNetShare;" "$privateConnection = $netShare.EnumEveryConnection |? { $netShare.NetConnectionProps.Invoke($_).Name " "-eq " "'wg_cli' };$privateConfig = " "$netShare.INetSharingConfigurationForINetConnection.Invoke($privateConnection);" "Write-Output $privateConfig}"), pInterfaceName))) { SPDLOG_ERROR("Format String Error"); return -ERR_MEMORY_STR; } if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) { SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret); return -ERR_CALL_SHELL; } if (retCode != 0) { SPDLOG_ERROR("PowerShell return error({1}): {0}", cmdBuf, retCode); return -ERR_PROCESS_RETURN; } SPDLOG_DEBUG("Run command [{0}] resutl \'{1}\' return {2}", cmdBuf, cmdResult, retCode); if (StrStr(cmdResult, TEXT("False")) != nullptr) { *pIsEnabled = false; return ERR_SUCCESS; } else { if (StrStr(cmdResult, TEXT("True")) != nullptr) { *pIsEnabled = true; return ERR_SUCCESS; } } return -ERR_ITEM_UNEXISTS; } #endif