-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipify.go
54 lines (40 loc) · 1.09 KB
/
ipify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package ipify
import (
"encoding/json"
"net/http"
"time"
)
const timeoutSecondsMin = 1
func bodyDispose(r *http.Response) {
if r != nil {
_ = r.Body.Close()
}
}
// PublicIP retrieves the current public ip from IPIFy https://www.ipify.org/
func PublicIP(timeoutSeconds int) (string, error) {
if timeoutSeconds < timeoutSecondsMin {
timeoutSeconds = timeoutSecondsMin
}
cli := &http.Client{Timeout: time.Duration(timeoutSeconds) * time.Second}
resp, err := cli.Get(`https://api.ipify.org?format=json`)
if err != nil {
return "", err
}
defer bodyDispose(resp)
var ipify struct {
IP string `json:"ip"`
}
if err0 := json.NewDecoder(resp.Body).Decode(&ipify); err0 != nil {
return "", err0
}
return ipify.IP, nil
}
// PublicIPString retrieves the current public ip from IPIFy https://www.ipify.org/
// The differences to PublicIP are:
// - PublicIPString ignores all errors and return an empty string if fails
// - PublicIPString defines arbitratry timeout value of 5 seconds
func PublicIPString() string {
const timeoutSeconds = 5
ip, _ := PublicIP(timeoutSeconds)
return ip
}