Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Session ls on big databases #76

Merged
merged 2 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
24 changes: 12 additions & 12 deletions ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down