summaryrefslogtreecommitdiffstats
path: root/src/bin/client/connection.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/bin/client/connection.rs19
1 files changed, 6 insertions, 13 deletions
diff --git a/src/bin/client/connection.rs b/src/bin/client/connection.rs
index 497ed36..b07bf01 100644
--- a/src/bin/client/connection.rs
+++ b/src/bin/client/connection.rs
@@ -4,17 +4,9 @@ use std::sync::mpsc;
4use std::thread; 4use std::thread;
5use std::time::{Duration, Instant}; 5use std::time::{Duration, Instant};
6use rusqlite::Connection; 6use rusqlite::Connection;
7use bcrypt::hash_with_salt;
8use crate::database; 7use crate::database;
9 8
10const SALT: [u8; 16] = *b"fixedsalt1234567"; 9pub fn connect(addr: &str, conn: &Connection) -> Result<(TcpStream, Option<String>), Box<dyn std::error::Error>> {
11
12fn hash_password(password: &str) -> String {
13 let result = hash_with_salt(password, 10, SALT).unwrap();
14 result.to_string()
15}
16
17pub fn connect(addr: &str, conn: &Connection) -> Result<TcpStream, Box<dyn std::error::Error>> {
18 let (tx, rx) = mpsc::channel(); 10 let (tx, rx) = mpsc::channel();
19 let addr_str = addr.to_string(); 11 let addr_str = addr.to_string();
20 thread::spawn(move || { 12 thread::spawn(move || {
@@ -36,19 +28,20 @@ pub fn connect(addr: &str, conn: &Connection) -> Result<TcpStream, Box<dyn std::
36 28
37 // Check for credentials and auto-auth 29 // Check for credentials and auto-auth
38 if let Some((username, password)) = database::get_credentials(conn, addr) { 30 if let Some((username, password)) = database::get_credentials(conn, addr) {
39 let hashed = hash_password(&password); 31 let auth_msg = format!("AUTH {} {}", username, password);
40 let auth_msg = format!("AUTH {} {}", username, hashed);
41 writeln!(ping_writer, "{}", auth_msg).unwrap(); 32 writeln!(ping_writer, "{}", auth_msg).unwrap();
42 let mut auth_response = String::new(); 33 let mut auth_response = String::new();
43 ping_reader.read_line(&mut auth_response).unwrap(); 34 ping_reader.read_line(&mut auth_response).unwrap();
44 if auth_response.trim().starts_with("OK") { 35 if auth_response.trim().starts_with("OK") {
45 println!("Authenticated as {}", username); 36 println!("Authenticated as {}", username);
37 Ok((s, Some(username)))
46 } else { 38 } else {
47 println!("Authentication failed: {}", auth_response.trim()); 39 println!("Authentication failed: {}", auth_response.trim());
48 // Do not fail, allow manual login 40 Ok((s, None))
49 } 41 }
42 } else {
43 Ok((s, None))
50 } 44 }
51 Ok(s)
52 } 45 }
53 Ok(Err(e)) => Err(Box::new(e)), 46 Ok(Err(e)) => Err(Box::new(e)),
54 Err(mpsc::RecvTimeoutError::Timeout) => Err("Connection timed out after 3 seconds".into()), 47 Err(mpsc::RecvTimeoutError::Timeout) => Err("Connection timed out after 3 seconds".into()),