-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipdb.go
117 lines (100 loc) · 2.69 KB
/
ipdb.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
package ipdb
import (
"encoding/binary"
"encoding/json"
"errors"
"io/ioutil"
"os"
"time"
)
// IPDb stores the whole IPDB file
type IPDb struct {
FileSize uint64
Metadata Metadata
Data []byte
startFrom uint32
}
// Metadata stores the metadata of the IPDB file
type Metadata struct {
Build int64 `json:"build"`
IPVersion uint16 `json:"ip_version"`
Languages map[string]int `json:"languages"`
NodeCount uint32 `json:"node_count"`
TotalSize uint64 `json:"total_size"`
Fields []string `json:"fields"`
}
var (
errDatabase = errors.New("database file may be corrupted")
errNoData = errors.New("no data")
errFileSize = errors.New("database file size too small")
errIPv4Only = errors.New("only IPv4 accepted")
errIPv6Only = errors.New("only IPv6 accepted")
)
// NewIPDb reads the IPDB file by providing filepath, returning an IPDb instance
// or error.
func NewIPDb(filepath string) (*IPDb, error) {
finfo, err := os.Stat(filepath)
if err != nil {
return nil, err
}
fsize := uint64(finfo.Size())
if fsize < 4 {
return nil, errFileSize
}
fbody, err := ioutil.ReadFile(filepath)
if err != nil {
return nil, err
}
metaLen := binary.BigEndian.Uint32(fbody[0:4])
if fsize < uint64(4+metaLen) {
return nil, errFileSize
}
var metadata Metadata
if err := json.Unmarshal(fbody[4:4+metaLen], &metadata); err != nil {
return nil, err
}
if fsize != uint64(4+metaLen)+metadata.TotalSize {
return nil, errFileSize
}
ipdb := &IPDb{
FileSize: fsize,
Metadata: metadata,
Data: fbody[4+metaLen:],
startFrom: 0,
}
// To simplify the search process, it omits the useless nodes in IPv4 database
if metadata.IPVersion == 1 {
offset := uint32(0)
for i := 0; i < 96 && offset < metadata.NodeCount; i++ {
if i >= 80 {
pos := offset*8 + 4
offset = binary.BigEndian.Uint32(ipdb.Data[pos : pos+4])
} else {
pos := offset * 8
offset = binary.BigEndian.Uint32(ipdb.Data[pos : pos+4])
}
}
ipdb.startFrom = offset
}
return ipdb, nil
}
// GetBuildTime converts build number in metadata into time.Time.
func (d *IPDb) GetBuildTime() time.Time {
return time.Unix(d.Metadata.Build, 0).In(time.UTC)
}
// GetLanguages returns the languages supported by the database.
func (d *IPDb) GetLanguages() []string {
languages := make([]string, 0, len(d.Metadata.Languages))
for k := range d.Metadata.Languages {
languages = append(languages, k)
}
return languages
}
// IsIPv4Db checks if the database an IPv4 database.
func (d *IPDb) IsIPv4Db() bool {
return d.Metadata.IPVersion == uint16(1)
}
// IsIPv6Db checks if the database an IPv6 database.
func (d *IPDb) IsIPv6Db() bool {
return d.Metadata.IPVersion == uint16(2)
}