feat(access): add --pidfile flag to tcp subcommand

This commit is contained in:
Hugh 2026-01-06 18:23:14 -08:00
parent 0d2a7a0385
commit d6a24c1ede
2 changed files with 27 additions and 0 deletions

View File

@ -5,8 +5,10 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"os"
"strings" "strings"
"github.com/mitchellh/go-homedir"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -69,6 +71,10 @@ func ssh(c *cli.Context) error {
} }
log := logger.CreateSSHLoggerFromContext(c, outputTerminal) log := logger.CreateSSHLoggerFromContext(c, outputTerminal)
if c.IsSet(sshPidFileFlag) {
writePidFile(c.String(sshPidFileFlag), log)
}
// get the hostname from the cmdline and error out if its not provided // get the hostname from the cmdline and error out if its not provided
rawHostName := c.String(sshHostnameFlag) rawHostName := c.String(sshHostnameFlag)
url, err := parseURL(rawHostName) url, err := parseURL(rawHostName)
@ -145,3 +151,18 @@ func ssh(c *cli.Context) error {
} }
return carrier.StartClient(wsConn, s, options) return carrier.StartClient(wsConn, s, options)
} }
func writePidFile(path string, log *zerolog.Logger) {
expandedPath, err := homedir.Expand(path)
if err != nil {
log.Err(err).Str("path", path).Msg("Unable to expand pidfile path")
return
}
file, err := os.Create(expandedPath)
if err != nil {
log.Err(err).Str("path", expandedPath).Msg("Unable to write pidfile")
return
}
defer file.Close()
fmt.Fprintf(file, "%d", os.Getpid())
}

View File

@ -38,6 +38,7 @@ const (
sshGenCertFlag = "short-lived-cert" sshGenCertFlag = "short-lived-cert"
sshConnectTo = "connect-to" sshConnectTo = "connect-to"
sshDebugStream = "debug-stream" sshDebugStream = "debug-stream"
sshPidFileFlag = "pidfile"
sshConfigTemplate = ` sshConfigTemplate = `
Add to your {{.Home}}/.ssh/config: Add to your {{.Home}}/.ssh/config:
@ -204,6 +205,11 @@ func Commands() []*cli.Command {
Hidden: true, Hidden: true,
Usage: "Writes up-to the max provided stream payloads to the logger as debug statements.", Usage: "Writes up-to the max provided stream payloads to the logger as debug statements.",
}, },
&cli.StringFlag{
Name: sshPidFileFlag,
Usage: "Write the application's PID to this file",
EnvVars: []string{"TUNNEL_SERVICE_PIDFILE"},
},
}, },
}, },
{ {