-
Notifications
You must be signed in to change notification settings - Fork 43
/
mmdb.go
182 lines (168 loc) · 5.17 KB
/
mmdb.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package yaklib
import (
"fmt"
"github.com/oschwald/maxminddb-golang"
"github.com/yaklang/yaklang/common/consts"
"github.com/yaklang/yaklang/common/geo"
"github.com/yaklang/yaklang/common/log"
"github.com/yaklang/yaklang/common/utils"
"github.com/yaklang/yaklang/common/utils/ziputil"
"io"
"net"
"net/http"
"os"
"path"
"path/filepath"
)
/*
(map[string]interface {}) (len=4) {
(string) (len=9) "GeoNameID": (float64) 1.814991e+06,
(string) (len=17) "IsInEuropeanUnion": (bool) false,
(string) (len=7) "IsoCode": (string) (len=2) "CN",
(string) (len=5) "Names": (map[string]interface {}) (len=8) {
(string) (len=5) "pt-BR": (string) (len=5) "China",
(string) (len=2) "ru": (string) (len=10) "Китай",
(string) (len=5) "zh-CN": (string) (len=6) "中国",
(string) (len=2) "de": (string) (len=5) "China",
(string) (len=2) "en": (string) (len=5) "China",
(string) (len=2) "es": (string) (len=5) "China",
(string) (len=2) "fr": (string) (len=5) "Chine",
(string) (len=2) "ja": (string) (len=6) "中国"
}
}
var CNCountry =
*/
var CNCountry = struct {
GeoNameID uint `maxminddb:"geoname_id"`
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
}(struct {
GeoNameID uint
IsInEuropeanUnion bool
IsoCode string
Names map[string]string
}{GeoNameID: 1814991, IsInEuropeanUnion: false, IsoCode: "CN", Names: map[string]string{
// (string) (len=5) "pt-BR": (string) (len=5) "China",
//(string) (len=2) "ru": (string) (len=10) "Китай",
//(string) (len=5) "zh-CN": (string) (len=6) "中国",
//(string) (len=2) "de": (string) (len=5) "China",
//(string) (len=2) "en": (string) (len=5) "China",
//(string) (len=2) "es": (string) (len=5) "China",
//(string) (len=2) "fr": (string) (len=5) "Chine",
//(string) (len=2) "ja": (string) (len=6) "中国"
"ru": "Китай",
"zh-CN": "中国",
"pt-BR": "China",
"en": "China",
"es": "China",
"de": "China",
"fr": "China",
"ja": "中国",
}})
var (
homeDir = utils.GetHomeDirDefault(".")
yakitHomeDir = consts.GetDefaultYakitBaseDir()
mmdbReader *maxminddb.Reader
mmdbISPReader *maxminddb.Reader
mmdbLocation = []string{
path.Join(yakitHomeDir, "GeoIP2-City.mmdb"),
path.Join(yakitHomeDir, "geoip_directory", "GeoIP2-City.mmdb"),
"GeoIP2-City.mmdb",
path.Join(homeDir, "GeoIP2-City.mmdb"),
path.Join(homeDir, "GeoIP2-City.mmdb"),
path.Join(homeDir, ".palm-desktop", "GeoIP2-City.mmdb"),
}
mmdbISPLocation = []string{
path.Join(yakitHomeDir, "GeoIP2-ISP.mmdb"),
path.Join(yakitHomeDir, "geoip_directory", "GeoIP2-ISP.mmdb"),
"GeoIP2-ISP.mmdb",
path.Join(homeDir, "GeoIP2-ISP.mmdb"),
path.Join(homeDir, ".palm-desktop", "GeoIP2-ISP.mmdb"),
}
)
var MmdbExports = map[string]interface{}{
"Open": maxminddb.Open,
"QueryIPCity": func(r *maxminddb.Reader, ip string) (*geo.City, error) {
var c geo.City
err := r.Lookup(net.ParseIP(utils.FixForParseIP(ip)), &c)
if err != nil {
return nil, utils.Errorf("loop up failed: %s", err)
}
if c.Country.IsoCode == "HK" || c.Country.IsoCode == "MO" {
c.City.Names = c.Country.Names
c.City.GeoNameID = c.Country.GeoNameID
c.Country = CNCountry
} else if c.Country.IsoCode == "TW" {
if c.City.GeoNameID > 0 {
var maps = make(map[string]string)
for k, v := range c.City.Names {
if c.Country.Names[k] != "" {
maps[k] = fmt.Sprintf("%v/%v", c.Country.Names[k], v)
}
}
} else {
c.City.Names = c.Country.Names
c.City.GeoNameID = c.Country.GeoNameID
}
c.Country = CNCountry
}
return &c, nil
},
}
func QueryIP(ip string) (*geo.City, error) {
var err error
if mmdbReader == nil {
mmdbReader, err = maxminddb.Open(utils.GetFirstExistedPath(mmdbLocation...))
if err != nil {
return nil, err
}
}
var c geo.City
err = mmdbReader.Lookup(net.ParseIP(utils.FixForParseIP(ip)), &c)
if err != nil {
return nil, err
}
return &c, nil
}
func QueryIPForISP(ip string) (*geo.ISP, error) {
var err error
if mmdbISPReader == nil {
mmdbISPReader, err = maxminddb.Open(utils.GetFirstExistedPath(mmdbISPLocation...))
if err != nil {
return nil, err
}
}
var isp geo.ISP
err = mmdbISPReader.Lookup(net.ParseIP(utils.FixForParseIP(ip)), &isp)
if err != nil {
return nil, err
}
return &isp, nil
}
func DownloadMMDB() error {
base := consts.GetDefaultYakitBaseDir()
geoipZip := filepath.Join(base, "geoip.zip")
_ = os.RemoveAll(geoipZip)
fp, err := os.OpenFile(geoipZip, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return utils.Errorf("open file failed: %s", err)
}
defer fp.Close()
log.Info("start to download geoip")
rsp, err := http.Get("https://www.yaklang.io/cloudflare/rule/geoip.zip")
if err != nil {
return utils.Errorf("download request to geoip failed: %s", err)
}
_, err = io.Copy(fp, rsp.Body)
if err != nil {
return utils.Errorf("download ... failed: %s", err)
}
geoipTarget := filepath.Join(base, "geoip_directory")
_ = os.RemoveAll(geoipTarget)
err = ziputil.DeCompress(geoipZip, geoipTarget)
if err != nil {
return utils.Errorf("unzip geoip failed: %s", err)
}
return nil
}