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