-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev_viola_nosnmp.go
303 lines (250 loc) · 7.08 KB
/
dev_viola_nosnmp.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package godevman
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"github.com/patrickmn/go-cache"
)
// Adds Viola specific functionality to snmpCommon type
type deviceViolaNoSNMP struct {
device
}
// Make http Get request and return byte slice of body.
// Argument string should contain request parameters.
func (d *deviceViolaNoSNMP) WebApiGet(params string) ([]byte, error) {
client := d.webSession.client
if d.webSession.client == nil {
// setup client
c, err := d.webClient(nil)
if err != nil {
return nil, err
}
client = c
}
res, err := client.Get("http://" + d.ip + "/" + params)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
if res.StatusCode > 299 {
return body, fmt.Errorf("response failed with status code: %d", res.StatusCode)
}
return body, nil
}
// Make http WebApiPostForm request and return byte slice of body.
// Argument string should contain remainder after base URL.
func (sd *deviceViolaNoSNMP) WebApiPostForm(params string, rd url.Values) ([]byte, error) {
client := sd.webSession.client
if sd.webSession.client == nil {
// setup client
c, err := sd.webClient(nil)
if err != nil {
return nil, err
}
client = c
}
baseUrl := "http://" + sd.ip + "/" + params
res, err := client.PostForm(baseUrl, rd)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
if res.StatusCode > 299 {
return body, fmt.Errorf("response failed with status code: %d", res.StatusCode)
}
return body, nil
}
// Login via web API and stores web session in deviceViolaNoSNMP.webSession.client.
// Use this before use of methods which are accessing restricted device web API.
func (sd *deviceViolaNoSNMP) WebAuth(userPass []string) error {
// setup client
client, err := sd.webClient(nil)
if err != nil {
return err
}
// credentials
cred := url.Values{
"FRID": {"0"},
"user": {userPass[0]},
"psw": {userPass[1]},
}
baseUrl := "http://" + sd.ip + "/cgi-bin/localconfig"
// login
res, err := client.PostForm(baseUrl, cred)
if err != nil {
return err
}
// close response body
defer res.Body.Close()
// read all response body
body, _ := ioutil.ReadAll(res.Body)
if res.StatusCode > 299 {
return fmt.Errorf("response failed with status code: %d", res.StatusCode)
}
// HACK device uses obsolete 'META HTTP-EQUIV="Set-Cookie"' to set cookies
// We need to parse and set cookie manually
reParts := regexp.MustCompile(`META HTTP-EQUIV="Set-Cookie" CONTENT="(LC_Token)=(\d+);"`)
parts := reParts.FindStringSubmatch(string(body))
if len(parts) < 3 {
return fmt.Errorf("authentication failed")
}
c := http.Cookie{
Name: parts[1],
Value: parts[2],
}
cookies := []*http.Cookie{&c}
urlObj, _ := url.Parse(baseUrl)
client.Jar.SetCookies(urlObj, cookies)
sd.webSession.client = client
return nil
}
// Logout via web API and delete web session from deviceEricssonMlPt.webSession.client.
// Use this after use of methods which are accessing restricted device web API.
func (sd *deviceViolaNoSNMP) WebLogout() error {
if sd.webSession.client == nil {
return nil
}
rd := url.Values{"FRID": {"12000"}}
_, err := sd.WebApiPostForm("cgi-bin/localconfig", rd)
if err != nil {
return fmt.Errorf("errors: WebApiPostForm - %s", err)
}
sd.webSession.client = nil
return nil
}
// Get device model
// Example output:
// map[string]string{
// "firmware":"IEC-104 RTU 5.2.1 (build 1095)"
// "flash":"8MB"
// "hwserial":"11244151"
// "hwtype":"3.1"
// "mac":"00:06:70:02:72:17"
// "os":"Linux version 2.4.19-uc1"
// "proc":"COLDFIRE(m5272)"
// "prodname":"Arctic Control (EDGE)"
// "ram":"31352 kB"
// "serial":"ACO5272-48-328-027217"
// }
func (sd *deviceViolaNoSNMP) SysInfo() (map[string]string, error) {
if err := sd.WebAuth(sd.webSession.cred); err != nil {
return nil, fmt.Errorf("error: WebAuth - %s", err)
}
body, err := sd.WebApiGet("cgi-bin/localconfig?1001")
if err != nil {
return nil, fmt.Errorf("get request from device api failed: %s", err)
}
err = sd.WebLogout()
if err != nil {
return nil, fmt.Errorf("errors: WebLogout - %s", err)
}
out := make(map[string]string)
reSep := `.*?</TD>.*?</TD>.*?>\s+`
reBr := `</TD>.*`
reParts := regexp.MustCompile(
"(?s)Product name" + reSep + "(.*?)" + reBr +
"Product serial number" + reSep + "(.*?)" + reBr +
"HW serial number" + reSep + "(.*?)" + reBr +
"HW version" + reSep + "(.*?)" + reBr +
"Operating system" + reSep + "(.*?)" + reBr +
"Firmware" + reSep + "(.*?)" + reBr +
"Processor" + reSep + "(.*?)" + reBr +
"MAC address" + reSep + "(.*?)" + reBr +
"RAM memory" + reSep + "(.*?)" + reBr +
"Flash memory" + reSep + "(.*?)" + reBr)
parts := reParts.FindStringSubmatch(string(body))
if len(parts) < 11 {
return nil, fmt.Errorf("info not found from device web")
}
out["prodname"] = parts[1]
out["serial"] = parts[2]
out["hwserial"] = parts[3]
out["hwtype"] = parts[4]
out["os"] = parts[5]
out["firmware"] = parts[6]
out["proc"] = parts[7]
out["mac"] = parts[8]
out["ram"] = parts[9]
out["flash"] = parts[10]
// save to cache
sd.cache.Set("SysInfo", out, cache.DefaultExpiration)
return out, nil
}
// Get info from web
// Valid targets values: "All", "Descr", "ObjectID"
func (sd *deviceViolaNoSNMP) System(targets []string) (System, error) {
results := func(t []string, i map[string]string) System {
out := new(System)
for _, t := range targets {
if t == "All" || t == "ObjectID" {
out.ObjectID.Value = "no-snmp-viola"
out.ObjectID.IsSet = true
}
if t == "All" || t == "Descr" {
out.Descr.Value = i["prodname"]
out.Descr.IsSet = true
}
}
return *out
}
// return from cache if allowed and cache is present
if sd.useCache {
if x, ok := sd.cache.Get("SysInfo"); ok {
info := x.(map[string]string)
return results(targets, info), nil
}
}
info, err := sd.SysInfo()
if err != nil {
return System{}, fmt.Errorf("errors: SysInfo - %s", err)
}
return results(targets, info), nil
}
// Get device model
// Example output:
// map[string]string{
// "hwtype":"3.1",
// "prodname":"Arctic Control (EDGE)",
// "serial":"ACO5272-48-328-027217"
// }
func (sd *deviceViolaNoSNMP) HwInfo() (map[string]string, error) {
results := func(i map[string]string) map[string]string {
out := map[string]string{
"hwtype": i["hwtype"],
"prodname": i["prodname"],
"serial": i["serial"],
}
return out
}
// return from cache if allowed and cache is present
if sd.useCache {
if x, ok := sd.cache.Get("SysInfo"); ok {
info := x.(map[string]string)
return results(info), nil
}
}
info, err := sd.SysInfo()
if err != nil {
return nil, fmt.Errorf("errors: SysInfo - %s", err)
}
return results(info), nil
}
// Get running software version
func (sd *deviceViolaNoSNMP) SwVersion() (string, error) {
// return from cache if allowed and cache is present
if sd.useCache {
if x, ok := sd.cache.Get("SysInfo"); ok {
info := x.(map[string]string)
return info["firmware"], nil
}
}
info, err := sd.SysInfo()
if err != nil {
return "", fmt.Errorf("errors: SysInfo - %s", err)
}
return info["firmware"], nil
}