OCT 1. 增加开启/关闭 NAT 功能
2. 增加 Windows Hyper-V 状态检查接口 3. 增加 Windows Hyper-V 开启/关闭接口
This commit is contained in:
parent
97234a2e4d
commit
48c978e203
|
@ -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;
|
||||
HANDLE hReadPipe = nullptr;
|
||||
HANDLE hWritePipe = nullptr;
|
||||
DWORD retCode;
|
||||
STARTUPINFO si;
|
||||
PROCESS_INFORMATION pi;
|
||||
SECURITY_ATTRIBUTES securityAttributes = {};
|
||||
|
@ -74,6 +75,13 @@ int RunCommand(TCHAR *pszCmd, TCHAR *pszResultBuffer, int dwResultBufferSize) {
|
|||
::ReadFile(hReadPipe, pszResultBuffer, dwResultBufferSize, nullptr, nullptr);
|
||||
}
|
||||
|
||||
// 获取调用程序返回值
|
||||
if (pRetCode) {
|
||||
if (GetExitCodeProcess(pi.hProcess, &retCode)) {
|
||||
*pRetCode = retCode;
|
||||
}
|
||||
}
|
||||
|
||||
// 关闭句柄, 释放内存
|
||||
::CloseHandle(pi.hThread);
|
||||
::CloseHandle(pi.hProcess);
|
||||
|
|
|
@ -8,7 +8,7 @@ extern "C" {
|
|||
#endif
|
||||
|
||||
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 掩码
|
||||
|
|
|
@ -9,21 +9,24 @@
|
|||
#include "globalcfg.h"
|
||||
#include "misc.h"
|
||||
|
||||
#include <shlwapi.h>
|
||||
|
||||
#pragma comment(lib, "Iphlpapi.lib")
|
||||
|
||||
int NetmaskToCIDR(const TCHAR *pNetMask) {
|
||||
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")};
|
||||
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")};
|
||||
|
||||
for (int i = 0; i < static_cast<int>(std::size(tabCIDR)); i++) {
|
||||
if (lstrcmp(tabCIDR[i], pNetMask) == 0) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -32,28 +35,90 @@ int NetmaskToCIDR(const TCHAR *pNetMask) {
|
|||
}
|
||||
|
||||
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)) {
|
||||
return tabCIDR[cidr - 1];
|
||||
if (cidr >= 1 && cidr <= std::size(g_TabCIDR)) {
|
||||
return g_TabCIDR[cidr - 1];
|
||||
}
|
||||
|
||||
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 ret;
|
||||
TCHAR cmdBuf[MAX_PATH];
|
||||
TCHAR cmdResult[MAX_PATH] = {};
|
||||
const TCHAR *pToken[] = {TEXT("\r\n"), TEXT(" "), TEXT("InterfaceIndex:")};
|
||||
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);
|
||||
|
@ -74,12 +139,12 @@ int GetInterfaceIndexByName(const TCHAR *pInterfaceName, int *pIndex) {
|
|||
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);
|
||||
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"));
|
||||
|
@ -91,19 +156,78 @@ int GetInterfaceIndexByName(const TCHAR *pInterfaceName, int *pIndex) {
|
|||
return ERR_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 删除接口网络地址
|
||||
* @param pInterfaceName 网卡名称
|
||||
* @return 函数执行结果 0: 成功, 小于0 失败 @see USER_ERRNO
|
||||
* - -ERR_INPUT_PARAMS 输入参数错误
|
||||
* - -ERR_MALLOC_MEMORY 分配内存失败
|
||||
* - -ERR_MEMORY_STR 字符串处理
|
||||
* - -ERR_CALL_SHELL 调用系统命令行失败
|
||||
* - 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);
|
||||
|
@ -118,17 +242,22 @@ int RemoveInterfaceIpAddress(const TCHAR *pInterfaceName) {
|
|||
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);
|
||||
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) {
|
||||
ULONG index = 0;
|
||||
WCHAR pIfName[] = L"{7D92693A-0F1E-0212-176D-31226AF3E147}";
|
||||
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;
|
||||
|
@ -144,22 +273,37 @@ int SetInterfaceIpAddress(const TCHAR *pInterfaceName, const TCHAR *pIpaddr, con
|
|||
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) {
|
||||
ShowWindowsErrorMessage("GetAdapterIndex");
|
||||
return -ERR_SYS_CALL;
|
||||
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];
|
||||
TCHAR cidrMask[4];
|
||||
const TCHAR *netmask;
|
||||
TCHAR *token, *p = nullptr;
|
||||
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);
|
||||
|
@ -187,14 +331,26 @@ int SetInterfaceIpAddressFromCIDR(const TCHAR *pInterfaceName, const TCHAR *pCid
|
|||
SPDLOG_ERROR("CIDR IpAddress string format error: {0}", pCidrIpaddr);
|
||||
return -ERR_INPUT_PARAMS;
|
||||
}
|
||||
StringCbCopy(cidrMask, 4, token);
|
||||
|
||||
int cidr = atoi(cidrMask);
|
||||
netmask = CIDRToNetmask(cidr);
|
||||
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;
|
||||
}
|
||||
|
||||
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) {
|
||||
|
|
|
@ -227,7 +227,7 @@ TUNNEL_API int __cdecl GetInterfaceIndexByName(const TCHAR *pInterfaceName, int
|
|||
|
||||
/**
|
||||
* @brief 删除接口网络地址
|
||||
* @param pInterfaceName 网卡名称
|
||||
* @param[in] pInterfaceName 网卡名称
|
||||
* @return 函数执行结果 0: 成功, 小于0 失败 @see USER_ERRNO
|
||||
* - -ERR_INPUT_PARAMS 输入参数错误
|
||||
* - -ERR_MALLOC_MEMORY 分配内存失败
|
||||
|
@ -237,7 +237,68 @@ TUNNEL_API int __cdecl GetInterfaceIndexByName(const TCHAR *pInterfaceName, int
|
|||
*/
|
||||
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);
|
||||
|
||||
/**
|
||||
* @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
|
||||
}
|
||||
#endif
|
|
@ -22,5 +22,6 @@ enum USER_ERRNO {
|
|||
ERR_MALLOC_MEMORY, ///< 分配内存失败
|
||||
ERR_MMAP_MEMORY, ///< 共享内存失败
|
||||
ERR_MEMORY_STR, ///< 字符串操作失败
|
||||
ERR_CREATE_PROCESS ///< 创建进程失败
|
||||
ERR_CREATE_PROCESS, ///< 创建进程失败
|
||||
ERR_PROCESS_RETURN ///< 进程调用返回失败
|
||||
};
|
|
@ -19,7 +19,7 @@ constexpr auto WINENVBUF_SIZE = (4096);
|
|||
|
||||
int WireGuardInstallServerService(bool bInstall) {
|
||||
TCHAR cfgVal[MAX_PATH];
|
||||
TCHAR cmdBuf[MAX_PATH];
|
||||
DWORD retCode;
|
||||
|
||||
GetPrivateProfileString(CFG_WIREGUARD_SECTION,
|
||||
CFG_WGCFG_PATH,
|
||||
|
@ -33,7 +33,8 @@ int WireGuardInstallServerService(bool bInstall) {
|
|||
const HANDLE hFind = FindFirstFile(cfgVal, &FindFileData);
|
||||
|
||||
if (hFind != INVALID_HANDLE_VALUE) {
|
||||
int ret;
|
||||
TCHAR cmdBuf[MAX_PATH];
|
||||
int ret;
|
||||
|
||||
if (bInstall) {
|
||||
// 安装服务
|
||||
|
@ -57,7 +58,7 @@ int WireGuardInstallServerService(bool bInstall) {
|
|||
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);
|
||||
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 ret;
|
||||
DWORD retCode;
|
||||
TCHAR cmdBuffer[MAX_PATH];
|
||||
TCHAR cmdResult[MAX_PATH];
|
||||
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);
|
||||
|
||||
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);
|
||||
return -ERR_CALL_SHELL;
|
||||
}
|
||||
|
@ -334,7 +336,7 @@ int GenerateWireguardKeyPairs(TCHAR *pPubKey, int pubkeySize, TCHAR *pPrivKey, i
|
|||
pCfg->wireguardCfg.wgPath);
|
||||
|
||||
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);
|
||||
return -ERR_CALL_SHELL;
|
||||
}
|
||||
|
|
|
@ -57,7 +57,13 @@ public:
|
|||
TEST_METHOD(TestSetInterfaceIpAddressFromCIDR) {
|
||||
Assert::AreEqual(
|
||||
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) {
|
||||
|
@ -65,9 +71,14 @@ public:
|
|||
Assert::AreEqual(RET_OK, GetInterfaceIndexByName(TEXT("wg_server"), &index));
|
||||
}
|
||||
|
||||
TEST_METHOD(TestRemoveInterfaceIpAddress) {
|
||||
int index;
|
||||
TEST_METHOD(TestRemoveInterfaceIpAddress) {
|
||||
Assert::AreEqual(RET_OK, RemoveInterfaceIpAddress(TEXT("wg_server")));
|
||||
}
|
||||
|
||||
TEST_METHOD(TestGetWindowsHyperVStatus) {
|
||||
int enable;
|
||||
Assert::AreEqual(RET_OK, GetWindowsHyperVStatus(&enable));
|
||||
Assert::AreEqual(TRUE, enable);
|
||||
}
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue