mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Add ipv6 support to ipinfo (#885)
This commit is contained in:
parent
ad650d3822
commit
e035f3a15a
@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"text/template"
|
||||
|
||||
@ -50,7 +51,15 @@ func (widget *Widget) Refresh() {
|
||||
//this method reads the config and calls ipinfo for ip information
|
||||
func (widget *Widget) ipinfo() {
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", "https://ipinfo.io/", nil)
|
||||
var url string
|
||||
ip, ipv6 := getMyIP()
|
||||
if ipv6 {
|
||||
url = fmt.Sprintf("https://ipinfo.io/%s", ip.String())
|
||||
} else {
|
||||
url = "https://ipinfo.io/"
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
widget.result = err.Error()
|
||||
return
|
||||
@ -109,3 +118,21 @@ func (widget *Widget) setResult(info *ipinfo) {
|
||||
func formatableText(key, value string) string {
|
||||
return fmt.Sprintf(" [{{.subheadingColor}}]%8s[-:-:-] [{{.valueColor}}]{{.%s}}\n", key, value)
|
||||
}
|
||||
|
||||
// getMyIP provides this system's default IPv4 or IPv6 IP address for routing WAN requests.
|
||||
// It does so by dialing out to a site known to have both an A and AAAA DNS records (IPv6)
|
||||
// The 'net' package is allowed to decide how to connect, connecting to both IPv4 or IPv6 address
|
||||
// depending on the availbility of IP protocols.
|
||||
func getMyIP() (ip net.IP, v6 bool) {
|
||||
conn, err := net.Dial("tcp", "fast.com:80")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
addr := conn.LocalAddr().(*net.TCPAddr)
|
||||
ip = addr.IP
|
||||
v6 = ip.To4() == nil
|
||||
|
||||
return
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user