Skip to content

Commit

Permalink
Merge pull request #1 from shafr/mail-notifications
Browse files Browse the repository at this point in the history
Added e-mail notifications
  • Loading branch information
shafr authored Feb 21, 2021
2 parents d595f32 + 62e8e41 commit b116542
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 15 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/esp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ name: ESP Build

on:
push:
branches:
- master

jobs:
test_builds:

runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
test-platform:
- nodemcuv2
Expand Down
17 changes: 12 additions & 5 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
src_dir = src
lib_dir = lib
data_dir = resources
default_envs = esp32
default_envs = nodemcuv2

; extra_scripts = LittleFSBuilder.py

Expand All @@ -18,6 +18,7 @@ lib_deps =
AsyncMqttClient@0.8.2
arduino-libraries/NTPClient
1532 ;thijse/Arduino-Log
mobizt/ESP Mail Client@^1.0.13

build_unflags =
-Wdeprecated-declarations
Expand All @@ -26,10 +27,16 @@ build_unflags =
platform = espressif8266
board = nodemcuv2

build_flags =
-Teagle.flash.4m.ld

[env:nodemcuv2ota]
platform = espressif8266
board = nodemcuv2

build_flags =
-Teagle.flash.4m.ld

# Don't forget to allow app through firewall - for platformio & Python
upload_port = 192.168.1.1
upload_protocol = espota
Expand All @@ -39,11 +46,11 @@ upload_flags =
-i 192.168.1.1
; -a 'H0We8Adu0SYGFUNTt25C7yuJ'

build_flags =
-Teagle.flash.4m.ld

[env:esp32]
platform = espressif32
board = esp32dev
build_flags =
-Teagle.flash.4m.ld
-Teagle.flash.4m.ld

# include_libs
# ;mobizt/ESP32 Mail Client@^2.1.6
5 changes: 3 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "reporting/reporting.h"
#include "system/ntp.h"
#include "system/ota.h"
#include <ArduinoOTA.h>

void ConnectToWifi()
{
Expand Down Expand Up @@ -57,9 +56,11 @@ void setup()
#ifdef TOMCAT_PORT
serveTomcat();
#endif

}

void loop()
{
ArduinoOTA.handle();
LoopOTA();
notifyLoop();
}
13 changes: 13 additions & 0 deletions src/reporting/consolelog.cpp
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);
}
11 changes: 11 additions & 0 deletions src/reporting/consolelog.h
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
105 changes: 105 additions & 0 deletions src/reporting/email.cpp
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");
}
}
7 changes: 7 additions & 0 deletions src/reporting/email.h
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
3 changes: 0 additions & 3 deletions src/reporting/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ void mqttNotify(String message){
//TODO - subscribe to reset / configure commands in topic ?

void mqttNotifyAttackOccurred(String attackerIpAddress){
Serial.println("Attack occured from: " + attackerIpAddress);

mqttClient.publish("/security/honeypot/attackinprogress", 2, true, "True");
mqttClient.publish("/security/honeypot/attackerip", 2, true, attackerIpAddress.c_str());
}

void mqttResetAttackState(){
Serial.println("Resetting attack state");
mqttClient.publish("/security/honeypot/attackinprogress", 2, false, "False");
}

Expand Down
65 changes: 64 additions & 1 deletion src/reporting/reporting.cpp
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;
}
}
1 change: 1 addition & 0 deletions src/reporting/reporting.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ void notifyAttackOccurred(String attackerIpAddress);
void notify(String message);
void resetAttackState();
void initReporting();
void notifyLoop();

#endif
4 changes: 4 additions & 0 deletions src/system/ota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ void configureOTA()
ArduinoOTA.setRebootOnSuccess(true);
ArduinoOTA.begin();
}

void LoopOTA(){
ArduinoOTA.handle();
}
1 change: 1 addition & 0 deletions src/system/ota.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
#define _OTA_H_

void configureOTA();
void LoopOTA();

#endif
Loading

0 comments on commit b116542

Please sign in to comment.