cdoc and some minor renames
This commit is contained in:
		@@ -2,20 +2,20 @@
 | 
			
		||||
// Created by lukas on 11.02.20.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#include <Credentials.h>
 | 
			
		||||
#include <Config.h>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <cstring>
 | 
			
		||||
 | 
			
		||||
#include "libconfig.h++"
 | 
			
		||||
 | 
			
		||||
std::string Credentials::dynuapikey;
 | 
			
		||||
std::string Credentials::domainid; //id of the dynu domain
 | 
			
		||||
std::string Credentials::domainname;
 | 
			
		||||
std::string Config::dynuapikey;
 | 
			
		||||
std::string Config::domainid; //id of the dynu domain
 | 
			
		||||
std::string Config::domainname;
 | 
			
		||||
 | 
			
		||||
std::string Credentials::telegramApiKey;
 | 
			
		||||
std::string Credentials::chatId;
 | 
			
		||||
std::string Config::telegramApiKey;
 | 
			
		||||
std::string Config::chatId;
 | 
			
		||||
 | 
			
		||||
bool Credentials::readCredentials() {
 | 
			
		||||
bool Config::readCredentials() {
 | 
			
		||||
    libconfig::Config cfg;
 | 
			
		||||
    try {
 | 
			
		||||
        cfg.readFile("/etc/iprefresher.cfg");
 | 
			
		||||
@@ -40,7 +40,6 @@ bool Credentials::readCredentials() {
 | 
			
		||||
        // optional parameters
 | 
			
		||||
        telegramApiKey = (std::string) cfg.lookup("telegramApiKey");
 | 
			
		||||
        chatId = (std::string) cfg.lookup("chatId");
 | 
			
		||||
        std::cout << "Store name: " << dynuapikey << std::endl;
 | 
			
		||||
    }
 | 
			
		||||
    catch (const libconfig::SettingNotFoundException &nfex) {
 | 
			
		||||
        // triggered if setting is missing in config
 | 
			
		||||
@@ -49,5 +48,5 @@ bool Credentials::readCredentials() {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // check if needed values aren't empty
 | 
			
		||||
    return !(Credentials::dynuapikey.empty() || Credentials::domainid.empty() || Credentials::domainname.empty());
 | 
			
		||||
    return !(Config::dynuapikey.empty() || Config::domainid.empty() || Config::domainname.empty());
 | 
			
		||||
}
 | 
			
		||||
@@ -14,7 +14,7 @@
 | 
			
		||||
#include <Logger.h>
 | 
			
		||||
 | 
			
		||||
#include <IPRefresher.h>
 | 
			
		||||
#include <Credentials.h>
 | 
			
		||||
#include <Config.h>
 | 
			
		||||
 | 
			
		||||
void IPRefresher::checkIPAdress(bool force) {
 | 
			
		||||
    FileLogger logger;
 | 
			
		||||
@@ -34,11 +34,11 @@ void IPRefresher::checkIPAdress(bool force) {
 | 
			
		||||
            Logger::message("ip changed! -- from :" + oldip + "to: " + ip);
 | 
			
		||||
 | 
			
		||||
            DynuAPI dynu;
 | 
			
		||||
            dynu.init(Credentials::dynuapikey, Credentials::domainid, Credentials::domainname);
 | 
			
		||||
            dynu.init(Config::dynuapikey, Config::domainid, Config::domainname);
 | 
			
		||||
 | 
			
		||||
            if (dynu.refreshIp(ip)) {
 | 
			
		||||
                TelegramAPI tele;
 | 
			
		||||
                tele.init(Credentials::telegramApiKey, Credentials::chatId);
 | 
			
		||||
                tele.init(Config::telegramApiKey, Config::chatId);
 | 
			
		||||
                tele.sendMessage(oldip + " moved to " + ip);
 | 
			
		||||
            } else {
 | 
			
		||||
                //error
 | 
			
		||||
@@ -53,14 +53,16 @@ void IPRefresher::checkIPAdress(bool force) {
 | 
			
		||||
IPRefresher::IPRefresher() = default;
 | 
			
		||||
 | 
			
		||||
IPRefresher::IPRefresher(bool loop) {
 | 
			
		||||
    if (Credentials::readCredentials()) {
 | 
			
		||||
    if (loop) {
 | 
			
		||||
        Logger::message("startup of service");
 | 
			
		||||
        while (loop) {
 | 
			
		||||
            Logger::message("starting check");
 | 
			
		||||
            checkIPAdress(false);
 | 
			
		||||
            std::this_thread::sleep_for(std::chrono::milliseconds(300000));
 | 
			
		||||
        if (Config::readCredentials()) {
 | 
			
		||||
            while (true) {
 | 
			
		||||
                Logger::message("starting check");
 | 
			
		||||
                checkIPAdress(false);
 | 
			
		||||
                std::this_thread::sleep_for(std::chrono::milliseconds(300000));
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            std::cout << "incorrect credentials!" << std::endl;
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        std::cout << "incorrect credentials!" << std::endl;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -7,12 +7,6 @@
 | 
			
		||||
 | 
			
		||||
#include "Logger.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
const int Logger::Warning = 1;
 | 
			
		||||
const int Logger::Debug = 2;
 | 
			
		||||
const int Logger::Message = 3;
 | 
			
		||||
const int Logger::Error = 4;
 | 
			
		||||
 | 
			
		||||
void Logger::debug(const std::string message) {
 | 
			
		||||
    log(message, Logger::Debug);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -5,19 +5,16 @@
 | 
			
		||||
#include "api/DynuAPI.h"
 | 
			
		||||
 | 
			
		||||
int DynuAPI::refreshIp(std:: string ip) {
 | 
			
		||||
 | 
			
		||||
    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);
 | 
			
		||||
    headers.emplace_back("accept: application/json");
 | 
			
		||||
    headers.emplace_back("User-Agent: Mozilla/5.0 (compatible; Rigor/1.0.0; http://rigor.com)");
 | 
			
		||||
    headers.emplace_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;
 | 
			
		||||
    const std::string dynurepl = request("https://api.dynu.com/v2/dns/" + domainid, true, args, headers);
 | 
			
		||||
 | 
			
		||||
    if (dynurepl != "{\"statusCode\":200}") {
 | 
			
		||||
        return -1;
 | 
			
		||||
@@ -26,7 +23,7 @@ int DynuAPI::refreshIp(std:: string ip) {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void DynuAPI::init(std::string dynuApiKey, std::string domainId, std::string domainName) {
 | 
			
		||||
void DynuAPI::init(const std::string& dynuApiKey, const std::string& domainId, const std::string& domainName) {
 | 
			
		||||
    this->dynuapikey=dynuApiKey;
 | 
			
		||||
    this->domainid=domainId;
 | 
			
		||||
    this->domainname=domainName;
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,9 @@
 | 
			
		||||
 | 
			
		||||
#include "api/TelegramAPI.h"
 | 
			
		||||
 | 
			
		||||
void TelegramAPI::sendMessage(std::string text) {
 | 
			
		||||
#include <Logger.h>
 | 
			
		||||
 | 
			
		||||
int TelegramAPI::sendMessage(const std::string& text) {
 | 
			
		||||
    Hashmap<std::string, std::string> args;
 | 
			
		||||
    args.add("chat_id", chatid);
 | 
			
		||||
    args.add("text", text);
 | 
			
		||||
@@ -12,10 +14,16 @@ void TelegramAPI::sendMessage(std::string text) {
 | 
			
		||||
    std::vector<std::string> headers;
 | 
			
		||||
 | 
			
		||||
    std::string reply = request("https://api.telegram.org/bot" + apikey + "/sendmessage", false, args, headers);
 | 
			
		||||
//    std::cout << "[DEBUG] " << reply << std::endl;
 | 
			
		||||
 | 
			
		||||
    unsigned const long ULONG_MAX = -1;
 | 
			
		||||
    if (reply.find("\"error_code\"") != ULONG_MAX) {
 | 
			
		||||
        Logger::error("failed to refresh the ip (Dynu API)");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void TelegramAPI::init(std::string apikey, std::string chatid) {
 | 
			
		||||
void TelegramAPI::init(const std::string apikey, const std::string chatid) {
 | 
			
		||||
    this->apikey = apikey;
 | 
			
		||||
    this->chatid = chatid;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										14
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								src/main.cpp
									
									
									
									
									
								
							@@ -1,9 +1,16 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by lukas on 18.06.19.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <IPRefresher.h>
 | 
			
		||||
#include <Version.h>
 | 
			
		||||
#include <Logger.h>
 | 
			
		||||
#include <Credentials.h>
 | 
			
		||||
#include <Config.h>
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * application entry point
 | 
			
		||||
 */
 | 
			
		||||
int main(int argc, char *argv[]) {
 | 
			
		||||
    if (argc > 1) {
 | 
			
		||||
        std::string firstarg(argv[1]);
 | 
			
		||||
@@ -17,7 +24,7 @@ int main(int argc, char *argv[]) {
 | 
			
		||||
            std::cout << "Version " << Version::VERSION << std::endl;
 | 
			
		||||
        } else if (firstarg == "-f" || firstarg == "--force") {
 | 
			
		||||
            IPRefresher ipr;
 | 
			
		||||
            if (Credentials::readCredentials()) {
 | 
			
		||||
            if (Config::readCredentials()) {
 | 
			
		||||
                ipr.checkIPAdress(true);
 | 
			
		||||
            } else {
 | 
			
		||||
                std::cout << "incorrect credentials!" << std::endl;
 | 
			
		||||
@@ -31,12 +38,11 @@ int main(int argc, char *argv[]) {
 | 
			
		||||
    } else {
 | 
			
		||||
        IPRefresher ipr;
 | 
			
		||||
        Logger::message("starting check");
 | 
			
		||||
        if (Credentials::readCredentials()) {
 | 
			
		||||
        if (Config::readCredentials()) {
 | 
			
		||||
            ipr.checkIPAdress(false);
 | 
			
		||||
        } else {
 | 
			
		||||
            std::cout << "incorrect credentials!" << std::endl;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user