Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
cc8c2b0497 | |||
8eae88edfe | |||
0d00ab10cf | |||
8e7c01c5ed | |||
eadbfe90fd | |||
6a3f1dc7a5 | |||
eecbc755ce | |||
133b21e1bd | |||
eeaf67906f | |||
5012cc517e | |||
314795d70d | |||
d91a52c6ba | |||
436051d641 | |||
b68982345d | |||
f325c3a371 | |||
700d7912df | |||
52cc5f204b | |||
904cd2e96a | |||
c5b5029cb1 | |||
07ce161155 |
2
.idea/.gitignore
generated
vendored
Normal file
2
.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
# Default ignored files
|
||||
/workspace.xml
|
148
CMakeLists.txt
148
CMakeLists.txt
@ -1,28 +1,140 @@
|
||||
cmake_minimum_required(VERSION 3.7)
|
||||
project(iprefresher)
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
project(iprefresher VERSION 1.2.0 DESCRIPTION "Dynu ip refresher")
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
set(CMAKE_BUILD_TYPE Release) # manually set build type (Release / Debug)
|
||||
set(libmethod STATIC) #SHARED / STATIC
|
||||
set(WinBuild false)
|
||||
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
|
||||
#set up toolchain
|
||||
|
||||
if (${WinBuild})
|
||||
set(LIBSUFFIX .dll)
|
||||
set(SUFFIX .exe)
|
||||
|
||||
set(CMAKE_SYSTEM_NAME Windows)
|
||||
set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)
|
||||
#set(TOOLCHAIN_PREFIX i686-w64-mingw32)
|
||||
|
||||
# cross compilers to use for C and C++
|
||||
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
|
||||
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
|
||||
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)
|
||||
|
||||
# target environment on the build host system
|
||||
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX}/sys-root/mingw /usr/lib/gcc/${TOOLCHAIN_PREFIX}/8.3.0)
|
||||
|
||||
|
||||
# modify default behavior of FIND_XXX() commands to
|
||||
# search for headers/libs in the target environment and
|
||||
# search for programs in the build host environment
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
|
||||
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
|
||||
|
||||
# set(CMAKE_CXX_STANDARD_LIBRARIES -lcurl -lpthread -static-libgcc -static-libstdc++ -lcrypto -lssl -lws2_32 -std=c++11 -static -DCURL_STATICLIB)
|
||||
set(CMAKE_CXX_STANDARD_LIBRARIES "-static-libgcc -static-libstdc++ -lcurl -lpthread -lstdc++ -lwsock32 -lws2_32 ${CMAKE_CSS_STANDARD_LIBRARIES}")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static -Wl,-Bstatic,--whole-archive -Wl,--no-whole-archive")
|
||||
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
|
||||
# set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS)
|
||||
add_definitions(-DCURL_STATICLIB)
|
||||
else ()
|
||||
set(LIBSUFFIX .so)
|
||||
set(SUFFIX "")
|
||||
endif ()
|
||||
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
|
||||
find_package(CURL REQUIRED)
|
||||
include_directories(${CURL_INCLUDE_DIR})
|
||||
if (CURL_FOUND)
|
||||
message(STATUS "Found CURL version: ${CURL_VERSION_STRING}")
|
||||
message(STATUS "Using CURL include dir(s): ${CURL_INCLUDE_DIRS}")
|
||||
message(STATUS "Using CURL lib(s): ${CURL_LIBRARIES}")
|
||||
# include_directories(${CURL_INCLUDE_DIRS})
|
||||
else ()
|
||||
message(FATAL_ERROR "Could not find CURLLL")
|
||||
endif ()
|
||||
include_directories(${CURL_INCLUDE_DIR} inc)
|
||||
|
||||
add_library(api ${libmethod}
|
||||
src/api/API.cpp
|
||||
|
||||
src/api/TelegramAPI.cpp
|
||||
|
||||
src/api/DynuAPI.cpp
|
||||
|
||||
src/api/IPAPI.cpp
|
||||
)
|
||||
|
||||
add_library(logger ${libmethod}
|
||||
src/Logger.cpp
|
||||
)
|
||||
|
||||
|
||||
set(SOURCE
|
||||
src/main.cpp
|
||||
|
||||
src/api/API.h
|
||||
src/api/API.cpp
|
||||
|
||||
src/api/Hashmap.h
|
||||
|
||||
src/Logger.cpp
|
||||
src/Logger.h
|
||||
|
||||
src/api/TelegramAPI.cpp
|
||||
src/api/TelegramAPI.h
|
||||
)
|
||||
src/IPRefresher.cpp inc/IPRefresher.h)
|
||||
|
||||
add_executable(iprefresher ${SOURCE})
|
||||
target_link_libraries(iprefresher ${CURL_LIBRARIES})
|
||||
|
||||
install (TARGETS iprefresher DESTINATION bin)
|
||||
# LINK generated LIBS #
|
||||
target_link_libraries(iprefresher api logger ${CURL_LIBRARIES})
|
||||
|
||||
# INSTALL to SYSTEM #
|
||||
install(TARGETS iprefresher DESTINATION bin)
|
||||
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/iprefresher.service DESTINATION /lib/systemd/system)
|
||||
|
||||
|
||||
IF (UNIX)
|
||||
SET(CPACK_DEB_COMPONENT_INSTALL 1)
|
||||
SET(CPACK_OUTPUT_FILE_PREFIX packages)
|
||||
|
||||
FIND_PROGRAM(RPMBUILD_EXECUTABLE rpmbuild)
|
||||
FIND_PROGRAM(DEB_EXECUTABLE dpkg)
|
||||
|
||||
SET(CPACK_GENERATOR "TGZ;TBZ2")
|
||||
|
||||
#check if rpm build is possible
|
||||
if (NOT ${RPMBUILD_EXECUTABLE} STREQUAL "RPMBUILD_EXECUTABLE-NOTFOUND")
|
||||
message(STATUS "found rpm build executeable --> able to build rpm")
|
||||
set(CPACK_GENERATOR "${CPACK_GENERATOR};RPM")
|
||||
set(CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION "/usr/lib/systemd/system" "/usr/lib/systemd") # --> needed to not override existing folders
|
||||
else (NOT ${RPMBUILD_EXECUTABLE} STREQUAL "RPMBUILD_EXECUTABLE-NOTFOUND")
|
||||
message(STATUS "not found rpm build tools --> not building rpm")
|
||||
endif (NOT ${RPMBUILD_EXECUTABLE} STREQUAL "RPMBUILD_EXECUTABLE-NOTFOUND")
|
||||
|
||||
#check if deb build is possible
|
||||
if (NOT ${DEB_EXECUTABLE} STREQUAL "DEB_EXECUTABLE-NOTFOUND")
|
||||
message(STATUS "found deb build tools --> able to build deb")
|
||||
set(CPACK_GENERATOR "${CPACK_GENERATOR};DEB")
|
||||
else (NOT ${DEB_EXECUTABLE} STREQUAL "DEB_EXECUTABLE-NOTFOUND")
|
||||
message(STATUS "not found deb build tools --> not building deb")
|
||||
endif (NOT ${DEB_EXECUTABLE} STREQUAL "DEB_EXECUTABLE-NOTFOUND")
|
||||
|
||||
|
||||
SET(CPACK_CMAKE_GENERATOR "Unix Makefiles")
|
||||
SET(CPACK_SOURCE_GENERATOR "TGZ;TBZ2")
|
||||
|
||||
SET(CPACK_PACKAGE_DESCRIPTION "IPrefresher to refresh Dynu ip address and notify user via Telegram")
|
||||
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "IPrefresher to refresh Dynu ip address and notify user via Telegram")
|
||||
SET(CPACK_PACKAGE_VENDOR "Lukas Heilgienbrunner")
|
||||
SET(CPACK_PACKAGE_VERSION_MAJOR "1")
|
||||
SET(CPACK_PACKAGE_VERSION_MINOR "2")
|
||||
SET(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}")
|
||||
SET(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${CPACK_PACKAGE_VERSION}")
|
||||
SET(CPACK_PACKAGE_CONTACT "Lukas Heiligenbrunner <ultimategameingcookie@gmail.com>")
|
||||
SET(CPACK_PACKAGE_SECTION "games")
|
||||
set(CPACK_PACKAGE_DEPENDS "libcurl (>= 7.0.0-1)")
|
||||
|
||||
INCLUDE(CPack)
|
||||
|
||||
add_custom_target(build-linux-packages
|
||||
"${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --target package
|
||||
DEPENDS ${PROJECT_NAME}
|
||||
COMMENT "Installing ${PROJECT_NAME}")
|
||||
|
||||
ENDIF (UNIX)
|
86
README.md
86
README.md
@ -2,84 +2,44 @@
|
||||
|
||||
## Build
|
||||
## Basic Build
|
||||
Download source files
|
||||
### Download source files
|
||||
|
||||
`git clone https://github.com/Lukas-Heiligenbrunner/DynuIPRefresher.git`
|
||||
|
||||
### install build dependencies
|
||||
|
||||
`libcurl`
|
||||
`libcurl-devel`
|
||||
|
||||
Debian:
|
||||
`apt install libcurl4-openssl-dev`
|
||||
|
||||
Fedora:
|
||||
`dnf install libcurl-devel`
|
||||
|
||||
#### dependencies for package build
|
||||
`dpkg` --> debian/ubuntu package
|
||||
|
||||
`rpmbuild` --> Fedora/RedHat/CentOS package
|
||||
|
||||
### cmake project
|
||||
cd into downloaded files and Generate makefiles:
|
||||
|
||||
`cmake .`
|
||||
`cmake -S . -B build`
|
||||
|
||||
compile project
|
||||
### compile project
|
||||
|
||||
`make`
|
||||
|
||||
install it to the system
|
||||
[root] install it to the system
|
||||
|
||||
`make install`
|
||||
|
||||
### Build a Debian package
|
||||
Download source files
|
||||
[root] or create Linux packages
|
||||
|
||||
`git clone https://github.com/Lukas-Heiligenbrunner/DynuIPRefresher.git`
|
||||
`make package`
|
||||
|
||||
rename Project folder to lower case:
|
||||
|
||||
`mv DynuIPRefresher iprefresher-1.0`
|
||||
|
||||
`cd iprefresher-1.0`
|
||||
|
||||
remove .idea folder because it´s useless in this case:
|
||||
|
||||
`rm -Rf .idea`
|
||||
|
||||
pack source in a tar.gz archive
|
||||
|
||||
`tar -zcvf iprefresher-1.0.tar.gz *`
|
||||
|
||||
create debian/ folder and create example config files
|
||||
|
||||
`dh_make -f iprefresher-1.0.tar.gz`
|
||||
|
||||
recently packed archive is useless now, delete it
|
||||
|
||||
`rm iprefresher-1.0.tar.gz`
|
||||
|
||||
move to debian/ folder:
|
||||
|
||||
`cd debian`
|
||||
|
||||
remove useless files:
|
||||
|
||||
`rm *.ex *.EX README.Debian README.source `
|
||||
|
||||
edit control file
|
||||
|
||||
`rm control`
|
||||
|
||||
`nano control`
|
||||
|
||||
```
|
||||
Source: iprefresher
|
||||
Section: net
|
||||
Priority: optional
|
||||
Maintainer: Lukas Heiligenbrunner <lukas@unknown>
|
||||
Build-Depends: debhelper (>= 10)
|
||||
Standards-Version: 4.1.2
|
||||
|
||||
Package: iprefresher
|
||||
Architecture: any
|
||||
Depends: ${shlibs:Depends}, ${misc:Depends}
|
||||
Description: a dynu.net ip refresher written in c++
|
||||
a dynu.net ip refresher written in c++
|
||||
```
|
||||
return to main folder
|
||||
|
||||
`cd ..`
|
||||
|
||||
buld .deb package
|
||||
|
||||
`dpkg-buildpackage -us -uc `
|
||||
|
||||
### Windows cross build
|
||||
TODO!
|
||||
|
17
inc/IPRefresher.h
Normal file
17
inc/IPRefresher.h
Normal file
@ -0,0 +1,17 @@
|
||||
//
|
||||
// Created by lukas on 02.08.19.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
class IPRefresher {
|
||||
public:
|
||||
/**
|
||||
* refresh ip address on Dynu server
|
||||
*/
|
||||
void checkIPAdress(bool force);
|
||||
|
||||
IPRefresher();
|
||||
IPRefresher(bool loop);
|
||||
};
|
27
inc/Logger.h
Normal file
27
inc/Logger.h
Normal file
@ -0,0 +1,27 @@
|
||||
//
|
||||
// Created by lukas on 05.05.19.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
class Logger {
|
||||
public:
|
||||
/**
|
||||
* log messages to logfile
|
||||
* @param text message
|
||||
*/
|
||||
void logToLogfile(std::string text);
|
||||
|
||||
/**
|
||||
* safe ip to temp file
|
||||
* @param ip ip address to save
|
||||
*/
|
||||
void safeip(std::string ip);
|
||||
|
||||
/**
|
||||
* read ip from file
|
||||
* @return read ip
|
||||
*/
|
||||
std::string readip();
|
||||
|
||||
};
|
32
inc/api/API.h
Normal file
32
inc/api/API.h
Normal file
@ -0,0 +1,32 @@
|
||||
//
|
||||
// Created by lukas on 06.04.19.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
#include <string>
|
||||
#include "Hashmap.h"
|
||||
|
||||
class API {
|
||||
public:
|
||||
/**
|
||||
* Simple API get request
|
||||
* @param myurl api url
|
||||
* @return return string of server
|
||||
*/
|
||||
std::string request(std::string myurl);
|
||||
|
||||
/**
|
||||
* complex request with post/get and header information support
|
||||
* @param myurl base request url
|
||||
* @param post boolean (false=get)
|
||||
* @param map post/get fields
|
||||
* @param headers header fields
|
||||
* @return return string of server
|
||||
*/
|
||||
std::string request(std::string myurl, bool post, Hashmap<std::string, std::string> &map, std::vector<std::string> &headers);
|
||||
|
||||
private:
|
||||
static size_t write_data(void *buffer, size_t size, size_t buffersize, FILE *stream);
|
||||
};
|
23
inc/api/DynuAPI.h
Normal file
23
inc/api/DynuAPI.h
Normal file
@ -0,0 +1,23 @@
|
||||
//
|
||||
// Created by lukas on 18.06.19.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
#include "API.h"
|
||||
|
||||
class DynuAPI : API{
|
||||
public:
|
||||
/**
|
||||
* refresh the ip of domain on Dynu server
|
||||
* @param ip new ip
|
||||
* @return request status
|
||||
*/
|
||||
int refreshIp(std::string ip);
|
||||
private:
|
||||
const std::string dynuapikey = "88vNpMfDhMM2YYDNfWR1DNYfRX9W6fYg";
|
||||
|
||||
const std::string domainid = "8506047"; //id of the dynu domain
|
||||
const std::string domainname = "luki.dynu.net";
|
||||
};
|
@ -2,8 +2,7 @@
|
||||
// Created by lukas on 07.04.19.
|
||||
//
|
||||
|
||||
#ifndef QT5PROJECT_HASHMAP_H
|
||||
#define QT5PROJECT_HASHMAP_H
|
||||
#pragma once
|
||||
|
||||
|
||||
#include <iostream>
|
||||
@ -12,12 +11,31 @@
|
||||
template<class keytype, class valuetype>
|
||||
class Hashmap {
|
||||
public:
|
||||
/**
|
||||
* add key-value pair to hashmap
|
||||
* @param key keyvalue
|
||||
* @param value valuevalue
|
||||
*/
|
||||
void add(keytype key, keytype value);
|
||||
|
||||
/**
|
||||
* get key of specific position
|
||||
* @param position int position
|
||||
* @return responding key object
|
||||
*/
|
||||
keytype getKey(int position);
|
||||
|
||||
/**
|
||||
* get value of specific position
|
||||
* @param position int position
|
||||
* @return responding value object
|
||||
*/
|
||||
valuetype getValue(int position);
|
||||
|
||||
/**
|
||||
* get size of Hashmap
|
||||
* @return size of Hashmap
|
||||
*/
|
||||
int size();
|
||||
|
||||
private:
|
||||
@ -43,8 +61,5 @@ valuetype Hashmap<keytype, valuetype>::getValue(int position) {
|
||||
|
||||
template<class keytype, class valuetype>
|
||||
int Hashmap<keytype, valuetype>::size() {
|
||||
return (int)(keys.size());
|
||||
return (int) (keys.size());
|
||||
}
|
||||
|
||||
|
||||
#endif //QT5PROJECT_HASHMAP_H
|
17
inc/api/IPAPI.h
Normal file
17
inc/api/IPAPI.h
Normal file
@ -0,0 +1,17 @@
|
||||
//
|
||||
// Created by lukas on 18.06.19.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "API.h"
|
||||
|
||||
class IPAPI : API{
|
||||
public:
|
||||
/**
|
||||
* get global ip of current internet connection
|
||||
* @return global ip
|
||||
*/
|
||||
std::string getGlobalIp();
|
||||
};
|
29
inc/api/TelegramAPI.h
Normal file
29
inc/api/TelegramAPI.h
Normal file
@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by lukas on 08.05.19.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
#include <string>
|
||||
#include "API.h"
|
||||
|
||||
class TelegramAPI : API {
|
||||
public:
|
||||
/**
|
||||
* send telegram Message to predefined destination
|
||||
* @param text message
|
||||
*/
|
||||
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:
|
||||
std::string apikey;
|
||||
std::string chatid;
|
||||
};
|
12
iprefresher.service
Normal file
12
iprefresher.service
Normal file
@ -0,0 +1,12 @@
|
||||
[Unit]
|
||||
Description=IP Refresher
|
||||
After=network.target
|
||||
[Service]
|
||||
ExecStart=iprefresher -l
|
||||
WorkingDirectory=/root
|
||||
StandardOutput=inherit
|
||||
StandardError=inherit
|
||||
Restart=always
|
||||
User=root
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
62
src/IPRefresher.cpp
Normal file
62
src/IPRefresher.cpp
Normal file
@ -0,0 +1,62 @@
|
||||
//
|
||||
// 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 <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include "IPRefresher.h"
|
||||
|
||||
void IPRefresher::checkIPAdress(bool force) {
|
||||
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 && !force) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
IPRefresher::IPRefresher() {
|
||||
|
||||
}
|
||||
|
||||
IPRefresher::IPRefresher(bool loop) {
|
||||
while(true){
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(300000));
|
||||
checkIPAdress(false);
|
||||
}
|
||||
}
|
20
src/Logger.h
20
src/Logger.h
@ -1,20 +0,0 @@
|
||||
//
|
||||
// Created by lukas on 05.05.19.
|
||||
//
|
||||
|
||||
#ifndef IPREFRESHER_LOGGER_H
|
||||
#define IPREFRESHER_LOGGER_H
|
||||
|
||||
|
||||
class Logger {
|
||||
public:
|
||||
void logToLogfile(std::string text);
|
||||
|
||||
void safeip(std::string ip);
|
||||
|
||||
std::string readip();
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //IPREFRESHER_LOGGER_H
|
@ -2,8 +2,8 @@
|
||||
// Created by lukas on 06.04.19.
|
||||
//
|
||||
|
||||
#include "API.h"
|
||||
#include "Hashmap.h"
|
||||
#include "api/API.h"
|
||||
#include "api/Hashmap.h"
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
@ -26,7 +26,7 @@ API::request(std::string myurl, bool post, Hashmap<std::string, std::string> &ma
|
||||
|
||||
struct curl_slist *list = nullptr;
|
||||
for (int j = 0; j < headers.size(); ++j) {
|
||||
list = curl_slist_append(list, headers.at((unsigned long)(j)).c_str());
|
||||
list = curl_slist_append(list, headers.at((unsigned long) (j)).c_str());
|
||||
}
|
||||
|
||||
std::string readString;
|
||||
|
@ -1,23 +0,0 @@
|
||||
//
|
||||
// Created by lukas on 06.04.19.
|
||||
//
|
||||
|
||||
#ifndef IPREFRESHER_API_H
|
||||
#define IPREFRESHER_API_H
|
||||
|
||||
|
||||
#include <string>
|
||||
#include "Hashmap.h"
|
||||
|
||||
class API {
|
||||
public:
|
||||
std::string request(std::string myurl);
|
||||
|
||||
std::string request(std::string myurl, bool post, Hashmap<std::string, std::string> &map, std::vector<std::string> &headers);
|
||||
|
||||
private:
|
||||
static size_t write_data(void *buffer, size_t size, size_t buffersize, FILE *stream);
|
||||
};
|
||||
|
||||
|
||||
#endif //IPREFRESHER_API_H
|
27
src/api/DynuAPI.cpp
Normal file
27
src/api/DynuAPI.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
//
|
||||
// Created by lukas on 18.06.19.
|
||||
//
|
||||
|
||||
#include "api/DynuAPI.h"
|
||||
|
||||
int DynuAPI::refreshIp(std:: string ip) {
|
||||
|
||||
Hashmap<std::string, std::string> args;
|
||||
args.add("name", domainname);
|
||||
args.add("ipv4Address", ip);
|
||||
|
||||
std::vector<std::string> headers;
|
||||
headers.push_back("accept: application/json");
|
||||
headers.push_back("User-Agent: Mozilla/5.0 (compatible; Rigor/1.0.0; http://rigor.com)");
|
||||
headers.push_back("API-Key: " + dynuapikey);
|
||||
|
||||
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}") {
|
||||
return -1;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
9
src/api/IPAPI.cpp
Normal file
9
src/api/IPAPI.cpp
Normal file
@ -0,0 +1,9 @@
|
||||
//
|
||||
// Created by lukas on 18.06.19.
|
||||
//
|
||||
|
||||
#include "api/IPAPI.h"
|
||||
|
||||
std::string IPAPI::getGlobalIp() {
|
||||
return request("https://api.ipify.org");
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
// Created by lukas on 08.05.19.
|
||||
//
|
||||
|
||||
#include "TelegramAPI.h"
|
||||
#include "api/TelegramAPI.h"
|
||||
|
||||
void TelegramAPI::sendMessage(std::string text) {
|
||||
Hashmap<std::string, std::string> args;
|
||||
@ -12,5 +12,10 @@ void TelegramAPI::sendMessage(std::string text) {
|
||||
std::vector<std::string> 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;
|
||||
}
|
||||
|
@ -1,22 +0,0 @@
|
||||
//
|
||||
// Created by lukas on 08.05.19.
|
||||
//
|
||||
|
||||
#ifndef IPREFRESHER_TELEGRAMAPI_H
|
||||
#define IPREFRESHER_TELEGRAMAPI_H
|
||||
|
||||
|
||||
#include <string>
|
||||
#include "API.h"
|
||||
|
||||
class TelegramAPI : API {
|
||||
public:
|
||||
void sendMessage(std::string text);
|
||||
|
||||
private:
|
||||
std::string apikey = "717213769:AAHan1nSXhUsxLJAN1Dv8Oc0z8wqwDdYPn4";
|
||||
std::string chatid = "618154204";
|
||||
};
|
||||
|
||||
|
||||
#endif //IPREFRESHER_TELEGRAMAPI_H
|
65
src/main.cpp
65
src/main.cpp
@ -1,10 +1,5 @@
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "api/API.h"
|
||||
#include "Logger.h"
|
||||
#include "api/TelegramAPI.h"
|
||||
#include <IPRefresher.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
@ -13,60 +8,24 @@ int main(int argc, char *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
|
||||
<< "[no argument] normal ip check and refresh" << std::endl;
|
||||
} else if (firstarg == "-v" || firstarg == "--version") {
|
||||
std::cout << "Version 1.0" << std::endl;
|
||||
std::cout << "Version 1.2" << std::endl;
|
||||
} else if (firstarg == "-f" || firstarg == "--force") {
|
||||
IPRefresher ipr;
|
||||
ipr.checkIPAdress(true);
|
||||
} else if (firstarg == "-l" || firstarg == "--loop") {
|
||||
IPRefresher ipr(true);
|
||||
ipr.checkIPAdress(false);
|
||||
} else {
|
||||
std::cout << "wrong arguments! -h for help" << std::endl;
|
||||
}
|
||||
} else {
|
||||
API api;
|
||||
Logger logger;
|
||||
IPRefresher ipr;
|
||||
ipr.checkIPAdress(false);
|
||||
|
||||
std::string ip = api.request("https://api.ipify.org");
|
||||
|
||||
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;
|
||||
|
||||
static std::string dynuapikey = "88vNpMfDhMM2YYDNfWR1DNYfRX9W6fYg";
|
||||
|
||||
static std::string domainid = "8506047"; //id of the dynu domain
|
||||
static std::string domainname = "luki.dynu.net";
|
||||
|
||||
Hashmap<std::string, std::string> args;
|
||||
args.add("name", domainname);
|
||||
args.add("ipv4Address", ip);
|
||||
|
||||
std::vector<std::string> headers;
|
||||
headers.push_back("accept: application/json");
|
||||
headers.push_back("User-Agent: Mozilla/5.0 (compatible; Rigor/1.0.0; http://rigor.com)");
|
||||
headers.push_back("API-Key: " + dynuapikey);
|
||||
|
||||
std::string dynurepl = api.request("https://api.dynu.com/v2/dns/" + domainid, true, args, headers);
|
||||
|
||||
std::cout << "[DEBUG] api reply:: " << dynurepl << std::endl;
|
||||
|
||||
if (dynurepl != "{\"statusCode\":200}") {
|
||||
logger.logToLogfile(" [ERROR] failed to write ip to dynu api!");
|
||||
} else {
|
||||
TelegramAPI tele;
|
||||
tele.sendMessage(oldip + " moved to " + ip);
|
||||
}
|
||||
|
||||
logger.safeip(ip);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
Reference in New Issue
Block a user