-
Notifications
You must be signed in to change notification settings - Fork 12
/
reader_country.go
88 lines (83 loc) · 2.17 KB
/
reader_country.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
package geoip2
import (
"errors"
"io/ioutil"
"net"
"strconv"
)
type CountryReader struct {
*reader
}
func (r *CountryReader) Lookup(ip net.IP) (*CountryResult, error) {
offset, err := r.getOffset(ip)
if err != nil {
return nil, err
}
dataType, size, offset, err := readControl(r.decoderBuffer, offset)
if err != nil {
return nil, err
}
if dataType != dataTypeMap {
return nil, errors.New("invalid Country type: " + strconv.Itoa(int(dataType)))
}
var key []byte
result := &CountryResult{}
for i := uint(0); i < size; i++ {
key, offset, err = readMapKey(r.decoderBuffer, offset)
if err != nil {
return nil, err
}
switch b2s(key) {
case "continent":
offset, err = readContinent(&result.Continent, r.decoderBuffer, offset)
if err != nil {
return nil, err
}
case "country":
offset, err = readCountry(&result.Country, r.decoderBuffer, offset)
if err != nil {
return nil, err
}
case "registered_country":
offset, err = readCountry(&result.RegisteredCountry, r.decoderBuffer, offset)
if err != nil {
return nil, err
}
case "represented_country":
offset, err = readCountry(&result.RepresentedCountry, r.decoderBuffer, offset)
if err != nil {
return nil, err
}
case "traits":
offset, err = readTraits(&result.Traits, r.decoderBuffer, offset)
if err != nil {
return nil, err
}
default:
return nil, errors.New("unknown Country response key: " + string(key) + ", type: " + strconv.Itoa(int(dataType)))
}
}
return result, nil
}
func NewCountryReader(buffer []byte) (*CountryReader, error) {
reader, err := newReader(buffer)
if err != nil {
return nil, err
}
if reader.metadata.DatabaseType != "GeoIP2-Country" &&
reader.metadata.DatabaseType != "GeoLite2-Country" &&
reader.metadata.DatabaseType != "DBIP-Country" &&
reader.metadata.DatabaseType != "DBIP-Country-Lite" {
return nil, errors.New("wrong MaxMind DB Country type: " + reader.metadata.DatabaseType)
}
return &CountryReader{
reader: reader,
}, nil
}
func NewCountryReaderFromFile(filename string) (*CountryReader, error) {
buffer, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return NewCountryReader(buffer)
}