1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-28 08:06:24 -07:00

Add ip2location GeoIP plugin

This commit is contained in:
Eggbertx 2024-03-04 15:37:34 -08:00
parent 422235d42c
commit 0f36a300c4
3 changed files with 87 additions and 0 deletions

View file

@ -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 <a href="https://lite.ip2location.com">IP geolocation</a>.
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)
}

2
go.mod
View file

@ -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
)

4
go.sum
View file

@ -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=