diff --git a/shell.go b/shell.go index d82e1a3c..253414c3 100644 --- a/shell.go +++ b/shell.go @@ -2066,6 +2066,7 @@ GLOBAL OPTIONS: Usage: "Lists sessions", Flags: []cli.Flag{ cli.BoolFlag{Name: "latest, l", Usage: "Show the latest session"}, + cli.BoolFlag{Name: "active, a", Usage: "Show only active session"}, cli.BoolFlag{Name: "quiet, q", Usage: "Only display IDs"}, }, Action: func(c *cli.Context) error { @@ -2074,7 +2075,14 @@ GLOBAL OPTIONS: } var sessions []*Session - query := db.Order("created_at desc").Preload("User").Preload("Host") + + limit, offset, status := 60000, -1, []string{string(SessionStatusActive), string(SessionStatusClosed), string(SessionStatusUnknown)} + if c.Bool("active") { + status = status[:1] + } + + query := db.Order("created_at desc").Limit(limit).Offset(offset).Where("status in (?)", status).Preload("User").Preload("Host") + if c.Bool("latest") { var session Session if err := query.First(&session).Error; err != nil { @@ -2085,6 +2093,20 @@ GLOBAL OPTIONS: if err := query.Find(&sessions).Error; err != nil { return err } + + factor := 1 + for len(sessions) >= limit*factor { + + var additionnalSessions []*Session + + offset = limit * factor + query := db.Order("created_at desc").Limit(limit).Offset(offset).Where("status in (?)", status).Preload("User").Preload("Host") + if err := query.Find(&additionnalSessions).Error; err != nil { + return err + } + sessions = append(sessions, additionnalSessions...) + factor++ + } } if c.Bool("quiet") { for _, session := range sessions { diff --git a/ssh.go b/ssh.go index d68e915d..97d963f6 100644 --- a/ssh.go +++ b/ssh.go @@ -163,19 +163,19 @@ func channelHandler(srv *ssh.Server, conn *gossh.ServerConn, newChan gossh.NewCh if err != nil { log.Printf("Error: %v", err) } - }() - now := time.Now() - sessUpdate := Session{ - Status: SessionStatusClosed, - ErrMsg: fmt.Sprintf("%v", err), - StoppedAt: &now, - } - switch sessUpdate.ErrMsg { - case "lch closed the connection", "rch closed the connection": - sessUpdate.ErrMsg = "" - } - actx.db.Model(&sess).Updates(&sessUpdate) + now := time.Now() + sessUpdate := Session{ + Status: SessionStatusClosed, + ErrMsg: fmt.Sprintf("%v", err), + StoppedAt: &now, + } + switch sessUpdate.ErrMsg { + case "lch closed the connection", "rch closed the connection": + sessUpdate.ErrMsg = "" + } + actx.db.Model(&sess).Updates(&sessUpdate) + }() case BastionSchemeTelnet: tmpSrv := ssh.Server{ // PtyCallback: srv.PtyCallback,