Skip to content

Commit b235656

Browse files
committed
Merge branch 'master' into rework/better-chunk-sending
2 parents 3715c86 + cc11874 commit b235656

File tree

12 files changed

+295
-10
lines changed

12 files changed

+295
-10
lines changed

.etc/example-config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ max_players = 100
1010
network_tick_rate = 30
1111
# World name to load
1212
world = "world"
13+
# Whether the server should validate players via the whitelist
14+
whitelist = false
1315
# Network compression threshold (can be negative). This decides how long a packet has to be before it is compressed.
1416
# Very small packets may actually increase in size when compressed, so setting it to 0 won't be perfect in all situations.
1517
# Set to -1 to disable compression.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ parking_lot = "0.12.3"
123123
rayon = "1.10.0"
124124

125125
# Network
126+
reqwest = { version = "0.12.9", features = ["json"] }
126127

127128
# Error handling
128129
thiserror = "2.0.3"
@@ -139,6 +140,7 @@ serde_derive = "1.0.210"
139140
base64 = "0.22.1"
140141
bitcode = "0.6.3"
141142
bitcode_derive = "0.6.3"
143+
toml = "0.8.19"
142144

143145
# Bit manipulation
144146
byteorder = "1.5.0"
@@ -184,6 +186,7 @@ colored = "2.1.0"
184186
# Misc
185187
deepsize = "0.2.0"
186188
page_size = "0.6.0"
189+
regex = "1.11.1"
187190

188191
# I/O
189192
tempfile = "3.12.0"

src/bin/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern crate core;
55
use crate::errors::BinaryError;
66
use clap::Parser;
77
use ferrumc_config::statics::get_global_config;
8+
use ferrumc_config::whitelist::create_whitelist;
89
use ferrumc_core::chunks::chunk_receiver::ChunkReceiver;
910
use ferrumc_ecs::Universe;
1011
use ferrumc_general_purpose::paths::get_root_path;
@@ -75,10 +76,11 @@ async fn main() {
7576
async fn entry() -> Result<()> {
7677
let state = create_state().await?;
7778
let global_state = Arc::new(state);
79+
create_whitelist().await;
7880

7981
let all_system_handles = tokio::spawn(definition::start_all_systems(global_state.clone()));
8082

81-
// Start the systems and wait until all of them are done
83+
//Start the systems and wait until all of them are done
8284
all_system_handles.await??;
8385

8486
// Stop all systems

src/bin/src/packet_handlers/login_process.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use ferrumc_config::statics::{get_global_config, get_whitelist};
12
use ferrumc_core::chunks::chunk_receiver::ChunkReceiver;
23
use ferrumc_core::identity::player_identity::PlayerIdentity;
34
use ferrumc_core::transform::grounded::OnGround;
@@ -16,6 +17,7 @@ use ferrumc_net::packets::outgoing::client_bound_known_packs::ClientBoundKnownPa
1617
use ferrumc_net::packets::outgoing::finish_configuration::FinishConfigurationPacket;
1718
use ferrumc_net::packets::outgoing::game_event::GameEventPacket;
1819
use ferrumc_net::packets::outgoing::keep_alive::OutgoingKeepAlivePacket;
20+
use ferrumc_net::packets::outgoing::login_disconnect::LoginDisconnectPacket;
1921
use ferrumc_net::packets::outgoing::login_play::LoginPlayPacket;
2022
use ferrumc_net::packets::outgoing::login_success::LoginSuccessPacket;
2123
use ferrumc_net::packets::outgoing::registry_data::get_registry_packets;
@@ -32,11 +34,10 @@ async fn handle_login_start(
3234
login_start_event: LoginStartEvent,
3335
state: GlobalState,
3436
) -> Result<LoginStartEvent, NetError> {
35-
debug!("Handling login start event");
36-
3737
let uuid = login_start_event.login_start_packet.uuid;
3838
let username = login_start_event.login_start_packet.username.as_str();
39-
debug!("Received login start from user with username {}", username);
39+
let player_identity = PlayerIdentity::new(username.to_string(), uuid);
40+
debug!("Handling login start event for user: {username}, uuid: {uuid}");
4041

4142
// Add the player identity component to the ECS for the entity.
4243
state
@@ -52,6 +53,28 @@ async fn handle_login_start(
5253
.universe
5354
.get_mut::<StreamWriter>(login_start_event.conn_id)?;
5455

56+
if get_global_config().whitelist {
57+
let whitelist = get_whitelist();
58+
59+
if whitelist.get(&uuid).is_none() {
60+
writer
61+
.send_packet(
62+
&LoginDisconnectPacket::new(
63+
"{\"translate\":\"multiplayer.disconnect.not_whitelisted\"}",
64+
),
65+
&NetEncodeOpts::WithLength,
66+
)
67+
.await?;
68+
return Ok(login_start_event);
69+
}
70+
}
71+
72+
// Add the player identity component to the ECS for the entity.
73+
state
74+
.universe
75+
.add_component::<PlayerIdentity>(login_start_event.conn_id, player_identity)?;
76+
77+
//Send a Login Success Response to further the login sequence
5578
writer
5679
.send_packet(
5780
&LoginSuccessPacket::new(uuid, username),
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use ferrumc_macros::{packet, NetEncode};
2+
use std::io::Write;
3+
4+
#[derive(NetEncode)]
5+
#[packet(packet_id = 0x00)]
6+
pub struct LoginDisconnectPacket<'a> {
7+
pub reason: &'a str,
8+
}
9+
10+
impl<'a> LoginDisconnectPacket<'a> {
11+
pub fn new(reason: &'a str) -> Self {
12+
Self { reason }
13+
}
14+
}

src/lib/net/src/packets/outgoing/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod disconnect;
66
pub mod finish_configuration;
77
pub mod game_event;
88
pub mod keep_alive;
9+
pub mod login_disconnect;
910
pub mod login_play;
1011
pub mod login_success;
1112
pub mod ping_response;

src/lib/utils/config/Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ edition = "2021"
55
description = "FerrumC configuration utilities"
66

77
[dependencies]
8-
toml = "0.8.19"
8+
toml = { workspace = true }
99
thiserror = { workspace = true }
1010
serde = { workspace = true }
1111
serde_derive = { workspace = true }
1212
tracing = { workspace = true }
1313
parking_lot = { workspace = true }
1414
base64 = { workspace = true }
1515
ferrumc-general-purpose = { workspace = true }
16-
lazy_static = { workspace = true }
16+
ferrumc-core = { workspace = true }
17+
lazy_static = { workspace = true }
18+
dashmap = { workspace = true }
19+
uuid = { workspace = true }
20+
regex = { workspace = true }
21+
reqwest = { workspace = true }
22+
futures = { workspace = true }

src/lib/utils/config/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub mod favicon;
1515
pub mod server_config;
1616
pub mod setup;
1717
pub mod statics;
18+
pub mod whitelist;
1819

1920
// Re-exports
2021
pub use server_config::DatabaseConfig;

src/lib/utils/config/src/server_config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use serde_derive::{Deserialize, Serialize};
1515
/// - `database` - [DatabaseConfig]: The configuration for the database.
1616
/// - `world`: The name of the world that the server will load.
1717
/// - `network_compression_threshold`: The threshold at which the server will compress network packets.
18+
/// - `whitelist`: Whether the server whitelist is enabled or not.
1819
#[derive(Debug, Deserialize, Serialize)]
1920
pub struct ServerConfig {
2021
pub host: String,
@@ -25,6 +26,7 @@ pub struct ServerConfig {
2526
pub database: DatabaseConfig,
2627
pub world: String,
2728
pub network_compression_threshold: i32, // Can be negative
29+
pub whitelist: bool,
2830
}
2931

3032
/// The database configuration section from [ServerConfig].

src/lib/utils/config/src/setup.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::statics::DEFAULT_CONFIG;
2+
use crate::whitelist::create_blank_whitelist_file;
23
use ferrumc_general_purpose::paths::get_root_path;
34
use std::fs::File;
45
use std::io::Write;
@@ -26,6 +27,9 @@ impl From<std::io::Error> for SetupError {
2627
}
2728

2829
pub fn setup() -> Result<(), SetupError> {
30+
if !std::fs::exists(get_root_path().join("whitelist.txt"))? {
31+
create_blank_whitelist_file();
32+
}
2933
if std::fs::exists(get_root_path().join("config.toml"))? {
3034
return Ok(());
3135
}

0 commit comments

Comments
 (0)