diff --git a/go.mod b/go.mod index c1614234..0e88c194 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.20 require ( github.com/fatih/color v1.13.0 github.com/hashicorp/go-multierror v1.1.1 - github.com/ipinfo/go/v2 v2.9.4 + github.com/ipinfo/go/v2 v2.10.0 github.com/ipinfo/mmdbctl v0.0.0-20230726080357-558a20ba8524 github.com/jszwec/csvutil v1.4.0 github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4 diff --git a/go.sum b/go.sum index bbc4dd7e..5693569d 100644 --- a/go.sum +++ b/go.sum @@ -14,6 +14,8 @@ github.com/ipinfo/go/v2 v2.9.2 h1:wih7S6ifXAdGE7OH5fgTfC/yA/lFYRKaG5z4FNiE+MY= github.com/ipinfo/go/v2 v2.9.2/go.mod h1:tRDkYfM20b1XzNqorn1Q1O6Xtg7uzw3Wn3I2R0SyJh4= github.com/ipinfo/go/v2 v2.9.4 h1:6LcbIQ6o/bfNgqGMoJCfkUxVtJsbLWdB9sDq4Vv3fF0= github.com/ipinfo/go/v2 v2.9.4/go.mod h1:tRDkYfM20b1XzNqorn1Q1O6Xtg7uzw3Wn3I2R0SyJh4= +github.com/ipinfo/go/v2 v2.10.0 h1:v9sFjaxnVVD+JVgpWpjgwols18Tuu4SgBDaHHaw0IXo= +github.com/ipinfo/go/v2 v2.10.0/go.mod h1:tRDkYfM20b1XzNqorn1Q1O6Xtg7uzw3Wn3I2R0SyJh4= github.com/ipinfo/mmdbctl v0.0.0-20230726080357-558a20ba8524 h1:om+KMCwwLE1scrxr5YirYikcgl27Z2KbKQSpobt/2b0= github.com/ipinfo/mmdbctl v0.0.0-20230726080357-558a20ba8524/go.mod h1:RVULurqCcBRklgMU3st4j+sIEuVNY+mB1IMhhi/wjLA= github.com/jszwec/csvutil v1.4.0 h1:ro7gZN8PRsyNUEX8qE/eYPE5/kffEXMs+4eRcOd1oUk= diff --git a/ipinfo/cmd_myip.go b/ipinfo/cmd_myip.go index 3739a5e9..cc363096 100644 --- a/ipinfo/cmd_myip.go +++ b/ipinfo/cmd_myip.go @@ -26,6 +26,8 @@ var completionsMyIP = &complete.Command{ "--json": predict.Nothing, "-c": predict.Nothing, "--csv": predict.Nothing, + "-6": predict.Nothing, + "--ipv6": predict.Nothing, }, } @@ -37,6 +39,8 @@ Options: General: --token , -t use as API token. + --ipv6, -6 + get IPv6 address. --nocache do not use the cache. --help, -h @@ -69,6 +73,7 @@ func cmdMyIP() error { var fJSON bool var fCSV bool var fYAML bool + var fV6 bool pflag.StringVarP(&fTok, "token", "t", "", "the token to use.") pflag.BoolVar(&fNoCache, "nocache", true, "disable the cache.") @@ -79,6 +84,7 @@ func cmdMyIP() error { pflag.BoolVarP(&fCSV, "csv", "c", false, "output CSV format.") pflag.BoolVarP(&fYAML, "yaml", "y", false, "output YAML format.") pflag.BoolVar(&fNoColor, "nocolor", false, "disable color output.") + pflag.BoolVarP(&fV6, "ipv6", "6", false, "use IPv6 address.") pflag.Parse() if fNoColor { @@ -91,7 +97,14 @@ func cmdMyIP() error { } ii = prepareIpinfoClient(fTok) - data, err := ii.GetIPInfo(nil) + + var data *ipinfo.Core + var err error + if fV6 { + data, err = ii.GetIPInfoV6(nil) + } else { + data, err = ii.GetIPInfo(nil) + } if err != nil { return err } diff --git a/vendor/github.com/ipinfo/go/v2/ipinfo/client.go b/vendor/github.com/ipinfo/go/v2/ipinfo/client.go index dee551d6..a7a4644e 100644 --- a/vendor/github.com/ipinfo/go/v2/ipinfo/client.go +++ b/vendor/github.com/ipinfo/go/v2/ipinfo/client.go @@ -12,8 +12,9 @@ import ( ) const ( - defaultBaseURL = "https://ipinfo.io/" - defaultUserAgent = "IPinfoClient/Go/2.9.4" + defaultBaseURL = "https://ipinfo.io/" + defaultBaseURLIPv6 = "https://v6.ipinfo.io/" + defaultUserAgent = "IPinfoClient/Go/2.10.0" ) // A Client is the main handler to communicate with the IPinfo API. @@ -64,14 +65,35 @@ func NewClient( } } +// newRequest for IPV4 +func (c *Client) newRequest( + ctx context.Context, + method string, + urlStr string, + body io.Reader, +) (*http.Request, error) { + return c.newRequestBase(ctx, method, urlStr, body, false) +} + +// newRequest for IPV6 +func (c *Client) newRequestV6( + ctx context.Context, + method string, + urlStr string, + body io.Reader, +) (*http.Request, error) { + return c.newRequestBase(ctx, method, urlStr, body, true) +} + // `newRequest` creates an API request. A relative URL can be provided in // urlStr, in which case it is resolved relative to the BaseURL of the Client. // Relative URLs should always be specified without a preceding slash. -func (c *Client) newRequest( +func (c *Client) newRequestBase( ctx context.Context, method string, urlStr string, body io.Reader, + useIPv6 bool, ) (*http.Request, error) { if ctx == nil { ctx = context.Background() @@ -79,12 +101,17 @@ func (c *Client) newRequest( u := new(url.URL) + baseURL := c.BaseURL + if useIPv6 { + baseURL, _ = url.Parse(defaultBaseURLIPv6) + } + // get final URL path. if rel, err := url.Parse(urlStr); err == nil { - u = c.BaseURL.ResolveReference(rel) + u = baseURL.ResolveReference(rel) } else if strings.ContainsRune(urlStr, ':') { // IPv6 strings fail to parse as URLs, so let's add it as a URL Path. - *u = *c.BaseURL + *u = *baseURL u.Path += urlStr } else { return nil, err diff --git a/vendor/github.com/ipinfo/go/v2/ipinfo/core.go b/vendor/github.com/ipinfo/go/v2/ipinfo/core.go index a09cd3b0..8b54dcf1 100644 --- a/vendor/github.com/ipinfo/go/v2/ipinfo/core.go +++ b/vendor/github.com/ipinfo/go/v2/ipinfo/core.go @@ -2,6 +2,7 @@ package ipinfo import ( "net" + "net/http" "net/netip" ) @@ -107,8 +108,22 @@ func GetIPInfo(ip net.IP) (*Core, error) { return DefaultClient.GetIPInfo(ip) } +// GetIPInfoV6 returns the details for the specified IPv6 IP. +func GetIPInfoV6(ip net.IP) (*Core, error) { + return DefaultClient.GetIPInfoV6(ip) +} + // GetIPInfo returns the details for the specified IP. func (c *Client) GetIPInfo(ip net.IP) (*Core, error) { + return c.getIPInfoBase(ip, false) +} + +// GetIPInfoV6 returns the details for the specified IPv6 IP. +func (c *Client) GetIPInfoV6(ip net.IP) (*Core, error) { + return c.getIPInfoBase(ip, true) +} + +func (c *Client) getIPInfoBase(ip net.IP, ipv6 bool) (*Core, error) { relURL := "" if ip != nil && isBogon(netip.MustParseAddr(ip.String())) { bogonResponse := new(Core) @@ -128,7 +143,13 @@ func (c *Client) GetIPInfo(ip net.IP) (*Core, error) { } // prepare req - req, err := c.newRequest(nil, "GET", relURL, nil) + var err error + var req *http.Request + if ipv6 { + req, err = c.newRequestV6(nil, "GET", relURL, nil) + } else { + req, err = c.newRequest(nil, "GET", relURL, nil) + } if err != nil { return nil, err } diff --git a/vendor/modules.txt b/vendor/modules.txt index dad9696b..33c24e02 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -10,7 +10,7 @@ github.com/hashicorp/errwrap # github.com/hashicorp/go-multierror v1.1.1 ## explicit; go 1.13 github.com/hashicorp/go-multierror -# github.com/ipinfo/go/v2 v2.9.4 +# github.com/ipinfo/go/v2 v2.10.0 ## explicit; go 1.15 github.com/ipinfo/go/v2/ipinfo github.com/ipinfo/go/v2/ipinfo/cache