Commit 262803eb authored by Eric's avatar Eric

[Update] upload command record data

parent 1efa620f
...@@ -57,15 +57,17 @@ func (p *ProxyServer) validatePermission() bool { ...@@ -57,15 +57,17 @@ func (p *ProxyServer) validatePermission() bool {
func (p *ProxyServer) getSSHConn() (srvConn *ServerSSHConnection, err error) { func (p *ProxyServer) getSSHConn() (srvConn *ServerSSHConnection, err error) {
srvConn = &ServerSSHConnection{ srvConn = &ServerSSHConnection{
host: p.Asset.Ip, name: p.Asset.Id,
port: strconv.Itoa(p.Asset.Port), host: p.Asset.Ip,
user: p.SystemUser.Username, port: strconv.Itoa(p.Asset.Port),
password: p.SystemUser.Password, user: p.SystemUser.Username,
timeout: config.GetConf().SSHTimeout, password: p.SystemUser.Password,
privateKey: p.SystemUser.PrivateKey,
timeout: config.GetConf().SSHTimeout,
} }
pty := p.UserConn.Pty() pty := p.UserConn.Pty()
done := make(chan struct{}) done := make(chan struct{})
go p.sendConnectingMsg(done) go p.sendConnectingMsg(done, srvConn.timeout)
err = srvConn.Connect(pty.Window.Height, pty.Window.Width, pty.Term) err = srvConn.Connect(pty.Window.Height, pty.Window.Width, pty.Term)
utils.IgnoreErrWriteString(p.UserConn, "\r\n") utils.IgnoreErrWriteString(p.UserConn, "\r\n")
close(done) close(done)
...@@ -86,12 +88,11 @@ func (p *ProxyServer) getServerConn() (srvConn ServerConnection, err error) { ...@@ -86,12 +88,11 @@ func (p *ProxyServer) getServerConn() (srvConn ServerConnection, err error) {
} }
} }
func (p *ProxyServer) sendConnectingMsg(done chan struct{}) { func (p *ProxyServer) sendConnectingMsg(done chan struct{}, delaySecond int) {
delay := 0.0 delay := 0.0
msg := fmt.Sprintf(i18n.T("Connecting to %s@%s %.1f"), p.SystemUser.Username, p.Asset.Ip, delay) msg := fmt.Sprintf(i18n.T("Connecting to %s@%s %.1f"), p.SystemUser.Username, p.Asset.Ip, delay)
utils.IgnoreErrWriteString(p.UserConn, msg) utils.IgnoreErrWriteString(p.UserConn, msg)
cf := config.GetConf() for int(delay) < delaySecond {
for int(delay) < cf.SSHTimeout {
select { select {
case <-done: case <-done:
return return
...@@ -145,5 +146,4 @@ func (p *ProxyServer) Proxy() { ...@@ -145,5 +146,4 @@ func (p *ProxyServer) Proxy() {
} }
sw.parser.SetCMDFilterRules(cmdRules) sw.parser.SetCMDFilterRules(cmdRules)
_ = sw.Bridge() _ = sw.Bridge()
_ = srvConn.Close()
} }
...@@ -30,13 +30,15 @@ type CommandRecorder struct { ...@@ -30,13 +30,15 @@ type CommandRecorder struct {
Session *SwitchSession Session *SwitchSession
storage CommandStorage storage CommandStorage
queue chan *model.Command queue chan *model.Command
closed chan struct{}
} }
func (c *CommandRecorder) initial() { func (c *CommandRecorder) initial() {
c.queue = make(chan *model.Command, 10) c.queue = make(chan *model.Command, 10)
//c.storage = NewCommandStorage() c.storage = NewCommandStorage()
c.storage, _ = NewFileCommandStorage("/tmp/abc.log") c.closed = make(chan struct{})
//c.storage, _ = NewFileCommandStorage("/tmp/abc.log")
go c.record() go c.record()
} }
...@@ -58,13 +60,22 @@ func (c *CommandRecorder) Record(command [2]string) { ...@@ -58,13 +60,22 @@ func (c *CommandRecorder) Record(command [2]string) {
} }
func (c *CommandRecorder) End() { func (c *CommandRecorder) End() {
close(c.queue) select {
case <-c.closed:
return
default:
}
close(c.closed)
} }
func (c *CommandRecorder) record() { func (c *CommandRecorder) record() {
cmdList := make([]*model.Command, 0) cmdList := make([]*model.Command, 0)
for { for {
select { select {
case <-c.closed:
if len(cmdList) == 0 {
return
}
case p, ok := <-c.queue: case p, ok := <-c.queue:
if !ok { if !ok {
logger.Debug("Session command recorder close: ", c.Session.Id) logger.Debug("Session command recorder close: ", c.Session.Id)
...@@ -79,7 +90,6 @@ func (c *CommandRecorder) record() { ...@@ -79,7 +90,6 @@ func (c *CommandRecorder) record() {
continue continue
} }
} }
err := c.storage.BulkSave(cmdList) err := c.storage.BulkSave(cmdList)
if err == nil { if err == nil {
cmdList = cmdList[:0] cmdList = cmdList[:0]
......
...@@ -4,7 +4,7 @@ import ( ...@@ -4,7 +4,7 @@ import (
"context" "context"
"time" "time"
"github.com/satori/go.uuid" uuid "github.com/satori/go.uuid"
"cocogo/pkg/i18n" "cocogo/pkg/i18n"
"cocogo/pkg/logger" "cocogo/pkg/logger"
...@@ -51,7 +51,7 @@ func (s *SwitchSession) Initial() { ...@@ -51,7 +51,7 @@ func (s *SwitchSession) Initial() {
s.SystemUser = s.srvConn.User() s.SystemUser = s.srvConn.User()
s.LoginFrom = s.userConn.LoginFrom() s.LoginFrom = s.userConn.LoginFrom()
s.RemoteAddr = s.userConn.RemoteAddr() s.RemoteAddr = s.userConn.RemoteAddr()
s.DateStart = time.Now() s.DateStart = time.Now().UTC()
s.cmdRecorder = NewCommandRecorder(s) s.cmdRecorder = NewCommandRecorder(s)
s.replayRecorder = NewReplyRecord(s) s.replayRecorder = NewReplyRecord(s)
...@@ -77,11 +77,13 @@ func (s *SwitchSession) recordCmd() { ...@@ -77,11 +77,13 @@ func (s *SwitchSession) recordCmd() {
} }
func (s *SwitchSession) postBridge() { func (s *SwitchSession) postBridge() {
s.cmdRecorder.End() s.DateEnd = time.Now().UTC()
s.replayRecorder.End()
s.parser.Close()
_ = s.userTran.Close() _ = s.userTran.Close()
_ = s.srvTran.Close() _ = s.srvTran.Close()
s.parser.Close()
s.replayRecorder.End()
s.cmdRecorder.End()
} }
func (s *SwitchSession) Bridge() (err error) { func (s *SwitchSession) Bridge() (err error) {
...@@ -94,6 +96,7 @@ func (s *SwitchSession) Bridge() (err error) { ...@@ -94,6 +96,7 @@ func (s *SwitchSession) Bridge() (err error) {
}() }()
go s.parser.Parse() go s.parser.Parse()
go s.recordCmd()
defer s.postBridge() defer s.postBridge()
for { for {
select { select {
......
...@@ -84,5 +84,9 @@ func PushSessionReplay(sessionID, gZipFile string) { ...@@ -84,5 +84,9 @@ func PushSessionReplay(sessionID, gZipFile string) {
} }
func PushSessionCommand(commands []*model.Command) (err error) { func PushSessionCommand(commands []*model.Command) (err error) {
err = authClient.Post(SessionCommandURL, commands, nil)
if err != nil {
logger.Error(err)
}
return return
} }
...@@ -20,9 +20,10 @@ const ( ...@@ -20,9 +20,10 @@ const (
TerminalConfigURL = "/api/terminal/v1/terminal/config/" // 从jumpserver获取coco的配置 TerminalConfigURL = "/api/terminal/v1/terminal/config/" // 从jumpserver获取coco的配置
TerminalHeartBeatURL = "/api/terminal/v1/terminal/status/" TerminalHeartBeatURL = "/api/terminal/v1/terminal/status/"
SessionListURL = "/api/terminal/v1/sessions/" //上传创建的资产会话session id SessionListURL = "/api/terminal/v1/sessions/" //上传创建的资产会话session id
SessionDetailURL = "/api/terminal/v1/sessions/%s/" // finish session的时候发送 SessionDetailURL = "/api/terminal/v1/sessions/%s/" // finish session的时候发送
SessionReplayURL = "/api/terminal/v1/sessions/%s/replay/" //上传录像 SessionReplayURL = "/api/terminal/v1/sessions/%s/replay/" //上传录像
SessionCommandURL = "/api/terminal/v1/command/" //上传批量命令
FinishTaskURL = "/api/terminal/v1/tasks/%s/" FinishTaskURL = "/api/terminal/v1/tasks/%s/"
......
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