-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvipr.go
144 lines (122 loc) · 3.16 KB
/
vipr.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
139
140
141
142
143
144
package dmc
import (
"net"
"net/http"
"regexp"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
type ViPR struct {
Username string
Password string
}
func (v *ViPR) Name() string {
return "ViPR Radio"
}
func (v *ViPR) Groups() []ModelType {
return []ModelType{RadioModel}
}
func (v *ViPR) Group(g ModelType) bool {
switch g {
case RadioModel:
return true
default:
return false
}
}
func (v *ViPR) MatchString(s string) bool {
return regexp.MustCompile("^ViPR").MatchString(s)
}
func (v *ViPR) Identify(orig string, ip net.IP, timeout time.Duration, retries int) (*State, error) {
s := State{Values: make(map[string]interface{})}
// default model
s.Values["model"] = "ViPR Radio"
cli := &http.Client{}
pages := []string{"UStatus.html", "ViPRDiag.html", "IPSetting.html"}
for _, p := range pages {
r, err := v.discover(cli, "http://"+ip.String()+"/"+p)
if err != nil {
return nil, err
}
for key, value := range r {
if t := value; key == "name" {
s.Values[key] = t
}
if t := value; key == "version" {
s.Values[key] = strings.Replace(t, "Dataradio ViPR ", "", -1)
}
if t := value; key == "firmware" {
s.Values[key] = t
}
if t := value; key == "macaddr" {
s.Values[key] = t
}
}
}
if _, ok := s.Values["name"]; !ok {
return nil, nil
}
if _, ok := s.Values["version"]; !ok {
return nil, nil
}
if _, ok := s.Values["firmware"]; !ok {
return nil, nil
}
if _, ok := s.Values["macaddr"]; !ok {
return nil, nil
}
return &s, nil
}
func (v *ViPR) discover(cli *http.Client, url string) (map[string]string, error) {
user := env(v.Username, "VIPR_USERNAME", "Admin")
password := env(v.Password, "VIPR_PASSWORD", "ADMINISTRATOR")
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
request.SetBasicAuth(user, password)
resp, err := cli.Do(request)
if resp == nil || err != nil {
return nil, err
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromResponse(resp)
if doc == nil || err != nil {
return nil, err
}
results := make(map[string]string)
// mac address is hidden in javascript and non changeable
doc.Find("td").Each(func(i int, s *goquery.Selection) {
p := strings.Fields(s.Text())
if len(p) > 0 && s.Next() != nil {
r := strings.Fields(strings.Replace(s.Next().Text(), "\"", "", -1))
if strings.HasPrefix(s.Text(), "Dataradio") {
results["version"] = strings.Join(p, " ")
}
if strings.HasPrefix(s.Text(), "MAC") {
if _, ok := results["macaddr"]; !ok && len(r) > 10 {
results["macaddr"] = strings.Join(r[5:10], ":")
}
}
if strings.HasPrefix(s.Text(), "RSSI") {
if len(r) > 1 {
results["signal"] = r[0]
}
}
switch strings.Join(p, " ") {
case "Modem Firmware Version":
results["firmware"] = strings.Replace(strings.Replace(strings.Join(r, " "), "(", "", -1), ")", "", -1)
case "Station Name":
results["name"] = strings.Join(r, " ")
case "Unit Status":
results["status"] = strings.Join(r, " ")
case "DC Input Voltage":
results["supply"] = strings.Join(r, " ")
case "Transceiver Temperature":
results["temperature"] = strings.Join(r, " ")
}
}
})
return results, nil
}