diff --git a/server.go b/server.go index 7dbaa0fa..c6032b18 100644 --- a/server.go +++ b/server.go @@ -45,6 +45,7 @@ type Server struct { PublicKeyHandler PublicKeyHandler // public key authentication handler PtyCallback PtyCallback // callback for allowing PTY sessions, allows all if nil ConnCallback ConnCallback // optional callback for wrapping net.Conn before handling + DisconnectCallback DisconnectCallback // optional callback executed when net.Conn disconnects LocalPortForwardingCallback LocalPortForwardingCallback // callback for allowing local port forwarding, denies all if nil ReversePortForwardingCallback ReversePortForwardingCallback // callback for allowing reverse port forwarding, denies all if nil ServerConfigCallback ServerConfigCallback // callback for configuring detailed SSH options @@ -307,6 +308,11 @@ func (srv *Server) HandleConn(newConn net.Conn) { conn.updateDeadline() srv.trackConn(sshConn, true) defer srv.trackConn(sshConn, false) + defer func() { + if srv.DisconnectCallback != nil { + srv.DisconnectCallback(ctx, conn) + } + }() ctx.SetValue(ContextKeyConn, sshConn) applyConnMetadata(ctx, sshConn) diff --git a/ssh.go b/ssh.go index 775b454f..834541a1 100644 --- a/ssh.go +++ b/ssh.go @@ -71,6 +71,11 @@ 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) +// DisconnectCallback is a hook for reporting closed connections +// (that is, connection that did not fire ConnectionFailedCallback) +// Please note: the net.Conn is likely closed or unusable at this point +type DisconnectCallback func(ctx Context, conn net.Conn) + // Window represents the size of a PTY window. type Window struct { Width int