drop dependency of libconfig and write own simple parser
This commit is contained in:
parent
d364bc2ed8
commit
b5aa78a59f
@ -16,8 +16,6 @@ conan:
|
|||||||
script:
|
script:
|
||||||
- conan profile new default --detect --force # Generates default profile detecting GCC and sets old ABI
|
- conan profile new default --detect --force # Generates default profile detecting GCC and sets old ABI
|
||||||
- conan profile update settings.compiler.libcxx=libstdc++11 default # Sets libcxx to C++11 ABI
|
- conan profile update settings.compiler.libcxx=libstdc++11 default # Sets libcxx to C++11 ABI
|
||||||
- conan remote add bintray https://api.bintray.com/conan/lheili/LibConfig --force
|
|
||||||
- conan remote add bincrafters https://api.bintray.com/conan/bincrafters/public-conan --force
|
|
||||||
- mkdir -p build # create build folder
|
- mkdir -p build # create build folder
|
||||||
- CONAN_SYSREQUIRES_MODE=disabled conan install . --build=missing -g cmake -if build
|
- CONAN_SYSREQUIRES_MODE=disabled conan install . --build=missing -g cmake -if build
|
||||||
- cmake -S . -B build -D WinBuild=OFF -D GUI=OFF #cmake project
|
- cmake -S . -B build -D WinBuild=OFF -D GUI=OFF #cmake project
|
||||||
|
@ -141,12 +141,13 @@ add_library(libdynuiprefresher ${LIB_METHOD}
|
|||||||
src/IpHelper.cpp
|
src/IpHelper.cpp
|
||||||
src/FileLogger.cpp
|
src/FileLogger.cpp
|
||||||
src/Logger.cpp
|
src/Logger.cpp
|
||||||
src/Config.cpp)
|
src/Config.cpp
|
||||||
|
src/CMDParser.cpp)
|
||||||
|
|
||||||
add_executable(${Application_Name} src/main.cpp)
|
add_executable(${Application_Name} src/main.cpp)
|
||||||
|
|
||||||
# LINK generated LIBS #
|
# LINK generated LIBS #
|
||||||
target_link_libraries(${Application_Name} libdynuiprefresher api CONAN_PKG::libcurl CONAN_PKG::LibConfig)
|
target_link_libraries(${Application_Name} libdynuiprefresher api CONAN_PKG::libcurl)
|
||||||
|
|
||||||
# setting install targets
|
# setting install targets
|
||||||
IF (NOT ${WinBuild})
|
IF (NOT ${WinBuild})
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
[requires]
|
[requires]
|
||||||
libcurl/7.72.0
|
libcurl/7.72.0
|
||||||
openssl/1.1.1i
|
openssl/1.1.1i
|
||||||
LibConfig/1.7.2@LibConfig/stable
|
|
||||||
|
|
||||||
[generators]
|
[generators]
|
||||||
cmake
|
cmake
|
||||||
|
@ -3,10 +3,10 @@
|
|||||||
# Lukas Heiligenbrunner
|
# Lukas Heiligenbrunner
|
||||||
|
|
||||||
## DYNU API Config
|
## DYNU API Config
|
||||||
dynuapikey = ""
|
dynuapikey=
|
||||||
domainid = ""
|
domainid=
|
||||||
domainname = ""
|
domainname=
|
||||||
|
|
||||||
## Telegram API Config (optional)
|
## Telegram API Config (optional)
|
||||||
#telegramApiKey = ""
|
#telegramApiKey=
|
||||||
#chatId = ""
|
#chatId=
|
||||||
|
23
inc/CMDParser.h
Normal file
23
inc/CMDParser.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
//
|
||||||
|
// Created by lukas on 15.03.21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
class CMDParser {
|
||||||
|
public:
|
||||||
|
class Arguments {
|
||||||
|
public:
|
||||||
|
// enable loop mode
|
||||||
|
bool loop = false;
|
||||||
|
bool force = false;
|
||||||
|
// enable currentIP mode
|
||||||
|
bool currentIP = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
Arguments* parseArguments(int argc, char *argv[]);
|
||||||
|
private:
|
||||||
|
Arguments args;
|
||||||
|
|
||||||
|
void processParameter(char *arg);
|
||||||
|
};
|
@ -97,6 +97,9 @@ private:
|
|||||||
*/
|
*/
|
||||||
Config() = default;
|
Config() = default;
|
||||||
|
|
||||||
|
static void writeDefaultConfig();
|
||||||
|
static void setParam(const std::string key, const std::string value);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* helper variable for managing telegram Support
|
* helper variable for managing telegram Support
|
||||||
*/
|
*/
|
||||||
|
42
src/CMDParser.cpp
Normal file
42
src/CMDParser.cpp
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
//
|
||||||
|
// Created by lukas on 15.03.21.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <Logger.h>
|
||||||
|
#include <StaticData.h>
|
||||||
|
#include "CMDParser.h"
|
||||||
|
|
||||||
|
CMDParser::Arguments* CMDParser::parseArguments(int argc, char **argv) {
|
||||||
|
for (int i = 1; i < argc; i++) {
|
||||||
|
this->processParameter(argv[i]);
|
||||||
|
}
|
||||||
|
return &args;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CMDParser::processParameter(char *arg) {
|
||||||
|
const std::string param = std::string(arg);
|
||||||
|
|
||||||
|
if (param == "-h" || param == "--help") {
|
||||||
|
std::cout << "help page: " << std::endl << "[-h] [--help] print this help page" << std::endl
|
||||||
|
<< "[-v] [--version] print the software version" << std::endl
|
||||||
|
<< "[-f] [--force] force refresh of ip" << std::endl
|
||||||
|
<< "[-l] [--loop] infinite loop to refresh ip every five minutes" << std::endl
|
||||||
|
<< "[-c] [--checkconfig] validate configuration" << std::endl
|
||||||
|
<< "[-ip] [--currentip] get current global ip" << std::endl
|
||||||
|
<< "[no argument] normal ip check and refresh" << std::endl;
|
||||||
|
exit(0);
|
||||||
|
} else if (param == "-v" || param == "--version") {
|
||||||
|
std::cout << "Version " << StaticData::VERSION << std::endl;
|
||||||
|
exit(0);
|
||||||
|
} else if (param == "-f" || param == "--force") {
|
||||||
|
args.force = true;
|
||||||
|
} else if (param == "-l" || param == "--loop") {
|
||||||
|
args.loop = true;
|
||||||
|
} else if (param == "-ip" || param == "--currentip") {
|
||||||
|
args.currentIP = true;
|
||||||
|
} else {
|
||||||
|
Logger::message("unknown argument: " + param + "! -h for help");
|
||||||
|
}
|
||||||
|
}
|
239
src/Config.cpp
239
src/Config.cpp
@ -5,8 +5,8 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <libconfig.h++>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
std::string Config::dynuapikey;
|
std::string Config::dynuapikey;
|
||||||
std::string Config::domainid; //id of the dynu domain
|
std::string Config::domainid; //id of the dynu domain
|
||||||
@ -18,75 +18,34 @@ std::string Config::chatId;
|
|||||||
bool Config::telegramSupport;
|
bool Config::telegramSupport;
|
||||||
|
|
||||||
bool Config::readConfig() {
|
bool Config::readConfig() {
|
||||||
libconfig::Config cfg;
|
// Read from the text file
|
||||||
try {
|
std::ifstream MyReadFile;
|
||||||
cfg.readFile(std::string(StaticData::ConfigDir + StaticData::ConfName).c_str());
|
MyReadFile.open(std::string(StaticData::ConfigDir + StaticData::ConfName));
|
||||||
}
|
if (!MyReadFile) {
|
||||||
catch (const libconfig::FileIOException &fioex) {
|
// file does not exist
|
||||||
std::cout << "I/O error while reading config file." << std::endl << "creating new config file!" << std::endl;
|
writeDefaultConfig();
|
||||||
|
|
||||||
// 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(StaticData::ConfigDir + StaticData::ConfName);
|
|
||||||
if (myfile.is_open()) {
|
|
||||||
myfile << StaticData::SAMPLECONFIG;
|
|
||||||
myfile.close();
|
|
||||||
} else {
|
|
||||||
Logger::error("error creating file");
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
catch (const libconfig::ParseException &pex) {
|
|
||||||
std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
|
|
||||||
<< " - " << pex.getError() << std::endl;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
std::string line;
|
||||||
// needed parameters
|
while (getline(MyReadFile, line)) {
|
||||||
dynuapikey = (std::string) cfg.lookup("dynuapikey");
|
// ignore line if it starts with #
|
||||||
domainid = (std::string) cfg.lookup("domainid");
|
if(line.rfind('#', 0) == 0 || line.empty()){
|
||||||
domainname = (std::string) cfg.lookup("domainname");
|
continue;
|
||||||
// optional parameters
|
}
|
||||||
telegramApiKey = (std::string) cfg.lookup("telegramApiKey");
|
|
||||||
chatId = (std::string) cfg.lookup("chatId");
|
std::istringstream is_line(line);
|
||||||
telegramSupport = true;
|
std::string key;
|
||||||
}
|
if (std::getline(is_line, key, '=')) {
|
||||||
catch (const libconfig::SettingNotFoundException &nfex) {
|
std::string value;
|
||||||
// triggered if setting is missing in config
|
if (std::getline(is_line, value))
|
||||||
if (!(std::strcmp("telegramApiKey", nfex.getPath()) == 0 || std::strcmp("chatId", nfex.getPath()) == 0)) {
|
setParam(key, value);
|
||||||
std::cerr << "No '" << nfex.getPath() << "' setting in configuration file." << std::endl;
|
|
||||||
} else {
|
|
||||||
Logger::message("no Telegram support - fields in config not set");
|
|
||||||
telegramSupport = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close the file
|
||||||
|
MyReadFile.close();
|
||||||
|
|
||||||
// check if needed values aren't empty
|
// check if needed values aren't empty
|
||||||
return !(Config::dynuapikey.empty() || Config::domainid.empty() || Config::domainname.empty());
|
return !(Config::dynuapikey.empty() || Config::domainid.empty() || Config::domainname.empty());
|
||||||
}
|
}
|
||||||
@ -97,51 +56,51 @@ bool Config::saveConfig() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Config::validateConfig() {
|
bool Config::validateConfig() {
|
||||||
libconfig::Config cfg;
|
// libconfig::Config cfg;
|
||||||
try {
|
// try {
|
||||||
Logger::message("reading config file: " + std::string(StaticData::ConfigDir + StaticData::ConfName));
|
// Logger::message("reading config file: " + std::string(StaticData::ConfigDir + StaticData::ConfName));
|
||||||
cfg.readFile(std::string(StaticData::ConfigDir + StaticData::ConfName).c_str());
|
// cfg.readFile(std::string(StaticData::ConfigDir + StaticData::ConfName).c_str());
|
||||||
}
|
// }
|
||||||
catch (const libconfig::FileIOException &fioex) {
|
// catch (const libconfig::FileIOException &fioex) {
|
||||||
Logger::warning("config file doesn't exist or permission denied!");
|
// Logger::warning("config file doesn't exist or permission denied!");
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
catch (const libconfig::ParseException &pex) {
|
// catch (const libconfig::ParseException &pex) {
|
||||||
std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
|
// std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
|
||||||
<< " - " << pex.getError() << std::endl;
|
// << " - " << pex.getError() << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
Logger::message("Syntax and Permission is OK");
|
// Logger::message("Syntax and Permission is OK");
|
||||||
|
//
|
||||||
try {
|
// try {
|
||||||
// needed parameters
|
// // needed parameters
|
||||||
if (((std::string) cfg.lookup("dynuapikey")).empty()) {
|
// if (((std::string) cfg.lookup("dynuapikey")).empty()) {
|
||||||
Logger::warning("required parameter \"dynuapikey\" seems to be empty.");
|
// Logger::warning("required parameter \"dynuapikey\" seems to be empty.");
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
if (((std::string) cfg.lookup("domainid")).empty()) {
|
// if (((std::string) cfg.lookup("domainid")).empty()) {
|
||||||
Logger::warning("required parameter \"domainid\" seems to be empty.");
|
// Logger::warning("required parameter \"domainid\" seems to be empty.");
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
if (((std::string) cfg.lookup("domainname")).empty()) {
|
// if (((std::string) cfg.lookup("domainname")).empty()) {
|
||||||
Logger::warning("required parameter \"domainname\" seems to be empty.");
|
// Logger::warning("required parameter \"domainname\" seems to be empty.");
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
// optional parameters
|
// // optional parameters
|
||||||
cfg.lookup("telegramApiKey");
|
// cfg.lookup("telegramApiKey");
|
||||||
cfg.lookup("chatId");
|
// cfg.lookup("chatId");
|
||||||
telegramSupport = true;
|
// telegramSupport = true;
|
||||||
}
|
// }
|
||||||
catch (const libconfig::SettingNotFoundException &nfex) {
|
// catch (const libconfig::SettingNotFoundException &nfex) {
|
||||||
// triggered if setting is missing in config
|
// // triggered if setting is missing in config
|
||||||
if (!(std::strcmp("telegramApiKey", nfex.getPath()) == 0 || std::strcmp("chatId", nfex.getPath()) == 0)) {
|
// if (!(std::strcmp("telegramApiKey", nfex.getPath()) == 0 || std::strcmp("chatId", nfex.getPath()) == 0)) {
|
||||||
std::cerr << "No '" << nfex.getPath() << "' setting in configuration file." << std::endl;
|
// std::cerr << "No '" << nfex.getPath() << "' setting in configuration file." << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
} else {
|
// } else {
|
||||||
Logger::message("no Telegram support - fields in config not set");
|
// Logger::message("no Telegram support - fields in config not set");
|
||||||
telegramSupport = false;
|
// telegramSupport = false;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,8 +134,66 @@ void Config::setValues(const std::string &domainname, const std::string &dynuapi
|
|||||||
Config::domainid = domainid;
|
Config::domainid = domainid;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Config::setValues(const std::string &domainname, const std::string &dynuapikey, const std::string &domainid, const std::string &telegramApiKey, const std::string &chatId) {
|
void Config::setValues(const std::string &domainname, const std::string &dynuapikey, const std::string &domainid,
|
||||||
|
const std::string &telegramApiKey, const std::string &chatId) {
|
||||||
setValues(domainname, dynuapikey, domainid);
|
setValues(domainname, dynuapikey, domainid);
|
||||||
Config::telegramApiKey = telegramApiKey;
|
Config::telegramApiKey = telegramApiKey;
|
||||||
Config::chatId = chatId;
|
Config::chatId = chatId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Config::writeDefaultConfig() {
|
||||||
|
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(StaticData::ConfigDir + StaticData::ConfName);
|
||||||
|
if (myfile.is_open()) {
|
||||||
|
myfile << StaticData::SAMPLECONFIG;
|
||||||
|
myfile.close();
|
||||||
|
} else {
|
||||||
|
Logger::error("error creating file");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Config::setParam(const std::string key, const std::string value) {
|
||||||
|
if (key == "dynuapikey")
|
||||||
|
dynuapikey = value;
|
||||||
|
else if (key == "domainid")
|
||||||
|
domainid = value;
|
||||||
|
else if (key == "domainname")
|
||||||
|
domainname = value;
|
||||||
|
else if (key == "telegramApiKey")
|
||||||
|
telegramApiKey = value;
|
||||||
|
else if (key == "chatId")
|
||||||
|
chatId = value;
|
||||||
|
else
|
||||||
|
Logger::warning("unkown key in config file: " + key);
|
||||||
|
|
||||||
|
if (!telegramApiKey.empty())
|
||||||
|
telegramSupport = true;
|
||||||
|
}
|
||||||
|
61
src/main.cpp
61
src/main.cpp
@ -1,6 +1,6 @@
|
|||||||
#include "StaticData.h"
|
#include <CMDParser.h>
|
||||||
|
#include <Logger.h>
|
||||||
#include "IPRefresher.h"
|
#include "IPRefresher.h"
|
||||||
#include "Logger.h"
|
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
#include "api/IPAPI.h"
|
#include "api/IPAPI.h"
|
||||||
|
|
||||||
@ -8,49 +8,26 @@
|
|||||||
* application entry point
|
* application entry point
|
||||||
*/
|
*/
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc > 1) {
|
CMDParser parser;
|
||||||
std::string firstarg(argv[1]);
|
CMDParser::Arguments *args = parser.parseArguments(argc, argv);
|
||||||
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
|
|
||||||
<< "[-f] [--force] force refresh of ip" << std::endl
|
|
||||||
<< "[-l] [--loop] infinite loop to refresh ip every five minutes" << std::endl
|
|
||||||
<< "[-c] [--checkconfig] validate configuration" << std::endl
|
|
||||||
<< "[-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 " << StaticData::VERSION << std::endl;
|
|
||||||
} else if (firstarg == "-f" || firstarg == "--force") {
|
|
||||||
if (Config::readConfig()) {
|
|
||||||
IPRefresher::checkIPAdress(true);
|
|
||||||
} else {
|
|
||||||
std::cout << "incorrect credentials!" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (firstarg == "-l" || firstarg == "--loop") {
|
if (args->currentIP) {
|
||||||
IPRefresher::startUpService(true);
|
IPAPI ipapi;
|
||||||
} else if (firstarg == "-c" || firstarg == "--checkconfig") {
|
const std::string ip = ipapi.getGlobalIp();
|
||||||
if (Config::validateConfig()) {
|
|
||||||
Logger::message("Config file is OK");
|
|
||||||
} else {
|
|
||||||
Logger::error("There are errors in config file!");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
} else if (firstarg == "-ip" || firstarg == "--currentip") {
|
|
||||||
IPAPI ipapi;
|
|
||||||
const std::string ip = ipapi.getGlobalIp();
|
|
||||||
|
|
||||||
std::cout << "Current global IP: " << ip << std::endl;
|
std::cout << "Current global IP: " << ip << std::endl;
|
||||||
} else {
|
return 0;
|
||||||
Logger::message("wrong arguments! -h for help");
|
}
|
||||||
}
|
|
||||||
|
// loop mode
|
||||||
|
if (args->loop)
|
||||||
|
IPRefresher::startUpService(true);
|
||||||
|
|
||||||
|
// default mode
|
||||||
|
if (Config::readConfig()) {
|
||||||
|
IPRefresher::checkIPAdress(args->force);
|
||||||
} else {
|
} else {
|
||||||
Logger::message("starting check");
|
Logger::error("incorrect credentials!");
|
||||||
if (Config::readConfig()) {
|
|
||||||
IPRefresher::checkIPAdress(false);
|
|
||||||
} else {
|
|
||||||
std::cout << "incorrect credentials!" << std::endl;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user