From 2679958caa2299cbde6ae9c8f2bbc5910e3ec737 Mon Sep 17 00:00:00 2001 From: Ycarus Date: Mon, 5 Nov 2018 19:14:23 +0100 Subject: [PATCH] Add option to send mail when connection status change --- luci-app-mail/Makefile | 15 +++++ luci-app-mail/luasrc/controller/mail.lua | 8 +++ luci-app-mail/luasrc/model/cbi/mail.lua | 44 ++++++++++++++ luci-app-mail/root/etc/config/mail | 1 + luci-app-mail/root/etc/init.d/msmtp | 60 +++++++++++++++++++ luci-app-mail/root/etc/uci-defaults/4101-mail | 8 +++ .../luasrc/model/cbi/omr-tracker.lua | 8 +++ .../share/omr/post-tracking.d/post-tracking | 10 ++++ openmptcprouter-full/Makefile | 1 + 9 files changed, 155 insertions(+) create mode 100644 luci-app-mail/Makefile create mode 100644 luci-app-mail/luasrc/controller/mail.lua create mode 100644 luci-app-mail/luasrc/model/cbi/mail.lua create mode 100644 luci-app-mail/root/etc/config/mail create mode 100755 luci-app-mail/root/etc/init.d/msmtp create mode 100755 luci-app-mail/root/etc/uci-defaults/4101-mail diff --git a/luci-app-mail/Makefile b/luci-app-mail/Makefile new file mode 100644 index 000000000..b871c4853 --- /dev/null +++ b/luci-app-mail/Makefile @@ -0,0 +1,15 @@ +# +# Copyright (C) 2018 Ycarus (Yannick Chabanois) +# +# + +include $(TOPDIR)/rules.mk + +LUCI_TITLE:=LuCI Mail Configuration +LUCI_DEPENDS:=+msmtp-mta +ca-certificates + +PKG_LICENSE:=GPLv3 + +include ../luci/luci.mk + +# call BuildPackage - OpenWrt buildroot signature diff --git a/luci-app-mail/luasrc/controller/mail.lua b/luci-app-mail/luasrc/controller/mail.lua new file mode 100644 index 000000000..712c20e32 --- /dev/null +++ b/luci-app-mail/luasrc/controller/mail.lua @@ -0,0 +1,8 @@ +local ucic = luci.model.uci.cursor() +local dt = require "luci.cbi.datatypes" +module("luci.controller.mail", package.seeall) + +function index() + entry({"admin", "services", "mail"}, alias("admin", "services", "mail", "index"), _("Mail")) + entry({"admin", "services", "mail", "index"}, cbi("mail")) +end diff --git a/luci-app-mail/luasrc/model/cbi/mail.lua b/luci-app-mail/luasrc/model/cbi/mail.lua new file mode 100644 index 000000000..63969db3b --- /dev/null +++ b/luci-app-mail/luasrc/model/cbi/mail.lua @@ -0,0 +1,44 @@ +-- Copyright 2018 Ycarus (Yannick Chabanois) +-- Licensed to the public under the Apache License 2.0. + +m = Map("mail", translate("Send mail"), translate("Send mail settings")) + +s = m:section(TypedSection, "smtp", translate("SMTP")) +s.anonymous = true +s.addremove = false + +server = s:option(Value, "server", translate("Server")) +server.datatype = "host" +server.placeholder = "smtp.gmail.com" +server.optional = false + +port = s:option(Value, "port", translate("Port")) +port.datatype = "port" +port.optional = false +port.rmempty = true +port.default = "25" + +tls = s:option(Flag, "tls", translate("TLS")) +tls.rmempty = false + +tls_starttls = s:option(Flag, "tls_starttls", translate("STARTTLS")) +tls_starttls.rmempty = false + +user = s:option(Value, "user", translate("User")) +user.rmempty = true + +password = s:option(Value, "password", translate("Password")) +password.password = true +password.rmempty = true + +from = s:option(Value, "from", translate("From")) +from.optional = false +from.rmempty = true +from.placeholder = "myself@gmail.com" + +to = s:option(Value, "to", translate("To")) +to.optional = false +to.rmempty = true +to.placeholder = "myself@gmail.com" + +return m diff --git a/luci-app-mail/root/etc/config/mail b/luci-app-mail/root/etc/config/mail new file mode 100644 index 000000000..07e4b469d --- /dev/null +++ b/luci-app-mail/root/etc/config/mail @@ -0,0 +1 @@ +config smtp 'default' \ No newline at end of file diff --git a/luci-app-mail/root/etc/init.d/msmtp b/luci-app-mail/root/etc/init.d/msmtp new file mode 100755 index 000000000..b6f4ac24d --- /dev/null +++ b/luci-app-mail/root/etc/init.d/msmtp @@ -0,0 +1,60 @@ +#!/bin/sh /etc/rc.common +# Copyright (C) 2018 Ycarus (Yannick Chabanois) + +START=90 +STOP=10 + +USE_PROCD=1 + +validate_section() { + uci_validate_section mail smtp "${1}" \ + 'server:host' \ + 'tls:bool:0' \ + 'tls_starttls:bool:0' \ + 'from:string' \ + 'user:string' \ + 'password:string' \ + 'port:port:25' +} + +config_account() { + local server tls tls_starttls from user password + + validate_section "${1}" || { + _err "validation failed" + return 1 + } + + cat > /tmp/msmtp.tmp <<-EOF + account default + host $server + port $port + EOF + [ -n "$user" ] && [ -n "$password" ] && { + cat >> /tmp/msmtp.tmp <<-EOF + auth on + user $user + password $password + EOF + } + [ -n "$from" ] && echo "from $from" >> /tmp/msmtp.tmp + [ "$tls" = "1" ] && { + cat >> /tmp/msmtp.tmp <<-EOF + tls_trust_file /etc/ssl/certs/ca-certificates.crt + tls on + EOF + } || { + echo 'tls off' >> /tmp/msmtp.tmp + } + [ "$tls_starttls" = "1" ] && { + echo 'tls_starttls on' >> /tmp/msmtp.tmp + } || { + echo 'tls_starttls off' >> /tmp/msmtp.tmp + } +} + +start_service() { + config_load mail + config_foreach config_account smtp + [ -f "/tmp/msmtp.tmp" ] && mv /tmp/msmtp.tmp /etc/msmtprc +} \ No newline at end of file diff --git a/luci-app-mail/root/etc/uci-defaults/4101-mail b/luci-app-mail/root/etc/uci-defaults/4101-mail new file mode 100755 index 000000000..357bf804a --- /dev/null +++ b/luci-app-mail/root/etc/uci-defaults/4101-mail @@ -0,0 +1,8 @@ +#!/bin/sh +uci -q batch <<-EOF >/dev/null + delete ucitrack.@mail[-1] + add ucitrack mail + set ucitrack.@mail[-1].init=mail + commit ucitrack +EOF +exit 0 \ No newline at end of file diff --git a/luci-app-omr-tracker/luasrc/model/cbi/omr-tracker.lua b/luci-app-omr-tracker/luasrc/model/cbi/omr-tracker.lua index 00a17ba11..73be06e2b 100644 --- a/luci-app-omr-tracker/luasrc/model/cbi/omr-tracker.lua +++ b/luci-app-omr-tracker/luasrc/model/cbi/omr-tracker.lua @@ -72,6 +72,10 @@ o:value("httping","httping") o:value("dns","dns") o:value("none","none") +o = s:option(Flag, "mail_alert", translate("Mail alert"), translate("Send a mail when connection state change")) +o.rmempty = false +o.default = false + o = s:option(DynamicList, "hosts", translate("Hosts")) o.placeholder = "4.2.2.1" o.default = { "4.2.2.1", "8.8.8.8" } @@ -115,6 +119,10 @@ o:value("httping","httping") o:value("dns","dns") o:value("none","none") +o = s:option(Flag, "mail_alert", translate("Mail alert"), translate("Send a mail when connection status change")) +o.rmempty = false +o.default = false + o = s:option(DynamicList, "hosts", translate("Hosts")) o.placeholder = "4.2.2.1" o.default = { "4.2.2.1", "8.8.8.8" } diff --git a/mptcp/files/usr/share/omr/post-tracking.d/post-tracking b/mptcp/files/usr/share/omr/post-tracking.d/post-tracking index 63ddc7de9..b4b9c382f 100755 --- a/mptcp/files/usr/share/omr/post-tracking.d/post-tracking +++ b/mptcp/files/usr/share/omr/post-tracking.d/post-tracking @@ -122,10 +122,14 @@ if [ "$OMR_TRACKER_STATUS" = "ERROR" ]; then exit 0 fi [ "$multipath_status" = "off" ] || { + mail_alert="$(uci -q get omr-tracker.$OMR_TRACKER_INTERFACE.mail_alert)" + [ -z "$mail_alert" ] && mail_alert="$(uci -q get omr-tracker.defaults.mail_alert)" if [ "$OMR_TRACKER_STATUS_MSG" = "" ]; then _log "$OMR_TRACKER_DEVICE switched off" + [ "$mail_alert" = "1" ] && echo -e "Subject: OpenMPTCProuter: $OMR_TRACKER_INTERFACE ($OMR_TRACKER_DEVICE) is down\n\nOpenMPTCProuter detected a connection failure of $OMR_TRACKER_INTERFACE ($OMR_TRACKER_DEVICE)." | sendmail $(uci -q get mail.default.to) else _log "$OMR_TRACKER_DEVICE switched off because $OMR_TRACKER_STATUS_MSG" + [ "$mail_alert" = "1" ] && echo -e "Subject: OpenMPTCProuter: $OMR_TRACKER_INTERFACE ($OMR_TRACKER_DEVICE) is down\n\nOpenMPTCProuter detected a connection failure of $OMR_TRACKER_INTERFACE ($OMR_TRACKER_DEVICE). The reason is \"$OMR_TRACKER_STATUS_MSG\"." | sendmail $(uci -q get mail.default.to) fi if [ "$(sysctl -n net.mptcp.mptcp_enabled | tr -d '\n')" = "1" ]; then multipath "$OMR_TRACKER_DEVICE" off > /dev/null 2>&1 @@ -213,6 +217,12 @@ if [ "$OMR_TRACKER_INTERFACE" = "glorytun" ] || [ "$OMR_TRACKER_INTERFACE" = "om exit 0 fi +if [ "$OMR_TRACKER_PREV_STATUS" != "" ] && [ "$OMR_TRACKER_PREV_STATUS" != "$OMR_TRACKER_STATUS" ]; then + mail_alert="$(uci -q get omr-tracker.$OMR_TRACKER_INTERFACE.mail_alert)" + [ -z "$mail_alert" ] && mail_alert="$(uci -q get omr-tracker.defaults.mail_alert)" + [ "$mail_alert" = "1" ] && echo -e "Subject: OpenMPTCProuter: $OMR_TRACKER_INTERFACE ($OMR_TRACKER_DEVICE) is down\n\nOpenMPTCProuter detected a connection failure of $OMR_TRACKER_INTERFACE ($OMR_TRACKER_DEVICE)." | sendmail $(uci -q get mail.default.to) +fi + multipath_config=$(uci -q get "network.$OMR_TRACKER_INTERFACE.multipath" || echo "off") if [ "$multipath_config" = "master" ]; then if [ "$default_gw" != "$OMR_TRACKER_DEVICE_GATEWAY" ] || [ "$default_gw" = "" ]; then diff --git a/openmptcprouter-full/Makefile b/openmptcprouter-full/Makefile index ca9e24c7f..692a441e1 100644 --- a/openmptcprouter-full/Makefile +++ b/openmptcprouter-full/Makefile @@ -37,6 +37,7 @@ MY_DEPENDS := \ luci-app-sqm sqm-scripts-extra \ luci-app-vnstat omr-quota luci-app-omr-quota \ luci-app-mptcp luci-app-openmptcprouter luci-app-omr-bypass \ + luci-app-mail \ omr-6in4 ip6tables-mod-nat luci-proto-ipv6 6to4 6in4 6rd iputils-traceroute6 \ speedtestc \ iftop \