outsourced main method of ip refresh and added some doc

This commit is contained in:
max mustermann 2019-08-02 22:44:42 +02:00
parent 700d7912df
commit f325c3a371
8 changed files with 82 additions and 43 deletions

2
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
# Default ignored files
/workspace.xml

View File

@ -26,7 +26,7 @@ add_library(logger
set(SOURCE set(SOURCE
src/main.cpp src/main.cpp
) src/IPRefresher.cpp inc/IPRefresher.h)
add_executable(iprefresher ${SOURCE}) add_executable(iprefresher ${SOURCE})

14
inc/IPRefresher.h Normal file
View File

@ -0,0 +1,14 @@
//
// Created by lukas on 02.08.19.
//
#pragma once
class IPRefresher {
public:
/**
* refresh ip address on Dynu server
*/
void checkIPAdress();
};

View File

@ -3,6 +3,7 @@
// //
#pragma once #pragma once
#include <string>
class Logger { class Logger {
public: public:
/** /**

View File

@ -16,7 +16,14 @@ public:
*/ */
void sendMessage(std::string text); void sendMessage(std::string text);
/**
* init Telegram api with apikey and chatid
* @param apikey recieved API key
* @param chatid chatid where bot should post into
*/
void init(std::string apikey, std::string chatid);
private: private:
const std::string apikey = "717213769:AAHan1nSXhUsxLJAN1Dv8Oc0z8wqwDdYPn4"; std::string apikey;
const std::string chatid = "618154204"; std::string chatid;
}; };

47
src/IPRefresher.cpp Normal file
View File

@ -0,0 +1,47 @@
//
// Created by lukas on 02.08.19.
//
#include <string>
#include <Logger.h>
#include <api/IPAPI.h>
#include <api/DynuAPI.h>
#include <api/TelegramAPI.h>
#include "IPRefresher.h"
void IPRefresher::checkIPAdress() {
Logger logger;
IPAPI ipapi;
std::string ip = ipapi.getGlobalIp();
if (ip.empty()) {
//no internet connection
logger.logToLogfile("[WARNING] no internet connection");
std::cout << "[WARNING] no internet connection" << std::endl;
} else {
std::string oldip = logger.readip();
if (oldip == ip) {
std::cout << "[INFO] no change -- ip: " << ip << std::endl;
logger.logToLogfile(" [INFO] no change -- ip: " + ip);
} else {
logger.logToLogfile(" [INFO] ip changed! -- from :" + oldip + "to: " + ip);
std::cout << "[INFO] ip changed! -- from :" << oldip << "to: " << ip << std::endl;
DynuAPI dynu;
if (dynu.refreshIp(ip)) {
TelegramAPI tele;
tele.init("717213769:AAHan1nSXhUsxLJAN1Dv8Oc0z8wqwDdYPn4","618154204");
tele.sendMessage(oldip + " moved to " + ip);
} else {
//error
logger.logToLogfile(" [ERROR] failed to write ip to dynu api!");
}
logger.safeip(ip);
}
}
}

View File

@ -14,3 +14,8 @@ void TelegramAPI::sendMessage(std::string text) {
std::string reply = request("https://api.telegram.org/bot" + apikey + "/sendmessage", false, args, headers); std::string reply = request("https://api.telegram.org/bot" + apikey + "/sendmessage", false, args, headers);
// std::cout << "[DEBUG] " << reply << std::endl; // std::cout << "[DEBUG] " << reply << std::endl;
} }
void TelegramAPI::init(std::string apikey, std::string chatid) {
this->apikey = apikey;
this->chatid = chatid;
}

View File

@ -1,12 +1,5 @@
#include <iostream> #include <iostream>
#include <ctime> #include <IPRefresher.h>
#include <unordered_map>
#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[]) { int main(int argc, char *argv[]) {
@ -22,38 +15,8 @@ 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 {
Logger logger; IPRefresher ipr;
ipr.checkIPAdress();
IPAPI ipapi;
std::string ip = ipapi.getGlobalIp();
if (ip.empty()) {
//no internet connection
logger.logToLogfile("[WARNING] no internet connection");
std::cout << "[WARNING] no internet connection" << std::endl;
} else {
std::string oldip = logger.readip();
if (oldip == ip) {
std::cout << "[INFO] no change -- ip: " << ip << std::endl;
logger.logToLogfile(" [INFO] no change -- ip: " + ip);
} else {
logger.logToLogfile(" [INFO] ip changed! -- from :" + oldip + "to: " + ip);
std::cout << "[INFO] ip changed! -- from :" << oldip << "to: " << ip << std::endl;
DynuAPI dynu;
if (dynu.refreshIp(ip)) {
TelegramAPI tele;
tele.sendMessage(oldip + " moved to " + ip);
} else {
//error
logger.logToLogfile(" [ERROR] failed to write ip to dynu api!");
}
logger.safeip(ip);
}
}
} }
return 0; return 0;