1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
13 "git.arvados.org/arvados.git/sdk/go/ctxlog"
14 "github.com/sirupsen/logrus"
17 func (sshconn ContainerSSHConnection) ServeHTTP(w http.ResponseWriter, req *http.Request) {
18 hj, ok := w.(http.Hijacker)
20 http.Error(w, "ResponseWriter does not support connection upgrade", http.StatusInternalServerError)
23 w.Header().Set("Connection", "upgrade")
24 w.Header().Set("Upgrade", "ssh")
25 w.WriteHeader(http.StatusSwitchingProtocols)
26 conn, bufrw, err := hj.Hijack()
28 ctxlog.FromContext(req.Context()).WithError(err).Error("error hijacking ResponseWriter")
33 var bytesIn, bytesOut int64
35 ctx, cancel := context.WithCancel(context.Background())
40 n, err := io.CopyN(conn, sshconn.Bufrw, int64(sshconn.Bufrw.Reader.Buffered()))
43 n, err = io.Copy(conn, sshconn.Conn)
47 ctxlog.FromContext(req.Context()).WithError(err).Error("error copying downstream")
54 n, err := io.CopyN(sshconn.Conn, bufrw, int64(bufrw.Reader.Buffered()))
57 n, err = io.Copy(sshconn.Conn, conn)
61 ctxlog.FromContext(req.Context()).WithError(err).Error("error copying upstream")
65 if sshconn.Logger != nil {
68 sshconn.Logger.WithFields(logrus.Fields{
71 }).Info("closed connection")