-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
335 lines (309 loc) · 9.39 KB
/
config.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
package simple_admin
import (
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/casbin/casbin/v2"
"github.com/casbin/casbin/v2/model"
xormadapter "github.com/casbin/xorm-adapter"
"github.com/pkg/errors"
"log"
"reflect"
"strings"
"time"
)
// 默认配置文件
func (config *Config) initConfig() Config {
return Config{
Name: "simpleAdmin",
UserModel: new(UserModel),
RunSync: true,
PageSize: 20,
Prefix: "/admin",
InitAdminUserName: "admin",
InitAdminPassword: "iris_best",
UserModelSpecialUniqueName: "simple_admin_user_model",
AbridgeName: "sp",
EnableSpiderWatch: true,
SpiderMatchList: []string{"Baiduspider", "Googlebot", "YisouSpider", "HaosouSpider", "Bytespider", "360Spider", "bingbot", "Soso", "YisouSpider", "spider", "bot", "crawler", "Trident", "slurp", "craw"},
}
}
// 表内信息扫描
func (config *Config) scanTableInfo() {
var result []TableInfoList
// 判断是否启用爬虫监测
if config.EnableSpiderWatch {
config.ModelList = append(config.ModelList, new(SpiderHistory))
}
config.ModelList = append(config.ModelList, new(DashBoardScreen), new(DashBoard))
// 把用户模型合并到模型表格中
config.ModelList = append(config.ModelList, config.UserModel)
for _, item := range config.ModelList {
name := config.Engine.TableName(item)
cb, err := config.tableNameReflectFieldsAndTypes(name)
if err != nil {
panic(errors.Wrap(err, fmt.Sprintf("初始化扫描表:%s信息出错", name)))
}
var d TableInfoList
if processor, ok := item.(SpTableNameProcess); ok {
d.RemarkName = processor.SpAlias()
}
d.RouterName = name
d.FieldList = cb
d.Actions = config.validAction(item)
result = append(result, d)
}
config.modelInfoList = result
}
// 验证配置文件
func (config *Config) valid() error {
if config.Engine == nil {
return MsgLog("please check config , engine is empty")
}
if config.App == nil {
return MsgLog("please check config , app(iris instance application) is empty")
}
if reflect.DeepEqual(config.UserModel, new(UserModel)) == false {
log.Printf("custom user model warning : 1.must has username password salt id fields 2.username must be unique 3.id must be autoincr fields")
}
if len(config.ModelList) < 1 {
return MsgLog("please check config , modelList is empty ")
}
if len(config.Prefix) < 1 {
return MsgLog("please check config , prefix is required")
}
if config.Prefix[0] != '/' {
config.Prefix = "/" + config.Prefix
}
// 把爬虫队列内容全部转换为小写
if len(config.SpiderMatchList) >= 1 {
var r []string
for _, s := range config.SpiderMatchList {
r = append(r, strings.ToLower(s))
}
config.SpiderMatchList = r
}
if len(config.SpiderSkipList) >= 1 {
var r []string
for _, s := range config.SpiderSkipList {
r = append(r, strings.ToLower(s))
}
config.SpiderSkipList = r
}
return nil
}
// 合并验证自定义action
func (config *Config) validAction(item interface{}) []CustomAction {
var resultList []CustomAction
typ := reflect.TypeOf(item)
vtp := reflect.ValueOf(item)
for i := 0; i < vtp.NumMethod(); i++ {
m := vtp.Method(i)
tm := typ.Method(i)
if !strings.HasPrefix(tm.Name, "SpAction") {
continue
}
// 判断返回值是否正确
if tm.Type.NumOut() < 1 {
continue
}
out := tm.Type.Out(0)
if out.Kind() != reflect.Struct {
continue
}
result := m.Call(nil)[0]
actionBase := reflect.TypeOf(new(CustomAction))
if actionBase.Kind() == reflect.Ptr {
actionBase = actionBase.Elem()
}
var d = reflect.Indirect(reflect.ValueOf(new(CustomAction)))
for p := 0; p < actionBase.NumField(); p++ {
if reflect.Indirect(d.Field(p)).CanInterface() {
var n = actionBase.Field(p).Name
reflect.TypeOf(actionBase.Field(p))
d.FieldByName(n).Set(result.FieldByName(n))
}
}
var r = d.Interface().(CustomAction)
if reflect.Indirect(reflect.ValueOf(r.Func)).IsNil() {
name := config.Engine.TableName(item)
log.Printf("[%s]自定义%s执行方法验证错误", name, tm.Name)
continue
}
if len(r.Methods) < 1 {
r.Methods = "POST"
}
if len(r.Path) < 1 {
r.Path = "p_" + RandStringBytes(6)
}
r.Scope = item
r.hasValid = !reflect.ValueOf(r.Valid).IsNil()
resultList = append(resultList, r)
}
return resultList
}
// 配置文件初始化权限
func (config *Config) initCasBin() (*casbin.Enforcer, error) {
m := model.NewModel()
m.AddDef("r", "r", "sub, obj, act")
m.AddDef("p", "p", "sub, obj, act")
m.AddDef("g", "g", "_, _")
m.AddDef("e", "e", "some(where (p.eft == allow))")
m.AddDef("m", "m", `g(r.sub, p.sub) && keyMatch3(r.obj, p.obj) && (r.act == p.act || p.act == "*")`)
adapter, err := xormadapter.NewAdapterByEngine(config.Engine)
if err != nil {
log.Fatalf("initConfig by engine error %s", err)
return nil, err
}
Enforcer, err := casbin.NewEnforcer(m, adapter)
if err != nil {
log.Fatalf("initConfig to new enforcer error %s", err)
return nil, err
}
return Enforcer, nil
}
// 进行SYNC
func (config *Config) runSync() error {
var err error
if config.RunSync {
err = config.Engine.Sync2(config.ModelList...)
} else {
err = config.Engine.Sync2(config.UserModel, new(DashBoard), new(DashBoardScreen))
}
return err
}
// 通过表名匹配是否有自定义action
func (config *Config) tableNameCustomActionScopeMatch(routerName string) []CustomActionResp {
result := make([]CustomActionResp, 0)
for _, m := range config.modelInfoList {
if routerName == m.RouterName {
for _, action := range m.Actions {
var d CustomActionResp
d.Path = action.Path
d.Methods = action.Methods
d.Name = action.Name
values := config.tableNameGetNestedStructMaps(reflect.TypeOf(action.Valid))
d.Fields = values
result = append(result, d)
}
}
}
return result
}
// 模型表名序列生成
func (config *Config) generateTables() []string {
tables := make([]string, 0, len(config.modelInfoList))
for _, item := range config.modelInfoList {
tables = append(tables, item.RouterName)
}
return tables
}
// 通过模型反射模型信息
func (config *Config) tableNameReflectFieldsAndTypes(tableName string) (TableFieldsResp, error) {
for _, item := range config.ModelList {
if config.Engine.TableName(item) == tableName {
modelInfo, err := config.Engine.TableInfo(item)
if err != nil {
return TableFieldsResp{}, nil
}
var resp TableFieldsResp
// 获取三要素
values := config.tableNameGetNestedStructMaps(reflect.TypeOf(item))
resp.Fields = values
resp.AutoIncrement = modelInfo.AutoIncrement
resp.Version = modelInfo.Version
resp.Deleted = modelInfo.Deleted
resp.Created = modelInfo.Created
resp.Updated = modelInfo.Updated
return resp, nil
}
}
return TableFieldsResp{}, MsgLog(fmt.Sprintf("not find this table %s", tableName))
}
// 通过模型名获取所有列信息 名称 类型 xorm tag validator comment
func (config *Config) tableNameGetNestedStructMaps(r reflect.Type) []structInfo {
if r.Kind() == reflect.Ptr {
r = r.Elem()
}
if r.Kind() != reflect.Struct {
return nil
}
v := reflect.New(r).Elem()
result := make([]structInfo, 0)
for i := 0; i < r.NumField(); i++ {
field := r.Field(i)
v := reflect.Indirect(v).FieldByName(field.Name)
fieldValue := v.Interface()
var d structInfo
switch fieldValue.(type) {
case time.Time, time.Duration:
d.Name = field.Name
d.Types = field.Type.String()
d.XormTags = field.Tag.Get("xorm")
d.SpTags = field.Tag.Get(config.AbridgeName)
d.ValidateTags = field.Tag.Get("validate")
d.CommentTags = field.Tag.Get("comment")
d.AttrTags = field.Tag.Get("attr")
d.MapName = config.Engine.GetColumnMapper().Obj2Table(field.Name)
result = append(result, d)
continue
}
if field.Type.Kind() == reflect.Struct {
values := config.tableNameGetNestedStructMaps(field.Type)
result = append(result, values...)
continue
}
d.Name = field.Name
d.Types = field.Type.String()
d.MapName = config.Engine.GetColumnMapper().Obj2Table(field.Name)
d.XormTags = field.Tag.Get("xorm")
d.SpTags = field.Tag.Get(config.AbridgeName)
d.CommentTags = field.Tag.Get("comment")
d.AttrTags = field.Tag.Get("attr")
d.ValidateTags = field.Tag.Get("validate")
result = append(result, d)
}
return result
}
// 通过模型名获取实例
func (config *Config) tableNameGetModel(tableName string) (interface{}, error) {
for _, item := range config.ModelList {
if config.Engine.TableName(item) == tableName {
return item, nil
}
}
return nil, MsgLog("not find table")
}
// 通过模型名获取模型信息
func (config *Config) tableNameGetModelInfo(tableName string) (TableInfoList, error) {
for _, l := range config.modelInfoList {
if l.RouterName == tableName {
return l, nil
}
}
return TableInfoList{}, errors.New("not found model")
}
// 获取用户表
func (config *Config) getUserModelTableName() string {
tableName := config.Engine.TableName(config.UserModel)
return tableName
}
// 密码加密
func (config *Config) passwordSalt(password string) (string, string) {
salt := RandStringBytes(4)
m5 := md5.New()
m5.Write([]byte(password))
m5.Write([]byte(salt))
st := m5.Sum(nil)
ps := hex.EncodeToString(st)
return ps, salt
}
// 密码比较
func (config *Config) validPassword(password, salt, m5 string) bool {
r := md5.New()
r.Write([]byte(password))
r.Write([]byte(salt))
st := r.Sum(nil)
ps := hex.EncodeToString(st)
return ps == m5
}