summaryrefslogtreecommitdiffstats
path: root/src/bin/client/connection.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/bin/client/connection.rs27
1 files changed, 26 insertions, 1 deletions
diff --git a/src/bin/client/connection.rs b/src/bin/client/connection.rs
index d03dc1a..497ed36 100644
--- a/src/bin/client/connection.rs
+++ b/src/bin/client/connection.rs
@@ -3,8 +3,18 @@ use std::net::TcpStream;
3use std::sync::mpsc; 3use std::sync::mpsc;
4use std::thread; 4use std::thread;
5use std::time::{Duration, Instant}; 5use std::time::{Duration, Instant};
6use rusqlite::Connection;
7use bcrypt::hash_with_salt;
8use crate::database;
6 9
7pub fn connect(addr: &str) -> Result<TcpStream, Box<dyn std::error::Error>> { 10const SALT: [u8; 16] = *b"fixedsalt1234567";
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>> {
8 let (tx, rx) = mpsc::channel(); 18 let (tx, rx) = mpsc::channel();
9 let addr_str = addr.to_string(); 19 let addr_str = addr.to_string();
10 thread::spawn(move || { 20 thread::spawn(move || {
@@ -23,6 +33,21 @@ pub fn connect(addr: &str) -> Result<TcpStream, Box<dyn std::error::Error>> {
23 ping_reader.read_line(&mut response).unwrap(); 33 ping_reader.read_line(&mut response).unwrap();
24 let elapsed = start.elapsed(); 34 let elapsed = start.elapsed();
25 println!("\n\rConnected to {} with ping delay: {:?}", addr, elapsed); 35 println!("\n\rConnected to {} with ping delay: {:?}", addr, elapsed);
36
37 // Check for credentials and auto-auth
38 if let Some((username, password)) = database::get_credentials(conn, addr) {
39 let hashed = hash_password(&password);
40 let auth_msg = format!("AUTH {} {}", username, hashed);
41 writeln!(ping_writer, "{}", auth_msg).unwrap();
42 let mut auth_response = String::new();
43 ping_reader.read_line(&mut auth_response).unwrap();
44 if auth_response.trim().starts_with("OK") {
45 println!("Authenticated as {}", username);
46 } else {
47 println!("Authentication failed: {}", auth_response.trim());
48 // Do not fail, allow manual login
49 }
50 }
26 Ok(s) 51 Ok(s)
27 } 52 }
28 Ok(Err(e)) => Err(Box::new(e)), 53 Ok(Err(e)) => Err(Box::new(e)),