|
|
@ -1,9 +1,7 @@ |
|
|
|
package main |
|
|
|
package giplib |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"github.com/miekg/dns" |
|
|
|
"github.com/miekg/dns" |
|
|
|
"os" |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
/* |
|
|
@ -11,7 +9,26 @@ dig {-4,-6} +short myip.opendns.com @resolver1.opendns.com |
|
|
|
dig {-4,-6} TXT +short o-o.myaddr.l.google.com @ns1.google.com |
|
|
|
dig {-4,-6} TXT +short o-o.myaddr.l.google.com @ns1.google.com |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
func getO() (ipv4 string, ipv6 string) { |
|
|
|
func GetIPv4() string { |
|
|
|
|
|
|
|
o4 := getO4() |
|
|
|
|
|
|
|
g4 := getG4() |
|
|
|
|
|
|
|
if o4 == g4 { |
|
|
|
|
|
|
|
return o4 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return "" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func GetIPv6() string { |
|
|
|
|
|
|
|
o6 := getO6() |
|
|
|
|
|
|
|
g6 := getG6() |
|
|
|
|
|
|
|
if o6 == g6 { |
|
|
|
|
|
|
|
return o6 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return "" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func getO4() string { |
|
|
|
|
|
|
|
var ipv4 string |
|
|
|
nameserver := dns.Fqdn("resolver1.opendns.com") + ":53" |
|
|
|
nameserver := dns.Fqdn("resolver1.opendns.com") + ":53" |
|
|
|
|
|
|
|
|
|
|
|
c := new(dns.Client) |
|
|
|
c := new(dns.Client) |
|
|
@ -21,28 +38,45 @@ func getO() (ipv4 string, ipv6 string) { |
|
|
|
}, |
|
|
|
}, |
|
|
|
Question: make([]dns.Question, 1), |
|
|
|
Question: make([]dns.Question, 1), |
|
|
|
} |
|
|
|
} |
|
|
|
// get IPv4 first
|
|
|
|
|
|
|
|
c.Net = "udp4" |
|
|
|
c.Net = "udp4" |
|
|
|
m.SetQuestion("myip.opendns.com.", dns.TypeA) |
|
|
|
m.SetQuestion("myip.opendns.com.", dns.TypeA) |
|
|
|
r4, _, e4 := c.Exchange(m, nameserver) |
|
|
|
r4, _, e4 := c.Exchange(m, nameserver) |
|
|
|
|
|
|
|
|
|
|
|
if e4 != nil { |
|
|
|
if e4 != nil { |
|
|
|
ipv4 = "" |
|
|
|
ipv4 = "" |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
ipv4 = getIP(r4.Answer) |
|
|
|
ipv4 = getIP(r4.Answer) |
|
|
|
} |
|
|
|
} |
|
|
|
// now IPv6
|
|
|
|
|
|
|
|
|
|
|
|
return ipv4 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func getO6() string { |
|
|
|
|
|
|
|
var 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), |
|
|
|
|
|
|
|
} |
|
|
|
c.Net = "udp6" |
|
|
|
c.Net = "udp6" |
|
|
|
m.SetQuestion("myip.opendns.com.", dns.TypeAAAA) |
|
|
|
m.SetQuestion("myip.opendns.com.", dns.TypeAAAA) |
|
|
|
r6, _, e6 := c.Exchange(m, nameserver) |
|
|
|
r6, _, e6 := c.Exchange(m, nameserver) |
|
|
|
|
|
|
|
|
|
|
|
if e6 != nil { |
|
|
|
if e6 != nil { |
|
|
|
ipv6 = "" |
|
|
|
ipv6 = "" |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
ipv6 = getIP(r6.Answer) |
|
|
|
ipv6 = getIP(r6.Answer) |
|
|
|
} |
|
|
|
} |
|
|
|
return ipv4, ipv6 |
|
|
|
|
|
|
|
|
|
|
|
return ipv6 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func getG() (ipv4 string, ipv6 string) { |
|
|
|
func getG4() string { |
|
|
|
|
|
|
|
var ipv4 string |
|
|
|
nameserver := dns.Fqdn("ns1.google.com") + ":53" |
|
|
|
nameserver := dns.Fqdn("ns1.google.com") + ":53" |
|
|
|
|
|
|
|
|
|
|
|
c := new(dns.Client) |
|
|
|
c := new(dns.Client) |
|
|
@ -52,25 +86,41 @@ func getG() (ipv4 string, ipv6 string) { |
|
|
|
}, |
|
|
|
}, |
|
|
|
Question: make([]dns.Question, 1), |
|
|
|
Question: make([]dns.Question, 1), |
|
|
|
} |
|
|
|
} |
|
|
|
// get IPv4 first
|
|
|
|
|
|
|
|
c.Net = "udp4" |
|
|
|
c.Net = "udp4" |
|
|
|
m.SetQuestion("o-o.myaddr.l.google.com.", dns.TypeTXT) |
|
|
|
m.SetQuestion("o-o.myaddr.l.google.com.", dns.TypeTXT) |
|
|
|
r4, _, e4 := c.Exchange(m, nameserver) |
|
|
|
r4, _, e4 := c.Exchange(m, nameserver) |
|
|
|
|
|
|
|
|
|
|
|
if e4 != nil { |
|
|
|
if e4 != nil { |
|
|
|
ipv4 = "" |
|
|
|
ipv4 = "" |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
ipv4 = getIP(r4.Answer) |
|
|
|
ipv4 = getIP(r4.Answer) |
|
|
|
} |
|
|
|
} |
|
|
|
// now IPv6
|
|
|
|
|
|
|
|
|
|
|
|
return ipv4 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func getG6() string { |
|
|
|
|
|
|
|
var 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), |
|
|
|
|
|
|
|
} |
|
|
|
c.Net = "udp6" |
|
|
|
c.Net = "udp6" |
|
|
|
m.SetQuestion("o-o.myaddr.l.google.com.", dns.TypeTXT) |
|
|
|
m.SetQuestion("o-o.myaddr.l.google.com.", dns.TypeTXT) |
|
|
|
r6, _, e6 := c.Exchange(m, nameserver) |
|
|
|
r6, _, e6 := c.Exchange(m, nameserver) |
|
|
|
|
|
|
|
|
|
|
|
if e6 != nil { |
|
|
|
if e6 != nil { |
|
|
|
ipv6 = "" |
|
|
|
ipv6 = "" |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
ipv6 = getIP(r6.Answer) |
|
|
|
ipv6 = getIP(r6.Answer) |
|
|
|
} |
|
|
|
} |
|
|
|
return ipv4, ipv6 |
|
|
|
|
|
|
|
|
|
|
|
return ipv6 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func getIP(ra []dns.RR) string { |
|
|
|
func getIP(ra []dns.RR) string { |
|
|
@ -87,26 +137,6 @@ func getIP(ra []dns.RR) string { |
|
|
|
ip = ansb.Txt[0] |
|
|
|
ip = ansb.Txt[0] |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return ip |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
return ip |
|
|
|
var ipv4, ipv6 string |
|
|
|
|
|
|
|
exit := 0 |
|
|
|
|
|
|
|
o4, o6 := getO() |
|
|
|
|
|
|
|
g4, g6 := getG() |
|
|
|
|
|
|
|
if o4 == g4 { |
|
|
|
|
|
|
|
ipv4 = o4 |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
ipv4 = "" |
|
|
|
|
|
|
|
exit = 111 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if o6 == g6 { |
|
|
|
|
|
|
|
ipv6 = o6 |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
ipv6 = "" |
|
|
|
|
|
|
|
exit = 111 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
fmt.Printf("%s\t%s\n", ipv4, ipv6) |
|
|
|
|
|
|
|
os.Exit(exit) |
|
|
|
|
|
|
|
} |
|
|
|
} |