From 8f502f148344c3e79784d0e4f47e22066e0eda21 Mon Sep 17 00:00:00 2001 From: Caleb Lemoine <21261388+circa10a@users.noreply.github.com> Date: Sat, 13 Jul 2024 11:24:57 -0700 Subject: [PATCH] Draft: refactor cache to use an interface (#1) * refactor cache to use an interface * bump action versions Signed-off-by: circa10a * fix api response types Signed-off-by: circa10a --------- Signed-off-by: circa10a --- .github/workflows/release.yaml | 8 +- .github/workflows/tag.yaml | 2 +- .github/workflows/test.yaml | 10 +- README.md | 7 +- cache/cache.go | 11 ++ cache/memory.go | 45 +++++++++ cache/redis.go | 59 +++++++++++ geofence.go | 178 +++++---------------------------- geofence_test.go | 8 +- go.mod | 14 +-- go.sum | 101 +++++++++++++++---- types.go | 129 ++++++++++++++++++++++++ 12 files changed, 375 insertions(+), 197 deletions(-) create mode 100644 cache/cache.go create mode 100644 cache/memory.go create mode 100644 cache/redis.go create mode 100644 types.go diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index b5cbfca..5da8be5 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -14,16 +14,16 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Unshallow run: git fetch --prune --unshallow - name: Install Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v5 - name: Run GoReleaser - uses: goreleaser/goreleaser-action@v3 + uses: goreleaser/goreleaser-action@v6 with: version: latest - args: release --rm-dist + args: release --clean env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: GoReportCard diff --git a/.github/workflows/tag.yaml b/.github/workflows/tag.yaml index 2cf1958..b738a68 100644 --- a/.github/workflows/tag.yaml +++ b/.github/workflows/tag.yaml @@ -15,7 +15,7 @@ jobs: with: fetch-depth: '0' - name: Install Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v5 - name: Bump version and push tag uses: anothrNick/github-tag-action@1.61.0 id: tagging diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index e831fcc..c742303 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -10,16 +10,16 @@ jobs: runs-on: ubuntu-latest steps: - name: Install Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v5 - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Test run: make test golangci-lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Install Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v5 - name: golangci-lint - uses: golangci/golangci-lint-action@v3 + uses: golangci/golangci-lint-action@v6 diff --git a/README.md b/README.md index 19d7e4b..2807f71 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ By default, the library will use an in-memory cache that will be used to reduce ### Persistent -If you need a persistent cache to live outside of your application, [Redis](https://redis.io/) is supported by this library. To have the library cache address proximity using a Redis instance, simply provide a `redis.RedisOptions` struct to `geofence.Config.RedisOptions`. If `RedisOptions` is configured, the in-memory cache will not be used. +If you need a persistent cache to live outside of your application, [Redis](https://redis.io/) is supported by this library. To have the library cache address proximity using a Redis instance, simply provide a `RedisOptions` struct using the `cache` package to `geofence.Config.RedisOptions`. If `RedisOptions` is configured, the in-memory cache will not be used. > Note: Only Redis 7 is currently supported at the time of this writing. @@ -76,7 +76,7 @@ import ( "time" "github.com/circa10a/go-geofence" - "github.com/go-redis/redis/v9" + geofencecache "github.com/circa10a/go-geofence/cache" ) func main() { @@ -87,7 +87,7 @@ func main() { AllowPrivateIPAddresses: true, CacheTTL: 7 * (24 * time.Hour), // 1 week // Use Redis for caching - RedisOptions: &redis.Options{ + RedisOptions: &geofencecache.RedisOptions{ Addr: "localhost:6379", Password: "", // no password set DB: 0, // use default DB @@ -100,6 +100,7 @@ func main() { if err != nil { log.Fatal(err) } + // Address nearby: false fmt.Println("Address nearby: ", isAddressNearby) } diff --git a/cache/cache.go b/cache/cache.go new file mode 100644 index 0000000..ff36c17 --- /dev/null +++ b/cache/cache.go @@ -0,0 +1,11 @@ +package cache + +import ( + "context" +) + +// Cache is an interface for caching ip addresses +type Cache interface { + Get(context.Context, string) (bool, bool, error) + Set(context.Context, string, bool) error +} diff --git a/cache/memory.go b/cache/memory.go new file mode 100644 index 0000000..62d9c6a --- /dev/null +++ b/cache/memory.go @@ -0,0 +1,45 @@ +package cache + +import ( + "context" + "time" + + gocache "github.com/patrickmn/go-cache" +) + +const ( + deleteExpiredCacheItemsInternal = 10 * time.Minute +) + +// MemoryCache is used to store/fetch ip proximity from an in-memory cache. +type MemoryCache struct { + memoryClient *gocache.Cache + memoryOptions *MemoryOptions +} + +// MemoryOptions holds in-memory cache configuration parameters. +type MemoryOptions struct { + TTL time.Duration +} + +// NewRedisCache provides a new in-memory cache client. +func NewMemoryCache(memoryOptions *MemoryOptions) *MemoryCache { + return &MemoryCache{ + memoryClient: gocache.New(memoryOptions.TTL, deleteExpiredCacheItemsInternal), + memoryOptions: memoryOptions, + } +} + +// Get gets value from the in-memory cache. +func (m *MemoryCache) Get(ctx context.Context, key string) (bool, bool, error) { + if isIPAddressNear, found := m.memoryClient.Get(key); found { + return isIPAddressNear.(bool), found, nil + } + return false, false, nil +} + +// Set sets k/v in the in-memory cache. +func (m *MemoryCache) Set(ctx context.Context, key string, value bool) error { + m.memoryClient.Set(key, value, m.memoryOptions.TTL) + return nil +} diff --git a/cache/redis.go b/cache/redis.go new file mode 100644 index 0000000..e276e91 --- /dev/null +++ b/cache/redis.go @@ -0,0 +1,59 @@ +package cache + +import ( + "context" + "strconv" + "time" + + "github.com/redis/go-redis/v9" +) + +// RedisCache is used to store/fetch ip proximity from redis. +type RedisCache struct { + redisClient *redis.Client + redisOptions *RedisOptions +} + +// RedisOptions holds redis configuration parameters. +type RedisOptions struct { + Addr string + Password string + DB int + TTL time.Duration +} + +// NewRedisCache provides a new redis cache client. +func NewRedisCache(redisOpts *RedisOptions) *RedisCache { + return &RedisCache{ + redisClient: redis.NewClient(&redis.Options{ + Addr: redisOpts.Addr, + Password: redisOpts.Password, + DB: redisOpts.DB, + }), + redisOptions: redisOpts, + } +} + +// Get gets value from redis. +func (r *RedisCache) Get(ctx context.Context, key string) (bool, bool, error) { + val, err := r.redisClient.Get(ctx, key).Result() + if err != nil { + // If key is not in redis + if err == redis.Nil { + return false, false, nil + } + return false, false, err + } + isIPAddressNear, err := strconv.ParseBool(val) + if err != nil { + return false, false, err + } + + return isIPAddressNear, true, nil +} + +// Set sets k/v in redis. +func (r *RedisCache) Set(ctx context.Context, key string, value bool) error { + // Redis stores false as 0 for whatever reason, so we'll store as a string and parse it out + return r.redisClient.Set(ctx, key, strconv.FormatBool(value), r.redisOptions.TTL).Err() +} diff --git a/geofence.go b/geofence.go index 22618f8..d715a7f 100644 --- a/geofence.go +++ b/geofence.go @@ -1,27 +1,23 @@ package geofence import ( - "fmt" + "errors" "net" - "strconv" "time" "github.com/EpicStep/go-simple-geo/v2/geo" - "github.com/go-redis/redis/v9" + "github.com/circa10a/go-geofence/cache" "github.com/go-resty/resty/v2" - "github.com/patrickmn/go-cache" "golang.org/x/net/context" ) const ( ipBaseBaseURL = "https://api.ipbase.com/v2" - // For in-memory cache - deleteExpiredCacheItemsInternal = 10 * time.Minute ) // Config holds the user configuration to setup a new geofence type Config struct { - RedisOptions *redis.Options + RedisOptions *cache.RedisOptions IPAddress string Token string Radius float64 @@ -31,99 +27,19 @@ type Config struct { // Geofence holds a ipbase.com client, redis client, in-memory cache and user supplied config type Geofence struct { - cache *cache.Cache + cache cache.Cache ipbaseClient *resty.Client - redisClient *redis.Client ctx context.Context - Config - Latitude float64 - Longitude float64 + Config Config + Latitude float64 + Longitude float64 } -// ipBaseResponse is the json response from ipbase.com +// ipbaseResponse is the json response from ipbase.com type ipbaseResponse struct { Data data `json:"data"` } -type timezone struct { - Id string `json:"id"` - CurrentTime string `json:"current_time"` - Code string `json:"code"` - IDaylightSaving bool `json:"is_daylight_saving"` - GmtOffset int `json:"gmt_offset"` -} - -type connection struct { - Organization string `json:"organization"` - Isp string `json:"isp"` - Asn int `json:"asn"` -} - -type continent struct { - Code string `json:"code"` - Name string `json:"name"` - NameTranslated string `json:"name_translated"` -} - -type currencies struct { - Symbol string `json:"symbol"` - Name string `json:"name"` - SymbolNative string `json:"symbol_native"` - Code string `json:"code"` - NamePlural string `json:"name_plural"` - DecimalDigits int `json:"decimal_digits"` - Rounding int `json:"rounding"` -} - -type languages struct { - Name string `json:"name"` - NameNative string `json:"name_native"` -} -type country struct { - Alpha2 string `json:"alpha2"` - Alpha3 string `json:"alpha3"` - CallingCodes []string `json:"calling_codes"` - Currencies []currencies `json:"currencies"` - Emoji string `json:"emoji"` - Ioc string `json:"ioc"` - Languages []languages `json:"languages"` - Name string `json:"name"` - NameTranslated string `json:"name_translated"` - Timezones []string `json:"timezones"` - IsInEuropeanUnion bool `json:"is_in_european_union"` -} - -type city struct { - Name string `json:"name"` - NameTranslated string `json:"name_translated"` -} - -type region struct { - Fips interface{} `json:"fips"` - Alpha2 interface{} `json:"alpha2"` - Name string `json:"name"` - NameTranslated string `json:"name_translated"` -} - -type location struct { - GeonamesID interface{} `json:"geonames_id"` - Region region `json:"region"` - Continent continent `json:"continent"` - City city `json:"city"` - Zip string `json:"zip"` - Country country `json:"country"` - Latitude float64 `json:"latitude"` - Longitude float64 `json:"longitude"` -} - -type data struct { - Timezone timezone `json:"timezone"` - IP string `json:"ip"` - Type string `json:"type"` - Connection connection `json:"connection"` - Location location `json:"location"` -} - // IPBaseError is the json response when there is an error from ipbase.com type IPBaseError struct { Message string `json:"message"` @@ -134,10 +50,7 @@ func (e *IPBaseError) Error() string { } // ErrInvalidIPAddress is the error raised when an invalid IP address is provided -var ErrInvalidIPAddress = fmt.Errorf("invalid IP address provided") - -// ErrCacheNotConfigured is the error raised when the cache was not set up correctly -var ErrCacheNotConfigured = fmt.Errorf("cache no configured") +var ErrInvalidIPAddress = errors.New("invalid IP address provided") // validateIPAddress ensures valid ip address func validateIPAddress(ipAddress string) error { @@ -154,7 +67,7 @@ func (g *Geofence) getIPGeoData(ipAddress string) (*ipbaseResponse, error) { resp, err := g.ipbaseClient.R(). SetHeader("Accept", "application/json"). - SetQueryParam("apikey", g.Token). + SetQueryParam("apikey", g.Config.Token). SetQueryParam("ip", ipAddress). SetResult(response). SetError(ipbaseError). @@ -186,16 +99,22 @@ func New(c *Config) (*Geofence, error) { } // Set up redis client if options are provided - // Else we create a local in-memory cache + // else we create a local in-memory cache if c.RedisOptions != nil { - geofence.redisClient = redis.NewClient(c.RedisOptions) + c.RedisOptions.TTL = c.CacheTTL + if c.CacheTTL < 0 { + c.RedisOptions.TTL = 0 + } + geofence.cache = cache.NewRedisCache(c.RedisOptions) } else { - geofence.cache = cache.New(c.CacheTTL, deleteExpiredCacheItemsInternal) + geofence.cache = cache.NewMemoryCache(&cache.MemoryOptions{ + TTL: c.CacheTTL, + }) } // Get current location of specified IP address // If empty string, use public IP of device running this - // Or use location of the specified IP + // or use location of the specified IP ipAddressLookupDetails, err := geofence.getIPGeoData(c.IPAddress) if err != nil { return geofence, err @@ -216,7 +135,7 @@ func (g *Geofence) IsIPAddressNear(ipAddress string) (bool, error) { return false, err } - if g.AllowPrivateIPAddresses { + if g.Config.AllowPrivateIPAddresses { ip := net.ParseIP(ipAddress) if ip.IsPrivate() || ip.IsLoopback() { return true, nil @@ -224,10 +143,11 @@ func (g *Geofence) IsIPAddressNear(ipAddress string) (bool, error) { } // Check if ipaddress has been looked up before and is in cache - isIPAddressNear, found, err := g.cacheGet(ipAddress) + isIPAddressNear, found, err := g.cache.Get(g.ctx, ipAddress) if err != nil { return false, err } + if found { return isIPAddressNear, nil } @@ -247,60 +167,12 @@ func (g *Geofence) IsIPAddressNear(ipAddress string) (bool, error) { // Compare coordinates // Distance must be less than or equal to the configured radius to be near - isNear := distance <= g.Radius + isNear := distance <= g.Config.Radius - err = g.cacheSet(ipAddress, isNear) + err = g.cache.Set(g.ctx, ipAddress, isNear) if err != nil { return false, err } return isNear, nil } - -func (g *Geofence) cacheGet(ipAddress string) (bool, bool, error) { - // Use redis if configured - if g.redisClient != nil { - val, err := g.redisClient.Get(g.ctx, ipAddress).Result() - if err != nil { - // If key is not in redis - if err == redis.Nil { - return false, false, nil - } - return false, false, err - } - isIPAddressNear, err := strconv.ParseBool(val) - if err != nil { - return false, false, err - } - return isIPAddressNear, true, nil - } - - // Use in memory cache if configured - if g.cache != nil { - if isIPAddressNear, found := g.cache.Get(ipAddress); found { - return isIPAddressNear.(bool), found, nil - } else { - return false, false, nil - } - } - - return false, false, ErrCacheNotConfigured -} - -func (g *Geofence) cacheSet(ipAddress string, isNear bool) error { - // Use redis if configured - if g.redisClient != nil { - // Redis stores false as 0 for whatever reason, so we'll store as a string and parse out in cacheGet - err := g.redisClient.Set(g.ctx, ipAddress, strconv.FormatBool(isNear), g.Config.CacheTTL).Err() - if err != nil { - return err - } - } - - // Use in memory cache if configured - if g.cache != nil { - g.cache.Set(ipAddress, isNear, g.Config.CacheTTL) - } - - return nil -} diff --git a/geofence_test.go b/geofence_test.go index fb2c998..9c78bc0 100644 --- a/geofence_test.go +++ b/geofence_test.go @@ -70,7 +70,7 @@ func TestGeofenceNear(t *testing.T) { httpmock.ActivateNonDefault(geofence.ipbaseClient.GetClient()) defer httpmock.DeactivateAndReset() - // mock json rsponse + // mock json response response := &ipbaseResponse{ Data: data{ IP: fakeIPAddress, @@ -83,7 +83,7 @@ func TestGeofenceNear(t *testing.T) { }, }, Timezone: timezone{ - Id: "America/Chicago", + ID: "America/Chicago", }, }, } @@ -146,7 +146,7 @@ func TestGeofencePrivateIP(t *testing.T) { }, }, Timezone: timezone{ - Id: "America/Chicago", + ID: "America/Chicago", }, }, } @@ -208,7 +208,7 @@ func TestGeofenceNotNear(t *testing.T) { }, }, Timezone: timezone{ - Id: "America/Chicago", + ID: "America/Chicago", }, }, } diff --git a/go.mod b/go.mod index 7cc4587..7756c09 100644 --- a/go.mod +++ b/go.mod @@ -1,20 +1,20 @@ module github.com/circa10a/go-geofence -go 1.17 +go 1.18 require ( github.com/EpicStep/go-simple-geo/v2 v2.0.1 - github.com/go-redis/redis/v9 v9.0.0-beta.2 - github.com/go-resty/resty/v2 v2.7.0 + github.com/go-resty/resty/v2 v2.13.1 github.com/jarcoal/httpmock v1.0.8 github.com/patrickmn/go-cache v2.1.0+incompatible - github.com/stretchr/testify v1.7.0 - golang.org/x/net v0.10.0 + github.com/redis/go-redis/v9 v9.5.4 + github.com/stretchr/testify v1.8.1 + golang.org/x/net v0.27.0 ) require ( - github.com/cespare/xxhash/v2 v2.2.0 // indirect - github.com/davecgh/go-spew v1.1.0 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/pmezard/go-difflib v1.0.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 7975c57..dd1a1dd 100644 --- a/go.sum +++ b/go.sum @@ -1,42 +1,103 @@ github.com/EpicStep/go-simple-geo/v2 v2.0.1 h1:+suZRwgZVZCuH8NXNE/D+7EH0iY90dqx2eA3faQ2v7c= github.com/EpicStep/go-simple-geo/v2 v2.0.1/go.mod h1:ELLmk0tgdNH4BLiL+jrSg+X6nz3aMgZrTRnHPWsaXvQ= -github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= -github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= -github.com/go-redis/redis/v9 v9.0.0-beta.2 h1:ZSr84TsnQyKMAg8gnV+oawuQezeJR11/09THcWCQzr4= -github.com/go-redis/redis/v9 v9.0.0-beta.2/go.mod h1:Bldcd/M/bm9HbnNPi/LUtYBSD8ttcZYBMupwMXhdU0o= -github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY= -github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I= -github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= +github.com/go-resty/resty/v2 v2.13.1 h1:x+LHXBI2nMB1vqndymf26quycC4aggYJ7DECYbiz03g= +github.com/go-resty/resty/v2 v2.13.1/go.mod h1:GznXlLxkq6Nh4sU59rPmUw3VtgpO3aS96ORAI6Q7d+0= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/jarcoal/httpmock v1.0.8 h1:8kI16SoO6LQKgPE7PvQuV+YuD/inwHd7fOOe2zMbo4k= github.com/jarcoal/httpmock v1.0.8/go.mod h1:ATjnClrvW/3tijVmpL/va5Z3aAyGvqU3gCT8nX0Txik= -github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= -github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= -github.com/onsi/gomega v1.20.0 h1:8W0cWlwFkflGPLltQvLRB7ZVD5HuP6ng320w2IS245Q= github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/redis/go-redis/v9 v9.5.4 h1:vOFYDKKVgrI5u++QvnMT7DksSMYg7Aw/Np4vLJLKLwY= +github.com/redis/go-redis/v9 v9.5.4/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -golang.org/x/net v0.0.0-20211029224645-99673261e6eb/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= +golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= +golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= +golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= +golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= +golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= +golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY= +golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= +golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= +golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/types.go b/types.go new file mode 100644 index 0000000..5d03cd8 --- /dev/null +++ b/types.go @@ -0,0 +1,129 @@ +// Look. +// I know people hate types.go and want to keep the structs +// but damn these are some exaustive types and it flooded the primary package logic + +package geofence + +type rangeType struct { + Type string `json:"type"` + Description string `json:"description"` +} + +type connection struct { + Organization string `json:"organization"` + Isp string `json:"isp"` + Range string `json:"range"` + Asn int `json:"asn"` +} + +type continent struct { + Name string `json:"name"` + NameTranslated string `json:"name_translated"` + WikidataID string `json:"wikidata_id"` + Code int `json:"code"` + GeonamesID int `json:"geonames_id"` +} + +type currencies struct { + Symbol string `json:"symbol"` + Name string `json:"name"` + SymbolNative string `json:"symbol_native"` + Code string `json:"code"` + NamePlural string `json:"name_plural"` + DecimalDigits int `json:"decimal_digits"` + Rounding int `json:"rounding"` +} + +type languages struct { + Name string `json:"name"` + NameNative string `json:"name_native"` +} + +type country struct { + Fips string `json:"fips"` + Alpha3 string `json:"alpha3"` + WikidataID string `json:"wikidata_id"` + HascID string `json:"hasc_id"` + Emoji string `json:"emoji"` + Ioc string `json:"ioc"` + Alpha2 string `json:"alpha2"` + Name string `json:"name"` + NameTranslated string `json:"name_translated"` + Languages []languages `json:"languages"` + Timezones []string `json:"timezones"` + Currencies []currencies `json:"currencies"` + CallingCodes []string `json:"calling_codes"` + GeonamesID int `json:"geonames_id"` + IsInEuropeanUnion bool `json:"is_in_european_union"` +} + +type city struct { + Alpha2 any `json:"alpha2"` + HascID any `json:"hasc_id"` + Fips string `json:"fips"` + WikidataID string `json:"wikidata_id"` + Name string `json:"name"` + NameTranslated string `json:"name_translated"` + GeonamesID int `json:"geonames_id"` +} + +type region struct { + Fips string `json:"fips"` + Alpha2 string `json:"alpha2"` + HascID string `json:"hasc_id"` + WikidataID string `json:"wikidata_id"` + Name string `json:"name"` + NameTranslated string `json:"name_translated"` + GeonamesID int `json:"geonames_id"` +} + +type location struct { + Zip string `json:"zip"` + City city `json:"city"` + Region region `json:"region"` + Continent continent `json:"continent"` + Country country `json:"country"` + GeonamesID int `json:"geonames_id"` + Latitude float64 `json:"latitude"` + Longitude float64 `json:"longitude"` +} + +type timezone struct { + ID string `json:"id"` + CurrentTime string `json:"current_time"` + Code string `json:"code"` + IsDaylightSaving bool `json:"is_daylight_saving"` + GmtOffset int `json:"gmt_offset"` +} + +type security struct { + IsAnonymous bool `json:"is_anonymous"` + IsDatacenter bool `json:"is_datacenter"` + IsVpn bool `json:"is_vpn"` + IsBot bool `json:"is_bot"` + IsAbuser bool `json:"is_abuser"` + IsKnownAttacker bool `json:"is_known_attacker"` + IsProxy bool `json:"is_proxy"` + IsSpam bool `json:"is_spam"` + IsTor bool `json:"is_tor"` + IsIcloudRelay bool `json:"is_icloud_relay"` + ThreatScore int `json:"threat_score"` +} + +type domains struct { + Domains []string `json:"domains"` + Count int `json:"count"` +} + +type data struct { + RangeType rangeType `json:"range_type"` + IP string `json:"ip"` + Hostname string `json:"hostname"` + Type string `json:"type"` + Connection connection `json:"connection"` + Tlds []string `json:"tlds"` + Timezone timezone `json:"timezone"` + Domains domains `json:"domains"` + Location location `json:"location"` + Security security `json:"security"` +}