diff --git a/config/config.go b/config/config.go index 4f2c1c4..0658589 100644 --- a/config/config.go +++ b/config/config.go @@ -19,7 +19,7 @@ import ( func InitialConfig() model.Config { workPath, _ := os.Executable() filePath := path.Dir(workPath) - filePath = filepath.Join(filePath, "/config/.yml") + filePath = filepath.Join(filePath, "/config/config.yml") viper.SetConfigName("config") viper.SetConfigType("yml") viper.AddConfigPath("./config") @@ -41,9 +41,19 @@ func InitialConfig() model.Config { func getConfig() model.Config { var config model.Config - config.Quality = viper.GetString("img.quality") - config.UserName = viper.GetString("info.username") - config.PassWord = viper.GetString("info.password") + config.Quality = viper.GetString("images.quality") + config.UserName = viper.GetString("user.username") + config.PassWord = viper.GetString("user.password") + config.MsgEnabled = viper.GetBool("message.enabled") + config.MsgType = viper.GetString("message.type") config.BarkUrl = viper.GetString("message.bark.url") + config.BarkKey = viper.GetString("message.bark.key") + config.MailHost = viper.GetString("message.mail.host") + config.MailProtocol = viper.GetString("message.mail.protocol") + config.MailPort = viper.GetInt("message.mail.port") + config.MailUser = viper.GetString("message.mail.username") + config.MailPwd = viper.GetString("message.mail.password") + config.MailFromName = viper.GetString("message.mail.from_name") + config.MailTo = viper.GetStringSlice("message.mail.to") return config } diff --git a/go.mod b/go.mod index 697d480..c53c51c 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.18 require ( github.com/fsnotify/fsnotify v1.5.4 + github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible github.com/sirupsen/logrus v1.9.0 github.com/spf13/viper v1.13.0 gopkg.in/natefinch/lumberjack.v2 v2.0.0 diff --git a/go.sum b/go.sum index 15c367b..35117ec 100644 --- a/go.sum +++ b/go.sum @@ -123,6 +123,8 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= +github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA= +github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= diff --git a/message/bark.go b/message/bark.go new file mode 100644 index 0000000..839c1f1 --- /dev/null +++ b/message/bark.go @@ -0,0 +1,59 @@ +package message + +import ( + "bytes" + "encoding/json" + log "github.com/sirupsen/logrus" + + "luxshare-daily-report/global" + "net/http" +) + +type Bark struct { + url string + key string +} + +var b = Bark{} + +func initBark() { + b.url = global.GLO_CONFIG.BarkUrl + b.key = global.GLO_CONFIG.BarkKey + Register("bark", b) +} + +func (m Bark) Send(message Body) { + log.Println("[bark] Sending by bark...") + var reqBody = Request{ + DeviceKey: b.key, + Title: message.Title, + Body: message.Content, + Icon: "https://m.luxshare-ict.com/favicon.ico", + //Url: "https://github.com/zggsong", + } + req, _ := json.Marshal(reqBody) + resp, err := http.Post(m.url, "application/json; charset=utf-8", bytes.NewReader(req)) + if err != nil { + log.Fatalf("[bark] http post failed: %v\n", err) + } + defer resp.Body.Close() + log.Printf("[bark] Send successful") +} + +type Request struct { + Body string `json:"body"` + DeviceKey string `json:"device_key"` + Title string `json:"title"` + Badge int `json:"badge"` + Category string `json:"category"` + Sound string `json:"sound"` + Icon string `json:"icon"` + Group string `json:"group"` + Url string `json:"url"` +} + +type Response struct { + Code int `json:"code"` + Message string `json:"message"` + Timestamp int `json:"timestamp"` +} diff --git a/message/mail.go b/message/mail.go new file mode 100644 index 0000000..921f070 --- /dev/null +++ b/message/mail.go @@ -0,0 +1,48 @@ +package message + +import ( + "fmt" + "github.com/jordan-wright/email" + log "github.com/sirupsen/logrus" + "luxshare-daily-report/global" + "net/smtp" +) + +type Mail struct { + Host string `json:"host" yaml:"host"` + Protocol string `json:"protocol" yaml:"protocol"` + Port int `json:"port" yaml:"port"` + Username string `json:"username" yaml:"username"` + Password string `json:"password" yaml:"password"` + FromName string `json:"from_name" yaml:"from_name"` + To []string `json:"to" yaml:"to"` +} + +var m Mail + +func initMail() { + m = Mail{ + Host: global.GLO_CONFIG.MailHost, + Port: global.GLO_CONFIG.MailPort, + Username: global.GLO_CONFIG.MailUser, + Password: global.GLO_CONFIG.MailPwd, + FromName: global.GLO_CONFIG.MailFromName, + To: global.GLO_CONFIG.MailTo, + } + Register("mail", m) +} + +func (m Mail) Send(message Body) { + log.Println("[mail] Sending by mail...") + e := email.NewEmail() + e.From = m.FromName + e.To = m.To + e.Subject = message.Title + e.Text = []byte(message.Content) + addr := fmt.Sprintf("%v:%v", m.Host, m.Port) + err := e.Send(addr, smtp.PlainAuth("", m.Username, m.Password, m.Host)) + if err != nil { + log.Fatalf("[mail] Send failed: %v\n", err) + } + log.Printf("[mail] Send successful") +} diff --git a/message/message.go b/message/message.go index ede1b09..81f3463 100644 --- a/message/message.go +++ b/message/message.go @@ -1 +1,33 @@ package message + +import "luxshare-daily-report/global" + +var Messages = make(map[string]Message, 0) + +type Message interface { + Send(body Body) +} + +type Body struct { + Title string + Content string +} + +// GetSupport 获取支持 +func GetSupport() Message { + initBark() + initMail() + //key := "bark" + key := global.GLO_CONFIG.MsgType + return Messages[key] +} + +// Enabled 是否启用 +func Enabled() bool { + return global.GLO_CONFIG.MsgEnabled +} + +// Register 注册 +func Register(name string, message Message) { + Messages[name] = message +} diff --git a/model/model.go b/model/model.go index 21f82b7..c4ea9fb 100644 --- a/model/model.go +++ b/model/model.go @@ -75,8 +75,18 @@ type BarkResp struct { // @Description: config model // type Config struct { - Quality string - UserName string - PassWord string - BarkUrl string + Quality string + UserName string + PassWord string + MsgEnabled bool + MsgType string + BarkUrl string + BarkKey string + MailHost string + MailProtocol string + MailPort int + MailUser string + MailPwd string + MailFromName string + MailTo []string } diff --git a/serve/serve.go b/serve/serve.go index 4e1f832..5bd0c03 100644 --- a/serve/serve.go +++ b/serve/serve.go @@ -3,6 +3,7 @@ package serve import ( "bytes" "encoding/base64" + "errors" "fmt" log "github.com/sirupsen/logrus" "image" @@ -14,7 +15,7 @@ import ( "io/ioutil" "luxshare-daily-report/global" "luxshare-daily-report/serve/core" - "luxshare-daily-report/util" + "luxshare-daily-report/support" "net/http" "os" "strconv" @@ -82,7 +83,7 @@ https://github.com/zggsong`)) global.GLO_RECV_CHAN <- m } - w.Write([]byte("Successful...")) //这个写入到w的是输出到客户端的 + w.Write([]byte("服务端接收成功...")) //这个写入到w的是输出到客户端的 } // @@ -95,9 +96,8 @@ func DeclarationService(files map[string]string) { //ticket := "Q9okHMY42Fk7kzLA3rvPTCbUShhX3zqlbaT97CDjUbxql0NH0AAqKYw+XfSjwoytijuuHXOc7vNY9GePZoIZSg==" ticket, err := core.Login(global.GLO_CONFIG.UserName, global.GLO_CONFIG.PassWord) if ticket == "" || err != nil { - errMsg := fmt.Sprintf("ticket: %v, err:%v", ticket, err.Error()) - log.Printf(errMsg) - util.Send(errMsg) + log.Printf("ticket: %v, err:%v", ticket, err.Error()) + support.SendMessageError(err) return } @@ -111,10 +111,16 @@ func DeclarationService(files map[string]string) { m["jkm"] = resJkm //imagesLinks := []string{"https://p.luxshare-ict.com/KSLANTO/EpidemicSys/20221016/html5_2a05b9ab03844033a63b2c2ac06556ab.jpg", "https://p.luxshare-ict.com/KSLANTO/EpidemicSys/20221016/html5_7e64fd5c643c49299571583163623f7b.jpg"} imagesLinks, err := core.Upload2Azure(ticket, m) - //log.Printf("[DEBUG] images links: %s", imagesLinks) + //log.Printf("[DEBUG] get images links: %s", imagesLinks) if err != nil { log.Printf(err.Error()) - util.Send(err.Error()) + support.SendMessageError(err) + return + } + if imagesLinks == nil { + err = errors.New("[ERROR] Get no images links") + log.Printf(err.Error()) + support.SendMessageError(err) return } @@ -123,7 +129,7 @@ func DeclarationService(files map[string]string) { err = core.EpidemicRegistration(ticket, imagesLinks) if err != nil && i > 1 { log.Printf("重试3次失败,%v", err.Error()) - util.Send(err.Error()) + support.SendMessageError(err) return } else { break @@ -139,7 +145,7 @@ func DeclarationService(files map[string]string) { err = core.RefreshDoor(ticket) if err != nil && i > 1 { log.Printf("重试3次失败,%v", err.Error()) - util.Send(err.Error()) + support.SendMessageError(err) return } else { break @@ -147,7 +153,7 @@ func DeclarationService(files map[string]string) { } log.Printf("[INFO] 刷新门禁成功") - util.Send("[INFO] 每日申报+刷新门禁成功") + support.SendSuccess("【成功】每日申报、刷新门禁") } // diff --git a/support/support.go b/support/support.go new file mode 100644 index 0000000..5ec6a2d --- /dev/null +++ b/support/support.go @@ -0,0 +1,35 @@ +package support + +import ( + "fmt" + "luxshare-daily-report/message" +) + +// SendMessageError 发送错误信息 +func SendMessageError(err error) { + var body = message.Body{} + body.Title = "LuxShareReportServer" + body.Content = fmt.Sprintf("Error occurred: %s", err.Error()) + send(body) +} + +// SendSuccess 发送成功信息 +func SendSuccess(content string) { + body := message.Body{ + Title: "LuxShareReportServer", + Content: content, + } + send(body) +} + +// send 发送 +func send(body message.Body) { + m := message.GetSupport() + if m == nil { + return + } + enabled := message.Enabled() + if enabled { + m.Send(body) + } +}