-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
iptools.go
80 lines (69 loc) · 1.8 KB
/
iptools.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package utils
import (
"errors"
"fmt"
"net"
"net/url"
"strconv"
"strings"
"syscall"
"time"
)
// URLtoListenIPandPort for a given internal URL,
// find the correct IP/Interface to listen to.
func URLtoListenIPandPort(u string) (string, error) {
parsedURL, err := url.Parse(u)
if err != nil {
return "", fmt.Errorf("URLtoListenIPandPort parse error: %w", err)
}
callURL := parsedURL.Host
if parsedURL.Port() == "" {
switch parsedURL.Scheme {
case "http":
callURL = callURL + ":80"
case "https":
callURL = callURL + ":443"
}
}
conn, err := net.Dial("udp", callURL)
if err != nil {
return "", fmt.Errorf("URLtoListenIPandPort UDP call error: %w", err)
}
ipToListen := strings.Split(conn.LocalAddr().String(), ":")[0]
portToListen, err := checkAndPickPort(ipToListen, 3500)
if err != nil {
return "", fmt.Errorf("URLtoListenIPandPort port error: %w", err)
}
res := net.JoinHostPort(ipToListen, portToListen)
return res, nil
}
func checkAndPickPort(ip string, port int) (string, error) {
const maxAttempts = 1000
for attempt := 1; attempt <= maxAttempts; attempt++ {
conn, err := net.Listen("tcp", net.JoinHostPort(ip, strconv.Itoa(port)))
if err != nil {
if errors.Is(err, syscall.EADDRINUSE) {
if attempt == maxAttempts {
break
}
port++
continue
}
return "", fmt.Errorf("port pick error: %w", err)
}
conn.Close()
return strconv.Itoa(port), nil
}
return "", fmt.Errorf("port pick error. Exceeded maximum attempts")
}
// HostPortIsAlive - We use this function to periodically
// health check the selected device and decide if we want
// to keep the entry in the list or to remove it.
func HostPortIsAlive(h string) bool {
conn, err := net.DialTimeout("tcp", h, time.Duration(2*time.Second))
if err != nil {
return false
}
conn.Close()
return true
}