-
Notifications
You must be signed in to change notification settings - Fork 81
/
util.go
158 lines (136 loc) · 3.09 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
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"math/rand"
"os"
"path"
"strings"
"sync"
)
// 代码来自: goproxy
func readJsonConfig(filename string, config interface{}) error {
fileext := path.Ext(filename)
filename1 := strings.TrimSuffix(filename, fileext) + ".user" + fileext
cm := make(map[string]interface{})
for i, name := range []string{filename, filename1} {
data, err := os.ReadFile(name)
if err != nil {
if i == 0 {
return err
} else {
continue
}
}
data = bytes.TrimPrefix(data, []byte("\xef\xbb\xbf"))
data, err = readJson(bytes.NewReader(data))
if err != nil {
return err
}
cm1 := make(map[string]interface{})
d := json.NewDecoder(bytes.NewReader(data))
d.UseNumber()
if err = d.Decode(&cm1); err != nil {
return err
}
if err = mergeMap(cm, cm1); err != nil {
return err
}
}
data, err := json.Marshal(cm)
if err != nil {
return err
}
d := json.NewDecoder(bytes.NewReader(data))
d.UseNumber()
return d.Decode(config)
}
func readJson(r io.Reader) ([]byte, error) {
scanner := bufio.NewScanner(r)
var b bytes.Buffer
prev := ""
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "//") {
continue
}
if strings.HasPrefix(line, "}") || strings.HasPrefix(line, "]") {
prev = strings.TrimSuffix(prev, ",")
}
b.WriteString(prev)
prev = line
}
b.WriteString(prev)
return b.Bytes(), scanner.Err()
}
func mergeMap(m1 map[string]interface{}, m2 map[string]interface{}) error {
for key, value := range m2 {
m1v, m1_has_key := m1[key]
m2v, m2v_is_map := value.(map[string]interface{})
m1v1, m1v_is_map := m1v.(map[string]interface{})
switch {
case !m1_has_key, !m2v_is_map:
m1[key] = value
case !m1v_is_map:
return fmt.Errorf("m1v=%#v is not a map, but m2v=%#v is a map", m1v, m2v)
default:
mergeMap(m1v1, m2v)
}
}
return nil
}
func randInt(l, u int) int {
return rand.Intn(u-l) + l
}
func randomChoice[T any](a []T) T {
return a[rand.Intn(len(a))]
}
// 生成两段或三段的随机字符串当作 host
// llm.xadl
// unupk.bfrf.pvi
func randomHost() string {
a := make([][]byte, randInt(2, 4))
for i := range a {
m := randInt(3, 7)
b := make([]byte, m)
for j := 0; j < m; j++ {
b[j] = byte(randInt(97, 122))
}
a[i] = b
}
return string(bytes.Join(a, []byte{46}))
}
func or[T comparable](vals ...T) T {
var zero T
for _, val := range vals {
if val != zero {
return val
}
}
return zero
}
// pathExist 返回文件或文件夹是否存在
func pathExist(name string) bool {
_, err := os.Stat(name)
return !os.IsNotExist(err)
}
func ops(count, threads int, op func(i, thread int)) {
var wg sync.WaitGroup
wg.Add(threads)
for i := 0; i < threads; i++ {
s, e := count/threads*i, count/threads*(i+1)
if i == threads-1 {
e = count
}
go func(i, s, e int) {
for j := s; j < e; j++ {
op(j, i)
}
wg.Done()
}(i, s, e)
}
wg.Wait()
}