Only send status response favicon to client versions that support it

This commit is contained in:
timvisee 2021-11-24 13:27:42 +01:00
parent 20fb6ee715
commit aebb5563e0
No known key found for this signature in database
GPG Key ID: B8DB720BC383E172
2 changed files with 24 additions and 7 deletions

View File

@ -1,3 +1,8 @@
use crate::proto::client::ClientInfo;
/// Protocol version since when favicons are supported.
const FAVICON_PROTOCOL_VERSION: i32 = 4;
/// Get default server status favicon.
pub fn default_favicon() -> String {
encode_favicon(include_bytes!("../../res/unknown_server_optimized.png"))
@ -9,3 +14,13 @@ pub fn default_favicon() -> String {
pub fn encode_favicon(data: &[u8]) -> String {
format!("{}{}", "data:image/png;base64,", base64::encode(data))
}
/// Check whether the status response favicon is supported based on the given client info.
///
/// Defaults to `true` if unsure.
pub fn supports_favicon(client_info: &ClientInfo) -> bool {
client_info
.protocol_version
.map(|p| p >= FAVICON_PROTOCOL_VERSION)
.unwrap_or(true)
}

View File

@ -99,7 +99,7 @@ pub async fn serve(
// Hijack server status packet
if client_state == ClientState::Status && packet.id == packets::status::SERVER_STATUS {
let server_status = server_status(&config, &server).await;
let server_status = server_status(&client_info, &config, &server).await;
let packet = StatusResponse { server_status };
let mut data = Vec::new();
@ -197,7 +197,7 @@ pub async fn serve(
}
/// Build server status object to respond to client with.
async fn server_status(config: &Config, server: &Server) -> ServerStatus {
async fn server_status(client_info: &ClientInfo, config: &Config, server: &Server) -> ServerStatus {
let status = server.status().await;
let server_state = server.state();
@ -233,12 +233,14 @@ async fn server_status(config: &Config, server: &Server) -> ServerStatus {
// Extract favicon from real server status, load from disk, or use default
let mut favicon = None;
if favicon::supports_favicon(client_info) {
if config.motd.from_server && status.is_some() {
favicon = status.as_ref().unwrap().favicon.clone()
}
if favicon.is_none() {
favicon = Some(server_favicon(&config).await);
}
}
// Build status resposne
ServerStatus {