work
This commit is contained in:
@@ -1,96 +1,22 @@
|
||||
use bincode::config::Configuration;
|
||||
|
||||
mod conversion;
|
||||
mod data;
|
||||
mod msg;
|
||||
mod no_cert;
|
||||
mod request;
|
||||
mod transfer;
|
||||
|
||||
pub use data::{ClientMsgInst, ServerMsgInst};
|
||||
pub use msg::*;
|
||||
pub use no_cert::*;
|
||||
use rand::{TryRngCore, rngs::OsRng};
|
||||
pub use request::*;
|
||||
pub use transfer::*;
|
||||
|
||||
pub const SERVER_NAME: &str = "openworm";
|
||||
pub const BINCODE_CONFIG: Configuration = bincode::config::standard();
|
||||
|
||||
#[derive(Debug, bincode::Encode, bincode::Decode)]
|
||||
pub enum ClientMsg {
|
||||
SendMsg(NetClientMsg),
|
||||
RequestMsgs,
|
||||
CreateAccount {
|
||||
username: String,
|
||||
password: String,
|
||||
device: LoginKey,
|
||||
},
|
||||
Login {
|
||||
username: String,
|
||||
password: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, bincode::Encode, bincode::Decode)]
|
||||
pub enum ServerMsg {
|
||||
SendMsg(NetServerMsg),
|
||||
LoadMsgs(Vec<NetServerMsg>),
|
||||
Login { username: String },
|
||||
Error(ServerError),
|
||||
}
|
||||
|
||||
#[derive(Debug, bincode::Encode, bincode::Decode)]
|
||||
pub enum ServerError {
|
||||
NotLoggedIn,
|
||||
UnknownUsername,
|
||||
InvalidPassword,
|
||||
UsernameTaken,
|
||||
}
|
||||
|
||||
impl From<ServerError> for ServerMsg {
|
||||
fn from(value: ServerError) -> Self {
|
||||
Self::Error(value)
|
||||
}
|
||||
}
|
||||
|
||||
pub type ServerResp<T> = Result<T, String>;
|
||||
|
||||
#[derive(Debug, Clone, bincode::Encode, bincode::Decode)]
|
||||
pub struct NetClientMsg {
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, bincode::Encode, bincode::Decode)]
|
||||
pub struct NetServerMsg {
|
||||
pub content: String,
|
||||
pub user: String,
|
||||
}
|
||||
|
||||
impl From<NetServerMsg> for ServerMsg {
|
||||
fn from(value: NetServerMsg) -> Self {
|
||||
Self::SendMsg(value)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn install_crypto_provider() {
|
||||
quinn::rustls::crypto::ring::default_provider()
|
||||
.install_default()
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[derive(Debug, bincode::Encode, bincode::Decode)]
|
||||
pub struct LoginKey([u8; LoginKey::BYTE_LEN]);
|
||||
impl LoginKey {
|
||||
pub const BIT_LEN: usize = 1024;
|
||||
pub const BYTE_LEN: usize = Self::BIT_LEN / 8;
|
||||
|
||||
pub fn new() -> Self {
|
||||
let mut key = [0u8; Self::BYTE_LEN];
|
||||
OsRng
|
||||
.try_fill_bytes(&mut key)
|
||||
.expect("failed to generate random key");
|
||||
Self(key)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Vec<u8>> for LoginKey {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
|
||||
Ok(Self(value.try_into().map_err(|_| ())?))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user