DynuIPRefresher/src/main.cpp

60 lines
2.0 KiB
C++
Raw Normal View History

2019-05-05 13:38:48 +00:00
#include <iostream>
2019-05-05 14:35:38 +00:00
#include <ctime>
2019-05-11 09:09:48 +00:00
#include <unordered_map>
2019-05-05 14:42:39 +00:00
#include "api/API.h"
2019-05-05 14:35:38 +00:00
#include "Logger.h"
#include "api/TelegramAPI.h"
#include "api/DynuAPI.h"
#include "api/IPAPI.h"
2019-05-05 13:38:48 +00:00
2019-05-06 10:55:01 +00:00
int main(int argc, char *argv[]) {
2019-05-06 16:49:38 +00:00
if (argc > 1) {
2019-05-06 10:55:01 +00:00
std::string firstarg(argv[1]);
2019-05-06 16:49:38 +00:00
if (firstarg == "-h" || firstarg == "--help") {
std::cout << "help page: " << std::endl << "[-h] [--help] print this help page" << std::endl
<< "[-v] [--version] print the software version" << std::endl
<< "[no argument] normal ip check and refresh" << std::endl;
} else if (firstarg == "-v" || firstarg == "--version") {
2019-05-06 10:55:01 +00:00
std::cout << "Version 1.0" << std::endl;
2019-05-06 16:49:38 +00:00
} else {
2019-05-06 10:55:01 +00:00
std::cout << "wrong arguments! -h for help" << std::endl;
}
2019-05-06 16:49:38 +00:00
} else {
2019-05-06 10:55:01 +00:00
Logger logger;
2019-05-05 14:35:38 +00:00
IPAPI ipapi;
std::string ip = ipapi.getGlobalIp();
2019-05-05 14:35:38 +00:00
2019-05-11 09:09:48 +00:00
if (ip.empty()) {
2019-05-06 10:55:01 +00:00
//no internet connection
logger.logToLogfile("[WARNING] no internet connection");
std::cout << "[WARNING] no internet connection" << std::endl;
} else {
2019-05-06 10:55:01 +00:00
std::string oldip = logger.readip();
2019-05-05 14:35:38 +00:00
2019-05-06 10:55:01 +00:00
if (oldip == ip) {
2019-05-06 16:49:38 +00:00
std::cout << "[INFO] no change -- ip: " << ip << std::endl;
2019-05-06 10:55:01 +00:00
logger.logToLogfile(" [INFO] no change -- ip: " + ip);
} else {
logger.logToLogfile(" [INFO] ip changed! -- from :" + oldip + "to: " + ip);
2019-05-06 16:49:38 +00:00
std::cout << "[INFO] ip changed! -- from :" << oldip << "to: " << ip << std::endl;
2019-05-05 13:38:48 +00:00
DynuAPI dynu;
TelegramAPI tele;
2019-05-05 13:38:48 +00:00
if(dynu.refreshIp(ip)){
tele.sendMessage(oldip + " moved to " + ip);
} else{
//error
logger.logToLogfile(" [ERROR] failed to write ip to dynu api!");
2019-05-06 10:55:01 +00:00
}
logger.safeip(ip);
}
2019-05-05 16:50:10 +00:00
}
}
2019-05-05 13:38:48 +00:00
return 0;
}