/
base_station.go
117 lines (90 loc) · 2.05 KB
/
base_station.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 (
"reflect"
"time"
"os"
)
type BaseStationInfo struct {
CountryName string `json:"country_name"`
RegionName string `json:"region_name"`
CityName string `json:"city_name"`
OwnerDomain string `json:"owner_domain"`
IspDomain string `json:"isp_domain"`
BaseStation string `json:"base_station"`
}
type BaseStation struct {
reader *reader
}
func NewBaseStation(name string) (*BaseStation, error) {
r, e := newReader(name, &BaseStationInfo{})
if e != nil {
return nil, e
}
return &BaseStation{
reader: r,
}, nil
}
func (db *BaseStation) Reload(name string) error {
_, err := os.Stat(name)
if err != nil {
return err
}
reader, err := newReader(name, &BaseStationInfo{})
if err != nil {
return err
}
db.reader = reader
return nil
}
func (db *BaseStation) Find(addr, language string) ([]string, error) {
return db.reader.find1(addr, language)
}
func (db *BaseStation) FindMap(addr, language string) (map[string]string, error) {
data, err := db.reader.find1(addr, language)
if err != nil {
return nil, err
}
info := make(map[string]string, len(db.reader.meta.Fields))
for k, v := range data {
info[db.reader.meta.Fields[k]] = v
}
return info, nil
}
func (db *BaseStation) FindInfo(addr, language string) (*BaseStationInfo, error) {
data, err := db.reader.FindMap(addr, language)
if err != nil {
return nil, err
}
info := &BaseStationInfo{}
for k, v := range data {
sv := reflect.ValueOf(info).Elem()
sfv := sv.FieldByName(db.reader.refType[k])
if !sfv.IsValid() {
continue
}
if !sfv.CanSet() {
continue
}
sft := sfv.Type()
fv := reflect.ValueOf(v)
if sft == fv.Type() {
sfv.Set(fv)
}
}
return info, nil
}
func (db *BaseStation) IsIPv4() bool {
return db.reader.IsIPv4Support()
}
func (db *BaseStation) IsIPv6() bool {
return db.reader.IsIPv6Support()
}
func (db *BaseStation) Languages() []string {
return db.reader.Languages()
}
func (db *BaseStation) Fields() []string {
return db.reader.meta.Fields
}
func (db *BaseStation) BuildTime() time.Time {
return db.reader.Build()
}