Skip to content

Commit

Permalink
added check of a pre-existing bastion.sock
Browse files Browse the repository at this point in the history
  • Loading branch information
psihachina committed Nov 4, 2022
1 parent a6eb711 commit 80f4f49
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions internal/commands/tunnel_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,31 +552,42 @@ func writeSSHConfigFromConfig(forwardHost []string, dir string) error {
}

func checkTunnel(dir string) (bool, error) {
c := exec.Command(
"ssh", "-S", "bastion.sock", "-O", "check", "",
)
out := &bytes.Buffer{}
c.Stdout = out
c.Stderr = out
c.Dir = dir

err := c.Run()
if err == nil {
sshConfigPath := fmt.Sprintf("%s/ssh.config", dir)
sshConfig, err := getSSHConfig(sshConfigPath)
if err != nil {
return false, fmt.Errorf("can't check tunnel: %w", err)
}
pathToSocket := filepath.Join(dir, "bastion.sock")
if _, err := os.Stat(pathToSocket); !os.IsNotExist(err) {
pterm.Info.Printfln("A socket file from another tunnel has been detected: %s", pathToSocket)
c := exec.Command(
"ssh", "-S", "bastion.sock", "-O", "check", "",
)
out := &bytes.Buffer{}
c.Stdout = out
c.Stderr = out
c.Dir = dir

err := c.Run()
if err == nil {
sshConfigPath := fmt.Sprintf("%s/ssh.config", dir)
sshConfig, err := getSSHConfig(sshConfigPath)
if err != nil {
return false, fmt.Errorf("can't check tunnel: %w", err)
}

pterm.Success.Println("Tunnel is up. Forwarding config:")
hosts := getHosts(sshConfig)
var forwardConfig string
for _, h := range hosts {
forwardConfig += fmt.Sprintf("%s:%s ➡ localhost:%s\n", h[2], h[3], h[1])
}
pterm.Println(forwardConfig)
pterm.Success.Println("Tunnel is up. Forwarding config:")
hosts := getHosts(sshConfig)
var forwardConfig string
for _, h := range hosts {
forwardConfig += fmt.Sprintf("%s:%s ➡ localhost:%s\n", h[2], h[3], h[1])
}
pterm.Println(forwardConfig)

return true, nil
return true, nil
} else {
pterm.Warning.Println("Tunnel socket file seems to be not useable. We have deleted it")
err := os.Remove(pathToSocket)
if err != nil {
return false, err
}
return false, nil
}
}

return false, nil
Expand Down

0 comments on commit 80f4f49

Please sign in to comment.