Setup Base for Velocity (Proxy)

This commit is contained in:
Snowiiii committed 2024-08-15 15:00:41 +02:00
1 parent 333bf64235
commit ceb718d475
14 files changed
+180 -8

No files matched your search

+4
View File
@@ -43,6 +43,10 @@ reqwest = { version = "0.12.5", features = ["json"]}
sha1 = "0.10.6"
digest = "=0.11.0-pre.9"
# velocity en
hmac = "0.12.1"
sha2 = "0.10.8"
thiserror = "1.0.63"
# icon loading
+18 -1
View File
@@ -22,6 +22,7 @@ use sha1::{Digest, Sha1};
use crate::{
client::authentication::{self, GameProfile},
entity::player::{ChatMode, Hand},
proxy::velocity::velocity_login,
server::{Server, CURRENT_MC_VERSION},
};
@@ -34,6 +35,7 @@ use super::{
/// Implements the `Client` Packets
impl Client {
pub fn handle_handshake(&mut self, _server: &mut Server, handshake: SHandShake) {
dbg!("handshake");
self.protocol_version = handshake.protocol_version.0;
self.connection_state = handshake.next_state;
if self.connection_state == ConnectionState::Login {
@@ -56,9 +58,17 @@ impl Client {
self.close();
}
fn is_valid_player_name(name: &str) -> bool {
name.len() <= 16 && name.chars().all(|c| c > 32 as char && c < 127 as char)
}
pub fn handle_login_start(&mut self, server: &mut Server, login_start: SLoginStart) {
// TODO: do basic name validation
dbg!("login start");
if !Self::is_valid_player_name(&login_start.name) {
self.kick("Invalid characters in username");
return;
}
// default game profile, when no online mode
// TODO: make offline uuid
self.gameprofile = Some(GameProfile {
@@ -67,6 +77,13 @@ impl Client {
properties: vec![],
profile_actions: None,
});
let proxy = &server.advanced_config.proxy;
if proxy.enabled {
if proxy.velocity.enabled {
velocity_login(self)
}
return;
}
// TODO: check config for encryption
let verify_token: [u8; 4] = rand::random();
+5 -1
View File
@@ -1,12 +1,14 @@
use std::path::Path;
use auth_config::AuthenticationConfig;
use proxy::ProxyConfig;
use resource_pack::ResourcePackConfig;
use serde::{Deserialize, Serialize};
use crate::{entity::player::GameMode, server::Difficulty};
pub mod auth_config;
pub mod proxy;
pub mod resource_pack;
/// Current Config version of the Base Config
@@ -17,10 +19,11 @@ const CURRENT_BASE_VERSION: &str = "1.0.0";
/// This also allows you get some Performance or Resource boosts.
/// Important: The Configuration should match Vanilla by default
pub struct AdvancedConfiguration {
pub commands: CommandsConfig,
pub proxy: ProxyConfig,
pub authentication: AuthenticationConfig,
pub packet_compression: CompressionConfig,
pub resource_pack: ResourcePackConfig,
pub commands: CommandsConfig,
pub rcon: RCONConfig,
pub pvp: PVPConfig,
}
@@ -107,6 +110,7 @@ impl Default for CompressionConfig {
impl Default for AdvancedConfiguration {
fn default() -> Self {
Self {
proxy: ProxyConfig::default(),
authentication: AuthenticationConfig::default(),
commands: CommandsConfig::default(),
packet_compression: CompressionConfig::default(),
+22
View File
@@ -0,0 +1,22 @@
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Default)]
pub struct ProxyConfig {
pub enabled: bool,
pub velocity: VelocityConfig,
}
#[derive(Deserialize, Serialize)]
pub struct VelocityConfig {
pub enabled: bool,
pub secret: String,
}
impl Default for VelocityConfig {
fn default() -> Self {
Self {
enabled: false,
secret: "".into(),
}
}
}
+2 -1
View File
@@ -18,6 +18,7 @@ pub mod client;
pub mod commands;
pub mod config;
pub mod entity;
pub mod proxy;
pub mod rcon;
pub mod server;
pub mod util;
@@ -81,7 +82,7 @@ fn main() -> io::Result<()> {
let mut server = Server::new((basic_config, advanced_configuration));
log::info!("Started Server took {}ms", time.elapsed().as_millis());
log::info!("You now can connect to the server");
log::info!("You now can connect to the server, Listening on {}", addr);
if use_console {
thread::spawn(move || {
+1
View File
@@ -0,0 +1 @@
pub mod velocity;
+69
View File
@@ -0,0 +1,69 @@
use std::net::SocketAddr;
use bytes::{BufMut, BytesMut};
use hmac::{Hmac, Mac};
use pumpkin_protocol::{
bytebuf::ByteBuffer, client::login::CLoginPluginRequest, server::login::SLoginPluginResponse,
};
use sha2::Sha256;
use crate::{client::Client, config::proxy::VelocityConfig};
type HmacSha256 = Hmac<Sha256>;
const MAX_SUPPORTED_FORWARDING_VERSION: i32 = 4;
const PLAYER_INFO_CHANNEL: &str = "velocity:player_info";
pub fn velocity_login(client: &mut Client) {
let velocity_message_id: i32 = 0;
let mut buf = BytesMut::new();
buf.put_u8(MAX_SUPPORTED_FORWARDING_VERSION as u8);
client.send_packet(&CLoginPluginRequest::new(
velocity_message_id.into(),
PLAYER_INFO_CHANNEL,
&buf,
));
}
pub fn check_integrity(data: (&[u8], &[u8]), secret: String) -> bool {
let (signature, data_without_signature) = data;
let mut mac =
HmacSha256::new_from_slice(secret.as_bytes()).expect("HMAC can take key of any size");
mac.update(data_without_signature);
mac.verify_slice(signature).is_ok()
}
pub fn receive_plugin_response(
client: &mut Client,
config: VelocityConfig,
response: SLoginPluginResponse,
) {
dbg!("velocity response");
if let Some(data) = response.data {
let (signature, data_without_signature) = data.split_at(32);
if !check_integrity((signature, data_without_signature), config.secret) {
client.kick("Unable to verify player details");
return;
}
let mut buf = ByteBuffer::new(BytesMut::new());
buf.put_slice(data_without_signature);
// check velocity version
let version = buf.get_var_int();
let version = version.0;
if version > MAX_SUPPORTED_FORWARDING_VERSION {
client.kick(&format!(
"Unsupported forwarding version {version}, Max: {MAX_SUPPORTED_FORWARDING_VERSION}"
));
return;
}
// TODO: no unwrap
let addr: SocketAddr = buf.get_string().unwrap().parse().unwrap();
client.address = addr;
todo!()
} else {
client.kick("This server requires you to connect with Velocity.")
}
}