-
Notifications
You must be signed in to change notification settings - Fork 1
/
getExternalIP.go
51 lines (42 loc) · 1.05 KB
/
getExternalIP.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
// Package getExternalIP getting IP of the service by myexternalip.com
package getExternalIP
import (
"encoding/json"
"fmt"
"net/http"
"os"
)
const getIPURL = "http://ipinfo.io/json"
// IPstruct ip address info structure
type IPstruct struct {
IP string `json:"ip"`
City string `json:"city"`
Region string `json:"region"`
Country string `json:"country"`
Loc string `json:"loc"`
Org string `json:"org"`
}
// GetIP getting external IP from some service
func GetIP() string {
resp, err := http.Get(getIPURL)
if err != nil {
fmt.Println("Error during get external IP info from:" + getIPURL)
os.Exit(1)
}
defer resp.Body.Close()
var newIP IPstruct
json.NewDecoder(resp.Body).Decode(&newIP)
return newIP.IP
}
// GetIPinfo getting external IP full info from some service
func GetIPinfo() IPstruct {
resp, err := http.Get(getIPURL)
if err != nil {
fmt.Println("Error during get external IP info from:" + getIPURL)
os.Exit(1)
}
defer resp.Body.Close()
var newIP IPstruct
json.NewDecoder(resp.Body).Decode(&newIP)
return newIP
}