https://github.com/circa10a/go-geofence
A small library to detect if an IP address is close to yours or another of your choosing using https://ipbase.com
https://github.com/circa10a/go-geofence
geofence geofence-api geofencing go golang golang-library golang-package
Last synced: 9 months ago
JSON representation
A small library to detect if an IP address is close to yours or another of your choosing using https://ipbase.com
- Host: GitHub
- URL: https://github.com/circa10a/go-geofence
- Owner: circa10a
- License: mit
- Created: 2021-11-08T14:57:16.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-07-21T01:26:37.000Z (almost 2 years ago)
- Last Synced: 2025-01-10T22:29:31.024Z (over 1 year ago)
- Topics: geofence, geofence-api, geofencing, go, golang, golang-library, golang-package
- Language: Go
- Homepage:
- Size: 39.1 KB
- Stars: 18
- Watchers: 2
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-geofence

[](https://pkg.go.dev/github.com/circa10a/go-geofence?tab=overview)
[](https://goreportcard.com/report/github.com/circa10a/go-geofence)
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](https://ipbase.com/)
```bash
go get github.com/circa10a/go-geofence
```
```go
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](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.
#### Example Redis Usage
```go
package main
import (
"fmt"
"log"
"time"
"github.com/circa10a/go-geofence"
geofencecache "github.com/circa10a/go-geofence/cache"
)
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: &geofencecache.RedisOptions{
Addr: "localhost:6379",
Username: "", // no username set
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)
}
```