diff options
| author | salmonik <raisersoft@proton.me> | 2026-02-28 19:19:28 +0100 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-02-28 19:19:28 +0100 |
| commit | 4732867ffa8041a3406dade3ae3ad32cf95f0a0c (patch) | |
| tree | cd64723174b55b3459f9da05f883743da811f26b /src/bin/client/connection.rs | |
| parent | better raw mode for windows (diff) | |
| download | zagan-bet-4732867ffa8041a3406dade3ae3ad32cf95f0a0c.tar.gz zagan-bet-4732867ffa8041a3406dade3ae3ad32cf95f0a0c.zip | |
add authentication barebone
Diffstat (limited to '')
| -rw-r--r-- | src/bin/client/connection.rs | 27 |
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; | |||
| 3 | use std::sync::mpsc; | 3 | use std::sync::mpsc; |
| 4 | use std::thread; | 4 | use std::thread; |
| 5 | use std::time::{Duration, Instant}; | 5 | use std::time::{Duration, Instant}; |
| 6 | use rusqlite::Connection; | ||
| 7 | use bcrypt::hash_with_salt; | ||
| 8 | use crate::database; | ||
| 6 | 9 | ||
| 7 | pub fn connect(addr: &str) -> Result<TcpStream, Box<dyn std::error::Error>> { | 10 | const SALT: [u8; 16] = *b"fixedsalt1234567"; |
| 11 | |||
| 12 | fn hash_password(password: &str) -> String { | ||
| 13 | let result = hash_with_salt(password, 10, SALT).unwrap(); | ||
| 14 | result.to_string() | ||
| 15 | } | ||
| 16 | |||
| 17 | pub 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)), |
