/
tool.go
239 lines (230 loc) · 5.94 KB
/
tool.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
package weSubDatabase
import (
"fmt"
"math"
"regexp"
"strconv"
"time"
)
// ===============
//
// 加密主键, *Setting.SEKey 为 nil 时不加密
// queryDatas []map[string]string "查询结果"
// primaryKey string "主键字段名
// 为空字符串时不加密"
// return []map[string]string "查询结果"
//
// ===============
//
// Encrypt primary key, *Setting.SEKey is not encrypted when nil
// queryDatas []map[string]string "query result"
// primaryKey string "primary key field name
// When it is an empty string,
// it is not encrypted"
// return []map[string]string query result
func (s *Setting) EncryptPrimaryKey(queryDatas []map[string]string, primaryKey string) []map[string]string {
if s.SEKey == nil {
return queryDatas
}
if primaryKey == "" {
return queryDatas
}
for i := 0; i < len(queryDatas); i++ {
idStr := queryDatas[i][primaryKey]
if idStr == "" {
continue
}
dbI := queryDatas[i]["db"]
if dbI == "" {
continue
}
queryDatas[i][primaryKey] = s.SEKey.Encrypt(idStr, dbI)
delete(queryDatas[i], "db")
}
return queryDatas
}
// ===============
//
// 根据主键和id数组解密出数据库ID和主键ID
// primaryKey string "主键字段名"
// ids []string "主键ID数组"
// return 1 []bool "解密出的ID是否有对应的数据库"
// return 2 [][]string "解密出的数据库ID"
// return 3 [][]int "解密出的主键ID在原数组中的位置"
//
// ===============
//
// Decrypt the database ID and primary key ID based on the primary key and ID array
// primaryKey string "primary key field name"
// ids []string "primary key ID array"
// return 1 []bool "Whether the decrypted ID has a
// corresponding database"
// return 2 [][]string "Decrypted database ID"
// return 3 [][]int "The position of the decrypted
// primary key ID in the original
// array"
func (s *Setting) DecryptID(primaryKey string, ids []string) ([]bool, [][]string, [][]int) {
var (
dbIList []bool = []bool{}
idList [][]string = [][]string{}
pwList [][]int = [][]int{}
)
for i := 0; i < len(s.SqlConfigs); i++ {
dbIList = append(dbIList, false)
idList = append(idList, []string{})
pwList = append(pwList, []int{})
}
for i, v := range ids {
id, dbI, err := s.SEKey.Decrypt(v)
if err != nil {
continue
}
dbIint, err := strconv.Atoi(dbI)
if err != nil {
continue
}
dbIList[dbIint] = true
idList[dbIint] = append(idList[dbIint], id)
pwList[dbIint] = append(pwList[dbIint], i)
}
return dbIList, idList, pwList
}
// ===============
//
// 判断是否可以连接数据库
// item int "需要连接的数据库在配置中的位置"
// return 1 bool "是否可以尝试连接"
//
// ===============
//
// Determine whether the database can be connected
// item int "Location of the database to be
// connected in the configuration"
// return 1 bool "Whether to try to connect"
func (s *Setting) IsRetryConnect(item int) bool {
if s.ConnectFailTime[item] != nil {
tn := time.Now()
tend := s.ConnectFailTime[item].Add(time.Millisecond * time.Duration(s.ConnectAgainTime))
if tn.After(tend) {
s.ConnectFailTime[item] = nil
return true
} else {
return false
}
}
return true
}
// ===============
//
// 根据下一个数据库ID和限制数目生成限制字符串
// nextDBID int "下一个数据库ID"
// pageNumber string "页码"
// limit int "限制数目"
// isContinues []bool "是否可以连接数据库"
// return []string "限制字符串数组"
//
// ===============
//
// Generate limit string based on next database ID and limit number
// nextDBID int "Next database ID"
// pageNumber string "Page number"
// limit int "Limit number"
// isContinues []bool "Whether the database can be
// connected"
// return []string "Limit string array"
func toLimit(nextDBID int, pageNumber string, limit int, isContinues []bool) []string {
if limit <= 0 {
return nil
}
limitList := []string{}
linkDBCount := 0
for i := 0; i < len(isContinues); i++ {
limitList = append(limitList, "")
if isContinues[i] {
linkDBCount += 1
}
}
maxDBNum := len(isContinues)
f := math.Floor(float64(limit) / float64(linkDBCount))
remainder := limit % linkDBCount
ii := 0
for i := nextDBID; i < maxDBNum; i++ {
if !isContinues[i] {
if i+1 >= maxDBNum {
i = -1
}
ii++
if ii >= maxDBNum {
break
}
continue
}
limit := ""
if pageNumber != "" {
limit = pageNumber + ","
}
if remainder > 0 {
limit += fmt.Sprintf("%d", int(f)+1)
remainder -= 1
} else {
limit += fmt.Sprintf("%d", int(f))
}
limitList[i] = limit
if i+1 >= maxDBNum {
i = -1
}
ii++
if ii >= maxDBNum {
break
}
}
return limitList
}
// 正则表达式
//
// Regular expression
var regexpString string = ``
// 默认正则表达式
//
// Default regular expression
const defaultRegexpString string = `([\x00-\x08\x0B-\x0C\x0E-\x1F\x7F]+)|(?:')|(?:\\)|(?:--)|(\b(\\x|�|select|update|and|or|delete|insert|trancate|char|chr|into|substr|ascii|declare|exec|count|master|into|drop|execute)\b)`
// ===============
//
// 设置正则表达式
//
// regexpStr string "正则表达式
// 空字符串时修改为默认值"
//
// ===============
//
// Set regular expression
//
// regexpStr string "Regular expression
// When the string is empty, it is default"
func SetRegexpString(regexpStr string) {
regexpString = regexpStr
}
// ===============
//
// 检查字符串中是否有linux下的非法字符和SQL注入
//
// "
//
// str string "需要检查的字符串"
// return bool "是否有非法字符"
//
// ===============
//
// Check whether there are illegal characters and SQL injection under Linux in the string
//
// str string "String to be checked"
// return bool "Whether there are illegal characters"
func CheckString(str string) []string {
var re *regexp.Regexp
if regexpString == "" {
re = regexp.MustCompile(defaultRegexpString)
} else {
re = regexp.MustCompile(regexpString)
}
return re.FindAllString(str, -1)
}