mirror of
https://github.com/MeexReay/sRAC.git
synced 2025-06-24 18:42:58 +03:00
Compare commits
No commits in common. "main" and "1.0.0" have entirely different histories.
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,4 +2,3 @@
|
||||
/result
|
||||
/messages.txt
|
||||
/accounts.txt
|
||||
/.idea
|
645
Cargo.lock
generated
645
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
11
Cargo.toml
11
Cargo.toml
@ -5,18 +5,11 @@ edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
chrono = "0.4.40"
|
||||
bRAC = { git = "https://github.com/MeexReay/bRAC.git", default-features = false, tag = "0.1.2+2.0" }
|
||||
md-5 = "0.10.6"
|
||||
rand = "0.9.1"
|
||||
rand = "0.9.0"
|
||||
clap = { version = "4.5.36", features = ["derive"] }
|
||||
rustls = "0.23.25"
|
||||
tungstenite = "0.27.0"
|
||||
colog = "1.3.0"
|
||||
log = "0.4.27"
|
||||
regex = "1.11.1"
|
||||
colored = "3.0.0"
|
||||
lazy_static = "1.5.0"
|
||||
|
||||
[dependencies.bRAC]
|
||||
git = "https://github.com/MeexReay/bRAC"
|
||||
rev = "f3b6cbd01c5443e03d012c3c866487ba19785baf"
|
||||
default-features = false
|
43
README.md
43
README.md
@ -1,32 +1,29 @@
|
||||
# sRAC
|
||||
simple server for RAC
|
||||
|
||||
## features
|
||||
|
||||
- RACv2.0 and WRACv2.0 protocols
|
||||
- SSL encryption (via rustls)
|
||||
- messages limits by size
|
||||
- splash message
|
||||
- message sanitizing (removes all shit)
|
||||
- auth-only mode and accounts
|
||||
- messages saving into file
|
||||
- register and message timeouts
|
||||
|
||||
## usage
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
git clone https://github.com/MeexReay/sRAC.git; cd sRAC
|
||||
cargo run -- -H rac://127.0.0.1:42666
|
||||
git clone https://github.com/MeexReay/sRAC
|
||||
cd sRAC
|
||||
cargo run -- -H 127.0.0.1:42666
|
||||
```
|
||||
|
||||
## roadmap
|
||||
### My server config
|
||||
|
||||
- [x] WRAC protocol
|
||||
- [x] RACS protocol
|
||||
- [x] Proxy-mode
|
||||
- [ ] Notifications by ip (private messages)
|
||||
```bash
|
||||
cargo run -- \
|
||||
--host 127.0.0.1:42666 \
|
||||
--splash "please register (/register and /login commands in bRAC)" \
|
||||
--messages-file "messages.txt" \
|
||||
--accounts-file "accounts.txt" \
|
||||
--register-timeout 3600 \
|
||||
--sanitize \
|
||||
--auth-only
|
||||
```
|
||||
## Roadmap
|
||||
|
||||
- [ ] Notifications by ip
|
||||
- [ ] Server commands
|
||||
|
||||
## license
|
||||
|
||||
This project is licensed under the WTFPL. Do what the fuck you want to.
|
||||
- [x] WRAC protocol
|
||||
- [x] RACS protocol
|
@ -1,4 +0,0 @@
|
||||
{ pkgs ? import <nixpkgs> {} }:
|
||||
pkgs.mkShell {
|
||||
buildInputs = with pkgs; [ pkg-config openssl ];
|
||||
}
|
76
src/ctx.rs
76
src/ctx.rs
@ -11,15 +11,13 @@ use std::{
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use bRAC::{chat::format_message, util::sanitize_text};
|
||||
use chrono::{DateTime, Local, TimeZone};
|
||||
use log::info;
|
||||
use md5::{Digest, Md5};
|
||||
use rand::{Rng, distr::Alphanumeric};
|
||||
|
||||
use crate::{
|
||||
Args,
|
||||
util::{format_message, read_string, read_u32, sanitize_text},
|
||||
};
|
||||
use crate::Args;
|
||||
|
||||
fn load_accounts(accounts_file: Option<String>) -> Vec<Account> {
|
||||
if let Some(accounts_file) = accounts_file.clone() {
|
||||
@ -71,8 +69,8 @@ impl Context {
|
||||
args,
|
||||
messages_file: messages_file.clone(),
|
||||
accounts_file: accounts_file.clone(),
|
||||
messages: RwLock::new(load_messages(messages_file)),
|
||||
accounts: RwLock::new(load_accounts(accounts_file)),
|
||||
messages: RwLock::new(load_messages(messages_file.clone())),
|
||||
accounts: RwLock::new(load_accounts(accounts_file.clone())),
|
||||
messages_offset: AtomicU64::default(),
|
||||
notifications: RwLock::new(HashMap::new()),
|
||||
timeouts: RwLock::new(HashMap::new()),
|
||||
@ -82,6 +80,7 @@ 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)?;
|
||||
@ -111,26 +110,27 @@ impl Context {
|
||||
}
|
||||
|
||||
pub fn get_account_by_addr(&self, addr: &str) -> Option<Account> {
|
||||
self.accounts
|
||||
.read()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.find(|acc| acc.addr() == addr)
|
||||
.cloned()
|
||||
for acc in self.accounts.read().unwrap().iter().rev() {
|
||||
if acc.addr() == addr {
|
||||
return Some(acc.clone());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn get_account(&self, name: &str) -> Option<Account> {
|
||||
self.accounts
|
||||
.read()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.find(|acc| acc.name() == name)
|
||||
.cloned()
|
||||
for acc in self.accounts.read().unwrap().iter() {
|
||||
if acc.name() == name {
|
||||
return Some(acc.clone());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
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)?;
|
||||
@ -213,21 +213,41 @@ impl Account {
|
||||
}
|
||||
|
||||
pub fn from_bytes(text: Vec<u8>) -> Result<Self, Box<dyn Error>> {
|
||||
let mut cursor = Cursor::new(text);
|
||||
let mut text = Cursor::new(text);
|
||||
|
||||
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 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 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 pass = vec![0; pass_len];
|
||||
cursor.read_exact(&mut pass)?;
|
||||
text.read_exact(&mut pass)?;
|
||||
|
||||
let mut date = [0; 8];
|
||||
cursor.read_exact(&mut date)?;
|
||||
text.read_exact(&mut date)?;
|
||||
let date = i64::from_le_bytes(date);
|
||||
|
||||
Ok(Account {
|
||||
|
83
src/logic.rs
83
src/logic.rs
@ -4,24 +4,12 @@ use std::{
|
||||
sync::{Arc, atomic::Ordering},
|
||||
};
|
||||
|
||||
use bRAC::proto::{connect, read_messages, register_user, send_message, send_message_auth};
|
||||
use chrono::Local;
|
||||
use log::info;
|
||||
|
||||
use crate::ctx::{Account, Context, add_message};
|
||||
|
||||
pub fn on_total_size(ctx: Arc<Context>, _: SocketAddr) -> Result<u64, Box<dyn Error>> {
|
||||
if let Some(url) = ctx.args.proxy_to.as_ref() {
|
||||
return read_messages(
|
||||
&mut connect(url, ctx.args.use_proxy.clone())?,
|
||||
1024, // TODO: softcode this
|
||||
0,
|
||||
false,
|
||||
)?
|
||||
.map(|o| o.1 as u64)
|
||||
.ok_or("err on reading in proxy mode".into()); // TODO: fix reading two times
|
||||
}
|
||||
|
||||
let messages_len = ctx.messages.read().unwrap().len() as u64;
|
||||
let offset = ctx.messages_offset.load(Ordering::SeqCst);
|
||||
|
||||
@ -35,19 +23,8 @@ pub fn on_total_size(ctx: Arc<Context>, _: SocketAddr) -> Result<u64, Box<dyn Er
|
||||
pub fn on_total_data(
|
||||
ctx: Arc<Context>,
|
||||
_: SocketAddr,
|
||||
_sent_size: Option<u64>,
|
||||
_: Option<u64>, // sent_size
|
||||
) -> Result<Vec<u8>, Box<dyn Error>> {
|
||||
if let Some(url) = ctx.args.proxy_to.as_ref() {
|
||||
return read_messages(
|
||||
&mut connect(url, ctx.args.use_proxy.clone())?,
|
||||
1024, // TODO: softcode this
|
||||
0,
|
||||
false,
|
||||
)?
|
||||
.map(|o| (o.0.join("\n") + "\n").as_bytes().to_vec())
|
||||
.ok_or("err on reading in proxy mode".into()); // TODO: fix reading two times
|
||||
}
|
||||
|
||||
let mut messages = ctx.messages.read().unwrap().clone();
|
||||
let offset = ctx.messages_offset.load(Ordering::SeqCst);
|
||||
|
||||
@ -69,20 +46,9 @@ pub fn on_total_data(
|
||||
pub fn on_chunked_data(
|
||||
ctx: Arc<Context>,
|
||||
_: SocketAddr,
|
||||
_sent_size: Option<u64>,
|
||||
_: Option<u64>, // sent_size
|
||||
client_has: u64,
|
||||
) -> Result<Vec<u8>, Box<dyn Error>> {
|
||||
if let Some(url) = ctx.args.proxy_to.as_ref() {
|
||||
return read_messages(
|
||||
&mut connect(url, ctx.args.use_proxy.clone())?,
|
||||
1024, // TODO: softcode this
|
||||
client_has as usize,
|
||||
true,
|
||||
)?
|
||||
.map(|o| (o.0.join("\n") + "\n").as_bytes().to_vec())
|
||||
.ok_or("err on reading in proxy mode".into());
|
||||
}
|
||||
|
||||
let messages = ctx.messages.read().unwrap().clone();
|
||||
let offset = ctx.messages_offset.load(Ordering::SeqCst);
|
||||
let client_has = if let Some(splash) = &ctx.args.splash {
|
||||
@ -104,18 +70,11 @@ pub fn on_send_message(
|
||||
addr: SocketAddr,
|
||||
message: Vec<u8>,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
if let Some(url) = ctx.args.proxy_to.as_ref() {
|
||||
return send_message(
|
||||
&mut connect(url, ctx.args.use_proxy.clone())?,
|
||||
&String::from_utf8_lossy(&message),
|
||||
); // TODO: make brac accept message in bytes
|
||||
}
|
||||
|
||||
if !ctx.args.auth_only {
|
||||
let mut message = message;
|
||||
message.truncate(ctx.args.message_limit);
|
||||
|
||||
add_message(&message, ctx, Some(addr.ip()))?;
|
||||
add_message(&message.clone(), ctx.clone(), Some(addr.ip()))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -127,31 +86,18 @@ pub fn on_send_auth_message(
|
||||
password: &str,
|
||||
text: &str,
|
||||
) -> Result<Option<u8>, Box<dyn Error>> {
|
||||
if let Some(url) = ctx.args.proxy_to.as_ref() {
|
||||
return match send_message_auth(
|
||||
&mut connect(url, ctx.args.use_proxy.clone())?,
|
||||
name,
|
||||
password,
|
||||
text,
|
||||
) {
|
||||
Ok(0) => Ok(None),
|
||||
Ok(n) => Ok(Some(n)),
|
||||
Err(err) => Err(err),
|
||||
};
|
||||
}
|
||||
|
||||
if let Some(acc) = ctx.get_account(name) {
|
||||
if acc.check_password(password) {
|
||||
let mut name = name.to_string();
|
||||
name.truncate(256); // TODO: softcode this
|
||||
name.truncate(256); // FIXME: softcode this
|
||||
|
||||
let mut password = password.to_string();
|
||||
password.truncate(256); // TODO: softcode this
|
||||
password.truncate(256); // FIXME: softcode this
|
||||
|
||||
let mut text = text.to_string();
|
||||
text.truncate(ctx.args.message_limit);
|
||||
|
||||
add_message(format!("<{name}> {text}").as_bytes(), ctx, None)?;
|
||||
add_message(&text.as_bytes(), ctx.clone(), None)?;
|
||||
|
||||
Ok(None)
|
||||
} else {
|
||||
@ -168,19 +114,6 @@ pub fn on_register_user(
|
||||
name: &str,
|
||||
password: &str,
|
||||
) -> Result<Option<u8>, Box<dyn Error>> {
|
||||
if let Some(url) = ctx.args.proxy_to.as_ref() {
|
||||
return Ok(
|
||||
match register_user(
|
||||
&mut connect(url, ctx.args.use_proxy.clone())?,
|
||||
name,
|
||||
password,
|
||||
) {
|
||||
Ok(true) => None,
|
||||
_ => Some(0x01),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
let addr = addr.ip().to_string();
|
||||
|
||||
let now: i64 = Local::now().timestamp_millis();
|
||||
@ -203,7 +136,3 @@ pub fn on_register_user(
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub fn on_server_info(_: Arc<Context>, _: SocketAddr) -> Result<(u8, String), Box<dyn Error>> {
|
||||
Ok((0x03, format!("sRAC {}", env!("CARGO_PKG_VERSION"))))
|
||||
}
|
||||
|
22
src/main.rs
22
src/main.rs
@ -1,6 +1,5 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use bRAC::proto::parse_rac_url;
|
||||
use clap::Parser;
|
||||
use log::info;
|
||||
|
||||
@ -9,12 +8,11 @@ use crate::{ctx::Context, proto::run_listener};
|
||||
pub mod ctx;
|
||||
pub mod logic;
|
||||
pub mod proto;
|
||||
pub mod util;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version)]
|
||||
pub struct Args {
|
||||
/// Server host (RAC URL)
|
||||
/// Server host
|
||||
#[arg(short = 'H', long)]
|
||||
host: String,
|
||||
|
||||
@ -54,6 +52,10 @@ pub struct Args {
|
||||
#[arg(long, default_value_t = 4194304)]
|
||||
messages_total_limit: usize,
|
||||
|
||||
/// Enable SSL (RACS)
|
||||
#[arg(short = 'l', long)]
|
||||
enable_ssl: bool,
|
||||
|
||||
/// Set ssl certificate path (x509)
|
||||
#[arg(long)]
|
||||
ssl_key: Option<String>,
|
||||
@ -62,13 +64,9 @@ pub struct Args {
|
||||
#[arg(long)]
|
||||
ssl_cert: Option<String>,
|
||||
|
||||
/// Enable Proxy-Mode (RAC URL)
|
||||
#[arg(short = 'P', long)]
|
||||
proxy_to: Option<String>,
|
||||
|
||||
/// Use Socks5 proxy (to connect to the server in proxy-mode)
|
||||
#[arg(long)]
|
||||
use_proxy: Option<String>,
|
||||
/// Enable WRAC
|
||||
#[arg(short = 'w', long)]
|
||||
enable_wrac: bool,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@ -84,7 +82,5 @@ fn main() {
|
||||
|
||||
info!("Server started on {}", &args.host);
|
||||
|
||||
let (host, ssl, wrac) = parse_rac_url(&args.host).expect("INVALID RAC URL");
|
||||
|
||||
run_listener(context, &host, ssl, wrac);
|
||||
run_listener(context);
|
||||
}
|
||||
|
@ -24,9 +24,8 @@ fn accept_stream(
|
||||
stream: impl Read + Write,
|
||||
addr: SocketAddr,
|
||||
ctx: Arc<Context>,
|
||||
wrac: bool,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
if wrac {
|
||||
if ctx.args.enable_wrac {
|
||||
accept_wrac_stream(stream, addr, ctx)?;
|
||||
} else {
|
||||
accept_rac_stream(stream, addr, ctx)?;
|
||||
@ -35,8 +34,9 @@ fn accept_stream(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_normal_listener(ctx: Arc<Context>, host: &str, wrac: bool) {
|
||||
let listener = TcpListener::bind(host).expect("error trying bind to the provided addr");
|
||||
fn run_normal_listener(ctx: Arc<Context>) {
|
||||
let listener =
|
||||
TcpListener::bind(&ctx.args.host).expect("error trying bind to the provided addr");
|
||||
|
||||
for stream in listener.incoming() {
|
||||
let Ok(stream) = stream else { continue };
|
||||
@ -47,7 +47,7 @@ fn run_normal_listener(ctx: Arc<Context>, host: &str, wrac: bool) {
|
||||
let Ok(addr) = stream.peer_addr() else {
|
||||
return;
|
||||
};
|
||||
match accept_stream(stream, addr, ctx, wrac) {
|
||||
match accept_stream(stream, addr, ctx) {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
debug!("{}", e)
|
||||
@ -57,8 +57,9 @@ fn run_normal_listener(ctx: Arc<Context>, host: &str, wrac: bool) {
|
||||
}
|
||||
}
|
||||
|
||||
fn run_secure_listener(ctx: Arc<Context>, host: &str, wrac: bool) {
|
||||
let listener = TcpListener::bind(host).expect("error trying bind to the provided addr");
|
||||
fn run_secure_listener(ctx: Arc<Context>) {
|
||||
let listener =
|
||||
TcpListener::bind(&ctx.args.host).expect("error trying bind to the provided addr");
|
||||
|
||||
let server_config = Arc::new(
|
||||
ServerConfig::builder()
|
||||
@ -100,7 +101,7 @@ fn run_secure_listener(ctx: Arc<Context>, host: &str, wrac: bool) {
|
||||
};
|
||||
}
|
||||
|
||||
match accept_stream(stream, addr, ctx, wrac) {
|
||||
match accept_stream(stream, addr, ctx) {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
debug!("{}", e)
|
||||
@ -110,10 +111,10 @@ fn run_secure_listener(ctx: Arc<Context>, host: &str, wrac: bool) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_listener(ctx: Arc<Context>, host: &str, ssl: bool, wrac: bool) {
|
||||
if ssl {
|
||||
run_secure_listener(ctx, host, wrac);
|
||||
pub fn run_listener(ctx: Arc<Context>) {
|
||||
if ctx.args.enable_ssl {
|
||||
run_secure_listener(ctx);
|
||||
} else {
|
||||
run_normal_listener(ctx, host, wrac);
|
||||
run_normal_listener(ctx);
|
||||
}
|
||||
}
|
||||
|
@ -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)?;
|
||||
let total_size = on_total_size(ctx.clone(), addr.clone())?;
|
||||
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, Some(total_size))?)?;
|
||||
stream.write_all(&on_total_data(ctx.clone(), addr.clone(), 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,
|
||||
addr.clone(),
|
||||
Some(total_size),
|
||||
client_has,
|
||||
)?)?;
|
||||
@ -42,9 +42,9 @@ pub fn accept_rac_stream(
|
||||
let size = stream.read(&mut buf)?;
|
||||
buf.truncate(size);
|
||||
|
||||
on_send_message(ctx.clone(), addr, buf)?;
|
||||
on_send_message(ctx.clone(), addr.clone(), buf)?;
|
||||
} else if buf[0] == 0x02 {
|
||||
let mut buf = vec![0; ctx.args.message_limit + 2 + 512]; // TODO: 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)?;
|
||||
buf.truncate(size);
|
||||
|
||||
@ -62,7 +62,9 @@ pub fn accept_rac_stream(
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
if let Some(resp_id) = on_send_auth_message(ctx.clone(), addr, name, password, text)? {
|
||||
if let Some(resp_id) =
|
||||
on_send_auth_message(ctx.clone(), addr.clone(), name, password, text)?
|
||||
{
|
||||
stream.write_all(&[resp_id])?;
|
||||
}
|
||||
} else if buf[0] == 0x03 {
|
||||
@ -81,17 +83,9 @@ pub fn accept_rac_stream(
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
if let Some(resp_id) = on_register_user(ctx.clone(), addr, name, password)? {
|
||||
if let Some(resp_id) = on_register_user(ctx.clone(), addr.clone(), name, password)? {
|
||||
stream.write_all(&[resp_id])?;
|
||||
}
|
||||
} else if buf[0] == 0x69 {
|
||||
let (protocol_version, name) = on_server_info(ctx.clone(), addr)?;
|
||||
|
||||
let mut data = Vec::new();
|
||||
data.push(protocol_version);
|
||||
data.append(&mut name.as_bytes().to_vec());
|
||||
|
||||
stream.write_all(&data)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -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)?;
|
||||
let total_size = on_total_size(ctx.clone(), addr.clone())?;
|
||||
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,
|
||||
addr.clone(),
|
||||
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,
|
||||
addr.clone(),
|
||||
sent_size,
|
||||
client_has,
|
||||
)?)))?;
|
||||
@ -66,7 +66,7 @@ pub fn accept_wrac_stream(
|
||||
}
|
||||
}
|
||||
} else if id == 0x01 {
|
||||
on_send_message(ctx.clone(), addr, data)?;
|
||||
on_send_message(ctx.clone(), addr.clone(), 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, name, password, text)?
|
||||
on_send_auth_message(ctx.clone(), addr.clone(), name, password, text)?
|
||||
{
|
||||
websocket.write(Message::Binary(Bytes::from(vec![resp_id])))?;
|
||||
websocket.flush()?;
|
||||
@ -96,24 +96,15 @@ 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, name, password)? {
|
||||
if let Some(resp_id) = on_register_user(ctx.clone(), addr.clone(), name, password)?
|
||||
{
|
||||
websocket.write(Message::Binary(Bytes::from(vec![resp_id])))?;
|
||||
websocket.flush()?;
|
||||
}
|
||||
} else if id == 0x69 {
|
||||
let (protocol_version, name) = on_server_info(ctx.clone(), addr)?;
|
||||
|
||||
let mut data = Vec::new();
|
||||
data.push(protocol_version);
|
||||
data.append(&mut name.as_bytes().to_vec());
|
||||
|
||||
websocket.write(Message::Binary(Bytes::from(data)))?;
|
||||
websocket.flush()?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
116
src/util.rs
116
src/util.rs
@ -1,116 +0,0 @@
|
||||
use std::{error::Error, io::Read};
|
||||
|
||||
use colored::{Color, Colorize};
|
||||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref DATE_REGEX: Regex = Regex::new(r"\[(.*?)\] (.*)").unwrap();
|
||||
pub static ref IP_REGEX: Regex = Regex::new(r"\{(.*?)\} (.*)").unwrap();
|
||||
pub static ref COLORED_USERNAMES: Vec<(Regex, Color)> = vec![
|
||||
(Regex::new(r"\u{B9AC}\u{3E70}<(.*?)> (.*)").unwrap(), Color::Green), // bRAC
|
||||
(Regex::new(r"\u{2550}\u{2550}\u{2550}<(.*?)> (.*)").unwrap(), Color::BrightRed), // CRAB
|
||||
(Regex::new(r"\u{00B0}\u{0298}<(.*?)> (.*)").unwrap(), Color::Magenta), // Mefidroniy
|
||||
(Regex::new(r"<(.*?)> (.*)").unwrap(), Color::Cyan), // clRAC
|
||||
];
|
||||
pub static ref ANSI_REGEX: Regex = Regex::new(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])").unwrap();
|
||||
pub static ref CONTROL_CHARS_REGEX: Regex = Regex::new(r"[\x00-\x1F\x7F]").unwrap();
|
||||
}
|
||||
|
||||
pub fn sanitize_text(input: &str) -> String {
|
||||
let without_ansi = ANSI_REGEX.replace_all(input, "");
|
||||
let cleaned_text = CONTROL_CHARS_REGEX.replace_all(&without_ansi, "");
|
||||
cleaned_text.into_owned()
|
||||
}
|
||||
|
||||
pub fn format_message(enable_ip_viewing: bool, message: String) -> Option<String> {
|
||||
let message = sanitize_text(&message);
|
||||
|
||||
let date = DATE_REGEX.captures(&message)?;
|
||||
let (date, message) = (
|
||||
date.get(1)?.as_str().to_string(),
|
||||
date.get(2)?.as_str().to_string(),
|
||||
);
|
||||
|
||||
let (ip, message) = if let Some(message) = IP_REGEX.captures(&message) {
|
||||
(
|
||||
Some(message.get(1)?.as_str().to_string()),
|
||||
message.get(2)?.as_str().to_string(),
|
||||
)
|
||||
} else {
|
||||
(None, message)
|
||||
};
|
||||
|
||||
let message = message
|
||||
.trim_start_matches("(UNREGISTERED)")
|
||||
.trim_start_matches("(UNAUTHORIZED)")
|
||||
.trim_start_matches("(UNAUTHENTICATED)")
|
||||
.trim()
|
||||
.to_string()
|
||||
+ " ";
|
||||
|
||||
let prefix = if enable_ip_viewing {
|
||||
if let Some(ip) = ip {
|
||||
format!(
|
||||
"{}{} [{}]",
|
||||
ip,
|
||||
" ".repeat(if 15 >= ip.chars().count() {
|
||||
15 - ip.chars().count()
|
||||
} else {
|
||||
0
|
||||
}),
|
||||
date
|
||||
)
|
||||
} else {
|
||||
format!("{} [{}]", " ".repeat(15), date)
|
||||
}
|
||||
} else {
|
||||
format!("[{}]", date)
|
||||
};
|
||||
|
||||
Some(if let Some(captures) = find_username_color(&message) {
|
||||
let nick = captures.0;
|
||||
let content = captures.1;
|
||||
let color = captures.2;
|
||||
|
||||
format!(
|
||||
"{} {} {}",
|
||||
prefix.white().dimmed(),
|
||||
format!("<{}>", nick).color(color).bold(),
|
||||
content.white().blink()
|
||||
)
|
||||
} else {
|
||||
format!("{} {}", prefix.white().dimmed(), message.white().blink())
|
||||
})
|
||||
}
|
||||
|
||||
pub fn find_username_color(message: &str) -> Option<(String, String, Color)> {
|
||||
for (re, color) in COLORED_USERNAMES.iter() {
|
||||
if let Some(captures) = re.captures(message) {
|
||||
return Some((
|
||||
captures[1].to_string(),
|
||||
captures[2].to_string(),
|
||||
color.clone(),
|
||||
));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn read_u32<R>(cursor: &mut R) -> Result<u32, Box<dyn Error>>
|
||||
where
|
||||
R: Read,
|
||||
{
|
||||
let mut buf = [0; 4];
|
||||
cursor.read_exact(&mut buf)?;
|
||||
Ok(u32::from_le_bytes(buf))
|
||||
}
|
||||
|
||||
pub fn read_string<R>(cursor: &mut R, len: usize) -> Result<String, Box<dyn Error>>
|
||||
where
|
||||
R: Read,
|
||||
{
|
||||
let mut buf = vec![0; len];
|
||||
cursor.read_exact(&mut buf)?;
|
||||
String::from_utf8(buf).map_err(|e| e.into())
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user