GoDoxy/internal/net/ping.go
Yuzerion 57292f0fe8
feat: proxmox idlewatcher (#88)
* feat: idle sleep for proxmox LXCs

* refactor: replace deprecated docker api types

* chore(api): remove debug task list endpoint

* refactor: move servemux to gphttp/servemux; favicon.go to v1/favicon

* refactor: introduce Pool interface, move agent_pool to agent module

* refactor: simplify api code

* feat: introduce debug api

* refactor: remove net.URL and net.CIDR types, improved unmarshal handling

* chore: update Makefile for debug build tag, update README

* chore: add gperr.Unwrap method

* feat: relative time and duration formatting

* chore: add ROOT_DIR environment variable, refactor

* migration: move homepage override and icon cache to $BASE_DIR/data, add migration code

* fix: nil dereference on marshalling service health

* fix: wait for route deletion

* chore: enhance tasks debuggability

* feat: stdout access logger and MultiWriter

* fix(agent): remove agent properly on verify error

* fix(metrics): disk exclusion logic and added corresponding tests

* chore: update schema and prettify, fix package.json and Makefile

* fix: I/O buffer not being shrunk before putting back to pool

* feat: enhanced error handling module

* chore: deps upgrade

* feat: better value formatting and handling

---------

Co-authored-by: yusing <yusing@6uo.me>
2025-04-16 14:52:33 +08:00

120 lines
2.2 KiB
Go

package netutils
import (
"context"
"errors"
"fmt"
"net"
"os"
"time"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)
var (
ipv4EchoBytes []byte
ipv6EchoBytes []byte
)
func init() {
echoBody := &icmp.Echo{
ID: os.Getpid() & 0xffff,
Seq: 1,
Data: []byte("Hello"),
}
ipv4Echo := &icmp.Message{
Type: ipv4.ICMPTypeEcho,
Body: echoBody,
}
ipv6Echo := &icmp.Message{
Type: ipv6.ICMPTypeEchoRequest,
Body: echoBody,
}
var err error
ipv4EchoBytes, err = ipv4Echo.Marshal(nil)
if err != nil {
panic(err)
}
ipv6EchoBytes, err = ipv6Echo.Marshal(nil)
if err != nil {
panic(err)
}
}
// Ping pings the IP address using ICMP.
func Ping(ctx context.Context, ip net.IP) (bool, error) {
var msgBytes []byte
if ip.To4() != nil {
msgBytes = ipv4EchoBytes
} else {
msgBytes = ipv6EchoBytes
}
conn, err := icmp.ListenPacket("ip:icmp", ip.String())
if err != nil {
return false, err
}
defer conn.Close()
err = conn.SetWriteDeadline(time.Now().Add(5 * time.Second))
if err != nil {
return false, err
}
_, err = conn.WriteTo(msgBytes, &net.IPAddr{IP: ip})
if err != nil {
return false, err
}
err = conn.SetReadDeadline(time.Now().Add(5 * time.Second))
if err != nil {
return false, err
}
buf := make([]byte, 1500)
for {
select {
case <-ctx.Done():
return false, ctx.Err()
default:
}
n, _, err := conn.ReadFrom(buf)
if err != nil {
return false, err
}
m, err := icmp.ParseMessage(ipv4.ICMPTypeEchoReply.Protocol(), buf[:n])
if err != nil {
continue
}
if m.Type == ipv4.ICMPTypeEchoReply {
return true, nil
}
}
}
var pingDialer = &net.Dialer{
Timeout: 2 * time.Second,
}
// 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.
func PingWithTCPFallback(ctx context.Context, ip net.IP, port int) (bool, error) {
ok, err := Ping(ctx, ip)
if err != nil {
if !errors.Is(err, os.ErrPermission) {
return false, err
}
} else {
return ok, nil
}
conn, err := pingDialer.DialContext(ctx, "tcp", fmt.Sprintf("%s:%d", ip, port))
if err != nil {
return false, err
}
defer conn.Close()
return true, nil
}