mirror of
https://github.com/MeexReay/bRAC.git
synced 2025-05-06 13:38:04 +03:00
auth refactor
This commit is contained in:
parent
0063e56438
commit
3f163abb7c
@ -3,7 +3,6 @@
|
||||
## bRAC
|
||||
|
||||
regex - `\uB9AC\u3E70<(.*?)> (.*)` \
|
||||
regex with auth (after sanitizing) - `\uB9AC\u3E70<(.*?)> (.*)\s*>.*` \
|
||||
color - green \
|
||||
example - `리㹰<nick> text`
|
||||
|
||||
|
28
src/chat.rs
28
src/chat.rs
@ -161,15 +161,25 @@ pub fn print_console(ctx: Arc<Context>, messages: Vec<String>, input: &str) -> R
|
||||
|
||||
|
||||
fn prepare_message(context: Arc<Context>, message: &str) -> String {
|
||||
format!("{}{}{}\r",
|
||||
format!("{}{}{}",
|
||||
if !context.disable_hiding_ip {
|
||||
"\r\x07"
|
||||
} else {
|
||||
""
|
||||
},
|
||||
message,
|
||||
if !context.disable_hiding_ip && message.chars().count() < 53 {
|
||||
" ".repeat(53-message.chars().count())
|
||||
if !context.disable_hiding_ip {
|
||||
let spaces = if context.auth {
|
||||
39
|
||||
} else {
|
||||
54
|
||||
};
|
||||
|
||||
if message.chars().count() < spaces {
|
||||
" ".repeat(spaces-message.chars().count())
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
@ -192,7 +202,7 @@ fn format_message(ctx: Arc<Context>, message: String) -> Option<String> {
|
||||
(None, message)
|
||||
};
|
||||
|
||||
let message = message.trim_start_matches("(UNREGISTERED)").trim_start();
|
||||
let message = message.trim_start_matches("(UNREGISTERED)").trim().to_string()+" ";
|
||||
|
||||
let prefix = if ctx.enable_ip_viewing {
|
||||
if let Some(ip) = ip {
|
||||
@ -296,15 +306,19 @@ fn poll_events(ctx: Arc<Context>) -> Result<(), Box<dyn Error>> {
|
||||
|
||||
cursor = 0;
|
||||
|
||||
history_cursor = history.len()-1;
|
||||
history.push(String::new());
|
||||
history_cursor = history.len()-1;
|
||||
|
||||
if message.starts_with("/") && !ctx.disable_commands {
|
||||
on_command(ctx.clone(), &message)?;
|
||||
} else {
|
||||
let message = prepare_message(ctx.clone(), &ctx.message_format
|
||||
let message = prepare_message(
|
||||
ctx.clone(),
|
||||
&ctx.message_format
|
||||
.replace("{name}", &ctx.name)
|
||||
.replace("{text}", &message));
|
||||
.replace("{text}", &message)
|
||||
);
|
||||
|
||||
if ctx.auth {
|
||||
send_message_auth(&ctx.host, &message)?;
|
||||
} else {
|
||||
|
@ -29,6 +29,8 @@ pub struct Config {
|
||||
pub enable_ip_viewing: bool,
|
||||
#[serde(default)]
|
||||
pub disable_ip_hiding: bool,
|
||||
#[serde(default)]
|
||||
pub enable_auth: bool,
|
||||
}
|
||||
|
||||
fn default_max_messages() -> usize { 200 }
|
||||
@ -69,6 +71,7 @@ pub fn configure(path: PathBuf) -> Config {
|
||||
let message_format = ask_string("Message format", default_message_format());
|
||||
let enable_ip_viewing = ask_bool("Enable users IP viewing?", false);
|
||||
let disable_ip_hiding = ask_bool("Enable your IP viewing?", false);
|
||||
let enable_auth = ask_bool("Enable auth-mode?", false);
|
||||
|
||||
let config = Config {
|
||||
host,
|
||||
@ -77,7 +80,8 @@ pub fn configure(path: PathBuf) -> Config {
|
||||
update_time,
|
||||
max_messages,
|
||||
enable_ip_viewing,
|
||||
disable_ip_hiding
|
||||
disable_ip_hiding,
|
||||
enable_auth
|
||||
};
|
||||
|
||||
let config_text = serde_yml::to_string(&config).expect("Config save error");
|
||||
@ -173,9 +177,9 @@ pub struct Args {
|
||||
#[arg(short='C', long)]
|
||||
pub configure: bool,
|
||||
|
||||
/// Disable authentication
|
||||
/// Enable authentication
|
||||
#[arg(short='a', long)]
|
||||
pub disable_auth: bool,
|
||||
pub enable_auth: bool,
|
||||
}
|
||||
|
||||
pub struct Context {
|
||||
@ -209,7 +213,7 @@ impl Context {
|
||||
max_messages: config.max_messages,
|
||||
enable_ip_viewing: args.enable_users_ip_viewing || config.enable_ip_viewing,
|
||||
scroll: Arc::new(AtomicUsize::new(0)),
|
||||
auth: !args.disable_auth
|
||||
auth: args.enable_auth || config.enable_auth
|
||||
}
|
||||
}
|
||||
}
|
@ -13,9 +13,7 @@ lazy_static! {
|
||||
static ref DATE_REGEX: Regex = Regex::new(r"\[(.*?)\] (.*)").unwrap();
|
||||
static ref IP_REGEX: Regex = Regex::new(r"\{(.*?)\} (.*)").unwrap();
|
||||
static ref COLORED_USERNAMES: Vec<(Regex, Color)> = vec![
|
||||
(Regex::new(r"\u{B9AC}\u{3E70}<(.*?)> (.*)\s*>.*").unwrap(), Color::Green), // bRAC with auth sanitized
|
||||
(Regex::new(r"\u{B9AC}\u{3E70}<(.*?)> (.*)").unwrap(), Color::Green), // bRAC
|
||||
// (Regex::new(r"\u{B9AC}\u{3E70}\r<(.*?)> (.*)>\r ").unwrap(), Color::Green), // bRAC with auth
|
||||
(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
|
||||
|
33
src/proto.rs
33
src/proto.rs
@ -10,24 +10,33 @@ pub fn send_message(host: &str, message: &str) -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
|
||||
pub fn send_message_auth(host: &str, message: &str) -> Result<(), Box<dyn Error>> {
|
||||
let mut stream = TcpStream::connect(host)?;
|
||||
stream.write_all(&[0x03])?;
|
||||
stream.write_all(message.as_bytes())?;
|
||||
stream.write_all(&[b'\n'])?;
|
||||
stream.write_all(message.as_bytes())?;
|
||||
stream.shutdown(Shutdown::Both)?;
|
||||
let Some((name, message)) = message.split_once("> ") else { return send_message(host, message) };
|
||||
|
||||
let mut stream = TcpStream::connect(host)?;
|
||||
stream.write_all(&[0x02])?;
|
||||
stream.write_all(name.as_bytes())?;
|
||||
stream.write_all(b"\n")?;
|
||||
stream.write_all(name.as_bytes())?;
|
||||
stream.write_all(b"\n")?;
|
||||
stream.write_all(message.as_bytes())?;
|
||||
stream.write_all(&[b'\n'])?;
|
||||
stream.write_all(message.as_bytes())?;
|
||||
stream.write_all(b"\n\r ")?;
|
||||
|
||||
let mut buf = vec![0; 1];
|
||||
if let Ok(_) = stream.read_exact(&mut buf) {
|
||||
if buf[0] == 0x02 {
|
||||
send_message_auth(host, &format!("\x1f{message}"))?;
|
||||
}
|
||||
let name = format!("\x1f{name}");
|
||||
register_user(host, &name, &name)?;
|
||||
let message = format!("{name}> {message}");
|
||||
send_message_auth(host, &message)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register_user(host: &str, name: &str, password: &str) -> Result<(), Box<dyn Error>> {
|
||||
let mut stream = TcpStream::connect(host)?;
|
||||
stream.write_all(&[0x03])?;
|
||||
stream.write_all(name.as_bytes())?;
|
||||
stream.write_all(&[b'\n'])?;
|
||||
stream.write_all(password.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user