This repository has been archived by the owner on Jul 8, 2021. It is now read-only.
/
zgrab.go
128 lines (116 loc) · 2.56 KB
/
zgrab.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* ZGrab Copyright 2015 Regents of the University of Michigan
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*/
package zlib
import (
"encoding/json"
"errors"
"net"
"time"
)
type Grab struct {
Host net.IP `json:"host"`
Domain string `json:"domain"`
Time time.Time `json:"timestamp"`
Log []ConnectionEvent `json:"log"`
}
type ConnectionEvent struct {
Data EventData
Error error
}
type encodedGrab struct {
Host string `json:"host"`
Domain *string `json:"domain"`
Time string `json:"time"`
Log []ConnectionEvent `json:"log"`
}
type encodedConnectionEvent struct {
Type EventType `json:"type"`
Data EventData `json:"data"`
Error *string `json:"error"`
}
type partialConnectionEvent struct {
Data EventData `json:"data"`
Error *string `json:"error"`
}
func (ce *ConnectionEvent) MarshalJSON() ([]byte, error) {
t := ce.Data.GetType()
var esp *string
if ce.Error != nil {
es := ce.Error.Error()
esp = &es
}
obj := encodedConnectionEvent{
Type: t,
Data: ce.Data,
Error: esp,
}
return json.Marshal(obj)
}
func (ce *ConnectionEvent) UnmarshalJSON(b []byte) error {
ece := new(partialConnectionEvent)
tn := struct {
TypeName string `json:"type"`
}{}
if err := json.Unmarshal(b, &tn); err != nil {
return err
}
t, typeErr := EventTypeFromName(tn.TypeName)
if typeErr != nil {
return typeErr
}
ece.Data = t.GetEmptyInstance()
if err := json.Unmarshal(b, &ece); err != nil {
return err
}
ce.Data = ece.Data
if ece.Error != nil {
ce.Error = errors.New(*ece.Error)
}
return nil
}
func (g *Grab) MarshalJSON() ([]byte, error) {
var domainPtr *string
if g.Domain != "" {
domainPtr = &g.Domain
}
time := g.Time.Format(time.RFC3339)
obj := encodedGrab{
Host: g.Host.String(),
Domain: domainPtr,
Time: time,
Log: g.Log,
}
return json.Marshal(obj)
}
func (g *Grab) UnmarshalJSON(b []byte) error {
eg := new(encodedGrab)
err := json.Unmarshal(b, eg)
if err != nil {
return err
}
g.Host = net.ParseIP(eg.Host)
if eg.Domain != nil {
g.Domain = *eg.Domain
}
if g.Time, err = time.Parse(time.RFC3339, eg.Time); err != nil {
return err
}
g.Log = eg.Log
return nil
}
func (g *Grab) status() status {
if len(g.Log) == 0 {
return status_failure
}
for _, entry := range g.Log {
if entry.Error != nil {
return status_failure
}
}
return status_success
}