1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//! Module for email check and send

use check_if_email_exists::syntax::check_syntax;
use check_if_email_exists::{check_email, CheckEmailInput, Reachable};
use chrono::Utc;
use crypto::{digest::Digest, hmac::Hmac, mac::Mac, sha2::Sha256};
use lazy_static::lazy_static;
use reqwest::header::HeaderMap;
use serde::Serialize;

use crate::config::email::*;

lazy_static! {
    static ref MAIL_DOMAINS: Vec<String> = vec![
        "tsinghua.edu.cn".to_string(),
        "mail.tsinghua.edu.cn".to_string(),
        "mails.tsinghua.edu.cn".to_string(),
        "thuburrow.com".to_string(),
    ];
}

#[derive(Debug, Clone)]
pub enum EmailExistMessage {
    EmailExist,
    EmailNotExist,
    EmailSyntaxError,
    MxNotExist,
    MiscInvalid,
    SmtpUnreachable,
    InternalServerError,
}

pub async fn check_email_exist(email_address: &str) -> (bool, EmailExistMessage) {
    let mut input = CheckEmailInput::new(vec![email_address.into()]);

    input
        .set_from_email("hello@thuburrow.com".into())
        .set_hello_name("thuburrow.com".into());

    let result = check_email(&input).await;

    let result = match result.get(0) {
        Some(res) => res,
        None => {
            return (false, EmailExistMessage::InternalServerError);
        }
    };
    if !result.syntax.is_valid_syntax {
        return (false, EmailExistMessage::EmailSyntaxError);
    }
    if result.is_reachable == Reachable::Invalid
        || result.is_reachable == Reachable::Unknown
        || !result.syntax.is_valid_syntax
    {
        return (false, EmailExistMessage::EmailNotExist);
    }
    match result.mx {
        Ok(ref mx) => {
            let records = mx
                .lookup
                .as_ref()
                .map(|lookup| {
                    lookup
                        .iter()
                        .map(|host| host.exchange().to_string())
                        .collect::<Vec<_>>()
                })
                .unwrap_or_else(|_| Vec::new());
            if records.is_empty() {
                return (false, EmailExistMessage::MxNotExist);
            }
        }
        Err(_) => {
            return (false, EmailExistMessage::InternalServerError);
        }
    }
    match result.misc {
        Ok(ref misc) => {
            if misc.is_disposable || misc.is_role_account {
                return (false, EmailExistMessage::MiscInvalid);
            }
        }
        Err(_) => {
            return (false, EmailExistMessage::InternalServerError);
        }
    }
    match result.smtp {
        Ok(ref smtp) => {
            if !smtp.can_connect_smtp
                || smtp.has_full_inbox
                || !smtp.is_deliverable
                || smtp.is_disabled
            {
                return (false, EmailExistMessage::SmtpUnreachable);
            }
        }
        Err(_) => {
            return (false, EmailExistMessage::InternalServerError);
        }
    }
    (true, EmailExistMessage::EmailExist)
}

pub fn check_email_syntax(email_address: &str) -> bool {
    let syntax_result = check_syntax(email_address);
    if syntax_result.is_valid_syntax {
        MAIL_DOMAINS.contains(&syntax_result.domain)
    } else {
        false
    }
}

#[derive(Serialize)]
pub struct Template {
    #[serde(rename = "TemplateID")]
    pub template_id: i32,
    #[serde(rename = "TemplateData")]
    pub template_data: String,
}

#[derive(Serialize)]
pub struct Body {
    #[serde(rename = "FromEmailAddress")]
    pub from_email_address: String,
    #[serde(rename = "Destination")]
    pub destination: Vec<String>,
    #[serde(rename = "Template")]
    pub template: Template,
    #[serde(rename = "Subject")]
    pub subject: String,
}

fn hash(s: String) -> String {
    let mut hasher = Sha256::new();
    hasher.input_str(&s);
    hasher.result_str()
}

pub fn get_payload(param: &Body) -> String {
    let payload_str = serde_json::to_string(param).unwrap();
    let payload_vec: Vec<&str> = payload_str.split(':').collect();
    let payload = payload_vec.join(": ");
    let payload_vec: Vec<&str> = payload.split(',').collect();
    payload_vec.join(", ")
}

pub fn sign<'a>(key: &'a [u8], msg: &'a [u8]) -> Vec<u8> {
    let mut hmac = Hmac::new(Sha256::new(), key);
    hmac.input(msg);
    let result = hmac.result();
    let code = result.code();
    code.to_vec()
}

pub fn assemble_headers(timestamp: String) -> HeaderMap {
    let mut headers = HeaderMap::new();
    headers.insert("Host", "ses.tencentcloudapi.com".parse().unwrap());
    headers.insert(
        "Content-Type",
        "application/json; charset=utf-8".parse().unwrap(),
    );
    headers.insert("X-TC-Action", "SendEmail".parse().unwrap());
    headers.insert("X-TC-Timestamp", timestamp.parse().unwrap());
    headers.insert("X-TC-Version", "2020-10-02".parse().unwrap());
    headers.insert("X-TC-Region", "ap-hongkong".parse().unwrap());
    headers
}

pub fn signature(param: &Body, timestamp: String, date: String) -> String {
    // define signature parameters
    let secret_id = &*SECRET_ID;
    let secret_key = &*SECRET_KEY;
    let host = "ses.tencentcloudapi.com".to_string();

    let service = "ses".to_string();
    let algorithm = "TC3-HMAC-SHA256";
    // step 1: attach standard request string
    let http_request_method = "POST".to_string();
    let canonical_uri = "/".to_string();
    let canonical_querystring = "".to_string();
    let ct = "application/json; charset=utf-8".to_string();
    let canonical_headers = format!("content-type:{}\nhost:{}\n", ct, host);
    let signed_headers = "content-type;host".to_string();
    let payload = get_payload(param);
    let hashed_request_payload = hash(payload);

    let canonical_request = format!(
        "{}\n{}\n{}\n{}\n{}\n{}",
        http_request_method,
        canonical_uri,
        canonical_querystring,
        canonical_headers,
        signed_headers,
        hashed_request_payload
    );
    // println!("{}", canonical_request);

    // step 2: attach string to sign
    let credential_scope = format!("{}/{}/tc3_request", date, service);
    let hashed_canonical_request = hash(canonical_request);
    let string_to_sign = format!(
        "{}\n{}\n{}\n{}",
        algorithm, timestamp, credential_scope, hashed_canonical_request
    );
    // println!("{}", string_to_sign);

    // step 3: generate signature
    let key = "TC3".to_string() + secret_key;
    let secret_date = sign(key.as_bytes(), date.as_bytes());
    let secret_service = sign(&secret_date, service.as_bytes());
    let secret_signing = sign(&secret_service, "tc3_request".as_bytes());
    let mut hmac = Hmac::new(Sha256::new(), &secret_signing);
    hmac.input(string_to_sign.as_bytes());
    let signature = hex::encode(hmac.result().code());
    // println!("{}", signature);

    // step 4: attach Authorization
    format!(
        "{} Credential={}/{}, SignedHeaders={}, Signature={}",
        algorithm, secret_id, credential_scope, signed_headers, signature
    )
}

pub async fn send(user_email: String, verification_code: String) -> Result<String, reqwest::Error> {
    // create client
    let client = reqwest::Client::new();
    let now = Utc::now();
    let timestamp = now.timestamp().to_string();
    let date = now.format("%Y-%m-%d").to_string();
    // let timestamp = 1638791815.to_string();
    // let host = "ses.tencentcloudapi.com".to_string();
    // let action = "SendEmail".to_string();
    // let region = "ap-hongkong".to_string();
    // let version = "2020-10-02".to_string();

    // generate header
    let mut headers = assemble_headers(timestamp.clone());

    // operation type
    let operation: String = match verification_code.len() {
        6 => "注册".to_string(),
        _ => "找回密码".to_string(),
    };

    // generate body data
    let body = Body {
        from_email_address: "THUBurrow <no-reply@mail.thuburrow.com>".to_string(),
        destination: vec![user_email],
        template: Template {
            template_id: 22078,
            template_data: format!(
                "{{\\\"operation\\\":\"{}\",\\\"code\\\":\"{}\"}}",
                operation, verification_code
            ),
        },
        subject: "Verification Email".to_string(),
    };
    let payload = get_payload(&body);
    // println!("{}", payload);

    // generate authorization, set header
    let auth = signature(&body, timestamp.clone(), date.clone());
    headers.insert("Authorization", auth.parse().unwrap());
    // let req = format!("\ncurl -X POST https://{} -H \"Authorization: {}\" -H \"Content-Type: application/json; charset=utf-8\" -H \"Host: {}\" -H \"X-TC-Action: {}\" -H \"X-TC-Timestamp: {}\" -H \"X-TC-Version: {}\" -H \"X-TC-Region: {}\" -d \'{}\'"
    //     , host
    //     , auth
    //     , host
    //     , action
    //     , timestamp
    //     , version
    //     , region
    //     , payload
    // );
    // println!("{}", req);
    // send request
    let response = client
        .post("https://ses.tencentcloudapi.com")
        .headers(headers)
        .body(payload)
        .send()
        .await?;
    // println!("{}", response);
    Ok(response.text().await?)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_check_email_syntax() {
        assert_eq!(check_email_syntax(""), false);
        assert_eq!(check_email_syntax("a"), false);
        assert_eq!(check_email_syntax("a@"), false);
        assert_eq!(check_email_syntax("a@b"), false);
        assert_eq!(check_email_syntax("a@b."), false);
        assert_eq!(check_email_syntax("test@mails.tsinghua.edu.cn"), true);
        assert_eq!(check_email_syntax("@mails.tsinghua.edu.cn"), false);
        assert_eq!(check_email_syntax("test@163.com"), false);
        assert_eq!(check_email_syntax("test()@mails.tsinghua.edu.cn"), false);
        assert_eq!(check_email_syntax("sys-learn2018@tsinghua.edu.cn"), true);
        assert_eq!(check_email_syntax("shetuan@mail.tsinghua.edu.cn"), true);
    }

    #[tokio::test]
    async fn test_check_email_exist() {
        assert_eq!(check_email_exist("").await.0, false);
        assert_eq!(
            check_email_exist("test@mails.tsinghua.edu.cn").await.0,
            false
        );
        assert_eq!(check_email_exist("test@163.com").await.0, false);
        assert_eq!(
            check_email_exist("test()@mails.tsinghua.edu.cn").await.0,
            false
        );
        assert_eq!(
            check_email_exist("sys-learn2018@tsinghua.edu.cn").await.0,
            true
        );
        assert_eq!(
            check_email_exist("shetuan@mail.tsinghua.edu.cn").await.0,
            true
        );
    }

    #[test]
    fn test_to_hex() {
        assert_eq!(hex::encode(b"hello"), "68656c6c6f".to_string());
    }

    #[test]
    fn test_sign() {
        assert_eq!(
            sign(b"key", b"msg"),
            hex::decode("2d93cbc1be167bcb1637a4a23cbff01a7878f0c50ee833954ea5221bb1b8c628")
                .unwrap()
        );
    }

    #[test]
    fn test_assemble_headers() {
        let timestamp = "1638791815".to_string();
        let headers = assemble_headers(timestamp);
        assert_eq!(headers["Host"], "ses.tencentcloudapi.com");
        assert_eq!(headers["Content-Type"], "application/json; charset=utf-8");
        assert!(headers.contains_key("X-TC-Timestamp"));
        assert_eq!(headers["X-TC-Region"], "ap-hongkong");
        assert_eq!(headers["X-TC-Action"], "SendEmail");
        assert_eq!(headers["X-TC-Version"], "2020-10-02");
    }

    #[test]
    fn test_gen_payload() {
        let param = Body {
            from_email_address: "THUBurrow <noreply@testmail.thuburrow.com>".to_string(),
            destination: vec!["abc@qq.com".to_string()],
            template: Template {
                template_id: 21517,
                template_data: format!("{{\\\"code\\\":\"{}\"}}", "abc123"),
            },
            subject: "Verification Email".to_string(),
        };
        println!("{}", get_payload(&param));
        assert_eq!(get_payload(&param), r#"{"FromEmailAddress": "THUBurrow <noreply@testmail.thuburrow.com>", "Destination": ["abc@qq.com"], "Template": {"TemplateID": 21517, "TemplateData": "{\\\"code\\\": \"abc123\"}"}, "Subject": "Verification Email"}"#.to_string());
    }

    #[test]
    fn test_signature() {
        let param = Body {
            from_email_address: "THUBurrow <noreply@testmail.thuburrow.com>".to_string(),
            destination: vec!["abc@qq.com".to_string()],
            template: Template {
                template_id: 21517,
                template_data: format!("{{\\\"code\\\":\"{}\"}}", "abc123"),
            },
            subject: "Verification Email".to_string(),
        };
        let timestamp = 1638791815.to_string();
        let date = "2021-12-06".to_string();
        println!("{}", signature(&param, timestamp.clone(), date.clone()));
        assert_eq!(signature(&param, timestamp, date), "TC3-HMAC-SHA256 Credential=/2021-12-06/ses/tc3_request, SignedHeaders=content-type;host, Signature=8cd08830134ead51d3b488e84a14a148131caa5a81fa29a8366370ba39a6eb18".to_string());
    }
}