OCT 1. 增加 WireGuard 服务安装接口

This commit is contained in:
黄昕 2023-06-20 18:23:31 +08:00
parent 098d9c2c4e
commit b13b13f4d4
7 changed files with 110 additions and 13 deletions

View File

@ -90,17 +90,31 @@ public partial class MainForm : Form
Console.WriteLine("Create WireGuard Client Configure File: {0}", v); Console.WriteLine("Create WireGuard Client Configure File: {0}", v);
} }
private void test_uninstall_wg_server()
{
var v = NetTunnelLib.WireGuardInstallServerService(false);
Console.WriteLine("Uninstall Result: {0}", v);
}
private void test_install_wg_server()
{
var v = NetTunnelLib.WireGuardInstallServerService(true);
Console.WriteLine("Install Result: {0}", v);
}
private void button1_Click(object sender, EventArgs e) private void button1_Click(object sender, EventArgs e)
{ {
var buffer = new StringBuilder(256 + 1); //var buffer = new StringBuilder(256 + 1);
//NetTunnelLib.TunnelSDKInitEnv(); //NetTunnelLib.TunnelSDKInitEnv();
var v = NetTunnelLib.FindWireguardExe(buffer, 256); //var v = NetTunnelLib.FindWireguardExe(buffer, 256);
MessageBox.Show(buffer.ToString() + "----" + v.ToString()); //MessageBox.Show(buffer.ToString() + "----" + v.ToString());
test_install_wg_server();
} }
private void button2_Click(object sender, EventArgs e) private void button2_Click(object sender, EventArgs e)
{ {
test_create_wg_svr_cfg();
test_create_wg_cli_cfg(); test_uninstall_wg_server();
} }
} }

View File

@ -129,6 +129,9 @@ public class NetTunnelLib
[DllImport("NetTunnelSDK.dll", CallingConvention = CallingConvention.Cdecl)] [DllImport("NetTunnelSDK.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int WireGuardCreateClientConfig(WgCliConfig cfg); public static extern int WireGuardCreateClientConfig(WgCliConfig cfg);
[DllImport("NetTunnelSDK.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int WireGuardInstallServerService(bool install);
//[DllImport("NetTunnelSDK.dll", CallingConvention = CallingConvention.Cdecl)] //[DllImport("NetTunnelSDK.dll", CallingConvention = CallingConvention.Cdecl)]
//public static extern int RunPipeCmd(String pszCmd, StringBuilder pszResultBuffer, int dwResultBufferSize); //public static extern int RunPipeCmd(String pszCmd, StringBuilder pszResultBuffer, int dwResultBufferSize);
} }

View File

@ -18,7 +18,7 @@ void RemoveTailLineBreak(TCHAR *pInputStr, int strSize) {
} }
} }
int RunPipeCmd(TCHAR *pszCmd, TCHAR *pszResultBuffer, int dwResultBufferSize) { int RunCommand(TCHAR *pszCmd, TCHAR *pszResultBuffer, int dwResultBufferSize) {
BOOL bRet; BOOL bRet;
HANDLE hReadPipe = nullptr; HANDLE hReadPipe = nullptr;
HANDLE hWritePipe = nullptr; HANDLE hWritePipe = nullptr;
@ -26,6 +26,18 @@ int RunPipeCmd(TCHAR *pszCmd, TCHAR *pszResultBuffer, int dwResultBufferSize) {
PROCESS_INFORMATION pi; PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES securityAttributes = {}; SECURITY_ATTRIBUTES securityAttributes = {};
if (pszCmd == nullptr) {
SPDLOG_ERROR(TEXT("Input params Error: [{0}]"), pszCmd);
return -ERR_INPUT_PARAMS;
}
if (pszResultBuffer && dwResultBufferSize > 0) {
memset(pszResultBuffer, 0, dwResultBufferSize);
} else {
WinExec(pszCmd, SW_HIDE);
return ERR_SUCCESS;
}
memset(&si, 0, sizeof(STARTUPINFO)); memset(&si, 0, sizeof(STARTUPINFO));
memset(&pi, 0, sizeof(PROCESS_INFORMATION)); memset(&pi, 0, sizeof(PROCESS_INFORMATION));

View File

@ -7,8 +7,8 @@ extern "C" {
// we need to export the C interface // we need to export the C interface
#endif #endif
void RemoveTailLineBreak(TCHAR* pInputStr, int strSize); void RemoveTailLineBreak(TCHAR *pInputStr, int strSize);
int RunPipeCmd(TCHAR* pszCmd, TCHAR* pszResultBuffer, int dwResultBufferSize); int RunCommand(TCHAR *pszCmd, TCHAR *pszResultBuffer, int dwResultBufferSize);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -183,6 +183,13 @@ TUNNEL_API int __cdecl WireGuardCreateServerConfig(const PWGSERVER_CONFIG pWgCon
*/ */
TUNNEL_API int __cdecl WireGuardCreateClientConfig(const PWGCLIENT_CONFIG pWgConfig); TUNNEL_API int __cdecl WireGuardCreateClientConfig(const PWGCLIENT_CONFIG pWgConfig);
/**
* @brief / WireGuard
* @param[in] bInstall TRUE , FALSE
* @return 0: 0 @see USER_ERRNO
*/
TUNNEL_API int __cdecl WireGuardInstallServerService(bool bInstall);
/** /**
* @brief SDK * @brief SDK
*/ */

View File

@ -3,10 +3,13 @@
#include "usrerr.h" #include "usrerr.h"
#include <strsafe.h> #include <strsafe.h>
#include <tchar.h> #include <tchar.h>
#include <shlwapi.h>
#include "globalcfg.h" #include "globalcfg.h"
#include "misc.h" #include "misc.h"
#pragma comment(lib, "Shlwapi.lib")
constexpr auto WINENVBUF_SIZE = (4096); constexpr auto WINENVBUF_SIZE = (4096);
#define CFG_WIREGUARD_SECTION TEXT("WireGuard") #define CFG_WIREGUARD_SECTION TEXT("WireGuard")
@ -15,6 +18,64 @@ constexpr auto WINENVBUF_SIZE = (4096);
#define CFG_WGSVR_PATH TEXT("ServerConfig") #define CFG_WGSVR_PATH TEXT("ServerConfig")
#define CFG_WG_PATH TEXT("WgExe") #define CFG_WG_PATH TEXT("WgExe")
int WireGuardInstallServerService(bool bInstall) {
TCHAR cfgVal[MAX_PATH];
TCHAR cmdBuf[MAX_PATH];
GetPrivateProfileString(CFG_WIREGUARD_SECTION,
CFG_WGSVR_PATH,
TEXT(""),
cfgVal,
MAX_PATH,
GetGlobalCfgInfo()->cfgPath);
if (lstrlen(cfgVal) > 0) {
WIN32_FIND_DATA FindFileData;
const HANDLE hFind = FindFirstFile(cfgVal, &FindFileData);
if (hFind != INVALID_HANDLE_VALUE) {
int ret;
if (bInstall) {
// 安装服务
StringCbPrintf(cmdBuf,
MAX_PATH,
TEXT("\"%s\" /installtunnelservice \"%s\""),
GetGlobalCfgInfo()->wireguardCfg.wireguardPath,
cfgVal);
} else {
// 卸载服务
TCHAR svrName[MAX_PATH];
StringCbCopy(svrName, MAX_PATH, cfgVal);
PathStripPath(svrName);
PathRemoveExtension(svrName);
StringCbPrintf(cmdBuf,
MAX_PATH,
TEXT("\"%s\" /uninstalltunnelservice %s"),
GetGlobalCfgInfo()->wireguardCfg.wireguardPath,
svrName);
}
if ((ret = RunCommand(cmdBuf, nullptr, 0)) != ERR_SUCCESS) {
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuf, ret);
return -ERR_CALL_SHELL;
}
SPDLOG_DEBUG("Run command [{0}]", cmdBuf);
return ERR_SUCCESS;
} else {
SPDLOG_ERROR("WireGuard configure file [{0}] not found", cfgVal);
return -ERR_FILE_NOT_EXISTS;
}
} else {
SPDLOG_ERROR("Configure [{0}] not found", CFG_WGSVR_PATH);
return -ERR_ITEM_UNEXISTS;
}
}
int WireGuardCreateClientConfig(const PWGCLIENT_CONFIG pWgConfig) { int WireGuardCreateClientConfig(const PWGCLIENT_CONFIG pWgConfig) {
const size_t bufSize = 4096 * sizeof(TCHAR); const size_t bufSize = 4096 * sizeof(TCHAR);
const TCHAR cfgFormat[] = TEXT( const TCHAR cfgFormat[] = TEXT(
@ -255,7 +316,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 = RunPipeCmd(cmdBuffer, cmdResult, MAX_PATH)) != ERR_SUCCESS) { if ((ret = RunCommand(cmdBuffer, cmdResult, MAX_PATH)) != 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;
} }
@ -271,7 +332,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 = RunPipeCmd(cmdBuffer, cmdResult, MAX_PATH)) != ERR_SUCCESS) { if ((ret = RunCommand(cmdBuffer, cmdResult, MAX_PATH)) != 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

@ -46,7 +46,7 @@ namespace TestNetTunnelSDK
TCHAR buf[1024]; TCHAR buf[1024];
int ret = ERR_SUCCESS; int ret = ERR_SUCCESS;
Assert::AreEqual(ret, RunPipeCmd(TEXT("cmd.exe /C dir E:\\"), buf, 1024)); Assert::AreEqual(ret, RunCommand(TEXT("cmd.exe /C dir E:\\"), buf, 1024));
Logger::WriteMessage("Return:"); Logger::WriteMessage("Return:");
Logger::WriteMessage(buf); Logger::WriteMessage(buf);
#endif #endif