1
0
mirror of https://github.com/zu1k/nali.git synced 2025-01-23 05:39:03 +08:00
nali/internal/entity/parse.go
2021-08-02 12:01:25 +08:00

64 lines
1.2 KiB
Go

package entity
import (
"sort"
"github.com/zu1k/nali/pkg/dbif"
"github.com/zu1k/nali/internal/db"
"github.com/zu1k/nali/internal/re"
)
// ParseLine parse a line into entities
func ParseLine(line string) Entities {
ip4sLoc := re.IPv4Re.FindAllStringIndex(line, -1)
ip6sLoc := re.IPv6Re.FindAllStringIndex(line, -1)
domainsLoc := re.DomainRe.FindAllStringIndex(line, -1)
tmp := make(Entities, 0, len(ip4sLoc)+len(ip6sLoc)+len(domainsLoc))
for _, e := range ip4sLoc {
tmp = append(tmp, &Entity{
Loc: e,
Type: TypeIPv4,
Text: line[e[0]:e[1]],
})
}
for _, e := range ip6sLoc {
tmp = append(tmp, &Entity{
Loc: e,
Type: TypeIPv6,
Text: line[e[0]:e[1]],
})
}
for _, e := range domainsLoc {
tmp = append(tmp, &Entity{
Loc: e,
Type: TypeDomain,
Text: line[e[0]:e[1]],
})
}
sort.Sort(tmp)
es := make(Entities, 0, len(tmp))
idx := 0
for _, e := range tmp {
start := e.Loc[0]
if start >= idx {
if start > idx {
es = append(es, &Entity{
Loc: []int{idx, start},
Type: TypePlain,
Text: line[idx:start],
})
}
e.Info = db.Find(dbif.QueryType(e.Type), e.Text)
es = append(es, e)
idx = e.Loc[1]
}
}
return es
}