better error correction if ip syntax not correct

This commit is contained in:
lukas 2021-02-01 18:33:36 +01:00
parent 095eb5c0cd
commit 2d1e99260d
4 changed files with 13 additions and 8 deletions

View File

@ -16,7 +16,7 @@ public:
* @param ip ip address to test
* @return validity
*/
static bool isIpValid(std::string ip);
static bool isIpValid(const std::string& ip);
private:
};

View File

@ -23,8 +23,5 @@ std::string FileLogger::readip() {
in >> ip;
// when received ip has no . return 0.0.0.0
if (!IpHelper::isIpValid(ip))
return "0.0.0.0";
else
return ip;
return (IpHelper::isIpValid(ip) ? ip : "0.0.0.0");
}

View File

@ -1,5 +1,7 @@
#include <regex>
#include "IpHelper.h"
bool IpHelper::isIpValid(std::string ip) {
return (ip.find('.') != SIZE_MAX);
bool IpHelper::isIpValid(const std::string& ip) {
const std::regex rgx(R"(^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$)");
return (std::regex_match(ip, rgx));
}

View File

@ -1,5 +1,11 @@
#include <IpHelper.h>
#include <Logger.h>
#include "api/IPAPI.h"
std::string IPAPI::getGlobalIp() {
return request("https://api.ipify.org");
const std::string ip = request("https://api.ipify.org");
if(!IpHelper::isIpValid(ip))
Logger::warning("no valid ip returned from ipapi");
return ip;
}