通知内 IP 脱敏
This commit is contained in:
parent
06914eed99
commit
2b05dcfc23
@ -37,7 +37,7 @@ func UpdateIP() {
|
|||||||
for {
|
for {
|
||||||
ipv4 := fetchGeoIP(ipv4Servers, false)
|
ipv4 := fetchGeoIP(ipv4Servers, false)
|
||||||
ipv6 := fetchGeoIP(ipv6Servers, true)
|
ipv6 := fetchGeoIP(ipv6Servers, true)
|
||||||
cachedIP = fmt.Sprintf("ip(v4:%s,v6:%s)", ipv4.IP, ipv6.IP)
|
cachedIP = fmt.Sprintf("ip(v4:%s,v6:[%s])", ipv4.IP, ipv6.IP)
|
||||||
if ipv4.CountryCode != "" {
|
if ipv4.CountryCode != "" {
|
||||||
cachedCountry = ipv4.CountryCode
|
cachedCountry = ipv4.CountryCode
|
||||||
} else if ipv6.CountryCode != "" {
|
} else if ipv6.CountryCode != "" {
|
||||||
|
@ -292,10 +292,13 @@ func (ma *memberAPI) addOrEditCron(c *gin.Context) {
|
|||||||
Type: model.TaskTypeCommand,
|
Type: model.TaskTypeCommand,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
dao.SendNotification(fmt.Sprintf("计划任务:%s,服务器:%d 离线,无法执行。", cr.Name, cr.Servers[j]), false)
|
dao.SendNotification(fmt.Sprintf("计划任务:%s,服务器:%s 离线,无法执行。", cr.Name, dao.ServerList[cr.Servers[j]].Name), false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
delete(dao.Crons, cr.ID)
|
delete(dao.Crons, cr.ID)
|
||||||
dao.Crons[cr.ID] = &cr
|
dao.Crons[cr.ID] = &cr
|
||||||
|
@ -95,7 +95,7 @@ func loadCrons() {
|
|||||||
Type: model.TaskTypeCommand,
|
Type: model.TaskTypeCommand,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
dao.SendNotification(fmt.Sprintf("计划任务:%s,服务器:%d 离线,无法执行。", cr.Name, cr.Servers[j]), false)
|
dao.SendNotification(fmt.Sprintf("计划任务:%s,服务器:%s 离线,无法执行。", cr.Name, dao.ServerList[cr.Servers[j]].Name), false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
@ -45,3 +46,21 @@ func MD5(plantext string) string {
|
|||||||
func IsWindows() bool {
|
func IsWindows() bool {
|
||||||
return os.PathSeparator == '\\' && os.PathListSeparator == ';'
|
return os.PathSeparator == '\\' && os.PathListSeparator == ';'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ipv4Re = regexp.MustCompile(`(\d*\.).*(\.\d*)`)
|
||||||
|
|
||||||
|
func ipv4Desensitize(ipv4Addr string) string {
|
||||||
|
return ipv4Re.ReplaceAllString(ipv4Addr, "$1****$2")
|
||||||
|
}
|
||||||
|
|
||||||
|
var ipv6Re = regexp.MustCompile(`(\w*:\w*:).*(:\w*:\w*)`)
|
||||||
|
|
||||||
|
func ipv6Desensitize(ipv6Addr string) string {
|
||||||
|
return ipv6Re.ReplaceAllString(ipv6Addr, "$1****$2")
|
||||||
|
}
|
||||||
|
|
||||||
|
func IPDesensitize(ipAddr string) string {
|
||||||
|
ipAddr = ipv4Desensitize(ipAddr)
|
||||||
|
ipAddr = ipv6Desensitize(ipAddr)
|
||||||
|
return ipAddr
|
||||||
|
}
|
||||||
|
41
pkg/utils/utils_test.go
Normal file
41
pkg/utils/utils_test.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
type testSt struct {
|
||||||
|
input string
|
||||||
|
output string
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNotification(t *testing.T) {
|
||||||
|
cases := []testSt{
|
||||||
|
{
|
||||||
|
input: "ip(v4:103.80.236.249,v6:[d5ce:d811:cdb8:067a:a873:2076:9521:9d2d])",
|
||||||
|
output: "ip(v4:103.****.249,v6:[d5ce:d811:****:9521:9d2d])",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "ip(v4:3.80.236.29,v6:[d5ce::cdb8:067a:a873:2076:9521:9d2d])",
|
||||||
|
output: "ip(v4:3.****.29,v6:[d5ce::****:9521:9d2d])",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "ip(v4:3.80.236.29,v6:[d5ce::cdb8:067a:a873:2076::9d2d])",
|
||||||
|
output: "ip(v4:3.****.29,v6:[d5ce::****::9d2d])",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "ip(v4:3.80.236.9,v6:[d5ce::cdb8:067a:a873:2076::9d2d])",
|
||||||
|
output: "ip(v4:3.****.9,v6:[d5ce::****::9d2d])",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "ip(v4:3.80.236.9,v6:[d5ce::cdb8:067a:a873:2076::9d2d])",
|
||||||
|
output: "ip(v4:3.****.9,v6:[d5ce::****::9d2d])",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
assert.Equal(t, IPDesensitize(c.input), c.output)
|
||||||
|
}
|
||||||
|
}
|
@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/naiba/nezha/model"
|
"github.com/naiba/nezha/model"
|
||||||
|
"github.com/naiba/nezha/pkg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 报警规则
|
// 报警规则
|
||||||
@ -100,7 +101,7 @@ func checkStatus() {
|
|||||||
// 发送通知
|
// 发送通知
|
||||||
max, desc := alert.Check(alertsStore[alert.ID][server.ID])
|
max, desc := alert.Check(alertsStore[alert.ID][server.ID])
|
||||||
if desc != "" {
|
if desc != "" {
|
||||||
message := fmt.Sprintf("报警规则:%s,服务器:%s(%s),%s,逮到咯,快去看看!", alert.Name, server.Name, server.Host.IP, desc)
|
message := fmt.Sprintf("报警规则:%s,服务器:%s(%s),逮到咯,快去看看!", alert.Name, server.Name, utils.IPDesensitize(server.Host.IP))
|
||||||
go SendNotification(message, true)
|
go SendNotification(message, true)
|
||||||
}
|
}
|
||||||
// 清理旧数据
|
// 清理旧数据
|
||||||
|
@ -69,7 +69,7 @@ func CronTrigger(c *model.Cron) {
|
|||||||
Type: model.TaskTypeCommand,
|
Type: model.TaskTypeCommand,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
SendNotification(fmt.Sprintf("计划任务:%s,服务器:%d 离线,无法执行。", c.Name, c.Servers[j]), false)
|
SendNotification(fmt.Sprintf("计划任务:%s,服务器:%s 离线,无法执行。", c.Name, ServerList[c.Servers[j]].Name), false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -275,7 +275,9 @@ func (ss *ServiceSentinel) worker() {
|
|||||||
}
|
}
|
||||||
stateStr := getStateStr(upPercent)
|
stateStr := getStateStr(upPercent)
|
||||||
if Conf.Debug {
|
if Conf.Debug {
|
||||||
log.Println("服务监控上报:", ss.monitors[mh.MonitorID].Target, stateStr, "上报者:", r.Reporter, "是否正常:", mh.Successful, "请求输出:", mh.Data)
|
ServerLock.RLock()
|
||||||
|
log.Println("服务监控上报:", ss.monitors[mh.MonitorID].Target, stateStr, "上报者:", ServerList[r.Reporter].Name, "是否正常:", mh.Successful, "请求输出:", mh.Data)
|
||||||
|
ServerLock.RUnlock()
|
||||||
}
|
}
|
||||||
if stateStr == "故障" || stateStr != ss.lastStatus[mh.MonitorID] {
|
if stateStr == "故障" || stateStr != ss.lastStatus[mh.MonitorID] {
|
||||||
ss.monitorsLock.RLock()
|
ss.monitorsLock.RLock()
|
||||||
|
@ -31,11 +31,13 @@ func (s *NezhaHandler) ReportTask(c context.Context, r *pb.TaskResult) (*pb.Rece
|
|||||||
defer dao.CronLock.RUnlock()
|
defer dao.CronLock.RUnlock()
|
||||||
cr := dao.Crons[r.GetId()]
|
cr := dao.Crons[r.GetId()]
|
||||||
if cr != nil {
|
if cr != nil {
|
||||||
|
dao.ServerLock.RLock()
|
||||||
|
defer dao.ServerLock.RUnlock()
|
||||||
if cr.PushSuccessful && r.GetSuccessful() {
|
if cr.PushSuccessful && r.GetSuccessful() {
|
||||||
dao.SendNotification(fmt.Sprintf("成功计划任务:%s ,服务器:%d,日志:\n%s", cr.Name, clientID, r.GetData()), false)
|
dao.SendNotification(fmt.Sprintf("成功计划任务:%s ,服务器:%s,日志:\n%s", cr.Name, dao.ServerList[clientID].Name, r.GetData()), false)
|
||||||
}
|
}
|
||||||
if !r.GetSuccessful() {
|
if !r.GetSuccessful() {
|
||||||
dao.SendNotification(fmt.Sprintf("失败计划任务:%s ,服务器:%d,日志:\n%s", cr.Name, clientID, r.GetData()), false)
|
dao.SendNotification(fmt.Sprintf("失败计划任务:%s ,服务器:%s,日志:\n%s", cr.Name, dao.ServerList[clientID].Name, r.GetData()), false)
|
||||||
}
|
}
|
||||||
dao.DB.Model(cr).Updates(model.Cron{
|
dao.DB.Model(cr).Updates(model.Cron{
|
||||||
LastExecutedAt: time.Now().Add(time.Second * -1 * time.Duration(r.GetDelay())),
|
LastExecutedAt: time.Now().Add(time.Second * -1 * time.Duration(r.GetDelay())),
|
||||||
|
Loading…
Reference in New Issue
Block a user