From 80f4f49c1805c1e41a2609df34a52fd744a419f2 Mon Sep 17 00:00:00 2001 From: Nikita Podshivalov Date: Thu, 3 Nov 2022 15:23:06 +0300 Subject: [PATCH] added check of a pre-existing bastion.sock --- internal/commands/tunnel_up.go | 57 ++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/internal/commands/tunnel_up.go b/internal/commands/tunnel_up.go index ace3e87c..e8597e8b 100644 --- a/internal/commands/tunnel_up.go +++ b/internal/commands/tunnel_up.go @@ -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