geofence

package module
v0.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2023 License: MIT Imports: 9 Imported by: 1

README

go-geofence

GitHub tag (latest semver) PkgGoDev Go Report Card

A small library to detect if an IP address is close to yours or another of your choosing using https://ipbase.com/

Usage

First you will need a free API Token from ipbase.com

go get github.com/circa10a/go-geofence
package main

import (
	"fmt"
	"log"
	"time"

	"github.com/circa10a/go-geofence"
)

func main() {
	geofence, err := geofence.New(&geofence.Config{
		// Empty string to geofence your current public IP address, or you can monitor a remote address by supplying it as the first parameter
		IPAddress: "",
		// ipbase.com API token
		Token: "YOUR_IPBASE_API_TOKEN",
		// Maximum radius of the geofence in kilometers, only clients less than or equal to this distance will return true with IsIPAddressNear()
		// 1 kilometer
		Radius: 1.0,
		// Allow 192.X, 172.X, 10.X and loopback addresses
		AllowPrivateIPAddresses: true,
		// How long to cache if any ip address is nearby
		CacheTTL: 7 * (24 * time.Hour), // 1 week
	})
	if err != nil {
		log.Fatal(err)
	}
	isAddressNearby, err := geofence.IsIPAddressNear("8.8.8.8")
	if err != nil {
		log.Fatal(err)
	}
	// Address nearby: false
	fmt.Println("Address nearby: ", isAddressNearby)
}

Caching

To cache keys indefinitely, set CacheTTL: -1

Local (in-memory)

By default, the library will use an in-memory cache that will be used to reduce the number of calls to ipbase.com and increase performance. If no CacheTTL value is set (0), the in-memory cache is disabled.

Persistent

If you need a persistent cache to live outside of your application, Redis 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.

Note: Only Redis 7 is currently supported at the time of this writing.

Example Redis Usage
package main

import (
	"fmt"
	"log"
	"time"

	"github.com/circa10a/go-geofence"
	"github.com/go-redis/redis/v9"
)

func main() {
	geofence, err := geofence.New(&geofence.Config{
		IPAddress: "",
		Token: "YOUR_IPBASE_API_TOKEN",
		Radius: 1.0,
		AllowPrivateIPAddresses: true,
		CacheTTL: 7 * (24 * time.Hour), // 1 week
		// Use Redis for caching
		RedisOptions: &redis.Options{
			Addr:     "localhost:6379",
			Password: "", // no password set
			DB:       0,  // use default DB
		},
	})
	if err != nil {
		log.Fatal(err)
	}
	isAddressNearby, err := geofence.IsIPAddressNear("8.8.8.8")
	if err != nil {
		log.Fatal(err)
	}
	// Address nearby: false
	fmt.Println("Address nearby: ", isAddressNearby)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheNotConfigured = fmt.Errorf("cache no configured")

ErrCacheNotConfigured is the error raised when the cache was not set up correctly

View Source
var ErrInvalidIPAddress = fmt.Errorf("invalid IP address provided")

ErrInvalidIPAddress is the error raised when an invalid IP address is provided

Functions

This section is empty.

Types

type Config added in v0.3.0

type Config struct {
	RedisOptions            *redis.Options
	IPAddress               string
	Token                   string
	Radius                  float64
	CacheTTL                time.Duration
	AllowPrivateIPAddresses bool
}

Config holds the user configuration to setup a new geofence

type Geofence

type Geofence struct {
	Config
	Latitude  float64
	Longitude float64
	// contains filtered or unexported fields
}

Geofence holds a ipbase.com client, redis client, in-memory cache and user supplied config

func New

func New(c *Config) (*Geofence, error)

New creates a new geofence for the IP address specified. Use "" as the ip address to geofence the machine your application is running on Token comes from https://ipbase.com/

func (*Geofence) IsIPAddressNear added in v0.1.1

func (g *Geofence) IsIPAddressNear(ipAddress string) (bool, error)

IsIPAddressNear returns true if the specified address is within proximity

type IPBaseError added in v0.6.0

type IPBaseError struct {
	Message string `json:"message"`
}

IPBaseError is the json response when there is an error from ipbase.com

func (*IPBaseError) Error added in v0.6.0

func (e *IPBaseError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL