updated dip
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Paul 2022-10-23 20:58:28 +02:00
parent c09ead8ae0
commit deb1c47ca4

View File

@ -12,15 +12,13 @@ import (
geoip2 "github.com/oschwald/geoip2-golang" 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 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" var APIUrl = "https://git.paulbsd.com/api/v1/repos/paulbsd/GeoLite.mmdb/releases"
func GetLastVersion() string { func GetLastVersion() string {
var apiresults []ApiRes var apiresults []struct {
Tag string `json:"tag_name"`
}
res, err := http.Get(APIUrl) res, err := http.Get(APIUrl)
if err != nil { if err != nil {
log.Println(err) 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"), "city": fmt.Sprintf(RootURL, version, "GeoLite2-City.mmdb"),
"asn": fmt.Sprintf(RootURL, version, "GeoLite2-ASN.mmdb")} "asn": fmt.Sprintf(RootURL, version, "GeoLite2-ASN.mmdb")}
log.Printf("Fetching GeoLite.mmdb version %s\n", version) log.Printf("Fetching GeoLite.mmdb version %s\n", version)
citydb, err = FetchDB(dbs["city"]) citydb, err = FetchDB(dbs["city"], version)
if err != nil { if err != nil {
return return
} }
asndb, err = FetchDB(dbs["asn"]) asndb, err = FetchDB(dbs["asn"], version)
if err != nil { if err != nil {
return return
} }
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 { if _, err := os.Stat("geoip"); err != nil {
os.MkdirAll("geoip", 0777) os.MkdirAll("geoip", 0777)
} }
splited := strings.Split(db, "/") 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) dbpath := fmt.Sprintf("%s/%s", "geoip", filename)
if _, err := os.Stat(dbpath); err != nil { if _, err := os.Stat(dbpath); err != nil {
@ -67,8 +65,15 @@ func FetchDB(db string) (*geoip2.Reader, error) {
if err != nil { if err != nil {
return nil, nil return nil, nil
} }
f, _ := os.Create(dbpath) file, err := os.Create(dbpath)
io.Copy(f, resp.Body) if err != nil {
return nil, nil
}
_, err = io.Copy(file, resp.Body)
if err != nil {
return nil, nil
}
} }
return geoip2.Open(dbpath) return geoip2.Open(dbpath)
} }