summaryrefslogtreecommitdiff
path: root/cmd/root.go
diff options
context:
space:
mode:
authorEnderByEndera <[email protected]>2020-12-23 08:47:10 +0800
committerEnderByEndera <[email protected]>2020-12-23 08:47:10 +0800
commit513479f9007c11160277ee5c60da7af7f1b7dff0 (patch)
treec061cf20b66a3141e85a6ab8ca83f6b8cb5db3a0 /cmd/root.go
parentc70c7a0425593ae1fa58987c108c7a931a900271 (diff)
Added Gin router to provide json output to the net
let LogLevel exported, so it can be used by init() func in every package
Diffstat (limited to 'cmd/root.go')
-rw-r--r--cmd/root.go86
1 files changed, 62 insertions, 24 deletions
diff --git a/cmd/root.go b/cmd/root.go
index c377e3e..7b13123 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -1,7 +1,7 @@
/*
* @Author: EnderByEndera
* @Date: 2020-12-19 11:59:02
- * @LastEditTime: 2020-12-19 17:05:53
+ * @LastEditTime: 2020-12-22 17:21:05
* @LastEditors: Please set LastEditors
* @Description: root of the commdetection cmd
* @FilePath: /commdetection/cmd/root.go
@@ -13,12 +13,16 @@ import (
"commdetection/comm"
"commdetection/logger"
"commdetection/rules"
- "commdetection/yaml"
+ "context"
"encoding/json"
"io/ioutil"
+ "net/http"
"os"
+ "os/signal"
"path/filepath"
+ "syscall"
+ "github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
@@ -33,38 +37,47 @@ var rootCmd = &cobra.Command{
}
var (
- filterStr string = "which"
+ filterStr string
evaluations []string
- file string = "/root/.bash_history"
- logLevel uint32 = 5
+ file string
+ logLevel uint32
+ output string
filters = []comm.Filter{}
rs = rules.Rules{}
buf []byte
+ jsonBuf []byte
+ r *gin.Engine
+ srv *http.Server
)
// Execute executes the command
-func Execute() {
- // 初始化
- initialize()
- rootCmd.Execute()
+func Execute() error {
+ return rootCmd.Execute()
}
-func initialize() {
+func init() {
+ // initilize gin-gonic backend engine
+ r = gin.New()
+ r.Use(logger.Middleware())
+ r.Use(gin.Recovery())
+
+ srv = &http.Server{
+ Addr: ":8080",
+ Handler: r,
+ }
+
rootCmd.PersistentFlags().StringVar(&filterStr, "filter", "which", "choose one filter to filt the data from the file")
- rootCmd.PersistentFlags().StringSliceVarP(&evaluations, "evaluations", "e", []string{}, "choose one or more evaluations to evaluate commands")
+ rootCmd.PersistentFlags().StringSliceVarP(&evaluations, "evaluations", "e", []string{"command"}, "choose one or more evaluations to evaluate commands")
rootCmd.PersistentFlags().StringVar(&file, "file", "/root/.bash_history", "choose one file storing data of the commands")
rootCmd.PersistentFlags().Uint32Var(&logLevel, "loglevel", uint32(logrus.DebugLevel), "choose log level")
- rootCmd.AddCommand(verCmd)
-
- logger.Init(logrus.Level(logLevel), &logrus.TextFormatter{
- FullTimestamp: true,
- TimestampFormat: "2006-01-02 15:07:05",
- })
+ rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "file", "choose the output method: json(json) or file.json(file), choose json will open a server to provide json output on the net")
+ rootCmd.AddCommand(verCmd) // add version as a subcommand
+}
- if err := yaml.InitYamlSetting(); err != nil {
- logger.Fatalln(err)
- }
+// GetLogLevel returns loglevel sent by rootCmd
+func GetLogLevel() uint32 {
+ return logLevel
}
func root() {
@@ -108,8 +121,33 @@ func root() {
// 评估命令,利用rs中保留的规则进行评估
logger.Debugln("Evaluating commands' scores using ", evaluations)
css = rules.EvaluateCommScore(css, rs)
- // 将命令得分保存到json文件中
- logger.Debugln("Storing result to output.json file")
- jsonBuf, _ := json.Marshal(css)
- ioutil.WriteFile(filepath.Join(os.Getenv("COMMDEPATH"), "output.json"), jsonBuf, os.ModeAppend)
+
+ jsonBuf, _ = json.Marshal(css)
+ if output == "file" {
+ // 将命令得分保存到json文件中
+ logger.Debugln("Storing result to output.json file")
+ ioutil.WriteFile(filepath.Join(os.Getenv("COMMDEPATH"), "output.json"), jsonBuf, os.ModeAppend)
+ } else if output == "json" {
+ // router "/scores"
+ r.GET("/scores", func(c *gin.Context) {
+ c.JSON(200, css)
+ })
+ }
+ go func() {
+ // engine start on http://127.0.0.1:8000
+ if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
+ logger.Fatalln(err)
+ }
+ }()
+
+ // Make a quit channel to receive SIGINT and SIGTERM signal
+ quit := make(chan os.Signal)
+ signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
+ <-quit
+ logger.Debugln("Server shutting down...")
+
+ if err := srv.Shutdown(context.Background()); err != nil {
+ logger.Fatalln("Server Shutdown with err: ", err)
+ }
+ logger.Debugln("Server shutted down")
}