summaryrefslogtreecommitdiffstats
path: root/src/client/connection.rs
diff options
context:
space:
mode:
authorsalmonik <raisersoft@proton.me>2026-02-28 21:10:20 +0100
committersalmonik <raisersoft@proton.me>2026-02-28 21:10:20 +0100
commit9e3757eea25b71461c45521dd1ef6c17be34744f (patch)
tree74f598beb0ccf8370c920031df7b981294bcdefe /src/client/connection.rs
parentfolder restructuring (diff)
downloadzagan-bet-9e3757eea25b71461c45521dd1ef6c17be34744f.tar.gz
zagan-bet-9e3757eea25b71461c45521dd1ef6c17be34744f.zip
Revert "folder restructuring"
This reverts commit 17082597464198fe84be80313b7d51e1e57de639.
Diffstat (limited to 'src/client/connection.rs')
-rw-r--r--src/client/connection.rs57
1 files changed, 0 insertions, 57 deletions
diff --git a/src/client/connection.rs b/src/client/connection.rs
deleted file mode 100644
index 497ed36..0000000
--- a/src/client/connection.rs
+++ /dev/null
@@ -1,57 +0,0 @@
1use std::io::{BufRead, BufReader, Write};
2use std::net::TcpStream;
3use std::sync::mpsc;
4use std::thread;
5use std::time::{Duration, Instant};
6use rusqlite::Connection;
7use bcrypt::hash_with_salt;
8use crate::database;
9
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>> {
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}