diff --git a/internal/netproxy/refused_test.go b/internal/netproxy/refused_test.go new file mode 100644 index 000000000..26044f4cb --- /dev/null +++ b/internal/netproxy/refused_test.go @@ -0,0 +1,21 @@ +package netproxy + +import ( + "strings" + "testing" +) + +// Windows says a refused connection in words of its own, and a proxy or +// SSH server with nothing listening was explained on macOS and Linux +// only: TestSSHProxySaysWhyItFailed was red on Windows. +func TestRefusedIsExplainedOnEverySystem(t *testing.T) { + for _, msg := range []string{ + "proxyconnect tcp: dial tcp 127.0.0.1:7890: connect: connection refused", + "proxyconnect tcp: dial tcp 127.0.0.1:7890: connectex: No connection could be made because the target machine actively refused it.", + } { + w := Explain("http://127.0.0.1:7890", msg) + if w == nil || !strings.HasPrefix(w.Why, "nothing answers at {addr}") { + t.Errorf("%q: %+v, want nothing answers", msg, w) + } + } +} diff --git a/internal/netproxy/ssh.go b/internal/netproxy/ssh.go index 715b85b02..a05e1c1d8 100644 --- a/internal/netproxy/ssh.go +++ b/internal/netproxy/ssh.go @@ -383,7 +383,7 @@ func sshWhy(addr string, err error) error { // keyboard-interactive at once, where the library waits for its // questions (60), after the password was turned away w.Why = "the SSH server at {addr} turned the login away: check the user and the password or key" - case strings.Contains(msg, "connection refused"): + case refused(msg): w.Why = "nothing answers SSH at {addr}: check the address and port" case strings.Contains(msg, "no such host"): w.Why = "the SSH server's name doesn't resolve" diff --git a/internal/netproxy/why.go b/internal/netproxy/why.go index 4bf95e5e1..a1e2ecdfb 100644 --- a/internal/netproxy/why.go +++ b/internal/netproxy/why.go @@ -63,7 +63,7 @@ func Explain(address, msg string) *Why { w.Why = "{addr} turned the username and password away" case strings.Contains(msg, "no acceptable authentication methods"): w.Why = "{addr} wants a username and password" - case strings.Contains(msg, "connection refused"): + case refused(msg): w.Why = "nothing answers at {addr}: check the address and port, and that the proxy is running" case strings.Contains(msg, "no such host"): w.Why = "the proxy's host name doesn't resolve" @@ -77,6 +77,14 @@ func Explain(address, msg string) *Why { return w } +// refused is whether msg is a connection refused: "connection refused" on +// macOS and Linux, and Windows' "No connection could be made because the +// target machine actively refused it" (WSAECONNREFUSED), which magpie on +// Windows read as nothing it knew. +func refused(msg string) bool { + return strings.Contains(msg, "connection refused") || strings.Contains(msg, "actively refused") +} + // ExplainServer is Explain, and when the error text can't tell what // answered (Go reads a real OpenSSH banner, "SSH-2.0-OpenSSH_9.6p1 // Ubuntu-3ubuntu13.20", as an HTTP status line with a malformed code),