summaryrefslogtreecommitdiffstats
path: root/src/bin
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-10-29 22:16:15 +0100
committersalmonik <salmonik@tuta.io>2026-10-29 22:16:15 +0100
commitbc0de9955c375cf27d52c9934403964205afc182 (patch)
tree42799fb426fb0ec164f44c92a63525003c582f35 /src/bin
parentinitial connection timeout set to 10s (diff)
downloadzagan-bet-bc0de9955c375cf27d52c9934403964205afc182.tar.gz
zagan-bet-bc0de9955c375cf27d52c9934403964205afc182.zip
formatting
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/client/repl.rs27
-rw-r--r--src/bin/client/tls.rs17
-rw-r--r--src/bin/server/args.rs5
-rw-r--r--src/bin/server/main.rs6
-rw-r--r--src/bin/server/tls.rs5
5 files changed, 22 insertions, 38 deletions
diff --git a/src/bin/client/repl.rs b/src/bin/client/repl.rs
index 7fa462a..9af4ea9 100644
--- a/src/bin/client/repl.rs
+++ b/src/bin/client/repl.rs
@@ -1,14 +1,14 @@
1use rusqlite::Connection; 1use rusqlite::Connection;
2use std::io::{stdout, Write}; 2use std::io::{Write, stdout};
3use std::sync::{Arc, Mutex}; 3use std::sync::{Arc, Mutex};
4use std::time::Instant; 4use std::time::Instant;
5 5
6use crossterm::{ 6use crossterm::{
7 cursor::{MoveToColumn, MoveUp, Show}, 7 cursor::{MoveToColumn, MoveUp, Show},
8 event::{read, Event, KeyCode, KeyEventKind, KeyModifiers}, 8 event::{Event, KeyCode, KeyEventKind, KeyModifiers, read},
9 execute, 9 execute,
10 style::Stylize, 10 style::Stylize,
11 terminal::{disable_raw_mode, enable_raw_mode, Clear, ClearType}, 11 terminal::{Clear, ClearType, disable_raw_mode, enable_raw_mode},
12}; 12};
13 13
14use sha2::{Digest, Sha256}; 14use sha2::{Digest, Sha256};
@@ -20,7 +20,7 @@ use zagan_bet::client::ui::{
20 visual_line_count, 20 visual_line_count,
21}; 21};
22use zagan_bet::protocol::io_failure_message; 22use zagan_bet::protocol::io_failure_message;
23use zagan_bet::roulette::{color as roulette_color, WHEEL}; 23use zagan_bet::roulette::{WHEEL, color as roulette_color};
24 24
25use crate::connection::TlsStream; 25use crate::connection::TlsStream;
26 26
@@ -71,8 +71,7 @@ impl ClientState {
71 self.core.set_connected(true); 71 self.core.set_connected(true);
72 self.core.set_logged_in(authed_user); 72 self.core.set_logged_in(authed_user);
73 let fingerprint = database::get_fingerprint(&self.conn, addr); 73 let fingerprint = database::get_fingerprint(&self.conn, addr);
74 self.core 74 self.core.set_host_info(Some(addr.to_string()), fingerprint);
75 .set_host_info(Some(addr.to_string()), fingerprint);
76 database::save_host(&self.conn, addr); 75 database::save_host(&self.conn, addr);
77 } 76 }
78 Err(e) => { 77 Err(e) => {
@@ -178,12 +177,9 @@ impl ClientState {
178 Effect::Print(lines) => { 177 Effect::Print(lines) => {
179 self.commit_prompt_line(); 178 self.commit_prompt_line();
180 for line in lines { 179 for line in lines {
181 let rendered = if line.starts_with("ERROR") 180 let rendered = if line.starts_with("ERROR") || line.contains("failed") {
182 || line.contains("failed")
183 {
184 line.as_str().red().to_string() 181 line.as_str().red().to_string()
185 } else if line.starts_with("Logged in") 182 } else if line.starts_with("Logged in") || line.starts_with("Successfully")
186 || line.starts_with("Successfully")
187 { 183 {
188 line.as_str().green().to_string() 184 line.as_str().green().to_string()
189 } else if line.starts_with("Available commands:") 185 } else if line.starts_with("Available commands:")
@@ -249,10 +245,7 @@ impl ClientState {
249 Effect::ForgetHost(target) => { 245 Effect::ForgetHost(target) => {
250 self.commit_prompt_line(); 246 self.commit_prompt_line();
251 match database::forget_host(&self.conn, &target) { 247 match database::forget_host(&self.conn, &target) {
252 Ok(_) => println!( 248 Ok(_) => println!("Cleared trust and stored credentials for {}.", target),
253 "Cleared trust and stored credentials for {}.",
254 target
255 ),
256 Err(error) => println!("Failed to forget host: {}", error), 249 Err(error) => println!("Failed to forget host: {}", error),
257 } 250 }
258 } 251 }
@@ -297,9 +290,7 @@ impl ClientState {
297 let roulette = lines 290 let roulette = lines
298 .first() 291 .first()
299 .is_some_and(|first| first.contains("ROULETTE")); 292 .is_some_and(|first| first.contains("ROULETTE"));
300 let crash = lines 293 let crash = lines.first().is_some_and(|first| first.contains("CRASH"));
301 .first()
302 .is_some_and(|first| first.contains("CRASH"));
303 let rendered: Vec<String> = lines 294 let rendered: Vec<String> = lines
304 .iter() 295 .iter()
305 .map(|line| { 296 .map(|line| {
diff --git a/src/bin/client/tls.rs b/src/bin/client/tls.rs
index b46f52b..e14ed98 100644
--- a/src/bin/client/tls.rs
+++ b/src/bin/client/tls.rs
@@ -17,10 +17,10 @@ use std::net::TcpStream;
17use std::sync::Arc; 17use std::sync::Arc;
18 18
19use parking_lot::Mutex; 19use parking_lot::Mutex;
20use rustls::client::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier};
21use rustls::DigitallySignedStruct; 20use rustls::DigitallySignedStruct;
21use rustls::client::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier};
22use zagan_bet::protocol::LineBuffer; 22use zagan_bet::protocol::LineBuffer;
23use zagan_bet::tofu::{decide, spki_fingerprint, TrustDecision}; 23use zagan_bet::tofu::{TrustDecision, decide, spki_fingerprint};
24 24
25pub struct TlsConnector { 25pub struct TlsConnector {
26 config: Arc<rustls::ClientConfig>, 26 config: Arc<rustls::ClientConfig>,
@@ -53,7 +53,10 @@ impl TlsConnector {
53 }) 53 })
54 } 54 }
55 55
56 pub fn connect(&self, stream: TcpStream) -> Result<TlsStream, Box<dyn std::error::Error + Send + Sync>> { 56 pub fn connect(
57 &self,
58 stream: TcpStream,
59 ) -> Result<TlsStream, Box<dyn std::error::Error + Send + Sync>> {
57 let mut conn = 60 let mut conn =
58 rustls::ClientConnection::new(Arc::clone(&self.config), self.server_name.clone())?; 61 rustls::ClientConnection::new(Arc::clone(&self.config), self.server_name.clone())?;
59 let mut sock = stream; 62 let mut sock = stream;
@@ -200,10 +203,7 @@ impl TlsStream {
200 } 203 }
201 204
202 /// Apply a read timeout to the underlying TCP socket. 205 /// Apply a read timeout to the underlying TCP socket.
203 pub fn set_read_timeout( 206 pub fn set_read_timeout(&self, duration: Option<std::time::Duration>) -> std::io::Result<()> {
204 &self,
205 duration: Option<std::time::Duration>,
206 ) -> std::io::Result<()> {
207 let mut s = self.session.lock(); 207 let mut s = self.session.lock();
208 s.read_timeout = duration; 208 s.read_timeout = duration;
209 s.sock.set_read_timeout(duration) 209 s.sock.set_read_timeout(duration)
@@ -235,8 +235,7 @@ impl TlsStream {
235 Err(error) 235 Err(error)
236 if matches!( 236 if matches!(
237 error.kind(), 237 error.kind(),
238 std::io::ErrorKind::WouldBlock 238 std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut
239 | std::io::ErrorKind::TimedOut
240 ) => 239 ) =>
241 { 240 {
242 return Ok(()); 241 return Ok(());
diff --git a/src/bin/server/args.rs b/src/bin/server/args.rs
index 1babd1d..1db2a28 100644
--- a/src/bin/server/args.rs
+++ b/src/bin/server/args.rs
@@ -31,10 +31,7 @@ pub fn parse_args() -> Args {
31 if i + 1 < args.len() { 31 if i + 1 < args.len() {
32 ip = args[i + 1].clone(); 32 ip = args[i + 1].clone();
33 if ip != "127.0.0.1" && ip != "0.0.0.0" { 33 if ip != "127.0.0.1" && ip != "0.0.0.0" {
34 eprintln!( 34 eprintln!("Invalid IP address: {}. Must be 127.0.0.1 or 0.0.0.0", ip);
35 "Invalid IP address: {}. Must be 127.0.0.1 or 0.0.0.0",
36 ip
37 );
38 std::process::exit(1); 35 std::process::exit(1);
39 } 36 }
40 i += 2; 37 i += 2;
diff --git a/src/bin/server/main.rs b/src/bin/server/main.rs
index 85e3134..f42574d 100644
--- a/src/bin/server/main.rs
+++ b/src/bin/server/main.rs
@@ -40,9 +40,9 @@ fn main() {
40 let db_path = std::env::var("SERVER_DB").unwrap_or_else(|_| "server.db".to_string()); 40 let db_path = std::env::var("SERVER_DB").unwrap_or_else(|_| "server.db".to_string());
41 match zagan_bet::server::core::recover_orphaned_reservations(&db_path) { 41 match zagan_bet::server::core::recover_orphaned_reservations(&db_path) {
42 Ok(0) => {} 42 Ok(0) => {}
43 Ok(count) => println!( 43 Ok(count) => {
44 "Settled {count} orphaned reservation(s) as abandoned after a restart" 44 println!("Settled {count} orphaned reservation(s) as abandoned after a restart")
45 ), 45 }
46 Err(error) => { 46 Err(error) => {
47 eprintln!("Failed to settle orphaned reservations in {db_path}: {error}"); 47 eprintln!("Failed to settle orphaned reservations in {db_path}: {error}");
48 std::process::exit(1); 48 std::process::exit(1);
diff --git a/src/bin/server/tls.rs b/src/bin/server/tls.rs
index 5d84589..420b5b4 100644
--- a/src/bin/server/tls.rs
+++ b/src/bin/server/tls.rs
@@ -80,10 +80,7 @@ impl TlsStream {
80 80
81 /// Read timeout for the socket. The connection loop uses a short one so it 81 /// Read timeout for the socket. The connection loop uses a short one so it
82 /// can advance crash rounds while no request is in flight. 82 /// can advance crash rounds while no request is in flight.
83 pub fn set_read_timeout( 83 pub fn set_read_timeout(&self, duration: Option<std::time::Duration>) -> std::io::Result<()> {
84 &self,
85 duration: Option<std::time::Duration>,
86 ) -> std::io::Result<()> {
87 self.stream.get_ref().set_read_timeout(duration) 84 self.stream.get_ref().set_read_timeout(duration)
88 } 85 }
89} 86}