1 parent
df99b6ea7f
commit
d146f11f02
63 files changed
+8331
-2709
No files matched your search
@@ -4,6 +4,7 @@
|
||||
#include <NetworkInterface.hpp>
|
||||
#include <format>
|
||||
#include <future>
|
||||
#include <mutex>
|
||||
#include <regex>
|
||||
|
||||
namespace Echoion::Core
|
||||
@@ -11,16 +12,23 @@ namespace Echoion::Core
|
||||
|
||||
std::string getPublicIpAddress()
|
||||
{
|
||||
// Try to get public IP from external services
|
||||
// Try to get public IP from external services. The cache is shared
|
||||
// across threads (this command can run concurrently), so guard it with a
|
||||
// mutex. The mutex is only held around cache reads/writes, never across
|
||||
// the blocking network calls below.
|
||||
static std::mutex cacheMutex;
|
||||
static std::string cachedIp;
|
||||
static std::chrono::steady_clock::time_point lastFetch;
|
||||
|
||||
// Cache IP for 5 minutes to avoid excessive API calls
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
if (!cachedIp.empty() &&
|
||||
std::chrono::duration_cast<std::chrono::minutes>(now - lastFetch)
|
||||
.count() < 5) {
|
||||
return cachedIp;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(cacheMutex);
|
||||
if (!cachedIp.empty() &&
|
||||
std::chrono::duration_cast<std::chrono::minutes>(now - lastFetch)
|
||||
.count() < 5) {
|
||||
return cachedIp;
|
||||
}
|
||||
}
|
||||
|
||||
// List of services to try
|
||||
@@ -55,6 +63,7 @@ namespace Echoion::Core
|
||||
std::regex ipRegex(
|
||||
R"(^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$)");
|
||||
if (std::regex_match(ip, ipRegex)) {
|
||||
std::lock_guard<std::mutex> lock(cacheMutex);
|
||||
cachedIp = ip;
|
||||
lastFetch = now;
|
||||
return cachedIp;
|
||||
@@ -67,6 +76,7 @@ namespace Echoion::Core
|
||||
}
|
||||
|
||||
// Return cached IP or localhost if all services failed
|
||||
std::lock_guard<std::mutex> lock(cacheMutex);
|
||||
return cachedIp.empty() ? "127.0.0.1" : cachedIp;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user