-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
91 lines (80 loc) · 1.76 KB
/
utils.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
package main
import (
"os"
"fmt"
"path/filepath"
"io/ioutil"
"github.com/cdevr/WapSNMP"
"github.com/fullsailor/pkcs7"
)
func TLV(t int64, v ...[]byte) []byte {
value := make([]byte, 0)
for _, i := range v {
value = append( value, i... )
}
return append([]byte{uint8(t), uint8(len(value))}, value...)
}
func Snmp(oidString string, v interface{}) []byte {
oid, err := wapsnmp.ParseOid(oidString)
if err != nil {
return nil
}
res, err := wapsnmp.EncodeSequence([]interface{}{
wapsnmp.Sequence,
oid,
v,
})
if err != nil {
return nil
}
return res
}
func SnmpGauge(v int64) wapsnmp.Gauge {
return wapsnmp.Gauge(v)
}
func ChunkSplit(buf []byte, lim int) [][]byte {
var chunk []byte
chunks := make([][]byte, 0, len(buf)/lim+1)
for len(buf) >= lim {
chunk, buf = buf[:lim], buf[lim:]
chunks = append(chunks, chunk)
}
if len(buf) > 0 {
chunks = append(chunks, buf[:len(buf)])
}
return chunks
}
func ExtractCVC(filename string) ([]byte) {
content, err := ioutil.ReadFile(filepath.Join("/", filename))
if err != nil {
log.Error(err)
return []byte("")
}
p, err := pkcs7.Parse(content)
if err != nil {
log.Error(err)
return []byte("")
}
cert := p.GetOnlySigner()
return cert.Raw
}
func GetFirmwarePath(model string, hwver string) string {
paths := []string{
filepath.Join("/firmware", fmt.Sprintf("%s-%s", model, hwver), "current"),
filepath.Join("/firmware", model, "current"),
}
for _, p := range paths {
if _, err := os.Stat(p); err == nil {
sym, err := filepath.EvalSymlinks(p)
if err != nil {
continue
}
rel, err := filepath.Rel("/", sym)
if err != nil {
continue
}
return rel
}
}
return ""
}