-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from shafr/mail-notifications
Added e-mail notifications
- Loading branch information
Showing
13 changed files
with
233 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "Arduino.h" | ||
|
||
void consoleLogNotifyAttackOccurred(String attackerIpAddress){ | ||
Serial.println("[NOTIFICATION]: Attack occured from: " + attackerIpAddress); | ||
} | ||
|
||
void consoleLogResetAttackState(){ | ||
Serial.println("Resetting attack state"); | ||
} | ||
|
||
void consoleLogNotify(String message){ | ||
Serial.println("[NOTIFICATION]: " + message); | ||
} |
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,11 @@ | ||
#pragma once | ||
#ifndef _CONSOLE_LOG_H_ | ||
#define _CONSOLE_LOG_H_ | ||
|
||
void consoleLogNotifyAttackOccurred(String attackerIpAddress); | ||
|
||
void consoleLogResetAttackState(); | ||
|
||
void consoleLogNotify(String message); | ||
|
||
#endif |
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,105 @@ | ||
#include <Arduino.h> | ||
#include "user_config.h" | ||
#include <SD.h> | ||
|
||
#ifdef ESP8266 | ||
#include <Ethernet.h> | ||
#endif | ||
|
||
#include <ESP_Mail_Client.h> | ||
|
||
SMTPSession smtp; | ||
ESP_Mail_Session session; | ||
|
||
void smtpCallback(SMTP_Status status); | ||
|
||
void emailInit(){ | ||
smtp.debug(1); | ||
|
||
smtp.callback(smtpCallback); | ||
|
||
session.server.host_name = SMTP_HOST; | ||
session.server.port = SMTP_PORT; | ||
session.login.email = AUTHOR_EMAIL; | ||
session.login.password = AUTHOR_PASSWORD; | ||
// session.login.user_domain = "mydomain.net"; | ||
} | ||
|
||
void sendMail(const char* subject, const char* mailBody) | ||
{ | ||
SMTP_Message message; | ||
|
||
message.sender.name = "ESP Honeypot"; | ||
message.sender.email = AUTHOR_EMAIL; | ||
message.subject = subject; | ||
message.addRecipient("Honeypot User", EMAIL_TARGET_RECEPIENT); | ||
|
||
message.text.content = mailBody; | ||
|
||
// TODO - should we expose encding here as param? | ||
message.text.charSet = "utf-8"; | ||
|
||
/** The content transfer encoding e.g. | ||
* enc_7bit or "7bit" (not encoded) | ||
* enc_qp or "quoted-printable" (encoded) | ||
* enc_base64 or "base64" (encoded) | ||
* enc_binary or "binary" (not encoded) | ||
* enc_8bit or "8bit" (not encoded) | ||
* The default value is "7bit" | ||
*/ | ||
// message.text.transfer_encoding = Content_Transfer_Encoding::enc_7bit; | ||
|
||
/** The message priority | ||
* esp_mail_smtp_priority_high or 1 | ||
* esp_mail_smtp_priority_normal or 3 | ||
* esp_mail_smtp_priority_low or 5 | ||
* The default value is esp_mail_smtp_priority_low | ||
*/ | ||
message.priority = esp_mail_smtp_priority::esp_mail_smtp_priority_normal; | ||
|
||
/** The Delivery Status Notifications e.g. | ||
* esp_mail_smtp_notify_never | ||
* esp_mail_smtp_notify_success | ||
* esp_mail_smtp_notify_failure | ||
* esp_mail_smtp_notify_delay | ||
* The default value is esp_mail_smtp_notify_never | ||
*/ | ||
message.response.notify = esp_mail_smtp_notify_success | esp_mail_smtp_notify_failure | esp_mail_smtp_notify_delay; | ||
|
||
/* Connect to server with the session config */ | ||
if (!smtp.connect(&session)) | ||
return; | ||
|
||
/* Start sending Email and close the session */ | ||
if (!MailClient.sendMail(&smtp, &message)) | ||
Serial.println("Error sending Email, " + smtp.errorReason()); | ||
} | ||
|
||
void smtpCallback(SMTP_Status status) | ||
{ | ||
/* Print the current status */ | ||
Serial.println(status.info()); | ||
|
||
/* Print the sending result */ | ||
if (status.success()) | ||
{ | ||
Serial.println("----------------"); | ||
Serial.printf("Message sent success: %d\n", status.completedCount()); | ||
Serial.printf("Message sent failled: %d\n", status.failedCount()); | ||
Serial.println("----------------\n"); | ||
struct tm dt; | ||
|
||
for (size_t i = 0; i < smtp.sendingResult.size(); i++) | ||
{ | ||
SMTP_Result result = smtp.sendingResult.getItem(i); | ||
localtime_r(&result.timesstamp, &dt); | ||
|
||
Serial.printf("Message No: %d\n", i + 1); | ||
Serial.printf("Status: %s\n", result.completed ? "success" : "failed"); | ||
Serial.printf("Date/Time: %d/%d/%d %d:%d:%d\n", dt.tm_year + 1900, dt.tm_mon + 1, dt.tm_mday, dt.tm_hour, dt.tm_min, dt.tm_sec); | ||
Serial.printf("Recipient: %s\n", result.recipients); | ||
Serial.printf("Subject: %s\n", result.subject); | ||
} | ||
Serial.println("----------------\n"); | ||
} | ||
} |
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,7 @@ | ||
#pragma once | ||
#ifndef _EMAIL_H_ | ||
#define _EMAIL_H_ | ||
|
||
void sendMail(const char* subject, const char* mailBody); | ||
void emailInit(); | ||
#endif |
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 |
---|---|---|
@@ -1,28 +1,91 @@ | ||
#include "Arduino.h" | ||
#include "../user_config.h" | ||
|
||
boolean messagesAvailable = false; | ||
|
||
String notifyMessage = ""; | ||
String attackerIpAddress = ""; | ||
|
||
#if MQTT_ENABLED | ||
#include "mqtt.h" | ||
#endif | ||
|
||
#if EMAIL_ENABLED | ||
#include "email.h" | ||
#endif | ||
|
||
#include "consolelog.h" | ||
|
||
void initReporting() | ||
{ | ||
#if MQTT_ENABLED | ||
mqttInit(); | ||
#endif | ||
|
||
#if EMAIL_ENABLED | ||
emailInit(); | ||
#endif | ||
} | ||
|
||
void notify(String message) | ||
{ | ||
messagesAvailable = true; | ||
notifyMessage = message; | ||
} | ||
|
||
void notifyAttackOccurred(String attackerIp) | ||
{ | ||
messagesAvailable = true; | ||
attackerIpAddress = attackerIp; | ||
} | ||
|
||
void sendNotify(String message) | ||
{ | ||
consoleLogNotify(message); | ||
|
||
#if MQTT_ENABLED | ||
mqttNotify(message); | ||
#endif | ||
#if EMAIL_ENABLED | ||
sendMail(String("Notification").c_str(), message.c_str()); | ||
#endif | ||
} | ||
void notifyAttackOccurred(String attackerIpAddress) | ||
void sendNotifyAttackOccurred(String attackerIpAddress) | ||
{ | ||
consoleLogNotifyAttackOccurred(attackerIpAddress); | ||
|
||
#if MQTT_ENABLED | ||
mqttNotifyAttackOccurred(attackerIpAddress); | ||
#endif | ||
#if EMAIL_ENABLED | ||
sendMail("Attack had occurred!", attackerIpAddress.c_str()); | ||
#endif | ||
} | ||
void resetAttackState() | ||
{ | ||
#if MQTT_ENABLED | ||
mqttResetAttackState(); | ||
#endif | ||
} | ||
|
||
void notifyLoop() | ||
{ | ||
if (!messagesAvailable) | ||
{ | ||
return; | ||
} | ||
|
||
if (notifyMessage.length() > 0) | ||
{ | ||
sendNotify(notifyMessage); | ||
notifyMessage = ""; | ||
messagesAvailable = false; | ||
} | ||
|
||
if (attackerIpAddress.length() > 0) | ||
{ | ||
sendNotifyAttackOccurred(attackerIpAddress); | ||
attackerIpAddress = ""; | ||
messagesAvailable = false; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
#define _OTA_H_ | ||
|
||
void configureOTA(); | ||
void LoopOTA(); | ||
|
||
#endif |
Oops, something went wrong.