165 lines
5.2 KiB
C++
165 lines
5.2 KiB
C++
|
#include "pch.h"
|
|||
|
|
|||
|
#include "tunnel.h"
|
|||
|
#include "usrerr.h"
|
|||
|
#include "globalcfg.h"
|
|||
|
#include "misc.h"
|
|||
|
#include "protocol.h"
|
|||
|
#include "user.h"
|
|||
|
|
|||
|
#include <shlwapi.h>
|
|||
|
#include <strsafe.h>
|
|||
|
|
|||
|
int UserLogin(const TCHAR *pUserName, const TCHAR *pToken, PUSER_CLIENT_CONFIG *pCliCfg) {
|
|||
|
PUSER_CLIENT_CONFIG pCfg;
|
|||
|
PVM_CFG pVm;
|
|||
|
PUSER_CONFIG pUser = &GetGlobalCfgInfo()->userCfg;
|
|||
|
TCHAR userPath[MAX_PATH];
|
|||
|
int ret;
|
|||
|
unsigned int memSize;
|
|||
|
|
|||
|
ProtocolRequest<ReqGetUserCfgParams> req;
|
|||
|
ProtocolResponse<UserClientConfigParams> rsp;
|
|||
|
|
|||
|
if (pUserName == nullptr || lstrlen(pUserName) == 0) {
|
|||
|
SPDLOG_ERROR(TEXT("Input pUserName params error: {0}"), pUserName);
|
|||
|
return -ERR_INPUT_PARAMS;
|
|||
|
}
|
|||
|
|
|||
|
if (pToken == nullptr || lstrlen(pToken) == 0) {
|
|||
|
SPDLOG_ERROR(TEXT("Input pToken params error: {0}"), pToken);
|
|||
|
return -ERR_INPUT_PARAMS;
|
|||
|
}
|
|||
|
|
|||
|
StringCbPrintf(userPath, MAX_PATH, "%s\\%s", GetGlobalCfgInfo()->configDirectory, pUserName);
|
|||
|
// 如果配置目录不存在则自动创建
|
|||
|
if (!PathFileExists(userPath)) {
|
|||
|
if (!CreateDirectory(userPath, nullptr)) {
|
|||
|
SPDLOG_ERROR(TEXT("Create user {1} directory '{0}' error."), userPath, pUserName);
|
|||
|
return -ERR_CREATE_FILE;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
memset(pUser->userName, 0, MAX_PATH);
|
|||
|
StringCbCopy(pUser->userName, MAX_PATH, pUserName);
|
|||
|
StringCbCopy(pUser->userToken, MAX_PATH, pToken);
|
|||
|
|
|||
|
req.msgContent.token = pToken;
|
|||
|
req.msgContent.user = pUserName;
|
|||
|
|
|||
|
ret = ProtolPostMessage(&req, &rsp);
|
|||
|
|
|||
|
if (ret != ERR_SUCCESS) {
|
|||
|
return ret;
|
|||
|
}
|
|||
|
|
|||
|
pCfg = static_cast<PUSER_CLIENT_CONFIG>(CoTaskMemAlloc(sizeof(USER_CLIENT_CONFIG)));
|
|||
|
|
|||
|
if (pCfg == nullptr) {
|
|||
|
SPDLOG_ERROR(TEXT("Error allocating memory {0} bytes"), sizeof(USER_CLIENT_CONFIG));
|
|||
|
return -ERR_MALLOC_MEMORY;
|
|||
|
}
|
|||
|
|
|||
|
memset(pCfg, 0, sizeof(USER_CLIENT_CONFIG));
|
|||
|
|
|||
|
memSize = sizeof(VM_CFG) * static_cast<UINT>(rsp.msgContent.vmConfig.size());
|
|||
|
pCfg->pVMConfig = static_cast<PVM_CFG>(CoTaskMemAlloc(memSize));
|
|||
|
|
|||
|
if (pCfg->pVMConfig == nullptr) {
|
|||
|
SPDLOG_ERROR(TEXT("Error allocating memory {0} bytes"), memSize);
|
|||
|
CoTaskMemFree(pCfg);
|
|||
|
return -ERR_MALLOC_MEMORY;
|
|||
|
}
|
|||
|
|
|||
|
memset(pCfg->pVMConfig, 0, memSize);
|
|||
|
|
|||
|
pCfg->scgCtrlAppId = rsp.msgContent.scgCtrlAppId;
|
|||
|
pCfg->scgTunnelAppId = rsp.msgContent.scgTunnelAppId;
|
|||
|
StringCbCopy(pCfg->cliPrivateKey, 64, rsp.msgContent.cliPrivateKey.c_str());
|
|||
|
StringCbCopy(pCfg->cliAddress, MAX_IP_LEN, rsp.msgContent.cliAddress.c_str());
|
|||
|
pCfg->tolVM = static_cast<int>(rsp.msgContent.vmConfig.size());
|
|||
|
|
|||
|
pVm = pCfg->pVMConfig;
|
|||
|
for (auto vm : rsp.msgContent.vmConfig) {
|
|||
|
pVm->vmId = vm.vmId;
|
|||
|
StringCbCopy(pVm->vmName, MAX_PATH, vm.vmName.c_str());
|
|||
|
StringCbCopy(pVm->svrPublicKey, 64, vm.svrPublicKey.c_str());
|
|||
|
StringCbCopy(pVm->vmNetwork, MAX_IP_LEN, vm.vmNetwork.c_str());
|
|||
|
StringCbCopy(pVm->scgGateWay, MAX_PATH, vm.scgGateway.c_str());
|
|||
|
pVm++;
|
|||
|
}
|
|||
|
|
|||
|
*pCliCfg = pCfg;
|
|||
|
return ERR_SUCCESS;
|
|||
|
}
|
|||
|
|
|||
|
int GetUserConfigFiles(const TCHAR *pUserName, PUSER_CFGFILE *pCfgFile, int *pItems) {
|
|||
|
PUSER_CFGFILE pCfg;
|
|||
|
FILE_LIST fileList = {nullptr, 0};
|
|||
|
TCHAR fnPath[MAX_PATH] = {};
|
|||
|
TCHAR cfgVal[MAX_PATH];
|
|||
|
bool isSelected = false;
|
|||
|
|
|||
|
if (pUserName == nullptr || lstrlen(pUserName) == 0) {
|
|||
|
SPDLOG_ERROR(TEXT("Input pUserName params error: {0}"), pUserName);
|
|||
|
return -ERR_INPUT_PARAMS;
|
|||
|
}
|
|||
|
|
|||
|
if (pCfgFile == nullptr) {
|
|||
|
SPDLOG_ERROR(TEXT("Input pCfgFile params error"));
|
|||
|
return -ERR_INPUT_PARAMS;
|
|||
|
}
|
|||
|
|
|||
|
if (pItems == nullptr) {
|
|||
|
SPDLOG_ERROR(TEXT("Input pItems params error"));
|
|||
|
return -ERR_INPUT_PARAMS;
|
|||
|
}
|
|||
|
|
|||
|
GetPrivateProfileString(CFG_WIREGUARD_SECTION,
|
|||
|
CFG_WGCFG_PATH,
|
|||
|
TEXT(""),
|
|||
|
cfgVal,
|
|||
|
MAX_PATH,
|
|||
|
GetGlobalCfgInfo()->cfgPath);
|
|||
|
|
|||
|
if (PathFileExists(cfgVal)) {
|
|||
|
isSelected = true;
|
|||
|
}
|
|||
|
|
|||
|
StringCbPrintf(fnPath, MAX_PATH, "%s\\%s\\*.conf", GetGlobalCfgInfo()->configDirectory, pUserName);
|
|||
|
|
|||
|
int ret = FindFile(fnPath, &fileList, false);
|
|||
|
|
|||
|
if (ret != ERR_SUCCESS) {
|
|||
|
SPDLOG_ERROR(TEXT("Find WireGuard user {1} configure file error: {0}"), ret, pUserName);
|
|||
|
return ret;
|
|||
|
}
|
|||
|
|
|||
|
pCfg = static_cast<PUSER_CFGFILE>(CoTaskMemAlloc(sizeof(USER_CFGFILE) * fileList.nItems));
|
|||
|
|
|||
|
if (pCfg == nullptr) {
|
|||
|
SPDLOG_ERROR(TEXT("Error allocating memory {0} bytes"), sizeof(USER_CFGFILE) * fileList.nItems);
|
|||
|
return -ERR_SUCCESS;
|
|||
|
}
|
|||
|
|
|||
|
memset(pCfg, 0, sizeof(USER_CFGFILE) * fileList.nItems);
|
|||
|
|
|||
|
*pCfgFile = pCfg;
|
|||
|
*pItems = static_cast<int>(fileList.nItems);
|
|||
|
|
|||
|
for (unsigned int i = 0; fileList.pFilePath && i < fileList.nItems; i++) {
|
|||
|
StringCbCopy(pCfg->CfgPath, MAX_PATH, fileList.pFilePath[i].path);
|
|||
|
if (isSelected && StrCmp(pCfg->CfgPath, cfgVal) == 0) {
|
|||
|
pCfg->isCurrent = true;
|
|||
|
} else {
|
|||
|
pCfg->isCurrent = false;
|
|||
|
}
|
|||
|
pCfg++;
|
|||
|
}
|
|||
|
|
|||
|
if (fileList.pFilePath) {
|
|||
|
HeapFree(GetProcessHeap(), 0, fileList.pFilePath);
|
|||
|
}
|
|||
|
|
|||
|
return ERR_SUCCESS;
|
|||
|
}
|