From 2e6606a8f2612df3353064110a720f4fda1d33ef Mon Sep 17 00:00:00 2001 From: Jake Probst Date: Sat, 24 Aug 2019 14:45:58 -0700 Subject: [PATCH] new serverstatus stuff plus a login test! --- src/login/character.rs | 44 ++++++++++---- src/login/login.rs | 128 +++++++++++++++++++++++++++++++++++------ 2 files changed, 141 insertions(+), 31 deletions(-) diff --git a/src/login/character.rs b/src/login/character.rs index 62c2062..29cbea3 100644 --- a/src/login/character.rs +++ b/src/login/character.rs @@ -13,7 +13,7 @@ use elseware::common::pktvec::PktVec; use elseware::common::cipherkeys::{ELSEWHERE_PRIVATE_KEY, ELSEWHERE_PARRAY}; use elseware::common::network::{PacketNetworkError}; use elseware::common::client::Client; -use elseware::common::serverstate::{ServerPacket, ServerState, OnConnect}; +use elseware::common::serverstate::{SendServerPacket, RecvServerPacket, ServerState, OnConnect}; use elseware::common::util::array_to_utf8; use crate::dataaccess::DataAccess; @@ -27,21 +27,39 @@ pub enum CharacterError { } #[derive(Debug)] -pub enum CharacterPacket { +pub enum RecvCharacterPacket { Login(Login), RequestSettings(RequestSettings), } -impl ServerPacket for CharacterPacket { - fn from_bytes(data: &Vec) -> Result { +impl RecvServerPacket for RecvCharacterPacket { + fn from_bytes(data: &Vec) -> Result { match data[2] { - 0x93 => Ok(CharacterPacket::Login(Login::from_bytes(data)?)), - 0xE0 => Ok(CharacterPacket::RequestSettings(RequestSettings::from_bytes(data)?)), + 0x93 => Ok(RecvCharacterPacket::Login(Login::from_bytes(data)?)), + 0xE0 => Ok(RecvCharacterPacket::RequestSettings(RequestSettings::from_bytes(data)?)), _ => Err(PacketParseError::WrongPacketForServerType) } } } + +#[derive(Debug)] +pub enum SendCharacterPacket { + //LoginResponse(LoginResponse), + LoginWelcome(LoginWelcome), + //RedirectClient(RedirectClient), +} + +impl SendServerPacket for SendCharacterPacket { + fn as_bytes(&self) -> Vec { + match self { + //SendCharacterPacket::LoginResponse(pkt) => pkt.as_bytes(), + SendCharacterPacket::LoginWelcome(pkt) => pkt.as_bytes(), + //SendLoginPacket::RedirectClient(pkt) => pkt.as_bytes(), + } + } +} + pub struct CharacterServerState { shared_state: SharedLoginState, user: Option, @@ -76,10 +94,11 @@ impl CharacterServerState { } impl ServerState for CharacterServerState { - type Packet = CharacterPacket; + type SendPacket = SendCharacterPacket; + type RecvPacket = RecvCharacterPacket; type PacketError = CharacterError; - fn on_connect(&mut self) -> Vec { + fn on_connect(&mut self) -> Vec> { let mut rng = rand::thread_rng(); let mut server_key = [0u8; 48]; @@ -87,22 +106,23 @@ impl ServerState for CharacterServerState { rng.fill(&mut server_key[..]); rng.fill(&mut client_key[..]); - vec![OnConnect::Packet(Box::new(LoginWelcome::new(server_key, client_key))), + vec![OnConnect::Packet(SendCharacterPacket::LoginWelcome(LoginWelcome::new(server_key, client_key))), OnConnect::Cipher((Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, client_key)), Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, server_key)))) ] } - fn handle(&mut self, pkt: &CharacterPacket) -> Box>> { + fn handle(&mut self, pkt: &RecvCharacterPacket) -> Box> { println!("[character: recv] {:?}", pkt); - match pkt { + /*match pkt { CharacterPacket::Login(login) => { self.validate_login(login) }, CharacterPacket::RequestSettings(_req) => { self.get_settings() } - } + }*/ + Box::new(None.into_iter()) } } diff --git a/src/login/login.rs b/src/login/login.rs index 963777a..1920c9c 100644 --- a/src/login/login.rs +++ b/src/login/login.rs @@ -10,39 +10,57 @@ use libpso::{PacketParseError, PSOPacket}; use libpso::crypto::{CipherError, PSOCipher, NullCipher}; use libpso::crypto::bb::PSOBBCipher; -use elseware::pktvec; +use elseware::{pktvec, utf8_to_array}; use elseware::common::pktvec::PktVec; use elseware::common::cipherkeys::{ELSEWHERE_PRIVATE_KEY, ELSEWHERE_PARRAY}; //use elseware::common::network::{PacketNetworkError}; use elseware::common::client::Client; -use elseware::common::serverstate::{ServerPacket, ServerState, OnConnect}; +use elseware::common::serverstate::{SendServerPacket, RecvServerPacket, ServerState, OnConnect}; use elseware::common::util::array_to_utf8; use crate::dataaccess::DataAccess; use crate::models::UserAccount; pub const LOGIN_PORT: u16 = 12000; -//type ConnectionPool = r2d2::Pool>; #[derive(Debug)] pub enum LoginError { } -#[derive(Debug)] -pub enum LoginPacket { +#[derive(Debug, PartialEq)] +pub enum RecvLoginPacket { Login(Login), } -impl ServerPacket for LoginPacket { - fn from_bytes(data: &Vec) -> Result { +impl RecvServerPacket for RecvLoginPacket { + fn from_bytes(data: &Vec) -> Result { match data[2] { - 0x93 => Ok(LoginPacket::Login(Login::from_bytes(data)?)), + 0x93 => Ok(RecvLoginPacket::Login(Login::from_bytes(data)?)), _ => Err(PacketParseError::WrongPacketForServerType) } } } + +#[derive(Debug, PartialEq)] +pub enum SendLoginPacket { + LoginResponse(LoginResponse), + LoginWelcome(LoginWelcome), + RedirectClient(RedirectClient), +} + +impl SendServerPacket for SendLoginPacket { + fn as_bytes(&self) -> Vec { + match self { + SendLoginPacket::LoginResponse(pkt) => pkt.as_bytes(), + SendLoginPacket::LoginWelcome(pkt) => pkt.as_bytes(), + SendLoginPacket::RedirectClient(pkt) => pkt.as_bytes(), + } + } +} + + #[derive(Clone)] pub struct SharedLoginState { pub data_access: DA, @@ -56,7 +74,6 @@ impl SharedLoginState { } } - pub struct LoginServerState { pub shared_state: SharedLoginState, } @@ -81,26 +98,28 @@ impl LoginServerState { } } - fn validate_login(&mut self, pkt: &Login) -> Box { + fn validate_login(&mut self, pkt: &Login) -> Vec { match get_login_status(&self.shared_state.data_access, pkt) { Ok(_user) => { - let response = LoginResponse::by_status(AccountStatus::Ok, pkt.security_data); + let response = SendLoginPacket::LoginResponse(LoginResponse::by_status(AccountStatus::Ok, pkt.security_data)); let ip = net::Ipv4Addr::new(127,0,0,1); let ip = u32::from_ne_bytes(ip.octets()); - pktvec![response, RedirectClient::new(ip, crate::character::CHARACTER_PORT)] + vec![response, + SendLoginPacket::RedirectClient(RedirectClient::new(ip, crate::character::CHARACTER_PORT))] }, Err(err) => { - pktvec![LoginResponse::by_status(err, pkt.security_data)] + vec![SendLoginPacket::LoginResponse(LoginResponse::by_status(err, pkt.security_data))] } } } } impl ServerState for LoginServerState { - type Packet = LoginPacket; + type SendPacket = SendLoginPacket; + type RecvPacket = RecvLoginPacket; type PacketError = LoginError; - fn on_connect(&mut self) -> Vec { + fn on_connect(&mut self) -> Vec> { let mut rng = rand::thread_rng(); let mut server_key = [0u8; 48]; @@ -108,16 +127,16 @@ impl ServerState for LoginServerState { rng.fill(&mut server_key[..]); rng.fill(&mut client_key[..]); - vec![OnConnect::Packet(Box::new(LoginWelcome::new(server_key, client_key))), + vec![OnConnect::Packet(SendLoginPacket::LoginWelcome(LoginWelcome::new(server_key, client_key))), OnConnect::Cipher((Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, client_key)), Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, server_key)))) ] } - fn handle(&mut self, pkt: &LoginPacket) -> Box>> { + fn handle(&mut self, pkt: &Self::RecvPacket) -> Box> { match pkt { - LoginPacket::Login(login) => { - self.validate_login(login) + RecvLoginPacket::Login(login) => { + Box::new(self.validate_login(login).into_iter()) } } } @@ -129,3 +148,74 @@ pub fn new_client(socket: mio::tcp::TcpStream, shared_ let client = Client::new(socket, Box::new(state)); client.io_loop(); } + +#[cfg(test)] +mod test { + use std::time::SystemTime; + use super::*; + + #[test] + fn test_correct_login() { + struct TestData { + } + + impl DataAccess for TestData { + fn get_user_by_name(&self, name: String) -> Option { + assert!(name == "testuser"); + Some(UserAccount { + id: 1, + username: "testuser".to_owned(), + password: bcrypt::hash("mypassword", 5).unwrap(), + guildcard: None, + team_id: None, + banned: false, + muted_until: SystemTime::now(), + created_at: SystemTime::now(), + }) + } + } + + let shared = SharedLoginState::new(TestData {}); + let mut server = LoginServerState::new(shared); + + //let username: [u8; 16] = utf8_to_array!("testuser", 16); + //let password: [u8; 16] = utf8_to_array!("mypassword", 16); + let send = server.handle(&RecvLoginPacket::Login(Login { + flag: 0, + tag: 65536, + guildcard: 0, + version: 65, + unknown1: [0, 0, 0, 255, 0, 14], + team: 0, + username: utf8_to_array!("testuser", 16), + unknown2: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + password: utf8_to_array!("mypassword", 16), + unknown3: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0], + hwinfo: [129, 1, 1, 1, 1, 1, 1, 1], + security_data: [74, 97, 107, 101, 115, 101, 114, 118, 50, 48, 50, 48, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + + })).collect::>(); + println!("{:?}", send); + assert!(send == vec![ + SendLoginPacket::LoginResponse(LoginResponse { + flag: 0, + status: AccountStatus::Ok, + tag: 65536, + guildcard: 0, + team_id: 0, + security_data: [74, 97, 107, 101, 115, 101, 114, 118, 50, 48, 50, 48, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + caps: 258 + }), + SendLoginPacket::RedirectClient(RedirectClient { + flag: 0, + ip: 16777343, + port: 12001, + padding: 0, + })]) + } +}