Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #49 from vidar-team/dev
Browse files Browse the repository at this point in the history
v0.5.2
  • Loading branch information
wuhan005 authored Jun 12, 2020
2 parents 6038546 + 5c840d0 commit 7f2f437
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ chmod +x ./Cardinal
<!-- markdownlint-disable -->
<table>
<tr>
<td align="center"><a href="https://github.red"><img src="https://avatars3.githubusercontent.com/u/12731778?v=4" width="100px;" alt=""/><br /><sub><b>John Wu</b></sub></a><br /><a href="https://github.com/vidar-team/Cardinal/commits?author=wuhan005" title="Code">💻</a> <a href="#design-wuhan005" title="Design">🎨</a> <a href="https://github.com/vidar-team/Cardinal/commits?author=wuhan005" title="Documentation">📖</a> <a href="#maintenance-wuhan005" title="Maintenance">🚧</a></td>
<td align="center"><a href="https://github.com/wuhan005"><img src="https://avatars3.githubusercontent.com/u/12731778?v=4" width="100px;" alt=""/><br /><sub><b>E99p1ant</b></sub></a><br /><a href="https://github.com/vidar-team/Cardinal/commits?author=wuhan005" title="Code">💻</a> <a href="#design-wuhan005" title="Design">🎨</a> <a href="https://github.com/vidar-team/Cardinal/commits?author=wuhan005" title="Documentation">📖</a> <a href="#maintenance-wuhan005" title="Maintenance">🚧</a></td>
<td align="center"><a href="https://github.com/Moesang"><img src="https://avatars2.githubusercontent.com/u/46858006?v=4" width="100px;" alt=""/><br /><sub><b>Moesang</b></sub></a><br /><a href="https://github.com/vidar-team/Cardinal/commits?author=Moesang" title="Code">💻</a> <a href="https://github.com/vidar-team/Cardinal/commits?author=Moesang" title="Documentation">📖</a> <a href="#maintenance-Moesang" title="Maintenance">🚧</a></td>
<td align="center"><a href="https://github.com/michaelfyc"><img src="https://avatars2.githubusercontent.com/u/45136049?v=4" width="100px;" alt=""/><br /><sub><b>michaelfyc</b></sub></a><br /><a href="#translation-michaelfyc" title="Translation">🌍</a></td>
</tr>
Expand All @@ -106,13 +106,6 @@ chmod +x ./Cardinal

十分欢迎您和我们一起改进 Cardinal,您可以改进现有程序,加入新功能,完善文档,优化代码等。

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->
<!-- ALL-CONTRIBUTORS-LIST:END -->

## 协议与许可

© Vidar-Team
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
7 changes: 6 additions & 1 deletion src/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
28 changes: 25 additions & 3 deletions src/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand All @@ -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)

Expand All @@ -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,
Expand All @@ -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)
}
1 change: 1 addition & 0 deletions src/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ en-US:

check:
repeat: "Duplicated Check Ignored."
not_visible: "Challenge is now Invisible."

config:
load_success: "Load Config Files Succeeded!"
Expand Down
1 change: 1 addition & 0 deletions src/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ zh-CN:

check:
repeat: "重复 Check,已忽略"
not_visible: "题目未开题,CheckDown 失败"

config:
load_success: "加载配置文件成功"
Expand Down
8 changes: 8 additions & 0 deletions src/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 9 additions & 6 deletions src/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"bufio"
"bytes"
"crypto/hmac"
"crypto/sha1"
"fmt"
Expand Down Expand Up @@ -87,28 +88,30 @@ 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)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
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
}

0 comments on commit 7f2f437

Please sign in to comment.