| 17 | ) |
| 18 | |
| 19 | func (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) |