forked from IncSW/geoip2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_domain.go
71 lines (66 loc) · 1.57 KB
/
reader_domain.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
package geoip2
import (
"errors"
"io/ioutil"
"net"
"strconv"
)
type DomainReader struct {
*reader
}
func (r *DomainReader) Lookup(ip net.IP) (string, error) {
offset, err := r.getOffset(ip)
if err != nil {
return "", err
}
dataType, size, offset, err := readControl(r.decoderBuffer, offset)
if err != nil {
return "", err
}
result := &Domain{}
switch dataType {
case dataTypeMap:
_, err = readDomainMap(result, r.decoderBuffer, size, offset)
if err != nil {
return "", err
}
case dataTypePointer:
pointer, _, err := readPointer(r.decoderBuffer, size, offset)
if err != nil {
return "", err
}
dataType, size, offset, err := readControl(r.decoderBuffer, pointer)
if err != nil {
return "", err
}
if dataType != dataTypeMap {
return "", errors.New("invalid Domain pointer type: " + strconv.Itoa(int(dataType)))
}
_, err = readDomainMap(result, r.decoderBuffer, size, offset)
if err != nil {
return "", err
}
default:
return "", errors.New("invalid Domain type: " + strconv.Itoa(int(dataType)))
}
return result.Domain, nil
}
func NewDomainReader(buffer []byte) (*DomainReader, error) {
reader, err := newReader(buffer)
if err != nil {
return nil, err
}
if reader.metadata.DatabaseType != "GeoIP2-Domain" {
return nil, errors.New("wrong MaxMind DB Domain type: " + reader.metadata.DatabaseType)
}
return &DomainReader{
reader: reader,
}, nil
}
func NewDomainReaderFromFile(filename string) (*DomainReader, error) {
buffer, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return NewDomainReader(buffer)
}