1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
//! Terminal adapter around the shared client transport.
//!
//! All protocol work (TLS, TOFU, HELLO, auto-login) lives in
//! `zagan_bet::client::transport`; this file only decides how to ask a
//! terminal player about a new certificate and how to print the result.
use parking_lot::Mutex;
use std::error::Error;
use zagan_bet::client::transport::{self, TrustPrompt};
use zagan_bet::client::ui::first_contact_lines;
use zagan_bet::version::{is_newer, VERSION};
pub use zagan_bet::client::transport::TlsStream;
static UNSAFE_MODE: Mutex<bool> = Mutex::new(false);
pub fn set_unsafe_mode(unsafe_mode: bool) {
*UNSAFE_MODE.lock() = unsafe_mode;
}
pub fn connect(
addr: &str,
conn: &rusqlite::Connection,
) -> Result<(TlsStream, Option<String>), Box<dyn Error>> {
let unsafe_mode = *UNSAFE_MODE.lock();
// The terminal has no dialog: print the first-contact notice and trust.
let mut trust = |prompt: &TrustPrompt| {
for line in first_contact_lines(&prompt.addr, &prompt.fingerprint) {
println!("{line}");
}
true
};
let connected = transport::connect(addr, conn, unsafe_mode, &mut trust)
.map_err(|error| Box::new(error) as Box<dyn Error>)?;
println!(
"\n\rConnected to {} with ping delay: {:.1}ms (TLS)",
addr,
connected.elapsed.as_secs_f64() * 1000.0
);
if is_newer(&connected.latest, VERSION) {
println!(
"A newer version is available: {} ({})",
connected.latest, connected.url
);
}
if let Some(user) = &connected.user {
println!("Authenticated as {user}");
}
if let Some(error) = &connected.auth_error {
println!("Authentication failed: {error}");
}
Ok((connected.stream, connected.user))
}
|