Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ssh-gateway] improve send heartbeat and add track #10965

Merged
merged 2 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions components/ws-proxy/pkg/sshproxy/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"sync"
"time"

"github.com/gitpod-io/gitpod/common-go/analytics"
"github.com/gitpod-io/gitpod/common-go/log"
tracker "github.com/gitpod-io/gitpod/ws-proxy/pkg/analytics"
"github.com/gitpod-io/golang-crypto/ssh"
"golang.org/x/net/context"
)
Expand All @@ -29,7 +31,7 @@ func (s *Server) ChannelForward(ctx context.Context, session *Session, targetCon
return
}
if originChannel.ChannelType() == "session" {
originChan = startHeartbeatingChannel(originChan, s.Heartbeater, session.InstanceID)
originChan = startHeartbeatingChannel(originChan, s.Heartbeater, session)
}
defer originChan.Close()

Expand All @@ -40,9 +42,10 @@ func (s *Server) ChannelForward(ctx context.Context, session *Session, targetCon
switch req.Type {
case "pty-req", "shell":
log.WithFields(log.OWI("", session.WorkspaceID, session.InstanceID)).Debugf("forwarding %s request", req.Type)
if req.WantReply {
req.Reply(true, []byte{})
req.WantReply = false
if channel, ok := originChan.(*heartbeatingChannel); ok && req.Type == "pty-req" {
channel.mux.Lock()
channel.requestedPty = true
channel.mux.Unlock()
}
}
maskedReqs <- req
Expand Down Expand Up @@ -89,7 +92,19 @@ func (s *Server) ChannelForward(ctx context.Context, session *Session, targetCon
log.WithFields(log.OWI("", session.WorkspaceID, session.InstanceID)).Debug("session forward stop")
}

func startHeartbeatingChannel(c ssh.Channel, heartbeat Heartbeat, instanceID string) ssh.Channel {
func TrackIDECloseSignal(session *Session) {
propertics := make(map[string]interface{})
propertics["workspaceId"] = session.WorkspaceID
propertics["instanceId"] = session.InstanceID
propertics["clientKind"] = "ssh"
tracker.Track(analytics.TrackMessage{
Identity: analytics.Identity{UserID: session.OwnerUserId},
Event: "ide_close_signal",
Properties: propertics,
})
}

func startHeartbeatingChannel(c ssh.Channel, heartbeat Heartbeat, session *Session) ssh.Channel {
ctx, cancel := context.WithCancel(context.Background())
res := &heartbeatingChannel{
Channel: c,
Expand All @@ -101,15 +116,19 @@ func startHeartbeatingChannel(c ssh.Channel, heartbeat Heartbeat, instanceID str
select {
case <-res.t.C:
res.mux.Lock()
if !res.sawActivity {
if !res.sawActivity || !res.requestedPty {
res.mux.Unlock()
continue
}
res.sawActivity = false
res.mux.Unlock()
heartbeat.SendHeartbeat(instanceID, false)
heartbeat.SendHeartbeat(session.InstanceID, false)
case <-ctx.Done():
heartbeat.SendHeartbeat(instanceID, true)
if res.requestedPty {
heartbeat.SendHeartbeat(session.InstanceID, true)
TrackIDECloseSignal(session)
log.WithField("instanceId", session.InstanceID).Info("send closed heartbeat")
}
return
}
}
Expand All @@ -126,6 +145,8 @@ type heartbeatingChannel struct {
t *time.Ticker

cancel context.CancelFunc

requestedPty bool
}

// Read reads up to len(data) bytes from the channel.
Expand Down
2 changes: 2 additions & 0 deletions components/ws-proxy/pkg/sshproxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type Session struct {

WorkspaceID string
InstanceID string
OwnerUserId string

PublicKey ssh.PublicKey
WorkspacePrivateKey ssh.Signer
Expand Down Expand Up @@ -251,6 +252,7 @@ func (s *Server) HandleConn(c net.Conn) {
Conn: clientConn,
WorkspaceID: workspaceId,
InstanceID: wsInfo.InstanceID,
OwnerUserId: wsInfo.OwnerUserId,
WorkspacePrivateKey: key,
}
remoteAddr := wsInfo.IPAddress + ":23001"
Expand Down