2023-06-15 06:31:01 +00:00
|
|
|
|
#include "pch.h"
|
2023-06-19 11:08:42 +00:00
|
|
|
|
#include "tunnel.h"
|
|
|
|
|
#include "usrerr.h"
|
|
|
|
|
#include <strsafe.h>
|
|
|
|
|
#include <tchar.h>
|
2023-06-20 10:23:31 +00:00
|
|
|
|
#include <shlwapi.h>
|
2023-06-19 11:08:42 +00:00
|
|
|
|
|
|
|
|
|
#include "globalcfg.h"
|
|
|
|
|
#include "misc.h"
|
|
|
|
|
|
2023-06-20 10:23:31 +00:00
|
|
|
|
#pragma comment(lib, "Shlwapi.lib")
|
|
|
|
|
|
2023-06-20 09:25:14 +00:00
|
|
|
|
constexpr auto WINENVBUF_SIZE = (4096);
|
|
|
|
|
|
|
|
|
|
#define CFG_WIREGUARD_SECTION TEXT("WireGuard")
|
|
|
|
|
#define CFG_WIREGUARD_PATH TEXT("WireGuardExe")
|
2023-06-21 10:04:16 +00:00
|
|
|
|
#define CFG_WGCFG_PATH TEXT("WgCfgPath")
|
2023-06-20 09:25:14 +00:00
|
|
|
|
#define CFG_WG_PATH TEXT("WgExe")
|
|
|
|
|
|
2023-06-20 10:23:31 +00:00
|
|
|
|
int WireGuardInstallServerService(bool bInstall) {
|
|
|
|
|
TCHAR cfgVal[MAX_PATH];
|
|
|
|
|
TCHAR cmdBuf[MAX_PATH];
|
|
|
|
|
|
|
|
|
|
GetPrivateProfileString(CFG_WIREGUARD_SECTION,
|
2023-06-21 10:04:16 +00:00
|
|
|
|
CFG_WGCFG_PATH,
|
2023-06-20 10:23:31 +00:00
|
|
|
|
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 {
|
2023-06-21 10:04:16 +00:00
|
|
|
|
SPDLOG_ERROR("Configure [{0}] not found", CFG_WGCFG_PATH);
|
2023-06-20 10:23:31 +00:00
|
|
|
|
return -ERR_ITEM_UNEXISTS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 09:25:14 +00:00
|
|
|
|
int WireGuardCreateClientConfig(const PWGCLIENT_CONFIG pWgConfig) {
|
|
|
|
|
const size_t bufSize = 4096 * sizeof(TCHAR);
|
|
|
|
|
const TCHAR cfgFormat[] = TEXT(
|
|
|
|
|
"[Interface]\nPrivateKey = %s\nAddress = %s\n\n[Peer]\nPublicKey = %s\nAllowedIPs = %s\nEndpoint = "
|
|
|
|
|
"%s\nPersistentKeepalive = 30\n");
|
|
|
|
|
TCHAR cfgPath[MAX_PATH];
|
|
|
|
|
size_t length;
|
|
|
|
|
HANDLE hFile;
|
|
|
|
|
TCHAR *pBuf;
|
|
|
|
|
|
|
|
|
|
#pragma region
|
|
|
|
|
if (pWgConfig == nullptr) {
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbLength(pWgConfig->Name, 64, &length)) || 0 == length) {
|
|
|
|
|
SPDLOG_ERROR("WireGuard Name error: {0}", pWgConfig->Name);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbLength(pWgConfig->Address, 32, &length)) || 0 == length) {
|
|
|
|
|
SPDLOG_ERROR("WireGuard Address error: {0}", pWgConfig->Address);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbLength(pWgConfig->PrivateKey, 64, &length)) || 0 == length) {
|
|
|
|
|
SPDLOG_ERROR("WireGuard Private key error: {0}", pWgConfig->PrivateKey);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbLength(pWgConfig->SvrPubKey, 64, &length)) || 0 == length) {
|
|
|
|
|
SPDLOG_ERROR("WireGuard Server Public key error: {0}", pWgConfig->SvrPubKey);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbLength(pWgConfig->AllowNet, 256, &length)) || 0 == length) {
|
|
|
|
|
SPDLOG_ERROR("WireGuard Allow Client Network error: {0}", pWgConfig->AllowNet);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbLength(pWgConfig->ServerURL, 256, &length)) || 0 == length) {
|
|
|
|
|
SPDLOG_ERROR("WireGuard Server Network error: {0}", pWgConfig->ServerURL);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
#pragma endregion 参数检查
|
|
|
|
|
|
|
|
|
|
pBuf = static_cast<TCHAR *>(malloc(bufSize));
|
|
|
|
|
|
|
|
|
|
if (pBuf == nullptr) {
|
|
|
|
|
SPDLOG_ERROR("Malloc {1} bytes memory error: {0}", GetLastError(), bufSize);
|
|
|
|
|
return -ERR_MALLOC_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(pBuf, 0, bufSize);
|
|
|
|
|
|
|
|
|
|
StringCbPrintf(cfgPath, MAX_PATH, "%s\\%s.conf", GetGlobalCfgInfo()->workDirectory, pWgConfig->Name);
|
|
|
|
|
|
|
|
|
|
hFile = CreateFile(cfgPath, // name of the write
|
|
|
|
|
GENERIC_WRITE | GENERIC_READ, // open for writing
|
|
|
|
|
FILE_SHARE_READ, // do not share
|
|
|
|
|
nullptr, // default security
|
|
|
|
|
CREATE_ALWAYS, // create new file only
|
|
|
|
|
FILE_ATTRIBUTE_NORMAL, // normal file
|
|
|
|
|
nullptr); // no attr. template
|
|
|
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE) {
|
|
|
|
|
SPDLOG_ERROR("CreatFile [{0}] error: {1}", cfgPath, GetLastError());
|
|
|
|
|
free(pBuf);
|
|
|
|
|
return -ERR_OPEN_FILE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 保存到配置文件中
|
2023-06-21 10:04:16 +00:00
|
|
|
|
WritePrivateProfileString(CFG_WIREGUARD_SECTION, CFG_WGCFG_PATH, cfgPath, GetGlobalCfgInfo()->cfgPath);
|
2023-06-20 09:25:14 +00:00
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbPrintf(pBuf,
|
|
|
|
|
bufSize,
|
|
|
|
|
cfgFormat,
|
|
|
|
|
pWgConfig->PrivateKey,
|
|
|
|
|
pWgConfig->Address,
|
|
|
|
|
pWgConfig->SvrPubKey,
|
|
|
|
|
pWgConfig->AllowNet,
|
|
|
|
|
pWgConfig->ServerURL))) {
|
|
|
|
|
SPDLOG_ERROR("Format string error: {0}", GetLastError());
|
|
|
|
|
free(pBuf);
|
|
|
|
|
::CloseHandle(hFile);
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbLength(pBuf, bufSize, &length))) {
|
|
|
|
|
SPDLOG_ERROR("Get string \'{0}\' length error: {1}", pBuf, GetLastError());
|
|
|
|
|
free(pBuf);
|
|
|
|
|
::CloseHandle(hFile);
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SPDLOG_DEBUG("WG Client Configure:\n{0}", pBuf);
|
|
|
|
|
|
|
|
|
|
if (!WriteFile(hFile, // open file handle
|
|
|
|
|
pBuf, // start of data to write
|
|
|
|
|
static_cast<DWORD>(length), // number of bytes to write
|
|
|
|
|
nullptr, // number of bytes that were written
|
|
|
|
|
nullptr)) {
|
|
|
|
|
SPDLOG_ERROR("WriteFile [{0}] error: {1}", cfgPath, GetLastError());
|
|
|
|
|
free(pBuf);
|
|
|
|
|
::CloseHandle(hFile);
|
|
|
|
|
return -ERR_OPEN_FILE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
::CloseHandle(hFile);
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int WireGuardCreateServerConfig(const PWGSERVER_CONFIG pWgConfig) {
|
|
|
|
|
const size_t bufSize = 4096 * sizeof(TCHAR);
|
|
|
|
|
const TCHAR cfgFormat[] = TEXT(
|
|
|
|
|
"[Interface]\nAddress = %s\nListenPort = %d\nPrivateKey = %s\n\n[Peer]\nPublicKey = %s\nAllowedIPs = %s\n");
|
|
|
|
|
TCHAR cfgPath[MAX_PATH];
|
|
|
|
|
size_t length;
|
|
|
|
|
HANDLE hFile;
|
|
|
|
|
TCHAR *pBuf;
|
|
|
|
|
|
|
|
|
|
#pragma region
|
|
|
|
|
if (pWgConfig == nullptr) {
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbLength(pWgConfig->Name, 64, &length)) || 0 == length) {
|
|
|
|
|
SPDLOG_ERROR("WireGuard Name error: {0}", pWgConfig->Name);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbLength(pWgConfig->Address, 32, &length)) || 0 == length) {
|
|
|
|
|
SPDLOG_ERROR("WireGuard Address error: {0}", pWgConfig->Address);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbLength(pWgConfig->PrivateKey, 64, &length)) || 0 == length) {
|
|
|
|
|
SPDLOG_ERROR("WireGuard Private key error: {0}", pWgConfig->PrivateKey);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pWgConfig->ListenPort <= 1024 || pWgConfig->ListenPort >= 65535) {
|
|
|
|
|
SPDLOG_ERROR("WireGuard Listen port error: {0}, should be in arrange (1024, 65535)", pWgConfig->ListenPort);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbLength(pWgConfig->CliPubKey, 64, &length)) || 0 == length) {
|
|
|
|
|
SPDLOG_ERROR("WireGuard Client Public key error: {0}", pWgConfig->CliPubKey);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbLength(pWgConfig->AllowNet, 256, &length)) || 0 == length) {
|
|
|
|
|
SPDLOG_ERROR("WireGuard Allow Client Network error: {0}", pWgConfig->AllowNet);
|
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
#pragma endregion 参数检查
|
|
|
|
|
|
|
|
|
|
pBuf = static_cast<TCHAR *>(malloc(bufSize));
|
|
|
|
|
|
|
|
|
|
if (pBuf == nullptr) {
|
|
|
|
|
SPDLOG_ERROR("Malloc {1} bytes memory error: {0}", GetLastError(), bufSize);
|
|
|
|
|
return -ERR_MALLOC_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(pBuf, 0, bufSize);
|
|
|
|
|
|
|
|
|
|
StringCbPrintf(cfgPath, MAX_PATH, "%s\\%s.conf", GetGlobalCfgInfo()->workDirectory, pWgConfig->Name);
|
|
|
|
|
|
|
|
|
|
hFile = CreateFile(cfgPath, // name of the write
|
|
|
|
|
GENERIC_WRITE | GENERIC_READ, // open for writing
|
|
|
|
|
FILE_SHARE_READ, // do not share
|
|
|
|
|
nullptr, // default security
|
|
|
|
|
CREATE_ALWAYS, // create new file only
|
|
|
|
|
FILE_ATTRIBUTE_NORMAL, // normal file
|
|
|
|
|
nullptr); // no attr. template
|
|
|
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE) {
|
|
|
|
|
SPDLOG_ERROR("CreatFile [{0}] error: {1}", cfgPath, GetLastError());
|
|
|
|
|
free(pBuf);
|
|
|
|
|
return -ERR_OPEN_FILE;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 10:04:16 +00:00
|
|
|
|
WritePrivateProfileString(CFG_WIREGUARD_SECTION, CFG_WGCFG_PATH, cfgPath, GetGlobalCfgInfo()->cfgPath);
|
2023-06-20 09:25:14 +00:00
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbPrintf(pBuf,
|
|
|
|
|
bufSize,
|
|
|
|
|
cfgFormat,
|
|
|
|
|
pWgConfig->Address,
|
|
|
|
|
pWgConfig->ListenPort,
|
|
|
|
|
pWgConfig->PrivateKey,
|
|
|
|
|
pWgConfig->CliPubKey,
|
|
|
|
|
pWgConfig->AllowNet))) {
|
|
|
|
|
SPDLOG_ERROR("Format string error: {0}", GetLastError());
|
|
|
|
|
free(pBuf);
|
|
|
|
|
::CloseHandle(hFile);
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FAILED(StringCbLength(pBuf, bufSize, &length))) {
|
|
|
|
|
SPDLOG_ERROR("Get string \'{0}\' length error: {1}", pBuf, GetLastError());
|
|
|
|
|
free(pBuf);
|
|
|
|
|
::CloseHandle(hFile);
|
|
|
|
|
return -ERR_MEMORY_STR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SPDLOG_DEBUG("WG Server Configure:\n{0}", pBuf);
|
|
|
|
|
|
|
|
|
|
if (FALSE ==
|
|
|
|
|
WriteFile(hFile, // open file handle
|
|
|
|
|
pBuf, // start of data to write
|
|
|
|
|
static_cast<DWORD>(length), // number of bytes to write
|
|
|
|
|
nullptr, // number of bytes that were written
|
|
|
|
|
nullptr)) // no overlapped structure)
|
|
|
|
|
{
|
|
|
|
|
SPDLOG_ERROR("WriteFile [{0}] error: {1}", cfgPath, GetLastError());
|
|
|
|
|
free(pBuf);
|
|
|
|
|
::CloseHandle(hFile);
|
|
|
|
|
return -ERR_OPEN_FILE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
::CloseHandle(hFile);
|
2023-06-21 10:04:16 +00:00
|
|
|
|
|
|
|
|
|
StringCbCopy(GetGlobalCfgInfo()->wgServerCfg.wgName, 260, pWgConfig->Name);
|
|
|
|
|
StringCbCopy(GetGlobalCfgInfo()->wgServerCfg.wgIpaddr, MAX_IP_LEN, pWgConfig->Address);
|
2023-06-20 09:25:14 +00:00
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int GenerateWireguardKeyPairs(TCHAR *pPubKey, int pubkeySize, TCHAR *pPrivKey, int privKeySize) {
|
|
|
|
|
int ret;
|
|
|
|
|
TCHAR cmdBuffer[MAX_PATH];
|
|
|
|
|
TCHAR cmdResult[MAX_PATH];
|
2023-06-19 11:08:42 +00:00
|
|
|
|
PSDK_CONFIG pCfg = GetGlobalCfgInfo();
|
|
|
|
|
|
|
|
|
|
// WireGuard 不存在或者未配置目录
|
2023-06-20 09:25:14 +00:00
|
|
|
|
if (!pCfg->wireguardCfg.wgExists || !pCfg->wireguardCfg.wireguardExists) {
|
2023-06-19 11:08:42 +00:00
|
|
|
|
return -ERR_ITEM_UNEXISTS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memset(cmdBuffer, 0, MAX_PATH);
|
|
|
|
|
memset(cmdResult, 0, MAX_PATH);
|
|
|
|
|
|
|
|
|
|
StringCbPrintf(cmdBuffer, MAX_PATH, TEXT("cmd.exe /C \"%s\" genkey"), pCfg->wireguardCfg.wgPath);
|
|
|
|
|
|
2023-06-20 10:23:31 +00:00
|
|
|
|
if ((ret = RunCommand(cmdBuffer, cmdResult, MAX_PATH)) != ERR_SUCCESS) {
|
2023-06-19 11:08:42 +00:00
|
|
|
|
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuffer, ret);
|
|
|
|
|
return -ERR_CALL_SHELL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SPDLOG_DEBUG("Run command [{0}] resutl \'{1}\'", cmdBuffer, cmdResult);
|
|
|
|
|
|
|
|
|
|
StringCbCopy(pPrivKey, privKeySize, cmdResult);
|
|
|
|
|
memset(cmdBuffer, 0, MAX_PATH);
|
2023-06-20 09:25:14 +00:00
|
|
|
|
StringCbPrintf(cmdBuffer,
|
|
|
|
|
MAX_PATH,
|
|
|
|
|
TEXT("cmd.exe /C echo %s | \"%s\" pubkey"),
|
|
|
|
|
cmdResult,
|
2023-06-19 11:08:42 +00:00
|
|
|
|
pCfg->wireguardCfg.wgPath);
|
|
|
|
|
|
|
|
|
|
memset(cmdResult, 0, MAX_PATH);
|
2023-06-20 10:23:31 +00:00
|
|
|
|
if ((ret = RunCommand(cmdBuffer, cmdResult, MAX_PATH)) != ERR_SUCCESS) {
|
2023-06-19 11:08:42 +00:00
|
|
|
|
SPDLOG_ERROR("Run command [{0}] error: {1}", cmdBuffer, ret);
|
|
|
|
|
return -ERR_CALL_SHELL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringCbCopy(pPubKey, pubkeySize, cmdResult);
|
|
|
|
|
SPDLOG_DEBUG("Run command [{0}] resutl \'{1}\'", cmdBuffer, cmdResult);
|
|
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 09:25:14 +00:00
|
|
|
|
int SetWireguardPath(const TCHAR *pPath) {
|
2023-06-19 11:08:42 +00:00
|
|
|
|
WIN32_FIND_DATA FindFileData;
|
2023-06-20 09:25:14 +00:00
|
|
|
|
HANDLE hFind;
|
2023-06-19 11:08:42 +00:00
|
|
|
|
|
2023-06-20 09:25:14 +00:00
|
|
|
|
if (pPath == nullptr) {
|
2023-06-19 11:08:42 +00:00
|
|
|
|
return -ERR_INPUT_PARAMS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hFind = FindFirstFile(pPath, &FindFileData);
|
|
|
|
|
|
2023-06-20 09:25:14 +00:00
|
|
|
|
if (hFind != INVALID_HANDLE_VALUE) {
|
2023-06-19 11:08:42 +00:00
|
|
|
|
TCHAR wgPath[MAX_PATH];
|
|
|
|
|
|
2023-06-20 09:25:14 +00:00
|
|
|
|
SPDLOG_DEBUG(TEXT("Used configure file:{0}"), GetGlobalCfgInfo()->cfgPath);
|
2023-06-19 11:08:42 +00:00
|
|
|
|
|
2023-06-20 09:25:14 +00:00
|
|
|
|
WritePrivateProfileString(CFG_WIREGUARD_SECTION, CFG_WIREGUARD_PATH, pPath, GetGlobalCfgInfo()->cfgPath);
|
2023-06-19 11:08:42 +00:00
|
|
|
|
SPDLOG_DEBUG(TEXT("Save configure: {1} --> {0}"), pPath, CFG_WIREGUARD_PATH);
|
|
|
|
|
|
|
|
|
|
StringCbCopy(wgPath, MAX_PATH, pPath);
|
|
|
|
|
|
2023-06-20 09:25:14 +00:00
|
|
|
|
if (TCHAR *pIndex = _tcsrchr(wgPath, '\\')) {
|
2023-06-19 11:08:42 +00:00
|
|
|
|
*pIndex = 0;
|
|
|
|
|
StringCbCat(wgPath, MAX_PATH, "\\wg.exe");
|
2023-06-20 09:25:14 +00:00
|
|
|
|
WritePrivateProfileString(CFG_WIREGUARD_SECTION, CFG_WG_PATH, wgPath, GetGlobalCfgInfo()->cfgPath);
|
2023-06-19 11:08:42 +00:00
|
|
|
|
SPDLOG_DEBUG(TEXT("Save configure: {1} --> {0}"), wgPath, CFG_WG_PATH);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
2023-06-20 09:25:14 +00:00
|
|
|
|
} else {
|
|
|
|
|
SPDLOG_ERROR(TEXT("WireGuard not found: {0}"), pPath);
|
2023-06-19 11:08:42 +00:00
|
|
|
|
return -ERR_ITEM_UNEXISTS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 09:25:14 +00:00
|
|
|
|
int FindWireguardExe(TCHAR *pFullPath, int maxSize) {
|
|
|
|
|
TCHAR path[MAX_PATH];
|
|
|
|
|
TCHAR wrieguardPath[MAX_PATH];
|
2023-06-19 11:08:42 +00:00
|
|
|
|
WIN32_FIND_DATA FindFileData;
|
2023-06-20 09:25:14 +00:00
|
|
|
|
HANDLE hFind;
|
|
|
|
|
DWORD dwRet;
|
|
|
|
|
LPSTR pEnvBuf;
|
|
|
|
|
TCHAR *token, *p = nullptr;
|
|
|
|
|
|
|
|
|
|
GetPrivateProfileString(CFG_WIREGUARD_SECTION,
|
|
|
|
|
CFG_WIREGUARD_PATH,
|
|
|
|
|
TEXT(""),
|
|
|
|
|
wrieguardPath,
|
|
|
|
|
MAX_PATH,
|
|
|
|
|
GetGlobalCfgInfo()->cfgPath);
|
2023-06-19 11:08:42 +00:00
|
|
|
|
|
|
|
|
|
hFind = FindFirstFile(wrieguardPath, &FindFileData);
|
2023-06-20 09:25:14 +00:00
|
|
|
|
if (hFind != INVALID_HANDLE_VALUE) {
|
|
|
|
|
if (pFullPath && maxSize > 0) {
|
2023-06-19 11:08:42 +00:00
|
|
|
|
StringCbCopy(pFullPath, maxSize, wrieguardPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringCbCopy(GetGlobalCfgInfo()->wireguardCfg.wireguardPath, MAX_PATH, wrieguardPath);
|
|
|
|
|
GetGlobalCfgInfo()->wireguardCfg.wireguardExists = TRUE;
|
|
|
|
|
|
|
|
|
|
SPDLOG_DEBUG(TEXT("Ini found WireGuard at: {0}"), wrieguardPath);
|
|
|
|
|
|
2023-06-20 09:25:14 +00:00
|
|
|
|
GetPrivateProfileString(CFG_WIREGUARD_SECTION,
|
|
|
|
|
CFG_WG_PATH,
|
|
|
|
|
TEXT(""),
|
|
|
|
|
wrieguardPath,
|
|
|
|
|
MAX_PATH,
|
|
|
|
|
GetGlobalCfgInfo()->cfgPath);
|
2023-06-19 11:08:42 +00:00
|
|
|
|
|
|
|
|
|
hFind = FindFirstFile(wrieguardPath, &FindFileData);
|
2023-06-20 09:25:14 +00:00
|
|
|
|
if (hFind != INVALID_HANDLE_VALUE) {
|
2023-06-19 11:08:42 +00:00
|
|
|
|
StringCbCopy(GetGlobalCfgInfo()->wireguardCfg.wgPath, MAX_PATH, wrieguardPath);
|
|
|
|
|
GetGlobalCfgInfo()->wireguardCfg.wgExists = TRUE;
|
|
|
|
|
SPDLOG_DEBUG(TEXT("Ini found WireGuard Tools at: {0}"), wrieguardPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pEnvBuf = static_cast<LPSTR>(malloc(WINENVBUF_SIZE));
|
2023-06-20 09:25:14 +00:00
|
|
|
|
if (nullptr == pEnvBuf) {
|
2023-06-19 11:08:42 +00:00
|
|
|
|
SPDLOG_ERROR(TEXT("Malloc {0} bytes memory error"), WINENVBUF_SIZE);
|
|
|
|
|
return -ERR_MALLOC_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dwRet = GetEnvironmentVariable(TEXT("path"), pEnvBuf, WINENVBUF_SIZE);
|
|
|
|
|
|
2023-06-20 09:25:14 +00:00
|
|
|
|
if (0 == dwRet) {
|
|
|
|
|
DWORD dwErr;
|
2023-06-19 11:08:42 +00:00
|
|
|
|
dwErr = GetLastError();
|
2023-06-20 09:25:14 +00:00
|
|
|
|
if (ERROR_ENVVAR_NOT_FOUND == dwErr) {
|
2023-06-19 11:08:42 +00:00
|
|
|
|
SPDLOG_DEBUG(TEXT("Environment variable path does not exist."));
|
|
|
|
|
free(pEnvBuf);
|
|
|
|
|
return -ERR_FILE_NOT_EXISTS;
|
|
|
|
|
}
|
2023-06-20 09:25:14 +00:00
|
|
|
|
} else if (WINENVBUF_SIZE < dwRet) {
|
|
|
|
|
LPSTR pBuf = static_cast<LPSTR>(realloc(pEnvBuf, dwRet * sizeof(CHAR)));
|
|
|
|
|
if (nullptr == pBuf) {
|
2023-06-19 11:08:42 +00:00
|
|
|
|
SPDLOG_ERROR(TEXT("Malloc {0} bytes memory error"), dwRet * sizeof(CHAR));
|
2023-06-20 09:25:14 +00:00
|
|
|
|
free(pEnvBuf);
|
2023-06-19 11:08:42 +00:00
|
|
|
|
return -ERR_MALLOC_MEMORY;
|
|
|
|
|
}
|
2023-06-20 09:25:14 +00:00
|
|
|
|
|
|
|
|
|
pEnvBuf = pBuf;
|
|
|
|
|
dwRet = GetEnvironmentVariable("path", pEnvBuf, dwRet);
|
|
|
|
|
if (!dwRet) {
|
2023-06-19 11:08:42 +00:00
|
|
|
|
SPDLOG_ERROR(TEXT("GetEnvironmentVariable failed (%d)"), GetLastError());
|
|
|
|
|
free(pEnvBuf);
|
|
|
|
|
return -ERR_FILE_NOT_EXISTS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token = strtok_s(pEnvBuf, TEXT(";"), &p);
|
|
|
|
|
|
2023-06-20 09:25:14 +00:00
|
|
|
|
while (token != nullptr) {
|
2023-06-19 11:08:42 +00:00
|
|
|
|
memset(path, 0, MAX_PATH);
|
|
|
|
|
StringCbPrintfA(path, MAX_PATH, TEXT("%s\\wireguard.exe"), token);
|
|
|
|
|
|
|
|
|
|
hFind = FindFirstFile(path, &FindFileData);
|
|
|
|
|
|
2023-06-20 09:25:14 +00:00
|
|
|
|
if (hFind != INVALID_HANDLE_VALUE) {
|
|
|
|
|
if (pFullPath && maxSize > 0) {
|
2023-06-19 11:08:42 +00:00
|
|
|
|
StringCbCopy(pFullPath, maxSize, path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 保存路径到配置文件
|
|
|
|
|
SetWireguardPath(path);
|
|
|
|
|
SPDLOG_DEBUG(TEXT("Path Environment found WireGuard at: {0}"), path);
|
|
|
|
|
|
|
|
|
|
StringCbCopy(GetGlobalCfgInfo()->wireguardCfg.wireguardPath, MAX_PATH, wrieguardPath);
|
|
|
|
|
GetGlobalCfgInfo()->wireguardCfg.wireguardExists = TRUE;
|
|
|
|
|
|
|
|
|
|
memset(path, 0, MAX_PATH);
|
|
|
|
|
StringCbPrintf(path, MAX_PATH, TEXT("%s\\wg.exe"), token);
|
|
|
|
|
|
|
|
|
|
SPDLOG_DEBUG(TEXT("Find WireGuard tools at: {0}"), path);
|
|
|
|
|
|
|
|
|
|
hFind = FindFirstFile(path, &FindFileData);
|
2023-06-20 09:25:14 +00:00
|
|
|
|
if (hFind != INVALID_HANDLE_VALUE) {
|
2023-06-19 11:08:42 +00:00
|
|
|
|
StringCbCopy(GetGlobalCfgInfo()->wireguardCfg.wgPath, MAX_PATH, path);
|
|
|
|
|
GetGlobalCfgInfo()->wireguardCfg.wgExists = TRUE;
|
|
|
|
|
|
|
|
|
|
SPDLOG_DEBUG(TEXT("Path Environment found WireGuard tools at: {0}"), path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO: throw exception by C# call, why??????
|
|
|
|
|
//CloseHandle(hFind);
|
|
|
|
|
free(pEnvBuf);
|
|
|
|
|
return ERR_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
token = strtok_s(nullptr, TEXT(";"), &p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(pEnvBuf);
|
|
|
|
|
return -ERR_FILE_NOT_EXISTS;
|
2023-06-20 09:25:14 +00:00
|
|
|
|
}
|