Custom 25565 port page (#5369)

+ Add a custom response when an HTTP request comes in to port 25565...

Co-authored-by: Alexander Harkness <me@bearbin.net>
This commit is contained in:
Rorkh 2022-01-27 05:56:45 +05:00 committed by GitHub
parent fa92f5294f
commit 5fe1fe899b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 0 deletions

View File

@ -90,6 +90,11 @@ void cMultiVersionProtocol::HandleIncomingDataInRecognitionStage(cClientHandle &
return; return;
} }
if (TryHandleHTTPRequest(a_Client, a_Data))
{
return;
}
// TODO: recover from git history // TODO: recover from git history
// Unlengthed protocol, ... // Unlengthed protocol, ...
@ -238,6 +243,37 @@ void cMultiVersionProtocol::SendDisconnect(cClientHandle & a_Client, const AStri
bool cMultiVersionProtocol::TryHandleHTTPRequest(cClientHandle & a_Client, ContiguousByteBuffer & a_Data)
{
const auto RedirectUrl = cRoot::Get()->GetServer()->GetCustomRedirectUrl();
if (RedirectUrl.empty())
{
return false;
}
ContiguousByteBuffer Buffer;
m_Buffer.ReadSome(Buffer, 10U);
m_Buffer.ResetRead();
// The request line, hastily decoded with the hope that it's encoded in US-ASCII.
const std::string_view Value(reinterpret_cast<const char *>(Buffer.data()), Buffer.size());
if (Value == u8"GET / HTTP")
{
const auto Response = fmt::format(u8"HTTP/1.0 303 See Other\r\nLocation: {}\r\n\r\n", cRoot::Get()->GetServer()->GetCustomRedirectUrl());
a_Client.SendData({ reinterpret_cast<const std::byte *>(Response.data()), Response.size() });
a_Client.Destroy();
return true;
}
return false;
}
std::unique_ptr<cProtocol> cMultiVersionProtocol::TryRecognizeLengthedProtocol(cClientHandle & a_Client) std::unique_ptr<cProtocol> cMultiVersionProtocol::TryRecognizeLengthedProtocol(cClientHandle & a_Client)
{ {
UInt32 PacketType; UInt32 PacketType;

View File

@ -59,6 +59,9 @@ private:
/** Handles and responds to unsupported clients sending pings. */ /** Handles and responds to unsupported clients sending pings. */
void HandleIncomingDataInOldPingResponseStage(cClientHandle & a_Client, ContiguousByteBufferView a_Data); void HandleIncomingDataInOldPingResponseStage(cClientHandle & a_Client, ContiguousByteBufferView a_Data);
/* Checks if incoming data is an HTTP request and handles it if it is. */
bool TryHandleHTTPRequest(cClientHandle & a_Client, ContiguousByteBuffer & a_Data);
/** Tries to recognize a protocol in the lengthed family (1.7+), based on m_Buffer. /** Tries to recognize a protocol in the lengthed family (1.7+), based on m_Buffer.
Returns a cProtocol_XXX instance if recognized. */ Returns a cProtocol_XXX instance if recognized. */
std::unique_ptr<cProtocol> TryRecognizeLengthedProtocol(cClientHandle & a_Client); std::unique_ptr<cProtocol> TryRecognizeLengthedProtocol(cClientHandle & a_Client);

View File

@ -158,6 +158,7 @@ bool cServer::InitServer(cSettingsRepositoryInterface & a_Settings, bool a_Shoul
m_bIsHardcore = a_Settings.GetValueSetB("Server", "HardcoreEnabled", false); m_bIsHardcore = a_Settings.GetValueSetB("Server", "HardcoreEnabled", false);
m_bAllowMultiLogin = a_Settings.GetValueSetB("Server", "AllowMultiLogin", false); m_bAllowMultiLogin = a_Settings.GetValueSetB("Server", "AllowMultiLogin", false);
m_ResourcePackUrl = a_Settings.GetValueSet("Server", "ResourcePackUrl", ""); m_ResourcePackUrl = a_Settings.GetValueSet("Server", "ResourcePackUrl", "");
m_CustomRedirectUrl = a_Settings.GetValueSet("Server", "CustomRedirectUrl", "https://youtu.be/dQw4w9WgXcQ");
m_FaviconData = Base64Encode(cFile::ReadWholeFile(AString("favicon.png"))); // Will return empty string if file nonexistant; client doesn't mind m_FaviconData = Base64Encode(cFile::ReadWholeFile(AString("favicon.png"))); // Will return empty string if file nonexistant; client doesn't mind

View File

@ -96,6 +96,8 @@ public:
const AString & GetResourcePackUrl(void) { return m_ResourcePackUrl; } const AString & GetResourcePackUrl(void) { return m_ResourcePackUrl; }
std::string_view GetCustomRedirectUrl(void) { return m_CustomRedirectUrl; }
bool Start(void); bool Start(void);
bool Command(cClientHandle & a_Client, AString & a_Cmd); bool Command(cClientHandle & a_Client, AString & a_Cmd);
@ -222,6 +224,7 @@ private:
size_t m_MaxPlayers; size_t m_MaxPlayers;
bool m_bIsHardcore; bool m_bIsHardcore;
AString m_ResourcePackUrl; AString m_ResourcePackUrl;
AString m_CustomRedirectUrl;
/** Map of protocol version to Forge mods (map of ModName -> ModVersionString) */ /** Map of protocol version to Forge mods (map of ModName -> ModVersionString) */
std::map<UInt32, AStringMap> m_ForgeModsByVersion; std::map<UInt32, AStringMap> m_ForgeModsByVersion;