NetTunnelWindows/NetTunnelSDK/network.cpp

1330 lines
44 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "pch.h"
#include "tunnel.h"
#include "usrerr.h"
#include <strsafe.h>
#include <tchar.h>
#include <Iphlpapi.h>
#include <iostream>
#include <ws2tcpip.h>
#include <netlistmgr.h>
#include <timeapi.h>
#include "globalcfg.h"
#include "misc.h"
#include "network.h"
#include <netcon.h>
#include <shlwapi.h>
#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<int>(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<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;
}
}
*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<int>(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<void **>(&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<void **>(&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<void **>(&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]);
// <20>ҵ<EFBFBD><D2B5><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>
if (StrCmp(pGUID, strGuid) != 0) {
continue;
}
memset(ifName, 0, MAX_NETCARD_NAME);
ret = WideCharToTChar(pNP->pszwName, ifName, MAX_NETCARD_NAME);
// ִ<><D6B4><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA>
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<void **>(&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}({1}) network connected now..."), ifName, pInterfaceName);
return ERR_SUCCESS;
}
}
}
}
Sleep(1000);
} while (timeGetTime() - startTime <= static_cast<DWORD>(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<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) {
int id = 0;
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
while (pAdapter && id < NET_CARD_MAX) {
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
g_NetAdapterInfo[id].InterfaceIndex = static_cast<int>(pAdapter->Index);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
StringCbCopy(g_NetAdapterInfo[id].NetCardUUID, MAX_ADAPTER_NAME_LENGTH, pAdapter->AdapterName);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϸ<EFBFBD><CFB8><EFBFBD><EFBFBD>
StringCbCopy(g_NetAdapterInfo[id].NetCardDescription,
MAX_ADAPTER_DESCRIPTION_LENGTH,
pAdapter->Description);
// <20><><EFBFBD><EFBFBD> IP <20><>ַ
StringCbCopy(g_NetAdapterInfo[id].NetCardIpaddr, MAX_IP_LEN - 1, pAdapter->IpAddressList.IpAddress.String);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
StringCbCopy(g_NetAdapterInfo[id].NetCardNetmask, MAX_IP_LEN - 1, pAdapter->IpAddressList.IpMask.String);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ַ
StringCbCopy(g_NetAdapterInfo[id].NetCardGateway, MAX_IP_LEN - 1, pAdapter->GatewayList.IpAddress.String);
// <20><><EFBFBD><EFBFBD> MAC <20><>ַ
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<PNIC_CONTENT>(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<MIB_IPFORWARDTABLE *>(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<MIB_IPFORWARDTABLE *>(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<int>(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;
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,
IID_INetworkListManager,
reinterpret_cast<void **>(&pNLM));
if (hr != S_OK || pNLM == nullptr) {
SPDLOG_ERROR(TEXT("CoCreateInstance NetworkListManager failed: {0}."), hr);
return -ERR_CREATE_COMMOBJECT;
}
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) {
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;
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<void **>(&pNLM));
if (hr != S_OK || pNLM == nullptr) {
SPDLOG_ERROR(TEXT("CoCreateInstance NetworkListManager failed: {0}."), hr);
return -ERR_CREATE_COMMOBJECT;
}
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;
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<void **>(&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<void **>(&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<void **>(&pNC));
if (pNC) {
TCHAR ifName[MAX_PATH];
NETCON_PROPERTIES *pNP;
pNC->GetProperties(&pNP);
// ִ<><D6B4><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA>
if (WideCharToTChar(pNP->pszwName, ifName, MAX_PATH) != ERR_SUCCESS) {
continue;
}
// <20>ҵ<EFBFBD><D2B5><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>
if (StrCmp(pInterfaceName, ifName) != 0) {
continue;
}
// δ<><CEB4><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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<void **>(&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<void **>(&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<void **>(&pNC));
if (pNC) {
TCHAR ifName[MAX_PATH];
NETCON_PROPERTIES *pNP;
pNC->GetProperties(&pNP);
// ִ<><D6B4><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA>
if (WideCharToTChar(pNP->pszwName, ifName, MAX_PATH) != ERR_SUCCESS) {
continue;
}
// <20>ҵ<EFBFBD><D2B5><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD>
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 <20><><EFBFBD><EFBFBD>ϵͳ·<CDB3>ɱ<EFBFBD><C9B1><EFBFBD>
* @param[in] ifIndex <20><><EFBFBD><EFBFBD> Index
* @param[in] pIP Ŀ<><C4BF> IP <20><>ַ
* @param[in] pMask Ŀ<><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param[in] pGateway ·<><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return <20><><EFBFBD><EFBFBD>ִ<EFBFBD>н<EFBFBD><D0BD><EFBFBD> 0: <20>ɹ<EFBFBD><C9B9><EFBFBD> С<><D0A1>0 ʧ<><CAA7> @see USER_ERRNO
* - -ERR_INPUT_PARAMS <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* - -ERR_UN_SUPPORT IP<49><50>ַת<D6B7><D7AA><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַʧ<D6B7><CAA7>
* - -ERR_NET_ADD_ROUTE <20><><EFBFBD><EFBFBD>·<EFBFBD>ɱ<EFBFBD><C9B1><EFBFBD>ʧ<EFBFBD><CAA7>
* - ERR_SUCCESS <20>ɹ<EFBFBD>
*/
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);
// <20>Ƴ<EFBFBD>
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);
// <20><>ȡǰ<C8A1><C7B0>IP<49><50>ַ
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);
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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);
// <20>ж<EFBFBD> WireGuard NAT <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>װ
if (!PathFileExists(psCmdPath)) {
if (!CreateDirectory(psCmdPath, nullptr)) {
return false;
}
}
StringCbCat(psCmdPath, MAX_PATH, "\\wireguard.psm1");
if (PathFileExists(psCmdPath)) {
// <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><CBB5><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD>װ
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