commit e49998d86719900e895dc22f9e8e16a19afe6b60 Author: Peter Sykora Date: Sun Mar 18 12:36:11 2018 +0100 Initial import diff --git a/CCEngine.sln b/CCEngine.sln new file mode 100644 index 0000000..677b2ee --- /dev/null +++ b/CCEngine.sln @@ -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 diff --git a/CCEngine/CCEngine.cpp b/CCEngine/CCEngine.cpp new file mode 100644 index 0000000..3678448 --- /dev/null +++ b/CCEngine/CCEngine.cpp @@ -0,0 +1,158 @@ +#include "SystemParamsProvider_win.h" +#include "HTTPClient.h" +#include "LicenseClient.h" +#include "CCServer.h" +#include "ModuleManager.h" +#include "JSONModuleDatabase.h" + +#include + +#include + +//------------------------------------------------------------------------------ + +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 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(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
\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(std::atoi(argv[2])); + std::string const doc_root = argv[3]; + auto const threads = std::max(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( + ioc, + tcp::endpoint{ address, port }, + doc_root)->run(); + + // Run the I/O service on the requested number of threads + std::vector 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; +} \ No newline at end of file diff --git a/CCEngine/CCEngine.vcxproj b/CCEngine/CCEngine.vcxproj new file mode 100644 index 0000000..b2974ad --- /dev/null +++ b/CCEngine/CCEngine.vcxproj @@ -0,0 +1,208 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {51637EB3-9942-43AE-9272-9DD85412EFC7} + Win32Proj + CCEngine + 10.0.16299.0 + + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + NotUsing + Level3 + Disabled + true + _WIN32_WINNT=0x0501;WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + $(ProjectDir)..\..\..\openssl\include\;%(AdditionalIncludeDirectories) + stdcpplatest + + + Console + true + /verbose:lib %(AdditionalOptions) + + + + + NotUsing + Level3 + Disabled + true + _WIN32_WINNT=0x0501;_DEBUG;_LIB;%(PreprocessorDefinitions) + true + $(ProjectDir)..\..\..\openssl\include\;%(AdditionalIncludeDirectories) + stdcpplatest + + + Console + true + + + + + NotUsing + Level3 + MaxSpeed + true + true + true + _WIN32_WINNT=0x0501;WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions);BOOST_EXCEPTION_DISABLE + true + $(ProjectDir)..\..\..\openssl\include\;%(AdditionalIncludeDirectories) + stdcpplatest + false + false + None + + + Console + true + true + false + YES + false + + false + + + + + NotUsing + Level3 + MaxSpeed + true + true + true + _WIN32_WINNT=0x0501;NDEBUG;_LIB;%(PreprocessorDefinitions) + true + $(ProjectDir)..\..\..\openssl\include\;%(AdditionalIncludeDirectories) + stdcpplatest + + + Console + true + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CCEngine/CCEngine.vcxproj.filters b/CCEngine/CCEngine.vcxproj.filters new file mode 100644 index 0000000..e61f1b8 --- /dev/null +++ b/CCEngine/CCEngine.vcxproj.filters @@ -0,0 +1,92 @@ + + + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {62810fe9-1f5f-41c9-a552-642d73dbcfcb} + + + + + src\updclient + + + src\updclient + + + src\updclient + + + src\updclient + + + src\updclient + + + src\updclient + + + src\updclient + + + src + + + src + + + src + + + src + + + src\updclient + + + + + src\updclient + + + src\updclient + + + src\updclient + + + src\updclient + + + src\updclient + + + src\updclient + + + src\updclient + + + src\updclient + + + + src + + + src\updclient + + + src\updclient + + + + + \ No newline at end of file diff --git a/CCEngine/CCServer.cpp b/CCEngine/CCServer.cpp new file mode 100644 index 0000000..461ca64 --- /dev/null +++ b/CCEngine/CCServer.cpp @@ -0,0 +1,807 @@ +#include "CCServer.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace fs = boost::filesystem; // from +using tcp = boost::asio::ip::tcp; // from +namespace http = boost::beast::http; // from + +static constexpr auto APIS_UINT8_MIN = std::numeric_limits::min(); +static constexpr auto APIS_UINT8_MAX = std::numeric_limits::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) + 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& 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 decrypted(buffer.size() - cryptFileHeader.size()); + is.read((char*)decrypted.data(), decrypted.size()); + if (decrypted.size() > sizeof(uint32_t)) + { + auto headerLen = reinterpret_cast(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 lock(mutex_); + map_.emplace(path, etag); + } + + bool check_path(boost::beast::string_view path, boost::beast::string_view etag) const + { + std::shared_lock lock(mutex_); + auto it = map_.find(static_cast(path)); + if (it != map_.end() && it->second == etag) + { + return true; + } + + return false; + } + + void clear() + { + std::unique_lock lock(mutex_); + map_.clear(); + } + +private: + mutable std::shared_mutex mutex_; + std::unordered_map 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>&& req, + Send&& send) +{ + // Returns a bad request response + auto const bad_request = + [&req](boost::beast::string_view why) + { + http::response 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 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 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 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> Tokenizer; + boost::char_separator 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 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 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 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 +{ + // 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> 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 + void + operator()(http::message&& msg) + { + // This holds a work item + struct work_impl : work + { + http_session& self_; + http::message msg_; + + work_impl( + http_session& self, + http::message&& 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(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 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 +{ + 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( + 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( + 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 m_threads; +}; + +} + +CCServer::CCServer(const std::string& addressToListen, uint16_t portToListen, const std::string& docRoot, int threads) +: m_impl(std::make_unique(addressToListen, portToListen, docRoot, threads)) +{} + + +CCServer::~CCServer() +{} + +void CCServer::shutdown() +{ + m_impl->shutdown(); +} + +void CCServer::clearCache() +{ + m_impl->clearCache(); +} + +void CCServer::run() +{ + m_impl->run(); +} + diff --git a/CCEngine/CCServer.h b/CCEngine/CCServer.h new file mode 100644 index 0000000..f6d6fa2 --- /dev/null +++ b/CCEngine/CCServer.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include + +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 m_impl; +}; diff --git a/CCEngine/HTTPClient.cpp b/CCEngine/HTTPClient.cpp new file mode 100644 index 0000000..3eee61c --- /dev/null +++ b/CCEngine/HTTPClient.cpp @@ -0,0 +1,312 @@ +#include "HTTPClient.h" + +#include + +#include +#include +#include + +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= 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 conn; + std::unique_ptr extraHeaders; +}; + +DownloadSession initCurlRequest(const std::string& url, std::ostream &os) +{ + DownloadSession result; + CURLcode code; + + std::unique_ptr 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); +} diff --git a/CCEngine/HTTPClient.h b/CCEngine/HTTPClient.h new file mode 100644 index 0000000..7d78bba --- /dev/null +++ b/CCEngine/HTTPClient.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include + +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); +}; diff --git a/CCEngine/HashUtils.cpp b/CCEngine/HashUtils.cpp new file mode 100644 index 0000000..1d8c45f --- /dev/null +++ b/CCEngine/HashUtils.cpp @@ -0,0 +1,22 @@ +#include "HashUtils.h" + +#include +#include +#include + +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; +} diff --git a/CCEngine/HashUtils.h b/CCEngine/HashUtils.h new file mode 100644 index 0000000..883950f --- /dev/null +++ b/CCEngine/HashUtils.h @@ -0,0 +1,6 @@ +#pragma once + +#include +#include + +std::string calcSHA256(const boost::filesystem::path& file); diff --git a/CCEngine/IModuleDatabase.h b/CCEngine/IModuleDatabase.h new file mode 100644 index 0000000..5b234a2 --- /dev/null +++ b/CCEngine/IModuleDatabase.h @@ -0,0 +1,27 @@ +#pragma once + +#include "ModuleVersion.h" + +#include + +#include +#include +#include + +struct Module +{ + std::string moduleId; + ModuleVersion version; + std::set filePaths; +}; + +class IModuleDatabase +{ +public: + virtual std::map listModules() = 0; + virtual boost::optional findModule(const std::string& moduleId) = 0; + virtual void storeModule(const Module& module) = 0; + +protected: + ~IModuleDatabase() {} +}; diff --git a/CCEngine/JSONModuleDatabase.cpp b/CCEngine/JSONModuleDatabase.cpp new file mode 100644 index 0000000..3765d57 --- /dev/null +++ b/CCEngine/JSONModuleDatabase.cpp @@ -0,0 +1,117 @@ +#include "JSONModuleDatabase.h" + +#include "JSONSerialization.h" + +#include +#include +#include + +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 JSONModuleDatabase::listModules() +{ + std::map 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 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& 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); +} diff --git a/CCEngine/JSONModuleDatabase.h b/CCEngine/JSONModuleDatabase.h new file mode 100644 index 0000000..a589ae2 --- /dev/null +++ b/CCEngine/JSONModuleDatabase.h @@ -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 listModules() override; + boost::optional findModule(const std::string& moduleId) override; + void storeModule(const Module& module) override; + +private: + void storeModuleList(const std::map& moduleList); + void updateModuleList(const std::string& moduleId, const ModuleVersion& version); + +private: + std::string m_baseDir; +}; diff --git a/CCEngine/JSONSerialization.cpp b/CCEngine/JSONSerialization.cpp new file mode 100644 index 0000000..b74647e --- /dev/null +++ b/CCEngine/JSONSerialization.cpp @@ -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(); +} diff --git a/CCEngine/JSONSerialization.h b/CCEngine/JSONSerialization.h new file mode 100644 index 0000000..2c88c59 --- /dev/null +++ b/CCEngine/JSONSerialization.h @@ -0,0 +1,118 @@ +#pragma once + +#include + +#include +#include +#include +#include +#include + +void serialize(std::ostream& os, const std::string& str); + +template::value, int>::type = 0> +void serialize(std::ostream& os, T intVal) +{ + os << intVal; +} + +template +void serialize(std::ostream& os, const std::vector& 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 +void serialize(std::ostream& os, const std::set& 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 +void serialize(std::ostream& os, const std::map& 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::value, int>::type = 0> +void deserialize(const boost::property_tree::ptree & tree, T& intVal) +{ + intVal = tree.get_value(); +} + +template +void deserialize(const boost::property_tree::ptree& tree, std::vector& 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 +void deserialize(const boost::property_tree::ptree& tree, std::set& 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 +void deserialize(const boost::property_tree::ptree& tree, std::map& 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))); + } +} diff --git a/CCEngine/LicenseClient.cpp b/CCEngine/LicenseClient.cpp new file mode 100644 index 0000000..6ca9006 --- /dev/null +++ b/CCEngine/LicenseClient.cpp @@ -0,0 +1,667 @@ +#include "LicenseClient.h" + +#include "JSONSerialization.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + + +#include +#include +#include +#include +#include +#include + +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::max(); + sum2 = (sum2 + sum1) % std::numeric_limits::max(); + } + + return (sum2 << std::numeric_limits::digits) | sum1; +} + +typedef std::string ParamHash; +ParamHash fletcher64(const std::string& input) +{ + std::vector 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(t.tellg())); + t.seekg(0, std::ios::beg); + + str.assign((std::istreambuf_iterator(t)), + std::istreambuf_iterator()); + + 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 licenseFile; +}; + +struct CheckUpdatesRequest +{ + SystemParams systemParams; + std::string activationId; + std::map moduleVersions; +}; + +struct CheckUpdatesResponse +{ + bool success; + std::optional licenseFile; + std::vector 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 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 LicenseClient::checkForUpdates(HTTPClient & httpClient, const std::map& 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::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::PublicKey pubKey; + //CryptoPP::FileSource fs("c:\\work\\ccengine\\openssl\\bin\\ccengine-pub.der", true /*binary*/); + CryptoPP::ArraySource arraySource(static_cast(publicKey), sizeof(publicKey), true); + pubKey.Load(arraySource); + ECDSA::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; +} diff --git a/CCEngine/LicenseClient.h b/CCEngine/LicenseClient.h new file mode 100644 index 0000000..cfcd5a3 --- /dev/null +++ b/CCEngine/LicenseClient.h @@ -0,0 +1,44 @@ +#pragma once + +#include "HTTPClient.h" +#include "ModuleUpdate.h" +#include "SystemParamsProvider.h" + +#include +#include +#include + +struct ActivationData +{ + std::string activationId; + std::string appId; + SystemParams systemParams; + std::set licensedModules; +}; + +std::optional 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 checkForUpdates(HTTPClient &httpClient, const std::map& currentVersions); + +private: + bool loadActivationData(); + bool validateActivationData(const ActivationData& activationData); + +private: + SystemParamsProvider& m_systemParamsProvider; + SystemParams m_systemParams; + std::string m_licenseFile; + std::optional m_activationData; +}; diff --git a/CCEngine/ModuleManager.cpp b/CCEngine/ModuleManager.cpp new file mode 100644 index 0000000..5ee6826 --- /dev/null +++ b/CCEngine/ModuleManager.cpp @@ -0,0 +1,153 @@ +#include "ModuleManager.h" + +#include "HashUtils.h" + +#include +#include + +#include +#include + +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(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 +#include + +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 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 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; +} diff --git a/CCEngine/ModuleManager.h b/CCEngine/ModuleManager.h new file mode 100644 index 0000000..7bdaaae --- /dev/null +++ b/CCEngine/ModuleManager.h @@ -0,0 +1,38 @@ +#pragma once + +#include "JSONSerialization.h" +#include "IModuleDatabase.h" +#include "ModuleUpdate.h" +#include "HTTPClient.h" + +#include +#include +#include +#include +#include +#include + +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 extractUpdate(const boost::filesystem::path& archivePath, const std::string& targetPath); + +private: + std::string m_appBaseDir; + IModuleDatabase & m_db; + HTTPClient& m_httpClient; +}; diff --git a/CCEngine/ModuleUpdate.cpp b/CCEngine/ModuleUpdate.cpp new file mode 100644 index 0000000..b5a36c5 --- /dev/null +++ b/CCEngine/ModuleUpdate.cpp @@ -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); +} diff --git a/CCEngine/ModuleUpdate.h b/CCEngine/ModuleUpdate.h new file mode 100644 index 0000000..edf0e98 --- /dev/null +++ b/CCEngine/ModuleUpdate.h @@ -0,0 +1,28 @@ +#pragma once + +#include "ModuleVersion.h" + +#include + +#include +#include + +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); diff --git a/CCEngine/ModuleVersion.h b/CCEngine/ModuleVersion.h new file mode 100644 index 0000000..3da5b6d --- /dev/null +++ b/CCEngine/ModuleVersion.h @@ -0,0 +1,3 @@ +#pragma once + +typedef int ModuleVersion; \ No newline at end of file diff --git a/CCEngine/SystemParams.cpp b/CCEngine/SystemParams.cpp new file mode 100644 index 0000000..ebdf1a7 --- /dev/null +++ b/CCEngine/SystemParams.cpp @@ -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" }; diff --git a/CCEngine/SystemParams.h b/CCEngine/SystemParams.h new file mode 100644 index 0000000..56c13cc --- /dev/null +++ b/CCEngine/SystemParams.h @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +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 SystemParams; diff --git a/CCEngine/SystemParamsProvider.h b/CCEngine/SystemParamsProvider.h new file mode 100644 index 0000000..9533ccc --- /dev/null +++ b/CCEngine/SystemParamsProvider.h @@ -0,0 +1,8 @@ +#pragma once + +#ifdef WIN32 + +#include "SystemParamsProvider_win.h" +typedef SystemParamsProvider_win SystemParamsProvider; + +#endif // WIN32 diff --git a/CCEngine/SystemParamsProvider_win.cpp b/CCEngine/SystemParamsProvider_win.cpp new file mode 100644 index 0000000..d5b6663 --- /dev/null +++ b/CCEngine/SystemParamsProvider_win.cpp @@ -0,0 +1,382 @@ +#include "SystemParamsProvider_win.h" + +#define _WIN32_DCOM +#include +#include + +#include + +#include +#include +#include + +#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 Fields_type; + typedef std::vector> 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> result; + + for (int i = 0; i < lObjectCount; i++) + { + std::vector 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 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 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> QueryResult_type; + + private: + std::future queryWmiInfo(const std::string& wmiClass, const std::vector& 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()) +{ +} + +SystemParamsProvider_win::~SystemParamsProvider_win() +{ +} + +SystemParams SystemParamsProvider_win::retrieveSystemParams() +{ + return m_impl->retrieveSystemParams(); +} diff --git a/CCEngine/SystemParamsProvider_win.h b/CCEngine/SystemParamsProvider_win.h new file mode 100644 index 0000000..62ecfae --- /dev/null +++ b/CCEngine/SystemParamsProvider_win.h @@ -0,0 +1,23 @@ +#pragma once + +#include "SystemParams.h" + +#include +#include + +namespace detail +{ + class SystemParamsProvider_winImpl; +} + +class SystemParamsProvider_win final +{ +public: + SystemParamsProvider_win(); + ~SystemParamsProvider_win(); + +public: + SystemParams retrieveSystemParams(); +private: + std::unique_ptr m_impl; +};