mirror of
https://github.com/zyllian/classics.git
synced 2025-05-10 05:26:39 -07:00
add basic server configuration + password protection
This commit is contained in:
parent
ca94ec10f2
commit
c78303cf44
7 changed files with 146 additions and 17 deletions
23
src/main.rs
23
src/main.rs
|
@ -1,13 +1,32 @@
|
|||
use server::Server;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use server::{config::ServerConfig, Server};
|
||||
|
||||
mod level;
|
||||
mod packet;
|
||||
mod player;
|
||||
mod server;
|
||||
|
||||
const CONFIG_FILE: &str = "./server-config.json";
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let mut server = Server::new().await?;
|
||||
let config_path = PathBuf::from(CONFIG_FILE);
|
||||
let config = if config_path.exists() {
|
||||
serde_json::from_str(&std::fs::read_to_string(config_path)?)
|
||||
.expect("failed to deserialize config")
|
||||
} else {
|
||||
let config = ServerConfig::default();
|
||||
std::fs::write(
|
||||
config_path,
|
||||
serde_json::to_string_pretty(&config).expect("failed to serialize default config"),
|
||||
)?;
|
||||
config
|
||||
};
|
||||
|
||||
println!("starting server with config: {config:#?}");
|
||||
|
||||
let mut server = Server::new(config).await?;
|
||||
|
||||
server.run().await?;
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
pub mod config;
|
||||
mod network;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
@ -8,6 +9,8 @@ use tokio::{net::TcpListener, sync::RwLock};
|
|||
|
||||
use crate::{level::Level, player::Player};
|
||||
|
||||
use self::config::ServerConfig;
|
||||
|
||||
const DEFAULT_SERVER_SIZE: usize = 128;
|
||||
|
||||
/// the server
|
||||
|
@ -28,18 +31,25 @@ pub struct ServerData {
|
|||
pub players: Vec<Player>,
|
||||
/// list of player ids which have been freed up
|
||||
pub free_player_ids: Vec<i8>,
|
||||
/// the server's config
|
||||
pub config: ServerConfig,
|
||||
}
|
||||
|
||||
impl Server {
|
||||
/// creates a new server with a generated level
|
||||
pub async fn new() -> std::io::Result<Self> {
|
||||
pub async fn new(config: ServerConfig) -> std::io::Result<Self> {
|
||||
println!("generating level");
|
||||
let mut rng = rand::thread_rng();
|
||||
let mut level = Level::new(
|
||||
DEFAULT_SERVER_SIZE,
|
||||
DEFAULT_SERVER_SIZE,
|
||||
DEFAULT_SERVER_SIZE,
|
||||
);
|
||||
let (level_x, level_y, level_z) = if let Some(size) = &config.level_size {
|
||||
(size.x, size.y, size.z)
|
||||
} else {
|
||||
(
|
||||
DEFAULT_SERVER_SIZE,
|
||||
DEFAULT_SERVER_SIZE,
|
||||
DEFAULT_SERVER_SIZE,
|
||||
)
|
||||
};
|
||||
let mut level = Level::new(level_x, level_y, level_z);
|
||||
for x in 0..level.x_size {
|
||||
for y in 0..(level.y_size / 2) {
|
||||
for z in 0..level.z_size {
|
||||
|
@ -49,11 +59,11 @@ impl Server {
|
|||
}
|
||||
println!("done!");
|
||||
|
||||
Self::new_with_level(level).await
|
||||
Self::new_with_level(config, level).await
|
||||
}
|
||||
|
||||
/// creates a new server with the given level
|
||||
pub async fn new_with_level(level: Level) -> std::io::Result<Self> {
|
||||
pub async fn new_with_level(config: ServerConfig, level: Level) -> std::io::Result<Self> {
|
||||
let listener = TcpListener::bind("127.0.0.1:25565").await?;
|
||||
|
||||
Ok(Self {
|
||||
|
@ -61,6 +71,7 @@ impl Server {
|
|||
level,
|
||||
players: Default::default(),
|
||||
free_player_ids: Vec::new(),
|
||||
config,
|
||||
})),
|
||||
listener,
|
||||
})
|
||||
|
|
39
src/server/config.rs
Normal file
39
src/server/config.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// configuration for the server
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct ServerConfig {
|
||||
/// the server's name
|
||||
pub name: String,
|
||||
/// the server's motd
|
||||
pub motd: String,
|
||||
/// the server's password, if any
|
||||
pub password: Option<String>,
|
||||
/// the level's size
|
||||
pub level_size: Option<ConfigCoordinates>,
|
||||
/// the level's spawn point
|
||||
pub spawn: Option<ConfigCoordinates>,
|
||||
}
|
||||
|
||||
impl Default for ServerConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
name: "classic server wowie".to_string(),
|
||||
motd: "here's the default server motd".to_string(),
|
||||
password: None,
|
||||
level_size: None,
|
||||
spawn: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// coordinates as stored in configuration
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ConfigCoordinates {
|
||||
/// the X coordinate
|
||||
pub x: usize,
|
||||
/// the Y coordinate
|
||||
pub y: usize,
|
||||
/// the Z coordinate
|
||||
pub z: usize,
|
||||
}
|
|
@ -98,12 +98,11 @@ async fn handle_stream_inner(
|
|||
let mut reader = PacketReader::new(&read_buf);
|
||||
|
||||
if let Some(packet) = ClientPacket::read(packet_buf[0], &mut reader) {
|
||||
// println!("{packet:#?}");
|
||||
match packet {
|
||||
ClientPacket::PlayerIdentification {
|
||||
protocol_version,
|
||||
username,
|
||||
verification_key: _,
|
||||
verification_key,
|
||||
_unused,
|
||||
} => {
|
||||
if protocol_version != 0x07 {
|
||||
|
@ -114,6 +113,12 @@ async fn handle_stream_inner(
|
|||
|
||||
let mut data = data.write().await;
|
||||
|
||||
if let Some(password) = &data.config.password {
|
||||
if verification_key != *password {
|
||||
return Ok(Some("Incorrect password!".to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
for player in &data.players {
|
||||
if player.username == username {
|
||||
return Ok(Some(
|
||||
|
@ -144,8 +149,8 @@ async fn handle_stream_inner(
|
|||
|
||||
reply_queue.push_back(ServerPacket::ServerIdentification {
|
||||
protocol_version: 0x07,
|
||||
server_name: "test server".to_string(),
|
||||
server_motd: "whoaaaaaa".to_string(),
|
||||
server_name: data.config.name.clone(),
|
||||
server_motd: data.config.motd.clone(),
|
||||
user_type: PlayerType::Normal,
|
||||
});
|
||||
|
||||
|
@ -156,12 +161,19 @@ async fn handle_stream_inner(
|
|||
let username = player.username.clone();
|
||||
data.players.push(player);
|
||||
|
||||
let (spawn_x, spawn_y, spawn_z) =
|
||||
if let Some(spawn) = &data.config.spawn {
|
||||
(spawn.x, spawn.y, spawn.z)
|
||||
} else {
|
||||
(16, data.level.y_size / 2 + 2, 16)
|
||||
};
|
||||
|
||||
let spawn_packet = ServerPacket::SpawnPlayer {
|
||||
player_id: *own_id,
|
||||
player_name: username.clone(),
|
||||
x: f16::from_f32(16.5),
|
||||
y: f16::from_f32((data.level.y_size / 2 + 2) as f32),
|
||||
z: f16::from_f32(16.5),
|
||||
x: f16::from_f32(spawn_x as f32 + 0.5),
|
||||
y: f16::from_f32(spawn_y as f32),
|
||||
z: f16::from_f32(spawn_z as f32 + 0.5),
|
||||
yaw: 0,
|
||||
pitch: 0,
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue