OCT 1. 获取网卡接口增加网卡状态字段

2. 隧道支持端口映射配置,以及配置协议字段需求
3. 日志支持按天分割文件
This commit is contained in:
黄昕 2023-08-21 09:22:52 +08:00
parent 83d7cf638e
commit 2d9da7b215
12 changed files with 138 additions and 60 deletions

View File

@ -104,6 +104,8 @@ public struct NetCard
{
public int IfIndex;
public int netConnStatus;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string UUID;

View File

@ -598,7 +598,7 @@ int GetUserClientConfigure(const TCHAR *pUserName, const TCHAR *pToken, PUSER_CL
//StringCbCopy(pVm->scgGateWay, MAX_PATH, vm.scgGateway.c_str());
StringCbPrintf(pVm->scgGateWay, MAX_PATH, TEXT("http://%s:%d"), vm.scgIp.c_str(), vm.scgPort);
#if USED_PORTMAP_TUNNEL
StringCbPrintf(pVm->scgTunnelGw, MAX_PATH, TEXT("%s:%d"), vm.portMapIp.c_str(), vm.portMapPort - 1);
StringCbPrintf(pVm->scgTunnelGw, MAX_PATH, TEXT("%s:%d"), vm.portMapIp.c_str(), vm.portMapPort);
#else
StringCbPrintf(pVm->scgTunnelGw, MAX_PATH, TEXT("%s:%d"), vm.scgIp.c_str(), vm.scgPort - 1);
#endif

View File

@ -1,31 +1,30 @@
#pragma once
#define USED_PORTMAP_TUNNEL (1)
/**
* @brief WireGuard key
*/
#define WG_KEY_MAX (64)
#define WG_KEY_MAX (64)
/**
* @brief
*/
#define NET_CARD_MAX (32)
#define NET_CARD_MAX (32)
/**
* @brief IP (IPv6)
*/
#define MAX_IP_LEN (48)
#define MAX_IP_LEN (48)
/**
* @brief IP
*/
#define MIN_IP_LEN (7)
#define MIN_IP_LEN (7)
/**
* @brief (IPv6)
*/
#define MAX_NETCARD_NAME (64)
#define MAX_NETCARD_NAME (64)
/**
* @brief SCG ID
@ -49,6 +48,29 @@ typedef enum {
CRYPTO_MAX,
} PROTO_CRYPTO_TYPE;
/**
* @brief
*
*/
typedef enum {
STATUS_DISCONNECTED = 0, ///< 连接已断开连接
STATUS_CONNECTING, ///< 连接正在进行连接
STATUS_CONNECTED, ///< 连接处于连接状态
STATUS_DISCONNECTING, ///< 连接正在断开连接
STATUS_HARDWARE_NOT_PRESENT, ///< 连接的硬件(例如网络接口卡 (NIC) )不存在
STATUS_HARDWARE_DISABLED, ///< 连接的硬件存在,但未启用
STATUS_HARDWARE_MALFUNCTION, ///< 连接的硬件中发生了故障
STATUS_MEDIA_DISCONNECTED, ///< 媒体(例如网络电缆)断开连接
STATUS_AUTHENTICATING, ///< 连接正在等待身份验证发生
STATUS_AUTHENTICATION_SUCCEEDED, ///< 身份验证在此连接上成功
STATUS_AUTHENTICATION_FAILED, ///< 此连接上身份验证失败
STATUS_INVALID_ADDRESS, ///< 地址无效
STATUS_CREDENTIALS_REQUIRED, ///< 需要安全凭据
STATUS_ACTION_REQUIRED, ///< 连接需要其它动作
STATUS_ACTION_REQUIRED_RETRY, ///< 重试连接其它动作
STATUS_CONNECT_FAILED, ///< 连接失败
} NET_CONNECT_STATUS;
/**
* @brief
*

View File

@ -24,6 +24,7 @@ int GetInterfaceIfIndexByIpAddr(const TCHAR *pIpAddr, ULONG *pIfIndex);
* @brief GUDI
* @param[in] pGUID GUID
* @param[out] ifName
* @param[out] pConnStatus
* @return 0: 0 @see USER_ERRNO
* - -ERR_INPUT_PARAMS
* - -ERR_MALLOC_MEMORY
@ -32,7 +33,7 @@ int GetInterfaceIfIndexByIpAddr(const TCHAR *pIpAddr, ULONG *pIfIndex);
* - -ERR_ITEM_UNEXISTS GUID
* - ERR_SUCCESS
*/
int GetInterfaceNameByGUID(const TCHAR *pGUID, TCHAR ifName[MAX_NETCARD_NAME]);
int GetInterfaceNameByGUID(const TCHAR *pGUID, TCHAR ifName[MAX_NETCARD_NAME], int* pConnStatus);
/**
* @brief

View File

@ -17,14 +17,15 @@ typedef PTUNNEL_HEART_ROUTINE LPTUNNEL_HEART_ROUTINE;
* @brief
*/
typedef struct {
int InterfaceIndex; ///< 网卡索引
TCHAR NetCardUUID[260]; ///< 网卡名称, Windows标识为 UUID
TCHAR NetCardName[MAX_NETCARD_NAME]; ///< 网卡名称
TCHAR NetCardDescription[132]; ///< 网卡描述
TCHAR NetCardIpaddr[MAX_IP_LEN]; ///< 网卡 IP 地址
TCHAR NetCardNetmask[MAX_IP_LEN]; ///< 网卡子网掩码
TCHAR NetCardGateway[MAX_IP_LEN]; ///< 网卡网关
TCHAR NetCardMacAddr[20]; ///< 网卡 MAC 地址
int InterfaceIndex; ///< 网卡索引
NET_CONNECT_STATUS netConnStatus; ///< 网卡状态 @see NET_CONNECT_STATUS
TCHAR NetCardUUID[260]; ///< 网卡名称, Windows标识为 UUID
TCHAR NetCardName[MAX_NETCARD_NAME]; ///< 网卡名称
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;
#ifdef __cplusplus // If used by C++ code,

View File

@ -2,17 +2,22 @@
#include <strsafe.h>
#include <spdlog/spdlog.h>
#include "globalcfg.h"
#include "usrerr.h"
#include "misc.h"
#if !USED_PORTMAP_TUNNEL
#include "globalcfg.h"
#include <winsock2.h>
#include <ws2tcpip.h>
#include <spdlog/fmt/bin_to_hex.h>
#define SCG_UDP_HEAD_SIZE (11)
#endif
void StopUDPProxyServer() {
#if !USED_PORTMAP_TUNNEL
const PSCG_PROXY_INFO pProxy = &GetGlobalCfgInfo()->scgProxy;
pProxy->exitNow = true;
@ -31,8 +36,10 @@ void StopUDPProxyServer() {
closesocket(pProxy->scgGwSock);
}
#endif
}
#if !USED_PORTMAP_TUNNEL
static DWORD UDPProxvRemoteThread(LPVOID lpParameter) {
const auto pPeerSock = static_cast<sockaddr_in *>(lpParameter);
const PSCG_PROXY_INFO pProxy = &GetGlobalCfgInfo()->scgProxy;
@ -207,8 +214,9 @@ static DWORD UDPProxyRecvThread(LPVOID lpParameter) {
}
return 0;
}
#endif
int CreateUDPProxyServer() {
#if !USED_PORTMAP_TUNNEL
HANDLE handle;
int ret;
int addrSize = sizeof(sockaddr_in);
@ -272,6 +280,6 @@ int CreateUDPProxyServer() {
}
pProxy->hProxyTunnelThread = handle;
#endif
return ERR_SUCCESS;
}

View File

@ -140,7 +140,7 @@ int GetInterfaceIfIndexByGUID(const TCHAR *pGUID, int *pIfIndex) {
return -ERR_ITEM_UNEXISTS;
}
int GetInterfaceNameByGUID(const TCHAR *pGUID, TCHAR ifName[MAX_NETCARD_NAME]) {
int GetInterfaceNameByGUID(const TCHAR *pGUID, TCHAR ifName[MAX_NETCARD_NAME], int *pConnStatus) {
VARIANT v;
INetConnection *pNC = nullptr;
IEnumVARIANT *pEV = nullptr;
@ -245,6 +245,9 @@ int GetInterfaceNameByGUID(const TCHAR *pGUID, TCHAR ifName[MAX_NETCARD_NAME]) {
return ret;
}
if (pConnStatus) {
*pConnStatus = pNP->Status;
}
return ERR_SUCCESS;
}
}
@ -425,7 +428,7 @@ int GetInterfaceIfIndexByName(const TCHAR *pInterfaceName, int *pIfIndex) {
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
while (pAdapter) {
TCHAR NetCardName[MAX_NETCARD_NAME] = {};
GetInterfaceNameByGUID(pAdapter->AdapterName, NetCardName);
GetInterfaceNameByGUID(pAdapter->AdapterName, NetCardName, nullptr);
if (StrCmp(pInterfaceName, NetCardName) == 0) {
*pIfIndex = static_cast<int>(pAdapter->Index);
@ -485,7 +488,7 @@ int GetInterfaceGUIDByName(const TCHAR *pInterfaceName, GUID *pGuid) {
while (pAdapter) {
int ret;
TCHAR NetCardName[MAX_NETCARD_NAME] = {};
GetInterfaceNameByGUID(pAdapter->AdapterName, NetCardName);
GetInterfaceNameByGUID(pAdapter->AdapterName, NetCardName, nullptr);
if (StrCmp(pInterfaceName, NetCardName) == 0) {
WCHAR strGuid[MAX_PATH];
@ -632,7 +635,8 @@ int GetAllNICInfo(PNIC_CONTENT *pInfo, int *pItemCounts) {
}
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
int id = 0;
int id = 0;
int ncStatus;
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
while (pAdapter && id < NET_CARD_MAX) {
// Íø¿¨Ë÷Òý
@ -660,7 +664,10 @@ int GetAllNICInfo(PNIC_CONTENT *pInfo, int *pItemCounts) {
pAdapter->Address[4],
pAdapter->Address[5]);
GetInterfaceNameByGUID(pAdapter->AdapterName, g_NetAdapterInfo[id].NetCardName);
if (GetInterfaceNameByGUID(pAdapter->AdapterName, g_NetAdapterInfo[id].NetCardName, &ncStatus) ==
ERR_SUCCESS) {
g_NetAdapterInfo[id].netConnStatus = static_cast<NET_CONNECT_STATUS>(ncStatus);
}
id++;
pAdapter = pAdapter->Next;

View File

@ -38,6 +38,7 @@ int InitControlServer(const TCHAR *pUserSvrUrl) {
g_tunnelHttpCtx->set_connection_timeout(0, 1000000); // 1 second
g_tunnelHttpCtx->set_read_timeout(5, 0); // 5 seconds
g_tunnelHttpCtx->set_write_timeout(5, 0); // 5 seconds
g_tunnelHttpCtx->set_keep_alive(true);
g_tunnelHttpCtx->set_post_connect_cb([](socket_t sock) {
if (UsedSCGProxy()) {
int ret;
@ -96,10 +97,11 @@ template<class T> int CreateProtocolRequest(T *pReqParams, TCHAR **pOutJson) {
if (!g_httpCtx && lstrlen(GetGlobalCfgInfo()->platformServerUrl) > 0) {
g_httpCtx = new httplib::Client(GetGlobalCfgInfo()->platformServerUrl);
if (g_httpCtx) {
g_httpCtx->enable_server_certificate_verification(false);
g_httpCtx->set_connection_timeout(0, 300000); // 300 milliseconds
g_httpCtx->set_read_timeout(5, 0); // 5 seconds
g_httpCtx->set_write_timeout(5, 0); // 5 seconds
g_httpCtx->set_keep_alive(true);
g_httpCtx->enable_server_certificate_verification(false);
}
}
@ -344,30 +346,30 @@ template<class T1, class T2> int PlatformProtolPostMessage(const TCHAR *pUrlPath
lstrlen(GetGlobalCfgInfo()->clientSecret),
hashValeu,
true) == ERR_SUCCESS) {
if (lstrcmp(typeid(T1).name(), TEXT("class PlatformReqClientCfgParms")) == 0) {
if (typeid(T1) == typeid(PlatformReqClientCfgParms)) {
const auto *p = reinterpret_cast<PlatformReqClientCfgParms *>(pReq);
const httplib::Headers headers = {
{"gzs-client-id", GetGlobalCfgInfo()->clientId},
{"gzs-client-id", GetGlobalCfgInfo()->clientId },
{"gzs-sign", hashValeu },
{"gzs-timestamp", timestamp },
{"gzs-userid", p->token.c_str() },
{"gzs-timestamp", timestamp },
{"Authorization", ("Bearer " + p->token).c_str()},
};
res = g_httpCtx->Post(pUrlPath, headers, pJson, HTTP_JSON_CONTENT);
} else {
const httplib::Headers headers = {
{"gzs-client-id", GetGlobalCfgInfo()->clientId},
{"gzs-sign", hashValeu },
{"gzs-sign", hashValeu },
{"gzs-timestamp", timestamp },
};
res = g_httpCtx->Post(pUrlPath, headers, pJson, HTTP_JSON_CONTENT);
}
} else {
if (lstrcmp(typeid(T1).name(), TEXT("class PlatformReqClientCfgParms")) == 0) {
if (typeid(T1) == typeid(PlatformReqClientCfgParms)) {
const auto *p = reinterpret_cast<PlatformReqClientCfgParms *>(pReq);
const httplib::Headers headers = {
{"gzs-userid", p->token.c_str()},
{"Authorization", ("Bearer " + p->token).c_str()},
};
res = g_httpCtx->Post(pUrlPath, headers, pJson, HTTP_JSON_CONTENT);
} else {
@ -375,6 +377,8 @@ template<class T1, class T2> int PlatformProtolPostMessage(const TCHAR *pUrlPath
}
}
SPDLOG_DEBUG(TEXT("+++++ Http Request {0}\n---- Http Response {1}"), pJson, res->body.c_str());
if (res.error() != httplib::Error::Success) {
SPDLOG_ERROR(TEXT("[{0}]:Post Data {1} error: {2}"), pUrlPath, pJson, httplib::to_string(res.error()));
free(pJson);
@ -387,8 +391,6 @@ template<class T1, class T2> int PlatformProtolPostMessage(const TCHAR *pUrlPath
return -ERR_HTTP_SERVER_RSP;
}
SPDLOG_DEBUG(TEXT("+++++ Http Request {0}\n---- Http Response {1}"), pJson, res->body.c_str());
free(pJson);
if (lstrlen(res->body.c_str()) == 0) {

View File

@ -1,31 +1,30 @@
#pragma once
#define USED_PORTMAP_TUNNEL (1)
#define USED_PORTMAP_TUNNEL (0)
/**
* @brief WireGuard key
*/
#define WG_KEY_MAX (64)
#define WG_KEY_MAX (64)
/**
* @brief
*/
#define NET_CARD_MAX (32)
#define NET_CARD_MAX (32)
/**
* @brief IP (IPv6)
*/
#define MAX_IP_LEN (48)
#define MAX_IP_LEN (48)
/**
* @brief IP
*/
#define MIN_IP_LEN (7)
#define MIN_IP_LEN (7)
/**
* @brief (IPv6)
*/
#define MAX_NETCARD_NAME (64)
#define MAX_NETCARD_NAME (64)
/**
* @brief SCG ID
@ -49,6 +48,29 @@ typedef enum {
CRYPTO_MAX,
} PROTO_CRYPTO_TYPE;
/**
* @brief
*
*/
typedef enum {
STATUS_DISCONNECTED = 0, ///< 连接已断开连接
STATUS_CONNECTING, ///< 连接正在进行连接
STATUS_CONNECTED, ///< 连接处于连接状态
STATUS_DISCONNECTING, ///< 连接正在断开连接
STATUS_HARDWARE_NOT_PRESENT, ///< 连接的硬件(例如网络接口卡 (NIC) )不存在
STATUS_HARDWARE_DISABLED, ///< 连接的硬件存在,但未启用
STATUS_HARDWARE_MALFUNCTION, ///< 连接的硬件中发生了故障
STATUS_MEDIA_DISCONNECTED, ///< 媒体(例如网络电缆)断开连接
STATUS_AUTHENTICATING, ///< 连接正在等待身份验证发生
STATUS_AUTHENTICATION_SUCCEEDED, ///< 身份验证在此连接上成功
STATUS_AUTHENTICATION_FAILED, ///< 此连接上身份验证失败
STATUS_INVALID_ADDRESS, ///< 地址无效
STATUS_CREDENTIALS_REQUIRED, ///< 需要安全凭据
STATUS_ACTION_REQUIRED, ///< 连接需要其它动作
STATUS_ACTION_REQUIRED_RETRY, ///< 重试连接其它动作
STATUS_CONNECT_FAILED, ///< 连接失败
} NET_CONNECT_STATUS;
/**
* @brief
*

View File

@ -17,14 +17,15 @@ typedef PTUNNEL_HEART_ROUTINE LPTUNNEL_HEART_ROUTINE;
* @brief
*/
typedef struct {
int InterfaceIndex; ///< 网卡索引
CHAR NetCardUUID[260]; ///< 网卡名称, Windows标识为 UUID
CHAR NetCardName[MAX_NETCARD_NAME]; ///< 网卡名称
CHAR NetCardDescription[132]; ///< 网卡描述
CHAR NetCardIpaddr[MAX_IP_LEN]; ///< 网卡 IP 地址
CHAR NetCardNetmask[MAX_IP_LEN]; ///< 网卡子网掩码
CHAR NetCardGateway[MAX_IP_LEN]; ///< 网卡网关
CHAR NetCardMacAddr[20]; ///< 网卡 MAC 地址
int InterfaceIndex; ///< 网卡索引
NET_CONNECT_STATUS netConnStatus; ///< 网卡状态 @see NET_CONNECT_STATUS
CHAR NetCardUUID[260]; ///< 网卡名称, Windows标识为 UUID
CHAR NetCardName[MAX_NETCARD_NAME]; ///< 网卡名称
CHAR NetCardDescription[132]; ///< 网卡描述
CHAR NetCardIpaddr[MAX_IP_LEN]; ///< 网卡 IP 地址
CHAR NetCardNetmask[MAX_IP_LEN]; ///< 网卡子网掩码
CHAR NetCardGateway[MAX_IP_LEN]; ///< 网卡网关
CHAR NetCardMacAddr[20]; ///< 网卡 MAC 地址
} NIC_CONTENT, *PNIC_CONTENT;
#ifdef __cplusplus // If used by C++ code,

View File

@ -67,6 +67,7 @@ static void InitTunnelSDKLog(const TCHAR *pLogFile, LOG_LEVEL level) {
const auto dupFileFilter = std::make_shared<spdlog::sinks::dup_filter_sink_st>(std::chrono::seconds(5));
const auto dupStdFilter = std::make_shared<spdlog::sinks::dup_filter_sink_st>(std::chrono::seconds(5));
//std::make_shared<spdlog::sinks::rotating_file_sink_mt>(buf, 1024 * 1024 * 5, 10)->
dupFileFilter->add_sink(std::make_shared<spdlog::sinks::daily_file_sink_mt>(buf, 2, 30));
//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>());

View File

@ -2,7 +2,6 @@
#include "CppUnitTest.h"
#include "sccsdk.h"
#include <iostream>
#include <strsafe.h>
#include <cppcodec/base64_url_unpadded.hpp>
@ -16,10 +15,10 @@ TEST_MODULE_INITIALIZE(ModuleInitialize) {
const TCHAR *path = TEXT(
"C:\\Users\\HuangXin\\Documents\\development\\visual_studio\\tunnel_windows\\NetTunnelApp\\bin\\Debug");
Logger::WriteMessage("In Module Initialize\n");
Logger::WriteMessage("In Module Initialize:\n");
Assert::AreEqual(0,
TunnelSDKInitEnv(path,
"http://172.21.40.39:32549",
"https://112.17.28.201:1443",
TEXT("C:\\Users\\HuangXin\\Documents\\development\\visual_studio\\tunnel_"
"windows\\NetTunnelApp\\bin\\Debug\\utest.log"),
LOG_TRACE,
@ -199,18 +198,17 @@ public:
TEST_METHOD(TestBase64UrlEncode) {
const std::string src = "iTeBvsuhkSOIoLB/t8qC/dBOu1lhLWwIZ675nF37MwM=";
using base64urlunPad = cppcodec::base64_url_unpadded;
using base64url = cppcodec::base64_url;
using base64 = cppcodec::base64_rfc4648;
using base64urlunPad = cppcodec::base64_url_unpadded;
using base64url = cppcodec::base64_url;
using base64 = cppcodec::base64_rfc4648;
std::string base = base64::encode(src);
std::string baseurl = base64url::encode(src);
std::string base = base64::encode(src);
std::string baseurl = base64url::encode(src);
std::string baseunpad = base64urlunPad::encode(src);
//std::string dbase = base64::decode(base);
//std::cout << base64::encode(pSrc, lstrlen(pSrc)) << std::endl;
//std::cout << base64::encode(pSrc, lstrlen(pSrc)) << std::endl;
//std::string encode = ::encode(pSrc);
Logger::WriteMessage("[");
Logger::WriteMessage(base64::encode(src).c_str());
@ -219,7 +217,20 @@ public:
TEST_METHOD(TestUserLogin) {
PUSER_CLIENT_CONFIG pCfg;
Assert::AreEqual(RET_OK, GetUserClientConfigure(TEXT("admin"), TEXT("1689189114026041344"), &pCfg));
Assert::AreEqual(
RET_OK,
GetUserClientConfigure(
TEXT("admin"),
TEXT("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9."
"eyJ1c2VyX25hbWUiOiIxNjg5MTAyMjczNzEzMTg0NzY4Iiwic2NvcGUiOlsiYWxsIl0sInVzZXJUeXBlIjoidXNlciIsImV4c"
"CI6MTY5NDg1NDM0MSwiYXV0aG9yaXRpZXMiOlsiUk9MRV9SRVRBSU58MCJdLCJqdGkiOiJDOG5HNFdFSWRIdGRDWDJ3X084a2"
"xORlpYNVUiLCJhY2NvdW50IjoiY2hlbnhpYW5nIiwiY2xpZW50X2lkIjoic2Mtd2ludmRpc2RrLWVmYTl2MTJ4d3R6NWVwcHI"
"ifQ.cPJaSruDAc1BAuDnSqyvYaswGhS0jNB7MIw6pWqEwgdCBVK2JZy42t9t8MQ2xlsS41nsE0ddm-"
"qzZPPS51T5cx7SYhp8eOJva_"
"HNfWbH0H2yfYYzwe6CzjrHIT0S7wegK75tT38ZUEGQJVdVliauDO1cfj7FjKKddauBZoycLuZf7QC4O1_"
"omRkhN7r4eHvh1ZmPXarONumc0BuR7eMEu9qfbEX9IkIWzBXX-7qyAeMEdsDjROohKn9g9M1xs1_"
"uPGVclhUxhwp24038EMeMkHT9WYinZokbKIaIfFF1MCioWNSJc3BnxMXLdOKzeegt8EqYOBbmjn0SkWwnHeAevQ"),
&pCfg));
}
TEST_METHOD(TestClientProgress) {