mirror of
https://github.com/MeexReay/sRAC.git
synced 2025-06-24 02:22:57 +03:00
refactor: remove unnecessary cloning and simplify code
This commit is contained in:
parent
f262536e39
commit
87c5a7ce42
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@
|
|||||||
/result
|
/result
|
||||||
/messages.txt
|
/messages.txt
|
||||||
/accounts.txt
|
/accounts.txt
|
||||||
|
/.idea
|
85
src/ctx.rs
85
src/ctx.rs
@ -71,8 +71,8 @@ impl Context {
|
|||||||
args,
|
args,
|
||||||
messages_file: messages_file.clone(),
|
messages_file: messages_file.clone(),
|
||||||
accounts_file: accounts_file.clone(),
|
accounts_file: accounts_file.clone(),
|
||||||
messages: RwLock::new(load_messages(messages_file.clone())),
|
messages: RwLock::new(load_messages(messages_file)),
|
||||||
accounts: RwLock::new(load_accounts(accounts_file.clone())),
|
accounts: RwLock::new(load_accounts(accounts_file)),
|
||||||
messages_offset: AtomicU64::default(),
|
messages_offset: AtomicU64::default(),
|
||||||
notifications: RwLock::new(HashMap::new()),
|
notifications: RwLock::new(HashMap::new()),
|
||||||
timeouts: RwLock::new(HashMap::new()),
|
timeouts: RwLock::new(HashMap::new()),
|
||||||
@ -82,7 +82,6 @@ impl Context {
|
|||||||
pub fn push_message(&self, msg: Vec<u8>) -> Result<(), Box<dyn Error>> {
|
pub fn push_message(&self, msg: Vec<u8>) -> Result<(), Box<dyn Error>> {
|
||||||
if let Some(messages_file) = self.messages_file.clone() {
|
if let Some(messages_file) = self.messages_file.clone() {
|
||||||
let mut file = OpenOptions::new()
|
let mut file = OpenOptions::new()
|
||||||
.write(true)
|
|
||||||
.append(true)
|
.append(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.open(messages_file)?;
|
.open(messages_file)?;
|
||||||
@ -112,27 +111,26 @@ impl Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_account_by_addr(&self, addr: &str) -> Option<Account> {
|
pub fn get_account_by_addr(&self, addr: &str) -> Option<Account> {
|
||||||
for acc in self.accounts.read().unwrap().iter().rev() {
|
self.accounts
|
||||||
if acc.addr() == addr {
|
.read()
|
||||||
return Some(acc.clone());
|
.unwrap()
|
||||||
}
|
.iter()
|
||||||
}
|
.find(|acc| acc.addr() == addr)
|
||||||
None
|
.cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_account(&self, name: &str) -> Option<Account> {
|
pub fn get_account(&self, name: &str) -> Option<Account> {
|
||||||
for acc in self.accounts.read().unwrap().iter() {
|
self.accounts
|
||||||
if acc.name() == name {
|
.read()
|
||||||
return Some(acc.clone());
|
.unwrap()
|
||||||
}
|
.iter()
|
||||||
}
|
.find(|acc| acc.name() == name)
|
||||||
None
|
.cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push_account(&self, acc: Account) -> Result<(), Box<dyn Error>> {
|
pub fn push_account(&self, acc: Account) -> Result<(), Box<dyn Error>> {
|
||||||
if let Some(accounts_file) = self.accounts_file.clone() {
|
if let Some(accounts_file) = self.accounts_file.clone() {
|
||||||
let mut file = OpenOptions::new()
|
let mut file = OpenOptions::new()
|
||||||
.write(true)
|
|
||||||
.append(true)
|
.append(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.open(accounts_file)?;
|
.open(accounts_file)?;
|
||||||
@ -215,41 +213,34 @@ impl Account {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_bytes(text: Vec<u8>) -> Result<Self, Box<dyn Error>> {
|
pub fn from_bytes(text: Vec<u8>) -> Result<Self, Box<dyn Error>> {
|
||||||
let mut text = Cursor::new(text);
|
let mut cursor = Cursor::new(text);
|
||||||
|
|
||||||
let mut name_len = [0; 4];
|
let read_u32 = |cursor: &mut Cursor<Vec<u8>>| -> Result<u32, Box<dyn Error>> {
|
||||||
text.read_exact(&mut name_len)?;
|
let mut buf = [0; 4];
|
||||||
let name_len = u32::from_le_bytes(name_len) as usize;
|
cursor.read_exact(&mut buf)?;
|
||||||
|
Ok(u32::from_le_bytes(buf))
|
||||||
let mut salt_len = [0; 4];
|
};
|
||||||
text.read_exact(&mut salt_len)?;
|
|
||||||
let salt_len = u32::from_le_bytes(salt_len) as usize;
|
let name_len = read_u32(&mut cursor)? as usize;
|
||||||
|
let salt_len = read_u32(&mut cursor)? as usize;
|
||||||
let mut addr_len = [0; 4];
|
let addr_len = read_u32(&mut cursor)? as usize;
|
||||||
text.read_exact(&mut addr_len)?;
|
let pass_len = read_u32(&mut cursor)? as usize;
|
||||||
let addr_len = u32::from_le_bytes(addr_len) as usize;
|
|
||||||
|
let read_string = |cursor: &mut Cursor<Vec<u8>>, len: usize| -> Result<String, Box<dyn Error>> {
|
||||||
let mut pass_len = [0; 4];
|
let mut buf = vec![0; len];
|
||||||
text.read_exact(&mut pass_len)?;
|
cursor.read_exact(&mut buf)?;
|
||||||
let pass_len = u32::from_le_bytes(pass_len) as usize;
|
String::from_utf8(buf).map_err(|e| e.into())
|
||||||
|
};
|
||||||
let mut name = vec![0; name_len];
|
|
||||||
text.read_exact(&mut name)?;
|
let name = read_string(&mut cursor, name_len)?;
|
||||||
let name = String::from_utf8_lossy(&name).to_string();
|
let salt = read_string(&mut cursor, salt_len)?;
|
||||||
|
let addr = read_string(&mut cursor, addr_len)?;
|
||||||
let mut salt = vec![0; salt_len];
|
|
||||||
text.read_exact(&mut salt)?;
|
|
||||||
let salt = String::from_utf8_lossy(&salt).to_string();
|
|
||||||
|
|
||||||
let mut addr = vec![0; addr_len];
|
|
||||||
text.read_exact(&mut addr)?;
|
|
||||||
let addr = String::from_utf8_lossy(&addr).to_string();
|
|
||||||
|
|
||||||
let mut pass = vec![0; pass_len];
|
let mut pass = vec![0; pass_len];
|
||||||
text.read_exact(&mut pass)?;
|
cursor.read_exact(&mut pass)?;
|
||||||
|
|
||||||
let mut date = [0; 8];
|
let mut date = [0; 8];
|
||||||
text.read_exact(&mut date)?;
|
cursor.read_exact(&mut date)?;
|
||||||
let date = i64::from_le_bytes(date);
|
let date = i64::from_le_bytes(date);
|
||||||
|
|
||||||
Ok(Account {
|
Ok(Account {
|
||||||
|
@ -16,14 +16,14 @@ pub fn accept_rac_stream(
|
|||||||
stream.read_exact(&mut buf)?;
|
stream.read_exact(&mut buf)?;
|
||||||
|
|
||||||
if buf[0] == 0x00 {
|
if buf[0] == 0x00 {
|
||||||
let total_size = on_total_size(ctx.clone(), addr.clone())?;
|
let total_size = on_total_size(ctx.clone(), addr)?;
|
||||||
stream.write_all(total_size.to_string().as_bytes())?;
|
stream.write_all(total_size.to_string().as_bytes())?;
|
||||||
|
|
||||||
let mut id = vec![0];
|
let mut id = vec![0];
|
||||||
stream.read_exact(&mut id)?;
|
stream.read_exact(&mut id)?;
|
||||||
|
|
||||||
if id[0] == 0x01 {
|
if id[0] == 0x01 {
|
||||||
stream.write_all(&on_total_data(ctx.clone(), addr.clone(), Some(total_size))?)?;
|
stream.write_all(&on_total_data(ctx.clone(), addr, Some(total_size))?)?;
|
||||||
} else if id[0] == 0x02 {
|
} else if id[0] == 0x02 {
|
||||||
let mut buf = vec![0; 10];
|
let mut buf = vec![0; 10];
|
||||||
let size = stream.read(&mut buf)?;
|
let size = stream.read(&mut buf)?;
|
||||||
@ -32,7 +32,7 @@ pub fn accept_rac_stream(
|
|||||||
let client_has: u64 = String::from_utf8(buf)?.parse()?;
|
let client_has: u64 = String::from_utf8(buf)?.parse()?;
|
||||||
stream.write_all(&on_chunked_data(
|
stream.write_all(&on_chunked_data(
|
||||||
ctx.clone(),
|
ctx.clone(),
|
||||||
addr.clone(),
|
addr,
|
||||||
Some(total_size),
|
Some(total_size),
|
||||||
client_has,
|
client_has,
|
||||||
)?)?;
|
)?)?;
|
||||||
@ -42,7 +42,7 @@ pub fn accept_rac_stream(
|
|||||||
let size = stream.read(&mut buf)?;
|
let size = stream.read(&mut buf)?;
|
||||||
buf.truncate(size);
|
buf.truncate(size);
|
||||||
|
|
||||||
on_send_message(ctx.clone(), addr.clone(), buf)?;
|
on_send_message(ctx.clone(), addr, buf)?;
|
||||||
} else if buf[0] == 0x02 {
|
} else if buf[0] == 0x02 {
|
||||||
let mut buf = vec![0; ctx.args.message_limit + 2 + 512]; // FIXME: softcode this (512 = name + password)
|
let mut buf = vec![0; ctx.args.message_limit + 2 + 512]; // FIXME: softcode this (512 = name + password)
|
||||||
let size = stream.read(&mut buf)?;
|
let size = stream.read(&mut buf)?;
|
||||||
@ -62,9 +62,7 @@ pub fn accept_rac_stream(
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(resp_id) =
|
if let Some(resp_id) = on_send_auth_message(ctx.clone(), addr, name, password, text)? {
|
||||||
on_send_auth_message(ctx.clone(), addr.clone(), name, password, text)?
|
|
||||||
{
|
|
||||||
stream.write_all(&[resp_id])?;
|
stream.write_all(&[resp_id])?;
|
||||||
}
|
}
|
||||||
} else if buf[0] == 0x03 {
|
} else if buf[0] == 0x03 {
|
||||||
@ -83,7 +81,7 @@ pub fn accept_rac_stream(
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(resp_id) = on_register_user(ctx.clone(), addr.clone(), name, password)? {
|
if let Some(resp_id) = on_register_user(ctx.clone(), addr, name, password)? {
|
||||||
stream.write_all(&[resp_id])?;
|
stream.write_all(&[resp_id])?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ pub fn accept_wrac_stream(
|
|||||||
|
|
||||||
if id == 0x00 {
|
if id == 0x00 {
|
||||||
if data.is_empty() {
|
if data.is_empty() {
|
||||||
let total_size = on_total_size(ctx.clone(), addr.clone())?;
|
let total_size = on_total_size(ctx.clone(), addr)?;
|
||||||
sent_size = Some(total_size);
|
sent_size = Some(total_size);
|
||||||
|
|
||||||
websocket.write(Message::Binary(Bytes::from(
|
websocket.write(Message::Binary(Bytes::from(
|
||||||
@ -50,7 +50,7 @@ pub fn accept_wrac_stream(
|
|||||||
if id == 0x01 {
|
if id == 0x01 {
|
||||||
websocket.write(Message::Binary(Bytes::from(on_total_data(
|
websocket.write(Message::Binary(Bytes::from(on_total_data(
|
||||||
ctx.clone(),
|
ctx.clone(),
|
||||||
addr.clone(),
|
addr,
|
||||||
sent_size,
|
sent_size,
|
||||||
)?)))?;
|
)?)))?;
|
||||||
websocket.flush()?;
|
websocket.flush()?;
|
||||||
@ -58,7 +58,7 @@ pub fn accept_wrac_stream(
|
|||||||
let client_has = String::from_utf8(data)?.parse()?;
|
let client_has = String::from_utf8(data)?.parse()?;
|
||||||
websocket.write(Message::Binary(Bytes::from(on_chunked_data(
|
websocket.write(Message::Binary(Bytes::from(on_chunked_data(
|
||||||
ctx.clone(),
|
ctx.clone(),
|
||||||
addr.clone(),
|
addr,
|
||||||
sent_size,
|
sent_size,
|
||||||
client_has,
|
client_has,
|
||||||
)?)))?;
|
)?)))?;
|
||||||
@ -66,7 +66,7 @@ pub fn accept_wrac_stream(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if id == 0x01 {
|
} else if id == 0x01 {
|
||||||
on_send_message(ctx.clone(), addr.clone(), data)?;
|
on_send_message(ctx.clone(), addr, data)?;
|
||||||
} else if id == 0x02 {
|
} else if id == 0x02 {
|
||||||
let msg = String::from_utf8_lossy(&data).to_string();
|
let msg = String::from_utf8_lossy(&data).to_string();
|
||||||
|
|
||||||
@ -83,7 +83,7 @@ pub fn accept_wrac_stream(
|
|||||||
};
|
};
|
||||||
|
|
||||||
if let Some(resp_id) =
|
if let Some(resp_id) =
|
||||||
on_send_auth_message(ctx.clone(), addr.clone(), name, password, text)?
|
on_send_auth_message(ctx.clone(), addr, name, password, text)?
|
||||||
{
|
{
|
||||||
websocket.write(Message::Binary(Bytes::from(vec![resp_id])))?;
|
websocket.write(Message::Binary(Bytes::from(vec![resp_id])))?;
|
||||||
websocket.flush()?;
|
websocket.flush()?;
|
||||||
@ -96,12 +96,12 @@ pub fn accept_wrac_stream(
|
|||||||
let Some(name) = segments.next() else {
|
let Some(name) = segments.next() else {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
let Some(password) = segments.next() else {
|
let Some(password) = segments.next() else {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(resp_id) = on_register_user(ctx.clone(), addr.clone(), name, password)?
|
if let Some(resp_id) = on_register_user(ctx.clone(), addr, name, password)? {
|
||||||
{
|
|
||||||
websocket.write(Message::Binary(Bytes::from(vec![resp_id])))?;
|
websocket.write(Message::Binary(Bytes::from(vec![resp_id])))?;
|
||||||
websocket.flush()?;
|
websocket.flush()?;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user