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
78
79
80
81
82
83
84
85
86
87
88
89
package proxy
import (
"io"
"regexp"
"strings"
"sync"
"github.com/jumpserver/koko/pkg/logger"
"github.com/jumpserver/koko/pkg/utils"
)
var ps1Pattern = regexp.MustCompile(`^\[?.*@.*\]?[\\$#]\s|mysql>\s`)
func NewCmdParser() *CmdParser {
parser := &CmdParser{}
parser.initial()
return parser
}
type CmdParser struct {
term *utils.Terminal
reader io.ReadCloser
writer io.WriteCloser
currentLines []string
lock *sync.Mutex
maxLength int
currentLength int
}
func (cp *CmdParser) WriteData(p []byte) (int,error){
return cp.writer.Write(p)
}
func (cp *CmdParser) Write (p []byte) (int,error){
return len(p),nil
}
func (cp *CmdParser) Read(p []byte)(int,error){
return cp.reader.Read(p)
}
func (cp *CmdParser) Close() error{
return cp.writer.Close()
}
func (cp *CmdParser) initial() {
cp.reader,cp.writer = io.Pipe()
cp.currentLines = make([]string,0)
cp.lock = new(sync.Mutex)
cp.maxLength = 1024
cp.currentLength = 0
cp.term = utils.NewTerminal(cp, "")
cp.term.SetEcho(false)
go func() {
logger.Debug("command Parser start")
defer logger.Debug("command Parser close")
for {
line, err := cp.term.ReadLine()
if err != nil{
break
}
cp.lock.Lock()
cp.currentLength += len(line)
if cp.currentLength < cp.maxLength {
cp.currentLines = append(cp.currentLines,line)
}
cp.lock.Unlock()
}
}()
}
func (cp *CmdParser) parsePS1(s string) string {
return ps1Pattern.ReplaceAllString(s, "")
}
// Parse 解析命令或输出
func (cp *CmdParser) Parse() string {
cp.writer.Write([]byte("\r"))
cp.lock.Lock()
defer cp.lock.Unlock()
output := strings.TrimSpace(strings.Join(cp.currentLines, "\r\n"))
output = cp.parsePS1(output)
cp.currentLines = make([]string,0)
cp.currentLength = 0
return output
}