-
Notifications
You must be signed in to change notification settings - Fork 36
/
common.go
214 lines (195 loc) · 5.31 KB
/
common.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
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/tls"
"encoding/json"
"fmt"
"math/rand"
"net"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
const MinNodeNameLen = 8
func getmac(ip string) string {
//get mac relative to the ip address which connected to the mq.
ifaces, err := net.Interfaces()
if err != nil {
return ""
}
firstMac := ""
for _, iface := range ifaces {
addrs, _ := iface.Addrs()
for _, addr := range addrs {
if firstMac == "" {
firstMac = iface.HardwareAddr.String()
}
if ipNet, ok := addr.(*net.IPNet); ok && ipNet.IP.String() == ip {
if iface.HardwareAddr.String() != "" {
return iface.HardwareAddr.String()
}
return firstMac
}
}
}
return firstMac
}
var cbcIVBlock = []byte("UHNJUSBACIJFYSQN")
var paddingArray = [][]byte{
{0},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
{3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3},
{4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
{5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5},
{6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6},
{7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7},
{8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8},
{9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9},
{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
{11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11},
{12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12},
{13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13},
{14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14},
{15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15},
{16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
}
func pkcs7Padding(plainData []byte, dataLen, blockSize int) int {
padLen := blockSize - dataLen%blockSize
pPadding := plainData[dataLen : dataLen+padLen]
copy(pPadding, paddingArray[padLen][:padLen])
return padLen
}
func pkcs7UnPadding(origData []byte, dataLen int) ([]byte, error) {
unPadLen := int(origData[dataLen-1])
if unPadLen <= 0 || unPadLen > 16 {
return nil, fmt.Errorf("wrong pkcs7 padding head size:%d", unPadLen)
}
return origData[:(dataLen - unPadLen)], nil
}
// AES-CBC
func encryptBytes(key []byte, out, in []byte, plainLen int) ([]byte, error) {
if len(key) == 0 {
return in[:plainLen], nil
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
//iv := out[:aes.BlockSize]
//if _, err := io.ReadFull(rand.Reader, iv); err != nil {
// return nil, err
//}
mode := cipher.NewCBCEncrypter(block, cbcIVBlock)
total := pkcs7Padding(in, plainLen, aes.BlockSize) + plainLen
mode.CryptBlocks(out[:total], in[:total])
return out[:total], nil
}
func decryptBytes(key []byte, out, in []byte, dataLen int) ([]byte, error) {
if len(key) == 0 {
return in[:dataLen], nil
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
mode := cipher.NewCBCDecrypter(block, cbcIVBlock)
mode.CryptBlocks(out[:dataLen], in[:dataLen])
return pkcs7UnPadding(out, dataLen)
}
// {240e:3b7:622:3440:59ad:7fa1:170c:ef7f 47924975352157270363627191692449083263 China CN 0xc0000965c8 Guangdong GD 0 Guangzhou 23.1167 113.25 Asia/Shanghai AS4134 Chinanet }
func netInfo() *NetInfo {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
// DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
// var d net.Dialer
// return d.DialContext(ctx, "tcp6", addr)
// },
}
// sometime will be failed, retry
for i := 0; i < 2; i++ {
client := &http.Client{Transport: tr, Timeout: time.Second * 10}
r, err := client.Get("https://ifconfig.co/json")
if err != nil {
gLog.Println(LvINFO, "netInfo error:", err)
continue
}
defer r.Body.Close()
buf := make([]byte, 1024*64)
n, err := r.Body.Read(buf)
if err != nil {
gLog.Println(LvINFO, "netInfo error:", err)
continue
}
rsp := NetInfo{}
err = json.Unmarshal(buf[:n], &rsp)
if err != nil {
gLog.Printf(LvERROR, "wrong NetInfo:%s", err)
continue
}
return &rsp
}
return nil
}
func execOutput(name string, args ...string) string {
cmdGetOsName := exec.Command(name, args...)
var cmdOut bytes.Buffer
cmdGetOsName.Stdout = &cmdOut
cmdGetOsName.Run()
return cmdOut.String()
}
func defaultNodeName() string {
name, _ := os.Hostname()
for len(name) < MinNodeNameLen {
name = fmt.Sprintf("%s%d", name, rand.Int()%10)
}
return name
}
const EQUAL int = 0
const GREATER int = 1
const LESS int = -1
func compareVersion(v1, v2 string) int {
if v1 == v2 {
return EQUAL
}
v1Arr := strings.Split(v1, ".")
v2Arr := strings.Split(v2, ".")
for i, subVer := range v1Arr {
if len(v2Arr) <= i {
return GREATER
}
subv1, _ := strconv.Atoi(subVer)
subv2, _ := strconv.Atoi(v2Arr[i])
if subv1 > subv2 {
return GREATER
}
if subv1 < subv2 {
return LESS
}
}
return LESS
}
func parseMajorVer(ver string) int {
v1Arr := strings.Split(ver, ".")
if len(v1Arr) > 0 {
n, _ := strconv.ParseInt(v1Arr[0], 10, 32)
return int(n)
}
return 0
}
func IsIPv6(address string) bool {
return strings.Count(address, ":") >= 2
}
var letters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-")
func randStr(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}