diff options
Diffstat (limited to 'src/bin/client')
| -rw-r--r-- | src/bin/client/connection.rs | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/src/bin/client/connection.rs b/src/bin/client/connection.rs index 7516836..6bc9b09 100644 --- a/src/bin/client/connection.rs +++ b/src/bin/client/connection.rs | |||
| @@ -8,6 +8,7 @@ use std::thread; | |||
| 8 | use std::time::{Duration, Instant}; | 8 | use std::time::{Duration, Instant}; |
| 9 | use zagan_bet::client::database; | 9 | use zagan_bet::client::database; |
| 10 | use zagan_bet::client::ui::first_contact_lines; | 10 | use zagan_bet::client::ui::first_contact_lines; |
| 11 | use zagan_bet::version::{is_newer, parse_hello, VERSION}; | ||
| 11 | 12 | ||
| 12 | static UNSAFE_MODE: Mutex<bool> = Mutex::new(false); | 13 | static UNSAFE_MODE: Mutex<bool> = Mutex::new(false); |
| 13 | 14 | ||
| @@ -69,16 +70,35 @@ pub fn connect( | |||
| 69 | let mut tls = tls; | 70 | let mut tls = tls; |
| 70 | tls.set_read_timeout(Some(Duration::from_secs(10)))?; | 71 | tls.set_read_timeout(Some(Duration::from_secs(10)))?; |
| 71 | 72 | ||
| 73 | // Version lockstep: the first line after TLS is the handshake, and no | ||
| 74 | // credentials are sent until the server accepts it. | ||
| 72 | let start = Instant::now(); | 75 | let start = Instant::now(); |
| 73 | tls.write_line("PING")?; | 76 | tls.write_line(&format!("HELLO {VERSION}"))?; |
| 74 | let mut response = String::new(); | 77 | let mut response = String::new(); |
| 75 | tls.read_line(&mut response)?; | 78 | if tls.read_line(&mut response)? == 0 { |
| 79 | return Err("server closed the connection during HELLO".into()); | ||
| 80 | } | ||
| 76 | let elapsed = start.elapsed(); | 81 | let elapsed = start.elapsed(); |
| 82 | let response = response.trim(); | ||
| 83 | |||
| 84 | if let Some(message) = response.strip_prefix("ERROR") { | ||
| 85 | return Err(message.trim().to_string().into()); | ||
| 86 | } | ||
| 87 | let Some(hello) = parse_hello(response) else { | ||
| 88 | return Err(format!("unexpected HELLO reply: {response}").into()); | ||
| 89 | }; | ||
| 90 | |||
| 77 | println!( | 91 | println!( |
| 78 | "\n\rConnected to {} with ping delay: {:.1}ms (TLS)", | 92 | "\n\rConnected to {} with ping delay: {:.1}ms (TLS)", |
| 79 | addr, | 93 | addr, |
| 80 | elapsed.as_secs_f64() * 1000.0 | 94 | elapsed.as_secs_f64() * 1000.0 |
| 81 | ); | 95 | ); |
| 96 | if is_newer(&hello.latest, VERSION) { | ||
| 97 | println!( | ||
| 98 | "A newer version is available: {} ({})", | ||
| 99 | hello.latest, hello.url | ||
| 100 | ); | ||
| 101 | } | ||
| 82 | 102 | ||
| 83 | // Credentials are stored per server identity, so any address presenting | 103 | // Credentials are stored per server identity, so any address presenting |
| 84 | // this certificate key can log in with the same stored account. | 104 | // this certificate key can log in with the same stored account. |
