-
Notifications
You must be signed in to change notification settings - Fork 104
/
Utils.go
236 lines (205 loc) · 5.36 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
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
package main
import (
"bytes"
"encoding/binary"
"encoding/hex"
"io"
"net"
"regexp"
"strconv"
"strings"
)
// IP2Long IP转整数
// 来自 <https://www.socketloop.com/tutorials/golang-convert-ip-address-string-to-long-unsigned-32-bit-integer>
func IP2Long(ip string) uint32 {
var long uint32
binary.Read(bytes.NewBuffer(net.ParseIP(ip).To4()), binary.BigEndian, &long)
return long
}
// Long2IP 整数转IP
// 来自 <https://www.socketloop.com/tutorials/golang-convert-ip-address-string-to-long-unsigned-32-bit-integer>
func Long2IP(ipLong uint32) string {
ipInt := int64(ipLong)
// need to do two bit shifting and “0xff” masking
b0 := strconv.FormatInt((ipInt>>24)&0xff, 10)
b1 := strconv.FormatInt((ipInt>>16)&0xff, 10)
b2 := strconv.FormatInt((ipInt>>8)&0xff, 10)
b3 := strconv.FormatInt((ipInt & 0xff), 10)
return b0 + "." + b1 + "." + b2 + "." + b3
}
// Uint64ToBin unit64 转二进制
func Uint64ToBin(num uint64) []byte {
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.BigEndian, num)
return bytesBuffer.Bytes()
}
// Uint64ToHex unit64 转 hex
func Uint64ToHex(num uint64) string {
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.BigEndian, num)
return hex.EncodeToString(bytesBuffer.Bytes())
}
// Uint32ToBin unit32 转二进制
func Uint32ToBin(num uint32) []byte {
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.BigEndian, num)
return bytesBuffer.Bytes()
}
// Uint32ToHex unit32 转 hex
func Uint32ToHex(num uint32) string {
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.BigEndian, num)
return hex.EncodeToString(bytesBuffer.Bytes())
}
// Uint32ToHexLE unit32 转 hex (小端字节序)
func Uint32ToHexLE(num uint32) string {
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.LittleEndian, num)
return hex.EncodeToString(bytesBuffer.Bytes())
}
// Uint16ToHex uint16 转 hex
func Uint16ToHex(num uint16) string {
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.BigEndian, num)
return hex.EncodeToString(bytesBuffer.Bytes())
}
// SubString 字符串截取
// <http://outofmemory.cn/code-snippet/1365/Go-language-jiequ-string-function>
func SubString(str string, start, length int) string {
rs := []rune(str)
rl := len(rs)
end := 0
if start < 0 {
start = rl - 1 + start
}
end = start + length
if start > end {
start, end = end, start
}
if start < 0 {
start = 0
}
if start > rl {
start = rl
}
if end < 0 {
end = 0
}
if end > rl {
end = rl
}
return string(rs[start:end])
}
// IOCopyBuffer 在写入失败时还能拿到Buffer的IO拷贝函数
func IOCopyBuffer(dst io.Writer, src io.Reader, buf []byte) (bufferLen int, err error) {
if buf == nil {
err = ErrInvalidBuffer
return
}
if src == nil {
err = ErrInvalidReader
return
}
if dst == nil {
err = ErrInvalidWritter
return
}
for {
nr, er := src.Read(buf)
bufferLen = nr
if nr > 0 {
nw, ew := dst.Write(buf[0:nr])
if ew != nil {
err = ErrWriteFailed
break
}
if nr != nw {
err = ErrWriteFailed
break
}
}
if er != nil {
err = ErrReadFailed
break
}
}
return
}
// StripEthAddrFromFullName 从矿机名中去除不必要的以太坊钱包地址
func StripEthAddrFromFullName(fullNameStr string) string {
pos := strings.Index(fullNameStr, ".")
// The Ethereum address is 42 bytes and starting with "0x".
// Example: 0x00d8c82Eb65124Ea3452CaC59B64aCC230AA3482
if pos == 42 && fullNameStr[0] == '0' && (fullNameStr[1] == 'x' || fullNameStr[1] == 'X') {
return fullNameStr[pos+1:]
}
return fullNameStr
}
// FilterWorkerName 过滤矿工名
func FilterWorkerName(workerName string) string {
pattren := regexp.MustCompile(`[^a-zA-Z0-9,=/.\-_:|^]`)
return pattren.ReplaceAllString(workerName, "")
}
func IPAsWorkerName(format string, ip string) string {
if len(ip) < 1 {
return ip
}
addr, err := net.ResolveTCPAddr("tcp", ip)
if err != nil {
return ip
}
len := len(addr.IP)
if len < 4 {
return ip
}
var base int
if ip[0] == '[' {
base = 16
} else {
base = 10
}
format = strings.ReplaceAll(format, "{1}", strconv.FormatUint(uint64(addr.IP[len-4]), base))
format = strings.ReplaceAll(format, "{2}", strconv.FormatUint(uint64(addr.IP[len-3]), base))
format = strings.ReplaceAll(format, "{3}", strconv.FormatUint(uint64(addr.IP[len-2]), base))
format = strings.ReplaceAll(format, "{4}", strconv.FormatUint(uint64(addr.IP[len-1]), base))
return format
}
func IsEnabled(option bool) string {
if option {
return "Enabled"
}
return "Disabled"
}
func HexAddPrefix(hexStr string) string {
if len(hexStr) == 0 || (len(hexStr) >= 2 && hexStr[0] == '0' && (hexStr[1] == 'x' || hexStr[1] == 'X')) {
return hexStr
}
return "0x" + hexStr
}
func HexRemovePrefix(hexStr string) string {
// remove prefix "0x" or "0X"
if len(hexStr) >= 2 && hexStr[0] == '0' && (hexStr[1] == 'x' || hexStr[1] == 'X') {
hexStr = hexStr[2:]
}
return hexStr
}
func Hex2Bin(hexStr string) (bin []byte, err error) {
hexStr = HexRemovePrefix(hexStr)
bin, err = hex.DecodeString(hexStr)
return
}
func Hex2Uint64(hexStr string) (result uint64, err error) {
hexStr = HexRemovePrefix(hexStr)
result, err = strconv.ParseUint(hexStr, 16, 64)
return
}
// BinReverse 颠倒字节序列
func BinReverse(bin []byte) {
i := 0
j := len(bin) - 1
for i < j {
bin[i], bin[j] = bin[j], bin[i]
i++
j--
}
}