dip now get to latest version of geoip databases
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
Paul 2022-10-23 18:30:44 +02:00
parent 3e23f7e872
commit c09ead8ae0

View File

@ -1,8 +1,10 @@
package geoip
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
@ -10,14 +12,36 @@ import (
geoip2 "github.com/oschwald/geoip2-golang"
)
var RootURL = "https://git.paulbsd.com/paulbsd/GeoLite.mmdb/releases/download/%s/%s"
var Version = "2022.06.16"
type ApiRes struct {
Tag string `json:"tag_name"`
}
var dbs map[string]string = map[string]string{
"city": fmt.Sprintf(RootURL, Version, "GeoLite2-City.mmdb"),
"asn": fmt.Sprintf(RootURL, Version, "GeoLite2-ASN.mmdb")}
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
res, err := http.Get(APIUrl)
if err != nil {
log.Println(err)
}
body, err := io.ReadAll(res.Body)
if err != nil {
log.Println(err)
}
err = json.Unmarshal(body, &apiresults)
if err != nil {
log.Println(err)
}
return apiresults[0].Tag
}
func InitGeoIP() (citydb *geoip2.Reader, asndb *geoip2.Reader, err error) {
var version = GetLastVersion()
var dbs map[string]string = map[string]string{
"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"])
if err != nil {
return