mirror of
https://github.com/tobychui/zoraxy.git
synced 2025-06-06 07:37:21 +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:
parent
e72b2f9e09
commit
64b6769695
@ -43,7 +43,7 @@ const (
|
|||||||
/* Build Constants */
|
/* Build Constants */
|
||||||
SYSTEM_NAME = "Zoraxy"
|
SYSTEM_NAME = "Zoraxy"
|
||||||
SYSTEM_VERSION = "3.1.5"
|
SYSTEM_VERSION = "3.1.5"
|
||||||
DEVELOPMENT_BUILD = true /* Development: Set to false to use embedded web fs */
|
DEVELOPMENT_BUILD = false /* Development: Set to false to use embedded web fs */
|
||||||
|
|
||||||
/* System Constants */
|
/* System Constants */
|
||||||
DATABASE_PATH = "sys.db"
|
DATABASE_PATH = "sys.db"
|
||||||
@ -89,6 +89,9 @@ var (
|
|||||||
staticWebServerRoot = flag.String("webroot", "./www", "Static web server root folder. Only allow chnage in start paramters")
|
staticWebServerRoot = flag.String("webroot", "./www", "Static web server root folder. Only allow chnage in start paramters")
|
||||||
allowWebFileManager = flag.Bool("webfm", true, "Enable web file manager for static web server root folder")
|
allowWebFileManager = flag.Bool("webfm", true, "Enable web file manager for static web server root folder")
|
||||||
enableAutoUpdate = flag.Bool("cfgupgrade", true, "Enable auto config upgrade if breaking change is detected")
|
enableAutoUpdate = flag.Bool("cfgupgrade", true, "Enable auto config upgrade if breaking change is detected")
|
||||||
|
|
||||||
|
/* Maintaince Function Flags */
|
||||||
|
geoDbUpdate = flag.Bool("update_geoip", false, "Download the latest GeoIP data and exit")
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Global Variables and Handlers */
|
/* Global Variables and Handlers */
|
||||||
|
@ -42,6 +42,7 @@ import (
|
|||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/gorilla/csrf"
|
"github.com/gorilla/csrf"
|
||||||
|
"imuslab.com/zoraxy/mod/geodb"
|
||||||
"imuslab.com/zoraxy/mod/update"
|
"imuslab.com/zoraxy/mod/update"
|
||||||
"imuslab.com/zoraxy/mod/utils"
|
"imuslab.com/zoraxy/mod/utils"
|
||||||
)
|
)
|
||||||
@ -60,11 +61,18 @@ func SetupCloseHandler() {
|
|||||||
func main() {
|
func main() {
|
||||||
//Parse startup flags
|
//Parse startup flags
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
/* Maintaince Function Modes */
|
||||||
if *showver {
|
if *showver {
|
||||||
fmt.Println(SYSTEM_NAME + " - Version " + SYSTEM_VERSION)
|
fmt.Println(SYSTEM_NAME + " - Version " + SYSTEM_VERSION)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
if *geoDbUpdate {
|
||||||
|
geodb.DownloadGeoDBUpdate("./conf/geodb")
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Main Zoraxy Routines */
|
||||||
if !utils.ValidateListeningAddress(*webUIPort) {
|
if !utils.ValidateListeningAddress(*webUIPort) {
|
||||||
fmt.Println("Malformed -port (listening address) paramter. Do you mean -port=:" + *webUIPort + "?")
|
fmt.Println("Malformed -port (listening address) paramter. Do you mean -port=:" + *webUIPort + "?")
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
@ -3,11 +3,14 @@ package geodb
|
|||||||
import (
|
import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"imuslab.com/zoraxy/mod/database"
|
"imuslab.com/zoraxy/mod/database"
|
||||||
|
"imuslab.com/zoraxy/mod/info/logger"
|
||||||
"imuslab.com/zoraxy/mod/netutils"
|
"imuslab.com/zoraxy/mod/netutils"
|
||||||
|
"imuslab.com/zoraxy/mod/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed geoipv4.csv
|
//go:embed geoipv4.csv
|
||||||
@ -32,6 +35,7 @@ type Store struct {
|
|||||||
type StoreOptions struct {
|
type StoreOptions struct {
|
||||||
AllowSlowIpv4LookUp bool
|
AllowSlowIpv4LookUp bool
|
||||||
AllowSlowIpv6Lookup bool
|
AllowSlowIpv6Lookup bool
|
||||||
|
Logger *logger.Logger
|
||||||
SlowLookupCacheClearInterval time.Duration //Clear slow lookup cache interval
|
SlowLookupCacheClearInterval time.Duration //Clear slow lookup cache interval
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,6 +45,23 @@ type CountryInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewGeoDb(sysdb *database.Database, option *StoreOptions) (*Store, error) {
|
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)
|
parsedGeoData, err := parseCSV(geoipv4)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"imuslab.com/zoraxy/mod/geodb"
|
"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{
|
store, err := geodb.NewGeoDb(nil, &geodb.StoreOptions{
|
||||||
true,
|
true,
|
||||||
true,
|
true,
|
||||||
|
&logger.Logger{},
|
||||||
0,
|
0,
|
||||||
})
|
})
|
||||||
if err != nil {
|
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)
|
||||||
|
}
|
@ -114,6 +114,7 @@ func startupSequence() {
|
|||||||
geodbStore, err = geodb.NewGeoDb(sysdb, &geodb.StoreOptions{
|
geodbStore, err = geodb.NewGeoDb(sysdb, &geodb.StoreOptions{
|
||||||
AllowSlowIpv4LookUp: !*enableHighSpeedGeoIPLookup,
|
AllowSlowIpv4LookUp: !*enableHighSpeedGeoIPLookup,
|
||||||
AllowSlowIpv6Lookup: !*enableHighSpeedGeoIPLookup,
|
AllowSlowIpv6Lookup: !*enableHighSpeedGeoIPLookup,
|
||||||
|
Logger: SystemWideLogger,
|
||||||
SlowLookupCacheClearInterval: GEODB_CACHE_CLEAR_INTERVAL * time.Minute,
|
SlowLookupCacheClearInterval: GEODB_CACHE_CLEAR_INTERVAL * time.Minute,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user