2024-11-03 10:28:14 -08:00
|
|
|
use crate::repository::PasswordDirectory;
|
2024-11-02 15:58:35 -07:00
|
|
|
use common::{Request, RESPONSE_NO, RESPONSE_OK};
|
|
|
|
|
use std::io::{Error, Write};
|
|
|
|
|
use std::os::unix::net::UnixStream;
|
|
|
|
|
|
2024-11-03 10:31:31 -08:00
|
|
|
pub struct Handler {
|
2024-11-02 15:58:35 -07:00
|
|
|
stream: UnixStream,
|
2024-11-03 10:28:14 -08:00
|
|
|
handler: PasswordDirectory,
|
2024-11-02 15:58:35 -07:00
|
|
|
}
|
|
|
|
|
|
2024-11-03 10:31:31 -08:00
|
|
|
impl Handler {
|
|
|
|
|
pub fn new(stream: UnixStream, handler: PasswordDirectory) -> Result<Handler, Error> {
|
|
|
|
|
Ok(Handler { stream, handler })
|
2024-11-02 15:58:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn communicate(&mut self) -> Result<(), Error> {
|
|
|
|
|
let request = Request::from_stream(&mut self.stream)?;
|
|
|
|
|
|
|
|
|
|
println!("userid: {}", request.userid);
|
|
|
|
|
if self.handler.check_auth(&request) {
|
|
|
|
|
self.stream.write_all(&RESPONSE_OK)?
|
|
|
|
|
} else {
|
|
|
|
|
self.stream.write_all(&RESPONSE_NO)?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn handle_client(&mut self) {
|
|
|
|
|
if let Err(error) = self.communicate() {
|
|
|
|
|
eprint!("{error}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|