Compare commits

..

No commits in common. "83d17269691c6d1d1f385a187c7bd79e43324e6d" and "a5ea2277485a4bfc1c24f225ea7fc6cac5afc438" have entirely different histories.

7 changed files with 661 additions and 280 deletions

806
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -15,8 +15,4 @@ log = "0.4.27"
regex = "1.11.1" regex = "1.11.1"
colored = "3.0.0" colored = "3.0.0"
lazy_static = "1.5.0" lazy_static = "1.5.0"
bRAC = { git = "https://github.com/MeexReay/bRAC.git" }
[dependencies.bRAC]
git = "https://github.com/MeexReay/bRAC"
rev = "5b23a3bd"
default-features = false

View File

@ -21,11 +21,11 @@ cargo run -- -H rac://127.0.0.1:42666
## roadmap ## roadmap
- [ ] Proxy-mode
- [ ] Notifications by ip
- [ ] Server commands
- [x] WRAC protocol - [x] WRAC protocol
- [x] RACS protocol - [x] RACS protocol
- [x] Proxy-mode
- [ ] Notifications by ip (private messages)
- [ ] Server commands
## license ## license

View File

@ -1,4 +0,0 @@
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = with pkgs; [ pkg-config openssl ];
}

View File

@ -4,24 +4,12 @@ use std::{
sync::{Arc, atomic::Ordering}, sync::{Arc, atomic::Ordering},
}; };
use bRAC::proto::{connect, read_messages, register_user, send_message, send_message_auth};
use chrono::Local; use chrono::Local;
use log::info; use log::info;
use crate::ctx::{Account, Context, add_message}; use crate::ctx::{Account, Context, add_message};
pub fn on_total_size(ctx: Arc<Context>, _: SocketAddr) -> Result<u64, Box<dyn Error>> { 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 messages_len = ctx.messages.read().unwrap().len() as u64;
let offset = ctx.messages_offset.load(Ordering::SeqCst); let offset = ctx.messages_offset.load(Ordering::SeqCst);
@ -37,17 +25,6 @@ pub fn on_total_data(
_: SocketAddr, _: SocketAddr,
_: Option<u64>, // sent_size _: Option<u64>, // sent_size
) -> Result<Vec<u8>, Box<dyn Error>> { ) -> 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 mut messages = ctx.messages.read().unwrap().clone();
let offset = ctx.messages_offset.load(Ordering::SeqCst); let offset = ctx.messages_offset.load(Ordering::SeqCst);
@ -72,17 +49,6 @@ pub fn on_chunked_data(
_: Option<u64>, // sent_size _: Option<u64>, // sent_size
client_has: u64, client_has: u64,
) -> Result<Vec<u8>, Box<dyn Error>> { ) -> 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 messages = ctx.messages.read().unwrap().clone();
let offset = ctx.messages_offset.load(Ordering::SeqCst); let offset = ctx.messages_offset.load(Ordering::SeqCst);
let client_has = if let Some(splash) = &ctx.args.splash { let client_has = if let Some(splash) = &ctx.args.splash {
@ -104,13 +70,6 @@ pub fn on_send_message(
addr: SocketAddr, addr: SocketAddr,
message: Vec<u8>, message: Vec<u8>,
) -> Result<(), Box<dyn Error>> { ) -> 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 { if !ctx.args.auth_only {
let mut message = message; let mut message = message;
message.truncate(ctx.args.message_limit); message.truncate(ctx.args.message_limit);
@ -127,26 +86,13 @@ pub fn on_send_auth_message(
password: &str, password: &str,
text: &str, text: &str,
) -> Result<Option<u8>, Box<dyn Error>> { ) -> 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 let Some(acc) = ctx.get_account(name) {
if acc.check_password(password) { if acc.check_password(password) {
let mut name = name.to_string(); let mut name = name.to_string();
name.truncate(256); // TODO: softcode this name.truncate(256); // FIXME: softcode this
let mut password = password.to_string(); let mut password = password.to_string();
password.truncate(256); // TODO: softcode this password.truncate(256); // FIXME: softcode this
let mut text = text.to_string(); let mut text = text.to_string();
text.truncate(ctx.args.message_limit); text.truncate(ctx.args.message_limit);
@ -168,19 +114,6 @@ pub fn on_register_user(
name: &str, name: &str,
password: &str, password: &str,
) -> Result<Option<u8>, Box<dyn Error>> { ) -> 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 addr = addr.ip().to_string();
let now: i64 = Local::now().timestamp_millis(); let now: i64 = Local::now().timestamp_millis();
@ -199,7 +132,7 @@ pub fn on_register_user(
info!("user registered: {name}"); info!("user registered: {name}");
ctx.push_account(account)?; ctx.push_account(account);
Ok(None) Ok(None)
} }

View File

@ -1,6 +1,5 @@
use std::sync::Arc; use std::sync::Arc;
use bRAC::proto::parse_rac_url;
use clap::Parser; use clap::Parser;
use log::info; use log::info;
@ -14,7 +13,7 @@ pub mod util;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version)] #[command(version)]
pub struct Args { pub struct Args {
/// Server host (RAC URL) /// Server host
#[arg(short = 'H', long)] #[arg(short = 'H', long)]
host: String, host: String,
@ -54,6 +53,10 @@ pub struct Args {
#[arg(long, default_value_t = 4194304)] #[arg(long, default_value_t = 4194304)]
messages_total_limit: usize, messages_total_limit: usize,
/// Enable SSL (RACS)
#[arg(short = 'l', long)]
enable_ssl: bool,
/// Set ssl certificate path (x509) /// Set ssl certificate path (x509)
#[arg(long)] #[arg(long)]
ssl_key: Option<String>, ssl_key: Option<String>,
@ -62,13 +65,9 @@ pub struct Args {
#[arg(long)] #[arg(long)]
ssl_cert: Option<String>, ssl_cert: Option<String>,
/// Enable Proxy-Mode (RAC URL) /// Enable WRAC
#[arg(short = 'P', long)] #[arg(short = 'w', long)]
proxy_to: Option<String>, enable_wrac: bool,
/// Use Socks5 proxy (to connect to the server in proxy-mode)
#[arg(long)]
use_proxy: Option<String>,
} }
fn main() { fn main() {
@ -84,7 +83,5 @@ fn main() {
info!("Server started on {}", &args.host); info!("Server started on {}", &args.host);
let (host, ssl, wrac) = parse_rac_url(&args.host).expect("INVALID RAC URL"); run_listener(context);
run_listener(context, &host, ssl, wrac);
} }

View File

@ -24,9 +24,8 @@ fn accept_stream(
stream: impl Read + Write, stream: impl Read + Write,
addr: SocketAddr, addr: SocketAddr,
ctx: Arc<Context>, ctx: Arc<Context>,
wrac: bool,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
if wrac { if ctx.args.enable_wrac {
accept_wrac_stream(stream, addr, ctx)?; accept_wrac_stream(stream, addr, ctx)?;
} else { } else {
accept_rac_stream(stream, addr, ctx)?; accept_rac_stream(stream, addr, ctx)?;
@ -35,8 +34,9 @@ fn accept_stream(
Ok(()) Ok(())
} }
fn run_normal_listener(ctx: Arc<Context>, host: &str, wrac: bool) { fn run_normal_listener(ctx: Arc<Context>) {
let listener = TcpListener::bind(host).expect("error trying bind to the provided addr"); let listener =
TcpListener::bind(&ctx.args.host).expect("error trying bind to the provided addr");
for stream in listener.incoming() { for stream in listener.incoming() {
let Ok(stream) = stream else { continue }; 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 { let Ok(addr) = stream.peer_addr() else {
return; return;
}; };
match accept_stream(stream, addr, ctx, wrac) { match accept_stream(stream, addr, ctx) {
Ok(_) => {} Ok(_) => {}
Err(e) => { Err(e) => {
debug!("{}", 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) { fn run_secure_listener(ctx: Arc<Context>) {
let listener = TcpListener::bind(host).expect("error trying bind to the provided addr"); let listener =
TcpListener::bind(&ctx.args.host).expect("error trying bind to the provided addr");
let server_config = Arc::new( let server_config = Arc::new(
ServerConfig::builder() 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(_) => {} Ok(_) => {}
Err(e) => { Err(e) => {
debug!("{}", 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) { pub fn run_listener(ctx: Arc<Context>) {
if ssl { if ctx.args.enable_ssl {
run_secure_listener(ctx, host, wrac); run_secure_listener(ctx);
} else { } else {
run_normal_listener(ctx, host, wrac); run_normal_listener(ctx);
} }
} }