/
util.go
342 lines (305 loc) · 7.53 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright 2022 The kubegems.io Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package utils
import (
"bytes"
"crypto/cipher"
"crypto/des"
"encoding/base64"
"errors"
"fmt"
"math"
"math/rand"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"time"
"golang.org/x/crypto/bcrypt"
)
var (
regA_Z = regexp.MustCompile("[A-Z]+")
rega_z = regexp.MustCompile("[a-z]+")
reg0_9 = regexp.MustCompile("[0-9]+")
reg_chs = regexp.MustCompile(`[!\.@#$%~]+`)
)
var (
TimeForever = time.Date(9893, time.December, 26, 0, 0, 0, 0, time.UTC) // 伟人8000年诞辰
MaxDuration time.Duration = 1<<63 - 1
)
func StrOrDef(s string, def string) string {
if s == "" {
return def
}
return s
}
// 保留n位小数
func RoundTo(n float64, decimals uint32) float64 {
return math.Round(n*math.Pow(10, float64(decimals))) / math.Pow(10, float64(decimals))
}
// DayStartTime 返回当天0点
func DayStartTime(t time.Time) time.Time {
year, month, day := t.Date()
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
}
func NextDayStartTime(t time.Time) time.Time {
return DayStartTime(t).Add(24 * time.Hour)
}
func ToUint(id string) uint {
idint, err := strconv.Atoi(id)
if err != nil {
return 0
}
return uint(idint)
}
func ValidPassword(input string) error {
if len(input) < 8 {
return errors.New("密码长度至少8位,包含大小写字母和数字以及特殊字符(.!@#$%~)")
}
if !regA_Z.Match([]byte(input)) {
return errors.New("密码长度至少8位,包含大小写字母和数字以及特殊字符(.!@#$%~)")
}
if !rega_z.Match([]byte(input)) {
return errors.New("密码长度至少8位,包含大小写字母和数字以及特殊字符(.!@#$%~)")
}
if !reg0_9.Match([]byte(input)) {
return errors.New("密码长度至少8位,包含大小写字母和数字以及特殊字符(.!@#$%~)")
}
if !reg_chs.Match([]byte(input)) {
return errors.New("密码长度至少8位,包含大小写字母和数字以及特殊字符(.!@#$%~)")
}
return nil
}
func MakePassword(input string) (string, error) {
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(input), bcrypt.DefaultCost)
return string(hashedPassword), err
}
func GeneratePassword() string {
r := []rune{}
r = append(r, RandomRune(4, RuneKindLower)...)
r = append(r, RandomRune(3, RuneKindUpper)...)
r = append(r, RandomRune(2, RuneKindNum)...)
r = append(r, RandomRune(1, RuneKindChar)...)
rand.Shuffle(len(r), func(i, j int) {
if rand.Intn(10) > 5 {
r[i], r[j] = r[j], r[i]
}
})
return string(r)
}
func ValidatePassword(password string, hashedPassword string) error {
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
}
func JoinFlagName(prefix, key string) string {
if prefix == "" {
return strings.ToLower(key)
}
return strings.ToLower(prefix + "-" + key)
}
const (
RuneKindNum = "num"
RuneKindLower = "lower"
RuneKindUpper = "upper"
RuneKindChar = "char"
)
var (
lowerLetterRunes = []rune("abcdefghijklmnopqrstuvwxyz")
upperLetterRunes = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
numRunes = []rune("0123456789")
charRunes = []rune("!.@#$%~")
)
func RandomRune(n int, kind string) []rune {
b := make([]rune, n)
var l []rune
switch kind {
case RuneKindChar:
l = charRunes
case RuneKindUpper:
l = upperLetterRunes
case RuneKindLower:
l = lowerLetterRunes
case RuneKindNum:
l = numRunes
default:
l = lowerLetterRunes
}
length := len(l)
for i := range b {
b[i] = l[rand.Intn(length)]
}
return b
}
// BoolPointer converts bool to *bool
func BoolPointer(b bool) *bool {
return &b
}
func BoolToString(a bool) string {
if a {
return "1"
}
return "0"
}
func BoolToFloat64(a bool) float64 {
if a {
return 1
}
return 0
}
func TimeZeroToNull(t *time.Time) *time.Time {
if t == nil || t.IsZero() {
return nil
}
return t
}
func FormatMysqlDumpTime(t *time.Time) string {
if t == nil {
return ""
}
return t.Format("2006-01-02 15:04:05.000")
}
func UintToStr(i *uint) string {
if i == nil {
return ""
}
return strconv.Itoa(int(*i))
}
type DesEncryptor struct {
Key []byte
}
func (e *DesEncryptor) EncryptBase64(input string) (string, error) {
data := []byte(input)
block, err := des.NewCipher(e.Key)
if err != nil {
return "", err
}
data = e.Padding(data, block.BlockSize())
blockMode := cipher.NewCBCEncrypter(block, e.Key)
crypted := make([]byte, len(data))
blockMode.CryptBlocks(crypted, data)
return base64.StdEncoding.EncodeToString(crypted), nil
}
func (e *DesEncryptor) DecryptBase64(input string) (string, error) {
data, err := base64.StdEncoding.DecodeString(input)
if err != nil {
return "", err
}
block, err := des.NewCipher(e.Key)
if err != nil {
return "", err
}
blockMode := cipher.NewCBCDecrypter(block, e.Key)
origData := make([]byte, len(data))
blockMode.CryptBlocks(origData, data)
origData = e.UnPadding(origData)
return string(origData), nil
}
func (e *DesEncryptor) Padding(cipherText []byte, blockSize int) []byte {
padding := blockSize - len(cipherText)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(cipherText, padText...)
}
func (e *DesEncryptor) UnPadding(data []byte) []byte {
length := len(data)
if length == 0 {
return []byte{}
}
unpadding := int(data[length-1])
return data[:(length - unpadding)]
}
const (
BYTE = 1 << (10 * iota)
KILOBYTE
MEGABYTE
GIGABYTE
TERABYTE
PETABYTE
EXABYTE
)
// ConvertBytes 保留两位小数
func ConvertBytes(bytes float64) string {
unit := ""
value := float64(bytes)
switch {
case bytes >= EXABYTE:
unit = "EB"
value = value / EXABYTE
case bytes >= PETABYTE:
unit = "PB"
value = value / PETABYTE
case bytes >= TERABYTE:
unit = "TB"
value = value / TERABYTE
case bytes >= GIGABYTE:
unit = "GB"
value = value / GIGABYTE
case bytes >= MEGABYTE:
unit = "MB"
value = value / MEGABYTE
case bytes >= KILOBYTE:
unit = "KB"
value = value / KILOBYTE
case bytes >= BYTE:
unit = "B"
case bytes == 0:
return "0B"
}
result := strconv.FormatFloat(value, 'f', 2, 64)
result = strings.TrimSuffix(result, ".00")
return result + unit
}
// WaitGroupWithTimeout waits for the waitgroup for the specified max timeout.
// Returns true if waiting timed out.
func WaitGroupWithTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait()
}()
select {
case <-c:
return false
case <-time.After(timeout):
return true
}
}
// CheckStructFieldsEmpty check all struct fileds not empty.
// Now only check string and int.
func CheckStructFieldsEmpty(obj any) error {
value := reflect.ValueOf(obj)
t := reflect.TypeOf(obj)
if value.Kind() == reflect.Ptr {
value = value.Elem()
t = t.Elem()
}
emptyFieldName := ""
for i := 0; i < value.NumField(); i++ {
field := value.Field(i)
switch field.Kind() {
case reflect.String:
if field.String() == "" {
emptyFieldName = t.Field(i).Name
break
}
case reflect.Int:
if field.Int() == 0 {
emptyFieldName = t.Field(i).Name
}
}
}
if emptyFieldName != "" {
return fmt.Errorf("field %s is tmpty", emptyFieldName)
}
return nil
}