* new class for IP utils
* method to check telegram support out of config * getters in Config.cpp * lots of reformatings and cdoc
This commit is contained in:
		@@ -18,10 +18,11 @@ std::string Config::domainname;
 | 
			
		||||
std::string Config::telegramApiKey;
 | 
			
		||||
std::string Config::chatId;
 | 
			
		||||
 | 
			
		||||
bool Config::readCredentials() {
 | 
			
		||||
bool Config::telegramSupport;
 | 
			
		||||
 | 
			
		||||
bool Config::readConfig() {
 | 
			
		||||
    libconfig::Config cfg;
 | 
			
		||||
    try {
 | 
			
		||||
        // todo make dynamic here
 | 
			
		||||
        cfg.readFile(Version::ConfigDir.c_str());
 | 
			
		||||
    }
 | 
			
		||||
    catch (const libconfig::FileIOException &fioex) {
 | 
			
		||||
@@ -33,7 +34,7 @@ bool Config::readCredentials() {
 | 
			
		||||
            myfile << Version::SAMPLECONFIG;
 | 
			
		||||
            myfile.close();
 | 
			
		||||
        } else {
 | 
			
		||||
            std::cout << "error creating file" << std::endl;
 | 
			
		||||
            Logger::error("error creating file");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return false;
 | 
			
		||||
@@ -52,11 +53,15 @@ bool Config::readCredentials() {
 | 
			
		||||
        // optional parameters
 | 
			
		||||
        telegramApiKey = (std::string) cfg.lookup("telegramApiKey");
 | 
			
		||||
        chatId = (std::string) cfg.lookup("chatId");
 | 
			
		||||
        telegramSupport = true;
 | 
			
		||||
    }
 | 
			
		||||
    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;
 | 
			
		||||
        } else {
 | 
			
		||||
            Logger::message("no Telegram support - fields in config not set");
 | 
			
		||||
            telegramSupport = false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // check if needed values aren't empty
 | 
			
		||||
@@ -83,27 +88,57 @@ bool Config::validateConfig() {
 | 
			
		||||
    try {
 | 
			
		||||
        // needed parameters
 | 
			
		||||
        if (((std::string) cfg.lookup("dynuapikey")).empty()) {
 | 
			
		||||
            Logger::warning("required parameter dynuapikey seems to be 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.");
 | 
			
		||||
            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.");
 | 
			
		||||
            Logger::warning("required parameter \"domainname\" seems to be empty.");
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        // optional parameters
 | 
			
		||||
        cfg.lookup("telegramApiKey");
 | 
			
		||||
        cfg.lookup("chatId");
 | 
			
		||||
        telegramSupport = true;
 | 
			
		||||
    }
 | 
			
		||||
    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;
 | 
			
		||||
        } else {
 | 
			
		||||
            Logger::message("no Telegram support - fields in config not set");
 | 
			
		||||
            telegramSupport = false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool Config::isTelegramSupported() {
 | 
			
		||||
    return telegramSupport;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const std::string &Config::getDynuapikey() {
 | 
			
		||||
    return dynuapikey;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const std::string &Config::getDomainid() {
 | 
			
		||||
    return domainid;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const std::string &Config::getDomainname() {
 | 
			
		||||
    return domainname;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const std::string &Config::getTelegramApiKey() {
 | 
			
		||||
    return telegramApiKey;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const std::string &Config::getChatId() {
 | 
			
		||||
    return chatId;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Config::Config() = default;
 | 
			
		||||
 
 | 
			
		||||
@@ -3,10 +3,10 @@
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#include "FileLogger.h"
 | 
			
		||||
#include "IpHelper.h"
 | 
			
		||||
 | 
			
		||||
#include <fstream>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <climits>
 | 
			
		||||
 | 
			
		||||
void FileLogger::safeip(std::string ip) {
 | 
			
		||||
    std::ofstream out;
 | 
			
		||||
@@ -25,8 +25,8 @@ std::string FileLogger::readip() {
 | 
			
		||||
 | 
			
		||||
    in >> ip;
 | 
			
		||||
 | 
			
		||||
    // when received ip has no : return 0.0.0.0
 | 
			
		||||
    if (ip.find(':') == ULONG_MAX)
 | 
			
		||||
    // when received ip has no . return 0.0.0.0
 | 
			
		||||
    if (!IpHelper::isIpValid(ip))
 | 
			
		||||
        return "0.0.0.0";
 | 
			
		||||
    else
 | 
			
		||||
        return ip;
 | 
			
		||||
 
 | 
			
		||||
@@ -9,12 +9,11 @@
 | 
			
		||||
#include "api/TelegramAPI.h"
 | 
			
		||||
#include "Config.h"
 | 
			
		||||
#include "Version.h"
 | 
			
		||||
#include "IpHelper.h"
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <chrono>
 | 
			
		||||
#include <thread>
 | 
			
		||||
#include <Logger.h>
 | 
			
		||||
#include <climits>
 | 
			
		||||
 | 
			
		||||
void IPRefresher::checkIPAdress(bool force) {
 | 
			
		||||
    FileLogger logger;
 | 
			
		||||
@@ -25,7 +24,7 @@ void IPRefresher::checkIPAdress(bool force) {
 | 
			
		||||
    if (ip.empty()) {
 | 
			
		||||
        //no internet connection (or other error)
 | 
			
		||||
        Logger::warning("no internet connection");
 | 
			
		||||
    } else if (ip.find(':') == ULONG_MAX) {
 | 
			
		||||
    } else if (!IpHelper::isIpValid(ip)) {
 | 
			
		||||
        // error when ip doesn't contain a :
 | 
			
		||||
        Logger::warning("an error occured when getting the global ip");
 | 
			
		||||
    } else {
 | 
			
		||||
@@ -37,13 +36,15 @@ void IPRefresher::checkIPAdress(bool force) {
 | 
			
		||||
            Logger::message("ip changed! -- from :" + oldip + "to: " + ip);
 | 
			
		||||
 | 
			
		||||
            DynuAPI dynu;
 | 
			
		||||
            dynu.init(Config::dynuapikey, Config::domainid, Config::domainname);
 | 
			
		||||
            dynu.init(Config::getDynuapikey(), Config::getDomainid(), Config::getDomainname());
 | 
			
		||||
            // actual refresh of IP in api - here
 | 
			
		||||
            bool result = dynu.refreshIp(ip);
 | 
			
		||||
 | 
			
		||||
            if (dynu.refreshIp(ip)) {
 | 
			
		||||
            if (result && Config::isTelegramSupported()) {
 | 
			
		||||
                TelegramAPI tele;
 | 
			
		||||
                tele.init(Config::telegramApiKey, Config::chatId);
 | 
			
		||||
                tele.init(Config::getTelegramApiKey(), Config::getChatId());
 | 
			
		||||
                tele.sendMessage(oldip + " moved to " + ip);
 | 
			
		||||
            } else {
 | 
			
		||||
            } else if (!result) {
 | 
			
		||||
                //error
 | 
			
		||||
                Logger::error("failed to write ip to dynu api!");
 | 
			
		||||
            }
 | 
			
		||||
@@ -53,13 +54,11 @@ void IPRefresher::checkIPAdress(bool force) {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
IPRefresher::IPRefresher() = default;
 | 
			
		||||
 | 
			
		||||
IPRefresher::IPRefresher(bool loop) {
 | 
			
		||||
    if (loop) {
 | 
			
		||||
        Logger::message("startup of service");
 | 
			
		||||
        Logger::message("Version: " + Version::VERSION);
 | 
			
		||||
        if (Config::readCredentials()) {
 | 
			
		||||
        if (Config::readConfig()) {
 | 
			
		||||
            while (true) {
 | 
			
		||||
                Logger::message("starting check");
 | 
			
		||||
                checkIPAdress(false);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										11
									
								
								src/IpHelper.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/IpHelper.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by lukas on 07.05.20.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#include "IpHelper.h"
 | 
			
		||||
 | 
			
		||||
#include <climits>
 | 
			
		||||
 | 
			
		||||
bool IpHelper::isIpValid(std::string ip) {
 | 
			
		||||
    return (ip.find('.') != ULONG_MAX);
 | 
			
		||||
}
 | 
			
		||||
@@ -4,7 +4,7 @@
 | 
			
		||||
 | 
			
		||||
#include "api/DynuAPI.h"
 | 
			
		||||
 | 
			
		||||
int DynuAPI::refreshIp(std:: string ip) {
 | 
			
		||||
bool DynuAPI::refreshIp(std::string ip) {
 | 
			
		||||
    Hashmap<std::string, std::string> args;
 | 
			
		||||
    args.add("name", domainname);
 | 
			
		||||
    args.add("ipv4Address", ip);
 | 
			
		||||
@@ -16,15 +16,11 @@ int DynuAPI::refreshIp(std:: string ip) {
 | 
			
		||||
 | 
			
		||||
    const std::string dynurepl = request("https://api.dynu.com/v2/dns/" + domainid, true, args, headers);
 | 
			
		||||
 | 
			
		||||
    if (dynurepl != "{\"statusCode\":200}") {
 | 
			
		||||
        return -1;
 | 
			
		||||
    } else {
 | 
			
		||||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
    return (dynurepl == "{\"statusCode\":200}");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void DynuAPI::init(const std::string& dynuApiKey, const std::string& domainId, const std::string& domainName) {
 | 
			
		||||
    this->dynuapikey=dynuApiKey;
 | 
			
		||||
    this->domainid=domainId;
 | 
			
		||||
    this->domainname=domainName;
 | 
			
		||||
void DynuAPI::init(const std::string &dynuApiKey, const std::string &domainId, const std::string &domainName) {
 | 
			
		||||
    this->dynuapikey = dynuApiKey;
 | 
			
		||||
    this->domainid = domainId;
 | 
			
		||||
    this->domainname = domainName;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -6,4 +6,4 @@
 | 
			
		||||
 | 
			
		||||
std::string IPAPI::getGlobalIp() {
 | 
			
		||||
    return request("https://api.ipify.org");
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
@@ -7,7 +7,7 @@
 | 
			
		||||
 | 
			
		||||
#include <climits>
 | 
			
		||||
 | 
			
		||||
int TelegramAPI::sendMessage(const std::string& text) {
 | 
			
		||||
int TelegramAPI::sendMessage(const std::string &text) {
 | 
			
		||||
    Hashmap<std::string, std::string> args;
 | 
			
		||||
    args.add("chat_id", chatid);
 | 
			
		||||
    args.add("text", text);
 | 
			
		||||
 
 | 
			
		||||
@@ -8,8 +8,6 @@
 | 
			
		||||
#include "Config.h"
 | 
			
		||||
#include "api/IPAPI.h"
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * application entry point
 | 
			
		||||
 */
 | 
			
		||||
@@ -28,7 +26,7 @@ int main(int argc, char *argv[]) {
 | 
			
		||||
            std::cout << "Version " << Version::VERSION << std::endl;
 | 
			
		||||
        } else if (firstarg == "-f" || firstarg == "--force") {
 | 
			
		||||
            IPRefresher ipr;
 | 
			
		||||
            if (Config::readCredentials()) {
 | 
			
		||||
            if (Config::readConfig()) {
 | 
			
		||||
                ipr.checkIPAdress(true);
 | 
			
		||||
            } else {
 | 
			
		||||
                std::cout << "incorrect credentials!" << std::endl;
 | 
			
		||||
@@ -40,7 +38,7 @@ int main(int argc, char *argv[]) {
 | 
			
		||||
            if (Config::validateConfig()) {
 | 
			
		||||
                Logger::message("Config file is OK");
 | 
			
		||||
            } else {
 | 
			
		||||
                Logger::warning("There are errors in config file!");
 | 
			
		||||
                Logger::error("There are errors in config file!");
 | 
			
		||||
                return -1;
 | 
			
		||||
            }
 | 
			
		||||
        } else if (firstarg == "-ip" || firstarg == "--currentip") {
 | 
			
		||||
@@ -52,7 +50,7 @@ int main(int argc, char *argv[]) {
 | 
			
		||||
    } else {
 | 
			
		||||
        IPRefresher ipr;
 | 
			
		||||
        Logger::message("starting check");
 | 
			
		||||
        if (Config::readCredentials()) {
 | 
			
		||||
        if (Config::readConfig()) {
 | 
			
		||||
            ipr.checkIPAdress(false);
 | 
			
		||||
        } else {
 | 
			
		||||
            std::cout << "incorrect credentials!" << std::endl;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user