This commit is contained in:
parent
c09ead8ae0
commit
deb1c47ca4
@ -12,15 +12,13 @@ import (
|
||||
geoip2 "github.com/oschwald/geoip2-golang"
|
||||
)
|
||||
|
||||
type ApiRes struct {
|
||||
Tag string `json:"tag_name"`
|
||||
}
|
||||
|
||||
var RootURL = "https://git.paulbsd.com/paulbsd/GeoLite.mmdb/releases/download/%s/%s"
|
||||
var APIUrl = "https://git.paulbsd.com/api/v1/repos/paulbsd/GeoLite.mmdb/releases"
|
||||
|
||||
func GetLastVersion() string {
|
||||
var apiresults []ApiRes
|
||||
var apiresults []struct {
|
||||
Tag string `json:"tag_name"`
|
||||
}
|
||||
res, err := http.Get(APIUrl)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
@ -42,24 +40,24 @@ func InitGeoIP() (citydb *geoip2.Reader, asndb *geoip2.Reader, err error) {
|
||||
"city": fmt.Sprintf(RootURL, version, "GeoLite2-City.mmdb"),
|
||||
"asn": fmt.Sprintf(RootURL, version, "GeoLite2-ASN.mmdb")}
|
||||
log.Printf("Fetching GeoLite.mmdb version %s\n", version)
|
||||
citydb, err = FetchDB(dbs["city"])
|
||||
citydb, err = FetchDB(dbs["city"], version)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
asndb, err = FetchDB(dbs["asn"])
|
||||
asndb, err = FetchDB(dbs["asn"], version)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func FetchDB(db string) (*geoip2.Reader, error) {
|
||||
func FetchDB(db string, version string) (reader *geoip2.Reader, err error) {
|
||||
if _, err := os.Stat("geoip"); err != nil {
|
||||
os.MkdirAll("geoip", 0777)
|
||||
}
|
||||
|
||||
splited := strings.Split(db, "/")
|
||||
filename := splited[len(splited)-1]
|
||||
filename := strings.Replace(splited[len(splited)-1], ".", fmt.Sprintf("-%s.", version), 1)
|
||||
dbpath := fmt.Sprintf("%s/%s", "geoip", filename)
|
||||
|
||||
if _, err := os.Stat(dbpath); err != nil {
|
||||
@ -67,8 +65,15 @@ func FetchDB(db string) (*geoip2.Reader, error) {
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
f, _ := os.Create(dbpath)
|
||||
io.Copy(f, resp.Body)
|
||||
file, err := os.Create(dbpath)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
_, err = io.Copy(file, resp.Body)
|
||||
if err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
return geoip2.Open(dbpath)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user