1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package handler
import (
"bytes"
"fmt"
"io"
"text/template"
"github.com/gliderlabs/ssh"
"cocogo/pkg/config"
"cocogo/pkg/i18n"
"cocogo/pkg/logger"
)
var defaultTitle string
var menu Menu
type MenuItem struct {
id int
instruct string
helpText string
showText string
}
func (mi *MenuItem) Text() string {
if mi.showText != "" {
return mi.showText
}
cm := ColorMeta{GreenBoldColor: "\033[1;32m", ColorEnd: "\033[0m"}
line := fmt.Sprintf(i18n.T("\t%d) Enter {{.GreenBoldColor}}%s{{.ColorEnd}} to %s.%s"), mi.id, mi.instruct, mi.helpText, "\r\n")
tmpl := template.Must(template.New("item").Parse(line))
var buf bytes.Buffer
err := tmpl.Execute(&buf, cm)
if err != nil {
logger.Error(err)
}
mi.showText = string(buf.Bytes())
return mi.showText
}
type Menu []MenuItem
func init() {
defaultTitle = i18n.T("Welcome to use Jumpserver open source fortress system")
menu = Menu{
{id: 1, instruct: "ID", helpText: i18n.T("directly login")},
{id: 2, instruct: i18n.T("part IP, Hostname, Comment"), helpText: i18n.T("to search login if unique")},
{id: 3, instruct: i18n.T("/ + IP, Hostname, Comment"), helpText: i18n.T("to search, such as: /192.168")},
{id: 4, instruct: "p", helpText: i18n.T("display the host you have permission")},
{id: 5, instruct: "g", helpText: "display the node that you have permission"},
{id: 6, instruct: "r", helpText: "refresh your assets and nodes"},
{id: 7, instruct: "s", helpText: "switch Chinese-english language"},
{id: 8, instruct: "h", helpText: "print help"},
{id: 9, instruct: "q", helpText: "exit"},
}
}
type ColorMeta struct {
GreenBoldColor string
ColorEnd string
}
func displayBanner(sess ssh.Session, user string) {
title := defaultTitle
if config.Conf.HeaderTitle != "" {
title = config.Conf.HeaderTitle
}
welcomeMsg := CharClear + CharTab + user + " " + title + CharNewLine
_, err := io.WriteString(sess, welcomeMsg)
if err != nil {
logger.Error("Send to client error, %s", err)
}
for _, v := range menu {
_, _ = io.WriteString(sess, v.Text())
}
}