This repository has been archived by the owner on Feb 11, 2022. It is now read-only.
/
config.go
executable file
·95 lines (79 loc) · 2.04 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
package controllers
import (
"encoding/json"
"yulong-hids/web/models"
"yulong-hids/web/settings"
"yulong-hids/web/utils"
"github.com/astaxie/beego"
"gopkg.in/mgo.v2/bson"
)
// ConfigController /config
type ConfigController struct {
BaseController
}
// Get HTTP method GET
func (c *ConfigController) Get() {
cli := models.NewConfig()
json := cli.GetAll()
for _, item := range json {
if item["type"] == "server" {
dic := item["dic"].(bson.M)
for _, secretkey := range settings.SecretKeyLst {
dic[secretkey] = utils.Md5String(dic[secretkey].(string))
}
}
}
c.Data["json"] = json
c.ServeJSON()
return
}
// Edit HTTP method POST
func (c *ConfigController) Edit() {
cli := models.NewConfig()
j := models.EditCfgForm{}
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &j); err != nil {
beego.Debug("Config edit error:", err)
c.Data["json"] = models.NewErrorInfo(settings.EditCfgFailure)
c.ServeJSON()
return
}
res := cli.EditByID(j.Id, j.Key, j.Input)
c.Data["json"] = bson.M{"status": res}
c.ServeJSON()
return
}
// Del HTTP method DELETE
func (c *ConfigController) Del() {
cli := models.NewConfig()
j := models.EditCfgForm{}
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &j); err != nil {
beego.Debug("Config edit error:", err)
c.Data["json"] = models.NewErrorInfo(settings.EditCfgFailure)
c.ServeJSON()
return
}
res := cli.DelOne(j.Id, j.Key, j.Input)
c.Data["json"] = bson.M{"status": res}
c.ServeJSON()
return
}
// Add HTTP method PUT
func (c *ConfigController) Add() {
cli := models.NewConfig()
j := models.EditCfgForm{}
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &j); err != nil {
beego.Debug("Config add error:", err)
c.Data["json"] = models.NewErrorInfo(settings.EditCfgFailure)
c.ServeJSON()
return
}
// support blacklist and whitelist convenient tag
if j.Id == "blacklist" || j.Id == "whitelist" {
config := cli.FindOne(bson.M{"type": j.Id})
j.Id = config.Id.Hex()
}
res := cli.AddOne(j.Id, j.Key, j.Input)
c.Data["json"] = bson.M{"status": res}
c.ServeJSON()
return
}