added encrytion

This commit is contained in:
Snowiiii committed 2024-07-29 23:53:58 +02:00
1 parent b5541ab5ba
commit 8a5e69f8e3
16 files changed
+1104 -58

No files matched your search

+40 -3
View File
@@ -1,10 +1,19 @@
use std::io::Cursor;
use base64::{engine::general_purpose, Engine};
use rsa::{rand_core::OsRng, traits::PublicKeyParts, RsaPrivateKey, RsaPublicKey};
use crate::protocol::{Players, Sample, StatusResponse, Version};
pub struct Server {
pub compression_threshold: Option<u8>,
pub online_mode: bool,
pub encriyption: bool, // encription is always required when online_mode is disabled
pub public_key: RsaPublicKey,
pub private_key: RsaPrivateKey,
pub public_key_der: Box<[u8]>,
pub status_response: StatusResponse,
}
@@ -16,9 +25,32 @@ impl Default for Server {
impl Server {
pub fn new() -> Self {
let status_response = Self::default_response();
// todo, only create when needed
let (public_key, private_key) = Self::generate_keys();
let public_key_der = rsa_der::public_key_to_der(
&private_key.n().to_bytes_be(),
&private_key.e().to_bytes_be(),
)
.into_boxed_slice();
Self {
online_mode: true,
encriyption: true,
compression_threshold: None, // 256
public_key,
private_key,
status_response,
public_key_der,
}
}
pub fn default_response() -> StatusResponse {
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/icon.png");
let status_response = StatusResponse {
StatusResponse {
version: Version {
name: "1.21".into(),
protocol: 767,
@@ -33,8 +65,7 @@ impl Server {
},
description: "Pumpkin Server".into(),
favicon: Self::load_icon(path),
};
Self { status_response }
}
}
pub fn load_icon(path: &str) -> String {
@@ -50,4 +81,10 @@ impl Server {
general_purpose::STANDARD.encode_string(image, &mut result);
result
}
pub fn generate_keys() -> (RsaPublicKey, RsaPrivateKey) {
let priv_key = RsaPrivateKey::new(&mut OsRng, 1024).expect("failed to generate a key");
let pub_key = RsaPublicKey::from(&priv_key);
(pub_key, priv_key)
}
}