Skip to content

Commit 7785627

Browse files
committed
Clean up Channel and Request Handler interfaces
1 parent 75b6954 commit 7785627

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

server.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,26 @@ type Server struct {
4747
connWg sync.WaitGroup
4848
doneChan chan struct{}
4949
}
50+
5051
type RequestHandler interface {
51-
HandleRequest(ctx Context, srv *Server, req *gossh.Request) (ok bool, payload []byte)
52+
HandleSSHRequest(ctx Context, srv *Server, req *gossh.Request) (ok bool, payload []byte)
53+
}
54+
55+
type RequestHandlerFunc func(ctx Context, srv *Server, req *gossh.Request) (ok bool, payload []byte)
56+
57+
func (f RequestHandlerFunc) HandleSSHRequest(ctx Context, srv *Server, req *gossh.Request) (ok bool, payload []byte) {
58+
return f(ctx, srv, req)
5259
}
5360

54-
type ChannelHandler func(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx Context)
61+
type ChannelHandler interface {
62+
HandleSSHChannel(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx Context)
63+
}
64+
65+
type ChannelHandlerFunc func(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx Context)
66+
67+
func (f ChannelHandlerFunc) HandleSSHChannel(srv *Server, conn *gossh.ServerConn, newChan gossh.NewChannel, ctx Context) {
68+
f(srv, conn, newChan, ctx)
69+
}
5570

5671
func (srv *Server) ensureHostSigner() error {
5772
if len(srv.HostSigners) == 0 {
@@ -75,8 +90,8 @@ func (srv *Server) ensureHandlers() {
7590
}
7691
if srv.channelHandlers == nil {
7792
srv.channelHandlers = map[string]ChannelHandler{
78-
"session": sessionHandler,
79-
"direct-tcpip": directTcpipHandler,
93+
"session": ChannelHandlerFunc(sessionHandler),
94+
"direct-tcpip": ChannelHandlerFunc(directTcpipHandler),
8095
}
8196
}
8297
}
@@ -274,7 +289,7 @@ func (srv *Server) handleConn(newConn net.Conn) {
274289
ch.Reject(gossh.UnknownChannelType, "unsupported channel type")
275290
continue
276291
}
277-
go handler(srv, sshConn, ch, ctx)
292+
go handler.HandleSSHChannel(srv, sshConn, ch, ctx)
278293
}
279294
}
280295

@@ -289,7 +304,7 @@ func (srv *Server) handleRequests(ctx Context, in <-chan *gossh.Request) {
289304
}
290305
/*reqCtx, cancel := context.WithCancel(ctx)
291306
defer cancel() */
292-
ret, payload := handler.HandleRequest(ctx, srv, req)
307+
ret, payload := handler.HandleSSHRequest(ctx, srv, req)
293308
if req.WantReply {
294309
req.Reply(ret, payload)
295310
}

session_test.go

Lines changed: 2 additions & 2 deletions
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": sessionHandler,
24-
"direct-tcpip": directTcpipHandler,
23+
"session": ChannelHandlerFunc(sessionHandler),
24+
"direct-tcpip": ChannelHandlerFunc(directTcpipHandler),
2525
}
2626
srv.handleConn(conn)
2727
return nil

tcpip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ type forwardedTCPHandler struct {
8989
sync.Mutex
9090
}
9191

92-
func (h forwardedTCPHandler) HandleRequest(ctx Context, srv *Server, req *gossh.Request) (bool, []byte) {
92+
func (h forwardedTCPHandler) HandleSSHRequest(ctx Context, srv *Server, req *gossh.Request) (bool, []byte) {
9393
h.Lock()
9494
if h.forwards == nil {
9595
h.forwards = make(map[string]net.Listener)

0 commit comments

Comments
 (0)