浏览代码

Updated notification agent design

Toby Chui 3 年之前
父节点
当前提交
d7a238ff4d
共有 5 个文件被更改,包括 73 次插入20 次删除
  1. 1 0
      .gitignore
  2. 6 3
      Makefile
  3. 42 4
      mod/notification/agents/smtpn/smtpn.go
  4. 17 13
      notification.go
  5. 7 0
      system/smtp_conf.json.example

+ 1 - 0
.gitignore

@@ -26,6 +26,7 @@ fsdb/*
 system/network/wifi/ap/*
 system/storage.json
 system/dev.uuid
+system/smtp_conf.json
 system/storage.json
 system/bridge.json
 system/storage/*.json

+ 6 - 3
Makefile

@@ -4,7 +4,7 @@ temp = $(subst /, ,$@)
 os = $(word 1, $(temp))
 arch = $(word 2, $(temp))
 
-all: $(PLATFORMS) web.tar.gz arozos_file_checksum.sha1
+all:  web.tar.gz $(PLATFORMS) arozos_file_checksum.sha1
 
 binary: $(PLATFORMS)
 
@@ -18,7 +18,7 @@ clean:
 
 $(PLATFORMS):
 	@echo "Building $(os)/$(arch)"
-	GOROOT_FINAL=Git/ GOOS=$(os) GOARCH=$(arch) go build -o 'arozos_$(os)_$(arch)'  -ldflags "-s -w" -trimpath
+	GOROOT_FINAL=Git/ GOOS=$(os) GOARCH=$(arch) go build -o './automake/arozos_$(os)_$(arch)'  -ldflags "-s -w" -trimpath
 
 web.tar.gz:
 	-mkdir ./automake/
@@ -35,11 +35,14 @@ web.tar.gz:
 	@ echo "Remove sensitive information"
 	-rm ./automake/system/dev.uuid
 	-rm ./automake/system/ao.db
-	-mv ./automake/system/storage.json ./automake/system/storage.json.example
+	-rm ./automake/system/smtp_conf.json
+	-rm ./automake/system/storage.json
+	# -mv ./automake/system/storage.json ./automake/system/storage.json.example
 	-rm -rf ./automake/system/aecron/
 	-rm ./automake/system/cron.json
 	-rm ./automake/system/bridge.json
 	-rm ./automake/system/auth/authlog.db
+	
 
 	@ echo "Remove modules that should not go into the build folder"
 	-rm -rf "./automake/web/Cyinput"

+ 42 - 4
mod/notification/agents/smtpn/smtpn.go

@@ -9,6 +9,8 @@ package smtpn
 */
 
 import (
+	"encoding/json"
+	"errors"
 	"io/ioutil"
 	"log"
 	"net/smtp"
@@ -20,13 +22,48 @@ import (
 )
 
 type Agent struct {
-	Hostname              string
+	Hostname              string `json:"-"`
 	SMTPSenderDisplayName string
 	SMTPSender            string
 	SMTPPassword          string
 	SMTPDomain            string
 	SMTPPort              int
-	UsernameToEmailMap    map[string]string
+	UsernameToEmail       func(string) (string, error) `json:"-"`
+}
+
+func NewSMTPNotificationAgent(hostname string, configFile string, usernameToEmailFunction func(string) (string, error)) (*Agent, error) {
+	config, err := ioutil.ReadFile(configFile)
+
+	if err != nil {
+		return nil, errors.New("Unable to load config from file: " + err.Error())
+	}
+
+	//Pasre the json file to agent object
+	newAgent := Agent{}
+	err = json.Unmarshal(config, &newAgent)
+	if err != nil {
+		return nil, errors.New("Unable to parse config file for SMTP authentication")
+	}
+
+	newAgent.Hostname = hostname
+	newAgent.UsernameToEmail = usernameToEmailFunction
+	return &newAgent, nil
+
+}
+
+//Generate an empty config filepath
+func GenerateEmptyConfigFile(configFilepath string) error {
+	demoConfig := Agent{}
+	//Stringify the empty struct
+	js, err := json.MarshalIndent(demoConfig, "", " ")
+	if err != nil {
+		return err
+	}
+
+	//Write to file
+	err = ioutil.WriteFile(configFilepath, js, 0775)
+	return err
+
 }
 
 func (a Agent) Name() string {
@@ -52,8 +89,9 @@ func (a Agent) ConsumerNotification(incomingNotification *notification.Notificat
 	userEmails := [][]string{}
 
 	for _, username := range incomingNotification.Receiver {
-		userEmail, ok := a.UsernameToEmailMap[username]
-		if ok {
+
+		userEmail, err := a.UsernameToEmail(username)
+		if err == nil {
 			userEmails = append(userEmails, []string{username, userEmail})
 		} else {
 			log.Println("[SMTP Notification] Unable to notify " + username + ": Email not set")

+ 17 - 13
notification.go

@@ -1,6 +1,7 @@
 package main
 
 import (
+	"log"
 	"strconv"
 	"time"
 
@@ -17,7 +18,6 @@ func notificationInit() {
 	//Register the notification agents
 
 	//SMTP Mail Sender
-	//Load configuration from database
 
 	//Load username and their email from authAgent
 	userEmailmap := map[string]string{}
@@ -28,27 +28,31 @@ func notificationInit() {
 		}
 	}
 
-	//log.Println(userEmailmap)
-
-	smtpAgent := smtpn.Agent{
-		Hostname:           *host_name,
-		SMTPSender:         "[email protected]",
-		SMTPPassword:       "",
-		SMTPDomain:         "mail.gandi.net",
-		SMTPPort:           587,
-		UsernameToEmailMap: userEmailmap,
+	smtpnConfigPath := "./system/smtp_conf.json"
+	if !fileExists(smtpnConfigPath) {
+		//Create an empty one
+		smtpn.GenerateEmptyConfigFile(smtpnConfigPath)
 	}
 
-	notificationQueue.RegisterNotificationAgent(smtpAgent)
+	smtpAgent, err := smtpn.NewSMTPNotificationAgent(*host_name, smtpnConfigPath,
+		func(username string) (string, error) {
+			//Translate username to email
+			return registerHandler.GetUserEmail(username)
+		})
+
+	if err != nil {
+		log.Println("[Notification/SMTPN] Unable to start smtpn agent: " + err.Error())
+	} else {
+		notificationQueue.RegisterNotificationAgent(smtpAgent)
+	}
 
 	//Create and register other notification agents
 
 	go func() {
 		time.Sleep(10 * time.Second)
-		return
 		notificationQueue.BroadcastNotification(&notification.NotificationPayload{
 			ID:            strconv.Itoa(int(time.Now().Unix())),
-			Title:         "Disk SMART Error",
+			Title:         "Email Test",
 			Message:       "This is a testing notification for showcasing a sample email when DISK SMART error was scanned and discovered.<br> Please visit <a href='https://blog.teacat.io'>here</a> for more information.",
 			Receiver:      []string{"TC"},
 			Sender:        "SMART Nightly Scanner",

+ 7 - 0
system/smtp_conf.json.example

@@ -0,0 +1,7 @@
+{
+ "SMTPSenderDisplayName": "早川ゆい",
+ "SMTPSender": "[email protected]",
+ "SMTPPassword": "verySecurePassword",
+ "SMTPDomain": "mail.gandi.net",
+ "SMTPPort": 587
+}