mirror of
https://github.com/zu1k/nali.git
synced 2025-01-23 21:59:02 +08:00
Merge pull request #23 from hyliang96/issue22
fix issue #22: replace ip4/ip6/domain with regexp with lookahead and lookback
This commit is contained in:
commit
35d2fc4db7
@ -52,7 +52,6 @@ func find(cname string) string {
|
|||||||
|
|
||||||
func ReplaceCDNInString(str string) (result string) {
|
func ReplaceCDNInString(str string) (result string) {
|
||||||
done := make(map[string]bool)
|
done := make(map[string]bool)
|
||||||
|
|
||||||
cnames := domainRe.FindAllString(str, -1)
|
cnames := domainRe.FindAllString(str, -1)
|
||||||
result = str
|
result = str
|
||||||
for _, cname := range cnames {
|
for _, cname := range cnames {
|
||||||
@ -61,7 +60,7 @@ func ReplaceCDNInString(str string) (result string) {
|
|||||||
if _, found := done[cname]; found {
|
if _, found := done[cname]; found {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
result = tools.ReplaceAdd(result, cname, fmt.Sprintf("%s [%s]", cname, name))
|
result = tools.AddInfoDomain(result, cname, name)
|
||||||
done[cname] = true
|
done[cname] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,6 +56,23 @@ func ParseIPs(ips []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RemoveRepeatedElement(arr []string) (newArr []string) {
|
||||||
|
newArr = make([]string, 0)
|
||||||
|
for i := 0; i < len(arr); i++ {
|
||||||
|
repeat := false
|
||||||
|
for j := i + 1; j < len(arr); j++ {
|
||||||
|
if arr[i] == arr[j] {
|
||||||
|
repeat = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !repeat {
|
||||||
|
newArr = append(newArr, arr[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func ReplaceIPInString(str string) (result string) {
|
func ReplaceIPInString(str string) (result string) {
|
||||||
db0 := db[0]
|
db0 := db[0]
|
||||||
var db1 ipdb.IPDB
|
var db1 ipdb.IPDB
|
||||||
@ -67,15 +84,17 @@ func ReplaceIPInString(str string) (result string) {
|
|||||||
|
|
||||||
result = str
|
result = str
|
||||||
ip4s := tools.GetIP4FromString(str)
|
ip4s := tools.GetIP4FromString(str)
|
||||||
|
ip4s = RemoveRepeatedElement(ip4s)
|
||||||
for _, ip := range ip4s {
|
for _, ip := range ip4s {
|
||||||
info := db0.Find(ip)
|
info := db0.Find(ip)
|
||||||
result = tools.ReplaceAdd(result, ip, formatResult(ip, info))
|
result = tools.AddInfoIp4(result, ip, info)
|
||||||
}
|
}
|
||||||
|
|
||||||
ip6s := tools.GetIP6FromString(str)
|
ip6s := tools.GetIP6FromString(str)
|
||||||
|
ip6s = RemoveRepeatedElement(ip6s)
|
||||||
for _, ip := range ip6s {
|
for _, ip := range ip6s {
|
||||||
info := db1.Find(ip)
|
info := db1.Find(ip)
|
||||||
result = tools.ReplaceAdd(result, ip, formatResult(ip, info))
|
result = tools.AddInfoIp6(result, ip, info)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,23 @@ package tools
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ReplaceAdd(origin string, old string, new string) (result string) {
|
func AddInfoIp4(origin string, ip string, info string) (result string) {
|
||||||
subLen := len(new) - len(old)
|
re := regexp.MustCompile("(^|[^0-9.])(" + strings.ReplaceAll(ip, ".", "\\.") + ")($|[^0-9.])")
|
||||||
wanted := old + strings.Repeat(" ", subLen)
|
result = re.ReplaceAllString(origin, "$1$2"+" ["+info+"]$3")
|
||||||
if strings.Contains(origin, wanted) {
|
return strings.TrimRight(result, " \t")
|
||||||
result = strings.ReplaceAll(origin, wanted, new)
|
}
|
||||||
}
|
|
||||||
result = strings.ReplaceAll(origin, old, new)
|
func AddInfoIp6(origin string, ip string, info string) (result string) {
|
||||||
|
re := regexp.MustCompile("(^|[^0-9a-fA-F:])(" + strings.ReplaceAll(ip, ".", "\\.") + ")($|[^0-9a-fA-F:])")
|
||||||
|
result = re.ReplaceAllString(origin, "$1$2"+" ["+info+"]$3")
|
||||||
|
return strings.TrimRight(result, " \t")
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddInfoDomain(origin string, domain string, info string) (result string) {
|
||||||
|
re := regexp.MustCompile("(^|[^0-9a-zA-Z-])(" + strings.ReplaceAll(domain, ".", "\\.") + ")($|[^0-9a-zA-Z-\\.])")
|
||||||
|
result = re.ReplaceAllString(origin, "$1$2"+" ["+info+"]$3")
|
||||||
return strings.TrimRight(result, " \t")
|
return strings.TrimRight(result, " \t")
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user