NetTunnelWindows/NetTunnelSDK/tunnel.cpp

146 lines
4.9 KiB
C++

#include "pch.h"
#include "tunnel.h"
#include <shlobj_core.h>
#include <strsafe.h>
#include <spdlog/sinks/wincolor_sink.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/sinks/dup_filter_sink.h>
#include "usrerr.h"
#include "globalcfg.h"
#define CONFIG_FILE_NAME TEXT("tunnelsdk.ini")
static SDK_CONFIG g_globalConfig;
PSDK_CONFIG GetGlobalCfgInfo() {
return &g_globalConfig;
}
int TunnelSDKInitEnv(const TCHAR *pWorkDir) {
size_t length;
BOOL logEnable = g_globalConfig.enableLog;
spdlog::level::level_enum lv = g_globalConfig.logLevel;
memset(&g_globalConfig, 0, sizeof(SDK_CONFIG));
g_globalConfig.logLevel = lv;
g_globalConfig.enableLog = logEnable;
if (!logEnable) {
spdlog::set_level(spdlog::level::off);
}
if (SUCCEEDED(
SHGetFolderPath(NULL, CSIDL_WINDOWS | CSIDL_FLAG_NO_ALIAS, NULL, 0, g_globalConfig.systemDirectory))) {
SPDLOG_ERROR(TEXT("Get Windows system directory error."));
return -ERR_ITEM_EXISTS;
}
if (pWorkDir == nullptr) {
// 获取当前文件默认路径
GetCurrentDirectory(MAX_PATH, g_globalConfig.workDirectory);
} else {
if (StringCbLengthA(pWorkDir, MAX_PATH, &length) == S_OK && length == 0) {
GetCurrentDirectory(MAX_PATH, g_globalConfig.workDirectory);
} else {
StringCbCopy(g_globalConfig.workDirectory, MAX_PATH, pWorkDir);
}
}
StringCbPrintf(g_globalConfig.cfgPath, MAX_PATH, TEXT("%s\\%s"), g_globalConfig.workDirectory, CONFIG_FILE_NAME);
if (FindWireguardExe(nullptr, 0) != ERR_SUCCESS) {
SPDLOG_ERROR(TEXT("WireGuard not found, Please install WireGuard first or set the WireGuard Path."));
return -ERR_ITEM_EXISTS;
}
return ERR_SUCCESS;
}
void TunnelSDKUnInit() {
}
static spdlog::level::level_enum logLevelToSpdlogLevel(LOG_LEVEL level) {
switch (level) {
case LOG_TRACE:
return spdlog::level::level_enum::trace;
case LOG_DEBUG:
return spdlog::level::level_enum::debug;
case LOG_INFO:
return spdlog::level::level_enum::info;
case LOG_WARN:
return spdlog::level::level_enum::warn;
case LOG_ERROR:
return spdlog::level::level_enum::err;
case LOG_CRITICAL:
return spdlog::level::level_enum::critical;
case LOG_OFF:
return spdlog::level::level_enum::off;
default:
return spdlog::level::level_enum::info;
}
}
void InitTunnelSDKLog(const TCHAR *pLogFile, LOG_LEVEL level) {
TCHAR buf[MAX_PATH] = {0};
if (pLogFile && strlen(pLogFile) > 0) {
StringCbCopy(buf, MAX_PATH, pLogFile);
} else {
StringCbCopy(buf, MAX_PATH, TEXT("tunnelsdk.log"));
}
g_globalConfig.enableLog = TRUE;
g_globalConfig.logLevel = logLevelToSpdlogLevel(level);
auto dupFileFilter = std::make_shared<spdlog::sinks::dup_filter_sink_st>(std::chrono::seconds(5));
auto dupStdFilter = std::make_shared<spdlog::sinks::dup_filter_sink_st>(std::chrono::seconds(5));
dupFileFilter->add_sink(std::make_shared<spdlog::sinks::rotating_file_sink_mt>(buf, 1024 * 1024 * 5, 10));
dupStdFilter->add_sink(std::make_shared<spdlog::sinks::wincolor_stdout_sink_mt>());
std::vector<spdlog::sink_ptr> sinks {dupStdFilter, dupFileFilter};
auto logger = std::make_shared<spdlog::logger>(TEXT("tunnelSDK"), sinks.begin(), sinks.end());
spdlog::set_default_logger(logger);
spdlog::set_level(g_globalConfig.logLevel);
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e][%l][%s:%#] %v");
spdlog::flush_every(std::chrono::seconds(1));
#if 0
std::cout << "TRACE: " << logger->should_log(spdlog::level::trace) << std::endl;
std::cout << "DEBUG: " << logger->should_log(spdlog::level::debug) << std::endl;
std::cout << "INFO: " << logger->should_log(spdlog::level::info) << std::endl;
std::cout << "WARN: " << logger->should_log(spdlog::level::warn) << std::endl;
std::cout << "ERROR: " << logger->should_log(spdlog::level::err) << std::endl;
std::cout << "CRITICAL: " << logger->should_log(spdlog::level::critical) << std::endl;
#endif
SPDLOG_INFO(TEXT("Log({1}): {0}"), buf, static_cast<int>(level));
}
void TunnelLogEnable(bool enLog) {
if (enLog) {
spdlog::set_level(g_globalConfig.logLevel);
} else {
spdlog::set_level(spdlog::level::level_enum::off);
}
}
int SetProtocolEncryptType(const PROTO_CRYPTO_TYPE type, const TCHAR *pProKey) {
if (type > CRYPTO_BASE64 && type < CRYPTO_MAX) {
if (pProKey == nullptr || strlen(pProKey) < 8) {
return -ERR_INPUT_PARAMS;
}
}
g_globalConfig.proCryptoType = type;
StringCbCopy(g_globalConfig.proKeyBuf, 256, pProKey);
SPDLOG_DEBUG(TEXT("Protocol crypto type: {0} with key [{1}]"),
static_cast<int>(type),
pProKey ? pProKey : TEXT(""));
return ERR_SUCCESS;
}