|
@ -1,13 +1,10 @@ |
|
|
|
|
|
// TODO: rename this module to auth
|
|
|
|
|
|
|
|
|
use std::net;
|
|
|
use std::net;
|
|
|
|
|
|
|
|
|
use rand::{Rng, RngCore};
|
|
|
use rand::{Rng, RngCore};
|
|
|
use bcrypt::{DEFAULT_COST, hash, verify};
|
|
|
use bcrypt::{DEFAULT_COST, hash, verify};
|
|
|
|
|
|
|
|
|
use diesel::r2d2;
|
|
|
|
|
|
use diesel::prelude::*;
|
|
|
|
|
|
use diesel::pg::PgConnection;
|
|
|
|
|
|
use elseware::schema::*;
|
|
|
|
|
|
|
|
|
|
|
|
use libpso::packet::login::*;
|
|
|
use libpso::packet::login::*;
|
|
|
use libpso::{PacketParseError, PSOPacket};
|
|
|
use libpso::{PacketParseError, PSOPacket};
|
|
|
use libpso::crypto::{CipherError, PSOCipher, NullCipher};
|
|
|
use libpso::crypto::{CipherError, PSOCipher, NullCipher};
|
|
@ -16,15 +13,16 @@ use libpso::crypto::bb::PSOBBCipher; |
|
|
use elseware::pktvec;
|
|
|
use elseware::pktvec;
|
|
|
use elseware::common::pktvec::PktVec;
|
|
|
use elseware::common::pktvec::PktVec;
|
|
|
use elseware::common::cipherkeys::{ELSEWHERE_PRIVATE_KEY, ELSEWHERE_PARRAY};
|
|
|
use elseware::common::cipherkeys::{ELSEWHERE_PRIVATE_KEY, ELSEWHERE_PARRAY};
|
|
|
use elseware::common::network::{PacketNetworkError};
|
|
|
|
|
|
|
|
|
//use elseware::common::network::{PacketNetworkError};
|
|
|
use elseware::common::client::Client;
|
|
|
use elseware::common::client::Client;
|
|
|
use elseware::common::serverstate::{ServerPacket, ServerState, OnConnect};
|
|
|
use elseware::common::serverstate::{ServerPacket, ServerState, OnConnect};
|
|
|
use elseware::common::util::array_to_utf8;
|
|
|
use elseware::common::util::array_to_utf8;
|
|
|
|
|
|
|
|
|
|
|
|
use crate::dataaccess::DataAccess;
|
|
|
use crate::models::UserAccount;
|
|
|
use crate::models::UserAccount;
|
|
|
|
|
|
|
|
|
pub const LOGIN_PORT: u16 = 12000;
|
|
|
pub const LOGIN_PORT: u16 = 12000;
|
|
|
type ConnectionPool = r2d2::Pool<r2d2::ConnectionManager<PgConnection>>;
|
|
|
|
|
|
|
|
|
//type ConnectionPool = r2d2::Pool<r2d2::ConnectionManager<PgConnection>>;
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
#[derive(Debug)]
|
|
|
pub enum LoginError {
|
|
|
pub enum LoginError {
|
|
@ -46,78 +44,45 @@ impl ServerPacket for LoginPacket { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
#[derive(Clone)]
|
|
|
pub struct SharedLoginState {
|
|
|
|
|
|
pub connection_pool: ConnectionPool,
|
|
|
|
|
|
|
|
|
pub struct SharedLoginState<DA: DataAccess> {
|
|
|
|
|
|
pub data_access: DA,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl SharedLoginState {
|
|
|
|
|
|
pub fn new(pool: ConnectionPool) -> SharedLoginState {
|
|
|
|
|
|
|
|
|
impl<DA: DataAccess> SharedLoginState<DA> {
|
|
|
|
|
|
pub fn new(data_access: DA) -> SharedLoginState<DA> {
|
|
|
SharedLoginState {
|
|
|
SharedLoginState {
|
|
|
connection_pool: pool,
|
|
|
|
|
|
|
|
|
data_access: data_access,
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct LoginServerState {
|
|
|
|
|
|
pub shared_state: SharedLoginState,
|
|
|
|
|
|
|
|
|
pub struct LoginServerState<DA: DataAccess> {
|
|
|
|
|
|
pub shared_state: SharedLoginState<DA>,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn get_login_status(connection_pool: &ConnectionPool, pkt: &Login) -> Result<UserAccount, AccountStatus> {
|
|
|
|
|
|
use elseware::schema::user_accounts::dsl::{user_accounts, username};
|
|
|
|
|
|
connection_pool.get()
|
|
|
|
|
|
.map_err(|_err| AccountStatus::Error)
|
|
|
|
|
|
.and_then(|conn| {
|
|
|
|
|
|
array_to_utf8(pkt.username)
|
|
|
|
|
|
.map(|username_str| {
|
|
|
|
|
|
(conn, username_str)
|
|
|
|
|
|
})
|
|
|
|
|
|
.map_err(|err| {
|
|
|
|
|
|
println!("utf err: {:?}", err);
|
|
|
|
|
|
AccountStatus::Error
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
.and_then(|(conn, username_str)| {
|
|
|
|
|
|
user_accounts.filter(username.eq(username_str)).load::<UserAccount>(&conn)
|
|
|
|
|
|
.map_err(|err| {
|
|
|
|
|
|
println!("sql err: {:?}", err);
|
|
|
|
|
|
AccountStatus::Error
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
.and_then(|mut result| {
|
|
|
|
|
|
result.pop().ok_or(AccountStatus::InvalidUser)
|
|
|
|
|
|
})
|
|
|
|
|
|
.and_then(|user| {
|
|
|
|
|
|
array_to_utf8(pkt.password)
|
|
|
|
|
|
.map(|password| {
|
|
|
|
|
|
(user, password)
|
|
|
|
|
|
})
|
|
|
|
|
|
.map_err(|_err| AccountStatus::Error)
|
|
|
|
|
|
})
|
|
|
|
|
|
.and_then(|(user, password)| {
|
|
|
|
|
|
bcrypt::verify(password, user.password.as_str())
|
|
|
|
|
|
.map_err(|err| {
|
|
|
|
|
|
println!("bcrypt err: {:?}", err);
|
|
|
|
|
|
AccountStatus::Error
|
|
|
|
|
|
})
|
|
|
|
|
|
.and_then(|correct_password| {
|
|
|
|
|
|
match correct_password {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get_login_status(data_access: &dyn DataAccess, pkt: &Login) -> Result<UserAccount, AccountStatus> {
|
|
|
|
|
|
let username = array_to_utf8(pkt.username).map_err(|_err| AccountStatus::Error)?;
|
|
|
|
|
|
let password = array_to_utf8(pkt.password).map_err(|_err| AccountStatus::Error)?;
|
|
|
|
|
|
let user = data_access.get_user_by_name(username).ok_or(AccountStatus::InvalidUser)?;
|
|
|
|
|
|
let verified = bcrypt::verify(password, user.password.as_str()).map_err(|_err| AccountStatus::Error)?;
|
|
|
|
|
|
match verified {
|
|
|
true => Ok(user),
|
|
|
true => Ok(user),
|
|
|
false => Err(AccountStatus::InvalidPassword),
|
|
|
|
|
|
|
|
|
false => Err(AccountStatus::InvalidPassword)
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl LoginServerState {
|
|
|
|
|
|
fn new(shared_state: SharedLoginState) -> LoginServerState {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<DA: DataAccess> LoginServerState<DA> {
|
|
|
|
|
|
fn new(shared_state: SharedLoginState<DA>) -> LoginServerState<DA> {
|
|
|
LoginServerState {
|
|
|
LoginServerState {
|
|
|
shared_state: shared_state,
|
|
|
shared_state: shared_state,
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn validate_login(&mut self, pkt: &Login) -> Box<PktVec> {
|
|
|
fn validate_login(&mut self, pkt: &Login) -> Box<PktVec> {
|
|
|
match get_login_status(&self.shared_state.connection_pool, pkt) {
|
|
|
|
|
|
|
|
|
match get_login_status(&self.shared_state.data_access, pkt) {
|
|
|
Ok(_user) => {
|
|
|
Ok(_user) => {
|
|
|
let response = LoginResponse::by_status(AccountStatus::Ok, pkt.security_data);
|
|
|
let response = LoginResponse::by_status(AccountStatus::Ok, pkt.security_data);
|
|
|
let ip = net::Ipv4Addr::new(127,0,0,1);
|
|
|
let ip = net::Ipv4Addr::new(127,0,0,1);
|
|
@ -131,7 +96,7 @@ impl LoginServerState { |
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
impl ServerState for LoginServerState {
|
|
|
|
|
|
|
|
|
impl<DA: DataAccess> ServerState for LoginServerState<DA> {
|
|
|
type Packet = LoginPacket;
|
|
|
type Packet = LoginPacket;
|
|
|
type PacketError = LoginError;
|
|
|
type PacketError = LoginError;
|
|
|
|
|
|
|
|
@ -150,7 +115,6 @@ impl ServerState for LoginServerState { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
fn handle(&mut self, pkt: &LoginPacket) -> Box<dyn Iterator<Item = Box<dyn PSOPacket>>> {
|
|
|
fn handle(&mut self, pkt: &LoginPacket) -> Box<dyn Iterator<Item = Box<dyn PSOPacket>>> {
|
|
|
println!("[login: recv] {:?}", pkt);
|
|
|
|
|
|
match pkt {
|
|
|
match pkt {
|
|
|
LoginPacket::Login(login) => {
|
|
|
LoginPacket::Login(login) => {
|
|
|
self.validate_login(login)
|
|
|
self.validate_login(login)
|
|
@ -160,9 +124,7 @@ impl ServerState for LoginServerState { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn new_client(socket: mio::tcp::TcpStream, shared_state: SharedLoginState) {
|
|
|
|
|
|
|
|
|
pub fn new_client<DA: DataAccess + 'static>(socket: mio::tcp::TcpStream, shared_state: SharedLoginState<DA>) {
|
|
|
let state = LoginServerState::new(shared_state);
|
|
|
let state = LoginServerState::new(shared_state);
|
|
|
let client = Client::new(socket, Box::new(state));
|
|
|
let client = Client::new(socket, Box::new(state));
|
|
|
client.io_loop();
|
|
|
client.io_loop();
|
|
|