mirror of
https://github.com/chatmail/core.git
synced 2026-05-17 05:46:30 +03:00
refactor(imap): unify IMAP connection setup in Client::connect()
All functions like Client::connect_secure() are now private and every new connection is established in Client::connect().
This commit is contained in:
55
src/imap.rs
55
src/imap.rs
@@ -36,7 +36,6 @@ use crate::login_param::{CertificateChecks, LoginParam, ServerLoginParam};
|
|||||||
use crate::message::{self, Message, MessageState, MessengerMessage, MsgId, Viewtype};
|
use crate::message::{self, Message, MessageState, MessengerMessage, MsgId, Viewtype};
|
||||||
use crate::mimeparser;
|
use crate::mimeparser;
|
||||||
use crate::oauth2::get_oauth2_access_token;
|
use crate::oauth2::get_oauth2_access_token;
|
||||||
use crate::provider::Socket;
|
|
||||||
use crate::receive_imf::{
|
use crate::receive_imf::{
|
||||||
from_field_to_contact_id, get_prefetch_parent_message, receive_imf_inner, ReceivedMsg,
|
from_field_to_contact_id, get_prefetch_parent_message, receive_imf_inner, ReceivedMsg,
|
||||||
};
|
};
|
||||||
@@ -342,52 +341,16 @@ impl Imap {
|
|||||||
);
|
);
|
||||||
self.conn_backoff_ms = max(BACKOFF_MIN_MS, self.conn_backoff_ms);
|
self.conn_backoff_ms = max(BACKOFF_MIN_MS, self.conn_backoff_ms);
|
||||||
|
|
||||||
let connection_res: Result<Client> =
|
let connection_res = Client::connect(
|
||||||
if self.lp.security == Socket::Starttls || self.lp.security == Socket::Plain {
|
context,
|
||||||
let imap_server: &str = self.lp.server.as_ref();
|
self.lp.server.as_ref(),
|
||||||
let imap_port = self.lp.port;
|
self.lp.port,
|
||||||
|
self.strict_tls,
|
||||||
|
self.socks5_config.clone(),
|
||||||
|
self.lp.security,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
if let Some(socks5_config) = &self.socks5_config {
|
|
||||||
if self.lp.security == Socket::Starttls {
|
|
||||||
Client::connect_starttls_socks5(
|
|
||||||
context,
|
|
||||||
imap_server,
|
|
||||||
imap_port,
|
|
||||||
socks5_config.clone(),
|
|
||||||
self.strict_tls,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
} else {
|
|
||||||
Client::connect_insecure_socks5(
|
|
||||||
context,
|
|
||||||
imap_server,
|
|
||||||
imap_port,
|
|
||||||
socks5_config.clone(),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
} else if self.lp.security == Socket::Starttls {
|
|
||||||
Client::connect_starttls(context, imap_server, imap_port, self.strict_tls).await
|
|
||||||
} else {
|
|
||||||
Client::connect_insecure(context, imap_server, imap_port).await
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let imap_server: &str = self.lp.server.as_ref();
|
|
||||||
let imap_port = self.lp.port;
|
|
||||||
|
|
||||||
if let Some(socks5_config) = &self.socks5_config {
|
|
||||||
Client::connect_secure_socks5(
|
|
||||||
context,
|
|
||||||
imap_server,
|
|
||||||
imap_port,
|
|
||||||
self.strict_tls,
|
|
||||||
socks5_config.clone(),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
} else {
|
|
||||||
Client::connect_secure(context, imap_server, imap_port, self.strict_tls).await
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let client = connection_res?;
|
let client = connection_res?;
|
||||||
self.conn_backoff_ms = BACKOFF_MIN_MS;
|
self.conn_backoff_ms = BACKOFF_MIN_MS;
|
||||||
self.ratelimit.send();
|
self.ratelimit.send();
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
use anyhow::{Context as _, Result};
|
use anyhow::{bail, Context as _, Result};
|
||||||
use async_imap::Client as ImapClient;
|
use async_imap::Client as ImapClient;
|
||||||
use async_imap::Session as ImapSession;
|
use async_imap::Session as ImapSession;
|
||||||
use tokio::io::BufWriter;
|
use tokio::io::BufWriter;
|
||||||
@@ -11,6 +11,7 @@ use crate::context::Context;
|
|||||||
use crate::net::session::SessionStream;
|
use crate::net::session::SessionStream;
|
||||||
use crate::net::tls::wrap_tls;
|
use crate::net::tls::wrap_tls;
|
||||||
use crate::net::{connect_starttls_imap, connect_tcp, connect_tls};
|
use crate::net::{connect_starttls_imap, connect_tcp, connect_tls};
|
||||||
|
use crate::provider::Socket;
|
||||||
use crate::socks::Socks5Config;
|
use crate::socks::Socks5Config;
|
||||||
use fast_socks5::client::Socks5Stream;
|
use fast_socks5::client::Socks5Stream;
|
||||||
|
|
||||||
@@ -92,7 +93,40 @@ impl Client {
|
|||||||
Ok(Session::new(session, capabilities))
|
Ok(Session::new(session, capabilities))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn connect_secure(
|
pub async fn connect(
|
||||||
|
context: &Context,
|
||||||
|
host: &str,
|
||||||
|
port: u16,
|
||||||
|
strict_tls: bool,
|
||||||
|
socks5_config: Option<Socks5Config>,
|
||||||
|
security: Socket,
|
||||||
|
) -> Result<Self> {
|
||||||
|
if let Some(socks5_config) = socks5_config {
|
||||||
|
match security {
|
||||||
|
Socket::Automatic => bail!("IMAP port security is not configured"),
|
||||||
|
Socket::Ssl => {
|
||||||
|
Client::connect_secure_socks5(context, host, port, strict_tls, socks5_config)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
Socket::Starttls => {
|
||||||
|
Client::connect_starttls_socks5(context, host, port, socks5_config, strict_tls)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
Socket::Plain => {
|
||||||
|
Client::connect_insecure_socks5(context, host, port, socks5_config).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
match security {
|
||||||
|
Socket::Automatic => bail!("IMAP port security is not configured"),
|
||||||
|
Socket::Ssl => Client::connect_secure(context, host, port, strict_tls).await,
|
||||||
|
Socket::Starttls => Client::connect_starttls(context, host, port, strict_tls).await,
|
||||||
|
Socket::Plain => Client::connect_insecure(context, host, port).await,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn connect_secure(
|
||||||
context: &Context,
|
context: &Context,
|
||||||
hostname: &str,
|
hostname: &str,
|
||||||
port: u16,
|
port: u16,
|
||||||
@@ -109,7 +143,7 @@ impl Client {
|
|||||||
Ok(client)
|
Ok(client)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn connect_insecure(context: &Context, hostname: &str, port: u16) -> Result<Self> {
|
async fn connect_insecure(context: &Context, hostname: &str, port: u16) -> Result<Self> {
|
||||||
let tcp_stream = connect_tcp(context, hostname, port, false).await?;
|
let tcp_stream = connect_tcp(context, hostname, port, false).await?;
|
||||||
let buffered_stream = BufWriter::new(tcp_stream);
|
let buffered_stream = BufWriter::new(tcp_stream);
|
||||||
let session_stream: Box<dyn SessionStream> = Box::new(buffered_stream);
|
let session_stream: Box<dyn SessionStream> = Box::new(buffered_stream);
|
||||||
@@ -121,7 +155,7 @@ impl Client {
|
|||||||
Ok(client)
|
Ok(client)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn connect_starttls(
|
async fn connect_starttls(
|
||||||
context: &Context,
|
context: &Context,
|
||||||
hostname: &str,
|
hostname: &str,
|
||||||
port: u16,
|
port: u16,
|
||||||
@@ -135,7 +169,7 @@ impl Client {
|
|||||||
Ok(client)
|
Ok(client)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn connect_secure_socks5(
|
async fn connect_secure_socks5(
|
||||||
context: &Context,
|
context: &Context,
|
||||||
domain: &str,
|
domain: &str,
|
||||||
port: u16,
|
port: u16,
|
||||||
@@ -156,7 +190,7 @@ impl Client {
|
|||||||
Ok(client)
|
Ok(client)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn connect_insecure_socks5(
|
async fn connect_insecure_socks5(
|
||||||
context: &Context,
|
context: &Context,
|
||||||
domain: &str,
|
domain: &str,
|
||||||
port: u16,
|
port: u16,
|
||||||
@@ -173,7 +207,7 @@ impl Client {
|
|||||||
Ok(client)
|
Ok(client)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn connect_starttls_socks5(
|
async fn connect_starttls_socks5(
|
||||||
context: &Context,
|
context: &Context,
|
||||||
hostname: &str,
|
hostname: &str,
|
||||||
port: u16,
|
port: u16,
|
||||||
|
|||||||
Reference in New Issue
Block a user