/
menu.go
180 lines (152 loc) · 4.67 KB
/
menu.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
package yakit
import (
"encoding/json"
"github.com/jinzhu/gorm"
"github.com/yaklang/yaklang/common/utils"
"github.com/yaklang/yaklang/common/yak/yaklib/codec"
"github.com/yaklang/yaklang/common/yakgrpc/ypb"
"strconv"
)
type MenuItem struct {
gorm.Model
Group string `json:"group" `
Verbose string `json:"verbose"`
YakScriptName string `json:"yak_script_name"`
Hash string `json:"-" gorm:"unique_index"`
// quoted json
BatchPluginFilterJson string `json:"batch_plugin_filter_json"`
Mode string `json:"mode"`
MenuSort int64 `json:"menu_sort"`
GroupSort int64 `json:"group_sort"`
}
/*
{"group":"12312","name":"aaa","query":{"type":"mitm,port-scan,nuclei","tags":"","include":["ElasticSearch 未授权访问","[wptouch-open-redirect]: WPTouch Switch Desktop 3.x Open Redirection","[wpmudev-pub-keys]: Wpmudev Dashboard Pub Key","[wpdm-cache-session]: Wpdm-Cache Session"],"exclude":[]}}
*/
type batchExecutionSchema struct {
Group string `json:"group"`
Name string `json:"name"`
Query batchExecutionSchemaFilter `json:"query"`
}
type batchExecutionSchemaFilter struct {
Types string `json:"type"`
Tags string `json:"tags"`
Include []string `json:"include"`
Exclude []string `json:"exclude"`
}
func NewMenuItemByBatchExecuteConfig(raw interface{}) (*MenuItem, error) {
jsonBody := utils.InterfaceToBytes(raw)
var schema batchExecutionSchema
err := json.Unmarshal(jsonBody, &schema)
if err != nil {
return nil, utils.Errorf("cannot load json to (menuItem): %s", err)
}
queryFilter, err := json.Marshal(schema.Query)
if err != nil {
return nil, utils.Errorf("loading query filter failed: %s", err)
}
item := &MenuItem{
Group: schema.Group,
Verbose: schema.Name,
BatchPluginFilterJson: strconv.Quote(string(queryFilter)),
}
return item, nil
}
func (m *MenuItem) CalcHash() string {
key := m.Verbose
if key == "" {
key = m.YakScriptName
}
if key == "" {
key = codec.Sha256(m.BatchPluginFilterJson)
}
return utils.CalcSha1(m.Group, m.Mode, key)
}
func (m *MenuItem) BeforeSave() error {
if m.Group == "" {
m.Group = "UserDefined"
}
m.Hash = m.CalcHash()
return nil
}
func CreateOrUpdateMenuItem(db *gorm.DB, hash string, i interface{}) error {
db = UserDataAndPluginDatabaseScope(db)
db = db.Model(&MenuItem{})
if db := db.Where("hash = ?", hash).Assign(i).FirstOrCreate(&MenuItem{}); db.Error != nil {
return utils.Errorf("create/update MenuItem failed: %s", db.Error)
}
return nil
}
func GetMenuItemById(db *gorm.DB, id int64) (*MenuItem, error) {
db = UserDataAndPluginDatabaseScope(db)
var req MenuItem
if db := db.Model(&MenuItem{}).Where("id = ?", id).First(&req); db.Error != nil {
return nil, utils.Errorf("get MenuItem failed: %s", db.Error)
}
return &req, nil
}
func DeleteMenuItemByID(db *gorm.DB, id int64) error {
db = UserDataAndPluginDatabaseScope(db)
if db := db.Model(&MenuItem{}).Where(
"id = ?", id,
).Unscoped().Delete(&MenuItem{}); db.Error != nil {
return db.Error
}
return nil
}
func DeleteMenuItem(db *gorm.DB, group string, name string, mode string) error {
db = UserDataAndPluginDatabaseScope(db)
db = db.Model(&MenuItem{}).Where(
"`group` = ? AND yak_script_name = ?", group, name,
)
if mode != "" {
db = db.Where("mode = ?", mode)
}
db = db.Unscoped().Delete(&MenuItem{})
if db.Error != nil {
return db.Error
}
return nil
}
func DeleteMenuItemAll(db *gorm.DB) error {
db = UserDataAndPluginDatabaseScope(db)
if db := db.Model(&MenuItem{}).Where(
"true",
).Unscoped().Delete(&MenuItem{}); db.Error != nil {
return db.Error
}
return nil
}
func GetAllMenuItem(db *gorm.DB) []*MenuItem {
db = UserDataAndPluginDatabaseScope(db)
var items []*MenuItem
if db := db.Model(&MenuItem{}).Where("true").Find(&items); db.Error != nil {
return nil
}
return items
}
func GetMenuItem(db *gorm.DB, group string, name string) (*MenuItem, error) {
db = UserDataAndPluginDatabaseScope(db)
var req MenuItem
if db := db.Model(&MenuItem{}).Where("`group` = ? AND yak_script_string = ?", group, name).First(&req); db.Error != nil {
return nil, utils.Errorf("get MenuItem failed: %s", db.Error)
}
return &req, nil
}
func QueryAllMenuItemByWhere(db *gorm.DB, req *ypb.QueryAllMenuItemRequest) []*MenuItem {
var items []*MenuItem
db = UserDataAndPluginDatabaseScope(db)
db = db.Model(&MenuItem{})
if req.Mode != "" {
db = db.Where("mode = ?", req.Mode)
} else {
db = db.Where("mode IS NULL OR mode = '' ")
}
if req.Group != "" {
db = db.Where("`group` = ?", req.Group)
}
db = db.Order("menu_sort, group_sort asc ").Scan(&items)
if db.Error != nil {
return nil
}
return items
}