added seperate class for ip request and dynu request
This commit is contained in:
		
							
								
								
									
										32
									
								
								src/api/DynuAPI.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/api/DynuAPI.cpp
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										20
									
								
								src/api/DynuAPI.h
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										9
									
								
								src/api/IPAPI.cpp
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										19
									
								
								src/api/IPAPI.h
									
									
									
									
									
										Normal 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
 | 
			
		||||
							
								
								
									
										33
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								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<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;
 | 
			
		||||
                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);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user