Skip to content

Commit

Permalink
Add ssh keep alive to ssh tunnel to workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
janekbaraniewski committed Nov 26, 2024
1 parent 8f2b433 commit 31416db
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ func (cmd *SSHCmd) startTunnel(ctx context.Context, devPodConfig *config.Config,
// Traffic is coming in from the outside, we need to forward it to the container
if cmd.Proxy || cmd.Stdio {
if cmd.Proxy {
go startSSHKeepAlive(ctx, containerClient, log)

go func() {
if err := cmd.startRunnerServices(ctx, devPodConfig, containerClient, log); err != nil {
log.Error(err)
Expand All @@ -451,6 +453,7 @@ func (cmd *SSHCmd) startTunnel(ctx context.Context, devPodConfig *config.Config,
!cmd.Proxy && cmd.AgentForwarding &&
devPodConfig.ContextOption(config.ContextOptionSSHAgentForwarding) == "true",
func(ctx context.Context, stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
go startSSHKeepAlive(ctx, containerClient, log)
return devssh.Run(ctx, containerClient, command, stdin, stdout, stderr, envVars)
},
writer,
Expand Down Expand Up @@ -659,3 +662,21 @@ func preparePipes() (io.Reader, io.WriteCloser, io.Reader, io.WriteCloser, error

return stdoutReader, stdoutWriter, stdinReader, stdinWriter, nil
}

func startSSHKeepAlive(ctx context.Context, client *ssh.Client, log log.Logger) {
ticker := time.NewTicker(55 * time.Second)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
log.Infof("SSH keepalive routine stopped: %v", ctx.Err())
return
case <-ticker.C:
_, _, err := client.SendRequest("[email protected]", true, nil)
if err != nil {
log.Errorf("Failed to send keepalive: %w", err)
}
}
}
}

0 comments on commit 31416db

Please sign in to comment.