2020-07-01 09:51:37 +00:00
|
|
|
use client::{
|
|
|
|
error::{Error as ClientError, NetworkError},
|
|
|
|
Client,
|
|
|
|
};
|
2020-01-02 08:43:45 +00:00
|
|
|
use crossbeam::channel::{unbounded, Receiver, Sender, TryRecvError};
|
2020-02-01 20:39:39 +00:00
|
|
|
use std::{
|
|
|
|
net::ToSocketAddrs,
|
|
|
|
sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Arc,
|
|
|
|
},
|
|
|
|
thread,
|
|
|
|
time::Duration,
|
|
|
|
};
|
2020-07-09 11:42:38 +00:00
|
|
|
use tracing::{debug, trace, warn};
|
2019-04-14 23:28:29 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Error parsing input string or error resolving host name.
|
2019-04-14 23:28:29 +00:00
|
|
|
BadAddress(std::io::Error),
|
2020-01-02 08:43:45 +00:00
|
|
|
// Parsing/host name resolution successful but there was an error within the client.
|
|
|
|
ClientError(ClientError),
|
2019-05-17 09:22:32 +00:00
|
|
|
// Parsing yielded an empty iterator (specifically to_socket_addrs()).
|
2019-04-14 23:28:29 +00:00
|
|
|
NoAddress,
|
2019-05-07 11:09:50 +00:00
|
|
|
ClientCrashed,
|
2019-04-14 23:28:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::large_enum_variant)] // TODO: Pending review in #587
|
2020-01-02 08:43:45 +00:00
|
|
|
pub enum Msg {
|
|
|
|
IsAuthTrusted(String),
|
|
|
|
Done(Result<Client, Error>),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AuthTrust(String, bool);
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Used to asynchronously parse the server address, resolve host names,
|
2020-02-01 20:39:39 +00:00
|
|
|
// and create the client (which involves establishing a connection to the
|
|
|
|
// server).
|
2019-04-14 23:28:29 +00:00
|
|
|
pub struct ClientInit {
|
2020-01-02 08:43:45 +00:00
|
|
|
rx: Receiver<Msg>,
|
|
|
|
trust_tx: Sender<AuthTrust>,
|
2019-10-18 20:05:37 +00:00
|
|
|
cancel: Arc<AtomicBool>,
|
2019-04-14 23:28:29 +00:00
|
|
|
}
|
|
|
|
impl ClientInit {
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::op_ref)] // TODO: Pending review in #587
|
|
|
|
#[allow(clippy::or_fun_call)] // TODO: Pending review in #587
|
2019-08-08 16:05:38 +00:00
|
|
|
pub fn new(
|
|
|
|
connection_args: (String, u16, bool),
|
2020-01-11 21:04:49 +00:00
|
|
|
username: String,
|
|
|
|
view_distance: Option<u32>,
|
2019-08-08 16:05:38 +00:00
|
|
|
password: String,
|
|
|
|
) -> Self {
|
2019-04-14 23:28:29 +00:00
|
|
|
let (server_address, default_port, prefer_ipv6) = connection_args;
|
|
|
|
|
2019-08-15 22:07:09 +00:00
|
|
|
let (tx, rx) = unbounded();
|
2020-01-02 08:43:45 +00:00
|
|
|
let (trust_tx, trust_rx) = unbounded();
|
2019-10-18 20:05:37 +00:00
|
|
|
let cancel = Arc::new(AtomicBool::new(false));
|
|
|
|
let cancel2 = Arc::clone(&cancel);
|
2019-04-14 23:28:29 +00:00
|
|
|
|
2019-05-17 07:55:51 +00:00
|
|
|
thread::spawn(move || {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Parse ip address or resolves hostname.
|
2020-02-01 20:39:39 +00:00
|
|
|
// Note: if you use an ipv6 address, the number after the last colon will be
|
|
|
|
// used as the port unless you use [] around the address.
|
2019-04-14 23:28:29 +00:00
|
|
|
match server_address
|
|
|
|
.to_socket_addrs()
|
|
|
|
.or((server_address.as_ref(), default_port).to_socket_addrs())
|
|
|
|
{
|
2019-05-17 09:22:32 +00:00
|
|
|
Ok(socket_address) => {
|
2019-04-14 23:28:29 +00:00
|
|
|
let (first_addrs, second_addrs) =
|
2019-05-17 09:22:32 +00:00
|
|
|
socket_address.partition::<Vec<_>, _>(|a| a.is_ipv6() == prefer_ipv6);
|
2019-04-14 23:28:29 +00:00
|
|
|
|
|
|
|
let mut last_err = None;
|
|
|
|
|
2020-07-04 10:30:37 +00:00
|
|
|
const FOUR_MINUTES_RETRIES: u64 = 48;
|
|
|
|
'tries: for _ in 0..FOUR_MINUTES_RETRIES {
|
2019-10-18 20:05:37 +00:00
|
|
|
if cancel2.load(Ordering::Relaxed) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for socket_addr in
|
|
|
|
first_addrs.clone().into_iter().chain(second_addrs.clone())
|
|
|
|
{
|
2020-01-11 21:04:49 +00:00
|
|
|
match Client::new(socket_addr, view_distance) {
|
2019-10-18 20:05:37 +00:00
|
|
|
Ok(mut client) => {
|
2020-07-09 11:42:38 +00:00
|
|
|
if let Err(e) =
|
2020-01-11 21:04:49 +00:00
|
|
|
client.register(username, password, |auth_server| {
|
2020-01-02 08:43:45 +00:00
|
|
|
let _ = tx
|
|
|
|
.send(Msg::IsAuthTrusted(auth_server.to_string()));
|
|
|
|
trust_rx
|
|
|
|
.recv()
|
|
|
|
.map(|AuthTrust(server, trust)| {
|
|
|
|
trust && &server == auth_server
|
|
|
|
})
|
|
|
|
.unwrap_or(false)
|
|
|
|
})
|
2019-10-18 20:05:37 +00:00
|
|
|
{
|
2020-07-09 11:42:38 +00:00
|
|
|
last_err = Some(Error::ClientError(e));
|
2020-01-02 08:43:45 +00:00
|
|
|
break 'tries;
|
2019-07-01 11:19:26 +00:00
|
|
|
}
|
2020-01-11 21:04:49 +00:00
|
|
|
let _ = tx.send(Msg::Done(Ok(client)));
|
2019-10-18 20:05:37 +00:00
|
|
|
return;
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-07-09 11:42:38 +00:00
|
|
|
Err(ClientError::NetworkErr(NetworkError::ConnectFailed(e))) => {
|
|
|
|
if e.kind() == std::io::ErrorKind::PermissionDenied {
|
2020-07-13 21:13:54 +00:00
|
|
|
warn!(?e, "Cannot connect to server: Incompatible version");
|
2020-07-09 11:42:38 +00:00
|
|
|
last_err = Some(Error::ClientError(
|
|
|
|
ClientError::NetworkErr(NetworkError::ConnectFailed(e)),
|
|
|
|
));
|
|
|
|
break 'tries;
|
|
|
|
} else {
|
2020-07-13 21:13:54 +00:00
|
|
|
debug!("Cannot connect to server: Timeout (retrying...)");
|
2019-10-18 20:05:37 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2020-07-09 11:42:38 +00:00
|
|
|
Err(e) => {
|
2020-07-13 21:13:54 +00:00
|
|
|
trace!(?e, "Aborting server connection attempt");
|
2020-07-09 11:42:38 +00:00
|
|
|
last_err = Some(Error::ClientError(e));
|
|
|
|
break 'tries;
|
|
|
|
},
|
2019-04-14 23:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-18 20:05:37 +00:00
|
|
|
thread::sleep(Duration::from_secs(5));
|
2019-04-14 23:28:29 +00:00
|
|
|
}
|
2019-05-17 09:22:32 +00:00
|
|
|
// Parsing/host name resolution successful but no connection succeeded.
|
2020-01-02 08:43:45 +00:00
|
|
|
let _ = tx.send(Msg::Done(Err(last_err.unwrap_or(Error::NoAddress))));
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-04-14 23:28:29 +00:00
|
|
|
Err(err) => {
|
2019-05-17 09:22:32 +00:00
|
|
|
// Error parsing input string or error resolving host name.
|
2020-01-02 08:43:45 +00:00
|
|
|
let _ = tx.send(Msg::Done(Err(Error::BadAddress(err))));
|
2020-02-01 20:39:39 +00:00
|
|
|
},
|
2019-04-14 23:28:29 +00:00
|
|
|
}
|
2019-05-17 07:55:51 +00:00
|
|
|
});
|
2019-04-14 23:28:29 +00:00
|
|
|
|
2020-01-02 08:43:45 +00:00
|
|
|
ClientInit {
|
|
|
|
rx,
|
|
|
|
trust_tx,
|
|
|
|
cancel,
|
|
|
|
}
|
2019-04-14 23:28:29 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// Poll if the thread is complete.
|
2020-02-01 20:39:39 +00:00
|
|
|
/// Returns None if the thread is still running, otherwise returns the
|
|
|
|
/// Result of client creation.
|
2020-01-02 08:43:45 +00:00
|
|
|
pub fn poll(&self) -> Option<Msg> {
|
2019-04-14 23:28:29 +00:00
|
|
|
match self.rx.try_recv() {
|
2020-01-02 08:43:45 +00:00
|
|
|
Ok(msg) => Some(msg),
|
2019-04-14 23:28:29 +00:00
|
|
|
Err(TryRecvError::Empty) => None,
|
2020-01-02 08:43:45 +00:00
|
|
|
Err(TryRecvError::Disconnected) => Some(Msg::Done(Err(Error::ClientCrashed))),
|
2019-04-14 23:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2020-01-02 08:43:45 +00:00
|
|
|
/// Report trust status of auth server
|
|
|
|
pub fn auth_trust(&self, auth_server: String, trusted: bool) {
|
|
|
|
let _ = self.trust_tx.send(AuthTrust(auth_server, trusted));
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn cancel(&mut self) { self.cancel.store(true, Ordering::Relaxed); }
|
2019-10-18 20:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for ClientInit {
|
2020-02-01 20:39:39 +00:00
|
|
|
fn drop(&mut self) { self.cancel(); }
|
2019-04-14 23:28:29 +00:00
|
|
|
}
|