diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a0e887..c0aad81 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,7 +20,9 @@ set(SOURCE src/api/TelegramAPI.cpp src/api/TelegramAPI.h - ) + + src/api/DynuAPI.cpp + src/api/DynuAPI.h src/api/IPAPI.cpp src/api/IPAPI.h) add_executable(iprefresher ${SOURCE}) target_link_libraries(iprefresher ${CURL_LIBRARIES}) diff --git a/src/api/DynuAPI.cpp b/src/api/DynuAPI.cpp new file mode 100644 index 0000000..1cc3bb0 --- /dev/null +++ b/src/api/DynuAPI.cpp @@ -0,0 +1,32 @@ +// +// Created by lukas on 18.06.19. +// + +#include "DynuAPI.h" + +int DynuAPI::refreshIp(std:: string ip) { + + static std::string dynuapikey = "88vNpMfDhMM2YYDNfWR1DNYfRX9W6fYg"; + + static std::string domainid = "8506047"; //id of the dynu domain + static std::string domainname = "luki.dynu.net"; + + Hashmap args; + args.add("name", domainname); + args.add("ipv4Address", ip); + + std::vector headers; + headers.push_back("accept: application/json"); + headers.push_back("User-Agent: Mozilla/5.0 (compatible; Rigor/1.0.0; http://rigor.com)"); + headers.push_back("API-Key: " + dynuapikey); + + std::string dynurepl = request("https://api.dynu.com/v2/dns/" + domainid, true, args, headers); + + std::cout << "[DEBUG] api reply:: " << dynurepl << std::endl; + + if (dynurepl != "{\"statusCode\":200}") { + return -1; + } else { + return 1; + } +} diff --git a/src/api/DynuAPI.h b/src/api/DynuAPI.h new file mode 100644 index 0000000..53d0d42 --- /dev/null +++ b/src/api/DynuAPI.h @@ -0,0 +1,20 @@ +// +// Created by lukas on 18.06.19. +// + +#ifndef IPREFRESHER_DYNUAPI_H +#define IPREFRESHER_DYNUAPI_H + + +#include "API.h" + +class DynuAPI : API{ +public: + int refreshIp(std::string ip); +private: + + +}; + + +#endif //IPREFRESHER_DYNUAPI_H diff --git a/src/api/IPAPI.cpp b/src/api/IPAPI.cpp new file mode 100644 index 0000000..d2005b9 --- /dev/null +++ b/src/api/IPAPI.cpp @@ -0,0 +1,9 @@ +// +// Created by lukas on 18.06.19. +// + +#include "IPAPI.h" + +std::string IPAPI::getGlobalIp() { + return request("https://api.ipify.org"); +} diff --git a/src/api/IPAPI.h b/src/api/IPAPI.h new file mode 100644 index 0000000..1a280d8 --- /dev/null +++ b/src/api/IPAPI.h @@ -0,0 +1,19 @@ +// +// Created by lukas on 18.06.19. +// + +#ifndef IPREFRESHER_IPAPI_H +#define IPREFRESHER_IPAPI_H + + +#include +#include "API.h" + +class IPAPI : API{ +public: + std::string getGlobalIp(); + +}; + + +#endif //IPREFRESHER_IPAPI_H diff --git a/src/main.cpp b/src/main.cpp index 08d237c..18a87d7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,8 @@ #include "api/API.h" #include "Logger.h" #include "api/TelegramAPI.h" +#include "api/DynuAPI.h" +#include "api/IPAPI.h" int main(int argc, char *argv[]) { @@ -20,10 +22,10 @@ int main(int argc, char *argv[]) { std::cout << "wrong arguments! -h for help" << std::endl; } } else { - API api; Logger logger; - std::string ip = api.request("https://api.ipify.org"); + IPAPI ipapi; + std::string ip = ipapi.getGlobalIp(); if (ip.empty()) { //no internet connection @@ -39,29 +41,14 @@ int main(int argc, char *argv[]) { logger.logToLogfile(" [INFO] ip changed! -- from :" + oldip + "to: " + ip); std::cout << "[INFO] ip changed! -- from :" << oldip << "to: " << ip << std::endl; - static std::string dynuapikey = "88vNpMfDhMM2YYDNfWR1DNYfRX9W6fYg"; + DynuAPI dynu; + TelegramAPI tele; - static std::string domainid = "8506047"; //id of the dynu domain - static std::string domainname = "luki.dynu.net"; - - Hashmap args; - args.add("name", domainname); - args.add("ipv4Address", ip); - - std::vector headers; - headers.push_back("accept: application/json"); - headers.push_back("User-Agent: Mozilla/5.0 (compatible; Rigor/1.0.0; http://rigor.com)"); - headers.push_back("API-Key: " + dynuapikey); - - std::string dynurepl = api.request("https://api.dynu.com/v2/dns/" + domainid, true, args, headers); - - std::cout << "[DEBUG] api reply:: " << dynurepl << std::endl; - - if (dynurepl != "{\"statusCode\":200}") { - logger.logToLogfile(" [ERROR] failed to write ip to dynu api!"); - } else { - TelegramAPI tele; + if(dynu.refreshIp(ip)){ tele.sendMessage(oldip + " moved to " + ip); + } else{ + //error + logger.logToLogfile(" [ERROR] failed to write ip to dynu api!"); } logger.safeip(ip);