Skip to content

Commit

Permalink
Merge pull request #786 from openmultiplayer/ksn/http_ipv4
Browse files Browse the repository at this point in the history
Add core->requestHTTP4. Force IPV4 requests for announce. Use bind address if provided
  • Loading branch information
AmyrAhmady authored Nov 27, 2023
2 parents 7825487 + c4e1ff8 commit f09bd5c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
7 changes: 7 additions & 0 deletions SDK/include/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Server/Components/LegacyNetwork/legacy_network_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand Down
21 changes: 19 additions & 2 deletions Server/Source/core_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -937,6 +945,9 @@ class HTTPAsyncIO
String url;
String data;

bool force_v4;
String bindAddr;

std::atomic_bool finished;
int response;
String body;
Expand Down Expand Up @@ -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);
}
};

0 comments on commit f09bd5c

Please sign in to comment.