Commit 9295a3eb authored by Eric's avatar Eric

fix commmand parse

parents 2e4496e2 50388696
package proxy package proxy
import ( import (
"io" "bytes"
"github.com/jumpserver/koko/pkg/logger"
"github.com/jumpserver/koko/pkg/utils"
"regexp" "regexp"
"strings" "strings"
"sync" "sync"
"github.com/jumpserver/koko/pkg/logger"
"github.com/jumpserver/koko/pkg/utils"
) )
var ps1Pattern = regexp.MustCompile(`^\[?.*@.*\]?[\\$#]\s|mysql>\s`) var ps1Pattern = regexp.MustCompile(`^\[?.*@.*\]?[\\$#]\s|mysql>\s`)
...@@ -21,91 +20,29 @@ func NewCmdParser(sid, name string) *CmdParser { ...@@ -21,91 +20,29 @@ func NewCmdParser(sid, name string) *CmdParser {
type CmdParser struct { type CmdParser struct {
id string id string
name string name string
buf bytes.Buffer
term *utils.Terminal
reader io.ReadCloser
writer io.WriteCloser
currentLines []string
lock *sync.Mutex lock *sync.Mutex
maxLength int maxLength int
currentLength int currentLength int
closed chan struct{}
} }
func (cp *CmdParser) WriteData(p []byte) (int, error) { func (cp *CmdParser) WriteData(p []byte) (int, error) {
select { cp.lock.Lock()
case <-cp.closed: defer cp.lock.Unlock()
return 0, io.EOF if cp.buf.Len() >= 1024 {
default: return 0, nil
}
return cp.writer.Write(p)
}
func (cp *CmdParser) Write(p []byte) (int, error) {
select {
case <-cp.closed:
return 0, io.EOF
default:
}
return len(p), nil
}
func (cp *CmdParser) Read(p []byte) (int, error) {
select {
case <-cp.closed:
return 0, io.EOF
default:
} }
return cp.reader.Read(p) return cp.buf.Write(p)
} }
func (cp *CmdParser) Close() error { func (cp *CmdParser) Close() error {
select { logger.Infof("session ID: %s, parser name: %s", cp.id, cp.name)
case <-cp.closed: return nil
return nil
default:
close(cp.closed)
}
_ = cp.reader.Close()
return cp.writer.Close()
} }
func (cp *CmdParser) initial() { func (cp *CmdParser) initial() {
cp.reader, cp.writer = io.Pipe()
cp.currentLines = make([]string, 0)
cp.lock = new(sync.Mutex) cp.lock = new(sync.Mutex)
cp.maxLength = 1024
cp.currentLength = 0
cp.closed = make(chan struct{})
cp.term = utils.NewTerminal(cp, "")
cp.term.SetEcho(false)
go func() {
logger.Infof("Session %s: %s start", cp.id, cp.name)
defer logger.Infof("Session %s: %s close", cp.id, cp.name)
loop:
for {
line, err := cp.term.ReadLine()
if err != nil {
select {
case <-cp.closed:
logger.Debugf("Session %s %s term err: %s break loop", cp.id, cp.name, err)
break loop
default:
}
logger.Debugf("Session %s %s term err: %s,loop continue", cp.id, cp.name, err)
goto loop
}
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 { func (cp *CmdParser) parsePS1(s string) string {
...@@ -114,16 +51,11 @@ func (cp *CmdParser) parsePS1(s string) string { ...@@ -114,16 +51,11 @@ func (cp *CmdParser) parsePS1(s string) string {
// Parse 解析命令或输出 // Parse 解析命令或输出
func (cp *CmdParser) Parse() string { func (cp *CmdParser) Parse() string {
select {
case <-cp.closed:
default:
cp.writer.Write([]byte("\r"))
}
cp.lock.Lock() cp.lock.Lock()
defer cp.lock.Unlock() defer cp.lock.Unlock()
output := strings.TrimSpace(strings.Join(cp.currentLines, "\r\n")) lines := utils.ParseTerminalData(cp.buf.Bytes())
output := strings.TrimSpace(strings.Join(lines, "\r\n"))
output = cp.parsePS1(output) output = cp.parsePS1(output)
cp.currentLines = make([]string, 0) cp.buf.Reset()
cp.currentLength = 0
return output return output
} }
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment