-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip_functions.go
62 lines (49 loc) · 1.29 KB
/
ip_functions.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 (
"io/ioutil"
"log"
"net"
"net/http"
"strings"
)
// getPublicIP returns public IPv4 and IPv6 IPs of host.
func getPublicIP(ipApiUrl string) string {
// Get public IP address of host
reqAddr, err := http.Get(ipApiUrl)
if err != nil {
log.Fatalf("Could not fetch public IP address: %v\n", err)
}
publicIPAddress, err := ioutil.ReadAll(reqAddr.Body)
if err != nil {
log.Fatalf("Could not read public IP response: %v\n", err)
}
return string(publicIPAddress)
}
// getDomainIP returns the A or AAAA records for a provided domain.
// recordType can be 'A' for IPv4 or 'AAAA' for IPv6
func getDomainIP(domain, recordType string) string {
var ipV4, ipV6, dnsRecord string
ips, err := net.LookupIP(domain)
if err != nil {
log.Fatalf("Could not resolve IPs: %v\n", err)
}
for _, ip := range ips {
ipString := ip.String()
if strings.Count(ipString, ":") < 2 {
ipV4 = ipString
} else if strings.Count(ipString, ":") >= 2 {
ipV6 = ipString
}
}
if recordType == "A" {
dnsRecord = ipV4
} else if recordType == "AAAA" {
dnsRecord = ipV6
}
return dnsRecord
}
// checkIPsMatch returns if two IP addresses match.
func checkIPsMatch(publicIPAddress, domainIPAddress string) bool {
ipAddressesMatch := publicIPAddress == domainIPAddress
return ipAddressesMatch
}