Skip to content

Commit cc11874

Browse files
authored
Merge pull request #140 from WhaleFromMars/feature-whitelist
IK clippy fails but that's due to a clippy version mismatch
2 parents b7ec7ac + 0988fdb commit cc11874

File tree

12 files changed

+295
-17
lines changed

12 files changed

+295
-17
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
@@ -120,6 +120,7 @@ parking_lot = "0.12.3"
120120
rayon = "1.10.0"
121121

122122
# Network
123+
reqwest = { version = "0.12.9", features = ["json"] }
123124

124125
# Error handling
125126
thiserror = "2.0.3"
@@ -136,6 +137,7 @@ serde_derive = "1.0.210"
136137
base64 = "0.22.1"
137138
bitcode = "0.6.3"
138139
bitcode_derive = "0.6.3"
140+
toml = "0.8.19"
139141

140142
# Bit manipulation
141143
byteorder = "1.5.0"
@@ -180,6 +182,7 @@ colored = "2.1.0"
180182
# Misc
181183
deepsize = "0.2.0"
182184
page_size = "0.6.0"
185+
regex = "1.11.1"
183186

184187
# I/O
185188
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_ecs::Universe;
910
use ferrumc_general_purpose::paths::get_root_path;
1011
use ferrumc_net::server::create_server_listener;
@@ -59,10 +60,11 @@ async fn main() {
5960
async fn entry() -> Result<()> {
6061
let state = create_state().await?;
6162
let global_state = Arc::new(state);
63+
create_whitelist().await;
6264

6365
let all_system_handles = tokio::spawn(definition::start_all_systems(global_state.clone()));
6466

65-
// Start the systems and wait until all of them are done
67+
//Start the systems and wait until all of them are done
6668
all_system_handles.await??;
6769

6870
// Stop all systems

src/bin/src/packet_handlers/login_process.rs

Lines changed: 26 additions & 10 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::identity::player_identity::PlayerIdentity;
23
use ferrumc_core::transform::grounded::OnGround;
34
use ferrumc_core::transform::position::Position;
@@ -15,6 +16,7 @@ use ferrumc_net::packets::outgoing::client_bound_known_packs::ClientBoundKnownPa
1516
use ferrumc_net::packets::outgoing::finish_configuration::FinishConfigurationPacket;
1617
use ferrumc_net::packets::outgoing::game_event::GameEventPacket;
1718
use ferrumc_net::packets::outgoing::keep_alive::OutgoingKeepAlivePacket;
19+
use ferrumc_net::packets::outgoing::login_disconnect::LoginDisconnectPacket;
1820
use ferrumc_net::packets::outgoing::login_play::LoginPlayPacket;
1921
use ferrumc_net::packets::outgoing::login_success::LoginSuccessPacket;
2022
use ferrumc_net::packets::outgoing::registry_data::get_registry_packets;
@@ -31,23 +33,37 @@ async fn handle_login_start(
3133
login_start_event: LoginStartEvent,
3234
state: GlobalState,
3335
) -> Result<LoginStartEvent, NetError> {
34-
debug!("Handling login start event");
35-
3636
let uuid = login_start_event.login_start_packet.uuid;
3737
let username = login_start_event.login_start_packet.username.as_str();
38-
debug!("Received login start from user with username {}", username);
39-
40-
// Add the player identity component to the ECS for the entity.
41-
state.universe.add_component::<PlayerIdentity>(
42-
login_start_event.conn_id,
43-
PlayerIdentity::new(username.to_string(), uuid),
44-
)?;
38+
let player_identity = PlayerIdentity::new(username.to_string(), uuid);
39+
debug!("Handling login start event for user: {username}, uuid: {uuid}");
4540

46-
//Send a Login Success Response to further the login sequence
4741
let mut writer = state
4842
.universe
4943
.get_mut::<StreamWriter>(login_start_event.conn_id)?;
5044

45+
if get_global_config().whitelist {
46+
let whitelist = get_whitelist();
47+
48+
if whitelist.get(&uuid).is_none() {
49+
writer
50+
.send_packet(
51+
&LoginDisconnectPacket::new(
52+
"{\"translate\":\"multiplayer.disconnect.not_whitelisted\"}",
53+
),
54+
&NetEncodeOpts::WithLength,
55+
)
56+
.await?;
57+
return Ok(login_start_event);
58+
}
59+
}
60+
61+
// Add the player identity component to the ECS for the entity.
62+
state
63+
.universe
64+
.add_component::<PlayerIdentity>(login_start_event.conn_id, player_identity)?;
65+
66+
//Send a Login Success Response to further the login sequence
5167
writer
5268
.send_packet(
5369
&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
@@ -4,6 +4,7 @@ pub mod disconnect;
44
pub mod finish_configuration;
55
pub mod game_event;
66
pub mod keep_alive;
7+
pub mod login_disconnect;
78
pub mod login_play;
89
pub mod login_success;
910
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)