Browse Source

new serverstatus stuff plus a login test!

pbs
Jake Probst 5 years ago
parent
commit
2e6606a8f2
  1. 44
      src/login/character.rs
  2. 128
      src/login/login.rs

44
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<u8>) -> Result<CharacterPacket, PacketParseError> {
impl RecvServerPacket for RecvCharacterPacket {
fn from_bytes(data: &Vec<u8>) -> Result<RecvCharacterPacket, PacketParseError> {
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<u8> {
match self {
//SendCharacterPacket::LoginResponse(pkt) => pkt.as_bytes(),
SendCharacterPacket::LoginWelcome(pkt) => pkt.as_bytes(),
//SendLoginPacket::RedirectClient(pkt) => pkt.as_bytes(),
}
}
}
pub struct CharacterServerState<DA: DataAccess> {
shared_state: SharedLoginState<DA>,
user: Option<UserAccount>,
@ -76,10 +94,11 @@ impl<DA: DataAccess> CharacterServerState<DA> {
}
impl<DA: DataAccess> ServerState for CharacterServerState<DA> {
type Packet = CharacterPacket;
type SendPacket = SendCharacterPacket;
type RecvPacket = RecvCharacterPacket;
type PacketError = CharacterError;
fn on_connect(&mut self) -> Vec<OnConnect> {
fn on_connect(&mut self) -> Vec<OnConnect<Self::SendPacket>> {
let mut rng = rand::thread_rng();
let mut server_key = [0u8; 48];
@ -87,22 +106,23 @@ impl<DA: DataAccess> ServerState for CharacterServerState<DA> {
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<dyn Iterator<Item = Box<dyn PSOPacket>>> {
fn handle(&mut self, pkt: &RecvCharacterPacket) -> Box<dyn Iterator<Item = SendCharacterPacket>> {
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())
}
}

128
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<r2d2::ConnectionManager<PgConnection>>;
#[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<u8>) -> Result<LoginPacket, PacketParseError> {
impl RecvServerPacket for RecvLoginPacket {
fn from_bytes(data: &Vec<u8>) -> Result<RecvLoginPacket, PacketParseError> {
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<u8> {
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<DA: DataAccess> {
pub data_access: DA,
@ -56,7 +74,6 @@ impl<DA: DataAccess> SharedLoginState<DA> {
}
}
pub struct LoginServerState<DA: DataAccess> {
pub shared_state: SharedLoginState<DA>,
}
@ -81,26 +98,28 @@ impl<DA: DataAccess> LoginServerState<DA> {
}
}
fn validate_login(&mut self, pkt: &Login) -> Box<PktVec> {
fn validate_login(&mut self, pkt: &Login) -> Vec<SendLoginPacket> {
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<DA: DataAccess> ServerState for LoginServerState<DA> {
type Packet = LoginPacket;
type SendPacket = SendLoginPacket;
type RecvPacket = RecvLoginPacket;
type PacketError = LoginError;
fn on_connect(&mut self) -> Vec<OnConnect> {
fn on_connect(&mut self) -> Vec<OnConnect<Self::SendPacket>> {
let mut rng = rand::thread_rng();
let mut server_key = [0u8; 48];
@ -108,16 +127,16 @@ impl<DA: DataAccess> ServerState for LoginServerState<DA> {
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<dyn Iterator<Item = Box<dyn PSOPacket>>> {
fn handle(&mut self, pkt: &Self::RecvPacket) -> Box<dyn Iterator<Item = Self::SendPacket>> {
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<DA: DataAccess + 'static>(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<UserAccount> {
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::<Vec<_>>();
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,
})])
}
}
Loading…
Cancel
Save