Skip to content

Commit

Permalink
Simplify handlers to remove additional types
Browse files Browse the repository at this point in the history
  • Loading branch information
belak committed Jun 19, 2019
1 parent f199e8c commit bb0e5c0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
40 changes: 12 additions & 28 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ import (
// and ListenAndServeTLS methods after a call to Shutdown or Close.
var ErrServerClosed = errors.New("ssh: Server closed")

type RequestHandler func(ctx Context, srv *Server, req *gossh.Request) (ok bool, payload []byte)

var DefaultRequestHandlers = map[string]RequestHandler{}

type ChannelHandler func(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx Context)

var DefaultChannelHandlers = map[string]ChannelHandler{
"session": DefaultSessionHandler,
}

// Server defines parameters for running an SSH server. The zero value for
// Server is a valid configuration. When both PasswordHandler and
// PublicKeyHandler are nil, no client authentication is performed.
Expand Down Expand Up @@ -55,32 +65,6 @@ type Server struct {
doneChan chan struct{}
}

type RequestHandler interface {
HandleSSHRequest(ctx Context, srv *Server, req *gossh.Request) (ok bool, payload []byte)
}

type RequestHandlerFunc func(ctx Context, srv *Server, req *gossh.Request) (ok bool, payload []byte)

func (f RequestHandlerFunc) HandleSSHRequest(ctx Context, srv *Server, req *gossh.Request) (ok bool, payload []byte) {
return f(ctx, srv, req)
}

var DefaultRequestHandlers = map[string]RequestHandler{}

type ChannelHandler interface {
HandleSSHChannel(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx Context)
}

type ChannelHandlerFunc func(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx Context)

func (f ChannelHandlerFunc) HandleSSHChannel(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx Context) {
f(srv, conn, newChan, ctx)
}

var DefaultChannelHandlers = map[string]ChannelHandler{
"session": ChannelHandlerFunc(DefaultSessionHandler),
}

func (srv *Server) ensureHostSigner() error {
if len(srv.HostSigners) == 0 {
signer, err := generateSigner()
Expand Down Expand Up @@ -288,7 +272,7 @@ func (srv *Server) handleConn(newConn net.Conn) {
ch.Reject(gossh.UnknownChannelType, "unsupported channel type")
continue
}
go handler.HandleSSHChannel(srv, sshConn, ch, ctx)
go handler(srv, sshConn, ch, ctx)
}
}

Expand All @@ -304,7 +288,7 @@ func (srv *Server) handleRequests(ctx Context, in <-chan *gossh.Request) {
}
/*reqCtx, cancel := context.WithCancel(ctx)
defer cancel() */
ret, payload := handler.HandleSSHRequest(ctx, srv, req)
ret, payload := handler(ctx, srv, req)
req.Reply(ret, payload)
}
}
Expand Down
4 changes: 2 additions & 2 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func (srv *Server) serveOnce(l net.Listener) error {
return e
}
srv.ChannelHandlers = map[string]ChannelHandler{
"session": ChannelHandlerFunc(DefaultSessionHandler),
"direct-tcpip": ChannelHandlerFunc(DirectTCPIPHandler),
"session": DefaultSessionHandler,
"direct-tcpip": DirectTCPIPHandler,
}
srv.handleConn(conn)
return nil
Expand Down
4 changes: 2 additions & 2 deletions tcpip.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ type remoteForwardChannelData struct {
}

// ForwardedTCPHandler can be enabled by creating a ForwardedTCPHandler and
// adding it to the server's RequestHandlers under tcpip-forward and
// cancel-tcpip-forward.
// adding the HandleSSHRequest callback to the server's RequestHandlers under
// tcpip-forward and cancel-tcpip-forward.
type ForwardedTCPHandler struct {
forwards map[string]net.Listener
sync.Mutex
Expand Down

0 comments on commit bb0e5c0

Please sign in to comment.