Skip to content

Commit f780134

Browse files
committed
Improve proxy retry
1 parent 99ad043 commit f780134

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

tunnel/tunnel.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"fmt"
55
"net"
66
"net/url"
7+
"os"
78
"strings"
9+
"syscall"
810
"time"
911

1012
"github.com/balibuild/tunnelssh/cli"
@@ -63,6 +65,34 @@ func (bm *BoringMachine) DialTunnel(network string, address string, timeout time
6365
return nil, cli.ErrorCat("not support current scheme", u.Scheme)
6466
}
6567

68+
func isProxyOffline(err error) bool {
69+
netErr, ok := err.(net.Error)
70+
if !ok {
71+
return false
72+
}
73+
if netErr.Timeout() {
74+
return true
75+
}
76+
opErr, ok := netErr.(*net.OpError)
77+
if !ok {
78+
return false
79+
}
80+
switch t := opErr.Err.(type) {
81+
case *net.DNSError:
82+
return false
83+
case *os.SyscallError:
84+
if errno, ok := t.Err.(syscall.Errno); ok {
85+
switch errno {
86+
case syscall.ECONNREFUSED:
87+
return true
88+
case syscall.ETIMEDOUT:
89+
return true
90+
}
91+
}
92+
}
93+
return false
94+
}
95+
6696
// DialDirect todo
6797
func (bm *BoringMachine) DialDirect(network string, address string, timeout time.Duration) (net.Conn, error) {
6898
conn, err := net.DialTimeout(network, address, timeout)
@@ -79,10 +109,10 @@ func (bm *BoringMachine) DialTimeout(network string, address string, timeout tim
79109
return bm.DialDirect(network, address, timeout)
80110
}
81111
conn, err := bm.DialTunnel(network, address, timeout)
82-
if err == nil {
112+
if err == nil && !isProxyOffline(err) {
83113
return conn, err
84114
}
85-
bm.DebugPrint("Tunnel cannot establish,try connect direct %s", address)
115+
bm.DebugPrint("Tunnel cannot establish, try connect direct %s", address)
86116
return bm.DialDirect(network, address, timeout)
87117
}
88118

0 commit comments

Comments
 (0)