diff --git a/README.md b/README.md index f5a5b1f..c373211 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ https://user-images.githubusercontent.com/856222/141378688-882082be-9efa-4cfe-81 - Automatically manages `server.properties` (host, port and RCON settings) - Graceful server sleep/shutdown through RCON (with `SIGTERM` fallback on Linux/Unix) - Restart server on crash +- Lockout mode ## Requirements diff --git a/res/lazymc.toml b/res/lazymc.toml index b24d0a9..099743a 100644 --- a/res/lazymc.toml +++ b/res/lazymc.toml @@ -67,6 +67,13 @@ command = "java -Xmx1G -Xms1G -jar server.jar --nogui" #login_starting = "Server is starting... §c♥§r\n\nThis may take some time.\n\nPlease try to reconnect in a minute." #login_stopping = "Server is going to sleep... §7☠§r\n\nPlease try to reconnect in a minute to wake it again." +[lockout] +# Enable to prevent everybody from connecting through lazymc. Instantly kicks player. +#enabled = false + +# Kick players with following message. +#message = "Server is closed §7☠§r\n\nPlease try to reconnect in a minute." + [rcon] # Enable sleeping server through RCON. # Must be enabled on Windows. diff --git a/src/config.rs b/src/config.rs index 78d2d6c..17f8179 100644 --- a/src/config.rs +++ b/src/config.rs @@ -73,6 +73,10 @@ pub struct Config { #[serde(default)] pub messages: Messages, + /// Lockout feature. + #[serde(default)] + pub lockout: Lockout, + /// RCON configuration. #[serde(default)] pub rcon: Rcon, @@ -216,6 +220,26 @@ impl Default for Messages { } } +/// Lockout configuration. +#[derive(Debug, Deserialize)] +#[serde(default)] +pub struct Lockout { + /// Enable to prevent everybody from connecting through lazymc. Instantly kicks player. + pub enabled: bool, + + /// Kick players with following message. + pub message: String, +} + +impl Default for Lockout { + fn default() -> Self { + Self { + enabled: false, + message: "Server is closed §7☠§r\n\nPlease come back another time.".into(), + } + } +} + /// RCON configuration. #[derive(Debug, Deserialize)] #[serde(default)] diff --git a/src/service/server.rs b/src/service/server.rs index 408406a..7654c4c 100644 --- a/src/service/server.rs +++ b/src/service/server.rs @@ -38,6 +38,13 @@ pub async fn service(config: Arc) -> Result<(), ()> { config.public.address, config.server.address, ); + if config.lockout.enabled { + warn!( + target: "lazymc", + "Lockout mode is enabled, nobody will be able to connect through the proxy", + ); + } + // Spawn server monitor and signal handler services tokio::spawn(service::monitor::service(config.clone(), server.clone())); tokio::spawn(service::signal::service(config.clone(), server.clone())); @@ -58,7 +65,8 @@ pub async fn service(config: Arc) -> Result<(), ()> { /// Route inbound TCP stream to correct service, spawning a new task. #[inline] fn route(inbound: TcpStream, config: Arc, server: Arc) { - if server.state() == server::State::Started { + let should_proxy = server.state() == server::State::Started && !config.lockout.enabled; + if should_proxy { route_proxy(inbound, config) } else { route_status(inbound, config, server) diff --git a/src/status.rs b/src/status.rs index c0dda5d..9748e83 100644 --- a/src/status.rs +++ b/src/status.rs @@ -104,6 +104,18 @@ pub async fn serve( .ok() .map(|p| p.name); + // Kick if lockout is enabled + if config.lockout.enabled { + match username { + Some(username) => { + info!(target: "lazymc", "Kicked '{}' because lockout is enabled", username) + } + None => info!(target: "lazymc", "Kicked player because lockout is enabled"), + } + kick(&config.lockout.message, &mut writer).await?; + break; + } + // Start server if not starting yet Server::start(config.clone(), server.clone(), username).await;