-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (121 loc) · 3.34 KB
/
main.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//Package goip provides API for GeoIP2 Precision web services
//http://maxmind.com
package goip
import (
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
//GoIP is a core object of this package
//You can use Connect function to get the instance
type GoIP struct {
httpAuth string
}
//ResponseNames contains names of object in different languages
type ResponseNames struct {
De string
En string
Es string
Fr string
Ja string
Ru string
Pt string `json:"pt-BR"`
Zh string `json:"zh-CN"`
}
//ResponseContinent contains information about a single continent
type ResponseContinent struct {
Names ResponseNames
Code string
GeonameID int `json:"geoname_id"`
}
//ResponseCountry contains information about a single country
type ResponseCountry struct {
Confidence int `json:"confidence"`
IsoCode string `json:"iso_code"`
GeonameID int `json:"geoname_id"`
Names ResponseNames
}
//ResponseInfo contains technical information about service
type ResponseInfo struct {
QueriesRemaining int `json:"queries_remaining"`
}
//Response contains all info from the GeoIp service
type Response struct {
Error string
Country ResponseCountry
RegisteredCountry ResponseCountry
Continent ResponseContinent
MaxMind ResponseInfo
}
//New takes name and license of GeoIp user
//It returns GoIP object that can be used for further information retrieving
func New(user string, pass string) *GoIP {
auth := base64.StdEncoding.EncodeToString([]byte(user + ":" + pass))
return &GoIP{auth}
}
//method sends request to the GeoIP service and
//returns country-level info about the IP address
func (m *GoIP) info(ip string) (*Response, error) {
response := Response{}
req, err := http.NewRequest("GET", "https://geoip.maxmind.com/geoip/v2.1/country/"+ip, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Accept-Charset", "UTF-8")
req.Header.Set("Authorization", "Basic "+m.httpAuth)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}
if response.Error != "" {
return nil, errors.New(response.Error)
}
return &response, nil
}
//Country method retrieves info about IP address
//and returns the Country structure with information about the related Country
func (m *GoIP) Country(ip string) (*ResponseCountry, error) {
info, err := m.info(ip)
if err != nil {
return nil, err
}
return &info.Country, err
}
//Continent method retrieves info about IP address
//and returns the Continent structure with information about the related continent
func (m *GoIP) Continent(ip string) (*ResponseContinent, error) {
info, err := m.info(ip)
if err != nil {
return nil, err
}
return &info.Continent, err
}
//ContinentName method returns English name of continent by IP
func (m *GoIP) ContinentName(ip string) (string, error) {
country, err := m.Continent(ip)
if err == nil {
return country.Names.En, nil
}
return "", err
}
//CountryName method returns English name of country by IP
func (m *GoIP) CountryName(ip string) (string, error) {
country, err := m.Country(ip)
if err == nil {
return country.Names.En, nil
}
return "", err
}