diff --git a/CMakeLists.txt b/CMakeLists.txt index 872d260a608f8d800f7ca6277fbcf7e5575cdd17..057dec0e95d3cd1557220c4fe2502bd033a1078b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,17 +7,19 @@ set ( VERSION_MAJOR 0 ) set ( VERSION_MINOR 0 ) # cpr requires c++11 -set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" ) if(WIN32) add_definitions( -DBOOST_ALL_NO_LIB ) set(Boost_USE_STATIC_LIBS ON) add_definitions(-DCURL_STATICLIB) add_definitions(-D_CRT_SECURE_NO_WARNINGS) add_definitions(-D_WIN32_WINNT=0x0501) + # add_compile_options("/std:c++11") + # set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++11) +else() + set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" ) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc") endif() -set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc") - # src : main, collect functions + jsoncpp library file ( GLOB SOURCES src/agent/*.cpp ) @@ -37,7 +39,6 @@ option( RUN_CURL_TESTS "Set to ON to run cURL tests." OFF ) option( CURL_STATICLIB "Set to ON to generated curl statically." ON ) add_subdirectory ( lib/cpr ) -include_directories ( ${CPR_INCLUDE_DIRS} ) include_directories ( ${CURL_INCLUDE_DIRS} ) # headers @@ -53,7 +54,7 @@ include ( "modules/boost.cmake" ) # compile set ( CMAKE_RUNTIME_OUTPUT_DIRECTORY "../bin" ) add_executable ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} ${SOURCES} ) -target_link_libraries ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} ${CPR_LIBRARIES} ${CURL_LIBRARIES} ${FILESYSTEM_LIB} ${SYSTEM_LIB} ${REGEX_LIB} ${PROGRAM_OPTIONS_LIB} ${CROSS_STATIC} ) +target_link_libraries ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} ${CURL_LIBRARIES} ${FILESYSTEM_LIB} ${SYSTEM_LIB} ${REGEX_LIB} ${PROGRAM_OPTIONS_LIB} ${CROSS_STATIC} ) add_dependencies ( agent-v${VERSION_MAJOR}.${VERSION_MINOR} Boost) diff --git a/include/agent/connection.h b/include/agent/connection.h index b127f3155d08e192da63c552c354f15749c80bae..d0aa5f355cdc95c26dd05d49764bbb711257b056 100644 --- a/include/agent/connection.h +++ b/include/agent/connection.h @@ -26,11 +26,11 @@ class Connection{ long httpCode = 0; /*!< Code return from webSerice (200, 404, 502 and so on) */ public: - bool downloadAgent (std::string, Agent*, int); - bool getVersionByProxy(Agent*, std::string*, std::string); - int getInstallerNameByProxy(std::string, Agent*, std::string*); - void setCurlOption(std::string, Agent*, std::string*); - int sendJsonByProxy(Agent*, std::string, int); + bool downloadAgent (std::string, Agent*, bool); + bool getVersion(Agent*, std::string*, std::string, bool); + int getInstallerName(std::string, Agent*, std::string*, bool); + void setCurlOption(std::string, Agent*, std::string*, bool); + int sendJson(Agent*, std::string, int, bool); /** Default callback function that is necessary to make a curl request. * @brief get agent binary (agent.run or agent.exe) diff --git a/include/agent/get.h b/include/agent/get.h index b7d5ffc088487094db983485dede9d98eb053549..474c3e38d250dbde0aed830345edc66ed6c06b36 100644 --- a/include/agent/get.h +++ b/include/agent/get.h @@ -2,7 +2,6 @@ #include <iostream> #include <fstream> #include <stdlib.h> -#include <cpr/cpr.h> #include <json/json.h> #include <agent/common.h> #include <agent/connection.h> @@ -24,6 +23,6 @@ const std::string url = "http://localhost:3001"; int check_update(Agent*); bool create_directories(std::string); bool compare_version(Agent*, std::string); -bool download(Agent*, std::string, int); +bool download(Agent*, std::string, bool); void execute_agent(Agent*, boost::filesystem::path); -bool get_agent_installer_name(Agent*, std::string*, int); +bool get_agent_installer_name(Agent*, std::string*, bool); diff --git a/include/agent/main.h b/include/agent/main.h index ac6b3c06596a693cb3a1f36419dc6890d66842a8..2c513f3279f36301a83c2a1dabf314002c21f79b 100644 --- a/include/agent/main.h +++ b/include/agent/main.h @@ -17,7 +17,6 @@ #include <boost/program_options/parsers.hpp> #include <boost/program_options/variables_map.hpp> #include <boost/filesystem/fstream.hpp> -#include <cpr/cpr.h> namespace pt = boost::posix_time; namespace po = boost::program_options; diff --git a/include/agent/post.h b/include/agent/post.h index dfd9e5de0ecaab98f98c1af862cf5a192458b218..70d3af6c83360c2d1d887d319f52b825ea94a428 100644 --- a/include/agent/post.h +++ b/include/agent/post.h @@ -1,6 +1,5 @@ #pragma once #include <iostream> -#include <cpr/cpr.h> #include <json/json.h> #include <agent/agent.h> #include <agent/connection.h> diff --git a/src/agent/connection.cpp b/src/agent/connection.cpp index 89dab77f46318219ab4f365129ae979388949133..b9a4a02a0263ec3879a88bd346593d8a3f57c9a5 100644 --- a/src/agent/connection.cpp +++ b/src/agent/connection.cpp @@ -31,7 +31,7 @@ * @param agent object agent * @param option use proxy or not (option == 0 - use proxy) */ -bool Connection::downloadAgent(std::string url, Agent* agent, int option) { +bool Connection::downloadAgent(std::string url, Agent* agent, bool option) { curl = curl_easy_init(); if (!curl) { agent->logFile << __DATE__ + std::string(" - ERROR: Curl ") + @@ -79,13 +79,15 @@ bool Connection::downloadAgent(std::string url, Agent* agent, int option) { curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Connection::callBackDownload); - if (option == 0) { + if (option) { std::string::size_type sz; // NOLINT [runtime/int] long port; // NOLINT [runtime/int] try { port = std::stol(agent->getProxy()->getPort(), &sz); // NOLINT [runtime/int] } catch (std::invalid_argument& ex) { + agent->logFile << __DATE__ + std::string(" - ERROR: ") + + std::string(ex.what()) << std::endl; port = 0; } @@ -147,28 +149,34 @@ bool Connection::downloadAgent(std::string url, Agent* agent, int option) { * @param version pointer variable thats receive version or filename from web service. */ void Connection::setCurlOption(std::string url, Agent* agent, - std::string* out) { + std::string* out, bool opt) { std::string::size_type sz; // NOLINT [runtime/int] long port; // NOLINT [runtime/int] try { port = std::stol(agent->getProxy()->getPort(), &sz); // NOLINT [runtime/int] } catch (std::invalid_argument& ex) { + agent->logFile << __DATE__ + std::string(" - ERROR: ") + + std::string(ex.what()) << std::endl; port = 0; } curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); - curl_easy_setopt(curl, CURLOPT_PROXY, agent->getProxy()->getHost().c_str()); - curl_easy_setopt(curl, CURLOPT_PROXYPORT, port); - curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME, + if (opt) { + curl_easy_setopt(curl, CURLOPT_PROXY, + agent->getProxy()->getHost().c_str()); + curl_easy_setopt(curl, CURLOPT_PROXYPORT, port); + curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME, agent->getProxy()->getUser().c_str()); - curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD, + curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD, agent->getProxy()->getPassword().c_str()); + } + curl_easy_setopt(curl, CURLOPT_WRITEDATA, out); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Connection::callback); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); - // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); - curl_easy_setopt(curl, CURLOPT_FAILONERROR, true); + curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); + // curl_easy_setopt(curl, CURLOPT_FAILONERROR, true); response = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode); curl_easy_cleanup(curl); @@ -181,9 +189,10 @@ void Connection::setCurlOption(std::string url, Agent* agent, * @param url references to webService * @param agent object agent * @param version pointer variable thats receive version from web service; +* @param opt is proxy request or not */ -bool Connection::getVersionByProxy(Agent* agent, std::string *version, - std::string url) { +bool Connection::getVersion(Agent* agent, std::string *version, + std::string url, bool opt) { struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Accept: application/json"); headers = curl_slist_append(headers, "Content-Type: application/json"); @@ -193,8 +202,8 @@ bool Connection::getVersionByProxy(Agent* agent, std::string *version, curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); - this->setCurlOption(url, agent, version); - if (httpCode == 200) + this->setCurlOption(url, agent, version, opt); + if (this->httpCode == 200) return true; else return false; @@ -207,12 +216,13 @@ bool Connection::getVersionByProxy(Agent* agent, std::string *version, * @param url references to webService * @param agent object agent * @param version pointer variable thats receive file name from web service; +* @param opt is proxy request or not */ -int Connection::getInstallerNameByProxy(std::string url, Agent* agent, - std::string* filename) { +int Connection::getInstallerName(std::string url, Agent* agent, + std::string* filename, bool opt) { curl = curl_easy_init(); - this->setCurlOption(url, agent, filename); - return httpCode; + this->setCurlOption(url, agent, filename, opt); + return this->httpCode; } /** Contact webservice server using proxy to send json (inventory, net_usage or user_history) @@ -221,15 +231,19 @@ int Connection::getInstallerNameByProxy(std::string url, Agent* agent, * * @param agent object agent * @param json references to json -* @param opt opt == 0 (send inventory) | opt == 2 (send net bandwidth) | otherwise (send user history) +* @param send send == 0 (send inventory) | send == 2 (send net bandwidth) | otherwise (send user history) +* @param opt is proxy request or not */ -int Connection::sendJsonByProxy(Agent* agent, std::string json, int opt) { +int Connection::sendJson(Agent* agent, std::string json, + int send, bool opt) { std::string::size_type sz; // NOLINT [runtime/int] long port; // NOLINT [runtime/int] try { port = std::stol(agent->getProxy()->getPort(), &sz); // NOLINT [runtime/int] } catch (std::invalid_argument& ex) { + agent->logFile << __DATE__ + std::string(" - ERROR: ") + + std::string(ex.what()) << std::endl; port = 0; } @@ -243,9 +257,9 @@ int Connection::sendJsonByProxy(Agent* agent, std::string json, int opt) { // opt == 1 {send inventory} // opt = 2 {send net bandwidth} // otherwise, {send user history} - if (opt == 1) + if (send == 1) base_url = base_url + std::string("/api/agent/collect/inventory"); - else if (opt == 2) + else if (send == 2) base_url = base_url + std::string("/api/agent/collect/net_usage"); else base_url = base_url + std::string("/api/agent/collect/user_history"); @@ -254,12 +268,15 @@ int Connection::sendJsonByProxy(Agent* agent, std::string json, int opt) { curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_URL, base_url.c_str()); - curl_easy_setopt(curl, CURLOPT_PROXY, agent->getProxy()->getHost().c_str()); - curl_easy_setopt(curl, CURLOPT_PROXYPORT, port); - curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME, - agent->getProxy()->getUser().c_str()); - curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD, - agent->getProxy()->getPassword().c_str()); + if (opt) { + curl_easy_setopt(curl, CURLOPT_PROXY, + agent->getProxy()->getHost().c_str()); + curl_easy_setopt(curl, CURLOPT_PROXYPORT, port); + curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME, + agent->getProxy()->getUser().c_str()); + curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD, + agent->getProxy()->getPassword().c_str()); + } curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json.c_str()); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); diff --git a/src/agent/get.cpp b/src/agent/get.cpp index b3047e3a043448c34575f659c4ad5c5a2fe82e5e..e1eca5f9a181531252ea72dd8ea46afe80277672 100644 --- a/src/agent/get.cpp +++ b/src/agent/get.cpp @@ -35,29 +35,22 @@ int check_update(Agent* agent) { Json::Reader reader; Json::Value version_json; Json::FastWriter fastWriter; - int option = 1; - - /* Contact webservice server to get client version. */ - auto p = cpr::Get(cpr::Url{agent->getConf()->getWebService() + - "/api/agent/version"}, - cpr::Header{{"content-type", "application/json"}}); - - // Check error with connection - if (p.status_code != 200) { - option = 0; - // Case if not connected before, try it with proxy - std::string web_service = agent->getConf()->getWebService() + + bool option = false; + + std::string web_service = agent->getConf()->getWebService() + std::string("/api/agent/version"); - if (!agent->getConnection()->getVersionByProxy(agent, &version, - web_service)) { + if (!agent->getConnection()->getVersion(agent, &version, + web_service, 0)) { agent->logFile << __DATE__ << " - ERROR: Couldn't get current " << - "version from simmc web service." << std::endl; + "version from simmc web service without pŕoxy." << std::endl; + option = true; + if (!agent->getConnection()->getVersion(agent, &version, + web_service, 1)) { + agent->logFile << __DATE__ << " - ERROR: Couldn't get current " << + "version from simmc web service with proxy." << std::endl; } - } else { - version = p.text; } - // p.text - response from api reader.parse(version, version_json, false); version = fastWriter.write(version_json["version"]); boost::regex re("(\")|(\n)"); @@ -130,7 +123,7 @@ bool compare_version(Agent *agent, std::string version) { } /* Contact webservice server to request a agent installer. */ -bool get_agent_installer_name(Agent* agent, std::string* filename, int opt) { +bool get_agent_installer_name(Agent* agent, std::string* filename, bool opt) { Json::FastWriter fastWriter; Json::Value filename_json; Json::Reader reader; @@ -147,17 +140,13 @@ bool get_agent_installer_name(Agent* agent, std::string* filename, int opt) { + std::string(os) + std::string("/") + std::to_string(agent->getConf()->getIdPoint()); - if (opt == 1) { - auto p = cpr::Get(cpr::Url{ws}, // NOLINT [whitespace/line_length] - cpr::Header{{"content-type", "application/json"}}); - status_code = p.status_code; - *filename = p.text; - } else { - status_code = agent->getConnection()->getInstallerNameByProxy(ws, agent, filename); // NOLINT [whitespace/line_length] - } + if (opt) + status_code = agent->getConnection()->getInstallerName(ws, agent, filename, opt); // NOLINT [whitespace/line_length] + else + status_code = agent->getConnection()->getInstallerName(ws, agent, filename, opt); // NOLINT [whitespace/line_length] + if (status_code == 200) { - // p.text - response from api reader.parse(*filename, filename_json, false); *filename = fastWriter.write(filename_json["filename"]); @@ -173,11 +162,9 @@ bool get_agent_installer_name(Agent* agent, std::string* filename, int opt) { /** * To make agent download. * Curl library is used to make a webService request to get agent zipfile. - * option -> indicate proxy or not - * 0 -> proxy - * 1 -> not proxy + * option -> indicate request with proxy or not */ -bool download(Agent* agent, std::string filename, int option) { +bool download(Agent* agent, std::string filename, bool option) { std::string web_service = agent->getConf()->getWebService() + std::string("/install/") + std::string(filename); diff --git a/src/agent/post.cpp b/src/agent/post.cpp index 6489c9330f37fe942b4bbb9c684adff7ed8c4c40..6c275b060d86eb65e41f4045fa86ad81c6969d3f 100644 --- a/src/agent/post.cpp +++ b/src/agent/post.cpp @@ -42,18 +42,14 @@ int send_inventory(bool print, Agent* agent) { std::cout << "Empty inventory object" << std::endl; } - auto p = cpr::Post(cpr::Url{base_url + "/api/agent/collect/inventory"}, - cpr::Body{fastWriter.write(inventory)}, - cpr::Header{{"content-type", "application/json"}}); - - if (p.status_code == 200) + if (agent->getConnection()->sendJson(agent, + fastWriter.write(inventory), 1, 0) == 200) + return 1; + else if (agent->getConnection()->sendJson(agent, + fastWriter.write(inventory), 1, 1) == 200) return 1; else - if (agent->getConnection()->sendJsonByProxy(agent, - fastWriter.write(inventory), 1) == 200) - return 1; - - return 0; + return 0; } /*************************************** @@ -77,18 +73,14 @@ int send_net_bandwidth(bool print, Agent* agent) { std::cout << "Empty network_bandwidth object" << std::endl; } - auto p = cpr::Post(cpr::Url{base_url + "/api/agent/collect/net_usage"}, - cpr::Body{fastWriter.write(net)}, - cpr::Header{{"content-type", "application/json"}}); - - if (p.status_code == 200) + if (agent->getConnection()->sendJson(agent, + fastWriter.write(net), 2, 0) == 200) + return 1; + else if (agent->getConnection()->sendJson(agent, + fastWriter.write(net), 2, 1) == 200) return 1; else - if (agent->getConnection()->sendJsonByProxy(agent, - fastWriter.write(net), 2) == 200) - return 1; - - return 0; + return 0; } /********************************** @@ -112,19 +104,14 @@ int send_user_history(bool print, Agent* agent) { std::cout << "Empty user_history object" << std::endl; } - auto p = cpr::Post(cpr::Url{base_url + "/api/agent/collect/user_history"}, - cpr::Parameters{{"type", "user_history"}}, - cpr::Body{fastWriter.write(u_h)}, - cpr::Header{{"content-type", "application/json"}}); - - if (p.status_code == 200) + if (agent->getConnection()->sendJson(agent, + fastWriter.write(u_h), 3, 0) == 200) + return 1; + else if (agent->getConnection()->sendJson(agent, + fastWriter.write(u_h), 3, 1) == 200) return 1; else - if (agent->getConnection()->sendJsonByProxy(agent, - fastWriter.write(u_h), 3) == 200) - return 1; - - return 0; + return 0; } /**