From 63d2fb93f44d91d41f7550626fd21c9068a8a715 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Sun, 10 Jan 2021 22:32:25 +0200 Subject: [PATCH] melib/nntp: fix not connecting with TLS --- melib/src/backends/nntp.rs | 21 +++++---------------- melib/src/backends/nntp/connection.rs | 20 ++++++++++++++++---- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/melib/src/backends/nntp.rs b/melib/src/backends/nntp.rs index 26f51b00..c3176e26 100644 --- a/melib/src/backends/nntp.rs +++ b/melib/src/backends/nntp.rs @@ -33,18 +33,18 @@ mod connection; pub use connection::*; use crate::conf::AccountSettings; +use crate::connections::timeout; use crate::email::*; use crate::error::{MeliError, Result, ResultIntoMeliError}; use crate::{backends::*, Collection}; use futures::lock::Mutex as FutureMutex; use futures::stream::Stream; use std::collections::{hash_map::DefaultHasher, BTreeSet, HashMap, HashSet}; -use std::future::Future; use std::hash::Hasher; use std::pin::Pin; use std::str::FromStr; use std::sync::{Arc, Mutex}; -use std::time::Instant; +use std::time::{Duration, Instant}; pub type UID = usize; pub static SUPPORTED_CAPABILITIES: &[&str] = &[ @@ -222,10 +222,11 @@ impl MailBackend for NntpType { fn is_online(&self) -> ResultFuture<()> { let connection = self.connection.clone(); Ok(Box::pin(async move { - match timeout(std::time::Duration::from_secs(3), connection.lock()).await { + match timeout(Some(Duration::from_secs(60 * 16)), connection.lock()).await { Ok(mut conn) => { debug!("is_online"); - match debug!(timeout(std::time::Duration::from_secs(3), conn.connect()).await) { + match debug!(timeout(Some(Duration::from_secs(60 * 16)), conn.connect()).await) + { Ok(Ok(())) => Ok(()), Err(err) | Ok(Err(err)) => { conn.stream = Err(err.clone()); @@ -630,15 +631,3 @@ impl FetchState { Ok(Some(ret)) } } - -use futures::future::{self, Either}; - -async fn timeout(dur: std::time::Duration, f: impl Future) -> Result { - futures::pin_mut!(f); - match future::select(f, smol::Timer::after(dur)).await { - Either::Left((out, _)) => Ok(out), - Either::Right(_) => { - Err(MeliError::new("Timedout").set_kind(crate::error::ErrorKind::Network)) - } - } -} diff --git a/melib/src/backends/nntp/connection.rs b/melib/src/backends/nntp/connection.rs index 8d19b54b..0db33caf 100644 --- a/melib/src/backends/nntp/connection.rs +++ b/melib/src/backends/nntp/connection.rs @@ -90,7 +90,7 @@ impl NntpStream { let stream = { let addr = lookup_ipv4(path, server_conf.server_port)?; AsyncWrapper::new(Connection::Tcp( - TcpStream::connect_timeout(&addr, std::time::Duration::new(4, 0)) + TcpStream::connect_timeout(&addr, std::time::Duration::new(16, 0)) .chain_err_kind(crate::error::ErrorKind::Network)?, )) .chain_err_kind(crate::error::ErrorKind::Network)? @@ -130,7 +130,7 @@ impl NntpStream { .any(|cap| cap.eq_ignore_ascii_case("VERSION 2")) { return Err(MeliError::new(format!( - "Could not connect to {}: server is not NNTP compliant", + "Could not connect to {}: server is not NNTP VERSION 2 compliant", &server_conf.server_hostname ))); } @@ -190,8 +190,12 @@ impl NntpStream { ret.stream = AsyncWrapper::new(Connection::Tls( conn_result.chain_err_kind(crate::error::ErrorKind::Network)?, )) + .chain_err_summary(|| format!("Could not initiate TLS negotiation to {}.", path)) .chain_err_kind(crate::error::ErrorKind::Network)?; } + } else { + ret.read_response(&mut res, false, &["200 ", "201 "]) + .await?; } //ret.send_command( // format!( @@ -201,9 +205,17 @@ impl NntpStream { // .as_bytes(), //) //.await?; + if let Err(err) = ret + .stream + .get_ref() + .set_keepalive(Some(std::time::Duration::new(60 * 9, 0))) + { + crate::log( + format!("Could not set TCP keepalive in NNTP connection: {}", err), + crate::LoggingLevel::WARN, + ); + } - ret.read_response(&mut res, false, &["200 ", "201 "]) - .await?; ret.send_command(b"CAPABILITIES").await?; ret.read_response(&mut res, true, command_to_replycodes("CAPABILITIES")) .await?;