diff --git a/cli/main.go b/cli/main.go index 7f5f43da..e9d9ce54 100644 --- a/cli/main.go +++ b/cli/main.go @@ -2,13 +2,28 @@ package main import ( "errors" + "fmt" "os" "strings" + "syscall" "github.com/spf13/cobra" "golang.org/x/exp/slog" ) +const ( + // WSAEACCES is the Windows error code for attempting to access a socket that you don't have permission to access. + // + // This commonly occurs if the socket is in use or was not closed correctly, and can be resolved by restarting the hns service. + WSAEACCES = 10013 +) + +// IsWindowsPortAccessError determines if the given error is the error WSAEACCES. +func IsWindowsPortAccessError(err error) bool { + var syscallErr *syscall.Errno + return errors.As(err, &syscallErr) && *syscallErr == WSAEACCES +} + func init() { var opts slog.HandlerOptions if os.Getenv("DEBUG") == "1" { @@ -27,6 +42,12 @@ func main() { rootCmd.SetArgs(args) err := rootCmd.Execute() + if IsWindowsPortAccessError(err) { + fmt.Fprintf(os.Stderr, "Encountered an issue when opening the port for KeyConjurer: %s\n", err) + fmt.Fprintln(os.Stderr, "Consider running `net stop hns` and then `net start hns`") + os.Exit(ExitCodeConnectivityError) + } + var codeErr codeError if errors.As(err, &codeErr) { cobra.CheckErr(codeErr)