From 722d25b32588b73ca4408f1cd6afd023df06a607 Mon Sep 17 00:00:00 2001 From: lukas Date: Wed, 29 Apr 2020 20:06:27 +0200 Subject: [PATCH] use libconfig for a /etc/iprefresher.cfg config file there can all the api credentials be specified --- .idea/inspectionProfiles/Project_Default.xml | 7 +++ CMakeLists.txt | 32 +++++++++++-- config/iprefresher.cfg | 12 +++++ inc/Credentials.h | 2 +- .../iprefresher.service | 0 src/Credentials.cpp | 48 ++++++++++++++++--- src/main.cpp | 2 +- 7 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 config/iprefresher.cfg rename iprefresher.service => service/iprefresher.service (100%) diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..7f519ab --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index b67c9d1..f991a69 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_BUILD_TYPE Release) # manually set build type (Release / Debug) set(LIB_METHOD STATIC) #SHARED / STATIC set(WinBuild false) -set(PROJECT_VERSION 1.2.2) +set(PROJECT_VERSION 1.2.3) option(BUILD_DOC "Build documentation" ON) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) @@ -59,9 +59,32 @@ if (CURL_FOUND) else () message(FATAL_ERROR "Could not find CURL") endif () -message("") include_directories(${CURL_INCLUDE_DIR} inc) +# libconfig +FIND_PATH(CONFIG++_INCLUDE_DIR libconfig.h++ /usr/include /usr/local/include) + +FIND_LIBRARY(CONFIG++_LIBRARY NAMES config++ PATH /usr/lib /usr/local/lib) + +IF (CONFIG++_INCLUDE_DIR AND CONFIG++_LIBRARY) + SET(CONFIG++_FOUND TRUE) +ENDIF ( CONFIG++_INCLUDE_DIR AND CONFIG++_LIBRARY) + +IF (CONFIG++_FOUND) + MESSAGE(STATUS "Found Config++: ${CONFIG++_LIBRARY}") +ELSE(CONFIG++_FOUND) + IF (Config++_FIND_REQUIRED) + IF(NOT CONFIG++_INCLUDE_DIR) + MESSAGE(FATAL_ERROR "Could not find LibConfig++ header file!") + ENDIF(NOT CONFIG++_INCLUDE_DIR) + + IF(NOT CONFIG++_LIBRARY) + MESSAGE(FATAL_ERROR "Could not find LibConfig++ library file!") + ENDIF(NOT CONFIG++_LIBRARY) + ENDIF (Config++_FIND_REQUIRED) +ENDIF (CONFIG++_FOUND) +message("") + #add version header FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h "\#pragma once\nclass Version {\npublic:\n static const std::string VERSION;\n};\n\nstd::string const Version::VERSION = \"${PROJECT_VERSION}\";" @@ -91,12 +114,13 @@ set(SOURCE add_executable(iprefresher ${SOURCE}) # LINK generated LIBS # -target_link_libraries(iprefresher api logger ${CURL_LIBRARIES}) +target_link_libraries(iprefresher api logger ${CURL_LIBRARIES} config++) # INSTALL to SYSTEM # set(CMAKE_INSTALL_PREFIX "/") install(TARGETS iprefresher DESTINATION usr/local/bin) -install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/iprefresher.service DESTINATION lib/systemd/system) +install(FILES service/iprefresher.service DESTINATION lib/systemd/system) +install(FILES config/iprefresher.cfg DESTINATION etc) IF (UNIX) diff --git a/config/iprefresher.cfg b/config/iprefresher.cfg new file mode 100644 index 0000000..74ee30f --- /dev/null +++ b/config/iprefresher.cfg @@ -0,0 +1,12 @@ +# Dynu IP refresher config +# 2020 +# Lukas Heiligenbrunner + +## DYNU API Config +dynuapikey = "12345" +domainid = "dd" +#domainname = "jj" + +## Telegram API Config +#telegramApiKey = "" +#chatId ="" diff --git a/inc/Credentials.h b/inc/Credentials.h index 3bbc1c8..db0acc5 100644 --- a/inc/Credentials.h +++ b/inc/Credentials.h @@ -16,7 +16,7 @@ public: static std::string telegramApiKey; static std::string chatId; - static bool checkCredentialValidity(); + static bool readCredentials(); private: }; \ No newline at end of file diff --git a/iprefresher.service b/service/iprefresher.service similarity index 100% rename from iprefresher.service rename to service/iprefresher.service diff --git a/src/Credentials.cpp b/src/Credentials.cpp index 9ae7227..b944d3a 100644 --- a/src/Credentials.cpp +++ b/src/Credentials.cpp @@ -3,14 +3,50 @@ // #include +#include +#include -std::string Credentials::dynuapikey = ""; -std::string Credentials::domainid = ""; //id of the dynu domain -std::string Credentials::domainname = ""; +#include "libconfig.h++" -std::string Credentials::telegramApiKey = ""; -std::string Credentials::chatId = ""; +std::string Credentials::dynuapikey; +std::string Credentials::domainid; //id of the dynu domain +std::string Credentials::domainname; -bool Credentials::checkCredentialValidity() { +std::string Credentials::telegramApiKey; +std::string Credentials::chatId; + +bool Credentials::readCredentials() { + libconfig::Config cfg; + try { + cfg.readFile("/etc/iprefresher.cfg"); + } + catch (const libconfig::FileIOException &fioex) { + std::cout << "I/O error while reading config file." << std::endl << "creating new config file!" << std::endl; + cfg.writeFile("/etc/iprefresher.cfg"); + return false; + } + catch (const libconfig::ParseException &pex) { + std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine() + << " - " << pex.getError() << std::endl; + return false; + } + + // Get the store name. + try { + // needed parameters + dynuapikey = (std::string) cfg.lookup("dynuapikey"); + domainid = (std::string) cfg.lookup("domainid"); + domainname = (std::string) cfg.lookup("domainname"); + // optional parameters + telegramApiKey = (std::string) cfg.lookup("telegramApiKey"); + chatId = (std::string) cfg.lookup("chatId"); + std::cout << "Store name: " << dynuapikey << std::endl; + } + catch (const libconfig::SettingNotFoundException &nfex) { + // triggered if setting is missing in config + if (!(std::strcmp("telegramApiKey", nfex.getPath()) == 0 || std::strcmp("chatId", nfex.getPath()) == 0)) { + std::cerr << "No '" << nfex.getPath() << "' setting in configuration file." << std::endl; + } + } return !(Credentials::dynuapikey.empty() || Credentials::domainid.empty() || Credentials::domainname.empty()); } diff --git a/src/main.cpp b/src/main.cpp index e6aea6e..654bb47 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,7 +5,7 @@ #include int main(int argc, char *argv[]) { - if (!Credentials::checkCredentialValidity()) { + if (!Credentials::readCredentials()) { std::cout << "incorrect credentials!" << std::endl; return -1; }