From 30756782e8f8f4d9a67038974db7e09f5b7110f2 Mon Sep 17 00:00:00 2001 From: i18n Date: Sat, 6 Jan 2024 20:38:39 +0800 Subject: [PATCH 1/2] change addr to IpAddr --- src/lib.rs | 13 +++++++------ src/smtp/builder.rs | 31 +++++++++++++++++++++---------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e9f7377..381411a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -125,15 +125,15 @@ //! pub mod smtp; +use std::net::IpAddr; use std::{fmt::Display, hash::Hash, time::Duration}; -use tokio::io::{AsyncRead, AsyncWrite}; -use tokio_rustls::TlsConnector; - -#[cfg(feature = "builder")] -pub use mail_builder; #[cfg(feature = "dkim")] pub use mail_auth; +#[cfg(feature = "builder")] +pub use mail_builder; +use tokio::io::{AsyncRead, AsyncWrite}; +use tokio_rustls::TlsConnector; #[derive(Debug)] pub enum Error { @@ -201,10 +201,11 @@ pub struct SmtpClientBuilder + PartialEq + Eq + Hash> { pub tls_hostname: T, pub tls_implicit: bool, pub credentials: Option>, - pub addr: String, + pub addr: Option, pub is_lmtp: bool, pub say_ehlo: bool, pub local_host: String, + pub port: u16, } /// SMTP client builder diff --git a/src/smtp/builder.rs b/src/smtp/builder.rs index 56f1808..332314a 100644 --- a/src/smtp/builder.rs +++ b/src/smtp/builder.rs @@ -8,23 +8,27 @@ * except according to those terms. */ +use crate::{Credentials, SmtpClient, SmtpClientBuilder}; use smtp_proto::{EhloResponse, EXT_START_TLS}; use std::hash::Hash; use std::time::Duration; +use tokio::net::ToSocketAddrs; use tokio::{ io::{AsyncRead, AsyncWrite}, net::TcpStream, }; use tokio_rustls::client::TlsStream; -use crate::{Credentials, SmtpClient, SmtpClientBuilder}; - use super::{tls::build_tls_connector, AssertReply}; -impl + PartialEq + Eq + Hash> SmtpClientBuilder { +impl + PartialEq + Eq + Hash> SmtpClientBuilder +where + for<'a> (&'a T, u16): ToSocketAddrs, +{ pub fn new(hostname: T, port: u16) -> Self { SmtpClientBuilder { - addr: format!("{}:{}", hostname.as_ref(), port), + addr: None, + port, timeout: Duration::from_secs(60 * 60), tls_connector: build_tls_connector(false), tls_hostname: hostname, @@ -85,7 +89,7 @@ impl + PartialEq + Eq + Hash> SmtpClientBuilder { pub async fn connect(&self) -> crate::Result>> { tokio::time::timeout(self.timeout, async { let mut client = SmtpClient { - stream: TcpStream::connect(&self.addr).await?, + stream: self.tcp_connect().await?, timeout: self.timeout, }; @@ -130,14 +134,21 @@ impl + PartialEq + Eq + Hash> SmtpClientBuilder { .map_err(|_| crate::Error::Timeout)? } + pub async fn tcp_connect(&self) -> std::io::Result { + let port = self.port; + if let Some(addr) = self.addr { + TcpStream::connect((addr, port)).await + } else { + TcpStream::connect((&self.tls_hostname, port)).await + } + } + /// Connect over clear text (should not be used) pub async fn connect_plain(&self) -> crate::Result> { let mut client = SmtpClient { - stream: tokio::time::timeout(self.timeout, async { - TcpStream::connect(&self.addr).await - }) - .await - .map_err(|_| crate::Error::Timeout)??, + stream: tokio::time::timeout(self.timeout, async { self.tcp_connect().await }) + .await + .map_err(|_| crate::Error::Timeout)??, timeout: self.timeout, }; From 55c69a5849fadcabad09a45d7c168909a4e243b4 Mon Sep 17 00:00:00 2001 From: i18n Date: Sat, 6 Jan 2024 20:49:50 +0800 Subject: [PATCH 2/2] add new_bind_ip --- src/smtp/builder.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/smtp/builder.rs b/src/smtp/builder.rs index 332314a..3f8f544 100644 --- a/src/smtp/builder.rs +++ b/src/smtp/builder.rs @@ -11,8 +11,8 @@ use crate::{Credentials, SmtpClient, SmtpClientBuilder}; use smtp_proto::{EhloResponse, EXT_START_TLS}; use std::hash::Hash; +use std::net::IpAddr; use std::time::Duration; -use tokio::net::ToSocketAddrs; use tokio::{ io::{AsyncRead, AsyncWrite}, net::TcpStream, @@ -21,13 +21,18 @@ use tokio_rustls::client::TlsStream; use super::{tls::build_tls_connector, AssertReply}; -impl + PartialEq + Eq + Hash> SmtpClientBuilder -where - for<'a> (&'a T, u16): ToSocketAddrs, -{ +impl + PartialEq + Eq + Hash> SmtpClientBuilder { pub fn new(hostname: T, port: u16) -> Self { + SmtpClientBuilder::_new(hostname, None, port) + } + + pub fn new_bind_ip(hostname: T, addr: IpAddr, port: u16) -> Self { + SmtpClientBuilder::_new(hostname, Some(addr), port) + } + + fn _new(hostname: T, addr: Option, port: u16) -> Self { SmtpClientBuilder { - addr: None, + addr, port, timeout: Duration::from_secs(60 * 60), tls_connector: build_tls_connector(false), @@ -139,7 +144,7 @@ where if let Some(addr) = self.addr { TcpStream::connect((addr, port)).await } else { - TcpStream::connect((&self.tls_hostname, port)).await + TcpStream::connect((self.tls_hostname.as_ref(), port)).await } }