From d5812f8266bec443fa780b9e997a8c11e684d99e Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 5 May 2020 15:32:07 +0200 Subject: [PATCH 1/4] added option to get current global ip --- src/main.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 4eb5fec..a7d1dd2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,8 @@ #include #include +#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"); } From a3c8fe44dd0c0b4d845d3e19e608449a0ebc70bd Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 5 May 2020 19:11:47 +0200 Subject: [PATCH 2/4] use climits for MAX_ULONG check if received ip from api has at least a : --- src/IPRefresher.cpp | 4 ++++ src/api/TelegramAPI.cpp | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/IPRefresher.cpp b/src/IPRefresher.cpp index 724129c..14d5335 100644 --- a/src/IPRefresher.cpp +++ b/src/IPRefresher.cpp @@ -16,6 +16,7 @@ #include #include #include +#include void IPRefresher::checkIPAdress(bool force) { FileLogger logger; @@ -26,6 +27,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/api/TelegramAPI.cpp b/src/api/TelegramAPI.cpp index 3df25ea..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,7 +16,6 @@ int TelegramAPI::sendMessage(const std::string& text) { std::string reply = request("https://api.telegram.org/bot" + apikey + "/sendmessage", false, args, headers); - unsigned const long ULONG_MAX = -1; if (reply.find("\"error_code\"") != ULONG_MAX) { Logger::error("failed to refresh the ip (Dynu API)"); return -1; From 1756b0cf5ec3bc660ae19820de0f8c05be4e5431 Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 5 May 2020 19:15:08 +0200 Subject: [PATCH 3/4] check also read ip from file if valid --- src/FileLogger.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/FileLogger.cpp b/src/FileLogger.cpp index 79b1a5d..a851d91 100644 --- a/src/FileLogger.cpp +++ b/src/FileLogger.cpp @@ -4,6 +4,7 @@ #include #include +#include #include "FileLogger.h" @@ -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; } From aef369b38b8baab877f1778ca26748bb74ab4781 Mon Sep 17 00:00:00 2001 From: lukas Date: Tue, 5 May 2020 19:25:23 +0200 Subject: [PATCH 4/4] correct order of #includes to include own libs first and then system ones --- inc/api/API.h | 2 +- inc/api/IPAPI.h | 3 ++- inc/api/TelegramAPI.h | 2 +- src/Config.cpp | 6 +++--- src/FileLogger.cpp | 4 ++-- src/IPRefresher.cpp | 18 ++++++++---------- src/Logger.cpp | 4 ++-- src/api/API.cpp | 1 - src/main.cpp | 10 +++++----- 9 files changed, 24 insertions(+), 26 deletions(-) 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 07bc961..417f84e 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 a851d91..4468e09 100644 --- a/src/FileLogger.cpp +++ b/src/FileLogger.cpp @@ -2,12 +2,12 @@ // Created by lukas on 05.05.19. // +#include "FileLogger.h" + #include #include #include -#include "FileLogger.h" - void FileLogger::safeip(std::string ip) { std::ofstream out; out.open("ip.txt", std::ios::out); diff --git a/src/IPRefresher.cpp b/src/IPRefresher.cpp index 14d5335..41347f3 100644 --- a/src/IPRefresher.cpp +++ b/src/IPRefresher.cpp @@ -2,20 +2,18 @@ // 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) { 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/main.cpp b/src/main.cpp index a7d1dd2..141e5f5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,13 +2,13 @@ // 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 -#include /** * application entry point