merge register.rs with creation.rs
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
81c6efb0de
commit
7cf34e7b9a
|
@ -1,5 +1,11 @@
|
|||
use log::warn;
|
||||
use data_encoding::HEXUPPER;
|
||||
|
||||
use crate::common::message::inst::CommandInst;
|
||||
use crate::common::message::message_builder::message_builder;
|
||||
use crate::common::message::message_type::MessageType;
|
||||
use crate::common::misc::assert_msg::assert_msg;
|
||||
|
||||
use crate::common::account::portfolio::Portfolio;
|
||||
use crate::common::message::message::Message;
|
||||
use crate::common::misc::return_flags::ReturnFlags;
|
||||
|
@ -8,10 +14,34 @@ use crate::server::account::hash_email::hash_email;
|
|||
use crate::server::account::hash_pwd::hash_pwd;
|
||||
use crate::server::ds::account::Account;
|
||||
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::net::TcpStream;
|
||||
use tokio_rustls::server::TlsStream;
|
||||
|
||||
pub async fn acc_create(
|
||||
sql_conn: &tokio_postgres::Client,
|
||||
tls_connection: &mut TlsStream<TcpStream>,
|
||||
message: &Message,
|
||||
) -> Result<(), ReturnFlags> {
|
||||
) -> std::io::Result<()> {
|
||||
/* assert recieved message */
|
||||
if !assert_msg(
|
||||
message,
|
||||
MessageType::Command,
|
||||
true,
|
||||
5,
|
||||
false,
|
||||
0,
|
||||
false,
|
||||
0,
|
||||
false,
|
||||
0,
|
||||
) && message.instruction == CommandInst::Register as i64
|
||||
&& message.data.len() != 0
|
||||
{
|
||||
warn!("REGISTER_INVALID_MESSAGE");
|
||||
return tls_connection.shutdown().await;
|
||||
}
|
||||
|
||||
/*
|
||||
* Parse account data
|
||||
* */
|
||||
|
@ -83,7 +113,20 @@ pub async fn acc_create(
|
|||
.await
|
||||
.unwrap()
|
||||
{
|
||||
return Err(ReturnFlags::ServerAccUserExists);
|
||||
/*
|
||||
* Inform cient that user already exists
|
||||
* Note: figure out if this is a security? issue
|
||||
*/
|
||||
let server_response =
|
||||
message_builder(MessageType::ServerReturn,
|
||||
0, 0, 0, 0,
|
||||
bincode::serialize(
|
||||
&format!("{:#?}", ReturnFlags::ServerAccUserExists)).unwrap());
|
||||
match tls_connection
|
||||
.write_all(&bincode::serialize(
|
||||
&server_response).unwrap()).await { // Don't care if user didn't recieve a reply
|
||||
_ => return Ok(())
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -101,14 +144,26 @@ pub async fn acc_create(
|
|||
/*
|
||||
* Write the account to the database.
|
||||
* */
|
||||
match sql_conn.execute("INSERT INTO accounts_schema.accounts \
|
||||
let creation_result = sql_conn.execute("INSERT INTO accounts_schema.accounts \
|
||||
(username, email_hash, server_email_salt, client_email_salt, pass_hash, server_pass_salt, client_pass_salt)
|
||||
VALUES \
|
||||
($1, $2, $3, $4, $5, $6, $7)",
|
||||
&[&account.username,
|
||||
&account.email_hash, &account.server_email_salt, &account.client_email_salt,
|
||||
&account.pass_hash, &account.server_pass_salt, &account.client_pass_salt]).await {
|
||||
Ok(_) => return Ok(()),
|
||||
Err(_) => return Err(ReturnFlags::ServerDbWriteFailed),
|
||||
}
|
||||
&account.pass_hash, &account.server_pass_salt, &account.client_pass_salt]).await;
|
||||
|
||||
/*
|
||||
* Send to client SQL result
|
||||
*/
|
||||
let server_response =
|
||||
message_builder(MessageType::ServerReturn,
|
||||
if creation_result.is_ok() { 1 } else { 0 },
|
||||
0, 0, 0,
|
||||
if creation_result.is_ok() { Vec::new() } else {
|
||||
bincode::serialize(&format!("{:#?}", creation_result)).unwrap()});
|
||||
match tls_connection
|
||||
.write_all(&bincode::serialize(
|
||||
&server_response).unwrap()).await { // Don't care if user didn't recieve a reply
|
||||
_ => Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -151,8 +151,10 @@ pub async fn libtrader_init_server() -> std::io::Result<()> {
|
|||
.next()
|
||||
.ok_or_else(|| std::io::Error::from(std::io::ErrorKind::AddrNotAvailable))?;
|
||||
|
||||
let config = gen_tls_server_config(&Path::new(&std::env::var("CONFIG_CERT_FILE").unwrap()),
|
||||
&Path::new(&std::env::var("CONFIG_KEY_FILE").unwrap()))?;
|
||||
let config = gen_tls_server_config(
|
||||
&Path::new(&std::env::var("CONFIG_CERT_FILE").unwrap()),
|
||||
&Path::new(&std::env::var("CONFIG_KEY_FILE").unwrap()),
|
||||
)?;
|
||||
let acceptor = TlsAcceptor::from(config);
|
||||
|
||||
let listener = TcpListener::bind(&addr).await?;
|
||||
|
|
|
@ -2,6 +2,5 @@ pub mod get_asset_data;
|
|||
pub mod get_asset_info;
|
||||
pub mod login_normal;
|
||||
pub mod purchase_asset;
|
||||
pub mod register;
|
||||
pub mod retrieve_portfolio;
|
||||
pub mod retrieve_transactions;
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
use log::warn;
|
||||
|
||||
use crate::common::message::inst::CommandInst;
|
||||
use crate::common::message::message::Message;
|
||||
use crate::common::message::message_builder::message_builder;
|
||||
use crate::common::message::message_type::MessageType;
|
||||
use crate::common::misc::assert_msg::assert_msg;
|
||||
|
||||
use crate::server::account::creation::acc_create;
|
||||
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::net::TcpStream;
|
||||
use tokio_rustls::server::TlsStream;
|
||||
|
||||
pub async fn register(
|
||||
sql_conn: &tokio_postgres::Client,
|
||||
tls_connection: &mut TlsStream<TcpStream>,
|
||||
message: &Message,
|
||||
) -> std::io::Result<()> {
|
||||
/* assert recieved message */
|
||||
if !assert_msg(
|
||||
message,
|
||||
MessageType::Command,
|
||||
true,
|
||||
5,
|
||||
false,
|
||||
0,
|
||||
false,
|
||||
0,
|
||||
false,
|
||||
0,
|
||||
) && message.instruction == CommandInst::Register as i64
|
||||
&& message.data.len() != 0
|
||||
{
|
||||
warn!("REGISTER_INVALID_MESSAGE");
|
||||
return tls_connection.shutdown().await;
|
||||
}
|
||||
|
||||
/* call acc_create() server version */
|
||||
match acc_create(sql_conn, message).await {
|
||||
Ok(_) => {
|
||||
let server_response =
|
||||
message_builder(MessageType::ServerReturn, 1, 0, 0, 0, Vec::new());
|
||||
tls_connection
|
||||
.write_all(&bincode::serialize(&server_response).unwrap())
|
||||
.await
|
||||
}
|
||||
Err(err) => {
|
||||
warn!("REGISTER_FAILED: {}", err);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,8 +5,9 @@ use crate::common::message::message::Message;
|
|||
use crate::common::message::message_builder::message_builder;
|
||||
use crate::common::message::message_type::MessageType;
|
||||
|
||||
use crate::server::account::creation::acc_create;
|
||||
|
||||
use crate::server::network::cmd::login_normal::login_normal;
|
||||
use crate::server::network::cmd::register::register;
|
||||
use crate::server::network::cmd::retrieve_portfolio::retrieve_portfolio;
|
||||
use crate::server::network::cmd::retrieve_transactions::retrieve_transactions;
|
||||
|
||||
|
@ -116,7 +117,7 @@ pub async fn handle_data(
|
|||
}
|
||||
}
|
||||
_ if client_msg.instruction == CommandInst::Register as i64 => {
|
||||
register(sql_conn, socket, &client_msg).await
|
||||
acc_create(sql_conn, socket, &client_msg).await
|
||||
}
|
||||
_ if client_msg.instruction == CommandInst::LoginMethod1 as i64 => {
|
||||
login_normal(sql_conn, socket, &client_msg).await
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
## To do
|
||||
|
||||
- server move network code to somewhere plausable.
|
||||
- create correct modules
|
||||
- data implmentation stuff
|
||||
* [ ] implement buy & sell
|
||||
|
@ -18,6 +17,7 @@
|
|||
|
||||
## In progress
|
||||
|
||||
- server move network code to somewhere plausable.
|
||||
|
||||
## Done
|
||||
|
||||
|
|
Loading…
Reference in New Issue