Skip to content

feat: adds callback call when connection ends #4

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

Merged
merged 1 commit into from
Jun 21, 2023
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
10 changes: 9 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ type Server struct {
ServerConfigCallback ServerConfigCallback // callback for configuring detailed SSH options
SessionRequestCallback SessionRequestCallback // callback for allowing or denying SSH sessions

ConnectionFailedCallback ConnectionFailedCallback // callback to report connection failures
// server calls Failed callback for connections that fail initial handshake, and Complete callback for those that
// succeed, never both.
ConnectionFailedCallback ConnectionFailedCallback // callback to report connection failures
ConnectionCompleteCallback ConnectionCompleteCallback // callback to report connection completion

IdleTimeout time.Duration // connection timeout when no activity, none if empty
MaxTimeout time.Duration // absolute connection timeout, none if empty
Expand Down Expand Up @@ -295,6 +298,11 @@ func (srv *Server) HandleConn(newConn net.Conn) {
}
return
}
if srv.ConnectionCompleteCallback != nil {
defer func() {
srv.ConnectionCompleteCallback(sshConn, sshConn.Wait())
}()
}

srv.trackConn(sshConn, true)
defer srv.trackConn(sshConn, false)
Expand Down
8 changes: 8 additions & 0 deletions ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ 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)

// ConnectionCompleteCallback is a hook for reporting connections that
// complete. The included error is from the underlying SSH transport
// protocol mux (golang.org/x/crypto/ssh), and is non-nil, even for
// normal termination.
//
// Please note: the ServerConn is closed at this point
type ConnectionCompleteCallback func(conn *gossh.ServerConn, err error)

// Window represents the size of a PTY window.
//
// See https://datatracker.ietf.org/doc/html/rfc4254#section-6.2
Expand Down