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

Commit 7f2f437

Browse files
authored
Merge pull request #49 from vidar-team/dev
v0.5.2
2 parents 6038546 + 5c840d0 commit 7f2f437

10 files changed

Lines changed: 59 additions & 22 deletions

File tree

.all-contributorsrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"contributors": [
88
{
99
"login": "wuhan005",
10-
"name": "John Wu",
10+
"name": "E99p1ant",
1111
"avatar_url": "https://avatars3.githubusercontent.com/u/12731778?v=4",
12-
"profile": "https://github.red",
12+
"profile": "https://github.com/wuhan005",
1313
"contributions": [
1414
"code",
1515
"design",

README.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ chmod +x ./Cardinal
9494
<!-- markdownlint-disable -->
9595
<table>
9696
<tr>
97-
<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>
97+
<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>
9898
<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>
9999
<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>
100100
</tr>
@@ -106,13 +106,6 @@ chmod +x ./Cardinal
106106

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

109-
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
110-
<!-- prettier-ignore-start -->
111-
<!-- markdownlint-disable -->
112-
<!-- markdownlint-enable -->
113-
<!-- prettier-ignore-end -->
114-
<!-- ALL-CONTRIBUTORS-LIST:END -->
115-
116109
## 协议与许可
117110

118111
© Vidar-Team

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ require (
4141
github.com/theplant/cldr v0.0.0-20190423050709-9f76f7ce4ee8 // indirect
4242
github.com/theplant/htmltestingutils v0.0.0-20190423050759-0e06de7b6967 // indirect
4343
github.com/theplant/testingutils v0.0.0-20190603093022-26d8b4d95c61 // indirect
44-
github.com/vidar-team/Cardinal_frontend v0.5.0
45-
github.com/vidar-team/Cardinal_manager_frontend v0.5.0
44+
github.com/vidar-team/Cardinal_frontend v0.5.2-alpha
45+
github.com/vidar-team/Cardinal_manager_frontend v0.5.2-alpha
4646
github.com/yosssi/gohtml v0.0.0-20200424144038-a48de20dd9dd // indirect
4747
golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd
4848
golang.org/x/text v0.3.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,14 @@ github.com/vidar-team/Cardinal_frontend v0.0.4 h1:FWpzrPMcp/YIYrrfRtAPEQK6wm2902
158158
github.com/vidar-team/Cardinal_frontend v0.0.4/go.mod h1:wD4qPD/Z+T3eT0SiOQe9262VY6xSyWz12UQXmkH9uiI=
159159
github.com/vidar-team/Cardinal_frontend v0.5.0 h1:DlnoViBY6x+/u75fDKIxABzYhhQW+FRziAKMKGmR7GY=
160160
github.com/vidar-team/Cardinal_frontend v0.5.0/go.mod h1:wD4qPD/Z+T3eT0SiOQe9262VY6xSyWz12UQXmkH9uiI=
161+
github.com/vidar-team/Cardinal_frontend v0.5.2-alpha h1:sG37Ypm0G25bdfrvmNh3n6rD+xSLLFMxbWCQ0cevHAo=
162+
github.com/vidar-team/Cardinal_frontend v0.5.2-alpha/go.mod h1:wD4qPD/Z+T3eT0SiOQe9262VY6xSyWz12UQXmkH9uiI=
161163
github.com/vidar-team/Cardinal_manager_frontend v0.0.5-rc1 h1:uAeBZBaDZIXD4uTP871+6MiXrlFMBLUERO7ynfEwVuk=
162164
github.com/vidar-team/Cardinal_manager_frontend v0.0.5-rc1/go.mod h1:uJBQtuXHTLNyuIMONoM1OqYmW7Q/N40+587Qt90kqAk=
163165
github.com/vidar-team/Cardinal_manager_frontend v0.5.0 h1:JspK4PXWFZuxxQSTP+QhUYt0Dd8WH+tz5h2tSE201n4=
164166
github.com/vidar-team/Cardinal_manager_frontend v0.5.0/go.mod h1:uJBQtuXHTLNyuIMONoM1OqYmW7Q/N40+587Qt90kqAk=
167+
github.com/vidar-team/Cardinal_manager_frontend v0.5.2-alpha h1:xgXwPqXCjHiHVQC7qoj54Cf750cwENXutqnYK13+US8=
168+
github.com/vidar-team/Cardinal_manager_frontend v0.5.2-alpha/go.mod h1:uJBQtuXHTLNyuIMONoM1OqYmW7Q/N40+587Qt90kqAk=
165169
github.com/yosssi/gohtml v0.0.0-20200424144038-a48de20dd9dd h1:LxsPdxzQnF9yE7Xv/1Ln8C8JJbEh1SlRyIDvgR/bsKg=
166170
github.com/yosssi/gohtml v0.0.0-20200424144038-a48de20dd9dd/go.mod h1:+ccdNT0xMY1dtc5XBxumbYfOUhmduiGudqaDgD2rVRE=
167171
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

src/check.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,17 @@ func (s *Service) CheckDown(c *gin.Context) (int, interface{}) {
5151

5252
// Check the gamebox is existed or not.
5353
var gameBox GameBox
54-
s.Mysql.Model(&GameBox{}).Where(&GameBox{Model: gorm.Model{ID: inputForm.GameBoxID}, Visible: true}).Find(&gameBox)
54+
s.Mysql.Model(&GameBox{}).Where(&GameBox{Model: gorm.Model{ID: inputForm.GameBoxID}}).Find(&gameBox)
5555
if gameBox.ID == 0 {
5656
return utils.MakeErrJSON(403, 40312,
5757
locales.I18n.T(c.GetString("lang"), "gamebox.not_found"),
5858
)
5959
}
60+
if !gameBox.Visible {
61+
return utils.MakeErrJSON(403, 40314,
62+
locales.I18n.T(c.GetString("lang"), "check.not_visible"),
63+
)
64+
}
6065

6166
// No problem! Update the gamebox status to down.
6267
s.Mysql.Model(&GameBox{}).Where(&GameBox{Model: gorm.Model{ID: gameBox.ID}}).Update(&GameBox{IsDown: true})

src/flag.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func (s *Service) refreshFlag() {
230230
// Replace the flag placeholder.
231231
// strings.ReplaceAll need Go 1.13+, so we use strings.Replace here.
232232
command := strings.Replace(challenge.Command, "{{FLAG}}", flag.Flag, -1)
233-
err := utils.SSHExecute(gamebox.IP, gamebox.SSHPort, gamebox.SSHUser, gamebox.SSHPassword, command)
233+
_, err := utils.SSHExecute(gamebox.IP, gamebox.SSHPort, gamebox.SSHUser, gamebox.SSHPassword, command)
234234
if err != nil {
235235
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()))
236236
}
@@ -240,7 +240,7 @@ func (s *Service) refreshFlag() {
240240
}
241241
}
242242

243-
func (s *Service) testSSH(c *gin.Context) (int, interface{}) {
243+
func (s *Service) testAllSSH(c *gin.Context) (int, interface{}) {
244244
var challenges []Challenge
245245
s.Mysql.Model(&Challenge{}).Where(&Challenge{AutoRefreshFlag: true}).Find(&challenges)
246246

@@ -261,7 +261,7 @@ func (s *Service) testSSH(c *gin.Context) (int, interface{}) {
261261
wg.Add(1)
262262
go func(gamebox GameBox, challenge Challenge) {
263263
defer wg.Done()
264-
err := utils.SSHExecute(gamebox.IP, gamebox.SSHPort, gamebox.SSHUser, gamebox.SSHPassword, "whoami")
264+
_, err := utils.SSHExecute(gamebox.IP, gamebox.SSHPort, gamebox.SSHUser, gamebox.SSHPassword, "whoami")
265265
if err != nil {
266266
errs = append(errs, errorMessage{
267267
TeamID: gamebox.TeamID,
@@ -276,3 +276,25 @@ func (s *Service) testSSH(c *gin.Context) (int, interface{}) {
276276
wg.Wait()
277277
return utils.MakeSuccessJSON(errs)
278278
}
279+
280+
func (s *Service) testSSH(c *gin.Context) (int, interface{}) {
281+
var inputForm struct {
282+
IP string `binding:"required"`
283+
Port string `binding:"required"`
284+
User string `binding:"required"`
285+
Password string `binding:"required"`
286+
Command string `binding:"required"`
287+
}
288+
err := c.BindJSON(&inputForm)
289+
if err != nil {
290+
return utils.MakeErrJSON(400, 40036,
291+
locales.I18n.T(c.GetString("lang"), "general.error_payload"),
292+
)
293+
}
294+
295+
output, err := utils.SSHExecute(inputForm.IP, inputForm.Port, inputForm.User, inputForm.Password, inputForm.Command)
296+
if err != nil {
297+
return utils.MakeErrJSON(400, 40037, err)
298+
}
299+
return utils.MakeSuccessJSON(output)
300+
}

src/locales/en-US.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ en-US:
3333

3434
check:
3535
repeat: "Duplicated Check Ignored."
36+
not_visible: "Challenge is now Invisible."
3637

3738
config:
3839
load_success: "Load Config Files Succeeded!"

src/locales/zh-CN.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ zh-CN:
3333

3434
check:
3535
repeat: "重复 Check,已忽略"
36+
not_visible: "题目未开题,CheckDown 失败"
3637

3738
config:
3839
load_success: "加载配置文件成功"

src/router.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,16 @@ func (s *Service) initRouter() *gin.Engine {
117117
c.JSON(s.EditGameBox(c))
118118
})
119119
manager.GET("/gameboxes/sshTest", func(c *gin.Context) {
120+
c.JSON(s.testAllSSH(c))
121+
})
122+
manager.POST("/gameboxes/sshTest", func(c *gin.Context) {
120123
c.JSON(s.testSSH(c))
121124
})
125+
manager.GET("/gameboxes/refreshFlag", func(c *gin.Context) {
126+
s.refreshFlag()
127+
// TODO: i18n
128+
c.JSON(utils.MakeSuccessJSON("刷新 Flag 操作已执行,请在数据大屏查看是否有错误信息"))
129+
})
122130

123131
// Team
124132
manager.GET("/teams", func(c *gin.Context) {

src/utils/utils.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
"bufio"
5+
"bytes"
56
"crypto/hmac"
67
"crypto/sha1"
78
"fmt"
@@ -87,28 +88,30 @@ func InputString(str *string, hint string) {
8788
}
8889
}
8990

90-
func SSHExecute(ip string, port string, user string, password string, command string) error {
91+
func SSHExecute(ip string, port string, user string, password string, command string) (string, error) {
9192
client, err := ssh.Dial("tcp", ip+":"+port, &ssh.ClientConfig{
9293
User: user,
9394
Auth: []ssh.AuthMethod{ssh.Password(password)},
9495
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
9596
Timeout: 5 * time.Second,
9697
})
9798
if err != nil {
98-
return err
99+
return "", err
99100
}
100101
defer client.Close()
101102

102103
session, err := client.NewSession()
103104
if err != nil {
104-
return err
105+
return "", err
105106
}
106107
defer session.Close()
107108

109+
var output bytes.Buffer
110+
session.Stdout = &output
108111
err = session.Run(command)
109112
if err != nil {
110-
return err
113+
return "", err
111114
}
112-
113-
return nil
115+
116+
return output.String(), nil
114117
}

0 commit comments

Comments
 (0)