Skip to content

Commit 614f154

Browse files
committed
Fix crash when user connects to rssh client, then closes their entire session, add recover to ensure all command handlers cannot crash server, closes #196
1 parent 3a475aa commit 614f154

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

internal/server/commands/connect.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,12 @@ func attachSession(newSession ssh.Channel, currentClientSession io.ReadWriter, c
184184
RequestsProxyPasser:
185185
for {
186186
select {
187-
case r := <-currentClientRequests:
187+
case r, ok := <-currentClientRequests:
188+
if !ok || r == nil {
189+
// user has disconnected
190+
return nil
191+
}
192+
188193
response, err := internal.SendRequest(*r, newSession)
189194
if err != nil {
190195
break RequestsProxyPasser

internal/server/handlers/session.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/binary"
55
"fmt"
66
"io"
7+
"runtime/debug"
78

89
"github.com/NHAS/reverse_ssh/internal"
910
"github.com/NHAS/reverse_ssh/internal/server/commands"
@@ -47,6 +48,13 @@ func Session(datadir string) ChannelHandler {
4748
}
4849
defer connection.Close()
4950

51+
defer func() {
52+
if r := recover(); r != nil {
53+
fmt.Println("Recovered a panic:", r)
54+
debug.PrintStack()
55+
}
56+
}()
57+
5058
sess.ShellRequests = requests
5159

5260
for req := range requests {

internal/terminal/terminal.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ func (t *Terminal) handleWindowSize() {
203203
return
204204
case req := <-t.session.ShellRequests:
205205
if req == nil { // Channel has closed, so therefore end this default handler
206+
206207
return
207208
}
208209

0 commit comments

Comments
 (0)