-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutil.go
202 lines (163 loc) · 3.86 KB
/
util.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
package vega
import (
"encoding/binary"
"fmt"
"math/rand"
"net"
"runtime"
"strconv"
crand "crypto/rand"
)
// Lovely borrowed from consul
/*
* Contains an entry for each private block:
* 10.0.0.0/8
* 172.16.0.0/12
* 192.168/16
*/
var privateBlocks []*net.IPNet
var randSrc rand.Source
var randGen *rand.Rand
func init() {
// Add each private block
privateBlocks = make([]*net.IPNet, 3)
_, block, err := net.ParseCIDR("10.0.0.0/8")
if err != nil {
panic(fmt.Sprintf("Bad cidr. Got %v", err))
}
privateBlocks[0] = block
_, block, err = net.ParseCIDR("172.16.0.0/12")
if err != nil {
panic(fmt.Sprintf("Bad cidr. Got %v", err))
}
privateBlocks[1] = block
_, block, err = net.ParseCIDR("192.168.0.0/16")
if err != nil {
panic(fmt.Sprintf("Bad cidr. Got %v", err))
}
privateBlocks[2] = block
var n int64
binary.Read(crand.Reader, binary.BigEndian, &n)
randSrc = rand.NewSource(n)
randGen = rand.New(randSrc)
}
// Returns if the given IP is in a private block
func isPrivateIP(ip_str string) bool {
ip := net.ParseIP(ip_str)
for _, priv := range privateBlocks {
if priv.Contains(ip) {
return true
}
}
return false
}
// GetPrivateIP is used to return the first private IP address
// associated with an interface on the machine
func GetPrivateIP() (net.IP, error) {
addresses, err := net.InterfaceAddrs()
if err != nil {
return nil, fmt.Errorf("Failed to get interface addresses: %v", err)
}
// Find private IPv4 address
for _, rawAddr := range addresses {
var ip net.IP
switch addr := rawAddr.(type) {
case *net.IPAddr:
ip = addr.IP
case *net.IPNet:
ip = addr.IP
default:
continue
}
if ip.To4() == nil {
continue
}
if !isPrivateIP(ip.String()) {
continue
}
return ip, nil
}
return nil, fmt.Errorf("No private IP address found")
}
// runtimeStats is used to return various runtime information
func runtimeStats() map[string]string {
return map[string]string{
"os": runtime.GOOS,
"arch": runtime.GOARCH,
"version": runtime.Version(),
"max_procs": strconv.FormatInt(int64(runtime.GOMAXPROCS(0)), 10),
"goroutines": strconv.FormatInt(int64(runtime.NumGoroutine()), 10),
"cpu_count": strconv.FormatInt(int64(runtime.NumCPU()), 10),
}
}
// generateUUID is used to generate a random UUID
func generateUUID() string {
uuid := make([]byte, 16)
for i := 0; i < 16; i += 8 {
binary.BigEndian.PutUint64(uuid[i:i+8], uint64(randGen.Int63()))
}
// if _, err := rand.Read(uuid); err != nil {
// panic(fmt.Errorf("failed to read random bytes: %v", err))
// }
uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4
uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant is 10
return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
uuid[0:4],
uuid[4:6],
uuid[6:8],
uuid[8:10],
uuid[10:16])
}
// generateUUID is used to generate a random UUID
func generateUUIDSecure() string {
uuid := make([]byte, 16)
if _, err := crand.Read(uuid); err != nil {
panic(fmt.Errorf("failed to read random bytes: %v", err))
}
uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4
uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant is 10
return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
uuid[0:4],
uuid[4:6],
uuid[6:8],
uuid[8:10],
uuid[10:16])
}
func RandomMailbox() string {
return "gen-" + generateUUID()
}
func RandomID() string {
return "m" + generateUUID()
}
func RandomKey(size int) []byte {
key := make([]byte, size)
n, err := crand.Read(key)
if n != size {
panic("Not enough random material returned")
}
if err != nil {
panic(err)
}
return key
}
func RandomIV(size int) []byte {
m := size % 8
if m != 0 {
size += (8 - m)
}
iv := make([]byte, size)
for i := 0; i < size; i += 8 {
binary.BigEndian.PutUint64(iv[i:i+8], uint64(randGen.Int63()))
}
return iv
}
func XORBytes(dst, a, b []byte) int {
n := len(a)
if len(b) < n {
n = len(b)
}
for i := 0; i < n; i++ {
dst[i] = a[i] ^ b[i]
}
return n
}