From d57d9ffdcc9b8a5843c5ed00b54d6102a0b10341 Mon Sep 17 00:00:00 2001 From: DaniD3v Date: Thu, 29 Aug 2024 16:56:35 +0200 Subject: [PATCH] Use proper IP Address abstraction instead of using format!() --- pumpkin/src/config/mod.rs | 19 ++++++++----------- pumpkin/src/main.rs | 9 +-------- pumpkin/src/rcon/mod.rs | 5 +---- 3 files changed, 10 insertions(+), 23 deletions(-) diff --git a/pumpkin/src/config/mod.rs b/pumpkin/src/config/mod.rs index e98b78dc7..fffe2c1a0 100644 --- a/pumpkin/src/config/mod.rs +++ b/pumpkin/src/config/mod.rs @@ -1,9 +1,11 @@ -use std::path::Path; - use auth_config::AuthenticationConfig; use proxy::ProxyConfig; use resource_pack::ResourcePackConfig; use serde::{Deserialize, Serialize}; +use std::{ + net::{Ipv4Addr, SocketAddr}, + path::Path, +}; use crate::{entity::player::GameMode, server::Difficulty}; @@ -33,8 +35,7 @@ pub struct AdvancedConfiguration { #[derive(Deserialize, Serialize, Clone)] pub struct RCONConfig { pub enabled: bool, - pub ip: String, - pub port: u16, + pub address: SocketAddr, pub password: String, } @@ -42,8 +43,7 @@ impl Default for RCONConfig { fn default() -> Self { Self { enabled: false, - ip: "0.0.0.0".to_string(), - port: 25575, + address: SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 25575), password: "".to_string(), } } @@ -116,9 +116,7 @@ pub struct BasicConfiguration { /// A version identifier for the configuration format. pub config_version: String, /// The address to bind the server to. - pub server_address: String, - /// The port to listen on. - pub server_port: u16, + pub server_address: SocketAddr, /// The seed for world generation. pub seed: String, /// The maximum number of players allowed on the server. @@ -147,8 +145,7 @@ impl Default for BasicConfiguration { fn default() -> Self { Self { config_version: CURRENT_BASE_VERSION.to_string(), - server_address: "0.0.0.0".to_string(), - server_port: 25565, + server_address: SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), 25565), seed: "".to_string(), max_players: 100000, view_distance: 10, diff --git a/pumpkin/src/main.rs b/pumpkin/src/main.rs index 56281f274..d586b6040 100644 --- a/pumpkin/src/main.rs +++ b/pumpkin/src/main.rs @@ -74,14 +74,7 @@ fn main() -> io::Result<()> { let mut events = Events::with_capacity(128); // Setup the TCP server socket. - - let addr = format!( - "{}:{}", - basic_config.server_address, basic_config.server_port - ) - .parse() - .unwrap(); - + let addr = basic_config.server_address; let mut listener = TcpListener::bind(addr)?; // Register the server with poll we can receive events for it. diff --git a/pumpkin/src/rcon/mod.rs b/pumpkin/src/rcon/mod.rs index e6a94fdf8..3f20ffd54 100644 --- a/pumpkin/src/rcon/mod.rs +++ b/pumpkin/src/rcon/mod.rs @@ -35,11 +35,8 @@ impl RCONServer { server: Arc>, ) -> Result { assert!(config.enabled, "RCON is not enabled"); - let addr = format!("{}:{}", config.ip, config.port) - .parse() - .expect("Failed to parse RCON address"); let mut poll = Poll::new().unwrap(); - let mut listener = TcpListener::bind(addr).unwrap(); + let mut listener = TcpListener::bind(config.address).unwrap(); poll.registry() .register(&mut listener, SERVER, Interest::READABLE)