diff --git a/CMakePresets.json b/CMakePresets.json index 9e560b9..63f4984 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -79,6 +79,23 @@ "CMAKE_SYSTEM_VERSION": "8.1" }, "vendor": { "microsoft.com/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Windows" ] } } + }, + { + "name": "win64-release", + "displayName": "Windows x64 Release", + "description": "Target Windows with the Visual Studio development environment.", + "generator": "Ninja", + "binaryDir": "${sourceDir}/out/build/${presetName}", + "architecture": { + "value": "x64", + "strategy": "external" + }, + "cacheVariables": { + "CMAKE_BUILD_TYPE": "Debug", + "CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}", + "CMAKE_SYSTEM_VERSION": "8.1" + }, + "vendor": { "microsoft.com/VisualStudioSettings/CMake/1.0": { "hostOS": [ "Windows" ] } } } ], "buildPresets": [ @@ -93,6 +110,10 @@ { "name": "win64-debug", "configurePreset": "win64-debug" + }, + { + "name": "win64-release", + "configurePreset": "win64-release" } ], "vendor": { diff --git a/CMakeProject1/CMakeProject1.cpp b/CMakeProject1/CMakeProject1.cpp index 7452e33..9ef0af1 100644 --- a/CMakeProject1/CMakeProject1.cpp +++ b/CMakeProject1/CMakeProject1.cpp @@ -3,6 +3,13 @@ #include "LicenseClient.h" #include "SystemParamsProvider.h" +#include +#include +#include +#include +#include +#include + #include #include #include @@ -12,6 +19,9 @@ namespace po = boost::program_options; namespace net = boost::asio; // from +namespace logging = boost::log; +namespace expr = boost::log::expressions; +namespace keywords = boost::log::keywords; volatile bool signalCaught = false; @@ -21,47 +31,88 @@ void signalHandler(int signum) { signalCaught = true; } +void initLogging(std::optional logFolder, bool debug = false) +{ + if (logFolder.has_value()) { + logging::add_file_log( + keywords::file_name = (boost::filesystem::path(logFolder.value()) / "CCEngineServer_%N.log").native(), + keywords::open_mode = std::ios_base::app, + keywords::rotation_size = 10 * 1024 * 1024, + keywords::max_files = 10, + keywords::format = ( + expr::stream + << expr::format_date_time("TimeStamp", "%Y-%m-%d %H:%M:%S") + << ": <" << logging::trivial::severity + << "> " << expr::smessage) + ); + } + logging::core::get()->set_filter + ( + logging::trivial::severity >= ((!debug) ? logging::trivial::info : logging::trivial::debug) + ); + logging::add_common_attributes(); +} + + int main(int argc, const char* argv[]) { // Declare the supported options. - po::options_description desc("Allowed options"); - desc.add_options() + po::options_description generalOptions("General options"); + generalOptions.add_options() ("help", "produce help message") ("bind_ip", po::value()->default_value("0.0.0.0"), "ip address to listen for connections") ("port", po::value()->default_value(8080), "port") ("data_root", po::value()->default_value("."), "root directory with data") ("threads", po::value()->default_value(4), "port") - ; - + ("log_folder", po::value(), "folder to store logs") + ; + + po::options_description debugOptions("Debug options"); + debugOptions.add_options() + ("debug", "Debugging logs") + ; + + po::options_description allOptions("Allowed options"); + allOptions.add(generalOptions).add(debugOptions); + po::variables_map vm; - po::store(po::parse_command_line(argc, argv, desc), vm); + po::store(po::parse_command_line(argc, argv, allOptions), vm); po::notify(vm); - + if (vm.count("help")) { - std::cout << desc << "\n"; + std::cout << generalOptions << "\n"; return 1; } - + + std::optional logFolder; + if (vm.count("log_folder")) { + logFolder = vm["log_folder"].as(); + } + initLogging(logFolder, vm.count("debug")); + + BOOST_LOG_TRIVIAL(info) << "Starting application..."; + + BOOST_LOG_TRIVIAL(debug) << "Initializing params provider..."; SystemParamsProvider systemParamsProvider; LicenseClient licenseClient(systemParamsProvider, "license.dat"); licenseClient.init(); if (!licenseClient.isActivated()) { - std::cerr << "Application is not activated. Please contact Catalogue of Currencies support. Exiting..." << std::endl; - std::cout << "Activation request: " << licenseClient.buildActivationRequest() << std::endl; + const std::string msg = "Application is not activated. Please contact Catalogue of Currencies support."; + BOOST_LOG_TRIVIAL(error) << msg << " Exiting..."; + std::cout << msg << std::endl; + std::cout << "Activation request: " << std::endl << licenseClient.buildActivationRequest() << std::endl; return -2; } - if (vm.count("port")) { - std::cout << "Port " - << vm["port"].as() << ".\n"; - } - auto addressToListen = vm["bind_ip"].as(); auto portToListen = vm["port"].as(); auto docRoot = vm["data_root"].as(); auto threads = vm["threads"].as(); + BOOST_LOG_TRIVIAL(debug) << "Will listen on: " << addressToListen << ":" << portToListen; + BOOST_LOG_TRIVIAL(debug) << "Will serve data from : " << docRoot; + BOOST_LOG_TRIVIAL(debug) << "Number of serving threads : " << threads; auto ccServer = std::make_unique(addressToListen, portToListen, docRoot, threads); ccServer->run(); @@ -69,6 +120,7 @@ int main(int argc, const char* argv[]) { std::this_thread::sleep_for(std::chrono::milliseconds(500)); } + BOOST_LOG_TRIVIAL(info) << "Caught signal. Shutting down..."; ccServer->shutdown(); return 0; diff --git a/CMakeProject1/LicenseClient.cpp b/CMakeProject1/LicenseClient.cpp index 7f57d19..f01d025 100644 --- a/CMakeProject1/LicenseClient.cpp +++ b/CMakeProject1/LicenseClient.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -56,7 +57,7 @@ namespace ASN1 = CryptoPP::ASN1; namespace { -static const std::string productId = "coc"; +static const std::string productId = "cocserver"; static const uint32_t initializationVectorSize = AES::BLOCKSIZE; static const uint32_t macTagSize = 16; @@ -263,10 +264,12 @@ void LicenseClient::init() { const auto systemParams = skipEmptyParams(m_systemParamsProvider.retrieveSystemParams()); - std::cout << "Collected params: " << std::endl; - for (const auto& entry : systemParams) { - std::cout << "\t" << entry.first << " = " << entry.second << " ~= " << fletcher64(entry.second) << std::endl; + BOOST_LOG_TRIVIAL(debug) << "Collected params: "; + for (const auto& entry : systemParams) + { + BOOST_LOG_TRIVIAL(debug) << "\t" << entry.first << " = " << entry.second << " ~= " << fletcher64(entry.second); + } } m_systemParams = hashParams(systemParams); @@ -317,7 +320,7 @@ std::string LicenseClient::buildActivationRequest() ); // StringSource return std::string("-----BEGIN ACTIVATION REQUEST-----\n") + - base64Encode(cipher, std::make_optional(64)) + + base64Encode(std::string(iv, iv + initializationVectorSize) + cipher, std::make_optional(64)) + std::string("-----END ACTIVATION REQUEST-----\n"); } diff --git a/CMakeProject1/SystemParamsProvider_linux.cpp b/CMakeProject1/SystemParamsProvider_linux.cpp index 2ccba5e..9e23203 100644 --- a/CMakeProject1/SystemParamsProvider_linux.cpp +++ b/CMakeProject1/SystemParamsProvider_linux.cpp @@ -1,6 +1,7 @@ #include "SystemParamsProvider_linux.h" #include +#include #include #include @@ -47,7 +48,7 @@ namespace std::string line; std::ifstream myfile(fileName); if (myfile.is_open()) { - std::cout << "File " << fileName << " successfully open" << std::endl; + BOOST_LOG_TRIVIAL(debug) << "File " << fileName << " successfully open"; if (getline (myfile,line)) { return line; } @@ -58,6 +59,7 @@ namespace std::optional loadOsId() { + BOOST_LOG_TRIVIAL(debug) << "Trying to find Operating system ID..."; auto machineId = readFileLine("/var/lib/dbus/machine-id"); if (machineId.value_or("").empty()) { machineId = readFileLine("/etc/machine-id"); @@ -67,16 +69,19 @@ namespace std::optional loadBoardSerial() { + BOOST_LOG_TRIVIAL(debug) << "Trying to find mainboard serial number..."; return readFileLine("/sys/class/dmi/id/board_serial"); } std::optional loadComputerUUID() { + BOOST_LOG_TRIVIAL(debug) << "Trying to find product UUID..."; return readFileLine("/sys/class/dmi/id/product_uuid"); } std::optional loadComputerSerial() { + BOOST_LOG_TRIVIAL(debug) << "Trying to find computer serial number..."; auto productSerial = readFileLine("/sys/class/dmi/id/product_serial"); if(!productSerial.value_or("").empty()) { @@ -101,6 +106,7 @@ namespace CpuIdInfo loadCpuId() { + BOOST_LOG_TRIVIAL(debug) << "Trying to find processor idenfication..."; uint32_t a, b, c, d; __cpuid (0 /* vendor string */, a, b, c, d); std::cout << "EAX: " << std::hex << std::setw(8) << std::setfill('0') << a << "\nEBX: " << b << "\nECX: " << c << "\nEDX: " << d << "\n"; @@ -131,7 +137,7 @@ namespace std::regex line_regex(parameterRegex + ": " + valueRegex); std::smatch line_match; if(std::regex_search(line, line_match, line_regex)) { - std::cout << "DEBUG: Found " << line_match[1] << ": " << line_match[2] << std::endl; + BOOST_LOG_TRIVIAL(trace) << "Matched parameter pair " << line_match[1] << ": " << line_match[2]; return std::make_pair(line_match[1], line_match[2]); } return {}; @@ -173,7 +179,7 @@ namespace std::smatch section_match; if(std::regex_search(line, section_match, section_regex)) { section = std::atoi(section_match[1].str().c_str()); - std::cout << "DEBUG: Found section: " << section_match[1] << std::endl; + BOOST_LOG_TRIVIAL(debug) << "Found section: " << section_match[1]; } continue; } @@ -243,7 +249,7 @@ namespace if (!line.empty() && line[0] == 'e') { std::smatch section_match; if(std::regex_match(line, section_match, section_regex)) { - std::cout << "DEBUG: Found device: " << section_match[1] << " with MAC: " << section_match[2] << std::endl; + BOOST_LOG_TRIVIAL(debug) << "Found device : " << section_match[1] << " with MAC : " << section_match[2]; output[section_match[1]] = section_match[2]; } continue; @@ -321,7 +327,7 @@ namespace return {}; } - std::cout << "Found best mount point: " << bestEntry->mountPoint << std::endl; + BOOST_LOG_TRIVIAL(debug) << "Found best mount point: " << bestEntry->mountPoint; int depth = 0; while(depth < 10 && !bestEntry->parentName.empty()) { @@ -391,11 +397,11 @@ namespace detail } auto cpuIdInfo = loadCpuId(); - std::cout << "Vendor ID: " << cpuIdInfo.vendorId << std::endl; - std::cout << "Model ID: " << cpuIdInfo.modelId << std::endl; - std::cout << "Vendor String: " << cpuIdInfo.vendorString << std::endl; - std::cout << "Is hypervisor: " << cpuIdInfo.isHypervisor << std::endl; - std::cout << "Serial number: " << cpuIdInfo.serialNumber << std::endl; + BOOST_LOG_TRIVIAL(trace) << "Vendor ID: " << cpuIdInfo.vendorId << std::endl; + BOOST_LOG_TRIVIAL(trace) << "Model ID: " << cpuIdInfo.modelId << std::endl; + BOOST_LOG_TRIVIAL(trace) << "Vendor String: " << cpuIdInfo.vendorString << std::endl; + BOOST_LOG_TRIVIAL(trace) << "Is hypervisor: " << cpuIdInfo.isHypervisor << std::endl; + BOOST_LOG_TRIVIAL(trace) << "Serial number: " << cpuIdInfo.serialNumber << std::endl; result[SystemParamTypes::cpuIdModel] = intToHex(cpuIdInfo.vendorId) + intToHex(cpuIdInfo.modelId); if(cpuIdInfo.isHypervisor) {