Compare commits
3 Commits
d8c63763fd
...
v0.8
| Author | SHA1 | Date | |
|---|---|---|---|
|
af71b64160
|
|||
|
8471fcd3de
|
|||
|
cf79e2e8f0
|
@@ -1,3 +1,8 @@
|
|||||||
# gip
|
# gip
|
||||||
|
|
||||||
Find your public IP
|
Find your public IP
|
||||||
|
gip will print your public IPv4 followed by a tab and your public IPv6.
|
||||||
|
If either is missing they will be replaced by "". Please note that due
|
||||||
|
to their way usage the IPv4 will most probably be the public IP of your
|
||||||
|
gateway while the IPv6 will be the public IP of the computer gip is run
|
||||||
|
on.
|
||||||
|
|||||||
@@ -1,68 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/miekg/dns"
|
|
||||||
)
|
|
||||||
|
|
||||||
/*
|
|
||||||
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) {
|
|
||||||
c.Net = "udp4"
|
|
||||||
m.SetQuestion("myip.opendns.com.", dns.TypeA)
|
|
||||||
|
|
||||||
r, _, err := c.Exchange(m, nameserver)
|
|
||||||
return r, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func get6(c *dns.Client, m *dns.Msg, nameserver string) (*dns.Msg, error) {
|
|
||||||
c.Net = "udp6"
|
|
||||||
m.SetQuestion("myip.opendns.com.", dns.TypeAAAA)
|
|
||||||
|
|
||||||
r, _, err := c.Exchange(m, nameserver)
|
|
||||||
return r, err
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func printIP(ra []dns.RR) []string {
|
|
||||||
var ips []string
|
|
||||||
for _, ansa := range ra {
|
|
||||||
switch ansb := ansa.(type) {
|
|
||||||
case *dns.A:
|
|
||||||
ips = append(ips, ansb.A.String())
|
|
||||||
case *dns.AAAA:
|
|
||||||
ips = append(ips, ansb.AAAA.String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ips
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
} else {
|
|
||||||
fmt.Println(printIP(r4.Answer))
|
|
||||||
}
|
|
||||||
r6, e6 := get6(c, m, nameserver)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(e6)
|
|
||||||
} else {
|
|
||||||
fmt.Println(printIP(r6.Answer))
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
package giplib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/miekg/dns"
|
||||||
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
|
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 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"
|
||||||
|
|
||||||
|
c := new(dns.Client)
|
||||||
|
m := &dns.Msg{
|
||||||
|
MsgHdr: dns.MsgHdr{
|
||||||
|
RecursionDesired: true,
|
||||||
|
},
|
||||||
|
Question: make([]dns.Question, 1),
|
||||||
|
}
|
||||||
|
c.Net = "udp4"
|
||||||
|
m.SetQuestion("myip.opendns.com.", dns.TypeA)
|
||||||
|
r4, _, e4 := c.Exchange(m, nameserver)
|
||||||
|
|
||||||
|
if e4 != nil {
|
||||||
|
ipv4 = ""
|
||||||
|
} else {
|
||||||
|
ipv4 = getIP(r4.Answer)
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
||||||
|
m.SetQuestion("myip.opendns.com.", dns.TypeAAAA)
|
||||||
|
r6, _, e6 := c.Exchange(m, nameserver)
|
||||||
|
|
||||||
|
if e6 != nil {
|
||||||
|
ipv6 = ""
|
||||||
|
} else {
|
||||||
|
ipv6 = getIP(r6.Answer)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ipv6
|
||||||
|
}
|
||||||
|
|
||||||
|
func getG4() string {
|
||||||
|
var ipv4 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 = "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 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"
|
||||||
|
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 ipv6
|
||||||
|
}
|
||||||
|
|
||||||
|
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:
|
||||||
|
ip = ansb.A.String()
|
||||||
|
case *dns.AAAA:
|
||||||
|
ip = ansb.AAAA.String()
|
||||||
|
case *dns.TXT:
|
||||||
|
// we always expect only one IP in the field
|
||||||
|
ip = ansb.Txt[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ip
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"git.jpi.io/karasz/gip/giplib"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
//Version contains the git hashtag injected by make
|
||||||
|
Version = "N/A"
|
||||||
|
//BuildTime contains the build timestamp injected by make
|
||||||
|
BuildTime = "N/A"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var all bool
|
||||||
|
ptr4 := flag.Bool("4", false, "return only IPv4")
|
||||||
|
ptr6 := flag.Bool("6", false, "return only IPv6")
|
||||||
|
version := flag.Bool("v", false, "return version")
|
||||||
|
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if *version {
|
||||||
|
fmt.Println(Version, " built on ", BuildTime)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
if *ptr4 == *ptr6 {
|
||||||
|
all = true
|
||||||
|
}
|
||||||
|
if all {
|
||||||
|
fmt.Println(giplib.GetIPv4())
|
||||||
|
fmt.Println(giplib.GetIPv6())
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
if *ptr4 {
|
||||||
|
fmt.Println(giplib.GetIPv4())
|
||||||
|
}
|
||||||
|
if *ptr6 {
|
||||||
|
fmt.Println(giplib.GetIPv6())
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user