Simplify handlers to remove additional types
This commit is contained in:
parent
f199e8cd1e
commit
bb0e5c0692
40
server.go
40
server.go
@ -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.
|
||||
@ -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()
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
4
tcpip.go
4
tcpip.go
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user