diff options
| -rw-r--r-- | cmd/root.go | 7 | ||||
| -rw-r--r-- | comm/commflush.go | 29 | ||||
| -rw-r--r-- | comm/commflush_test.go | 21 | ||||
| -rw-r--r-- | comm/commget.go | 4 | ||||
| -rw-r--r-- | model/comm_model.go | 69 | ||||
| -rw-r--r-- | model/comm_model_test.go | 19 | ||||
| -rw-r--r-- | model/json_marshal.go | 5 | ||||
| -rw-r--r-- | router/comm_endpoint.go | 34 | ||||
| -rw-r--r-- | router/endpoint.go | 141 | ||||
| -rw-r--r-- | router/endpoint_test.go | 26 | ||||
| -rw-r--r-- | router/router.go | 18 | ||||
| -rw-r--r-- | router/rule_endpoint.go | 137 |
12 files changed, 299 insertions, 211 deletions
diff --git a/cmd/root.go b/cmd/root.go index 5612915..3c93ff0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,7 +1,7 @@ /* * @Author: EnderByEndera * @Date: 2020-12-19 11:59:02 - * @LastEditTime: 2021-01-06 09:51:43 + * @LastEditTime: 2021-01-15 07:16:12 * @LastEditors: Please set LastEditors * @Description: root of the commdetection cmd * @FilePath: /commdetection/cmd/root.go @@ -89,8 +89,11 @@ func root() { go func() { for { - comms := comm.GetCommands() + var comms model.Commands + comms = comm.GetCommands() comms = comm.FlushCommands(comms, filters) + comms.InsertAllTo("test", "commands") + comms.GetCommandsFrom("test", "commands") css := rules.InitCommScores(comms) css = rules.EvaluateCommScore(css, rs) jsonBuf, _ := json.Marshal(css) diff --git a/comm/commflush.go b/comm/commflush.go index cf88d8f..0935462 100644 --- a/comm/commflush.go +++ b/comm/commflush.go @@ -1,7 +1,7 @@ /* * @Author: EnderByEndera * @Date: 2020-12-07 09:22:53 - * @LastEditTime: 2021-01-05 13:27:31 + * @LastEditTime: 2021-01-14 15:31:24 * @LastEditors: Please set LastEditors * @Description: this file flushes invalid commands using various types of filters * @FilePath: /commdetection/commflush.go @@ -15,7 +15,6 @@ import ( "errors" "os/exec" "regexp" - "sort" "time" ) @@ -28,13 +27,6 @@ const MANENTRYREG = "No manual entry for (.*)" // Filter defines filter function to clean invalid commadns or ops given by file or net type Filter func([]model.Command) []model.Command -//IntSlice includes a list of n array in order to use sort.Sort() method to sort n -type IntSlice []int - -func (s IntSlice) Len() int { return len(s) } -func (s IntSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } -func (s IntSlice) Less(i, j int) bool { return s[i] > s[j] } - type errorNum struct { err error num int @@ -48,23 +40,6 @@ func FlushCommands(commands []model.Command, filters []Filter) []model.Command { return commands } -// removeOneCommand removes only one command from the list given the index -func removeOneCommand(commands []model.Command, n int) []model.Command { - return append(commands[:n], commands[n+1:]...) -} - -// removeCommands removes a list of commands using index -func removeCommands(commands []model.Command, n IntSlice) []model.Command { - if len(n) == 0 { - return commands - } - sort.Sort(n) - for i := range n { - commands = removeOneCommand(commands, n[i]) - } - return commands -} - // SimpleCommandFilter tries to run command without any flag to see whether this command can be run properly // Deprecated: use WhichCommandFilter instead func SimpleCommandFilter(commands []model.Command) []model.Command { @@ -144,5 +119,5 @@ func cmdsFilter(commands []model.Command, execute func(string) error) []model.Co invalidNum = append(invalidNum, e.num) } } - return removeCommands(commands, invalidNum) + return model.RemoveCommands(commands, invalidNum) } diff --git a/comm/commflush_test.go b/comm/commflush_test.go index fea14f8..3363003 100644 --- a/comm/commflush_test.go +++ b/comm/commflush_test.go @@ -1,7 +1,7 @@ /* * @Author: your name * @Date: 2020-12-03 12:51:28 - * @LastEditTime: 2021-01-06 10:01:49 + * @LastEditTime: 2021-01-16 08:41:14 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: /commdetection/comm/commflush_test.go @@ -80,6 +80,18 @@ func TestFlushCommands(t *testing.T) { t.Errorf("results are not as predicted") } }) + + t.Run("FlushCommands with mongodb", func(t *testing.T) { + comms := GetCommands() + comms = FlushCommands(comms, []Filter{WhichCommandFilter}) + commands := model.Commands(comms) + err := commands.InsertAllTo("test", "commands") + err = commands.InsertAllTo("test", "commands") + if err != nil { + t.Error(err) + } + + }) } func BenchmarkFlushCommands(b *testing.B) { @@ -91,3 +103,10 @@ func BenchmarkFlushCommands(b *testing.B) { b.StopTimer() }) } + +func BenchmarkInsertFromMongo(b *testing.B) { + commands := GetCommands() + commands = FlushCommands(commands, []Filter{WhichCommandFilter}) + commands.InsertAllTo("test", "commands") + commands.InsertAllTo("test", "commands") +} diff --git a/comm/commget.go b/comm/commget.go index f2e4d26..d628c3e 100644 --- a/comm/commget.go +++ b/comm/commget.go @@ -1,7 +1,7 @@ /* * @Author: EnderByEndera * @Date: 2020-12-02 17:08:59 - * @LastEditTime: 2021-01-10 09:49:55 + * @LastEditTime: 2021-01-16 08:39:11 * @LastEditors: Please set LastEditors * @Description: Get commands from file or network * @FilePath: /commdetection/preprocessing/commget.go @@ -22,7 +22,7 @@ import ( ) // GetCommands returns a list of commands preprocessed which first get commands from file then net -func GetCommands() []model.Command { +func GetCommands() model.Commands { commands, err := getCommandsFromHist() if err != nil { logger.Warnln(err) diff --git a/model/comm_model.go b/model/comm_model.go index f16f667..f89b6c5 100644 --- a/model/comm_model.go +++ b/model/comm_model.go @@ -1,7 +1,7 @@ /* * @Author: your name * @Date: 2021-01-06 09:56:18 - * @LastEditTime: 2021-01-12 16:49:24 + * @LastEditTime: 2021-01-19 12:02:23 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: /commdetection/model/comm_model.go @@ -20,6 +20,13 @@ import ( "go.mongodb.org/mongo-driver/mongo" ) +//IntSlice includes a list of n array in order to use sort.Sort() method to sort n +type IntSlice []int + +func (s IntSlice) Len() int { return len(s) } +func (s IntSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } +func (s IntSlice) Less(i, j int) bool { return s[i] > s[j] } + // Command contains command and its flags or symbols type Command struct { CommName string `json:"commname" bson:"commname"` @@ -56,6 +63,23 @@ func (c Commands) Has(command Command) bool { return true } +// removeOneCommand removes only one command from the list given the index +func removeOneCommand(commands []Command, n int) []Command { + return append(commands[:n], commands[n+1:]...) +} + +// RemoveCommands removes a list of commands using index +func RemoveCommands(commands []Command, n IntSlice) []Command { + if len(n) == 0 { + return commands + } + sort.Sort(n) + for i := range n { + commands = removeOneCommand(commands, n[i]) + } + return commands +} + // GetCommandsFrom gets all the commands in the mongodb collections func (c *Commands) GetCommandsFrom(dbName string, cName string) error { return mongoOpsWithoutIndex(getCommandsFromFn, opParams{ @@ -73,10 +97,10 @@ func getCommandsFromFn(sc mongo.SessionContext) error { } collection := client.Database(params.dbName).Collection(params.cName) cur, err := collection.Find(sc, bson.D{}) - defer cur.Close(sc) if err != nil { return err } + defer cur.Close(sc) for cur.Next(sc) { var next Command err := cur.Decode(&next) @@ -103,16 +127,34 @@ func insertAllCommandsToFn(sc mongo.SessionContext) error { if !ok { return fmt.Errorf("Error tranfering the params") } + if params.commands.Len() == 0 { + return nil + } + + var deleteIndexes []int + for i := 0; i < params.commands.Len()-1; i++ { + if reflect.DeepEqual((*params.commands)[i], (*params.commands)[i+1]) { + deleteIndexes = append(deleteIndexes, i) + } + } + *params.commands = RemoveCommands(*params.commands, deleteIndexes) collections := client.Database(params.dbName).Collection(params.cName) var documents []interface{} for _, command := range *params.commands { - documents = append(documents, command) + // if the command is not found in the mongodb + if res := collections.FindOne(sc, command); res.Err() == mongo.ErrNoDocuments { + // add the command to the ready-inserted documents + documents = append(documents, command) + } + } + // If every command is inserted into the db, documents may be nil + if documents == nil { + return nil } - res, err := collections.InsertMany(sc, documents) + _, err := collections.InsertMany(sc, documents) if err != nil { return err } - logger.Debugln("Insert succeeded, Ids are ", res) return nil } @@ -133,11 +175,13 @@ func insertAnyCommandToFn(sc mongo.SessionContext) error { return fmt.Errorf("Error transfering the params") } collections := client.Database(params.dbName).Collection(params.cName) - res, err := collections.InsertOne(sc, (*params.commands)[int(params.index)]) + if res := collections.FindOne(sc, (*params.commands)[int(params.index)]); res.Err() != mongo.ErrNoDocuments { + return nil + } + _, err := collections.InsertOne(sc, (*params.commands)[int(params.index)]) if err != nil { return err } - logger.Debugln("Insert succeeded, Id is ", res) return nil } @@ -160,14 +204,17 @@ func updateAnyCommandFn(sc mongo.SessionContext) error { } collection := client.Database(params.dbName).Collection(params.cName) command := (*params.commands)[int(params.index)] - res, err := collection.UpdateOne(sc, params.updateFilter, bson.D{{ + // if the command is found in the mongodb, update will be useless, so return nil + if res := collection.FindOne(sc, command); res.Err() != mongo.ErrNoDocuments { + return nil + } + _, err := collection.UpdateOne(sc, params.updateFilter, bson.D{{ "$set", command, }}) if err != nil { return err } - logger.Debugln("update succeeded, id is ", res) return nil } @@ -188,11 +235,10 @@ func deleteOneCommandFromFn(sc mongo.SessionContext) error { return fmt.Errorf("Error transfering the params") } collections := client.Database(params.dbName).Collection(params.cName) - res, err := collections.DeleteOne(sc, (*params.commands)[int(params.index)]) + _, err := collections.DeleteOne(sc, (*params.commands)[int(params.index)]) if err != nil { return err } - logger.Debugln("Delete succeeded, Id is ", res) return nil } @@ -220,6 +266,5 @@ func deleteAllCommandsFromFn(sc mongo.SessionContext) error { } deleteResults = append(deleteResults, res) } - logger.Debugln("Delete succeeded, ids are ", deleteResults) return nil } diff --git a/model/comm_model_test.go b/model/comm_model_test.go index b0f4e79..a3a97ff 100644 --- a/model/comm_model_test.go +++ b/model/comm_model_test.go @@ -1,7 +1,7 @@ /* * @Author: EnderByEndera * @Date: 2021-01-06 15:26:48 - * @LastEditTime: 2021-01-12 16:49:55 + * @LastEditTime: 2021-01-19 11:57:21 * @LastEditors: Please set LastEditors * @Description: test mongo.go * @FilePath: /commdetection/model/mongo_test.go @@ -10,6 +10,7 @@ package model import ( + "reflect" "testing" "time" @@ -30,19 +31,31 @@ func TestGetCommandsFromMongo(t *testing.T) { func TestInsertCommandsToMongo(t *testing.T) { t.Run("Test InsertManyTo func", func(t *testing.T) { commands := Commands{} + now := time.Now() for i := 0; i < 10; i++ { commands = append(commands, Command{ CommName: "wget", Args: []string{}, Flags: []string{}, - TimeStamp: time.Now(), + TimeStamp: now, User: "root", + Mac: "25:21:4A:1A:4C:11", }) } err := commands.InsertAllTo("test", "commands") if err != nil { t.Error(err) } + commands.InsertAllTo("test", "commands") + if err != nil { + t.Error(err) + } + commands.GetCommandsFrom("test", "commands") + for i := 0; i < commands.Len()-1; i++ { + if reflect.DeepEqual(commands[i], commands[i+1]) { + t.Error("commands has same data") + } + } }) t.Run("Test InsertAnyTo func", func(t *testing.T) { @@ -76,6 +89,8 @@ func TestUpdateCommandsToMongo(t *testing.T) { User: "root", }, } + _ = commands.InsertAnyTo("test", "commands", 0) + err := commands.UpdateAnyTo("test", "commands", 0, bson.D{{"commname", "wget"}}) if err != nil { t.Error(err) diff --git a/model/json_marshal.go b/model/json_marshal.go index 6fd4590..83f53a4 100644 --- a/model/json_marshal.go +++ b/model/json_marshal.go @@ -1,7 +1,7 @@ /* * @Author: your name * @Date: 2021-01-05 10:37:28 - * @LastEditTime: 2021-01-10 09:39:58 + * @LastEditTime: 2021-01-14 09:32:07 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: /commdetection/model/marshal_and_unmarshal.go @@ -21,7 +21,6 @@ import ( "commdetection/logger" "commdetection/yaml" "encoding/json" - "fmt" "io/ioutil" "os" "path/filepath" @@ -65,7 +64,7 @@ func UnmarshalEvaluationRules() (r Rules, err error) { } err = unmarshalSetting(fileName, r) if err != nil { - fmt.Printf("json file settings conversion to %s failed, please check json file %s is correct or not", reflect.TypeOf(r).Name(), fileName) + logger.Warnf("json file settings conversion to %s failed, please check json file %s is correct or not", reflect.TypeOf(r).Name(), fileName) } return } diff --git a/router/comm_endpoint.go b/router/comm_endpoint.go new file mode 100644 index 0000000..1054b5f --- /dev/null +++ b/router/comm_endpoint.go @@ -0,0 +1,34 @@ +/* + * @Author: EnderByEndera + * @Date: 2021-01-16 08:14:22 + * @LastEditTime: 2021-01-16 08:16:11 + * @LastEditors: Please set LastEditors + * @Description: comm_endpoint.go has the function for all the commands' endpoint + * @FilePath: /commdetection/router/comm_endpoint.go + */ +package router + +import ( + "net/http" + "path/filepath" + + "github.com/gin-gonic/gin" +) + +// uploadCommEndpoint uploads the file storing commands to the system +func uploadCommEndpoint(c *gin.Context) { + commfile, err := c.FormFile("commfile") + if err != nil { + handleErr(c, err) + return + } + + filename := filepath.Join(StaticRoute, filepath.Base(commfile.Filename)) + if err := c.SaveUploadedFile(commfile, filename); err != nil { + handleErr(c, err) + return + } + c.JSON(http.StatusOK, gin.H{ + "message": "ok", + }) +} diff --git a/router/endpoint.go b/router/endpoint.go index 16033b2..fc94bfd 100644 --- a/router/endpoint.go +++ b/router/endpoint.go @@ -1,7 +1,7 @@ /* * @Author: EnderByEndera * @Date: 2020-12-28 15:53:37 - * @LastEditTime: 2021-01-05 11:53:02 + * @LastEditTime: 2021-01-16 08:15:11 * @LastEditors: Please set LastEditors * @Description: endpoint.go contains all the endpoints * @FilePath: /commdetection/router/endpoint.go @@ -11,150 +11,11 @@ package router import ( "commdetection/logger" - "commdetection/model" "net/http" - "path/filepath" "github.com/gin-gonic/gin" ) -// uploadCommEndpoint uploads the file storing commands to the system -func uploadCommEndpoint(c *gin.Context) { - commfile, err := c.FormFile("commfile") - if err != nil { - handleErr(c, err) - return - } - - filename := filepath.Join(StaticRoute, filepath.Base(commfile.Filename)) - if err := c.SaveUploadedFile(commfile, filename); err != nil { - handleErr(c, err) - return - } - c.JSON(http.StatusOK, gin.H{ - "message": "ok", - }) -} - -// getCommrulesEndpoint gets all the command rules stored in the json file -// if error happened, handleErr will be executed -func getCommrulesEndpoint(c *gin.Context) { - scomms, err := model.UnmarshalSensitiveCommSetting() - if err != nil { - handleErr(c, err) - return - } - logger.Debugln("Start to send scomms to the front end") - c.JSON(http.StatusOK, gin.H{ - "message": "ok", - "scomms": scomms, - }) -} - -// setCommrulesEndpoint get the command rule setting in the post form and marshal it to the json file -// Post form should be like: -// -// { -// "commrules": [ -// { -// "comm": "command name", -// "coefficient": "coefficient num", -// }, ... -// ] -// } -func setCommrulesEndpoint(c *gin.Context) { - commrules := model.SComms{} - if err := c.ShouldBind(&commrules); err != nil { - handleErr(c, err) - return - } - if err := model.MarshalSensitiveCommSetting(commrules); err != nil { - handleErr(c, err) - return - } - c.JSON(http.StatusOK, gin.H{ - "message": "ok", - }) -} - -func getPathrulesEndpoint(c *gin.Context) { - spaths, err := model.UnmarshalSensitivePathSetting() - if err != nil { - handleErr(c, err) - return - } - logger.Debugln("Start to send spaths to the front end") - c.JSON(http.StatusOK, gin.H{ - "message": "ok", - "spaths": spaths, - }) -} - -// setPathrulesEndpoint get the paths rule setting in the post form and marshal it to the json file -// Post form should be like: -// -// { -// "pathrules": [ -// { -// "path": "command name", -// "coefficient": "coefficient num", -// }, ... -// ] -// } -func setPathrulesEndpoint(c *gin.Context) { - pathrules := model.SPaths{} - if err := c.ShouldBind(&pathrules); err != nil { - handleErr(c, err) - return - } - if err := model.MarshalSensitivePathSetting(pathrules); err != nil { - handleErr(c, err) - return - } - c.JSON(http.StatusOK, gin.H{ - "message": "ok", - }) -} - -func getEvaluationRulesEndpoint(c *gin.Context) { - rs, err := model.UnmarshalSensitivePathSetting() - if err != nil { - handleErr(c, err) - return - } - logger.Debugln("Start to send evaluation rules to the front end") - c.JSON(http.StatusOK, gin.H{ - "message": "ok", - "rs": rs, - }) -} - -// setPathrulesEndpoint get the paths rule setting in the post form and marshal it to the json file -// Post form should be like: -// -// { -// "rules": [ -// { -// "name": "rule name", -// "rulefunc": "func name", -// }, ... -// ] -// } -func setEvaluationRulesEndpoint(c *gin.Context) { - rs := model.Rules{} - if err := c.ShouldBind(&rs); err != nil { - handleErr(c, err) - return - } - if err := model.MarshalEvaluationRules(rs); err != nil { - handleErr(c, err) - return - } - c.JSON(http.StatusOK, gin.H{ - "message": "ok", - }) -} - // handleErr handles all the error, set warning info in the command line, returns the error to the frontend // and exit the endpoint func handleErr(c *gin.Context, err error) { diff --git a/router/endpoint_test.go b/router/endpoint_test.go index 2a011ce..5b67e16 100644 --- a/router/endpoint_test.go +++ b/router/endpoint_test.go @@ -1,7 +1,7 @@ /* * @Author: your name * @Date: 2020-12-28 16:05:24 - * @LastEditTime: 2021-01-05 15:52:59 + * @LastEditTime: 2021-01-19 12:05:49 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: /commdetection/router/endpoint_test.go @@ -69,7 +69,7 @@ func TestEndpoint(t *testing.T) { }) t.Run("Test getCommrulesEndpoint", func(t *testing.T) { - e.GET("/commrules/getrules"). + e.GET("/commrules/get"). Expect(). Status(http.StatusOK). JSON().Object(). @@ -80,7 +80,7 @@ func TestEndpoint(t *testing.T) { }) t.Run("Test setCommrulesEndpoint", func(t *testing.T) { - e.POST("/commrules/setrules"). + e.POST("/commrules/set"). WithJSON(testscomms). Expect(). Status(http.StatusOK). @@ -88,7 +88,7 @@ func TestEndpoint(t *testing.T) { }) t.Run("Test getPathrulesEndpoint", func(t *testing.T) { - e.GET("/pathrules/getrules"). + e.GET("/pathrules/get"). Expect(). Status(http.StatusOK). JSON().Object(). @@ -98,7 +98,7 @@ func TestEndpoint(t *testing.T) { }) t.Run("Test setPathrulesEndpoint", func(t *testing.T) { - e.POST("/pathrules/setrules"). + e.POST("/pathrules/set"). WithJSON(testspaths). Expect(). Status(http.StatusOK). @@ -107,7 +107,7 @@ func TestEndpoint(t *testing.T) { }) t.Run("Test getEvaluationRulesEndpoint", func(t *testing.T) { - e.GET("/rules/getrules"). + e.GET("/rules/get"). Expect(). Status(http.StatusOK). JSON().Object(). @@ -117,7 +117,7 @@ func TestEndpoint(t *testing.T) { }) t.Run("Test setEvaluationRulesEndpoint", func(t *testing.T) { - e.POST("/rules/setrules"). + e.POST("/rules/set"). WithJSON(testrs). Expect(). Status(http.StatusOK). @@ -144,29 +144,29 @@ func BenchmarkEndpoint(b *testing.B) { }) b.Run("Benchmark getCommrulesEndpoint", func(b *testing.B) { - e.GET("/commrules/getrules").Expect() + e.GET("/commrules/get").Expect() }) b.Run("Benchmark setCommrulesEndpoint", func(b *testing.B) { - e.POST("/commrules/setrules"). + e.POST("/commrules/set"). WithJSON(testscomms).Expect() }) b.Run("Benchmark getPathrulesEndpoint", func(b *testing.B) { - e.GET("/pathrules/getrules").Expect() + e.GET("/pathrules/get").Expect() }) b.Run("Benchmark setPathrulesEndpoint", func(b *testing.B) { - e.POST("/pathrules/setrules"). + e.POST("/pathrules/set"). WithJSON(testspaths).Expect() }) b.Run("Benchmark getEvaluationRulesEndpoint", func(b *testing.B) { - e.GET("/rules/getrules").Expect() + e.GET("/rules/get").Expect() }) b.Run("Benchmark setEvaluationRulesEndpoint", func(b *testing.B) { - e.POST("rules/setrules"). + e.POST("rules/set"). WithJSON(testrs).Expect() }) } diff --git a/router/router.go b/router/router.go index 2ded80b..8c08a7b 100644 --- a/router/router.go +++ b/router/router.go @@ -1,7 +1,7 @@ /* * @Author: EnderByEndera * @Date: 2020-12-23 13:17:08 - * @LastEditTime: 2021-01-05 17:54:33 + * @LastEditTime: 2021-01-19 12:06:31 * @LastEditors: Please set LastEditors * @Description: router.go defines gin.Engine and its router * @FilePath: /commdetection/route/router.go @@ -30,7 +30,6 @@ func init() { logger.Warnln(err) } gin.SetMode(ginmode) - logger.Debugf("Sets Gin mode to \"%s\" mode", ginmode) router = gin.New() router.Use(gin.Recovery()) @@ -55,7 +54,8 @@ var ( // StartRouter starts and sets ending method for gin engine func StartRouter() { - + ginmode, _ := yaml.GetGinMode() + logger.Debugf("Sets gin mode to \"%s\"", ginmode) go func() { // engine start on http://127.0.0.1:8000 logger.Debugln("Server started at", srv.Addr) @@ -88,17 +88,17 @@ func manageRouter() { } commr := router.Group("/commrules") { - commr.POST("/setrules", setCommrulesEndpoint) - commr.GET("/getrules", getCommrulesEndpoint) + commr.POST("/set", setCommrulesEndpoint) + commr.GET("/get", getCommrulesEndpoint) } pathr := router.Group("/pathrules") { - pathr.POST("/setrules", setPathrulesEndpoint) - pathr.GET("/getrules", getPathrulesEndpoint) + pathr.POST("/set", setPathrulesEndpoint) + pathr.GET("/get", getPathrulesEndpoint) } rsr := router.Group("/rules") { - rsr.POST("/setrules", setEvaluationRulesEndpoint) - rsr.GET("/getrules", getEvaluationRulesEndpoint) + rsr.POST("/set", setEvaluationRulesEndpoint) + rsr.GET("/get", getEvaluationRulesEndpoint) } } diff --git a/router/rule_endpoint.go b/router/rule_endpoint.go new file mode 100644 index 0000000..d8bc59f --- /dev/null +++ b/router/rule_endpoint.go @@ -0,0 +1,137 @@ +/* + * @Author: EnderByEndera + * @Date: 2021-01-16 08:12:49 + * @LastEditTime: 2021-01-16 08:16:53 + * @LastEditors: Please set LastEditors + * @Description: rule_endpoint has the function for all the command rules' endpoint, including getter and setter + * @FilePath: /commdetection/router/rule_endpoint.go + */ + +package router + +import ( + "commdetection/logger" + "commdetection/model" + "net/http" + + "github.com/gin-gonic/gin" +) + +// getCommrulesEndpoint gets all the command rules stored in the json file +// if error happened, handleErr will be executed +func getCommrulesEndpoint(c *gin.Context) { + scomms, err := model.UnmarshalSensitiveCommSetting() + if err != nil { + handleErr(c, err) + return + } + logger.Debugln("Start to send scomms to the front end") + c.JSON(http.StatusOK, gin.H{ + "message": "ok", + "scomms": scomms, + }) +} + +// setCommrulesEndpoint get the command rule setting in the post form and marshal it to the json file +// Post form should be like: +// +// { +// "commrules": [ +// { +// "comm": "command name", +// "coefficient": "coefficient num", +// }, ... +// ] +// } +func setCommrulesEndpoint(c *gin.Context) { + commrules := model.SComms{} + if err := c.ShouldBind(&commrules); err != nil { + handleErr(c, err) + return + } + if err := model.MarshalSensitiveCommSetting(commrules); err != nil { + handleErr(c, err) + return + } + c.JSON(http.StatusOK, gin.H{ + "message": "ok", + }) +} + +func getPathrulesEndpoint(c *gin.Context) { + spaths, err := model.UnmarshalSensitivePathSetting() + if err != nil { + handleErr(c, err) + return + } + logger.Debugln("Start to send spaths to the front end") + c.JSON(http.StatusOK, gin.H{ + "message": "ok", + "spaths": spaths, + }) +} + +// setPathrulesEndpoint get the paths rule setting in the post form and marshal it to the json file +// Post form should be like: +// +// { +// "pathrules": [ +// { +// "path": "command name", +// "coefficient": "coefficient num", +// }, ... +// ] +// } +func setPathrulesEndpoint(c *gin.Context) { + pathrules := model.SPaths{} + if err := c.ShouldBind(&pathrules); err != nil { + handleErr(c, err) + return + } + if err := model.MarshalSensitivePathSetting(pathrules); err != nil { + handleErr(c, err) + return + } + c.JSON(http.StatusOK, gin.H{ + "message": "ok", + }) +} + +func getEvaluationRulesEndpoint(c *gin.Context) { + rs, err := model.UnmarshalSensitivePathSetting() + if err != nil { + handleErr(c, err) + return + } + logger.Debugln("Start to send evaluation rules to the front end") + c.JSON(http.StatusOK, gin.H{ + "message": "ok", + "rs": rs, + }) +} + +// setPathrulesEndpoint get the paths rule setting in the post form and marshal it to the json file +// Post form should be like: +// +// { +// "rules": [ +// { +// "name": "rule name", +// "rulefunc": "func name", +// }, ... +// ] +// } +func setEvaluationRulesEndpoint(c *gin.Context) { + rs := model.Rules{} + if err := c.ShouldBind(&rs); err != nil { + handleErr(c, err) + return + } + if err := model.MarshalEvaluationRules(rs); err != nil { + handleErr(c, err) + return + } + c.JSON(http.StatusOK, gin.H{ + "message": "ok", + }) +} |
