|
|
@ -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,
|
|
|
|
})])
|
|
|
|
}
|
|
|
|
}
|