9 Commits

9 changed files with 165 additions and 35 deletions

View File

@ -1,16 +1,25 @@
# @author Lukas Heiligenbrunner
# main CMake file
#
# Build lib dependencies:
## libcurl (with sources)
## libconfig (with sources)
#
# documenation build needs doxygen to be installed.
cmake_minimum_required(VERSION 3.13)
project(iprefresher DESCRIPTION "Dynu ip refresher")
set(CMAKE_CXX_STANDARD 17)
SET(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Release) # manually set build type (Release / Debug)
set(LIB_METHOD STATIC) #SHARED / STATIC
set(PROJECT_VERSION 1.3.1)
SET(CMAKE_BUILD_TYPE Release) # manually SET build type (Release / Debug)
SET(LIB_METHOD STATIC) #SHARED / STATIC
SET(PROJECT_VERSION 1.3.2)
option(BUILD_DOC "Build documentation" ON)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# config libs
message(STATUS "Config of Libraries")
@ -45,28 +54,39 @@ include_directories(${LIBCONFIG_INCLUDE_DIR})
message("")
#read sample config
FILE(READ ${CMAKE_SOURCE_DIR}/config/iprefresher.cfg SAMPLECONFIG)
#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}\";"
"\#pragma once
#include <string>
namespace Version {
const std::string VERSION = \"${PROJECT_VERSION}\";
const std::string SAMPLECONFIG = R\"(${SAMPLECONFIG})\";
}"
)
# generate post script for checking if configuration already exists
FILE(WRITE ${CMAKE_SOURCE_DIR}/postinst
"#!/bin/bash
if [ ! -f /etc/iprefresher.cfg ]; then
cat > /etc/iprefresher.cfg <<- EOM
${SAMPLECONFIG}EOM
fi\n"
)
add_library(api ${LIB_METHOD}
src/api/API.cpp
src/api/TelegramAPI.cpp
src/api/DynuAPI.cpp
src/api/IPAPI.cpp
)
src/api/IPAPI.cpp)
add_library(logger ${LIB_METHOD}
src/FileLogger.cpp
src/Logger.cpp
)
src/Logger.cpp)
set(SOURCE
SET(SOURCE
src/main.cpp
src/IPRefresher.cpp
src/Config.cpp)
@ -77,17 +97,18 @@ add_executable(iprefresher ${SOURCE})
target_link_libraries(iprefresher api logger ${CURL_LIBRARIES} config++)
# INSTALL to SYSTEM #
set(CMAKE_INSTALL_PREFIX "/")
SET(CMAKE_INSTALL_PREFIX "/")
# install binaries
install(TARGETS iprefresher DESTINATION usr/bin)
# install systemd service and enable it
install(FILES service/iprefresher.service DESTINATION lib/systemd/system)
install(FILES config/iprefresher.cfg DESTINATION etc)
IF (UNIX)
message(STATUS "config of Package build")
SET(CPACK_DEB_COMPONENT_INSTALL 1)
SET(CPACK_OUTPUT_FILE_PREFIX packages)
set(CPACK_PACKAGING_INSTALL_PREFIX "/") # no prefix for package structure
SET(CPACK_PACKAGING_INSTALL_PREFIX "/") # no prefix for package structure
FIND_PROGRAM(RPMBUILD_EXECUTABLE rpmbuild)
FIND_PROGRAM(DEB_EXECUTABLE dpkg)
@ -97,8 +118,10 @@ IF (UNIX)
#check if rpm build is possible
if (NOT ${RPMBUILD_EXECUTABLE} STREQUAL "RPMBUILD_EXECUTABLE-NOTFOUND")
message(STATUS "found rpm build executeable --> able to build rpm")
set(CPACK_GENERATOR "${CPACK_GENERATOR};RPM")
set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION "/lib/systemd/system" "/lib/systemd" "/lib" "/usr/local/bin" "/usr/local") # --> needed to not override existing folders
SET(CPACK_GENERATOR "${CPACK_GENERATOR};RPM")
SET(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/postinst")
SET(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION "/lib/systemd/system" "/lib/systemd" "/lib" "/usr/local/bin" "/usr/local") # --> needed to not override existing folders
set(CPACK_RPM_PACKAGE_REQUIRES "libcurl,libconfig")
else (NOT ${RPMBUILD_EXECUTABLE} STREQUAL "RPMBUILD_EXECUTABLE-NOTFOUND")
message(STATUS "not found rpm build tools --> not building rpm")
endif (NOT ${RPMBUILD_EXECUTABLE} STREQUAL "RPMBUILD_EXECUTABLE-NOTFOUND")
@ -106,7 +129,9 @@ IF (UNIX)
#check if deb build is possible
if (NOT ${DEB_EXECUTABLE} STREQUAL "DEB_EXECUTABLE-NOTFOUND")
message(STATUS "found deb build tools --> able to build deb")
set(CPACK_GENERATOR "${CPACK_GENERATOR};DEB")
SET(CPACK_GENERATOR "${CPACK_GENERATOR};DEB") # add deb generator
set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${CMAKE_CURRENT_SOURCE_DIR}/postinst") # set post inst file
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libcurl4,libconfig++9v5") # add debian dependencies
else (NOT ${DEB_EXECUTABLE} STREQUAL "DEB_EXECUTABLE-NOTFOUND")
message(STATUS "not found deb build tools --> not building deb")
endif (NOT ${DEB_EXECUTABLE} STREQUAL "DEB_EXECUTABLE-NOTFOUND")
@ -122,7 +147,6 @@ IF (UNIX)
SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${PROJECT_VERSION}")
SET(CPACK_PACKAGE_CONTACT "Lukas Heiligenbrunner <lukas.heiligenbrunner@gmail.com>")
SET(CPACK_PACKAGE_SECTION "games")
set(CPACK_PACKAGE_DEPENDS "libcurl (>= 7.0.0-1), libconfig (>= 1.5.0)")
INCLUDE(CPack)
@ -138,9 +162,9 @@ if (BUILD_DOC)
message(STATUS "config of DOxygen build")
find_package(Doxygen)
if (DOXYGEN_FOUND)
# set input and output files
set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in)
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
# SET input and output files
SET(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in)
SET(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
# request to configure the file
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)

View File

@ -11,6 +11,9 @@ But you can still compile the code my your own (see build section).
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.
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`
## Build
## Basic Build

View File

@ -16,7 +16,19 @@ public:
static std::string telegramApiKey;
static std::string chatId;
/**
* read configuration out of config file
*
* @return success of config read
*/
static bool readCredentials();
/**
* validate config file
*
* @return validity of config file
*/
static bool validateConfig();
private:
};

View File

@ -8,13 +8,38 @@
class Logger {
public:
/**
* a debug message
* @param message message
*/
static void debug(std::string message);
/**
* a default message
* @param message message
*/
static void message(std::string message);
/**
* a warning message
* @param message message
*/
static void warning(std::string message);
/**
* a error message
* @param message message
*/
static void error(std::string message);
/**
* a log message with manual level set
* @param message message
* @param level loglevel (1-4) or predefined labels
*/
static void log(const std::string &message, int level);
static const int Debug = 4;
static const int Message = 3;
static const int Warning = 2;

View File

@ -3,10 +3,13 @@
//
#include <Config.h>
#include <Logger.h>
#include <iostream>
#include <cstring>
#include "libconfig.h++"
#include <fstream>
#include <libconfig.h++>
#include <Version.h>
std::string Config::dynuapikey;
std::string Config::domainid; //id of the dynu domain
@ -22,7 +25,16 @@ bool Config::readCredentials() {
}
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");
std::ofstream myfile;
myfile.open("/etc/iprefresher.cfg");
if (myfile.is_open()) {
myfile << Version::SAMPLECONFIG;
myfile.close();
} else {
std::cout << "error creating file" << std::endl;
}
return false;
}
catch (const libconfig::ParseException &pex) {
@ -31,7 +43,6 @@ bool Config::readCredentials() {
return false;
}
// Get the store name.
try {
// needed parameters
dynuapikey = (std::string) cfg.lookup("dynuapikey");
@ -50,3 +61,48 @@ bool Config::readCredentials() {
// check if needed values aren't empty
return !(Config::dynuapikey.empty() || Config::domainid.empty() || Config::domainname.empty());
}
bool Config::validateConfig() {
libconfig::Config cfg;
try {
Logger::message("reading config file");
cfg.readFile("/etc/iprefresher.cfg");
}
catch (const libconfig::FileIOException &fioex) {
Logger::warning("config file doesn't exist or permission denied!");
return false;
}
catch (const libconfig::ParseException &pex) {
std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
<< " - " << pex.getError() << std::endl;
return false;
}
Logger::message("Syntax and Permission is OK");
try {
// needed parameters
if (((std::string) cfg.lookup("dynuapikey")).empty()) {
Logger::warning("required parameter dynuapikey seems to be empty.");
return false;
}
if (((std::string) cfg.lookup("domainid")).empty()) {
Logger::warning("required parameter domainid seems to be empty.");
return false;
}
if (((std::string) cfg.lookup("domainname")).empty()) {
Logger::warning("required parameter domainname seems to be empty.");
return false;
}
// optional parameters
cfg.lookup("telegramApiKey");
cfg.lookup("chatId");
}
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 false;
}
}
return true;
}

View File

@ -15,6 +15,7 @@
#include <IPRefresher.h>
#include <Config.h>
#include <Version.h>
void IPRefresher::checkIPAdress(bool force) {
FileLogger logger;
@ -55,6 +56,7 @@ IPRefresher::IPRefresher() = default;
IPRefresher::IPRefresher(bool loop) {
if (loop) {
Logger::message("startup of service");
Logger::message("Version: " + Version::VERSION);
if (Config::readCredentials()) {
while (true) {
Logger::message("starting check");

View File

@ -39,6 +39,9 @@ void Logger::log(const std::string &message, int level) {
case Error:
out << "ERROR";
break;
default:
out << "UNDEFINED";
break;
}
out << "] ";
out << message;

View File

@ -3,10 +3,7 @@
//
#include "api/API.h"
#include "api/Hashmap.h"
#include <string>
#include <iostream>
#include <sstream>
#include <curl/curl.h>

View File

@ -2,12 +2,12 @@
// Created by lukas on 18.06.19.
//
#include <iostream>
#include <IPRefresher.h>
#include <Version.h>
#include <IPRefresher.h>
#include <Logger.h>
#include <Config.h>
#include <iostream>
/**
* application entry point
*/
@ -19,6 +19,7 @@ int main(int argc, char *argv[]) {
<< "[-v] [--version] print the software version" << std::endl
<< "[-f] [--force] force refresh of ip" << std::endl
<< "[-l] [--loop] infinite loop to refresh ip every five minutes" << std::endl
<< "[-c] [--checkconfig] validate configuration" << std::endl
<< "[no argument] normal ip check and refresh" << std::endl;
} else if (firstarg == "-v" || firstarg == "--version") {
std::cout << "Version " << Version::VERSION << std::endl;
@ -32,6 +33,13 @@ int main(int argc, char *argv[]) {
} else if (firstarg == "-l" || firstarg == "--loop") {
IPRefresher(true);
} else if (firstarg == "-c" || firstarg == "--checkconfig") {
if (Config::validateConfig()) {
Logger::message("Config file is OK");
} else {
Logger::warning("There are errors in config file!");
return -1;
}
} else {
Logger::message("wrong arguments! -h for help");
}