Skip to content

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

fix: Use chromedriver instead of gecko for parallel requests

Amaury Me282e28aeb72
diff --git a/Dockerfile b/Dockerfile
index 03188e66..121ac20d 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -20,43 +20,36 @@ RUN cargo build --bin reacher_backend --release --target=x86_64-unknown-linux-mu
# Final Stage
# ------------------------------------------------------------------------------
-FROM alpine:latest
+FROM zenika/alpine-chrome
-RUN addgroup -g 1000 reacher
+WORKDIR /home/reacher/
-RUN adduser -D -s /bin/sh -u 1000 -G reacher reacher
+USER root
-WORKDIR /home/reacher/bin/
-
-# Install glibc
-# https://github.com/sgerrand/alpine-pkg-glibc
-RUN wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \
- wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r0/glibc-2.35-r0.apk && \
- apk add glibc-2.35-r0.apk
-
-# We need Firefox if we actually want to use GeckoDriver
-RUN apk add firefox-esr
-
-# Then install GeckoDriver
-ARG GECKODRIVER_VERSION=v0.31.0
-RUN wget https://github.com/mozilla/geckodriver/releases/download/$GECKODRIVER_VERSION/geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz && \
- tar -zxf geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz -C /usr/bin
+# Install chromedriver
+# https://github.com/Zenika/alpine-chrome/blob/master/with-chromedriver/Dockerfile
+RUN apk add --no-cache chromium-chromedriver
COPY --from=cargo-build /usr/src/reacher/target/x86_64-unknown-linux-musl/release/reacher_backend .
COPY --from=cargo-build /usr/src/reacher/docker.sh .
-RUN chown reacher:reacher reacher_backend
-RUN chown reacher:reacher docker.sh
+RUN chown chrome:chrome reacher_backend
+RUN chown chrome:chrome docker.sh
-USER reacher
+# User chrome was created in zenika/alpine-chrome
+USER chrome
ENV RUST_LOG=reacher=info
ENV RCH_HTTP_HOST=0.0.0.0
ENV PORT=8080
-ENV RCH_HOTMAIL_USE_HEADLESS=http://localhost:4444
+ENV RCH_HOTMAIL_USE_HEADLESS=http://localhost:9515
# Bulk verification is disabled by default. Set to 1 to enable it.
ENV RCH_ENABLE_BULK=0
EXPOSE 8080
+# Remove entrypoint from parent Docker file
+# https://stackoverflow.com/questions/40122152/how-to-remove-entrypoint-from-parent-image-on-dockerfile
+ENTRYPOINT []
+
CMD ["./docker.sh"]
diff --git a/backend/README.md b/backend/README.md
index b1981ede..25853afc 100644
--- a/backend/README.md
+++ b/backend/README.md
@@ -48,20 +48,20 @@ Then send a `POST http://localhost:8080/v0/check_email` request with the followi
These are the environment variables used to configure the HTTP server. To pass them to the Docker container, use the `-e {ENV_VAR}={VALUE}` flag.
-| Env Var | Required? | Description | Default |
-| ----------------------------------- | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------ |
-| `RCH_ENABLE_BULK` | No | If set to `1`, then bulk verification endpoints will be added to the backend. | 0 |
-| `DATABASE_URL` | Yes if `RCH_ENABLE_BULK==1` | Database connection string for storing results and task queue | not defined |
-| `RCH_HTTP_HOST` | No | The host name to bind the HTTP server to. | `127.0.0.1` |
-| `PORT` | No | The port to bind the HTTP server to, often populated by the cloud provider. | `8080` |
-| `RCH_HOTMAIL_USE_HEADLESS` | No | Set to a running WebDriver process endpoint (e.g. `http://localhost:4444`) to use a headless navigator to Hotmail's password recovery page to check Hotmail/Outlook addresses. | not defined |
-| `RCH_FROM_EMAIL` | No | The email to use in the `MAIL FROM:` SMTP command. | `[email protected]` |
-| `RCH_SENTRY_DSN` | No | If set, bug reports will be sent to this [Sentry](https://sentry.io) DSN. | not defined |
-| `RCH_HEADER_SECRET` | No | If set, then all HTTP requests must have the `x-reacher-secret` header set to this value. This is used to protect the backend against public unwanted HTTP requests. | undefined |
-| `RCH_DATABASE_MAX_CONNECTIONS` | No | (Bulk) Connections created for the database pool | 5 |
-| `RCH_MINIMUM_TASK_CONCURRENCY` | No | (Bulk) Minimum number of concurrent running tasks below which more tasks are fetched | 10 |
-| `RCH_MAXIMUM_CONCURRENT_TASK_FETCH` | No | (Bulk) Maximum number of tasks fetched at once | 20 |
-| `RUST_LOG` | No | One of `trace,debug,warn,error,info`. 💡 PRO TIP: `RUST_LOG=debug` is very handful for debugging purposes. | not defined |
+| Env Var | Required? | Description | Default |
+| ----------------------------------- | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------ |
+| `RCH_ENABLE_BULK` | No | If set to `1`, then bulk verification endpoints will be added to the backend. | 0 |
+| `DATABASE_URL` | Yes if `RCH_ENABLE_BULK==1` | Database connection string for storing results and task queue | not defined |
+| `RCH_HTTP_HOST` | No | The host name to bind the HTTP server to. | `127.0.0.1` |
+| `PORT` | No | The port to bind the HTTP server to, often populated by the cloud provider. | `8080` |
+| `RCH_HOTMAIL_USE_HEADLESS` | No | Set to a running WebDriver process endpoint (e.g. `http://localhost:4444`) to use a headless navigator to Hotmail's password recovery page to check Hotmail/Outlook addresses. We recommend `chromedriver` as it allows parallel requests. | not defined |
+| `RCH_FROM_EMAIL` | No | The email to use in the `MAIL FROM:` SMTP command. | `[email protected]` |
+| `RCH_SENTRY_DSN` | No | If set, bug reports will be sent to this [Sentry](https://sentry.io) DSN. | not defined |
+| `RCH_HEADER_SECRET` | No | If set, then all HTTP requests must have the `x-reacher-secret` header set to this value. This is used to protect the backend against public unwanted HTTP requests. | undefined |
+| `RCH_DATABASE_MAX_CONNECTIONS` | No | (Bulk) Connections created for the database pool | 5 |
+| `RCH_MINIMUM_TASK_CONCURRENCY` | No | (Bulk) Minimum number of concurrent running tasks below which more tasks are fetched | 10 |
+| `RCH_MAXIMUM_CONCURRENT_TASK_FETCH` | No | (Bulk) Maximum number of tasks fetched at once | 20 |
+| `RUST_LOG` | No | One of `trace,debug,warn,error,info`. 💡 PRO TIP: `RUST_LOG=debug` is very handful for debugging purposes. | not defined |
## REST API Documentation
diff --git a/core/src/smtp/hotmail.rs b/core/src/smtp/hotmail.rs
index eb12dcb7..2819738d 100644
--- a/core/src/smtp/hotmail.rs
+++ b/core/src/smtp/hotmail.rs
@@ -24,6 +24,7 @@ use fantoccini::{
};
use futures::TryFutureExt;
use serde::Serialize;
+use serde_json::Map;
use super::SmtpDetails;
use crate::util::ser_with_display::ser_with_display;
@@ -64,8 +65,26 @@ pub async fn check_password_recovery(
to_email,
);
- // Connect to webdriver instance that is listening on port 4444
- let c = ClientBuilder::native().connect(webdriver).await?;
+ // Running in a container, I run into the following error:
+ // Failed to move to new namespace: PID namespaces supported, Network namespace supported, but failed: errno = Operation not permitted
+ // In searching around I found a few different workarounds:
+ // - Enable namespaces: https://github.com/jessfraz/dockerfiles/issues/65#issuecomment-266532289
+ // - Run it with a custom seccomp: https://github.com/jessfraz/dockerfiles/issues/65#issuecomment-217214671
+ // - Run with --no-sandbox: https://github.com/karma-runner/karma-chrome-launcher/issues/125#issuecomment-312668593
+ // For now I went with the --no-sandbox.
+ //
+ // TODO Look into security implications...
+ let mut caps = Map::new();
+ let opts = serde_json::json!({
+ "args": ["--headless", "--disable-gpu", "--no-sandbox", "--disable-dev-shm-usage"],
+ });
+ caps.insert("goog:chromeOptions".to_string(), opts);
+
+ // Connect to WebDriver instance that is listening on `webdriver`
+ let c = ClientBuilder::native()
+ .capabilities(caps)
+ .connect(webdriver)
+ .await?;
// Navigate to Microsoft password recovery page.
c.goto("https://account.live.com/password/reset").await?;
@@ -125,11 +144,12 @@ pub async fn check_password_recovery(
mod tests {
use super::check_password_recovery;
use async_smtp::EmailAddress;
+ use async_std::prelude::FutureExt;
use std::str::FromStr;
// Ignoring this test as it requires a local process of WebDriver running on
// "http://localhost:4444". To debug the headless password recovery page,
- // run e.g. geckodriver and remove the "#[ignore]".
+ // run chromedriver and remove the "#[ignore]".
// Also see: https://github.com/jonhoo/fantoccini
#[tokio::test]
#[ignore]
@@ -144,5 +164,33 @@ mod tests {
.unwrap();
assert!(!res.is_deliverable)
}
+
+ // This email does exist.
+ let email = EmailAddress::from_str("[email protected]").unwrap();
+ // Run 10 headless sessions with the above fake email (not deliverable).
+ // It should not error.
+ for _ in 0..10 {
+ let res = check_password_recovery(&email, "http://localhost:4444")
+ .await
+ .unwrap();
+ assert!(res.is_deliverable)
+ }
+ }
+
+ // This test tests that we can run 2 instances of check_password_recovery.
+ // This will only work with chromedriver (which supports parallel cleints),
+ // but will fail with geckodriver.
+ // ref: https://github.com/jonhoo/fantoccini/issues/111#issuecomment-727650629
+ #[tokio::test]
+ #[ignore]
+ async fn test_parallel() {
+ // This email does not exist.
+ let email = EmailAddress::from_str("[email protected]").unwrap();
+
+ let f1 = check_password_recovery(&email, "http://localhost:4444");
+ let f2 = check_password_recovery(&email, "http://localhost:4444");
+
+ let f = f1.try_join(f2).await;
+ assert!(f.is_ok(), "{:?}", f);
}
}
diff --git a/core/src/util/input_output.rs b/core/src/util/input_output.rs
index ead265e4..48fe8d90 100644
--- a/core/src/util/input_output.rs
+++ b/core/src/util/input_output.rs
@@ -93,7 +93,8 @@ pub struct CheckEmailInput {
/// For Hotmail/Outlook email addresses, use a headless navigator
/// connecting to the password recovery page instead of the SMTP server.
/// This assumes you have a WebDriver compatible process running, then pass
- /// its endpoint, usually http://localhost:4444.
+ /// its endpoint, usually http://localhost:4444. We recommend running
+ /// chromedriver (and not geckodriver) as it allows parallel requests.
///
/// Defaults to None.
#[cfg(feature = "headless")]
diff --git a/docker.sh b/docker.sh
index f42ba237..63d1b370 100755
--- a/docker.sh
+++ b/docker.sh
@@ -1,5 +1,5 @@
#!/bin/ash
# https://docs.docker.com/config/containers/multi-service_container/
-MOZ_HEADLESS=1 geckodriver &
+chromedriver &
./reacher_backend