diff --git a/annotate.go b/annotate.go index 0b80411..f7950e3 100644 --- a/annotate.go +++ b/annotate.go @@ -19,6 +19,7 @@ import ( "encoding/csv" "fmt" "os/signal" + "reflect" "slices" "syscall" "time" @@ -246,6 +247,19 @@ func AnnotateWrite(path string, out <-chan string, wg *sync.WaitGroup) { log.Debug("write thread finished") } +func isPtrNil(i any) bool { + if i == nil { + return true + } + val := reflect.ValueOf(i) + switch val.Kind() { + case reflect.Pointer, reflect.Interface, reflect.Slice, reflect.Map: + return val.IsNil() + default: + return false + } +} + func AnnotateWorker(conf *GlobalConf, a Annotator, inChan <-chan inProcessIP, outChan chan<- inProcessIP, fieldName string, wg *sync.WaitGroup, i int) { name := a.GetFieldName() @@ -257,9 +271,18 @@ func AnnotateWorker(conf *GlobalConf, a Annotator, inChan <-chan inProcessIP, for inProcess := range inChan { if fieldName != "" && slices.Contains([]string{"json", "csv"}, conf.InputFileType) { p := inProcess.Out[fieldName].(map[string]interface{}) - p[name] = a.Annotate(inProcess.Ip) + res := a.Annotate(inProcess.Ip) + if isPtrNil(res) { + res = struct{}{} // Don't return null, breaks downstream JSON parsing + } + p[name] = res + } else { - inProcess.Out[name] = a.Annotate(inProcess.Ip) + res := a.Annotate(inProcess.Ip) + if isPtrNil(res) { + res = struct{}{} // Don't return null, breaks downstream JSON parsing + } + inProcess.Out[name] = res } outChan <- inProcess } diff --git a/censys.go b/censys.go index bb98061..1de7c76 100644 --- a/censys.go +++ b/censys.go @@ -83,7 +83,6 @@ var censysAPIHostLookupURL = "https://api.platform.censys.io/v3/global/asset/hos // Annotate performs a Censys host lookup for the given IP address and returns the results. // If an error occurs or a lookup fails, it returns nil func (a *CensysAnnotator) Annotate(ip net.IP) interface{} { - req, err := http.NewRequest("GET", censysAPIHostLookupURL+ip.String(), nil) if err != nil { // If we can't even form a request, we'll fail to enrich anything. Erroring out.