Compare commits

..

2 Commits

Author SHA1 Message Date
83d1726969 Feature: Proxy-Mode 2025-06-19 23:51:42 +03:00
f4ad746453 make using rac url 2025-06-19 23:08:44 +03:00
7 changed files with 277 additions and 658 deletions

800
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -15,4 +15,8 @@ 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

4
shell.nix Normal file
View File

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

View File

@ -4,12 +4,24 @@ 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);
@ -25,6 +37,17 @@ 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);
@ -49,6 +72,17 @@ 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 {
@ -70,6 +104,13 @@ 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);
@ -86,13 +127,26 @@ 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); // FIXME: softcode this name.truncate(256); // TODO: softcode this
let mut password = password.to_string(); let mut password = password.to_string();
password.truncate(256); // FIXME: softcode this password.truncate(256); // TODO: 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);
@ -114,6 +168,19 @@ 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();
@ -132,7 +199,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,5 +1,6 @@
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;
@ -13,7 +14,7 @@ pub mod util;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version)] #[command(version)]
pub struct Args { pub struct Args {
/// Server host /// Server host (RAC URL)
#[arg(short = 'H', long)] #[arg(short = 'H', long)]
host: String, host: String,
@ -53,10 +54,6 @@ 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>,
@ -65,9 +62,13 @@ pub struct Args {
#[arg(long)] #[arg(long)]
ssl_cert: Option<String>, ssl_cert: Option<String>,
/// Enable WRAC /// Enable Proxy-Mode (RAC URL)
#[arg(short = 'w', long)] #[arg(short = 'P', long)]
enable_wrac: bool, proxy_to: Option<String>,
/// Use Socks5 proxy (to connect to the server in proxy-mode)
#[arg(long)]
use_proxy: Option<String>,
} }
fn main() { fn main() {
@ -83,5 +84,7 @@ fn main() {
info!("Server started on {}", &args.host); info!("Server started on {}", &args.host);
run_listener(context); let (host, ssl, wrac) = parse_rac_url(&args.host).expect("INVALID RAC URL");
run_listener(context, &host, ssl, wrac);
} }

View File

@ -24,8 +24,9 @@ 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 ctx.args.enable_wrac { if 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)?;
@ -34,9 +35,8 @@ fn accept_stream(
Ok(()) Ok(())
} }
fn run_normal_listener(ctx: Arc<Context>) { fn run_normal_listener(ctx: Arc<Context>, host: &str, wrac: bool) {
let listener = let listener = TcpListener::bind(host).expect("error trying bind to the provided addr");
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>) {
let Ok(addr) = stream.peer_addr() else { let Ok(addr) = stream.peer_addr() else {
return; return;
}; };
match accept_stream(stream, addr, ctx) { match accept_stream(stream, addr, ctx, wrac) {
Ok(_) => {} Ok(_) => {}
Err(e) => { Err(e) => {
debug!("{}", e) debug!("{}", e)
@ -57,9 +57,8 @@ fn run_normal_listener(ctx: Arc<Context>) {
} }
} }
fn run_secure_listener(ctx: Arc<Context>) { fn run_secure_listener(ctx: Arc<Context>, host: &str, wrac: bool) {
let listener = let listener = TcpListener::bind(host).expect("error trying bind to the provided addr");
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()
@ -101,7 +100,7 @@ fn run_secure_listener(ctx: Arc<Context>) {
}; };
} }
match accept_stream(stream, addr, ctx) { match accept_stream(stream, addr, ctx, wrac) {
Ok(_) => {} Ok(_) => {}
Err(e) => { Err(e) => {
debug!("{}", e) debug!("{}", e)
@ -111,10 +110,10 @@ fn run_secure_listener(ctx: Arc<Context>) {
} }
} }
pub fn run_listener(ctx: Arc<Context>) { pub fn run_listener(ctx: Arc<Context>, host: &str, ssl: bool, wrac: bool) {
if ctx.args.enable_ssl { if ssl {
run_secure_listener(ctx); run_secure_listener(ctx, host, wrac);
} else { } else {
run_normal_listener(ctx); run_normal_listener(ctx, host, wrac);
} }
} }