mirror of
https://github.com/MeexReay/sRAC.git
synced 2025-06-24 02:22:57 +03:00
Compare commits
2 Commits
a5ea227748
...
83d1726969
Author | SHA1 | Date | |
---|---|---|---|
83d1726969 | |||
f4ad746453 |
800
Cargo.lock
generated
800
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -15,4 +15,8 @@ log = "0.4.27"
|
||||
regex = "1.11.1"
|
||||
colored = "3.0.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
|
@ -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
|
||||
|
||||
|
4
shell.nix
Normal file
4
shell.nix
Normal file
@ -0,0 +1,4 @@
|
||||
{ pkgs ? import <nixpkgs> {} }:
|
||||
pkgs.mkShell {
|
||||
buildInputs = with pkgs; [ pkg-config openssl ];
|
||||
}
|
73
src/logic.rs
73
src/logic.rs
@ -4,12 +4,24 @@ 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);
|
||||
|
||||
@ -25,6 +37,17 @@ 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);
|
||||
|
||||
@ -49,6 +72,17 @@ 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 {
|
||||
@ -70,6 +104,13 @@ 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);
|
||||
@ -86,13 +127,26 @@ 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); // FIXME: softcode this
|
||||
name.truncate(256); // TODO: softcode this
|
||||
|
||||
let mut password = password.to_string();
|
||||
password.truncate(256); // FIXME: softcode this
|
||||
password.truncate(256); // TODO: softcode this
|
||||
|
||||
let mut text = text.to_string();
|
||||
text.truncate(ctx.args.message_limit);
|
||||
@ -114,6 +168,19 @@ 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();
|
||||
@ -132,7 +199,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,5 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use bRAC::proto::parse_rac_url;
|
||||
use clap::Parser;
|
||||
use log::info;
|
||||
|
||||
@ -13,7 +14,7 @@ pub mod util;
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version)]
|
||||
pub struct Args {
|
||||
/// Server host
|
||||
/// Server host (RAC URL)
|
||||
#[arg(short = 'H', long)]
|
||||
host: String,
|
||||
|
||||
@ -53,10 +54,6 @@ 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>,
|
||||
@ -65,9 +62,13 @@ pub struct Args {
|
||||
#[arg(long)]
|
||||
ssl_cert: Option<String>,
|
||||
|
||||
/// Enable WRAC
|
||||
#[arg(short = 'w', long)]
|
||||
enable_wrac: bool,
|
||||
/// 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>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@ -83,5 +84,7 @@ fn main() {
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -24,8 +24,9 @@ fn accept_stream(
|
||||
stream: impl Read + Write,
|
||||
addr: SocketAddr,
|
||||
ctx: Arc<Context>,
|
||||
wrac: bool,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
if ctx.args.enable_wrac {
|
||||
if wrac {
|
||||
accept_wrac_stream(stream, addr, ctx)?;
|
||||
} else {
|
||||
accept_rac_stream(stream, addr, ctx)?;
|
||||
@ -34,9 +35,8 @@ fn accept_stream(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_normal_listener(ctx: Arc<Context>) {
|
||||
let listener =
|
||||
TcpListener::bind(&ctx.args.host).expect("error trying bind to the provided addr");
|
||||
fn run_normal_listener(ctx: Arc<Context>, host: &str, wrac: bool) {
|
||||
let listener = TcpListener::bind(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>) {
|
||||
let Ok(addr) = stream.peer_addr() else {
|
||||
return;
|
||||
};
|
||||
match accept_stream(stream, addr, ctx) {
|
||||
match accept_stream(stream, addr, ctx, wrac) {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
debug!("{}", e)
|
||||
@ -57,9 +57,8 @@ fn run_normal_listener(ctx: Arc<Context>) {
|
||||
}
|
||||
}
|
||||
|
||||
fn run_secure_listener(ctx: Arc<Context>) {
|
||||
let listener =
|
||||
TcpListener::bind(&ctx.args.host).expect("error trying bind to the provided addr");
|
||||
fn run_secure_listener(ctx: Arc<Context>, host: &str, wrac: bool) {
|
||||
let listener = TcpListener::bind(host).expect("error trying bind to the provided addr");
|
||||
|
||||
let server_config = Arc::new(
|
||||
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(_) => {}
|
||||
Err(e) => {
|
||||
debug!("{}", e)
|
||||
@ -111,10 +110,10 @@ fn run_secure_listener(ctx: Arc<Context>) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_listener(ctx: Arc<Context>) {
|
||||
if ctx.args.enable_ssl {
|
||||
run_secure_listener(ctx);
|
||||
pub fn run_listener(ctx: Arc<Context>, host: &str, ssl: bool, wrac: bool) {
|
||||
if ssl {
|
||||
run_secure_listener(ctx, host, wrac);
|
||||
} else {
|
||||
run_normal_listener(ctx);
|
||||
run_normal_listener(ctx, host, wrac);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user