diff options
| author | salmonik <raisersoft@proton.me> | 2026-02-28 19:32:20 +0100 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-02-28 19:32:20 +0100 |
| commit | 17082597464198fe84be80313b7d51e1e57de639 (patch) | |
| tree | 7b2c7b90c38e3c47e1839247174df2161b736df4 /src/bin/client/connection.rs | |
| parent | remove Cargo.lock from git tracking (diff) | |
| download | zagan-bet-17082597464198fe84be80313b7d51e1e57de639.tar.gz zagan-bet-17082597464198fe84be80313b7d51e1e57de639.zip | |
folder restructuring
Diffstat (limited to 'src/bin/client/connection.rs')
| -rw-r--r-- | src/bin/client/connection.rs | 57 |
1 files changed, 0 insertions, 57 deletions
diff --git a/src/bin/client/connection.rs b/src/bin/client/connection.rs deleted file mode 100644 index 497ed36..0000000 --- a/src/bin/client/connection.rs +++ /dev/null | |||
| @@ -1,57 +0,0 @@ | |||
| 1 | use std::io::{BufRead, BufReader, Write}; | ||
| 2 | use std::net::TcpStream; | ||
| 3 | use std::sync::mpsc; | ||
| 4 | use std::thread; | ||
| 5 | use std::time::{Duration, Instant}; | ||
| 6 | use rusqlite::Connection; | ||
| 7 | use bcrypt::hash_with_salt; | ||
| 8 | use crate::database; | ||
| 9 | |||
| 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>> { | ||
| 18 | let (tx, rx) = mpsc::channel(); | ||
| 19 | let addr_str = addr.to_string(); | ||
| 20 | thread::spawn(move || { | ||
| 21 | match TcpStream::connect(&addr_str) { | ||
| 22 | Ok(s) => tx.send(Ok(s)).unwrap(), | ||
| 23 | Err(e) => tx.send(Err(e)).unwrap(), | ||
| 24 | } | ||
| 25 | }); | ||
| 26 | match rx.recv_timeout(Duration::from_secs(3)) { | ||
| 27 | Ok(Ok(s)) => { | ||
| 28 | let mut ping_writer = s.try_clone().unwrap(); | ||
| 29 | let mut ping_reader = BufReader::new(s.try_clone().unwrap()); | ||
| 30 | let start = Instant::now(); | ||
| 31 | writeln!(ping_writer, "PING").unwrap(); | ||
| 32 | let mut response = String::new(); | ||
| 33 | ping_reader.read_line(&mut response).unwrap(); | ||
| 34 | let elapsed = start.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 | } | ||
| 51 | Ok(s) | ||
| 52 | } | ||
| 53 | Ok(Err(e)) => Err(Box::new(e)), | ||
| 54 | Err(mpsc::RecvTimeoutError::Timeout) => Err("Connection timed out after 3 seconds".into()), | ||
| 55 | Err(_) => Err("Connection attempt failed".into()), | ||
| 56 | } | ||
| 57 | } | ||
