-
Notifications
You must be signed in to change notification settings - Fork 5
/
tools.go
759 lines (693 loc) · 17.7 KB
/
tools.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
package tools
import (
"bytes"
"compress/flate"
"compress/gzip"
"compress/zlib"
"context"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"encoding/base64"
"errors"
"fmt"
"hash"
"image"
"io"
"math"
"math/rand"
"slices"
"github.com/mholt/archives"
"net"
"net/url"
"os"
"path/filepath"
"reflect"
"runtime"
"sort"
"strings"
"sync"
"time"
"unsafe"
_ "image/png"
_ "embed"
"github.com/gospider007/kinds"
"github.com/gospider007/re"
_ "golang.org/x/image/webp"
"golang.org/x/net/html/charset"
"golang.org/x/text/encoding/simplifiedchinese"
)
// if exist return true
func PathExist(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
}
if os.IsNotExist(err) {
return false
}
fmt.Println(err)
return false
}
return true
}
// create dir
func MkDir(path string) error {
err := os.MkdirAll(path, os.ModePerm)
return err
}
// join url patch
func UrlJoin(base, href string) (string, error) {
baseUrl, err := url.Parse(base)
if err != nil {
return base, err
}
refUrl, err := url.Parse(href)
if err != nil {
return href, err
}
return baseUrl.ResolveReference(refUrl).String(), nil
}
// html auto decode
func Charset(content []byte, content_type string) ([]byte, string, error) {
chset, chset_name, _ := charset.DetermineEncoding(content, content_type)
chset_content, err := chset.NewDecoder().Bytes(content)
if err != nil {
return content, chset_name, err
}
return chset_content, chset_name, err
}
// content decode
func Decode[T string | []byte](txt T, code string) T {
var result any
switch val := (any)(txt).(type) {
case string:
switch code {
case "gb2312":
result, _ = simplifiedchinese.HZGB2312.NewDecoder().String(val)
case "gbk":
result, _ = simplifiedchinese.GBK.NewDecoder().String(val)
default:
result = val
}
case []byte:
switch code {
case "gb2312":
result, _ = simplifiedchinese.HZGB2312.NewDecoder().Bytes(val)
case "gbk":
result, _ = simplifiedchinese.GBK.NewDecoder().Bytes(val)
default:
result = val
}
}
return result.(T)
}
// decode encoding with reader
func DecodeRead(txt io.Reader, code string) io.Reader {
switch code {
case "gb2312":
txt = simplifiedchinese.HZGB2312.NewDecoder().Reader(txt)
case "gbk":
txt = simplifiedchinese.GBK.NewDecoder().Reader(txt)
}
return txt
}
// c1 为结构体指针,c2 为结构体,c1无值,c2有值,则c2的值赋值给c1
func Merge(c1 any, c2 any) error {
c2Value := reflect.ValueOf(c2) //初始化为c2保管的具体值的c2Value
c1Value := reflect.ValueOf(c1).Elem() //返回 c1 指针保管的值
c2Type := reflect.TypeOf(c2)
for i := 0; i < c2Type.NumField(); i++ {
c2Name := c2Type.Field(i).Name
field2 := c2Value.FieldByName(c2Name)
field1 := c1Value.FieldByName(c2Name)
if field1.Type() != field2.Type() {
return fmt.Errorf("field %s type not match", c2Name)
}
if !field1.IsValid() || !field2.IsValid() {
return fmt.Errorf("field %s is can not valid", c2Name)
}
if !field1.CanSet() {
return fmt.Errorf("field %s is can not set", c2Name)
}
if !reflect.DeepEqual(field2.Interface(), reflect.Zero(field2.Type()).Interface()) && reflect.DeepEqual(field1.Interface(), reflect.Zero(field1.Type()).Interface()) { //c1无值,c2有值
field1.Set(field2) //设置值
}
}
return nil
}
var zhNumStr = "[零〇一壹二贰三叁四肆五伍六陆七柒八捌九玖]"
var zhNumMap = map[string]string{
"零": "0",
"〇": "0",
"一": "1",
"壹": "1",
"二": "2",
"贰": "2",
"三": "3",
"叁": "3",
"四": "4",
"肆": "4",
"五": "5",
"伍": "5",
"六": "6",
"陆": "6",
"七": "7",
"柒": "7",
"八": "8",
"捌": "8",
"九": "9",
"玖": "9",
}
// get time
func GetTime(txt string, desc ...bool) string {
txt = re.SubFunc(zhNumStr, func(s string) string {
return zhNumMap[s]
}, txt)
txt = re.SubFunc(`\d?十\d*`, func(s string) string {
if s == "十" {
return "10"
} else if strings.HasPrefix(s, "十") {
return strings.Replace(s, "十", "1", 1)
} else if strings.HasSuffix(s, "十") {
return strings.Replace(s, "十", "0", 1)
} else {
return strings.Replace(s, "十", "", 1)
}
}, txt)
lls := re.FindAll(`\D(20[012]\d(?:[\.\-/]|\s?年\s?)[01]?\d(?:[\.\-/]|\s?月\s?)[0123]?\d)\D`, "a"+txt+"a")
data := kinds.NewSet[string]()
for _, ll := range lls {
ll_str := re.Sub(`[\.\-年月/]`, "-", ll.Group(1))
value_lls := strings.Split(ll_str, "-")
moth := value_lls[1]
day := value_lls[2]
if len(moth) == 1 {
moth = "0" + moth
}
if len(day) == 1 {
day = "0" + day
}
data.Add(value_lls[0] + "-" + moth + "-" + day)
}
var result string
if data.Len() > 0 {
temData := data.Array()
sort.Strings(temData)
if len(desc) > 0 && desc[0] {
result = temData[data.Len()-1]
} else {
result = temData[0]
}
}
return result
}
// escape path with file path
func PathEscape(txt string) string { //空格转换%20
return url.PathEscape(txt)
}
// unescape path with file path
func PathUnescape(txt string) (string, error) {
return url.PathUnescape(txt)
}
// escape path with url query
func QueryEscape(txt string) string { //空格转换为+
return url.QueryEscape(txt)
}
// unescape path with url query
func QueryUnescape(txt string) (string, error) {
return url.QueryUnescape(txt)
}
// user default dir
func GetDefaultDir() (string, error) {
userHomeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("could not get user home directory: %v", err)
}
switch runtime.GOOS {
case "windows":
return filepath.Join(userHomeDir, "AppData", "Local"), nil
case "darwin":
return filepath.Join(userHomeDir, "Library", "Caches"), nil
case "linux":
return filepath.Join(userHomeDir, ".cache"), nil
}
return "", errors.New("could not determine cache directory")
}
// join path with file path
func PathJoin(elem ...string) string {
return filepath.Join(elem...)
}
// aes encode
func AesEncode(val []byte, key []byte) (string, error) {
keyLen := len(key)
if keyLen > 16 {
key = key[:16]
} else if keyLen < 16 {
key = append(key, make([]byte, 16-keyLen)...)
}
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
blockSize := block.BlockSize()
padNum := blockSize - len(val)%blockSize
pad := bytes.Repeat([]byte{byte(padNum)}, padNum)
val = append(val, pad...)
blockMode := cipher.NewCBCEncrypter(block, key)
blockMode.CryptBlocks(val, val)
return Base64Encode(val), nil
}
// HmacSha1 encode
func HmacSha1[T string | []byte](val, key T) []byte {
var mac hash.Hash
switch con := (any)(key).(type) {
case string:
mac = hmac.New(sha1.New, StringToBytes(con))
case []byte:
mac = hmac.New(sha1.New, con)
}
switch con := (any)(val).(type) {
case string:
mac.Write(StringToBytes(con))
case []byte:
mac.Write(con)
}
return mac.Sum(nil)
}
// Sha1 encode
func Sha1[T string | []byte](val T) []byte {
mac := sha1.New()
switch con := (any)(val).(type) {
case string:
mac.Write(StringToBytes(con))
case []byte:
mac.Write(con)
}
return mac.Sum(nil)
}
// md5 encode
func Md5[T string | []byte](val T) [16]byte {
var result [16]byte
switch con := (any)(val).(type) {
case string:
result = md5.Sum(StringToBytes(con))
case []byte:
result = md5.Sum(con)
}
return result
}
// md5 encode
func Md5Raw[T string | []byte](val T) []byte {
h := md5.New()
switch con := (any)(val).(type) {
case string:
h.Write(StringToBytes(con))
case []byte:
h.Write(con)
}
return h.Sum(nil)
}
// base64 encode
func Base64Encode[T string | []byte](val T) string {
switch con := (any)(val).(type) {
case string:
return base64.StdEncoding.EncodeToString(StringToBytes(con))
case []byte:
return base64.StdEncoding.EncodeToString(con)
}
return ""
}
// base64 decode
func Base64Decode(val string) ([]byte, error) {
return base64.StdEncoding.DecodeString(val)
}
// any to hex
func Hex(val any) string {
return fmt.Sprintf("%x", val)
}
// ase decode
func AesDecode(val string, key []byte) ([]byte, error) {
src, err := Base64Decode(val)
if err != nil {
return nil, nil
}
keyLen := len(key)
if keyLen > 16 {
key = key[:16]
} else if keyLen < 16 {
key = append(key, make([]byte, 16-keyLen)...)
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if len(key) != block.BlockSize() {
return nil, errors.New("cipher.NewCBCDecrypter: IV length must equal block size")
}
if len(src)%block.BlockSize() != 0 {
return nil, errors.New("crypto/cipher: input not full blocks")
}
blockMode := cipher.NewCBCDecrypter(block, key)
blockMode.CryptBlocks(src, src)
n := len(src)
un := n - 1
if un < 0 {
return src, errors.New("crypto/cipher: length error")
}
unPadNum := int(src[un])
un2 := n - unPadNum
if un2 < 0 {
return src, errors.New("crypto/cipher: padding error")
}
src = src[:un2]
return src, nil
}
func compressionBrDecode(r io.Reader) (io.ReadCloser, error) {
return archives.Brotli{}.OpenReader(r)
}
func compressionZstdDecode(r io.Reader) (io.ReadCloser, error) {
return archives.Zstd{}.OpenReader(r)
}
func compressionDeflateDecode(r io.Reader) io.ReadCloser {
return flate.NewReader(r)
}
func compressionGzipDecode(r io.Reader) (io.ReadCloser, error) {
return gzip.NewReader(r)
}
func compressionZlibDecode(r io.Reader) (io.ReadCloser, error) {
return zlib.NewReader(r)
}
// compression decode
func CompressionDecode(r io.ReadCloser, encoding string) (io.ReadCloser, error) {
switch encoding {
case "br":
return compressionBrDecode(r)
case "deflate":
return compressionDeflateDecode(r), nil
case "gzip":
return compressionGzipDecode(r)
case "zlib":
return compressionZlibDecode(r)
case "zstd":
return compressionZstdDecode(r)
default:
return nil, nil
}
}
// bytes to string
func BytesToString(b []byte) string {
if len(b) == 0 {
return ""
}
return unsafe.String(&b[0], len(b))
}
// string to bytes
func StringToBytes(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}
var Rand = rand.New(rand.NewSource(time.Now().UnixMilli()))
var bidChars = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
var defaultAlphabet = []rune(bidChars)
var defaultAlphabetLen = len(defaultAlphabet)
// create naoid
func NaoId(l ...int) string {
var size int
if len(l) > 0 {
size = l[0]
} else {
size = 21
}
id := make([]rune, size)
for i := 0; i < size; i++ {
id[i] = defaultAlphabet[RanInt(0, defaultAlphabetLen)]
}
return string(id)
}
// create naoid with string
func NaoIdWithStr(val string, l ...int) string {
var size int
if len(l) > 0 {
size = l[0]
} else {
size = 21
}
alphabet := []rune(val)
alphabetLen := len(alphabet)
id := make([]rune, size)
for i := 0; i < size; i++ {
id[i] = alphabet[RanInt(0, alphabetLen)]
}
return string(id)
}
type bidclient struct {
bidMax int64
curNum int64
bidPid string
bidCharsILen int64
bidCharsFLen float64
bidChars string
curTime int64
lock sync.Mutex
}
func newBidClient() *bidclient {
bidCli := &bidclient{
bidMax: 78074896 - 1,
bidChars: bidChars,
}
bidCli.bidCharsILen = int64(len(bidCli.bidChars))
bidCli.bidCharsFLen = float64(len(bidCli.bidChars))
bidCli.bidPid = bidCli.bidEncode(int64(os.Getpid()), 4)
return bidCli
}
var bidClient = newBidClient()
type BonId struct {
Timestamp int64
Count int64
String string
}
func (obj *bidclient) bidEncode(num int64, lens ...int) string {
bytesResult := []byte{}
for num > 0 {
bytesResult = append(bytesResult, obj.bidChars[num%obj.bidCharsILen])
num = num / obj.bidCharsILen
}
for left, right := 0, len(bytesResult)-1; left < right; left, right = left+1, right-1 {
bytesResult[left], bytesResult[right] = bytesResult[right], bytesResult[left]
}
result := BytesToString(bytesResult)
if len(lens) == 0 {
return result
}
if len(result) < lens[0] {
res := bytes.NewBuffer(nil)
for i := len(result); i < lens[0]; i++ {
res.WriteString("0")
}
res.Write(bytesResult)
return res.String()
} else {
return result
}
}
func (obj *bidclient) bidDecode(str string) (int64, error) {
var num int64
n := len(str)
for i := 0; i < n; i++ {
pos := strings.IndexByte(obj.bidChars, str[i])
if pos == -1 {
return 0, errors.New("char error")
}
num += int64(math.Pow(obj.bidCharsFLen, float64(n-i-1)) * float64(pos))
}
return num, nil
}
func NewBonId() BonId {
bidClient.lock.Lock()
defer bidClient.lock.Unlock()
var result BonId
result.Timestamp = time.Now().Unix()
if bidClient.curTime != result.Timestamp {
bidClient.curTime = result.Timestamp
bidClient.curNum = -1
} else if bidClient.curNum >= bidClient.bidMax {
panic("too max num")
}
bidClient.curNum++
result.Count = bidClient.curNum
result.String = bidClient.bidEncode(result.Timestamp, 5) + bidClient.bidEncode(bidClient.curNum, 4) + bidClient.bidPid + NaoId(4)
return result
}
func BonIdFromString(val string) (BonId, error) {
var result BonId
if len(val) != 17 {
return result, errors.New("错误的字符串")
}
tt, err := bidClient.bidDecode(val[:5])
if err != nil {
return result, err
}
result.Timestamp = tt
tt, err = bidClient.bidDecode(val[5:9])
if err != nil {
return result, err
}
result.Count = tt
result.String = val
return result, err
}
func FreePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", ":0")
if err != nil {
return 0, err
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, err
}
func RanInt64(val, val2 int64) int64 {
if val == val2 {
return val
} else if val2 > val {
return val + Rand.Int63n(val2-val)
} else {
return val2 + Rand.Int63n(val-val2)
}
}
func RanInt(val, val2 int) int {
if val == val2 {
return val
} else if val2 > val {
return val + Rand.Intn(val2-val)
} else {
return val2 + Rand.Intn(val-val2)
}
}
func RanFloat64(val, val2 int64) float64 {
return float64(RanInt64(val, val2)) + Rand.Float64()
}
// :param point0: start
// :param point1: end
// :param control_point: contol
// :param point_nums: Generate the number of curve coordinate points. The more the number, the more uneven the graph will be, and the less the number, the smoother it will be
func GetTrack(point0, point1 [2]float64, point_nums float64) [][2]float64 {
x1, y1 := point1[0], point1[1]
abs_x := math.Abs(point0[0]-x1) / 2 //两点横坐标相减绝对值/2
abs_y := math.Abs(point0[1]-y1) / 2 //两点纵坐标相减绝对值/2
pointList := [][2]float64{}
cx, cy := (point0[0]+x1)/2+RanFloat64(int64(abs_x*-1), int64(abs_x)), (point0[1]+y1)/2+RanFloat64(int64(abs_y*-1), int64(abs_y))
var i float64
for i = 0; i < point_nums+1; i++ {
t := i / point_nums
x := math.Pow(1-t, 2)*point0[0] + 2*t*(1-t)*cx + math.Pow(t, 2)*x1
y := math.Pow(1-t, 2)*point0[1] + 2*t*(1-t)*cy + math.Pow(t, 2)*y1
pointList = append(pointList, [2]float64{x, y})
}
return pointList
}
// del slince with indexs
func DelSliceIndex[T any](val []T, indexs ...int) []T {
indexs = kinds.NewSet(indexs...).Array()
l := len(indexs)
switch l {
case 0:
return val
case 1:
return append(val[:indexs[0]], val[indexs[0]+1:]...)
default:
sort.Ints(indexs)
for i := l - 1; i >= 0; i-- {
val = DelSliceIndex(val, indexs[i])
}
return val
}
}
func DelSliceVals[T comparable](val []T, v T) []T {
for {
index := slices.Index(val, v)
if index == -1 {
return val
}
val = DelSliceIndex(val, index)
}
}
func DelSliceVal[T comparable](val []T, v T) []T {
index := slices.Index(val, v)
if index == -1 {
return val
}
return DelSliceIndex(val, index)
}
func WrapError(err error, val ...any) error {
return fmt.Errorf("%w,%s", err, fmt.Sprint(val...))
}
func CopyWitchContext(ctx context.Context, writer io.Writer, reader io.ReadCloser) (err error) {
if ctx == nil {
_, err = io.Copy(writer, reader)
if errors.Is(err, io.ErrUnexpectedEOF) {
err = nil
}
return
}
done := make(chan struct{})
go func() {
_, err = io.Copy(writer, reader)
if err == io.ErrUnexpectedEOF {
err = nil
}
close(done)
}()
select {
case <-ctx.Done():
if err == nil {
err = context.Cause(ctx)
}
case <-done:
}
return
}
func ImgDiffer(c, c2 []byte) (float64, error) {
img1, _, err := image.Decode(bytes.NewBuffer(c))
if err != nil {
return 0, err
}
img2, _, err := image.Decode(bytes.NewBuffer(c2))
if err != nil {
return 0, err
}
var score float64
bounds := img1.Bounds()
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
r1, g1, b1, _ := img1.At(x, y).RGBA()
r2, g2, b2, _ := img2.At(x, y).RGBA()
score += math.Pow(float64(r1)-float64(r2), 2)
score += math.Pow(float64(g1)-float64(g2), 2)
score += math.Pow(float64(b1)-float64(b2), 2)
}
}
score /= math.Pow(2, 16) * math.Pow(float64(bounds.Dx()), 2) * math.Pow(float64(bounds.Dy()), 2)
return score, nil
}
func AnyJoin[T any](values []T, sep string) string {
lls := make([]string, len(values))
for i, value := range values {
lls[i] = fmt.Sprint(value)
}
return strings.Join(lls, sep)
}
func ArraySet[T comparable](arr []T) []T {
set := []T{}
for _, val := range arr {
if !slices.Contains(set, val) {
set = append(set, val)
}
}
return set
}