-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrock.go
138 lines (117 loc) · 3.12 KB
/
rock.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
129
130
131
132
133
134
135
136
137
138
package dmc
import (
"net"
"net/http"
"net/http/cookiejar"
"regexp"
"strings"
"time"
"code.google.com/p/mlab-ns2/gae/ns/digest"
"github.com/PuerkitoBio/goquery"
)
type Rock struct {
}
func (r *Rock) Name() string {
return "Rock"
}
func (r *Rock) MatchString(s string) bool {
return regexp.MustCompile("^Kinemetrics").MatchString(s)
}
func (r *Rock) Groups() []ModelType {
return []ModelType{StrongModel}
}
func (r *Rock) Group(g ModelType) bool {
switch g {
case StrongModel:
return true
default:
return false
}
}
func (r *Rock) Identify(orig string, ip net.IP, timeout time.Duration, retries int) (*State, error) {
transport := digest.NewTransport("rock", "kmi")
cookieJar, _ := cookiejar.New(nil)
client := &http.Client{
Transport: transport,
Jar: cookieJar,
}
s := State{Values: make(map[string]interface{})}
resp, err := client.Get("http://" + ip.String() + "/")
if resp == nil || err != nil {
return nil, err
}
defer client.Get("http://" + ip.String() + "/logoff")
resp, err = client.Get("http://" + ip.String() + "/menuload")
if resp == nil || err != nil {
// there's a bug in golang http which means it may work the next time ...
resp, err = client.Get("http://" + ip.String() + "/menuload")
if resp == nil || err != nil {
return nil, err
}
}
defer client.Get("http://" + ip.String() + "/logoff")
doc, err := goquery.NewDocumentFromResponse(resp)
if doc == nil || err != nil {
return nil, err
}
raw, err := doc.Html()
if raw == "" || err != nil {
return nil, err
}
switch {
case strings.Contains(raw, "rockhound.jpg"):
s.Values["model"] = "Kinemetrics Slate"
case strings.Contains(raw, "basalt.jpg"):
s.Values["model"] = "Kinemetrics Basalt"
default:
s.Values["model"] = "Kinemetrics Rock"
}
doc.Find("h4").Each(func(i int, g *goquery.Selection) {
h, _ := g.Html()
y := strings.Fields(regexp.MustCompile("<[a-z0-9#=\"\\s\\_\\-\\+/]+>").ReplaceAllString(h, " "))
if !(len(y) > 1) {
return
}
switch y[0] {
case "Station":
s.Values["code"] = y[1]
}
})
resp, err = client.Get("http://" + ip.String() + "/homeload")
if resp == nil || err != nil {
// there's a bug in golang http which means it may work the next time ...
resp, err = client.Get("http://" + ip.String() + "/homeload")
if resp == nil || err != nil {
return nil, err
}
}
defer client.Get("http://" + ip.String() + "/logoff")
doc, err = goquery.NewDocumentFromResponse(resp)
if doc == nil || err != nil {
return nil, err
}
doc.Find("p").Each(func(i int, g *goquery.Selection) {
h, err := g.Html()
if h == "" || err != nil {
return
}
x := strings.Split(h, "<br/>")
for _, y := range x {
b := strings.Fields(y)
if len(b) > 1 {
switch {
case strings.Contains(y, "filesystem"):
s.Values["filesystem"] = b[len(b)-1]
case strings.Contains(y, "Update"):
s.Values["hardware"] = strings.Join(b[0:2], " ")
s.Values["update"] = b[len(b)-1]
case strings.Contains(y, "Serial number"):
s.Values["serial"] = b[len(b)-1]
case strings.Contains(y, "Software version"):
s.Values["software"] = b[len(b)-1]
}
}
}
})
return &s, nil
}