From a356bca964744e88019bc64bb74a383f8536f175 Mon Sep 17 00:00:00 2001 From: lukas Date: Mon, 18 May 2020 19:46:19 +0200 Subject: [PATCH 01/17] install config in other folder win --- CMakeLists.txt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 70dc0b8..ae57b65 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,8 +18,8 @@ SET(LIB_METHOD STATIC) #SHARED / STATIC option(BUILD_DOC "Build documentation" ON) # additional dependency for Doxygen option(PACKAGING "Allow Packaging to , or " ON) # additional dependencies for RPMbuild,dpkg or NSIS -option(TESTS "Build Tests" ON) # additional dependencies for GTEST - to build tests -set(WinBuild false) +option(TESTS "Build Tests" false) # additional dependencies for GTEST - to build tests +set(WinBuild true) # helper variables SET(CMAKE_CXX_STANDARD 17) @@ -65,7 +65,7 @@ if (${WinBuild}) # or add_definitions(-DCURL_STATICLIB) # windows config path is same as executable - set(CONFIG_PATH "./iprefresher.cfg") + set(CONFIG_PATH "../Common Files/iprefresher/iprefresher.cfg") else () set(LIBSUFFIX .so) set(SUFFIX "") @@ -188,6 +188,9 @@ ELSE () set_target_properties(iprefresher PROPERTIES SUFFIX ".exe") install(TARGETS iprefresher DESTINATION .) + # create config directory + install(DIRECTORY DESTINATION "../Common\ Files/iprefresher") + # install .dll dependencies # todo check if files exist... install(FILES /usr/${TOOLCHAIN_PREFIX}/sys-root/mingw/bin/libcurl-4.dll From da53941cba5f9ca309d13dfeeaad89b465111cd2 Mon Sep 17 00:00:00 2001 From: Lukas-Heiligenbrunner <30468956+Lukas-Heiligenbrunner@users.noreply.github.com> Date: Tue, 19 May 2020 16:35:57 +0200 Subject: [PATCH 02/17] enable and start iprefresher service automatically after package installation (#18) - dont terminate service if config not valid. --- CMakeLists.txt | 6 ++++-- src/IPRefresher.cpp | 13 +++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 70dc0b8..5e1953f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -226,8 +226,10 @@ if (${PACKAGING}) if [ ! -f ${CONFIG_PATH} ]; then cat > ${CONFIG_PATH} <<- EOM ${SAMPLECONFIG}EOM -fi\n" - ) +fi + +systemctl enable iprefresher.service +systemctl start iprefresher.service") SET(CPACK_DEB_COMPONENT_INSTALL 1) diff --git a/src/IPRefresher.cpp b/src/IPRefresher.cpp index 3dbcf0f..092b577 100644 --- a/src/IPRefresher.cpp +++ b/src/IPRefresher.cpp @@ -54,14 +54,15 @@ IPRefresher::IPRefresher(bool loop) { if (loop) { Logger::message("startup of service"); Logger::message("Version: " + Version::VERSION); - if (Config::readConfig()) { - while (true) { - Logger::message("starting check"); + + while (true) { + Logger::message("starting check"); + if (Config::readConfig()) { checkIPAdress(false); - std::this_thread::sleep_for(std::chrono::milliseconds(300000)); + } else { + std::cout << "incorrect credentials!" << std::endl; } - } else { - std::cout << "incorrect credentials!" << std::endl; + std::this_thread::sleep_for(std::chrono::milliseconds(300000)); } } } From fb65ef98fa5e1399cfaa49f914b34d7cff206fde Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 19 May 2020 20:58:18 +0200 Subject: [PATCH 03/17] new gitlab ci for windows build cmake- check if gtest is installed on build system validate compiler after setting of compiler paths --- .gitlab-ci.yml | 84 +++++++++++++++++++++++++++++++++++++++++--------- CMakeLists.txt | 84 ++++++++++++++++++++++++++++++-------------------- 2 files changed, 119 insertions(+), 49 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 4524b1c..6cb35d0 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,49 +1,103 @@ -image: luki42/dynuiprefresher_build:latest - stages: - cmake - build - - build_package - - test - -cache: - paths: - - build/ - - src/ - - inc/ - - tests/ - - postinst + - post +# Unix Build cmake: stage: cmake + image: luki42/dynuiprefresher_build:latest script: - cmake -S . -B build + cache: + paths: + - build/ + - inc/ + - postinst build: stage: build + image: luki42/dynuiprefresher_build:latest script: - cd build - make artifacts: paths: - "build/bin/*" + cache: + paths: + - build/ + - inc/ + - postinst build_package: - stage: build_package + stage: post + image: luki42/dynuiprefresher_build:latest script: - cd build - make package artifacts: paths: - "build/packages/*" + cache: + paths: + - build/ + - inc/ + - postinst test: - stage: test + stage: post + image: luki42/dynuiprefresher_build:latest script: - cd build - make test - make build-xml artifacts: reports: - junit: build/report.xml \ No newline at end of file + junit: build/report.xml + cache: + paths: + - build/ + - inc/ + - postinst + +# Windows Build +cmake_win64: + stage: cmake + image: luki42/dynuiprefresher_build:windows + script: + - cmake -S . -B build -D WinBuild=ON + cache: + paths: + - build/ + - inc/ + +build_win64: + stage: build + image: luki42/dynuiprefresher_build:windows + script: + - cd build + - make iprefresher + artifacts: + paths: + - "build/bin/*" + cache: + paths: + - build/ + - inc/ + + +build_package_win64: + stage: post + image: luki42/dynuiprefresher_build:windows + script: + - cd build + - make package + artifacts: + paths: + - "build/packages/*" + cache: + paths: + - build/ + - inc/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e1953f..4d341ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ # for test build gtest needs to be installed. cmake_minimum_required(VERSION 3.13) -project(iprefresher DESCRIPTION "Dynu ip refresher") +project(iprefresher DESCRIPTION "Dynu ip refresher" LANGUAGES) SET(PROJECT_VERSION 1.3.3) # CONFIGURATION @@ -19,7 +19,7 @@ SET(LIB_METHOD STATIC) #SHARED / STATIC option(BUILD_DOC "Build documentation" ON) # additional dependency for Doxygen option(PACKAGING "Allow Packaging to , or " ON) # additional dependencies for RPMbuild,dpkg or NSIS option(TESTS "Build Tests" ON) # additional dependencies for GTEST - to build tests -set(WinBuild false) +option(WinBuild "cross compile for Windows Platform" OFF) # helper variables SET(CMAKE_CXX_STANDARD 17) @@ -31,6 +31,7 @@ SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) # setup winbuild compilers if (${WinBuild}) + message(STATUS "setup Mingw Toolchain for cross compile.") set(LIBSUFFIX .dll) set(SUFFIX .exe) @@ -40,9 +41,9 @@ if (${WinBuild}) #set(TOOLCHAIN_PREFIX i686-w64-mingw32) # cross compilers to use for C and C++ - set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc) - set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++) - set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres) + set(CMAKE_C_COMPILER /usr/bin/${TOOLCHAIN_PREFIX}-gcc) + set(CMAKE_CXX_COMPILER /usr/bin/${TOOLCHAIN_PREFIX}-g++) + set(CMAKE_RC_COMPILER /usr/bin/${TOOLCHAIN_PREFIX}-windres) # target environment on the build host system set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX}) @@ -67,6 +68,7 @@ if (${WinBuild}) # windows config path is same as executable set(CONFIG_PATH "./iprefresher.cfg") else () + message(STATUS "using nativ gcc toolchain.") set(LIBSUFFIX .so) set(SUFFIX "") @@ -74,8 +76,16 @@ else () set(CONFIG_PATH "/etc/iprefresher.cfg") endif () +# test compiler settings and enable languages here +message("") +message(STATUS "Testing the C++ compiler!") +enable_language(CXX) +message("") +message(STATUS "Testing the C compiler!") +enable_language(C) # config libs +message("") message(STATUS "Config of Libraries") # libcurl if (${WinBuild}) @@ -313,39 +323,45 @@ endif (BUILD_DOC) # Test Cases if (TESTS) - include(GoogleTest) + # include(GoogleTest) + message(STATUS "Configuring GTEST") + find_package(GTest) + if (GTEST_FOUND) - mark_as_advanced( - BUILD_GMOCK BUILD_GTEST BUILD_SHARED_LIBS - gmock_build_tests gtest_build_samples gtest_build_tests - gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols - ) + mark_as_advanced( + BUILD_GMOCK BUILD_GTEST BUILD_SHARED_LIBS + gmock_build_tests gtest_build_samples gtest_build_tests + gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols + ) - enable_testing() + enable_testing() - macro(package_add_test TESTNAME) - # create an exectuable in which the tests will be stored - add_executable(${TESTNAME} ${ARGN}) - # link the Google test infrastructure, mocking library, and a default main fuction to - target_link_libraries(${TESTNAME} gtest gtest_main -lpthread -lm dynuiprefresher api ${CURL_LIBRARIES} ${LIBCONFIG++_LIBRARIES}) - # see https://cmake.org/cmake/help/v3.10/module/GoogleTest.html for more options to pass to it - gtest_discover_tests(${TESTNAME} - WORKING_DIRECTORY ${PROJECT_DIR} - EXTRA_ARGS --gtest_output=xml:report.xml -VV - PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${PROJECT_DIR}" - ) - set_target_properties(${TESTNAME} PROPERTIES FOLDER tests) - endmacro() + macro(package_add_test TESTNAME) + # create an exectuable in which the tests will be stored + add_executable(${TESTNAME} ${ARGN}) + # link the Google test infrastructure, mocking library, and a default main fuction to + target_link_libraries(${TESTNAME} gtest gtest_main -lpthread -lm dynuiprefresher api ${CURL_LIBRARIES} ${LIBCONFIG++_LIBRARIES}) + # see https://cmake.org/cmake/help/v3.10/module/GoogleTest.html for more options to pass to it + gtest_discover_tests(${TESTNAME} + WORKING_DIRECTORY ${PROJECT_DIR} + EXTRA_ARGS --gtest_output=xml:report.xml -VV + PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${PROJECT_DIR}" + ) + set_target_properties(${TESTNAME} PROPERTIES FOLDER tests) + endmacro() - package_add_test(test1 tests/UnitTest.cpp ${SOURCE}) + package_add_test(test1 tests/UnitTest.cpp ${SOURCE}) - add_custom_target(build-test - "${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --target test - DEPENDS ${PROJECT_NAME} - COMMENT "Packing ${PROJECT_NAME}") + add_custom_target(build-test + "${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --target test1 test + DEPENDS ${PROJECT_NAME} + COMMENT "Packing ${PROJECT_NAME}") - add_custom_target(build-xml - "bin/test1" --gtest_output="xml:report.xml" - DEPENDS ${PROJECT_NAME} - COMMENT "Packing ${PROJECT_NAME}") + add_custom_target(build-xml + "bin/test1" --gtest_output="xml:report.xml" + DEPENDS ${PROJECT_NAME} + COMMENT "Packing ${PROJECT_NAME}") + else () + message(STATUS "GTEST environment not found!") + endif () ENDIF () \ No newline at end of file From 5271a25cbc5d5c6966f419a257d66d54ffed52ff Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 19 May 2020 21:13:53 +0200 Subject: [PATCH 04/17] use artifacts instead of cache --- .gitlab-ci.yml | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6cb35d0..b95c1da 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -9,7 +9,7 @@ cmake: image: luki42/dynuiprefresher_build:latest script: - cmake -S . -B build - cache: + artifacts: paths: - build/ - inc/ @@ -22,9 +22,6 @@ build: - cd build - make artifacts: - paths: - - "build/bin/*" - cache: paths: - build/ - inc/ @@ -38,9 +35,6 @@ build_package: - cd build - make package artifacts: - paths: - - "build/packages/*" - cache: paths: - build/ - inc/ @@ -56,7 +50,6 @@ test: artifacts: reports: junit: build/report.xml - cache: paths: - build/ - inc/ @@ -68,7 +61,7 @@ cmake_win64: image: luki42/dynuiprefresher_build:windows script: - cmake -S . -B build -D WinBuild=ON - cache: + artifacts: paths: - build/ - inc/ @@ -80,9 +73,6 @@ build_win64: - cd build - make iprefresher artifacts: - paths: - - "build/bin/*" - cache: paths: - build/ - inc/ @@ -95,9 +85,6 @@ build_package_win64: - cd build - make package artifacts: - paths: - - "build/packages/*" - cache: paths: - build/ - inc/ \ No newline at end of file From 1239ae015a08c08cd582d436e1c49c5d35d13497 Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 19 May 2020 21:29:36 +0200 Subject: [PATCH 05/17] right ci camke dependencies for win and unix build --- .gitlab-ci.yml | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b95c1da..9025908 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -20,12 +20,14 @@ build: image: luki42/dynuiprefresher_build:latest script: - cd build - - make + - make iprefresher artifacts: paths: - build/ - inc/ - postinst + dependencies: + - cmake build_package: @@ -39,6 +41,8 @@ build_package: - build/ - inc/ - postinst + dependencies: + - build test: stage: post @@ -54,6 +58,8 @@ test: - build/ - inc/ - postinst + dependencies: + - build # Windows Build cmake_win64: @@ -76,6 +82,8 @@ build_win64: paths: - build/ - inc/ + dependencies: + - cmake_win64 build_package_win64: @@ -87,4 +95,6 @@ build_package_win64: artifacts: paths: - build/ - - inc/ \ No newline at end of file + - inc/ + dependencies: + - build_win64 \ No newline at end of file From ede1afcce3fb7d7d04e0a8cf06aef7820dd9ef9c Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 19 May 2020 21:50:43 +0200 Subject: [PATCH 06/17] correct build dependencies for unit test --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9025908..c461647 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -49,7 +49,7 @@ test: image: luki42/dynuiprefresher_build:latest script: - cd build - - make test + - make build-test - make build-xml artifacts: reports: From d8c3d4ffb82f177bf24004f24c98baea0b0862da Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 19 May 2020 23:57:16 +0200 Subject: [PATCH 07/17] correct build of gtests in one target with xml output --- .gitlab-ci.yml | 5 ----- CMakeLists.txt | 8 ++------ 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c461647..089c8ab 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -50,14 +50,9 @@ test: script: - cd build - make build-test - - make build-xml artifacts: reports: junit: build/report.xml - paths: - - build/ - - inc/ - - postinst dependencies: - build diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d341ab..ed2a914 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -353,12 +353,8 @@ if (TESTS) package_add_test(test1 tests/UnitTest.cpp ${SOURCE}) add_custom_target(build-test - "${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --target test1 test - DEPENDS ${PROJECT_NAME} - COMMENT "Packing ${PROJECT_NAME}") - - add_custom_target(build-xml - "bin/test1" --gtest_output="xml:report.xml" + "${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --target test1 + COMMAND ctest --force-new-ctest-process --gtest_output="xml:report.xml" DEPENDS ${PROJECT_NAME} COMMENT "Packing ${PROJECT_NAME}") else () From 998f3ea5e818baba9fef74e5c4d55ccd114d202a Mon Sep 17 00:00:00 2001 From: lukas Date: Wed, 20 May 2020 19:25:37 +0200 Subject: [PATCH 08/17] right xml export of unit test cases for test target --- .gitlab-ci.yml | 2 +- CMakeLists.txt | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 089c8ab..b2e2c5b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -52,7 +52,7 @@ test: - make build-test artifacts: reports: - junit: build/report.xml + junit: build/*.xml dependencies: - build diff --git a/CMakeLists.txt b/CMakeLists.txt index ed2a914..5a428cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -343,20 +343,23 @@ if (TESTS) target_link_libraries(${TESTNAME} gtest gtest_main -lpthread -lm dynuiprefresher api ${CURL_LIBRARIES} ${LIBCONFIG++_LIBRARIES}) # see https://cmake.org/cmake/help/v3.10/module/GoogleTest.html for more options to pass to it gtest_discover_tests(${TESTNAME} - WORKING_DIRECTORY ${PROJECT_DIR} - EXTRA_ARGS --gtest_output=xml:report.xml -VV - PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${PROJECT_DIR}" + TEST_PREFIX new: + EXTRA_ARGS --gtest_output=xml + XML_OUTPUT_DIR diri ) - set_target_properties(${TESTNAME} PROPERTIES FOLDER tests) + + add_custom_command(TARGET build-test + POST_BUILD + COMMAND "bin/${item}" --gtest_output=xml:${item}-test-report.xml) endmacro() + add_custom_target(build-test + "${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --target ${testTargets} + DEPENDS ${PROJECT_NAME} + COMMENT "Testing ${PROJECT_NAME}") + package_add_test(test1 tests/UnitTest.cpp ${SOURCE}) - add_custom_target(build-test - "${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --target test1 - COMMAND ctest --force-new-ctest-process --gtest_output="xml:report.xml" - DEPENDS ${PROJECT_NAME} - COMMENT "Packing ${PROJECT_NAME}") else () message(STATUS "GTEST environment not found!") endif () From 8199fc2c1e2dc3a081027ef1ec81e50924181c87 Mon Sep 17 00:00:00 2001 From: lukas Date: Wed, 20 May 2020 19:37:39 +0200 Subject: [PATCH 09/17] correct build of test targets correct upload of report.xml --- CMakeLists.txt | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a428cc..d9ea427 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -342,19 +342,15 @@ if (TESTS) # link the Google test infrastructure, mocking library, and a default main fuction to target_link_libraries(${TESTNAME} gtest gtest_main -lpthread -lm dynuiprefresher api ${CURL_LIBRARIES} ${LIBCONFIG++_LIBRARIES}) # see https://cmake.org/cmake/help/v3.10/module/GoogleTest.html for more options to pass to it - gtest_discover_tests(${TESTNAME} - TEST_PREFIX new: - EXTRA_ARGS --gtest_output=xml - XML_OUTPUT_DIR diri - ) + gtest_discover_tests(${TESTNAME}) add_custom_command(TARGET build-test POST_BUILD - COMMAND "bin/${item}" --gtest_output=xml:${item}-test-report.xml) + COMMAND "${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --target ${TESTNAME} + COMMAND "bin/${TESTNAME}" --gtest_output=xml:${TESTNAME}-report.xml) endmacro() add_custom_target(build-test - "${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --target ${testTargets} DEPENDS ${PROJECT_NAME} COMMENT "Testing ${PROJECT_NAME}") From ca47949a38bb53dad50e3a62ccf2ba7c946dbbfe Mon Sep 17 00:00:00 2001 From: lukas Date: Thu, 21 May 2020 11:26:15 +0200 Subject: [PATCH 10/17] check if config folder exists if not create it right appdata folder for windows hosts seperate config path and config name in cmake header --- CMakeLists.txt | 9 +++++---- src/Config.cpp | 30 ++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ae57b65..71afba9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,7 @@ SET(LIB_METHOD STATIC) #SHARED / STATIC option(BUILD_DOC "Build documentation" ON) # additional dependency for Doxygen option(PACKAGING "Allow Packaging to , or " ON) # additional dependencies for RPMbuild,dpkg or NSIS option(TESTS "Build Tests" false) # additional dependencies for GTEST - to build tests -set(WinBuild true) +set(WinBuild false) # helper variables SET(CMAKE_CXX_STANDARD 17) @@ -65,13 +65,13 @@ if (${WinBuild}) # or add_definitions(-DCURL_STATICLIB) # windows config path is same as executable - set(CONFIG_PATH "../Common Files/iprefresher/iprefresher.cfg") + set(CONFIG_PATH "std::string(std::getenv(\"USERPROFILE\")) + \"\\\\AppData\\\\Roaming\\\\DynuIPrefresher\\\\\"") else () set(LIBSUFFIX .so) set(SUFFIX "") # set /etc/ config path - set(CONFIG_PATH "/etc/iprefresher.cfg") + set(CONFIG_PATH "\"/etc/\"") endif () @@ -149,7 +149,8 @@ FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h namespace Version { const std::string VERSION = \"${PROJECT_VERSION}\"; - const std::string ConfigDir = \"${CONFIG_PATH}\"; + const std::string ConfigDir = ${CONFIG_PATH}; + const std::string ConfName = \"iprefresher.cfg\"; const std::string SAMPLECONFIG = R\"(${SAMPLECONFIG})\"; }" ) diff --git a/src/Config.cpp b/src/Config.cpp index 32aba2a..217bdc7 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -6,6 +6,7 @@ #include #include #include +#include std::string Config::dynuapikey; std::string Config::domainid; //id of the dynu domain @@ -19,13 +20,38 @@ bool Config::telegramSupport; bool Config::readConfig() { libconfig::Config cfg; try { - cfg.readFile(Version::ConfigDir.c_str()); + cfg.readFile(std::string(Version::ConfigDir + Version::ConfName).c_str()); } catch (const libconfig::FileIOException &fioex) { std::cout << "I/O error while reading config file." << std::endl << "creating new config file!" << std::endl; + // check if config folder exists + struct stat info{}; + + if (stat(Version::ConfigDir.c_str(), &info) != 0) { + Logger::warning("The config folder doesn't exist. Trying to create it."); + +#ifdef __unix + int check = mkdir(Version::ConfigDir.c_str(), 777); +#else + int check = mkdir(Version::ConfigDir.c_str()); +#endif + + // check if directory is created or not + if (!check) + Logger::message("config directory successfully created. "); + else + Logger::error("unable to create config directory."); + + } else if (info.st_mode & S_IFDIR) { + Logger::debug("config directory exists already"); + } else { + Logger::error("A file exists with the same name as the config dir should be"); + } + + std::ofstream myfile; - myfile.open(Version::ConfigDir); + myfile.open(Version::ConfigDir + Version::ConfName); if (myfile.is_open()) { myfile << Version::SAMPLECONFIG; myfile.close(); From ea648d5946b3fec8c80b2fb9d6840e11e17b83dd Mon Sep 17 00:00:00 2001 From: lukas Date: Thu, 21 May 2020 11:38:39 +0200 Subject: [PATCH 11/17] added usage info to readme --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 06e556a..36a5506 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,22 @@ Furthermore, you can optionally specify a Telegram API key and a Chat ID if you To enable and start the service: `systemctl enable iprefresher.service` and `systemctl start iprefresher.service` +## Usage +First of all configure the right keys in the `/etc/iprefresher.cfg` or `%appdata%\DynuIpRefresher\iprefresher.cfg` config file. + +Afterwards use the following options: + +``` +help page: +[-h] [--help] print this help page +[-v] [--version] print the software version +[-f] [--force] force refresh of ip +[-l] [--loop] infinite loop to refresh ip every five minutes +[-c] [--checkconfig] validate configuration +[-ip] [--currentip] get current global ip +[no argument] normal ip check and refresh +``` + ## Build ## Basic Build From 12ff16dc8eba726ca016d79ef7262b7fda95c53c Mon Sep 17 00:00:00 2001 From: lukas Date: Thu, 21 May 2020 11:52:55 +0200 Subject: [PATCH 12/17] renamed service-binarayname and config file name to dynuiprefresher --- CMakeLists.txt | 30 +++++++++---------- .../{iprefresher.cfg => dynuiprefresher.cfg} | 0 ...resher.service => dynuiprefresher.service} | 0 3 files changed, 14 insertions(+), 16 deletions(-) rename config/{iprefresher.cfg => dynuiprefresher.cfg} (100%) rename service/{iprefresher.service => dynuiprefresher.service} (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 88d723f..e25a86f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ # for test build gtest needs to be installed. cmake_minimum_required(VERSION 3.13) -project(iprefresher DESCRIPTION "Dynu ip refresher" LANGUAGES) +project(DynuIpRefresher DESCRIPTION "Dynu ip refresher" LANGUAGES) SET(PROJECT_VERSION 1.3.3) # CONFIGURATION @@ -24,6 +24,7 @@ option(WinBuild "cross compile for Windows Platform" OFF) # helper variables SET(CMAKE_CXX_STANDARD 17) string(TIMESTAMP TIMESTAMP_NOW "%d.%m.%Y") +SET(Application_Name "dynuiprefresher") SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) @@ -142,7 +143,7 @@ endif () message("") #read sample config -FILE(READ ${CMAKE_SOURCE_DIR}/config/iprefresher.cfg SAMPLECONFIG) +FILE(READ ${CMAKE_SOURCE_DIR}/config/dynuiprefresher.cfg SAMPLECONFIG) #add version header FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h "/** @@ -160,7 +161,7 @@ FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h namespace Version { const std::string VERSION = \"${PROJECT_VERSION}\"; const std::string ConfigDir = ${CONFIG_PATH}; - const std::string ConfName = \"iprefresher.cfg\"; + const std::string ConfName = \"${Application_Name}.cfg\"; const std::string SAMPLECONFIG = R\"(${SAMPLECONFIG})\"; }" ) @@ -171,7 +172,7 @@ add_library(api ${LIB_METHOD} src/api/DynuAPI.cpp src/api/IPAPI.cpp) -add_library(dynuiprefresher ${LIB_METHOD} +add_library(libdynuiprefresher ${LIB_METHOD} src/IPRefresher.cpp src/Config.cpp src/IpHelper.cpp @@ -179,28 +180,25 @@ add_library(dynuiprefresher ${LIB_METHOD} src/Logger.cpp ) -add_executable(iprefresher src/main.cpp) +add_executable(${Application_Name} src/main.cpp) # LINK generated LIBS # -target_link_libraries(iprefresher dynuiprefresher api ${CURL_LIBRARIES} ${LIBCONFIG++_LIBRARIES}) +target_link_libraries(${Application_Name} libdynuiprefresher api ${CURL_LIBRARIES} ${LIBCONFIG++_LIBRARIES}) # setting install targets IF (NOT ${WinBuild}) # INSTALL to Linux SYSTEM # # install binaries - install(TARGETS iprefresher DESTINATION usr/bin) + install(TARGETS ${Application_Name} DESTINATION usr/bin) # install systemd service and enable it - install(FILES service/iprefresher.service DESTINATION lib/systemd/system) + install(FILES service/${Application_Name}.service DESTINATION lib/systemd/system) ELSE () # INSTALL to Windows SYSTEM # # install binary to current folder - set_target_properties(iprefresher PROPERTIES SUFFIX ".exe") - install(TARGETS iprefresher DESTINATION .) - - # create config directory - install(DIRECTORY DESTINATION "../Common\ Files/iprefresher") + set_target_properties(${Application_Name} PROPERTIES SUFFIX ".exe") + install(TARGETS ${Application_Name} DESTINATION .) # install .dll dependencies # todo check if files exist... @@ -242,8 +240,8 @@ if [ ! -f ${CONFIG_PATH} ]; then ${SAMPLECONFIG}EOM fi -systemctl enable iprefresher.service -systemctl start iprefresher.service") +systemctl enable ${Application_Name}.service +systemctl start ${Application_Name}.service") SET(CPACK_DEB_COMPONENT_INSTALL 1) @@ -344,7 +342,7 @@ if (TESTS) # create an exectuable in which the tests will be stored add_executable(${TESTNAME} ${ARGN}) # link the Google test infrastructure, mocking library, and a default main fuction to - target_link_libraries(${TESTNAME} gtest gtest_main -lpthread -lm dynuiprefresher api ${CURL_LIBRARIES} ${LIBCONFIG++_LIBRARIES}) + target_link_libraries(${TESTNAME} gtest gtest_main -lpthread -lm libdynuiprefresher api ${CURL_LIBRARIES} ${LIBCONFIG++_LIBRARIES}) # see https://cmake.org/cmake/help/v3.10/module/GoogleTest.html for more options to pass to it gtest_discover_tests(${TESTNAME}) diff --git a/config/iprefresher.cfg b/config/dynuiprefresher.cfg similarity index 100% rename from config/iprefresher.cfg rename to config/dynuiprefresher.cfg diff --git a/service/iprefresher.service b/service/dynuiprefresher.service similarity index 100% rename from service/iprefresher.service rename to service/dynuiprefresher.service From 86fd9959237dd013e0396d84c64e60e0cbe3d039 Mon Sep 17 00:00:00 2001 From: lukas Date: Thu, 21 May 2020 14:09:17 +0200 Subject: [PATCH 13/17] some minor renamings edited README.md --- CMakeLists.txt | 6 +++--- README.md | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e25a86f..b4d9d14 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ # for test build gtest needs to be installed. cmake_minimum_required(VERSION 3.13) -project(DynuIpRefresher DESCRIPTION "Dynu ip refresher" LANGUAGES) +project(dynuiprefresher DESCRIPTION "Dynu ip refresher" LANGUAGES) SET(PROJECT_VERSION 1.3.3) # CONFIGURATION @@ -293,8 +293,8 @@ systemctl start ${Application_Name}.service") add_custom_target(build-packages "${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --target package - DEPENDS ${PROJECT_NAME} - COMMENT "Packing ${PROJECT_NAME}") + DEPENDS ${Application_Name} + COMMENT "Packing ${Application_Name}") message("") ENDIF () diff --git a/README.md b/README.md index 36a5506..bf7f869 100644 --- a/README.md +++ b/README.md @@ -9,14 +9,14 @@ I'm providing executables for Debian/Ubuntu (.deb) and RHEL/Debian (.rpm). But you can still compile the code my your own (see build section). ## Configuration -There is a configuration file `/etc/iprefresher.cfg` where you have to specify the DYNU API key (get it from their homepage), the domainid and your domain. +There is a configuration file `/etc/dynuiprefresher.cfg` where you have to specify the DYNU API key (get it from their homepage), the domainid and your domain. Furthermore, you can optionally specify a Telegram API key and a Chat ID if you want to be notfied when your local ip changes. To enable and start the service: -`systemctl enable iprefresher.service` and `systemctl start iprefresher.service` +`systemctl enable dynuiprefresher.service` and `systemctl start dynuiprefresher.service` ## Usage -First of all configure the right keys in the `/etc/iprefresher.cfg` or `%appdata%\DynuIpRefresher\iprefresher.cfg` config file. +First of all configure the right keys in the `/etc/dynuiprefresher.cfg` or `%appdata%\DynuIpRefresher\dynuiprefresher.cfg` config file. Afterwards use the following options: From ce4a3a830e4625acccbd179c807b49bc742c1b6f Mon Sep 17 00:00:00 2001 From: lukas Date: Thu, 21 May 2020 14:18:29 +0200 Subject: [PATCH 14/17] release version v1.3.4 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b4d9d14..fcf1bd0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ cmake_minimum_required(VERSION 3.13) project(dynuiprefresher DESCRIPTION "Dynu ip refresher" LANGUAGES) -SET(PROJECT_VERSION 1.3.3) +SET(PROJECT_VERSION 1.3.4) # CONFIGURATION SET(CMAKE_BUILD_TYPE Release) # manually SET build type (Release / Debug) From f4069d02bf6aac12ac9b000a6431db65afddb5cc Mon Sep 17 00:00:00 2001 From: lukas Date: Thu, 21 May 2020 14:27:16 +0200 Subject: [PATCH 15/17] right naming in gitlab ci --- .gitlab-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b2e2c5b..32ef521 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -20,7 +20,7 @@ build: image: luki42/dynuiprefresher_build:latest script: - cd build - - make iprefresher + - make dynuiprefresher artifacts: paths: - build/ @@ -72,7 +72,7 @@ build_win64: image: luki42/dynuiprefresher_build:windows script: - cd build - - make iprefresher + - make dynuiprefresher artifacts: paths: - build/ From 3f75484f770c3661f21683deb49cfe99bac4af12 Mon Sep 17 00:00:00 2001 From: lukas Date: Fri, 22 May 2020 21:58:51 +0200 Subject: [PATCH 16/17] improved inc directory inclusion --- CMakeLists.txt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fcf1bd0..6502211 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,7 +99,7 @@ if (${WinBuild}) message(STATUS "Using CURL lib(s): ${CURL_LIBRARIES}") message(STATUS "") - include_directories(${CURL_INCLUDE_DIRS} inc) + include_directories(${CURL_INCLUDE_DIRS}) # configure libconfig FIND_PATH(LIBCONFIG++_INCLUDE_DIRS libconfig.h++ /usr/${TOOLCHAIN_PREFIX}/sys-root/mingw/include/) # search for libconfig include headers @@ -108,7 +108,7 @@ if (${WinBuild}) message(STATUS "Using LIBCONFIG++ include dir(s): ${LIBCONFIG++_INCLUDE_DIRS}") message(STATUS "Using LIBCONFIG++ lib(s): ${LIBCONFIG++_LIBRARIES}") - include_directories(${LIBCONFIG++_INCLUDE_DIRS} inc) + include_directories(${LIBCONFIG++_INCLUDE_DIRS}) else () find_package(CURL REQUIRED) if (CURL_INCLUDE_DIRS AND CURL_LIBRARIES) @@ -118,7 +118,7 @@ else () else () message(FATAL_ERROR "Could not find CURL") endif () - include_directories(${CURL_INCLUDE_DIR} inc) + include_directories(${CURL_INCLUDE_DIR}) message("") # libconfig @@ -140,6 +140,8 @@ else () include_directories(${LIBCONFIG_INCLUDE_DIRS}) endif () +include_directories(inc) + message("") #read sample config From 81de98bf6ce2cc7b6ab83cfb5d73c8fd8bfa2302 Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 26 May 2020 16:51:29 +0200 Subject: [PATCH 17/17] rename Version.h to StaticData.h new temp file path for windows and linux temp file now writeable on windows compare string find method with SIZE_MAX instead of ULONG_MAX --- CMakeLists.txt | 17 +++++++++++------ src/Config.cpp | 17 +++++++++-------- src/FileLogger.cpp | 5 +++-- src/IPRefresher.cpp | 4 ++-- src/IpHelper.cpp | 4 +--- src/api/TelegramAPI.cpp | 6 ++---- src/main.cpp | 4 ++-- tests/UnitTest.cpp | 3 +-- 8 files changed, 31 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6502211..8ba8683 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,15 +66,19 @@ if (${WinBuild}) # or set(CMAKE_CXX_STANDARD_LIBRARIES -lcurl -lpthread -static-libgcc -static-libstdc++ -lcrypto -lssl -lws2_32 -static -DCURL_STATICLIB) # or add_definitions(-DCURL_STATICLIB) - # windows config path is same as executable + # windows config path is in %appdata% folder of user set(CONFIG_PATH "std::string(std::getenv(\"USERPROFILE\")) + \"\\\\AppData\\\\Roaming\\\\DynuIPrefresher\\\\\"") + # temp file is also stored in appdata folder + set(TempFilePath "std::string(std::getenv(\"USERPROFILE\")) + \"\\\\AppData\\\\Roaming\\\\DynuIPrefresher\\\\\"") else () message(STATUS "using nativ gcc toolchain.") set(LIBSUFFIX .so) set(SUFFIX "") - # set /etc/ config path + # set linux config path set(CONFIG_PATH "\"/etc/\"") + # set path of temp file + set(TempFilePath "\"/var/tmp/\"") endif () # test compiler settings and enable languages here @@ -146,10 +150,10 @@ message("") #read sample config FILE(READ ${CMAKE_SOURCE_DIR}/config/dynuiprefresher.cfg SAMPLECONFIG) -#add version header -FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h +#add StaticData header +FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/StaticData.h "/** - * Version header to store Version, Config dir and a Sample config + * StaticData header to store Version, Config dir and a Sample config * Do not edit this file manually, it is generated by the cmake script! * * @author Lukas Heiligenbrunner @@ -160,8 +164,9 @@ FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h #include -namespace Version { +namespace StaticData { const std::string VERSION = \"${PROJECT_VERSION}\"; + const std::string TempFilePath = ${TempFilePath}; const std::string ConfigDir = ${CONFIG_PATH}; const std::string ConfName = \"${Application_Name}.cfg\"; const std::string SAMPLECONFIG = R\"(${SAMPLECONFIG})\"; diff --git a/src/Config.cpp b/src/Config.cpp index 217bdc7..26ad6ba 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -1,6 +1,6 @@ #include "Config.h" #include "Logger.h" -#include "Version.h" +#include "StaticData.h" #include #include @@ -20,7 +20,7 @@ bool Config::telegramSupport; bool Config::readConfig() { libconfig::Config cfg; try { - cfg.readFile(std::string(Version::ConfigDir + Version::ConfName).c_str()); + cfg.readFile(std::string(StaticData::ConfigDir + StaticData::ConfName).c_str()); } catch (const libconfig::FileIOException &fioex) { std::cout << "I/O error while reading config file." << std::endl << "creating new config file!" << std::endl; @@ -28,13 +28,14 @@ bool Config::readConfig() { // check if config folder exists struct stat info{}; - if (stat(Version::ConfigDir.c_str(), &info) != 0) { + if (stat(StaticData::ConfigDir.c_str(), &info) != 0) { Logger::warning("The config folder doesn't exist. Trying to create it."); +// mkdir command is different defined for windows #ifdef __unix - int check = mkdir(Version::ConfigDir.c_str(), 777); + int check = mkdir(StaticData::ConfigDir.c_str(), 777); #else - int check = mkdir(Version::ConfigDir.c_str()); + int check = mkdir(StaticData::ConfigDir.c_str()); #endif // check if directory is created or not @@ -51,9 +52,9 @@ bool Config::readConfig() { std::ofstream myfile; - myfile.open(Version::ConfigDir + Version::ConfName); + myfile.open(StaticData::ConfigDir + StaticData::ConfName); if (myfile.is_open()) { - myfile << Version::SAMPLECONFIG; + myfile << StaticData::SAMPLECONFIG; myfile.close(); } else { Logger::error("error creating file"); @@ -94,7 +95,7 @@ bool Config::validateConfig() { libconfig::Config cfg; try { Logger::message("reading config file"); - cfg.readFile(Version::ConfigDir.c_str()); + cfg.readFile(StaticData::ConfigDir.c_str()); } catch (const libconfig::FileIOException &fioex) { Logger::warning("config file doesn't exist or permission denied!"); diff --git a/src/FileLogger.cpp b/src/FileLogger.cpp index 78d670c..0e9caf0 100644 --- a/src/FileLogger.cpp +++ b/src/FileLogger.cpp @@ -1,12 +1,13 @@ #include "FileLogger.h" #include "IpHelper.h" +#include "StaticData.h" #include #include void FileLogger::safeip(std::string ip) { std::ofstream out; - out.open("ip.txt", std::ios::out); + out.open(StaticData::TempFilePath + "temp-dynuiprefresher.txt", std::ios::out); out << ip; @@ -15,7 +16,7 @@ void FileLogger::safeip(std::string ip) { std::string FileLogger::readip() { std::ifstream in; - in.open("ip.txt", std::ios::in); + in.open(StaticData::TempFilePath + "temp-dynuiprefresher.txt", std::ios::in); std::string ip; diff --git a/src/IPRefresher.cpp b/src/IPRefresher.cpp index 092b577..af66491 100644 --- a/src/IPRefresher.cpp +++ b/src/IPRefresher.cpp @@ -4,7 +4,7 @@ #include "api/DynuAPI.h" #include "api/TelegramAPI.h" #include "Config.h" -#include "Version.h" +#include "StaticData.h" #include "IpHelper.h" #include @@ -53,7 +53,7 @@ void IPRefresher::checkIPAdress(bool force) { IPRefresher::IPRefresher(bool loop) { if (loop) { Logger::message("startup of service"); - Logger::message("Version: " + Version::VERSION); + Logger::message("Version: " + StaticData::VERSION); while (true) { Logger::message("starting check"); diff --git a/src/IpHelper.cpp b/src/IpHelper.cpp index 67c5384..5be0bf0 100644 --- a/src/IpHelper.cpp +++ b/src/IpHelper.cpp @@ -1,7 +1,5 @@ #include "IpHelper.h" -#include - bool IpHelper::isIpValid(std::string ip) { - return (ip.find('.') != ULONG_MAX); + return (ip.find('.') != SIZE_MAX); } diff --git a/src/api/TelegramAPI.cpp b/src/api/TelegramAPI.cpp index c89a247..4cc3acf 100644 --- a/src/api/TelegramAPI.cpp +++ b/src/api/TelegramAPI.cpp @@ -1,8 +1,6 @@ #include "api/TelegramAPI.h" #include "Logger.h" -#include - int TelegramAPI::sendMessage(const std::string &text) { Hashmap args; args.add("chat_id", chatid); @@ -12,8 +10,8 @@ int TelegramAPI::sendMessage(const std::string &text) { std::string reply = request("https://api.telegram.org/bot" + apikey + "/sendmessage", false, args, headers); - if (reply.find("\"error_code\"") != ULONG_MAX) { - Logger::error("failed to refresh the ip (Dynu API)"); + if (reply.find("\"error_code\"") != SIZE_MAX) { + Logger::error("failed to send the Telegram Message"); return -1; } return 1; diff --git a/src/main.cpp b/src/main.cpp index 6518f92..5f830a6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,4 @@ -#include "Version.h" +#include "StaticData.h" #include "IPRefresher.h" #include "Logger.h" #include "Config.h" @@ -19,7 +19,7 @@ int main(int argc, char *argv[]) { << "[-ip] [--currentip] get current global ip" << std::endl << "[no argument] normal ip check and refresh" << std::endl; } else if (firstarg == "-v" || firstarg == "--version") { - std::cout << "Version " << Version::VERSION << std::endl; + std::cout << "Version " << StaticData::VERSION << std::endl; } else if (firstarg == "-f" || firstarg == "--force") { IPRefresher ipr; if (Config::readConfig()) { diff --git a/tests/UnitTest.cpp b/tests/UnitTest.cpp index e1d5bcc..2c97a9e 100644 --- a/tests/UnitTest.cpp +++ b/tests/UnitTest.cpp @@ -4,7 +4,6 @@ #include #include -#include #include "gtest/gtest.h" /** @@ -21,7 +20,7 @@ TEST(ReadIp, testzeroIpIfNotExists) { TEST(IPAPI, testIpAPIcheckIPSyntax) { IPAPI ipapi; std::string ip = ipapi.getGlobalIp(); - if (ip.find('.') == ULONG_MAX) { + if (ip.find('.') == SIZE_MAX) { // error when ip doesn't contain a . ASSERT_TRUE(false); } else {