diff --git a/inc/api/API.h b/inc/api/API.h index 5c3e3c2..d2f4f75 100644 --- a/inc/api/API.h +++ b/inc/api/API.h @@ -4,9 +4,9 @@ #pragma once +#include "Hashmap.h" #include -#include "Hashmap.h" class API { public: diff --git a/inc/api/IPAPI.h b/inc/api/IPAPI.h index fbcfd48..ed67133 100644 --- a/inc/api/IPAPI.h +++ b/inc/api/IPAPI.h @@ -4,9 +4,10 @@ #pragma once -#include #include "API.h" +#include + class IPAPI : API{ public: /** diff --git a/inc/api/TelegramAPI.h b/inc/api/TelegramAPI.h index 5cdaaf0..676cfd5 100644 --- a/inc/api/TelegramAPI.h +++ b/inc/api/TelegramAPI.h @@ -4,9 +4,9 @@ #pragma once +#include "API.h" #include -#include "API.h" class TelegramAPI : API { public: diff --git a/src/Config.cpp b/src/Config.cpp index 8fdfd2f..50052ac 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -2,14 +2,14 @@ // Created by lukas on 11.02.20. // -#include -#include +#include "Config.h" +#include "Logger.h" +#include "Version.h" #include #include #include #include -#include std::string Config::dynuapikey; std::string Config::domainid; //id of the dynu domain diff --git a/src/FileLogger.cpp b/src/FileLogger.cpp index 79b1a5d..4468e09 100644 --- a/src/FileLogger.cpp +++ b/src/FileLogger.cpp @@ -2,10 +2,11 @@ // Created by lukas on 05.05.19. // +#include "FileLogger.h" + #include #include - -#include "FileLogger.h" +#include void FileLogger::safeip(std::string ip) { std::ofstream out; @@ -24,5 +25,9 @@ std::string FileLogger::readip() { in >> ip; - return ip; + // when received ip has no : return 0.0.0.0 + if (ip.find(':') == ULONG_MAX) + return "0.0.0.0"; + else + return ip; } diff --git a/src/IPRefresher.cpp b/src/IPRefresher.cpp index 724129c..41347f3 100644 --- a/src/IPRefresher.cpp +++ b/src/IPRefresher.cpp @@ -2,20 +2,19 @@ // Created by lukas on 02.08.19. // +#include "IPRefresher.h" +#include "FileLogger.h" +#include "api/IPAPI.h" +#include "api/DynuAPI.h" +#include "api/TelegramAPI.h" +#include "Config.h" +#include "Version.h" + #include - -#include -#include -#include -#include - #include #include #include - -#include -#include -#include +#include void IPRefresher::checkIPAdress(bool force) { FileLogger logger; @@ -26,6 +25,9 @@ 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) { + // error when ip doesn't contain a : + Logger::warning("an error occured when getting the global ip"); } else { std::string oldip = logger.readip(); diff --git a/src/Logger.cpp b/src/Logger.cpp index 77a81ca..2bfbabe 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -2,11 +2,11 @@ // Created by lukas on 26.10.19. // +#include "Logger.h" + #include #include -#include "Logger.h" - void Logger::debug(const std::string message) { log(message, Logger::Debug); } diff --git a/src/api/API.cpp b/src/api/API.cpp index e246450..b41cd89 100644 --- a/src/api/API.cpp +++ b/src/api/API.cpp @@ -7,7 +7,6 @@ #include #include - std::string API::request(std::string myurl) { Hashmap map; std::vector str; diff --git a/src/api/TelegramAPI.cpp b/src/api/TelegramAPI.cpp index 00131ac..0c26a34 100644 --- a/src/api/TelegramAPI.cpp +++ b/src/api/TelegramAPI.cpp @@ -3,8 +3,9 @@ // #include "api/TelegramAPI.h" +#include "Logger.h" -#include +#include int TelegramAPI::sendMessage(const std::string& text) { Hashmap args; @@ -15,8 +16,7 @@ int TelegramAPI::sendMessage(const std::string& text) { std::string reply = request("https://api.telegram.org/bot" + apikey + "/sendmessage", false, args, headers); - unsigned long ulongmax = -1; - if (reply.find("\"error_code\"") != ulongmax) { + if (reply.find("\"error_code\"") != ULONG_MAX) { Logger::error("failed to refresh the ip (Dynu API)"); return -1; } diff --git a/src/main.cpp b/src/main.cpp index 4eb5fec..141e5f5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,12 +2,14 @@ // Created by lukas on 18.06.19. // -#include -#include -#include -#include +#include "Version.h" +#include "IPRefresher.h" +#include "Logger.h" +#include "Config.h" +#include "api/IPAPI.h" #include + /** * application entry point */ @@ -20,6 +22,7 @@ int main(int argc, char *argv[]) { << "[-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 + << "[-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; @@ -40,6 +43,9 @@ int main(int argc, char *argv[]) { Logger::warning("There are errors in config file!"); return -1; } + } else if (firstarg == "-ip" || firstarg == "--currentip") { + IPAPI ipapi; + std::cout << "Current global IP: " << ipapi.getGlobalIp() << std::endl; } else { Logger::message("wrong arguments! -h for help"); }