通知内 IP 脱敏

This commit is contained in:
naiba
2021-05-27 20:48:12 +08:00
parent 06914eed99
commit 2b05dcfc23
9 changed files with 76 additions and 8 deletions

View File

@@ -5,6 +5,7 @@ import (
"encoding/hex"
"math/rand"
"os"
"regexp"
"time"
"unsafe"
)
@@ -45,3 +46,21 @@ func MD5(plantext string) string {
func IsWindows() bool {
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
View 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)
}
}