mirror of
https://github.com/italicsjenga/valence.git
synced 2025-01-11 07:11:30 +11:00
Clean up Config docs and add compression threshold (#148)
This commit is contained in:
parent
a885b949da
commit
ff136c2a4a
103
src/config.rs
103
src/config.rs
|
@ -85,9 +85,9 @@ pub trait Config: Sized + Send + Sync + 'static {
|
||||||
STANDARD_TPS
|
STANDARD_TPS
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Called once at startup to get the connection mode option, which
|
/// Called to get the connection mode option, which determines if client
|
||||||
/// determines if client authentication and encryption should take place
|
/// authentication and encryption should take place and if the server
|
||||||
/// and if the server should get the player data from a proxy.
|
/// should get the player data from a proxy.
|
||||||
///
|
///
|
||||||
/// # Default Implementation
|
/// # Default Implementation
|
||||||
///
|
///
|
||||||
|
@ -96,13 +96,62 @@ pub trait Config: Sized + Send + Sync + 'static {
|
||||||
ConnectionMode::Online
|
ConnectionMode::Online
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Called once at startup to get the "prevent-proxy-connections" option,
|
/// Obtains the compression threshold to use for compressing packets. For a
|
||||||
/// which determines if client IP validation should take place.
|
/// compression threshold of `Some(N)`, packets with encoded lengths >= `N`
|
||||||
///
|
/// are compressed while all others are not. `None` disables compression.
|
||||||
/// When prevent_proxy_connections is enabled, clients can no longer log-in
|
|
||||||
/// if they connected to the yggdrasil server using a different IP.
|
|
||||||
///
|
///
|
||||||
/// # Default Implementation
|
/// # Default Implementation
|
||||||
|
///
|
||||||
|
/// If the connection mode is [`ConnectionMode::Online`], `Some(256)` is
|
||||||
|
/// returned. Otherwise, compression is disabled.
|
||||||
|
fn compression_threshold(&self) -> Option<u32> {
|
||||||
|
match self.connection_mode() {
|
||||||
|
ConnectionMode::Online => Some(256),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Called upon every client login to obtain the full URL to use for session
|
||||||
|
/// server requests. This is done to authenticate player accounts. This
|
||||||
|
/// method is not called unless [online mode] is enabled.
|
||||||
|
///
|
||||||
|
/// It is assumed that upon successful request, a structure matching the
|
||||||
|
/// description in the [wiki](https://wiki.vg/Protocol_Encryption#Server) was obtained.
|
||||||
|
/// Providing a URL that does not return such a structure will result in a
|
||||||
|
/// disconnect for every client that connects.
|
||||||
|
///
|
||||||
|
/// The arguments are described in the linked wiki article.
|
||||||
|
///
|
||||||
|
/// # Default Implementation
|
||||||
|
///
|
||||||
|
/// Uses the official Minecraft session server. This is formatted as
|
||||||
|
/// `https://sessionserver.mojang.com/session/minecraft/hasJoined?username=<username>&serverId=<auth-digest>&ip=<player-ip>`.
|
||||||
|
///
|
||||||
|
/// [online mode]: crate::config::ConnectionMode::Online
|
||||||
|
fn session_server(
|
||||||
|
&self,
|
||||||
|
server: &SharedServer<Self>,
|
||||||
|
username: Username<&str>,
|
||||||
|
auth_digest: &str,
|
||||||
|
player_ip: &IpAddr,
|
||||||
|
) -> String {
|
||||||
|
if self.prevent_proxy_connections() {
|
||||||
|
format!("https://sessionserver.mojang.com/session/minecraft/hasJoined?username={username}&serverId={auth_digest}&ip={player_ip}")
|
||||||
|
} else {
|
||||||
|
format!("https://sessionserver.mojang.com/session/minecraft/hasJoined?username={username}&serverId={auth_digest}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Called from the default implementation of [`Config::session_server`] to
|
||||||
|
/// get the "prevent-proxy-connections" option, which determines if client
|
||||||
|
/// IP validation should take place.
|
||||||
|
///
|
||||||
|
/// When `prevent_proxy_connections` is enabled, clients can no longer
|
||||||
|
/// log-in if they connected to the yggdrasil server using a different
|
||||||
|
/// IP.
|
||||||
|
///
|
||||||
|
/// # Default Implementation
|
||||||
|
///
|
||||||
/// Proxy connections are allowed.
|
/// Proxy connections are allowed.
|
||||||
///
|
///
|
||||||
/// Returns `false`.
|
/// Returns `false`.
|
||||||
|
@ -119,8 +168,8 @@ pub trait Config: Sized + Send + Sync + 'static {
|
||||||
///
|
///
|
||||||
/// # Default Implementation
|
/// # Default Implementation
|
||||||
///
|
///
|
||||||
/// An unspecified value is returned that should be adequate in most
|
/// An unspecified value is returned that should be adequate for most
|
||||||
/// situations.
|
/// situations. This default may change in future versions.
|
||||||
fn incoming_capacity(&self) -> usize {
|
fn incoming_capacity(&self) -> usize {
|
||||||
MAX_PACKET_SIZE as usize
|
MAX_PACKET_SIZE as usize
|
||||||
}
|
}
|
||||||
|
@ -134,8 +183,8 @@ pub trait Config: Sized + Send + Sync + 'static {
|
||||||
///
|
///
|
||||||
/// # Default Implementation
|
/// # Default Implementation
|
||||||
///
|
///
|
||||||
/// An unspecified value is returned that should be adequate in most
|
/// An unspecified value is returned that should be adequate for most
|
||||||
/// situations.
|
/// situations. This default may change in future versions.
|
||||||
fn outgoing_capacity(&self) -> usize {
|
fn outgoing_capacity(&self) -> usize {
|
||||||
MAX_PACKET_SIZE as usize * 4
|
MAX_PACKET_SIZE as usize * 4
|
||||||
}
|
}
|
||||||
|
@ -229,30 +278,6 @@ pub trait Config: Sized + Send + Sync + 'static {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Called upon (every) client connect (if online mode is enabled) to obtain
|
|
||||||
/// the full URL to use for session server requests. Defaults to
|
|
||||||
/// `https://sessionserver.mojang.com/session/minecraft/hasJoined?username=<username>&serverId=<auth-digest>&ip=<player-ip>`.
|
|
||||||
///
|
|
||||||
/// It is assumed, that upon successful request, a structure matching the
|
|
||||||
/// description in the [wiki](https://wiki.vg/Protocol_Encryption#Server) was obtained.
|
|
||||||
/// Providing a URL that does not return such a structure will result in a
|
|
||||||
/// disconnect for every client that connects.
|
|
||||||
///
|
|
||||||
/// The arguments are described in the linked wiki article.
|
|
||||||
fn format_session_server_url(
|
|
||||||
&self,
|
|
||||||
server: &SharedServer<Self>,
|
|
||||||
username: Username<&str>,
|
|
||||||
auth_digest: &str,
|
|
||||||
player_ip: &IpAddr,
|
|
||||||
) -> String {
|
|
||||||
if self.prevent_proxy_connections() {
|
|
||||||
format!("https://sessionserver.mojang.com/session/minecraft/hasJoined?username={username}&serverId={auth_digest}&ip={player_ip}")
|
|
||||||
} else {
|
|
||||||
format!("https://sessionserver.mojang.com/session/minecraft/hasJoined?username={username}&serverId={auth_digest}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Called after the server is created, but prior to accepting connections
|
/// Called after the server is created, but prior to accepting connections
|
||||||
/// and entering the update loop.
|
/// and entering the update loop.
|
||||||
///
|
///
|
||||||
|
@ -276,7 +301,7 @@ pub trait Config: Sized + Send + Sync + 'static {
|
||||||
/// # Default Implementation
|
/// # Default Implementation
|
||||||
///
|
///
|
||||||
/// The default implementation does nothing.
|
/// The default implementation does nothing.
|
||||||
fn update(&self, server: &mut Server<Self>);
|
fn update(&self, server: &mut Server<Self>) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The result of the [`server_list_ping`](Config::server_list_ping) callback.
|
/// The result of the [`server_list_ping`](Config::server_list_ping) callback.
|
||||||
|
@ -316,7 +341,7 @@ pub enum ConnectionMode {
|
||||||
/// This mode should be used for all publicly exposed servers which are not
|
/// This mode should be used for all publicly exposed servers which are not
|
||||||
/// behind a proxy.
|
/// behind a proxy.
|
||||||
///
|
///
|
||||||
/// [configured session server]: Config::format_session_server_url
|
/// [configured session server]: Config::session_server
|
||||||
#[default]
|
#[default]
|
||||||
Online,
|
Online,
|
||||||
/// Disables client authentication with the configured session server.
|
/// Disables client authentication with the configured session server.
|
||||||
|
@ -395,6 +420,4 @@ where
|
||||||
type WorldState = W;
|
type WorldState = W;
|
||||||
type ChunkState = Ch;
|
type ChunkState = Ch;
|
||||||
type PlayerListState = P;
|
type PlayerListState = P;
|
||||||
|
|
||||||
fn update(&self, _server: &mut Server<Self>) {}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,7 +53,7 @@ impl PacketEncoder {
|
||||||
|
|
||||||
if let Some(threshold) = self.compression_threshold {
|
if let Some(threshold) = self.compression_threshold {
|
||||||
if data_len >= threshold as usize {
|
if data_len >= threshold as usize {
|
||||||
let mut z = ZlibEncoder::new(&mut self.compress_buf, Compression::best());
|
let mut z = ZlibEncoder::new(&mut self.compress_buf, Compression::new(4));
|
||||||
pkt.encode_packet(&mut z)?;
|
pkt.encode_packet(&mut z)?;
|
||||||
drop(z);
|
drop(z);
|
||||||
|
|
||||||
|
|
|
@ -615,13 +615,14 @@ async fn handle_login(
|
||||||
ConnectionMode::Velocity { secret } => login::velocity(ctrl, username, secret).await?,
|
ConnectionMode::Velocity { secret } => login::velocity(ctrl, username, secret).await?,
|
||||||
};
|
};
|
||||||
|
|
||||||
let compression_threshold = 256;
|
if let Some(threshold) = server.0.cfg.compression_threshold() {
|
||||||
ctrl.send_packet(&SetCompression {
|
ctrl.send_packet(&SetCompression {
|
||||||
threshold: VarInt(compression_threshold as i32),
|
threshold: VarInt(threshold as i32),
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
ctrl.set_compression(Some(compression_threshold));
|
ctrl.set_compression(Some(threshold));
|
||||||
|
}
|
||||||
|
|
||||||
if let Err(reason) = server.0.cfg.login(server, &ncd).await {
|
if let Err(reason) = server.0.cfg.login(server, &ncd).await {
|
||||||
log::info!("Disconnect at login: \"{reason}\"");
|
log::info!("Disconnect at login: \"{reason}\"");
|
||||||
|
|
|
@ -123,7 +123,7 @@ impl ByteSender {
|
||||||
pub enum TrySendError {
|
pub enum TrySendError {
|
||||||
#[error("sender disconnected")]
|
#[error("sender disconnected")]
|
||||||
Disconnected(BytesMut),
|
Disconnected(BytesMut),
|
||||||
#[error("channel full")]
|
#[error("channel full (see `Config::outgoing_capacity`)")]
|
||||||
Full(BytesMut),
|
Full(BytesMut),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -96,7 +96,7 @@ pub(super) async fn online(
|
||||||
.chain(&server.0.public_key_der)
|
.chain(&server.0.public_key_der)
|
||||||
.finalize();
|
.finalize();
|
||||||
|
|
||||||
let url = server.config().format_session_server_url(
|
let url = server.config().session_server(
|
||||||
server,
|
server,
|
||||||
username.as_str_username(),
|
username.as_str_username(),
|
||||||
&auth_digest(&hash),
|
&auth_digest(&hash),
|
||||||
|
|
Loading…
Reference in a new issue