备份-基础ssl通信

This commit is contained in:
睿 安
2026-01-23 08:39:07 +08:00
parent ef6b8511b1
commit dbb053a691
625 changed files with 305003 additions and 0 deletions

200
Server/Server.vcxproj Normal file
View File

@@ -0,0 +1,200 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<ProjectGuid>{8A3F7B5E-9D2C-4E1A-B3F6-7C8D9E0F1A2B}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>Server</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>..\..\$(Configuration)\x86\</OutDir>
<IntDir>$(OutDir)obj\$(SolutionName)\$(ProjectName)\</IntDir>
<TargetName>$(SolutionName)-$(ProjectName)</TargetName>
<IncludePath>..\..\..\Dependent\openssl\14x\x86\include;$(IncludePath)</IncludePath>
<LibraryPath>..\..\..\Lib\HPSocket\x86\static;..\..\..\Dependent\openssl\14x\x86\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<OutDir>..\..\$(Configuration)\x86\</OutDir>
<IntDir>$(OutDir)obj\$(SolutionName)\$(ProjectName)\</IntDir>
<TargetName>$(SolutionName)-$(ProjectName)</TargetName>
<IncludePath>..\..\..\Dependent\openssl\14x\x86\include;$(IncludePath)</IncludePath>
<LibraryPath>..\..\..\Lib\HPSocket\x86\static;..\..\..\Dependent\openssl\14x\x86\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<OutDir>..\..\$(Configuration)\x64\</OutDir>
<IntDir>$(OutDir)obj\$(SolutionName)\$(ProjectName)\</IntDir>
<TargetName>$(SolutionName)-$(ProjectName)</TargetName>
<IncludePath>..\..\..\Dependent\openssl\14x\x64\include;..\_lib\protobuf\include;$(IncludePath)</IncludePath>
<LibraryPath>..\..\..\Lib\HPSocket\x64\static;..\..\..\Dependent\openssl\14x\x64\lib;..\_lib\protobuf\lib\X64;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<OutDir>..\..\$(Configuration)\x64\</OutDir>
<IntDir>$(OutDir)obj\$(SolutionName)\$(ProjectName)\</IntDir>
<TargetName>$(SolutionName)-$(ProjectName)</TargetName>
<IncludePath>..\..\..\Dependent\openssl\14x\x64\include;$(IncludePath)</IncludePath>
<LibraryPath>..\..\..\Lib\HPSocket\x64\static;..\..\..\Dependent\openssl\14x\x64\lib;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_SSL_SUPPORT;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>HPSocket_U_Debug_x86.lib;crypt32.lib;libssl.lib;libcrypto.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_SSL_SUPPORT;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>HPSocket_U_Release_x86.lib;crypt32.lib;libssl.lib;libcrypto.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_SSL_SUPPORT;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<BufferSecurityCheck>false</BufferSecurityCheck>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>HPSocket_U_Debug_x64.lib;crypt32.lib;libssl.lib;libcrypto.lib;ws2_32.lib;libprotobuf.lib
;libprotoc.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_SSL_SUPPORT;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>false</GenerateDebugInformation>
<AdditionalDependencies>HPSocket_U_Release_x64.lib;crypt32.lib;libssl.lib;libcrypto.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\Include\HPSocket\HPTypeDef.h" />
<ClInclude Include="..\..\..\Include\HPSocket\SocketInterface.h" />
<ClInclude Include="..\..\..\Src\SSLHelper.h" />
<ClInclude Include="..\..\..\Src\SSLServer.h" />
<ClInclude Include="..\..\..\Src\TcpServer.h" />
<ClInclude Include="pch.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>

255
Server/main.cpp Normal file
View File

@@ -0,0 +1,255 @@
// TestEcho-SSL-Console Server
// 基于HP-Socket的SSL服务器控制台程序
#include "pch.h"
// SSL证书配置来自helper.cpp
// 服务端证书
static const char* g_s_lpszPemCert = R"(-----BEGIN CERTIFICATE-----
MIIEJjCCAw6gAwIBAgIBAzANBgkqhkiG9w0BAQsFADB7MQswCQYDVQQGEwJDTjEL
MAkGA1UECAwCR0QxCzAJBgNVBAcMAkdaMQwwCgYDVQQKDANTU1QxDzANBgNVBAsM
Bkplc3NtYTETMBEGA1UEAwwKamVzc21hLm9yZzEeMBwGCSqGSIb3DQEJAR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QzIvNzDuoLklE44g8XuqsZZlOZ2wRWJxc/hDG0MkKhRnc1
mqeaoY/79QZNE1RvX/aRRJoSl7NQ00/rMP8MU6OMzPvbIsMVK2uT+BVZG0RZJXaG
ikQJvxYZrDVZdRZL6tWPtS2wI49KkzGHNH4S1Fni/dDq3P2rxzisMY1gtKQLeVYY
eTQDDybjTWWiTg==
-----END CERTIFICATE-----
)";
// 服务端私钥
static const char* g_s_lpszPemKey = R"(-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFHTBXBgkqhkiG9w0BBQ0wSjApBgkqhkiG9w0BBQwwHAQIgAqUxS2ufB0CAggA
MAwGCCqGSIb3DQIJBQAwHQYJYIZIAWUDBAEqBBD8E9f397mpmzmM9zYTWt2DBIIE
wIo2i0YH7cT/WCLmSnVvpsbayeo0mbVUFS4xp2VerQUS+UXHlrrOFeKU8NYfG6SS
m+3PXksvUlDV9iGT0p/w2Fm1GVKhE7UAeTJkff7KWi2pc926DUbYxhFOUNyCnrQS
pWCdV6M/243A0kHg6kIlSblsXjzKMC6eSlh2FGa7M7my9A+nu0WGOqvazkOm/8jk
h20xmB3t/Qa8NQp59E9oLXs5+CokIKL17+PmtBKeKpQBxAUtFjcqRAugIiNpPjOS
K99cY/Tt1C8ugMIsFH1/4HzFDYiSDRZ630dKOzBruprHVkIvQXI1CW6edPlfFZKG
yHeH95kigPVWUxWwluwALVmlPwD4h/GWHGOS7HH6x4ubStogjmC2B9f4VQCd6YRu
lee3cdDvdqLoRCoU48SoM/+RMR3NpF508ulWhKrDZ4eGcqdUYrx3rWVyOrmfMJDe
kfckGKnhCA1hL3wb/HQuAs2st2lDKBwsYIsOc8UXhueRFHKk+W0O/5kureMPF14O
DAxqAW3meHq7CLY8WuqatptIrJVDT8Wkbx47tXhLapTwlI0Cbf2AGfJIWSGmM+VF
I5l13pW2tycQnAXzSdd0y9wE0df/EoyXfIJeEfBNkzVhkIIC3KmOiI8BCnei6UR9
jun4+6aeFyfGybJ04ybixXyFsCsVUa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Zoe
zbQGBxCXpDOtMs1vujzcl/iZbSzwEDF3g4la5U8q4MlmfGFKz9CJbvoxecqYA206
nNbW2XZsW/xMiQv6iAw5iP/LOR9HAyxcvXIsL790nfcgnTYLmyP254Dj4outc6R+
PA+f/c1FvkbUBTR5vJt2tsvHcNU218rY2hyOIhDmZeUWprqBO19sUk3scLbVPr3+
WEWEl2XaCekKuPtAnMgVQuFsocXGyiuIhkOe5Z4=
-----END CERTIFICATE-----
)";
static const int g_s_iVerifyMode = SSL_VM_PEER | SSL_VM_FAIL_IF_NO_PEER_CERT;
static const char* g_s_lpszKeyPassword = "123456";
static const LPCTSTR ADDRESS = _T("0.0.0.0");
static const USHORT PORT = 5555;
// 读取文件内容到字节向量
inline std::vector<unsigned char> readFile(const std::string& path) {
std::ifstream f(path, std::ios::binary);
return std::vector<unsigned char>(std::istreambuf_iterator<char>(f), {});
}
// 服务器监听器类
class CServerListener : public CTcpServerListener
{
public:
virtual EnHandleResult OnPrepareListen(ITcpServer* pSender, SOCKET soListen) override
{
TCHAR szAddress[100];
int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
USHORT usPort;
pSender->GetListenAddress(szAddress, iAddressLen, usPort);
std::wcout << L"[服务器] 准备监听: " << szAddress << L":" << usPort << std::endl;
return HR_OK;
}
virtual EnHandleResult OnAccept(ITcpServer* pSender, CONNID dwConnID, UINT_PTR soClient) override
{
TCHAR szAddress[100];
int iAddressLen = sizeof(szAddress) / sizeof(TCHAR);
USHORT usPort;
pSender->GetRemoteAddress(dwConnID, szAddress, iAddressLen, usPort);
std::wcout << L"[服务器] 客户端连接: ID=" << dwConnID
<< L", 地址=" << szAddress << L":" << usPort << std::endl;
return HR_OK;
}
virtual EnHandleResult OnHandShake(ITcpServer* pSender, CONNID dwConnID) override
{
std::wcout << L"[服务器] SSL握手完成: ID=" << dwConnID << std::endl;
return HR_OK;
}
virtual EnHandleResult OnReceive(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength) override
{
std::string strData((char*)pData, iLength);
std::cout << "[服务器] 接收到消息: ID=" << dwConnID << ", 内容=\"" << strData << " from client\"" << std::endl;
// 回显数据给客户端
std::string response = "hello!";
std::vector<unsigned char> fileData = readFile("Client.exe");
//if (pSender->Send(dwConnID, fileData.data(), (int)fileData.size()))
if (pSender->Send(dwConnID, (const BYTE*)response.c_str(), (int)response.size()))
{
std::cout << "[服务器] 发送数据,长度:" << response.size() << std::endl;
}
else
{
std::cout << "[服务器] 发送响应失败" << std::endl;
}
return HR_OK;
}
virtual EnHandleResult OnSend(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength) override
{
return HR_OK;
}
virtual EnHandleResult OnClose(ITcpServer* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode) override
{
if (iErrorCode == SE_OK)
{
std::wcout << L"[服务器] 客户端断开: ID=" << dwConnID << std::endl;
}
else
{
std::wcout << L"[服务器] 客户端连接错误: ID=" << dwConnID
<< L", 错误码=" << iErrorCode << std::endl;
}
return HR_OK;
}
virtual EnHandleResult OnShutdown(ITcpServer* pSender) override
{
std::wcout << L"[服务器] 服务器关闭" << std::endl;
return HR_OK;
}
};
int main()
{
// 设置控制台UTF-8编码和locale
SetConsoleOutputCP(CP_UTF8);
std::locale::global(std::locale(""));
std::wcout.imbue(std::locale(""));
std::wcout << L"========================================" << std::endl;
std::wcout << L" HP-Socket SSL Server 控制台演示程序" << std::endl;
std::wcout << L"========================================" << std::endl;
std::wcout << std::endl;
// 创建监听器和服务器
CServerListener listener;
CSSLServerPtr server(&listener);
// 初始化SSL上下文
std::wcout << L"[服务器] 正在初始化SSL环境..." << std::endl;
if (!server->SetupSSLContextByMemory(g_s_iVerifyMode, g_s_lpszPemCert, g_s_lpszPemKey,
g_s_lpszKeyPassword, g_s_lpszCAPemCert))
{
std::wcout << L"[服务器] SSL初始化失败错误码: " << server->GetLastError() << std::endl;
std::wcout << L"按任意键退出..." << std::endl;
_getch();
return 1;
}
std::wcout << L"[服务器] SSL环境初始化成功" << std::endl;
// 启动服务器
std::wcout << L"[服务器] 正在启动服务器..." << std::endl;
if (!server->Start(ADDRESS, PORT))
{
std::wcout << L"[服务器] 启动失败!错误码: " << server->GetLastError() << std::endl;
std::wcout << L"按任意键退出..." << std::endl;
_getch();
return 1;
}
std::wcout << L"[服务器] 服务器启动成功,监听端口: " << PORT << std::endl;
std::wcout << L"[服务器] 按 'Q' 键停止服务器..." << std::endl;
std::wcout << std::endl;
// 等待用户输入
while (true)
{
if (_kbhit())
{
char ch = _getch();
if (ch == 'q' || ch == 'Q')
{
break;
}
}
Sleep(100);
}
// 停止服务器
std::wcout << L"[服务器] 正在停止服务器..." << std::endl;
if (server->Stop())
{
std::wcout << L"[服务器] 服务器已停止" << std::endl;
}
// 清理SSL上下文
server->CleanupSSLContext();
std::wcout << L"按任意键退出..." << std::endl;
_getch();
return 0;
}

5
Server/pch.cpp Normal file
View File

@@ -0,0 +1,5 @@
// pch.cpp: 与预编译标头对应的源文件
#include "pch.h"
// 当使用预编译的头时,需要使用此源文件,编译才能成功。

33
Server/pch.h Normal file
View File

@@ -0,0 +1,33 @@

#pragma once
// 静态库定义(必须在包含HPSocket之前)
#define HPSOCKET_STATIC_LIB
// SSL 支持定义(必须在包含HPSocket之前)
#define _NEED_SSL
#define _SSL_SUPPORT
// 防止winsock.h被包含使用winsock2.h
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
// 标准库
#include <iostream>
#include <string>
#include <locale>
#include <conio.h>
#include <tchar.h>
#include <vector>
#include <filesystem>
#include <sstream>
#include <fstream>
// Windows 头文件
#include <windows.h>
#include <winsock2.h>
// HP-Socket 头文件
#include "../../../Include/HPSocket/HPSocket.h"
#include "../../../Include/HPSocket/HPSocket-SSL.h"