mirror of
https://github.com/yusing/godoxy.git
synced 2025-07-01 13:04:25 +02:00
refactor: ping if-flow and remove timeout
This commit is contained in:
parent
cfd1d8fff0
commit
c7e0dcbfd8
1 changed files with 6 additions and 10 deletions
|
@ -94,24 +94,20 @@ func Ping(ctx context.Context, ip net.IP) (bool, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var pingDialer = &net.Dialer{
|
|
||||||
Timeout: 2 * time.Second,
|
|
||||||
}
|
|
||||||
|
|
||||||
// PingWithTCPFallback pings the IP address using ICMP and TCP fallback.
|
// PingWithTCPFallback pings the IP address using ICMP and TCP fallback.
|
||||||
//
|
//
|
||||||
// If the ICMP ping fails due to permission error, it will try to connect to the specified port.
|
// If the ICMP ping fails due to permission error, it will try to connect to the specified port.
|
||||||
func PingWithTCPFallback(ctx context.Context, ip net.IP, port int) (bool, error) {
|
func PingWithTCPFallback(ctx context.Context, ip net.IP, port int) (bool, error) {
|
||||||
ok, err := Ping(ctx, ip)
|
ok, err := Ping(ctx, ip)
|
||||||
if err != nil {
|
if err == nil {
|
||||||
|
return ok, nil
|
||||||
|
}
|
||||||
if !errors.Is(err, os.ErrPermission) {
|
if !errors.Is(err, os.ErrPermission) {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return ok, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
conn, err := pingDialer.DialContext(ctx, "tcp", fmt.Sprintf("%s:%d", ip, port))
|
var dialer net.Dialer
|
||||||
|
conn, err := dialer.DialContext(ctx, "tcp", fmt.Sprintf("%s:%d", ip, port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue