-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
94 lines (73 loc) · 2.49 KB
/
model.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
package ncrack
import (
"encoding/xml"
"strconv"
"time"
)
type Run struct {
XMLName xml.Name `xml:"ncrackrun" json:"-"`
Args string `xml:"args,attr" json:"args"`
Start Timestamp `xml:"start,attr" json:"start"`
StartStr string `xml:"startstr,attr" json:"startstr"`
Version string `xml:"version,attr" json:"version"`
XMLOutputVersion string `xml:"xmloutputversion,attr" json:"xmloutputversion"`
Services []Service `xml:"service" json:"service"`
rawXML []byte `xml:"-" json:"-"`
}
func (r Run) Bytes() []byte {
return r.rawXML
}
type Service struct {
StartTime Timestamp `xml:"starttime,attr" json:"starttime"`
EndTime Timestamp `xml:"endtime,attr" json:"endtime"`
Address Address `xml:"address" json:"address"`
Port Port `xml:"port" json:"port"`
Credentials Credentials `xml:"credentials" json:"credentials"`
}
type Address struct {
Addr string `xml:"addr,attr" json:"addr"`
Type string `xml:"addrtype,attr" json:"addrtype"`
}
type Port struct {
Protocol string `xml:"protocol,attr" json:"protocol"`
Port uint16 `xml:"portid,attr" json:"portid"`
Name string `xml:"name,attr" json:"name"`
}
type Credentials struct {
Username string `xml:"username,attr" json:"username"`
Password string `xml:"password,attr" json:"password"`
}
// Timestamp represents time as a UNIX timestamp in seconds.
type Timestamp time.Time
// ParseTime converts a UNIX timestamp string to a time.Time.
func (t *Timestamp) ParseTime(s string) error {
timestamp, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
*t = Timestamp(time.Unix(timestamp, 0))
return nil
}
// FormatTime formats the time.Time value as a UNIX timestamp string.
func (t Timestamp) FormatTime() string {
return strconv.FormatInt(time.Time(t).Unix(), 10)
}
// MarshalJSON implements the json.Marshaler interface.
func (t Timestamp) MarshalJSON() ([]byte, error) {
return []byte(t.FormatTime()), nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (t *Timestamp) UnmarshalJSON(b []byte) error {
return t.ParseTime(string(b))
}
// MarshalXMLAttr implements the xml.MarshalerAttr interface.
func (t Timestamp) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
if time.Time(t).IsZero() {
return xml.Attr{}, nil
}
return xml.Attr{Name: name, Value: t.FormatTime()}, nil
}
// UnmarshalXMLAttr implements the xml.UnmarshalXMLAttr interface.
func (t *Timestamp) UnmarshalXMLAttr(attr xml.Attr) (err error) {
return t.ParseTime(attr.Value)
}