Skip to content

Commit bb0e5c0

Browse files
committed
Simplify handlers to remove additional types
1 parent f199e8c commit bb0e5c0

File tree

3 files changed

+16
-32
lines changed

3 files changed

+16
-32
lines changed

server.go

+12-28
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ import (
1515
// and ListenAndServeTLS methods after a call to Shutdown or Close.
1616
var ErrServerClosed = errors.New("ssh: Server closed")
1717

18+
type RequestHandler func(ctx Context, srv *Server, req *gossh.Request) (ok bool, payload []byte)
19+
20+
var DefaultRequestHandlers = map[string]RequestHandler{}
21+
22+
type ChannelHandler func(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx Context)
23+
24+
var DefaultChannelHandlers = map[string]ChannelHandler{
25+
"session": DefaultSessionHandler,
26+
}
27+
1828
// Server defines parameters for running an SSH server. The zero value for
1929
// Server is a valid configuration. When both PasswordHandler and
2030
// PublicKeyHandler are nil, no client authentication is performed.
@@ -55,32 +65,6 @@ type Server struct {
5565
doneChan chan struct{}
5666
}
5767

58-
type RequestHandler interface {
59-
HandleSSHRequest(ctx Context, srv *Server, req *gossh.Request) (ok bool, payload []byte)
60-
}
61-
62-
type RequestHandlerFunc func(ctx Context, srv *Server, req *gossh.Request) (ok bool, payload []byte)
63-
64-
func (f RequestHandlerFunc) HandleSSHRequest(ctx Context, srv *Server, req *gossh.Request) (ok bool, payload []byte) {
65-
return f(ctx, srv, req)
66-
}
67-
68-
var DefaultRequestHandlers = map[string]RequestHandler{}
69-
70-
type ChannelHandler interface {
71-
HandleSSHChannel(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx Context)
72-
}
73-
74-
type ChannelHandlerFunc func(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx Context)
75-
76-
func (f ChannelHandlerFunc) HandleSSHChannel(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx Context) {
77-
f(srv, conn, newChan, ctx)
78-
}
79-
80-
var DefaultChannelHandlers = map[string]ChannelHandler{
81-
"session": ChannelHandlerFunc(DefaultSessionHandler),
82-
}
83-
8468
func (srv *Server) ensureHostSigner() error {
8569
if len(srv.HostSigners) == 0 {
8670
signer, err := generateSigner()
@@ -288,7 +272,7 @@ func (srv *Server) handleConn(newConn net.Conn) {
288272
ch.Reject(gossh.UnknownChannelType, "unsupported channel type")
289273
continue
290274
}
291-
go handler.HandleSSHChannel(srv, sshConn, ch, ctx)
275+
go handler(srv, sshConn, ch, ctx)
292276
}
293277
}
294278

@@ -304,7 +288,7 @@ func (srv *Server) handleRequests(ctx Context, in <-chan *gossh.Request) {
304288
}
305289
/*reqCtx, cancel := context.WithCancel(ctx)
306290
defer cancel() */
307-
ret, payload := handler.HandleSSHRequest(ctx, srv, req)
291+
ret, payload := handler(ctx, srv, req)
308292
req.Reply(ret, payload)
309293
}
310294
}

session_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ func (srv *Server) serveOnce(l net.Listener) error {
2020
return e
2121
}
2222
srv.ChannelHandlers = map[string]ChannelHandler{
23-
"session": ChannelHandlerFunc(DefaultSessionHandler),
24-
"direct-tcpip": ChannelHandlerFunc(DirectTCPIPHandler),
23+
"session": DefaultSessionHandler,
24+
"direct-tcpip": DirectTCPIPHandler,
2525
}
2626
srv.handleConn(conn)
2727
return nil

tcpip.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ type remoteForwardChannelData struct {
8787
}
8888

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

0 commit comments

Comments
 (0)