From c4e1ff862bdb583dbfced9dcf40aad0445860d64 Mon Sep 17 00:00:00 2001 From: kseny Date: Fri, 24 Nov 2023 22:50:30 +0200 Subject: [PATCH] Add core->requestHTTP4. Force IPV4 requests for annonce. Use bind address if provided. --- SDK/include/core.hpp | 7 +++++++ .../LegacyNetwork/legacy_network_impl.cpp | 2 +- Server/Source/core_impl.hpp | 21 +++++++++++++++++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/SDK/include/core.hpp b/SDK/include/core.hpp index 0e8cbc8be..212d3c7db 100644 --- a/SDK/include/core.hpp +++ b/SDK/include/core.hpp @@ -344,6 +344,13 @@ struct ICore : public IExtensible, public ILogger network->update(); } } + + /// Launch an HTTP request (IPV4) and read the response + /// @param handler The handler that will handle the response + /// @param type The request type + /// @param url The URL + /// @param[opt] data The POST data + virtual void requestHTTP4(HTTPResponseHandler* handler, HTTPRequestType type, StringView url, StringView data = StringView()) = 0; }; /// Helper class to get streamer config properties diff --git a/Server/Components/LegacyNetwork/legacy_network_impl.cpp b/Server/Components/LegacyNetwork/legacy_network_impl.cpp index ca5cab50c..290ecb067 100644 --- a/Server/Components/LegacyNetwork/legacy_network_impl.cpp +++ b/Server/Components/LegacyNetwork/legacy_network_impl.cpp @@ -859,7 +859,7 @@ void RakNetLegacyNetwork::start() if (*config.getBool("announce")) { const String get = "https://api.open.mp/0.3.7/announce/" + std::to_string(port); - core->requestHTTP(new AnnounceHTTPResponseHandler(core), HTTPRequestType::HTTPRequestType_Get, get.data()); + core->requestHTTP4(new AnnounceHTTPResponseHandler(core), HTTPRequestType::HTTPRequestType_Get, get.data()); } } diff --git a/Server/Source/core_impl.hpp b/Server/Source/core_impl.hpp index df3a5eb62..bc3851498 100644 --- a/Server/Source/core_impl.hpp +++ b/Server/Source/core_impl.hpp @@ -824,11 +824,13 @@ class Config final : public IEarlyConfig class HTTPAsyncIO { public: - HTTPAsyncIO(HTTPResponseHandler* handler, HTTPRequestType type, StringView url, StringView data) + HTTPAsyncIO(HTTPResponseHandler* handler, HTTPRequestType type, StringView url, StringView data, bool force_v4 = false, StringView bindAddr = "") : handler(handler) , type(type) , url(url) , data(data) + , force_v4(force_v4) + , bindAddr(bindAddr) , finished(false) , response(0) { @@ -903,6 +905,12 @@ class HTTPAsyncIO request.set_write_timeout(Seconds(5)); request.set_keep_alive(true); + if (params->force_v4) + request.set_address_family(AF_INET); + + if (!params->bindAddr.empty()) + request.set_interface(params->bindAddr); + // Run request httplib::Result res(nullptr, httplib::Error::Canceled); switch (type) @@ -937,6 +945,9 @@ class HTTPAsyncIO String url; String data; + bool force_v4; + String bindAddr; + std::atomic_bool finished; int response; String body; @@ -2091,4 +2102,10 @@ class Core final : public ICore, public PlayerConnectEventHandler, public Consol } return false; } -}; + + void requestHTTP4(HTTPResponseHandler* handler, HTTPRequestType type, StringView url, StringView data) override + { + HTTPAsyncIO* httpIO = new HTTPAsyncIO(handler, type, url, data, true, config.getString("network.bind")); + httpFutures.emplace(httpIO); + } +}; \ No newline at end of file