|
|
|
@ -2,8 +2,8 @@ package main
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"fmt" |
|
|
|
|
|
|
|
|
|
"github.com/miekg/dns" |
|
|
|
|
"os" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
@ -11,58 +11,102 @@ dig {-4,-6} +short myip.opendns.com @resolver1.opendns.com
|
|
|
|
|
dig {-4,-6} TXT +short o-o.myaddr.l.google.com @ns1.google.com |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
func get4(c *dns.Client, m *dns.Msg, nameserver string) (*dns.Msg, error) { |
|
|
|
|
func getO() (ipv4 string, ipv6 string) { |
|
|
|
|
nameserver := dns.Fqdn("resolver1.opendns.com") + ":53" |
|
|
|
|
|
|
|
|
|
c := new(dns.Client) |
|
|
|
|
m := &dns.Msg{ |
|
|
|
|
MsgHdr: dns.MsgHdr{ |
|
|
|
|
RecursionDesired: true, |
|
|
|
|
}, |
|
|
|
|
Question: make([]dns.Question, 1), |
|
|
|
|
} |
|
|
|
|
// get IPv4 first
|
|
|
|
|
c.Net = "udp4" |
|
|
|
|
m.SetQuestion("myip.opendns.com.", dns.TypeA) |
|
|
|
|
|
|
|
|
|
r, _, err := c.Exchange(m, nameserver) |
|
|
|
|
return r, err |
|
|
|
|
r4, _, e4 := c.Exchange(m, nameserver) |
|
|
|
|
if e4 != nil { |
|
|
|
|
ipv4 = "" |
|
|
|
|
} else { |
|
|
|
|
ipv4 = getIP(r4.Answer) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func get6(c *dns.Client, m *dns.Msg, nameserver string) (*dns.Msg, error) { |
|
|
|
|
// now IPv6
|
|
|
|
|
c.Net = "udp6" |
|
|
|
|
m.SetQuestion("myip.opendns.com.", dns.TypeAAAA) |
|
|
|
|
r6, _, e6 := c.Exchange(m, nameserver) |
|
|
|
|
if e6 != nil { |
|
|
|
|
ipv6 = "" |
|
|
|
|
} else { |
|
|
|
|
ipv6 = getIP(r6.Answer) |
|
|
|
|
} |
|
|
|
|
return ipv4, ipv6 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
r, _, err := c.Exchange(m, nameserver) |
|
|
|
|
return r, err |
|
|
|
|
func getG() (ipv4 string, ipv6 string) { |
|
|
|
|
nameserver := dns.Fqdn("ns1.google.com") + ":53" |
|
|
|
|
|
|
|
|
|
c := new(dns.Client) |
|
|
|
|
m := &dns.Msg{ |
|
|
|
|
MsgHdr: dns.MsgHdr{ |
|
|
|
|
RecursionDesired: true, |
|
|
|
|
}, |
|
|
|
|
Question: make([]dns.Question, 1), |
|
|
|
|
} |
|
|
|
|
// get IPv4 first
|
|
|
|
|
c.Net = "udp4" |
|
|
|
|
m.SetQuestion("o-o.myaddr.l.google.com.", dns.TypeTXT) |
|
|
|
|
r4, _, e4 := c.Exchange(m, nameserver) |
|
|
|
|
if e4 != nil { |
|
|
|
|
ipv4 = "" |
|
|
|
|
} else { |
|
|
|
|
ipv4 = getIP(r4.Answer) |
|
|
|
|
} |
|
|
|
|
// now IPv6
|
|
|
|
|
c.Net = "udp6" |
|
|
|
|
m.SetQuestion("o-o.myaddr.l.google.com.", dns.TypeTXT) |
|
|
|
|
r6, _, e6 := c.Exchange(m, nameserver) |
|
|
|
|
if e6 != nil { |
|
|
|
|
ipv6 = "" |
|
|
|
|
} else { |
|
|
|
|
ipv6 = getIP(r6.Answer) |
|
|
|
|
} |
|
|
|
|
return ipv4, ipv6 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func printIP(ra []dns.RR) []string { |
|
|
|
|
var ips []string |
|
|
|
|
func getIP(ra []dns.RR) string { |
|
|
|
|
//the answer will always be one IP or none
|
|
|
|
|
var ip string |
|
|
|
|
for _, ansa := range ra { |
|
|
|
|
switch ansb := ansa.(type) { |
|
|
|
|
case *dns.A: |
|
|
|
|
ips = append(ips, ansb.A.String()) |
|
|
|
|
ip = ansb.A.String() |
|
|
|
|
case *dns.AAAA: |
|
|
|
|
ips = append(ips, ansb.AAAA.String()) |
|
|
|
|
ip = ansb.AAAA.String() |
|
|
|
|
case *dns.TXT: |
|
|
|
|
// we always expect only one IP in the field
|
|
|
|
|
ip = ansb.Txt[0] |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return ips |
|
|
|
|
return ip |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
|
nameserver := dns.Fqdn("resolver1.opendns.com") + ":53" |
|
|
|
|
|
|
|
|
|
c := new(dns.Client) |
|
|
|
|
c.Net = "udp4" |
|
|
|
|
m := &dns.Msg{ |
|
|
|
|
MsgHdr: dns.MsgHdr{ |
|
|
|
|
RecursionDesired: true, |
|
|
|
|
}, |
|
|
|
|
Question: make([]dns.Question, 1), |
|
|
|
|
} |
|
|
|
|
r4, err := get4(c, m, nameserver) |
|
|
|
|
if err != nil { |
|
|
|
|
fmt.Println(err) |
|
|
|
|
var ipv4, ipv6 string |
|
|
|
|
exit := 0 |
|
|
|
|
o4, o6 := getO() |
|
|
|
|
g4, g6 := getG() |
|
|
|
|
if o4 == g4 { |
|
|
|
|
ipv4 = o4 |
|
|
|
|
} else { |
|
|
|
|
fmt.Println(printIP(r4.Answer)) |
|
|
|
|
ipv4 = "" |
|
|
|
|
exit = 111 |
|
|
|
|
} |
|
|
|
|
r6, e6 := get6(c, m, nameserver) |
|
|
|
|
if err != nil { |
|
|
|
|
fmt.Println(e6) |
|
|
|
|
if o6 == g6 { |
|
|
|
|
ipv6 = o6 |
|
|
|
|
} else { |
|
|
|
|
fmt.Println(printIP(r6.Answer)) |
|
|
|
|
ipv6 = "" |
|
|
|
|
exit = 111 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fmt.Printf("%s\t%s\n", ipv4, ipv6) |
|
|
|
|
os.Exit(exit) |
|
|
|
|
} |
|
|
|
|