cdoc and some minor renames

This commit is contained in:
lukas 2020-04-30 19:37:11 +02:00
parent 53e59e9931
commit 78d07ac95b
15 changed files with 71 additions and 52 deletions

View File

@ -5,7 +5,7 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Release) # manually set build type (Release / Debug) set(CMAKE_BUILD_TYPE Release) # manually set build type (Release / Debug)
set(LIB_METHOD STATIC) #SHARED / STATIC set(LIB_METHOD STATIC) #SHARED / STATIC
set(PROJECT_VERSION 1.3.1-dev) set(PROJECT_VERSION 1.3.1)
option(BUILD_DOC "Build documentation" ON) option(BUILD_DOC "Build documentation" ON)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
@ -69,7 +69,7 @@ add_library(logger ${LIB_METHOD}
set(SOURCE set(SOURCE
src/main.cpp src/main.cpp
src/IPRefresher.cpp src/IPRefresher.cpp
src/Credentials.cpp) src/Config.cpp)
add_executable(iprefresher ${SOURCE}) add_executable(iprefresher ${SOURCE})

View File

@ -1,5 +1,16 @@
# DynuIPRefresher # DynuIPRefresher
A lightweight C++ application to setup a service for refreshing a dynamic IP to the Dynu servers. A lightweight C++ application to setup a service for refreshing a dynamic IP to the Dynu servers.
Dynu.com is a free DDns service and provides an API.
## Installation
Download the latest Release at [Release_Page](https://github.com/Lukas-Heiligenbrunner/DynuIPRefresher/releases).
I'm providing executables for Debian/Ubuntu (.deb) and RHEL/Debian (.rpm).
But you can still compile the code my your own (see build section).
## Configuration
There is a configuration file `/etc/iprefresher.cfg` where you have to specify the DYNU API key (get it from their homepage), the domainid and your domain.
Furthermore, you can optionally specify a Telegram API key and a Chat ID if you want to be notfied when your local ip changes.
## Build ## Build
## Basic Build ## Basic Build

View File

@ -6,7 +6,7 @@
#include <string> #include <string>
class Credentials { class Config {
public: public:
static std::string dynuapikey; static std::string dynuapikey;

View File

@ -11,6 +11,9 @@ public:
*/ */
void checkIPAdress(bool force); void checkIPAdress(bool force);
/**
* default constructor
*/
IPRefresher(); IPRefresher();
/** /**

View File

@ -15,8 +15,8 @@ public:
static void log(const std::string &message, int level); static void log(const std::string &message, int level);
static const int Debug; static const int Debug = 4;
static const int Message; static const int Message = 3;
static const int Warning; static const int Warning = 2;
static const int Error; static const int Error = 1;
}; };

View File

@ -21,9 +21,9 @@ public:
* @param domainId ID of domain received by Dynu * @param domainId ID of domain received by Dynu
* @param domainName domainname to refresh * @param domainName domainname to refresh
*/ */
void init(std::string dynuApiKey, std::string domainId, std::string domainName); void init(const std::string& dynuApiKey, const std::string& domainId, const std::string& domainName);
private: private:
std::string dynuapikey; std::string dynuapikey; // Dynu API key
std::string domainid; //id of the dynu domain std::string domainid; //id of the dynu domain
std::string domainname; std::string domainname;

View File

@ -4,7 +4,6 @@
#pragma once #pragma once
#include <iostream> #include <iostream>
#include <vector> #include <vector>

View File

@ -14,7 +14,7 @@ public:
* send telegram Message to predefined destination * send telegram Message to predefined destination
* @param text message * @param text message
*/ */
void sendMessage(std::string text); int sendMessage(const std::string& text);
/** /**
* init Telegram api with apikey and chatid * init Telegram api with apikey and chatid

View File

@ -2,20 +2,20 @@
// Created by lukas on 11.02.20. // Created by lukas on 11.02.20.
// //
#include <Credentials.h> #include <Config.h>
#include <iostream> #include <iostream>
#include <cstring> #include <cstring>
#include "libconfig.h++" #include "libconfig.h++"
std::string Credentials::dynuapikey; std::string Config::dynuapikey;
std::string Credentials::domainid; //id of the dynu domain std::string Config::domainid; //id of the dynu domain
std::string Credentials::domainname; std::string Config::domainname;
std::string Credentials::telegramApiKey; std::string Config::telegramApiKey;
std::string Credentials::chatId; std::string Config::chatId;
bool Credentials::readCredentials() { bool Config::readCredentials() {
libconfig::Config cfg; libconfig::Config cfg;
try { try {
cfg.readFile("/etc/iprefresher.cfg"); cfg.readFile("/etc/iprefresher.cfg");
@ -40,7 +40,6 @@ bool Credentials::readCredentials() {
// optional parameters // optional parameters
telegramApiKey = (std::string) cfg.lookup("telegramApiKey"); telegramApiKey = (std::string) cfg.lookup("telegramApiKey");
chatId = (std::string) cfg.lookup("chatId"); chatId = (std::string) cfg.lookup("chatId");
std::cout << "Store name: " << dynuapikey << std::endl;
} }
catch (const libconfig::SettingNotFoundException &nfex) { catch (const libconfig::SettingNotFoundException &nfex) {
// triggered if setting is missing in config // triggered if setting is missing in config
@ -49,5 +48,5 @@ bool Credentials::readCredentials() {
} }
} }
// check if needed values aren't empty // 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());
} }

View File

@ -14,7 +14,7 @@
#include <Logger.h> #include <Logger.h>
#include <IPRefresher.h> #include <IPRefresher.h>
#include <Credentials.h> #include <Config.h>
void IPRefresher::checkIPAdress(bool force) { void IPRefresher::checkIPAdress(bool force) {
FileLogger logger; FileLogger logger;
@ -34,11 +34,11 @@ void IPRefresher::checkIPAdress(bool force) {
Logger::message("ip changed! -- from :" + oldip + "to: " + ip); Logger::message("ip changed! -- from :" + oldip + "to: " + ip);
DynuAPI dynu; DynuAPI dynu;
dynu.init(Credentials::dynuapikey, Credentials::domainid, Credentials::domainname); dynu.init(Config::dynuapikey, Config::domainid, Config::domainname);
if (dynu.refreshIp(ip)) { if (dynu.refreshIp(ip)) {
TelegramAPI tele; TelegramAPI tele;
tele.init(Credentials::telegramApiKey, Credentials::chatId); tele.init(Config::telegramApiKey, Config::chatId);
tele.sendMessage(oldip + " moved to " + ip); tele.sendMessage(oldip + " moved to " + ip);
} else { } else {
//error //error
@ -53,9 +53,10 @@ void IPRefresher::checkIPAdress(bool force) {
IPRefresher::IPRefresher() = default; IPRefresher::IPRefresher() = default;
IPRefresher::IPRefresher(bool loop) { IPRefresher::IPRefresher(bool loop) {
if (Credentials::readCredentials()) { if (loop) {
Logger::message("startup of service"); Logger::message("startup of service");
while (loop) { if (Config::readCredentials()) {
while (true) {
Logger::message("starting check"); Logger::message("starting check");
checkIPAdress(false); checkIPAdress(false);
std::this_thread::sleep_for(std::chrono::milliseconds(300000)); std::this_thread::sleep_for(std::chrono::milliseconds(300000));
@ -64,3 +65,4 @@ IPRefresher::IPRefresher(bool loop) {
std::cout << "incorrect credentials!" << std::endl; std::cout << "incorrect credentials!" << std::endl;
} }
} }
}

View File

@ -7,12 +7,6 @@
#include "Logger.h" #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) { void Logger::debug(const std::string message) {
log(message, Logger::Debug); log(message, Logger::Debug);
} }

View File

@ -5,19 +5,16 @@
#include "api/DynuAPI.h" #include "api/DynuAPI.h"
int DynuAPI::refreshIp(std:: string ip) { int DynuAPI::refreshIp(std:: string ip) {
Hashmap<std::string, std::string> args; Hashmap<std::string, std::string> args;
args.add("name", domainname); args.add("name", domainname);
args.add("ipv4Address", ip); args.add("ipv4Address", ip);
std::vector<std::string> headers; std::vector<std::string> headers;
headers.push_back("accept: application/json"); headers.emplace_back("accept: application/json");
headers.push_back("User-Agent: Mozilla/5.0 (compatible; Rigor/1.0.0; http://rigor.com)"); headers.emplace_back("User-Agent: Mozilla/5.0 (compatible; Rigor/1.0.0; http://rigor.com)");
headers.push_back("API-Key: " + dynuapikey); headers.emplace_back("API-Key: " + dynuapikey);
std::string dynurepl = request("https://api.dynu.com/v2/dns/" + domainid, true, args, headers); const std::string dynurepl = request("https://api.dynu.com/v2/dns/" + domainid, true, args, headers);
// std::cout << "[DEBUG] api reply:: " << dynurepl << std::endl;
if (dynurepl != "{\"statusCode\":200}") { if (dynurepl != "{\"statusCode\":200}") {
return -1; 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->dynuapikey=dynuApiKey;
this->domainid=domainId; this->domainid=domainId;
this->domainname=domainName; this->domainname=domainName;

View File

@ -4,7 +4,9 @@
#include "api/TelegramAPI.h" #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; Hashmap<std::string, std::string> args;
args.add("chat_id", chatid); args.add("chat_id", chatid);
args.add("text", text); args.add("text", text);
@ -12,10 +14,16 @@ void TelegramAPI::sendMessage(std::string text) {
std::vector<std::string> headers; std::vector<std::string> headers;
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;
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->apikey = apikey;
this->chatid = chatid; this->chatid = chatid;
} }

View File

@ -1,9 +1,16 @@
//
// Created by lukas on 18.06.19.
//
#include <iostream> #include <iostream>
#include <IPRefresher.h> #include <IPRefresher.h>
#include <Version.h> #include <Version.h>
#include <Logger.h> #include <Logger.h>
#include <Credentials.h> #include <Config.h>
/**
* application entry point
*/
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc > 1) { if (argc > 1) {
std::string firstarg(argv[1]); std::string firstarg(argv[1]);
@ -17,7 +24,7 @@ int main(int argc, char *argv[]) {
std::cout << "Version " << Version::VERSION << std::endl; std::cout << "Version " << Version::VERSION << std::endl;
} else if (firstarg == "-f" || firstarg == "--force") { } else if (firstarg == "-f" || firstarg == "--force") {
IPRefresher ipr; IPRefresher ipr;
if (Credentials::readCredentials()) { if (Config::readCredentials()) {
ipr.checkIPAdress(true); ipr.checkIPAdress(true);
} else { } else {
std::cout << "incorrect credentials!" << std::endl; std::cout << "incorrect credentials!" << std::endl;
@ -31,12 +38,11 @@ int main(int argc, char *argv[]) {
} else { } else {
IPRefresher ipr; IPRefresher ipr;
Logger::message("starting check"); Logger::message("starting check");
if (Credentials::readCredentials()) { if (Config::readCredentials()) {
ipr.checkIPAdress(false); ipr.checkIPAdress(false);
} else { } else {
std::cout << "incorrect credentials!" << std::endl; std::cout << "incorrect credentials!" << std::endl;
} }
} }
return 0; return 0;