diff options
author | pg9182 <96569817+pg9182@users.noreply.github.com> | 2022-10-19 03:03:19 -0400 |
---|---|---|
committer | pg9182 <96569817+pg9182@users.noreply.github.com> | 2022-10-19 03:03:19 -0400 |
commit | 36bc4748543380a55b3d1c9de08cfd867f5707df (patch) | |
tree | 073cc3a37efc7d541bed5a0aa4312861a8a8a370 | |
parent | f3317eec17b78421d4bcfbaf2efc6216931a3643 (diff) | |
download | Atlas-36bc4748543380a55b3d1c9de08cfd867f5707df.tar.gz Atlas-36bc4748543380a55b3d1c9de08cfd867f5707df.zip |
pkg/a2s: Explicit timeout error
-rw-r--r-- | pkg/a2s/a2s.go | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/pkg/a2s/a2s.go b/pkg/a2s/a2s.go index 9d1d65d..04648c8 100644 --- a/pkg/a2s/a2s.go +++ b/pkg/a2s/a2s.go @@ -8,14 +8,18 @@ import ( "crypto/cipher" "crypto/rand" "encoding/binary" + "errors" "fmt" "net" "net/netip" + "os" "time" ) const ProbeUID uint64 = 1000000001337 +var ErrTimeout = errors.New("connection timed out") + func Probe(addr netip.AddrPort, timeout time.Duration) error { conn, err := net.DialUDP("udp", nil, net.UDPAddrFromAddrPort(addr)) if err != nil { @@ -32,11 +36,17 @@ func Probe(addr netip.AddrPort, timeout time.Duration) error { return fmt.Errorf("encrypt connection packet: %w", err) } if _, err := conn.Write(pkt); err != nil { + if errors.Is(err, os.ErrDeadlineExceeded) { + err = fmt.Errorf("%w: %v", ErrTimeout, err) + } return fmt.Errorf("send connection packet: %w", err) } resp := make([]byte, 1500) if n, err := conn.Read(resp); err != nil { + if errors.Is(err, os.ErrDeadlineExceeded) { + err = fmt.Errorf("%w: %v", ErrTimeout, err) + } return fmt.Errorf("receive packet: %w", err) } else { resp = resp[:n] |