Skip to content

Commit c9e327e

Browse files
committed
Remove Handler getters and setters
1 parent 7785627 commit c9e327e

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

server.go

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,15 @@ type Server struct {
3737
IdleTimeout time.Duration // connection timeout when no activity, none if empty
3838
MaxTimeout time.Duration // absolute connection timeout, none if empty
3939

40-
channelHandlers map[string]ChannelHandler // fallback channel handlers
41-
requestHandlers map[string]RequestHandler
40+
// ChannelHandlers allow overriding the built-in session handlers or provide
41+
// extensions to the protocol, such as tcpip forwarding. By default only the
42+
// "session" handler is enabled.
43+
ChannelHandlers map[string]ChannelHandler
44+
45+
// RequestHandlers allow overriding the server-level request handlers or
46+
// provide extensions to the protocol, such as tcpip forwarding. By default
47+
// no handlers are enabled.
48+
RequestHandlers map[string]RequestHandler
4249

4350
listenerWg sync.WaitGroup
4451
mu sync.Mutex
@@ -82,14 +89,14 @@ func (srv *Server) ensureHostSigner() error {
8289
func (srv *Server) ensureHandlers() {
8390
srv.mu.Lock()
8491
defer srv.mu.Unlock()
85-
if srv.requestHandlers == nil {
86-
srv.requestHandlers = map[string]RequestHandler{
92+
if srv.RequestHandlers == nil {
93+
srv.RequestHandlers = map[string]RequestHandler{
8794
"tcpip-forward": forwardedTCPHandler{},
8895
"cancel-tcpip-forward": forwardedTCPHandler{},
8996
}
9097
}
91-
if srv.channelHandlers == nil {
92-
srv.channelHandlers = map[string]ChannelHandler{
98+
if srv.ChannelHandlers == nil {
99+
srv.ChannelHandlers = map[string]ChannelHandler{
93100
"session": ChannelHandlerFunc(sessionHandler),
94101
"direct-tcpip": ChannelHandlerFunc(directTcpipHandler),
95102
}
@@ -234,18 +241,6 @@ func (srv *Server) Serve(l net.Listener) error {
234241
}
235242
}
236243

237-
func (srv *Server) SetChannelHandler(kind string, handler ChannelHandler) {
238-
srv.ensureHandlers()
239-
srv.mu.Lock()
240-
defer srv.mu.Unlock()
241-
srv.channelHandlers[kind] = handler
242-
}
243-
244-
func (srv *Server) ChannelHandler(kind string) ChannelHandler {
245-
srv.ensureHandlers()
246-
return srv.channelHandlers[kind]
247-
}
248-
249244
func (srv *Server) handleConn(newConn net.Conn) {
250245
if srv.ConnCallback != nil {
251246
cbConn := srv.ConnCallback(newConn)
@@ -279,11 +274,9 @@ func (srv *Server) handleConn(newConn net.Conn) {
279274
//go gossh.DiscardRequests(reqs)
280275
go srv.handleRequests(ctx, reqs)
281276
for ch := range chans {
282-
handler, found := srv.channelHandlers[ch.ChannelType()]
283-
if !found || handler == nil {
284-
if defaultHandler, found := srv.channelHandlers["default"]; found {
285-
handler = defaultHandler
286-
}
277+
handler := srv.ChannelHandlers[ch.ChannelType()]
278+
if handler == nil {
279+
handler = srv.ChannelHandlers["default"]
287280
}
288281
if handler == nil {
289282
ch.Reject(gossh.UnknownChannelType, "unsupported channel type")
@@ -295,8 +288,11 @@ func (srv *Server) handleConn(newConn net.Conn) {
295288

296289
func (srv *Server) handleRequests(ctx Context, in <-chan *gossh.Request) {
297290
for req := range in {
298-
handler, found := srv.requestHandlers[req.Type]
299-
if !found {
291+
handler := srv.RequestHandlers[req.Type]
292+
if handler == nil {
293+
handler = srv.RequestHandlers["default"]
294+
}
295+
if handler == nil {
300296
if req.WantReply {
301297
req.Reply(false, nil)
302298
}

session_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func (srv *Server) serveOnce(l net.Listener) error {
1919
if e != nil {
2020
return e
2121
}
22-
srv.channelHandlers = map[string]ChannelHandler{
22+
srv.ChannelHandlers = map[string]ChannelHandler{
2323
"session": ChannelHandlerFunc(sessionHandler),
2424
"direct-tcpip": ChannelHandlerFunc(directTcpipHandler),
2525
}

0 commit comments

Comments
 (0)