From e0e9733267bcd477116b41bf16b72869875aca51 Mon Sep 17 00:00:00 2001 From: eriknordmark Date: Sun, 4 Mar 2018 21:05:05 -0800 Subject: [PATCH 1/9] Added WithOptions. --- ipinfo.go | 35 ++++++++++++++++++++++++++---- src/github.com/eriknordmark/ipinfo | 1 + 2 files changed, 32 insertions(+), 4 deletions(-) create mode 120000 src/github.com/eriknordmark/ipinfo diff --git a/ipinfo.go b/ipinfo.go index 99cc098..c313d0f 100644 --- a/ipinfo.go +++ b/ipinfo.go @@ -5,7 +5,9 @@ package ipinfo import ( "encoding/json" "fmt" + "net" "net/http" + "time" ) var ipinfoURI = "http://ipinfo.io" @@ -22,19 +24,44 @@ type IPInfo struct { Postal string `json:"postal"` } +type ipinfoOptions struct { + Timeout time.Duration + SourceIp net.IP +} + // MyIP provides information about the public IP address of the client. func MyIP() (*IPInfo, error) { - return getInfo(fmt.Sprintf("%s/json", ipinfoURI)) + return getInfo(fmt.Sprintf("%s/json", ipinfoURI), nil) } // ForeignIP provides information about the given IP address (IPv4 or IPv6) func ForeignIP(ip string) (*IPInfo, error) { - return getInfo(fmt.Sprintf("%s/%s/json", ipinfoURI, ip)) + return getInfo(fmt.Sprintf("%s/%s/json", ipinfoURI, ip), nil) +} + +// MyIP provides information about the public IP address of the client. +func MyIPWithOptions(opt ipinfoOptions) (*IPInfo, error) { + return getInfo(fmt.Sprintf("%s/json", ipinfoURI), &opt) } // Undercover code that makes the real call to the webservice -func getInfo(url string) (*IPInfo, error) { - response, err := http.Get(url) +func getInfo(url string, opt *ipinfoOptions) (*IPInfo, error) { + var localAddr net.IP + if opt != nil { + localAddr = opt.SourceIp + } + localTCPAddr := net.TCPAddr{IP: localAddr} + fmt.Printf("Connecting to %s using intf %s source %v\n", + url, localTCPAddr) + d := net.Dialer{LocalAddr: &localTCPAddr} + if opt != nil && opt.Timeout != 0 { + d.Timeout = opt.Timeout + } + transport := &http.Transport{ + Dial: d.Dial, + } + client := &http.Client{Transport: transport} + response, err := client.Get(url) if err != nil { return nil, err } diff --git a/src/github.com/eriknordmark/ipinfo b/src/github.com/eriknordmark/ipinfo new file mode 120000 index 0000000..a8a4f8c --- /dev/null +++ b/src/github.com/eriknordmark/ipinfo @@ -0,0 +1 @@ +../../.. \ No newline at end of file From 244fd253ca118483cc3109eb11bf3d6b5541195b Mon Sep 17 00:00:00 2001 From: eriknordmark Date: Sun, 4 Mar 2018 21:12:47 -0800 Subject: [PATCH 2/9] export --- ipinfo.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ipinfo.go b/ipinfo.go index c313d0f..9638090 100644 --- a/ipinfo.go +++ b/ipinfo.go @@ -24,7 +24,7 @@ type IPInfo struct { Postal string `json:"postal"` } -type ipinfoOptions struct { +type Options struct { Timeout time.Duration SourceIp net.IP } @@ -40,12 +40,12 @@ func ForeignIP(ip string) (*IPInfo, error) { } // MyIP provides information about the public IP address of the client. -func MyIPWithOptions(opt ipinfoOptions) (*IPInfo, error) { +func MyIPWithOptions(opt Options) (*IPInfo, error) { return getInfo(fmt.Sprintf("%s/json", ipinfoURI), &opt) } // Undercover code that makes the real call to the webservice -func getInfo(url string, opt *ipinfoOptions) (*IPInfo, error) { +func getInfo(url string, opt *Options) (*IPInfo, error) { var localAddr net.IP if opt != nil { localAddr = opt.SourceIp From 05072ecb852bc1aba5e02f82f1af13b89b3b378d Mon Sep 17 00:00:00 2001 From: eriknordmark Date: Sun, 4 Mar 2018 21:18:17 -0800 Subject: [PATCH 3/9] . --- ipinfo.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ipinfo.go b/ipinfo.go index 9638090..32e2897 100644 --- a/ipinfo.go +++ b/ipinfo.go @@ -51,8 +51,7 @@ func getInfo(url string, opt *Options) (*IPInfo, error) { localAddr = opt.SourceIp } localTCPAddr := net.TCPAddr{IP: localAddr} - fmt.Printf("Connecting to %s using intf %s source %v\n", - url, localTCPAddr) + // XXX fmt.Printf("Connecting to %s using source %v\n", url, localTCPAddr) d := net.Dialer{LocalAddr: &localTCPAddr} if opt != nil && opt.Timeout != 0 { d.Timeout = opt.Timeout From e0887f9e41f089cd7e1d0ce703c154a415a0071f Mon Sep 17 00:00:00 2001 From: eriknordmark Date: Tue, 6 Mar 2018 10:06:59 -0800 Subject: [PATCH 4/9] Cleanup --- ipinfo.go | 1 - src/github.com/eriknordmark/ipinfo | 1 - 2 files changed, 2 deletions(-) delete mode 120000 src/github.com/eriknordmark/ipinfo diff --git a/ipinfo.go b/ipinfo.go index 32e2897..03c7ee8 100644 --- a/ipinfo.go +++ b/ipinfo.go @@ -51,7 +51,6 @@ func getInfo(url string, opt *Options) (*IPInfo, error) { localAddr = opt.SourceIp } localTCPAddr := net.TCPAddr{IP: localAddr} - // XXX fmt.Printf("Connecting to %s using source %v\n", url, localTCPAddr) d := net.Dialer{LocalAddr: &localTCPAddr} if opt != nil && opt.Timeout != 0 { d.Timeout = opt.Timeout diff --git a/src/github.com/eriknordmark/ipinfo b/src/github.com/eriknordmark/ipinfo deleted file mode 120000 index a8a4f8c..0000000 --- a/src/github.com/eriknordmark/ipinfo +++ /dev/null @@ -1 +0,0 @@ -../../.. \ No newline at end of file From beff5cd6527619bdfec41ba304a02088b25e7009 Mon Sep 17 00:00:00 2001 From: eriknordmark Date: Thu, 3 May 2018 17:04:22 -0700 Subject: [PATCH 5/9] token and error return --- ipinfo.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/ipinfo.go b/ipinfo.go index 03c7ee8..4f0885f 100644 --- a/ipinfo.go +++ b/ipinfo.go @@ -4,6 +4,7 @@ package ipinfo import ( "encoding/json" + "errors" "fmt" "net" "net/http" @@ -27,11 +28,12 @@ type IPInfo struct { type Options struct { Timeout time.Duration SourceIp net.IP + Token string } // MyIP provides information about the public IP address of the client. func MyIP() (*IPInfo, error) { - return getInfo(fmt.Sprintf("%s/json", ipinfoURI), nil) + return getInfo(fmt.Sprintf("%s/json%s", ipinfoURI), nil) } // ForeignIP provides information about the given IP address (IPv4 or IPv6) @@ -49,6 +51,9 @@ func getInfo(url string, opt *Options) (*IPInfo, error) { var localAddr net.IP if opt != nil { localAddr = opt.SourceIp + if opt.Token != "" { + url += fmt.Sprintf("/?token=%s", opt.Token) + } } localTCPAddr := net.TCPAddr{IP: localAddr} d := net.Dialer{LocalAddr: &localTCPAddr} @@ -64,12 +69,18 @@ func getInfo(url string, opt *Options) (*IPInfo, error) { return nil, err } defer response.Body.Close() - - var ipinfo IPInfo - err = json.NewDecoder(response.Body).Decode(&ipinfo) - if err != nil { - return nil, err + switch response.StatusCode { + case http.StatusOK: + var ipinfo IPInfo + err = json.NewDecoder(response.Body).Decode(&ipinfo) + if err != nil { + return nil, err + } + return &ipinfo, nil + default: + errStr := fmt.Sprintf("%s got statuscode %d %s body <%v>", + url, response.StatusCode, + http.StatusText(response.StatusCode), response.Body) + return nil, errors.New(errStr) } - - return &ipinfo, nil } From ddb538ad9575d275460c28d68e0fc1ad09e3fd58 Mon Sep 17 00:00:00 2001 From: eriknordmark Date: Wed, 27 Jun 2018 07:13:58 -0700 Subject: [PATCH 6/9] typo causing ipinfo failures --- ipinfo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipinfo.go b/ipinfo.go index 4f0885f..8922ab9 100644 --- a/ipinfo.go +++ b/ipinfo.go @@ -33,7 +33,7 @@ type Options struct { // MyIP provides information about the public IP address of the client. func MyIP() (*IPInfo, error) { - return getInfo(fmt.Sprintf("%s/json%s", ipinfoURI), nil) + return getInfo(fmt.Sprintf("%s/json", ipinfoURI), nil) } // ForeignIP provides information about the given IP address (IPv4 or IPv6) From b148f0155466cd0944ae7057fc0acca0c7eb1229 Mon Sep 17 00:00:00 2001 From: eriknordmark Date: Fri, 17 Aug 2018 15:50:10 -0700 Subject: [PATCH 7/9] use proxy --- ipinfo.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ipinfo.go b/ipinfo.go index 8922ab9..a99be30 100644 --- a/ipinfo.go +++ b/ipinfo.go @@ -61,7 +61,8 @@ func getInfo(url string, opt *Options) (*IPInfo, error) { d.Timeout = opt.Timeout } transport := &http.Transport{ - Dial: d.Dial, + Dial: d.Dial, + Proxy: http.ProxyFromEnvironment, } client := &http.Client{Transport: transport} response, err := client.Get(url) From 092e757631849b45c020eba33c9d3427f900d019 Mon Sep 17 00:00:00 2001 From: GopiKrishna Kodali Date: Wed, 20 Feb 2019 13:31:47 +0530 Subject: [PATCH 8/9] Introduce timeout in HTTP Client object along with Dialer --- ipinfo.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ipinfo.go b/ipinfo.go index a99be30..d9f9633 100644 --- a/ipinfo.go +++ b/ipinfo.go @@ -65,6 +65,9 @@ func getInfo(url string, opt *Options) (*IPInfo, error) { Proxy: http.ProxyFromEnvironment, } client := &http.Client{Transport: transport} + if opt != nil && opt.Timeout != 0 { + client.Timeout = opt.Timeout + } response, err := client.Get(url) if err != nil { return nil, err From c5a3681d5d1873c55082eb37cd018088199c23cb Mon Sep 17 00:00:00 2001 From: Shahriyar Jalayeri Date: Fri, 28 Jul 2023 11:00:44 +0000 Subject: [PATCH 9/9] connect with https instead of http Signed-off-by: Shahriyar Jalayeri --- ipinfo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipinfo.go b/ipinfo.go index d9f9633..4c8d6e2 100644 --- a/ipinfo.go +++ b/ipinfo.go @@ -11,7 +11,7 @@ import ( "time" ) -var ipinfoURI = "http://ipinfo.io" +var ipinfoURI = "https://ipinfo.io" // IPInfo wraps json response type IPInfo struct {