mirror of
https://github.com/MeexReay/sRAC.git
synced 2025-06-25 02:52:57 +03:00
Compare commits
No commits in common. "83d17269691c6d1d1f385a187c7bd79e43324e6d" and "a5ea2277485a4bfc1c24f225ea7fc6cac5afc438" have entirely different histories.
83d1726969
...
a5ea227748
806
Cargo.lock
generated
806
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -15,8 +15,4 @@ 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 = "5b23a3bd"
|
||||
default-features = false
|
||||
bRAC = { git = "https://github.com/MeexReay/bRAC.git" }
|
@ -21,11 +21,11 @@ cargo run -- -H rac://127.0.0.1:42666
|
||||
|
||||
## roadmap
|
||||
|
||||
- [ ] Proxy-mode
|
||||
- [ ] Notifications by ip
|
||||
- [ ] Server commands
|
||||
- [x] WRAC protocol
|
||||
- [x] RACS protocol
|
||||
- [x] Proxy-mode
|
||||
- [ ] Notifications by ip (private messages)
|
||||
- [ ] Server commands
|
||||
|
||||
## license
|
||||
|
||||
|
@ -1,4 +0,0 @@
|
||||
{ pkgs ? import <nixpkgs> {} }:
|
||||
pkgs.mkShell {
|
||||
buildInputs = with pkgs; [ pkg-config openssl ];
|
||||
}
|
73
src/logic.rs
73
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);
|
||||
|
||||
@ -37,17 +25,6 @@ pub fn on_total_data(
|
||||
_: SocketAddr,
|
||||
_: 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);
|
||||
|
||||
@ -72,17 +49,6 @@ pub fn on_chunked_data(
|
||||
_: 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,13 +70,6 @@ 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);
|
||||
@ -127,26 +86,13 @@ 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);
|
||||
@ -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();
|
||||
@ -199,7 +132,7 @@ pub fn on_register_user(
|
||||
|
||||
info!("user registered: {name}");
|
||||
|
||||
ctx.push_account(account)?;
|
||||
ctx.push_account(account);
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
21
src/main.rs
21
src/main.rs
@ -1,6 +1,5 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use bRAC::proto::parse_rac_url;
|
||||
use clap::Parser;
|
||||
use log::info;
|
||||
|
||||
@ -14,7 +13,7 @@ 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 +53,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 +65,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 +83,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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user