better error correction if ip syntax not correct
This commit is contained in:
parent
095eb5c0cd
commit
2d1e99260d
@ -16,7 +16,7 @@ public:
|
|||||||
* @param ip ip address to test
|
* @param ip ip address to test
|
||||||
* @return validity
|
* @return validity
|
||||||
*/
|
*/
|
||||||
static bool isIpValid(std::string ip);
|
static bool isIpValid(const std::string& ip);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
};
|
};
|
||||||
|
@ -23,8 +23,5 @@ std::string FileLogger::readip() {
|
|||||||
in >> ip;
|
in >> ip;
|
||||||
|
|
||||||
// when received ip has no . return 0.0.0.0
|
// when received ip has no . return 0.0.0.0
|
||||||
if (!IpHelper::isIpValid(ip))
|
return (IpHelper::isIpValid(ip) ? ip : "0.0.0.0");
|
||||||
return "0.0.0.0";
|
|
||||||
else
|
|
||||||
return ip;
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
|
#include <regex>
|
||||||
#include "IpHelper.h"
|
#include "IpHelper.h"
|
||||||
|
|
||||||
bool IpHelper::isIpValid(std::string ip) {
|
bool IpHelper::isIpValid(const std::string& ip) {
|
||||||
return (ip.find('.') != SIZE_MAX);
|
const std::regex rgx(R"(^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$)");
|
||||||
|
return (std::regex_match(ip, rgx));
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
|
#include <IpHelper.h>
|
||||||
|
#include <Logger.h>
|
||||||
#include "api/IPAPI.h"
|
#include "api/IPAPI.h"
|
||||||
|
|
||||||
std::string IPAPI::getGlobalIp() {
|
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;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user