-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet.go
62 lines (49 loc) · 1.3 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"net/http"
"time"
cf "github.com/cloudflare/cloudflare-go"
)
func createAPIClient(apiKey string) (*cf.API, error) {
api, err := cf.NewWithAPIToken(apiKey)
if err != nil {
return api, fmt.Errorf("Error creating Cloudflare client: %v", err)
}
return api, nil
}
func createHTTPClient() *http.Client {
return &http.Client{
Timeout: 5 * time.Second,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse // Do not follow redirects
},
}
}
func isHTTPResponsive(client *http.Client, ip string) (bool, error) {
resp, err := client.Get("http://" + ip)
if err != nil {
return false, err
}
defer resp.Body.Close()
return true, nil
}
func getResponsiveIP(httpClient *http.Client, r Record, logCh chan<- logEntry) (string, error) {
var responsiveIP string
for _, ip := range r.IPs {
ipResponsive, err := isHTTPResponsive(httpClient, ip)
if err != nil {
sendLogEntry(logCh, r.Domain, fmt.Sprintf("%s: Error checking response for IP %s: %v", r.Domain, ip, err))
continue
}
if !ipResponsive {
continue
}
responsiveIP = ip
break // Exit the loop since we found a responsive IP
}
if responsiveIP == "" {
return responsiveIP, fmt.Errorf("%s: No responsive IPs found", r.Domain)
}
return responsiveIP, nil
}