added seperate class for ip request and dynu request

This commit is contained in:
Lukas-Heiligenbrunner 2019-06-18 11:23:24 +02:00
parent 0e8783d354
commit 07ce161155
6 changed files with 93 additions and 24 deletions

View File

@ -20,7 +20,9 @@ set(SOURCE
src/api/TelegramAPI.cpp src/api/TelegramAPI.cpp
src/api/TelegramAPI.h src/api/TelegramAPI.h
)
src/api/DynuAPI.cpp
src/api/DynuAPI.h src/api/IPAPI.cpp src/api/IPAPI.h)
add_executable(iprefresher ${SOURCE}) add_executable(iprefresher ${SOURCE})
target_link_libraries(iprefresher ${CURL_LIBRARIES}) target_link_libraries(iprefresher ${CURL_LIBRARIES})

32
src/api/DynuAPI.cpp Normal file
View File

@ -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<std::string, std::string> args;
args.add("name", domainname);
args.add("ipv4Address", ip);
std::vector<std::string> 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;
}
}

20
src/api/DynuAPI.h Normal file
View File

@ -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

9
src/api/IPAPI.cpp Normal file
View File

@ -0,0 +1,9 @@
//
// Created by lukas on 18.06.19.
//
#include "IPAPI.h"
std::string IPAPI::getGlobalIp() {
return request("https://api.ipify.org");
}

19
src/api/IPAPI.h Normal file
View File

@ -0,0 +1,19 @@
//
// Created by lukas on 18.06.19.
//
#ifndef IPREFRESHER_IPAPI_H
#define IPREFRESHER_IPAPI_H
#include <string>
#include "API.h"
class IPAPI : API{
public:
std::string getGlobalIp();
};
#endif //IPREFRESHER_IPAPI_H

View File

@ -5,6 +5,8 @@
#include "api/API.h" #include "api/API.h"
#include "Logger.h" #include "Logger.h"
#include "api/TelegramAPI.h" #include "api/TelegramAPI.h"
#include "api/DynuAPI.h"
#include "api/IPAPI.h"
int main(int argc, char *argv[]) { 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; std::cout << "wrong arguments! -h for help" << std::endl;
} }
} else { } else {
API api;
Logger logger; Logger logger;
std::string ip = api.request("https://api.ipify.org"); IPAPI ipapi;
std::string ip = ipapi.getGlobalIp();
if (ip.empty()) { if (ip.empty()) {
//no internet connection //no internet connection
@ -39,29 +41,14 @@ int main(int argc, char *argv[]) {
logger.logToLogfile(" [INFO] ip changed! -- from :" + oldip + "to: " + ip); logger.logToLogfile(" [INFO] ip changed! -- from :" + oldip + "to: " + ip);
std::cout << "[INFO] ip changed! -- from :" << oldip << "to: " << ip << std::endl; 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 if(dynu.refreshIp(ip)){
static std::string domainname = "luki.dynu.net";
Hashmap<std::string, std::string> args;
args.add("name", domainname);
args.add("ipv4Address", ip);
std::vector<std::string> 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;
tele.sendMessage(oldip + " moved to " + ip); tele.sendMessage(oldip + " moved to " + ip);
} else{
//error
logger.logToLogfile(" [ERROR] failed to write ip to dynu api!");
} }
logger.safeip(ip); logger.safeip(ip);