Initial import
This commit is contained in:
commit
e49998d867
61
CCEngine.sln
Normal file
61
CCEngine.sln
Normal file
@ -0,0 +1,61 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 15
|
||||
VisualStudioVersion = 15.0.27130.2036
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CCEngine", "CCEngine\CCEngine.vcxproj", "{51637EB3-9942-43AE-9272-9DD85412EFC7}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libModuleManagerTest", "libModuleManagerTest\libModuleManagerTest.vcxproj", "{45ECA063-033A-47D0-B8A8-5F7B504C4CE9}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CCEngineLoader", "CCEngineLoader\CCEngineLoader.vcxproj", "{450E76A9-E271-4349-9C06-3C0540F9B00F}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libLicenseClient", "..\libLicenseClient\libLicenseClient.vcxproj", "{51345E59-83E5-4389-93A9-0131B40522B7}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{51637EB3-9942-43AE-9272-9DD85412EFC7}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{51637EB3-9942-43AE-9272-9DD85412EFC7}.Debug|x64.Build.0 = Debug|x64
|
||||
{51637EB3-9942-43AE-9272-9DD85412EFC7}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{51637EB3-9942-43AE-9272-9DD85412EFC7}.Debug|x86.Build.0 = Debug|Win32
|
||||
{51637EB3-9942-43AE-9272-9DD85412EFC7}.Release|x64.ActiveCfg = Release|x64
|
||||
{51637EB3-9942-43AE-9272-9DD85412EFC7}.Release|x64.Build.0 = Release|x64
|
||||
{51637EB3-9942-43AE-9272-9DD85412EFC7}.Release|x86.ActiveCfg = Release|Win32
|
||||
{51637EB3-9942-43AE-9272-9DD85412EFC7}.Release|x86.Build.0 = Release|Win32
|
||||
{45ECA063-033A-47D0-B8A8-5F7B504C4CE9}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{45ECA063-033A-47D0-B8A8-5F7B504C4CE9}.Debug|x64.Build.0 = Debug|x64
|
||||
{45ECA063-033A-47D0-B8A8-5F7B504C4CE9}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{45ECA063-033A-47D0-B8A8-5F7B504C4CE9}.Debug|x86.Build.0 = Debug|Win32
|
||||
{45ECA063-033A-47D0-B8A8-5F7B504C4CE9}.Release|x64.ActiveCfg = Release|x64
|
||||
{45ECA063-033A-47D0-B8A8-5F7B504C4CE9}.Release|x64.Build.0 = Release|x64
|
||||
{45ECA063-033A-47D0-B8A8-5F7B504C4CE9}.Release|x86.ActiveCfg = Release|Win32
|
||||
{45ECA063-033A-47D0-B8A8-5F7B504C4CE9}.Release|x86.Build.0 = Release|Win32
|
||||
{450E76A9-E271-4349-9C06-3C0540F9B00F}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{450E76A9-E271-4349-9C06-3C0540F9B00F}.Debug|x64.Build.0 = Debug|x64
|
||||
{450E76A9-E271-4349-9C06-3C0540F9B00F}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{450E76A9-E271-4349-9C06-3C0540F9B00F}.Debug|x86.Build.0 = Debug|Win32
|
||||
{450E76A9-E271-4349-9C06-3C0540F9B00F}.Release|x64.ActiveCfg = Release|x64
|
||||
{450E76A9-E271-4349-9C06-3C0540F9B00F}.Release|x64.Build.0 = Release|x64
|
||||
{450E76A9-E271-4349-9C06-3C0540F9B00F}.Release|x86.ActiveCfg = Release|Win32
|
||||
{450E76A9-E271-4349-9C06-3C0540F9B00F}.Release|x86.Build.0 = Release|Win32
|
||||
{51345E59-83E5-4389-93A9-0131B40522B7}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{51345E59-83E5-4389-93A9-0131B40522B7}.Debug|x64.Build.0 = Debug|x64
|
||||
{51345E59-83E5-4389-93A9-0131B40522B7}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{51345E59-83E5-4389-93A9-0131B40522B7}.Debug|x86.Build.0 = Debug|Win32
|
||||
{51345E59-83E5-4389-93A9-0131B40522B7}.Release|x64.ActiveCfg = Release|x64
|
||||
{51345E59-83E5-4389-93A9-0131B40522B7}.Release|x64.Build.0 = Release|x64
|
||||
{51345E59-83E5-4389-93A9-0131B40522B7}.Release|x86.ActiveCfg = Release|Win32
|
||||
{51345E59-83E5-4389-93A9-0131B40522B7}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {4CBA44D0-8074-48F8-A6DD-5914FE3F0026}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
158
CCEngine/CCEngine.cpp
Normal file
158
CCEngine/CCEngine.cpp
Normal file
@ -0,0 +1,158 @@
|
||||
#include "SystemParamsProvider_win.h"
|
||||
#include "HTTPClient.h"
|
||||
#include "LicenseClient.h"
|
||||
#include "CCServer.h"
|
||||
#include "ModuleManager.h"
|
||||
#include "JSONModuleDatabase.h"
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
auto appDir = boost::filesystem::system_complete(argv[0]).parent_path();
|
||||
SystemParamsProvider_win idProvider;
|
||||
|
||||
HTTPClient httpClient;
|
||||
LicenseClient licenseClient(idProvider, (appDir / "license.dat").string());
|
||||
licenseClient.init();
|
||||
|
||||
if (!licenseClient.isActivated())
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!licenseClient.tryPreactivate(httpClient))
|
||||
{
|
||||
std::string licenseKey;
|
||||
std::cout << "Your installation is not yet activated." << std::endl <<
|
||||
"Please enter license key: ";
|
||||
std::getline(std::cin, licenseKey);
|
||||
auto validLicenseKey = validateLicenseKey(licenseKey);
|
||||
if (!validLicenseKey)
|
||||
{
|
||||
throw std::runtime_error("Invalid license key. Please try again.");
|
||||
}
|
||||
else if (!licenseClient.activate(httpClient, validLicenseKey.value()))
|
||||
{
|
||||
throw std::runtime_error("Activation failed. Please try again.");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (CouldNotConnectException&)
|
||||
{
|
||||
std::cerr << "Error: The system could not be activated since your system seems offline. Please check your internet connection!" << std::endl;
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
JSONModuleDatabase moduleDatabase((appDir / ".inst").string());
|
||||
auto currentModules = moduleDatabase.listModules();
|
||||
std::vector<ModuleUpdate> moduleUpdates;
|
||||
try
|
||||
{
|
||||
moduleUpdates = licenseClient.checkForUpdates(httpClient, currentModules);
|
||||
}
|
||||
catch (CouldNotConnectException&)
|
||||
{
|
||||
std::cout << "Warning: Could not check for udates since your system looks offlince" << std::endl;
|
||||
}
|
||||
|
||||
bool restartRequired = false;
|
||||
if (!moduleUpdates.empty())
|
||||
{
|
||||
char what;
|
||||
std::cout << "Updates available. Do you want to update now? [yn]: ";
|
||||
std::cin >> what;
|
||||
if (what == 'y')
|
||||
{
|
||||
ModuleManager moduleManager(appDir.string(), moduleDatabase, httpClient);
|
||||
for (const auto& update : moduleUpdates)
|
||||
{
|
||||
restartRequired = restartRequired || (update.flag & static_cast<uint32_t>(ModuleUpdateFlags::restartRequired));
|
||||
moduleManager.applyUpdate(update.moduleId, update);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (restartRequired)
|
||||
{
|
||||
std::cout << "Restart required" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto docRoot = appDir / "data";
|
||||
|
||||
CCServer server("127.0.0.1", 8080, docRoot.string(), 4);
|
||||
server.run();
|
||||
|
||||
char c = 0;
|
||||
while (c != 'q')
|
||||
{
|
||||
std::cin >> c;
|
||||
}
|
||||
|
||||
server.shutdown();
|
||||
|
||||
/* if (!licenseManager->isActivated())
|
||||
{
|
||||
activate(systemId);
|
||||
}
|
||||
|
||||
if (!licenseManager->checkActivation(systemId))
|
||||
{
|
||||
throw std::runtime_error("Integrity error. Please contact support.");
|
||||
} */
|
||||
|
||||
// Check command line arguments.
|
||||
/*
|
||||
if (argc != 5)
|
||||
{
|
||||
std::cerr <<
|
||||
"Usage: ccengine <address> <port> <doc_root> <threads>\n" <<
|
||||
"Example:\n" <<
|
||||
" advanced-server 0.0.0.0 8080 . 1\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
auto const address = boost::asio::ip::make_address(argv[1]);
|
||||
auto const port = static_cast<unsigned short>(std::atoi(argv[2]));
|
||||
std::string const doc_root = argv[3];
|
||||
auto const threads = std::max<int>(1, std::atoi(argv[4]));
|
||||
|
||||
// The io_context is required for all I/O
|
||||
boost::asio::io_context ioc{ threads };
|
||||
|
||||
// Create and launch a listening port
|
||||
std::make_shared<listener>(
|
||||
ioc,
|
||||
tcp::endpoint{ address, port },
|
||||
doc_root)->run();
|
||||
|
||||
// Run the I/O service on the requested number of threads
|
||||
std::vector<std::thread> v;
|
||||
v.reserve(threads);
|
||||
for (auto i = threads - 1; i >= 0; --i)
|
||||
v.emplace_back(
|
||||
[&ioc]
|
||||
{
|
||||
ioc.run();
|
||||
});
|
||||
|
||||
|
||||
{
|
||||
HTTPClient httpClient;
|
||||
std::ofstream os("testfile.txt", std::ofstream::binary);
|
||||
|
||||
httpClient.downloadFile("https://github.com/cpp-netlib/cpp-netlib/raw/master/libs/network/example/http_client.cpp", os);
|
||||
}
|
||||
|
||||
{
|
||||
LicenseClient licenseClient(idProvider, "license.dat");
|
||||
licenseClient.init();
|
||||
} */
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
208
CCEngine/CCEngine.vcxproj
Normal file
208
CCEngine/CCEngine.vcxproj
Normal file
@ -0,0 +1,208 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>15.0</VCProjectVersion>
|
||||
<ProjectGuid>{51637EB3-9942-43AE-9272-9DD85412EFC7}</ProjectGuid>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<RootNamespace>CCEngine</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0.16299.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>v141</PlatformToolset>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>MultiByte</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" />
|
||||
<Import Project="..\..\thirdparty\boost-1.66.0\boost.props" />
|
||||
<Import Project="..\..\thirdparty\curl-7.58.0\curl-static.props" />
|
||||
<Import Project="..\..\thirdparty\zlib-1.2.11\zlib-static.props" />
|
||||
<Import Project="..\..\thirdparty\libzip-1.4.0\libzip-static.props" />
|
||||
<Import Project="..\..\thirdparty\cryptopp-6.1.0\cryptopp-static.props" />
|
||||
</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" />
|
||||
<Import Project="..\..\thirdparty\boost-1.66.0\boost.props" />
|
||||
<Import Project="..\..\thirdparty\curl-7.58.0\curl-static.props" />
|
||||
<Import Project="..\..\thirdparty\zlib-1.2.11\zlib-static.props" />
|
||||
<Import Project="..\..\thirdparty\libzip-1.4.0\libzip-static.props" />
|
||||
<Import Project="..\..\thirdparty\cryptopp-6.1.0\cryptopp-static.props" />
|
||||
</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>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_WIN32_WINNT=0x0501;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir)..\..\..\openssl\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<LanguageStandard>stdcpplatest</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalOptions>/verbose:lib %(AdditionalOptions)</AdditionalOptions>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_WIN32_WINNT=0x0501;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir)..\..\..\openssl\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<LanguageStandard>stdcpplatest</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_WIN32_WINNT=0x0501;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions);BOOST_EXCEPTION_DISABLE</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir)..\..\..\openssl\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<LanguageStandard>stdcpplatest</LanguageStandard>
|
||||
<CreateHotpatchableImage>false</CreateHotpatchableImage>
|
||||
<RuntimeTypeInfo>false</RuntimeTypeInfo>
|
||||
<DebugInformationFormat>None</DebugInformationFormat>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>false</GenerateDebugInformation>
|
||||
<StripPrivateSymbols>YES</StripPrivateSymbols>
|
||||
<AssemblyDebug>false</AssemblyDebug>
|
||||
<ProgramDatabaseFile />
|
||||
<FullProgramDatabaseFile>false</FullProgramDatabaseFile>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>MaxSpeed</Optimization>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_WIN32_WINNT=0x0501;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir)..\..\..\openssl\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<LanguageStandard>stdcpplatest</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="CCEngine.cpp" />
|
||||
<ClCompile Include="CCServer.cpp" />
|
||||
<ClCompile Include="HashUtils.cpp" />
|
||||
<ClCompile Include="HTTPClient.cpp" />
|
||||
<ClCompile Include="JSONModuleDatabase.cpp" />
|
||||
<ClCompile Include="JSONSerialization.cpp" />
|
||||
<ClCompile Include="LicenseClient.cpp" />
|
||||
<ClCompile Include="ModuleManager.cpp" />
|
||||
<ClCompile Include="ModuleUpdate.cpp" />
|
||||
<ClCompile Include="SystemParams.cpp" />
|
||||
<ClCompile Include="SystemParamsProvider_win.cpp" />
|
||||
<ClCompile Include="updclient\CCUpdClient.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="CCServer.h" />
|
||||
<ClInclude Include="HashUtils.h" />
|
||||
<ClInclude Include="HTTPClient.h" />
|
||||
<ClInclude Include="IModuleDatabase.h" />
|
||||
<ClInclude Include="JSONModuleDatabase.h" />
|
||||
<ClInclude Include="JSONSerialization.h" />
|
||||
<ClInclude Include="LicenseClient.h" />
|
||||
<ClInclude Include="ModuleManager.h" />
|
||||
<ClInclude Include="ModuleUpdate.h" />
|
||||
<ClInclude Include="ModuleVersion.h" />
|
||||
<ClInclude Include="SystemParamsProvider.h" />
|
||||
<ClInclude Include="SystemParamsProvider_win.h" />
|
||||
<ClInclude Include="SystemParams.h" />
|
||||
<ClInclude Include="updclient\CCUpdClient.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
92
CCEngine/CCEngine.vcxproj.filters
Normal file
92
CCEngine/CCEngine.vcxproj.filters
Normal file
@ -0,0 +1,92 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Resource Files">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="src">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="src\updclient">
|
||||
<UniqueIdentifier>{62810fe9-1f5f-41c9-a552-642d73dbcfcb}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="updclient\CCUpdClient.cpp">
|
||||
<Filter>src\updclient</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JSONSerialization.cpp">
|
||||
<Filter>src\updclient</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="ModuleManager.cpp">
|
||||
<Filter>src\updclient</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="JSONModuleDatabase.cpp">
|
||||
<Filter>src\updclient</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="HTTPClient.cpp">
|
||||
<Filter>src\updclient</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="SystemParamsProvider_win.cpp">
|
||||
<Filter>src\updclient</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="SystemParams.cpp">
|
||||
<Filter>src\updclient</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="LicenseClient.cpp">
|
||||
<Filter>src</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="HashUtils.cpp">
|
||||
<Filter>src</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CCServer.cpp">
|
||||
<Filter>src</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="ModuleUpdate.cpp">
|
||||
<Filter>src</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CCEngine.cpp">
|
||||
<Filter>src\updclient</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="updclient\CCUpdClient.h">
|
||||
<Filter>src\updclient</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="ModuleManager.h">
|
||||
<Filter>src\updclient</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="JSONSerialization.h">
|
||||
<Filter>src\updclient</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="JSONModuleDatabase.h">
|
||||
<Filter>src\updclient</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="IModuleDatabase.h">
|
||||
<Filter>src\updclient</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="HTTPClient.h">
|
||||
<Filter>src\updclient</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="SystemParams.h">
|
||||
<Filter>src\updclient</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="SystemParamsProvider_win.h">
|
||||
<Filter>src\updclient</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="LicenseClient.h" />
|
||||
<ClInclude Include="SystemParamsProvider.h">
|
||||
<Filter>src</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="ModuleUpdate.h">
|
||||
<Filter>src\updclient</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="ModuleVersion.h">
|
||||
<Filter>src\updclient</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="HashUtils.h" />
|
||||
<ClInclude Include="CCServer.h" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
807
CCEngine/CCServer.cpp
Normal file
807
CCEngine/CCServer.cpp
Normal file
@ -0,0 +1,807 @@
|
||||
#include "CCServer.h"
|
||||
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/version.hpp>
|
||||
#include <boost/asio/bind_executor.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/asio/strand.hpp>
|
||||
#include <boost/asio/steady_timer.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/crc.hpp>
|
||||
#include <boost/iostreams/filtering_stream.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/tokenizer.hpp>
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <shared_mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace fs = boost::filesystem; // from <boost/filesystem.hpp>
|
||||
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
|
||||
namespace http = boost::beast::http; // from <boost/beast/http.hpp>
|
||||
|
||||
static constexpr auto APIS_UINT8_MIN = std::numeric_limits<uint8_t>::min();
|
||||
static constexpr auto APIS_UINT8_MAX = std::numeric_limits<uint8_t>::max();
|
||||
|
||||
#define MAXHESLO 225
|
||||
#define MAX_DEKODHESLO (MAXHESLO-3)
|
||||
|
||||
#define KodcharToChar(znak1,znak2,poloha,viet) { \
|
||||
(znak1)=(-(password[poloha]-APIS_UINT8_MIN)+(~(znak2))-(poloha)-(viet)); \
|
||||
if((znak1)<APIS_UINT8_MIN) (znak1)=(-(APIS_UINT8_MIN-(znak1)))&APIS_UINT8_MAX; \
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
static const std::string cryptFileHeader("##CAPISF## Copyright (c) 2003 APIS spol. s r.o. SLOVAKIA~");
|
||||
static const std::string password("flckdscwjregfvfgbujvbnqajmadmgfjmhfmghplgghesrrhsufservvysfidgfysdnmsybsaftoprecofoplyrmnfsvcapridssedalenttrmfusposvresalbostreqnnapswolnfrdweneplmnzcavsfgusqootrpqaressnvclpacfdrgesklojphsdqtyczvxcbfhtryntinokusgkhjgchtx");
|
||||
|
||||
class apis_read_filter
|
||||
{
|
||||
public:
|
||||
typedef char char_type;
|
||||
explicit apis_read_filter(size_t initialPosition)
|
||||
: m_position(initialPosition)
|
||||
{ }
|
||||
|
||||
struct category
|
||||
: boost::iostreams::input
|
||||
, boost::iostreams::filter_tag
|
||||
, boost::iostreams::multichar_tag
|
||||
, boost::iostreams::optimally_buffered_tag
|
||||
{};
|
||||
|
||||
std::streamsize optimal_buffer_size() const { return 0; }
|
||||
|
||||
template<typename Source>
|
||||
std::streamsize read(Source& src, char_type* s, std::streamsize n)
|
||||
{
|
||||
std::streamsize result = boost::iostreams::read(src, s, n);
|
||||
if (result == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int dwPacketPos = ((m_position / MAX_DEKODHESLO) % 29) + 2;
|
||||
int iPoloha = m_position % MAX_DEKODHESLO;
|
||||
|
||||
m_position += result;
|
||||
|
||||
for (int i = 0; i < result; i++)
|
||||
{
|
||||
if (iPoloha == MAX_DEKODHESLO)
|
||||
{
|
||||
iPoloha = 0;
|
||||
dwPacketPos++;
|
||||
}
|
||||
if (dwPacketPos >= 31)
|
||||
{
|
||||
dwPacketPos = 2;
|
||||
}
|
||||
|
||||
int znak;
|
||||
KodcharToChar(znak, s[i], iPoloha, dwPacketPos);
|
||||
s[i] = znak;
|
||||
++iPoloha;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
private:
|
||||
size_t m_position;
|
||||
};
|
||||
|
||||
void decryptBuffer(std::vector<uint8_t>& buffer)
|
||||
{
|
||||
bool encrypted = false;
|
||||
if (buffer.size() >= cryptFileHeader.size())
|
||||
{
|
||||
encrypted = std::equal(cryptFileHeader.begin(), cryptFileHeader.end(), buffer.begin());
|
||||
}
|
||||
if (!encrypted)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
boost::iostreams::array_source src{ (const char*)buffer.data() + cryptFileHeader.size(), buffer.size() - cryptFileHeader.size()};
|
||||
boost::iostreams::filtering_istream is;
|
||||
is.push(apis_read_filter({ cryptFileHeader.size() }));
|
||||
is.push(src);
|
||||
|
||||
std::vector<uint8_t> decrypted(buffer.size() - cryptFileHeader.size());
|
||||
is.read((char*)decrypted.data(), decrypted.size());
|
||||
if (decrypted.size() > sizeof(uint32_t))
|
||||
{
|
||||
auto headerLen = reinterpret_cast<uint32_t*>(decrypted.data())[0];
|
||||
if (decrypted.size() > headerLen)
|
||||
{
|
||||
decrypted.erase(decrypted.begin(), decrypted.begin() + headerLen);
|
||||
}
|
||||
}
|
||||
buffer = decrypted;
|
||||
}
|
||||
|
||||
class etag_cache
|
||||
{
|
||||
public:
|
||||
void insert(boost::beast::string_view path, boost::beast::string_view etag)
|
||||
{
|
||||
std::unique_lock<std::shared_mutex> lock(mutex_);
|
||||
map_.emplace(path, etag);
|
||||
}
|
||||
|
||||
bool check_path(boost::beast::string_view path, boost::beast::string_view etag) const
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> lock(mutex_);
|
||||
auto it = map_.find(static_cast<std::string>(path));
|
||||
if (it != map_.end() && it->second == etag)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
std::unique_lock<std::shared_mutex> lock(mutex_);
|
||||
map_.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
mutable std::shared_mutex mutex_;
|
||||
std::unordered_map<std::string, std::string> map_;
|
||||
};
|
||||
|
||||
// Return a reasonable mime type based on the extension of a file.
|
||||
boost::beast::string_view
|
||||
mime_type(boost::beast::string_view path)
|
||||
{
|
||||
using boost::beast::iequals;
|
||||
auto const ext = [&path]
|
||||
{
|
||||
auto const pos = path.rfind(".");
|
||||
if (pos == boost::beast::string_view::npos)
|
||||
return boost::beast::string_view{};
|
||||
return path.substr(pos);
|
||||
}();
|
||||
if (iequals(ext, ".htm")) return "text/html";
|
||||
if (iequals(ext, ".html")) return "text/html";
|
||||
if (iequals(ext, ".php")) return "text/html";
|
||||
if (iequals(ext, ".css")) return "text/css";
|
||||
if (iequals(ext, ".txt")) return "text/plain";
|
||||
if (iequals(ext, ".js")) return "application/javascript";
|
||||
if (iequals(ext, ".json")) return "application/json";
|
||||
if (iequals(ext, ".xml")) return "application/xml";
|
||||
if (iequals(ext, ".swf")) return "application/x-shockwave-flash";
|
||||
if (iequals(ext, ".flv")) return "video/x-flv";
|
||||
if (iequals(ext, ".png")) return "image/png";
|
||||
if (iequals(ext, ".jpe")) return "image/jpeg";
|
||||
if (iequals(ext, ".jpeg")) return "image/jpeg";
|
||||
if (iequals(ext, ".jpg")) return "image/jpeg";
|
||||
if (iequals(ext, ".gif")) return "image/gif";
|
||||
if (iequals(ext, ".bmp")) return "image/bmp";
|
||||
if (iequals(ext, ".ico")) return "image/vnd.microsoft.icon";
|
||||
if (iequals(ext, ".tiff")) return "image/tiff";
|
||||
if (iequals(ext, ".tif")) return "image/tiff";
|
||||
if (iequals(ext, ".svg")) return "image/svg+xml";
|
||||
if (iequals(ext, ".svgz")) return "image/svg+xml";
|
||||
return "application/text";
|
||||
}
|
||||
|
||||
// Append an HTTP rel-path to a local filesystem path.
|
||||
// The returned path is normalized for the platform.
|
||||
std::string
|
||||
path_cat(
|
||||
boost::beast::string_view base,
|
||||
boost::beast::string_view path)
|
||||
{
|
||||
if (base.empty())
|
||||
return path.to_string();
|
||||
std::string result = base.to_string();
|
||||
#if BOOST_MSVC
|
||||
char constexpr path_separator = '\\';
|
||||
if (result.back() == path_separator)
|
||||
result.resize(result.size() - 1);
|
||||
result.append(path.data(), path.size());
|
||||
for (auto& c : result)
|
||||
if (c == '/')
|
||||
c = path_separator;
|
||||
#else
|
||||
char constexpr path_separator = '/';
|
||||
if (result.back() == path_separator)
|
||||
result.resize(result.size() - 1);
|
||||
result.append(path.data(), path.size());
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
||||
// This function produces an HTTP response for the given
|
||||
// request. The type of the response object depends on the
|
||||
// contents of the request, so the interface requires the
|
||||
// caller to pass a generic lambda for receiving the response.
|
||||
template<
|
||||
class Body, class Allocator,
|
||||
class Send>
|
||||
void
|
||||
handle_request(
|
||||
boost::beast::string_view doc_root,
|
||||
etag_cache& cache,
|
||||
http::request<Body, http::basic_fields<Allocator>>&& req,
|
||||
Send&& send)
|
||||
{
|
||||
// Returns a bad request response
|
||||
auto const bad_request =
|
||||
[&req](boost::beast::string_view why)
|
||||
{
|
||||
http::response<http::string_body> res{ http::status::bad_request, req.version() };
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
res.set(http::field::content_type, "text/html");
|
||||
res.keep_alive(req.keep_alive());
|
||||
res.body() = why.to_string();
|
||||
res.prepare_payload();
|
||||
return res;
|
||||
};
|
||||
|
||||
// Returns a not found response
|
||||
auto const not_found =
|
||||
[&req](boost::beast::string_view target)
|
||||
{
|
||||
http::response<http::string_body> res{ http::status::not_found, req.version() };
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
res.set(http::field::content_type, "text/html");
|
||||
res.keep_alive(req.keep_alive());
|
||||
res.body() = "The resource '" + target.to_string() + "' was not found.";
|
||||
res.prepare_payload();
|
||||
return res;
|
||||
};
|
||||
|
||||
// Returns a server error response
|
||||
auto const server_error =
|
||||
[&req](boost::beast::string_view what)
|
||||
{
|
||||
http::response<http::string_body> res{ http::status::internal_server_error, req.version() };
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
res.set(http::field::content_type, "text/html");
|
||||
res.keep_alive(req.keep_alive());
|
||||
res.body() = "An error occurred: '" + what.to_string() + "'";
|
||||
res.prepare_payload();
|
||||
return res;
|
||||
};
|
||||
|
||||
// Returns a server error response
|
||||
auto const not_modified =
|
||||
[&req](boost::beast::string_view etag)
|
||||
{
|
||||
http::response<http::empty_body> res{ http::status::not_modified, req.version() };
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
res.set(http::field::etag, etag);
|
||||
res.keep_alive(req.keep_alive());
|
||||
res.prepare_payload();
|
||||
return res;
|
||||
};
|
||||
|
||||
// Make sure we can handle the method
|
||||
if (req.method() != http::verb::get &&
|
||||
req.method() != http::verb::head)
|
||||
return send(bad_request("Unknown HTTP-method"));
|
||||
|
||||
// Request path must be absolute and not contain "..".
|
||||
if (req.target().empty() ||
|
||||
req.target()[0] != '/' ||
|
||||
req.target().find("..") != boost::beast::string_view::npos)
|
||||
return send(bad_request("Illegal request-target"));
|
||||
|
||||
auto it = req.find(http::field::if_none_match);
|
||||
if (it != req.end())
|
||||
{
|
||||
auto etags = std::string(it->value());
|
||||
|
||||
typedef boost::tokenizer<boost::char_separator<char>> Tokenizer;
|
||||
boost::char_separator<char> sep(", ");
|
||||
Tokenizer tok{ etags, sep };
|
||||
for (Tokenizer::iterator tokIt = tok.begin(); tokIt != tok.end(); ++tokIt)
|
||||
{
|
||||
std::string etag(*tokIt);
|
||||
boost::string_view checksum(etag);
|
||||
if (checksum.front() == '"')
|
||||
{
|
||||
checksum.remove_prefix(1);
|
||||
checksum.remove_suffix(1);
|
||||
}
|
||||
|
||||
if (cache.check_path(req.target(), checksum))
|
||||
{
|
||||
return send(not_modified(etag));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Build the path to the requested file
|
||||
std::string path = path_cat(doc_root, req.target());
|
||||
if (req.target().back() == '/')
|
||||
path.append("index.html");
|
||||
|
||||
// Attempt to open the file
|
||||
if (!boost::filesystem::is_regular_file(path))
|
||||
{
|
||||
return send(not_found(req.target()));
|
||||
}
|
||||
|
||||
std::ifstream body(path, std::ifstream::binary);
|
||||
|
||||
body.seekg(0, std::ios::end);
|
||||
size_t size = body.tellg();
|
||||
std::vector<uint8_t> buffer(size, 0);
|
||||
body.seekg(0);
|
||||
body.read((char*)buffer.data(), size);
|
||||
body.close();
|
||||
|
||||
decryptBuffer(buffer);
|
||||
size = buffer.size();
|
||||
|
||||
boost::crc_32_type result;
|
||||
result.process_bytes(buffer.data(), buffer.size());
|
||||
std::ostringstream checksum;
|
||||
checksum << std::hex << result.checksum();
|
||||
|
||||
auto etag = std::string("\"") + checksum.str() + "\"";
|
||||
cache.insert(req.target(), checksum.str());
|
||||
|
||||
// Respond to HEAD request
|
||||
if (req.method() == http::verb::head)
|
||||
{
|
||||
http::response<http::empty_body> res{ http::status::ok, req.version() };
|
||||
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
|
||||
res.set(http::field::content_type, mime_type(path));
|
||||
res.content_length(size);
|
||||
res.keep_alive(req.keep_alive());
|
||||
return send(std::move(res));
|
||||
}
|
||||
|
||||
// Respond to GET request
|
||||
// TODO: replace this by decryption
|
||||
http::response<http::buffer_body> res{ http::status::ok, req.version() };
|
||||
res.set(http::field::server, "Based on " BOOST_BEAST_VERSION_STRING);
|
||||
res.set(http::field::content_type, mime_type(path));
|
||||
res.set(http::field::etag, etag);
|
||||
res.body().data = buffer.data();
|
||||
res.body().size = buffer.size();
|
||||
res.body().more = false;
|
||||
res.content_length(size);
|
||||
res.keep_alive(req.keep_alive());
|
||||
return send(std::move(res));
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// Report a failure
|
||||
void
|
||||
fail(boost::system::error_code ec, char const* what)
|
||||
{
|
||||
std::cerr << what << ": " << ec.message() << "\n";
|
||||
}
|
||||
|
||||
// Handles an HTTP server connection
|
||||
class http_session : public std::enable_shared_from_this<http_session>
|
||||
{
|
||||
// This queue is used for HTTP pipelining.
|
||||
class queue
|
||||
{
|
||||
enum
|
||||
{
|
||||
// Maximum number of responses we will queue
|
||||
limit = 8
|
||||
};
|
||||
|
||||
// The type-erased, saved work item
|
||||
struct work
|
||||
{
|
||||
virtual ~work() = default;
|
||||
virtual void operator()() = 0;
|
||||
};
|
||||
|
||||
http_session& self_;
|
||||
std::vector<std::unique_ptr<work>> items_;
|
||||
|
||||
public:
|
||||
explicit
|
||||
queue(http_session& self)
|
||||
: self_(self)
|
||||
{
|
||||
static_assert(limit > 0, "queue limit must be positive");
|
||||
items_.reserve(limit);
|
||||
}
|
||||
|
||||
// Returns `true` if we have reached the queue limit
|
||||
bool
|
||||
is_full() const
|
||||
{
|
||||
return items_.size() >= limit;
|
||||
}
|
||||
|
||||
// Called when a message finishes sending
|
||||
// Returns `true` if the caller should initiate a read
|
||||
bool
|
||||
on_write()
|
||||
{
|
||||
BOOST_ASSERT(!items_.empty());
|
||||
auto const was_full = is_full();
|
||||
items_.erase(items_.begin());
|
||||
if (!items_.empty())
|
||||
(*items_.front())();
|
||||
return was_full;
|
||||
}
|
||||
|
||||
// Called by the HTTP handler to send a response.
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
void
|
||||
operator()(http::message<isRequest, Body, Fields>&& msg)
|
||||
{
|
||||
// This holds a work item
|
||||
struct work_impl : work
|
||||
{
|
||||
http_session& self_;
|
||||
http::message<isRequest, Body, Fields> msg_;
|
||||
|
||||
work_impl(
|
||||
http_session& self,
|
||||
http::message<isRequest, Body, Fields>&& msg)
|
||||
: self_(self)
|
||||
, msg_(std::move(msg))
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
operator()()
|
||||
{
|
||||
http::async_write(
|
||||
self_.socket_,
|
||||
msg_,
|
||||
boost::asio::bind_executor(
|
||||
self_.strand_,
|
||||
std::bind(
|
||||
&http_session::on_write,
|
||||
self_.shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
msg_.need_eof())));
|
||||
}
|
||||
};
|
||||
|
||||
// Allocate and store the work
|
||||
items_.push_back(std::make_unique<work_impl>(self_, std::move(msg)));
|
||||
|
||||
// If there was no previous work, start this one
|
||||
if (items_.size() == 1)
|
||||
(*items_.front())();
|
||||
}
|
||||
};
|
||||
|
||||
tcp::socket socket_;
|
||||
boost::asio::strand<
|
||||
boost::asio::io_context::executor_type> strand_;
|
||||
boost::asio::steady_timer timer_;
|
||||
boost::beast::flat_buffer buffer_;
|
||||
std::string const& doc_root_;
|
||||
etag_cache& etag_cache_;
|
||||
http::request<http::string_body> req_;
|
||||
queue queue_;
|
||||
|
||||
public:
|
||||
// Take ownership of the socket
|
||||
explicit
|
||||
http_session(
|
||||
tcp::socket socket,
|
||||
std::string const& doc_root,
|
||||
etag_cache& etagCache)
|
||||
: socket_(std::move(socket))
|
||||
, strand_(socket_.get_executor())
|
||||
, timer_(socket_.get_executor().context(),
|
||||
(std::chrono::steady_clock::time_point::max)())
|
||||
, doc_root_(doc_root)
|
||||
, etag_cache_(etagCache)
|
||||
, queue_(*this)
|
||||
{
|
||||
}
|
||||
|
||||
// Start the asynchronous operation
|
||||
void
|
||||
run()
|
||||
{
|
||||
// Run the timer. The timer is operated
|
||||
// continuously, this simplifies the code.
|
||||
on_timer({});
|
||||
|
||||
do_read();
|
||||
}
|
||||
|
||||
void
|
||||
do_read()
|
||||
{
|
||||
// Set the timer
|
||||
timer_.expires_after(std::chrono::seconds(15));
|
||||
|
||||
// Read a request
|
||||
http::async_read(socket_, buffer_, req_,
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&http_session::on_read,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1)));
|
||||
}
|
||||
|
||||
// Called when the timer expires.
|
||||
void
|
||||
on_timer(boost::system::error_code ec)
|
||||
{
|
||||
if (ec && ec != boost::asio::error::operation_aborted)
|
||||
return fail(ec, "timer");
|
||||
|
||||
// Verify that the timer really expired since the deadline may have moved.
|
||||
if (timer_.expiry() <= std::chrono::steady_clock::now())
|
||||
{
|
||||
// Closing the socket cancels all outstanding operations. They
|
||||
// will complete with boost::asio::error::operation_aborted
|
||||
socket_.shutdown(tcp::socket::shutdown_both, ec);
|
||||
socket_.close(ec);
|
||||
return;
|
||||
}
|
||||
|
||||
// Wait on the timer
|
||||
timer_.async_wait(
|
||||
boost::asio::bind_executor(
|
||||
strand_,
|
||||
std::bind(
|
||||
&http_session::on_timer,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1)));
|
||||
}
|
||||
|
||||
void
|
||||
on_read(boost::system::error_code ec)
|
||||
{
|
||||
// Happens when the timer closes the socket
|
||||
if (ec == boost::asio::error::operation_aborted)
|
||||
return;
|
||||
|
||||
// This means they closed the connection
|
||||
if (ec == http::error::end_of_stream)
|
||||
return do_close();
|
||||
|
||||
if (ec)
|
||||
return fail(ec, "read");
|
||||
|
||||
// Send the response
|
||||
handle_request(doc_root_, etag_cache_, std::move(req_), queue_);
|
||||
|
||||
// If we aren't at the queue limit, try to pipeline another request
|
||||
if (!queue_.is_full())
|
||||
do_read();
|
||||
}
|
||||
|
||||
void
|
||||
on_write(boost::system::error_code ec, bool close)
|
||||
{
|
||||
// Happens when the timer closes the socket
|
||||
if (ec == boost::asio::error::operation_aborted)
|
||||
return;
|
||||
|
||||
if (ec)
|
||||
return fail(ec, "write");
|
||||
|
||||
if (close)
|
||||
{
|
||||
// This means we should close the connection, usually because
|
||||
// the response indicated the "Connection: close" semantic.
|
||||
return do_close();
|
||||
}
|
||||
|
||||
// Inform the queue that a write completed
|
||||
if (queue_.on_write())
|
||||
{
|
||||
// Read another request
|
||||
do_read();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
do_close()
|
||||
{
|
||||
// Send a TCP shutdown
|
||||
boost::system::error_code ec;
|
||||
socket_.shutdown(tcp::socket::shutdown_send, ec);
|
||||
|
||||
// At this point the connection is closed gracefully
|
||||
}
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
// Accepts incoming connections and launches the sessions
|
||||
class listener : public std::enable_shared_from_this<listener>
|
||||
{
|
||||
tcp::acceptor acceptor_;
|
||||
tcp::socket socket_;
|
||||
std::string const& doc_root_;
|
||||
etag_cache& etag_cache_;
|
||||
|
||||
public:
|
||||
listener(
|
||||
boost::asio::io_context& ioc,
|
||||
tcp::endpoint endpoint,
|
||||
std::string const& doc_root,
|
||||
etag_cache& etagCache)
|
||||
: acceptor_(ioc)
|
||||
, socket_(ioc)
|
||||
, doc_root_(doc_root)
|
||||
, etag_cache_(etagCache)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
|
||||
// Open the acceptor
|
||||
acceptor_.open(endpoint.protocol(), ec);
|
||||
if (ec)
|
||||
{
|
||||
fail(ec, "open");
|
||||
return;
|
||||
}
|
||||
|
||||
// Bind to the server address
|
||||
acceptor_.bind(endpoint, ec);
|
||||
if (ec)
|
||||
{
|
||||
fail(ec, "bind");
|
||||
return;
|
||||
}
|
||||
|
||||
// Start listening for connections
|
||||
acceptor_.listen(
|
||||
boost::asio::socket_base::max_listen_connections, ec);
|
||||
if (ec)
|
||||
{
|
||||
fail(ec, "listen");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Start accepting incoming connections
|
||||
void
|
||||
run()
|
||||
{
|
||||
if (!acceptor_.is_open())
|
||||
return;
|
||||
do_accept();
|
||||
}
|
||||
|
||||
void
|
||||
do_accept()
|
||||
{
|
||||
acceptor_.async_accept(
|
||||
socket_,
|
||||
std::bind(
|
||||
&listener::on_accept,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1));
|
||||
}
|
||||
|
||||
void
|
||||
on_accept(boost::system::error_code ec)
|
||||
{
|
||||
if (ec)
|
||||
{
|
||||
fail(ec, "accept");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Create the http_session and run it
|
||||
std::make_shared<http_session>(
|
||||
std::move(socket_),
|
||||
doc_root_,
|
||||
etag_cache_)->run();
|
||||
}
|
||||
|
||||
// Accept another connection
|
||||
do_accept();
|
||||
}
|
||||
};
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
class CCServerImpl final
|
||||
{
|
||||
public:
|
||||
CCServerImpl(const std::string& addressToListen, uint16_t portToListen, const std::string& docRoot, int threads)
|
||||
: m_maxThreads{std::max(1, threads)}
|
||||
, m_docRoot{ docRoot }
|
||||
, m_etagCache{}
|
||||
, m_endpoint{ boost::asio::ip::make_address(addressToListen), portToListen }
|
||||
, m_ioc{ m_maxThreads }
|
||||
, m_threads{}
|
||||
{
|
||||
}
|
||||
|
||||
~CCServerImpl()
|
||||
{
|
||||
shutdown();
|
||||
}
|
||||
|
||||
public:
|
||||
void run()
|
||||
{
|
||||
// Create and launch a listening port
|
||||
std::make_shared<listener>(
|
||||
m_ioc,
|
||||
m_endpoint,
|
||||
m_docRoot,
|
||||
m_etagCache)->run();
|
||||
|
||||
// Run the I/O service on the requested number of threads
|
||||
m_threads.reserve(m_maxThreads);
|
||||
for (auto i = m_maxThreads - 1; i >= 0; --i)
|
||||
{
|
||||
m_threads.emplace_back([&]()
|
||||
{
|
||||
m_ioc.run();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void shutdown()
|
||||
{
|
||||
m_ioc.stop();
|
||||
for (auto& t : m_threads)
|
||||
{
|
||||
t.join();
|
||||
}
|
||||
m_threads.clear();
|
||||
}
|
||||
|
||||
void clearCache()
|
||||
{
|
||||
m_etagCache.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
int m_maxThreads;
|
||||
std::string m_docRoot;
|
||||
etag_cache m_etagCache;
|
||||
tcp::endpoint m_endpoint;
|
||||
boost::asio::io_context m_ioc;
|
||||
std::vector<std::thread> m_threads;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
CCServer::CCServer(const std::string& addressToListen, uint16_t portToListen, const std::string& docRoot, int threads)
|
||||
: m_impl(std::make_unique<detail::CCServerImpl>(addressToListen, portToListen, docRoot, threads))
|
||||
{}
|
||||
|
||||
|
||||
CCServer::~CCServer()
|
||||
{}
|
||||
|
||||
void CCServer::shutdown()
|
||||
{
|
||||
m_impl->shutdown();
|
||||
}
|
||||
|
||||
void CCServer::clearCache()
|
||||
{
|
||||
m_impl->clearCache();
|
||||
}
|
||||
|
||||
void CCServer::run()
|
||||
{
|
||||
m_impl->run();
|
||||
}
|
||||
|
||||
24
CCEngine/CCServer.h
Normal file
24
CCEngine/CCServer.h
Normal file
@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace detail
|
||||
{
|
||||
class CCServerImpl;
|
||||
}
|
||||
|
||||
class CCServer final
|
||||
{
|
||||
public:
|
||||
CCServer(const std::string& addressToListen, uint16_t portToListen, const std::string& docRoot, int threads);
|
||||
~CCServer();
|
||||
|
||||
public:
|
||||
void run();
|
||||
void shutdown();
|
||||
void clearCache();
|
||||
|
||||
private:
|
||||
std::unique_ptr<detail::CCServerImpl> m_impl;
|
||||
};
|
||||
312
CCEngine/HTTPClient.cpp
Normal file
312
CCEngine/HTTPClient.cpp
Normal file
@ -0,0 +1,312 @@
|
||||
#include "HTTPClient.h"
|
||||
|
||||
#include <curl/curl.h>
|
||||
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
static
|
||||
void dump(const char *text,
|
||||
FILE *stream, unsigned char *ptr, size_t size)
|
||||
{
|
||||
size_t i;
|
||||
size_t c;
|
||||
unsigned int width = 0x10;
|
||||
|
||||
fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
|
||||
text, (long)size, (long)size);
|
||||
|
||||
fwrite(ptr, 1, size, stream);
|
||||
fputc('\n', stream); // newline
|
||||
/* for (i = 0; i<size; i += width) {
|
||||
fprintf(stream, "%4.4lx: ", (long)i);
|
||||
|
||||
// show hex to the left
|
||||
for (c = 0; c < width; c++) {
|
||||
if (i + c < size)
|
||||
fprintf(stream, "%02x ", ptr[i + c]);
|
||||
else
|
||||
fputs(" ", stream);
|
||||
}
|
||||
|
||||
// show data on the right
|
||||
for (c = 0; (c < width) && (i + c < size); c++) {
|
||||
char x = (ptr[i + c] >= 0x20 && ptr[i + c] < 0x80) ? ptr[i + c] : '.';
|
||||
fputc(x, stream);
|
||||
}
|
||||
|
||||
fputc('\n', stream); // newline
|
||||
} */
|
||||
}
|
||||
|
||||
static
|
||||
int my_trace(CURL *handle, curl_infotype type,
|
||||
char *data, size_t size,
|
||||
void *userp)
|
||||
{
|
||||
const char *text;
|
||||
(void)handle; /* prevent compiler warning */
|
||||
(void)userp;
|
||||
|
||||
switch (type) {
|
||||
case CURLINFO_TEXT:
|
||||
fprintf(stderr, "== Info: %s", data);
|
||||
default: /* in case a new one is introduced to shock us */
|
||||
return 0;
|
||||
|
||||
case CURLINFO_HEADER_OUT:
|
||||
text = "=> Send header";
|
||||
break;
|
||||
case CURLINFO_DATA_OUT:
|
||||
text = "=> Send data";
|
||||
break;
|
||||
case CURLINFO_SSL_DATA_OUT:
|
||||
text = "=> Send SSL data";
|
||||
break;
|
||||
case CURLINFO_HEADER_IN:
|
||||
text = "<= Recv header";
|
||||
break;
|
||||
case CURLINFO_DATA_IN:
|
||||
text = "<= Recv data";
|
||||
break;
|
||||
case CURLINFO_SSL_DATA_IN:
|
||||
text = "<= Recv SSL data";
|
||||
break;
|
||||
}
|
||||
|
||||
dump(text, stderr, (unsigned char *)data, size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static size_t reader(char *ptr, size_t size, size_t nmemb, std::istream *is)
|
||||
{
|
||||
std::streamsize totalRead = 0;
|
||||
if (*is)
|
||||
{
|
||||
is->read(&ptr[totalRead], size * nmemb - totalRead);
|
||||
// is->read(&ptr[totalRead], 1);
|
||||
totalRead = is->gcount();
|
||||
}
|
||||
|
||||
return totalRead;
|
||||
}
|
||||
|
||||
static int writer(char *data, size_t size, size_t nmemb, std::ostream *os)
|
||||
{
|
||||
if (os == NULL)
|
||||
return 0;
|
||||
|
||||
os->write(data, size*nmemb);
|
||||
|
||||
return size * nmemb;
|
||||
}
|
||||
|
||||
struct CURLDeleter
|
||||
{
|
||||
void operator() (CURL* ptr)
|
||||
{
|
||||
if (ptr)
|
||||
{
|
||||
curl_easy_cleanup(ptr);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct curl_slist_deleter
|
||||
{
|
||||
void operator() (curl_slist* ptr)
|
||||
{
|
||||
if (ptr)
|
||||
{
|
||||
curl_slist_free_all(ptr);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct DownloadSession
|
||||
{
|
||||
char errorBuffer[CURL_ERROR_SIZE] = { 0 };
|
||||
std::unique_ptr<CURL, CURLDeleter> conn;
|
||||
std::unique_ptr<curl_slist, curl_slist_deleter> extraHeaders;
|
||||
};
|
||||
|
||||
DownloadSession initCurlRequest(const std::string& url, std::ostream &os)
|
||||
{
|
||||
DownloadSession result;
|
||||
CURLcode code;
|
||||
|
||||
std::unique_ptr<CURL, CURLDeleter> conn(curl_easy_init());
|
||||
|
||||
if (!conn)
|
||||
{
|
||||
throw std::runtime_error("Failed to create CURL connection");
|
||||
}
|
||||
|
||||
code = curl_easy_setopt(conn.get(), CURLOPT_ERRORBUFFER, result.errorBuffer);
|
||||
if (code != CURLE_OK)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Failed to set error buffer [" << code << "]";
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
|
||||
const char* errorBuffer = result.errorBuffer;
|
||||
code = curl_easy_setopt(conn.get(), CURLOPT_URL, url.c_str());
|
||||
if (code != CURLE_OK)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Failed to set URL [" << errorBuffer << "]";
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
|
||||
code = curl_easy_setopt(conn.get(), CURLOPT_FOLLOWLOCATION, 1L);
|
||||
if (code != CURLE_OK)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Failed to set redirect option [" << errorBuffer << "]";
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
|
||||
code = curl_easy_setopt(conn.get(), CURLOPT_MAXREDIRS, 5L);
|
||||
if (code != CURLE_OK)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Failed to set maximum number of redirects [" << errorBuffer << "]";
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
|
||||
code = curl_easy_setopt(conn.get(), CURLOPT_WRITEFUNCTION, writer);
|
||||
if (code != CURLE_OK)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Failed to set write function [" << errorBuffer << "]";
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
|
||||
code = curl_easy_setopt(conn.get(), CURLOPT_WRITEDATA, &os);
|
||||
if (code != CURLE_OK)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Failed to set write data [" << errorBuffer << "]";
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
|
||||
// DEBUG:
|
||||
code = curl_easy_setopt(conn.get(), CURLOPT_DEBUGFUNCTION, my_trace);
|
||||
code = curl_easy_setopt(conn.get(), CURLOPT_VERBOSE, 1);
|
||||
if (code != CURLE_OK)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Failed to set verbose mode";
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
|
||||
result.conn = std::move(conn);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
DownloadSession initCurlPostJsonRequest(const std::string& url, std::istream& is, size_t length, std::ostream &os)
|
||||
{
|
||||
DownloadSession result = initCurlRequest(url, os);
|
||||
CURLcode code;
|
||||
|
||||
auto contentLengthHeader = std::string("Content-Length: ") + std::to_string(length);
|
||||
|
||||
struct curl_slist *headers = NULL;
|
||||
headers = curl_slist_append(headers, "Accept: application/json");
|
||||
headers = curl_slist_append(headers, "Content-Type: application/json");
|
||||
headers = curl_slist_append(headers, contentLengthHeader.c_str());
|
||||
headers = curl_slist_append(headers, "Charsets: utf-8");
|
||||
result.extraHeaders.reset(headers);
|
||||
|
||||
code = curl_easy_setopt(result.conn.get(), CURLOPT_POST, 1L);
|
||||
if (code != CURLE_OK)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Failed to set post option [" << result.errorBuffer << "]";
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
|
||||
code = curl_easy_setopt(result.conn.get(), CURLOPT_HTTPHEADER, result.extraHeaders.get());
|
||||
if (code != CURLE_OK)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Failed to set http headers [" << result.errorBuffer << "]";
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
|
||||
code = curl_easy_setopt(result.conn.get(), CURLOPT_READFUNCTION, reader);
|
||||
if (code != CURLE_OK)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Failed to set read function [" << result.errorBuffer << "]";
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
|
||||
code = curl_easy_setopt(result.conn.get(), CURLOPT_READDATA, &is);
|
||||
if (code != CURLE_OK)
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "Failed to set read data [" << result.errorBuffer << "]";
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
HTTPClient::HTTPClient()
|
||||
{
|
||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
}
|
||||
|
||||
void HTTPClient::get(const std::string& url, std::ostream& dstStream)
|
||||
{
|
||||
auto session = initCurlRequest(url, dstStream);
|
||||
|
||||
auto code = curl_easy_perform(session.conn.get());
|
||||
session.conn.release();
|
||||
|
||||
if (code != CURLE_OK)
|
||||
{
|
||||
if (code == CURLE_COULDNT_CONNECT)
|
||||
{
|
||||
throw CouldNotConnectException();
|
||||
}
|
||||
|
||||
std::ostringstream oss;
|
||||
oss << "Failed to get '" << url << "' [" << session.errorBuffer << "]";
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
}
|
||||
|
||||
void HTTPClient::postJson(const std::string& url, std::istream& json, size_t length, std::ostream& dstStream)
|
||||
{
|
||||
auto session = initCurlPostJsonRequest(url, json, length, dstStream);
|
||||
|
||||
auto code = curl_easy_perform(session.conn.get());
|
||||
session.conn.release();
|
||||
|
||||
if (code != CURLE_OK)
|
||||
{
|
||||
if (code == CURLE_COULDNT_CONNECT)
|
||||
{
|
||||
throw CouldNotConnectException();
|
||||
}
|
||||
std::ostringstream oss;
|
||||
oss << "Failed to post '" << url << "' [" << session.errorBuffer << "]";
|
||||
throw std::runtime_error(oss.str());
|
||||
}
|
||||
}
|
||||
|
||||
void HTTPClient::postJson(const std::string& url, const std::string& json, std::ostream& dstStream)
|
||||
{
|
||||
std::istringstream is(json);
|
||||
return postJson(url, is, json.size(), dstStream);
|
||||
}
|
||||
24
CCEngine/HTTPClient.h
Normal file
24
CCEngine/HTTPClient.h
Normal file
@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <ostream>
|
||||
|
||||
class CouldNotConnectException : public std::exception
|
||||
{
|
||||
public:
|
||||
CouldNotConnectException()
|
||||
{}
|
||||
|
||||
virtual ~CouldNotConnectException() throw () {}
|
||||
};
|
||||
|
||||
class HTTPClient
|
||||
{
|
||||
public:
|
||||
HTTPClient();
|
||||
|
||||
public:
|
||||
void get(const std::string& url, std::ostream& dstStream) throw (CouldNotConnectException);
|
||||
void postJson(const std::string& url, std::istream& json, size_t length, std::ostream& dstStream) throw (CouldNotConnectException);
|
||||
void postJson(const std::string& url, const std::string& json, std::ostream& dstStream) throw (CouldNotConnectException);
|
||||
};
|
||||
22
CCEngine/HashUtils.cpp
Normal file
22
CCEngine/HashUtils.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
#include "HashUtils.h"
|
||||
|
||||
#include <cryptopp/sha.h>
|
||||
#include <cryptopp/files.h>
|
||||
#include <cryptopp/hex.h>
|
||||
|
||||
using CryptoPP::SHA256;
|
||||
using CryptoPP::FileSource;
|
||||
using CryptoPP::HashFilter;
|
||||
using CryptoPP::HexEncoder;
|
||||
using CryptoPP::StringSink;
|
||||
namespace fs = boost::filesystem;
|
||||
|
||||
std::string calcSHA256(const fs::path& file)
|
||||
{
|
||||
SHA256 hash;
|
||||
std::string digest;
|
||||
|
||||
FileSource f(file.native().c_str(), true, new HashFilter(hash, new HexEncoder(new StringSink(digest))));
|
||||
|
||||
return digest;
|
||||
}
|
||||
6
CCEngine/HashUtils.h
Normal file
6
CCEngine/HashUtils.h
Normal file
@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <string>
|
||||
|
||||
std::string calcSHA256(const boost::filesystem::path& file);
|
||||
27
CCEngine/IModuleDatabase.h
Normal file
27
CCEngine/IModuleDatabase.h
Normal file
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include "ModuleVersion.h"
|
||||
|
||||
#include <boost/optional.hpp>
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
struct Module
|
||||
{
|
||||
std::string moduleId;
|
||||
ModuleVersion version;
|
||||
std::set<std::string> filePaths;
|
||||
};
|
||||
|
||||
class IModuleDatabase
|
||||
{
|
||||
public:
|
||||
virtual std::map<std::string, ModuleVersion> listModules() = 0;
|
||||
virtual boost::optional<Module> findModule(const std::string& moduleId) = 0;
|
||||
virtual void storeModule(const Module& module) = 0;
|
||||
|
||||
protected:
|
||||
~IModuleDatabase() {}
|
||||
};
|
||||
117
CCEngine/JSONModuleDatabase.cpp
Normal file
117
CCEngine/JSONModuleDatabase.cpp
Normal file
@ -0,0 +1,117 @@
|
||||
#include "JSONModuleDatabase.h"
|
||||
|
||||
#include "JSONSerialization.h"
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/property_tree/ptree.hpp>
|
||||
#include <boost/property_tree/json_parser.hpp>
|
||||
|
||||
namespace pt = boost::property_tree;
|
||||
|
||||
void serialize(std::ostream& os, const Module& m)
|
||||
{
|
||||
os << "{";
|
||||
os << "moduleId:";
|
||||
serialize(os, m.moduleId);
|
||||
os << ",version:";
|
||||
serialize(os, m.version);
|
||||
os << ",filePaths:";
|
||||
serialize(os, m.filePaths);
|
||||
os << "}";
|
||||
}
|
||||
|
||||
void deserialize(const pt::ptree& tree, Module& m)
|
||||
{
|
||||
deserialize(tree.get_child("moduleId"), m.moduleId);
|
||||
deserialize(tree.get_child("version"), m.version);
|
||||
deserialize(tree.get_child("filePaths"), m.filePaths);
|
||||
}
|
||||
|
||||
JSONModuleDatabase::JSONModuleDatabase(const std::string & baseDir)
|
||||
: m_baseDir(baseDir)
|
||||
{
|
||||
if (!boost::filesystem::exists(baseDir))
|
||||
{
|
||||
boost::filesystem::create_directories(baseDir);
|
||||
}
|
||||
|
||||
if (!boost::filesystem::is_directory(baseDir))
|
||||
{
|
||||
throw std::runtime_error(".inst folder is not directory");
|
||||
}
|
||||
}
|
||||
|
||||
std::map<std::string, ModuleVersion> JSONModuleDatabase::listModules()
|
||||
{
|
||||
std::map<std::string, ModuleVersion> result;
|
||||
|
||||
boost::filesystem::path path(m_baseDir);
|
||||
path /= ".modules.json";
|
||||
if (boost::filesystem::is_regular_file(path))
|
||||
{
|
||||
pt::ptree root;
|
||||
pt::read_json(path.string(), root);
|
||||
|
||||
deserialize(root, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
boost::optional<Module> JSONModuleDatabase::findModule(const std::string & moduleId)
|
||||
{
|
||||
boost::filesystem::path path(m_baseDir);
|
||||
|
||||
if (!boost::filesystem::portable_file_name(moduleId))
|
||||
{
|
||||
throw std::runtime_error("Invalid module name");
|
||||
}
|
||||
|
||||
path /= moduleId;
|
||||
path /= ".json";
|
||||
if (boost::filesystem::is_regular_file(path))
|
||||
{
|
||||
pt::ptree root;
|
||||
pt::read_json(path.string(), root);
|
||||
|
||||
Module m;
|
||||
deserialize(root, m);
|
||||
return m;
|
||||
}
|
||||
|
||||
return boost::none;
|
||||
}
|
||||
|
||||
void JSONModuleDatabase::storeModule(const Module & module)
|
||||
{
|
||||
boost::filesystem::path path(m_baseDir);
|
||||
|
||||
if (!boost::filesystem::portable_file_name(module.moduleId))
|
||||
{
|
||||
throw std::runtime_error("Invalid module name");
|
||||
}
|
||||
|
||||
path /= module.moduleId + ".json";
|
||||
{
|
||||
std::ofstream os(path.string(), std::ostream::out);
|
||||
serialize(os, module);
|
||||
}
|
||||
|
||||
updateModuleList(module.moduleId, module.version);
|
||||
}
|
||||
|
||||
void JSONModuleDatabase::storeModuleList(const std::map<std::string, ModuleVersion>& moduleList)
|
||||
{
|
||||
boost::filesystem::path path(m_baseDir);
|
||||
path /= ".modules.json";
|
||||
|
||||
std::ofstream os(path.string(), std::ostream::out);
|
||||
serialize(os, moduleList);
|
||||
}
|
||||
|
||||
void JSONModuleDatabase::updateModuleList(const std::string & moduleId, const ModuleVersion & version)
|
||||
{
|
||||
auto list = listModules();
|
||||
list[moduleId] = version;
|
||||
storeModuleList(list);
|
||||
}
|
||||
23
CCEngine/JSONModuleDatabase.h
Normal file
23
CCEngine/JSONModuleDatabase.h
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "IModuleDatabase.h"
|
||||
|
||||
class JSONModuleDatabase final : public IModuleDatabase
|
||||
{
|
||||
public:
|
||||
JSONModuleDatabase(const std::string& baseDir);
|
||||
JSONModuleDatabase(const JSONModuleDatabase&) = delete;
|
||||
JSONModuleDatabase& operator=(const JSONModuleDatabase&) = delete;
|
||||
|
||||
public:
|
||||
std::map<std::string, ModuleVersion> listModules() override;
|
||||
boost::optional<Module> findModule(const std::string& moduleId) override;
|
||||
void storeModule(const Module& module) override;
|
||||
|
||||
private:
|
||||
void storeModuleList(const std::map<std::string, ModuleVersion>& moduleList);
|
||||
void updateModuleList(const std::string& moduleId, const ModuleVersion& version);
|
||||
|
||||
private:
|
||||
std::string m_baseDir;
|
||||
};
|
||||
15
CCEngine/JSONSerialization.cpp
Normal file
15
CCEngine/JSONSerialization.cpp
Normal file
@ -0,0 +1,15 @@
|
||||
#include "JSONSerialization.h"
|
||||
|
||||
namespace pt = boost::property_tree;
|
||||
|
||||
void serialize(std::ostream & os, const std::string & str)
|
||||
{
|
||||
os << '"'
|
||||
<< str
|
||||
<< '"';
|
||||
}
|
||||
|
||||
void deserialize(const pt::ptree & tree, std::string & stringVal)
|
||||
{
|
||||
stringVal = tree.get_value<std::string>();
|
||||
}
|
||||
118
CCEngine/JSONSerialization.h
Normal file
118
CCEngine/JSONSerialization.h
Normal file
@ -0,0 +1,118 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/property_tree/ptree.hpp>
|
||||
|
||||
#include <map>
|
||||
#include <ostream>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
void serialize(std::ostream& os, const std::string& str);
|
||||
|
||||
template<class T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
|
||||
void serialize(std::ostream& os, T intVal)
|
||||
{
|
||||
os << intVal;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void serialize(std::ostream& os, const std::vector<T>& vec)
|
||||
{
|
||||
os << "[";
|
||||
if (vec.size() > 0)
|
||||
{
|
||||
serialize(os, vec[0]);
|
||||
std::for_each(std::begin(vec) + 1, std::end(vec), [&os](const auto& el)
|
||||
{
|
||||
os << ',';
|
||||
serialize(os, el);
|
||||
});
|
||||
}
|
||||
os << "]";
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void serialize(std::ostream& os, const std::set<T>& setVal)
|
||||
{
|
||||
bool first = true;
|
||||
os << "[";
|
||||
std::for_each(std::begin(setVal), std::end(setVal), [&os, &first](const auto& el)
|
||||
{
|
||||
if (!first)
|
||||
{
|
||||
os << ',';
|
||||
}
|
||||
serialize(os, el);
|
||||
first = false;
|
||||
});
|
||||
os << "]";
|
||||
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void serialize(std::ostream& os, const std::map<std::string, T>& m)
|
||||
{
|
||||
bool first = true;
|
||||
os << "{";
|
||||
std::for_each(std::begin(m), std::end(m), [&os, &first](const auto& pair)
|
||||
{
|
||||
if (!first)
|
||||
{
|
||||
os << ',';
|
||||
}
|
||||
serialize(os, pair.first);
|
||||
os << ':';
|
||||
serialize(os, pair.second);
|
||||
first = false;
|
||||
});
|
||||
os << "}";
|
||||
}
|
||||
|
||||
void deserialize(const boost::property_tree::ptree& tree, std::string& stringVal);
|
||||
|
||||
template<class T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
|
||||
void deserialize(const boost::property_tree::ptree & tree, T& intVal)
|
||||
{
|
||||
intVal = tree.get_value<T>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void deserialize(const boost::property_tree::ptree& tree, std::vector<T>& vec)
|
||||
{
|
||||
vec.clear();
|
||||
for (const boost::property_tree::ptree::value_type &el : tree)
|
||||
{
|
||||
// el.first contain the string "" if it was JSON array
|
||||
T val;
|
||||
deserialize(el.second, val);
|
||||
vec.push_back(std::move(val));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void deserialize(const boost::property_tree::ptree& tree, std::set<T>& setVal)
|
||||
{
|
||||
setVal.clear();
|
||||
for (const boost::property_tree::ptree::value_type &el : tree)
|
||||
{
|
||||
// el.first contain the string "" if it was JSON array
|
||||
T val;
|
||||
deserialize(el.second, val);
|
||||
setVal.insert(std::move(val));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void deserialize(const boost::property_tree::ptree& tree, std::map<std::string, T>& mapVal)
|
||||
{
|
||||
mapVal.clear();
|
||||
for (const boost::property_tree::ptree::value_type &el : tree)
|
||||
{
|
||||
std::string key;
|
||||
T val;
|
||||
key = el.first;
|
||||
deserialize(el.second, val);
|
||||
mapVal.insert(std::make_pair(std::move(key), std::move(val)));
|
||||
}
|
||||
}
|
||||
667
CCEngine/LicenseClient.cpp
Normal file
667
CCEngine/LicenseClient.cpp
Normal file
@ -0,0 +1,667 @@
|
||||
#include "LicenseClient.h"
|
||||
|
||||
#include "JSONSerialization.h"
|
||||
|
||||
#include <cryptopp/cryptlib.h>
|
||||
#include <cryptopp/aes.h>
|
||||
#include <cryptopp/gcm.h>
|
||||
#include <cryptopp/osrng.h>
|
||||
#include <cryptopp/eccrypto.h>
|
||||
#include <cryptopp/oids.h>
|
||||
#include <cryptopp/files.h>
|
||||
#include <cryptopp/hex.h>
|
||||
#include <cryptopp/base64.h>
|
||||
#include <cryptopp/base32.h>
|
||||
#include <cryptopp/dsa.h>
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/iostreams/device/array.hpp>
|
||||
#include <boost/iostreams/filtering_stream.hpp>
|
||||
#include <boost/iostreams/filter/zlib.hpp>
|
||||
#include <boost/property_tree/json_parser.hpp>
|
||||
#include <boost/property_tree/ptree.hpp>
|
||||
|
||||
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <streambuf>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace pt = boost::property_tree;
|
||||
namespace fs = boost::filesystem;
|
||||
|
||||
using CryptoPP::AES;
|
||||
using CryptoPP::GCM;
|
||||
using CryptoPP::byte;
|
||||
using CryptoPP::SecByteBlock;
|
||||
using CryptoPP::AutoSeededRandomPool;
|
||||
using CryptoPP::AuthenticatedDecryptionFilter;
|
||||
using CryptoPP::StringSink;
|
||||
using CryptoPP::StringSource;
|
||||
using CryptoPP::Redirector;
|
||||
using CryptoPP::ECDSA;
|
||||
using CryptoPP::EC2N;
|
||||
using CryptoPP::SHA256;
|
||||
using CryptoPP::HexDecoder;
|
||||
using CryptoPP::Base64Decoder;
|
||||
using CryptoPP::Base32Decoder;
|
||||
using CryptoPP::Base32Encoder;
|
||||
namespace ASN1 = CryptoPP::ASN1;
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
static const std::string appId = "coc";
|
||||
|
||||
static const uint32_t initializationVectorSize = AES::BLOCKSIZE;
|
||||
static const uint32_t macTagSize = 16;
|
||||
static const uint32_t ecdsaSignatureSize = 72;
|
||||
|
||||
template< typename T >
|
||||
std::string intToHex(T i)
|
||||
{
|
||||
std::stringstream stream;
|
||||
stream
|
||||
<< std::setfill('0') << std::setw(sizeof(T) * 2)
|
||||
<< std::hex << i;
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
uint64_t fletcher64(uint32_t *data, int count)
|
||||
{
|
||||
uint64_t sum1 = 0;
|
||||
uint64_t sum2 = 0;
|
||||
int index;
|
||||
|
||||
for (index = 0; index < count; ++index)
|
||||
{
|
||||
sum1 = (sum1 + data[index]) % std::numeric_limits<uint32_t>::max();
|
||||
sum2 = (sum2 + sum1) % std::numeric_limits<uint32_t>::max();
|
||||
}
|
||||
|
||||
return (sum2 << std::numeric_limits<uint32_t>::digits) | sum1;
|
||||
}
|
||||
|
||||
typedef std::string ParamHash;
|
||||
ParamHash fletcher64(const std::string& input)
|
||||
{
|
||||
std::vector<uint32_t> buf((input.size() + sizeof(uint32_t) - 1) / sizeof(uint32_t), 0);
|
||||
std::copy_n(std::begin(input), input.size(), stdext::make_checked_array_iterator((std::string::pointer)buf.data(), buf.size() * sizeof(uint32_t)));
|
||||
auto resInt = fletcher64(buf.data(), buf.size());
|
||||
return intToHex(resInt);
|
||||
}
|
||||
|
||||
SystemParams skipEmptyParams(const SystemParams& systemParams)
|
||||
{
|
||||
SystemParams result;
|
||||
for (const auto& entry : systemParams)
|
||||
{
|
||||
if (!entry.second.empty())
|
||||
{
|
||||
result[entry.first] = entry.second;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
SystemParams hashParams(const SystemParams& systemParams)
|
||||
{
|
||||
SystemParams result;
|
||||
for (const auto& entry : systemParams)
|
||||
{
|
||||
result[entry.first] = fletcher64(entry.second);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string base64Decode(const std::string& encoded)
|
||||
{
|
||||
std::string decoded;
|
||||
|
||||
StringSource ss(encoded, true,
|
||||
new Base64Decoder(
|
||||
new StringSink(decoded)
|
||||
)
|
||||
);
|
||||
|
||||
return decoded;
|
||||
}
|
||||
|
||||
std::string base32Decode(const std::string& encoded)
|
||||
{
|
||||
// Decoder
|
||||
int lookup[256] = {0};
|
||||
const byte ALPHABET[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
||||
Base64Decoder::InitializeDecodingLookupArray(lookup, ALPHABET, 32, true);
|
||||
|
||||
Base32Decoder decoder;
|
||||
CryptoPP::AlgorithmParameters params = CryptoPP::MakeParameters(CryptoPP::Name::DecodingLookupArray(), (const int *)lookup);
|
||||
decoder.IsolatedInitialize(params);
|
||||
std::string decoded;
|
||||
|
||||
decoder.Attach(new StringSink(decoded));
|
||||
decoder.Put((const byte*)encoded.data(), encoded.size());
|
||||
decoder.MessageEnd();
|
||||
|
||||
return decoded;
|
||||
}
|
||||
|
||||
std::string base32Encode(const std::string& data)
|
||||
{
|
||||
// Encoder
|
||||
Base32Encoder encoder;
|
||||
const byte ALPHABET[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
||||
CryptoPP::AlgorithmParameters params = CryptoPP::MakeParameters(CryptoPP::Name::EncodingLookupArray(), (const byte *)ALPHABET);
|
||||
encoder.IsolatedInitialize(params);
|
||||
|
||||
std::string encoded;
|
||||
|
||||
encoder.Attach(new StringSink(encoded));
|
||||
encoder.Put((const byte*)data.data(), data.size());
|
||||
encoder.MessageEnd();
|
||||
|
||||
return encoded;
|
||||
}
|
||||
|
||||
typedef std::string Signature;
|
||||
|
||||
struct SignedData
|
||||
{
|
||||
std::string data;
|
||||
std::string signature;
|
||||
};
|
||||
|
||||
std::string readBinaryFile(const std::string& filename)
|
||||
{
|
||||
std::ifstream t(filename, std::istream::binary);
|
||||
std::string str;
|
||||
|
||||
t.seekg(0, std::ios::end);
|
||||
str.reserve(static_cast<size_t>(t.tellg()));
|
||||
t.seekg(0, std::ios::beg);
|
||||
|
||||
str.assign((std::istreambuf_iterator<char>(t)),
|
||||
std::istreambuf_iterator<char>());
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
struct PreactivationRequest
|
||||
{
|
||||
std::string appId = appId;
|
||||
SystemParams systemParams;
|
||||
};
|
||||
|
||||
struct ActivationRequest
|
||||
{
|
||||
std::string appId = appId;
|
||||
SystemParams systemParams;
|
||||
std::string licenseNumber;
|
||||
};
|
||||
|
||||
struct ActivationResponse
|
||||
{
|
||||
bool success;
|
||||
std::optional<std::string> licenseFile;
|
||||
};
|
||||
|
||||
struct CheckUpdatesRequest
|
||||
{
|
||||
SystemParams systemParams;
|
||||
std::string activationId;
|
||||
std::map<std::string, ModuleVersion> moduleVersions;
|
||||
};
|
||||
|
||||
struct CheckUpdatesResponse
|
||||
{
|
||||
bool success;
|
||||
std::optional<std::string> licenseFile;
|
||||
std::vector<ModuleUpdate> moduleUpdates;
|
||||
};
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
void serialize(std::ostream& os, const ActivationData& a)
|
||||
{
|
||||
os << "{";
|
||||
os << "\"activationId\":";
|
||||
serialize(os, a.activationId);
|
||||
os << "\"appId\":";
|
||||
serialize(os, a.appId);
|
||||
os << ",\"systemParams\":";
|
||||
serialize(os, a.systemParams);
|
||||
os << ",\"licensedModules\":";
|
||||
serialize(os, a.licensedModules);
|
||||
os << "}";
|
||||
}
|
||||
|
||||
void deserialize(const pt::ptree& tree, ActivationData& a)
|
||||
{
|
||||
deserialize(tree.get_child("activationId"), a.activationId);
|
||||
deserialize(tree.get_child("appId"), a.appId);
|
||||
deserialize(tree.get_child("systemParams"), a.systemParams);
|
||||
deserialize(tree.get_child("licensedModules"), a.licensedModules);
|
||||
}
|
||||
|
||||
void serialize(std::ostream& os, const SignedData& d)
|
||||
{
|
||||
os << "{";
|
||||
os << "\"data\":";
|
||||
serialize(os, d.data);
|
||||
os << ",\"signature\":";
|
||||
serialize(os, d.signature);
|
||||
os << "}";
|
||||
}
|
||||
|
||||
void deserialize(const pt::ptree& tree, SignedData& d)
|
||||
{
|
||||
deserialize(tree.get_child("data"), d.data);
|
||||
deserialize(tree.get_child("signature"), d.signature);
|
||||
}
|
||||
|
||||
void serialize(std::ostream& os, const PreactivationRequest& a)
|
||||
{
|
||||
os << "{";
|
||||
os << "\"appId\":";
|
||||
::serialize(os, a.appId);
|
||||
os << "\"systemParams\":";
|
||||
::serialize(os, a.systemParams);
|
||||
os << "}";
|
||||
}
|
||||
|
||||
void serialize(std::ostream& os, const ActivationRequest& a)
|
||||
{
|
||||
os << "{";
|
||||
os << "\"appId\":";
|
||||
::serialize(os, a.appId);
|
||||
os << "\"systemParams\":";
|
||||
::serialize(os, a.systemParams);
|
||||
os << ",\"licenseNumber\":";
|
||||
::serialize(os, a.licenseNumber);
|
||||
os << "}";
|
||||
}
|
||||
|
||||
void deserialize(const pt::ptree& tree, ActivationResponse& a)
|
||||
{
|
||||
deserialize(tree.get_child("success"), a.success);
|
||||
auto licenseFileOpt = tree.get_child_optional("licenseFile");
|
||||
if (licenseFileOpt)
|
||||
{
|
||||
std::string res;
|
||||
deserialize(licenseFileOpt.value(), res);
|
||||
a.licenseFile = base64Decode(res);
|
||||
}
|
||||
else
|
||||
{
|
||||
a.licenseFile = {};
|
||||
}
|
||||
}
|
||||
|
||||
void serialize(std::ostream& os, const CheckUpdatesRequest& r)
|
||||
{
|
||||
os << "{";
|
||||
os << "\"systemParams\":";
|
||||
::serialize(os, r.systemParams);
|
||||
os << ",\"activationId\":";
|
||||
::serialize(os, r.activationId);
|
||||
os << ",\"moduleVersions\":";
|
||||
::serialize(os, r.moduleVersions);
|
||||
os << "}";
|
||||
}
|
||||
|
||||
void deserialize(const pt::ptree& tree, CheckUpdatesResponse& r)
|
||||
{
|
||||
deserialize(tree.get_child("success"), r.success);
|
||||
auto licenseFileOpt = tree.get_child_optional("licenseFile");
|
||||
if (licenseFileOpt)
|
||||
{
|
||||
std::string res;
|
||||
deserialize(licenseFileOpt.value(), res);
|
||||
r.licenseFile = base64Decode(res);
|
||||
}
|
||||
else
|
||||
{
|
||||
r.licenseFile = {};
|
||||
}
|
||||
deserialize(tree.get_child("moduleUpdates"), r.moduleUpdates);
|
||||
}
|
||||
|
||||
|
||||
std::optional<std::string> validateLicenseKey(const std::string& licenseKey)
|
||||
{
|
||||
auto result = base32Decode(licenseKey);
|
||||
if (result.size() != 15)
|
||||
return {};
|
||||
return base32Encode(result);
|
||||
}
|
||||
|
||||
LicenseClient::LicenseClient(SystemParamsProvider& systemParamsProvider, const std::string& licenseFile)
|
||||
: m_systemParamsProvider(systemParamsProvider)
|
||||
, m_licenseFile(licenseFile)
|
||||
{
|
||||
}
|
||||
|
||||
LicenseClient::~LicenseClient()
|
||||
{
|
||||
}
|
||||
|
||||
void LicenseClient::init()
|
||||
{
|
||||
m_systemParams = hashParams(skipEmptyParams(m_systemParamsProvider.retrieveSystemParams()));
|
||||
|
||||
loadActivationData();
|
||||
}
|
||||
|
||||
bool LicenseClient::tryPreactivate(HTTPClient &httpClient)
|
||||
{
|
||||
PreactivationRequest req{ appId, m_systemParams };
|
||||
|
||||
std::string jsonReq;
|
||||
{
|
||||
std::ostringstream ss1;
|
||||
serialize(ss1, req);
|
||||
jsonReq = ss1.str();
|
||||
}
|
||||
|
||||
std::string jsonRes;
|
||||
{
|
||||
std::ostringstream ss2;
|
||||
httpClient.postJson("http://localhost:3000/activate0", jsonReq, ss2);
|
||||
jsonRes = ss2.str();
|
||||
}
|
||||
|
||||
ActivationResponse activationResponse;
|
||||
pt::ptree root;
|
||||
std::istringstream ss2(jsonRes);
|
||||
pt::read_json(ss2, root);
|
||||
deserialize(root, activationResponse);
|
||||
if (activationResponse.success)
|
||||
{
|
||||
{
|
||||
const auto& licenseData = activationResponse.licenseFile.value();
|
||||
std::ofstream os(m_licenseFile, std::ofstream::binary);
|
||||
os.write(licenseData.data(), licenseData.size());
|
||||
}
|
||||
return loadActivationData();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool LicenseClient::activate(HTTPClient &httpClient, const std::string & licenseNumber)
|
||||
{
|
||||
std::string jsonReq;
|
||||
|
||||
std::ostringstream ss1;
|
||||
|
||||
ActivationRequest req{ appId, m_systemParams, licenseNumber };
|
||||
serialize(ss1, req);
|
||||
|
||||
std::stringstream ss2;
|
||||
httpClient.postJson("http://localhost:3000/activate", ss1.str() , ss2);
|
||||
|
||||
ActivationResponse activationResponse;
|
||||
pt::ptree root;
|
||||
pt::read_json(ss2, root);
|
||||
deserialize(root, activationResponse);
|
||||
if (activationResponse.success)
|
||||
{
|
||||
{
|
||||
const auto& licenseData = activationResponse.licenseFile.value();
|
||||
std::ofstream os(m_licenseFile, std::ofstream::binary);
|
||||
os.write(licenseData.data(), licenseData.size());
|
||||
}
|
||||
return loadActivationData();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<ModuleUpdate> LicenseClient::checkForUpdates(HTTPClient & httpClient, const std::map<std::string, ModuleVersion>& currentVersions)
|
||||
{
|
||||
if (!isActivated()) { throw std::runtime_error("Not active"); }
|
||||
|
||||
CheckUpdatesRequest req{ m_systemParams, m_activationData->activationId, currentVersions };
|
||||
|
||||
std::ostringstream ss1;
|
||||
serialize(ss1, req);
|
||||
|
||||
std::stringstream ss2;
|
||||
httpClient.postJson("http://localhost:3000/check", ss1.str(), ss2);
|
||||
|
||||
CheckUpdatesResponse checkUpdatesResponse;
|
||||
pt::ptree root;
|
||||
pt::read_json(ss2, root);
|
||||
deserialize(root, checkUpdatesResponse);
|
||||
|
||||
if (!checkUpdatesResponse.success)
|
||||
{
|
||||
throw std::exception("Could not check for updates");
|
||||
}
|
||||
|
||||
if (checkUpdatesResponse.licenseFile)
|
||||
{
|
||||
// Something changes - Server sent us updated licenseFile
|
||||
{
|
||||
const auto& licenseData = checkUpdatesResponse.licenseFile.value();
|
||||
std::ofstream os(m_licenseFile);
|
||||
os.write(licenseData.data(), licenseData.size());
|
||||
}
|
||||
loadActivationData();
|
||||
}
|
||||
|
||||
return checkUpdatesResponse.moduleUpdates;
|
||||
}
|
||||
|
||||
bool LicenseClient::loadActivationData()
|
||||
{
|
||||
if (fs::is_regular_file(m_licenseFile))
|
||||
{
|
||||
// Encrypt in nodejs
|
||||
/*
|
||||
let activationData = {
|
||||
activationId: '1234567890',
|
||||
systemParams: {
|
||||
biosSerialNum: '1234567797980980',
|
||||
diskSerialNum: '1234567857845764'
|
||||
},
|
||||
licensedModules: ['ccengine', 'cc-data-usd', 'cc-data-rub']
|
||||
};
|
||||
|
||||
let algorithm = 'aes-256-gcm';
|
||||
let password = Buffer.from('e73db572349005f1c41979baf8166a0900745119fa096b9c3efbcee11ddd8b88', 'hex');
|
||||
let privateKey = '-----BEGIN EC PRIVATE KEY-----' + "\n" +
|
||||
'MIGAAgEBBCQBPIZnOt/mEsgtH3S9XZMGRuHkB5hYbMJ/BxcGmAc/pZLdxDWgBwYF' + "\n" +
|
||||
'K4EEABGhTANKAAQHyyrnJFywb+B0pcaVRHIOcEao3OtSMSJJZiluIMme1aE+20UA' + "\n" +
|
||||
'0c0+2u+M6bMi072XrXLf8KudcAxihG/aqCqbVVZS6i10SSM=' + "\n" +
|
||||
'-----END EC PRIVATE KEY-----';
|
||||
crypto.randomBytes(16, (err, nonce) => {
|
||||
activationData.nonce = nonce.toString('base64');
|
||||
let data = JSON.stringify(activationData);
|
||||
let sign = crypto.createSign('SHA256');
|
||||
sign.write(data);
|
||||
sign.end();
|
||||
let signature = sign.sign(privateKey, 'hex');
|
||||
data = JSON.stringify({ data, signature });
|
||||
crypto.randomBytes(16, (err, iv) => {
|
||||
zlib.deflateRaw(data, (err, compressed) => {
|
||||
let cipher = crypto.createCipheriv(algorithm, password, iv);
|
||||
let encrypted = cipher.update(compressed);
|
||||
encrypted = Buffer.concat([encrypted, cipher.final()]);
|
||||
let tag = cipher.getAuthTag();
|
||||
let output = Buffer.concat([iv, tag, encrypted]);
|
||||
console.log(output.toString('hex'));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
*/
|
||||
|
||||
/*unsigned char publicKey[] = {
|
||||
0x04, 0x07, 0xcb, 0x2a, 0xe7, 0x24, 0x5c, 0xb0, 0x6f, 0xe0, 0x74, 0xa5, 0xc6, 0x95, 0x44, 0x72,
|
||||
0x0e, 0x70, 0x46, 0xa8, 0xdc, 0xeb, 0x52, 0x31, 0x22, 0x49, 0x66, 0x29, 0x6e, 0x20, 0xc9, 0x9e,
|
||||
0xd5, 0xa1, 0x3e, 0xdb, 0x45, 0x00, 0xd1, 0xcd, 0x3e, 0xda, 0xef, 0x8c, 0xe9, 0xb3, 0x22, 0xd3,
|
||||
0xbd, 0x97, 0xad, 0x72, 0xdf, 0xf0, 0xab, 0x9d, 0x70, 0x0c, 0x62, 0x84, 0x6f, 0xda, 0xa8, 0x2a,
|
||||
0x9b, 0x55, 0x56, 0x52, 0xea, 0x2d, 0x74, 0x49, 0x23 }; */
|
||||
|
||||
unsigned char publicKey[] = {
|
||||
0x30, 0x5e, 0x30, 0x10, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x05, 0x2b,
|
||||
0x81, 0x04, 0x00, 0x11, 0x03, 0x4a, 0x00, 0x04, 0x07, 0xcb, 0x2a, 0xe7, 0x24, 0x5c, 0xb0, 0x6f,
|
||||
0xe0, 0x74, 0xa5, 0xc6, 0x95, 0x44, 0x72, 0x0e, 0x70, 0x46, 0xa8, 0xdc, 0xeb, 0x52, 0x31, 0x22,
|
||||
0x49, 0x66, 0x29, 0x6e, 0x20, 0xc9, 0x9e, 0xd5, 0xa1, 0x3e, 0xdb, 0x45, 0x00, 0xd1, 0xcd, 0x3e,
|
||||
0xda, 0xef, 0x8c, 0xe9, 0xb3, 0x22, 0xd3, 0xbd, 0x97, 0xad, 0x72, 0xdf, 0xf0, 0xab, 0x9d, 0x70,
|
||||
0x0c, 0x62, 0x84, 0x6f, 0xda, 0xa8, 0x2a, 0x9b, 0x55, 0x56, 0x52, 0xea, 0x2d, 0x74, 0x49, 0x23 };
|
||||
|
||||
unsigned char password[] = {
|
||||
0xe7, 0x3d, 0xb5, 0x72, 0x34, 0x90, 0x05, 0xf1, 0xc4, 0x19, 0x79, 0xba, 0xf8, 0x16, 0x6a, 0x09,
|
||||
0x00, 0x74, 0x51, 0x19, 0xfa, 0x09, 0x6b, 0x9c, 0x3e, 0xfb, 0xce, 0xe1, 0x1d, 0xdd, 0x8b, 0x88 };
|
||||
/* unsigned char encrypted[] = {
|
||||
0x5d, 0xc1, 0x4e, 0xaf, 0x95, 0xf0, 0x1d, 0x84, 0x09, 0x71, 0x66, 0x0f, 0x87, 0x19, 0x7a, 0xa1,
|
||||
0x6a, 0x77, 0x39, 0x1e, 0x0a, 0xde, 0x93, 0x0c, 0xda, 0xa8, 0x62, 0x76, 0x53, 0xcb, 0xa7, 0x9f,
|
||||
0x8d, 0x36, 0x2a, 0x74, 0xcd, 0x5d, 0x78, 0x6e, 0x83, 0x14, 0xa4, 0x21, 0x3c }; */
|
||||
/* {
|
||||
unsigned char encrypted[] = {
|
||||
0x88, 0x5d, 0x38, 0xfe, 0xfc, 0x51, 0x7d, 0x3c, 0xb5, 0x95, 0x49, 0xae, 0xa4, 0x6a, 0xa4, 0x7e,
|
||||
0xda, 0x5d, 0x29, 0x84, 0xc2, 0x85, 0xb6, 0x18, 0x6b, 0xd6, 0x40, 0x77, 0x28, 0xc3, 0xa4, 0x0c,
|
||||
0xd1, 0x47, 0x78, 0xf9, 0xce, 0xe4, 0x22, 0xec, 0x68, 0x3f, 0x34, 0xe3, 0xa0, 0x23, 0x42, 0xcc,
|
||||
0x35, 0x50, 0x2a, 0x34, 0xa5, 0xc3, 0x0b, 0x77, 0xa6, 0xb1, 0x00, 0x53, 0xf7, 0x86, 0x08, 0x94,
|
||||
0x72, 0x99, 0x88, 0xc6, 0x07, 0x18, 0x2b, 0xb0, 0xd9, 0xd2, 0x1d, 0xea, 0x5c, 0x96, 0x14, 0x25,
|
||||
0x70, 0xd8, 0x02, 0xb6, 0xc7, 0xa2, 0xae, 0x9e, 0x89, 0x87, 0xb9, 0x9f, 0xad, 0xd6, 0xc6, 0x8a,
|
||||
0xb2, 0x53, 0x8f, 0xfb, 0x3d, 0x4b, 0x21, 0xd3, 0xa1, 0x43, 0x88, 0xef, 0x16, 0x20, 0x19, 0xa2,
|
||||
0x6c, 0x36, 0xc4, 0xfd, 0x17, 0x0c, 0xad, 0x30, 0xef, 0xfc, 0x6c, 0xe8, 0x2c, 0x3a, 0x55, 0x18,
|
||||
0x00, 0x8a, 0x15, 0x46, 0xd6, 0x36, 0x03, 0xb6, 0x8f, 0xb9, 0x86, 0x29, 0x1f, 0x9e, 0xc2, 0x89,
|
||||
0xa2, 0x71, 0x49, 0x64, 0xc7, 0xa6, 0x70, 0x80, 0x00, 0x4c, 0x5d, 0x7c, 0x22, 0x6b, 0xdd, 0x0e,
|
||||
0x2d, 0x17, 0xab, 0xe6, 0xf8, 0x75, 0x8b, 0xd2, 0x5d, 0x2d, 0x40, 0xd6, 0xea, 0x1b, 0x4f, 0xca,
|
||||
0x02, 0x2e, 0x98, 0x16, 0x99, 0xdb, 0x14, 0x67, 0x90, 0xd6, 0x8f, 0xbf, 0xc6, 0x4d, 0xd2, 0x92,
|
||||
0xd2, 0x7b, 0x37, 0x5c, 0x60, 0x7b, 0x78, 0x90, 0x47, 0x73, 0x0a, 0xda, 0x4d, 0xa5, 0x31, 0x51,
|
||||
0x0c, 0xb6, 0x88, 0x93, 0x37, 0x4e, 0x39, 0x5c, 0x06, 0x90, 0x49, 0xd7, 0x48, 0x67, 0x60, 0xfc,
|
||||
0x9f, 0x40, 0xaf, 0x50, 0x67, 0xc0, 0xf5, 0xb4, 0xab, 0xac, 0xa1, 0x1c, 0x95, 0xd8, 0x57, 0x15,
|
||||
0x7d, 0xe8, 0xa7, 0x7f, 0x1a, 0xad, 0x64, 0x7d, 0xa9, 0x3d, 0x38, 0xa6, 0x06, 0xc2, 0x5a, 0x46,
|
||||
0xae, 0x07, 0x53, 0x97, 0x68, 0x6c, 0xc5, 0xf8, 0x2a, 0xb4, 0x86, 0x8e, 0x9a, 0x7b, 0x48, 0x51,
|
||||
0xb4, 0x76, 0x8d, 0x9e, 0x6d, 0x47, 0xa8, 0x55, 0x39, 0x73, 0x1d, 0x35, 0x7c, 0xd2, 0xc1, 0x6a,
|
||||
0x22, 0x91, 0x59, 0x4d, 0xaa, 0x69, 0x11, 0xdf, 0xf3, 0x4f, 0x41, 0x04, 0xff, 0xb4, 0x5d, 0x42,
|
||||
0x42, 0x73, 0x07, 0xc4, 0xfc, 0xac, 0xa4, 0x98, 0x40, 0x24, 0x1f, 0x0a, 0x86, 0xda, 0x06 };
|
||||
|
||||
|
||||
std::ofstream ofs("license.dat", std::ofstream::binary);
|
||||
ofs.write((const char*)encrypted, sizeof(encrypted));
|
||||
} */
|
||||
|
||||
SecByteBlock key(sizeof(password));
|
||||
key.Assign(password, sizeof(password));
|
||||
|
||||
std::string encrypted = readBinaryFile(m_licenseFile);
|
||||
if (std::size(encrypted) < initializationVectorSize)
|
||||
{
|
||||
throw std::runtime_error("Invalid license file");
|
||||
}
|
||||
|
||||
std::string recoveredData;
|
||||
|
||||
GCM<AES>::Decryption d;
|
||||
d.SetKeyWithIV(key, key.size(), (const CryptoPP::byte*)encrypted.data(), initializationVectorSize);
|
||||
|
||||
AuthenticatedDecryptionFilter df(d,
|
||||
new StringSink(recoveredData),
|
||||
AuthenticatedDecryptionFilter::MAC_AT_BEGIN |
|
||||
AuthenticatedDecryptionFilter::THROW_EXCEPTION, macTagSize
|
||||
); // AuthenticatedDecryptionFilter
|
||||
|
||||
// The StringSource dtor will be called immediately
|
||||
// after construction below. This will cause the
|
||||
// destruction of objects it owns. To stop the
|
||||
// behavior so we can get the decoding result from
|
||||
// the DecryptionFilter, we must use a redirector
|
||||
// or manually Put(...) into the filter without
|
||||
// using a StringSource.
|
||||
StringSource ss2((const CryptoPP::byte*)encrypted.data() + initializationVectorSize, encrypted.size() - initializationVectorSize, true,
|
||||
new Redirector(df /*, PASS_EVERYTHING */)
|
||||
); // StringSource
|
||||
|
||||
if (!df.GetLastResult())
|
||||
{
|
||||
throw std::runtime_error("Unable to decrypt data");
|
||||
}
|
||||
|
||||
boost::iostreams::array_source src{ recoveredData.data(), recoveredData.size() };
|
||||
boost::iostreams::filtering_istream is;
|
||||
boost::iostreams::zlib_params zlibParams;
|
||||
zlibParams.noheader = true;
|
||||
is.push(boost::iostreams::zlib_decompressor{zlibParams});
|
||||
is.push(src);
|
||||
|
||||
SignedData signedData;
|
||||
{
|
||||
pt::ptree root;
|
||||
pt::read_json(is, root);
|
||||
deserialize(root, signedData);
|
||||
}
|
||||
|
||||
ECDSA<EC2N, SHA256>::PublicKey pubKey;
|
||||
//CryptoPP::FileSource fs("c:\\work\\ccengine\\openssl\\bin\\ccengine-pub.der", true /*binary*/);
|
||||
CryptoPP::ArraySource arraySource(static_cast<const CryptoPP::byte*>(publicKey), sizeof(publicKey), true);
|
||||
pubKey.Load(arraySource);
|
||||
ECDSA<EC2N, SHA256>::Verifier verifier(pubKey);
|
||||
|
||||
std::string signatureDer;
|
||||
StringSource ss(signedData.signature, true,
|
||||
new HexDecoder(
|
||||
new StringSink(signatureDer)
|
||||
) // HexDecoder
|
||||
); // StringSource
|
||||
|
||||
byte signature[ecdsaSignatureSize] = { 0 };
|
||||
size_t signLength = CryptoPP::DSAConvertSignatureFormat(signature, sizeof(signature), CryptoPP::DSA_P1363,
|
||||
(const CryptoPP::byte*)signatureDer.data(), signatureDer.size(), CryptoPP::DSA_DER);
|
||||
|
||||
bool result = verifier.VerifyMessage((const byte*)signedData.data.data(), signedData.data.size(), signature, signLength);
|
||||
if (!result)
|
||||
{
|
||||
throw std::runtime_error("Signature could not be verified");
|
||||
}
|
||||
|
||||
ActivationData activationData;
|
||||
std::istringstream iss(signedData.data);
|
||||
{
|
||||
pt::ptree root;
|
||||
pt::read_json(iss, root);
|
||||
deserialize(root, activationData);
|
||||
}
|
||||
|
||||
if (!validateActivationData(activationData))
|
||||
{
|
||||
throw std::runtime_error("You system is not genuine. Please contact support!");
|
||||
}
|
||||
|
||||
m_activationData = std::move(activationData);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool LicenseClient::validateActivationData(const ActivationData & activationData)
|
||||
{
|
||||
if (activationData.systemParams.empty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (activationData.appId != appId)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// activation parameters must match system parameters
|
||||
for (const auto& entry : activationData.systemParams)
|
||||
{
|
||||
auto it = m_systemParams.find(entry.first);
|
||||
if (it == m_systemParams.end() || it->second != entry.second)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
44
CCEngine/LicenseClient.h
Normal file
44
CCEngine/LicenseClient.h
Normal file
@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include "HTTPClient.h"
|
||||
#include "ModuleUpdate.h"
|
||||
#include "SystemParamsProvider.h"
|
||||
|
||||
#include <optional>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
struct ActivationData
|
||||
{
|
||||
std::string activationId;
|
||||
std::string appId;
|
||||
SystemParams systemParams;
|
||||
std::set<std::string> licensedModules;
|
||||
};
|
||||
|
||||
std::optional<std::string> validateLicenseKey(const std::string& licenseKey);
|
||||
|
||||
class LicenseClient
|
||||
{
|
||||
public:
|
||||
explicit LicenseClient(SystemParamsProvider& systemParamsProvider, const std::string& licenseFile);
|
||||
virtual ~LicenseClient();
|
||||
|
||||
public:
|
||||
void init();
|
||||
bool isActivated() const { return m_activationData.has_value(); }
|
||||
bool tryPreactivate(HTTPClient &httpClient);
|
||||
bool activate(HTTPClient &httpClient, const std::string& licenseNumber );
|
||||
auto licensedModules() { if (!isActivated()) { throw std::runtime_error("Not active"); } return m_activationData->licensedModules; }
|
||||
std::vector<ModuleUpdate> checkForUpdates(HTTPClient &httpClient, const std::map<std::string, ModuleVersion>& currentVersions);
|
||||
|
||||
private:
|
||||
bool loadActivationData();
|
||||
bool validateActivationData(const ActivationData& activationData);
|
||||
|
||||
private:
|
||||
SystemParamsProvider& m_systemParamsProvider;
|
||||
SystemParams m_systemParams;
|
||||
std::string m_licenseFile;
|
||||
std::optional<ActivationData> m_activationData;
|
||||
};
|
||||
153
CCEngine/ModuleManager.cpp
Normal file
153
CCEngine/ModuleManager.cpp
Normal file
@ -0,0 +1,153 @@
|
||||
#include "ModuleManager.h"
|
||||
|
||||
#include "HashUtils.h"
|
||||
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
|
||||
void ModuleManager::applyUpdate(const std::string & moduleId, const ModuleUpdate & update)
|
||||
{
|
||||
auto module = m_db.findModule(moduleId).value_or(Module{ moduleId, -1 , {} });
|
||||
|
||||
applyUpdate(module, update);
|
||||
|
||||
m_db.storeModule(module);
|
||||
}
|
||||
|
||||
void ModuleManager::applyUpdate(Module & module, const ModuleUpdate & update)
|
||||
{
|
||||
const auto updatePath = retrieveUpdate(update.updateUri);
|
||||
|
||||
auto hash = calcSHA256(updatePath);
|
||||
if (hash != boost::algorithm::to_upper_copy(update.checksum))
|
||||
{
|
||||
throw std::runtime_error("Integrity check of the update has failed");
|
||||
}
|
||||
|
||||
if (!(update.flag & static_cast<uint32_t>(ModuleUpdateFlags::incremental)))
|
||||
{
|
||||
// We should remove old files first before extracting new ones
|
||||
boost::filesystem::path basePath(m_appBaseDir);
|
||||
std::for_each(module.filePaths.begin(), module.filePaths.end(), [&basePath](const auto& filePath)
|
||||
{
|
||||
boost::filesystem::remove(basePath / filePath);
|
||||
});
|
||||
module.filePaths.clear();
|
||||
}
|
||||
|
||||
auto filePaths = extractUpdate(updatePath, update.instPath);
|
||||
|
||||
module.version = update.version;
|
||||
|
||||
module.filePaths.insert(filePaths.begin(), filePaths.end());
|
||||
}
|
||||
|
||||
boost::filesystem::path ModuleManager::retrieveUpdate(const std::string & updateUrl)
|
||||
{
|
||||
auto tmpFile = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
|
||||
std::ofstream os(tmpFile.string(), std::ofstream::binary);
|
||||
m_httpClient.get(updateUrl, os);
|
||||
return tmpFile;
|
||||
}
|
||||
|
||||
#include <zip.h>
|
||||
#include <stdio.h>
|
||||
|
||||
std::string zipErrorToStr(int err)
|
||||
{
|
||||
static const int bufsize = 100;
|
||||
char buf[bufsize] = { 0 };
|
||||
zip_error_to_str(buf, bufsize, err, errno);
|
||||
return buf;
|
||||
}
|
||||
|
||||
inline std::set<std::string> ModuleManager::extractUpdate(const boost::filesystem::path& archivePath, const std::string & targetPath)
|
||||
{
|
||||
struct zip_file *zf = nullptr;
|
||||
struct zip_stat sb;
|
||||
int err;
|
||||
boost::filesystem::path basePath(m_appBaseDir);
|
||||
basePath /= targetPath;
|
||||
boost::filesystem::path basePathRel(targetPath);
|
||||
|
||||
if (!boost::filesystem::exists(basePath))
|
||||
{
|
||||
boost::filesystem::create_directories(basePath);
|
||||
}
|
||||
|
||||
if (!boost::filesystem::is_directory(basePath))
|
||||
{
|
||||
throw std::runtime_error("Target path is not a directory");
|
||||
}
|
||||
|
||||
std::set<std::string> extractedFiles;
|
||||
|
||||
struct zip *za = zip_open(archivePath.string().c_str(), 0, &err);
|
||||
if (za == NULL)
|
||||
{
|
||||
throw std::runtime_error(zipErrorToStr(err));
|
||||
}
|
||||
|
||||
char buf[1024] = { 0 };
|
||||
const int numZipEntries = zip_get_num_entries(za, 0);
|
||||
for (int i = 0; i < numZipEntries; ++i)
|
||||
{
|
||||
if (zip_stat_index(za, i, 0, &sb) == 0)
|
||||
{
|
||||
printf("==================\n");
|
||||
auto len = strlen(sb.name);
|
||||
printf("Name: [%s], ", sb.name);
|
||||
printf("Size: [%llu], ", sb.size);
|
||||
printf("mtime: [%u]\n", (unsigned int)sb.mtime);
|
||||
auto outPath = basePath / sb.name;
|
||||
auto outPathRel = basePathRel / sb.name;
|
||||
if (sb.name[len - 1] == '/')
|
||||
{
|
||||
if (!boost::filesystem::is_directory(outPath) && !boost::filesystem::create_directory(outPath))
|
||||
{
|
||||
throw std::runtime_error("Directory can't be extracted");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
zf = zip_fopen_index(za, i, 0);
|
||||
if (!zf)
|
||||
{
|
||||
throw std::runtime_error("Can't open file from the archive");
|
||||
}
|
||||
|
||||
{
|
||||
std::ofstream os(outPath.string(), std::ofstream::binary);
|
||||
|
||||
uint64_t sum = 0;
|
||||
while (sum != sb.size) {
|
||||
auto len = zip_fread(zf, buf, sizeof(buf));
|
||||
if (len < 0) {
|
||||
throw std::runtime_error("Can't read file from the archive");
|
||||
}
|
||||
|
||||
os.write(buf, len);
|
||||
sum += len;
|
||||
}
|
||||
}
|
||||
boost::filesystem::last_write_time(outPath, sb.mtime);
|
||||
extractedFiles.insert(outPathRel.string());
|
||||
|
||||
zip_fclose(zf);
|
||||
}
|
||||
}
|
||||
else {
|
||||
throw std::runtime_error("Could not get file information from archive");
|
||||
}
|
||||
}
|
||||
|
||||
if (zip_close(za) == -1)
|
||||
{
|
||||
throw std::runtime_error("Can't close the zip archive");
|
||||
}
|
||||
|
||||
return extractedFiles;
|
||||
}
|
||||
38
CCEngine/ModuleManager.h
Normal file
38
CCEngine/ModuleManager.h
Normal file
@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include "JSONSerialization.h"
|
||||
#include "IModuleDatabase.h"
|
||||
#include "ModuleUpdate.h"
|
||||
#include "HTTPClient.h"
|
||||
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
class ModuleManager
|
||||
{
|
||||
public:
|
||||
ModuleManager(const std::string& appBaseDir, IModuleDatabase& db, HTTPClient& httpClient)
|
||||
: m_appBaseDir(appBaseDir)
|
||||
, m_db(db)
|
||||
, m_httpClient(httpClient)
|
||||
{}
|
||||
|
||||
public:
|
||||
void applyUpdate(const std::string& moduleId, const ModuleUpdate& update);
|
||||
|
||||
private:
|
||||
void applyUpdate(Module& module, const ModuleUpdate& update);
|
||||
|
||||
boost::filesystem::path retrieveUpdate(const std::string& updateUrl);
|
||||
|
||||
std::set<std::string> extractUpdate(const boost::filesystem::path& archivePath, const std::string& targetPath);
|
||||
|
||||
private:
|
||||
std::string m_appBaseDir;
|
||||
IModuleDatabase & m_db;
|
||||
HTTPClient& m_httpClient;
|
||||
};
|
||||
31
CCEngine/ModuleUpdate.cpp
Normal file
31
CCEngine/ModuleUpdate.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
#include "ModuleUpdate.h"
|
||||
|
||||
#include "JSONSerialization.h"
|
||||
|
||||
void serialize(std::ostream& os, const ModuleUpdate& u)
|
||||
{
|
||||
os << "{";
|
||||
os << "moduleId:";
|
||||
::serialize(os, u.moduleId);
|
||||
os << ",updatePath:";
|
||||
::serialize(os, u.updateUri);
|
||||
os << ",instPath:";
|
||||
::serialize(os, u.instPath);
|
||||
os << ",version:";
|
||||
::serialize(os, u.version);
|
||||
os << ",checksum:";
|
||||
::serialize(os, u.checksum);
|
||||
os << ",flag:";
|
||||
::serialize(os, u.flag);
|
||||
os << "}";
|
||||
}
|
||||
|
||||
void deserialize(const boost::property_tree::ptree& tree, ModuleUpdate& u)
|
||||
{
|
||||
::deserialize(tree.get_child("moduleId"), u.moduleId);
|
||||
::deserialize(tree.get_child("updateUri"), u.updateUri);
|
||||
::deserialize(tree.get_child("instPath"), u.instPath);
|
||||
::deserialize(tree.get_child("version"), u.version);
|
||||
::deserialize(tree.get_child("checksum"), u.checksum);
|
||||
::deserialize(tree.get_child("flag"), u.flag);
|
||||
}
|
||||
28
CCEngine/ModuleUpdate.h
Normal file
28
CCEngine/ModuleUpdate.h
Normal file
@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "ModuleVersion.h"
|
||||
|
||||
#include <boost/property_tree/ptree.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <ostream>
|
||||
|
||||
enum class ModuleUpdateFlags : uint32_t
|
||||
{
|
||||
defaultValue = 0,
|
||||
incremental = 1,
|
||||
restartRequired = 2
|
||||
};
|
||||
|
||||
struct ModuleUpdate
|
||||
{
|
||||
std::string moduleId;
|
||||
std::string updateUri;
|
||||
std::string instPath;
|
||||
ModuleVersion version;
|
||||
std::string checksum;
|
||||
int flag;
|
||||
};
|
||||
|
||||
void serialize(std::ostream& os, const ModuleUpdate& a);
|
||||
void deserialize(const boost::property_tree::ptree& tree, ModuleUpdate& a);
|
||||
3
CCEngine/ModuleVersion.h
Normal file
3
CCEngine/ModuleVersion.h
Normal file
@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
typedef int ModuleVersion;
|
||||
7
CCEngine/SystemParams.cpp
Normal file
7
CCEngine/SystemParams.cpp
Normal file
@ -0,0 +1,7 @@
|
||||
#include "SystemParams.h"
|
||||
|
||||
const std::string SystemParamTypes::biosSerialNum{ "biosSerialNum" };
|
||||
const std::string SystemParamTypes::computerUUID{ "computerUUID" };
|
||||
const std::string SystemParamTypes::diskSerialNum{ "diskSerialNum" };
|
||||
const std::string SystemParamTypes::osId{ "osId" };
|
||||
const std::string SystemParamTypes::nicMac{ "nicMac" };
|
||||
15
CCEngine/SystemParams.h
Normal file
15
CCEngine/SystemParams.h
Normal file
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
struct SystemParamTypes
|
||||
{
|
||||
static const std::string biosSerialNum;
|
||||
static const std::string computerUUID;
|
||||
static const std::string diskSerialNum;
|
||||
static const std::string osId;
|
||||
static const std::string nicMac;
|
||||
};
|
||||
|
||||
typedef std::map<std::string, std::string> SystemParams;
|
||||
8
CCEngine/SystemParamsProvider.h
Normal file
8
CCEngine/SystemParamsProvider.h
Normal file
@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef WIN32
|
||||
|
||||
#include "SystemParamsProvider_win.h"
|
||||
typedef SystemParamsProvider_win SystemParamsProvider;
|
||||
|
||||
#endif // WIN32
|
||||
382
CCEngine/SystemParamsProvider_win.cpp
Normal file
382
CCEngine/SystemParamsProvider_win.cpp
Normal file
@ -0,0 +1,382 @@
|
||||
#include "SystemParamsProvider_win.h"
|
||||
|
||||
#define _WIN32_DCOM
|
||||
#include <comdef.h>
|
||||
#include <Wbemidl.h>
|
||||
|
||||
#include <boost/algorithm/string/join.hpp>
|
||||
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#pragma comment(lib, "wbemuuid.lib")
|
||||
|
||||
namespace
|
||||
{
|
||||
std::string convertWCSToMBS(const wchar_t* pstr, long wslen)
|
||||
{
|
||||
int len = ::WideCharToMultiByte(CP_ACP, 0, pstr, wslen, NULL, 0, NULL, NULL);
|
||||
|
||||
std::string dblstr(len, '\0');
|
||||
len = ::WideCharToMultiByte(CP_ACP, 0 /* no flags */,
|
||||
pstr, wslen /* not necessary NULL-terminated */,
|
||||
&dblstr[0], len,
|
||||
NULL, NULL /* no default char */);
|
||||
|
||||
return dblstr;
|
||||
}
|
||||
|
||||
std::string convertBSTRToMBS(BSTR bstr)
|
||||
{
|
||||
int wslen = ::SysStringLen(bstr);
|
||||
return convertWCSToMBS((wchar_t*)bstr, wslen);
|
||||
}
|
||||
|
||||
BSTR convertMBSToBSTR(const std::string& str)
|
||||
{
|
||||
int wslen = ::MultiByteToWideChar(CP_ACP, 0 /* no flags */,
|
||||
str.data(), str.length(),
|
||||
NULL, 0);
|
||||
|
||||
BSTR wsdata = ::SysAllocStringLen(NULL, wslen);
|
||||
::MultiByteToWideChar(CP_ACP, 0 /* no flags */,
|
||||
str.data(), str.length(),
|
||||
wsdata, wslen);
|
||||
return wsdata;
|
||||
}
|
||||
|
||||
class QuerySink final : public IWbemObjectSink
|
||||
{
|
||||
public:
|
||||
typedef std::vector<std::string> Fields_type;
|
||||
typedef std::vector<std::vector<std::string>> Result_type;
|
||||
|
||||
public:
|
||||
QuerySink(const std::string& wmiClass, const Fields_type& fields)
|
||||
: m_wmiClass(wmiClass)
|
||||
, m_fields(fields)
|
||||
{
|
||||
m_lRef = 0;
|
||||
m_bDone = false;
|
||||
InitializeCriticalSection(&m_threadLock);
|
||||
}
|
||||
~QuerySink() {
|
||||
m_bDone = true;
|
||||
DeleteCriticalSection(&m_threadLock);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
virtual ULONG STDMETHODCALLTYPE AddRef() override
|
||||
{
|
||||
return InterlockedIncrement(&m_lRef);
|
||||
}
|
||||
|
||||
virtual ULONG STDMETHODCALLTYPE Release() override
|
||||
{
|
||||
LONG lRef = InterlockedDecrement(&m_lRef);
|
||||
if (lRef == 0)
|
||||
delete this;
|
||||
return lRef;
|
||||
}
|
||||
|
||||
virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppv) override
|
||||
{
|
||||
if (riid == IID_IUnknown || riid == IID_IWbemObjectSink)
|
||||
{
|
||||
*ppv = (IWbemObjectSink *)this;
|
||||
AddRef();
|
||||
return WBEM_S_NO_ERROR;
|
||||
}
|
||||
else return E_NOINTERFACE;
|
||||
}
|
||||
|
||||
virtual HRESULT STDMETHODCALLTYPE Indicate(
|
||||
LONG lObjectCount,
|
||||
IWbemClassObject __RPC_FAR *__RPC_FAR *apObjArray
|
||||
) override
|
||||
{
|
||||
std::vector<std::vector<std::string>> result;
|
||||
|
||||
for (int i = 0; i < lObjectCount; i++)
|
||||
{
|
||||
std::vector<std::string> lineResult;
|
||||
lineResult.reserve(m_fields.size());
|
||||
for (const auto& field : m_fields)
|
||||
{
|
||||
HRESULT hres = S_OK;
|
||||
VARIANT varName;
|
||||
hres = apObjArray[i]->Get(convertMBSToBSTR(field),
|
||||
0, &varName, 0, 0);
|
||||
|
||||
if (FAILED(hres))
|
||||
{
|
||||
std::ostringstream ostr;
|
||||
ostr << "Failed to get the data from the query. Error code = 0x"
|
||||
<< std::hex << hres;
|
||||
std::cerr << ostr.str() << std::endl;
|
||||
return WBEM_E_FAILED; // Program has failed.
|
||||
}
|
||||
|
||||
lineResult.push_back(convertBSTRToMBS(V_BSTR(&varName)));
|
||||
}
|
||||
|
||||
result.push_back(lineResult);
|
||||
}
|
||||
|
||||
m_resultPromise.set_value(std::move(result));
|
||||
|
||||
return WBEM_S_NO_ERROR;
|
||||
}
|
||||
|
||||
virtual HRESULT STDMETHODCALLTYPE SetStatus(
|
||||
/* [in] */ LONG lFlags,
|
||||
/* [in] */ HRESULT hResult,
|
||||
/* [in] */ BSTR strParam,
|
||||
/* [in] */ IWbemClassObject __RPC_FAR *pObjParam
|
||||
) override
|
||||
{
|
||||
if (lFlags == WBEM_STATUS_COMPLETE)
|
||||
{
|
||||
// Call complete
|
||||
EnterCriticalSection(&m_threadLock);
|
||||
m_bDone = true;
|
||||
LeaveCriticalSection(&m_threadLock);
|
||||
}
|
||||
else if (lFlags == WBEM_STATUS_PROGRESS)
|
||||
{
|
||||
// Call in progress...
|
||||
}
|
||||
|
||||
return WBEM_S_NO_ERROR;
|
||||
}
|
||||
|
||||
std::future<Result_type> getFuture()
|
||||
{
|
||||
return m_resultPromise.get_future();
|
||||
}
|
||||
|
||||
bool IsDone() const
|
||||
{
|
||||
bool done = true;
|
||||
|
||||
EnterCriticalSection(&m_threadLock);
|
||||
done = m_bDone;
|
||||
LeaveCriticalSection(&m_threadLock);
|
||||
|
||||
return done;
|
||||
}
|
||||
|
||||
private:
|
||||
LONG m_lRef;
|
||||
bool m_bDone;
|
||||
mutable CRITICAL_SECTION m_threadLock; // for thread safety
|
||||
const std::string m_wmiClass;
|
||||
const Fields_type m_fields;
|
||||
std::promise<Result_type> m_resultPromise;
|
||||
};
|
||||
}
|
||||
|
||||
namespace detail
|
||||
{
|
||||
class SystemParamsProvider_winImpl final
|
||||
{
|
||||
public:
|
||||
SystemParamsProvider_winImpl()
|
||||
{
|
||||
HRESULT hres = 0;
|
||||
|
||||
// Initialize COM. ------------------------------------------
|
||||
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
|
||||
if (FAILED(hres))
|
||||
{
|
||||
std::ostringstream ostr;
|
||||
ostr << "Failed to initialize COM library. Error code = 0x" << std::hex << hres;
|
||||
throw std::runtime_error(ostr.str());
|
||||
}
|
||||
|
||||
// Set general COM security levels --------------------------
|
||||
hres = CoInitializeSecurity(NULL,
|
||||
-1, // COM authentication
|
||||
NULL, // Authentication services
|
||||
NULL, // Reserved
|
||||
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
|
||||
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
|
||||
NULL, // Authentication info
|
||||
EOAC_NONE, // Additional capabilities
|
||||
NULL); // Reserved
|
||||
|
||||
if (FAILED(hres))
|
||||
{
|
||||
CoUninitialize();
|
||||
std::ostringstream ostr;
|
||||
ostr << "Failed to initialize security. Error code = 0x" << std::hex << hres;
|
||||
throw std::runtime_error(ostr.str());
|
||||
}
|
||||
|
||||
// Obtain the initial locator to WMI -------------------------
|
||||
hres = CoCreateInstance(
|
||||
CLSID_WbemLocator,
|
||||
0,
|
||||
CLSCTX_INPROC_SERVER,
|
||||
IID_IWbemLocator, (LPVOID *)&m_pLoc);
|
||||
|
||||
if (FAILED(hres))
|
||||
{
|
||||
CoUninitialize();
|
||||
std::ostringstream ostr;
|
||||
ostr << "Failed to create IWbemLocator object. Err code = 0x" << std::hex << hres;
|
||||
throw std::runtime_error(ostr.str());
|
||||
}
|
||||
|
||||
// Connect to WMI through the IWbemLocator::ConnectServer method
|
||||
// Connect to the local root\cimv2 namespace
|
||||
// and obtain pointer pSvc to make IWbemServices calls.
|
||||
hres = m_pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"),
|
||||
NULL,
|
||||
NULL,
|
||||
0,
|
||||
NULL,
|
||||
0,
|
||||
0,
|
||||
&m_pSvc);
|
||||
|
||||
if (FAILED(hres))
|
||||
{
|
||||
m_pLoc->Release();
|
||||
CoUninitialize();
|
||||
std::ostringstream ostr;
|
||||
ostr << "Could not connect. Error code = 0x" << std::hex << hres;
|
||||
throw std::runtime_error(ostr.str());
|
||||
}
|
||||
|
||||
// Set security levels on the proxy -------------------------
|
||||
hres = CoSetProxyBlanket(m_pSvc, // Indicates the proxy to set
|
||||
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
|
||||
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
|
||||
NULL, // Server principal name
|
||||
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
|
||||
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
|
||||
NULL, // client identity
|
||||
EOAC_NONE); // proxy capabilities
|
||||
|
||||
if (FAILED(hres))
|
||||
{
|
||||
m_pSvc->Release();
|
||||
m_pLoc->Release();
|
||||
CoUninitialize();
|
||||
std::ostringstream ostr;
|
||||
ostr << "Could not set proxy blanket. Error code = 0x" << std::hex << hres;
|
||||
throw std::runtime_error(ostr.str());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
~SystemParamsProvider_winImpl()
|
||||
{
|
||||
m_pSvc->Release();
|
||||
m_pLoc->Release();
|
||||
CoUninitialize();
|
||||
}
|
||||
|
||||
public:
|
||||
SystemParams retrieveSystemParams()
|
||||
{
|
||||
auto biosFuture = queryWmiInfo("Win32_BIOS", { "SerialNumber" });
|
||||
auto computerSystemProductFuture = queryWmiInfo("Win32_ComputerSystemProduct", { "UUID" });
|
||||
auto diskDriveFuture = queryWmiInfo("Win32_DiskDrive", { "SerialNumber" });
|
||||
auto osFuture = queryWmiInfo("Win32_OperatingSystem", { "SerialNumber" });
|
||||
auto nicFuture = queryWmiInfo("Win32_NetworkAdapter", { "MACAddress" }, "PhysicalAdapter = TRUE");
|
||||
|
||||
SystemParams result;
|
||||
result[SystemParamTypes::biosSerialNum] = processBiosData(biosFuture.get());
|
||||
result[SystemParamTypes::computerUUID] = processComputerSystemProductData(computerSystemProductFuture.get());
|
||||
result[SystemParamTypes::diskSerialNum] = processDiskDrive(diskDriveFuture.get());
|
||||
result[SystemParamTypes::osId] = processOs(osFuture.get());
|
||||
result[SystemParamTypes::nicMac] = processNic(nicFuture.get());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
typedef std::vector<std::vector<std::string>> QueryResult_type;
|
||||
|
||||
private:
|
||||
std::future<QueryResult_type> queryWmiInfo(const std::string& wmiClass, const std::vector<std::string>& fieldNames, const std::string& condition = "")
|
||||
{
|
||||
HRESULT hres = 0;
|
||||
|
||||
std::ostringstream queryStr;
|
||||
queryStr << "SELECT " << boost::algorithm::join(fieldNames, ",") << " FROM " << wmiClass;
|
||||
|
||||
if (!condition.empty())
|
||||
{
|
||||
queryStr << " WHERE " << condition;
|
||||
}
|
||||
|
||||
// Use the IWbemServices pointer to make requests of WMI ----
|
||||
QuerySink* pResponseSink = new QuerySink(wmiClass, fieldNames);
|
||||
pResponseSink->AddRef();
|
||||
auto resultFuture = pResponseSink->getFuture();
|
||||
hres = m_pSvc->ExecQueryAsync(bstr_t("WQL"),
|
||||
convertMBSToBSTR(queryStr.str()),
|
||||
WBEM_FLAG_BIDIRECTIONAL,
|
||||
NULL,
|
||||
pResponseSink);
|
||||
|
||||
if (FAILED(hres))
|
||||
{
|
||||
pResponseSink->Release();
|
||||
std::ostringstream ostr;
|
||||
ostr << "Query for " << boost::algorithm::join(fieldNames, ", ") << " failed. Error code = 0x" << std::hex << hres;
|
||||
throw std::runtime_error(ostr.str());
|
||||
}
|
||||
|
||||
return resultFuture;
|
||||
}
|
||||
|
||||
static std::string processBiosData(const QueryResult_type& biosInfo)
|
||||
{
|
||||
return biosInfo[0][0];
|
||||
}
|
||||
|
||||
static std::string processComputerSystemProductData(const QueryResult_type& csInfo)
|
||||
{
|
||||
return csInfo[0][0];
|
||||
}
|
||||
|
||||
static std::string processDiskDrive(const QueryResult_type& csInfo)
|
||||
{
|
||||
return csInfo[0][0];
|
||||
}
|
||||
|
||||
static std::string processOs(const QueryResult_type& csInfo)
|
||||
{
|
||||
return csInfo[0][0];
|
||||
}
|
||||
|
||||
static std::string processNic(const QueryResult_type& csInfo)
|
||||
{
|
||||
return csInfo[0][0];
|
||||
}
|
||||
|
||||
private:
|
||||
IWbemLocator* m_pLoc = nullptr;
|
||||
IWbemServices *m_pSvc = nullptr;
|
||||
};
|
||||
}
|
||||
|
||||
SystemParamsProvider_win::SystemParamsProvider_win()
|
||||
: m_impl(std::make_unique<detail::SystemParamsProvider_winImpl>())
|
||||
{
|
||||
}
|
||||
|
||||
SystemParamsProvider_win::~SystemParamsProvider_win()
|
||||
{
|
||||
}
|
||||
|
||||
SystemParams SystemParamsProvider_win::retrieveSystemParams()
|
||||
{
|
||||
return m_impl->retrieveSystemParams();
|
||||
}
|
||||
23
CCEngine/SystemParamsProvider_win.h
Normal file
23
CCEngine/SystemParamsProvider_win.h
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "SystemParams.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace detail
|
||||
{
|
||||
class SystemParamsProvider_winImpl;
|
||||
}
|
||||
|
||||
class SystemParamsProvider_win final
|
||||
{
|
||||
public:
|
||||
SystemParamsProvider_win();
|
||||
~SystemParamsProvider_win();
|
||||
|
||||
public:
|
||||
SystemParams retrieveSystemParams();
|
||||
private:
|
||||
std::unique_ptr<detail::SystemParamsProvider_winImpl> m_impl;
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user