Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
308041d440
|
+48
-52
@@ -1,45 +1,44 @@
|
|||||||
|
/*
|
||||||
|
Package giplib implements simple routines for
|
||||||
|
retrieving one's IPv4 and IPv6 addresses from
|
||||||
|
various DNS providers. For now it implements
|
||||||
|
OpenDNS for IPv4 and IPv6, Google for IPv4 and
|
||||||
|
IPv6 and Akamai for IPv4
|
||||||
|
*/
|
||||||
package giplib
|
package giplib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
c *dns.Client
|
||||||
|
m *dns.Msg
|
||||||
|
ipv4, ipv6 string
|
||||||
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
dig {-4,-6} +short myip.opendns.com @resolver1.opendns.com
|
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
|
||||||
|
dig -4 @ns1-1.akamaitech.net whoami.akamai.net +short
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func GetIPv4() string {
|
func init() {
|
||||||
o4 := getO4()
|
c = new(dns.Client)
|
||||||
g4 := getG4()
|
m = &dns.Msg{
|
||||||
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"
|
|
||||||
|
|
||||||
c := new(dns.Client)
|
|
||||||
m := &dns.Msg{
|
|
||||||
MsgHdr: dns.MsgHdr{
|
MsgHdr: dns.MsgHdr{
|
||||||
RecursionDesired: true,
|
RecursionDesired: true,
|
||||||
},
|
},
|
||||||
Question: make([]dns.Question, 1),
|
Question: make([]dns.Question, 1),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//GetOpenDNS4 will retrieve the IPv4 public IP as seen by OpenDNS
|
||||||
|
func GetOpenDNS4() string {
|
||||||
|
nameserver := dns.Fqdn("ns1-1.akamaitech.net") + ":53"
|
||||||
c.Net = "udp4"
|
c.Net = "udp4"
|
||||||
m.SetQuestion("myip.opendns.com.", dns.TypeA)
|
m.SetQuestion("whoami.akamai.net.", dns.TypeA)
|
||||||
r4, _, e4 := c.Exchange(m, nameserver)
|
r4, _, e4 := c.Exchange(m, nameserver)
|
||||||
|
|
||||||
if e4 != nil {
|
if e4 != nil {
|
||||||
@@ -51,17 +50,10 @@ func getO4() string {
|
|||||||
return ipv4
|
return ipv4
|
||||||
}
|
}
|
||||||
|
|
||||||
func getO6() string {
|
//GetOpenDNS6 will retrieve the IPv6 IP as seen by OpenDNS
|
||||||
var ipv6 string
|
func GetOpenDNS6() string {
|
||||||
nameserver := dns.Fqdn("resolver1.opendns.com") + ":53"
|
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)
|
||||||
@@ -75,17 +67,10 @@ func getO6() string {
|
|||||||
return ipv6
|
return ipv6
|
||||||
}
|
}
|
||||||
|
|
||||||
func getG4() string {
|
//GetGoogle4 will retrieve the IPv4 public IP as seen by Google
|
||||||
var ipv4 string
|
func GetGoogle4() string {
|
||||||
nameserver := dns.Fqdn("ns1.google.com") + ":53"
|
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 = "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)
|
||||||
@@ -99,17 +84,10 @@ func getG4() string {
|
|||||||
return ipv4
|
return ipv4
|
||||||
}
|
}
|
||||||
|
|
||||||
func getG6() string {
|
//GetGoogle6 will retrieve the IPv6 IP as seen by Google
|
||||||
var ipv6 string
|
func GetGoogle6() string {
|
||||||
nameserver := dns.Fqdn("ns1.google.com") + ":53"
|
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)
|
||||||
@@ -123,6 +101,24 @@ func getG6() string {
|
|||||||
return ipv6
|
return ipv6
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//GetAkamai4 will retrieve the IPv4 public IP as seen by Akamai
|
||||||
|
func GetAkamai4() string {
|
||||||
|
nameserver := dns.Fqdn("ns1.google.com") + ":53"
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ipv4
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func getIP(ra []dns.RR) string {
|
func getIP(ra []dns.RR) string {
|
||||||
//the answer will always be one IP or none
|
//the answer will always be one IP or none
|
||||||
var ip string
|
var ip string
|
||||||
|
|||||||
@@ -31,15 +31,15 @@ func main() {
|
|||||||
all = true
|
all = true
|
||||||
}
|
}
|
||||||
if all {
|
if all {
|
||||||
fmt.Println(giplib.GetIPv4())
|
fmt.Println(giplib.GetOpenDNS4())
|
||||||
fmt.Println(giplib.GetIPv6())
|
fmt.Println(giplib.GetGoogle6())
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
if *ptr4 {
|
if *ptr4 {
|
||||||
fmt.Println(giplib.GetIPv4())
|
fmt.Println(giplib.GetOpenDNS4())
|
||||||
}
|
}
|
||||||
if *ptr6 {
|
if *ptr6 {
|
||||||
fmt.Println(giplib.GetIPv6())
|
fmt.Println(giplib.GetOpenDNS6())
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user