mirror of
https://github.com/MeexReay/bRAC.git
synced 2025-05-06 13:38:04 +03:00
auth test
This commit is contained in:
parent
e3f51cfacc
commit
04bfc95df1
14
src/chat.rs
14
src/chat.rs
@ -4,7 +4,7 @@ use colored::{Color, Colorize};
|
|||||||
use crossterm::{cursor::{MoveLeft, MoveRight}, event::{self, Event, KeyCode, KeyModifiers, MouseEventKind}, terminal::{self, disable_raw_mode, enable_raw_mode}};
|
use crossterm::{cursor::{MoveLeft, MoveRight}, event::{self, Event, KeyCode, KeyModifiers, MouseEventKind}, terminal::{self, disable_raw_mode, enable_raw_mode}};
|
||||||
use rand::random;
|
use rand::random;
|
||||||
|
|
||||||
use crate::{util::string_chunks, IP_REGEX};
|
use crate::{proto::send_message_auth, util::string_chunks, IP_REGEX};
|
||||||
|
|
||||||
use super::{proto::read_messages, util::sanitize_text, COLORED_USERNAMES, DATE_REGEX, config::Context, proto::send_message};
|
use super::{proto::read_messages, util::sanitize_text, COLORED_USERNAMES, DATE_REGEX, config::Context, proto::send_message};
|
||||||
|
|
||||||
@ -269,10 +269,14 @@ fn poll_events(ctx: Arc<Context>) -> Result<(), Box<dyn Error>> {
|
|||||||
if message.starts_with("/") && !ctx.disable_commands {
|
if message.starts_with("/") && !ctx.disable_commands {
|
||||||
on_command(ctx.clone(), &message)?;
|
on_command(ctx.clone(), &message)?;
|
||||||
} else {
|
} else {
|
||||||
let message = ctx.message_format
|
if let Some(password) = &ctx.auth_password {
|
||||||
.replace("{name}", &ctx.name)
|
send_message_auth(&ctx.host, &ctx.name, password, &message)?;
|
||||||
.replace("{text}", &message);
|
} else {
|
||||||
send_message(&ctx.host, &message)?;
|
let message = ctx.message_format
|
||||||
|
.replace("{name}", &ctx.name)
|
||||||
|
.replace("{text}", &message);
|
||||||
|
send_message(&ctx.host, &message)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
print_console(
|
print_console(
|
||||||
|
@ -169,6 +169,10 @@ pub struct Args {
|
|||||||
/// Configure client
|
/// Configure client
|
||||||
#[arg(short='C', long)]
|
#[arg(short='C', long)]
|
||||||
pub configure: bool,
|
pub configure: bool,
|
||||||
|
|
||||||
|
/// Authentication password
|
||||||
|
#[arg(short='a', long, value_name="PASSWORD")]
|
||||||
|
pub auth: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Context {
|
pub struct Context {
|
||||||
@ -183,7 +187,8 @@ pub struct Context {
|
|||||||
pub update_time: usize,
|
pub update_time: usize,
|
||||||
pub max_messages: usize,
|
pub max_messages: usize,
|
||||||
pub enable_ip_viewing: bool,
|
pub enable_ip_viewing: bool,
|
||||||
pub scroll: Arc<AtomicUsize>
|
pub scroll: Arc<AtomicUsize>,
|
||||||
|
pub auth_password: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Context {
|
impl Context {
|
||||||
@ -206,7 +211,8 @@ impl Context {
|
|||||||
update_time: config.update_time,
|
update_time: config.update_time,
|
||||||
max_messages: config.max_messages,
|
max_messages: config.max_messages,
|
||||||
enable_ip_viewing: args.enable_users_ip_viewing || config.enable_ip_viewing,
|
enable_ip_viewing: args.enable_users_ip_viewing || config.enable_ip_viewing,
|
||||||
scroll: Arc::new(AtomicUsize::new(0))
|
scroll: Arc::new(AtomicUsize::new(0)),
|
||||||
|
auth_password: args.auth.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
34
src/proto.rs
34
src/proto.rs
@ -9,6 +9,40 @@ pub fn send_message(host: &str, message: &str) -> Result<(), Box<dyn Error>> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn send_message_auth(host: &str, name: &str, password: &str, message: &str) -> Result<(), Box<dyn Error>> {
|
||||||
|
let mut stream = TcpStream::connect(host)?;
|
||||||
|
stream.write_all(&[0x02])?;
|
||||||
|
stream.write_all(name.as_bytes())?;
|
||||||
|
stream.write_all(&[b'\n'])?;
|
||||||
|
stream.write_all(password.as_bytes())?;
|
||||||
|
stream.write_all(&[b'\n'])?;
|
||||||
|
stream.write_all(message.as_bytes())?;
|
||||||
|
let mut buf = vec![0; 1];
|
||||||
|
stream.read_exact(&mut buf)?;
|
||||||
|
if buf[0] == 0x01 {
|
||||||
|
register_user(host, name, password)?;
|
||||||
|
send_message_auth(host, name, password, message)?
|
||||||
|
} else if buf[0] == 0x02 {
|
||||||
|
println!("Password is incorrect");
|
||||||
|
panic!()
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn register_user(host: &str, name: &str, password: &str) -> Result<(), Box<dyn Error>> {
|
||||||
|
let mut stream = TcpStream::connect(host)?;
|
||||||
|
stream.write_all(&[0x00])?;
|
||||||
|
stream.write_all(name.as_bytes())?;
|
||||||
|
stream.write_all(&[b'\n'])?;
|
||||||
|
stream.write_all(password.as_bytes())?;
|
||||||
|
// let mut buf = vec![0; 1];
|
||||||
|
// stream.read(&mut buf)?;
|
||||||
|
// if buf[0] == 0x01 {
|
||||||
|
// // похуй
|
||||||
|
// }
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn skip_null(stream: &mut TcpStream) -> Result<Vec<u8>, Box<dyn Error>> {
|
fn skip_null(stream: &mut TcpStream) -> Result<Vec<u8>, Box<dyn Error>> {
|
||||||
loop {
|
loop {
|
||||||
let mut buf = vec![0; 1];
|
let mut buf = vec![0; 1];
|
||||||
|
Loading…
Reference in New Issue
Block a user