OCT 1. 增加开启/关闭 NAT 功能

2. 增加 Windows Hyper-V 状态检查接口
3. 增加 Windows Hyper-V 开启/关闭接口
This commit is contained in:
黄昕 2023-06-25 15:57:47 +08:00
parent 97234a2e4d
commit 48c978e203
7 changed files with 307 additions and 68 deletions

View File

@ -18,10 +18,11 @@ void RemoveTailLineBreak(TCHAR *pInputStr, int strSize) {
} }
} }
int RunCommand(TCHAR *pszCmd, TCHAR *pszResultBuffer, int dwResultBufferSize) { int RunCommand(TCHAR *pszCmd, TCHAR *pszResultBuffer, int dwResultBufferSize, unsigned long *pRetCode) {
BOOL bRet; BOOL bRet;
HANDLE hReadPipe = nullptr; HANDLE hReadPipe = nullptr;
HANDLE hWritePipe = nullptr; HANDLE hWritePipe = nullptr;
DWORD retCode;
STARTUPINFO si; STARTUPINFO si;
PROCESS_INFORMATION pi; PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES securityAttributes = {}; SECURITY_ATTRIBUTES securityAttributes = {};
@ -74,6 +75,13 @@ int RunCommand(TCHAR *pszCmd, TCHAR *pszResultBuffer, int dwResultBufferSize) {
::ReadFile(hReadPipe, pszResultBuffer, dwResultBufferSize, nullptr, nullptr); ::ReadFile(hReadPipe, pszResultBuffer, dwResultBufferSize, nullptr, nullptr);
} }
// 获取调用程序返回值
if (pRetCode) {
if (GetExitCodeProcess(pi.hProcess, &retCode)) {
*pRetCode = retCode;
}
}
// 关闭句柄, 释放内存 // 关闭句柄, 释放内存
::CloseHandle(pi.hThread); ::CloseHandle(pi.hThread);
::CloseHandle(pi.hProcess); ::CloseHandle(pi.hProcess);

View File

@ -8,7 +8,7 @@ extern "C" {
#endif #endif
void RemoveTailLineBreak(TCHAR *pInputStr, int strSize); void RemoveTailLineBreak(TCHAR *pInputStr, int strSize);
int RunCommand(TCHAR *pszCmd, TCHAR *pszResultBuffer, int dwResultBufferSize); int RunCommand(TCHAR *pszCmd, TCHAR *pszResultBuffer, int dwResultBufferSize, unsigned long *pRetCode);
/** /**
* @brief IPv4 CIDR * @brief IPv4 CIDR

View File

@ -9,21 +9,24 @@
#include "globalcfg.h" #include "globalcfg.h"
#include "misc.h" #include "misc.h"
#include <shlwapi.h>
#pragma comment(lib, "Iphlpapi.lib") #pragma comment(lib, "Iphlpapi.lib")
int NetmaskToCIDR(const TCHAR *pNetMask) { static const TCHAR *g_TabCIDR[] = {
const TCHAR *tabCIDR[] = { TEXT("128.0.0.0"), TEXT("192.0.0.0"), TEXT("224.0.0.0"), TEXT("240.0.0.0"),
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("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.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.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.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.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.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")};
TEXT("255.255.255.248"), TEXT("255.255.255.252"), TEXT("255.255.255.254"), TEXT("255.255.255.255")};
for (int i = 0; i < static_cast<int>(std::size(tabCIDR)); i++) { int NetmaskToCIDR(const TCHAR *pNetMask) {
if (lstrcmp(tabCIDR[i], pNetMask) == 0) {
for (int i = 0; i < static_cast<int>(std::size(g_TabCIDR)); i++) {
if (lstrcmp(g_TabCIDR[i], pNetMask) == 0) {
return i + 1; return i + 1;
} }
} }
@ -32,28 +35,90 @@ int NetmaskToCIDR(const TCHAR *pNetMask) {
} }
const TCHAR *CIDRToNetmask(const UINT8 cidr) { const TCHAR *CIDRToNetmask(const UINT8 cidr) {
const TCHAR *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")};
if (cidr >= 1 && cidr <= std::size(tabCIDR)) { if (cidr >= 1 && cidr <= std::size(g_TabCIDR)) {
return tabCIDR[cidr - 1]; return g_TabCIDR[cidr - 1];
} }
return TEXT(""); return TEXT("");
} }
int GetWindowsHyperVStatus(int *pEnabled) {
int ret;
TCHAR cmdBuf[MAX_PATH];
TCHAR cmdResult[2048];
DWORD retCode;
if (pEnabled == nullptr) {
SPDLOG_ERROR("Input pEnabled params error");
return -ERR_INPUT_PARAMS;
}
if (FAILED(StringCbPrintf(cmdBuf,
MAX_PATH,
"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("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,
"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,
"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("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;
}
int GetInterfaceIndexByName(const TCHAR *pInterfaceName, int *pIndex) { int GetInterfaceIndexByName(const TCHAR *pInterfaceName, int *pIndex) {
int ret; int ret;
TCHAR cmdBuf[MAX_PATH]; DWORD retCode;
TCHAR cmdResult[MAX_PATH] = {}; TCHAR cmdBuf[MAX_PATH];
const TCHAR *pToken[] = {TEXT("\r\n"), TEXT(" "), TEXT("InterfaceIndex:")}; TCHAR cmdResult[MAX_PATH] = {};
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) { if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName); SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName);
@ -74,12 +139,12 @@ int GetInterfaceIndexByName(const TCHAR *pInterfaceName, int *pIndex) {
return -ERR_MEMORY_STR; return -ERR_MEMORY_STR;
} }
if ((ret = RunCommand(cmdBuf, cmdResult, MAX_PATH)) != ERR_SUCCESS) { if ((ret = RunCommand(cmdBuf, cmdResult, MAX_PATH, &retCode)) != ERR_SUCCESS) {
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret); SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret);
return -ERR_CALL_SHELL; return -ERR_CALL_SHELL;
} }
SPDLOG_DEBUG("Run command [{0}] resutl \'{1}\'", cmdBuf, cmdResult); SPDLOG_DEBUG("Run command [{0}] resutl \'{1}\' return {2}", cmdBuf, cmdResult, retCode);
// ÒƳý // ÒƳý
StringRemoveAll(cmdResult, TEXT("\r\n")); StringRemoveAll(cmdResult, TEXT("\r\n"));
@ -91,19 +156,78 @@ int GetInterfaceIndexByName(const TCHAR *pInterfaceName, int *pIndex) {
return ERR_SUCCESS; return ERR_SUCCESS;
} }
/** int SetNATRule(const TCHAR *pInterfaceName, const TCHAR *pCidrIpaddr) {
* @brief int ret;
* @param pInterfaceName TCHAR cmdBuf[MAX_PATH];
* @return 0: 0 @see USER_ERRNO DWORD retCode;
* - -ERR_INPUT_PARAMS
* - -ERR_MALLOC_MEMORY if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
* - -ERR_MEMORY_STR SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName);
* - -ERR_CALL_SHELL return -ERR_INPUT_PARAMS;
* - ERR_SUCCESS }
*/
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 RemoveInterfaceIpAddress(const TCHAR *pInterfaceName) {
int ret; int ret;
TCHAR cmdBuf[MAX_PATH]; TCHAR cmdBuf[MAX_PATH];
DWORD retCode;
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) { if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName); SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName);
@ -118,17 +242,22 @@ int RemoveInterfaceIpAddress(const TCHAR *pInterfaceName) {
return -ERR_MEMORY_STR; return -ERR_MEMORY_STR;
} }
if ((ret = RunCommand(cmdBuf, nullptr, 0)) != ERR_SUCCESS) { if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) {
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret); SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret);
return -ERR_CALL_SHELL; return -ERR_CALL_SHELL;
} }
SPDLOG_DEBUG("Run Set IP Command({1}): {0}", cmdBuf, retCode);
return ERR_SUCCESS; return ERR_SUCCESS;
} }
int SetInterfaceIpAddress(const TCHAR *pInterfaceName, const TCHAR *pIpaddr, const TCHAR *pNetmask) { int SetInterfaceIpAddress(const TCHAR *pInterfaceName, const TCHAR *pIpaddr, const TCHAR *pNetmask) {
ULONG index = 0; int ret;
WCHAR pIfName[] = L"{7D92693A-0F1E-0212-176D-31226AF3E147}"; TCHAR cmdBuf[MAX_PATH];
int cidr;
DWORD retCode;
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) { if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName); SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName);
return -ERR_INPUT_PARAMS; return -ERR_INPUT_PARAMS;
@ -144,22 +273,37 @@ int SetInterfaceIpAddress(const TCHAR *pInterfaceName, const TCHAR *pIpaddr, con
return -ERR_INPUT_PARAMS; return -ERR_INPUT_PARAMS;
} }
//MultiByteToWideChar(CP_UTF8, 0, pInterfaceName, lstrlen(pInterfaceName), pIfName, MAX_PATH / sizeof(WCHAR)); cidr = NetmaskToCIDR(pNetmask);
if (GetAdapterIndex(pIfName, &index) != NO_ERROR) { if (FAILED(StringCbPrintf(
ShowWindowsErrorMessage("GetAdapterIndex"); cmdBuf,
return -ERR_SYS_CALL; 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; return ERR_SUCCESS;
} }
int SetInterfaceIpAddressFromCIDR(const TCHAR *pInterfaceName, const TCHAR *pCidrIpaddr) { int SetInterfaceIpAddressFromCIDR(const TCHAR *pInterfaceName, const TCHAR *pCidrIpaddr) {
TCHAR ipAddr[MAX_IP_LEN]; TCHAR ipAddr[MAX_IP_LEN];
TCHAR ip[MAX_IP_LEN]; TCHAR ip[MAX_IP_LEN];
TCHAR cidrMask[4]; int ret;
const TCHAR *netmask; TCHAR cmdBuf[MAX_PATH];
TCHAR *token, *p = nullptr; DWORD retCode;
TCHAR *token, *p = nullptr;
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) { if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName); SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName);
@ -187,14 +331,26 @@ int SetInterfaceIpAddressFromCIDR(const TCHAR *pInterfaceName, const TCHAR *pCid
SPDLOG_ERROR("CIDR IpAddress string format error: {0}", pCidrIpaddr); SPDLOG_ERROR("CIDR IpAddress string format error: {0}", pCidrIpaddr);
return -ERR_INPUT_PARAMS; return -ERR_INPUT_PARAMS;
} }
StringCbCopy(cidrMask, 4, token);
int cidr = atoi(cidrMask); if (FAILED(StringCbPrintf(
netmask = CIDRToNetmask(cidr); 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;
}
SPDLOG_DEBUG("CIDR IpAddress convert:{0} --> {1}/{2}", pCidrIpaddr, ip, netmask); if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) {
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret);
return -ERR_CALL_SHELL;
}
return SetInterfaceIpAddress(pInterfaceName, ip, netmask); SPDLOG_DEBUG("Run Set IP Command({1}): {0}", cmdBuf, retCode);
return ERR_SUCCESS;
} }
int GetAllNICInfo(PNIC_CONTENT pInfo, int *pItemCounts) { int GetAllNICInfo(PNIC_CONTENT pInfo, int *pItemCounts) {

View File

@ -227,7 +227,7 @@ TUNNEL_API int __cdecl GetInterfaceIndexByName(const TCHAR *pInterfaceName, int
/** /**
* @brief * @brief
* @param pInterfaceName * @param[in] pInterfaceName
* @return 0: 0 @see USER_ERRNO * @return 0: 0 @see USER_ERRNO
* - -ERR_INPUT_PARAMS * - -ERR_INPUT_PARAMS
* - -ERR_MALLOC_MEMORY * - -ERR_MALLOC_MEMORY
@ -237,7 +237,68 @@ TUNNEL_API int __cdecl GetInterfaceIndexByName(const TCHAR *pInterfaceName, int
*/ */
TUNNEL_API int __cdecl RemoveInterfaceIpAddress(const TCHAR *pInterfaceName); TUNNEL_API int __cdecl RemoveInterfaceIpAddress(const TCHAR *pInterfaceName);
/**
* @brief IP地址
* @param[in] pInterfaceName
* @param[in] pCidrIpaddr CIDR类型IP地址
* @return 0: 0 @see USER_ERRNO
* - -ERR_INPUT_PARAMS
* - -ERR_MALLOC_MEMORY
* - -ERR_MEMORY_STR
* - -ERR_CALL_SHELL
* - ERR_SUCCESS
*/
TUNNEL_API int __cdecl SetInterfaceIpAddressFromCIDR(const TCHAR *pInterfaceName, const TCHAR *pCidrIpaddr); TUNNEL_API int __cdecl SetInterfaceIpAddressFromCIDR(const TCHAR *pInterfaceName, const TCHAR *pCidrIpaddr);
/**
* @brief IP地址
* @param[in] pInterfaceName
* @param[in] pIpaddr IP
* @param[in] pNetmask
* @return 0: 0 @see USER_ERRNO
* - -ERR_INPUT_PARAMS
* - -ERR_MALLOC_MEMORY
* - -ERR_MEMORY_STR
* - -ERR_CALL_SHELL
* - ERR_SUCCESS
*/
TUNNEL_API int __cdecl SetInterfaceIpAddress(const TCHAR *pInterfaceName, const TCHAR *pIpaddr, const TCHAR *pNetmask);
/**
* @brief Windows Hyper-V NAT转发功能
* @param[out] pEnabled Hyper-V , TRUE , FALSE
* @return 0: 0 @see USER_ERRNO
* - -ERR_INPUT_PARAMS
* - -ERR_MEMORY_STR
* - -ERR_CALL_SHELL
* - ERR_SUCCESS
*/
TUNNEL_API int __cdecl GetWindowsHyperVStatus(int *pEnabled);
/**
* @brief / Windows Hyper-V
* @param[in] enabled TRUE , FALSE
* @return 0: 0 @see USER_ERRNO
* - -ERR_INPUT_PARAMS
* - -ERR_MEMORY_STR
* - -ERR_CALL_SHELL
* - -ERR_PROCESS_RETURN
* - ERR_SUCCESS
*/
TUNNEL_API int __cdecl EnableWindowsHyperV(bool enabled);
/**
* @brief Windows WireGuard NAT
* @param pInterfaceName
* @param pCidrIpaddr CIDR
* @return 0: 0 @see USER_ERRNO
* - -ERR_INPUT_PARAMS
* - -ERR_MEMORY_STR
* - -ERR_CALL_SHELL
* - -ERR_PROCESS_RETURN
* - ERR_SUCCESS
*/
TUNNEL_API int __cdecl SetNATRule(const TCHAR *pInterfaceName, const TCHAR *pCidrIpaddr);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -22,5 +22,6 @@ enum USER_ERRNO {
ERR_MALLOC_MEMORY, ///< 分配内存失败 ERR_MALLOC_MEMORY, ///< 分配内存失败
ERR_MMAP_MEMORY, ///< 共享内存失败 ERR_MMAP_MEMORY, ///< 共享内存失败
ERR_MEMORY_STR, ///< 字符串操作失败 ERR_MEMORY_STR, ///< 字符串操作失败
ERR_CREATE_PROCESS ///< 创建进程失败 ERR_CREATE_PROCESS, ///< 创建进程失败
ERR_PROCESS_RETURN ///< 进程调用返回失败
}; };

View File

@ -19,7 +19,7 @@ constexpr auto WINENVBUF_SIZE = (4096);
int WireGuardInstallServerService(bool bInstall) { int WireGuardInstallServerService(bool bInstall) {
TCHAR cfgVal[MAX_PATH]; TCHAR cfgVal[MAX_PATH];
TCHAR cmdBuf[MAX_PATH]; DWORD retCode;
GetPrivateProfileString(CFG_WIREGUARD_SECTION, GetPrivateProfileString(CFG_WIREGUARD_SECTION,
CFG_WGCFG_PATH, CFG_WGCFG_PATH,
@ -33,7 +33,8 @@ int WireGuardInstallServerService(bool bInstall) {
const HANDLE hFind = FindFirstFile(cfgVal, &FindFileData); const HANDLE hFind = FindFirstFile(cfgVal, &FindFileData);
if (hFind != INVALID_HANDLE_VALUE) { if (hFind != INVALID_HANDLE_VALUE) {
int ret; TCHAR cmdBuf[MAX_PATH];
int ret;
if (bInstall) { if (bInstall) {
// 安装服务 // 安装服务
@ -57,7 +58,7 @@ int WireGuardInstallServerService(bool bInstall) {
svrName); svrName);
} }
if ((ret = RunCommand(cmdBuf, nullptr, 0)) != ERR_SUCCESS) { if ((ret = RunCommand(cmdBuf, nullptr, 0, &retCode)) != ERR_SUCCESS) {
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret); SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret);
return -ERR_CALL_SHELL; return -ERR_CALL_SHELL;
} }
@ -304,6 +305,7 @@ int WireGuardCreateServerConfig(const PWGSERVER_CONFIG pWgConfig) {
int GenerateWireguardKeyPairs(TCHAR *pPubKey, int pubkeySize, TCHAR *pPrivKey, int privKeySize) { int GenerateWireguardKeyPairs(TCHAR *pPubKey, int pubkeySize, TCHAR *pPrivKey, int privKeySize) {
int ret; int ret;
DWORD retCode;
TCHAR cmdBuffer[MAX_PATH]; TCHAR cmdBuffer[MAX_PATH];
TCHAR cmdResult[MAX_PATH]; TCHAR cmdResult[MAX_PATH];
PSDK_CONFIG pCfg = GetGlobalCfgInfo(); PSDK_CONFIG pCfg = GetGlobalCfgInfo();
@ -318,7 +320,7 @@ int GenerateWireguardKeyPairs(TCHAR *pPubKey, int pubkeySize, TCHAR *pPrivKey, i
StringCbPrintf(cmdBuffer, MAX_PATH, TEXT("cmd.exe /C \"%s\" genkey"), pCfg->wireguardCfg.wgPath); StringCbPrintf(cmdBuffer, MAX_PATH, TEXT("cmd.exe /C \"%s\" genkey"), pCfg->wireguardCfg.wgPath);
if ((ret = RunCommand(cmdBuffer, cmdResult, MAX_PATH)) != ERR_SUCCESS) { if ((ret = RunCommand(cmdBuffer, cmdResult, MAX_PATH, &retCode)) != ERR_SUCCESS) {
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuffer, ret); SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuffer, ret);
return -ERR_CALL_SHELL; return -ERR_CALL_SHELL;
} }
@ -334,7 +336,7 @@ int GenerateWireguardKeyPairs(TCHAR *pPubKey, int pubkeySize, TCHAR *pPrivKey, i
pCfg->wireguardCfg.wgPath); pCfg->wireguardCfg.wgPath);
memset(cmdResult, 0, MAX_PATH); memset(cmdResult, 0, MAX_PATH);
if ((ret = RunCommand(cmdBuffer, cmdResult, MAX_PATH)) != ERR_SUCCESS) { if ((ret = RunCommand(cmdBuffer, cmdResult, MAX_PATH, &retCode)) != ERR_SUCCESS) {
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuffer, ret); SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuffer, ret);
return -ERR_CALL_SHELL; return -ERR_CALL_SHELL;
} }

View File

@ -57,7 +57,13 @@ public:
TEST_METHOD(TestSetInterfaceIpAddressFromCIDR) { TEST_METHOD(TestSetInterfaceIpAddressFromCIDR) {
Assert::AreEqual( Assert::AreEqual(
RET_OK, RET_OK,
SetInterfaceIpAddressFromCIDR(TEXT("{7D92693A-0F1E-0212-176D-31226AF3E147}"), TEXT("192.168.100.250/24"))); SetInterfaceIpAddressFromCIDR(TEXT("wg_server"), TEXT("192.168.100.250/24")));
}
TEST_METHOD(TestSetInterfaceIpAddress) {
Assert::AreEqual(
RET_OK,
SetInterfaceIpAddress(TEXT("wg_server"), TEXT("192.168.101.250"), TEXT("255.255.0.0")));
} }
TEST_METHOD(TestGetInterfaceIndexByName) { TEST_METHOD(TestGetInterfaceIndexByName) {
@ -65,9 +71,14 @@ public:
Assert::AreEqual(RET_OK, GetInterfaceIndexByName(TEXT("wg_server"), &index)); Assert::AreEqual(RET_OK, GetInterfaceIndexByName(TEXT("wg_server"), &index));
} }
TEST_METHOD(TestRemoveInterfaceIpAddress) { TEST_METHOD(TestRemoveInterfaceIpAddress) {
int index;
Assert::AreEqual(RET_OK, RemoveInterfaceIpAddress(TEXT("wg_server"))); Assert::AreEqual(RET_OK, RemoveInterfaceIpAddress(TEXT("wg_server")));
} }
TEST_METHOD(TestGetWindowsHyperVStatus) {
int enable;
Assert::AreEqual(RET_OK, GetWindowsHyperVStatus(&enable));
Assert::AreEqual(TRUE, enable);
}
}; };
} }