Commit 262803eb authored by Eric's avatar Eric

[Update] upload command record data

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