From 0f36a300c4e50a0e931fc322ac4076311775391f Mon Sep 17 00:00:00 2001 From: Eggbertx Date: Mon, 4 Mar 2024 15:37:34 -0800 Subject: [PATCH] Add ip2location GeoIP plugin --- examples/plugins/ip2location/main.go | 81 ++++++++++++++++++++++++++++ go.mod | 2 + go.sum | 4 ++ 3 files changed, 87 insertions(+) create mode 100644 examples/plugins/ip2location/main.go diff --git a/examples/plugins/ip2location/main.go b/examples/plugins/ip2location/main.go new file mode 100644 index 00000000..777d95d8 --- /dev/null +++ b/examples/plugins/ip2location/main.go @@ -0,0 +1,81 @@ +package main + +import ( + "errors" + "fmt" + "net/http" + "strings" + + "github.com/gochan-org/gochan/pkg/config" + "github.com/gochan-org/gochan/pkg/gcutil" + "github.com/gochan-org/gochan/pkg/posting/geoip" + "github.com/ip2location/ip2location-go/v9" + "github.com/rs/zerolog" +) + +// [Your site name or product name] uses the IP2Location LITE database for IP geolocation. + +var ( + i2ldb = &ip2locationDB{} +) + +type ip2locationDB struct { + db *ip2location.DB +} + +// Close implements geoip.GeoIPHandler. +func (i *ip2locationDB) Close() error { + if i.db == nil { + return nil + } + i.db.Close() + return nil +} + +// GetCountry implements geoip.GeoIPHandler. +func (i *ip2locationDB) GetCountry(request *http.Request, board string, errEv *zerolog.Event) (*geoip.Country, error) { + var err error + if i.db == nil { + if err = i.Init(config.GetSiteConfig().GeoIPOptions); err != nil { + return nil, err + } + } + + country := &geoip.Country{} + ip := gcutil.GetRealIP(request) + + record, err := i.db.Get_country_long(ip) + if err != nil { + return nil, err + } + country.Name = record.Country_long + + if record, err = i.db.Get_country_short(ip); err != nil { + return nil, err + } + country.Flag = record.Country_short + return country, nil +} + +// Init implements geoip.GeoIPHandler. +func (i *ip2locationDB) Init(options map[string]any) (err error) { + for key, val := range options { + keyLower := strings.ToLower(key) + switch keyLower { + case "database": + fallthrough + case "dblocation": + dbLocation, ok := val.(string) + if !ok { + return fmt.Errorf("invalid %q value type %T, expected string", key, key) + } + i.db, err = ip2location.OpenDB(dbLocation) + return err + } + } + return errors.New("missing 'database' JSON key in gochan.json") +} + +func InitPlugin() error { + return geoip.RegisterGeoIPHandler("ip2location", i2ldb) +} diff --git a/go.mod b/go.mod index db0882bd..8a822345 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/disintegration/imaging v1.6.2 github.com/frustra/bbcode v0.0.0-20201127003707-6ef347fbe1c8 github.com/go-sql-driver/mysql v1.7.1 + github.com/ip2location/ip2location-go/v9 v9.7.0 github.com/lib/pq v1.10.9 github.com/mattn/go-sqlite3 v1.14.19 github.com/oschwald/maxminddb-golang v1.12.0 @@ -41,4 +42,5 @@ require ( golang.org/x/text v0.14.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + lukechampine.com/uint128 v1.2.0 // indirect ) diff --git a/go.sum b/go.sum index b50f3209..2f0f3d41 100755 --- a/go.sum +++ b/go.sum @@ -65,6 +65,8 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/ip2location/ip2location-go/v9 v9.7.0 h1:ipwl67HOWcrw+6GOChkEXcreRQR37NabqBd2ayYa4Q0= +github.com/ip2location/ip2location-go/v9 v9.7.0/go.mod h1:MPLnsKxwQlvd2lBNcQCsLoyzJLDBFizuO67wXXdzoyI= github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= @@ -238,3 +240,5 @@ layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf h1:rRz0YsF7VXj9fXRF6yQg layeh.com/gopher-json v0.0.0-20201124131017-552bb3c4c3bf/go.mod h1:ivKkcY8Zxw5ba0jldhZCYYQfGdb2K6u9tbYK1AwMIBc= layeh.com/gopher-luar v1.0.11 h1:8zJudpKI6HWkoh9eyyNFaTM79PY6CAPcIr6X/KTiliw= layeh.com/gopher-luar v1.0.11/go.mod h1:TPnIVCZ2RJBndm7ohXyaqfhzjlZ+OA2SZR/YwL8tECk= +lukechampine.com/uint128 v1.2.0 h1:mBi/5l91vocEN8otkC5bDLhi2KdCticRiwbdB0O+rjI= +lukechampine.com/uint128 v1.2.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=