mirror of
https://github.com/tobychui/zoraxy.git
synced 2025-08-07 05:38:30 +02:00
Added external geoip db option
- Added support for loading geoip db from external file - Added -update_geoip flag for automatically update the geoip
This commit is contained in:
@@ -3,11 +3,14 @@ package geodb
|
||||
import (
|
||||
_ "embed"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"imuslab.com/zoraxy/mod/database"
|
||||
"imuslab.com/zoraxy/mod/info/logger"
|
||||
"imuslab.com/zoraxy/mod/netutils"
|
||||
"imuslab.com/zoraxy/mod/utils"
|
||||
)
|
||||
|
||||
//go:embed geoipv4.csv
|
||||
@@ -32,6 +35,7 @@ type Store struct {
|
||||
type StoreOptions struct {
|
||||
AllowSlowIpv4LookUp bool
|
||||
AllowSlowIpv6Lookup bool
|
||||
Logger *logger.Logger
|
||||
SlowLookupCacheClearInterval time.Duration //Clear slow lookup cache interval
|
||||
}
|
||||
|
||||
@@ -41,6 +45,23 @@ type CountryInfo struct {
|
||||
}
|
||||
|
||||
func NewGeoDb(sysdb *database.Database, option *StoreOptions) (*Store, error) {
|
||||
//Check if external geoDB data is available
|
||||
if utils.FileExists("./conf/geodb/geoipv4.csv") {
|
||||
externalV4Db, err := os.ReadFile("./conf/geodb/geoipv4.csv")
|
||||
if err == nil {
|
||||
option.Logger.PrintAndLog("GeoDB", "External GeoDB data found, using external IPv4 GeoIP data", nil)
|
||||
geoipv4 = externalV4Db
|
||||
}
|
||||
}
|
||||
|
||||
if utils.FileExists("./conf/geodb/geoipv6.csv") {
|
||||
externalV6Db, err := os.ReadFile("./conf/geodb/geoipv6.csv")
|
||||
if err == nil {
|
||||
option.Logger.PrintAndLog("GeoDB", "External GeoDB data found, using external IPv6 GeoIP data", nil)
|
||||
geoipv6 = externalV6Db
|
||||
}
|
||||
}
|
||||
|
||||
parsedGeoData, err := parseCSV(geoipv4)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"imuslab.com/zoraxy/mod/geodb"
|
||||
"imuslab.com/zoraxy/mod/info/logger"
|
||||
)
|
||||
|
||||
/*
|
||||
@@ -44,6 +45,7 @@ func TestResolveCountryCodeFromIP(t *testing.T) {
|
||||
store, err := geodb.NewGeoDb(nil, &geodb.StoreOptions{
|
||||
true,
|
||||
true,
|
||||
&logger.Logger{},
|
||||
0,
|
||||
})
|
||||
if err != nil {
|
||||
|
56
src/mod/geodb/updater.go
Normal file
56
src/mod/geodb/updater.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package geodb
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"imuslab.com/zoraxy/mod/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
ipv4UpdateSource = "https://cdn.jsdelivr.net/npm/@ip-location-db/geo-whois-asn-country/geo-whois-asn-country-ipv4.csv"
|
||||
ipv6UpdateSource = "https://cdn.jsdelivr.net/npm/@ip-location-db/geo-whois-asn-country/geo-whois-asn-country-ipv6.csv"
|
||||
)
|
||||
|
||||
// DownloadGeoDBUpdate download the latest geodb update
|
||||
func DownloadGeoDBUpdate(externalGeoDBStoragePath string) {
|
||||
//Create the storage path if not exist
|
||||
if !utils.FileExists(externalGeoDBStoragePath) {
|
||||
os.MkdirAll(externalGeoDBStoragePath, 0755)
|
||||
}
|
||||
|
||||
//Download the update
|
||||
log.Println("Downloading IPv4 database update...")
|
||||
err := downloadFile(ipv4UpdateSource, externalGeoDBStoragePath+"/geoipv4.csv")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("Downloading IPv6 database update...")
|
||||
err = downloadFile(ipv6UpdateSource, externalGeoDBStoragePath+"/geoipv6.csv")
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("GeoDB update stored at: " + externalGeoDBStoragePath)
|
||||
log.Println("Exiting...")
|
||||
}
|
||||
|
||||
// Utility functions
|
||||
func downloadFile(url string, savepath string) error {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
fileContent, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(savepath, fileContent, 0644)
|
||||
}
|
Reference in New Issue
Block a user