diff --git a/.all-contributorsrc b/.all-contributorsrc index 3459eb1..6719891 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -7,9 +7,9 @@ "contributors": [ { "login": "wuhan005", - "name": "John Wu", + "name": "E99p1ant", "avatar_url": "https://avatars3.githubusercontent.com/u/12731778?v=4", - "profile": "https://github.red", + "profile": "https://github.com/wuhan005", "contributions": [ "code", "design", diff --git a/README.md b/README.md index a10c3df..933cd50 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ chmod +x ./Cardinal - + @@ -106,13 +106,6 @@ chmod +x ./Cardinal 十分欢迎您和我们一起改进 Cardinal,您可以改进现有程序,加入新功能,完善文档,优化代码等。 - - - - - - - ## 协议与许可 © Vidar-Team diff --git a/go.mod b/go.mod index 90414f8..05c5962 100644 --- a/go.mod +++ b/go.mod @@ -41,8 +41,8 @@ require ( github.com/theplant/cldr v0.0.0-20190423050709-9f76f7ce4ee8 // indirect github.com/theplant/htmltestingutils v0.0.0-20190423050759-0e06de7b6967 // indirect github.com/theplant/testingutils v0.0.0-20190603093022-26d8b4d95c61 // indirect - github.com/vidar-team/Cardinal_frontend v0.5.0 - github.com/vidar-team/Cardinal_manager_frontend v0.5.0 + github.com/vidar-team/Cardinal_frontend v0.5.2-alpha + github.com/vidar-team/Cardinal_manager_frontend v0.5.2-alpha github.com/yosssi/gohtml v0.0.0-20200424144038-a48de20dd9dd // indirect golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd golang.org/x/text v0.3.0 diff --git a/go.sum b/go.sum index 9d4487b..cfa0a74 100644 --- a/go.sum +++ b/go.sum @@ -158,10 +158,14 @@ github.com/vidar-team/Cardinal_frontend v0.0.4 h1:FWpzrPMcp/YIYrrfRtAPEQK6wm2902 github.com/vidar-team/Cardinal_frontend v0.0.4/go.mod h1:wD4qPD/Z+T3eT0SiOQe9262VY6xSyWz12UQXmkH9uiI= github.com/vidar-team/Cardinal_frontend v0.5.0 h1:DlnoViBY6x+/u75fDKIxABzYhhQW+FRziAKMKGmR7GY= github.com/vidar-team/Cardinal_frontend v0.5.0/go.mod h1:wD4qPD/Z+T3eT0SiOQe9262VY6xSyWz12UQXmkH9uiI= +github.com/vidar-team/Cardinal_frontend v0.5.2-alpha h1:sG37Ypm0G25bdfrvmNh3n6rD+xSLLFMxbWCQ0cevHAo= +github.com/vidar-team/Cardinal_frontend v0.5.2-alpha/go.mod h1:wD4qPD/Z+T3eT0SiOQe9262VY6xSyWz12UQXmkH9uiI= github.com/vidar-team/Cardinal_manager_frontend v0.0.5-rc1 h1:uAeBZBaDZIXD4uTP871+6MiXrlFMBLUERO7ynfEwVuk= github.com/vidar-team/Cardinal_manager_frontend v0.0.5-rc1/go.mod h1:uJBQtuXHTLNyuIMONoM1OqYmW7Q/N40+587Qt90kqAk= github.com/vidar-team/Cardinal_manager_frontend v0.5.0 h1:JspK4PXWFZuxxQSTP+QhUYt0Dd8WH+tz5h2tSE201n4= github.com/vidar-team/Cardinal_manager_frontend v0.5.0/go.mod h1:uJBQtuXHTLNyuIMONoM1OqYmW7Q/N40+587Qt90kqAk= +github.com/vidar-team/Cardinal_manager_frontend v0.5.2-alpha h1:xgXwPqXCjHiHVQC7qoj54Cf750cwENXutqnYK13+US8= +github.com/vidar-team/Cardinal_manager_frontend v0.5.2-alpha/go.mod h1:uJBQtuXHTLNyuIMONoM1OqYmW7Q/N40+587Qt90kqAk= github.com/yosssi/gohtml v0.0.0-20200424144038-a48de20dd9dd h1:LxsPdxzQnF9yE7Xv/1Ln8C8JJbEh1SlRyIDvgR/bsKg= github.com/yosssi/gohtml v0.0.0-20200424144038-a48de20dd9dd/go.mod h1:+ccdNT0xMY1dtc5XBxumbYfOUhmduiGudqaDgD2rVRE= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/src/check.go b/src/check.go index ba53a7c..5642840 100644 --- a/src/check.go +++ b/src/check.go @@ -51,12 +51,17 @@ func (s *Service) CheckDown(c *gin.Context) (int, interface{}) { // Check the gamebox is existed or not. var gameBox GameBox - s.Mysql.Model(&GameBox{}).Where(&GameBox{Model: gorm.Model{ID: inputForm.GameBoxID}, Visible: true}).Find(&gameBox) + s.Mysql.Model(&GameBox{}).Where(&GameBox{Model: gorm.Model{ID: inputForm.GameBoxID}}).Find(&gameBox) if gameBox.ID == 0 { return utils.MakeErrJSON(403, 40312, locales.I18n.T(c.GetString("lang"), "gamebox.not_found"), ) } + if !gameBox.Visible { + return utils.MakeErrJSON(403, 40314, + locales.I18n.T(c.GetString("lang"), "check.not_visible"), + ) + } // No problem! Update the gamebox status to down. s.Mysql.Model(&GameBox{}).Where(&GameBox{Model: gorm.Model{ID: gameBox.ID}}).Update(&GameBox{IsDown: true}) diff --git a/src/flag.go b/src/flag.go index b9b32ca..aba5e81 100644 --- a/src/flag.go +++ b/src/flag.go @@ -230,7 +230,7 @@ func (s *Service) refreshFlag() { // Replace the flag placeholder. // strings.ReplaceAll need Go 1.13+, so we use strings.Replace here. command := strings.Replace(challenge.Command, "{{FLAG}}", flag.Flag, -1) - err := utils.SSHExecute(gamebox.IP, gamebox.SSHPort, gamebox.SSHUser, gamebox.SSHPassword, command) + _, err := utils.SSHExecute(gamebox.IP, gamebox.SSHPort, gamebox.SSHUser, gamebox.SSHPassword, command) if err != nil { s.NewLog(IMPORTANT, "ssh_error", fmt.Sprintf("Team:%d Gamebox:%d Round:%d SSH 更新 Flag 失败:%v", gamebox.TeamID, gamebox.ID, s.Timer.NowRound, err.Error())) } @@ -240,7 +240,7 @@ func (s *Service) refreshFlag() { } } -func (s *Service) testSSH(c *gin.Context) (int, interface{}) { +func (s *Service) testAllSSH(c *gin.Context) (int, interface{}) { var challenges []Challenge s.Mysql.Model(&Challenge{}).Where(&Challenge{AutoRefreshFlag: true}).Find(&challenges) @@ -261,7 +261,7 @@ func (s *Service) testSSH(c *gin.Context) (int, interface{}) { wg.Add(1) go func(gamebox GameBox, challenge Challenge) { defer wg.Done() - err := utils.SSHExecute(gamebox.IP, gamebox.SSHPort, gamebox.SSHUser, gamebox.SSHPassword, "whoami") + _, err := utils.SSHExecute(gamebox.IP, gamebox.SSHPort, gamebox.SSHUser, gamebox.SSHPassword, "whoami") if err != nil { errs = append(errs, errorMessage{ TeamID: gamebox.TeamID, @@ -276,3 +276,25 @@ func (s *Service) testSSH(c *gin.Context) (int, interface{}) { wg.Wait() return utils.MakeSuccessJSON(errs) } + +func (s *Service) testSSH(c *gin.Context) (int, interface{}) { + var inputForm struct { + IP string `binding:"required"` + Port string `binding:"required"` + User string `binding:"required"` + Password string `binding:"required"` + Command string `binding:"required"` + } + err := c.BindJSON(&inputForm) + if err != nil { + return utils.MakeErrJSON(400, 40036, + locales.I18n.T(c.GetString("lang"), "general.error_payload"), + ) + } + + output, err := utils.SSHExecute(inputForm.IP, inputForm.Port, inputForm.User, inputForm.Password, inputForm.Command) + if err != nil { + return utils.MakeErrJSON(400, 40037, err) + } + return utils.MakeSuccessJSON(output) +} diff --git a/src/locales/en-US.yml b/src/locales/en-US.yml index 6921d87..b1833f3 100644 --- a/src/locales/en-US.yml +++ b/src/locales/en-US.yml @@ -33,6 +33,7 @@ en-US: check: repeat: "Duplicated Check Ignored." + not_visible: "Challenge is now Invisible." config: load_success: "Load Config Files Succeeded!" diff --git a/src/locales/zh-CN.yml b/src/locales/zh-CN.yml index ac5d24e..4bdd833 100644 --- a/src/locales/zh-CN.yml +++ b/src/locales/zh-CN.yml @@ -33,6 +33,7 @@ zh-CN: check: repeat: "重复 Check,已忽略" + not_visible: "题目未开题,CheckDown 失败" config: load_success: "加载配置文件成功" diff --git a/src/router.go b/src/router.go index fd5c7d9..8b69cc7 100644 --- a/src/router.go +++ b/src/router.go @@ -117,8 +117,16 @@ func (s *Service) initRouter() *gin.Engine { c.JSON(s.EditGameBox(c)) }) manager.GET("/gameboxes/sshTest", func(c *gin.Context) { + c.JSON(s.testAllSSH(c)) + }) + manager.POST("/gameboxes/sshTest", func(c *gin.Context) { c.JSON(s.testSSH(c)) }) + manager.GET("/gameboxes/refreshFlag", func(c *gin.Context) { + s.refreshFlag() + // TODO: i18n + c.JSON(utils.MakeSuccessJSON("刷新 Flag 操作已执行,请在数据大屏查看是否有错误信息")) + }) // Team manager.GET("/teams", func(c *gin.Context) { diff --git a/src/utils/utils.go b/src/utils/utils.go index ccdae17..99e9cb8 100644 --- a/src/utils/utils.go +++ b/src/utils/utils.go @@ -2,6 +2,7 @@ package utils import ( "bufio" + "bytes" "crypto/hmac" "crypto/sha1" "fmt" @@ -87,7 +88,7 @@ func InputString(str *string, hint string) { } } -func SSHExecute(ip string, port string, user string, password string, command string) error { +func SSHExecute(ip string, port string, user string, password string, command string) (string, error) { client, err := ssh.Dial("tcp", ip+":"+port, &ssh.ClientConfig{ User: user, Auth: []ssh.AuthMethod{ssh.Password(password)}, @@ -95,20 +96,22 @@ func SSHExecute(ip string, port string, user string, password string, command st Timeout: 5 * time.Second, }) if err != nil { - return err + return "", err } defer client.Close() session, err := client.NewSession() if err != nil { - return err + return "", err } defer session.Close() + var output bytes.Buffer + session.Stdout = &output err = session.Run(command) if err != nil { - return err + return "", err } - - return nil + + return output.String(), nil }

John Wu

💻 🎨 📖 🚧

E99p1ant

💻 🎨 📖 🚧

Moesang

💻 📖 🚧

michaelfyc

🌍