Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
amaury1093 committed Dec 4, 2024
1 parent 3a13b7c commit 5d76fe7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
16 changes: 15 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion backend/src/http/v0/check_email/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ impl CheckEmailRequest {
.or_else(|| config.proxy.as_ref())
.cloned(),
smtp_timeout: config.smtp_timeout.map(Duration::from_secs),
smtp_port: self.smtp_port.unwrap_or_default(),
smtp_port: self
.smtp_port
.unwrap_or_else(|| CheckEmailInput::default().smtp_port),
sentry_dsn: config.sentry_dsn.clone(),
..Default::default()
}
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async-smtp = { version = "0.9.1", features = ["runtime-tokio"] }
chrono = { version = "0.4.31", features = ["serde"] }
config = "0.14"
derive_builder = "0.20"
fast-socks5 = "0.9"
fantoccini = { version = "0.21.2" }
futures = { version = "0.3.30" }
hickory-proto = "0.24.0"
Expand All @@ -37,5 +38,4 @@ serde = { version = "1.0.214", features = ["derive"] }
serde_json = "1.0.133"
thiserror = "1.0"
tokio = { version = "1.40.0", features = ["rt-multi-thread", "macros"] }
tokio-socks = "0.5"
tracing = "0.1.40"
23 changes: 16 additions & 7 deletions core/src/smtp/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ use async_recursion::async_recursion;
use async_smtp::commands::{MailCommand, RcptCommand};
use async_smtp::extension::ClientId;
use async_smtp::{SmtpClient, SmtpTransport};
use fast_socks5::client::Config;
use fast_socks5::{client::Socks5Stream, Result};
use rand::rngs::SmallRng;
use rand::{distributions::Alphanumeric, Rng, SeedableRng};
use std::iter;
use std::str::FromStr;
use tokio::io::{AsyncBufRead, AsyncRead, AsyncWrite, BufStream};
use tokio::net::TcpStream;
use tokio_socks::tcp::Socks5Stream;

use super::parser;
use super::{SmtpDetails, SmtpError};
Expand Down Expand Up @@ -66,23 +67,31 @@ async fn connect_to_host(
// hostname verification fails if it ends with '.', for example, using
// SOCKS5 proxies we can `io: incomplete` error.
let host = host.trim_end_matches('.').to_string();

let smtp_client = SmtpClient::new().hello_name(ClientId::Domain(input.hello_name.clone()));

let stream: BufStream<Box<dyn AsyncReadWrite>> = match &input.proxy {
Some(proxy) => {
let target_addr = format!("{}:{}", host, port);
let socks_stream = match (&proxy.username, &proxy.password) {
(Some(username), Some(password)) => {
Socks5Stream::connect_with_password(
(proxy.host.as_ref(), proxy.port),
target_addr,
username,
password,
host.clone(),
port,
username.clone(),
password.clone(),
Config::default(),
)
.await?
}
_ => {
Socks5Stream::connect(
(proxy.host.as_ref(), proxy.port),
host.clone(),
port,
Config::default(),
)
.await?
}
_ => Socks5Stream::connect((proxy.host.as_ref(), proxy.port), target_addr).await?,
};
BufStream::new(Box::new(socks_stream) as Box<dyn AsyncReadWrite>)
}
Expand Down
10 changes: 5 additions & 5 deletions core/src/smtp/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ pub enum SmtpError {
/// Timeout error.
#[error("Timeout error: {0:?}")]
Timeout(Duration),
/// Socks5 proxy error.
#[error("Socks5 error: {0}")]
/// SOCKS5 proxy error.
#[error("SOCKS5 error: {0}")]
#[serde(serialize_with = "ser_with_display")]
Socks5(tokio_socks::Error),
Socks5(fast_socks5::SocksError),
}

impl From<YahooError> for SmtpError {
Expand Down Expand Up @@ -94,8 +94,8 @@ impl From<std::io::Error> for SmtpError {
}
}

impl From<tokio_socks::Error> for SmtpError {
fn from(e: tokio_socks::Error) -> Self {
impl From<fast_socks5::SocksError> for SmtpError {
fn from(e: fast_socks5::SocksError) -> Self {
SmtpError::Socks5(e)
}
}
Expand Down

0 comments on commit 5d76fe7

Please sign in to comment.