Merge branch 'master' into GUI
This commit is contained in:
@ -1,11 +1,12 @@
|
||||
#include "Config.h"
|
||||
#include "Logger.h"
|
||||
#include "Version.h"
|
||||
#include "StaticData.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <libconfig.h++>
|
||||
#include <sys/stat.h>
|
||||
|
||||
std::string Config::dynuapikey;
|
||||
std::string Config::domainid; //id of the dynu domain
|
||||
@ -19,15 +20,41 @@ bool Config::telegramSupport;
|
||||
bool Config::readConfig() {
|
||||
libconfig::Config cfg;
|
||||
try {
|
||||
cfg.readFile(Version::ConfigDir.c_str());
|
||||
cfg.readFile(std::string(StaticData::ConfigDir + StaticData::ConfName).c_str());
|
||||
}
|
||||
catch (const libconfig::FileIOException &fioex) {
|
||||
std::cout << "I/O error while reading config file." << std::endl << "creating new config file!" << std::endl;
|
||||
|
||||
// check if config folder exists
|
||||
struct stat info{};
|
||||
|
||||
if (stat(StaticData::ConfigDir.c_str(), &info) != 0) {
|
||||
Logger::warning("The config folder doesn't exist. Trying to create it.");
|
||||
|
||||
// mkdir command is different defined for windows
|
||||
#ifdef __unix
|
||||
int check = mkdir(StaticData::ConfigDir.c_str(), 777);
|
||||
#else
|
||||
int check = mkdir(StaticData::ConfigDir.c_str());
|
||||
#endif
|
||||
|
||||
// check if directory is created or not
|
||||
if (!check)
|
||||
Logger::message("config directory successfully created. ");
|
||||
else
|
||||
Logger::error("unable to create config directory.");
|
||||
|
||||
} else if (info.st_mode & S_IFDIR) {
|
||||
Logger::debug("config directory exists already");
|
||||
} else {
|
||||
Logger::error("A file exists with the same name as the config dir should be");
|
||||
}
|
||||
|
||||
|
||||
std::ofstream myfile;
|
||||
myfile.open(Version::ConfigDir);
|
||||
myfile.open(StaticData::ConfigDir + StaticData::ConfName);
|
||||
if (myfile.is_open()) {
|
||||
myfile << Version::SAMPLECONFIG;
|
||||
myfile << StaticData::SAMPLECONFIG;
|
||||
myfile.close();
|
||||
} else {
|
||||
Logger::error("error creating file");
|
||||
@ -73,7 +100,7 @@ bool Config::validateConfig() {
|
||||
libconfig::Config cfg;
|
||||
try {
|
||||
Logger::message("reading config file");
|
||||
cfg.readFile(Version::ConfigDir.c_str());
|
||||
cfg.readFile(StaticData::ConfigDir.c_str());
|
||||
}
|
||||
catch (const libconfig::FileIOException &fioex) {
|
||||
Logger::warning("config file doesn't exist or permission denied!");
|
||||
|
@ -1,12 +1,13 @@
|
||||
#include "FileLogger.h"
|
||||
#include "IpHelper.h"
|
||||
#include "StaticData.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
void FileLogger::safeip(std::string ip) {
|
||||
std::ofstream out;
|
||||
out.open("ip.txt", std::ios::out);
|
||||
out.open(StaticData::TempFilePath + "temp-dynuiprefresher.txt", std::ios::out);
|
||||
|
||||
out << ip;
|
||||
|
||||
@ -15,7 +16,7 @@ void FileLogger::safeip(std::string ip) {
|
||||
|
||||
std::string FileLogger::readip() {
|
||||
std::ifstream in;
|
||||
in.open("ip.txt", std::ios::in);
|
||||
in.open(StaticData::TempFilePath + "temp-dynuiprefresher.txt", std::ios::in);
|
||||
|
||||
std::string ip;
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include "api/DynuAPI.h"
|
||||
#include "api/TelegramAPI.h"
|
||||
#include "Config.h"
|
||||
#include "Version.h"
|
||||
#include "StaticData.h"
|
||||
#include "IpHelper.h"
|
||||
|
||||
#include <chrono>
|
||||
@ -59,15 +59,18 @@ bool IPRefresher::checkIPAdress(bool force) {
|
||||
|
||||
void IPRefresher::startUpService(int interval) {
|
||||
Logger::message("startup of service");
|
||||
Logger::message("Version: " + Version::VERSION);
|
||||
Logger::message("Version: " + StaticData::VERSION);
|
||||
if (Config::readConfig()) {
|
||||
while (true) {
|
||||
Logger::message("starting check");
|
||||
checkIPAdress(false);
|
||||
if (Config::readConfig()) {
|
||||
checkIPAdress(false);
|
||||
} else {
|
||||
std::cout << "incorrect credentials!" << std::endl;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(interval * 1000));
|
||||
}
|
||||
} else {
|
||||
std::cout << "incorrect credentials!" << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
#include "IpHelper.h"
|
||||
|
||||
#include <climits>
|
||||
|
||||
bool IpHelper::isIpValid(std::string ip) {
|
||||
return (ip.find('.') != ULONG_MAX);
|
||||
return (ip.find('.') != SIZE_MAX);
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
#include "api/TelegramAPI.h"
|
||||
#include "Logger.h"
|
||||
|
||||
#include <climits>
|
||||
|
||||
int TelegramAPI::sendMessage(const std::string &text) {
|
||||
Hashmap<std::string, std::string> args;
|
||||
args.add("chat_id", chatid);
|
||||
@ -12,8 +10,8 @@ int TelegramAPI::sendMessage(const std::string &text) {
|
||||
|
||||
std::string reply = request("https://api.telegram.org/bot" + apikey + "/sendmessage", false, args, headers);
|
||||
|
||||
if (reply.find("\"error_code\"") != ULONG_MAX) {
|
||||
Logger::error("failed to refresh the ip (Dynu API)");
|
||||
if (reply.find("\"error_code\"") != SIZE_MAX) {
|
||||
Logger::error("failed to send the Telegram Message");
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include "Version.h"
|
||||
#include "StaticData.h"
|
||||
#include "IPRefresher.h"
|
||||
#include "Logger.h"
|
||||
#include "Config.h"
|
||||
@ -19,7 +19,7 @@ int main(int argc, char *argv[]) {
|
||||
<< "[-ip] [--currentip] get current global ip" << std::endl
|
||||
<< "[no argument] normal ip check and refresh" << std::endl;
|
||||
} else if (firstarg == "-v" || firstarg == "--version") {
|
||||
std::cout << "Version " << Version::VERSION << std::endl;
|
||||
std::cout << "Version " << StaticData::VERSION << std::endl;
|
||||
} else if (firstarg == "-f" || firstarg == "--force") {
|
||||
if (Config::readConfig()) {
|
||||
IPRefresher::checkIPAdress(true);
|
||||
|
Reference in New Issue
Block a user