-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
net.go
47 lines (38 loc) · 913 Bytes
/
net.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package utils
import (
"net"
"strings"
"time"
"github.com/Laisky/errors/v2"
)
// IsRemoteUDPPortOpen check is remote udp port open.
// return err if remote port is not open.
//
// Args:
//
// addr: "scanme.nmap.org:53"
func IsRemoteUDPPortOpen(addr string) error {
udpAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return errors.WithStack(err)
}
conn, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
return errors.WithStack(err)
}
defer func() { _ = conn.Close() }()
if err = conn.SetDeadline(Clock.GetUTCNow().Add(3 * time.Second)); err != nil {
return errors.WithStack(err)
}
if _, err = conn.Write([]byte("0")); err != nil {
return errors.Wrap(err, "write")
}
data := make([]byte, 1)
if _, _, err = conn.ReadFromUDP(data); err != nil {
if strings.Contains(err.Error(), "i/o timeout") {
return nil
}
return errors.WithStack(err)
}
return nil
}