Skip to content

Commit 3fc4e8e

Browse files
committed
Refactor HandleChannels: extract channel relay logic into separate relayChannel function for clarity
1 parent c164503 commit 3fc4e8e

1 file changed

Lines changed: 18 additions & 16 deletions

File tree

internal/ssh/channels.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ func (s *Server) HandleChannels(chans <-chan ssh.NewChannel) {
2828
continue
2929
}
3030

31-
targetHost := string(extra[4 : 4+l])
32-
targetPort := binary.BigEndian.Uint32(extra[4+l : 4+l+4])
31+
host := string(extra[4 : 4+l])
32+
port := binary.BigEndian.Uint32(extra[4+l : 4+l+4])
3333

3434
ch, reqs, err := newChannel.Accept()
3535
if err != nil {
@@ -38,19 +38,21 @@ func (s *Server) HandleChannels(chans <-chan ssh.NewChannel) {
3838

3939
go ssh.DiscardRequests(reqs)
4040

41-
go func() {
42-
defer ch.Close()
43-
targetConn, err := net.Dial("tcp", net.JoinHostPort(targetHost, strconv.Itoa(int(targetPort))))
44-
if err != nil {
45-
return
46-
}
47-
defer targetConn.Close()
48-
49-
go func() {
50-
io.Copy(targetConn, ch)
51-
targetConn.Close()
52-
}()
53-
io.Copy(ch, targetConn)
54-
}()
41+
go s.relayChannel(ch, host, port)
5542
}
5643
}
44+
45+
func (s *Server) relayChannel(ch ssh.Channel, host string, port uint32) {
46+
defer ch.Close()
47+
conn, err := net.Dial("tcp", net.JoinHostPort(host, strconv.Itoa(int(port))))
48+
if err != nil {
49+
return
50+
}
51+
defer conn.Close()
52+
53+
go func() {
54+
io.Copy(conn, ch)
55+
conn.Close()
56+
}()
57+
io.Copy(ch, conn)
58+
}

0 commit comments

Comments
 (0)