testing
This commit is contained in:
+26
-11
@@ -23,18 +23,33 @@ fn main() -> std::io::Result<()> {
|
||||
move || {
|
||||
let mut stream = stream.unwrap();
|
||||
let mut reader = BufReader::new(stream.try_clone().unwrap());
|
||||
let session = methods::Session::new(&mut reader, database).unwrap();
|
||||
|
||||
loop {
|
||||
let mut request_string = String::new();
|
||||
match reader.read_line(&mut request_string).unwrap() {
|
||||
0 => break,
|
||||
_ => {
|
||||
let (method, params) = parser::parse(&request_string).unwrap();
|
||||
let response = session.handle_request(&method, ¶ms);
|
||||
stream.write_all(response.to_string().as_bytes()).unwrap();
|
||||
}
|
||||
};
|
||||
match methods::Session::new(&mut reader, database) {
|
||||
Ok(session) => loop {
|
||||
let mut request_string = String::new();
|
||||
match reader.read_line(&mut request_string).unwrap() {
|
||||
0 => break,
|
||||
_ => {
|
||||
let (method, params) = parser::parse(&request_string).unwrap();
|
||||
let response = session.handle_request(&method, ¶ms);
|
||||
stream
|
||||
.write_all(
|
||||
match response {
|
||||
Ok(response) => response.to_string(),
|
||||
Err(e) => format!("!{e}"),
|
||||
}
|
||||
.as_bytes(),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
};
|
||||
},
|
||||
Err(e) => {
|
||||
stream.write_all(format!("!{e}").as_bytes()).unwrap();
|
||||
stream
|
||||
.shutdown(std::net::Shutdown::Both)
|
||||
.expect("shutdown call failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
+47
-13
@@ -1,38 +1,72 @@
|
||||
mod set_cloak;
|
||||
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
io::{self, BufRead, BufReader},
|
||||
io::{BufRead, BufReader},
|
||||
net::TcpStream,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
parser::ParamMap,
|
||||
response::{Response, Result},
|
||||
};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct LocalPlayer {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
pub struct Session {
|
||||
pub session_id: String,
|
||||
pub session_token: String,
|
||||
pub database: Arc<crate::database::Database>,
|
||||
pub player: LocalPlayer,
|
||||
}
|
||||
|
||||
impl Session {
|
||||
pub fn new(
|
||||
reader: &mut BufReader<TcpStream>,
|
||||
database: Arc<crate::database::Database>,
|
||||
) -> io::Result<Self> {
|
||||
let mut session_id = String::new();
|
||||
reader.read_line(&mut session_id)?;
|
||||
) -> Result<Self> {
|
||||
// Read the session token
|
||||
let mut session_token = String::new();
|
||||
reader
|
||||
.read_line(&mut session_token)
|
||||
.map_err(|_| crate::response::Error::InvalidHandshake)?;
|
||||
|
||||
// Validate session id
|
||||
let response = minreq::get("https://api.minecraftservices.com/minecraft/profile")
|
||||
.with_header("Authorization", &format!("Bearer {session_token}"))
|
||||
.send()
|
||||
.map_err(|_| crate::response::Error::InvalidSession)?;
|
||||
|
||||
// If the session is invalid, return an error
|
||||
if response.status_code != 200 {
|
||||
return Err(crate::response::Error::InvalidSession);
|
||||
}
|
||||
|
||||
// Parse the player data
|
||||
let player: LocalPlayer = response
|
||||
.json()
|
||||
.map_err(|_| crate::response::Error::InvalidSession)?;
|
||||
|
||||
Ok(Self {
|
||||
session_id,
|
||||
session_token,
|
||||
database,
|
||||
player,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn handle_request(
|
||||
&self,
|
||||
method: &str,
|
||||
params: &HashMap<String, String>,
|
||||
) -> crate::response::Response {
|
||||
pub fn handle_request(&self, method: &str, params: &HashMap<String, String>) -> Result {
|
||||
match method {
|
||||
"ping" => crate::response::Response::Pong,
|
||||
"ping" => Ok(Response::Pong),
|
||||
|
||||
_ => crate::response::Response::Error(crate::response::Error::InvalidRequest),
|
||||
"set_cloak" => set_cloak::set_cloak(self, params.parse_param("cloak")?),
|
||||
|
||||
_ => Err(crate::response::Error::InvalidRequest),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-4
@@ -1,14 +1,14 @@
|
||||
use std::{collections::HashMap, str::FromStr};
|
||||
|
||||
pub trait ParamMap {
|
||||
fn parse_param<T: FromStr>(&self, param: &str) -> Result<T, String>;
|
||||
fn parse_param<T: FromStr>(&self, param: &str) -> Result<T, crate::response::Error>;
|
||||
}
|
||||
|
||||
impl ParamMap for HashMap<String, String> {
|
||||
fn parse_param<T: FromStr>(&self, param: &str) -> Result<T, String> {
|
||||
fn parse_param<T: FromStr>(&self, param: &str) -> Result<T, crate::response::Error> {
|
||||
match self.get(param) {
|
||||
Some(value) => T::from_str(value).map_err(|_| format!("Invalid parameter: {}", param)),
|
||||
None => Err(format!("Parameter not found: {}", param)),
|
||||
Some(value) => T::from_str(value).map_err(|_| crate::response::Error::InvalidParameter),
|
||||
None => Err(crate::response::Error::InvalidParameter),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+8
-4
@@ -1,14 +1,16 @@
|
||||
pub type Result<T = Response> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Response {
|
||||
Pong,
|
||||
Error(Error),
|
||||
Success,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Response {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Response::Pong => write!(f, "Pong"),
|
||||
Response::Error(error) => write!(f, "!{}", error),
|
||||
Response::Success => write!(f, "Success"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,7 +21,8 @@ pub enum Error {
|
||||
InvalidMethod,
|
||||
InvalidParameter,
|
||||
ParameterNotFound,
|
||||
InvalidSessionToken,
|
||||
InvalidSession,
|
||||
InvalidHandshake,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Error {
|
||||
@@ -29,7 +32,8 @@ impl std::fmt::Display for Error {
|
||||
Error::InvalidMethod => write!(f, "Invalid method"),
|
||||
Error::InvalidParameter => write!(f, "Invalid parameter"),
|
||||
Error::ParameterNotFound => write!(f, "Parameter not found"),
|
||||
Error::InvalidSessionToken => write!(f, "Invalid session token"),
|
||||
Error::InvalidSession => write!(f, "Invalid session"),
|
||||
Error::InvalidHandshake => write!(f, "Invalid handshake"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user