1
0
mirror of https://github.com/zu1k/nali.git synced 2025-02-02 18:32:43 +08:00
nali/pkg/entity/entity.go

99 lines
1.8 KiB
Go
Raw Normal View History

2021-07-30 21:46:06 +08:00
package entity
import (
"encoding/json"
"log"
"strings"
2023-08-24 10:32:56 +08:00
"github.com/fatih/color"
"github.com/zu1k/nali/pkg/dbif"
2021-07-30 21:46:06 +08:00
)
type EntityType uint
const (
2021-08-02 12:01:25 +08:00
TypeIPv4 = dbif.TypeIPv4
TypeIPv6 = dbif.TypeIPv6
TypeDomain = dbif.TypeDomain
TypePlain = 100
2021-07-30 21:46:06 +08:00
)
type Entity struct {
Loc [2]int `json:"-"` // s[Loc[0]:Loc[1]]
Type EntityType `json:"type"`
2021-07-30 21:46:06 +08:00
Text string `json:"ip"`
InfoText string `json:"text"`
2023-05-20 18:12:31 +08:00
Source string `json:"source"`
Info interface{} `json:"info"`
2021-07-30 21:46:06 +08:00
}
func (e Entity) ParseInfo() error {
return nil
}
func (e Entity) Json() string {
jsonResult, err := json.Marshal(e)
if err != nil {
log.Fatal(err.Error())
}
return string(jsonResult)
}
2021-07-30 21:46:06 +08:00
type Entities []*Entity
func (es Entities) Len() int {
return len(es)
}
func (es Entities) Less(i, j int) bool {
2021-07-30 22:30:27 +08:00
return es[i].Loc[0] < es[j].Loc[0]
2021-07-30 21:46:06 +08:00
}
func (es Entities) Swap(i, j int) {
2021-07-30 22:30:27 +08:00
es[i], es[j] = es[j], es[i]
2021-07-30 21:46:06 +08:00
}
func (es Entities) String() string {
var result strings.Builder
for _, entity := range es {
result.WriteString(entity.Text)
if entity.Type != TypePlain && len(entity.InfoText) > 0 {
result.WriteString("[" + entity.InfoText + "] ")
2021-07-30 21:46:06 +08:00
}
}
return result.String()
2021-07-30 22:30:27 +08:00
}
2021-08-03 07:54:35 +08:00
func (es Entities) ColorString() string {
var line strings.Builder
for _, e := range es {
s := e.Text
2021-08-03 07:54:35 +08:00
switch e.Type {
case TypeIPv4:
s = color.GreenString(e.Text)
2021-08-03 07:54:35 +08:00
case TypeIPv6:
s = color.BlueString(e.Text)
2021-08-03 07:54:35 +08:00
case TypeDomain:
s = color.YellowString(e.Text)
2021-08-03 07:54:35 +08:00
}
if e.Type != TypePlain && len(e.InfoText) > 0 {
s += " [" + color.RedString(e.InfoText) + "] "
2021-08-03 07:54:35 +08:00
}
line.WriteString(s)
2021-08-03 07:54:35 +08:00
}
return line.String()
2021-08-03 07:54:35 +08:00
}
func (es Entities) Json() string {
var s strings.Builder
for _, e := range es {
if e.Type == TypePlain {
continue
}
s.WriteString(e.Json() + "\n")
}
return s.String()
}