forked from reacherhq/check-if-email-exists
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: additional Gmail validation (reacherhq#1193)
* feat: additional Gmail validation - check the validity of `gmail.com`/`googlemail.com` email addresses via the method outlined [here](https://blog.0day.rocks/abusing-gmail-to-get-previously-unlisted-e-mail-addresses-41544b62b2). - run only via the `--gmail-use-api`/`gmail_use_api` flags (defaulting to `false`.) relates reacherhq#937 * refactor: split out HTTP client move the `create_client` method to a separate file; have the `yahoo` and `gmail` modules reference this. * test: add test for Gmail HTTP API verify Gmail HTTP API behaviour with `[email protected]`, failure indicating that the API is no longer reliable. * fix: correct host checks for Gmail HTTP API should check as per the MX host: - for gmail.com or googlemail.com, this will look like `*.gmail-smtp-in.l.google.com.`. - for Google Apps/Workspace domains, this will look like `*.aspmx.l.google.com.`. Co-authored-by: PsypherPunk <[email protected]>
- Loading branch information
1 parent
bc67a28
commit 49c8f5c
Showing
9 changed files
with
182 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// check-if-email-exists | ||
// Copyright (C) 2018-2022 Reacher | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
|
||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
use super::SmtpDetails; | ||
use crate::{ | ||
smtp::http_api::create_client, | ||
util::{ | ||
constants::LOG_TARGET, input_output::CheckEmailInput, ser_with_display::ser_with_display, | ||
}, | ||
}; | ||
use async_smtp::EmailAddress; | ||
use reqwest::Error as ReqwestError; | ||
use serde::Serialize; | ||
use std::fmt; | ||
|
||
const GLXU_PAGE: &str = "https://mail.google.com/mail/gxlu"; | ||
|
||
/// Possible errors when checking Gmail email addresses. | ||
#[derive(Debug, Serialize)] | ||
pub enum GmailError { | ||
/// Error when serializing or deserializing HTTP requests and responses. | ||
#[serde(serialize_with = "ser_with_display")] | ||
ReqwestError(ReqwestError), | ||
} | ||
|
||
impl fmt::Display for GmailError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "{:?}", self) | ||
} | ||
} | ||
|
||
impl From<ReqwestError> for GmailError { | ||
fn from(error: ReqwestError) -> Self { | ||
GmailError::ReqwestError(error) | ||
} | ||
} | ||
|
||
/// Use HTTP request to verify if a Gmail email address exists. | ||
/// See: <https://blog.0day.rocks/abusing-gmail-to-get-previously-unlisted-e-mail-addresses-41544b62b2> | ||
pub async fn check_gmail( | ||
to_email: &EmailAddress, | ||
input: &CheckEmailInput, | ||
) -> Result<SmtpDetails, GmailError> { | ||
let response = create_client(input, "gmail")? | ||
.head(GLXU_PAGE) | ||
.query(&[("email", to_email)]) | ||
.send() | ||
.await?; | ||
|
||
let email_exists = response.headers().contains_key("Set-Cookie"); | ||
|
||
log::debug!( | ||
target: LOG_TARGET, | ||
"[email={}] gmail response: {:?}", | ||
to_email, | ||
response | ||
); | ||
|
||
Ok(SmtpDetails { | ||
can_connect_smtp: true, | ||
is_deliverable: email_exists, | ||
..Default::default() | ||
}) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::str::FromStr; | ||
|
||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn should_return_is_deliverable_true() { | ||
let to_email = EmailAddress::from_str("[email protected]").unwrap(); | ||
let input = CheckEmailInput::new("[email protected]".to_owned()); | ||
|
||
let smtp_details = check_gmail(&to_email, &input).await; | ||
|
||
assert!(smtp_details.is_ok()); | ||
assert!(smtp_details.unwrap().is_deliverable); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// check-if-email-exists | ||
// Copyright (C) 2018-2022 Reacher | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published | ||
// by the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
|
||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
use crate::util::{constants::LOG_TARGET, input_output::CheckEmailInput}; | ||
use reqwest::Error as ReqwestError; | ||
|
||
/// Helper function to create a reqwest client, with optional proxy. | ||
pub fn create_client( | ||
input: &CheckEmailInput, | ||
api_name: &str, | ||
) -> Result<reqwest::Client, ReqwestError> { | ||
if let Some(proxy) = &input.proxy { | ||
log::debug!( | ||
target: LOG_TARGET, | ||
"[email={}] Using proxy socks://{}:{} for {} API", | ||
input.to_email, | ||
proxy.host, | ||
proxy.port, | ||
api_name, | ||
); | ||
|
||
let proxy = reqwest::Proxy::all(&format!("socks5://{}:{}", proxy.host, proxy.port))?; | ||
reqwest::Client::builder().proxy(proxy).build() | ||
} else { | ||
Ok(reqwest::Client::new()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters