OCT 1. 增加网络操作相关接口

This commit is contained in:
黄昕 2023-06-21 18:04:16 +08:00
parent b13b13f4d4
commit 97234a2e4d
14 changed files with 1502 additions and 97 deletions

View File

@ -39,8 +39,9 @@ public partial class MainForm : Form
IntPtr ptr = new IntPtr(pt.ToInt64() + Marshal.SizeOf(typeof(NetCard)) * i);
NetCard card = (NetCard)Marshal.PtrToStructure(ptr, typeof(NetCard));
Console.WriteLine("Name:{0}", card.Name);
Console.WriteLine("UUID:{0}", card.UUID);
Console.WriteLine("Description:{0}", card.Description);
Console.WriteLine("Index:{0}", card.IfIndex);
Console.WriteLine("Ip:{0}", card.IpAddr);
Console.WriteLine("NetMask:{0}", card.NetMask);
Console.WriteLine("MacAddr:{0}", card.MacAddr);
@ -109,12 +110,14 @@ public partial class MainForm : Form
//var v = NetTunnelLib.FindWireguardExe(buffer, 256);
//MessageBox.Show(buffer.ToString() + "----" + v.ToString());
test_install_wg_server();
//test_install_wg_server();
}
private void button2_Click(object sender, EventArgs e)
{
test_uninstall_wg_server();
//test_uninstall_wg_server();
test_get_network_info();
}
}

View File

@ -27,18 +27,23 @@ public enum LogLevel
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct NetCard
{
public int IfIndex;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string Name;
public string UUID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 132)]
public string Description;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 48)]
public string IpAddr;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 48)]
public string NetMask;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 48)]
public string Gateway;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
public string MacAddr;
}

1052
NetTunnelSDK/AIGCJson.hpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -147,6 +147,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="AIGCJson.hpp" />
<ClInclude Include="framework.h" />
<ClInclude Include="globalcfg.h" />
<ClInclude Include="misc.h" />

View File

@ -33,6 +33,9 @@
<ClInclude Include="globalcfg.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="AIGCJson.hpp">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">

View File

@ -11,6 +11,13 @@ typedef struct {
BOOL wgExists; ///< wg.exe 是否存在
} WIREGUARD_CFG, *PWIREGUARD_CFG;
typedef struct {
TCHAR wgName[260]; ///< 网卡名称, Windows标识为 UUID
TCHAR wgIpaddr[MAX_IP_LEN]; ///< 网卡 IP 地址
TCHAR wgNetmask[MAX_IP_LEN]; ///< 网卡子网掩码
TCHAR wgCfgPath[MAX_PATH]; ///< 配置文件路径
} WGINTERFACE_CFG, *PWGINTERFACE_CFG;
/**
* @brief SDK
*/
@ -22,6 +29,8 @@ typedef struct {
spdlog::level::level_enum logLevel; ///< 日志等级
TCHAR cfgPath[MAX_PATH]; ///< 配置文件路径
WIREGUARD_CFG wireguardCfg; ///< wireguard 配置项 @see WIREGUARD_CFG
WGINTERFACE_CFG wgServerCfg; ///< wireguard 服务端网络接口配置
WGINTERFACE_CFG wgClientCfg; ///< wireguard 客户端网络接口配置
} SDK_CONFIG, *PSDK_CONFIG;
#ifdef __cplusplus // If used by C++ code,

View File

@ -33,9 +33,6 @@ int RunCommand(TCHAR *pszCmd, TCHAR *pszResultBuffer, int dwResultBufferSize) {
if (pszResultBuffer && dwResultBufferSize > 0) {
memset(pszResultBuffer, 0, dwResultBufferSize);
} else {
WinExec(pszCmd, SW_HIDE);
return ERR_SUCCESS;
}
memset(&si, 0, sizeof(STARTUPINFO));
@ -64,15 +61,18 @@ int RunCommand(TCHAR *pszCmd, TCHAR *pszResultBuffer, int dwResultBufferSize) {
bRet = ::CreateProcess(nullptr, (pszCmd), nullptr, nullptr, TRUE, 0, nullptr, nullptr, &si, &pi);
if (FALSE == bRet) {
SPDLOG_ERROR(TEXT("CreateProcess Error"));
return -ERR_CREATE_PROCESS;
}
// 等待命令执行结束
::WaitForSingleObject(pi.hThread, INFINITE);
::WaitForSingleObject(pi.hProcess, INFINITE);
// 从匿名管道中读取结果到输出缓冲区
::RtlZeroMemory(pszResultBuffer, dwResultBufferSize);
::ReadFile(hReadPipe, pszResultBuffer, dwResultBufferSize, nullptr, nullptr);
if (pszResultBuffer) {
// 从匿名管道中读取结果到输出缓冲区
::RtlZeroMemory(pszResultBuffer, dwResultBufferSize);
::ReadFile(hReadPipe, pszResultBuffer, dwResultBufferSize, nullptr, nullptr);
}
// 关闭句柄, 释放内存
::CloseHandle(pi.hThread);
@ -84,3 +84,41 @@ int RunCommand(TCHAR *pszCmd, TCHAR *pszResultBuffer, int dwResultBufferSize) {
//pszResultBuffer[dwResultBufferSize - 1] = 0;
return ERR_SUCCESS;
}
void ShowWindowsErrorMessage(const TCHAR *pMsgHead) {
LPVOID buf;
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_MAX_WIDTH_MASK,
nullptr,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&buf,
0,
nullptr)) {
SPDLOG_ERROR("{0} Error({1}): {2}", pMsgHead, GetLastError(), buf);
LocalFree(buf);
} else {
SPDLOG_ERROR("{0} Unknown Error{1}.", pMsgHead, GetLastError());
}
}
void StringReplaceAll(TCHAR *pOrigin, const TCHAR *pOldStr, const TCHAR *pNewStr) {
using namespace std;
const int maxSize = lstrlen(pOrigin);
string src = string(pOrigin);
for (string::size_type pos(0); pos != string::npos; pos += lstrlen(pNewStr)) {
if ((pos = src.find(pOldStr, pos)) != string::npos) {
src.replace(pos, lstrlen(pOldStr), pNewStr);
} else {
break;
}
}
memset(pOrigin, 0, maxSize);
StringCbCopyA(pOrigin, maxSize, src.c_str());
}
void StringRemoveAll(TCHAR *pOrigin, const TCHAR *pString) {
StringReplaceAll(pOrigin, pString, "");
}

View File

@ -10,6 +10,23 @@ extern "C" {
void RemoveTailLineBreak(TCHAR *pInputStr, int strSize);
int RunCommand(TCHAR *pszCmd, TCHAR *pszResultBuffer, int dwResultBufferSize);
/**
* @brief IPv4 CIDR
* @param[in] pNetMask IPv4
* @return IPv4 CIDR
*/
int __cdecl NetmaskToCIDR(const TCHAR *pNetMask);
/**
* @brief CIDR IPv4
* @param[in] cidr CIDR
* @return CIDR
*/
const TCHAR *CIDRToNetmask(const UINT8 cidr);
void ShowWindowsErrorMessage(const TCHAR *pMsgHead);
void StringReplaceAll(TCHAR *pOrigin, const TCHAR *pOldStr, const TCHAR *pNewStr);
void StringRemoveAll(TCHAR *pOrigin, const TCHAR *pString);
#ifdef __cplusplus
}
#endif

View File

@ -7,6 +7,7 @@
#include <iostream>
#include "globalcfg.h"
#include "misc.h"
#pragma comment(lib, "Iphlpapi.lib")
@ -30,6 +31,243 @@ int NetmaskToCIDR(const TCHAR *pNetMask) {
return 0xFF;
}
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];
}
return TEXT("");
}
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:")};
if (pInterfaceName == nullptr || lstrlen(pInterfaceName) == 0) {
SPDLOG_ERROR("Input pInterfaceName params error: {0}", pInterfaceName);
return -ERR_INPUT_PARAMS;
}
if (pIndex == nullptr) {
SPDLOG_ERROR("Input pIndex params error");
return -ERR_INPUT_PARAMS;
}
if (FAILED(
StringCbPrintf(cmdBuf,
MAX_PATH,
"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)) != ERR_SUCCESS) {
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret);
return -ERR_CALL_SHELL;
}
SPDLOG_DEBUG("Run command [{0}] resutl \'{1}\'", cmdBuf, cmdResult);
// 移除
StringRemoveAll(cmdResult, TEXT("\r\n"));
StringRemoveAll(cmdResult, TEXT(" "));
StringRemoveAll(cmdResult, TEXT("InterfaceIndex:"));
*pIndex = strtol(cmdResult, nullptr, 10);
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 RemoveInterfaceIpAddress(const TCHAR *pInterfaceName) {
int ret;
TCHAR cmdBuf[MAX_PATH];
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)) != ERR_SUCCESS) {
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret);
return -ERR_CALL_SHELL;
}
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}";
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;
}
//MultiByteToWideChar(CP_UTF8, 0, pInterfaceName, lstrlen(pInterfaceName), pIfName, MAX_PATH / sizeof(WCHAR));
if (GetAdapterIndex(pIfName, &index) != NO_ERROR) {
ShowWindowsErrorMessage("GetAdapterIndex");
return -ERR_SYS_CALL;
}
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;
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;
}
StringCbCopy(cidrMask, 4, token);
int cidr = atoi(cidrMask);
netmask = CIDRToNetmask(cidr);
SPDLOG_DEBUG("CIDR IpAddress convert:{0} --> {1}/{2}", pCidrIpaddr, ip, netmask);
return SetInterfaceIpAddress(pInterfaceName, ip, netmask);
}
int GetAllNICInfo(PNIC_CONTENT pInfo, int *pItemCounts) {
PIP_ADAPTER_INFO pAdapterInfo;
DWORD dwRetVal = 0;
if (pItemCounts == nullptr || pInfo == nullptr) {
SPDLOG_ERROR("Input params error: {0}");
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("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("Error allocating memory needed to call GetAdaptersinfo\n");
return -ERR_MALLOC_MEMORY;
}
}
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
UINT32 id = 0;
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
while (pAdapter && id < NET_CARD_MAX) {
// 网卡索引
pInfo[id].InterfaceIndex = pAdapter->Index;
// 网卡名称
StringCbCopy(pInfo[id].NetCardUUID, MAX_ADAPTER_NAME_LENGTH, pAdapter->AdapterName);
// 网卡详细描述
StringCbCopy(pInfo[id].NetCardDescription, MAX_ADAPTER_DESCRIPTION_LENGTH, pAdapter->Description);
// 网卡 IP 地址
StringCbCopy(pInfo[id].NetCardIpaddr, MAX_IP_LEN - 1, pAdapter->IpAddressList.IpAddress.String);
// 网卡子网掩码
StringCbCopy(pInfo[id].NetCardNetmask, MAX_IP_LEN - 1, pAdapter->IpAddressList.IpMask.String);
// 网卡网关地址
StringCbCopy(pInfo[id].NetCardGateway, MAX_IP_LEN - 1, pAdapter->GatewayList.IpAddress.String);
// 网卡 MAC 地址
StringCbPrintf(pInfo[id].NetCardMacAddr,
20 - 1,
"%02X:%02X:%02X:%02X:%02X:%02X",
pAdapter->Address[0],
pAdapter->Address[1],
pAdapter->Address[2],
pAdapter->Address[3],
pAdapter->Address[4],
pAdapter->Address[5]);
id++;
pAdapter = pAdapter->Next;
}
*pItemCounts = id;
} else {
SPDLOG_ERROR("GetAdaptersInfo failed with error: {0}\n", dwRetVal);
return -ERR_SYS_CALL;
}
if (pAdapterInfo) {
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
}
return ERR_SUCCESS;
}
#if 0
int GetAllNICInfo(PNIC_CONTENT pInfo, int *pItemCounts) {
int nRel, id = 0;
IP_ADDR_STRING *pIpAddrString;
@ -54,14 +292,15 @@ int GetAllNICInfo(PNIC_CONTENT pInfo, int *pItemCounts) {
PIP_ADAPTER_INFO cur = pIpAdapterInfo;
while (cur) {
StringCbCopy(pInfo[id].NetCardName, MAX_ADAPTER_NAME_LENGTH, cur->AdapterName);
pInfo[id].InterfaceIndex = cur->Index;
StringCbCopy(pInfo[id].NetCardUUID, MAX_ADAPTER_NAME_LENGTH, cur->AdapterName);
StringCbCopy(pInfo[id].NetCardDescription, MAX_ADAPTER_DESCRIPTION_LENGTH, cur->Description);
switch (cur->Type) {
case MIB_IF_TYPE_ETHERNET:
pIpAddrString = &(cur->IpAddressList);
StringCbCopy(pInfo[id].NetCardIpaddr, 20 - 1, pIpAddrString->IpAddress.String);
StringCbCopy(pInfo[id].NetCardNetmask, 20 - 1, pIpAddrString->IpMask.String);
StringCbCopy(pInfo[id].NetCardIpaddr, MAX_IP_LEN - 1, pIpAddrString->IpAddress.String);
StringCbCopy(pInfo[id].NetCardNetmask, MAX_IP_LEN - 1, pIpAddrString->IpMask.String);
break;
case MIB_IF_TYPE_OTHER:
case MIB_IF_TYPE_TOKENRING:
@ -72,8 +311,8 @@ int GetAllNICInfo(PNIC_CONTENT pInfo, int *pItemCounts) {
break;
default: // WIFI ,Unknown type
pIpAddrString = &(cur->IpAddressList);
StringCbCopy(pInfo[id].NetCardIpaddr, 20 - 1, pIpAddrString->IpAddress.String);
StringCbCopy(pInfo[id].NetCardNetmask, 20 - 1, pIpAddrString->IpMask.String);
StringCbCopy(pInfo[id].NetCardIpaddr, MAX_IP_LEN - 1, pIpAddrString->IpAddress.String);
StringCbCopy(pInfo[id].NetCardNetmask, MAX_IP_LEN - 1, pIpAddrString->IpMask.String);
break;
}
@ -97,3 +336,4 @@ int GetAllNICInfo(PNIC_CONTENT pInfo, int *pItemCounts) {
return ERR_SUCCESS;
}
#endif

View File

@ -18,6 +18,11 @@ constexpr auto WG_KEY_MAX = (64);
*/
#define NET_CARD_MAX (32)
/**
* @brief IP (IPv6)
*/
#define MAX_IP_LEN (48)
/**
* @brief
*
@ -50,11 +55,13 @@ enum LOG_LEVEL {
* @brief
*/
typedef struct {
TCHAR NetCardName[260]; ///< 网卡名称, Windows标识为 UUID
TCHAR NetCardDescription[132]; ///< 网卡描述
TCHAR NetCardIpaddr[20]; ///< 网卡 IP 地址
TCHAR NetCardNetmask[20]; ///< 网卡子网掩码
TCHAR NetCardMacAddr[20]; ///< 网卡 MAC 地址
int InterfaceIndex; ///< 网卡索引
TCHAR NetCardUUID[260]; ///< 网卡名称, Windows标识为 UUID
TCHAR NetCardDescription[132]; ///< 网卡描述
TCHAR NetCardIpaddr[MAX_IP_LEN]; ///< 网卡 IP 地址
TCHAR NetCardNetmask[MAX_IP_LEN]; ///< 网卡子网掩码
TCHAR NetCardGateway[MAX_IP_LEN]; ///< 网卡网关
TCHAR NetCardMacAddr[20]; ///< 网卡 MAC 地址
} NIC_CONTENT, *PNIC_CONTENT;
/**
@ -206,11 +213,31 @@ TUNNEL_API void __cdecl TunnelSDKUnInit();
TUNNEL_API int __cdecl GetAllNICInfo(PNIC_CONTENT pInfo, int *pItemCounts);
/**
* @brief IPv4 CIDR
* @param pNetMask IPv4
* @return IPv4 CIDR
* @brief
* @param pInterfaceName
* @param pIndex
* @return 0: 0 @see USER_ERRNO
* - -ERR_INPUT_PARAMS
* - -ERR_MALLOC_MEMORY
* - -ERR_MEMORY_STR
* - -ERR_CALL_SHELL
* - ERR_SUCCESS
*/
TUNNEL_API int __cdecl NetmaskToCIDR(const TCHAR *pNetMask);
TUNNEL_API int __cdecl GetInterfaceIndexByName(const TCHAR *pInterfaceName, int *pIndex);
/**
* @brief
* @param pInterfaceName
* @return 0: 0 @see USER_ERRNO
* - -ERR_INPUT_PARAMS
* - -ERR_MALLOC_MEMORY
* - -ERR_MEMORY_STR
* - -ERR_CALL_SHELL
* - ERR_SUCCESS
*/
TUNNEL_API int __cdecl RemoveInterfaceIpAddress(const TCHAR *pInterfaceName);
TUNNEL_API int __cdecl SetInterfaceIpAddressFromCIDR(const TCHAR *pInterfaceName, const TCHAR *pCidrIpaddr);
#ifdef __cplusplus
}
#endif

View File

@ -1,24 +1,26 @@
#pragma once
/**
* @brief SDK
*/
enum USER_ERRNO {
ERR_SUCCESS, ///< 成功
ERR_INPUT_PARAMS, ///< 输入参数错误
ERR_UN_SUPPORT, ///< 不支持的操作
ERR_CALL_SHELL, ///< 调用Shell命令失败
ERR_ITEM_EXISTS, ///< 该内容已经存在
ERR_ITEM_UNEXISTS, ///< 该内容不存在
ERR_SYS_INIT, ///< 系统中断
ERR_SYS_CALL, ///< 系统调用
ERR_OPEN_FILE, ///< 打开文件失败
ERR_READ_FILE, ///< 读取文件失败
ERR_WRITE_FILE, ///< 写入文件失败
ERR_FILE_NOT_EXISTS, ///< 文件不存在
ERR_FILE_LOCKED, ///< 文件被锁定
ERR_GET_FILE_SIZE, ///< 获取文件大小失败
ERR_COPY_FILE, ///< 复制文件失败
ERR_MALLOC_MEMORY, ///< 分配内存失败
ERR_MMAP_MEMORY, ///< 共享内存失败
ERR_MEMORY_STR ///< 字符串操作失败
ERR_SUCCESS, ///< 成功
ERR_INPUT_PARAMS, ///< 输入参数错误
ERR_UN_SUPPORT, ///< 不支持的操作
ERR_CALL_SHELL, ///< 调用Shell命令失败
ERR_ITEM_EXISTS, ///< 该内容已经存在
ERR_ITEM_UNEXISTS, ///< 该内容不存在
ERR_SYS_INIT, ///< 系统中断
ERR_SYS_CALL, ///< 系统调用
ERR_OPEN_FILE, ///< 打开文件失败
ERR_READ_FILE, ///< 读取文件失败
ERR_WRITE_FILE, ///< 写入文件失败
ERR_FILE_NOT_EXISTS, ///< 文件不存在
ERR_FILE_LOCKED, ///< 文件被锁定
ERR_GET_FILE_SIZE, ///< 获取文件大小失败
ERR_COPY_FILE, ///< 复制文件失败
ERR_MALLOC_MEMORY, ///< 分配内存失败
ERR_MMAP_MEMORY, ///< 共享内存失败
ERR_MEMORY_STR, ///< 字符串操作失败
ERR_CREATE_PROCESS ///< 创建进程失败
};

View File

@ -14,8 +14,7 @@ constexpr auto WINENVBUF_SIZE = (4096);
#define CFG_WIREGUARD_SECTION TEXT("WireGuard")
#define CFG_WIREGUARD_PATH TEXT("WireGuardExe")
#define CFG_WGCLI_PATH TEXT("ClientConfig")
#define CFG_WGSVR_PATH TEXT("ServerConfig")
#define CFG_WGCFG_PATH TEXT("WgCfgPath")
#define CFG_WG_PATH TEXT("WgExe")
int WireGuardInstallServerService(bool bInstall) {
@ -23,7 +22,7 @@ int WireGuardInstallServerService(bool bInstall) {
TCHAR cmdBuf[MAX_PATH];
GetPrivateProfileString(CFG_WIREGUARD_SECTION,
CFG_WGSVR_PATH,
CFG_WGCFG_PATH,
TEXT(""),
cfgVal,
MAX_PATH,
@ -71,7 +70,7 @@ int WireGuardInstallServerService(bool bInstall) {
return -ERR_FILE_NOT_EXISTS;
}
} else {
SPDLOG_ERROR("Configure [{0}] not found", CFG_WGSVR_PATH);
SPDLOG_ERROR("Configure [{0}] not found", CFG_WGCFG_PATH);
return -ERR_ITEM_UNEXISTS;
}
}
@ -148,7 +147,7 @@ int WireGuardCreateClientConfig(const PWGCLIENT_CONFIG pWgConfig) {
}
// 保存到配置文件中
WritePrivateProfileString(CFG_WIREGUARD_SECTION, CFG_WGCLI_PATH, cfgPath, GetGlobalCfgInfo()->cfgPath);
WritePrivateProfileString(CFG_WIREGUARD_SECTION, CFG_WGCFG_PATH, cfgPath, GetGlobalCfgInfo()->cfgPath);
if (FAILED(StringCbPrintf(pBuf,
bufSize,
@ -258,7 +257,7 @@ int WireGuardCreateServerConfig(const PWGSERVER_CONFIG pWgConfig) {
return -ERR_OPEN_FILE;
}
WritePrivateProfileString(CFG_WIREGUARD_SECTION, CFG_WGSVR_PATH, cfgPath, GetGlobalCfgInfo()->cfgPath);
WritePrivateProfileString(CFG_WIREGUARD_SECTION, CFG_WGCFG_PATH, cfgPath, GetGlobalCfgInfo()->cfgPath);
if (FAILED(StringCbPrintf(pBuf,
bufSize,
@ -297,6 +296,9 @@ int WireGuardCreateServerConfig(const PWGSERVER_CONFIG pWgConfig) {
}
::CloseHandle(hFile);
StringCbCopy(GetGlobalCfgInfo()->wgServerCfg.wgName, 260, pWgConfig->Name);
StringCbCopy(GetGlobalCfgInfo()->wgServerCfg.wgIpaddr, MAX_IP_LEN, pWgConfig->Address);
return ERR_SUCCESS;
}

View File

@ -6,42 +6,34 @@
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace TestNetTunnelSDK
{
TEST_MODULE_INITIALIZE(ModuleInitialize)
{
Logger::WriteMessage("In Module Initialize");
TunnelSDKInitEnv(TEXT(
"C:\\Users\\HuangXin\\Documents\\development\\visual_studio\\tunnel_windows\\NetTunnelApp\\bin\\Debug"));
InitTunnelSDKLog(
TEXT(
"C:\\Users\\HuangXin\\Documents\\development\\visual_studio\\tunnel_windows\\NetTunnelApp\\bin\\Debug\\utest.log"),
LOG_DEBUG);
namespace TestNetTunnelSDK {
TEST_MODULE_INITIALIZE(ModuleInitialize) {
Logger::WriteMessage("In Module Initialize");
TunnelSDKInitEnv(
TEXT("C:\\Users\\HuangXin\\Documents\\development\\visual_studio\\tunnel_windows\\NetTunnelApp\\bin\\Debug"));
InitTunnelSDKLog(TEXT("C:\\Users\\HuangXin\\Documents\\development\\visual_studio\\tunnel_"
"windows\\NetTunnelApp\\bin\\Debug\\utest.log"),
LOG_DEBUG);
}
TEST_MODULE_CLEANUP(ModuleCleanup) {
Logger::WriteMessage("In Module Cleanup");
TunnelSDKUnInit();
}
TEST_CLASS(TestNetTunnelSDK) {
public:
const int RET_OK = ERR_SUCCESS;
TEST_METHOD(TestFindWireguardExe) {
Assert::AreEqual(RET_OK, FindWireguardExe(nullptr, 0));
}
TEST_MODULE_CLEANUP(ModuleCleanup)
{
Logger::WriteMessage("In Module Cleanup");
TunnelSDKUnInit();
TEST_METHOD(TestSetWireguardPath) {
Assert::AreEqual(RET_OK, SetWireguardPath(TEXT("C:\\Program Files\\WireGuard\\wireguard.exe")));
}
TEST_CLASS(TestNetTunnelSDK)
{
public:
TEST_METHOD(TestFindWireguardExe)
{
int ret = ERR_SUCCESS;
Assert::AreEqual(ret, FindWireguardExe(nullptr, 0));
}
TEST_METHOD(TestSetWireguardPath)
{
int ret = ERR_SUCCESS;
Assert::AreEqual(ret, SetWireguardPath(TEXT("C:\\Program Files\\WireGuard\\wireguard.exe")));
}
TEST_METHOD(TestRunPipeCmd)
{
TEST_METHOD(TestRunPipeCmd) {
#if 0
TCHAR buf[1024];
int ret = ERR_SUCCESS;
@ -50,18 +42,32 @@ namespace TestNetTunnelSDK
Logger::WriteMessage("Return:");
Logger::WriteMessage(buf);
#endif
}
}
TEST_METHOD(TestGetAllNICInfo)
{
PNIC_CONTENT pInfo = (PNIC_CONTENT)malloc(sizeof(NIC_CONTENT) * 32);
TEST_METHOD(TestGetAllNICInfo) {
PNIC_CONTENT pInfo = (PNIC_CONTENT)malloc(sizeof(NIC_CONTENT) * 32);
int size = 0;
int ret = ERR_SUCCESS;
int size = 0;
Assert::AreEqual(ret, GetAllNICInfo(pInfo, &size));
Assert::AreEqual(RET_OK, GetAllNICInfo(pInfo, &size));
free(pInfo);
}
};
free(pInfo);
}
TEST_METHOD(TestSetInterfaceIpAddressFromCIDR) {
Assert::AreEqual(
RET_OK,
SetInterfaceIpAddressFromCIDR(TEXT("{7D92693A-0F1E-0212-176D-31226AF3E147}"), TEXT("192.168.100.250/24")));
}
TEST_METHOD(TestGetInterfaceIndexByName) {
int index;
Assert::AreEqual(RET_OK, GetInterfaceIndexByName(TEXT("wg_server"), &index));
}
TEST_METHOD(TestRemoveInterfaceIpAddress) {
int index;
Assert::AreEqual(RET_OK, RemoveInterfaceIpAddress(TEXT("wg_server")));
}
};
}

View File

@ -1,3 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/Highlighting/AnalysisEnabled/@EntryValue">SOLUTION</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/SweaWarningsMode/@EntryValue">ShowAndRun</s:String></wpf:ResourceDictionary>
</wpf:ResourceDictionary>