ParseLine parse a line into entities
(line string)
| 11 | |
| 12 | // ParseLine parse a line into entities |
| 13 | func ParseLine(line string) Entities { |
| 14 | ip4sLoc := re.IPv4Re.FindAllStringIndex(line, -1) |
| 15 | ip6sLoc := re.IPv6Re.FindAllStringIndex(line, -1) |
| 16 | domainsLoc := re.DomainRe.FindAllStringIndex(line, -1) |
| 17 | |
| 18 | tmp := make(Entities, 0, len(ip4sLoc)+len(ip6sLoc)+len(domainsLoc)) |
| 19 | for _, e := range ip4sLoc { |
| 20 | tmp = append(tmp, &Entity{ |
| 21 | Loc: *(*[2]int)(e), |
| 22 | Type: TypeIPv4, |
| 23 | Text: line[e[0]:e[1]], |
| 24 | }) |
| 25 | } |
| 26 | for _, e := range ip6sLoc { |
| 27 | text := line[e[0]:e[1]] |
| 28 | if ip, _ := netip.ParseAddr(text); !ip.Is4In6() { |
| 29 | tmp = append(tmp, &Entity{ |
| 30 | Loc: *(*[2]int)(e), |
| 31 | Type: TypeIPv6, |
| 32 | Text: text, |
| 33 | }) |
| 34 | } |
| 35 | } |
| 36 | for _, e := range domainsLoc { |
| 37 | tmp = append(tmp, &Entity{ |
| 38 | Loc: *(*[2]int)(e), |
| 39 | Type: TypeDomain, |
| 40 | Text: line[e[0]:e[1]], |
| 41 | }) |
| 42 | } |
| 43 | |
| 44 | sort.Sort(tmp) |
| 45 | var es Entities |
| 46 | |
| 47 | idx := 0 |
| 48 | for _, e := range tmp { |
| 49 | start := e.Loc[0] |
| 50 | if start >= idx { |
| 51 | if start > idx { |
| 52 | es = append(es, &Entity{ |
| 53 | Loc: [2]int{idx, start}, |
| 54 | Type: TypePlain, |
| 55 | Text: line[idx:start], |
| 56 | }) |
| 57 | } |
| 58 | res := db.Find(dbif.QueryType(e.Type), e.Text) |
| 59 | if res != nil { |
| 60 | e.InfoText = res.String() |
| 61 | e.Info = res.Result |
| 62 | e.Source = res.Source |
| 63 | } else { |
| 64 | e.Type = TypePlain |
| 65 | } |
| 66 | idx = e.Loc[1] |
| 67 | es = append(es, e) |
| 68 | } |
| 69 | } |
| 70 | if total := len(line); idx < total { |