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 3, 2022
1 parent 6726492 commit c43613f
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 @@ -522,31 +522,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 c43613f

Please sign in to comment.