package alert import ( "HFish/utils/try" "HFish/core/dbUtil" "strconv" "strings" "HFish/utils/send" "bytes" "net/http" "HFish/utils/log" "encoding/json" ) func AlertMail(model string, typex string, agent string, ipx string, country string, region string, city string, infox string) { // 判断邮件通知 try.Try(func() { // 只有新加入才会发送邮件通知 if (model == "new") { result, err := dbUtil.DB().Table("hfish_setting").Fields("status", "info").Where("type", "=", "alertMail").First() if err != nil { log.Pr("HFish", "127.0.0.1", "获取邮件告警配置失败", err) } status := strconv.FormatInt(result["status"].(int64), 10) // 判断是否启用通知 if status == "1" { info := result["info"] config := strings.Split(info.(string), "&&") if (country == "本地地址") { region = "" city = "" } else if (country == "局域网") { region = "" city = "" } text := `
Hi,上钩了!

集群名称:` + agent + `
攻击IP:` + ipx + `
地理信息:` + country + ` ` + region + ` ` + city + `
上钩内容:` + infox + `

(HFish 自动发送)
` send.SendMail(config[4:], "[HFish]提醒你,"+typex+"有鱼上钩!", text, config) } } }).Catch(func() { }) } func AlertWebHook(id string, model string, typex string, projectName string, agent string, ipx string, country string, region string, city string, infox string, time string) { // 判断 WebHook 通知 try.Try(func() { result, err := dbUtil.DB().Table("hfish_setting").Fields("status", "info").Where("type", "=", "webHook").First() if err != nil { log.Pr("HFish", "127.0.0.1", "获取WebHook配置失败", err) } status := strconv.FormatInt(result["status"].(int64), 10) // 判断是否启用通知 if status == "1" { info := result["info"] song := make(map[string]interface{}) song["id"] = id song["model"] = model song["project"] = projectName song["type"] = typex song["agent"] = agent song["ip"] = ipx song["country"] = country song["region"] = region song["city"] = city song["info"] = infox song["time"] = time bytesData, _ := json.Marshal(song) reader := bytes.NewReader(bytesData) request, _ := http.NewRequest("POST", info.(string), reader) request.Header.Set("Content-Type", "application/json;charset=UTF-8") client := http.Client{} resp, err := client.Do(request) if err != nil { log.Pr("HFish", "127.0.0.1", "WebHook 调用失败", err) } else { log.Pr("HFish", "127.0.0.1", "WebHook 调用成功") } defer resp.Body.Close() //defer request.Body.Close() } }).Catch(func() { }) }