Jake Probst
5 years ago
3 changed files with 350 additions and 89 deletions
@ -0,0 +1,123 @@ |
|||||
|
use std::net;
|
||||
|
|
||||
|
use rand::{Rng, RngCore};
|
||||
|
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::{PacketParseError, PSOPacket};
|
||||
|
use libpso::crypto::{CipherError, PSOCipher, NullCipher};
|
||||
|
use libpso::crypto::bb::PSOBBCipher;
|
||||
|
|
||||
|
use elseware::pktvec;
|
||||
|
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::util::array_to_utf8;
|
||||
|
|
||||
|
use crate::login::{SharedLoginState, get_login_status};
|
||||
|
use crate::models::UserAccount;
|
||||
|
|
||||
|
pub const CHARACTER_PORT: u16 = 12001;
|
||||
|
type ConnectionPool = r2d2::Pool<r2d2::ConnectionManager<PgConnection>>;
|
||||
|
|
||||
|
#[derive(Debug)]
|
||||
|
pub enum CharacterError {
|
||||
|
}
|
||||
|
|
||||
|
#[derive(Debug)]
|
||||
|
pub enum CharacterPacket {
|
||||
|
Login(Login),
|
||||
|
RequestSettings(RequestSettings),
|
||||
|
}
|
||||
|
|
||||
|
impl ServerPacket for CharacterPacket {
|
||||
|
fn from_bytes(data: &Vec<u8>) -> Result<CharacterPacket, PacketParseError> {
|
||||
|
match data[2] {
|
||||
|
0x93 => Ok(CharacterPacket::Login(Login::from_bytes(data)?)),
|
||||
|
0xE0 => Ok(CharacterPacket::RequestSettings(RequestSettings::from_bytes(data)?)),
|
||||
|
_ => Err(PacketParseError::WrongPacketForServerType)
|
||||
|
}
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
pub struct CharacterServerState {
|
||||
|
shared_state: SharedLoginState,
|
||||
|
user: Option<UserAccount>,
|
||||
|
}
|
||||
|
|
||||
|
impl CharacterServerState {
|
||||
|
fn new(shared_state: SharedLoginState) -> CharacterServerState {
|
||||
|
CharacterServerState {
|
||||
|
shared_state: shared_state,
|
||||
|
user: None,
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
fn validate_login(&mut self, pkt: &Login) -> Box<PktVec> {
|
||||
|
match get_login_status(&self.shared_state.connection_pool, pkt) {
|
||||
|
Ok(user) => {
|
||||
|
let mut response = LoginResponse::by_status(AccountStatus::Ok, pkt.security_data);
|
||||
|
response.guildcard = user.guildcard.map_or(0, |gc| gc) as u32;
|
||||
|
response.team_id = user.team_id.map_or(0, |ti| ti) as u32;
|
||||
|
self.user = Some(user);
|
||||
|
pktvec![response]
|
||||
|
},
|
||||
|
Err(err) => {
|
||||
|
pktvec![LoginResponse::by_status(err, pkt.security_data)]
|
||||
|
}
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
fn get_settings(&mut self) -> Box<PktVec> {
|
||||
|
pktvec![]
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
impl ServerState for CharacterServerState {
|
||||
|
type Packet = CharacterPacket;
|
||||
|
type PacketError = CharacterError;
|
||||
|
|
||||
|
fn on_connect(&mut self) -> Vec<OnConnect> {
|
||||
|
let mut rng = rand::thread_rng();
|
||||
|
|
||||
|
let mut server_key = [0u8; 48];
|
||||
|
let mut client_key = [0u8; 48];
|
||||
|
rng.fill(&mut server_key[..]);
|
||||
|
rng.fill(&mut client_key[..]);
|
||||
|
|
||||
|
vec![OnConnect::Packet(Box::new(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>>> {
|
||||
|
println!("[character: recv] {:?}", pkt);
|
||||
|
match pkt {
|
||||
|
CharacterPacket::Login(login) => {
|
||||
|
self.validate_login(login)
|
||||
|
},
|
||||
|
CharacterPacket::RequestSettings(_req) => {
|
||||
|
self.get_settings()
|
||||
|
}
|
||||
|
|
||||
|
}
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
pub fn new_client(socket: mio::tcp::TcpStream, shared_state: SharedLoginState) {
|
||||
|
let state = CharacterServerState::new(shared_state);
|
||||
|
let client = Client::new(socket, Box::new(state));
|
||||
|
client.io_loop();
|
||||
|
}
|
@ -0,0 +1,169 @@ |
|||||
|
use std::net;
|
||||
|
|
||||
|
use rand::{Rng, RngCore};
|
||||
|
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::{PacketParseError, PSOPacket};
|
||||
|
use libpso::crypto::{CipherError, PSOCipher, NullCipher};
|
||||
|
use libpso::crypto::bb::PSOBBCipher;
|
||||
|
|
||||
|
use elseware::pktvec;
|
||||
|
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::util::array_to_utf8;
|
||||
|
|
||||
|
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 {
|
||||
|
Login(Login),
|
||||
|
}
|
||||
|
|
||||
|
impl ServerPacket for LoginPacket {
|
||||
|
fn from_bytes(data: &Vec<u8>) -> Result<LoginPacket, PacketParseError> {
|
||||
|
match data[2] {
|
||||
|
0x93 => Ok(LoginPacket::Login(Login::from_bytes(data)?)),
|
||||
|
_ => Err(PacketParseError::WrongPacketForServerType)
|
||||
|
}
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
#[derive(Clone)]
|
||||
|
pub struct SharedLoginState {
|
||||
|
pub connection_pool: ConnectionPool,
|
||||
|
}
|
||||
|
|
||||
|
impl SharedLoginState {
|
||||
|
pub fn new(pool: ConnectionPool) -> SharedLoginState {
|
||||
|
SharedLoginState {
|
||||
|
connection_pool: pool,
|
||||
|
}
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
|
||||
|
pub struct LoginServerState {
|
||||
|
pub shared_state: SharedLoginState,
|
||||
|
}
|
||||
|
|
||||
|
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 {
|
||||
|
true => Ok(user),
|
||||
|
false => Err(AccountStatus::InvalidPassword),
|
||||
|
}
|
||||
|
})
|
||||
|
})
|
||||
|
}
|
||||
|
|
||||
|
impl LoginServerState {
|
||||
|
fn new(shared_state: SharedLoginState) -> LoginServerState {
|
||||
|
LoginServerState {
|
||||
|
shared_state: shared_state,
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
fn validate_login(&mut self, pkt: &Login) -> Box<PktVec> {
|
||||
|
match get_login_status(&self.shared_state.connection_pool, pkt) {
|
||||
|
Ok(_user) => {
|
||||
|
let response = 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)]
|
||||
|
},
|
||||
|
Err(err) => {
|
||||
|
pktvec![LoginResponse::by_status(err, pkt.security_data)]
|
||||
|
}
|
||||
|
}
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
impl ServerState for LoginServerState {
|
||||
|
type Packet = LoginPacket;
|
||||
|
type PacketError = LoginError;
|
||||
|
|
||||
|
fn on_connect(&mut self) -> Vec<OnConnect> {
|
||||
|
let mut rng = rand::thread_rng();
|
||||
|
|
||||
|
let mut server_key = [0u8; 48];
|
||||
|
let mut client_key = [0u8; 48];
|
||||
|
rng.fill(&mut server_key[..]);
|
||||
|
rng.fill(&mut client_key[..]);
|
||||
|
|
||||
|
vec![OnConnect::Packet(Box::new(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>>> {
|
||||
|
println!("[login: recv] {:?}", pkt);
|
||||
|
match pkt {
|
||||
|
LoginPacket::Login(login) => {
|
||||
|
self.validate_login(login)
|
||||
|
}
|
||||
|
}
|
||||
|
}
|
||||
|
}
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
pub fn new_client(socket: mio::tcp::TcpStream, shared_state: SharedLoginState) {
|
||||
|
let state = LoginServerState::new(shared_state);
|
||||
|
let client = Client::new(socket, Box::new(state));
|
||||
|
client.io_loop();
|
||||
|
}
|
@ -1,110 +1,79 @@ |
|||||
use std::net::{TcpListener, SocketAddr, Ipv4Addr};
|
|
||||
|
mod login;
|
||||
|
mod character;
|
||||
|
mod models;
|
||||
|
|
||||
|
use std::net::{SocketAddr, Ipv4Addr};
|
||||
use std::net;
|
use std::net;
|
||||
use std::thread;
|
use std::thread;
|
||||
|
use std::env;
|
||||
|
|
||||
|
use mio::tcp::TcpListener;
|
||||
use mio::{Events, Poll, Token, Ready, PollOpt};
|
use mio::{Events, Poll, Token, Ready, PollOpt};
|
||||
use rand::{Rng, RngCore};
|
|
||||
|
use dotenv::dotenv;
|
||||
|
|
||||
use libpso::{PacketParseError, PSOPacket};
|
|
||||
use libpso::packet::login::*;
|
|
||||
use libpso::crypto::{CipherError, PSOCipher, NullCipher};
|
|
||||
use libpso::crypto::bb::PSOBBCipher;
|
|
||||
|
use diesel::r2d2;
|
||||
|
use diesel::prelude::*;
|
||||
|
use diesel::pg::PgConnection;
|
||||
|
|
||||
use elseware::common::network::{send_packet, recv_packet, PacketNetworkError};
|
|
||||
use elseware::common::cipherkeys::{ELSEWHERE_PRIVATE_KEY, ELSEWHERE_PARRAY};
|
|
||||
|
use models::{NewUser, UserAccount};
|
||||
|
|
||||
const LOGIN_PORT: u16 = 12000;
|
|
||||
const CHARACTER_PORT: u16 = 12001;
|
|
||||
|
fn main() {
|
||||
|
dotenv().ok();
|
||||
|
|
||||
|
//let database_url = env::var("DATABASE_URL").unwrap();
|
||||
|
//let conn = PgConnection::establish(&database_url).unwrap();
|
||||
|
|
||||
struct Client {
|
|
||||
running: bool,
|
|
||||
socket: mio::tcp::TcpStream,
|
|
||||
cipher_in: Box<dyn PSOCipher>,
|
|
||||
cipher_out: Box<dyn PSOCipher>,
|
|
||||
}
|
|
||||
|
//use elseware::schema::user_accounts::dsl::*;
|
||||
|
//use elseware::schema::user_accounts::table;
|
||||
|
|
||||
impl Client {
|
|
||||
fn new(socket: net::TcpStream) -> Client {
|
|
||||
Client {
|
|
||||
running: true,
|
|
||||
socket: mio::tcp::TcpStream::from_stream(socket).expect("could not convert socket to nonblocking"),
|
|
||||
cipher_in: Box::new(NullCipher {}),
|
|
||||
cipher_out: Box::new(NullCipher {}),
|
|
||||
}
|
|
||||
}
|
|
||||
|
/*let u = NewUser::new("hi".to_string(), "qwer".to_string());
|
||||
|
diesel::insert_into(user_accounts).values(&u).execute(&conn).unwrap();
|
||||
|
let u = NewUser::new("hi2".to_string(), "qwer".to_string());
|
||||
|
diesel::insert_into(user_accounts).values(&u).execute(&conn).unwrap();*/
|
||||
|
|
||||
fn send(&mut self, pkt: &dyn PSOPacket) {
|
|
||||
match send_packet(&mut self.socket, &mut *self.cipher_out, pkt) {
|
|
||||
Ok(_) => {
|
|
||||
println!("[login] send ({:?}): {:?}", self.socket, pkt);
|
|
||||
},
|
|
||||
Err(err) => {
|
|
||||
println!("[login] error sending packet to {:?}: {:?}", self.socket, err);
|
|
||||
self.running = false;
|
|
||||
}
|
|
||||
}
|
|
||||
}
|
|
||||
}
|
|
||||
|
println!("[login+character] starting server");
|
||||
|
|
||||
fn client_loop(mut client: Client) {
|
|
||||
let poll = mio::Poll::new().unwrap();
|
|
||||
poll.register(&client.socket, Token(0), Ready::readable(), PollOpt::edge()).unwrap();
|
|
||||
|
let database_url = env::var("DATABASE_URL").unwrap();
|
||||
|
let connection_manager = r2d2::ConnectionManager::<PgConnection>::new(database_url);
|
||||
|
let connection_pool = r2d2::Pool::builder()
|
||||
|
.build(connection_manager).unwrap();
|
||||
|
|
||||
let mut events = Events::with_capacity(1024);
|
|
||||
loop {
|
|
||||
poll.poll(&mut events, None).unwrap();
|
|
||||
|
|
||||
for event in &events{
|
|
||||
println!("event! {:?}", event);
|
|
||||
if event.token() == Token(0) {
|
|
||||
loop {
|
|
||||
let pkt = recv_packet(&mut client.socket, &mut *client.cipher_in);
|
|
||||
println!("{:?}", pkt);
|
|
||||
|
let login_listener = TcpListener::bind(&SocketAddr::from((Ipv4Addr::new(0,0,0,0), login::LOGIN_PORT))).unwrap();
|
||||
|
let character_listener = TcpListener::bind(&SocketAddr::from((Ipv4Addr::new(0,0,0,0), character::CHARACTER_PORT))).unwrap();
|
||||
|
|
||||
match pkt {
|
|
||||
Ok(pkt) => {
|
|
||||
//handle_packet(&mut client, pkt).expect("could not handle packet");
|
|
||||
println!("[login] pkt: {:?}", pkt);
|
|
||||
},
|
|
||||
Err(err) => {
|
|
||||
println!("[login] error recv-ing packet with {:?}: {:?}", client.socket, err);
|
|
||||
break;
|
|
||||
}
|
|
||||
}
|
|
||||
}
|
|
||||
}
|
|
||||
}
|
|
||||
}
|
|
||||
}
|
|
||||
|
let login_shared_state = login::SharedLoginState::new(connection_pool);
|
||||
|
|
||||
fn new_client(socket: net::TcpStream) {
|
|
||||
let mut client = Client::new(socket);
|
|
||||
let mut rng = rand::thread_rng();
|
|
||||
|
|
||||
let mut server_key = [0u8; 48];
|
|
||||
let mut client_key = [0u8; 48];
|
|
||||
rng.fill(&mut server_key[..]);
|
|
||||
rng.fill(&mut client_key[..]);
|
|
||||
let welcome = LoginWelcome::new(server_key, client_key);
|
|
||||
client.send(&welcome);
|
|
||||
client.cipher_in = Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, client_key));
|
|
||||
client.cipher_out = Box::new(PSOBBCipher::new(ELSEWHERE_PARRAY, ELSEWHERE_PRIVATE_KEY, server_key));
|
|
||||
client_loop(client);
|
|
||||
}
|
|
||||
|
let poll = Poll::new().unwrap();
|
||||
|
poll.register(&login_listener, Token(0), Ready::readable(), PollOpt::edge()).unwrap();
|
||||
|
poll.register(&character_listener, Token(1), Ready::readable(), PollOpt::edge()).unwrap();
|
||||
|
|
||||
fn main() {
|
|
||||
println!("[login] starting server");
|
|
||||
|
let mut events = Events::with_capacity(1024);
|
||||
|
|
||||
let listener = TcpListener::bind(&SocketAddr::from((Ipv4Addr::new(0,0,0,0), LOGIN_PORT))).unwrap();
|
|
||||
|
loop {
|
||||
|
poll.poll(&mut events, None).unwrap();
|
||||
|
|
||||
while let Ok((socket, addr)) = listener.accept() {
|
|
||||
|
for event in &events {
|
||||
|
match event.token() {
|
||||
|
Token(0) => {
|
||||
|
login_listener.accept().map(|(socket, addr)| {
|
||||
|
let shared_state_clone = login_shared_state.clone();
|
||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||
println!("[login] accepted connection: {}", addr);
|
println!("[login] accepted connection: {}", addr);
|
||||
new_client(socket);
|
|
||||
|
login::new_client(socket, shared_state_clone);
|
||||
|
});
|
||||
});
|
});
|
||||
|
},
|
||||
|
Token(1) => {
|
||||
|
character_listener.accept().map(|(socket, addr)| {
|
||||
|
let shared_state_clone = login_shared_state.clone();
|
||||
|
thread::spawn(move || {
|
||||
|
println!("[character] accepted connection: {}", addr);
|
||||
|
character::new_client(socket, shared_state_clone);
|
||||
|
});
|
||||
|
});
|
||||
|
},
|
||||
|
_ => {}
|
||||
|
}
|
||||
|
}
|
||||
}
|
}
|
||||
|
|
||||
|
|
||||
println!("[login] exiting...");
|
|
||||
}
|
}
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue