server side preparation
This commit is contained in:
156
src/net/data.rs
156
src/net/data.rs
@@ -1,106 +1,110 @@
|
||||
use rand::TryRng;
|
||||
use iris::core::util::HashSet;
|
||||
|
||||
#[repr(u32)]
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub enum ClientMsgInst {
|
||||
CreateAccountV0(CreateAccountV0) = 0,
|
||||
LoginV0(LoginV0) = 1,
|
||||
RequestMsgsV0 = 2,
|
||||
SendMsgV0(SendMsgV0) = 3,
|
||||
RequestUsersV0(RequestUsersV0) = 4,
|
||||
}
|
||||
|
||||
#[repr(u32)]
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub enum ServerMsgInst {
|
||||
CreateAccountRespV0(CreateAccountRespV0) = 0,
|
||||
LoginRespV0(LoginRespV0) = 1,
|
||||
LoadMsgV0(LoadMsgV0) = 2,
|
||||
LoadMsgsV0(Vec<LoadMsgV0>) = 3,
|
||||
ServerErrorV0(ServerErrorV0) = 4,
|
||||
RequestUsersRespV0(RequestUsersRespV0) = 5,
|
||||
}
|
||||
|
||||
pub type UserIdV0 = u64;
|
||||
pub type UserId = u64;
|
||||
pub type AccountToken = String;
|
||||
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct CreateAccountV0 {
|
||||
pub struct CreateAccount {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub token: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct LoginV0 {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub enum CreateAccountRespV0 {
|
||||
Ok { id: UserIdV0 },
|
||||
pub enum CreateAccountResp {
|
||||
Ok { id: UserId },
|
||||
UsernameExists,
|
||||
InvalidToken,
|
||||
}
|
||||
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub enum LoginRespV0 {
|
||||
Ok { id: UserIdV0 },
|
||||
pub struct Login {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub enum LoginResp {
|
||||
Ok { id: UserId },
|
||||
UnknownUsername,
|
||||
InvalidPassword,
|
||||
}
|
||||
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct RequestUsersV0;
|
||||
|
||||
pub struct RequestUsers;
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct RequestUsersRespV0 {
|
||||
pub users: Vec<ServerUserV0>,
|
||||
pub struct RequestUsersResp {
|
||||
pub users: Vec<ServerUser>,
|
||||
}
|
||||
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct ServerUserV0 {
|
||||
pub id: UserIdV0,
|
||||
pub struct ServerUser {
|
||||
pub id: UserId,
|
||||
pub username: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct LoginKeyV0(Vec<u8>);
|
||||
impl LoginKeyV0 {
|
||||
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];
|
||||
rand::rngs::SysRng
|
||||
.try_fill_bytes(&mut key)
|
||||
.expect("failed to generate random key");
|
||||
Self(key.to_vec())
|
||||
}
|
||||
|
||||
pub fn bytes(&self) -> &[u8] {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
impl From<Vec<u8>> for LoginKeyV0 {
|
||||
fn from(value: Vec<u8>) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct SendMsgV0 {
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct LoadMsgV0 {
|
||||
pub content: String,
|
||||
pub author: UserIdV0,
|
||||
pub struct RequestFriends;
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct RequestFriendsResp {
|
||||
pub current: HashSet<UserId>,
|
||||
pub incoming: HashSet<UserId>,
|
||||
pub outgoing: HashSet<UserId>,
|
||||
}
|
||||
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub enum ServerErrorV0 {
|
||||
NotLoggedIn,
|
||||
NoPermission,
|
||||
pub struct AddFriend {
|
||||
pub username: String,
|
||||
}
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub enum AddFriendResp {
|
||||
Ok,
|
||||
UnknownUser,
|
||||
CannotAddSelf,
|
||||
AlreadySent,
|
||||
AlreadyFriends,
|
||||
}
|
||||
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct RemoveFriend {
|
||||
pub id: UserId,
|
||||
}
|
||||
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct AnswerFriendRequest {
|
||||
pub id: UserId,
|
||||
pub action: FriendRequestAction,
|
||||
}
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub enum FriendRequestAction {
|
||||
Accept,
|
||||
Deny,
|
||||
}
|
||||
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct GenerateToken {
|
||||
pub perms: ServerPerms,
|
||||
}
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct GenerateTokenResp {
|
||||
pub token: AccountToken,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct ServerPerms(u32);
|
||||
impl ServerPerms {
|
||||
pub const NONE: Self = Self(0);
|
||||
pub const ACCOUNT_TOKENS: Self = Self(1 << 0);
|
||||
pub const ALL: Self = Self(u32::MAX);
|
||||
}
|
||||
impl ServerPerms {
|
||||
pub fn contains(&self, other: Self) -> bool {
|
||||
(self.0 & other.0) == other.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct NotLoggedIn;
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct NoPermission;
|
||||
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
|
||||
pub struct AccountDeleted;
|
||||
|
||||
Reference in New Issue
Block a user