Skip to content

Commit

Permalink
Merge pull request #1679 from BishopFox/v1.6.0/fix-console-hang
Browse files Browse the repository at this point in the history
Fix #1668
  • Loading branch information
moloch-- committed May 10, 2024
2 parents 4bfdb7b + 6350863 commit b520eaf
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions client/cli/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package cli
*/

import (
"context"
"fmt"
"os"

"github.com/bishopfox/sliver/client/assets"
"github.com/bishopfox/sliver/client/command"
Expand All @@ -28,6 +30,7 @@ import (
"github.com/bishopfox/sliver/protobuf/rpcpb"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
)

// consoleCmd generates the console with required pre/post runners.
Expand Down Expand Up @@ -70,6 +73,9 @@ func consoleRunnerCmd(con *console.SliverClient, run bool) (pre, post func(cmd *
return nil
}

// Wait for any connection state changes and exit if the connection is lost.
go handleConnectionLost(ln)

return console.StartClient(con, rpc, command.ServerCommands(con, nil), command.SliverCommands(con), run)
}

Expand All @@ -84,3 +90,16 @@ func consoleRunnerCmd(con *console.SliverClient, run bool) (pre, post func(cmd *

return pre, post
}

func handleConnectionLost(ln *grpc.ClientConn) {
currentState := ln.GetState()
// currentState should be "Ready" when the connection is established.
if ln.WaitForStateChange(context.Background(), currentState) {
newState := ln.GetState()
// newState will be "Idle" if the connection is lost.
if newState == connectivity.Idle {
fmt.Println("\nLost connection to server. Exiting now.")
os.Exit(1)
}
}
}

0 comments on commit b520eaf

Please sign in to comment.