MCPcopy
hub / github.com/gitpod-io/gitpod / ChannelForward

Method ChannelForward

components/ws-proxy/pkg/sshproxy/forward.go:19–93  ·  view source on GitHub ↗
(ctx context.Context, session *Session, targetConn ssh.Conn, originChannel ssh.NewChannel)

Source from the content-addressed store, hash-verified

17)
18
19func (s *Server) ChannelForward(ctx context.Context, session *Session, targetConn ssh.Conn, originChannel ssh.NewChannel) {
20 targetChan, targetReqs, err := targetConn.OpenChannel(originChannel.ChannelType(), originChannel.ExtraData())
21 if err != nil {
22 log.WithFields(log.OWI("", session.WorkspaceID, session.InstanceID)).Error("open target channel error")
23 originChannel.Reject(ssh.ConnectionFailed, "open target channel error")
24 return
25 }
26 defer targetChan.Close()
27
28 originChan, originReqs, err := originChannel.Accept()
29 if err != nil {
30 log.WithFields(log.OWI("", session.WorkspaceID, session.InstanceID)).Error("accept origin channel failed")
31 return
32 }
33 if originChannel.ChannelType() == "session" {
34 originChan = startHeartbeatingChannel(originChan, s.Heartbeater, session)
35 }
36 defer originChan.Close()
37
38 maskedReqs := make(chan *ssh.Request, 1)
39
40 go func() {
41 for req := range originReqs {
42 switch req.Type {
43 case "pty-req", "shell":
44 log.WithFields(log.OWI("", session.WorkspaceID, session.InstanceID)).Debugf("forwarding %s request", req.Type)
45 if channel, ok := originChan.(*heartbeatingChannel); ok && req.Type == "pty-req" {
46 channel.mux.Lock()
47 channel.requestedPty = true
48 channel.mux.Unlock()
49 }
50 }
51 maskedReqs <- req
52 }
53 close(maskedReqs)
54 }()
55
56 go func() {
57 io.Copy(targetChan, originChan)
58 targetChan.CloseWrite()
59 }()
60
61 go func() {
62 io.Copy(originChan, targetChan)
63 originChan.CloseWrite()
64 }()
65
66 wg := sync.WaitGroup{}
67 forward := func(sourceReqs <-chan *ssh.Request, targetChan ssh.Channel) {
68 defer wg.Done()
69 for ctx.Err() == nil {
70 select {
71 case req, ok := <-sourceReqs:
72 if !ok {
73 targetChan.Close()
74 return
75 }
76 b, err := targetChan.SendRequest(req.Type, req.WantReply, req.Payload)

Callers 1

HandleConnMethod · 0.95

Calls 11

startHeartbeatingChannelFunction · 0.85
AcceptMethod · 0.80
DebugMethod · 0.80
CloseMethod · 0.65
closeFunction · 0.50
ErrorMethod · 0.45
OWIMethod · 0.45
DoneMethod · 0.45
ErrMethod · 0.45
AddMethod · 0.45
WaitMethod · 0.45

Tested by

no test coverage detected