use libconfig for a /etc/iprefresher.cfg config file
there can all the api credentials be specified
This commit is contained in:
		
							
								
								
									
										7
									
								
								.idea/inspectionProfiles/Project_Default.xml
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								.idea/inspectionProfiles/Project_Default.xml
									
									
									
										generated
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					<component name="InspectionProjectProfileManager">
 | 
				
			||||||
 | 
					  <profile version="1.0">
 | 
				
			||||||
 | 
					    <option name="myName" value="Project Default" />
 | 
				
			||||||
 | 
					    <inspection_tool class="DuplicateKeyInSection" enabled="true" level="WARNING" enabled_by_default="true" />
 | 
				
			||||||
 | 
					    <inspection_tool class="DuplicateSectionInFile" enabled="true" level="WARNING" enabled_by_default="true" />
 | 
				
			||||||
 | 
					  </profile>
 | 
				
			||||||
 | 
					</component>
 | 
				
			||||||
@@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 17)
 | 
				
			|||||||
set(CMAKE_BUILD_TYPE Release) # manually set build type (Release / Debug)
 | 
					set(CMAKE_BUILD_TYPE Release) # manually set build type (Release / Debug)
 | 
				
			||||||
set(LIB_METHOD STATIC) #SHARED / STATIC
 | 
					set(LIB_METHOD STATIC) #SHARED / STATIC
 | 
				
			||||||
set(WinBuild false)
 | 
					set(WinBuild false)
 | 
				
			||||||
set(PROJECT_VERSION 1.2.2)
 | 
					set(PROJECT_VERSION 1.2.3)
 | 
				
			||||||
option(BUILD_DOC "Build documentation" ON)
 | 
					option(BUILD_DOC "Build documentation" ON)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
 | 
					set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
 | 
				
			||||||
@@ -59,9 +59,32 @@ if (CURL_FOUND)
 | 
				
			|||||||
else ()
 | 
					else ()
 | 
				
			||||||
    message(FATAL_ERROR "Could not find CURL")
 | 
					    message(FATAL_ERROR "Could not find CURL")
 | 
				
			||||||
endif ()
 | 
					endif ()
 | 
				
			||||||
message("")
 | 
					 | 
				
			||||||
include_directories(${CURL_INCLUDE_DIR} inc)
 | 
					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
 | 
					#add version header
 | 
				
			||||||
FILE(WRITE ${CMAKE_SOURCE_DIR}/inc/Version.h
 | 
					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\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})
 | 
					add_executable(iprefresher ${SOURCE})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# LINK generated LIBS #
 | 
					# LINK generated LIBS #
 | 
				
			||||||
target_link_libraries(iprefresher api logger ${CURL_LIBRARIES})
 | 
					target_link_libraries(iprefresher api logger ${CURL_LIBRARIES} config++)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# INSTALL to SYSTEM #
 | 
					# INSTALL to SYSTEM #
 | 
				
			||||||
set(CMAKE_INSTALL_PREFIX "/")
 | 
					set(CMAKE_INSTALL_PREFIX "/")
 | 
				
			||||||
install(TARGETS iprefresher DESTINATION usr/local/bin)
 | 
					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)
 | 
					IF (UNIX)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										12
									
								
								config/iprefresher.cfg
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								config/iprefresher.cfg
									
									
									
									
									
										Normal file
									
								
							@@ -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 =""
 | 
				
			||||||
@@ -16,7 +16,7 @@ public:
 | 
				
			|||||||
    static std::string telegramApiKey;
 | 
					    static std::string telegramApiKey;
 | 
				
			||||||
    static std::string chatId;
 | 
					    static std::string chatId;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    static bool checkCredentialValidity();
 | 
					    static bool readCredentials();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
@@ -3,14 +3,50 @@
 | 
				
			|||||||
//
 | 
					//
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <Credentials.h>
 | 
					#include <Credentials.h>
 | 
				
			||||||
 | 
					#include <iostream>
 | 
				
			||||||
 | 
					#include <cstring>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
std::string Credentials::dynuapikey = "";
 | 
					#include "libconfig.h++"
 | 
				
			||||||
std::string Credentials::domainid = ""; //id of the dynu domain
 | 
					 | 
				
			||||||
std::string Credentials::domainname = "";
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
std::string Credentials::telegramApiKey = "";
 | 
					std::string Credentials::dynuapikey;
 | 
				
			||||||
std::string Credentials::chatId = "";
 | 
					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());
 | 
					    return !(Credentials::dynuapikey.empty() || Credentials::domainid.empty() || Credentials::domainname.empty());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,7 +5,7 @@
 | 
				
			|||||||
#include <Credentials.h>
 | 
					#include <Credentials.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int main(int argc, char *argv[]) {
 | 
					int main(int argc, char *argv[]) {
 | 
				
			||||||
    if (!Credentials::checkCredentialValidity()) {
 | 
					    if (!Credentials::readCredentials()) {
 | 
				
			||||||
        std::cout << "incorrect credentials!" << std::endl;
 | 
					        std::cout << "incorrect credentials!" << std::endl;
 | 
				
			||||||
        return -1;
 | 
					        return -1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user