Skip to content

Check if an email address exists without sending any email, written in Rust. Comes with a ⚙️ HTTP backend.

refactor: Move sentry from backend to core (#1511)

Amaurybedbd40c13da
diff --git a/Cargo.lock b/Cargo.lock
index ba279885..c0358eda 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -338,9 +338,11 @@ dependencies = [
"rand",
"regex",
"reqwest 0.12.5",
+ "sentry",
"serde",
"serde_json",
"tokio",
+ "tracing",
]
[[package]]
diff --git a/backend/Cargo.toml b/backend/Cargo.toml
index ffb76fb9..b03c8c16 100644
--- a/backend/Cargo.toml
+++ b/backend/Cargo.toml
@@ -7,7 +7,7 @@ publish = false
[dependencies]
async-smtp = "0.6"
-check-if-email-exists = { path = "../core", features = ["headless"] }
+check-if-email-exists = { path = "../core", features = ["headless", "sentry"] }
csv = "1.3.0"
dotenv = "0.15.0"
openssl = { version = "0.10.64", features = ["vendored"] }
diff --git a/backend/src/check.rs b/backend/src/check.rs
index e4047e27..b891e2a2 100644
--- a/backend/src/check.rs
+++ b/backend/src/check.rs
@@ -16,42 +16,10 @@
//! This file contains shared logic for checking one email.
-use std::{env, time::Duration};
+use std::env;
-use check_if_email_exists::{check_email as ciee_check_email, CheckEmailInput, CheckEmailOutput};
use warp::Filter;
-use super::sentry_util;
-
-/// Same as `check-if-email-exists`'s check email, but adds some additional
-/// inputs and error handling.
-pub async fn check_email(input: CheckEmailInput) -> CheckEmailOutput {
- let from_email =
- env::var("RCH_FROM_EMAIL").unwrap_or_else(|_| CheckEmailInput::default().from_email);
- let hello_name =
- env::var("RCH_HELLO_NAME").unwrap_or_else(|_| CheckEmailInput::default().hello_name);
- let smtp_timeout = env::var("RCH_SMTP_TIMEOUT")
- .ok()
- .and_then(|s| s.parse::<u64>().ok())
- .map(Duration::from_secs)
- .or_else(|| CheckEmailInput::default().smtp_timeout);
-
- let input = CheckEmailInput {
- // If we want to override core check-if-email-exists's default values
- // for CheckEmailInput for the backend, we do it here.
- from_email,
- hello_name,
- smtp_timeout,
- ..input
- };
-
- let res = ciee_check_email(&input).await;
-
- sentry_util::log_unknown_errors(&res);
-
- res
-}
-
/// The header which holds the Reacher backend secret.
pub const REACHER_SECRET_HEADER: &str = "x-reacher-secret";
@@ -68,7 +36,7 @@ pub fn check_header() -> warp::filters::BoxedFilter<()> {
let secret: &'static str = Box::leak(Box::new(secret));
- warp::header::exact("x-reacher-secret", secret).boxed()
+ warp::header::exact(REACHER_SECRET_HEADER, secret).boxed()
}
Err(_) => warp::any().boxed(),
}
diff --git a/backend/src/http/v0/bulk/task.rs b/backend/src/http/v0/bulk/task.rs
index e4bc07ac..108033c2 100644
--- a/backend/src/http/v0/bulk/task.rs
+++ b/backend/src/http/v0/bulk/task.rs
@@ -16,8 +16,9 @@
//! This file implements the `POST /bulk` endpoint.
-use check_if_email_exists::LOG_TARGET;
-use check_if_email_exists::{CheckEmailInput, CheckEmailInputProxy, CheckEmailOutput, Reachable};
+use check_if_email_exists::{
+ check_email, CheckEmailInput, CheckEmailInputProxy, CheckEmailOutput, Reachable, LOG_TARGET,
+};
use serde::{Deserialize, Serialize};
use sqlx::{Pool, Postgres};
use sqlxmq::{job, CurrentJob};
@@ -26,7 +27,6 @@ use tracing::{debug, error};
use uuid::Uuid;
use super::error::BulkError;
-use crate::check::check_email;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TaskInput {
@@ -159,7 +159,7 @@ pub async fn email_verification_task(
);
let to_email = check_email_input.to_email.clone();
- let response = check_email(check_email_input).await;
+ let response = check_email(&check_email_input).await;
debug!(
target: LOG_TARGET,
diff --git a/backend/src/http/v0/check_email/post.rs b/backend/src/http/v0/check_email/post.rs
index 677f117c..fdd76922 100644
--- a/backend/src/http/v0/check_email/post.rs
+++ b/backend/src/http/v0/check_email/post.rs
@@ -16,11 +16,10 @@
//! This file implements the `POST /v0/check_email` endpoint.
-use check_if_email_exists::CheckEmailInput;
-use check_if_email_exists::LOG_TARGET;
+use check_if_email_exists::{check_email, CheckEmailInput, LOG_TARGET};
use warp::{http, Filter};
-use crate::check::{check_email, check_header};
+use crate::check::check_header;
use crate::errors;
/// The main endpoint handler that implements the logic of this route.
@@ -33,7 +32,7 @@ async fn handler(body: CheckEmailInput) -> Result<impl warp::Reply, warp::Reject
}))
} else {
// Run the future to check an email.
- Ok(warp::reply::json(&check_email(body).await))
+ Ok(warp::reply::json(&check_email(&body).await))
}
}
diff --git a/backend/src/http/version/get.rs b/backend/src/http/version/get.rs
index 8c981291..a7fb0f32 100644
--- a/backend/src/http/version/get.rs
+++ b/backend/src/http/version/get.rs
@@ -16,7 +16,7 @@
//! This file implements the `GET /version` endpoint.
-use crate::sentry_util::CARGO_PKG_VERSION;
+use crate::CARGO_PKG_VERSION;
use serde::{Deserialize, Serialize};
use warp::Filter;
@@ -39,7 +39,7 @@ pub fn get_version() -> impl Filter<Extract = (impl warp::Reply,), Error = warp:
#[cfg(test)]
mod tests {
use super::get_version;
- use crate::sentry_util::CARGO_PKG_VERSION;
+ use crate::CARGO_PKG_VERSION;
use warp::http::StatusCode;
use warp::test::request;
diff --git a/backend/src/lib.rs b/backend/src/lib.rs
index 6404c911..3914908e 100644
--- a/backend/src/lib.rs
+++ b/backend/src/lib.rs
@@ -17,4 +17,5 @@
pub mod check;
mod errors;
pub mod http;
-pub mod sentry_util;
+
+const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
diff --git a/backend/src/main.rs b/backend/src/main.rs
index c068d8ea..fcdbe518 100644
--- a/backend/src/main.rs
+++ b/backend/src/main.rs
@@ -17,13 +17,12 @@
//! Main entry point of the `reacher_backend` binary. It has two `main`
//! functions, depending on whether the `bulk` feature is enabled or not.
-use check_if_email_exists::LOG_TARGET;
+use check_if_email_exists::{setup_sentry, LOG_TARGET};
use tracing::info;
-use reacher_backend::{
- http::run_warp_server,
- sentry_util::{setup_sentry, CARGO_PKG_VERSION},
-};
+use reacher_backend::http::run_warp_server;
+
+const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
/// Run a HTTP server using warp with bulk endpoints.
#[tokio::main]
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 2e213e76..d0653bab 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -31,8 +31,10 @@ pwned = "0.5.0"
rand = { version = "0.8.5", features = ["small_rng"] }
regex = "1.10.2"
reqwest = { version = "0.12.5", features = ["json", "socks"] }
+sentry = { version = "0.23", optional = true }
serde = { version = "1.0.195", features = ["derive"] }
serde_json = "1.0.124"
+tracing = "0.1.40"
[dev-dependencies]
tokio = { version = "1.35.1" }
diff --git a/core/src/lib.rs b/core/src/lib.rs
index e73a5236..b1f96c61 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -79,6 +79,8 @@ use std::time::{Duration, SystemTime};
use syntax::{check_syntax, get_similar_mail_provider};
pub use util::constants::LOG_TARGET;
pub use util::input_output::*;
+#[cfg(feature = "sentry")]
+pub use util::sentry::*;
use crate::rules::{has_rule, Rule};
@@ -246,7 +248,7 @@ pub async fn check_email(input: &CheckEmailInput) -> CheckEmailOutput {
}
let end_time = SystemTime::now();
- CheckEmailOutput {
+ let res = CheckEmailOutput {
input: to_email.to_string(),
is_reachable: calculate_reachable(&my_misc, &my_smtp),
misc: Ok(my_misc),
@@ -262,5 +264,10 @@ pub async fn check_email(input: &CheckEmailInput) -> CheckEmailOutput {
smtp: smtp_debug,
..Default::default()
},
- }
+ };
+
+ #[cfg(feature = "sentry")]
+ log_unknown_errors(&res);
+
+ res
}
diff --git a/core/src/util/input_output.rs b/core/src/util/input_output.rs
index a6ff3931..f7d27924 100644
--- a/core/src/util/input_output.rs
+++ b/core/src/util/input_output.rs
@@ -237,12 +237,17 @@ impl Default for CheckEmailInput {
fn default() -> Self {
CheckEmailInput {
to_email: "".into(),
- from_email: "[email protected]".into(), // Unused, owned by Reacher
- hello_name: "gmail.com".into(),
+ from_email: env::var("RCH_FROM_EMAIL")
+ .unwrap_or_else(|_| "[email protected]".into()), // Unused, owned by Reacher
+ hello_name: env::var("RCH_HELLO_NAME").unwrap_or_else(|_| "gmail.com".into()),
proxy: None,
smtp_port: 25,
smtp_security: SmtpSecurity::default(),
- smtp_timeout: Some(Duration::from_secs(45)),
+ smtp_timeout: env::var("RCH_SMTP_TIMEOUT")
+ .ok()
+ .and_then(|s| s.parse::<u64>().ok())
+ .map(Duration::from_secs)
+ .or(Some(Duration::from_secs(30))),
#[cfg(not(feature = "headless"))]
yahoo_verif_method: YahooVerifMethod::Api,
#[cfg(feature = "headless")]
diff --git a/core/src/util/mod.rs b/core/src/util/mod.rs
index 96328f4b..a9cb0d33 100644
--- a/core/src/util/mod.rs
+++ b/core/src/util/mod.rs
@@ -16,4 +16,6 @@
pub mod constants;
pub mod input_output;
+#[cfg(feature = "sentry")]
+pub mod sentry;
pub mod ser_with_display;
diff --git a/backend/src/sentry_util.rs b/core/src/util/sentry.rs
similarity index 92%
rename from backend/src/sentry_util.rs
rename to core/src/util/sentry.rs
index e1f967b2..827f3bb1 100644
--- a/backend/src/sentry_util.rs
+++ b/core/src/util/sentry.rs
@@ -24,16 +24,15 @@ use std::borrow::Cow;
use std::env;
use async_smtp::smtp::error::Error as AsyncSmtpError;
-use check_if_email_exists::misc::MiscError;
-use check_if_email_exists::mx::MxError;
-use check_if_email_exists::LOG_TARGET;
-use check_if_email_exists::{smtp::SmtpError, CheckEmailOutput};
use sentry::protocol::{Event, Exception, Level, Values};
use tracing::{debug, info};
-use super::sentry_util;
+use crate::misc::MiscError;
+use crate::mx::MxError;
+use crate::LOG_TARGET;
+use crate::{smtp::SmtpError, CheckEmailOutput};
-pub const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
+const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
/// Setup Sentry.
pub fn setup_sentry() -> sentry::ClientInitGuard {
@@ -128,11 +127,11 @@ pub fn log_unknown_errors(result: &CheckEmailOutput) {
match (&result.misc, &result.mx, &result.smtp) {
(Err(err), _, _) => {
// We log all misc errors.
- sentry_util::error(SentryError::Misc(err), result);
+ error(SentryError::Misc(err), result);
}
(_, Err(err), _) => {
// We log all mx errors.
- sentry_util::error(SentryError::Mx(err), result);
+ error(SentryError::Mx(err), result);
}
(_, _, Err(err)) if err.get_description().is_some() => {
// If the SMTP error is known, we don't track it in Sentry.
@@ -155,7 +154,7 @@ pub fn log_unknown_errors(result: &CheckEmailOutput) {
// Sentry, to be able to debug them better. We don't want to
// spam Sentry and log all instances of the error, hence the
// `count` check.
- sentry_util::error(SentryError::Smtp(err), result);
+ error(SentryError::Smtp(err), result);
}
// If everything is ok, we just return the result.
(Ok(_), Ok(_), Ok(_)) => {}