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

add ConnectionClosedCallback #245

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Server struct {
SessionRequestCallback SessionRequestCallback // callback for allowing or denying SSH sessions

ConnectionFailedCallback ConnectionFailedCallback // callback to report connection failures
ConnectionClosedCallback ConnectionClosedCallback // callback to report closed connections

IdleTimeout time.Duration // connection timeout when no activity, none if empty
MaxTimeout time.Duration // absolute connection timeout, none if empty
Expand Down Expand Up @@ -317,6 +318,9 @@ func (srv *Server) HandleConn(newConn net.Conn) {
}
go handler(srv, sshConn, ch, ctx)
}
if srv.ConnectionClosedCallback != nil {
srv.ConnectionClosedCallback()
}
}

func (srv *Server) handleRequests(ctx Context, in <-chan *gossh.Request) {
Expand Down
20 changes: 20 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,23 @@ func TestServerClose(t *testing.T) {
return
}
}

func TestServerCloseConnectionCallback(t *testing.T) {
connectionCloseCallbackCalled := make(chan struct{})
testBytes := []byte("Hello world\n")
_, _, cleanup := newTestSession(t, &Server{
Handler: func(s Session) {
s.Write(testBytes)
},
ConnectionClosedCallback: func() {
connectionCloseCallbackCalled <- struct{}{}
},
}, nil)
cleanup()

select {
case <-connectionCloseCallbackCalled:
case <-time.After(10 * time.Millisecond):
t.Fail()
}
}
3 changes: 3 additions & 0 deletions ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ type ServerConfigCallback func(ctx Context) *gossh.ServerConfig
// Please note: the net.Conn is likely to be closed at this point
type ConnectionFailedCallback func(conn net.Conn, err error)

// ConnectionFailedCallback is a hook for handling closed connections
type ConnectionClosedCallback func()

// Window represents the size of a PTY window.
type Window struct {
Width int
Expand Down